imgui_impl_wgpu.h 6.3 KB

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