Skip to content

Commit

Permalink
Add Surface API
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed May 14, 2024
1 parent 799a1cb commit 533d5f6
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 40 deletions.
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
43 changes: 43 additions & 0 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ wgpu${type}Release: (id) => WebGPU.mgr${type}.release(id),`;
DeviceLost: 2,
Unknown: 3,
},
CompositeAlphaMode: [
'auto',
'opaque',
'premultiplied',
'unpremultiplied',
'inherit',
],
CreatePipelineAsyncStatus: {
Success: 0,
ValidationError: 1,
Expand Down Expand Up @@ -222,6 +229,7 @@ var LibraryWebGPU = {
}

{{{ gpu.makeInitManager('Surface') }}}
{{{ gpu.makeInitManager('SurfaceTexture') }}}
{{{ gpu.makeInitManager('SwapChain') }}}

{{{ gpu.makeInitManager('Adapter') }}}
Expand Down Expand Up @@ -568,6 +576,14 @@ var LibraryWebGPU = {
'store',
'discard',
],
SurfaceGetCurrentTextureStatus: [
'success',
'timeout',
'outdated',
'lost',
'out-of-memory',
'device-lost',
],
TextureAspect: [
undefined,
'all',
Expand Down Expand Up @@ -2673,13 +2689,40 @@ var LibraryWebGPU = {
sampler.label = UTF8ToString(labelPtr);
},

// WGPUSurfaceCapabilities

wgpuSurfaceCapabilitiesFreeMembers: (value) => {
// TODO
},

// WGPUSurface

wgpuSurfaceConfigure: (surfaceId, configPtr) => {
// TODO
},

wgpuSurfaceGetCapabilities: (surfaceId, adapterId, capabilitiesPtr) => {
// TODO
},

wgpuSurfaceGetCurrentTexture: (surfaceId, surfaceTexturePtr) => {
var context = WebGPU.mgrSurface.get(surfaceId);
// TODO: WebGPU.mgrSurfaceTexture.create(context);
},

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

wgpuSurfacePresent: (surfaceId) => {
// TODO
},

wgpuSurfaceUnconfigure: (surfaceId) => {
// TODO
},

// 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

0 comments on commit 533d5f6

Please sign in to comment.