From f4c8a1908d768ac90431c1b5521796af8cc9752b Mon Sep 17 00:00:00 2001 From: Silver Date: Thu, 7 Apr 2022 00:17:21 +0100 Subject: [PATCH] gpu: fix BindGroup.Entry so that optional things are optional --- gpu/src/BindGroup.zig | 6 ++--- gpu/src/NativeInstance.zig | 53 ++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/gpu/src/BindGroup.zig b/gpu/src/BindGroup.zig index b082e775..8f81356d 100644 --- a/gpu/src/BindGroup.zig +++ b/gpu/src/BindGroup.zig @@ -30,11 +30,11 @@ pub inline fn setLabel(group: BindGroup, label: [:0]const u8) void { pub const Entry = struct { binding: u32, - buffer: Buffer, + buffer: ?Buffer, offset: u64, size: u64, - sampler: Sampler, - texture_view: TextureView, + sampler: ?Sampler, + texture_view: ?TextureView, }; pub const Descriptor = struct { diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index 62acc7d8..5913e5a9 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -378,36 +378,33 @@ const device_vtable = Device.VTable{ .createBindGroup = (struct { pub fn createBindGroup(ptr: *anyopaque, descriptor: *const BindGroup.Descriptor) BindGroup { var few_entries: [16]c.WGPUBindGroupEntry = undefined; - const entries = if (descriptor.entries.len <= 8) blk: { - for (descriptor.entries) |entry, i| { - few_entries[i] = c.WGPUBindGroupEntry{ - .nextInChain = null, - .binding = entry.binding, - .buffer = @ptrCast(c.WGPUBuffer, entry.buffer.ptr), - .offset = entry.offset, - .size = entry.size, - .sampler = @ptrCast(c.WGPUSampler, entry.sampler.ptr), - .textureView = @ptrCast(c.WGPUTextureView, entry.texture_view.ptr), - }; - } - break :blk few_entries[0..descriptor.entries.len]; - } else blk: { - const mem = std.heap.page_allocator.alloc(c.WGPUBindGroupEntry, descriptor.entries.len) catch unreachable; - for (descriptor.entries) |entry, i| { - mem[i] = c.WGPUBindGroupEntry{ - .nextInChain = null, - .binding = entry.binding, - .buffer = @ptrCast(c.WGPUBuffer, entry.buffer.ptr), - .offset = entry.offset, - .size = entry.size, - .sampler = @ptrCast(c.WGPUSampler, entry.sampler.ptr), - .textureView = @ptrCast(c.WGPUTextureView, entry.texture_view.ptr), - }; - } - break :blk mem; - }; + const entries = if (descriptor.entries.len <= 8) + few_entries[0..descriptor.entries.len] + else + std.heap.page_allocator.alloc(c.WGPUBindGroupEntry, descriptor.entries.len) catch unreachable; defer if (entries.len > 8) std.heap.page_allocator.free(entries); + for (descriptor.entries) |entry, i| { + entries[i] = c.WGPUBindGroupEntry{ + .nextInChain = null, + .binding = entry.binding, + .buffer = if (entry.buffer) |buf| + @ptrCast(c.WGPUBuffer, buf.ptr) + else + null, + .offset = entry.offset, + .size = entry.size, + .sampler = if (entry.sampler) |samp| + @ptrCast(c.WGPUSampler, samp.ptr) + else + null, + .textureView = if (entry.texture_view) |tex| + @ptrCast(c.WGPUTextureView, tex.ptr) + else + null, + }; + } + const desc = c.WGPUBindGroupDescriptor{ .nextInChain = null, .label = if (descriptor.label) |l| l else null,