From f5a5c7eaa7a8eac18f806a131f299039563212dc Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 11 Mar 2022 22:03:22 -0700 Subject: [PATCH] gpu: implement RenderPassColorAttachment Signed-off-by: Stephen Gutekanst --- gpu/src/NativeInstance.zig | 73 +++++++++++++++++++++++++++-------- gpu/src/RenderPassEncoder.zig | 6 +-- gpu/src/structs.zig | 2 +- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index 555a4ff1..81eda2a9 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -998,25 +998,66 @@ const command_encoder_vtable = CommandEncoder.VTable{ }).setLabel, .beginRenderPass = (struct { pub fn beginRenderPass(ptr: *anyopaque, d: *const RenderPassEncoder.Descriptor) RenderPassEncoder { + var few_color_attachments: [8]c.WGPURenderPassColorAttachment = undefined; + const color_attachments = if (d.color_attachments.len <= 8) blk: { + for (d.color_attachments) |v, i| { + few_color_attachments[i] = c.WGPURenderPassColorAttachment{ + .view = @ptrCast(c.WGPUTextureView, v.view.ptr), + .resolveTarget = if (v.resolve_target) |t| @ptrCast(c.WGPUTextureView, t.ptr) else null, + .loadOp = @enumToInt(v.load_op), + .storeOp = @enumToInt(v.load_op), + .clearValue = @bitCast(c.WGPUColor, v.clear_value), + // deprecated: + .clearColor = c.WGPUColor{ + .r = std.math.nan(f32), + .g = std.math.nan(f32), + .b = std.math.nan(f32), + .a = std.math.nan(f32), + }, + }; + } + break :blk few_color_attachments[0..d.color_attachments.len]; + } else blk: { + const mem = std.heap.page_allocator.alloc(c.WGPURenderPassColorAttachment, d.color_attachments.len) catch unreachable; + for (d.color_attachments) |v, i| { + mem[i] = c.WGPURenderPassColorAttachment{ + .view = @ptrCast(c.WGPUTextureView, v.view.ptr), + .resolveTarget = if (v.resolve_target) |t| @ptrCast(c.WGPUTextureView, t.ptr) else null, + .loadOp = @enumToInt(v.load_op), + .storeOp = @enumToInt(v.load_op), + .clearValue = @bitCast(c.WGPUColor, v.clear_value), + // deprecated: + .clearColor = c.WGPUColor{ + .r = std.math.nan(f32), + .g = std.math.nan(f32), + .b = std.math.nan(f32), + .a = std.math.nan(f32), + }, + }; + } + break :blk mem; + }; + defer if (d.color_attachments.len > 8) std.heap.page_allocator.free(color_attachments); + const desc = c.WGPURenderPassDescriptor{ .nextInChain = null, .label = if (d.label) |l| l else null, - .colorAttachmentCount = 0, // TODO - .colorAttachments = null, // TODO - .depthStencilAttachment = &c.WGPURenderPassDepthStencilAttachment{ - .view = @ptrCast(c.WGPUTextureView, d.depth_stencil_attachment.view.ptr), - .depthLoadOp = @enumToInt(d.depth_stencil_attachment.depth_load_op), - .depthStoreOp = @enumToInt(d.depth_stencil_attachment.depth_store_op), - .clearDepth = d.depth_stencil_attachment.clear_depth, - .depthClearValue = d.depth_stencil_attachment.depth_clear_value, - .depthReadOnly = d.depth_stencil_attachment.depth_read_only, - .stencilLoadOp = @enumToInt(d.depth_stencil_attachment.stencil_load_op), - .stencilStoreOp = @enumToInt(d.depth_stencil_attachment.stencil_store_op), - .clearStencil = d.depth_stencil_attachment.clear_stencil, - .stencilClearValue = d.depth_stencil_attachment.stencil_clear_value, - .stencilReadOnly = d.depth_stencil_attachment.stencil_read_only, - }, - .occlusionQuerySet = @ptrCast(c.WGPUQuerySet, d.occlusion_query_set.ptr), + .colorAttachmentCount = @intCast(u32, color_attachments.len), + .colorAttachments = &color_attachments[0], + .depthStencilAttachment = if (d.depth_stencil_attachment) |v| &c.WGPURenderPassDepthStencilAttachment{ + .view = @ptrCast(c.WGPUTextureView, v.view.ptr), + .depthLoadOp = @enumToInt(v.depth_load_op), + .depthStoreOp = @enumToInt(v.depth_store_op), + .clearDepth = v.clear_depth, + .depthClearValue = v.depth_clear_value, + .depthReadOnly = v.depth_read_only, + .stencilLoadOp = @enumToInt(v.stencil_load_op), + .stencilStoreOp = @enumToInt(v.stencil_store_op), + .clearStencil = v.clear_stencil, + .stencilClearValue = v.stencil_clear_value, + .stencilReadOnly = v.stencil_read_only, + } else null, + .occlusionQuerySet = if (d.occlusion_query_set) |v| @ptrCast(c.WGPUQuerySet, v.ptr) else null, .timestampWriteCount = 0, // TODO .timestampWrites = null, // TODO }; diff --git a/gpu/src/RenderPassEncoder.zig b/gpu/src/RenderPassEncoder.zig index 70589676..001b935c 100644 --- a/gpu/src/RenderPassEncoder.zig +++ b/gpu/src/RenderPassEncoder.zig @@ -53,9 +53,9 @@ pub inline fn setLabel(pass: RenderPassEncoder, label: [:0]const u8) void { pub const Descriptor = struct { label: ?[*:0]const u8 = null, color_attachments: []const RenderPassColorAttachment, - depth_stencil_attachment: *const RenderPassDepthStencilAttachment, - occlusion_query_set: QuerySet, - timestamp_writes: []RenderPassTimestampWrite, + depth_stencil_attachment: ?*const RenderPassDepthStencilAttachment, + occlusion_query_set: ?QuerySet = null, + timestamp_writes: ?[]RenderPassTimestampWrite = null, }; test "syntax" { diff --git a/gpu/src/structs.zig b/gpu/src/structs.zig index e2dbc51c..4a60405b 100644 --- a/gpu/src/structs.zig +++ b/gpu/src/structs.zig @@ -110,7 +110,7 @@ pub const RenderPassDepthStencilAttachment = struct { pub const RenderPassColorAttachment = struct { view: TextureView, - resolve_target: TextureView, + resolve_target: ?TextureView, load_op: LoadOp, store_op: StoreOp, clear_value: Color,