mach/examples/triangle/main.zig
Stephen Gutekanst edc3b5d60e examples: use default values, gpu helper APIs
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-08-17 01:42:27 -07:00

69 lines
2 KiB
Zig

const std = @import("std");
const mach = @import("mach");
const gpu = @import("gpu");
pub const App = @This();
pipeline: *gpu.RenderPipeline,
queue: *gpu.Queue,
pub fn init(app: *App, core: *mach.Core) !void {
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{};
const color_target = gpu.ColorTargetState{
.format = core.swap_chain_format,
.blend = &blend,
.write_mask = gpu.ColorWriteMaskFlags.all,
};
const fragment = gpu.FragmentState.init(.{
.module = fs_module,
.entry_point = "main",
.targets = &.{color_target},
});
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
.fragment = &fragment,
.vertex = gpu.VertexState{
.module = vs_module,
.entry_point = "main",
},
};
app.pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
app.queue = core.device.getQueue();
vs_module.release();
fs_module.release();
}
pub fn deinit(_: *App, _: *mach.Core) void {}
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,
.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.init(.{
.color_attachments = &.{color_attachment},
});
const pass = encoder.beginRenderPass(&render_pass_info);
pass.setPipeline(app.pipeline);
pass.draw(3, 1, 0, 0);
pass.end();
pass.release();
var command = encoder.finish(null);
encoder.release();
app.queue.submit(&.{command});
command.release();
core.swap_chain.?.present();
back_buffer_view.release();
}