projection matrix, render cell

This commit is contained in:
Mitchell Hashimoto
2022-10-29 14:49:01 -07:00
parent a17a7426a3
commit ea0265f021
2 changed files with 42 additions and 8 deletions

View File

@ -9,6 +9,7 @@ const macos = @import("macos");
const font = @import("../font/main.zig"); const font = @import("../font/main.zig");
const terminal = @import("../terminal/main.zig"); const terminal = @import("../terminal/main.zig");
const renderer = @import("../renderer.zig"); const renderer = @import("../renderer.zig");
const math = @import("../math.zig");
const assert = std.debug.assert; const assert = std.debug.assert;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Terminal = terminal.Terminal; const Terminal = terminal.Terminal;
@ -52,6 +53,11 @@ const GPUCell = extern struct {
foo: f64, foo: f64,
}; };
const GPUUniforms = extern struct {
projection_matrix: math.Mat,
cell_size: [2]f32,
};
/// Returns the hints that we want for this /// Returns the hints that we want for this
pub fn windowHints() glfw.Window.Hints { pub fn windowHints() glfw.Window.Hints {
return .{ return .{
@ -340,6 +346,19 @@ pub fn render(
const bounds = self.swapchain.getProperty(macos.graphics.Rect, "bounds"); const bounds = self.swapchain.getProperty(macos.graphics.Rect, "bounds");
self.swapchain.setProperty("drawableSize", bounds.size); 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) // Get our surface (CAMetalDrawable)
const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{}); const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{});
@ -399,6 +418,15 @@ pub fn render(
objc.sel("setVertexBuffer:offset:atIndex:"), objc.sel("setVertexBuffer:offset:atIndex:"),
.{ self.buf_cells.value, @as(c_ulong, 0), @as(c_ulong, 0) }, .{ 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 // Draw
// encoder.msgSend( // encoder.msgSend(

View File

@ -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 // Where we are in the grid (x, y) where top-left is origin
float2 grid_coord = float2(0.0f, 0.0f); 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 // 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 // Turn the cell position into a vertex point depending on the
// vertex ID. Since we use instanced drawing, we have 4 vertices // 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. // 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_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() { fragment half4 basic_fragment() {