{examples,shaderexp}: update to new mach/gpu API
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
852d232335
commit
f299d87aa2
39 changed files with 473 additions and 338 deletions
|
|
@ -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> {
|
||||
|
|
|
|||
|
|
@ -14,11 +14,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 fn init(app: *App, core: *mach.Core) !void {
|
||||
timer = try mach.Timer.start();
|
||||
|
|
@ -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,8 +46,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{
|
||||
|
|
@ -63,24 +67,27 @@ 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,
|
||||
};
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
|
|
@ -91,7 +98,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,
|
||||
|
|
@ -102,7 +110,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,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -112,7 +120,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(&.{
|
||||
|
|
@ -123,7 +131,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)),
|
||||
},
|
||||
},
|
||||
|
|
@ -168,8 +177,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},
|
||||
.depth_stencil_attachment = null,
|
||||
};
|
||||
|
||||
|
|
@ -191,7 +201,7 @@ pub fn update(app: *App, core: *mach.Core) !void {
|
|||
const ubo = UniformBufferObject{
|
||||
.mat = zm.transpose(mvp),
|
||||
};
|
||||
encoder.writeBuffer(app.uniform_buffer, 0, UniformBufferObject, &.{ubo});
|
||||
encoder.writeBuffer(app.uniform_buffer, 0, &[_]UniformBufferObject{ubo});
|
||||
}
|
||||
|
||||
const pass = encoder.beginRenderPass(&render_pass_info);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
@group(0) @binding(0) var<uniform> ubo : mat4x4<f32>;
|
||||
struct VertexOut {
|
||||
@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) fn main(
|
||||
@vertex fn main(
|
||||
@location(0) position : vec4<f32>,
|
||||
@location(1) uv: vec2<f32>
|
||||
) -> VertexOut {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue