gpu: fix BindGroup.Entry so that optional things are optional

This commit is contained in:
Silver 2022-04-07 00:17:21 +01:00 committed by Stephen Gutekanst
parent 35b38dfa96
commit f4c8a1908d
2 changed files with 28 additions and 31 deletions

View file

@ -30,11 +30,11 @@ pub inline fn setLabel(group: BindGroup, label: [:0]const u8) void {
pub const Entry = struct { pub const Entry = struct {
binding: u32, binding: u32,
buffer: Buffer, buffer: ?Buffer,
offset: u64, offset: u64,
size: u64, size: u64,
sampler: Sampler, sampler: ?Sampler,
texture_view: TextureView, texture_view: ?TextureView,
}; };
pub const Descriptor = struct { pub const Descriptor = struct {

View file

@ -378,36 +378,33 @@ const device_vtable = Device.VTable{
.createBindGroup = (struct { .createBindGroup = (struct {
pub fn createBindGroup(ptr: *anyopaque, descriptor: *const BindGroup.Descriptor) BindGroup { pub fn createBindGroup(ptr: *anyopaque, descriptor: *const BindGroup.Descriptor) BindGroup {
var few_entries: [16]c.WGPUBindGroupEntry = undefined; var few_entries: [16]c.WGPUBindGroupEntry = undefined;
const entries = if (descriptor.entries.len <= 8) blk: { const entries = if (descriptor.entries.len <= 8)
for (descriptor.entries) |entry, i| { few_entries[0..descriptor.entries.len]
few_entries[i] = c.WGPUBindGroupEntry{ else
.nextInChain = null, std.heap.page_allocator.alloc(c.WGPUBindGroupEntry, descriptor.entries.len) catch unreachable;
.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;
};
defer if (entries.len > 8) std.heap.page_allocator.free(entries); 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{ const desc = c.WGPUBindGroupDescriptor{
.nextInChain = null, .nextInChain = null,
.label = if (descriptor.label) |l| l else null, .label = if (descriptor.label) |l| l else null,