From f849e034439ef0ffcfc04dce9151b12b130b0f81 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 7 Mar 2022 11:28:25 -0700 Subject: [PATCH] gpu: implement Adapter properties Signed-off-by: Stephen Gutekanst --- gpu/src/Adapter.zig | 57 ++++++++++++++++++++++++++------------ gpu/src/NativeInstance.zig | 25 ++++++++--------- gpu/src/TODO | 12 -------- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/gpu/src/Adapter.zig b/gpu/src/Adapter.zig index 98cfbaed..0fe76e9a 100644 --- a/gpu/src/Adapter.zig +++ b/gpu/src/Adapter.zig @@ -40,6 +40,9 @@ limits: SupportedLimits, /// Always false on native implementations of WebGPU (TODO: why is this not queryable in Dawn?) fallback: bool, +// TODO: docs +properties: Properties, + // The type erased pointer to the Adapter implementation ptr: *anyopaque, vtable: *const VTable, @@ -60,22 +63,40 @@ pub fn hasFeature(adapter: Adapter, feature: FeatureName) bool { return false; } -// TODO: -// typedef struct WGPUAdapterProperties { -// WGPUChainedStructOut * nextInChain; -// uint32_t vendorID; -// uint32_t deviceID; -// char const * name; -// char const * driverDescription; -// WGPUAdapterType adapterType; -// WGPUBackendType backendType; -// } WGPUAdapterProperties; +// TODO: docs +pub const Properties = struct { + vendor_id: u32, + device_id: u32, + name: []const u8, + driver_description: []const u8, + adapter_type: Type, + backend_type: BackendType, +}; -// TODO: -// typedef enum WGPUAdapterType { -// WGPUAdapterType_DiscreteGPU = 0x00000000, -// WGPUAdapterType_IntegratedGPU = 0x00000001, -// WGPUAdapterType_CPU = 0x00000002, -// WGPUAdapterType_Unknown = 0x00000003, -// WGPUAdapterType_Force32 = 0x7FFFFFFF -// } WGPUAdapterType; +// TODO: docs +pub const Type = enum(u32) { + discrete_gpu, + integrated_gpu, + cpu, + unknown, +}; + +// TODO: docs +pub const BackendType = enum(u32) { + nul, + webgpu, + d3d11, + d3d12, + metal, + vulkan, + opengl, + opengles, +}; + +test "syntax" { + _ = VTable; + _ = hasFeature; + _ = Properties; + _ = Type; + _ = BackendType; +} diff --git a/gpu/src/NativeInstance.zig b/gpu/src/NativeInstance.zig index 9f5399f1..cf7ab628 100644 --- a/gpu/src/NativeInstance.zig +++ b/gpu/src/NativeInstance.zig @@ -84,18 +84,6 @@ const interface_vtable = Interface.VTable{ }).requestAdapter, }; -// TODO: -// typedef void (*WGPURequestAdapterCallback)(WGPURequestAdapterStatus status, WGPUAdapter adapter, char const * message, void * userdata); -// WGPU_EXPORT void wgpuInstanceRequestAdapter(WGPUInstance instance, WGPURequestAdapterOptions const * options, WGPURequestAdapterCallback callback, void * userdata); - -// typedef enum WGPURequestAdapterStatus { -// WGPURequestAdapterStatus_Success = 0x00000000, -// WGPURequestAdapterStatus_Unavailable = 0x00000001, -// WGPURequestAdapterStatus_Error = 0x00000002, -// WGPURequestAdapterStatus_Unknown = 0x00000003, -// WGPURequestAdapterStatus_Force32 = 0x7FFFFFFF -// } WGPURequestAdapterStatus; - /// Returns the gpu.Interface for interacting with this native instance. pub fn interface(native: *NativeInstance) Interface { return .{ @@ -191,17 +179,28 @@ const surface_vtable = Surface.VTable{ }; fn wrapAdapter(adapter: c.WGPUAdapter) Adapter { + var c_props: c.WGPUAdapterProperties = undefined; + c.wgpuAdapterGetProperties(adapter, &c_props); + const properties = Adapter.Properties{ + .vendor_id = c_props.vendorID, + .device_id = c_props.deviceID, + .name = std.mem.span(c_props.name), + .driver_description = std.mem.span(c_props.driverDescription), + .adapter_type = @intToEnum(Adapter.Type, c_props.adapterType), + .backend_type = @intToEnum(Adapter.BackendType, c_props.backendType), + }; + // TODO: implement Adapter interface: // WGPU_EXPORT size_t wgpuAdapterEnumerateFeatures(WGPUAdapter adapter, WGPUFeatureName * features); // WGPU_EXPORT bool wgpuAdapterHasFeature(WGPUAdapter adapter, WGPUFeatureName feature); // WGPU_EXPORT bool wgpuAdapterGetLimits(WGPUAdapter adapter, WGPUSupportedLimits * limits); - // WGPU_EXPORT void wgpuAdapterGetProperties(WGPUAdapter adapter, WGPUAdapterProperties * properties); return .{ // TODO: .features = undefined, // TODO: .limits = undefined, + .properties = properties, // TODO: why is fallback not queryable on Dawn? .fallback = false, diff --git a/gpu/src/TODO b/gpu/src/TODO index 24596905..bf065385 100644 --- a/gpu/src/TODO +++ b/gpu/src/TODO @@ -43,18 +43,6 @@ typedef enum WGPUAlphaMode { WGPUAlphaMode_Force32 = 0x7FFFFFFF } WGPUAlphaMode; -typedef enum WGPUBackendType { - WGPUBackendType_Null = 0x00000000, - WGPUBackendType_WebGPU = 0x00000001, - WGPUBackendType_D3D11 = 0x00000002, - WGPUBackendType_D3D12 = 0x00000003, - WGPUBackendType_Metal = 0x00000004, - WGPUBackendType_Vulkan = 0x00000005, - WGPUBackendType_OpenGL = 0x00000006, - WGPUBackendType_OpenGLES = 0x00000007, - WGPUBackendType_Force32 = 0x7FFFFFFF -} WGPUBackendType; - typedef enum WGPUBlendFactor { WGPUBlendFactor_Zero = 0x00000000, WGPUBlendFactor_One = 0x00000001,