Skip to content

Commit

Permalink
IDL: Remove GPUAdapter.name field and add support for GPUAdapter.requ…
Browse files Browse the repository at this point in the history
…estAdapterInfo() function. gpuweb/gpuweb#2660
  • Loading branch information
juj committed Aug 18, 2022
1 parent d3d3ddb commit 6de1d39
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
4 changes: 0 additions & 4 deletions hello_triangle/hello_triangle_verbose.c
Expand Up @@ -152,14 +152,10 @@ void ObtainedWebGpuAdapter(WGpuAdapter result, void *userData)

adapter = result;

char name[256];
WGpuSupportedLimits limits;
WGPU_FEATURES_BITFIELD features = wgpu_adapter_get_features(adapter);
wgpu_adapter_get_name(adapter, name, sizeof(name));
wgpu_adapter_get_limits(adapter, &limits);

emscripten_mini_stdio_printf("Adapter name: %s\n", name);

#define TEST_FEATURE(x) emscripten_mini_stdio_printf("Adapter supports feature " #x ": %s\n", (features & (x) ? "yes" : "no"))
TEST_FEATURE(WGPU_FEATURE_DEPTH_CLIP_CONTROL);
TEST_FEATURE(WGPU_FEATURE_DEPTH32FLOAT_STENCIL8);
Expand Down
8 changes: 7 additions & 1 deletion idl/webgpu.idl
Expand Up @@ -41,6 +41,12 @@ interface GPUSupportedFeatures {
readonly setlike<DOMString>;
};

[Exposed=(Window, DedicatedWorker), SecureContext]
interface GPUAdapterInfo {
readonly attribute DOMString vendor;
readonly attribute DOMString architecture;
readonly attribute DOMString device;
readonly attribute DOMString description;
};

