Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/graphics/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ struct PipelineInternal {
//layout: Vec<BufferLayout>,
//attributes: Vec<VertexAttributeInternal>,
_shader: ShaderId,
//params: PipelineParams,
params: PipelineParams,
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -408,7 +408,18 @@ impl RenderingBackend for MetalContext {
msg_send_![texture.texture, release];
}
}
fn apply_viewport(&mut self, _x: i32, _y: i32, _w: i32, _h: i32) {}
fn apply_viewport(&mut self, x: i32, y: i32, w: i32, h: i32) {
assert!(self.render_encoder.is_some());
let viewport = MTLViewport {
origin_x: x as f64,
origin_y: y as f64,
width: w as f64,
height: h as f64,
znear: 0.0,
zfar: 1.0,
};
unsafe { msg_send_![self.render_encoder.unwrap(), setViewport: viewport] };
}
fn apply_scissor_rect(&mut self, x: i32, y: i32, w: i32, h: i32) {
assert!(self.render_encoder.is_some());

Expand Down Expand Up @@ -1037,7 +1048,7 @@ impl RenderingBackend for MetalContext {
//layout: buffer_layout.to_vec(),
//attributes: vertex_layout,
_shader: shader,
//params,
params,
};

self.pipelines.push(pipeline);
Expand Down Expand Up @@ -1248,16 +1259,19 @@ impl RenderingBackend for MetalContext {
assert!(self.index_buffer.is_some());
let index_buffer = self.index_buffer.unwrap();

assert!(base_element == 0); // TODO: figure indexBufferOffset/baseVertex
assert!(self.current_pipeline.is_some(), "draw without pipeline");
let pip = &self.pipelines[self.current_pipeline.unwrap().0];
let primitive_type: MTLPrimitiveType = pip.params.primitive_type.into();

let index_type = MTLIndexType::UInt16;

unsafe {
msg_send_![render_encoder, drawIndexedPrimitives:MTLPrimitiveType::Triangle
msg_send_![render_encoder,drawIndexedPrimitives:primitive_type
indexCount:num_elements as u64
indexType:MTLIndexType::UInt16
indexType:index_type
indexBuffer:index_buffer
indexBufferOffset:0
indexBufferOffset: base_element as u64 * index_type.size() as u64
instanceCount:num_instances as u64
baseVertex:0
baseInstance:0
];
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/native/apple/frameworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,17 @@ pub enum MTLStoreAction {
CustomSampleDepthStore = 5,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct MTLViewport {
pub origin_x: f64,
pub origin_y: f64,
pub width: f64,
pub height: f64,
pub znear: f64,
pub zfar: f64,
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct MTLClearColor {
Expand Down Expand Up @@ -724,6 +735,14 @@ pub enum MTLIndexType {
UInt16 = 0,
UInt32 = 1,
}
impl MTLIndexType {
pub fn size(&self) -> usize {
match self {
MTLIndexType::UInt16 => size_of::<u16>(),
MTLIndexType::UInt32 => size_of::<u32>(),
}
}
}

#[repr(u64)]
pub enum MTLCompareFunction {
Expand Down
Loading