examples: use default values, gpu helper APIs

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-08-17 01:42:27 -07:00
parent 7227e9a13f
commit edc3b5d60e
11 changed files with 251 additions and 630 deletions

View file

@ -124,18 +124,15 @@ pub fn update(app: *App, core: *mach.Core) !void {
.store_op = .store, .store_op = .store,
}; };
const render_pass_descriptor = gpu.RenderPassDescriptor{ const render_pass_descriptor = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
.depth_stencil_attachment = &.{ .depth_stencil_attachment = &.{
.view = app.depth.?.view, .view = app.depth.?.view,
.depth_load_op = .clear, .depth_load_op = .clear,
.depth_store_op = .store, .depth_store_op = .store,
.stencil_load_op = .undef,
.stencil_store_op = .undef,
.depth_clear_value = 1.0, .depth_clear_value = 1.0,
}, },
}; });
const pass = encoder.beginRenderPass(&render_pass_descriptor); const pass = encoder.beginRenderPass(&render_pass_descriptor);
defer pass.release(); defer pass.release();
@ -227,13 +224,12 @@ const Camera = struct {
.size = @sizeOf(@TypeOf(uniform)), .size = @sizeOf(@TypeOf(uniform)),
}; };
const bind_group = device.createBindGroup(&gpu.BindGroup.Descriptor{ const bind_group = device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = Self.bindGroupLayout(device), .layout = Self.bindGroupLayout(device),
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, buffer.buffer, 0, buffer.size), gpu.BindGroup.Entry.buffer(0, buffer.buffer, 0, buffer.size),
}, },
}); }));
self.buffer = buffer; self.buffer = buffer;
self.bind_group = bind_group; self.bind_group = bind_group;
@ -259,12 +255,11 @@ const Camera = struct {
inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout { inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout {
const visibility = .{ .vertex = true, .fragment = true }; const visibility = .{ .vertex = true, .fragment = true };
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor{ return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroupLayout.Entry{
gpu.BindGroupLayout.Entry.buffer(0, visibility, .uniform, false, 0), gpu.BindGroupLayout.Entry.buffer(0, visibility, .uniform, false, 0),
}, },
}); }));
} }
}; };
@ -332,75 +327,51 @@ const Cube = struct {
fn pipeline(core: *mach.Core) *gpu.RenderPipeline { fn pipeline(core: *mach.Core) *gpu.RenderPipeline {
const device = core.device; const device = core.device;
const layout_descriptor = gpu.PipelineLayout.Descriptor{ const layout_descriptor = gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layout_count = 3, .bind_group_layouts = &.{
.bind_group_layouts = &[_]*gpu.BindGroupLayout{
Camera.bindGroupLayout(device), Camera.bindGroupLayout(device),
Texture.bindGroupLayout(device), Texture.bindGroupLayout(device),
Light.bindGroupLayout(device), Light.bindGroupLayout(device),
}, },
}; });
const layout = device.createPipelineLayout(&layout_descriptor); const layout = device.createPipelineLayout(&layout_descriptor);
defer layout.release(); defer layout.release();
const shader = device.createShaderModule(&.{ const shader = device.createShaderModuleWGSL("cube.wgsl", @embedFile("cube.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("cube.wgsl"),
} },
});
defer shader.release(); defer shader.release();
const blend = gpu.BlendState{ const blend = gpu.BlendState{};
.color = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.write_mask = gpu.ColorWriteMaskFlags.all,
.blend = &blend, .blend = &blend,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = shader, .module = shader,
.entry_point = "fs_main", .entry_point = "fs_main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const descriptor = gpu.RenderPipeline.Descriptor{ const descriptor = gpu.RenderPipeline.Descriptor{
.layout = layout, .layout = layout,
.fragment = &fragment, .fragment = &fragment,
.vertex = .{ .vertex = gpu.VertexState.init(.{
.module = shader, .module = shader,
.entry_point = "vs_main", .entry_point = "vs_main",
.buffer_count = 2, .buffers = &.{
.buffers = &[_]gpu.VertexBufferLayout{
Self.vertexBufferLayout(), Self.vertexBufferLayout(),
Self.instanceLayout(), Self.instanceLayout(),
}, },
}, }),
.depth_stencil = &.{ .depth_stencil = &.{
.format = Texture.DEPTH_FORMAT, .format = Texture.DEPTH_FORMAT,
.depth_write_enabled = true, .depth_write_enabled = true,
.depth_compare = .less, .depth_compare = .less,
}, },
.primitive = .{ .primitive = .{
.front_face = .ccw,
.cull_mode = .back, .cull_mode = .back,
// .cull_mode = .none,
.topology = .triangle_strip, .topology = .triangle_strip,
.strip_index_format = .undef,
}, },
}; };
@ -470,12 +441,10 @@ const Cube = struct {
.shader_location = 2, .shader_location = 2,
}, },
}; };
return gpu.VertexBufferLayout{ return gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf([8]f32), .array_stride = @sizeOf([8]f32),
.step_mode = .vertex,
.attribute_count = attributes.len,
.attributes = &attributes, .attributes = &attributes,
}; });
} }
fn instanceLayout() gpu.VertexBufferLayout { fn instanceLayout() gpu.VertexBufferLayout {
@ -502,12 +471,11 @@ const Cube = struct {
}, },
}; };
return gpu.VertexBufferLayout{ return gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf([16]f32), .array_stride = @sizeOf([16]f32),
.step_mode = .instance, .step_mode = .instance,
.attribute_count = attributes.len,
.attributes = &attributes, .attributes = &attributes,
}; });
} }
}; };
@ -606,21 +574,15 @@ const Texture = struct {
const texture = device.createTexture(&gpu.Texture.Descriptor{ const texture = device.createTexture(&gpu.Texture.Descriptor{
.size = extent, .size = extent,
.mip_level_count = 1,
.sample_count = 1,
.dimension = .dimension_2d,
.format = FORMAT, .format = FORMAT,
.usage = .{ .copy_dst = true, .texture_binding = true }, .usage = .{ .copy_dst = true, .texture_binding = true },
}); });
const view = texture.createView(&gpu.TextureView.Descriptor{ const view = texture.createView(&gpu.TextureView.Descriptor{
.aspect = .all,
.format = FORMAT, .format = FORMAT,
.dimension = .dimension_2d, .dimension = .dimension_2d,
.base_array_layer = 0,
.array_layer_count = 1, .array_layer_count = 1,
.mip_level_count = 1, .mip_level_count = 1,
.base_mip_level = 0,
}); });
const sampler = device.createSampler(&gpu.Sampler.Descriptor{ const sampler = device.createSampler(&gpu.Sampler.Descriptor{
@ -630,9 +592,6 @@ const Texture = struct {
.mag_filter = .linear, .mag_filter = .linear,
.min_filter = .linear, .min_filter = .linear,
.mipmap_filter = .linear, .mipmap_filter = .linear,
.compare = .undef,
.lod_min_clamp = 0.0,
.lod_max_clamp = std.math.f32_max,
.max_anisotropy = 1, // 1,2,4,8,16 .max_anisotropy = 1, // 1,2,4,8,16
}); });
@ -649,14 +608,13 @@ const Texture = struct {
); );
const bind_group_layout = Self.bindGroupLayout(device); const bind_group_layout = Self.bindGroupLayout(device);
const bind_group = device.createBindGroup(&gpu.BindGroup.Descriptor{ const bind_group = device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = bind_group_layout, .layout = bind_group_layout,
.entry_count = 2, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.textureView(0, view), gpu.BindGroup.Entry.textureView(0, view),
gpu.BindGroup.Entry.sampler(1, sampler), gpu.BindGroup.Entry.sampler(1, sampler),
}, },
}); }));
return Self{ return Self{
.view = view, .view = view,
@ -674,9 +632,6 @@ const Texture = struct {
const texture = device.createTexture(&gpu.Texture.Descriptor{ const texture = device.createTexture(&gpu.Texture.Descriptor{
.size = extent, .size = extent,
.mip_level_count = 1,
.sample_count = 1,
.dimension = .dimension_2d,
.format = DEPTH_FORMAT, .format = DEPTH_FORMAT,
.usage = .{ .usage = .{
.render_attachment = true, .render_attachment = true,
@ -685,26 +640,14 @@ const Texture = struct {
}); });
const view = texture.createView(&gpu.TextureView.Descriptor{ const view = texture.createView(&gpu.TextureView.Descriptor{
.aspect = .all,
.format = .undef,
.dimension = .dimension_2d, .dimension = .dimension_2d,
.base_array_layer = 0,
.array_layer_count = 1, .array_layer_count = 1,
.mip_level_count = 1, .mip_level_count = 1,
.base_mip_level = 0,
}); });
const sampler = device.createSampler(&gpu.Sampler.Descriptor{ const sampler = device.createSampler(&gpu.Sampler.Descriptor{
.address_mode_u = .clamp_to_edge,
.address_mode_v = .clamp_to_edge,
.address_mode_w = .clamp_to_edge,
.mag_filter = .linear, .mag_filter = .linear,
.min_filter = .nearest,
.mipmap_filter = .nearest,
.compare = .less_equal, .compare = .less_equal,
.lod_min_clamp = 0.0,
.lod_max_clamp = std.math.f32_max,
.max_anisotropy = 1,
}); });
return Self{ return Self{
@ -718,13 +661,12 @@ const Texture = struct {
inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout { inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout {
const visibility = .{ .fragment = true }; const visibility = .{ .fragment = true };
const Entry = gpu.BindGroupLayout.Entry; const Entry = gpu.BindGroupLayout.Entry;
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor{ return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 2, .entries = &.{
.entries = &[_]Entry{
Entry.texture(0, visibility, .float, .dimension_2d, false), Entry.texture(0, visibility, .float, .dimension_2d, false),
Entry.sampler(1, visibility, .filtering), Entry.sampler(1, visibility, .filtering),
}, },
}); }));
} }
}; };
@ -753,13 +695,12 @@ const Light = struct {
.size = @sizeOf(@TypeOf(uniform)), .size = @sizeOf(@TypeOf(uniform)),
}; };
const bind_group = device.createBindGroup(&gpu.BindGroup.Descriptor{ const bind_group = device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = Self.bindGroupLayout(device), .layout = Self.bindGroupLayout(device),
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, buffer.buffer, 0, buffer.size), gpu.BindGroup.Entry.buffer(0, buffer.buffer, 0, buffer.size),
}, },
}); }));
return Self{ return Self{
.buffer = buffer, .buffer = buffer,
@ -782,84 +723,59 @@ const Light = struct {
inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout { inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout {
const visibility = .{ .vertex = true, .fragment = true }; const visibility = .{ .vertex = true, .fragment = true };
const Entry = gpu.BindGroupLayout.Entry; const Entry = gpu.BindGroupLayout.Entry;
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor{ return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 1, .entries = &.{
.entries = &[_]Entry{
Entry.buffer(0, visibility, .uniform, false, 0), Entry.buffer(0, visibility, .uniform, false, 0),
}, },
}); }));
} }
fn pipeline(core: *mach.Core) *gpu.RenderPipeline { fn pipeline(core: *mach.Core) *gpu.RenderPipeline {
const device = core.device; const device = core.device;
const layout_descriptor = gpu.PipelineLayout.Descriptor{ const layout_descriptor = gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layout_count = 2, .bind_group_layouts = &.{
.bind_group_layouts = &[_]*gpu.BindGroupLayout{
Camera.bindGroupLayout(device), Camera.bindGroupLayout(device),
Light.bindGroupLayout(device), Light.bindGroupLayout(device),
}, },
}; });
const layout = device.createPipelineLayout(&layout_descriptor); const layout = device.createPipelineLayout(&layout_descriptor);
defer layout.release(); defer layout.release();
const shader = core.device.createShaderModule(&.{ const shader = core.device.createShaderModuleWGSL("light.wgsl", @embedFile("light.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("light.wgsl"),
} },
});
defer shader.release(); defer shader.release();
const blend = gpu.BlendState{ const blend = gpu.BlendState{};
.color = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.write_mask = gpu.ColorWriteMaskFlags.all,
.blend = &blend, .blend = &blend,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = shader, .module = shader,
.entry_point = "fs_main", .entry_point = "fs_main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const descriptor = gpu.RenderPipeline.Descriptor{ const descriptor = gpu.RenderPipeline.Descriptor{
.layout = layout, .layout = layout,
.fragment = &fragment, .fragment = &fragment,
.vertex = .{ .vertex = gpu.VertexState.init(.{
.module = shader, .module = shader,
.entry_point = "vs_main", .entry_point = "vs_main",
.buffer_count = 1, .buffers = &.{
.buffers = &[_]gpu.VertexBufferLayout{
Cube.vertexBufferLayout(), Cube.vertexBufferLayout(),
}, },
}, }),
.depth_stencil = &.{ .depth_stencil = &.{
.format = Texture.DEPTH_FORMAT, .format = Texture.DEPTH_FORMAT,
.depth_write_enabled = true, .depth_write_enabled = true,
.depth_compare = .less, .depth_compare = .less,
}, },
.primitive = .{ .primitive = .{
.front_face = .ccw,
.cull_mode = .back, .cull_mode = .back,
// .cull_mode = .none,
.topology = .triangle_strip, .topology = .triangle_strip,
.strip_index_format = .undef,
}, },
}; };