interface mixin NavigatorGPU {
Expand All @@ -67,12 +73,12 @@ enum GPUPowerPreference {

[Exposed=(Window, DedicatedWorker), SecureContext]
interface GPUAdapter {
readonly attribute DOMString name;
[SameObject] readonly attribute GPUSupportedFeatures features;
[SameObject] readonly attribute GPUSupportedLimits limits;
readonly attribute boolean isFallbackAdapter;

Promise<GPUDevice> requestDevice(optional GPUDeviceDescriptor descriptor = {});
Promise<GPUAdapterInfo> requestAdapterInfo(optional sequence<DOMString> unmaskHints = []);
};

dictionary GPUDeviceDescriptor : GPUObjectDescriptorBase {
Expand Down
32 changes: 27 additions & 5 deletions lib/lib_webgpu.h
Expand Up @@ -178,6 +178,23 @@ typedef int HTML_PREDEFINED_COLOR_SPACE;
#define HTML_PREDEFINED_COLOR_SPACE_SRGB 1
#define HTML_PREDEFINED_COLOR_SPACE_DISPLAY_P3 2

/*
[Exposed=(Window, DedicatedWorker), SecureContext]
interface GPUAdapterInfo {
readonly attribute DOMString vendor;
readonly attribute DOMString architecture;
readonly attribute DOMString device;
readonly attribute DOMString description;
};
*/
typedef struct WGpuAdapterInfo
{
char vendor[512];
char architecture[512];
char device[512];
char description[512];
} WGpuAdapterInfo;

/*
interface mixin NavigatorGPU {
[SameObject, SecureContext] readonly attribute GPU gpu;
Expand Down Expand Up @@ -243,22 +260,18 @@ typedef int WGPU_POWER_PREFERENCE;
/*
[Exposed=(Window, DedicatedWorker), SecureContext]
interface GPUAdapter {
readonly attribute DOMString name;
[SameObject] readonly attribute GPUSupportedFeatures features;
[SameObject] readonly attribute WGpuSupportedLimits limits;
readonly attribute boolean isFallbackAdapter;
Promise<GPUDevice> requestDevice(optional GPUDeviceDescriptor descriptor = {});
Promise<GPUAdapterInfo> requestAdapterInfo(optional sequence<DOMString> unmaskHints = []);
};
*/
typedef int WGpuAdapter;
// Returns true if the given handle references a valid GPUAdapter.
EM_BOOL wgpu_is_adapter(WGpuObjectBase object);

// Writes the name of the adapter to the provided string pointer. If the length of the adapter name would not fit in dstNameSize, then it will be truncated.
// Returns the number of bytes written. (if return value == dstNameSize, truncation likely occurred)
int wgpu_adapter_get_name(WGpuAdapter adapter, char *dstName __attribute__((nonnull)), int dstNameSize);

// Returns a bitfield of all the supported features on this adapter.
WGPU_FEATURES_BITFIELD wgpu_adapter_or_device_get_features(WGpuAdapter adapter);
#define wgpu_adapter_get_features wgpu_adapter_or_device_get_features
Expand All @@ -283,6 +296,15 @@ WGpuDevice wgpu_adapter_request_device_sync(WGpuAdapter adapter, const WGpuDevic
void wgpu_adapter_request_device_async_simple(WGpuAdapter adapter, WGpuRequestDeviceCallback deviceCallback);
WGpuDevice wgpu_adapter_request_device_sync_simple(WGpuAdapter adapter);

// Callback function type that is called when GPUAdapter information has been obtained. The information will be reported in a struct of
// type WGpuAdapterInfo. Do not hold on to this struct pointer after the duration of this call (but make a copy of the contents if desirable)
typedef void (*WGpuRequestAdapterInfoCallback)(WGpuAdapter adapter, const WGpuAdapterInfo *adapterInfo __attribute__((nonnull)), void *userData);

// Begins a process to asynchronously request GPUAdapter information. 'unmaskHints' should be a null-terminated array of null-terminated strings
// of which information to retrieve, e.g. { "vendor", "architecture", "device", "description", 0 }.
void wgpu_adapter_request_adapter_info_async(WGpuAdapter adapter, const char **unmaskHints, WGpuRequestAdapterInfoCallback callback, void *userData);
// TODO: Create asyncified wgpu_adapter_request_adapter_info_sync() function.

/*
dictionary GPUQueueDescriptor : GPUObjectDescriptorBase {
};
Expand Down
39 changes: 32 additions & 7 deletions lib/lib_webgpu.js
Expand Up @@ -465,13 +465,6 @@ mergeInto(LibraryManager.library, {
return GPUTextureAndVertexFormats.indexOf(navigator['gpu']['getPreferredCanvasFormat']());
},

wgpu_adapter_get_name: function(adapter, dstName, dstNameSize) {
{{{ wdebuglog('`wgpu_adapter_get_name(adapter: ${adapter}, dstName: ${dstName}, dstNameSize: ${dstNameSize})`'); }}}
{{{ wassert('wgpu[adapter] instanceof GPUAdapter'); }}}
{{{ wassert('dstName != 0 || dstNameSize == 0, "passed a null dstName pointer, but with a non-zero dstNameSize length"'); }}}
return stringToUTF8(wgpu[adapter]['name'], dstName, dstNameSize);
},

wgpu_adapter_or_device_get_features__deps: ['wgpuFeatures'],
wgpu_adapter_or_device_get_features: function(adapterOrDevice) {
{{{ wdebuglog('`wgpu_adapter_or_device_get_features(adapterOrDevice: ${adapterOrDevice})`'); }}}
Expand Down Expand Up @@ -646,6 +639,38 @@ mergeInto(LibraryManager.library, {
},
#endif

wgpu_adapter_request_adapter_info_async: function(adapter, unmaskHints, callback, userData) {
{{{ wdebuglog('`wgpu_adapter_request_adapter_info_async(adapter: ${adapter}, unmaskHints: ${unmaskHints}, callback: ${callback}, userData: ${userData})`'); }}}
{{{ wassert('wgpu[adapter] instanceof GPUAdapter'); }}}
{{{ wassert('callback != 0'); }}}
{{{ wassert('unmaskHints != 0'); }}}

function cb(adapterInfo) {
{{{ wdebuglog('`GPUAdapter.requestAdapterInfo() resolved with following adapterInfo:`'); }}}
{{{ wdebugdir('adapterInfo'); }}}

let stackTop = stackSave(),
info = stackAlloc(2048);
stringToUTF8(adapterInfo['vendor'], info, 512);
stringToUTF8(adapterInfo['architecture'], info + 512, 512);
stringToUTF8(adapterInfo['device'], info + 1024, 512);
stringToUTF8(adapterInfo['description'], info + 1536, 512);
{{{ makeDynCall('viii', 'callback') }}}(adapter, info, userData);
stackRestore(stackTop);
}

let hints = [];
{{{ ptrToIdx('unmaskHints', 2); }}}
while(HEAPU32[unmaskHints]) {
hints.push(UTF8ToString(HEAPU32[unmaskHints++]));
}

{{{ wdebuglog('`wgpu_adapter_request_adapter_info_async() requesting adapter info with hints [${hints.join(", ")}]`'); }}}
return wgpu[adapter]['requestAdapterInfo'](hints).then(cb).catch(()=>{cb()});
},

// TODO: Create asyncified wgpu_adapter_request_adapter_info_sync() function.

wgpu_device_get_queue: function(device) {
{{{ wdebuglog('`wgpu_device_get_queue(device=${device})`'); }}}
{{{ wassert('wgpu[device] instanceof GPUDevice'); }}}
Expand Down

0 comments on commit 6de1d39

Please sign in to comment.