WebGPU: Emscripten fixed PR #3780. (#3795)

This commit is contained in:
Branimir Karadžić
2026-06-28 00:39:55 -07:00
committed by GitHub
parent 67893bcf71
commit ecc78ad376
4 changed files with 87 additions and 11 deletions
+81 -4
View File
@@ -887,6 +887,12 @@ WGPU_IMPORT
renderCtx->m_device = _device;
}
WGPUWaitStatus waitForFuture(WGPUFutureWaitInfo& _fwi)
{
wgpuInstanceProcessEvents(m_instance);
return wgpuInstanceWaitAny(m_instance, 1, &_fwi, UINT64_MAX);
}
bool init(const Init& _init)
{
struct ErrorState
@@ -923,6 +929,7 @@ WGPU_IMPORT
bool imported = true;
#if !BX_PLATFORM_EMSCRIPTEN
m_webgpuDll = bx::dlopen(
#if BX_PLATFORM_WINDOWS
"webgpu_dawn.dll"
@@ -970,12 +977,21 @@ WGPU_IMPORT
goto error;
}
#else // BX_PLATFORM_EMSCRIPTEN
BX_TRACE("Emscripten: Using statically linked WebGPU.");
m_webgpuDll = NULL;
errorState = ErrorState::LoadedWebGPU;
#endif // !BX_PLATFORM_EMSCRIPTEN
{
{
WGPUInstanceFeatureName requiredFeatures[] =
{
WGPUInstanceFeatureName_TimedWaitAny,
#if !BX_PLATFORM_EMSCRIPTEN
WGPUInstanceFeatureName_ShaderSourceSPIRV,
#endif // !BX_PLATFORM_EMSCRIPTEN
};
WGPUInstanceDescriptor instanceDesc =
@@ -1021,7 +1037,7 @@ WGPU_IMPORT
.completed = false,
};
WGPUWaitStatus waitStatus = wgpuInstanceWaitAny(m_instance, 1, &fwi, UINT64_MAX);
WGPUWaitStatus waitStatus = waitForFuture(fwi);
if (WGPUWaitStatus_Success != waitStatus
|| NULL == m_adapter)
@@ -1157,15 +1173,18 @@ WGPU_IMPORT
if (WGPUStatus_Success == status)
{
#if !BX_PLATFORM_EMSCRIPTEN
requiredLimits.maxComputeWorkgroupSizeX = 1024;
requiredLimits.maxComputeWorkgroupSizeY = 1024;
requiredLimits.maxComputeWorkgroupSizeZ = 64;
#endif // !BX_PLATFORM_EMSCRIPTEN
}
static constexpr uint32_t kMaxEnabledTogles = 10;
const char* enabledToggles[kMaxEnabledTogles];
uint32_t enabledTogglesCount = 0;
#if !BX_PLATFORM_EMSCRIPTEN
enabledToggles[enabledTogglesCount++] = "allow_unsafe_apis"; // TimestampWrite requires this.
if (_init.debug)
@@ -1207,10 +1226,15 @@ WGPU_IMPORT
.disabledToggleCount = 0,
.disabledToggles = NULL,
};
#endif // !BX_PLATFORM_EMSCRIPTEN
WGPUDeviceDescriptor deviceDesc =
{
#if BX_PLATFORM_EMSCRIPTEN
.nextInChain = NULL,
#else
.nextInChain = &dawnTogglesDescriptor.chain,
#endif // BX_PLATFORM_EMSCRIPTEN
.label = WGPU_STRING_VIEW_INIT,
.requiredFeatureCount = requiredFeatureCount,
.requiredFeatures = requiredFeatures,
@@ -1219,7 +1243,10 @@ WGPU_IMPORT
.deviceLostCallbackInfo =
{
.nextInChain = NULL,
.mode = WGPUCallbackMode_WaitAnyOnly,
.mode = BX_ENABLED(BX_PLATFORM_EMSCRIPTEN)
? WGPUCallbackMode_AllowSpontaneous
: WGPUCallbackMode_WaitAnyOnly
,
.callback = deviceLostCb,
.userdata1 = this,
.userdata2 = NULL,
@@ -1246,7 +1273,7 @@ WGPU_IMPORT
.completed = false,
};
WGPUWaitStatus waitStatus = wgpuInstanceWaitAny(m_instance, 1, &fwi, UINT64_MAX);
WGPUWaitStatus waitStatus = waitForFuture(fwi);
if (WGPUWaitStatus_Success != waitStatus
|| NULL == m_device)
@@ -4340,7 +4367,7 @@ WGPU_IMPORT
{
m_resolution = _resolution;
WGPUSurfaceCapabilities surfaceCaps;
WGPUSurfaceCapabilities surfaceCaps = WGPU_SURFACE_CAPABILITIES_INIT;
WGPUStatus status = WGPU_CHECK(wgpuSurfaceGetCapabilities(m_surface, s_renderWGPU->m_adapter, &surfaceCaps) );
if (WGPUStatus_Success != status)
@@ -4364,6 +4391,24 @@ m_resolution.formatColor = TextureFormat::BGRA8;
}
}
#if BX_PLATFORM_EMSCRIPTEN
if (WGPUTextureFormat_Undefined != format
&& surfaceCaps.formatCount > 0
&& format != surfaceCaps.formats[0])
{
format = surfaceCaps.formats[0];
if (WGPUTextureFormat_BGRA8Unorm == format)
{
m_resolution.formatColor = TextureFormat::BGRA8;
}
else if (WGPUTextureFormat_RGBA8Unorm == format)
{
m_resolution.formatColor = TextureFormat::RGBA8;
}
}
#endif // BX_PLATFORM_EMSCRIPTEN
BX_ASSERT(WGPUTextureFormat_Undefined != format, "SwapChain surface format is not available!");
m_surfaceConfig =
@@ -4389,6 +4434,9 @@ m_resolution.formatColor = TextureFormat::BGRA8;
const uint32_t msaa = s_msaa[(_resolution.reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT];
wgpuRelease(m_depthStencilView);
wgpuRelease(m_msaaTextureView);
if (bimg::isDepth(bimg::TextureFormat::Enum(m_resolution.formatDepthStencil) ) )
{
WGPUTextureDescriptor textureDesc =
@@ -4474,6 +4522,7 @@ m_resolution.formatColor = TextureFormat::BGRA8;
};
m_msaaTextureView = WGPU_CHECK(wgpuTextureCreateView(texture, &textureViewDesc) );
wgpuRelease(texture);
}
return true;
@@ -4645,6 +4694,26 @@ m_resolution.formatColor = TextureFormat::BGRA8;
.layer = toMetalLayer(m_nwh),
};
surfaceDesc =
{
.nextInChain = &surfaceSource.chain,
.label = toWGPUStringView("SwapChainWGPU"),
};
#elif BX_PLATFORM_EMSCRIPTEN
WGPUEmscriptenSurfaceSourceCanvasHTMLSelector surfaceSource =
{
.chain =
{
.next = NULL,
.sType = WGPUSType_EmscriptenSurfaceSourceCanvasHTMLSelector,
},
.selector =
{
.data = static_cast<const char*>(m_nwh),
.length = uint32_t(bx::strLen(static_cast<const char*>(m_nwh))),
},
};
surfaceDesc =
{
.nextInChain = &surfaceSource.chain,
@@ -4662,7 +4731,9 @@ m_resolution.formatColor = TextureFormat::BGRA8;
void SwapChainWGPU::present()
{
wgpuRelease(m_textureView);
#if !BX_PLATFORM_EMSCRIPTEN
WGPU_CHECK(wgpuSurfacePresent(m_surface) );
#endif // !BX_PLATFORM_EMSCRIPTEN
WGPUSurfaceTexture surfaceTexture = WGPU_SURFACE_TEXTURE_INIT;
wgpuSurfaceGetCurrentTexture(m_surface, &surfaceTexture);
@@ -4890,17 +4961,23 @@ m_resolution.formatColor = TextureFormat::BGRA8;
wgpuRelease(m_commandEncoder);
++m_counter;
#if !BX_PLATFORM_EMSCRIPTEN
WGPU_CHECK(wgpuInstanceProcessEvents(s_renderWGPU->m_instance) );
#endif // !BX_PLATFORM_EMSCRIPTEN
m_commandEncoder = WGPU_CHECK(wgpuDeviceCreateCommandEncoder(s_renderWGPU->m_device, NULL) );
}
void CommandQueueWGPU::wait()
{
#if BX_PLATFORM_EMSCRIPTEN
wgpuInstanceProcessEvents(s_renderWGPU->m_instance);
#else
while (0 < m_counter)
{
WGPU_CHECK(wgpuInstanceProcessEvents(s_renderWGPU->m_instance) );
}
#endif // BX_PLATFORM_EMSCRIPTEN
}
void CommandQueueWGPU::frame()