|
|
|
const std = @import("std");
|
|
|
|
const core = @import("mach-core");
|
|
|
|
const gpu = core.gpu;
|
|
|
|
|
|
|
|
const zm = @import("zmath");
|
|
|
|
const vec = zm.f32x4;
|
|
|
|
const Mat = zm.Mat;
|
|
|
|
|
|
|
|
const VertexData = struct {
|
|
|
|
position: [3]f32,
|
|
|
|
color: [3]f32,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const App = @This();
|
|
|
|
|
|
|
|
app_timer: core.Timer,
|
|
|
|
title_timer: core.Timer,
|
|
|
|
|
|
|
|
pipeline: *gpu.RenderPipeline,
|
|
|
|
mvp_uniform_buffer: *gpu.Buffer,
|
|
|
|
mvp_bind_group: *gpu.BindGroup,
|
|
|
|
vertex_count: u32,
|
|
|
|
vertex_buffer: *gpu.Buffer,
|
|
|
|
|
|
|
|
pub fn init(app: *App) !void {
|
|
|
|
try core.init(.{});
|
|
|
|
|
|
|
|
app.app_timer = try core.Timer.start();
|
|
|
|
app.title_timer = try core.Timer.start();
|
|
|
|
|
|
|
|
const shader_module = core.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
|
|
|
defer shader_module.release();
|
|
|
|
|
|
|
|
// Set up rendering pipeline.
|
|
|
|
app.pipeline = core.device.createRenderPipeline(&.{
|
|
|
|
.vertex = gpu.VertexState.init(.{
|
|
|
|
.module = shader_module,
|
|
|
|
.entry_point = "vertex_main",
|
|
|
|
.buffers = &.{
|
|
|
|
gpu.VertexBufferLayout.init(.{
|
|
|
|
.array_stride = @sizeOf(VertexData),
|
|
|
|
.step_mode = .vertex,
|
|
|
|
.attributes = &.{
|
|
|
|
.{ .format = .float32x3, .shader_location = 0, .offset = @offsetOf(VertexData, "position") },
|
|
|
|
.{ .format = .float32x3, .shader_location = 1, .offset = @offsetOf(VertexData, "color") },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
.fragment = &gpu.FragmentState.init(.{
|
|
|
|
.module = shader_module,
|
|
|
|
.entry_point = "frag_main",
|
|
|
|
.targets = &.{.{ .format = core.descriptor.format }},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set up uniform buffer and bind group.
|
|
|
|
app.mvp_uniform_buffer = core.device.createBuffer(&.{
|
|
|
|
.usage = .{ .copy_dst = true, .uniform = true },
|
|
|
|
.size = @sizeOf(zm.Mat),
|
|
|
|
.mapped_at_creation = .false,
|
|
|
|
});
|
|
|
|
|
|
|
|
app.mvp_bind_group = core.device.createBindGroup(
|
|
|
|
&gpu.BindGroup.Descriptor.init(.{
|
|
|
|
.layout = app.pipeline.getBindGroupLayout(0),
|
|
|
|
.entries = &.{
|
|
|
|
gpu.BindGroup.Entry.buffer(0, app.mvp_uniform_buffer, 0, @sizeOf(zm.Mat)),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set up vertex buffer, containing the vertex data we want to draw.
|
|
|
|
// `vertices` contains three separate triangles, each with a different color.
|
|
|
|
const vertices = [_]VertexData{
|
|
|
|
.{ .position = .{ -1.0, 0.5 + 0.25, 0.0 }, .color = .{ 1.0, 0.0, 0.0 } },
|
|
|
|
.{ .position = .{ -1.5, -0.5 + 0.25, 0.0 }, .color = .{ 1.0, 0.0, 0.0 } },
|
|
|
|
.{ .position = .{ -0.5, -0.5 + 0.25, 0.0 }, .color = .{ 1.0, 0.0, 0.0 } },
|
|
|
|
|
|
|
|
.{ .position = .{ 0.0, 0.5 - 0.25, 0.0 }, .color = .{ 0.0, 1.0, 0.0 } },
|
|
|
|
.{ .position = .{ -0.5, -0.5 - 0.25, 0.0 }, .color = .{ 0.0, 1.0, 0.0 } },
|
|
|
|
.{ .position = .{ 0.5, -0.5 - 0.25, 0.0 }, .color = .{ 0.0, 1.0, 0.0 } },
|
|
|
|
|
|
|
|
.{ .position = .{ 1.0, 0.5, 0.0 }, .color = .{ 0.0, 0.0, 1.0 } },
|
|
|
|
.{ .position = .{ 0.5, -0.5, 0.0 }, .color = .{ 0.0, 0.0, 1.0 } },
|
|
|
|
.{ .position = .{ 1.5, -0.5, 0.0 }, .color = .{ 0.0, 0.0, 1.0 } },
|
|
|
|
};
|
|
|
|
app.vertex_count = vertices.len;
|
|
|
|
app.vertex_buffer = core.device.createBuffer(&.{
|
|
|
|
.size = app.vertex_count * @sizeOf(VertexData),
|
|
|
|
.usage = .{ .vertex = true, .copy_dst = true },
|
|
|
|
.mapped_at_creation = .false,
|
|
|
|
});
|
|
|
|
// Upload vertex buffer to the GPU.
|
|
|
|
core.queue.writeBuffer(app.vertex_buffer, 0, &vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(app: *App) void {
|
|
|
|
// Using `defer` here, so we can specify them
|
|
|
|
// in the order they were created in `init`.
|
|
|
|
defer core.deinit();
|
|
|
|
defer app.pipeline.release();
|
|
|
|
defer app.mvp_uniform_buffer.release();
|
|
|
|
defer app.mvp_bind_group.release();
|
|
|
|
defer app.vertex_buffer.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(app: *App) !bool {
|
|
|
|
var iter = core.pollEvents();
|
|
|
|
while (iter.next()) |event| {
|
|
|
|
switch (event) {
|
|
|
|
.close => return true,
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a view matrix from the camera transform.
|
|
|
|
// This moves everything to be relative to the camera.
|
|
|
|
// TODO: Actually implement camera transform instead of hardcoding a look-at matrix.
|
|
|
|
// const view_matrix = zm.inverse(app.camera_transform);
|
|
|
|
const time = app.app_timer.read();
|
|
|
|
const x = @cos(time * std.math.tau / 10);
|
|
|
|
const y = @sin(time * std.math.tau / 10);
|
|
|
|
const view_matrix = zm.lookAtLh(vec(x, y, -2, 1), vec(0, 0, 0, 1), vec(0, 1, 0, 1));
|
|
|
|
|
|
|
|
// Set up a projection matrix using the size of the window.
|
|
|
|
// The perspective projection will make things further away appear smaller.
|
|
|
|
const width: f32 = @floatFromInt(core.descriptor.width);
|
|
|
|
const height: f32 = @floatFromInt(core.descriptor.height);
|
|
|
|
const field_of_view = std.math.degreesToRadians(f32, 45.0);
|
|
|
|
const proj_matrix = zm.perspectiveFovLh(field_of_view, width / height, 0.1, 10);
|
|
|
|
|
|
|
|
const view_proj_matrix = zm.mul(view_matrix, proj_matrix);
|
|
|
|
|
|
|
|
// Get back buffer texture to render to.
|
|
|
|
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
|
|
|
defer back_buffer_view.release();
|
|
|
|
// Once rendering is done (hence `defer`), swap back buffer to the front to display.
|
|
|
|
defer core.swap_chain.present();
|
|
|
|
|
|
|
|
const render_pass_info = gpu.RenderPassDescriptor.init(.{
|
|
|
|
.color_attachments = &.{.{
|
|
|
|
.view = back_buffer_view,
|
|
|
|
.clear_value = std.mem.zeroes(gpu.Color),
|
|
|
|
.load_op = .clear,
|
|
|
|
.store_op = .store,
|
|
|
|
}},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create a `WGPUCommandEncoder` which provides an interface for recording GPU commands.
|
|
|
|
const encoder = core.device.createCommandEncoder(null);
|
|
|
|
defer encoder.release();
|
|
|
|
|
|
|
|
encoder.writeBuffer(app.mvp_uniform_buffer, 0, &[_]zm.Mat{zm.transpose(view_proj_matrix)});
|
|
|
|
|
|
|
|
{
|
|
|
|
const pass = encoder.beginRenderPass(&render_pass_info);
|
|
|
|
defer pass.release();
|
|
|
|
defer pass.end();
|
|
|
|
|
|
|
|
pass.setPipeline(app.pipeline);
|
|
|
|
pass.setBindGroup(0, app.mvp_bind_group, &.{});
|
|
|
|
pass.setVertexBuffer(0, app.vertex_buffer, 0, app.vertex_count * @sizeOf(VertexData));
|
|
|
|
|
|
|
|
// Draw the vertices in `vertex_buffer`.
|
|
|
|
pass.draw(app.vertex_count, 1, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish recording commands, creating a `WGPUCommandBuffer`.
|
|
|
|
var command = encoder.finish(null);
|
|
|
|
defer command.release();
|
|
|
|
|
|
|
|
// Submit the command(s) to the GPU.
|
|
|
|
core.queue.submit(&.{command});
|
|
|
|
|
|
|
|
// Update the window title to show FPS and input frequency.
|
|
|
|
if (app.title_timer.read() >= 1.0) {
|
|
|
|
app.title_timer.reset();
|
|
|
|
try core.printTitle("Triangle [ {d}fps ] [ Input {d}hz ]", .{ core.frameRate(), core.inputRate() });
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|