From 9f1775aa7c150cc8a069e9487e5c6b0e28b630cd Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 18 Mar 2022 20:03:38 -0700 Subject: [PATCH] gpu: correct feature storage location Signed-off-by: Stephen Gutekanst --- gpu/src/Adapter.zig | 1 + gpu/src/Device.zig | 3 +++ gpu/src/NativeInstance.zig | 22 ++++++++++++---------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/gpu/src/Adapter.zig b/gpu/src/Adapter.zig index bad044b6..ef54bfc8 100644 --- a/gpu/src/Adapter.zig +++ b/gpu/src/Adapter.zig @@ -27,6 +27,7 @@ const Adapter = @This(); /// The features which can be used to create devices on this adapter. features: []Feature, +_features: [std.enums.values(Feature).len]Feature = undefined, /// The best limits which can be used to create devices on this adapter. /// diff --git a/gpu/src/Device.zig b/gpu/src/Device.zig index ec98ee0d..cb0dc156 100644 --- a/gpu/src/Device.zig +++ b/gpu/src/Device.zig @@ -6,6 +6,8 @@ //! //! https://gpuweb.github.io/gpuweb/#devices //! https://gpuweb.github.io/gpuweb/#gpuadapter +const std = @import("std"); + const Feature = @import("enums.zig").Feature; const ErrorType = @import("enums.zig").ErrorType; const ErrorFilter = @import("enums.zig").ErrorFilter; @@ -34,6 +36,7 @@ const Device = @This(); /// The features supported by the device (i.e. the ones with which it was created). features: []Feature, +_features: [std.enums.values(Feature).len]Feature = undefined, /// The limits supported by the device (which are exactly the ones with which it was created). limits: Limits, diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index 80bf8bd1..0e0aa89b 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -225,14 +225,11 @@ fn wrapAdapter(adapter: c.WGPUAdapter) Adapter { .backend_type = @intToEnum(Adapter.BackendType, c_props.backendType), }; - var features: [std.enums.values(Feature).len]c.WGPUFeatureName = undefined; - const features_len = c.wgpuAdapterEnumerateFeatures(adapter.?, &features[0]); - var supported_limits: c.WGPUSupportedLimits = undefined; if (!c.wgpuAdapterGetLimits(adapter.?, &supported_limits)) @panic("failed to get adapter limits (this is a bug in mach/gpu)"); - return .{ - .features = @bitCast([]Feature, features[0..features_len]), + var wrapped = Adapter{ + .features = undefined, .limits = @bitCast(Limits, supported_limits.limits), .properties = properties, @@ -242,6 +239,10 @@ fn wrapAdapter(adapter: c.WGPUAdapter) Adapter { .ptr = adapter.?, .vtable = &adapter_vtable, }; + + const features_len = c.wgpuAdapterEnumerateFeatures(adapter.?, @ptrCast(*c.WGPUFeatureName, &wrapped._features[0])); + wrapped.features = wrapped._features[0..features_len]; + return wrapped; } const adapter_vtable = Adapter.VTable{ @@ -303,18 +304,19 @@ const adapter_vtable = Adapter.VTable{ }; fn wrapDevice(device: c.WGPUDevice) Device { - var features: [std.enums.values(Feature).len]c.WGPUFeatureName = undefined; - const features_len = c.wgpuDeviceEnumerateFeatures(device.?, &features[0]); - var supported_limits: c.WGPUSupportedLimits = undefined; if (!c.wgpuDeviceGetLimits(device.?, &supported_limits)) @panic("failed to get device limits (this is a bug in mach/gpu)"); - return .{ - .features = @bitCast([]Feature, features[0..features_len]), + var wrapped = Device{ + .features = undefined, .limits = @bitCast(Limits, supported_limits.limits), .ptr = device.?, .vtable = &device_vtable, }; + + const features_len = c.wgpuDeviceEnumerateFeatures(device.?, @ptrCast(*c.WGPUFeatureName, &wrapped._features[0])); + wrapped.features = wrapped._features[0..features_len]; + return wrapped; } const device_vtable = Device.VTable{