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

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