From b58836f2875e613d6b0fc5416ff7f30dcc2198e8 Mon Sep 17 00:00:00 2001 From: manuel Date: Fri, 1 May 2026 12:57:48 +0200 Subject: [PATCH] Backends: WebGPU: detect WGSL support at runtime instead of excluding WGVK at compile time. (#9387) Previously WGVK was hard-disabled from WGSL via #if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGVK), forcing the SPIRV fallback unconditionally. Now the WGSL path is attempted on all backends and an empty stage_desc is returned when the module fails to compile letting the existing SPIRV fallback at the call site kick in. --- backends/imgui_impl_wgpu.cpp | 28 ++++++++++++++++++++++++---- docs/CHANGELOG.txt | 2 +- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/backends/imgui_impl_wgpu.cpp b/backends/imgui_impl_wgpu.cpp index 5636178b5..cd952cdc2 100644 --- a/backends/imgui_impl_wgpu.cpp +++ b/backends/imgui_impl_wgpu.cpp @@ -328,11 +328,31 @@ static WGPUProgrammableStageDescriptor ImGui_ImplWGPU_CreateShaderModuleWGSL(con WGPUShaderModuleDescriptor desc = {}; desc.nextInChain = (WGPUChainedStruct*)&wgsl_desc; + // Detect shader compilation errors by using an error scope. + // Flag to be passed into the validation callback `userdata1` pointer. + int validation_error = 0; + wgpuDevicePushErrorScope(bd->wgpuDevice, WGPUErrorFilter_Validation); + WGPUShaderModule module = wgpuDeviceCreateShaderModule(bd->wgpuDevice, &desc); + WGPUPopErrorScopeCallbackInfo pop_cb = {}; + pop_cb.mode = WGPUCallbackMode_AllowSpontaneous; + pop_cb.callback = [](WGPUPopErrorScopeStatus, WGPUErrorType type, WGPUStringView, void* userdata1, void*) + { + if (type == WGPUErrorType_Validation) + *static_cast(userdata1) = 1; + }; + pop_cb.userdata1 = &validation_error; + wgpuDevicePopErrorScope(bd->wgpuDevice, pop_cb); + WGPUProgrammableStageDescriptor stage_desc = {}; -#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGVK) - stage_desc.module = wgpuDeviceCreateShaderModule(bd->wgpuDevice, &desc); - stage_desc.entryPoint = { "main", WGPU_STRLEN }; -#endif + if (module && !validation_error) + { + stage_desc.module = module; + stage_desc.entryPoint = { "main", WGPU_STRLEN }; + } + else if (module) + { + wgpuShaderModuleRelease(module); + } return stage_desc; } diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index da8b6fa44..45d3124ee 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -213,7 +213,7 @@ Other Changes: - SDL2: made `ImGui_ImplSDL2_GetContentScaleForWindow()`/`ImGui_ImplSDL2_GetContentScaleForDisplay()` helpers return a minimum of 1.0f, as some Linux setup seems to report <1.0f value and this breaks scaling border size. (#9369) - - WebGPU: always use SPIR-V shader on WGVK, as it cannot be detected at runtime. (#9316, #9246, #9257) + - WebGPU: rework choice/detection of using WGSL/SPIR-V shader on WGVK. (#9316, #9246, #9257, #9387) - Examples: - Update VS toolset in all .vcxproj from VS2015 (v140) to VS2017 (v141). The later is the first that supports vcpkg. Onward we will likely stop testing building the library with VS2015.