imgui_impl_wgpu.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  19. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  20. // Learn about Dear ImGui:
  21. // - FAQ https://dearimgui.com/faq
  22. // - Getting Started https://dearimgui.com/getting-started
  23. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  24. // - Introduction, links and more at the top of imgui.cpp
  25. #pragma once
  26. #include "imgui.h" // IMGUI_IMPL_API
  27. #ifndef IMGUI_DISABLE
  28. // Setup Emscripten default if not specified.
  29. #if defined(__EMSCRIPTEN__) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
  30. #include <emscripten/version.h>
  31. #if (__EMSCRIPTEN_major__ >= 4) && (__EMSCRIPTEN_minor__ >= 0) && (__EMSCRIPTEN_tiny__ >= 10)
  32. #define IMGUI_IMPL_WEBGPU_BACKEND_DAWN
  33. #else
  34. #define IMGUI_IMPL_WEBGPU_BACKEND_WGPU
  35. #endif
  36. #endif
  37. #include <webgpu/webgpu.h>
  38. #if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) && !defined(__EMSCRIPTEN__)
  39. #include <webgpu/wgpu.h> // WGPULogLevel
  40. #endif
  41. // Initialization data, for ImGui_ImplWGPU_Init()
  42. struct ImGui_ImplWGPU_InitInfo
  43. {
  44. WGPUDevice Device = nullptr;
  45. int NumFramesInFlight = 3;
  46. WGPUTextureFormat RenderTargetFormat = WGPUTextureFormat_Undefined;
  47. WGPUTextureFormat DepthStencilFormat = WGPUTextureFormat_Undefined;
  48. WGPUMultisampleState PipelineMultisampleState = {};
  49. ImGui_ImplWGPU_InitInfo()
  50. {
  51. PipelineMultisampleState.count = 1;
  52. PipelineMultisampleState.mask = UINT32_MAX;
  53. PipelineMultisampleState.alphaToCoverageEnabled = false;
  54. }
  55. };
  56. // Follow "Getting Started" link and check examples/ folder to learn about using backends!
  57. IMGUI_IMPL_API bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info);
  58. IMGUI_IMPL_API void ImGui_ImplWGPU_Shutdown();
  59. IMGUI_IMPL_API void ImGui_ImplWGPU_NewFrame();
  60. IMGUI_IMPL_API void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder pass_encoder);
  61. // Use if you want to reset your rendering device without losing Dear ImGui state.
  62. IMGUI_IMPL_API bool ImGui_ImplWGPU_CreateDeviceObjects();
  63. IMGUI_IMPL_API void ImGui_ImplWGPU_InvalidateDeviceObjects();
  64. // (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.
  65. IMGUI_IMPL_API void ImGui_ImplWGPU_UpdateTexture(ImTextureData* tex);
  66. // [BETA] Selected render state data shared with callbacks.
  67. // This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplWGPU_RenderDrawData() call.
  68. // (Please open an issue if you feel you need access to more data)
  69. struct ImGui_ImplWGPU_RenderState
  70. {
  71. WGPUDevice Device;
  72. WGPURenderPassEncoder RenderPassEncoder;
  73. };
  74. //-------------------------------------------------------------------------
  75. // Internal Helpers
  76. // Those are currently used by our example applications.
  77. //-------------------------------------------------------------------------
  78. // (Optional) Helper to wrap some of the Dawn/WGPU/Emscripten quirks
  79. bool ImGui_ImplWGPU_IsSurfaceStatusError(WGPUSurfaceGetCurrentTextureStatus status);
  80. bool ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(WGPUSurfaceGetCurrentTextureStatus status); // Return whether the texture is suboptimal and may need to be recreated.
  81. // (Optional) Helper for debugging/logging
  82. void ImGui_ImplWGPU_DebugPrintAdapterInfo(const WGPUAdapter& adapter);
  83. const char* ImGui_ImplWGPU_GetBackendTypeName(WGPUBackendType type);
  84. const char* ImGui_ImplWGPU_GetAdapterTypeName(WGPUAdapterType type);
  85. #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
  86. const char* ImGui_ImplWGPU_GetDeviceLostReasonName(WGPUDeviceLostReason type);
  87. const char* ImGui_ImplWGPU_GetErrorTypeName(WGPUErrorType type);
  88. #elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) && !defined(__EMSCRIPTEN__)
  89. const char* ImGui_ImplWGPU_GetLogLevelName(WGPULogLevel level);
  90. #endif
  91. // (Optional) Helper to create a surface on macOS/Wayland/X11/Window
  92. #ifndef __EMSCRIPTEN__
  93. struct ImGui_ImplWGPU_CreateSurfaceInfo
  94. {
  95. WGPUInstance Instance;
  96. const char* System; // "cocoa" | "wayland" | "x11" | "win32"
  97. void* RawWindow; // NSWindow* | 0 | Window | HWND
  98. void* RawDisplay; // 0 | wl_display* | Display* | 0
  99. void* RawSurface; // | wl_surface* | 0 | 0
  100. void* RawInstance; // 0 | 0 | 0 | HINSTANCE
  101. };
  102. WGPUSurface ImGui_ImplWGPU_CreateWGPUSurfaceHelper(ImGui_ImplWGPU_CreateSurfaceInfo* info);
  103. #endif // #ifndef __EMSCRIPTEN__
  104. #endif // #ifndef IMGUI_DISABLE