From 510c7de9f217d10949556a0a345f92090e329236 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 11 Mar 2022 21:00:01 -0700 Subject: [PATCH] gpu: implement CommandEncoder.beginRenderPass Signed-off-by: Stephen Gutekanst --- gpu/src/CommandEncoder.zig | 9 ++++++++- gpu/src/Device.zig | 1 + gpu/src/NativeInstance.zig | 31 +++++++++++++++++++++++++++++-- gpu/src/SwapChain.zig | 3 +++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/gpu/src/CommandEncoder.zig b/gpu/src/CommandEncoder.zig index 3ee71d00..e3e02938 100644 --- a/gpu/src/CommandEncoder.zig +++ b/gpu/src/CommandEncoder.zig @@ -1,3 +1,5 @@ +const RenderPassEncoder = @import("RenderPassEncoder.zig"); + const CommandEncoder = @This(); /// The type erased pointer to the CommandEncoder implementation @@ -10,7 +12,7 @@ pub const VTable = struct { release: fn (ptr: *anyopaque) void, // TODO: // WGPU_EXPORT WGPUComputePassEncoder wgpuCommandEncoderBeginComputePass(WGPUCommandEncoder commandEncoder, WGPUComputePassDescriptor const * descriptor); - // WGPU_EXPORT WGPURenderPassEncoder wgpuCommandEncoderBeginRenderPass(WGPUCommandEncoder commandEncoder, WGPURenderPassDescriptor const * descriptor); + beginRenderPass: fn (ptr: *anyopaque, descriptor: *const RenderPassEncoder.Descriptor) RenderPassEncoder, // WGPU_EXPORT void wgpuCommandEncoderClearBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer buffer, uint64_t offset, uint64_t size); // WGPU_EXPORT void wgpuCommandEncoderCopyBufferToBuffer(WGPUCommandEncoder commandEncoder, WGPUBuffer source, uint64_t sourceOffset, WGPUBuffer destination, uint64_t destinationOffset, uint64_t size); // WGPU_EXPORT void wgpuCommandEncoderCopyBufferToTexture(WGPUCommandEncoder commandEncoder, WGPUImageCopyBuffer const * source, WGPUImageCopyTexture const * destination, WGPUExtent3D const * copySize); @@ -40,6 +42,10 @@ pub inline fn setLabel(enc: CommandEncoder, label: [:0]const u8) void { enc.vtable.setLabel(enc.ptr, label); } +pub inline fn beginRenderPass(enc: CommandEncoder, descriptor: *const RenderPassEncoder.Descriptor) RenderPassEncoder { + return enc.vtable.beginRenderPass(enc.ptr, descriptor); +} + pub const Descriptor = struct { label: ?[*:0]const u8 = null, }; @@ -48,5 +54,6 @@ test "syntax" { _ = VTable; _ = reference; _ = release; + _ = beginRenderPass; _ = Descriptor; } diff --git a/gpu/src/Device.zig b/gpu/src/Device.zig index e36f99bb..fba9cbaa 100644 --- a/gpu/src/Device.zig +++ b/gpu/src/Device.zig @@ -110,6 +110,7 @@ test "syntax" { _ = createShaderModule; _ = nativeCreateSwapChain; _ = destroy; + _ = createCommandEncoder; _ = createRenderPipeline; _ = Descriptor; _ = LostReason; diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index a24237f4..555a4ff1 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -382,7 +382,7 @@ const device_vtable = Device.VTable{ pub fn createCommandEncoder(ptr: *anyopaque, descriptor: ?*const CommandEncoder.Descriptor) CommandEncoder { const desc: ?*c.WGPUCommandEncoderDescriptor = if (descriptor) |d| &.{ .nextInChain = null, - .label = d.label, + .label = if (d.label) |l| l else "", } else null; return wrapCommandEncoder(c.wgpuDeviceCreateCommandEncoder(@ptrCast(c.WGPUDevice, ptr), desc)); } @@ -430,7 +430,7 @@ inline fn convertRenderPipelineDescriptor( return c.WGPURenderPipelineDescriptor{ .nextInChain = null, - .label = d.label, + .label = if (d.label) |l| l else null, .layout = if (d.layout) |v| @ptrCast(c.WGPUPipelineLayout, v.ptr) else null, .vertex = c.WGPUVertexState{ .nextInChain = null, @@ -996,6 +996,33 @@ const command_encoder_vtable = CommandEncoder.VTable{ c.wgpuCommandEncoderSetLabel(@ptrCast(c.WGPUCommandEncoder, ptr), label); } }).setLabel, + .beginRenderPass = (struct { + pub fn beginRenderPass(ptr: *anyopaque, d: *const RenderPassEncoder.Descriptor) RenderPassEncoder { + 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), + .timestampWriteCount = 0, // TODO + .timestampWrites = null, // TODO + }; + return wrapRenderPassEncoder(c.wgpuCommandEncoderBeginRenderPass(@ptrCast(c.WGPUCommandEncoder, ptr), &desc)); + } + }).beginRenderPass, }; fn wrapComputePassEncoder(enc: c.WGPUComputePassEncoder) ComputePassEncoder { diff --git a/gpu/src/SwapChain.zig b/gpu/src/SwapChain.zig index e8ac14f8..e67e042b 100644 --- a/gpu/src/SwapChain.zig +++ b/gpu/src/SwapChain.zig @@ -71,5 +71,8 @@ test "syntax" { _ = VTable; _ = reference; _ = release; + _ = configure; + _ = getCurrentTextureView; + _ = present; _ = Descriptor; }