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] Add Surface API #21939

Merged
merged 14 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
29 changes: 29 additions & 0 deletions src/generated_struct_info32.json
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,29 @@
"limits": 8,
"nextInChain": 0
},
"WGPUSurfaceCapabilities": {
"__size__": 28,
"alphaModeCount": 20,
"alphaModes": 24,
"formatCount": 4,
"formats": 8,
"nextInChain": 0,
"presentModeCount": 12,
"presentModes": 16
},
"WGPUSurfaceConfiguration": {
"__size__": 40,
"alphaMode": 24,
"device": 4,
"format": 8,
"height": 32,
"nextInChain": 0,
"presentMode": 36,
"usage": 12,
"viewFormatCount": 16,
"viewFormats": 20,
"width": 28
},
"WGPUSurfaceDescriptor": {
"__size__": 8,
"label": 4,
Expand All @@ -1270,6 +1293,12 @@
"chain": 0,
"selector": 8
},
"WGPUSurfaceTexture": {
"__size__": 12,
"status": 8,
"suboptimal": 4,
"texture": 0
},
"WGPUSwapChainDescriptor": {
"__size__": 28,
"format": 12,
Expand Down
29 changes: 29 additions & 0 deletions src/generated_struct_info64.json
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,29 @@
"limits": 8,
"nextInChain": 0
},
"WGPUSurfaceCapabilities": {
"__size__": 56,
"alphaModeCount": 40,
"alphaModes": 48,
"formatCount": 8,
"formats": 16,
"nextInChain": 0,
"presentModeCount": 24,
"presentModes": 32
},
"WGPUSurfaceConfiguration": {
"__size__": 56,
"alphaMode": 40,
"device": 8,
"format": 16,
"height": 48,
"nextInChain": 0,
"presentMode": 52,
"usage": 20,
"viewFormatCount": 24,
"viewFormats": 32,
"width": 44
},
"WGPUSurfaceDescriptor": {
"__size__": 16,
"label": 8,
Expand All @@ -1270,6 +1293,12 @@
"chain": 0,
"selector": 16
},
"WGPUSurfaceTexture": {
"__size__": 16,
"status": 12,
"suboptimal": 8,
"texture": 0
},
"WGPUSwapChainDescriptor": {
"__size__": 40,
"format": 20,
Expand Down
5 changes: 5 additions & 0 deletions src/library_sigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1711,9 +1711,14 @@ sigs = {
wgpuShaderModuleReference__sig: 'vp',
wgpuShaderModuleRelease__sig: 'vp',
wgpuShaderModuleSetLabel__sig: 'vpp',
wgpuSurfaceCapabilitiesFreeMembers__sig: 'vp',
wgpuSurfaceConfigure__sig: 'vpp',
wgpuSurfaceGetCurrentTexture__sig: 'vpp',
wgpuSurfaceGetPreferredFormat__sig: 'ipp',
wgpuSurfacePresent__sig: 'vp',
wgpuSurfaceReference__sig: 'vp',
wgpuSurfaceRelease__sig: 'vp',
wgpuSurfaceUnconfigure__sig: 'vp',
wgpuSwapChainGetCurrentTexture__sig: 'pp',
wgpuSwapChainGetCurrentTextureView__sig: 'pp',
wgpuSwapChainPresent__sig: 'vp',
Expand Down
71 changes: 71 additions & 0 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2673,13 +2673,84 @@ var LibraryWebGPU = {
sampler.label = UTF8ToString(labelPtr);
},

// WGPUSurfaceCapabilities

wgpuSurfaceCapabilitiesFreeMembers: (value) => {
// wgpuSurfaceCapabilities does currently allocate anything
},

// WGPUSurface

wgpuSurfaceConfigure: (surfaceId, config) => {
{{{ gpu.makeCheckDescriptor('config') }}}
var deviceId = {{{ makeGetValue('config', C_STRUCTS.WGPUSurfaceConfiguration.device, '*') }}};
var context = WebGPU.mgrSurface.get(surfaceId);

#if ASSERTIONS
assert({{{ gpu.PresentMode.Fifo }}} ===
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.presentMode) }}});
#endif

var canvasSize = [
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.width) }}},
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.height) }}}
];

if (canvasSize[0] !== 0) {
context["canvas"]["width"] = canvasSize[0];
}

if (canvasSize[1] !== 0) {
context["canvas"]["height"] = canvasSize[1];
}

var configuration = {
"device": WebGPU.mgrDevice.get(deviceId),
"format": WebGPU.TextureFormat[
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.format) }}}],
"usage": {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.usage) }}},
// viewFormatCount
// viewFormats
"alphaMode": "opaque",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be either "opaque" or "premultiplied" depending on the configuration, and there should be an assert that it's one of the values that's valid in JS (auto, opaque, premultiplied). (Just look it up in the table above and then assert that the result is not undefined.)

Alternatively, assert the alphaMode is Opaque for now with a TODO, and implement this later if you'd like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've assert alphaMode is Opaque with a TODO.

"width": canvasSize[0],
"height": canvasSize[1],
"presentMode": {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.presentMode) }}},
};
context.configure(configuration);
},

wgpuSurfaceGetCurrentTexture: (surfaceId, surfaceTexturePtr) => {
{{{ gpu.makeCheck('surfaceTexturePtr') }}}
var context = WebGPU.mgrSurface.get(surfaceId);

try {
var texture = WebGPU.mgrTexture.create(context.getCurrentTexture());
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.texture, 'texture', '*') }}};
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.suboptimal, '0', 'i32') }}};
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.status, /*Success=*/0, 'i32') }}};
} catch (ex) {
#if ASSERTIONS
err(`wgpuSurfaceGetCurrentTexture() failed: ${ex}`);
#endif
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.status, /*Timeout=*/1, 'i32') }}};
}
},

wgpuSurfaceGetPreferredFormat: (surfaceId, adapterId) => {
var format = navigator["gpu"]["getPreferredCanvasFormat"]();
return WebGPU.Int_PreferredFormat[format];
},

wgpuSurfacePresent: (surfaceId) => {
// TODO: This could probably be emulated with ASYNCIFY.
abort('wgpuSurfacePresent is unsupported (use requestAnimationFrame via html5.h instead)');
},

wgpuSurfaceUnconfigure: (surfaceId) => {
var context = WebGPU.mgrSurface.get(surfaceId);
context.unconfigure();
},

// WGPUSwapChain

wgpuDeviceCreateSwapChain: (deviceId, surfaceId, descriptor) => {
Expand Down
26 changes: 26 additions & 0 deletions src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,27 @@
"format",
"viewDimension"
],
"WGPUSurfaceCapabilities": [
"nextInChain",
"formatCount",
"formats",
"presentModeCount",
"presentModes",
"alphaModeCount",
"alphaModes"
],
"WGPUSurfaceConfiguration": [
"nextInChain",
"device",
"format",
"usage",
"viewFormatCount",
"viewFormats",
"alphaMode",
"width",
"height",
"presentMode"
],
"WGPUSurfaceDescriptor": [
"nextInChain",
"label"
Expand All @@ -1488,6 +1509,11 @@
"chain",
"selector"
],
"WGPUSurfaceTexture": [
"texture",
"suboptimal",
"status"
],
"WGPUSwapChainDescriptor": [
"nextInChain",
"label",
Expand Down
8 changes: 4 additions & 4 deletions system/include/webgpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ These files, and several snippets of other files, are generated by Dawn
(Chromium's WebGPU library):
- [Generator](https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/generator/)
- [Generator input](https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/dawn.json)
- [Generator output](https://source.chromium.org/chromium/chromium/src/+/main:out/Debug/gen/third_party/dawn/emscripten-bits/)
- [Generator output](https://source.chromium.org/chromium/chromium/src/+/main:out/linux-Debug/gen/third_party/dawn/emscripten-bits/)

The C header is intended to be mostly the same as the "upstream"
[`webgpu.h`](https://github.com/webgpu-native/webgpu-headers/blob/main/webgpu.h),
Expand All @@ -16,13 +16,13 @@ is included here because it is strongly tied to an exact `webgpu.h` revision.
To update these bindings from Dawn:
1. Copy [`webgpu_enum_class_bitmasks.h`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/include/webgpu/webgpu_enum_class_bitmasks.h) from Dawn's source to `system/include/webgpu/webgpu_enum_class_bitmasks.h`
1. Build Dawn's `emscripten_bits_gen` target (in a gn or CMake build of Dawn, or a build of Chromium) - or, use the Chromium Code Search copy of the generated files if no changes are needed
1. Copy the generated [`emscripten-bits/system`](https://source.chromium.org/chromium/chromium/src/+/main:out/Debug/gen/third_party/dawn/emscripten-bits/system/) files into Emscripten's `system` directory
1. Copy the generated [`emscripten-bits/system`](https://source.chromium.org/chromium/chromium/src/+/main:out/linux-Debug/gen/third_party/dawn/emscripten-bits/system/) files into Emscripten's `system` directory
- `system/include/webgpu/webgpu.h`
- `system/include/webgpu/webgpu_cpp.h`
- `system/include/webgpu/webgpu_cpp_chained_struct.h`
- `system/lib/webgpu/webgpu_cpp.cpp`
1. Paste the contents of [`library_webgpu_enum_tables.js`](https://source.chromium.org/chromium/chromium/src/+/main:out/Debug/gen/third_party/dawn/emscripten-bits/library_webgpu_enum_tables.js) over the "Map from enum number to enum string" section of [`library_webgpu.js`](../../../src/library_webgpu.js)
1. Paste [`webgpu_struct_info.json`](https://source.chromium.org/chromium/chromium/src/+/main:out/Debug/gen/third_party/dawn/emscripten-bits/webgpu_struct_info.json) over the "WebGPU" section of [`struct_info.json`](../../../src/struct_info.json).
1. Paste the contents of [`library_webgpu_enum_tables.js`](https://source.chromium.org/chromium/chromium/src/+/main:out/linux-Debug/gen/third_party/dawn/emscripten-bits/library_webgpu_enum_tables.js) over the "Map from enum number to enum string" section of [`library_webgpu.js`](../../../src/library_webgpu.js)
1. Paste [`webgpu_struct_info.json`](https://source.chromium.org/chromium/chromium/src/+/main:out/linux-Debug/gen/third_party/dawn/emscripten-bits/webgpu_struct_info.json) over the "WebGPU" section of [`struct_info.json`](../../../src/struct_info.json).
1. **Manually update the `globalThis.gpu` compile-time enum tables (AdapterType, BackendType, etc.)**:
- Inspect the `webgpu.h` diff for changes to the integer values of any enums used here. (It's not necessary to add new enum values to these tables until they're needed for something.)
1. **Manually update the "Map from enum string back to enum number" tables.**
Expand Down
63 changes: 63 additions & 0 deletions system/include/webgpu/webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ struct WGPUShaderModuleWGSLDescriptor;
struct WGPUShaderModuleDescriptor;
struct WGPUStencilFaceState;
struct WGPUStorageTextureBindingLayout;
struct WGPUSurfaceCapabilities;
struct WGPUSurfaceConfiguration;
struct WGPUSurfaceDescriptor;
struct WGPUSurfaceDescriptorFromCanvasHTMLSelector;
struct WGPUSurfaceTexture;
struct WGPUSwapChainDescriptor;
struct WGPUTextureBindingLayout;
struct WGPUTextureBindingViewDimensionDescriptor;
Expand Down Expand Up @@ -305,6 +308,15 @@ typedef enum WGPUCompilationMessageType {
WGPUCompilationMessageType_Force32 = 0x7FFFFFFF
} WGPUCompilationMessageType WGPU_ENUM_ATTRIBUTE;

typedef enum WGPUCompositeAlphaMode {
WGPUCompositeAlphaMode_Auto = 0x00000000,
WGPUCompositeAlphaMode_Opaque = 0x00000001,
WGPUCompositeAlphaMode_Premultiplied = 0x00000002,
WGPUCompositeAlphaMode_Unpremultiplied = 0x00000003,
WGPUCompositeAlphaMode_Inherit = 0x00000004,
WGPUCompositeAlphaMode_Force32 = 0x7FFFFFFF
} WGPUCompositeAlphaMode WGPU_ENUM_ATTRIBUTE;

typedef enum WGPUCreatePipelineAsyncStatus {
WGPUCreatePipelineAsyncStatus_Success = 0x00000000,
WGPUCreatePipelineAsyncStatus_ValidationError = 0x00000001,
Expand Down Expand Up @@ -497,6 +509,16 @@ typedef enum WGPUStoreOp {
WGPUStoreOp_Force32 = 0x7FFFFFFF
} WGPUStoreOp WGPU_ENUM_ATTRIBUTE;

typedef enum WGPUSurfaceGetCurrentTextureStatus {
WGPUSurfaceGetCurrentTextureStatus_Success = 0x00000000,
WGPUSurfaceGetCurrentTextureStatus_Timeout = 0x00000001,
WGPUSurfaceGetCurrentTextureStatus_Outdated = 0x00000002,
WGPUSurfaceGetCurrentTextureStatus_Lost = 0x00000003,
WGPUSurfaceGetCurrentTextureStatus_OutOfMemory = 0x00000004,
WGPUSurfaceGetCurrentTextureStatus_DeviceLost = 0x00000005,
WGPUSurfaceGetCurrentTextureStatus_Force32 = 0x7FFFFFFF
} WGPUSurfaceGetCurrentTextureStatus WGPU_ENUM_ATTRIBUTE;

typedef enum WGPUTextureAspect {
WGPUTextureAspect_Undefined = 0x00000000,
WGPUTextureAspect_All = 0x00000001,
Expand Down Expand Up @@ -1069,6 +1091,29 @@ typedef struct WGPUStorageTextureBindingLayout {
WGPUTextureViewDimension viewDimension;
} WGPUStorageTextureBindingLayout WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUSurfaceCapabilities {
WGPUChainedStructOut * nextInChain;
size_t formatCount;
WGPUTextureFormat const * formats;
size_t presentModeCount;
WGPUPresentMode const * presentModes;
size_t alphaModeCount;
WGPUCompositeAlphaMode const * alphaModes;
} WGPUSurfaceCapabilities WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUSurfaceConfiguration {
WGPUChainedStruct const * nextInChain;
WGPUDevice device;
WGPUTextureFormat format;
WGPUTextureUsageFlags usage;
size_t viewFormatCount;
WGPUTextureFormat const * viewFormats;
WGPUCompositeAlphaMode alphaMode;
uint32_t width;
uint32_t height;
WGPUPresentMode presentMode;
} WGPUSurfaceConfiguration WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUSurfaceDescriptor {
WGPUChainedStruct const * nextInChain;
WGPU_NULLABLE char const * label;
Expand All @@ -1080,6 +1125,12 @@ typedef struct WGPUSurfaceDescriptorFromCanvasHTMLSelector {
char const * selector;
} WGPUSurfaceDescriptorFromCanvasHTMLSelector WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUSurfaceTexture {
WGPUTexture texture;
WGPUBool suboptimal;
WGPUSurfaceGetCurrentTextureStatus status;
} WGPUSurfaceTexture WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUSwapChainDescriptor {
WGPUChainedStruct const * nextInChain;
WGPU_NULLABLE char const * label;
Expand Down Expand Up @@ -1332,6 +1383,7 @@ typedef void (*WGPUProcAdapterPropertiesFreeMembers)(WGPUAdapterProperties value
typedef WGPUInstance (*WGPUProcCreateInstance)(WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE;
typedef WGPUBool (*WGPUProcGetInstanceFeatures)(WGPUInstanceFeatures * features) WGPU_FUNCTION_ATTRIBUTE;
typedef WGPUProc (*WGPUProcGetProcAddress)(WGPUDevice device, char const * procName) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcSurfaceCapabilitiesFreeMembers)(WGPUSurfaceCapabilities value) WGPU_FUNCTION_ATTRIBUTE;

// Procs of Adapter
typedef size_t (*WGPUProcAdapterEnumerateFeatures)(WGPUAdapter adapter, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE;
Expand Down Expand Up @@ -1532,7 +1584,12 @@ typedef void (*WGPUProcShaderModuleReference)(WGPUShaderModule shaderModule) WGP
typedef void (*WGPUProcShaderModuleRelease)(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE;

// Procs of Surface
typedef void (*WGPUProcSurfaceConfigure)(WGPUSurface surface, WGPUSurfaceConfiguration const * config) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcSurfaceGetCapabilities)(WGPUSurface surface, WGPUAdapter adapter, WGPUSurfaceCapabilities * capabilities) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcSurfaceGetCurrentTexture)(WGPUSurface surface, WGPUSurfaceTexture * surfaceTexture) WGPU_FUNCTION_ATTRIBUTE;
typedef WGPUTextureFormat (*WGPUProcSurfaceGetPreferredFormat)(WGPUSurface surface, WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcSurfacePresent)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcSurfaceUnconfigure)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcSurfaceReference)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;
typedef void (*WGPUProcSurfaceRelease)(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;

Expand Down Expand Up @@ -1572,6 +1629,7 @@ WGPU_EXPORT void wgpuAdapterPropertiesFreeMembers(WGPUAdapterProperties value) W
WGPU_EXPORT WGPUInstance wgpuCreateInstance(WGPU_NULLABLE WGPUInstanceDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT WGPUBool wgpuGetInstanceFeatures(WGPUInstanceFeatures * features) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT WGPUProc wgpuGetProcAddress(WGPU_NULLABLE WGPUDevice device, char const * procName) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuSurfaceCapabilitiesFreeMembers(WGPUSurfaceCapabilities value) WGPU_FUNCTION_ATTRIBUTE;

// Methods of Adapter
WGPU_EXPORT size_t wgpuAdapterEnumerateFeatures(WGPUAdapter adapter, WGPUFeatureName * features) WGPU_FUNCTION_ATTRIBUTE;
Expand Down Expand Up @@ -1771,7 +1829,12 @@ WGPU_EXPORT void wgpuShaderModuleReference(WGPUShaderModule shaderModule) WGPU_F
WGPU_EXPORT void wgpuShaderModuleRelease(WGPUShaderModule shaderModule) WGPU_FUNCTION_ATTRIBUTE;

// Methods of Surface
WGPU_EXPORT void wgpuSurfaceConfigure(WGPUSurface surface, WGPUSurfaceConfiguration const * config) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuSurfaceGetCapabilities(WGPUSurface surface, WGPUAdapter adapter, WGPUSurfaceCapabilities * capabilities) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuSurfaceGetCurrentTexture(WGPUSurface surface, WGPUSurfaceTexture * surfaceTexture) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT WGPUTextureFormat wgpuSurfaceGetPreferredFormat(WGPUSurface surface, WGPUAdapter adapter) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuSurfacePresent(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuSurfaceUnconfigure(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuSurfaceReference(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;
WGPU_EXPORT void wgpuSurfaceRelease(WGPUSurface surface) WGPU_FUNCTION_ATTRIBUTE;

Expand Down
Loading
Loading