From 1ac162f2b083819663fe12b7217c40abdaf501cb Mon Sep 17 00:00:00 2001 From: Aaron C Gaudette Date: Mon, 16 Sep 2024 16:07:02 +0200 Subject: [PATCH] Backends: WGPU: add IMGUI_IMPL_WEBGPU_BACKEND_DAWN/IMGUI_IMPL_WEBGPU_BACKEND_WGPU to support more targets. (#7977, #7969, #6602, #6188, #7523) --- backends/imgui_impl_wgpu.cpp | 29 +++++++++++++++++++++++ backends/imgui_impl_wgpu.h | 7 ++++++ docs/CHANGELOG.txt | 2 ++ examples/example_glfw_wgpu/CMakeLists.txt | 5 ++++ 4 files changed, 43 insertions(+) diff --git a/backends/imgui_impl_wgpu.cpp b/backends/imgui_impl_wgpu.cpp index bf741e1a4..503ac8efd 100644 --- a/backends/imgui_impl_wgpu.cpp +++ b/backends/imgui_impl_wgpu.cpp @@ -16,6 +16,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2024-09-16: Added support for optional IMGUI_IMPL_WEBGPU_BACKEND_DAWN / IMGUI_IMPL_WEBGPU_BACKEND_WGPU define to handle ever-changing native implementations. (#7977) // 2024-01-22: Added configurable PipelineMultisampleState struct. (#7240) // 2024-01-22: (Breaking) ImGui_ImplWGPU_Init() now takes a ImGui_ImplWGPU_InitInfo structure instead of variety of parameters, allowing for easier further changes. // 2024-01-22: Fixed pipeline layout leak. (#7245) @@ -35,6 +36,18 @@ // 2021-02-18: Change blending equation to preserve alpha in output buffer. // 2021-01-28: Initial version. +// When targeting native platforms (i.e. NOT emscripten), one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN +// or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details. +#ifndef __EMSCRIPTEN__ + #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) + #error exactly one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be defined! + #endif +#else + #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) + #error neither IMGUI_IMPL_WEBGPU_BACKEND_DAWN nor IMGUI_IMPL_WEBGPU_BACKEND_WGPU may be defined if targeting emscripten! + #endif +#endif + #include "imgui.h" #ifndef IMGUI_DISABLE #include "imgui_impl_wgpu.h" @@ -245,7 +258,11 @@ static WGPUProgrammableStageDescriptor ImGui_ImplWGPU_CreateShaderModule(const c ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData(); WGPUShaderModuleWGSLDescriptor wgsl_desc = {}; +#ifdef IMGUI_IMPL_WEBGPU_BACKEND_DAWN + wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL; +#else wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; +#endif wgsl_desc.code = wgsl_source; WGPUShaderModuleDescriptor desc = {}; @@ -660,7 +677,11 @@ bool ImGui_ImplWGPU_CreateDeviceObjects() // Create depth-stencil State WGPUDepthStencilState depth_stencil_state = {}; depth_stencil_state.format = bd->depthStencilFormat; +#ifdef IMGUI_IMPL_WEBGPU_BACKEND_DAWN + depth_stencil_state.depthWriteEnabled = WGPUOptionalBool_False; +#else depth_stencil_state.depthWriteEnabled = false; +#endif depth_stencil_state.depthCompare = WGPUCompareFunction_Always; depth_stencil_state.stencilFront.compare = WGPUCompareFunction_Always; depth_stencil_state.stencilFront.failOp = WGPUStencilOperation_Keep; @@ -730,7 +751,15 @@ bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info) // Setup backend capabilities flags ImGui_ImplWGPU_Data* bd = IM_NEW(ImGui_ImplWGPU_Data)(); io.BackendRendererUserData = (void*)bd; +#if defined(__EMSCRIPTEN__) + io.BackendRendererName = "imgui_impl_webgpu_emscripten"; +#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) + io.BackendRendererName = "imgui_impl_webgpu_dawn"; +#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) + io.BackendRendererName = "imgui_impl_webgpu_wgpu"; +#else io.BackendRendererName = "imgui_impl_webgpu"; +#endif io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. bd->initInfo = *init_info; diff --git a/backends/imgui_impl_wgpu.h b/backends/imgui_impl_wgpu.h index 87d9580bf..e6835dee9 100644 --- a/backends/imgui_impl_wgpu.h +++ b/backends/imgui_impl_wgpu.h @@ -2,6 +2,13 @@ // This needs to be used along with a Platform Binding (e.g. GLFW) // (Please note that WebGPU is currently experimental, will not run on non-beta browsers, and may break.) +// Important note to dawn and/or wgpu users: when targeting native platforms (i.e. NOT emscripten), +// one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. +// Add #define to your imconfig.h file, or as a compilation flag in your build system. +// This requirement will be removed once WebGPU stabilizes and backends converge on a unified interface. +//#define IMGUI_IMPL_WEBGPU_BACKEND_DAWN +//#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU + // Implemented features: // [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID! // [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices. diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 069b60388..95df895c4 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -77,6 +77,8 @@ Other changes: - Backends: SDL2: use SDL_Vulkan_GetDrawableSize() when available. (#7967, #3190) [@scribam] - Backends: GLFW+Emscripten: use OSX behaviors automatically when using contrib glfw port. (#7965, #7915) [@ypujante] +- Backends: WebGPU: Added support for optional IMGUI_IMPL_WEBGPU_BACKEND_DAWN / IMGUI_IMPL_WEBGPU_BACKEND_WGPU + defines to handle ever-changing native implementations. (#7977, #7969, #6602, #6188, #7523) [@acgaudette] ----------------------------------------------------------------------- VERSION 1.91.1 (Released 2024-09-04) diff --git a/examples/example_glfw_wgpu/CMakeLists.txt b/examples/example_glfw_wgpu/CMakeLists.txt index 6be95af44..8e164e488 100644 --- a/examples/example_glfw_wgpu/CMakeLists.txt +++ b/examples/example_glfw_wgpu/CMakeLists.txt @@ -79,6 +79,11 @@ add_executable(example_glfw_wgpu ${IMGUI_DIR}/imgui_tables.cpp ${IMGUI_DIR}/imgui_widgets.cpp ) +IF(NOT EMSCRIPTEN) + target_compile_definitions(example_glfw_wgpu PUBLIC + "IMGUI_IMPL_WEBGPU_BACKEND_DAWN" + ) +endif() target_include_directories(example_glfw_wgpu PUBLIC ${IMGUI_DIR} ${IMGUI_DIR}/backends