model direct gpu cell

This commit is contained in:
Mitchell Hashimoto
2022-04-15 09:10:32 -07:00
parent 128d28a7bd
commit 4d9b67fa65
3 changed files with 37 additions and 29 deletions

View File

@ -12,13 +12,6 @@ flat out vec4 bg_color;
uniform vec2 cell_size; uniform vec2 cell_size;
uniform mat4 projection; 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() { void main() {
// Top-left cell coordinates converted to world space // Top-left cell coordinates converted to world space
vec2 cell_pos = cell_size * grid_coord; vec2 cell_pos = cell_size * grid_coord;
@ -34,5 +27,5 @@ void main() {
cell_pos = cell_pos + cell_size * position; cell_pos = cell_pos + cell_size * position;
gl_Position = projection * vec4(cell_pos, 1.0, 1.0); 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);
} }

View File

@ -26,6 +26,19 @@ vao: gl.VertexArray,
ebo: gl.Buffer, ebo: gl.Buffer,
vbo: 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 { pub fn init(alloc: Allocator) !Grid {
// Initialize our font atlas. We will initially populate the // Initialize our font atlas. We will initially populate the
// font atlas with all the visible ASCII characters since they are common. // 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); var vbobind = try vbo.bind(.ArrayBuffer);
defer vbobind.unbind(); defer vbobind.unbind();
//try vbobind.setDataNull(vertices.items, .StaticDraw); //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(0, 1);
try vbobind.attributeDivisor(1, 1); try vbobind.attributeDivisor(1, 1);
@ -155,23 +173,20 @@ pub fn render(self: Grid) !void {
defer gl.VertexArray.unbind() catch null; defer gl.VertexArray.unbind() catch null;
// Build our data // 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); try vertices.ensureUnusedCapacity(self.alloc, self.size.columns * self.size.rows);
defer vertices.deinit(self.alloc); defer vertices.deinit(self.alloc);
var row: u32 = 0; var row: u32 = 0;
while (row < self.size.rows) : (row += 1) { while (row < self.size.rows) : (row += 1) {
var col: u32 = 0; var col: u32 = 0;
while (col < self.size.columns) : (col += 1) { while (col < self.size.columns) : (col += 1) {
const rowf = @intToFloat(f32, row); vertices.appendAssumeCapacity(.{
const colf = @intToFloat(f32, col); .grid_col = @intCast(u16, col),
const hue = ((colf * @intToFloat(f32, self.size.rows)) + rowf) / @intToFloat(f32, self.size.columns * self.size.rows); .grid_row = @intCast(u16, row),
vertices.appendAssumeCapacity([6]f32{ .bg_r = 200,
colf, .bg_g = 100,
rowf, .bg_b = 150,
hue, .bg_a = 255,
0.7,
0.8,
1.0,
}); });
} }
} }

View File

@ -134,18 +134,13 @@ pub const Binding = struct {
else => @compileError("unsupported type"), else => @compileError("unsupported type"),
}; };
const offsetPtr = if (offset > 0)
@intToPtr(*const anyopaque, offset * info.offset)
else
null;
try b.attributeAdvanced( try b.attributeAdvanced(
idx, idx,
size, size,
info.typ, info.typ,
false, false,
info.stride, info.stride,
offsetPtr, offset * info.offset,
); );
try b.enableAttribArray(idx); try b.enableAttribArray(idx);
} }
@ -163,10 +158,15 @@ pub const Binding = struct {
typ: c.GLenum, typ: c.GLenum,
normalized: bool, normalized: bool,
stride: c.GLsizei, stride: c.GLsizei,
ptr: ?*const anyopaque, offset: usize,
) !void { ) !void {
const normalized_c: c.GLboolean = if (normalized) c.GL_TRUE else c.GL_FALSE; 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(); try errors.getError();
} }