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,
};
const render_pass_descriptor = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
const render_pass_descriptor = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{color_attachment},
.depth_stencil_attachment = &.{
.view = app.depth.?.view,
.depth_load_op = .clear,
.depth_store_op = .store,
.stencil_load_op = .undef,
.stencil_store_op = .undef,
.depth_clear_value = 1.0,
},
};
});
const pass = encoder.beginRenderPass(&render_pass_descriptor);
defer pass.release();
@ -227,13 +224,12 @@ const Camera = struct {
.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),
.entry_count = 1,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.buffer(0, buffer.buffer, 0, buffer.size),
},
});
}));
self.buffer = buffer;
self.bind_group = bind_group;
@ -259,12 +255,11 @@ const Camera = struct {
inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout {
const visibility = .{ .vertex = true, .fragment = true };
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor{
.entry_count = 1,
.entries = &[_]gpu.BindGroupLayout.Entry{
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{
gpu.BindGroupLayout.Entry.buffer(0, visibility, .uniform, false, 0),
},
});
}));
}
};
@ -332,75 +327,51 @@ const Cube = struct {
fn pipeline(core: *mach.Core) *gpu.RenderPipeline {
const device = core.device;
const layout_descriptor = gpu.PipelineLayout.Descriptor{
.bind_group_layout_count = 3,
.bind_group_layouts = &[_]*gpu.BindGroupLayout{
const layout_descriptor = gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &.{
Camera.bindGroupLayout(device),
Texture.bindGroupLayout(device),
Light.bindGroupLayout(device),
},
};
});
const layout = device.createPipelineLayout(&layout_descriptor);
defer layout.release();
const shader = device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("cube.wgsl"),
} },
});
const shader = device.createShaderModuleWGSL("cube.wgsl", @embedFile("cube.wgsl"));
defer shader.release();
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{
.format = core.swap_chain_format,
.write_mask = gpu.ColorWriteMaskFlags.all,
.blend = &blend,
};
const fragment = gpu.FragmentState{
const fragment = gpu.FragmentState.init(.{
.module = shader,
.entry_point = "fs_main",
.target_count = 1,
.targets = &[_]gpu.ColorTargetState{color_target},
.constants = null,
};
.targets = &.{color_target},
});
const descriptor = gpu.RenderPipeline.Descriptor{
.layout = layout,
.fragment = &fragment,
.vertex = .{
.vertex = gpu.VertexState.init(.{
.module = shader,
.entry_point = "vs_main",
.buffer_count = 2,
.buffers = &[_]gpu.VertexBufferLayout{
.buffers = &.{
Self.vertexBufferLayout(),
Self.instanceLayout(),
},
},
}),
.depth_stencil = &.{
.format = Texture.DEPTH_FORMAT,
.depth_write_enabled = true,
.depth_compare = .less,
},
.primitive = .{
.front_face = .ccw,
.cull_mode = .back,
// .cull_mode = .none,
.topology = .triangle_strip,
.strip_index_format = .undef,
},
};
@ -470,12 +441,10 @@ const Cube = struct {
.shader_location = 2,
},
};
return gpu.VertexBufferLayout{
return gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf([8]f32),
.step_mode = .vertex,
.attribute_count = attributes.len,
.attributes = &attributes,
};
});
}
fn instanceLayout() gpu.VertexBufferLayout {
@ -502,12 +471,11 @@ const Cube = struct {
},
};
return gpu.VertexBufferLayout{
return gpu.VertexBufferLayout.init(.{
.array_stride = @sizeOf([16]f32),
.step_mode = .instance,
.attribute_count = attributes.len,
.attributes = &attributes,
};
});
}
};
@ -606,21 +574,15 @@ const Texture = struct {
const texture = device.createTexture(&gpu.Texture.Descriptor{
.size = extent,
.mip_level_count = 1,
.sample_count = 1,
.dimension = .dimension_2d,
.format = FORMAT,
.usage = .{ .copy_dst = true, .texture_binding = true },
});
const view = texture.createView(&gpu.TextureView.Descriptor{
.aspect = .all,
.format = FORMAT,
.dimension = .dimension_2d,
.base_array_layer = 0,
.array_layer_count = 1,
.mip_level_count = 1,
.base_mip_level = 0,
});
const sampler = device.createSampler(&gpu.Sampler.Descriptor{
@ -630,9 +592,6 @@ const Texture = struct {
.mag_filter = .linear,
.min_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
});
@ -649,14 +608,13 @@ const Texture = struct {
);
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,
.entry_count = 2,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.textureView(0, view),
gpu.BindGroup.Entry.sampler(1, sampler),
},
});
}));
return Self{
.view = view,
@ -674,9 +632,6 @@ const Texture = struct {
const texture = device.createTexture(&gpu.Texture.Descriptor{
.size = extent,
.mip_level_count = 1,
.sample_count = 1,
.dimension = .dimension_2d,
.format = DEPTH_FORMAT,
.usage = .{
.render_attachment = true,
@ -685,26 +640,14 @@ const Texture = struct {
});
const view = texture.createView(&gpu.TextureView.Descriptor{
.aspect = .all,
.format = .undef,
.dimension = .dimension_2d,
.base_array_layer = 0,
.array_layer_count = 1,
.mip_level_count = 1,
.base_mip_level = 0,
});
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,
.min_filter = .nearest,
.mipmap_filter = .nearest,
.compare = .less_equal,
.lod_min_clamp = 0.0,
.lod_max_clamp = std.math.f32_max,
.max_anisotropy = 1,
});
return Self{
@ -718,13 +661,12 @@ const Texture = struct {
inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout {
const visibility = .{ .fragment = true };
const Entry = gpu.BindGroupLayout.Entry;
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor{
.entry_count = 2,
.entries = &[_]Entry{
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{
Entry.texture(0, visibility, .float, .dimension_2d, false),
Entry.sampler(1, visibility, .filtering),
},
});
}));
}
};
@ -753,13 +695,12 @@ const Light = struct {
.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),
.entry_count = 1,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.buffer(0, buffer.buffer, 0, buffer.size),
},
});
}));
return Self{
.buffer = buffer,
@ -782,84 +723,59 @@ const Light = struct {
inline fn bindGroupLayout(device: *gpu.Device) *gpu.BindGroupLayout {
const visibility = .{ .vertex = true, .fragment = true };
const Entry = gpu.BindGroupLayout.Entry;
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor{
.entry_count = 1,
.entries = &[_]Entry{
return device.createBindGroupLayout(&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{
Entry.buffer(0, visibility, .uniform, false, 0),
},
});
}));
}
fn pipeline(core: *mach.Core) *gpu.RenderPipeline {
const device = core.device;
const layout_descriptor = gpu.PipelineLayout.Descriptor{
.bind_group_layout_count = 2,
.bind_group_layouts = &[_]*gpu.BindGroupLayout{
const layout_descriptor = gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &.{
Camera.bindGroupLayout(device),
Light.bindGroupLayout(device),
},
};
});
const layout = device.createPipelineLayout(&layout_descriptor);
defer layout.release();
const shader = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("light.wgsl"),
} },
});
const shader = core.device.createShaderModuleWGSL("light.wgsl", @embedFile("light.wgsl"));
defer shader.release();
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{
.format = core.swap_chain_format,
.write_mask = gpu.ColorWriteMaskFlags.all,
.blend = &blend,
};
const fragment = gpu.FragmentState{
const fragment = gpu.FragmentState.init(.{
.module = shader,
.entry_point = "fs_main",
.target_count = 1,
.targets = &[_]gpu.ColorTargetState{color_target},
.constants = null,
};
.targets = &.{color_target},
});
const descriptor = gpu.RenderPipeline.Descriptor{
.layout = layout,
.fragment = &fragment,
.vertex = .{
.vertex = gpu.VertexState.init(.{
.module = shader,
.entry_point = "vs_main",
.buffer_count = 1,
.buffers = &[_]gpu.VertexBufferLayout{
.buffers = &.{
Cube.vertexBufferLayout(),
},
},
}),
.depth_stencil = &.{
.format = Texture.DEPTH_FORMAT,
.depth_write_enabled = true,
.depth_compare = .less,
},
.primitive = .{
.front_face = .ccw,
.cull_mode = .back,
// .cull_mode = .none,
.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 {
const sprite_shader_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("sprite.wgsl"),
} },
.label = "sprite shader module",
});
const sprite_shader_module = core.device.createShaderModuleWGSL(
"sprite.wgsl",
@embedFile("sprite.wgsl"),
);
const update_sprite_shader_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("updateSprites.wgsl"),
} },
.label = "update sprite shader module",
});
const update_sprite_shader_module = core.device.createShaderModuleWGSL(
"updateSprites.wgsl",
@embedFile("updateSprites.wgsl"),
);
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{
.vertex = .{
.vertex = gpu.VertexState.init(.{
.module = sprite_shader_module,
.entry_point = "vert_main",
.buffer_count = 2,
.buffers = &[_]gpu.VertexBufferLayout{
.{
.buffers = &.{
gpu.VertexBufferLayout.init(.{
// instanced particles buffer
.array_stride = 4 * 4,
.step_mode = .instance,
.attribute_count = instanced_particles_attributes.len,
.attributes = &instanced_particles_attributes,
},
.{
}),
gpu.VertexBufferLayout.init(.{
// vertex buffer
.array_stride = 2 * 4,
.step_mode = .vertex,
.attribute_count = vertex_buffer_attributes.len,
.attributes = &vertex_buffer_attributes,
}),
},
},
},
.fragment = &gpu.FragmentState{
}),
.fragment = &gpu.FragmentState.init(.{
.module = sprite_shader_module,
.entry_point = "frag_main",
.target_count = 1,
.targets = &[_]gpu.ColorTargetState{.{
.format = core.swap_chain_format,
}},
},
}),
});
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;
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),
.entry_count = 3,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
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(2, particle_buffers[(i + 1) % 2], 0, initial_particle_data.len * @sizeOf(f32)),
},
});
}));
}
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 color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear,
.store_op = .store,
};
const render_pass_descriptor = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{
const render_pass_descriptor = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{
color_attachment,
},
};
});
sim_params[0] = @floatCast(f32, core.delta_time);
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
//! (we can't use the swapchain texture directly, but we can get the effect
//! 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
//! 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
//! 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.
//! We also need a second texture to use on the cube (after the render pass
//! it needs to copy the other texture.) We can't use the same texture since
//! 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,
//! are set to 0 after core.init() and because webgpu does not implement copyTextureToTexture,
//! for opengl
@ -45,71 +45,44 @@ 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 blend = gpu.BlendState{
.color = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
const blend = gpu.BlendState{};
const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.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 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_textureview = gpu.BindGroupLayout.Entry.texture(2, .{ .fragment = true }, .float, .dimension_2d, false);
const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entry_count = 3,
.entries = &[_]gpu.BindGroupLayout.Entry{ bgle_buffer, bgle_sampler, bgle_textureview },
},
&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{ bgle_buffer, bgle_sampler, bgle_textureview },
}),
);
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{
.bind_group_layout_count = 1,
const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &bind_group_layouts,
});
}));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment,
@ -119,22 +92,13 @@ 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},
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.buffers = &.{vertex_buffer_layout},
}),
.primitive = .{
.front_face = .ccw,
.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(
&gpu.BindGroup.Descriptor{
&gpu.BindGroup.Descriptor.init(.{
.layout = bgl,
.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_view),
},
},
}),
);
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{
.view = cube_view,
.resolve_target = null,
.clear_value = gpu.Color{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 1 },
.load_op = .clear,
.store_op = .store,
};
const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = gpu.Color{ .r = 0.5, .g = 0.5, .b = 0.5, .a = 1 },
.load_op = .clear,
.store_op = .store,
@ -263,21 +224,17 @@ 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 = .undef,
.stencil_store_op = .undef,
};
const encoder = core.device.createCommandEncoder(null);
const cube_render_pass_info = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{cube_color_attachment},
const cube_render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{cube_color_attachment},
.depth_stencil_attachment = &depth_stencil_attachment,
};
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 = &depth_stencil_attachment,
};
});
{
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 = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{
&gpu.BindGroup.Descriptor.init(.{
.layout = app.bgl,
.entry_count = 3,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
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),
},
},
}),
);
} else {
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.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(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.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 vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
const blend = gpu.BlendState{
.color = .{
@ -160,51 +149,34 @@ 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 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 sbgle = gpu.BindGroupLayout.Entry.sampler(2, .{ .fragment = true }, .filtering);
const tbgle = gpu.BindGroupLayout.Entry.texture(3, .{ .fragment = true }, .float, .dimension_2d, false);
const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entry_count = 4,
.entries = &[_]gpu.BindGroupLayout.Entry{ vbgle, fbgle, sbgle, tbgle },
},
&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{ vbgle, fbgle, sbgle, tbgle },
}),
);
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{
.bind_group_layout_count = 1,
const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &bind_group_layouts,
});
}));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment,
.layout = pipeline_layout,
.depth_stencil = null,
.vertex = .{
.vertex = gpu.VertexState.init(.{
.module = vs_module,
.entry_point = "main",
.buffer_count = 1,
.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,
},
.buffers = &.{draw.VERTEX_BUFFER_LAYOUT},
}),
};
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(
&gpu.BindGroup.Descriptor{
&gpu.BindGroup.Descriptor.init(.{
.layout = bgl,
.entry_count = 4,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
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.sampler(2, sampler),
gpu.BindGroup.Entry.textureView(3, texture.createView(&gpu.TextureView.Descriptor{ .dimension = .dimension_2d })),
},
},
}),
);
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 color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear,
.store_op = .store,
};
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},
});
{
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 },
});
const blur_shader_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("blur.wgsl"),
} },
.label = "blur shader module",
});
const blur_shader_module = core.device.createShaderModuleWGSL("blur.wgsl", @embedFile("blur.wgsl"));
const blur_pipeline_descriptor = gpu.ComputePipeline.Descriptor{
.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 fullscreen_quad_vs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("fullscreen_textured_quad.wgsl"),
} },
.label = "fullscreen quad vertex shader",
});
const fullscreen_quad_vs_module = core.device.createShaderModuleWGSL(
"fullscreen_textured_quad.wgsl",
@embedFile("fullscreen_textured_quad.wgsl"),
);
const fullscreen_quad_fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @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 fullscreen_quad_fs_module = core.device.createShaderModuleWGSL(
"fullscreen_textured_quad.wgsl",
@embedFile("fullscreen_textured_quad.wgsl"),
);
const blend = gpu.BlendState{};
const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all,
};
const fragment_state = gpu.FragmentState{
const fragment_state = gpu.FragmentState.init(.{
.module = fullscreen_quad_fs_module,
.entry_point = "frag_main",
.target_count = 1,
.targets = &[_]gpu.ColorTargetState{color_target},
.constants = null,
};
.targets = &.{color_target},
});
const fullscreen_quad_pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.layout = null,
.fragment = &fragment_state,
.vertex = .{
.module = fullscreen_quad_vs_module,
.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 },
});
const compute_constants = core.device.createBindGroup(&gpu.BindGroup.Descriptor{
const compute_constants = core.device.createBindGroup(&gpu.BindGroup.Descriptor.init(.{
.layout = blur_pipeline.getBindGroupLayout(0),
.entry_count = 2,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.sampler(0, sampler),
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),
.entry_count = 3,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
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.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),
.entry_count = 3,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
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.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),
.entry_count = 3,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
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.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),
.entry_count = 2,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.sampler(0, sampler),
gpu.BindGroup.Entry.textureView(1, textures[1].createView(&gpu.TextureView.Descriptor{})),
},
});
}));
const blur_params_buffer_data = [_]u32{ filter_size, block_dimension };
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{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear,
.store_op = .store,
};
const render_pass_descriptor = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{
color_attachment,
},
};
const render_pass_descriptor = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{color_attachment},
});
const render_pass = encoder.beginRenderPass(&render_pass_descriptor);
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 },
});
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 color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.blend = null,
.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 bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entry_count = 1,
.entries = &[_]gpu.BindGroupLayout.Entry{bgle},
},
&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{bgle},
}),
);
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{
.bind_group_layout_count = 1,
const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &bind_group_layouts,
});
}));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment,
.layout = pipeline_layout,
.depth_stencil = null,
.vertex = .{
.vertex = gpu.VertexState.init(.{
.module = vs_module,
.entry_point = "main",
.buffer_count = 1,
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout},
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.buffers = &.{vertex_buffer_layout},
}),
.primitive = .{
.front_face = .ccw,
.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,
});
const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{
&gpu.BindGroup.Descriptor.init(.{
.layout = bgl,
.entry_count = 1,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject) * num_instances),
},
},
}),
);
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 color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear,
.store_op = .store,
};
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},
});
{
const proj = zm.perspectiveFovRh(

View file

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

View file

@ -27,90 +27,54 @@ 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 blend = gpu.BlendState{
.color = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
const blend = gpu.BlendState{};
const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.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 bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entry_count = 1,
.entries = &[_]gpu.BindGroupLayout.Entry{bgle},
},
&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{bgle},
}),
);
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{
.bind_group_layout_count = 1,
const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &bind_group_layouts,
});
}));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment,
.layout = pipeline_layout,
.depth_stencil = null,
.vertex = .{
.vertex = gpu.VertexState.init(.{
.module = vs_module,
.entry_point = "main",
.buffer_count = 1,
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout},
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.buffers = &.{vertex_buffer_layout},
}),
.primitive = .{
.front_face = .ccw,
.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,
});
const bind_group = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{
&gpu.BindGroup.Descriptor.init(.{
.layout = bgl,
.entry_count = 1,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)),
},
},
}),
);
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 color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear,
.store_op = .store,
};
const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
.depth_stencil_attachment = null,
};
const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{color_attachment},
});
{
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 },
});
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();

