examples: prepare for adding more examples
This commit is contained in:
parent
2496b50b16
commit
9489fe7083
4 changed files with 1 additions and 1 deletions
3
examples/triangle/frag.wgsl
Normal file
3
examples/triangle/frag.wgsl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
@stage(fragment) fn main() -> @location(0) vec4<f32> {
|
||||
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
114
examples/triangle/main.zig
Normal file
114
examples/triangle/main.zig
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
const gpu = @import("gpu");
|
||||
|
||||
const App = mach.App(*FrameParams, .{});
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
var allocator = gpa.allocator();
|
||||
|
||||
const ctx = try allocator.create(FrameParams);
|
||||
var app = try App.init(allocator, ctx, .{});
|
||||
|
||||
const vs_module = app.device.createShaderModule(&.{
|
||||
.label = "my vertex shader",
|
||||
.code = .{ .wgsl = @embedFile("vert.wgsl") },
|
||||
});
|
||||
|
||||
const fs_module = app.device.createShaderModule(&.{
|
||||
.label = "my fragment shader",
|
||||
.code = .{ .wgsl = @embedFile("frag.wgsl") },
|
||||
});
|
||||
|
||||
// Fragment state
|
||||
const blend = gpu.BlendState{
|
||||
.color = .{
|
||||
.operation = .add,
|
||||
.src_factor = .one,
|
||||
.dst_factor = .one,
|
||||
},
|
||||
.alpha = .{
|
||||
.operation = .add,
|
||||
.src_factor = .one,
|
||||
.dst_factor = .one,
|
||||
},
|
||||
};
|
||||
const color_target = gpu.ColorTargetState{
|
||||
.format = app.swap_chain_format,
|
||||
.blend = &blend,
|
||||
.write_mask = gpu.ColorWriteMask.all,
|
||||
};
|
||||
const fragment = gpu.FragmentState{
|
||||
.module = fs_module,
|
||||
.entry_point = "main",
|
||||
.targets = &.{color_target},
|
||||
.constants = null,
|
||||
};
|
||||
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
|
||||
.fragment = &fragment,
|
||||
.layout = null,
|
||||
.depth_stencil = null,
|
||||
.vertex = .{
|
||||
.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 = .none,
|
||||
},
|
||||
};
|
||||
|
||||
ctx.* = FrameParams{
|
||||
.pipeline = app.device.createRenderPipeline(&pipeline_descriptor),
|
||||
.queue = app.device.getQueue(),
|
||||
};
|
||||
|
||||
vs_module.release();
|
||||
fs_module.release();
|
||||
|
||||
try app.run(.{ .frame = frame });
|
||||
}
|
||||
|
||||
const FrameParams = struct {
|
||||
pipeline: gpu.RenderPipeline,
|
||||
queue: gpu.Queue,
|
||||
};
|
||||
|
||||
fn frame(app: *App, params: *FrameParams) !void {
|
||||
const back_buffer_view = app.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 = app.device.createCommandEncoder(null);
|
||||
const render_pass_info = gpu.RenderPassEncoder.Descriptor{
|
||||
.color_attachments = &.{color_attachment},
|
||||
.depth_stencil_attachment = null,
|
||||
};
|
||||
const pass = encoder.beginRenderPass(&render_pass_info);
|
||||
pass.setPipeline(params.pipeline);
|
||||
pass.draw(3, 1, 0, 0);
|
||||
pass.end();
|
||||
pass.release();
|
||||
|
||||
var command = encoder.finish(null);
|
||||
encoder.release();
|
||||
|
||||
params.queue.submit(&.{command});
|
||||
command.release();
|
||||
app.swap_chain.?.present();
|
||||
back_buffer_view.release();
|
||||
}
|
||||
10
examples/triangle/vert.wgsl
Normal file
10
examples/triangle/vert.wgsl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
@stage(vertex) fn main(
|
||||
@builtin(vertex_index) VertexIndex : u32
|
||||
) -> @builtin(position) vec4<f32> {
|
||||
var pos = array<vec2<f32>, 3>(
|
||||
vec2<f32>( 0.0, 0.5),
|
||||
vec2<f32>(-0.5, -0.5),
|
||||
vec2<f32>( 0.5, -0.5)
|
||||
);
|
||||
return vec4<f32>(pos[VertexIndex], 0.0, 1.0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue