Skip to content

Commit 579da6c

Browse files
[webgpu] Add Surface API (#21939)
Support the webgpu.h Surface API. This supersedes the SwapChain API which will be removed later along with other breaking changes to this experimental header. Fixes #21745 --------- Co-authored-by: Kai Ninomiya <kainino@chromium.org>
1 parent b45857f commit 579da6c

File tree

11 files changed

+539
-42
lines changed

11 files changed

+539
-42
lines changed

src/generated_struct_info32.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,29 @@
12601260
"limits": 8,
12611261
"nextInChain": 0
12621262
},
1263+
"WGPUSurfaceCapabilities": {
1264+
"__size__": 28,
1265+
"alphaModeCount": 20,
1266+
"alphaModes": 24,
1267+
"formatCount": 4,
1268+
"formats": 8,
1269+
"nextInChain": 0,
1270+
"presentModeCount": 12,
1271+
"presentModes": 16
1272+
},
1273+
"WGPUSurfaceConfiguration": {
1274+
"__size__": 40,
1275+
"alphaMode": 24,
1276+
"device": 4,
1277+
"format": 8,
1278+
"height": 32,
1279+
"nextInChain": 0,
1280+
"presentMode": 36,
1281+
"usage": 12,
1282+
"viewFormatCount": 16,
1283+
"viewFormats": 20,
1284+
"width": 28
1285+
},
12631286
"WGPUSurfaceDescriptor": {
12641287
"__size__": 8,
12651288
"label": 4,
@@ -1270,6 +1293,12 @@
12701293
"chain": 0,
12711294
"selector": 8
12721295
},
1296+
"WGPUSurfaceTexture": {
1297+
"__size__": 12,
1298+
"status": 8,
1299+
"suboptimal": 4,
1300+
"texture": 0
1301+
},
12731302
"WGPUSwapChainDescriptor": {
12741303
"__size__": 28,
12751304
"format": 12,

src/generated_struct_info64.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,29 @@
12601260
"limits": 8,
12611261
"nextInChain": 0
12621262
},
1263+
"WGPUSurfaceCapabilities": {
1264+
"__size__": 56,
1265+
"alphaModeCount": 40,
1266+
"alphaModes": 48,
1267+
"formatCount": 8,
1268+
"formats": 16,
1269+
"nextInChain": 0,
1270+
"presentModeCount": 24,
1271+
"presentModes": 32
1272+
},
1273+
"WGPUSurfaceConfiguration": {
1274+
"__size__": 56,
1275+
"alphaMode": 40,
1276+
"device": 8,
1277+
"format": 16,
1278+
"height": 48,
1279+
"nextInChain": 0,
1280+
"presentMode": 52,
1281+
"usage": 20,
1282+
"viewFormatCount": 24,
1283+
"viewFormats": 32,
1284+
"width": 44
1285+
},
12631286
"WGPUSurfaceDescriptor": {
12641287
"__size__": 16,
12651288
"label": 8,
@@ -1270,6 +1293,12 @@
12701293
"chain": 0,
12711294
"selector": 16
12721295
},
1296+
"WGPUSurfaceTexture": {
1297+
"__size__": 16,
1298+
"status": 12,
1299+
"suboptimal": 8,
1300+
"texture": 0
1301+
},
12731302
"WGPUSwapChainDescriptor": {
12741303
"__size__": 40,
12751304
"format": 20,

src/library_sigs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,9 +1711,13 @@ sigs = {
17111711
wgpuShaderModuleReference__sig: 'vp',
17121712
wgpuShaderModuleRelease__sig: 'vp',
17131713
wgpuShaderModuleSetLabel__sig: 'vpp',
1714+
wgpuSurfaceConfigure__sig: 'vpp',
1715+
wgpuSurfaceGetCurrentTexture__sig: 'vpp',
17141716
wgpuSurfaceGetPreferredFormat__sig: 'ipp',
1717+
wgpuSurfacePresent__sig: 'vp',
17151718
wgpuSurfaceReference__sig: 'vp',
17161719
wgpuSurfaceRelease__sig: 'vp',
1720+
wgpuSurfaceUnconfigure__sig: 'vp',
17171721
wgpuSwapChainGetCurrentTexture__sig: 'pp',
17181722
wgpuSwapChainGetCurrentTextureView__sig: 'pp',
17191723
wgpuSwapChainPresent__sig: 'vp',

src/library_webgpu.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ wgpu${type}Release: (id) => WebGPU.mgr${type}.release(id),`;
9999
DeviceLost: 2,
100100
Unknown: 3,
101101
},
102+
CompositeAlphaMode: {
103+
Auto: 0,
104+
Opaque: 1,
105+
},
102106
CreatePipelineAsyncStatus: {
103107
Success: 0,
104108
ValidationError: 1,
@@ -154,6 +158,10 @@ wgpu${type}Release: (id) => WebGPU.mgr${type}.release(id),`;
154158
RenderPassDescriptorMaxDrawCount: 0xF,
155159
TextureBindingViewDimensionDescriptor: 0x11,
156160
},
161+
SurfaceGetCurrentTextureStatus: {
162+
Success: 0,
163+
DeviceLost: 5,
164+
},
157165
QueueWorkDoneStatus: {
158166
Success: 0,
159167
Error: 1,
@@ -2664,7 +2672,7 @@ var LibraryWebGPU = {
26642672
// WGPUAdapterProperties
26652673

26662674
wgpuAdapterPropertiesFreeMembers: (value) => {
2667-
// wgpuAdapterGetProperties does currently allocate anything
2675+
// wgpuAdapterGetProperties doesn't currently allocate anything.
26682676
},
26692677

26702678
// WGPUSampler
@@ -2676,11 +2684,83 @@ var LibraryWebGPU = {
26762684

26772685
// WGPUSurface
26782686

2687+
wgpuSurfaceConfigure: (surfaceId, config) => {
2688+
{{{ gpu.makeCheckDescriptor('config') }}}
2689+
var deviceId = {{{ makeGetValue('config', C_STRUCTS.WGPUSurfaceConfiguration.device, '*') }}};
2690+
var context = WebGPU.mgrSurface.get(surfaceId);
2691+
2692+
#if ASSERTIONS
2693+
var viewFormatCount = {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.viewFormatCount) }}};
2694+
var viewFormats = {{{ makeGetValue('config', C_STRUCTS.WGPUSurfaceConfiguration.viewFormats, '*') }}};
2695+
assert(viewFormatCount === 0 && viewFormats === 0, "TODO: Support viewFormats.");
2696+
var alphaMode = {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.alphaMode) }}};
2697+
assert(alphaMode === {{{ gpu.CompositeAlphaMode.Auto }}} ||
2698+
alphaMode === {{{ gpu.CompositeAlphaMode.Opaque }}},
2699+
"TODO: Support WGPUCompositeAlphaMode_Premultiplied.");
2700+
assert({{{ gpu.PresentMode.Fifo }}} ===
2701+
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.presentMode) }}});
2702+
#endif
2703+
2704+
var canvasSize = [
2705+
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.width) }}},
2706+
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.height) }}}
2707+
];
2708+
2709+
if (canvasSize[0] !== 0) {
2710+
context["canvas"]["width"] = canvasSize[0];
2711+
}
2712+
2713+
if (canvasSize[1] !== 0) {
2714+
context["canvas"]["height"] = canvasSize[1];
2715+
}
2716+
2717+
var configuration = {
2718+
"device": WebGPU.mgrDevice.get(deviceId),
2719+
"format": WebGPU.TextureFormat[
2720+
{{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.format) }}}],
2721+
"usage": {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.usage) }}},
2722+
"alphaMode": "opaque",
2723+
};
2724+
context.configure(configuration);
2725+
},
2726+
2727+
wgpuSurfaceGetCurrentTexture: (surfaceId, surfaceTexturePtr) => {
2728+
{{{ gpu.makeCheck('surfaceTexturePtr') }}}
2729+
var context = WebGPU.mgrSurface.get(surfaceId);
2730+
2731+
try {
2732+
var texture = WebGPU.mgrTexture.create(context.getCurrentTexture());
2733+
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.texture, 'texture', '*') }}};
2734+
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.suboptimal, '0', 'i32') }}};
2735+
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.status,
2736+
gpu.SurfaceGetCurrentTextureStatus.Success, 'i32') }}};
2737+
} catch (ex) {
2738+
#if ASSERTIONS
2739+
err(`wgpuSurfaceGetCurrentTexture() failed: ${ex}`);
2740+
#endif
2741+
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.texture, '0', '*') }}};
2742+
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.suboptimal, '0', 'i32') }}};
2743+
// TODO(https://github.com/webgpu-native/webgpu-headers/issues/291): What should the status be here?
2744+
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.status,
2745+
gpu.SurfaceGetCurrentTextureStatus.DeviceLost, 'i32') }}};
2746+
}
2747+
},
2748+
26792749
wgpuSurfaceGetPreferredFormat: (surfaceId, adapterId) => {
26802750
var format = navigator["gpu"]["getPreferredCanvasFormat"]();
26812751
return WebGPU.Int_PreferredFormat[format];
26822752
},
26832753