View file

@ -27,19 +27,15 @@ var sim_params = [_]f32{
}; };
pub fn init(app: *App, core: *mach.Core) !void { pub fn init(app: *App, core: *mach.Core) !void {
const sprite_shader_module = core.device.createShaderModule(&.{ const sprite_shader_module = core.device.createShaderModuleWGSL(
.next_in_chain = .{ .wgsl_descriptor = &.{ "sprite.wgsl",
.source = @embedFile("sprite.wgsl"), @embedFile("sprite.wgsl"),
} }, );
.label = "sprite shader module",
});
const update_sprite_shader_module = core.device.createShaderModule(&.{ const update_sprite_shader_module = core.device.createShaderModuleWGSL(
.next_in_chain = .{ .wgsl_descriptor = &.{ "updateSprites.wgsl",
.source = @embedFile("updateSprites.wgsl"), @embedFile("updateSprites.wgsl"),
} }, );
.label = "update sprite shader module",
});
const instanced_particles_attributes = [_]gpu.VertexAttribute{ const instanced_particles_attributes = [_]gpu.VertexAttribute{
.{ .{
@ -66,35 +62,31 @@ pub fn init(app: *App, core: *mach.Core) !void {
}; };
const render_pipeline = core.device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{ const render_pipeline = core.device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
.vertex = .{ .vertex = gpu.VertexState.init(.{
.module = sprite_shader_module, .module = sprite_shader_module,
.entry_point = "vert_main", .entry_point = "vert_main",
.buffer_count = 2, .buffers = &.{
.buffers = &[_]gpu.VertexBufferLayout{ gpu.VertexBufferLayout.init(.{
.{
// instanced particles buffer // instanced particles buffer
.array_stride = 4 * 4, .array_stride = 4 * 4,
.step_mode = .instance, .step_mode = .instance,
.attribute_count = instanced_particles_attributes.len,
.attributes = &instanced_particles_attributes, .attributes = &instanced_particles_attributes,
}, }),
.{ gpu.VertexBufferLayout.init(.{
// vertex buffer // vertex buffer
.array_stride = 2 * 4, .array_stride = 2 * 4,
.step_mode = .vertex, .step_mode = .vertex,
.attribute_count = vertex_buffer_attributes.len,
.attributes = &vertex_buffer_attributes, .attributes = &vertex_buffer_attributes,
}),
}, },
}, }),
}, .fragment = &gpu.FragmentState.init(.{
.fragment = &gpu.FragmentState{
.module = sprite_shader_module, .module = sprite_shader_module,
.entry_point = "frag_main", .entry_point = "frag_main",
.target_count = 1,
.targets = &[_]gpu.ColorTargetState{.{ .targets = &[_]gpu.ColorTargetState{.{
.format = core.swap_chain_format, .format = core.swap_chain_format,
}}, }},
}, }),
}); });
const compute_pipeline = core.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ .compute = gpu.ProgrammableStageDescriptor{ const compute_pipeline = core.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ .compute = gpu.ProgrammableStageDescriptor{
@ -155,15 +147,14 @@ pub fn init(app: *App, core: *mach.Core) !void {
i = 0; i = 0;
while (i < 2) : (i += 1) { while (i < 2) : (i += 1) {
particle_bind_groups[i] = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ particle_bind_groups[i] = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = compute_pipeline.getBindGroupLayout(0), .layout = compute_pipeline.getBindGroupLayout(0),
.entry_count = 3, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, sim_param_buffer, 0, sim_params.len * @sizeOf(f32)), gpu.BindGroup.Entry.buffer(0, sim_param_buffer, 0, sim_params.len * @sizeOf(f32)),
gpu.BindGroup.Entry.buffer(1, particle_buffers[i], 0, initial_particle_data.len * @sizeOf(f32)), gpu.BindGroup.Entry.buffer(1, particle_buffers[i], 0, initial_particle_data.len * @sizeOf(f32)),
gpu.BindGroup.Entry.buffer(2, particle_buffers[(i + 1) % 2], 0, initial_particle_data.len * @sizeOf(f32)), gpu.BindGroup.Entry.buffer(2, particle_buffers[(i + 1) % 2], 0, initial_particle_data.len * @sizeOf(f32)),
}, },
}); }));
} }
app.compute_pipeline = compute_pipeline; app.compute_pipeline = compute_pipeline;
@ -181,18 +172,16 @@ pub fn update(app: *App, core: *mach.Core) !void {
const back_buffer_view = core.swap_chain.?.getCurrentTextureView(); const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color), .clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const render_pass_descriptor = gpu.RenderPassDescriptor{ const render_pass_descriptor = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{
.color_attachments = &[_]gpu.RenderPassColorAttachment{
color_attachment, color_attachment,
}, },
}; });
sim_params[0] = @floatCast(f32, core.delta_time); sim_params[0] = @floatCast(f32, core.delta_time);
core.device.getQueue().writeBuffer(app.sim_param_buffer, 0, sim_params[0..]); core.device.getQueue().writeBuffer(app.sim_param_buffer, 0, sim_params[0..]);

