draw a square, handle resize

This commit is contained in:
Mitchell Hashimoto
2022-10-29 13:55:52 -07:00
parent 4d4c1790cb
commit a17a7426a3
2 changed files with 24 additions and 23 deletions

View File

@ -174,7 +174,7 @@ pub fn init(alloc: Allocator, font_group: *font.GroupCache) !Metal {
}; };
const func_vert = func_vert: { const func_vert = func_vert: {
const str = try macos.foundation.String.createWithBytes( const str = try macos.foundation.String.createWithBytes(
"demo_vertex", "basic_vertex",
.utf8, .utf8,
false, false,
); );
@ -336,6 +336,10 @@ pub fn render(
const pool = objc_autoreleasePoolPush(); const pool = objc_autoreleasePoolPush();
defer objc_autoreleasePoolPop(pool); defer objc_autoreleasePoolPop(pool);
// Ensure our layer size is always updated
const bounds = self.swapchain.getProperty(macos.graphics.Rect, "bounds");
self.swapchain.setProperty("drawableSize", bounds.size);
// Get our surface (CAMetalDrawable) // Get our surface (CAMetalDrawable)
const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{}); const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{});
@ -383,6 +387,9 @@ pub fn render(
); );
defer encoder.msgSend(void, objc.sel("endEncoding"), .{}); defer encoder.msgSend(void, objc.sel("endEncoding"), .{});
//do we need to do this?
//encoder.msgSend(void, objc.sel("setViewport:"), .{viewport});
// Use our shader pipeline // Use our shader pipeline
encoder.msgSend(void, objc.sel("setRenderPipelineState:"), .{self.pipeline.value}); encoder.msgSend(void, objc.sel("setRenderPipelineState:"), .{self.pipeline.value});
@ -394,29 +401,29 @@ pub fn render(
); );
// Draw // Draw
encoder.msgSend(
void,
objc.sel("drawPrimitives:vertexStart:vertexCount:instanceCount:"),
.{
@enumToInt(MTLPrimitiveType.triangle),
@as(c_ulong, 0),
@as(c_ulong, 3),
@as(c_ulong, 1),
},
);
// encoder.msgSend( // encoder.msgSend(
// void, // void,
// objc.sel("drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:"), // objc.sel("drawPrimitives:vertexStart:vertexCount:instanceCount:"),
// .{ // .{
// @enumToInt(MTLPrimitiveType.triangle), // @enumToInt(MTLPrimitiveType.triangle),
// @as(c_ulong, 6),
// @enumToInt(MTLIndexType.uint16),
// self.buf_instance.value,
// @as(c_ulong, 0), // @as(c_ulong, 0),
// @as(c_ulong, 3),
// @as(c_ulong, 1), // @as(c_ulong, 1),
// }, // },
// ); // );
encoder.msgSend(
void,
objc.sel("drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:"),
.{
@enumToInt(MTLPrimitiveType.triangle),
@as(c_ulong, 6),
@enumToInt(MTLIndexType.uint16),
self.buf_instance.value,
@as(c_ulong, 0),
@as(c_ulong, 1),
},
);
} }
buffer.msgSend(void, objc.sel("presentDrawable:"), .{surface.value}); buffer.msgSend(void, objc.sel("presentDrawable:"), .{surface.value});

View File

@ -26,17 +26,11 @@ vertex float4 basic_vertex(unsigned int vid [[ vertex_id ]]) {
// Calculate the final position of our cell in world space. // Calculate the final position of our cell in world space.
// We have to add our cell size since our vertices are offset // We have to add our cell size since our vertices are offset
// one cell up and to the left. (Do the math to verify yourself) // one cell up and to the left. (Do the math to verify yourself)
cell_pos = cell_size * position; cell_pos = cell_pos + cell_size * position;
return float4(cell_pos.x, cell_pos.y, 0.0f, 1.0f); return float4(cell_pos.x, cell_pos.y, 0.0f, 1.0f);
} }
vertex float4 demo_vertex(
const device packed_float3* vertex_array [[ buffer(0) ]],
unsigned int vid [[ vertex_id ]]) {
return float4(vertex_array[vid], 1.0);
}
fragment half4 basic_fragment() { fragment half4 basic_fragment() {
return half4(1.0, 0.0, 0.0, 1.0); return half4(1.0, 0.0, 0.0, 1.0);
} }