{examples,shaderexp}: update to new mach/gpu API

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-08-11 15:35:33 -07:00 committed by Stephen Gutekanst
parent 852d232335
commit f299d87aa2
39 changed files with 473 additions and 338 deletions

View file

@ -1,4 +1,4 @@
@stage(fragment) fn main(
@fragment fn main(
@location(0) fragUV: vec2<f32>,
@location(1) fragPosition: vec4<f32>
) -> @location(0) vec4<f32> {

View file

@ -12,11 +12,11 @@ const UniformBufferObject = struct {
var timer: mach.Timer = undefined;
pipeline: gpu.RenderPipeline,
queue: gpu.Queue,
vertex_buffer: gpu.Buffer,
uniform_buffer: gpu.Buffer,
bind_group: gpu.BindGroup,
pipeline: *gpu.RenderPipeline,
queue: *gpu.Queue,
vertex_buffer: *gpu.Buffer,
uniform_buffer: *gpu.Buffer,
bind_group: *gpu.BindGroup,
pub const App = @This();
@ -28,8 +28,10 @@ pub fn init(app: *App, core: *mach.Core) !void {
});
const vs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("vert.wgsl"),
} },
.label = "my vertex shader",
.code = .{ .wgsl = @embedFile("vert.wgsl") },
});
const vertex_attributes = [_]gpu.VertexAttribute{
@ -44,31 +46,36 @@ pub fn init(app: *App, core: *mach.Core) !void {
};
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
.code = .{ .wgsl = @embedFile("frag.wgsl") },
});
const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.blend = null,
.write_mask = gpu.ColorWriteMask.all,
.write_mask = gpu.ColorWriteMaskFlags.all,
};
const fragment = gpu.FragmentState{
.module = fs_module,
.entry_point = "main",
.targets = &.{color_target},
.target_count = 1,
.targets = &[_]gpu.ColorTargetState{color_target},
.constants = null,
};
const bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entries = &.{bgle},
.entry_count = 1,
.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(&.{
.bind_group_layout_count = 1,
.bind_group_layouts = &bind_group_layouts,
});
@ -79,7 +86,8 @@ pub fn init(app: *App, core: *mach.Core) !void {
.vertex = .{
.module = vs_module,
.entry_point = "main",
.buffers = &.{vertex_buffer_layout},
.buffer_count = 1,
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout},
},
.multisample = .{
.count = 1,
@ -90,7 +98,7 @@ pub fn init(app: *App, core: *mach.Core) !void {
.front_face = .ccw,
.cull_mode = .back,
.topology = .triangle_list,
.strip_index_format = .none,
.strip_index_format = .undef,
},
};
@ -100,7 +108,7 @@ pub fn init(app: *App, core: *mach.Core) !void {
.mapped_at_creation = true,
});
var vertex_mapped = vertex_buffer.getMappedRange(Vertex, 0, vertices.len);
std.mem.copy(Vertex, vertex_mapped, vertices[0..]);
std.mem.copy(Vertex, vertex_mapped.?, vertices[0..]);
vertex_buffer.unmap();
const x_count = 4;
@ -115,7 +123,8 @@ pub fn init(app: *App, core: *mach.Core) !void {
const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{
.layout = bgl,
.entries = &.{
.entry_count = 1,
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject) * num_instances),
},
},
@ -160,8 +169,9 @@ pub fn update(app: *App, core: *mach.Core) !void {
};
const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
.color_attachments = &.{color_attachment},
const render_pass_info = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
};
{
@ -191,7 +201,7 @@ pub fn update(app: *App, core: *mach.Core) !void {
m += 1;
}
}
encoder.writeBuffer(app.uniform_buffer, 0, UniformBufferObject, &ubos);
encoder.writeBuffer(app.uniform_buffer, 0, &ubos);
}
const pass = encoder.beginRenderPass(&render_pass_info);

View file

@ -1,12 +1,12 @@
@binding(0) @group(0) var<uniform> ubos : array<mat4x4<f32>, 16>;
struct VertexOutput {
@builtin(position) position_clip : vec4<f32>;
@location(0) fragUV : vec2<f32>;
@location(1) fragPosition: vec4<f32>;
@builtin(position) position_clip : vec4<f32>,
@location(0) fragUV : vec2<f32>,
@location(1) fragPosition: vec4<f32>,
};
@stage(vertex)
@vertex
fn main(@builtin(instance_index) instanceIdx : u32,
@location(0) position : vec4<f32>,
@location(1) uv : vec2<f32>) -> VertexOutput {