View file

@ -1,9 +1,9 @@
//! To get the effect we want, we need a texture on which to render //! To get the effect we want, we need a texture on which to render;
//! (we can't use the swapchain texture directly, but we can get the effect //! we can't use the swapchain texture directly, but we can get the effect
//! by doing the same render pass twice, on the texture and the swapchain. //! by doing the same render pass twice, on the texture and the swapchain.
//! We also need a second texture to use on the cube, that after the render pass //! We also need a second texture to use on the cube (after the render pass
//! needs to copy the other texture. We can't use the same texture since //! it needs to copy the other texture.) We can't use the same texture since
//! it would interfere with the sincronization on the gpu during the render pass. //! it would interfere with the synchronization on the gpu during the render pass.
//! This demo currently does not work on opengl, because core.current_desc.width/height, //! This demo currently does not work on opengl, because core.current_desc.width/height,
//! are set to 0 after core.init() and because webgpu does not implement copyTextureToTexture, //! are set to 0 after core.init() and because webgpu does not implement copyTextureToTexture,
//! for opengl //! for opengl
@ -45,71 +45,44 @@ pub fn init(app: *App, core: *mach.Core) !void {
.size_min = .{ .width = 20, .height = 20 }, .size_min = .{ .width = 20, .height = 20 },
}); });
const vs_module = core.device.createShaderModule(&.{ const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
});
const vertex_attributes = [_]gpu.VertexAttribute{ const vertex_attributes = [_]gpu.VertexAttribute{
.{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 }, .{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 },
.{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 }, .{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 },
}; };
const vertex_buffer_layout = gpu.VertexBufferLayout{ const vertex_buffer_layout = gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf(Vertex), .array_stride = @sizeOf(Vertex),
.step_mode = .vertex,
.attribute_count = vertex_attributes.len,
.attributes = &vertex_attributes, .attributes = &vertex_attributes,
};
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
}); });
const blend = gpu.BlendState{ const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
.color = .{
.operation = .add, const blend = gpu.BlendState{};
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.blend = &blend, .blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = fs_module, .module = fs_module,
.entry_point = "main", .entry_point = "main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const bgle_buffer = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0); const bgle_buffer = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgle_sampler = gpu.BindGroupLayout.Entry.sampler(1, .{ .fragment = true }, .filtering); const bgle_sampler = gpu.BindGroupLayout.Entry.sampler(1, .{ .fragment = true }, .filtering);
const bgle_textureview = gpu.BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false); const bgle_textureview = gpu.BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false);
const bgl = core.device.createBindGroupLayout( const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{ &gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 3, .entries = &.{ bgle_buffer, bgle_sampler, bgle_textureview },
.entries = &[_]gpu.BindGroupLayout.Entry{ bgle_buffer, bgle_sampler, bgle_textureview }, }),
},
); );
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl}; const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{ const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layout_count = 1,
.bind_group_layouts = &bind_group_layouts, .bind_group_layouts = &bind_group_layouts,
}); }));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
@ -119,22 +92,13 @@ pub fn init(app: *App, core: *mach.Core) !void {
.depth_write_enabled = true, .depth_write_enabled = true,
.depth_compare = .less, .depth_compare = .less,
}, },
.vertex = .{ .vertex = gpu.VertexState.init(.{
.module = vs_module, .module = vs_module,
.entry_point = "main", .entry_point = "main",
.buffer_count = 1, .buffers = &.{vertex_buffer_layout},
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout}, }),
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.primitive = .{ .primitive = .{
.front_face = .ccw,
.cull_mode = .back, .cull_mode = .back,
.topology = .triangle_list,
.strip_index_format = .undef,
}, },
}; };
@ -185,15 +149,14 @@ pub fn init(app: *App, core: *mach.Core) !void {
}); });
const bind_group = core.device.createBindGroup( const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = bgl, .layout = bgl,
.entry_count = 3, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)), gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)),
gpu.BindGroup.Entry.sampler(1, sampler), gpu.BindGroup.Entry.sampler(1, sampler),
gpu.BindGroup.Entry.textureView(2, cube_texture_view), gpu.BindGroup.Entry.textureView(2, cube_texture_view),
}, },
}, }),
); );
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor); app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
@ -245,14 +208,12 @@ pub fn update(app: *App, core: *mach.Core) !void {
const cube_color_attachment = gpu.RenderPassColorAttachment{ const cube_color_attachment = gpu.RenderPassColorAttachment{
.view = cube_view, .view = cube_view,
.resolve_target = null,
.clear_value = gpu.Color{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 1 }, .clear_value = gpu.Color{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 1 },
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = gpu.Color{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 1 }, .clear_value = gpu.Color{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 1 },
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
@ -263,21 +224,17 @@ pub fn update(app: *App, core: *mach.Core) !void {
.depth_load_op = .clear, .depth_load_op = .clear,
.depth_store_op = .store, .depth_store_op = .store,
.depth_clear_value = 1.0, .depth_clear_value = 1.0,
.stencil_load_op = .undef,
.stencil_store_op = .undef,
}; };
const encoder = core.device.createCommandEncoder(null); const encoder = core.device.createCommandEncoder(null);
const cube_render_pass_info = gpu.RenderPassDescriptor{ const cube_render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{cube_color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{cube_color_attachment},
.depth_stencil_attachment = &depth_stencil_attachment, .depth_stencil_attachment = &depth_stencil_attachment,
}; });
const render_pass_info = gpu.RenderPassDescriptor{ const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
.depth_stencil_attachment = &depth_stencil_attachment, .depth_stencil_attachment = &depth_stencil_attachment,
}; });
{ {
const time = timer.read(); const time = timer.read();
@ -381,15 +338,14 @@ pub fn resize(app: *App, core: *mach.Core, width: u32, height: u32) !void {
app.bind_group.release(); app.bind_group.release();
app.bind_group = core.device.createBindGroup( app.bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = app.bgl, .layout = app.bgl,
.entry_count = 3, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, app.uniform_buffer, 0, @sizeOf(UniformBufferObject)), gpu.BindGroup.Entry.buffer(0, app.uniform_buffer, 0, @sizeOf(UniformBufferObject)),
gpu.BindGroup.Entry.sampler(1, app.sampler), gpu.BindGroup.Entry.sampler(1, app.sampler),
gpu.BindGroup.Entry.textureView(2, app.cube_texture_view), gpu.BindGroup.Entry.textureView(2, app.cube_texture_view),
}, },
}, }),
); );
} else { } else {
app.depth_texture = core.device.createTexture(&gpu.Texture.Descriptor{ app.depth_texture = core.device.createTexture(&gpu.Texture.Descriptor{

View file

@ -128,19 +128,8 @@ pub fn init(app: *App, core: *mach.Core) !void {
// try draw.quad(app, .{ 0, 0 }, .{ 480, 480 }, .{}, .{ .bottom_left = .{ 0, 0 }, .width_and_height = .{ 1, 1 } }); // try draw.quad(app, .{ 0, 0 }, .{ 480, 480 }, .{}, .{ .bottom_left = .{ 0, 0 }, .width_and_height = .{ 1, 1 } });
// try draw.circle(app, .{ window_width / 2, window_height / 2 }, window_height / 2 - 10, .{ 0, 0.5, 0.75, 1.0 }, white_texture_uv_data); // try draw.circle(app, .{ window_width / 2, window_height / 2 }, window_height / 2 - 10, .{ 0, 0.5, 0.75, 1.0 }, white_texture_uv_data);
const vs_module = core.device.createShaderModule(&.{ const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{ const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
});
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
});
const blend = gpu.BlendState{ const blend = gpu.BlendState{
.color = .{ .color = .{
@ -160,51 +149,34 @@ pub fn init(app: *App, core: *mach.Core) !void {
.blend = &blend, .blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = fs_module, .module = fs_module,
.entry_point = "main", .entry_point = "main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const vbgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0); const vbgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const fbgle = gpu.BindGroupLayout.Entry.buffer(1, .{ .fragment = true }, .read_only_storage, true, 0); const fbgle = gpu.BindGroupLayout.Entry.buffer(1, .{ .fragment = true }, .read_only_storage, true, 0);
const sbgle = gpu.BindGroupLayout.Entry.sampler(2, .{ .fragment = true }, .filtering); const sbgle = gpu.BindGroupLayout.Entry.sampler(2, .{ .fragment = true }, .filtering);
const tbgle = gpu.BindGroupLayout.Entry.texture(3, .{ .fragment = true }, .float, .dimension_2d, false); const tbgle = gpu.BindGroupLayout.Entry.texture(3, .{ .fragment = true }, .float, .dimension_2d, false);
const bgl = core.device.createBindGroupLayout( const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{ &gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 4, .entries = &.{ vbgle, fbgle, sbgle, tbgle },
.entries = &[_]gpu.BindGroupLayout.Entry{ vbgle, fbgle, sbgle, tbgle }, }),
},
); );
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl}; const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{ const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layout_count = 1,
.bind_group_layouts = &bind_group_layouts, .bind_group_layouts = &bind_group_layouts,
}); }));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
.layout = pipeline_layout, .layout = pipeline_layout,
.depth_stencil = null, .vertex = gpu.VertexState.init(.{
.vertex = .{
.module = vs_module, .module = vs_module,
.entry_point = "main", .entry_point = "main",
.buffer_count = 1, .buffers = &.{draw.VERTEX_BUFFER_LAYOUT},
.buffers = &[_]gpu.VertexBufferLayout{draw.VERTEX_BUFFER_LAYOUT}, }),
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.primitive = .{
.front_face = .ccw,
.cull_mode = .none,
.topology = .triangle_list,
.strip_index_format = .undef,
},
}; };
const vertex_buffer = core.device.createBuffer(&.{ const vertex_buffer = core.device.createBuffer(&.{
@ -231,16 +203,15 @@ pub fn init(app: *App, core: *mach.Core) !void {
}); });
const bind_group = core.device.createBindGroup( const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = bgl, .layout = bgl,
.entry_count = 4, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, vertex_uniform_buffer, 0, @sizeOf(draw.VertexUniform)), gpu.BindGroup.Entry.buffer(0, vertex_uniform_buffer, 0, @sizeOf(draw.VertexUniform)),
gpu.BindGroup.Entry.buffer(1, frag_uniform_buffer, 0, @sizeOf(draw.FragUniform) * app.vertices.items.len / 3), gpu.BindGroup.Entry.buffer(1, frag_uniform_buffer, 0, @sizeOf(draw.FragUniform) * app.vertices.items.len / 3),
gpu.BindGroup.Entry.sampler(2, sampler), gpu.BindGroup.Entry.sampler(2, sampler),
gpu.BindGroup.Entry.textureView(3, texture.createView(&gpu.TextureView.Descriptor{ .dimension = .dimension_2d })), gpu.BindGroup.Entry.textureView(3, texture.createView(&gpu.TextureView.Descriptor{ .dimension = .dimension_2d })),
}, },
}, }),
); );
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor); app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
@ -283,17 +254,15 @@ pub fn update(app: *App, core: *mach.Core) !void {
const back_buffer_view = core.swap_chain.?.getCurrentTextureView(); const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color), .clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const encoder = core.device.createCommandEncoder(null); const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{ const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment}, });
};
{ {
if (app.update_vertex_buffer) { if (app.update_vertex_buffer) {

View file

@ -34,12 +34,7 @@ pub fn init(app: *App, core: *mach.Core) !void {
.size_min = .{ .width = 20, .height = 20 }, .size_min = .{ .width = 20, .height = 20 },
}); });
const blur_shader_module = core.device.createShaderModule(&.{ const blur_shader_module = core.device.createShaderModuleWGSL("blur.wgsl", @embedFile("blur.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("blur.wgsl"),
} },
.label = "blur shader module",
});
const blur_pipeline_descriptor = gpu.ComputePipeline.Descriptor{ const blur_pipeline_descriptor = gpu.ComputePipeline.Descriptor{
.compute = gpu.ProgrammableStageDescriptor{ .compute = gpu.ProgrammableStageDescriptor{
@ -50,54 +45,34 @@ pub fn init(app: *App, core: *mach.Core) !void {
const blur_pipeline = core.device.createComputePipeline(&blur_pipeline_descriptor); const blur_pipeline = core.device.createComputePipeline(&blur_pipeline_descriptor);
const fullscreen_quad_vs_module = core.device.createShaderModule(&.{ const fullscreen_quad_vs_module = core.device.createShaderModuleWGSL(
.next_in_chain = .{ .wgsl_descriptor = &.{ "fullscreen_textured_quad.wgsl",
.source = @embedFile("fullscreen_textured_quad.wgsl"), @embedFile("fullscreen_textured_quad.wgsl"),
} }, );
.label = "fullscreen quad vertex shader",
});
const fullscreen_quad_fs_module = core.device.createShaderModule(&.{ const fullscreen_quad_fs_module = core.device.createShaderModuleWGSL(
.next_in_chain = .{ .wgsl_descriptor = &.{ "fullscreen_textured_quad.wgsl",
.source = @embedFile("fullscreen_textured_quad.wgsl"), @embedFile("fullscreen_textured_quad.wgsl"),
} }, );
.label = "fullscreen quad fragment shader",
});
const blend = gpu.BlendState{
.color = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const blend = gpu.BlendState{};
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.blend = &blend, .blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment_state = gpu.FragmentState{ const fragment_state = gpu.FragmentState.init(.{
.module = fullscreen_quad_fs_module, .module = fullscreen_quad_fs_module,
.entry_point = "frag_main", .entry_point = "frag_main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const fullscreen_quad_pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const fullscreen_quad_pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.layout = null,
.fragment = &fragment_state, .fragment = &fragment_state,
.vertex = .{ .vertex = .{
.module = fullscreen_quad_vs_module, .module = fullscreen_quad_vs_module,
.entry_point = "vert_main", .entry_point = "vert_main",
.buffers = null,
}, },
}; };
@ -173,53 +148,48 @@ pub fn init(app: *App, core: *mach.Core) !void {
.usage = .{ .copy_dst = true, .uniform = true }, .usage = .{ .copy_dst = true, .uniform = true },
}); });
const compute_constants = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ const compute_constants = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = blur_pipeline.getBindGroupLayout(0), .layout = blur_pipeline.getBindGroupLayout(0),
.entry_count = 2, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.sampler(0, sampler), gpu.BindGroup.Entry.sampler(0, sampler),
gpu.BindGroup.Entry.buffer(1, blur_params_buffer, 0, 8), gpu.BindGroup.Entry.buffer(1, blur_params_buffer, 0, 8),
}, },
}); }));
const compute_bind_group_0 = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ const compute_bind_group_0 = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = blur_pipeline.getBindGroupLayout(1), .layout = blur_pipeline.getBindGroupLayout(1),
.entry_count = 3, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.textureView(1, cube_texture.createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(1, cube_texture.createView(&gpu.TextureView.Descriptor{})),
gpu.BindGroup.Entry.textureView(2, textures[0].createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(2, textures[0].createView(&gpu.TextureView.Descriptor{})),
gpu.BindGroup.Entry.buffer(3, flip[0], 0, 4), gpu.BindGroup.Entry.buffer(3, flip[0], 0, 4),
}, },
}); }));
const compute_bind_group_1 = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ const compute_bind_group_1 = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = blur_pipeline.getBindGroupLayout(1), .layout = blur_pipeline.getBindGroupLayout(1),
.entry_count = 3, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.textureView(1, textures[0].createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(1, textures[0].createView(&gpu.TextureView.Descriptor{})),
gpu.BindGroup.Entry.textureView(2, textures[1].createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(2, textures[1].createView(&gpu.TextureView.Descriptor{})),
gpu.BindGroup.Entry.buffer(3, flip[1], 0, 4), gpu.BindGroup.Entry.buffer(3, flip[1], 0, 4),
}, },
}); }));
const compute_bind_group_2 = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ const compute_bind_group_2 = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = blur_pipeline.getBindGroupLayout(1), .layout = blur_pipeline.getBindGroupLayout(1),
.entry_count = 3, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.textureView(1, textures[1].createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(1, textures[1].createView(&gpu.TextureView.Descriptor{})),
gpu.BindGroup.Entry.textureView(2, textures[0].createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(2, textures[0].createView(&gpu.TextureView.Descriptor{})),
gpu.BindGroup.Entry.buffer(3, flip[0], 0, 4), gpu.BindGroup.Entry.buffer(3, flip[0], 0, 4),
}, },
}); }));
const show_result_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ const show_result_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = fullscreen_quad_pipeline.getBindGroupLayout(0), .layout = fullscreen_quad_pipeline.getBindGroupLayout(0),
.entry_count = 2, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.sampler(0, sampler), gpu.BindGroup.Entry.sampler(0, sampler),
gpu.BindGroup.Entry.textureView(1, textures[1].createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(1, textures[1].createView(&gpu.TextureView.Descriptor{})),
}, },
}); }));
const blur_params_buffer_data = [_]u32{ filter_size, block_dimension }; const blur_params_buffer_data = [_]u32{ filter_size, block_dimension };
queue.writeBuffer(blur_params_buffer, 0, &blur_params_buffer_data); queue.writeBuffer(blur_params_buffer, 0, &blur_params_buffer_data);
@ -268,18 +238,14 @@ pub fn update(app: *App, core: *mach.Core) !void {
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color), .clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const render_pass_descriptor = gpu.RenderPassDescriptor{ const render_pass_descriptor = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{ });
color_attachment,
},
};
const render_pass = encoder.beginRenderPass(&render_pass_descriptor); const render_pass = encoder.beginRenderPass(&render_pass_descriptor);
render_pass.setPipeline(app.fullscreen_quad_pipeline); render_pass.setPipeline(app.fullscreen_quad_pipeline);

