render-zig

A 3D rendering engine written in Zig
git clone git://git.christianermann.dev/render-zig
Log | Files | Refs

commit 6e7f83c0ff4b16c3b156a031e09457108880fffe
parent 9bbfcb6ed996d596cd3e321fb01441585115dbdf
Author: Christian Ermann <christianermann@gmail.com>
Date:   Sun, 21 Jul 2024 15:26:39 -0400

Make line render pipeline buffers explicit

Diffstat:
Msrc/line_render_pipeline.zig | 27++++++++++++++++++---------
Msrc/main.zig | 14++++++++++++--
2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/src/line_render_pipeline.zig b/src/line_render_pipeline.zig @@ -9,17 +9,26 @@ const mat4 = @import("main.zig").mat4; const CubeMap = @import("cubemap.zig"); const Textures = @import("textures.zig"); const RenderPipeline = @import("render_pipeline.zig").RenderPipeline; +const Buffer = @import("main.zig").Buffer; +const InstanceData = @import("main.zig").InstanceData; const Self = @This(); +pub const Buffers = struct { + positions: *const Buffer(f32x4), + indices: *const Buffer(u32), + instances: *const Buffer(InstanceData), + indirect: *const Buffer([5]u32), +}; + gpu_pipeline: *gpu.RenderPipeline, -mesh_buffer: *const MeshBuffer, +buffers: Buffers, bind_group: *gpu.BindGroup, uniform_buffer: *gpu.Buffer, pub fn init( app: *App, - mesh_buffer: *const MeshBuffer, + buffers: Buffers, comptime shader_path: []const u8, ) !Self { const src = @embedFile(shader_path); @@ -57,7 +66,7 @@ pub fn init( const bind_group_descriptor = gpu.BindGroup.Descriptor.init(.{ .layout = bind_group_layout, .entries = &.{ - gpu.BindGroup.Entry.buffer(0, mesh_buffer.instances.data, 0, mesh_buffer.instances.size), + gpu.BindGroup.Entry.buffer(0, buffers.instances.data, 0, buffers.instances.size), gpu.BindGroup.Entry.buffer(1, uniform_buffer, 0, @sizeOf(UniformData)), }, }); @@ -89,7 +98,7 @@ pub fn init( return .{ .gpu_pipeline = app.device.createRenderPipeline(&pipeline_descriptor), - .mesh_buffer = mesh_buffer, + .buffers = buffers, .bind_group = bind_group, .uniform_buffer = uniform_buffer, }; @@ -126,14 +135,14 @@ pub fn frame(ptr: *anyopaque, pass: *gpu.RenderPassEncoder) void { const self: *Self = @ptrCast(@alignCast(ptr)); pass.setPipeline(self.gpu_pipeline); - pass.setVertexBuffer(0, self.mesh_buffer.positions.data, 0, self.mesh_buffer.positions.size); - pass.setIndexBuffer(self.mesh_buffer.indices.data, .uint32, 0, self.mesh_buffer.indices.size); + pass.setVertexBuffer(0, self.buffers.positions.data, 0, self.buffers.positions.size); + pass.setIndexBuffer(self.buffers.indices.data, .uint32, 0, self.buffers.indices.size); pass.setBindGroup(0, self.bind_group, null); - const num_indirect = self.mesh_buffer.lines_indirect.count(); + const num_indirect = self.buffers.indirect.count(); for (0..num_indirect) |idx| { - const offset = idx * self.mesh_buffer.lines_indirect.attrib_size; - pass.drawIndexedIndirect(self.mesh_buffer.lines_indirect.data, offset); + const offset = idx * self.buffers.indirect.attrib_size; + pass.drawIndexedIndirect(self.buffers.indirect.data, offset); } } diff --git a/src/main.zig b/src/main.zig @@ -288,7 +288,7 @@ pub const f32x3 = @Vector(3, f32); pub const f32x4 = @Vector(4, f32); pub const mat4 = [4]@Vector(4, f32); -const InstanceData = struct { +pub const InstanceData = struct { model_matrix: mat4, material: Material, }; @@ -722,7 +722,17 @@ pub fn main() !void { _ = try mesh_buffer.initLines(&corners, &indices); var rp = try MeshRenderPipeline.init(&app, &mesh_buffer, &textures, "shaders/mesh_clusters.wgsl"); - var lrp = try LineRenderPipeline.init(&app, &mesh_buffer, "shaders/lines.wgsl"); + + var lrp = try LineRenderPipeline.init( + &app, + .{ + .positions = &mesh_buffer.positions, + .indices = &mesh_buffer.indices, + .instances = &mesh_buffer.instances, + .indirect = &mesh_buffer.lines_indirect, + }, + "shaders/lines.wgsl", + ); const material = try Material.init( &textures,