{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,7 +1,7 @@
@binding(1) @group(0) var mySampler: sampler;
@binding(2) @group(0) var myTexture: texture_2d<f32>;
@stage(fragment) fn main(
@fragment fn main(
@location(0) fragUV: vec2<f32>,
@location(1) fragPosition: vec4<f32>
) -> @location(0) vec4<f32> {

View file

@ -24,19 +24,19 @@ 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,
depth_texture: ?gpu.Texture,
depth_texture_view: gpu.TextureView,
cube_texture: gpu.Texture,
cube_texture_view: gpu.TextureView,
cube_texture_render: gpu.Texture,
cube_texture_view_render: gpu.TextureView,
sampler: gpu.Sampler,
bgl: gpu.BindGroupLayout,
pipeline: *gpu.RenderPipeline,
queue: *gpu.Queue,
vertex_buffer: *gpu.Buffer,
uniform_buffer: *gpu.Buffer,
bind_group: *gpu.BindGroup,
depth_texture: ?*gpu.Texture,
depth_texture_view: *gpu.TextureView,
cube_texture: *gpu.Texture,
cube_texture_view: *gpu.TextureView,
cube_texture_render: *gpu.Texture,
cube_texture_view_render: *gpu.TextureView,
sampler: *gpu.Sampler,
bgl: *gpu.BindGroupLayout,
pub fn init(app: *App, core: *mach.Core) !void {
timer = try mach.Timer.start();
@ -46,8 +46,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{
@ -62,8 +64,10 @@ 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 blend = gpu.BlendState{
@ -81,12 +85,13 @@ pub fn init(app: *App, core: *mach.Core) !void {
const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.blend = &blend,
.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,
};
@ -95,12 +100,14 @@ pub fn init(app: *App, core: *mach.Core) !void {
const bgle_textureview = gpu.BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false);
const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entries = &.{ bgle_buffer, bgle_sampler, bgle_textureview },
.entry_count = 3,
.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(&.{
.bind_group_layout_count = 1,
.bind_group_layouts = &bind_group_layouts,
});
@ -115,7 +122,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,
@ -126,7 +134,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,
},
};
@ -136,7 +144,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 uniform_buffer = core.device.createBuffer(&.{
@ -179,7 +187,8 @@ pub fn init(app: *App, core: *mach.Core) !void {
const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{
.layout = bgl,
.entries = &.{
.entry_count = 3,
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)),
gpu.BindGroup.Entry.sampler(1, sampler),
gpu.BindGroup.Entry.textureView(2, cube_texture_view),
@ -254,17 +263,19 @@ pub fn update(app: *App, core: *mach.Core) !void {
.depth_load_op = .clear,
.depth_store_op = .store,
.depth_clear_value = 1.0,
.stencil_load_op = .none,
.stencil_store_op = .none,
.stencil_load_op = .undef,
.stencil_store_op = .undef,
};
const encoder = core.device.createCommandEncoder(null);
const cube_render_pass_info = gpu.RenderPassEncoder.Descriptor{
.color_attachments = &.{cube_color_attachment},
const cube_render_pass_info = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{cube_color_attachment},
.depth_stencil_attachment = &depth_stencil_attachment,
};
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},
.depth_stencil_attachment = &depth_stencil_attachment,
};
@ -285,7 +296,7 @@ pub fn update(app: *App, core: *mach.Core) !void {
const ubo = UniformBufferObject{
.mat = zm.transpose(zm.mul(zm.mul(model, view), proj)),
};
encoder.writeBuffer(app.uniform_buffer, 0, UniformBufferObject, &.{ubo});
encoder.writeBuffer(app.uniform_buffer, 0, &[_]UniformBufferObject{ubo});
}
const pass = encoder.beginRenderPass(&render_pass_info);
@ -372,7 +383,8 @@ pub fn resize(app: *App, core: *mach.Core, width: u32, height: u32) !void {
app.bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{
.layout = app.bgl,
.entries = &.{
.entry_count = 3,
.entries = &[_]gpu.BindGroup.Entry{
gpu.BindGroup.Entry.buffer(0, app.uniform_buffer, 0, @sizeOf(UniformBufferObject)),
gpu.BindGroup.Entry.sampler(1, app.sampler),
gpu.BindGroup.Entry.textureView(2, app.cube_texture_view),

View file

@ -10,7 +10,7 @@ struct VertexOut {
@location(1) fragPosition: vec4<f32>,
}
@stage(vertex) fn main(
@vertex fn main(
@location(0) position : vec4<f32>,
@location(1) uv: vec2<f32>
) -> VertexOut {