View file

@ -27,78 +27,52 @@ pub fn init(app: *App, core: *mach.Core) !void {
.size_min = .{ .width = 20, .height = 20 }, .size_min = .{ .width = 20, .height = 20 },
}); });
const vs_module = core.device.createShaderModule(&.{ const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
});
const vertex_attributes = [_]gpu.VertexAttribute{ const vertex_attributes = [_]gpu.VertexAttribute{
.{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 }, .{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 },
.{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 }, .{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 },
}; };
const vertex_buffer_layout = gpu.VertexBufferLayout{ const vertex_buffer_layout = gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf(Vertex), .array_stride = @sizeOf(Vertex),
.step_mode = .vertex, .step_mode = .vertex,
.attribute_count = vertex_attributes.len,
.attributes = &vertex_attributes, .attributes = &vertex_attributes,
};
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
}); });
const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.blend = null,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = fs_module, .module = fs_module,
.entry_point = "main", .entry_point = "main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0); const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgl = core.device.createBindGroupLayout( const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{ &gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 1, .entries = &.{bgle},
.entries = &[_]gpu.BindGroupLayout.Entry{bgle}, }),
},
); );
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl}; const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{ const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layout_count = 1,
.bind_group_layouts = &bind_group_layouts, .bind_group_layouts = &bind_group_layouts,
}); }));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
.layout = pipeline_layout, .layout = pipeline_layout,
.depth_stencil = null, .vertex = gpu.VertexState.init(.{
.vertex = .{
.module = vs_module, .module = vs_module,
.entry_point = "main", .entry_point = "main",
.buffer_count = 1, .buffers = &.{vertex_buffer_layout},
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout}, }),
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.primitive = .{ .primitive = .{
.front_face = .ccw,
.cull_mode = .back, .cull_mode = .back,
.topology = .triangle_list,
.strip_index_format = .undef,
}, },
}; };
@ -121,13 +95,12 @@ pub fn init(app: *App, core: *mach.Core) !void {
.mapped_at_creation = false, .mapped_at_creation = false,
}); });
const bind_group = core.device.createBindGroup( const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = bgl, .layout = bgl,
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject) * num_instances), gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject) * num_instances),
}, },
}, }),
); );
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor); app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
@ -162,17 +135,15 @@ pub fn update(app: *App, core: *mach.Core) !void {
const back_buffer_view = core.swap_chain.?.getCurrentTextureView(); const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color), .clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const encoder = core.device.createCommandEncoder(null); const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{ const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment}, });
};
{ {
const proj = zm.perspectiveFovRh( const proj = zm.perspectiveFovRh(

View file

@ -20,25 +20,19 @@ pub fn init(_: *App, core: *mach.Core) !void {
.mapped_at_creation = false, .mapped_at_creation = false,
}); });
const compute_module = core.device.createShaderModule(&.{ const compute_module = core.device.createShaderModuleWGSL("main.wgsl", @embedFile("main.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("main.wgsl"),
} },
.label = "shader module",
});
const compute_pipeline = core.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ .compute = gpu.ProgrammableStageDescriptor{ const compute_pipeline = core.device.createComputePipeline(&gpu.ComputePipeline.Descriptor{ .compute = gpu.ProgrammableStageDescriptor{
.module = compute_module, .module = compute_module,
.entry_point = "main", .entry_point = "main",
} }); } });
const compute_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor{ const compute_bind_group = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = compute_pipeline.getBindGroupLayout(0), .layout = compute_pipeline.getBindGroupLayout(0),
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, output, 0, buffer_size), gpu.BindGroup.Entry.buffer(0, output, 0, buffer_size),
}, },
}); }));
compute_module.release(); compute_module.release();