2754+
wgpuSurfacePresent: (surfaceId) => {
2755+
// TODO: This could probably be emulated with ASYNCIFY.
2756+
abort('wgpuSurfacePresent is unsupported (use requestAnimationFrame via html5.h instead)');
2757+
},
2758+
2759+
wgpuSurfaceUnconfigure: (surfaceId) => {
2760+
var context = WebGPU.mgrSurface.get(surfaceId);
2761+
context.unconfigure();
2762+
},
2763+
26842764
// WGPUSwapChain
26852765

26862766
wgpuDeviceCreateSwapChain: (deviceId, surfaceId, descriptor) => {

src/struct_info.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,27 @@
14801480
"format",
14811481
"viewDimension"
14821482
],
1483+
"WGPUSurfaceCapabilities": [
1484+
"nextInChain",
1485+
"formatCount",
1486+
"formats",
1487+
"presentModeCount",
1488+
"presentModes",
1489+
"alphaModeCount",
1490+
"alphaModes"
1491+
],
1492+
"WGPUSurfaceConfiguration": [
1493+
"nextInChain",
1494+
"device",
1495+
"format",
1496+
"usage",
1497+
"viewFormatCount",
1498+
"viewFormats",
1499+
"alphaMode",
1500+
"width",
1501+
"height",
1502+
"presentMode"
1503+
],
14831504
"WGPUSurfaceDescriptor": [
14841505
"nextInChain",
14851506
"label"
@@ -1488,6 +1509,11 @@
14881509
"chain",
14891510
"selector"
14901511
],
1512+
"WGPUSurfaceTexture": [
1513+
"texture",
1514+
"suboptimal",
1515+
"status"
1516+
],
14911517
"WGPUSwapChainDescriptor": [
14921518
"nextInChain",
14931519
"label",

system/include/webgpu/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ These files, and several snippets of other files, are generated by Dawn
22
(Chromium's WebGPU library):
33
- [Generator](https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/generator/)
44
- [Generator input](https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/dawn.json)
5-
- [Generator output](https://source.chromium.org/chromium/chromium/src/+/main:out/Debug/gen/third_party/dawn/emscripten-bits/)
5+
- [Generator output](https://source.chromium.org/chromium/chromium/src/+/main:out/linux-Debug/gen/third_party/dawn/emscripten-bits/)
66

77
The C header is intended to be mostly the same as the "upstream"
88
[`webgpu.h`](https://github.com/webgpu-native/webgpu-headers/blob/main/webgpu.h),
@@ -16,13 +16,13 @@ is included here because it is strongly tied to an exact `webgpu.h` revision.
1616
To update these bindings from Dawn:
1717
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`
1818
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
19-
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
19+
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
2020
- `system/include/webgpu/webgpu.h`
2121
- `system/include/webgpu/webgpu_cpp.h`
2222
- `system/include/webgpu/webgpu_cpp_chained_struct.h`
2323
- `system/lib/webgpu/webgpu_cpp.cpp`
24-
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)
25-
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).
24+
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)
25+
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).
2626
1. **Manually update the `globalThis.gpu` compile-time enum tables (AdapterType, BackendType, etc.)**:
2727
- 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.)
2828
1. **Manually update the "Map from enum string back to enum number" tables.**

0 commit comments

Comments
 (0)