Skip to content

Commit 3dc511c

Browse files
BrutPittocornut
authored andcommitted
Backends: WebGPU: update to compile with Dawn and Emscripten's 4.0.10+ '--use-port=emdawnwebgpu' ports. (#8381, #8898)
1 parent 36de604 commit 3dc511c

File tree

3 files changed

+64
-36
lines changed

3 files changed

+64
-36
lines changed

backends/imgui_impl_wgpu.cpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// dear imgui: Renderer for WebGPU
2-
// This needs to be used along with a Platform Binding (e.g. GLFW)
3-
// (Please note that WebGPU is currently experimental, will not run on non-beta browsers, and may break.)
2+
// This needs to be used along with a Platform Binding (e.g. GLFW, SDL2, SDL3)
3+
// (Please note that WebGPU is a recent API, may not be supported by all browser, and its ecosystem is generally a mess)
44

55
// Implemented features:
66
// [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID/ImTextureRef!
77
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset).
88
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'.
99
// [X] Renderer: Texture updates support for dynamic font system (ImGuiBackendFlags_RendererHasTextures).
1010

11+
// Read imgui_impl_wgpu.h about how to use the IMGUI_IMPL_WEBGPU_BACKEND_WGPU or IMGUI_IMPL_WEBGPU_BACKEND_DAWN flags.
12+
1113
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
1214
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
1315
// Learn about Dear ImGui:
@@ -18,6 +20,7 @@
1820

1921
// CHANGELOG
2022
// (minor and older changes stripped away, please see git history for details)
23+
// 2025-10-16: Update to compile with Dawn and Emscripten's 4.0.10+ '--use-port=emdawnwebgpu' ports. (#8381, #8898)
2124
// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown.
2225
// 2025-06-12: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. (#8465)
2326
// 2025-02-26: Recreate image bind groups during render. (#8426, #8046, #7765, #8027) + Update for latest webgpu-native changes.
@@ -46,20 +49,20 @@
4649

4750
#include "imgui.h"
4851

49-
// When targeting native platforms (i.e. NOT Emscripten), one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN
50-
// or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.
51-
#ifndef __EMSCRIPTEN__
52-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
53-
#error exactly one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be defined!
54-
#endif
55-
#else
56-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
57-
#error neither IMGUI_IMPL_WEBGPU_BACKEND_DAWN nor IMGUI_IMPL_WEBGPU_BACKEND_WGPU may be defined if targeting emscripten!
58-
#endif
59-
#endif
60-
6152
#ifndef IMGUI_DISABLE
6253
#include "imgui_impl_wgpu.h"
54+
55+
// One of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.
56+
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
57+
#error Exactly one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be defined!
58+
#endif
59+
60+
// This condition is true when it's built with EMSCRIPTEN using -sUSE_WEBGPU=1 flag (deprecated from 4.0.10)
61+
// This condition is false for all other 3 cases: WGPU-Native, DAWN-Native or DAWN-EMSCRIPTEN (using --use-port=emdawnwebgpu flag)
62+
#if defined(__EMSCRIPTEN__) && defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
63+
#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN
64+
#endif
65+
6366
#include <limits.h>
6467
#include <webgpu/webgpu.h>
6568

