|
@@ -1,5 +1,6 @@
|
|
-// Dear ImGui: standalone example application for Emscripten, using GLFW + WebGPU
|
|
|
|
-// (Emscripten is a C++-to-javascript compiler, used to publish executables for the web. See https://emscripten.org/)
|
|
|
|
|
|
+// Dear ImGui: standalone example application for using GLFW + WebGPU
|
|
|
|
+// - Emscripten is supported for publishing on web. See https://emscripten.org.
|
|
|
|
+// - Dawn is used as a WebGPU implementation on desktop.
|
|
|
|
|
|
// Learn about Dear ImGui:
|
|
// Learn about Dear ImGui:
|
|
// - FAQ https://dearimgui.com/faq
|
|
// - FAQ https://dearimgui.com/faq
|
|
@@ -11,11 +12,15 @@
|
|
#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>
|
|
|
|
+
|
|
#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>
|
|
|
|
+#else
|
|
|
|
+#include <webgpu/webgpu_glfw.h>
|
|
#endif
|
|
#endif
|
|
|
|
+
|
|
#include <GLFW/glfw3.h>
|
|
#include <GLFW/glfw3.h>
|
|
#include <webgpu/webgpu.h>
|
|
#include <webgpu/webgpu.h>
|
|
#include <webgpu/webgpu_cpp.h>
|
|
#include <webgpu/webgpu_cpp.h>
|
|
@@ -26,15 +31,16 @@
|
|
#endif
|
|
#endif
|
|
|
|
|
|
// Global WebGPU required states
|
|
// Global WebGPU required states
|
|
|
|
+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 = 0;
|
|
|
|
-static int wgpu_swap_chain_height = 0;
|
|
|
|
|
|
+static int wgpu_swap_chain_width = 1280;
|
|
|
|
+static int wgpu_swap_chain_height = 720;
|
|
|
|
|
|
// Forward declarations
|
|
// Forward declarations
|
|
-static bool InitWGPU();
|
|
|
|
|
|
+static bool InitWGPU(GLFWwindow* window);
|
|
static void CreateSwapChain(int width, int height);
|
|
static void CreateSwapChain(int width, int height);
|
|
|
|
|
|
static void glfw_error_callback(int error, const char* description)
|
|
static void glfw_error_callback(int error, const char* description)
|
|
@@ -66,18 +72,19 @@ 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(1280, 720, "Dear ImGui GLFW+WebGPU example", nullptr, nullptr);
|
|
|
|
|
|
+ GLFWwindow* window = glfwCreateWindow(wgpu_swap_chain_width, wgpu_swap_chain_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())
|
|
|
|
|
|
+ if (!InitWGPU(window))
|
|
{
|
|
{
|
|
if (window)
|
|
if (window)
|
|
glfwDestroyWindow(window);
|
|
glfwDestroyWindow(window);
|
|
glfwTerminate();
|
|
glfwTerminate();
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
+ CreateSwapChain(wgpu_swap_chain_width, wgpu_swap_chain_height);
|
|
glfwShowWindow(window);
|
|
glfwShowWindow(window);
|
|
|
|
|
|
// Setup Dear ImGui context
|
|
// Setup Dear ImGui context
|
|
@@ -115,7 +122,7 @@ int main(int, char**)
|
|
//io.Fonts->AddFontDefault();
|
|
//io.Fonts->AddFontDefault();
|
|
#ifndef IMGUI_DISABLE_FILE_FUNCTIONS
|
|
#ifndef IMGUI_DISABLE_FILE_FUNCTIONS
|
|
//io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf", 18.0f);
|
|
//io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf", 18.0f);
|
|
- io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf", 16.0f);
|
|
|
|
|
|
+ //io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf", 16.0f);
|
|
//io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf", 16.0f);
|
|
//io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf", 16.0f);
|
|
//io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf", 15.0f);
|
|
//io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf", 15.0f);
|
|
//io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf", 10.0f);
|
|
//io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf", 10.0f);
|
|
@@ -148,7 +155,7 @@ 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_swap_chain_width || height != wgpu_swap_chain_height)
|
|
{
|
|
{
|
|
ImGui_ImplWGPU_InvalidateDeviceObjects();
|
|
ImGui_ImplWGPU_InvalidateDeviceObjects();
|
|
CreateSwapChain(width, height);
|
|
CreateSwapChain(width, height);
|
|
@@ -200,6 +207,11 @@ int main(int, char**)
|
|
// Rendering
|
|
// Rendering
|
|
ImGui::Render();
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
+#ifndef __EMSCRIPTEN__
|
|
|
|
+ // Tick needs to be called in Dawn to display validation errors
|
|
|
|
+ wgpuDeviceTick(wgpu_device);
|
|
|
|
+#endif
|
|
|
|
+
|
|
WGPURenderPassColorAttachment color_attachments = {};
|
|
WGPURenderPassColorAttachment color_attachments = {};
|
|
color_attachments.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
|
|
color_attachments.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
|
|
color_attachments.loadOp = WGPULoadOp_Clear;
|
|
color_attachments.loadOp = WGPULoadOp_Clear;
|
|
@@ -223,6 +235,15 @@ int main(int, char**)
|
|
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
|
|
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
|
|
WGPUQueue queue = wgpuDeviceGetQueue(wgpu_device);
|
|
WGPUQueue queue = wgpuDeviceGetQueue(wgpu_device);
|
|
wgpuQueueSubmit(queue, 1, &cmd_buffer);
|
|
wgpuQueueSubmit(queue, 1, &cmd_buffer);
|
|
|
|
+
|
|
|
|
+#ifndef __EMSCRIPTEN__
|
|
|
|
+ wgpuSwapChainPresent(wgpu_swap_chain);
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+ wgpuTextureViewRelease(color_attachments.view);
|
|
|
|
+ wgpuRenderPassEncoderRelease(pass);
|
|
|
|
+ wgpuCommandEncoderRelease(encoder);
|
|
|
|
+ wgpuCommandBufferRelease(cmd_buffer);
|
|
}
|
|
}
|
|
#ifdef __EMSCRIPTEN__
|
|
#ifdef __EMSCRIPTEN__
|
|
EMSCRIPTEN_MAINLOOP_END;
|
|
EMSCRIPTEN_MAINLOOP_END;
|
|
@@ -239,29 +260,72 @@ int main(int, char**)
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-static bool InitWGPU()
|
|
|
|
|
|
+#ifndef __EMSCRIPTEN__
|
|
|
|
+static WGPUAdapter RequestAdapter(WGPUInstance instance)
|
|
|
|
+{
|
|
|
|
+ auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, const char* message, void* pUserData)
|
|
|
|
+ {
|
|
|
|
+ if (status == WGPURequestAdapterStatus_Success)
|
|
|
|
+ *(WGPUAdapter*)(pUserData) = adapter;
|
|
|
|
+ else
|
|
|
|
+ printf("Could not get WebGPU adapter: %s\n", message);
|
|
|
|
+};
|
|
|
|
+ WGPUAdapter adapter;
|
|
|
|
+ wgpuInstanceRequestAdapter(instance, nullptr, onAdapterRequestEnded, (void*)&adapter);
|
|
|
|
+ return adapter;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static WGPUDevice RequestDevice(WGPUAdapter& adapter)
|
|
{
|
|
{
|
|
|
|
+ auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, const char* message, void* pUserData)
|
|
|
|
+ {
|
|
|
|
+ if (status == WGPURequestDeviceStatus_Success)
|
|
|
|
+ *(WGPUDevice*)(pUserData) = device;
|
|
|
|
+ else
|
|
|
|
+ printf("Could not get WebGPU device: %s\n", message);
|
|
|
|
+ };
|
|
|
|
+ WGPUDevice device;
|
|
|
|
+ wgpuAdapterRequestDevice(adapter, nullptr, onDeviceRequestEnded, (void*)&device);
|
|
|
|
+ return device;
|
|
|
|
+}
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+static bool InitWGPU(GLFWwindow* window)
|
|
|
|
+{
|
|
|
|
+ wgpu::Instance instance = wgpuCreateInstance(nullptr);
|
|
|
|
+
|
|
|
|
+#ifdef __EMSCRIPTEN__
|
|
wgpu_device = emscripten_webgpu_get_device();
|
|
wgpu_device = emscripten_webgpu_get_device();
|
|
if (!wgpu_device)
|
|
if (!wgpu_device)
|
|
return false;
|
|
return false;
|
|
|
|
+#else
|
|
|
|
+ WGPUAdapter adapter = RequestAdapter(instance.Get());
|
|
|
|
+ if (!adapter)
|
|
|
|
+ return false;
|
|
|
|
+ wgpu_device = RequestDevice(adapter);
|
|
|
|
+#endif
|
|
|
|
|
|
- wgpuDeviceSetUncapturedErrorCallback(wgpu_device, wgpu_error_callback, nullptr);
|
|
|
|
-
|
|
|
|
- // Use C++ wrapper due to misbehavior in Emscripten.
|
|
|
|
- // Some offset computation for wgpuInstanceCreateSurface in JavaScript
|
|
|
|
- // seem to be inline with struct alignments in the C++ structure
|
|
|
|
|
|
+#ifdef __EMSCRIPTEN__
|
|
wgpu::SurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = {};
|
|
wgpu::SurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = {};
|
|
html_surface_desc.selector = "#canvas";
|
|
html_surface_desc.selector = "#canvas";
|
|
-
|
|
|
|
wgpu::SurfaceDescriptor surface_desc = {};
|
|
wgpu::SurfaceDescriptor surface_desc = {};
|
|
surface_desc.nextInChain = &html_surface_desc;
|
|
surface_desc.nextInChain = &html_surface_desc;
|
|
-
|
|
|
|
- wgpu::Instance instance = wgpuCreateInstance(nullptr);
|
|
|
|
wgpu::Surface surface = instance.CreateSurface(&surface_desc);
|
|
wgpu::Surface surface = instance.CreateSurface(&surface_desc);
|
|
|
|
+
|
|
wgpu::Adapter adapter = {};
|
|
wgpu::Adapter adapter = {};
|
|
wgpu_preferred_fmt = (WGPUTextureFormat)surface.GetPreferredFormat(adapter);
|
|
wgpu_preferred_fmt = (WGPUTextureFormat)surface.GetPreferredFormat(adapter);
|
|
|
|
+#else
|
|
|
|
+ wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
|
|
|
|
+ if (!surface)
|
|
|
|
+ return false;
|
|
|
|
+ wgpu_preferred_fmt = WGPUTextureFormat_BGRA8Unorm;
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+ wgpu_instance = instance.MoveToCHandle();
|
|
wgpu_surface = surface.MoveToCHandle();
|
|
wgpu_surface = surface.MoveToCHandle();
|
|
|
|
|
|
|
|
+ wgpuDeviceSetUncapturedErrorCallback(wgpu_device, wgpu_error_callback, nullptr);
|
|
|
|
+
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|