View file

@ -27,90 +27,54 @@ pub fn init(app: *App, core: *mach.Core) !void {
.size_min = .{ .width = 20, .height = 20 }, .size_min = .{ .width = 20, .height = 20 },
}); });
const vs_module = core.device.createShaderModule(&.{ const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
});
const vertex_attributes = [_]gpu.VertexAttribute{ const vertex_attributes = [_]gpu.VertexAttribute{
.{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 }, .{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 },
.{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 }, .{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 },
}; };
const vertex_buffer_layout = gpu.VertexBufferLayout{ const vertex_buffer_layout = gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf(Vertex), .array_stride = @sizeOf(Vertex),
.step_mode = .vertex, .step_mode = .vertex,
.attribute_count = vertex_attributes.len,
.attributes = &vertex_attributes, .attributes = &vertex_attributes,
};
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
}); });
const blend = gpu.BlendState{ const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
.color = .{
.operation = .add, const blend = gpu.BlendState{};
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.blend = &blend, .blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = fs_module, .module = fs_module,
.entry_point = "main", .entry_point = "main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0); const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgl = core.device.createBindGroupLayout( const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{ &gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 1, .entries = &.{bgle},
.entries = &[_]gpu.BindGroupLayout.Entry{bgle}, }),
},
); );
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl}; const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{ const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layout_count = 1,
.bind_group_layouts = &bind_group_layouts, .bind_group_layouts = &bind_group_layouts,
}); }));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
.layout = pipeline_layout, .layout = pipeline_layout,
.depth_stencil = null, .vertex = gpu.VertexState.init(.{
.vertex = .{
.module = vs_module, .module = vs_module,
.entry_point = "main", .entry_point = "main",
.buffer_count = 1, .buffers = &.{vertex_buffer_layout},
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout}, }),
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.primitive = .{ .primitive = .{
.front_face = .ccw,
.cull_mode = .back, .cull_mode = .back,
.topology = .triangle_list,
.strip_index_format = .undef,
}, },
}; };
@ -129,13 +93,12 @@ pub fn init(app: *App, core: *mach.Core) !void {
.mapped_at_creation = false, .mapped_at_creation = false,
}); });
const bind_group = core.device.createBindGroup( const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = bgl, .layout = bgl,
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)), gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)),
}, },
}, }),
); );
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor); app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
@ -170,18 +133,15 @@ pub fn update(app: *App, core: *mach.Core) !void {
const back_buffer_view = core.swap_chain.?.getCurrentTextureView(); const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color), .clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const encoder = core.device.createCommandEncoder(null); const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{ const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment}, });
.depth_stencil_attachment = null,
};
{ {
const time = timer.read(); const time = timer.read();

View file

@ -30,31 +30,20 @@ pub fn init(app: *App, core: *mach.Core) !void {
.size_min = .{ .width = 20, .height = 20 }, .size_min = .{ .width = 20, .height = 20 },
}); });
const vs_module = core.device.createShaderModule(&.{ const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
});
const vertex_attributes = [_]gpu.VertexAttribute{ const vertex_attributes = [_]gpu.VertexAttribute{
.{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 }, .{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 },
.{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 }, .{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 },
}; };
const vertex_buffer_layout = gpu.VertexBufferLayout{ const vertex_buffer_layout = gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf(Vertex), .array_stride = @sizeOf(Vertex),
.step_mode = .vertex, .step_mode = .vertex,
.attribute_count = vertex_attributes.len,
.attributes = &vertex_attributes, .attributes = &vertex_attributes,
};
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
}); });
const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
const blend = gpu.BlendState{ const blend = gpu.BlendState{
.color = .{ .color = .{
.operation = .add, .operation = .add,
@ -72,13 +61,11 @@ pub fn init(app: *App, core: *mach.Core) !void {
.blend = &blend, .blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = fs_module, .module = fs_module,
.entry_point = "main", .entry_point = "main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
@ -89,15 +76,12 @@ pub fn init(app: *App, core: *mach.Core) !void {
.depth_write_enabled = true, .depth_write_enabled = true,
.depth_compare = .less, .depth_compare = .less,
}, },
.vertex = .{ .vertex = gpu.VertexState.init(.{
.module = vs_module, .module = vs_module,
.entry_point = "main", .entry_point = "main",
.buffer_count = 1, .buffers = &.{vertex_buffer_layout},
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout}, }),
},
.primitive = .{ .primitive = .{
.topology = .triangle_list,
// Backface culling since the cube is solid piece of geometry. // Backface culling since the cube is solid piece of geometry.
// Faces pointing away from the camera will be occluded by faces // Faces pointing away from the camera will be occluded by faces
// pointing toward the camera. // pointing toward the camera.
@ -154,15 +138,14 @@ pub fn init(app: *App, core: *mach.Core) !void {
}); });
const bind_group = core.device.createBindGroup( const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = pipeline.getBindGroupLayout(0), .layout = pipeline.getBindGroupLayout(0),
.entry_count = 3, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)), gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)),
gpu.BindGroup.Entry.sampler(1, sampler), gpu.BindGroup.Entry.sampler(1, sampler),
gpu.BindGroup.Entry.textureView(2, cube_texture.createView(&gpu.TextureView.Descriptor{})), gpu.BindGroup.Entry.textureView(2, cube_texture.createView(&gpu.TextureView.Descriptor{})),
}, },
}, }),
); );
app.pipeline = pipeline; app.pipeline = pipeline;
@ -205,16 +188,15 @@ pub fn update(app: *App, core: *mach.Core) !void {
}; };
const encoder = core.device.createCommandEncoder(null); const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{ const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
.depth_stencil_attachment = &.{ .depth_stencil_attachment = &.{
.view = app.depth_texture_view, .view = app.depth_texture_view,
.depth_clear_value = 1.0, .depth_clear_value = 1.0,
.depth_load_op = .clear, .depth_load_op = .clear,
.depth_store_op = .store, .depth_store_op = .store,
}, },
}; });
{ {
const time = timer.read(); const time = timer.read();

View file

@ -8,64 +8,26 @@ pipeline: *gpu.RenderPipeline,
queue: *gpu.Queue, queue: *gpu.Queue,
pub fn init(app: *App, core: *mach.Core) !void { pub fn init(app: *App, core: *mach.Core) !void {
const vs_module = core.device.createShaderModule(&.{ const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{ const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
});
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
});
// Fragment state // Fragment state
const blend = gpu.BlendState{ const blend = gpu.BlendState{};
.color = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.blend = &blend, .blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = fs_module, .module = fs_module,
.entry_point = "main", .entry_point = "main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
.layout = null, .vertex = gpu.VertexState{
.depth_stencil = null,
.vertex = .{
.module = vs_module, .module = vs_module,
.entry_point = "main", .entry_point = "main",
.buffers = null,
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.primitive = .{
.front_face = .ccw,
.cull_mode = .none,
.topology = .triangle_list,
.strip_index_format = .undef,
}, },
}; };
@ -82,18 +44,15 @@ pub fn update(app: *App, core: *mach.Core) !void {
const back_buffer_view = core.swap_chain.?.getCurrentTextureView(); const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color), .clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const encoder = core.device.createCommandEncoder(null); const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{ const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment}, });
.depth_stencil_attachment = null,
};
const pass = encoder.beginRenderPass(&render_pass_info); const pass = encoder.beginRenderPass(&render_pass_info);
pass.setPipeline(app.pipeline); pass.setPipeline(app.pipeline);
pass.draw(3, 1, 0, 0); pass.draw(3, 1, 0, 0);

