Browse Source

Examples: GLFW+WGPU: various tweaks to reduce diff with #8381

ocornut 1 week ago
parent
commit
5af650fc6d
3 changed files with 36 additions and 31 deletions
  1. 1 1
      backends/imgui_impl_wgpu.cpp
  2. 1 1
      backends/imgui_impl_wgpu.h
  3. 34 29
      examples/example_glfw_wgpu/main.cpp

+ 1 - 1
backends/imgui_impl_wgpu.cpp

@@ -46,7 +46,7 @@
 
 
 #include "imgui.h"
 #include "imgui.h"
 
 
-// When targeting native platforms (i.e. NOT emscripten), one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN
+// When targeting native platforms (i.e. NOT Emscripten), one of IMGUI_IMPL_WEBGPU_BACKEND_DAWN
 // or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.
 // or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.
 #ifndef __EMSCRIPTEN__
 #ifndef __EMSCRIPTEN__
     #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
     #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)

+ 1 - 1
backends/imgui_impl_wgpu.h

@@ -32,7 +32,7 @@
 // Initialization data, for ImGui_ImplWGPU_Init()
 // Initialization data, for ImGui_ImplWGPU_Init()
 struct ImGui_ImplWGPU_InitInfo
 struct ImGui_ImplWGPU_InitInfo
 {
 {
-    WGPUDevice              Device;
+    WGPUDevice              Device = nullptr;
     int                     NumFramesInFlight = 3;
     int                     NumFramesInFlight = 3;
     WGPUTextureFormat       RenderTargetFormat = WGPUTextureFormat_Undefined;
     WGPUTextureFormat       RenderTargetFormat = WGPUTextureFormat_Undefined;
     WGPUTextureFormat       DepthStencilFormat = WGPUTextureFormat_Undefined;
     WGPUTextureFormat       DepthStencilFormat = WGPUTextureFormat_Undefined;

+ 34 - 29
examples/example_glfw_wgpu/main.cpp

@@ -1,4 +1,4 @@
-// Dear ImGui: standalone example application for using GLFW + WebGPU
+// Dear ImGui: standalone example application for GLFW + WebGPU
 // - Emscripten is supported for publishing on web. See https://emscripten.org.
 // - Emscripten is supported for publishing on web. See https://emscripten.org.
 // - Dawn is used as a WebGPU implementation on desktop.
 // - Dawn is used as a WebGPU implementation on desktop.
 
 
@@ -12,36 +12,32 @@
 #include "imgui_impl_glfw.h"
 #include "imgui_impl_glfw.h"
 #include "imgui_impl_wgpu.h"
 #include "imgui_impl_wgpu.h"
 #include <stdio.h>
 #include <stdio.h>
+#include <GLFW/glfw3.h>
 
 
+// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
 #ifdef __EMSCRIPTEN__
 #ifdef __EMSCRIPTEN__
 #include <emscripten.h>
 #include <emscripten.h>
 #include <emscripten/html5.h>
 #include <emscripten/html5.h>
 #include <emscripten/html5_webgpu.h>
 #include <emscripten/html5_webgpu.h>
+#include "../libs/emscripten/emscripten_mainloop_stub.h"
 #else
 #else
 #include <webgpu/webgpu_glfw.h>
 #include <webgpu/webgpu_glfw.h>
 #endif
 #endif
-
-#include <GLFW/glfw3.h>
 #include <webgpu/webgpu.h>
 #include <webgpu/webgpu.h>
 #include <webgpu/webgpu_cpp.h>
 #include <webgpu/webgpu_cpp.h>
 
 
-// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
-#ifdef __EMSCRIPTEN__
-#include "../libs/emscripten/emscripten_mainloop_stub.h"
-#endif
-
-// Global WebGPU required states
+// Data
 static WGPUInstance      wgpu_instance = nullptr;
 static WGPUInstance      wgpu_instance = nullptr;
 static WGPUDevice        wgpu_device = nullptr;
 static WGPUDevice        wgpu_device = nullptr;
 static WGPUSurface       wgpu_surface = nullptr;
 static WGPUSurface       wgpu_surface = nullptr;
 static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
 static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
 static WGPUSwapChain     wgpu_swap_chain = nullptr;
 static WGPUSwapChain     wgpu_swap_chain = nullptr;
-static int               wgpu_swap_chain_width = 1280;
-static int               wgpu_swap_chain_height = 800;
+static int               wgpu_surface_width = 1280;
+static int               wgpu_surface_height = 800;
 
 
 // Forward declarations
 // Forward declarations
 static bool InitWGPU(GLFWwindow* window);
 static bool InitWGPU(GLFWwindow* window);
-static void CreateSwapChain(int width, int height);
+static void ResizeSurface(int width, int height);
 
 
 static void glfw_error_callback(int error, const char* description)
 static void glfw_error_callback(int error, const char* description)
 {
 {
@@ -72,19 +68,23 @@ int main(int, char**)
     // Make sure GLFW does not initialize any graphics context.
     // Make sure GLFW does not initialize any graphics context.
     // This needs to be done explicitly later.
     // This needs to be done explicitly later.
     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
-    GLFWwindow* window = glfwCreateWindow(wgpu_swap_chain_width, wgpu_swap_chain_height, "Dear ImGui GLFW+WebGPU example", nullptr, nullptr);
+
+    // Create window
+    float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
+    wgpu_surface_width *= main_scale;
+    wgpu_surface_height *= main_scale;
+    GLFWwindow* window = glfwCreateWindow(wgpu_surface_width, wgpu_surface_height, "Dear ImGui GLFW+WebGPU example", nullptr, nullptr);
     if (window == nullptr)
     if (window == nullptr)
         return 1;
         return 1;
 
 
     // Initialize the WebGPU environment
     // Initialize the WebGPU environment
     if (!InitWGPU(window))
     if (!InitWGPU(window))
     {
     {
-        if (window)
-            glfwDestroyWindow(window);
+        glfwDestroyWindow(window);
         glfwTerminate();
         glfwTerminate();
         return 1;
         return 1;
     }
     }
-    CreateSwapChain(wgpu_swap_chain_width, wgpu_swap_chain_height);
+    ResizeSurface(wgpu_surface_width, wgpu_surface_height);
     glfwShowWindow(window);
     glfwShowWindow(window);
 
 
     // Setup Dear ImGui context
     // Setup Dear ImGui context
@@ -98,6 +98,11 @@ int main(int, char**)
     ImGui::StyleColorsDark();
     ImGui::StyleColorsDark();
     //ImGui::StyleColorsLight();
     //ImGui::StyleColorsLight();
 
 
+    // Setup scaling
+    ImGuiStyle& style = ImGui::GetStyle();
+    style.ScaleAllSizes(main_scale);        // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
+    style.FontScaleDpi = main_scale;        // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
+
     // Setup Platform/Renderer backends
     // Setup Platform/Renderer backends
     ImGui_ImplGlfw_InitForOther(window, true);
     ImGui_ImplGlfw_InitForOther(window, true);
 #ifdef __EMSCRIPTEN__
 #ifdef __EMSCRIPTEN__
@@ -117,15 +122,14 @@ int main(int, char**)
     // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
     // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
     // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
     // - Read 'docs/FONTS.md' for more instructions and details. If you like the default font but want it to scale better, consider using the 'ProggyVector' from the same author!
     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
-    // - Emscripten allows preloading a file or folder to be accessible at runtime. See Makefile for details.
-    //io.Fonts->AddFontDefault();
+    // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
     //style.FontSizeBase = 20.0f;
     //style.FontSizeBase = 20.0f;
+    //io.Fonts->AddFontDefault();
 #ifndef IMGUI_DISABLE_FILE_FUNCTIONS
 #ifndef IMGUI_DISABLE_FILE_FUNCTIONS
     //io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf");
     //io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf");
     //io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf");
     //io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf");
     //io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf");
     //io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf");
     //io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf");
     //io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf");
-    //io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf");
     //ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf");
     //ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf");
     //IM_ASSERT(font != nullptr);
     //IM_ASSERT(font != nullptr);
 #endif
 #endif
@@ -160,10 +164,10 @@ int main(int, char**)
         // React to changes in screen size
         // React to changes in screen size
         int width, height;
         int width, height;
         glfwGetFramebufferSize((GLFWwindow*)window, &width, &height);
         glfwGetFramebufferSize((GLFWwindow*)window, &width, &height);
-        if (width != wgpu_swap_chain_width || height != wgpu_swap_chain_height)
+        if (width != wgpu_surface_width || height != wgpu_surface_height)
         {
         {
             ImGui_ImplWGPU_InvalidateDeviceObjects();
             ImGui_ImplWGPU_InvalidateDeviceObjects();
-            CreateSwapChain(width, height);
+            ResizeSurface(width, height);
             ImGui_ImplWGPU_CreateDeviceObjects();
             ImGui_ImplWGPU_CreateDeviceObjects();
         }
         }
 
 
@@ -238,8 +242,8 @@ int main(int, char**)
 
 
         WGPUCommandBufferDescriptor cmd_buffer_desc = {};
         WGPUCommandBufferDescriptor cmd_buffer_desc = {};
         WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
         WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
-        WGPUQueue queue = wgpuDeviceGetQueue(wgpu_device);
-        wgpuQueueSubmit(queue, 1, &cmd_buffer);
+        WGPUQueue wgpu_queue = wgpuDeviceGetQueue(wgpu_device);
+        wgpuQueueSubmit(wgpu_queue, 1, &cmd_buffer);
 
 
 #ifndef __EMSCRIPTEN__
 #ifndef __EMSCRIPTEN__
         wgpuSwapChainPresent(wgpu_swap_chain);
         wgpuSwapChainPresent(wgpu_swap_chain);
@@ -311,10 +315,10 @@ static bool InitWGPU(GLFWwindow* window)
 #endif
 #endif
 
 
 #ifdef __EMSCRIPTEN__
 #ifdef __EMSCRIPTEN__
-    wgpu::SurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = {};
-    html_surface_desc.selector = "#canvas";
+    wgpu::SurfaceDescriptorFromCanvasHTMLSelector canvas_desc = {};
+    canvas_desc.selector = "#canvas";
     wgpu::SurfaceDescriptor surface_desc = {};
     wgpu::SurfaceDescriptor surface_desc = {};
-    surface_desc.nextInChain = &html_surface_desc;
+    surface_desc.nextInChain = &canvas_desc;
     wgpu::Surface surface = instance.CreateSurface(&surface_desc);
     wgpu::Surface surface = instance.CreateSurface(&surface_desc);
 
 
     wgpu::Adapter adapter = {};
     wgpu::Adapter adapter = {};
@@ -326,6 +330,7 @@ static bool InitWGPU(GLFWwindow* window)
     wgpu_preferred_fmt = WGPUTextureFormat_BGRA8Unorm;
     wgpu_preferred_fmt = WGPUTextureFormat_BGRA8Unorm;
 #endif
 #endif
 
 
+    // Moving Dawn objects into WGPU handles
     wgpu_instance = instance.MoveToCHandle();
     wgpu_instance = instance.MoveToCHandle();
     wgpu_surface = surface.MoveToCHandle();
     wgpu_surface = surface.MoveToCHandle();
 
 
@@ -334,12 +339,12 @@ static bool InitWGPU(GLFWwindow* window)
     return true;
     return true;
 }
 }
 
 
-static void CreateSwapChain(int width, int height)
+static void ResizeSurface(int width, int height)
 {
 {
     if (wgpu_swap_chain)
     if (wgpu_swap_chain)
         wgpuSwapChainRelease(wgpu_swap_chain);
         wgpuSwapChainRelease(wgpu_swap_chain);
-    wgpu_swap_chain_width = width;
-    wgpu_swap_chain_height = height;
+    wgpu_surface_width = width;
+    wgpu_surface_height = height;
     WGPUSwapChainDescriptor swap_chain_desc = {};
     WGPUSwapChainDescriptor swap_chain_desc = {};
     swap_chain_desc.usage = WGPUTextureUsage_RenderAttachment;
     swap_chain_desc.usage = WGPUTextureUsage_RenderAttachment;
     swap_chain_desc.format = wgpu_preferred_fmt;
     swap_chain_desc.format = wgpu_preferred_fmt;