From ea0265f021ff14b816350d0412d795d47038590e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 29 Oct 2022 14:49:01 -0700 Subject: [PATCH] projection matrix, render cell --- src/renderer/Metal.zig | 28 ++++++++++++++++++++++++++++ src/shaders/cell.metal | 22 ++++++++++++++-------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index 7dc2c1373..80d4e3d02 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -9,6 +9,7 @@ const macos = @import("macos"); const font = @import("../font/main.zig"); const terminal = @import("../terminal/main.zig"); const renderer = @import("../renderer.zig"); +const math = @import("../math.zig"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; const Terminal = terminal.Terminal; @@ -52,6 +53,11 @@ const GPUCell = extern struct { foo: f64, }; +const GPUUniforms = extern struct { + projection_matrix: math.Mat, + cell_size: [2]f32, +}; + /// Returns the hints that we want for this pub fn windowHints() glfw.Window.Hints { return .{ @@ -340,6 +346,19 @@ pub fn render( const bounds = self.swapchain.getProperty(macos.graphics.Rect, "bounds"); self.swapchain.setProperty("drawableSize", bounds.size); + // Setup our uniforms + const uniforms: GPUUniforms = .{ + .projection_matrix = math.ortho2d( + 0, + @floatCast(f32, bounds.size.width), + @floatCast(f32, bounds.size.height), + 0, + ), + + // TODO: get content scale to scale these + .cell_size = .{ self.cell_size.width / 2, self.cell_size.height / 2 }, + }; + // Get our surface (CAMetalDrawable) const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{}); @@ -399,6 +418,15 @@ pub fn render( objc.sel("setVertexBuffer:offset:atIndex:"), .{ self.buf_cells.value, @as(c_ulong, 0), @as(c_ulong, 0) }, ); + encoder.msgSend( + void, + objc.sel("setVertexBytes:length:atIndex:"), + .{ + @ptrCast(*const anyopaque, &uniforms), + @as(c_ulong, @sizeOf(@TypeOf(uniforms))), + @as(c_ulong, 1), + }, + ); // Draw // encoder.msgSend( diff --git a/src/shaders/cell.metal b/src/shaders/cell.metal index 538018e04..53ffc995c 100644 --- a/src/shaders/cell.metal +++ b/src/shaders/cell.metal @@ -1,13 +1,19 @@ -vertex float4 basic_vertex(unsigned int vid [[ vertex_id ]]) { +using namespace metal; + +struct Uniforms { + float4x4 projection_matrix; + float2 cell_size; +}; + +vertex float4 basic_vertex( + unsigned int vid [[ vertex_id ]], + constant Uniforms &uniforms [[ buffer(1) ]] +) { // Where we are in the grid (x, y) where top-left is origin float2 grid_coord = float2(0.0f, 0.0f); - // The size of a single cell in pixels - //float2 cell_size = float2(75.0f, 100.0f); - float2 cell_size = float2(1.0f, 1.0f); - // Convert the grid x,y into world space x, y by accounting for cell size - float2 cell_pos = cell_size * grid_coord; + float2 cell_pos = uniforms.cell_size * grid_coord; // Turn the cell position into a vertex point depending on the // vertex ID. Since we use instanced drawing, we have 4 vertices @@ -26,9 +32,9 @@ 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_pos + cell_size * position; + cell_pos = cell_pos + uniforms.cell_size * position; - return float4(cell_pos.x, cell_pos.y, 0.0f, 1.0f); + return uniforms.projection_matrix * float4(cell_pos.x, cell_pos.y, 0.0f, 1.0f); } fragment half4 basic_fragment() {