View file

@ -28,90 +28,54 @@ pub fn init(app: *App, core: *mach.Core) !void {
.size_min = .{ .width = 20, .height = 20 }, .size_min = .{ .width = 20, .height = 20 },
}); });
const vs_module = core.device.createShaderModule(&.{ const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
});
const vertex_attributes = [_]gpu.VertexAttribute{ const vertex_attributes = [_]gpu.VertexAttribute{
.{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 }, .{ .format = .float32x4, .offset = @offsetOf(Vertex, "pos"), .shader_location = 0 },
.{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 }, .{ .format = .float32x2, .offset = @offsetOf(Vertex, "uv"), .shader_location = 1 },
}; };
const vertex_buffer_layout = gpu.VertexBufferLayout{ const vertex_buffer_layout = gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf(Vertex), .array_stride = @sizeOf(Vertex),
.step_mode = .vertex, .step_mode = .vertex,
.attribute_count = vertex_attributes.len,
.attributes = &vertex_attributes, .attributes = &vertex_attributes,
};
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
}); });
const blend = gpu.BlendState{ const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
.color = .{
.operation = .add, const blend = gpu.BlendState{};
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const color_target = gpu.ColorTargetState{ const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format, .format = core.swap_chain_format,
.blend = &blend, .blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all, .write_mask = gpu.ColorWriteMaskFlags.all,
}; };
const fragment = gpu.FragmentState{ const fragment = gpu.FragmentState.init(.{
.module = fs_module, .module = fs_module,
.entry_point = "main", .entry_point = "main",
.target_count = 1, .targets = &.{color_target},
.targets = &[_]gpu.ColorTargetState{color_target}, });
.constants = null,
};
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0); const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgl = core.device.createBindGroupLayout( const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{ &gpu.BindGroupLayout.Descriptor.init(.{
.entry_count = 1, .entries = &.{bgle},
.entries = &[_]gpu.BindGroupLayout.Entry{bgle}, }),
},
); );
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl}; const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{ const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layout_count = 1,
.bind_group_layouts = &bind_group_layouts, .bind_group_layouts = &bind_group_layouts,
}); }));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
.layout = pipeline_layout, .layout = pipeline_layout,
.depth_stencil = null, .vertex = gpu.VertexState.init(.{
.vertex = .{
.module = vs_module, .module = vs_module,
.entry_point = "main", .entry_point = "main",
.buffer_count = 1, .buffers = &.{vertex_buffer_layout},
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout}, }),
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.primitive = .{ .primitive = .{
.front_face = .ccw,
.cull_mode = .back, .cull_mode = .back,
.topology = .triangle_list,
.strip_index_format = .undef,
}, },
}; };
@ -135,23 +99,21 @@ pub fn init(app: *App, core: *mach.Core) !void {
}); });
const bind_group1 = core.device.createBindGroup( const bind_group1 = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = bgl, .layout = bgl,
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)), gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)),
}, },
}, }),
); );
const bind_group2 = core.device.createBindGroup( const bind_group2 = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{ &gpu.BindGroup.Descriptor.init(.{
.layout = bgl, .layout = bgl,
.entry_count = 1, .entries = &.{
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, uniform_offset, @sizeOf(UniformBufferObject)), gpu.BindGroup.Entry.buffer(0, uniform_buffer, uniform_offset, @sizeOf(UniformBufferObject)),
}, },
}, }),
); );
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor); app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
@ -188,18 +150,15 @@ pub fn update(app: *App, core: *mach.Core) !void {
const back_buffer_view = core.swap_chain.?.getCurrentTextureView(); const back_buffer_view = core.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color), .clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
}; };
const encoder = core.device.createCommandEncoder(null); const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{ const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachment_count = 1, .color_attachments = &.{color_attachment},
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment}, });
.depth_stencil_attachment = null,
};
{ {
const time = timer.read(); const time = timer.read();