Преглед на файлове

Backends: WebGPU: ImGui_ImplWGPU_Init() now takes a ImGui_ImplWGPU_InitInfo structure instead of variety of parameters, allowing for easier further changes. (#7240)

ocornut преди 1 година
родител
ревизия
831d42c1ab
променени са 4 файла, в които са добавени 38 реда и са изтрити 19 реда
  1. 20 17
      backends/imgui_impl_wgpu.cpp
  2. 10 1
      backends/imgui_impl_wgpu.h
  3. 2 0
      docs/CHANGELOG.txt
  4. 6 1
      examples/example_emscripten_wgpu/main.cpp

+ 20 - 17
backends/imgui_impl_wgpu.cpp

@@ -16,6 +16,7 @@
 
 // CHANGELOG
 // (minor and older changes stripped away, please see git history for details)
+//  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-17: Explicitly fill all of WGPUDepthStencilState since standard removed defaults.
 //  2023-07-13: Use WGPUShaderModuleWGSLDescriptor's code instead of source. use WGPUMipmapFilterMode_Linear instead of WGPUFilterMode_Linear. (#6602)
 //  2023-04-11: Align buffer sizes. Use WGSL shaders instead of precompiled SPIR-V.
@@ -73,16 +74,17 @@ struct Uniforms
 
 struct ImGui_ImplWGPU_Data
 {
-    WGPUDevice          wgpuDevice = nullptr;
-    WGPUQueue           defaultQueue = nullptr;
-    WGPUTextureFormat   renderTargetFormat = WGPUTextureFormat_Undefined;
-    WGPUTextureFormat   depthStencilFormat = WGPUTextureFormat_Undefined;
-    WGPURenderPipeline  pipelineState = nullptr;
-
-    RenderResources     renderResources;
-    FrameResources*     pFrameResources = nullptr;
-    unsigned int        numFramesInFlight = 0;
-    unsigned int        frameIndex = UINT_MAX;
+    ImGui_ImplWGPU_InitInfo initInfo;
+    WGPUDevice              wgpuDevice = nullptr;
+    WGPUQueue               defaultQueue = nullptr;
+    WGPUTextureFormat       renderTargetFormat = WGPUTextureFormat_Undefined;
+    WGPUTextureFormat       depthStencilFormat = WGPUTextureFormat_Undefined;
+    WGPURenderPipeline      pipelineState = nullptr;
+
+    RenderResources         renderResources;
+    FrameResources*         pFrameResources = nullptr;
+    unsigned int            numFramesInFlight = 0;
+    unsigned int            frameIndex = UINT_MAX;
 };
 
 // Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
@@ -712,7 +714,7 @@ void ImGui_ImplWGPU_InvalidateDeviceObjects()
         SafeRelease(bd->pFrameResources[i]);
 }
 
-bool ImGui_ImplWGPU_Init(WGPUDevice device, int num_frames_in_flight, WGPUTextureFormat rt_format, WGPUTextureFormat depth_format)
+bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info)
 {
     ImGuiIO& io = ImGui::GetIO();
     IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");
@@ -723,11 +725,12 @@ bool ImGui_ImplWGPU_Init(WGPUDevice device, int num_frames_in_flight, WGPUTextur
     io.BackendRendererName = "imgui_impl_webgpu";
     io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;  // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
 
-    bd->wgpuDevice = device;
+    bd->initInfo = *init_info;
+    bd->wgpuDevice = init_info->Device;
     bd->defaultQueue = wgpuDeviceGetQueue(bd->wgpuDevice);
-    bd->renderTargetFormat = rt_format;
-    bd->depthStencilFormat = depth_format;
-    bd->numFramesInFlight = num_frames_in_flight;
+    bd->renderTargetFormat = init_info->RenderTargetFormat;
+    bd->depthStencilFormat = init_info->DepthStencilFormat;
+    bd->numFramesInFlight = init_info->NumFramesInFlight;
     bd->frameIndex = UINT_MAX;
 
     bd->renderResources.FontTexture = nullptr;
@@ -740,8 +743,8 @@ bool ImGui_ImplWGPU_Init(WGPUDevice device, int num_frames_in_flight, WGPUTextur
     bd->renderResources.ImageBindGroupLayout = nullptr;
 
     // Create buffers with a default size (they will later be grown as needed)
-    bd->pFrameResources = new FrameResources[num_frames_in_flight];
-    for (int i = 0; i < num_frames_in_flight; i++)
+    bd->pFrameResources = new FrameResources[bd->numFramesInFlight];
+    for (int i = 0; i < bd->numFramesInFlight; i++)
     {
         FrameResources* fr = &bd->pFrameResources[i];
         fr->IndexBuffer = nullptr;

+ 10 - 1
backends/imgui_impl_wgpu.h

@@ -20,7 +20,16 @@
 
 #include <webgpu/webgpu.h>
 
-IMGUI_IMPL_API bool ImGui_ImplWGPU_Init(WGPUDevice device, int num_frames_in_flight, WGPUTextureFormat rt_format, WGPUTextureFormat depth_format = WGPUTextureFormat_Undefined);
+// Initialization data, for ImGui_ImplWGPU_Init()
+struct ImGui_ImplWGPU_InitInfo
+{
+    WGPUDevice              Device;
+    int                     NumFramesInFlight = 3;
+    WGPUTextureFormat       RenderTargetFormat = WGPUTextureFormat_Undefined;
+    WGPUTextureFormat       DepthStencilFormat = WGPUTextureFormat_Undefined;
+};
+
+IMGUI_IMPL_API bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info);
 IMGUI_IMPL_API void ImGui_ImplWGPU_Shutdown();
 IMGUI_IMPL_API void ImGui_ImplWGPU_NewFrame();
 IMGUI_IMPL_API void ImGui_ImplWGPU_RenderDrawData(ImDrawData* draw_data, WGPURenderPassEncoder pass_encoder);

+ 2 - 0
docs/CHANGELOG.txt

@@ -43,6 +43,8 @@ Breaking changes:
 
 - Commented out ImGuiIO::ImeWindowHandle obsoleted in 1.87 in favor of writing
   to 'void* ImGuiViewport::PlatformHandleRaw'.
+- Backends: WebGPU: ImGui_ImplWGPU_Init() now takes a ImGui_ImplWGPU_InitInfo structure
+  instead of variety of parameters, allowing for easier further changes. (#7240)
 
 Other changes:
 

+ 6 - 1
examples/example_emscripten_wgpu/main.cpp

@@ -96,7 +96,12 @@ int main(int, char**)
 #ifdef __EMSCRIPTEN__
     ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback("#canvas");
 #endif
-    ImGui_ImplWGPU_Init(wgpu_device, 3, wgpu_preferred_fmt, WGPUTextureFormat_Undefined);
+    ImGui_ImplWGPU_InitInfo init_info;
+    init_info.Device = wgpu_device;
+    init_info.NumFramesInFlight = 3;
+    init_info.RenderTargetFormat = wgpu_preferred_fmt;
+    init_info.DepthStencilFormat = WGPUTextureFormat_Undefined;
+    ImGui_ImplWGPU_Init(&init_info);
 
     // Load Fonts
     // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.