From 4d9b67fa65f2e8235f7206c332239271b93a7f17 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 15 Apr 2022 09:10:32 -0700 Subject: [PATCH] model direct gpu cell --- shaders/cell.v.glsl | 9 +-------- src/Grid.zig | 41 ++++++++++++++++++++++++++++------------- src/opengl/Buffer.zig | 16 ++++++++-------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/shaders/cell.v.glsl b/shaders/cell.v.glsl index aaa03d78e..e1fa65c68 100644 --- a/shaders/cell.v.glsl +++ b/shaders/cell.v.glsl @@ -12,13 +12,6 @@ flat out vec4 bg_color; uniform vec2 cell_size; uniform mat4 projection; -vec3 hsv2rgb(vec3 c) -{ - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} - void main() { // Top-left cell coordinates converted to world space vec2 cell_pos = cell_size * grid_coord; @@ -34,5 +27,5 @@ void main() { cell_pos = cell_pos + cell_size * position; gl_Position = projection * vec4(cell_pos, 1.0, 1.0); - bg_color = vec4(hsv2rgb(bg_color_in.rgb), 1.0); + bg_color = vec4(bg_color_in.rgb / 255.0, 1.0); } diff --git a/src/Grid.zig b/src/Grid.zig index d816231d2..fe15f85bc 100644 --- a/src/Grid.zig +++ b/src/Grid.zig @@ -26,6 +26,19 @@ vao: gl.VertexArray, ebo: gl.Buffer, vbo: gl.Buffer, +/// The raw structure that maps directly to the buffer sent to the vertex shader. +const GPUCell = struct { + /// vec2 grid_coord + grid_col: u16, + grid_row: u16, + + /// vec4 bg_color_in + bg_r: u8, + bg_g: u8, + bg_b: u8, + bg_a: u8, +}; + pub fn init(alloc: Allocator) !Grid { // Initialize our font atlas. We will initially populate the // font atlas with all the visible ASCII characters since they are common. @@ -98,8 +111,13 @@ pub fn init(alloc: Allocator) !Grid { var vbobind = try vbo.bind(.ArrayBuffer); defer vbobind.unbind(); //try vbobind.setDataNull(vertices.items, .StaticDraw); - try vbobind.attribute(0, 2, [6]f32, 0); - try vbobind.attribute(1, 4, [6]f32, 2); + + var offset: usize = 0; + try vbobind.attributeAdvanced(0, 2, gl.c.GL_UNSIGNED_SHORT, false, @sizeOf(GPUCell), offset); + offset += 2 * @sizeOf(u16); + try vbobind.attributeAdvanced(1, 4, gl.c.GL_UNSIGNED_BYTE, false, @sizeOf(GPUCell), offset); + try vbobind.enableAttribArray(0); + try vbobind.enableAttribArray(1); try vbobind.attributeDivisor(0, 1); try vbobind.attributeDivisor(1, 1); @@ -155,23 +173,20 @@ pub fn render(self: Grid) !void { defer gl.VertexArray.unbind() catch null; // Build our data - var vertices: std.ArrayListUnmanaged([6]f32) = .{}; + var vertices: std.ArrayListUnmanaged(GPUCell) = .{}; try vertices.ensureUnusedCapacity(self.alloc, self.size.columns * self.size.rows); defer vertices.deinit(self.alloc); var row: u32 = 0; while (row < self.size.rows) : (row += 1) { var col: u32 = 0; while (col < self.size.columns) : (col += 1) { - const rowf = @intToFloat(f32, row); - const colf = @intToFloat(f32, col); - const hue = ((colf * @intToFloat(f32, self.size.rows)) + rowf) / @intToFloat(f32, self.size.columns * self.size.rows); - vertices.appendAssumeCapacity([6]f32{ - colf, - rowf, - hue, - 0.7, - 0.8, - 1.0, + vertices.appendAssumeCapacity(.{ + .grid_col = @intCast(u16, col), + .grid_row = @intCast(u16, row), + .bg_r = 200, + .bg_g = 100, + .bg_b = 150, + .bg_a = 255, }); } } diff --git a/src/opengl/Buffer.zig b/src/opengl/Buffer.zig index d5fffac62..7fa3a41f9 100644 --- a/src/opengl/Buffer.zig +++ b/src/opengl/Buffer.zig @@ -134,18 +134,13 @@ pub const Binding = struct { else => @compileError("unsupported type"), }; - const offsetPtr = if (offset > 0) - @intToPtr(*const anyopaque, offset * info.offset) - else - null; - try b.attributeAdvanced( idx, size, info.typ, false, info.stride, - offsetPtr, + offset * info.offset, ); try b.enableAttribArray(idx); } @@ -163,10 +158,15 @@ pub const Binding = struct { typ: c.GLenum, normalized: bool, stride: c.GLsizei, - ptr: ?*const anyopaque, + offset: usize, ) !void { const normalized_c: c.GLboolean = if (normalized) c.GL_TRUE else c.GL_FALSE; - c.glVertexAttribPointer(idx, size, typ, normalized_c, stride, ptr); + const offsetPtr = if (offset > 0) + @intToPtr(*const anyopaque, offset) + else + null; + + c.glVertexAttribPointer(idx, size, typ, normalized_c, stride, offsetPtr); try errors.getError(); }