From a17a7426a3eeab240da2894aaac406d86f1590fe Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 29 Oct 2022 13:55:52 -0700 Subject: [PATCH] draw a square, handle resize --- src/renderer/Metal.zig | 39 +++++++++++++++++++++++---------------- src/shaders/cell.metal | 8 +------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index 57e555863..7dc2c1373 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -174,7 +174,7 @@ pub fn init(alloc: Allocator, font_group: *font.GroupCache) !Metal { }; const func_vert = func_vert: { const str = try macos.foundation.String.createWithBytes( - "demo_vertex", + "basic_vertex", .utf8, false, ); @@ -336,6 +336,10 @@ pub fn render( const pool = objc_autoreleasePoolPush(); 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) const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{}); @@ -383,6 +387,9 @@ pub fn render( ); defer encoder.msgSend(void, objc.sel("endEncoding"), .{}); + //do we need to do this? + //encoder.msgSend(void, objc.sel("setViewport:"), .{viewport}); + // Use our shader pipeline encoder.msgSend(void, objc.sel("setRenderPipelineState:"), .{self.pipeline.value}); @@ -394,29 +401,29 @@ pub fn render( ); // 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( // void, - // objc.sel("drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:"), + // objc.sel("drawPrimitives:vertexStart:vertexCount:instanceCount:"), // .{ // @enumToInt(MTLPrimitiveType.triangle), - // @as(c_ulong, 6), - // @enumToInt(MTLIndexType.uint16), - // self.buf_instance.value, // @as(c_ulong, 0), + // @as(c_ulong, 3), // @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}); diff --git a/src/shaders/cell.metal b/src/shaders/cell.metal index 7a73cde0c..538018e04 100644 --- a/src/shaders/cell.metal +++ b/src/shaders/cell.metal @@ -26,17 +26,11 @@ vertex float4 basic_vertex(unsigned int vid [[ vertex_id ]]) { // Calculate the final position of our cell in world space. // 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) - cell_pos = cell_size * position; + cell_pos = cell_pos + cell_size * position; 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() { return half4(1.0, 0.0, 0.0, 1.0); }