commit 90673dac563c9dd955aa7c15b186d7fb028c8735
parent e480b0f0bf1c8a42199140025c933d9dd5b7eaba
Author: Christian Ermann <christianermann@gmail.com>
Date: Sat, 27 Apr 2024 16:06:06 -0400
Add RenderPipeline interface
Diffstat:
M | src/main.zig | | | 48 | +++++++++++++++++++++++++++++++++++++----------- |
1 file changed, 37 insertions(+), 11 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -49,7 +49,6 @@ const App = struct {
device: *gpu.Device,
queue: *gpu.Queue,
swap_chain: *AppSwapChain,
- pipeline: DefaultRenderPipeline,
pub fn init(app: *App, allocator: std.mem.Allocator) !void {
try gpu.Impl.init(allocator, .{});
@@ -97,9 +96,6 @@ const App = struct {
app.swap_chain = try app.createSwapChain(allocator);
app.window.setUserPointer(app.swap_chain);
-
- app.pipeline = undefined;
- app.pipeline.init(app);
}
pub fn deinit(app: *App) void {
@@ -199,7 +195,7 @@ const App = struct {
return sc.gpu_swap_chain.?;
}
- pub fn frame(app: *App) !void {
+ pub fn frame(app: *App, pipelines: []const RenderPipeline) !void {
app.device.tick();
const swap_chain = app.getCurrentSwapChain();
defer swap_chain.present();
@@ -227,8 +223,9 @@ const App = struct {
defer pass.release();
defer pass.end();
- pass.setPipeline(app.pipeline.pipeline);
- pass.draw(3, 1, 0, 0);
+ for (pipelines) |pipeline| {
+ pipeline.frame(pass);
+ }
}
{
@@ -248,10 +245,19 @@ const AppSwapChain = struct {
target_descriptor: gpu.SwapChain.Descriptor,
};
+const RenderPipeline = struct {
+ ptr: *anyopaque,
+ frameFn: *const fn (ptr: *anyopaque, pass: *gpu.RenderPassEncoder) void,
+
+ fn frame(self: *const RenderPipeline, pass: *gpu.RenderPassEncoder) void {
+ return self.frameFn(self.ptr, pass);
+ }
+};
+
const DefaultRenderPipeline = struct {
- pipeline: *gpu.RenderPipeline,
+ gpu_pipeline: *gpu.RenderPipeline,
- pub fn init(self: *DefaultRenderPipeline, app: *App) void {
+ pub fn init(app: *App) DefaultRenderPipeline {
const vs =
\\ @vertex fn main(
\\ @builtin(vertex_index) VertexIndex : u32
@@ -301,7 +307,23 @@ const DefaultRenderPipeline = struct {
.multisample = .{},
.primitive = .{},
};
- self.pipeline = app.device.createRenderPipeline(&pipeline_descriptor);
+
+ return .{
+ .gpu_pipeline = app.device.createRenderPipeline(&pipeline_descriptor),
+ };
+ }
+
+ pub fn frame(ptr: *anyopaque, pass: *gpu.RenderPassEncoder) void {
+ const self: *DefaultRenderPipeline = @ptrCast(@alignCast(ptr));
+ pass.setPipeline(self.gpu_pipeline);
+ pass.draw(3, 1, 0, 0);
+ }
+
+ pub fn pipeline(self: *DefaultRenderPipeline) RenderPipeline {
+ return .{
+ .ptr = self,
+ .frameFn = frame,
+ };
}
};
@@ -313,9 +335,13 @@ pub fn main() !void {
try app.init(allocator);
defer app.deinit();
+ var drp = DefaultRenderPipeline.init(&app);
+
+ const pipelines = [_]RenderPipeline{drp.pipeline()};
+
while (!app.window.shouldClose()) {
glfw.pollEvents();
- try app.frame();
+ try app.frame(&pipelines);
}
}