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 1 commit
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
41 changes: 10 additions & 31 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ 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 @@ -575,14 +568,6 @@ var LibraryWebGPU = {
'store',
'discard',
],
SurfaceGetCurrentTextureStatus: [
'success',
'timeout',
'outdated',
'lost',
'out-of-memory',
'device-lost',
],
TextureAspect: [
undefined,
'all',
Expand Down Expand Up @@ -2734,26 +2719,20 @@ var LibraryWebGPU = {
context.configure(configuration);
},

wgpuSurfaceGetCapabilities: (surfaceId, adapterId, capabilitiesPtr) => {
if (capabilitiesPtr !== 0) {
{{{ makeSetValue('capabilitiesPtr', C_STRUCTS.WGPUSurfaceCapabilities.formatCount, '1', 'i32') }}};
var format = navigator["gpu"]["getPreferredCanvasFormat"]();
var formatsPtr = WebGPU.Int_PreferredFormat[format]; // FIXME
{{{ makeSetValue('capabilitiesPtr', C_STRUCTS.WGPUSurfaceCapabilities.formats, 'formatsPtr', '*') }}};
// "presentModeCount",
// "presentModes",
// "alphaModeCount",
// "alphaModes"
}
},

wgpuSurfaceGetCurrentTexture: (surfaceId, surfaceTexturePtr) => {
{{{ gpu.makeCheck('surfaceTexturePtr') }}}
var context = WebGPU.mgrSurface.get(surfaceId);
var texture = WebGPU.mgrTexture.create(context.getCurrentTexture());
if (surfaceTexturePtr !== 0) {

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, '0', 'i32') }}};
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.status, /*Success=*/0, 'i32') }}};
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
} catch (ex) {
#if ASSERTIONS
err(`wgpuSurfaceGetCurrentTexture() failed: ${ex}`);
#endif
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
{{{ makeSetValue('surfaceTexturePtr', C_STRUCTS.WGPUSurfaceTexture.status, /*Timeout=*/1, 'i32') }}};
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
}
},

Expand Down
32 changes: 32 additions & 0 deletions system/lib/webgpu/webgpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@

#include <webgpu/webgpu.h>

#include <array>
#include <cassert>

static constexpr std::array<WGPUTextureFormat, 3>
kBGRA8UnormPreferredContextFormats = {WGPUTextureFormat_BGRA8Unorm,
WGPUTextureFormat_RGBA8Unorm,
WGPUTextureFormat_RGBA16Float};

static constexpr std::array<WGPUTextureFormat, 3>
kRGBA8UnormPreferredContextFormats = {WGPUTextureFormat_RGBA8Unorm,
WGPUTextureFormat_BGRA8Unorm,
WGPUTextureFormat_RGBA16Float};
kainino0x marked this conversation as resolved.
Show resolved Hide resolved

//
// WebGPU function definitions, with methods organized by "class". Note these
// don't need to be extern "C" because they are already declared in webgpu.h.
Expand All @@ -27,3 +38,24 @@ WGPUInstance wgpuCreateInstance(const WGPUInstanceDescriptor* descriptor) {

void wgpuInstanceReference(WGPUInstance) { /* no-op for now */ }
void wgpuInstanceRelease(WGPUInstance) { /* no-op for now */ }

// WGPUSurface

void wgpuSurfaceGetCapabilities(WGPUSurface surface,
WGPUAdapter adapter,
WGPUSurfaceCapabilities* capabilities) {
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
WGPUTextureFormat preferredFormat =
wgpuSurfaceGetPreferredFormat(surface, adapter);
assert(preferredFormat == WGPUTextureFormat_BGRA8Unorm ||
preferredFormat == WGPUTextureFormat_RGBA8Unorm);
capabilities->formatCount = 3;
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
if (preferredFormat == WGPUTextureFormat_RGBA8Unorm) {
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
capabilities->formats = reinterpret_cast<const WGPUTextureFormat*>(
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
kBGRA8UnormPreferredContextFormats.data());
} else {
capabilities->formats = reinterpret_cast<const WGPUTextureFormat*>(
kRGBA8UnormPreferredContextFormats.data());
}

// TODO: What do we return for presentModes and alphaModes?
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
};
3 changes: 1 addition & 2 deletions test/webgpu_basic_rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ void run() {

wgpu::SurfaceConfiguration config{
.device = device,
.format = wgpu::TextureFormat::BGRA8Unorm,
// .format = capabilities.formats[0],
.format = capabilities.formats[0],
.usage = wgpu::TextureUsage::RenderAttachment,
.alphaMode = capabilities.alphaModes[0],
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
.width = kWidth,
Expand Down
Loading