@@ -259,7 +262,7 @@ static WGPUProgrammableStageDescriptor ImGui_ImplWGPU_CreateShaderModule(const c
259262
{
260263
ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData();
261264

262-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
265+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
263266
WGPUShaderSourceWGSL wgsl_desc = {};
264267
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
265268
wgsl_desc.code = { wgsl_source, WGPU_STRLEN };
@@ -275,7 +278,7 @@ static WGPUProgrammableStageDescriptor ImGui_ImplWGPU_CreateShaderModule(const c
275278
WGPUProgrammableStageDescriptor stage_desc = {};
276279
stage_desc.module = wgpuDeviceCreateShaderModule(bd->wgpuDevice, &desc);
277280

278-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
281+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
279282
stage_desc.entryPoint = { "main", WGPU_STRLEN };
280283
#else
281284
stage_desc.entryPoint = "main";
@@ -398,9 +401,10 @@ void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder
398401
WGPUBufferDescriptor vb_desc =
399402
{
400403
nullptr,
404+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
405+
{ "Dear ImGui Vertex buffer", WGPU_STRLEN, },
406+
#else
401407
"Dear ImGui Vertex buffer",
402-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
403-
WGPU_STRLEN,
404408
#endif
405409
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
406410
MEMALIGN(fr->VertexBufferSize * sizeof(ImDrawVert), 4),
@@ -425,9 +429,10 @@ void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder
425429
WGPUBufferDescriptor ib_desc =
426430
{
427431
nullptr,
432+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
433+
{ "Dear ImGui Index buffer", WGPU_STRLEN, },
434+
#else
428435
"Dear ImGui Index buffer",
429-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
430-
WGPU_STRLEN,
431436
#endif
432437
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Index,
433438
MEMALIGN(fr->IndexBufferSize * sizeof(ImDrawIdx), 4),
@@ -560,7 +565,7 @@ void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex)
560565

561566
// Create texture
562567
WGPUTextureDescriptor tex_desc = {};
563-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
568+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
564569
tex_desc.label = { "Dear ImGui Texture", WGPU_STRLEN };
565570
#else
566571
tex_desc.label = "Dear ImGui Texture";
@@ -605,7 +610,7 @@ void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex)
605610

606611
// Update full texture or selected blocks. We only ever write to textures regions which have never been used before!
607612
// This backend choose to use tex->UpdateRect but you can use tex->Updates[] to upload individual regions.
608-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
613+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
609614
WGPUTexelCopyTextureInfo dst_view = {};
610615
#else
611616
WGPUImageCopyTexture dst_view = {};
@@ -614,7 +619,7 @@ void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex)
614619
dst_view.mipLevel = 0;
615620
dst_view.origin = { (uint32_t)upload_x, (uint32_t)upload_y, 0 };
616621
dst_view.aspect = WGPUTextureAspect_All;
617-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
622+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
618623
WGPUTexelCopyBufferLayout layout = {};
619624
#else
620625
WGPUTextureDataLayout layout = {};
@@ -636,9 +641,10 @@ static void ImGui_ImplWGPU_CreateUniformBuffer()
636641
WGPUBufferDescriptor ub_desc =
637642
{
638643
nullptr,
644+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
645+
{ "Dear ImGui Uniform buffer", WGPU_STRLEN, },
646+
#else
639647
"Dear ImGui Uniform buffer",
640-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
641-
WGPU_STRLEN,
642648
#endif
643649
WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
644650
MEMALIGN(sizeof(Uniforms), 16),
@@ -751,7 +757,7 @@ bool ImGui_ImplWGPU_CreateDeviceObjects()
751757
// Create depth-stencil State
752758
WGPUDepthStencilState depth_stencil_state = {};
753759
depth_stencil_state.format = bd->depthStencilFormat;
754-
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) || defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
760+
#if !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN)
755761
depth_stencil_state.depthWriteEnabled = WGPUOptionalBool_False;
756762
#else
757763
depth_stencil_state.depthWriteEnabled = false;
@@ -833,14 +839,18 @@ bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info)
833839
// Setup backend capabilities flags
834840
ImGui_ImplWGPU_Data* bd = IM_NEW(ImGui_ImplWGPU_Data)();
835841
io.BackendRendererUserData = (void*)bd;
842+
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
836843
#if defined(__EMSCRIPTEN__)
837-
io.BackendRendererName = "imgui_impl_webgpu_emscripten";
838-
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
839-
io.BackendRendererName = "imgui_impl_webgpu_dawn";
844+
io.BackendRendererName = "imgui_impl_wgpu (Dawn, Emscripten)"; // compiled & linked using EMSCRIPTEN with "--use-port=emdawnwebgpu" flag
845+
#else
846+
io.BackendRendererName = "imgui_impl_wgpu (Dawn, Native)";
847+
#endif
840848
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
841-
io.BackendRendererName = "imgui_impl_webgpu_wgpu";
849+
#if defined(__EMSCRIPTEN__)
850+
io.BackendRendererName = "imgui_impl_wgpu (WGPU, Emscripten)"; // linked using EMSCRIPTEN with "-sUSE_WEBGPU=1" flag, deprecated from EMSCRIPTEN 4.0.10
842851
#else
843-
io.BackendRendererName = "imgui_impl_webgpu";
852+
io.BackendRendererName = "imgui_impl_wgpu (WGPU, Native)";
853+
#endif
844854
#endif
845855
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
846856
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render.

backends/imgui_impl_wgpu.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// dear imgui: Renderer for WebGPU
2-
// This needs to be used along with a Platform Binding (e.g. GLFW)
3-
// (Please note that WebGPU is currently experimental, will not run on non-beta browsers, and may break.)
2+
// This needs to be used along with a Platform Binding (e.g. GLFW, SDL2, SDL3)
3+
// (Please note that WebGPU is a recent API, may not be supported by all browser, and its ecosystem is generally a mess)
44

5-
// Important note to dawn and/or wgpu users: when targeting native platforms (i.e. NOT emscripten),
6-
// one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided.
5+
// When targeting native platforms:
6+
// - One of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU *must* be provided.
7+
// When targeting Emscripten:
8+
// - We now defaults to IMGUI_IMPL_WEBGPU_BACKEND_DAWN is Emscripten version is 4.0.10+, which correspond to using Emscripten '--use-port=emdawnwebgpu'.
9+
// - We can still define IMGUI_IMPL_WEBGPU_BACKEND_WGPU to use Emscripten '-s USE_WEBGPU=1' which is marked as obsolete by Emscripten.
710
// Add #define to your imconfig.h file, or as a compilation flag in your build system.
8-
// This requirement will be removed once WebGPU stabilizes and backends converge on a unified interface.
11+
// This requirement may be removed once WebGPU stabilizes and backends converge on a unified interface.
912
//#define IMGUI_IMPL_WEBGPU_BACKEND_DAWN
1013
//#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU
1114

@@ -27,6 +30,16 @@
2730
#include "imgui.h" // IMGUI_IMPL_API
2831
#ifndef IMGUI_DISABLE
2932

33+
// Setup Emscripten default if not specified.
34+
#if defined(__EMSCRIPTEN__) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
35+
#include <emscripten/version.h>
36+
#if (__EMSCRIPTEN_major__ >= 4) && (__EMSCRIPTEN_minor__ >= 0) && (__EMSCRIPTEN_tiny__ >= 10)
37+
#define IMGUI_IMPL_WEBGPU_BACKEND_DAWN
38+
#else
39+
#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU
40+
#endif
41+
#endif
42+
3043
#include <webgpu/webgpu.h>
3144

3245
// Initialization data, for ImGui_ImplWGPU_Init()

docs/CHANGELOG.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ Other Changes:
5353
resizing the parent window while keeping the multi-line field active (which is
5454
most typically achieved when resizing programmatically or via a docking layout
5555
reacting to a platform window resize). (#3237, #9007) [@anton-kl, @ocornut]
56+
- Backends: WebGPU: update to compile with Dawn and Emscripten's 4.0.10+
57+
'--use-port=emdawnwebgpu' ports. (#8381, #8898) [@brutpitt, @trbabb]
58+
When using Emscripten 4.0.10+, backend now defaults to IMGUI_IMPL_WEBGPU_BACKEND_DAWN
59+
instead of IMGUI_IMPL_WEBGPU_BACKEND_WGPU, if neither are specified.
60+
(note: examples application were not updated yet)
5661

5762

5863
-----------------------------------------------------------------------

0 commit comments

Comments
 (0)