Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPU: Implement wgpuAdapterGetInfo on GPUAdapter.info #22031

Merged
merged 8 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/generated_struct_info32.json
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,18 @@
"minor": 1,
"patch": 2
},
"WGPUAdapterInfo": {
"__size__": 36,
"adapterType": 24,
"architecture": 8,
"backendType": 20,
"description": 16,
"device": 12,
"deviceID": 32,
"nextInChain": 0,
"vendor": 4,
"vendorID": 28
},
"WGPUAdapterProperties": {
"__size__": 40,
"adapterType": 28,
Expand Down
12 changes: 12 additions & 0 deletions src/generated_struct_info64.json
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,18 @@
"minor": 1,
"patch": 2
},
"WGPUAdapterInfo": {
"__size__": 56,
"adapterType": 44,
"architecture": 16,
"backendType": 40,
"description": 32,
"device": 24,
"deviceID": 52,
"nextInChain": 0,
"vendor": 8,
"vendorID": 48
},
"WGPUAdapterProperties": {
"__size__": 72,
"adapterType": 56,
Expand Down
1 change: 1 addition & 0 deletions src/library_sigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,7 @@ sigs = {
uuid_unparse_upper__sig: 'vpp',
uuid_variant__sig: 'ip',
wgpuAdapterEnumerateFeatures__sig: 'ppp',
wgpuAdapterGetInfo__sig: 'vpp',
wgpuAdapterGetLimits__sig: 'ipp',
wgpuAdapterGetProperties__sig: 'vpp',
wgpuAdapterHasFeature__sig: 'ipi',
Expand Down
21 changes: 21 additions & 0 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ wgpu${type}Release: (id) => WebGPU.mgr${type}.release(id),`;
MIP_LEVEL_COUNT_UNDEFINED: 0xFFFFFFFF,
ARRAY_LAYER_COUNT_UNDEFINED: 0xFFFFFFFF,
AdapterType: {
CPU: 3,
Unknown: 4,
},
BackendType: {
Expand Down Expand Up @@ -2536,6 +2537,26 @@ var LibraryWebGPU = {
return adapter.features.size;
},

wgpuAdapterGetInfo__deps: ['$stringToNewUTF8'],
wgpuAdapterGetInfo: (adapterId, info) => {
var adapter = WebGPU.mgrAdapter.get(adapterId);
{{{ gpu.makeCheckDescriptor('info') }}}

var vendorPtr = stringToNewUTF8(adapter.info.vendor);
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.vendor, 'vendorPtr', '*') }}};
var architecturePtr = stringToNewUTF8(adapter.info.architecture);
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.architecture, 'architecturePtr', '*') }}};
var devicePtr = stringToNewUTF8(adapter.info.device);
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.device, 'devicePtr', '*') }}};
var descriptionPtr = stringToNewUTF8(adapter.info.description);
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.description, 'descriptionPtr', '*') }}};
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.backendType, gpu.BackendType.WebGPU, 'i32') }}};
var adapterType = adapter.isFallbackAdapter ? {{{ gpu.AdapterType.CPU }}} : {{{ gpu.AdapterType.Unknown }}};
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.adapterType, 'adapterType', 'i32') }}};
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.vendorID, '0', 'i32') }}};
{{{ makeSetValue('info', C_STRUCTS.WGPUAdapterInfo.deviceID, '0', 'i32') }}};
},

wgpuAdapterGetProperties: (adapterId, properties) => {
{{{ gpu.makeCheckDescriptor('properties') }}}
{{{ makeSetValue('properties', C_STRUCTS.WGPUAdapterProperties.vendorID, '0', 'i32') }}};
Expand Down
11 changes: 11 additions & 0 deletions src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,17 @@
"next",
"sType"
],
"WGPUAdapterInfo": [
"nextInChain",
"vendor",
"architecture",
"device",
"description",
"backendType",
"adapterType",
"vendorID",
"deviceID"
],
"WGPUAdapterProperties": [
"nextInChain",
"vendorID",
Expand Down
17 changes: 17 additions & 0 deletions system/include/webgpu/webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ typedef struct WGPUTextureImpl* WGPUTexture WGPU_OBJECT_ATTRIBUTE;
typedef struct WGPUTextureViewImpl* WGPUTextureView WGPU_OBJECT_ATTRIBUTE;

// Structure forward declarations
struct WGPUAdapterInfo;
struct WGPUAdapterProperties;
struct WGPUBindGroupEntry;
struct WGPUBlendComponent;
Expand Down Expand Up @@ -787,6 +788,18 @@ typedef struct WGPUChainedStructOut {
WGPUSType sType;
} WGPUChainedStructOut WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUAdapterInfo {
WGPUChainedStructOut * nextInChain;
char const * vendor;
char const * architecture;
char const * device;
char const * description;
WGPUBackendType backendType;
WGPUAdapterType adapterType;
uint32_t vendorID;
uint32_t deviceID;
} WGPUAdapterInfo WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUAdapterProperties {
WGPUChainedStructOut * nextInChain;
uint32_t vendorID;
Expand Down Expand Up @@ -1380,6 +1393,7 @@ extern "C" {

#if !defined(WGPU_SKIP_PROCS)

typedef void (*WGPUProcAdapterInfoFreeMembers)(WGPUAdapterInfo value) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcAdapterPropertiesFreeMembers)(WGPUAdapterProperties value) WGPU_FUNCTION_ATTRIBUTE;
typedef WGPUInstance (*WGPUProcCreateInstance)(WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE;
typedef WGPUBool (*WGPUProcGetInstanceFeatures)(WGPUInstanceFeatures * features) WGPU_FUNCTION_ATTRIBUTE;
Expand All @@ -1388,6 +1402,7 @@ typedef void (*WGPUProcSurfaceCapabilitiesFreeMembers)(WGPUSurfaceCapabilities v

// Procs of Adapter
typedef size_t (*WGPUProcAdapterEnumerateFeatures)(WGPUAdapter adapter, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcAdapterGetInfo)(WGPUAdapter adapter, WGPUAdapterInfo * info) WGPU_FUNCTION_ATTRIBUTE;
typedef WGPUBool (*WGPUProcAdapterGetLimits)(WGPUAdapter adapter, WGPUSupportedLimits * limits) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcAdapterGetProperties)(WGPUAdapter adapter, WGPUAdapterProperties * properties) WGPU_FUNCTION_ATTRIBUTE;
typedef WGPUBool (*WGPUProcAdapterHasFeature)(WGPUAdapter adapter, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE;
Expand Down Expand Up @@ -1626,6 +1641,7 @@ typedef void (*WGPUProcTextureViewRelease)(WGPUTextureView textureView) WGPU_FUN

#if !defined(WGPU_SKIP_DECLARATIONS)

WGPU_EXPORT void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo value) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuAdapterPropertiesFreeMembers(WGPUAdapterProperties value) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT WGPUInstance wgpuCreateInstance(WGPU_NULLABLE WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT WGPUBool wgpuGetInstanceFeatures(WGPUInstanceFeatures * features) WGPU_FUNCTION_ATTRIBUTE;
Expand All @@ -1634,6 +1650,7 @@ WGPU_EXPORT void wgpuSurfaceCapabilitiesFreeMembers(WGPUSurfaceCapabilities valu

// Methods of Adapter
WGPU_EXPORT size_t wgpuAdapterEnumerateFeatures(WGPUAdapter adapter, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuAdapterGetInfo(WGPUAdapter adapter, WGPUAdapterInfo * info) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT WGPUBool wgpuAdapterGetLimits(WGPUAdapter adapter, WGPUSupportedLimits * limits) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuAdapterGetProperties(WGPUAdapter adapter, WGPUAdapterProperties * properties) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT WGPUBool wgpuAdapterHasFeature(WGPUAdapter adapter, WGPUFeatureName feature) WGPU_FUNCTION_ATTRIBUTE;
Expand Down
20 changes: 20 additions & 0 deletions system/include/webgpu/webgpu_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ namespace wgpu {
class Texture;
class TextureView;

struct AdapterInfo;
struct AdapterProperties;
struct BindGroupEntry;
struct BlendComponent;
Expand Down Expand Up @@ -797,6 +798,7 @@ namespace wgpu {
using ObjectBase::operator=;

size_t EnumerateFeatures(FeatureName * features) const;
void GetInfo(AdapterInfo * info) const;
Bool GetLimits(SupportedLimits * limits) const;
void GetProperties(AdapterProperties * properties) const;
Bool HasFeature(FeatureName feature) const;
Expand Down Expand Up @@ -1214,6 +1216,24 @@ namespace wgpu {
Bool GetInstanceFeatures(InstanceFeatures * features);
Proc GetProcAddress(Device device, char const * procName);

struct AdapterInfo {
AdapterInfo() = default;
~AdapterInfo();
AdapterInfo(const AdapterInfo&) = delete;
AdapterInfo& operator=(const AdapterInfo&) = delete;
AdapterInfo(AdapterInfo&&);
AdapterInfo& operator=(AdapterInfo&&);
ChainedStructOut * nextInChain = nullptr;
char const * const vendor = nullptr;
char const * const architecture = nullptr;
char const * const device = nullptr;
char const * const description = nullptr;
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
BackendType const backendType = {};
AdapterType const adapterType = {};
uint32_t const vendorID = {};
uint32_t const deviceID = {};
};

struct AdapterProperties {
AdapterProperties() = default;
~AdapterProperties();
Expand Down
10 changes: 10 additions & 0 deletions system/lib/webgpu/webgpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <webgpu/webgpu.h>

#include <array>
#include <cstdlib>
#include <cassert>

//
Expand Down Expand Up @@ -81,3 +82,12 @@ void wgpuSurfaceGetCapabilities(WGPUSurface surface,
void wgpuSurfaceCapabilitiesFreeMembers(WGPUSurfaceCapabilities value) {
// wgpuSurfaceCapabilities doesn't currently allocate anything.
}

// WGPUAdapterInfo

void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo value) {
free(const_cast<char *>(value.vendor));
free(const_cast<char *>(value.architecture));
free(const_cast<char *>(value.device));
free(const_cast<char *>(value.description));
}
77 changes: 77 additions & 0 deletions system/lib/webgpu/webgpu_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,30 @@ namespace wgpu {
static_assert(offsetof(ChainedStruct, sType) == offsetof(WGPUChainedStruct, sType),
"offsetof mismatch for ChainedStruct::sType");

// AdapterInfo

static_assert(sizeof(AdapterInfo) == sizeof(WGPUAdapterInfo), "sizeof mismatch for AdapterInfo");
static_assert(alignof(AdapterInfo) == alignof(WGPUAdapterInfo), "alignof mismatch for AdapterInfo");

static_assert(offsetof(AdapterInfo, nextInChain) == offsetof(WGPUAdapterInfo, nextInChain),
"offsetof mismatch for AdapterInfo::nextInChain");
static_assert(offsetof(AdapterInfo, vendor) == offsetof(WGPUAdapterInfo, vendor),
"offsetof mismatch for AdapterInfo::vendor");
static_assert(offsetof(AdapterInfo, architecture) == offsetof(WGPUAdapterInfo, architecture),
"offsetof mismatch for AdapterInfo::architecture");
static_assert(offsetof(AdapterInfo, device) == offsetof(WGPUAdapterInfo, device),
"offsetof mismatch for AdapterInfo::device");
static_assert(offsetof(AdapterInfo, description) == offsetof(WGPUAdapterInfo, description),
"offsetof mismatch for AdapterInfo::description");
static_assert(offsetof(AdapterInfo, backendType) == offsetof(WGPUAdapterInfo, backendType),
"offsetof mismatch for AdapterInfo::backendType");
static_assert(offsetof(AdapterInfo, adapterType) == offsetof(WGPUAdapterInfo, adapterType),
"offsetof mismatch for AdapterInfo::adapterType");
static_assert(offsetof(AdapterInfo, vendorID) == offsetof(WGPUAdapterInfo, vendorID),
"offsetof mismatch for AdapterInfo::vendorID");
static_assert(offsetof(AdapterInfo, deviceID) == offsetof(WGPUAdapterInfo, deviceID),
"offsetof mismatch for AdapterInfo::deviceID");

// AdapterProperties

static_assert(sizeof(AdapterProperties) == sizeof(WGPUAdapterProperties), "sizeof mismatch for AdapterProperties");
Expand Down Expand Up @@ -1924,6 +1948,55 @@ template <typename T>
static_assert(offsetof(RenderPipelineDescriptor, fragment) == offsetof(WGPURenderPipelineDescriptor, fragment),
"offsetof mismatch for RenderPipelineDescriptor::fragment");

// AdapterInfo implementation
AdapterInfo::~AdapterInfo() {
if (this->vendor != nullptr || this->architecture != nullptr || this->device != nullptr || this->description != nullptr) {
wgpuAdapterInfoFreeMembers(
*reinterpret_cast<WGPUAdapterInfo*>(this));
}
}

static void Reset(AdapterInfo& value) {
AdapterInfo defaultValue{};
AsNonConstReference(value.vendor) = defaultValue.vendor;
AsNonConstReference(value.architecture) = defaultValue.architecture;
AsNonConstReference(value.device) = defaultValue.device;
AsNonConstReference(value.description) = defaultValue.description;
AsNonConstReference(value.backendType) = defaultValue.backendType;
AsNonConstReference(value.adapterType) = defaultValue.adapterType;
AsNonConstReference(value.vendorID) = defaultValue.vendorID;
AsNonConstReference(value.deviceID) = defaultValue.deviceID;
}

AdapterInfo::AdapterInfo(AdapterInfo&& rhs)
: vendor(rhs.vendor),
architecture(rhs.architecture),
device(rhs.device),
description(rhs.description),
backendType(rhs.backendType),
adapterType(rhs.adapterType),
vendorID(rhs.vendorID),
deviceID(rhs.deviceID){
Reset(rhs);
}

AdapterInfo& AdapterInfo::operator=(AdapterInfo&& rhs) {
if (&rhs == this) {
return *this;
}
this->~AdapterInfo();
AsNonConstReference(this->vendor) = std::move(rhs.vendor);
AsNonConstReference(this->architecture) = std::move(rhs.architecture);
AsNonConstReference(this->device) = std::move(rhs.device);
AsNonConstReference(this->description) = std::move(rhs.description);
AsNonConstReference(this->backendType) = std::move(rhs.backendType);
AsNonConstReference(this->adapterType) = std::move(rhs.adapterType);
AsNonConstReference(this->vendorID) = std::move(rhs.vendorID);
AsNonConstReference(this->deviceID) = std::move(rhs.deviceID);
Reset(rhs);
return *this;
}

// AdapterProperties
AdapterProperties::~AdapterProperties() {
if (this->vendorName != nullptr || this->architecture != nullptr || this->name != nullptr || this->driverDescription != nullptr) {
Expand Down Expand Up @@ -1987,6 +2060,10 @@ template <typename T>
auto result = wgpuAdapterEnumerateFeatures(Get(), reinterpret_cast<WGPUFeatureName * >(features));
return result;
}
void Adapter::GetInfo(AdapterInfo * info) const {
*info = AdapterInfo();
wgpuAdapterGetInfo(Get(), reinterpret_cast<WGPUAdapterInfo * >(info));
}
Bool Adapter::GetLimits(SupportedLimits * limits) const {
auto result = wgpuAdapterGetLimits(Get(), reinterpret_cast<WGPUSupportedLimits * >(limits));
return result;
Expand Down
8 changes: 8 additions & 0 deletions test/webgpu_basic_rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ void run() {
int main() {
GetAdapter([](wgpu::Adapter a) {
adapter = a;

wgpu::AdapterInfo info;
adapter.GetInfo(&info);
printf("adapter vendor: %s\n", info.vendor);
printf("adapter architecture: %s\n", info.architecture);
printf("adapter device: %s\n", info.device);
printf("adapter description: %s\n", info.description);

GetDevice([](wgpu::Device dev) {
device = dev;
run();
Expand Down
Loading