mirror of
https://github.com/ocornut/imgui.git
synced 2025-09-27 21:58:30 +00:00
Backends, Examples: WGPU: wip refactor. (8381)
Rebased and squashed 47 commits. - ImGui WebGPU examples: removed swap-chain in favor of surfaceConfigure (8191) - New SDL2 WGPU example - Code optimization: removed the redundancies of assignment - Changes: ImGui code style - Lambdas RequestAdapter and RequestDevice callbacks declarated as signature - Validation Layer callbacks moved from lambdas to standard functions - Same changes of GLFW example: ImGui code style - Lambdas RequestAdapter and RequestDevice callbacks declarated as signature - Validation Layer callbacks moved from lambdas to standard functions - Scrollbars inhibition - Use of new direct CreateDevice function (w/o callback), added release of all resources used (not present in original example) - If the O.S. is Linux/Unix check if Wayland is the current active session and set DAWN_USE_WAYLAND=ON option (otherwise is always OFF) - example_glfw_wgpu: removed all workarounds - example_sdl2_wgpu: same style and functionality as GLFW example - sdl2wgpu tool to acquire surfaceDescriptor via SDL_syswm and to pass to wgpuInstanceCreateSurface to create WGPU Surface - css style to avoid the scrollbar - added `chek_surface_texture_status` function to check the `WGPUSurfaceTexture .status` and recreate the `Surface` in case of "not optimal" (bad) status. - Changed comment reference to `emwgpudawn` an EMSCRIPTEN WGPU binding maintained by Google - Adaptation to the last Google DAWN commit (1411699ba) Adaptation to the last EMSCRIPTEN 4.0.10, using new "--use-port=emdawnwebgpu" compiler/linker flag - Support for EMSCRIPTEN >= 4.0.10 (using "--use-port=emdawnwebgpu") and NEW support for WGPU-Native - Finalized the support of WGPU-Native for MacOS and minimal code adjustment - WebGPU examples - DAWN / WGPU native and EMSCRIPTEN - for GLFW/SDL2/SDL3 frameworks - "index.html" no more necessary (now the common one is used), "sdl2wgpu.cpp" It has been moved and renamed (sdl2_wgpu.c), "sdl2wgpu.h" no more necessary - added procedure for using CMake - added procedure for using CMake - Updated example_sdl3_wgpu build procedure for EMSCRIPTEN - WGPU+GLFW: Helper to create a WebGPU surface for Native application. Used only with WGPU-Native SDK: DAWN-Native already has a built-in function - WGPU+SDL2: helper to create a WebGPU surface (exclusively!) for Native/Desktop applications and available only together with WebGPU/WGPU backend - WGPU+SDL3: helper to create a WebGPU surface (exclusively!) for Native/Desktop applications and available only together with WebGPU/WGPU backend - WebGPU Helper functions and differentiation between the 4 compilation methods (via defines): Google DAWN (Native/Emscripten) / WGPU (Native/EMscripten) - example_glfw_wgpu: ImGui_ImplGLFW_CreateWGPUSurface_Helper (new helper function in imgui_impl_glfw backend), check status and error callback functions in imgui_impl_wgpu backend - example_sdl2_wgpu: ImGui_ImplSDL2_CreateWGPUSurface_Helper (new helper function in imgui_impl_sdl2 backend), check status and error callback functions in imgui_impl_wgpu backend - example_sdl3_wgpu: ImGui_ImplSDL3_CreateWGPUSurface_Helper (new helper function in imgui_impl_sdl3 backend), check status and error callback functions in imgui_impl_wgpu backend - Functions ImGui_ImplXXXX_CreateWGPUSurface_Helper were inserted into imgui_impl_xxxx (xxxx = GLFW / SDL2 / SDL3), and initialization has been integrated into every example: no more necessary, removed
This commit is contained in:
@@ -12,54 +12,55 @@
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_wgpu.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
#include <emscripten/html5_webgpu.h>
|
||||
#else
|
||||
#include <webgpu/webgpu_glfw.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <webgpu/webgpu.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"
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#include <emscripten/html5_webgpu.h>
|
||||
#endif
|
||||
#include <webgpu/webgpu.h>
|
||||
#include <webgpu/webgpu_cpp.h>
|
||||
#include "../libs/emscripten/emscripten_mainloop_stub.h"
|
||||
#else
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
#include <webgpu/webgpu_glfw.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Global WebGPU required states
|
||||
static WGPUInstance wgpu_instance = nullptr;
|
||||
static WGPUDevice wgpu_device = nullptr;
|
||||
static WGPUSurface wgpu_surface = nullptr;
|
||||
static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
|
||||
static WGPUSwapChain wgpu_swap_chain = nullptr;
|
||||
static int wgpu_swap_chain_width = 1280;
|
||||
static int wgpu_swap_chain_height = 800;
|
||||
static WGPUInstance wgpu_instance = nullptr;
|
||||
static WGPUDevice wgpu_device = nullptr;
|
||||
static WGPUSurface wgpu_surface = nullptr;
|
||||
static WGPUQueue wgpu_queue = nullptr;
|
||||
static WGPUSurfaceConfiguration wgpu_surface_configuration {};
|
||||
static int wgpu_surface_width = 1280;
|
||||
static int wgpu_surface_height = 800;
|
||||
|
||||
// Forward declarations
|
||||
static bool InitWGPU(GLFWwindow* window);
|
||||
static void CreateSwapChain(int width, int height);
|
||||
static bool InitWGPU(void* window);
|
||||
|
||||
static void glfw_error_callback(int error, const char* description)
|
||||
{
|
||||
printf("GLFW Error %d: %s\n", error, description);
|
||||
}
|
||||
|
||||
static void wgpu_error_callback(WGPUErrorType error_type, const char* message, void*)
|
||||
static void ResizeSurface(int width, int height)
|
||||
{
|
||||
const char* error_type_lbl = "";
|
||||
switch (error_type)
|
||||
{
|
||||
case WGPUErrorType_Validation: error_type_lbl = "Validation"; break;
|
||||
case WGPUErrorType_OutOfMemory: error_type_lbl = "Out of memory"; break;
|
||||
case WGPUErrorType_Unknown: error_type_lbl = "Unknown"; break;
|
||||
case WGPUErrorType_DeviceLost: error_type_lbl = "Device lost"; break;
|
||||
default: error_type_lbl = "Unknown";
|
||||
}
|
||||
printf("%s error: %s\n", error_type_lbl, message);
|
||||
wgpu_surface_configuration.width = wgpu_surface_width = width;
|
||||
wgpu_surface_configuration.height = wgpu_surface_height = height;
|
||||
|
||||
wgpuSurfaceConfigure(wgpu_surface, &wgpu_surface_configuration);
|
||||
}
|
||||
|
||||
static void ReleaseTextureAndConfigureSurface(WGPUTexture &texture, int fb_width, int fb_height)
|
||||
{
|
||||
if (texture)
|
||||
wgpuTextureRelease(texture);
|
||||
if ( fb_width > 0 && fb_height > 0 )
|
||||
ResizeSurface(fb_width, fb_height);
|
||||
}
|
||||
|
||||
// Main code
|
||||
@@ -67,24 +68,23 @@ int main(int, char**)
|
||||
{
|
||||
glfwSetErrorCallback(glfw_error_callback);
|
||||
if (!glfwInit())
|
||||
return 1;
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Make sure GLFW does not initialize any graphics context.
|
||||
// This needs to be done explicitly later.
|
||||
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);
|
||||
GLFWwindow* window = glfwCreateWindow(wgpu_surface_width, wgpu_surface_height, "Dear ImGui GLFW+WebGPU example", nullptr, nullptr);
|
||||
if (window == nullptr)
|
||||
return 1;
|
||||
return EXIT_FAILURE;
|
||||
|
||||
// Initialize the WebGPU environment
|
||||
if (!InitWGPU(window))
|
||||
{
|
||||
if (window)
|
||||
glfwDestroyWindow(window);
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
CreateSwapChain(wgpu_swap_chain_width, wgpu_swap_chain_height);
|
||||
|
||||
glfwShowWindow(window);
|
||||
|
||||
// Setup Dear ImGui context
|
||||
@@ -106,7 +106,7 @@ int main(int, char**)
|
||||
ImGui_ImplWGPU_InitInfo init_info;
|
||||
init_info.Device = wgpu_device;
|
||||
init_info.NumFramesInFlight = 3;
|
||||
init_info.RenderTargetFormat = wgpu_preferred_fmt;
|
||||
init_info.RenderTargetFormat = wgpu_surface_configuration.format;
|
||||
init_info.DepthStencilFormat = WGPUTextureFormat_Undefined;
|
||||
ImGui_ImplWGPU_Init(&init_info);
|
||||
|
||||
@@ -114,19 +114,19 @@ int main(int, char**)
|
||||
// - 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.
|
||||
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
|
||||
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
|
||||
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
|
||||
// - 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!
|
||||
// - 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();
|
||||
//style.FontSizeBase = 20.0f;
|
||||
#ifndef IMGUI_DISABLE_FILE_FUNCTIONS
|
||||
//io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("fonts/DroidSans.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf");
|
||||
//ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf", 18.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);
|
||||
//ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
|
||||
//IM_ASSERT(font != nullptr);
|
||||
#endif
|
||||
|
||||
@@ -160,13 +160,22 @@ int main(int, char**)
|
||||
// React to changes in screen size
|
||||
int 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();
|
||||
CreateSwapChain(width, height);
|
||||
ResizeSurface(width, height);
|
||||
ImGui_ImplWGPU_CreateDeviceObjects();
|
||||
}
|
||||
|
||||
WGPUSurfaceTexture surfaceTexture;
|
||||
wgpuSurfaceGetCurrentTexture(wgpu_surface, &surfaceTexture);
|
||||
|
||||
// Check SurfaceTexture status, if NOT optimal status we try to re-configure Surface
|
||||
if (!ImGui_ImplWGPU_CheckSurfaceTextureOptimalStatus_Helper(surfaceTexture.status)) {
|
||||
ReleaseTextureAndConfigureSurface(surfaceTexture.texture, width, height);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplWGPU_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
@@ -212,21 +221,26 @@ int main(int, char**)
|
||||
// Rendering
|
||||
ImGui::Render();
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
// Tick needs to be called in Dawn to display validation errors
|
||||
wgpuDeviceTick(wgpu_device);
|
||||
#endif
|
||||
WGPUTextureViewDescriptor viewDescriptor {};
|
||||
viewDescriptor.format = wgpu_surface_configuration.format;
|
||||
viewDescriptor.dimension = WGPUTextureViewDimension_2D ;
|
||||
viewDescriptor.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED;
|
||||
viewDescriptor.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED;
|
||||
viewDescriptor.aspect = WGPUTextureAspect_All;
|
||||
|
||||
WGPURenderPassColorAttachment color_attachments = {};
|
||||
WGPUTextureView textureView = wgpuTextureCreateView(surfaceTexture.texture, &viewDescriptor);
|
||||
|
||||
|
||||
WGPURenderPassColorAttachment color_attachments {};
|
||||
color_attachments.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
|
||||
color_attachments.loadOp = WGPULoadOp_Clear;
|
||||
color_attachments.storeOp = WGPUStoreOp_Store;
|
||||
color_attachments.loadOp = WGPULoadOp_Clear;
|
||||
color_attachments.storeOp = WGPUStoreOp_Store;
|
||||
color_attachments.clearValue = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
|
||||
color_attachments.view = wgpuSwapChainGetCurrentTextureView(wgpu_swap_chain);
|
||||
color_attachments.view = textureView;
|
||||
|
||||
WGPURenderPassDescriptor render_pass_desc = {};
|
||||
render_pass_desc.colorAttachmentCount = 1;
|
||||
render_pass_desc.colorAttachments = &color_attachments;
|
||||
WGPURenderPassDescriptor render_pass_desc {};
|
||||
render_pass_desc.colorAttachmentCount = 1;
|
||||
render_pass_desc.colorAttachments = &color_attachments;
|
||||
render_pass_desc.depthStencilAttachment = nullptr;
|
||||
|
||||
WGPUCommandEncoderDescriptor enc_desc = {};
|
||||
@@ -236,16 +250,18 @@ int main(int, char**)
|
||||
ImGui_ImplWGPU_RenderDrawData(ImGui::GetDrawData(), pass);
|
||||
wgpuRenderPassEncoderEnd(pass);
|
||||
|
||||
WGPUCommandBufferDescriptor cmd_buffer_desc = {};
|
||||
WGPUCommandBufferDescriptor cmd_buffer_desc {};
|
||||
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
|
||||
WGPUQueue queue = wgpuDeviceGetQueue(wgpu_device);
|
||||
wgpuQueueSubmit(queue, 1, &cmd_buffer);
|
||||
wgpuQueueSubmit(wgpu_queue, 1, &cmd_buffer);
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
wgpuSwapChainPresent(wgpu_swap_chain);
|
||||
wgpuSurfacePresent(wgpu_surface);
|
||||
// Tick needs to be called in Dawn to display validation errors
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
wgpuDeviceTick(wgpu_device);
|
||||
#endif
|
||||
|
||||
wgpuTextureViewRelease(color_attachments.view);
|
||||
#endif
|
||||
wgpuTextureViewRelease(textureView);
|
||||
wgpuRenderPassEncoderRelease(pass);
|
||||
wgpuCommandEncoderRelease(encoder);
|
||||
wgpuCommandBufferRelease(cmd_buffer);
|
||||
@@ -259,92 +275,222 @@ int main(int, char**)
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
wgpuSurfaceUnconfigure(wgpu_surface);
|
||||
wgpuSurfaceRelease(wgpu_surface);
|
||||
wgpuQueueRelease(wgpu_queue);
|
||||
wgpuDeviceRelease(wgpu_device);
|
||||
wgpuInstanceRelease(wgpu_instance);
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
#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)
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
static WGPUAdapter GetAdapter(wgpu::Instance &instance)
|
||||
{
|
||||
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);
|
||||
wgpu::Adapter acquiredAdapter;
|
||||
wgpu::RequestAdapterOptions adapterOptions;
|
||||
|
||||
auto onRequestAdapter = [&](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter, wgpu::StringView message) {
|
||||
if (status != wgpu::RequestAdapterStatus::Success)
|
||||
{
|
||||
printf("Failed to get an adapter: %s\n", message.data);
|
||||
return;
|
||||
}
|
||||
acquiredAdapter = std::move(adapter);
|
||||
};
|
||||
WGPUDevice device;
|
||||
wgpuAdapterRequestDevice(adapter, nullptr, onDeviceRequestEnded, (void*)&device);
|
||||
return device;
|
||||
|
||||
// Synchronously (wait until) acquire Adapter
|
||||
wgpu::Future waitAdapterFunc { instance.RequestAdapter(&adapterOptions, wgpu::CallbackMode::WaitAnyOnly, onRequestAdapter) };
|
||||
wgpu::WaitStatus waitStatusAdapter = instance.WaitAny(waitAdapterFunc, UINT64_MAX);
|
||||
assert(acquiredAdapter != nullptr && waitStatusAdapter == wgpu::WaitStatus::Success && "Error on Adapter request");
|
||||
#ifndef NDEBUG
|
||||
ImGui_ImplWGPU_PrintAdapterInfo_Helper(acquiredAdapter.Get());
|
||||
#endif
|
||||
return acquiredAdapter.MoveToCHandle();
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool InitWGPU(GLFWwindow* window)
|
||||
static WGPUDevice GetDevice(wgpu::Instance &instance, wgpu::Adapter &adapter)
|
||||
{
|
||||
wgpu::Instance instance = wgpuCreateInstance(nullptr);
|
||||
// Set device callback functions
|
||||
wgpu::DeviceDescriptor deviceDesc;
|
||||
deviceDesc.SetDeviceLostCallback(wgpu::CallbackMode::AllowSpontaneous, ImGui_ImplWGPU_DAWN_DeviceLostCallback_Helper);
|
||||
deviceDesc.SetUncapturedErrorCallback(ImGui_ImplWGPU_DAWN_ErrorCallback_Helper);
|
||||
|
||||
wgpu::Device acquiredDevice;
|
||||
auto onRequestDevice = [&](wgpu::RequestDeviceStatus status, wgpu::Device localDevice, wgpu::StringView message) {
|
||||
if (status != wgpu::RequestDeviceStatus::Success)
|
||||
{
|
||||
printf("Failed to get an device: %s\n", message.data);
|
||||
return;
|
||||
}
|
||||
acquiredDevice = std::move(localDevice);
|
||||
};
|
||||
|
||||
// Synchronously (wait until) get Device
|
||||
wgpu::Future waitDeviceFunc { adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::WaitAnyOnly, onRequestDevice) };
|
||||
wgpu::WaitStatus waitStatusDevice = instance.WaitAny(waitDeviceFunc, UINT64_MAX);
|
||||
assert(acquiredDevice != nullptr && waitStatusDevice == wgpu::WaitStatus::Success && "Error on Device request");
|
||||
return acquiredDevice.MoveToCHandle();
|
||||
}
|
||||
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#ifdef __EMSCRIPTEN__
|
||||
wgpu_device = emscripten_webgpu_get_device();
|
||||
if (!wgpu_device)
|
||||
return false;
|
||||
#else
|
||||
WGPUAdapter adapter = RequestAdapter(instance.Get());
|
||||
if (!adapter)
|
||||
return false;
|
||||
wgpu_device = RequestDevice(adapter);
|
||||
// Adapter and device initialization via JS
|
||||
EM_ASYNC_JS( void, getAdapterAndDeviceViaJS, (),
|
||||
{
|
||||
if (!navigator.gpu) throw Error("WebGPU not supported.");
|
||||
|
||||
const adapter = await navigator.gpu.requestAdapter();
|
||||
const device = await adapter.requestDevice();
|
||||
Module.preinitializedWebGPUDevice = device;
|
||||
} );
|
||||
#else // __EMSCRIPTEN__
|
||||
static void handle_request_adapter(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, void *userdata1, void *userdata2)
|
||||
{
|
||||
if (status == WGPURequestAdapterStatus_Success)
|
||||
{
|
||||
WGPUAdapter *extAdapter = (WGPUAdapter *) userdata1;
|
||||
*extAdapter = adapter;
|
||||
}
|
||||
else
|
||||
printf("Request_adapter status=%#.8x message=%.*s\n", status, (int) message.length, message.data);
|
||||
}
|
||||
|
||||
static void handle_request_device(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, void *userdata1, void *userdata2)
|
||||
{
|
||||
if (status == WGPURequestDeviceStatus_Success)
|
||||
{
|
||||
WGPUDevice *extDevice = (WGPUDevice *) userdata1;
|
||||
*extDevice = device;
|
||||
}
|
||||
else
|
||||
printf("Request_device status=%#.8x message=%.*s\n", status, (int) message.length, message.data);
|
||||
}
|
||||
|
||||
static WGPUAdapter GetAdapter(WGPUInstance &instance)
|
||||
{
|
||||
WGPURequestAdapterOptions adapterOptions = {};
|
||||
|
||||
static WGPUAdapter localAdapter;
|
||||
WGPURequestAdapterCallbackInfo adapterCallbackInfo = {};
|
||||
adapterCallbackInfo.callback = handle_request_adapter;
|
||||
adapterCallbackInfo.userdata1 = &localAdapter;
|
||||
|
||||
wgpuInstanceRequestAdapter(wgpu_instance, &adapterOptions, adapterCallbackInfo);
|
||||
assert(localAdapter && "Error on Adapter request");
|
||||
|
||||
#ifndef NDEBUG
|
||||
ImGui_ImplWGPU_PrintAdapterInfo_Helper(localAdapter);
|
||||
#endif
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
wgpu::SurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = {};
|
||||
html_surface_desc.selector = "#canvas";
|
||||
wgpu::SurfaceDescriptor surface_desc = {};
|
||||
surface_desc.nextInChain = &html_surface_desc;
|
||||
wgpu::Surface surface = instance.CreateSurface(&surface_desc);
|
||||
return localAdapter;
|
||||
}
|
||||
|
||||
wgpu::Adapter adapter = {};
|
||||
wgpu_preferred_fmt = (WGPUTextureFormat)surface.GetPreferredFormat(adapter);
|
||||
static WGPUDevice GetDevice(WGPUAdapter &adapter)
|
||||
{
|
||||
static WGPUDevice localDevice;
|
||||
WGPURequestDeviceCallbackInfo deviceCallbackInfo = {};
|
||||
deviceCallbackInfo.callback = handle_request_device;
|
||||
deviceCallbackInfo.userdata1 = &localDevice;
|
||||
|
||||
wgpuAdapterRequestDevice(adapter, NULL, deviceCallbackInfo);
|
||||
assert(localDevice && "Error on Device request");
|
||||
|
||||
return localDevice;
|
||||
}
|
||||
#endif // __EMSCRIPTEN__
|
||||
#endif // IMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
|
||||
static bool InitWGPU(void* window)
|
||||
{
|
||||
WGPUTextureFormat preferred_fmt = WGPUTextureFormat_Undefined; // acquired from SurfaceCapabilities
|
||||
|
||||
// Google DAWN backend: Adapter and Device acquisition, Surface creation
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
wgpu::InstanceDescriptor instanceDescriptor = {};
|
||||
instanceDescriptor.capabilities.timedWaitAnyEnable = true;
|
||||
wgpu::Instance instance = wgpu::CreateInstance(&instanceDescriptor);
|
||||
|
||||
wgpu::Adapter adapter { GetAdapter(instance) };
|
||||
wgpu_device = GetDevice(instance, adapter);
|
||||
|
||||
// Create the surface.
|
||||
#ifdef __EMSCRIPTEN__
|
||||
wgpu::EmscriptenSurfaceSourceCanvasHTMLSelector canvasDesc{};
|
||||
canvasDesc.selector = "#canvas";
|
||||
|
||||
wgpu::SurfaceDescriptor surfaceDesc = {};
|
||||
surfaceDesc.nextInChain = &canvasDesc;
|
||||
wgpu::Surface surface = instance.CreateSurface(&surfaceDesc);
|
||||
#else
|
||||
wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
|
||||
wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, (GLFWwindow *) window);
|
||||
#endif
|
||||
if (!surface)
|
||||
return false;
|
||||
wgpu_preferred_fmt = WGPUTextureFormat_BGRA8Unorm;
|
||||
#endif
|
||||
|
||||
// Moving Dawn objects into WGPU handles
|
||||
wgpu_instance = instance.MoveToCHandle();
|
||||
wgpu_surface = surface.MoveToCHandle();
|
||||
wgpu_surface = surface.MoveToCHandle();
|
||||
|
||||
wgpuDeviceSetUncapturedErrorCallback(wgpu_device, wgpu_error_callback, nullptr);
|
||||
WGPUSurfaceCapabilities surface_capabilities = {};
|
||||
wgpuSurfaceGetCapabilities(wgpu_surface, adapter.Get(), &surface_capabilities);
|
||||
|
||||
preferred_fmt = surface_capabilities.formats[0];
|
||||
|
||||
// WGPU backend: Adapter and Device acquisition, Surface creation
|
||||
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
wgpu_instance = wgpuCreateInstance(nullptr);
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
getAdapterAndDeviceViaJS();
|
||||
|
||||
wgpu_device = emscripten_webgpu_get_device();
|
||||
assert(wgpu_device != nullptr && "Error creating the Device");
|
||||
|
||||
WGPUSurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = {};
|
||||
html_surface_desc.chain.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector;
|
||||
html_surface_desc.selector = "#canvas";
|
||||
|
||||
WGPUSurfaceDescriptor surface_desc = {};
|
||||
surface_desc.nextInChain = &html_surface_desc.chain;
|
||||
|
||||
// Create the surface.
|
||||
wgpu_surface = wgpuInstanceCreateSurface(wgpu_instance, &surface_desc);
|
||||
preferred_fmt = wgpuSurfaceGetPreferredFormat(wgpu_surface, {} /* adapter */);
|
||||
#else // __EMSCRIPTEN__
|
||||
wgpuSetLogCallback(ImGui_ImplWGPU_WGPU_LogCallback_Helper, NULL);
|
||||
wgpuSetLogLevel(WGPULogLevel_Warn);
|
||||
|
||||
static WGPUAdapter adapter = GetAdapter(wgpu_instance);
|
||||
wgpu_device = GetDevice(adapter);
|
||||
|
||||
// Create the surface.
|
||||
wgpu_surface = ImGui_ImplGLFW_CreateWGPUSurface_Helper( wgpu_instance, (GLFWwindow*) window);
|
||||
|
||||
if (!wgpu_surface)
|
||||
return false;
|
||||
|
||||
WGPUSurfaceCapabilities surface_capabilities = {};
|
||||
wgpuSurfaceGetCapabilities(wgpu_surface, adapter, &surface_capabilities);
|
||||
|
||||
preferred_fmt = surface_capabilities.formats[0];
|
||||
#endif // __EMSCRIPTEN__
|
||||
#endif // IMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
|
||||
wgpu_surface_configuration.presentMode = WGPUPresentMode_Fifo;
|
||||
wgpu_surface_configuration.alphaMode = WGPUCompositeAlphaMode_Auto;
|
||||
wgpu_surface_configuration.usage = WGPUTextureUsage_RenderAttachment;
|
||||
wgpu_surface_configuration.width = wgpu_surface_width;
|
||||
wgpu_surface_configuration.height = wgpu_surface_height;
|
||||
wgpu_surface_configuration.device = wgpu_device;
|
||||
wgpu_surface_configuration.format = preferred_fmt;
|
||||
|
||||
wgpuSurfaceConfigure(wgpu_surface, &wgpu_surface_configuration);
|
||||
|
||||
wgpu_queue = wgpuDeviceGetQueue(wgpu_device);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void CreateSwapChain(int width, int height)
|
||||
{
|
||||
if (wgpu_swap_chain)
|
||||
wgpuSwapChainRelease(wgpu_swap_chain);
|
||||
wgpu_swap_chain_width = width;
|
||||
wgpu_swap_chain_height = height;
|
||||
WGPUSwapChainDescriptor swap_chain_desc = {};
|
||||
swap_chain_desc.usage = WGPUTextureUsage_RenderAttachment;
|
||||
swap_chain_desc.format = wgpu_preferred_fmt;
|
||||
swap_chain_desc.width = width;
|
||||
swap_chain_desc.height = height;
|
||||
swap_chain_desc.presentMode = WGPUPresentMode_Fifo;
|
||||
wgpu_swap_chain = wgpuDeviceCreateSwapChain(wgpu_device, wgpu_surface, &swap_chain_desc);
|
||||
}
|
||||
|
Reference in New Issue
Block a user