render-zig

A 3D rendering engine written in Zig
git clone git://git.christianermann.dev/render-zig
Log | Files | Refs

render_pass.zig (2122B)


      1 const std = @import("std");
      2 const gpu = @import("mach_gpu");
      3 
      4 const RenderPipeline = @import("render_pipeline.zig").RenderPipeline;
      5 
      6 pub const RenderPass = struct {
      7     ptr: *anyopaque,
      8     frameFn: *const fn (
      9         ptr: *anyopaque,
     10         encoder: *gpu.CommandEncoder,
     11         current_view: *gpu.TextureView,
     12     ) void,
     13 
     14     pub fn frame(
     15         self: *const RenderPass,
     16         encoder: *gpu.CommandEncoder,
     17         current_view: *gpu.TextureView,
     18     ) void {
     19         return self.frameFn(self.ptr, encoder, current_view);
     20     }
     21 };
     22 
     23 pub const ForwardScreenPass = struct {
     24     depth: gpu.RenderPassDepthStencilAttachment,
     25     pipelines: []const RenderPipeline,
     26 
     27     const Self = @This();
     28 
     29     pub const Args = struct {
     30         depth: *gpu.TextureView,
     31         pipelines: []const RenderPipeline,
     32     };
     33 
     34     pub fn init(args: Args) Self {
     35         return .{
     36             .depth = .{
     37                 .view = args.depth,
     38                 .depth_clear_value = 1.0,
     39                 .depth_load_op = .clear,
     40                 .depth_store_op = .store,
     41             },
     42             .pipelines = args.pipelines,
     43         };
     44     }
     45 
     46     pub fn frame(
     47         ptr: *anyopaque,
     48         encoder: *gpu.CommandEncoder,
     49         current_view: *gpu.TextureView,
     50     ) void {
     51         const self: *Self = @ptrCast(@alignCast(ptr));
     52 
     53         const color_attachment = gpu.RenderPassColorAttachment{
     54             .view = current_view,
     55             .resolve_target = null,
     56             .clear_value = gpu.Color{ .r = 1, .g = 1, .b = 1, .a = 1 },
     57             .load_op = .clear,
     58             .store_op = .store,
     59         };
     60         const descriptor = gpu.RenderPassDescriptor.init(.{
     61             .color_attachments = &.{color_attachment},
     62             .depth_stencil_attachment = &self.depth,
     63         });
     64         const pass = encoder.beginRenderPass(&descriptor);
     65         defer pass.release();
     66         defer pass.end();
     67 
     68         for (self.pipelines) |pipeline| {
     69             pipeline.frame(pass);
     70         }
     71     }
     72 
     73     pub fn renderPass(self: *Self) RenderPass {
     74         return .{
     75             .ptr = self,
     76             .frameFn = frame,
     77         };
     78     }
     79 };