View file

@ -8,64 +8,26 @@ pipeline: *gpu.RenderPipeline,
queue: *gpu.Queue,
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",
});
const fs_module = core.device.createShaderModule(&.{
.next_in_chain = .{ .wgsl_descriptor = &.{
.source = @embedFile("frag.wgsl"),
} },
.label = "my fragment shader",
});
const vs_module = core.device.createShaderModuleWGSL("vert.wgsl", @embedFile("vert.wgsl"));
const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
// Fragment state
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{
.format = core.swap_chain_format,
.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,
.layout = null,
.depth_stencil = null,
.vertex = .{
.vertex = gpu.VertexState{
.module = vs_module,
.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 color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear,
.store_op = .store,
};
const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
.depth_stencil_attachment = null,
};
const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{color_attachment},
});
const pass = encoder.beginRenderPass(&render_pass_info);
pass.setPipeline(app.pipeline);
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 },
});
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 blend = gpu.BlendState{
.color = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
.alpha = .{
.operation = .add,
.src_factor = .one,
.dst_factor = .zero,
},
};
const fs_module = core.device.createShaderModuleWGSL("frag.wgsl", @embedFile("frag.wgsl"));
const blend = gpu.BlendState{};
const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.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 bgle = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bgl = core.device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor{
.entry_count = 1,
.entries = &[_]gpu.BindGroupLayout.Entry{bgle},
},
&gpu.BindGroupLayout.Descriptor.init(.{
.entries = &.{bgle},
}),
);
const bind_group_layouts = [_]*gpu.BindGroupLayout{bgl};
const pipeline_layout = core.device.createPipelineLayout(&.{
.bind_group_layout_count = 1,
const pipeline_layout = core.device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.bind_group_layouts = &bind_group_layouts,
});
}));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment,
.layout = pipeline_layout,
.depth_stencil = null,
.vertex = .{
.vertex = gpu.VertexState.init(.{
.module = vs_module,
.entry_point = "main",
.buffer_count = 1,
.buffers = &[_]gpu.VertexBufferLayout{vertex_buffer_layout},
},
.multisample = .{
.count = 1,
.mask = 0xFFFFFFFF,
.alpha_to_coverage_enabled = false,
},
.buffers = &.{vertex_buffer_layout},
}),
.primitive = .{
.front_face = .ccw,
.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(
&gpu.BindGroup.Descriptor{
&gpu.BindGroup.Descriptor.init(.{
.layout = bgl,
.entry_count = 1,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, 0, @sizeOf(UniformBufferObject)),
},
},
}),
);
const bind_group2 = core.device.createBindGroup(
&gpu.BindGroup.Descriptor{
&gpu.BindGroup.Descriptor.init(.{
.layout = bgl,
.entry_count = 1,
.entries = &[_]gpu.BindGroup.Entry{
.entries = &.{
gpu.BindGroup.Entry.buffer(0, uniform_buffer, uniform_offset, @sizeOf(UniformBufferObject)),
},
},
}),
);
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 color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view,
.resolve_target = null,
.clear_value = std.mem.zeroes(gpu.Color),
.load_op = .clear,
.store_op = .store,
};
const encoder = core.device.createCommandEncoder(null);
const render_pass_info = gpu.RenderPassDescriptor{
.color_attachment_count = 1,
.color_attachments = &[_]gpu.RenderPassColorAttachment{color_attachment},
.depth_stencil_attachment = null,
};
const render_pass_info = gpu.RenderPassDescriptor.init(.{
.color_attachments = &.{color_attachment},
});
{
const time = timer.read();