gpu: fix alignment issues with getMappedRange, getConstMappedRange (#223)

fixes alignment issues with getMappedRange, getConstMappedRange

Prior to this change `getMappedRange` and `getConstMappedRange` would not handle alignment to `COPY_BUFFER_ALIGNMENT` for users, and so invocation could result in an unhelpful error message:

```
    thread 254201 panic: attempt to use null value
    .../mach/gpu/src/NativeInstance.zig:1721:42: 0x480747
    in .gpu.NativeInstance.buffer_vtable.getMappedRange (game)
            return @ptrCast([*c]u8, range.?)[0..size];
					 ^
```

To address this:

1. Ensure we always request a 4-byte aligned buffer when `createBuffer` is called.
2. Ensure we always map 4-byte aligned buffers.

Co-authored-by: d3m1gd <mach+d3m1gd@users.noreply.github.com>
This commit is contained in:
d3m1gd 2022-04-17 00:16:20 +07:00 committed by GitHub
parent c11d818c89
commit 05853da233
Failed to generate hash of commit
2 changed files with 8 additions and 4 deletions

View file

@ -37,12 +37,14 @@ pub inline fn destroy(buf: Buffer) void {
} }
pub inline fn getConstMappedRange(buf: Buffer, comptime T: type, offset: usize, len: usize) []const T { pub inline fn getConstMappedRange(buf: Buffer, comptime T: type, offset: usize, len: usize) []const T {
const data = buf.vtable.getConstMappedRange(buf.ptr, offset, @sizeOf(T) * len); const size = @sizeOf(T) * len;
return @ptrCast([*]const T, data.ptr)[0..len]; const data = buf.vtable.getConstMappedRange(buf.ptr, offset, size + size % 4);
return @ptrCast([*]const T, @alignCast(@alignOf(T), data.ptr))[0..len];
} }
pub inline fn getMappedRange(buf: Buffer, comptime T: type, offset: usize, len: usize) []T { pub inline fn getMappedRange(buf: Buffer, comptime T: type, offset: usize, len: usize) []T {
const data = buf.vtable.getMappedRange(buf.ptr, offset, @sizeOf(T) * len); const size = @sizeOf(T) * len;
const data = buf.vtable.getMappedRange(buf.ptr, offset, size + size % 4);
return @ptrCast([*]T, @alignCast(@alignOf(T), data.ptr))[0..len]; return @ptrCast([*]T, @alignCast(@alignOf(T), data.ptr))[0..len];
} }

View file

@ -177,7 +177,9 @@ pub inline fn destroy(device: Device) void {
} }
pub inline fn createBuffer(device: Device, descriptor: *const Buffer.Descriptor) Buffer { pub inline fn createBuffer(device: Device, descriptor: *const Buffer.Descriptor) Buffer {
return device.vtable.createBuffer(device.ptr, descriptor); var local_descriptor = descriptor.*;
local_descriptor.size += local_descriptor.size % 4;
return device.vtable.createBuffer(device.ptr, &local_descriptor);
} }
pub inline fn createCommandEncoder(device: Device, descriptor: ?*const CommandEncoder.Descriptor) CommandEncoder { pub inline fn createCommandEncoder(device: Device, descriptor: ?*const CommandEncoder.Descriptor) CommandEncoder {