Define layouts for bind groups and pipeline

main
copygirl 8 months ago
parent da82690c8a
commit 8e9be881dd
  1. 30
      src/renderer.zig

@ -40,8 +40,34 @@ pub fn init(app: *App) !*Renderer {
const shader_module = core.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl")); const shader_module = core.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
defer shader_module.release(); defer shader_module.release();
// Define layouts for our bind groups and pipeline.
// This helps find errors with missing or mismatching shader properties.
var camera_bind_group_layout = core.device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{
gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, false, 0),
},
}));
defer camera_bind_group_layout.release();
var model_bind_group_layout = core.device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{
gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, false, 0),
gpu.BindGroupLayout.Entry.buffer(1, .{ .vertex = true }, .uniform, false, 0),
},
}));
defer model_bind_group_layout.release();
const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &.{
camera_bind_group_layout,
model_bind_group_layout,
},
}));
defer pipeline_layout.release();
// Set up rendering pipeline. // Set up rendering pipeline.
const pipeline = core.device.createRenderPipeline(&.{ const pipeline = core.device.createRenderPipeline(&.{
.layout = pipeline_layout,
.vertex = gpu.VertexState.init(.{ .vertex = gpu.VertexState.init(.{
.module = shader_module, .module = shader_module,
.entry_point = "vertex_main", .entry_point = "vertex_main",
@ -78,7 +104,7 @@ pub fn init(app: *App) !*Renderer {
// "Bind groups" are used to associate data from buffers with shader parameters. // "Bind groups" are used to associate data from buffers with shader parameters.
// So for example the `camera_bind_group` is accessible via `@group(0)` in our shader. // So for example the `camera_bind_group` is accessible via `@group(0)` in our shader.
const camera_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{ const camera_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = pipeline.getBindGroupLayout(0), .layout = camera_bind_group_layout,
.entries = &.{ .entries = &.{
gpu.BindGroup.Entry.buffer(0, view_proj_buffer, 0, @sizeOf(zm.Mat)), gpu.BindGroup.Entry.buffer(0, view_proj_buffer, 0, @sizeOf(zm.Mat)),
}, },
@ -135,7 +161,7 @@ pub fn init(app: *App) !*Renderer {
defer model_color_buffer.release(); defer model_color_buffer.release();
const model_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{ const model_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = pipeline.getBindGroupLayout(1), .layout = model_bind_group_layout,
.entries = &.{ .entries = &.{
gpu.BindGroup.Entry.buffer(0, model_matrix_buffer, 0, @sizeOf(zm.Mat)), gpu.BindGroup.Entry.buffer(0, model_matrix_buffer, 0, @sizeOf(zm.Mat)),
gpu.BindGroup.Entry.buffer(1, model_color_buffer, 0, @sizeOf([3]f32)), gpu.BindGroup.Entry.buffer(1, model_color_buffer, 0, @sizeOf([3]f32)),

Loading…
Cancel
Save