| 1 | // dear imgui: Renderer for WebGPU |
| 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) |
| 4 | |
| 5 | // When targeting native platforms: |
| 6 | // - One of IMGUI_IMPL_WEBGPU_BACKEND_DAWN, IMGUI_IMPL_WEBGPU_BACKEND_WGPU or IMGUI_IMPL_WEBGPU_BACKEND_WGVK *must* be provided. |
| 7 | // When targeting Emscripten: |
| 8 | // - We now defaults to IMGUI_IMPL_WEBGPU_BACKEND_DAWN and requires Emscripten 4.0.10+, which correspond to using Emscripten '--use-port=emdawnwebgpu'. |
| 9 | // - Emscripten < 4.0.10 is not supported anymore (old '-sUSE_WEBGPU=1' option). |
| 10 | // - We can still define IMGUI_IMPL_WEBGPU_BACKEND_WGPU to use Emscripten '-s USE_WEBGPU=1' which is marked as obsolete by Emscripten. |
| 11 | // Add #define to your imconfig.h file, or as a compilation flag in your build system. |
| 12 | // This requirement may be removed once WebGPU stabilizes and backends converge on a unified interface. |
| 13 | //#define IMGUI_IMPL_WEBGPU_BACKEND_DAWN |
| 14 | //#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU |
| 15 | //#define IMGUI_IMPL_WEBGPU_BACKEND_WGVK |
| 16 | |
| 17 | // Implemented features: |
| 18 | // [X] Renderer: User texture binding. Use 'WGPUTextureView' as ImTextureID. Read the FAQ about ImTextureID/ImTextureRef! |
| 19 | // [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
| 20 | // [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'. |
| 21 | // [X] Renderer: Texture updates support for dynamic font system (ImGuiBackendFlags_RendererHasTextures). |
| 22 | // Missing features or Issues: |
| 23 | // [ ] Renderer: Multi-viewport support (multiple windows), useful for desktop. |
| 24 | |
| 25 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 26 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 27 | // Learn about Dear ImGui: |
| 28 | // - FAQ https://dearimgui.com/faq |
| 29 | // - Getting Started https://dearimgui.com/getting-started |
| 30 | // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 31 | // - Introduction, links and more at the top of imgui.cpp |
| 32 | |
| 33 | #pragma once |
| 34 | #include "imgui.h" // IMGUI_IMPL_API |
| 35 | #ifndef IMGUI_DISABLE |
| 36 | |
| 37 | // Setup Emscripten default if not specified. |
| 38 | #if defined(__EMSCRIPTEN__) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) |
| 39 | #include <emscripten/version.h> |
| 40 | #define IMGUI_IMPL_WEBGPU_BACKEND_DAWN |
| 41 | #endif |
| 42 | |
| 43 | #include <webgpu/webgpu.h> |
| 44 | #if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) && !defined(__EMSCRIPTEN__) |
| 45 | #include <webgpu/wgpu.h> // WGPULogLevel |
| 46 | #endif |
| 47 | |
| 48 | // Initialization data, for ImGui_ImplWGPU_Init() |
| 49 | struct ImGui_ImplWGPU_InitInfo |
| 50 | { |
| 51 | WGPUDevice Device = nullptr; |
| 52 | int NumFramesInFlight = 3; |
| 53 | WGPUTextureFormat RenderTargetFormat = WGPUTextureFormat_Undefined; |
| 54 | WGPUTextureFormat DepthStencilFormat = WGPUTextureFormat_Undefined; |
| 55 | WGPUMultisampleState PipelineMultisampleState = {}; |
| 56 | |
| 57 | ImGui_ImplWGPU_InitInfo() |
| 58 | { |
| 59 | PipelineMultisampleState.count = 1; |
| 60 | PipelineMultisampleState.mask = UINT32_MAX; |
| 61 | PipelineMultisampleState.alphaToCoverageEnabled = false; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | // Follow "Getting Started" link and check examples/ folder to learn about using backends! |
| 66 | IMGUI_IMPL_API bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info); |
| 67 | IMGUI_IMPL_API void ImGui_ImplWGPU_Shutdown(); |
| 68 | IMGUI_IMPL_API void ImGui_ImplWGPU_NewFrame(); |
| 69 | IMGUI_IMPL_API void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder pass_encoder); |
| 70 | |
| 71 | // Use if you want to reset your rendering device without losing Dear ImGui state. |
| 72 | IMGUI_IMPL_API bool ImGui_ImplWGPU_CreateDeviceObjects(); |
| 73 | IMGUI_IMPL_API void ImGui_ImplWGPU_InvalidateDeviceObjects(); |
| 74 | |
| 75 | // (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = nullptr to handle this manually. |
| 76 | IMGUI_IMPL_API void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex); |
| 77 | |
| 78 | // [BETA] Selected render state data shared with callbacks. |
| 79 | // This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplWGPU_RenderDrawData() call. |
| 80 | // (Please open an issue if you feel you need access to more data) |
| 81 | struct ImGui_ImplWGPU_RenderState |
| 82 | { |
| 83 | WGPUDevice Device; |
| 84 | WGPURenderPassEncoder RenderPassEncoder; |
| 85 | }; |
| 86 | |
| 87 | //------------------------------------------------------------------------- |
| 88 | // Internal Helpers |
| 89 | // Those are currently used by our example applications. |
| 90 | //------------------------------------------------------------------------- |
| 91 | |
| 92 | // (Optional) Helper to wrap some of the Dawn/WGPU/Emscripten quirks |
| 93 | bool ImGui_ImplWGPU_IsSurfaceStatusError(WGPUSurfaceGetCurrentTextureStatus status); |
| 94 | bool ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(WGPUSurfaceGetCurrentTextureStatus status); // Return whether the texture is suboptimal and may need to be recreated. |
| 95 | |
| 96 | // (Optional) Helper for debugging/logging |
| 97 | void ImGui_ImplWGPU_DebugPrintAdapterInfo(const WGPUAdapter& adapter); |
| 98 | const char* ImGui_ImplWGPU_GetBackendTypeName(WGPUBackendType type); |
| 99 | const char* ImGui_ImplWGPU_GetAdapterTypeName(WGPUAdapterType type); |
| 100 | #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) |
| 101 | const char* ImGui_ImplWGPU_GetDeviceLostReasonName(WGPUDeviceLostReason type); |
| 102 | const char* ImGui_ImplWGPU_GetErrorTypeName(WGPUErrorType type); |
| 103 | #elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) |
| 104 | const char* ImGui_ImplWGPU_GetLogLevelName(WGPULogLevel level); |
| 105 | #endif |
| 106 | |
| 107 | // (Optional) Helper to create a surface on macOS/Wayland/X11/Window |
| 108 | #ifndef __EMSCRIPTEN__ |
| 109 | struct ImGui_ImplWGPU_CreateSurfaceInfo |
| 110 | { |
| 111 | WGPUInstance Instance; |
| 112 | const char* System; // "cocoa" | "wayland" | "x11" | "win32" |
| 113 | void* RawWindow; // NSWindow* | 0 | Window | HWND |
| 114 | void* RawDisplay; // 0 | wl_display* | Display* | 0 |
| 115 | void* RawSurface; // | wl_surface* | 0 | 0 |
| 116 | void* RawInstance; // 0 | 0 | 0 | HINSTANCE |
| 117 | }; |
| 118 | WGPUSurface ImGui_ImplWGPU_CreateWGPUSurfaceHelper(ImGui_ImplWGPU_CreateSurfaceInfo* info); |
| 119 | #endif // #ifndef __EMSCRIPTEN__ |
| 120 | |
| 121 | #endif // #ifndef IMGUI_DISABLE |