From c3bb1683a6219984f48cf97f52a118ba09ceb0ed Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Tue, 16 Aug 2022 17:16:52 -0700 Subject: [PATCH] gpu: add BindGroup.Descriptor slice helper Signed-off-by: Stephen Gutekanst --- gpu/README.md | 5 +++++ gpu/src/bind_group.zig | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/gpu/README.md b/gpu/README.md index 0388cdec..e0910de3 100644 --- a/gpu/README.md +++ b/gpu/README.md @@ -163,6 +163,11 @@ The slice helpers are: * `RenderBundleEncoder.setBindGroup` * `RenderPassEncoder.setBindGroup` +And, to initialize data structures with slices in them, the following helpers are provided: + +* `BindGroupLayout.Descriptor.init` +* `BindGroup.Descriptor.init` + ### Typed callbacks Most WebGPU callbacks provide a way to provide a `userdata: *anyopaque` pointer to the callback for context. We alter these APIs to expose a typed context pointer instead (again, the original API is always available via the `gpu.Impl` type should you want it): diff --git a/gpu/src/bind_group.zig b/gpu/src/bind_group.zig index a1aa9ef4..a1bfc6c8 100644 --- a/gpu/src/bind_group.zig +++ b/gpu/src/bind_group.zig @@ -51,6 +51,22 @@ pub const BindGroup = opaque { // TODO: slice helper entry_count: u32 = 0, entries: ?[*]const Entry = null, + + /// Provides a slightly friendlier Zig API to initialize this structure. + pub fn init(v: struct { + next_in_chain: ?*const ChainedStruct = null, + label: ?[*:0]const u8 = null, + layout: *BindGroupLayout, + entries: ?[]const Entry = null, + }) Descriptor { + return .{ + .next_in_chain = v.next_in_chain, + .label = v.label, + .layout = v.layout, + .entry_count = if (v.entries) |e| @intCast(u32, e.len) else 0, + .entries = if (v.entries) |e| e.ptr else null, + }; + } }; pub inline fn setLabel(bind_group: *BindGroup, label: [*:0]const u8) void {