From 648278cd62fbbf1556f5f2942c7487db58de43cd Mon Sep 17 00:00:00 2001 From: ocornut Date: Tue, 16 Apr 2024 12:08:45 +0200 Subject: [PATCH] Examples: GLFW+WebGPU: Amends. (#7435, #7132) --- docs/CHANGELOG.txt | 1 + .../example_emscripten_wgpu/CMakeLists.txt | 17 ++--- examples/example_emscripten_wgpu/main.cpp | 69 ++++++++++--------- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index e4af7859f..cda9dd6d3 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -50,6 +50,7 @@ Other changes: mitigitate issues with reading vertex indexing limits with 16-bit indices. (#7496, #5720) - Backends: SDL3: Fixed text inputs. Re-enable calling SDL_StartTextInput()/SDL_StopTextInput() as SDL3 no longer enables it by default. (#7452, #6306, #6071, #1953) [@Green-Sky] +- Examples: GLFW+WebGPU: Added support for WebGPU-native/Dawn (#7435, #7132) [@eliasdaler, @Zelif] ----------------------------------------------------------------------- diff --git a/examples/example_emscripten_wgpu/CMakeLists.txt b/examples/example_emscripten_wgpu/CMakeLists.txt index 646c5b3c2..b08e36ede 100644 --- a/examples/example_emscripten_wgpu/CMakeLists.txt +++ b/examples/example_emscripten_wgpu/CMakeLists.txt @@ -1,15 +1,12 @@ # Building for desktop (WebGPU-native) with Dawn: -# -# git clone https://github.com/google/dawn dawn -# cmake -B build -DIMGUI_DAWN_DIR=dawn -# cmake --build build -# +# 1. git clone https://github.com/google/dawn dawn +# 2. cmake -B build -DIMGUI_DAWN_DIR=dawn +# 3. cmake --build build # The resulting binary will be found at one of the following locations: # * build/Debug/example_emscripten_wgpu[.exe] # * build/example_emscripten_wgpu[.exe] # Building for Emscripten: -# # 1. Install Emscripten SDK following the instructions: https://emscripten.org/docs/getting_started/downloads.html # 2. Install Ninja build system # 3. emcmake cmake -G Ninja -B build @@ -39,9 +36,9 @@ else() if (NOT IMGUI_DAWN_DIR) message(FATAL_ERROR "Please specify the Dawn repository by setting IMGUI_DAWN_DIR") endif() - + option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON) - + # Dawn builds many things by default - disable things we don't need option(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" OFF) option(TINT_BUILD_CMD_TOOLS "Build the Tint command line tools" OFF) @@ -58,9 +55,9 @@ else() option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" OFF) option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON) endif() - + add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL) - + set(LIBRARIES webgpu_dawn webgpu_cpp webgpu_glfw glfw) endif() diff --git a/examples/example_emscripten_wgpu/main.cpp b/examples/example_emscripten_wgpu/main.cpp index d6ed58f03..4e47b8323 100644 --- a/examples/example_emscripten_wgpu/main.cpp +++ b/examples/example_emscripten_wgpu/main.cpp @@ -1,7 +1,6 @@ // Dear ImGui: standalone example application for using GLFW + WebGPU -// Dawn is used as a WebGPU implementation on desktop and Emscripten is supported for -// publishing on web. -// (Emscripten is a C++-to-javascript compiler, used to publish executables for the 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. // Learn about Dear ImGui: // - FAQ https://dearimgui.com/faq @@ -12,7 +11,6 @@ #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_wgpu.h" - #include #ifdef __EMSCRIPTEN__ @@ -64,33 +62,6 @@ static void wgpu_error_callback(WGPUErrorType error_type, const char* message, v printf("%s error: %s\n", error_type_lbl, message); } -static WGPUAdapter requestAdapter(WGPUInstance instance) { - auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const* message, void* pUserData) { - if (status == WGPURequestAdapterStatus_Success) { - *static_cast(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, char const* message, void* pUserData) { - if (status == WGPURequestDeviceStatus_Success) { - *static_cast(pUserData) = device; - } else { - printf("Could not get WebGPU device: %s\n", message); - } - }; - - WGPUDevice device; - wgpuAdapterRequestDevice(adapter, nullptr, onDeviceRequestEnded, (void*)&device); - return device; -} - // Main code int main(int, char**) { @@ -151,7 +122,7 @@ int main(int, char**) //io.Fonts->AddFontDefault(); #ifndef IMGUI_DISABLE_FILE_FUNCTIONS //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/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf", 10.0f); @@ -289,6 +260,36 @@ int main(int, char**) return 0; } +#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); @@ -298,10 +299,10 @@ static bool InitWGPU(GLFWwindow* window) if (!wgpu_device) return false; #else - WGPUAdapter adapter = requestAdapter(instance.Get()); + WGPUAdapter adapter = RequestAdapter(instance.Get()); if (!adapter) return false; - wgpu_device = requestDevice(adapter); + wgpu_device = RequestDevice(adapter); #endif #ifdef __EMSCRIPTEN__