mirror of
https://github.com/ocornut/imgui.git
synced 2026-01-11 00:04:24 +00:00
commit
42015f7194
27 changed files with 3057 additions and 349 deletions
|
|
@ -24,6 +24,7 @@
|
|||
// CHANGELOG
|
||||
// (minor and older changes stripped away, please see git history for details)
|
||||
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
|
||||
// 2025-11-05: Fixed an issue with missing characters events when an already active text field changes viewports. (#9054)
|
||||
// 2025-10-22: Fixed Platform_OpenInShellFn() return value (unused in core).
|
||||
// 2025-09-24: Skip using the SDL_GetGlobalMouseState() state when one of our window is hovered, as the SDL_EVENT_MOUSE_MOTION data is reliable. Fix macOS notch mouse coordinates issue in fullscreen mode + better perf on X11. (#7919, #7786)
|
||||
// 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown.
|
||||
|
|
@ -124,6 +125,8 @@ struct ImGui_ImplSDL3_Data
|
|||
|
||||
// IME handling
|
||||
SDL_Window* ImeWindow;
|
||||
ImGuiPlatformImeData ImeData;
|
||||
bool ImeDirty;
|
||||
|
||||
// Mouse handling
|
||||
Uint32 MouseWindowID;
|
||||
|
|
@ -153,6 +156,7 @@ static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData()
|
|||
}
|
||||
|
||||
// Forward Declarations
|
||||
static void ImGui_ImplSDL3_UpdateIme();
|
||||
static void ImGui_ImplSDL3_UpdateMonitors();
|
||||
static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context);
|
||||
static void ImGui_ImplSDL3_ShutdownMultiViewportSupport();
|
||||
|
|
@ -172,21 +176,45 @@ static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text)
|
|||
SDL_SetClipboardText(text);
|
||||
}
|
||||
|
||||
static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
|
||||
static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id)
|
||||
{
|
||||
return ImGui::FindViewportByPlatformHandle((void*)(intptr_t)window_id);
|
||||
}
|
||||
|
||||
static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data)
|
||||
{
|
||||
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||
SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
|
||||
SDL_Window* window = SDL_GetWindowFromID(window_id);
|
||||
bd->ImeData = *data;
|
||||
bd->ImeDirty = true;
|
||||
ImGui_ImplSDL3_UpdateIme();
|
||||
}
|
||||
|
||||
// We discard viewport passed via ImGuiPlatformImeData and always call SDL_StartTextInput() on SDL_GetKeyboardFocus().
|
||||
static void ImGui_ImplSDL3_UpdateIme()
|
||||
{
|
||||
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
|
||||
ImGuiPlatformImeData* data = &bd->ImeData;
|
||||
SDL_Window* window = SDL_GetKeyboardFocus();
|
||||
|
||||
// Stop previous input
|
||||
if ((!(data->WantVisible || data->WantTextInput) || bd->ImeWindow != window) && bd->ImeWindow != nullptr)
|
||||
{
|
||||
SDL_StopTextInput(bd->ImeWindow);
|
||||
bd->ImeWindow = nullptr;
|
||||
}
|
||||
if ((!bd->ImeDirty && bd->ImeWindow == window) || (window == NULL))
|
||||
return;
|
||||
|
||||
// Start/update current input
|
||||
bd->ImeDirty = false;
|
||||
if (data->WantVisible)
|
||||
{
|
||||
ImVec2 viewport_pos;
|
||||
if (ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(window)))
|
||||
viewport_pos = viewport->Pos;
|
||||
SDL_Rect r;
|
||||
r.x = (int)(data->InputPos.x - viewport->Pos.x);
|
||||
r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight);
|
||||
r.x = (int)(data->InputPos.x - viewport_pos.x);
|
||||
r.y = (int)(data->InputPos.y - viewport_pos.y + data->InputLineHeight);
|
||||
r.w = 1;
|
||||
r.h = (int)data->InputLineHeight;
|
||||
SDL_SetTextInputArea(window, &r, 0);
|
||||
|
|
@ -358,11 +386,6 @@ static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
|
|||
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0);
|
||||
}
|
||||
|
||||
static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id)
|
||||
{
|
||||
return ImGui::FindViewportByPlatformHandle((void*)(intptr_t)window_id);
|
||||
}
|
||||
|
||||
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||||
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||||
|
|
@ -985,6 +1008,7 @@ void ImGui_ImplSDL3_NewFrame()
|
|||
|
||||
ImGui_ImplSDL3_UpdateMouseData();
|
||||
ImGui_ImplSDL3_UpdateMouseCursor();
|
||||
ImGui_ImplSDL3_UpdateIme();
|
||||
|
||||
// Update game controllers (if enabled and available)
|
||||
ImGui_ImplSDL3_UpdateGamepads();
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@
|
|||
|
||||
#ifndef IMGUI_DISABLE
|
||||
#include "imgui_impl_wgpu.h"
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// One of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
|
|
@ -65,9 +67,6 @@
|
|||
#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
#include <webgpu/webgpu.h>
|
||||
|
||||
#ifdef IMGUI_IMPL_WEBGPU_BACKEND_DAWN
|
||||
// Dawn renamed WGPUProgrammableStageDescriptor to WGPUComputeState (see: https://github.com/webgpu-native/webgpu-headers/pull/413)
|
||||
// Using type alias until WGPU adopts the same naming convention (#8369)
|
||||
|
|
@ -275,7 +274,7 @@ static WGPUProgrammableStageDescriptor ImGui_ImplWGPU_CreateShaderModule(const c
|
|||
#endif
|
||||
|
||||
WGPUShaderModuleDescriptor desc = {};
|
||||
desc.nextInChain = reinterpret_cast<WGPUChainedStruct*>(&wgsl_desc);
|
||||
desc.nextInChain = (WGPUChainedStruct*)&wgsl_desc;
|
||||
|
||||
WGPUProgrammableStageDescriptor stage_desc = {};
|
||||
stage_desc.module = wgpuDeviceCreateShaderModule(bd->wgpuDevice, &desc);
|
||||
|
|
@ -917,6 +916,173 @@ void ImGui_ImplWGPU_NewFrame()
|
|||
IM_ASSERT(0 && "ImGui_ImplWGPU_CreateDeviceObjects() failed!");
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Internal Helpers
|
||||
// Those are currently used by our example applications.
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
bool ImGui_ImplWGPU_IsSurfaceStatusError(WGPUSurfaceGetCurrentTextureStatus status)
|
||||
{
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
return (status == WGPUSurfaceGetCurrentTextureStatus_Error);
|
||||
#else
|
||||
return (status == WGPUSurfaceGetCurrentTextureStatus_OutOfMemory || status == WGPUSurfaceGetCurrentTextureStatus_DeviceLost);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(WGPUSurfaceGetCurrentTextureStatus status)
|
||||
{
|
||||
#if defined(__EMSCRIPTEN__) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
return (status == WGPUSurfaceGetCurrentTextureStatus_Timeout || status == WGPUSurfaceGetCurrentTextureStatus_Outdated || status == WGPUSurfaceGetCurrentTextureStatus_Lost);
|
||||
#else
|
||||
return (status == WGPUSurfaceGetCurrentTextureStatus_Timeout || status == WGPUSurfaceGetCurrentTextureStatus_Outdated || status == WGPUSurfaceGetCurrentTextureStatus_Lost || status == WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Helpers to obtain a string
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
const char* ImGui_ImplWGPU_GetErrorTypeName(WGPUErrorType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WGPUErrorType_Validation: return "Validation";
|
||||
case WGPUErrorType_OutOfMemory: return "OutOfMemory";
|
||||
case WGPUErrorType_Unknown: return "Unknown";
|
||||
case WGPUErrorType_Internal: return "Internal";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
const char* ImGui_ImplWGPU_GetDeviceLostReasonName(WGPUDeviceLostReason type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WGPUDeviceLostReason_Unknown: return "Unknown";
|
||||
case WGPUDeviceLostReason_Destroyed: return "Destroyed";
|
||||
case WGPUDeviceLostReason_CallbackCancelled: return "CallbackCancelled";
|
||||
case WGPUDeviceLostReason_FailedCreation: return "FailedCreation";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
#elif !defined(__EMSCRIPTEN__)
|
||||
const char* ImGui_ImplWGPU_GetLogLevelName(WGPULogLevel level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case WGPULogLevel_Error: return "Error";
|
||||
case WGPULogLevel_Warn: return "Warn";
|
||||
case WGPULogLevel_Info: return "Info";
|
||||
case WGPULogLevel_Debug: return "Debug";
|
||||
case WGPULogLevel_Trace: return "Trace";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
const char* ImGui_ImplWGPU_GetBackendTypeName(WGPUBackendType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WGPUBackendType_WebGPU: return "WebGPU";
|
||||
case WGPUBackendType_D3D11: return "D3D11";
|
||||
case WGPUBackendType_D3D12: return "D3D12";
|
||||
case WGPUBackendType_Metal: return "Metal";
|
||||
case WGPUBackendType_Vulkan: return "Vulkan";
|
||||
case WGPUBackendType_OpenGL: return "OpenGL";
|
||||
case WGPUBackendType_OpenGLES: return "OpenGLES";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const char* ImGui_ImplWGPU_GetAdapterTypeName(WGPUAdapterType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WGPUAdapterType_DiscreteGPU: return "DiscreteGPU";
|
||||
case WGPUAdapterType_IntegratedGPU: return "IntegratedGPU";
|
||||
case WGPUAdapterType_CPU: return "CPU";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
void ImGui_ImplWGPU_DebugPrintAdapterInfo(const WGPUAdapter& adapter)
|
||||
{
|
||||
WGPUAdapterInfo info = {};
|
||||
wgpuAdapterGetInfo(adapter, &info);
|
||||
printf("description: \"%.*s\"\n", (int)info.description.length, info.description.data);
|
||||
printf("vendor: \"%.*s\", vendorID: %x\n", (int)info.vendor.length, info.vendor.data, info.vendorID);
|
||||
printf("architecture: \"%.*s\"\n", (int) info.architecture.length, info.architecture.data);
|
||||
printf("device: \"%.*s\", deviceID: %x\n", (int)info.device.length, info.device.data, info.deviceID);
|
||||
printf("backendType: \"%s\"\n", ImGui_ImplWGPU_GetBackendTypeName(info.backendType));
|
||||
printf("adapterType: \"%s\"\n", ImGui_ImplWGPU_GetAdapterTypeName(info.adapterType));
|
||||
wgpuAdapterInfoFreeMembers(info);
|
||||
}
|
||||
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) && !defined(__EMSCRIPTEN__)
|
||||
|
||||
#if defined(__APPLE__)
|
||||
// MacOS specific: is necessary to compile with "-x objective-c++" flags
|
||||
// (e.g. using cmake: set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++") )
|
||||
#include <Cocoa/Cocoa.h>
|
||||
#include <QuartzCore/CAMetalLayer.h>
|
||||
#endif
|
||||
|
||||
WGPUSurface ImGui_ImplWGPU_CreateWGPUSurfaceHelper(ImGui_ImplWGPU_CreateSurfaceInfo* info)
|
||||
{
|
||||
WGPUSurfaceDescriptor surface_descriptor = {};
|
||||
WGPUSurface surface = {};
|
||||
#if defined(__APPLE__)
|
||||
if (strcmp(info->System, "cocoa") == 0)
|
||||
{
|
||||
IM_ASSERT(info->RawWindow != nullptr);
|
||||
NSWindow* ns_window = (NSWindow*)info->RawWindow;
|
||||
id metal_layer = [CAMetalLayer layer];
|
||||
[ns_window.contentView setWantsLayer : YES] ;
|
||||
[ns_window.contentView setLayer : metal_layer] ;
|
||||
WGPUSurfaceSourceMetalLayer surface_src_metal = {};
|
||||
surface_src_metal.chain.sType = WGPUSType_SurfaceSourceMetalLayer;
|
||||
surface_src_metal.layer = metal_layer;
|
||||
surface_descriptor.nextInChain = &surface_src_metal.chain;
|
||||
surface = wgpuInstanceCreateSurface(info->Instance, &surface_descriptor);
|
||||
}
|
||||
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
|
||||
if (strcmp(info->System, "wayland") == 0)
|
||||
{
|
||||
IM_ASSERT(info->RawDisplay != nullptr && info->RawSurface != nullptr);
|
||||
WGPUSurfaceSourceWaylandSurface surface_src_wayland = {};
|
||||
surface_src_wayland.chain.sType = WGPUSType_SurfaceSourceWaylandSurface;
|
||||
surface_src_wayland.display = info->RawDisplay;
|
||||
surface_src_wayland.surface = info->RawSurface;
|
||||
surface_descriptor.nextInChain = &surface_src_wayland.chain;
|
||||
surface = wgpuInstanceCreateSurface(info->Instance, &surface_descriptor);
|
||||
}
|
||||
else if (strcmp(info->System, "x11") == 0)
|
||||
{
|
||||
IM_ASSERT(info->RawDisplay != nullptr && info->RawWindow != nullptr);
|
||||
WGPUSurfaceSourceXlibWindow surface_src_xlib = {};
|
||||
surface_src_xlib.chain.sType = WGPUSType_SurfaceSourceXlibWindow;
|
||||
surface_src_xlib.display = info->RawDisplay;
|
||||
surface_src_xlib.window = (uint64_t)info->RawWindow;
|
||||
surface_descriptor.nextInChain = &surface_src_xlib.chain;
|
||||
surface = wgpuInstanceCreateSurface(info->Instance, &surface_descriptor);
|
||||
}
|
||||
#elif defined(_WIN32)
|
||||
if (strcmp(info->System, "win32") == 0)
|
||||
{
|
||||
IM_ASSERT(info->RawWindow != nullptr && info->RawInstance != nullptr);
|
||||
WGPUSurfaceSourceWindowsHWND surface_src_hwnd = {};
|
||||
surface_src_hwnd.chain.sType = WGPUSType_SurfaceSourceWindowsHWND;
|
||||
surface_src_hwnd.hinstance = info->RawInstance;
|
||||
surface_src_hwnd.hwnd = info->RawWindow;
|
||||
surface_descriptor.nextInChain = &surface_src_hwnd.chain;
|
||||
surface = wgpuInstanceCreateSurface(info->Instance, &surface_descriptor);
|
||||
}
|
||||
#else
|
||||
#error "Unsupported WebGPU native platform!"
|
||||
#endif
|
||||
return surface;
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif // #ifndef IMGUI_DISABLE
|
||||
|
|
|
|||
|
|
@ -43,6 +43,9 @@
|
|||
#endif
|
||||
|
||||
#include <webgpu/webgpu.h>
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) && !defined(__EMSCRIPTEN__)
|
||||
#include <webgpu/wgpu.h> // WGPULogLevel
|
||||
#endif
|
||||
|
||||
// Initialization data, for ImGui_ImplWGPU_Init()
|
||||
struct ImGui_ImplWGPU_InitInfo
|
||||
|
|
@ -83,4 +86,38 @@ struct ImGui_ImplWGPU_RenderState
|
|||
WGPURenderPassEncoder RenderPassEncoder;
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Internal Helpers
|
||||
// Those are currently used by our example applications.
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// (Optional) Helper to wrap some of the Dawn/WGPU/Emscripten quirks
|
||||
bool ImGui_ImplWGPU_IsSurfaceStatusError(WGPUSurfaceGetCurrentTextureStatus status);
|
||||
bool ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(WGPUSurfaceGetCurrentTextureStatus status); // Return whether the texture is suboptimal and may need to be recreated.
|
||||
|
||||
// (Optional) Helper for debugging/logging
|
||||
void ImGui_ImplWGPU_DebugPrintAdapterInfo(const WGPUAdapter& adapter);
|
||||
const char* ImGui_ImplWGPU_GetBackendTypeName(WGPUBackendType type);
|
||||
const char* ImGui_ImplWGPU_GetAdapterTypeName(WGPUAdapterType type);
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
const char* ImGui_ImplWGPU_GetDeviceLostReasonName(WGPUDeviceLostReason type);
|
||||
const char* ImGui_ImplWGPU_GetErrorTypeName(WGPUErrorType type);
|
||||
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) && !defined(__EMSCRIPTEN__)
|
||||
const char* ImGui_ImplWGPU_GetLogLevelName(WGPULogLevel level);
|
||||
#endif
|
||||
|
||||
// (Optional) Helper to create a surface on macOS/Wayland/X11/Window
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) && !defined(__EMSCRIPTEN__)
|
||||
struct ImGui_ImplWGPU_CreateSurfaceInfo
|
||||
{
|
||||
WGPUInstance Instance;
|
||||
const char* System; // "cocoa" | "wayland" | "x11" | "win32"
|
||||
void* RawWindow; // NSWindow* | 0 | Window | HWND
|
||||
void* RawDisplay; // 0 | wl_display* | Display* | 0
|
||||
void* RawSurface; // | wl_surface* | 0 | 0
|
||||
void* RawInstance; // 0 | 0 | 0 | HINSTANCE
|
||||
};
|
||||
WGPUSurface ImGui_ImplWGPU_CreateWGPUSurfaceHelper(ImGui_ImplWGPU_CreateSurfaceInfo* info);
|
||||
#endif
|
||||
|
||||
#endif // #ifndef IMGUI_DISABLE
|
||||
|
|
|
|||
|
|
@ -47,6 +47,9 @@ Other Changes:
|
|||
result in temporarily incorrect state, which would lead to bugs to side effects
|
||||
in various locations, e.g. GetContentRegionAvail() calls or using clipper. (#9005)
|
||||
EndTable() was mistakenly restoring a wrong current table.
|
||||
- Tables: Angled headers: fixed an auto-resize feedback loop that could
|
||||
affect tables with empty non-resizing columns using angled headers, making
|
||||
them typically flicker back and forth between +0 and +1 pixels.
|
||||
- Disabled: fixed a bug when a previously enabled item that got nav focus
|
||||
and then turns disabled could still be activated using keyboard. (#9036)
|
||||
- InputText: when buffer is not resizable, trying to paste contents that
|
||||
|
|
@ -60,6 +63,9 @@ Other Changes:
|
|||
resizing the parent window while keeping the multi-line field active (which is
|
||||
most typically achieved when resizing programmatically or via a docking layout
|
||||
reacting to a platform window resize). (#3237, #9007) [@anton-kl, @ocornut]
|
||||
- Textures:
|
||||
- Fixed an issue preventing multi-contexts from using each others' fonts
|
||||
if context 2 runs after context 1's Render() function. (#9039)
|
||||
- MultiSelect: added ImGuiMultiSelectFlags_NoSelectOnRightClick to disable default
|
||||
right-click processing, which selects item on mouse down and is designed for
|
||||
context-menus. (#8200, #9015)
|
||||
|
|
@ -70,25 +76,42 @@ Other Changes:
|
|||
- Demo: About Box: emit infos to convey when IM_ASSERT() macro is disabled,
|
||||
- so users don't miss out on programming errors being reported.
|
||||
- so it is included in config/build info submitted in new GitHub Issues.
|
||||
- Debug Tools: fixed DebugTextEncoding() potentially reading out of bounds
|
||||
if provided a trailing truncated UTF-8 sequence.
|
||||
- Debug Tools:
|
||||
- Fixed DebugTextEncoding() potentially reading out of bounds when
|
||||
provided a trailing truncated UTF-8 sequence.
|
||||
- Metrics: fixed table and columns rect highlight from display when
|
||||
debug/metrics window is not in the same viewport as the table.
|
||||
- Backends:
|
||||
- GLFW: fixed building on Linux platforms where Wayland headers
|
||||
are not available. (#9024, #8969, #8921, #8920) [@jagot]
|
||||
- SDL3: fixed Platform_OpenInShellFn() return value (the return value
|
||||
was unused in core but might be used by a direct caller). (#9027) [@achabense]
|
||||
- SDL3: fixed an issue with missing characters events when an already active text
|
||||
field changes viewports. (#9054)
|
||||
- Vulkan: added IMGUI_IMPL_VULKAN_VOLK_FILENAME to configure path to
|
||||
Volk (default to "volk.h"). (#9008, #7722, #6582, #4854) [@mwlasiuk]
|
||||
- WebGPU: added various internal/optional helpers to wrap some of the
|
||||
Dawn/WGPU/Emscripten debacle quirks: (#8381) [@brutpitt]
|
||||
- ImGui_ImplWGPU_CreateWGPUSurfaceHelper().
|
||||
- ImGui_ImplWGPU_IsSurfaceStatusError(), ImGui_ImplWGPU_IsSurfaceStatusSubOptimal().
|
||||
- ImGui_ImplWGPU_DebugPrintAdapterInfo(),
|
||||
- ImGui_ImplWGPU_GetBackendTypeName(), ImGui_ImplWGPU_GetAdapterTypeName(),
|
||||
ImGui_ImplWGPU_GetDeviceLostReasonName(), ImGui_ImplWGPU_GetErrorTypeName(),
|
||||
ImGui_ImplWGPU_GetLogLevelName().
|
||||
- WebGPU: update to compile with Dawn and Emscripten's 4.0.10+
|
||||
'--use-port=emdawnwebgpu' ports. (#8381, #8898) [@brutpitt, @trbabb]
|
||||
'--use-port=emdawnwebgpu' ports. (#8381, #8898, #7435) [@brutpitt, @trbabb]
|
||||
When using Emscripten 4.0.10+, backend now defaults to IMGUI_IMPL_WEBGPU_BACKEND_DAWN
|
||||
instead of IMGUI_IMPL_WEBGPU_BACKEND_WGPU, if neither are specified.
|
||||
(note: examples application were not updated yet)
|
||||
- Win32: Revert 1.92.4 change of comparing dwPacketNumber, which prevents
|
||||
refreshing accurate gamepad info after focus-out + io.ClearInputKeys(). (#8556)
|
||||
- Examples:
|
||||
- GLFW+WebGPU: update example for latest specs, to work on Emscripten 4.0.10+,
|
||||
latest Dawn-Native and WGPU-Native. (#8381, #8567, #8191, #7435) [@brutpitt]
|
||||
- GLFW+WebGPU: removed unnecessary ImGui_ImplWGPU_InvalidateDeviceObjects() call
|
||||
during surface resize. (#8381)
|
||||
- SDL2+WebGPU: added new example (Emscripten + native Dawn/WGPU). (#8381) [@brutpitt]
|
||||
- SDL3+WebGPU: added new example (Emscripten + native Dawn/WGPU). (#8381) [@brutpitt]
|
||||
|
||||
Docking+Viewports Branch:
|
||||
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ OSX + OpenGL2 example. <BR>
|
|||
You may prefer to use the GLFW Or SDL backends, which will also support Windows and Linux.)
|
||||
|
||||
[example_glfw_wgpu/](https://github.com/ocornut/imgui/blob/master/examples/example_glfw_wgpu/) <BR>
|
||||
GLFW + WebGPU example. Supports Emscripten (web) or Dawn (desktop) <BR>
|
||||
GLFW + WebGPU example. Supports Emscripten (web), Dawn (native), WGPU (native). <BR>
|
||||
= main.cpp + imgui_impl_glfw.cpp + imgui_impl_wgpu.cpp
|
||||
Note that the 'example_glfw_opengl3' and 'example_sdl2_opengl3' examples also supports Emscripten!
|
||||
|
||||
|
|
@ -149,6 +149,11 @@ SDL2 (Win32, Mac, Linux, etc.) + Vulkan example. <BR>
|
|||
This is quite long and tedious, because: Vulkan. <BR>
|
||||
For this example, the main.cpp file exceptionally use helpers function from imgui_impl_vulkan.h/cpp.
|
||||
|
||||
[example_sdl2_wgpu/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl2_wgpu/) <BR>
|
||||
SDL2 + WebGPU example. Supports Emscripten (web), Dawn (native), WGPU (native). <BR>
|
||||
= main.cpp + imgui_impl_sdl2.cpp + imgui_impl_wgpu.cpp
|
||||
(note that the 'example_sdl2_opengl3' example also supports Emscripten!)
|
||||
|
||||
[example_sdl3_directx11/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_directx11/) <BR>
|
||||
SDL3 + DirectX11 examples, Windows only. <BR>
|
||||
= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_dx11.cpp <BR>
|
||||
|
|
@ -177,6 +182,11 @@ SDL3 (Win32, Mac, Linux, etc.) + Vulkan example. <BR>
|
|||
This is quite long and tedious, because: Vulkan. <BR>
|
||||
For this example, the main.cpp file exceptionally use helpers function from imgui_impl_vulkan.h/cpp.
|
||||
|
||||
[example_sdl3_wgpu/](https://github.com/ocornut/imgui/blob/master/examples/example_sdl3_wgpu/) <BR>
|
||||
SDL3 + WebGPU example. Supports Emscripten (web), Dawn (native), WGPU (native). <BR>
|
||||
= main.cpp + imgui_impl_sdl3.cpp + imgui_impl_wgpu.cpp
|
||||
(note that the 'example_sdl3_opengl3' example also supports Emscripten!)
|
||||
|
||||
[example_win32_directx9/](https://github.com/ocornut/imgui/blob/master/examples/example_win32_directx9/) <BR>
|
||||
DirectX9 example, Windows only. <BR>
|
||||
= main.cpp + imgui_impl_win32.cpp + imgui_impl_dx9.cpp
|
||||
|
|
|
|||
|
|
@ -6,18 +6,30 @@
|
|||
# * build/Debug/example_glfw_wgpu[.exe]
|
||||
# * build/example_glfw_wgpu[.exe]
|
||||
|
||||
# Building for desktop (WGPU-Native) with WGPU-Native:
|
||||
# 1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
|
||||
# 2. unzip the downloaded file in your_preferred_folder
|
||||
# 3. cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder ("full path" or "relative" starting from current directory)
|
||||
# 4. cmake --build build
|
||||
# The resulting binary will be found at one of the following locations:
|
||||
# * build/Debug/example_glfw_wgpu[.exe]
|
||||
# * build/example_glfw_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
|
||||
# (optional) -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py", see ReadMe.md
|
||||
# 3. cmake --build build
|
||||
# 4. emrun build/index.html
|
||||
|
||||
cmake_minimum_required(VERSION 3.10.2)
|
||||
cmake_minimum_required(VERSION 3.22) # Dawn requires CMake >= 3.22
|
||||
project(imgui_example_glfw_wgpu C CXX)
|
||||
|
||||
set(IMGUI_EXECUTABLE example_glfw_wgpu)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
|
||||
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17) # Dawn requires C++17
|
||||
|
|
@ -25,93 +37,179 @@ set(CMAKE_CXX_STANDARD 17) # Dawn requires C++17
|
|||
# Dear ImGui
|
||||
set(IMGUI_DIR ../../)
|
||||
|
||||
# Libraries
|
||||
set(IMGUI_EXAMPLE_SOURCE_FILES
|
||||
# Example code
|
||||
main.cpp
|
||||
# Dear ImGui Backend files
|
||||
${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
|
||||
${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp
|
||||
# Dear ImGui files
|
||||
${IMGUI_DIR}/imgui.cpp
|
||||
${IMGUI_DIR}/imgui_draw.cpp
|
||||
${IMGUI_DIR}/imgui_demo.cpp
|
||||
${IMGUI_DIR}/imgui_tables.cpp
|
||||
${IMGUI_DIR}/imgui_widgets.cpp
|
||||
)
|
||||
|
||||
if(EMSCRIPTEN)
|
||||
if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "3.1.57")
|
||||
set(IMGUI_EMSCRIPTEN_GLFW3 "--use-port=contrib.glfw3" CACHE STRING "Choose between --use-port=contrib.glfw3 and -sUSE_GLFW=3 for GLFW implementation (default to --use-port=contrib.glfw3)")
|
||||
else()
|
||||
# cannot use contrib.glfw3 prior to 3.1.57
|
||||
set(IMGUI_EMSCRIPTEN_GLFW3 "-sUSE_GLFW=3" CACHE STRING "Use -sUSE_GLFW=3 for GLFW implementation" FORCE)
|
||||
endif()
|
||||
set(LIBRARIES glfw)
|
||||
add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1 -DIMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
if(NOT IMGUI_EMSCRIPTEN_WEBGPU_FLAG) # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG not used, set by current EMSCRIPTEN version
|
||||
if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "4.0.10")
|
||||
set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "--use-port=emdawnwebgpu" CACHE STRING "Choose between --use-port=emdawnwebgpu (Dawn implementation of EMSCRIPTEN) and -sUSE_WEBGPU=1 (WGPU implementation of EMSCRIPTEN, deprecated in 4.0.10): default to --use-port=emdawnwebgpu for EMSCRIPTEN >= 4.0.10")
|
||||
else()
|
||||
set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "-sUSE_WEBGPU=1" CACHE STRING "Use -sUSE_WEBGPU=1 for EMSCRIPTEN WGPU implementation")
|
||||
endif()
|
||||
else() # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG used, check correct version
|
||||
if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.10" AND "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
|
||||
# it's necessary EMSCRIPTEN >= v4.0.10 (although "--use-port=path/to/emdawnwebgpu.port.py" is supported/tested from v4.0.8)
|
||||
message(FATAL_ERROR "emdawnwebgpu needs EMSCRIPTEN version >= 4.0.10")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "3.1.57")
|
||||
set(IMGUI_EMSCRIPTEN_GLFW3 "--use-port=contrib.glfw3" CACHE STRING "Choose between --use-port=contrib.glfw3 and -sUSE_GLFW=3 for GLFW implementation (default to --use-port=contrib.glfw3)")
|
||||
else() # cannot use contrib.glfw3 prior to 3.1.57
|
||||
set(IMGUI_EMSCRIPTEN_GLFW3 "-sUSE_GLFW=3" CACHE STRING "Use -sUSE_GLFW=3 for GLFW implementation" FORCE)
|
||||
endif()
|
||||
|
||||
set(LIBRARIES glfw)
|
||||
add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
|
||||
else() # Native/Desktop build
|
||||
# Check DAWN/WGPU directory
|
||||
if(NOT IMGUI_DAWN_DIR AND NOT IMGUI_WGPU_DIR) # if it's Native/Desktop build, IMGUI_DAWN_DIR or IMGUI_WGPU_DIR must be specified
|
||||
message(FATAL_ERROR "Please specify the Dawn or WGPU base directory")
|
||||
endif()
|
||||
|
||||
if(IMGUI_DAWN_DIR AND IMGUI_WGPU_DIR) # both IMGUI_DAWN_DIR and IMGUI_WGPU_DIR cannot be set
|
||||
message(FATAL_ERROR "Please specify only one between Dawn / WGPU base directory")
|
||||
endif()
|
||||
|
||||
if(APPLE) # Add SDL2 module to get Surface, with libs and file property for MacOS build
|
||||
set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
|
||||
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal -framework MetalKit -framework Cocoa")
|
||||
endif()
|
||||
|
||||
find_package(glfw3 REQUIRED)
|
||||
|
||||
if(IMGUI_DAWN_DIR) # DAWN-Native build options
|
||||
list(APPEND CMAKE_PREFIX_PATH ${IMGUI_DAWN_DIR})
|
||||
find_package(Dawn) # Search for a Dawn installation using IMGUI_DAWN_DIR in CMAKE_PREFIX_PATH
|
||||
if(Dawn_FOUND)
|
||||
message("Dawn Installation has been found!")
|
||||
set(LIBRARIES dawn::webgpu_dawn glfw ${OS_LIBRARIES})
|
||||
else()
|
||||
set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
|
||||
|
||||
option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON)
|
||||
set(DAWN_BUILD_MONOLITHIC_LIBRARY "STATIC" CACHE STRING "Build monolithic library: SHARED, STATIC, or OFF.")
|
||||
|
||||
option(DAWN_USE_GLFW OFF) # disable buildin GLFW in DAWN when we use SDL2 / SDL3
|
||||
|
||||
# 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)
|
||||
option(TINT_BUILD_DOCS "Build documentation" OFF)
|
||||
option(TINT_BUILD_TESTS "Build tests" OFF)
|
||||
if(NOT APPLE)
|
||||
option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
|
||||
endif()
|
||||
if(WIN32)
|
||||
option(DAWN_FORCE_SYSTEM_COMPONENT_LOAD "Allow system component fallback" ON)
|
||||
option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
|
||||
option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
|
||||
option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
|
||||
option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
|
||||
option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
|
||||
option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
|
||||
endif()
|
||||
# check if WAYLAND is the current Session Type and enable DAWN_USE_WAYLAND Wayland option @compile time
|
||||
# You can override this using: cmake -DDAWN_USE_WAYLAND=X (X = ON | OFF)
|
||||
if(LINUX)
|
||||
if($ENV{XDG_SESSION_TYPE} MATCHES wayland)
|
||||
option(DAWN_USE_WAYLAND "Enable support for Wayland surface" ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL)
|
||||
|
||||
set(LIBRARIES webgpu_dawn glfw)
|
||||
endif()
|
||||
else() # WGPU-Native build options
|
||||
set(WGPU_NATIVE_LIB_DIR ${IMGUI_WGPU_DIR}/lib)
|
||||
find_library(WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native HINTS ${WGPU_NATIVE_LIB_DIR} REQUIRED)
|
||||
if(WIN32)
|
||||
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32 Propsys RuntimeObject)
|
||||
elseif(UNIX AND NOT APPLE)
|
||||
set(OS_LIBRARIES "-lm -ldl")
|
||||
endif()
|
||||
|
||||
set(LIBRARIES glfw ${WGPU_LIBRARY} ${OS_LIBRARIES})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(${IMGUI_EXECUTABLE} ${IMGUI_EXAMPLE_SOURCE_FILES})
|
||||
|
||||
target_include_directories(${IMGUI_EXECUTABLE} PUBLIC
|
||||
${IMGUI_DIR}
|
||||
${IMGUI_DIR}/backends
|
||||
)
|
||||
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_EXAMPLE_GLFW_WGPU")
|
||||
|
||||
# compiler option only for IMGUI_EXAMPLE_SOURCE_FILES
|
||||
if (MSVC)
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /W4) # warning level 4
|
||||
else()
|
||||
# Dawn wgpu desktop
|
||||
set(DAWN_FETCH_DEPENDENCIES ON)
|
||||
set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
|
||||
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)
|
||||
option(TINT_BUILD_DOCS "Build documentation" OFF)
|
||||
option(TINT_BUILD_TESTS "Build tests" OFF)
|
||||
if (NOT APPLE)
|
||||
option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
|
||||
endif()
|
||||
if(WIN32)
|
||||
option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
|
||||
option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
|
||||
option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
|
||||
option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
|
||||
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)
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC -Wall) # -Wextra -Wpedantic
|
||||
endif()
|
||||
|
||||
add_executable(example_glfw_wgpu
|
||||
main.cpp
|
||||
# backend files
|
||||
${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
|
||||
${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp
|
||||
# Dear ImGui files
|
||||
${IMGUI_DIR}/imgui.cpp
|
||||
${IMGUI_DIR}/imgui_draw.cpp
|
||||
${IMGUI_DIR}/imgui_demo.cpp
|
||||
${IMGUI_DIR}/imgui_tables.cpp
|
||||
${IMGUI_DIR}/imgui_widgets.cpp
|
||||
)
|
||||
IF(NOT EMSCRIPTEN)
|
||||
target_compile_definitions(example_glfw_wgpu PUBLIC
|
||||
"IMGUI_IMPL_WEBGPU_BACKEND_DAWN"
|
||||
)
|
||||
endif()
|
||||
target_include_directories(example_glfw_wgpu PUBLIC
|
||||
${IMGUI_DIR}
|
||||
${IMGUI_DIR}/backends
|
||||
)
|
||||
# In this example IMGUI_IMPL_WEBGPU_BACKEND_DAWN / IMGUI_IMPL_WEBGPU_BACKEND_WGPU internal define is set according to:
|
||||
# EMSCRIPTEN: by used FLAG
|
||||
# --use-port=emdawnwebgpu --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN defined
|
||||
# -sUSE_WEBGPU=1 --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU defined
|
||||
# NATIVE: by used SDK installation directory
|
||||
# if IMGUI_DAWN_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN defined
|
||||
# if IMGUI_WGPU_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU defined
|
||||
|
||||
target_link_libraries(example_glfw_wgpu PUBLIC ${LIBRARIES})
|
||||
if(NOT EMSCRIPTEN) # WegGPU-Native settings
|
||||
# Set IMGUI_IMPL_WEBGPU_BACKEND_XXXX based on the SDK (directory) used
|
||||
if(IMGUI_DAWN_DIR)
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
|
||||
if(NOT Dawn_FOUND)
|
||||
target_link_libraries(${IMGUI_EXECUTABLE} INTERFACE webgpu_cpp)
|
||||
endif()
|
||||
else()
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
|
||||
target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGPU_DIR}/include)
|
||||
endif()
|
||||
target_link_libraries(${IMGUI_EXECUTABLE} PUBLIC ${LIBRARIES})
|
||||
|
||||
# Emscripten settings
|
||||
if(EMSCRIPTEN)
|
||||
if("${IMGUI_EMSCRIPTEN_GLFW3}" STREQUAL "--use-port=contrib.glfw3")
|
||||
target_compile_options(example_glfw_wgpu PUBLIC
|
||||
"${IMGUI_EMSCRIPTEN_GLFW3}"
|
||||
else() # Emscripten settings
|
||||
set(CMAKE_EXECUTABLE_SUFFIX ".html")
|
||||
|
||||
if("${IMGUI_EMSCRIPTEN_GLFW3}" STREQUAL "--use-port=contrib.glfw3")
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_GLFW3}")
|
||||
endif()
|
||||
message(STATUS "Using ${IMGUI_EMSCRIPTEN_GLFW3} GLFW implementation")
|
||||
|
||||
if("${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}")
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
|
||||
else()
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
|
||||
endif()
|
||||
message(STATUS "Using ${IMGUI_EMSCRIPTEN_WEBGPU_FLAG} WebGPU implementation")
|
||||
|
||||
target_link_options(${IMGUI_EXECUTABLE} PRIVATE
|
||||
"${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}"
|
||||
"${IMGUI_EMSCRIPTEN_GLFW3}"
|
||||
"-sWASM=1"
|
||||
"-sASYNCIFY=1"
|
||||
"-sALLOW_MEMORY_GROWTH=1"
|
||||
"-sNO_EXIT_RUNTIME=0"
|
||||
"-sASSERTIONS=1"
|
||||
"-sDISABLE_EXCEPTION_CATCHING=1"
|
||||
"-sNO_FILESYSTEM=1"
|
||||
"--shell-file=${CMAKE_CURRENT_LIST_DIR}/../libs/emscripten/shell_minimal.html"
|
||||
)
|
||||
endif()
|
||||
message(STATUS "Using ${IMGUI_EMSCRIPTEN_GLFW3} GLFW implementation")
|
||||
target_link_options(example_glfw_wgpu PRIVATE
|
||||
"-sUSE_WEBGPU=1"
|
||||
"${IMGUI_EMSCRIPTEN_GLFW3}"
|
||||
"-sWASM=1"
|
||||
"-sALLOW_MEMORY_GROWTH=1"
|
||||
"-sNO_EXIT_RUNTIME=0"
|
||||
"-sASSERTIONS=1"
|
||||
"-sDISABLE_EXCEPTION_CATCHING=1"
|
||||
"-sNO_FILESYSTEM=1"
|
||||
)
|
||||
set_target_properties(example_glfw_wgpu PROPERTIES OUTPUT_NAME "index")
|
||||
# copy our custom index.html to build directory
|
||||
add_custom_command(TARGET example_glfw_wgpu POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_LIST_DIR}/web/index.html" $<TARGET_FILE_DIR:example_glfw_wgpu>
|
||||
)
|
||||
set_target_properties(${IMGUI_EXECUTABLE} PROPERTIES OUTPUT_NAME "index")
|
||||
endif()
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@
|
|||
# This Makefile assumes you have loaded emscripten's environment.
|
||||
# (On Windows, you may need to execute emsdk_env.bat or encmdprompt.bat ahead)
|
||||
#
|
||||
# Running `make` will produce three files:
|
||||
# - web/index.html (current stored in the repository)
|
||||
# Running `make -f Makefile.emscripten` will produce three files:
|
||||
# - web/index.html
|
||||
# - web/index.js
|
||||
# - web/index.wasm
|
||||
#
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
CC = emcc
|
||||
CXX = em++
|
||||
WEB_DIR = web
|
||||
EXE = $(WEB_DIR)/index.js
|
||||
EXE = $(WEB_DIR)/index.html
|
||||
IMGUI_DIR = ../..
|
||||
SOURCES = main.cpp
|
||||
SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
|
||||
|
|
@ -34,15 +34,19 @@ EMS =
|
|||
# ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
|
||||
# Note: For glfw, we use emscripten-glfw port (contrib.glfw3) instead of (-s USE_GLFW=3) to get a better support for High DPI displays.
|
||||
EMS += -s DISABLE_EXCEPTION_CATCHING=1 --use-port=contrib.glfw3
|
||||
LDFLAGS += -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1
|
||||
LDFLAGS += -s WASM=1
|
||||
LDFLAGS += -s ALLOW_MEMORY_GROWTH=1
|
||||
LDFLAGS += -s ASYNCIFY=1
|
||||
LDFLAGS += -s NO_EXIT_RUNTIME=0
|
||||
LDFLAGS += -s ASSERTIONS=1
|
||||
|
||||
# (1) Using legacy WebGPU implementation (Emscripten < 4.0.10)
|
||||
EMS += -DIMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
LDFLAGS += -s USE_WEBGPU=1
|
||||
#EMS += -DIMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
#LDFLAGS += -s USE_WEBGPU=1
|
||||
|
||||
# or (2) Using newer Dawn-based WebGPU port (Emscripten >= 4.0.10) (UNSUPPORTED YET)
|
||||
#EMS += --use-port=emdawnwebgpu
|
||||
#LDFLAGS += --use-port=emdawnwebgpu
|
||||
# or (2) Using newer Dawn-based WebGPU port (Emscripten >= 4.0.10)
|
||||
EMS += --use-port=emdawnwebgpu
|
||||
LDFLAGS += --use-port=emdawnwebgpu
|
||||
|
||||
# Build as single file (binary text encoded in .html file)
|
||||
#LDFLAGS += -sSINGLE_FILE
|
||||
|
|
@ -67,7 +71,7 @@ endif
|
|||
CPPFLAGS += -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
|
||||
#CPPFLAGS += -g
|
||||
CPPFLAGS += -Wall -Wformat -Os $(EMS)
|
||||
#LDFLAGS += --shell-file shell_minimal.html
|
||||
LDFLAGS += --shell-file ../libs/emscripten/shell_minimal.html
|
||||
LDFLAGS += $(EMS)
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -1,4 +1,145 @@
|
|||
## How to Build
|
||||
## How to Build
|
||||
|
||||
|
||||
---
|
||||
|
||||
### Using CMake
|
||||
#### Building for desktop (WebGPU-native) with Google Dawn:
|
||||
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_glfw_wgpu[.exe]
|
||||
* build/example_glfw_wgpu[.exe]
|
||||
|
||||
#### Building for desktop (WebGPU-Native) with WGPU:
|
||||
1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
|
||||
2. unzip the downloaded file in `your_preferred_folder`
|
||||
3. `cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder` ("full path" or "relative" starting from current directory)
|
||||
4. `cmake --build build`
|
||||
The resulting binary will be found at one of the following locations:
|
||||
* build/Debug/example_glfw_wgpu[.exe]
|
||||
* build/example_glfw_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`
|
||||
- (optional) `-DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py"`, see below
|
||||
4. `cmake --build build`
|
||||
|
||||
#### Sync Emscripten with latest Google Dawn:
|
||||
If you want to sync Emscripten with latest DAWN release it's necessary to download the `port-emdawnwgpu-package` (released daily by Google) here:
|
||||
https://github.com/google/dawn/releases
|
||||
Unpack it in your preferred folder and to replace the step 3 with:
|
||||
|
||||
3. `emcmake cmake -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py" -G Ninja -B build`
|
||||
|
||||
**N.B.**
|
||||
For the WASM code produced by Emscripten to work correctly, it will also be necessary to have the "corresponding" (or newer) version of Google Canary (nightly build for developers) that includes the latest changes
|
||||
|
||||
|
||||
---
|
||||
|
||||
### CMake by step
|
||||
|
||||
#### Generate Dawn Native:
|
||||
|
||||
- `cmake -G Ninja -DIMGUI_DAWN_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
- Using `IMGUI_DAWN_DIR` set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
|
||||
#### Generate WGPU Native:
|
||||
|
||||
- `cmake -G Ninja -DIMGUI_WGPU_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
|
||||
- Using `IMGUI_WGPU_DIR` set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten:
|
||||
|
||||
- `emcmake cmake -G Ninja -B where_to_build_dir`\
|
||||
CMake checks the EMSCRIPEN version then:
|
||||
- if EMS >= 4.0.10 uses `--use-port=emdawnwebgpu` flag to build
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
- if EMS < 4.0.10 uses `-sUSE_WEBGPU=1` flag to build
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten forcing `-sUSE_WEBGPU=1` deprecated flag even with EMS >= 4.0.10
|
||||
- `emcmake cmake -G Ninja -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="-sUSE_WEBGPU=1" -B where_to_build_dir`
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten using external WebGPU library (emdawnwebgpu_pkg)
|
||||
- `emcmake cmake -G Ninja -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path_to_emdawnwebgpu_pkg" -B where_to_build_dir`
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
- *To use external WebGPU library it's necessary to have EMS >= 4.0.10 or the minimum requirements specified by the package:*
|
||||
- https://github.com/google/dawn/releases
|
||||
|
||||
#### Build time
|
||||
|
||||
Once the procedure for the specific builder is generated, the build command is **always the same**:
|
||||
|
||||
- Build using CMake
|
||||
- `cmake --build where_to_build_dir`
|
||||
- It will use selected builder to build the example.
|
||||
|
||||
- Build explicitly:
|
||||
- `cd where_to_build_dir`
|
||||
- `ninja`
|
||||
- This is the builder chosen during the generation phase
|
||||
|
||||
---
|
||||
|
||||
### CMake useful options
|
||||
#### Generator types (alternative to **ninja** bulder):
|
||||
- `-G Ninja` to build with __ninja__ builder
|
||||
- `-G "Unix Makefiles"` to build with __make__ builder
|
||||
- `-G "Visual Studio 17 2022" -A x64` to create a VS 2022 solution (.sln) file, Windows only
|
||||
- **Native build only**
|
||||
- Not **officially** supported to build Google Dawn
|
||||
|
||||
Example:
|
||||
- using **make** instead **ninja**:
|
||||
- `cmake -G "Unix Makefiles" -DIMGUI_DAWN_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
|
||||
**Syntax is case sensitive and the "" are necessary in case of spaces between words*
|
||||
|
||||
#### Directories
|
||||
- The directory path can be absolute or relative (starting from the current directory)
|
||||
- It's necessary to use different `where_to_build_dir` for different CMake generations
|
||||
|
||||
|
||||
#### Build type
|
||||
The default build type is **Debug**
|
||||
It is possible to use a different build type using:
|
||||
- `-DCMAKE_BUILD_TYPE=Release`
|
||||
- `-DCMAKE_BUILD_TYPE=MinSizeRel`
|
||||
- `-DCMAKE_BUILD_TYPE=RelWithDebInfo`
|
||||
|
||||
Example:
|
||||
- building **Release**:
|
||||
- `cmake -G ninja -DIMGUI_WGPU_DIR=path_to_sdk_dir -DCMAKE_BUILD_TYPE=Release -B where_to_build_dir `
|
||||
|
||||
#### GLFW / SDL2 / SDL3 includes, libraries, search paths and package manager
|
||||
|
||||
Includes and libraries, by default, are searched in system/compiler paths (environment variables): you can add the path to your development tools to the environment variables without having to modify the `CMakeLists.txt` file.
|
||||
- e.g. CLang search in path specified from the following environment variables:
|
||||
- include files: CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH
|
||||
- library files: LIBRARY_PATH
|
||||
|
||||
If you are using a package manager (**vcpkg** / **conan** / ... ) you need/can to specify it, adding to cmake command:
|
||||
- `-DCMAKE_TOOLCHAIN_FILE=path/to/package_manager.cmake`
|
||||
|
||||
Examples:
|
||||
|
||||
- using **vcpkg** package manager it's necessary adding:
|
||||
- `-DCMAKE_TOOLCHAIN_FILE=<vcpkg_root_dir>/scripts/buildsystems/vcpkg.cmake`
|
||||
|
||||
- full cmake command using **vcpkg** package manager:
|
||||
- `cmake -G Ninja -DIMGUI_DAWN_DIR=path_to_sdk_dir -DCMAKE_TOOLCHAIN_FILE=<vcpkg_root_dir>/scripts/buildsystems/vcpkg.cmake -B where_to_build_dir`
|
||||
|
||||
---
|
||||
|
||||
|
||||
### Using makefile
|
||||
|
||||
- You need to install Emscripten from https://emscripten.org/docs/getting_started/downloads.html, and have the environment variables set, as described in https://emscripten.org/docs/getting_started/downloads.html#installation-instructions
|
||||
|
||||
|
|
@ -10,6 +151,8 @@
|
|||
|
||||
- Requires recent Emscripten as WGPU is still a work-in-progress API.
|
||||
|
||||
---
|
||||
|
||||
## How to Run
|
||||
|
||||
To run on a local machine:
|
||||
|
|
|
|||
|
|
@ -12,64 +12,47 @@
|
|||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_wgpu.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#include <emscripten/html5_webgpu.h>
|
||||
#include "../libs/emscripten/emscripten_mainloop_stub.h"
|
||||
#else
|
||||
#include <webgpu/webgpu_glfw.h>
|
||||
#endif
|
||||
#include "../libs/emscripten/emscripten_mainloop_stub.h"
|
||||
#endif
|
||||
|
||||
#include <webgpu/webgpu.h>
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
#include <webgpu/webgpu_cpp.h>
|
||||
#endif
|
||||
|
||||
// Data
|
||||
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 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 bool InitWGPU(GLFWwindow* window);
|
||||
static WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, GLFWwindow* 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*)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
static void ResizeSurface(int width, int height)
|
||||
{
|
||||
if (wgpu_swap_chain)
|
||||
wgpuSwapChainRelease(wgpu_swap_chain);
|
||||
wgpu_surface_width = width;
|
||||
wgpu_surface_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);
|
||||
wgpu_surface_configuration.width = wgpu_surface_width = width;
|
||||
wgpu_surface_configuration.height = wgpu_surface_height = height;
|
||||
wgpuSurfaceConfigure(wgpu_surface, &wgpu_surface_configuration);
|
||||
}
|
||||
|
||||
// Main code
|
||||
|
|
@ -98,7 +81,7 @@ int main(int, char**)
|
|||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
ResizeSurface(wgpu_surface_width, wgpu_surface_height);
|
||||
|
||||
glfwShowWindow(window);
|
||||
|
||||
// Setup Dear ImGui context
|
||||
|
|
@ -126,7 +109,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);
|
||||
|
||||
|
|
@ -182,6 +165,23 @@ int main(int, char**)
|
|||
if (width != wgpu_surface_width || height != wgpu_surface_height)
|
||||
ResizeSurface(width, height);
|
||||
|
||||
// Check surface status for error. If texture is not optimal, try to reconfigure the surface.
|
||||
WGPUSurfaceTexture surface_texture;
|
||||
wgpuSurfaceGetCurrentTexture(wgpu_surface, &surface_texture);
|
||||
if (ImGui_ImplWGPU_IsSurfaceStatusError(surface_texture.status))
|
||||
{
|
||||
fprintf(stderr, "Unrecoverable Surface Texture status=%#.8x\n", surface_texture.status);
|
||||
abort();
|
||||
}
|
||||
if (ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(surface_texture.status))
|
||||
{
|
||||
if (surface_texture.texture)
|
||||
wgpuTextureRelease(surface_texture.texture);
|
||||
if (width > 0 && height > 0)
|
||||
ResizeSurface(width, height);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplWGPU_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
|
|
@ -227,17 +227,21 @@ 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 view_desc = {};
|
||||
view_desc.format = wgpu_surface_configuration.format;
|
||||
view_desc.dimension = WGPUTextureViewDimension_2D ;
|
||||
view_desc.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED;
|
||||
view_desc.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED;
|
||||
view_desc.aspect = WGPUTextureAspect_All;
|
||||
|
||||
WGPUTextureView texture_view = wgpuTextureCreateView(surface_texture.texture, &view_desc);
|
||||
|
||||
WGPURenderPassColorAttachment color_attachments = {};
|
||||
color_attachments.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
|
||||
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 = texture_view;
|
||||
|
||||
WGPURenderPassDescriptor render_pass_desc = {};
|
||||
render_pass_desc.colorAttachmentCount = 1;
|
||||
|
|
@ -253,14 +257,16 @@ int main(int, char**)
|
|||
|
||||
WGPUCommandBufferDescriptor cmd_buffer_desc = {};
|
||||
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
|
||||
WGPUQueue wgpu_queue = wgpuDeviceGetQueue(wgpu_device);
|
||||
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(texture_view);
|
||||
wgpuRenderPassEncoderRelease(pass);
|
||||
wgpuCommandEncoderRelease(encoder);
|
||||
wgpuCommandBufferRelease(cmd_buffer);
|
||||
|
|
@ -274,75 +280,298 @@ 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;
|
||||
}
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
static WGPUAdapter RequestAdapter(WGPUInstance instance)
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
static WGPUAdapter RequestAdapter(wgpu::Instance& instance)
|
||||
{
|
||||
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, const char* message, void* pUserData)
|
||||
wgpu::Adapter acquired_adapter;
|
||||
wgpu::RequestAdapterOptions adapter_options;
|
||||
auto onRequestAdapter = [&](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter, wgpu::StringView message)
|
||||
{
|
||||
if (status == WGPURequestAdapterStatus_Success)
|
||||
*(WGPUAdapter*)pUserData = adapter;
|
||||
else
|
||||
printf("Could not get WebGPU adapter: %s\n", message);
|
||||
if (status != wgpu::RequestAdapterStatus::Success)
|
||||
{
|
||||
printf("Failed to get an adapter: %s\n", message.data);
|
||||
return;
|
||||
}
|
||||
acquired_adapter = std::move(adapter);
|
||||
};
|
||||
WGPUAdapter adapter;
|
||||
wgpuInstanceRequestAdapter(instance, nullptr, onAdapterRequestEnded, (void*)&adapter);
|
||||
return adapter;
|
||||
|
||||
// Synchronously (wait until) acquire Adapter
|
||||
wgpu::Future waitAdapterFunc { instance.RequestAdapter(&adapter_options, wgpu::CallbackMode::WaitAnyOnly, onRequestAdapter) };
|
||||
wgpu::WaitStatus waitStatusAdapter = instance.WaitAny(waitAdapterFunc, UINT64_MAX);
|
||||
IM_ASSERT(acquired_adapter != nullptr && waitStatusAdapter == wgpu::WaitStatus::Success && "Error on Adapter request");
|
||||
return acquired_adapter.MoveToCHandle();
|
||||
}
|
||||
|
||||
static WGPUDevice RequestDevice(WGPUAdapter adapter)
|
||||
static WGPUDevice RequestDevice(wgpu::Instance& instance, wgpu::Adapter& adapter)
|
||||
{
|
||||
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, const char* message, void* pUserData)
|
||||
// Set device callback functions
|
||||
wgpu::DeviceDescriptor device_desc;
|
||||
device_desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowSpontaneous,
|
||||
[](const wgpu::Device&, wgpu::DeviceLostReason type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetDeviceLostReasonName((WGPUDeviceLostReason)type), msg.data); }
|
||||
);
|
||||
device_desc.SetUncapturedErrorCallback(
|
||||
[](const wgpu::Device&, wgpu::ErrorType type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetErrorTypeName((WGPUErrorType)type), msg.data); }
|
||||
);
|
||||
|
||||
wgpu::Device acquired_device;
|
||||
auto onRequestDevice = [&](wgpu::RequestDeviceStatus status, wgpu::Device local_device, wgpu::StringView message)
|
||||
{
|
||||
if (status == WGPURequestDeviceStatus_Success)
|
||||
*(WGPUDevice*)pUserData = device;
|
||||
else
|
||||
printf("Could not get WebGPU device: %s\n", message);
|
||||
if (status != wgpu::RequestDeviceStatus::Success)
|
||||
{
|
||||
printf("Failed to get an device: %s\n", message.data);
|
||||
return;
|
||||
}
|
||||
acquired_device = std::move(local_device);
|
||||
};
|
||||
WGPUDevice device;
|
||||
wgpuAdapterRequestDevice(adapter, nullptr, onDeviceRequestEnded, (void*)&device);
|
||||
return device;
|
||||
|
||||
// Synchronously (wait until) get Device
|
||||
wgpu::Future waitDeviceFunc { adapter.RequestDevice(&device_desc, wgpu::CallbackMode::WaitAnyOnly, onRequestDevice) };
|
||||
wgpu::WaitStatus waitStatusDevice = instance.WaitAny(waitDeviceFunc, UINT64_MAX);
|
||||
IM_ASSERT(acquired_device != nullptr && waitStatusDevice == wgpu::WaitStatus::Success && "Error on Device request");
|
||||
return acquired_device.MoveToCHandle();
|
||||
}
|
||||
#endif
|
||||
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// 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 RequestAdapter(WGPUInstance& instance)
|
||||
{
|
||||
WGPURequestAdapterOptions adapter_options = {};
|
||||
|
||||
WGPUAdapter local_adapter;
|
||||
WGPURequestAdapterCallbackInfo adapterCallbackInfo = {};
|
||||
adapterCallbackInfo.callback = handle_request_adapter;
|
||||
adapterCallbackInfo.userdata1 = &local_adapter;
|
||||
|
||||
wgpuInstanceRequestAdapter(instance, &adapter_options, adapterCallbackInfo);
|
||||
IM_ASSERT(local_adapter && "Error on Adapter request");
|
||||
return local_adapter;
|
||||
}
|
||||
|
||||
static WGPUDevice RequestDevice(WGPUAdapter& adapter)
|
||||
{
|
||||
WGPUDevice local_device;
|
||||
WGPURequestDeviceCallbackInfo deviceCallbackInfo = {};
|
||||
deviceCallbackInfo.callback = handle_request_device;
|
||||
deviceCallbackInfo.userdata1 = &local_device;
|
||||
wgpuAdapterRequestDevice(adapter, nullptr, deviceCallbackInfo);
|
||||
IM_ASSERT(local_device && "Error on Device request");
|
||||
return local_device;
|
||||
}
|
||||
#endif // __EMSCRIPTEN__
|
||||
#endif // IMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
|
||||
static bool InitWGPU(GLFWwindow* window)
|
||||
{
|
||||
wgpu::Instance instance = wgpuCreateInstance(nullptr);
|
||||
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 instance_desc = {};
|
||||
static constexpr wgpu::InstanceFeatureName timedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
|
||||
instance_desc.requiredFeatureCount = 1;
|
||||
instance_desc.requiredFeatures = &timedWaitAny;
|
||||
wgpu::Instance instance = wgpu::CreateInstance(&instance_desc);
|
||||
|
||||
wgpu::Adapter adapter = RequestAdapter(instance);
|
||||
ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter.Get());
|
||||
|
||||
wgpu_device = RequestDevice(instance, adapter);
|
||||
|
||||
// Create the surface.
|
||||
#ifdef __EMSCRIPTEN__
|
||||
wgpu_device = emscripten_webgpu_get_device();
|
||||
if (!wgpu_device)
|
||||
return false;
|
||||
|
||||
wgpu::SurfaceDescriptorFromCanvasHTMLSelector canvas_desc = {};
|
||||
wgpu::EmscriptenSurfaceSourceCanvasHTMLSelector canvas_desc = {};
|
||||
canvas_desc.selector = "#canvas";
|
||||
|
||||
wgpu::SurfaceDescriptor surface_desc = {};
|
||||
surface_desc.nextInChain = &canvas_desc;
|
||||
wgpu::Surface surface = instance.CreateSurface(&surface_desc);
|
||||
|
||||
wgpu::Adapter adapter = {};
|
||||
wgpu_preferred_fmt = (WGPUTextureFormat)surface.GetPreferredFormat(adapter);
|
||||
wgpu_surface = instance.CreateSurface(&surface_desc).MoveToCHandle();
|
||||
#else
|
||||
WGPUAdapter adapter = RequestAdapter(instance.Get());
|
||||
if (!adapter)
|
||||
return false;
|
||||
wgpu_device = RequestDevice(adapter);
|
||||
|
||||
wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
|
||||
if (!surface)
|
||||
return false;
|
||||
wgpu_preferred_fmt = WGPUTextureFormat_BGRA8Unorm;
|
||||
wgpu_surface = CreateWGPUSurface(instance.Get(), window);
|
||||
#endif
|
||||
if (!wgpu_surface)
|
||||
return false;
|
||||
|
||||
// Moving Dawn objects into WGPU handles
|
||||
wgpu_instance = instance.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();
|
||||
IM_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(
|
||||
[](WGPULogLevel level, WGPUStringView msg, void* userdata) { fprintf(stderr, "%s: %.*s\n", ImGui_ImplWGPU_GetLogLevelName(level), (int)msg.length, msg.data); }, nullptr
|
||||
);
|
||||
wgpuSetLogLevel(WGPULogLevel_Warn);
|
||||
|
||||
WGPUAdapter adapter = RequestAdapter(wgpu_instance);
|
||||
ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter);
|
||||
|
||||
wgpu_device = RequestDevice(adapter);
|
||||
|
||||
// Create the surface.
|
||||
wgpu_surface = CreateWGPUSurface(wgpu_instance, 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;
|
||||
}
|
||||
|
||||
// GLFW helper to create a WebGPU surface, used only in WGPU-Native. DAWN-Native already has a built-in function
|
||||
// As of today (2025/10) there is no "official" support in GLFW to create a surface for WebGPU backend
|
||||
// This stub uses "low level" GLFW calls to acquire information from a specific Window Manager.
|
||||
// Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
|
||||
#if !defined(__EMSCRIPTEN__) && (defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN))
|
||||
|
||||
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
|
||||
#define GLFW_HAS_X11_OR_WAYLAND 1
|
||||
#else
|
||||
#define GLFW_HAS_X11_OR_WAYLAND 0
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
#undef APIENTRY
|
||||
#ifndef GLFW_EXPOSE_NATIVE_WIN32 // for glfwGetWin32Window()
|
||||
#define GLFW_EXPOSE_NATIVE_WIN32
|
||||
#endif
|
||||
#elif defined(__APPLE__)
|
||||
#ifndef GLFW_EXPOSE_NATIVE_COCOA // for glfwGetCocoaWindow()
|
||||
#define GLFW_EXPOSE_NATIVE_COCOA
|
||||
#endif
|
||||
#elif GLFW_HAS_X11_OR_WAYLAND
|
||||
#ifndef GLFW_EXPOSE_NATIVE_X11 // for glfwGetX11Display(), glfwGetX11Window() on Freedesktop (Linux, BSD, etc.)
|
||||
#define GLFW_EXPOSE_NATIVE_X11
|
||||
#endif
|
||||
#ifndef GLFW_EXPOSE_NATIVE_WAYLAND
|
||||
#if defined(__has_include) && __has_include(<wayland-client.h>)
|
||||
#define GLFW_EXPOSE_NATIVE_WAYLAND
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#include <GLFW/glfw3native.h>
|
||||
#undef Status // X11 headers are leaking this and also 'Success', 'Always', 'None', all used in DAWN api. Add #undef if necessary.
|
||||
|
||||
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, GLFWwindow* window)
|
||||
{
|
||||
ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
|
||||
create_info.Instance = instance;
|
||||
#if defined(GLFW_EXPOSE_NATIVE_COCOA)
|
||||
{
|
||||
create_info.System = "cocoa";
|
||||
create_info.RawWindow = (void*)glfwGetCocoaWindow(window);
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#elif defined(GLFW_EXPOSE_NATIVE_WAYLAND)
|
||||
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
|
||||
{
|
||||
create_info.System = "wayland";
|
||||
create_info.RawDisplay = (void*)glfwGetWaylandDisplay();
|
||||
create_info.RawSurface = (void*)glfwGetWaylandWindow(window);
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#elif defined(GLFW_EXPOSE_NATIVE_X11)
|
||||
if (glfwGetPlatform() == GLFW_PLATFORM_X11)
|
||||
{
|
||||
create_info.System = "x11";
|
||||
create_info.RawWindow = (void*)glfwGetX11Window(window);
|
||||
create_info.RawDisplay = (void*)glfwGetX11Display();
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#elif defined(GLFW_EXPOSE_NATIVE_WIN32)
|
||||
{
|
||||
create_info.System = "win32";
|
||||
create_info.RawWindow = (void*)glfwGetWin32Window(window);
|
||||
create_info.RawInstance = (void*)::GetModuleHandle(NULL);
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#else
|
||||
#error "Unsupported WebGPU native platform!"
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,84 +0,0 @@
|
|||
<!doctype html>
|
||||
<html lang="en-us">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
|
||||
<title>Dear ImGui Emscripten+GLFW+WebGPU example</title>
|
||||
<style>
|
||||
body { margin: 0; background-color: black }
|
||||
.emscripten {
|
||||
position: absolute;
|
||||
top: 0px;
|
||||
left: 0px;
|
||||
margin: 0px;
|
||||
border: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
display: block;
|
||||
image-rendering: optimizeSpeed;
|
||||
image-rendering: -moz-crisp-edges;
|
||||
image-rendering: -o-crisp-edges;
|
||||
image-rendering: -webkit-optimize-contrast;
|
||||
image-rendering: optimize-contrast;
|
||||
image-rendering: crisp-edges;
|
||||
image-rendering: pixelated;
|
||||
-ms-interpolation-mode: nearest-neighbor;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
|
||||
<script type='text/javascript'>
|
||||
var Module;
|
||||
(async () => {
|
||||
Module = {
|
||||
preRun: [],
|
||||
postRun: [],
|
||||
print: (function() {
|
||||
return function(text) {
|
||||
text = Array.prototype.slice.call(arguments).join(' ');
|
||||
console.log(text);
|
||||
};
|
||||
})(),
|
||||
printErr: function(text) {
|
||||
text = Array.prototype.slice.call(arguments).join(' ');
|
||||
console.error(text);
|
||||
},
|
||||
canvas: (function() {
|
||||
var canvas = document.getElementById('canvas');
|
||||
//canvas.addEventListener("webglcontextlost", function(e) { alert('FIXME: WebGL context lost, please reload the page'); e.preventDefault(); }, false);
|
||||
return canvas;
|
||||
})(),
|
||||
setStatus: function(text) {
|
||||
console.log("status: " + text);
|
||||
},
|
||||
monitorRunDependencies: function(left) {
|
||||
// no run dependencies to log
|
||||
}
|
||||
};
|
||||
window.onerror = function() {
|
||||
console.log("onerror: " + event);
|
||||
};
|
||||
|
||||
// Initialize the graphics adapter
|
||||
{
|
||||
if (!navigator.gpu) {
|
||||
throw Error("WebGPU not supported.");
|
||||
}
|
||||
|
||||
const adapter = await navigator.gpu.requestAdapter();
|
||||
const device = await adapter.requestDevice();
|
||||
Module.preinitializedWebGPUDevice = device;
|
||||
}
|
||||
|
||||
{
|
||||
const js = document.createElement('script');
|
||||
js.async = true;
|
||||
js.src = "index.js";
|
||||
document.body.appendChild(js);
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
205
examples/example_sdl2_wgpu/CMakeLists.txt
Normal file
205
examples/example_sdl2_wgpu/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
# Building for desktop (WebGPU-native) with Dawn:
|
||||
# 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_sdl2_wgpu[.exe]
|
||||
# * build/example_sdl2_wgpu[.exe]
|
||||
|
||||
# Building for desktop (WGPU-Native) with WGPU-Native:
|
||||
# 1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
|
||||
# 2. unzip the downloaded file in your_preferred_folder
|
||||
# 3. cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder ("full path" or "relative" starting from current directory)
|
||||
# 4. cmake --build build
|
||||
# The resulting binary will be found at one of the following locations:
|
||||
# * build/Debug/example_sdl2_wgpu[.exe]
|
||||
# * build/example_sdl2_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
|
||||
# (optional) -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py", see ReadMe.md
|
||||
# 3. cmake --build build
|
||||
# 4. emrun build/index.html
|
||||
|
||||
cmake_minimum_required(VERSION 3.22) # Dawn requires CMake >= 3.22
|
||||
project(imgui_example_sdl2_wgpu C CXX)
|
||||
|
||||
set(IMGUI_EXECUTABLE example_sdl2_wgpu)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17) # Dawn requires C++17
|
||||
|
||||
# Dear ImGui
|
||||
set(IMGUI_DIR ../../)
|
||||
|
||||
# ImGui example commons source files
|
||||
set(IMGUI_EXAMPLE_SOURCE_FILES
|
||||
main.cpp
|
||||
# backend files
|
||||
${IMGUI_DIR}/backends/imgui_impl_sdl2.cpp
|
||||
${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp
|
||||
# Dear ImGui files
|
||||
${IMGUI_DIR}/imgui.cpp
|
||||
${IMGUI_DIR}/imgui_draw.cpp
|
||||
${IMGUI_DIR}/imgui_demo.cpp
|
||||
${IMGUI_DIR}/imgui_tables.cpp
|
||||
${IMGUI_DIR}/imgui_widgets.cpp
|
||||
)
|
||||
|
||||
if(EMSCRIPTEN)
|
||||
if(NOT IMGUI_EMSCRIPTEN_WEBGPU_FLAG) # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG not used, set by current EMSCRIPTEN version
|
||||
if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "4.0.10")
|
||||
set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "--use-port=emdawnwebgpu" CACHE STRING "Choose between --use-port=emdawnwebgpu (Dawn implementation of EMSCRIPTEN) and -sUSE_WEBGPU=1 (WGPU implementation of EMSCRIPTEN, deprecated in 4.0.10): default to --use-port=emdawnwebgpu for EMSCRIPTEN >= 4.0.10")
|
||||
else()
|
||||
set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "-sUSE_WEBGPU=1" CACHE STRING "Use -sUSE_WEBGPU=1 for EMSCRIPTEN WGPU implementation")
|
||||
endif()
|
||||
else() # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG used, check correct version
|
||||
if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.10" AND "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
|
||||
# it's necessary EMSCRIPTEN >= v4.0.10 (although "--use-port=path/to/emdawnwebgpu.port.py" is supported/tested from v4.0.8)
|
||||
message(FATAL_ERROR "emdawnwebgpu needs EMSCRIPTEN version >= 4.0.10")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
|
||||
else() # Native/Desktop build
|
||||
if(NOT IMGUI_DAWN_DIR AND NOT IMGUI_WGPU_DIR) # if it's Native/Desktop build, IMGUI_DAWN_DIR or IMGUI_WGPU_DIR must be specified
|
||||
message(FATAL_ERROR "Please specify the Dawn or WGPU base directory")
|
||||
endif()
|
||||
|
||||
if(IMGUI_DAWN_DIR AND IMGUI_WGPU_DIR) # both IMGUI_DAWN_DIR and IMGUI_WGPU_DIR cannot be set
|
||||
message(FATAL_ERROR "Please specify only one between Dawn / WGPU base directory")
|
||||
endif()
|
||||
|
||||
if(APPLE) # Add SDL2 module to get Surface, with libs and file property for MacOS build
|
||||
set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
|
||||
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal -framework MetalKit -framework Cocoa")
|
||||
endif()
|
||||
|
||||
find_package(SDL2 REQUIRED) # SDL_MAIN_HANDLED
|
||||
|
||||
if(IMGUI_DAWN_DIR) # DAWN-Native build options
|
||||
list(APPEND CMAKE_PREFIX_PATH ${IMGUI_DAWN_DIR})
|
||||
find_package(Threads) # required from Dawn installation
|
||||
find_package(Dawn) # Search for a Dawn installation using IMGUI_DAWN_DIR in CMAKE_PREFIX_PATH
|
||||
if(Dawn_FOUND)
|
||||
message("Dawn Installation has been found!")
|
||||
set(LIBRARIES dawn::webgpu_dawn ${OS_LIBRARIES})
|
||||
else()
|
||||
set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
|
||||
|
||||
option(DAWN_USE_GLFW OFF) # disable buildin GLFW in DAWN when we use SDL2 / SDL3
|
||||
|
||||
option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON)
|
||||
set(DAWN_BUILD_MONOLITHIC_LIBRARY "STATIC" CACHE STRING "Build monolithic library: SHARED, STATIC, or OFF.")
|
||||
|
||||
# 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)
|
||||
option(TINT_BUILD_DOCS "Build documentation" OFF)
|
||||
option(TINT_BUILD_TESTS "Build tests" OFF)
|
||||
if(NOT APPLE)
|
||||
option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
|
||||
endif()
|
||||
if(WIN32)
|
||||
option(DAWN_FORCE_SYSTEM_COMPONENT_LOAD "Allow system component fallback" ON)
|
||||
option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
|
||||
option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
|
||||
option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
|
||||
option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
|
||||
option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
|
||||
option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
|
||||
endif()
|
||||
# check if WAYLAND is the current Session Type and enable DAWN_USE_WAYLAND Wayland option @compile time
|
||||
# You can override this using: cmake -DDAWN_USE_WAYLAND=X (X = ON | OFF)
|
||||
if(LINUX)
|
||||
if($ENV{XDG_SESSION_TYPE} MATCHES wayland)
|
||||
option(DAWN_USE_WAYLAND "Enable support for Wayland surface" ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL)
|
||||
|
||||
set(LIBRARIES webgpu_dawn ${OS_LIBRARIES})
|
||||
endif()
|
||||
else() # WGPU-Native build options
|
||||
|
||||
set(WGPU_NATIVE_LIB_DIR ${IMGUI_WGPU_DIR}/lib)
|
||||
find_library(WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native HINTS ${WGPU_NATIVE_LIB_DIR} REQUIRED)
|
||||
if(WIN32)
|
||||
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32 Propsys RuntimeObject)
|
||||
elseif(UNIX AND NOT APPLE)
|
||||
set(OS_LIBRARIES "-lm -ldl")
|
||||
endif()
|
||||
|
||||
set(LIBRARIES ${WGPU_LIBRARY} ${OS_LIBRARIES})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(${IMGUI_EXECUTABLE} ${IMGUI_EXAMPLE_SOURCE_FILES})
|
||||
|
||||
target_include_directories(${IMGUI_EXECUTABLE} PUBLIC
|
||||
${IMGUI_DIR}
|
||||
${IMGUI_DIR}/backends
|
||||
${SDL2_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_EXAMPLE_SDL2_WGPU")
|
||||
|
||||
# compiler option only for IMGUI_EXAMPLE_SOURCE_FILES
|
||||
if (MSVC)
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /W4) # warning level 4
|
||||
else()
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC -Wall) # -Wextra -Wpedantic
|
||||
endif()
|
||||
|
||||
# IMGUI_IMPL_WEBGPU_BACKEND_DAWN/WGPU internal define is set according to:
|
||||
# EMSCRIPTEN: by used FLAG
|
||||
# --use-port=emdawnwebgpu --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled (+EMSCRIPTEN)
|
||||
# -sUSE_WEBGPU=1 --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled (+EMSCRIPTEN)
|
||||
# NATIVE: by used SDK installation directory
|
||||
# if IMGUI_DAWN_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled
|
||||
# if IMGUI_WGPU_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled
|
||||
|
||||
if(NOT EMSCRIPTEN) # WegGPU-Native settings
|
||||
if(IMGUI_DAWN_DIR)
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
|
||||
if(NOT Dawn_FOUND)
|
||||
target_link_libraries(${IMGUI_EXECUTABLE} INTERFACE webgpu_cpp)
|
||||
endif()
|
||||
else()
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
|
||||
target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGPU_DIR}/include)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${IMGUI_EXECUTABLE} PUBLIC ${LIBRARIES} ${SDL2_LIBRARIES})
|
||||
else() # Emscripten settings
|
||||
set(CMAKE_EXECUTABLE_SUFFIX ".html")
|
||||
|
||||
if("${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}")
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
|
||||
else()
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
|
||||
endif()
|
||||
message(STATUS "Using ${IMGUI_EMSCRIPTEN_WEBGPU_FLAG} WebGPU implementation")
|
||||
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "-sUSE_SDL=2")
|
||||
target_link_options(${IMGUI_EXECUTABLE} PRIVATE
|
||||
"${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}"
|
||||
"-sUSE_SDL=2"
|
||||
"-sWASM=1"
|
||||
"-sASYNCIFY=1"
|
||||
"-sALLOW_MEMORY_GROWTH=1"
|
||||
"-sNO_EXIT_RUNTIME=0"
|
||||
"-sASSERTIONS=1"
|
||||
"-sDISABLE_EXCEPTION_CATCHING=1"
|
||||
"-sNO_FILESYSTEM=1"
|
||||
"--shell-file=${CMAKE_CURRENT_LIST_DIR}/../libs/emscripten/shell_minimal.html"
|
||||
)
|
||||
set_target_properties(${IMGUI_EXECUTABLE} PROPERTIES OUTPUT_NAME "index")
|
||||
endif()
|
||||
103
examples/example_sdl2_wgpu/Makefile.emscripten
Normal file
103
examples/example_sdl2_wgpu/Makefile.emscripten
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#
|
||||
# Makefile to use with emscripten
|
||||
# See https://emscripten.org/docs/getting_started/downloads.html
|
||||
# for installation instructions.
|
||||
#
|
||||
# This Makefile assumes you have loaded emscripten's environment.
|
||||
# (On Windows, you may need to execute emsdk_env.bat or encmdprompt.bat ahead)
|
||||
#
|
||||
# Running `make -f Makefile.emscripten` will produce three files:
|
||||
# - web/index.html
|
||||
# - web/index.js
|
||||
# - web/index.wasm
|
||||
#
|
||||
# All three are needed to run the demo.
|
||||
|
||||
CC = emcc
|
||||
CXX = em++
|
||||
WEB_DIR = web
|
||||
EXE = $(WEB_DIR)/index.html
|
||||
IMGUI_DIR = ../..
|
||||
SOURCES = main.cpp
|
||||
SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
|
||||
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl2.cpp $(IMGUI_DIR)/backends/imgui_impl_wgpu.cpp
|
||||
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
|
||||
UNAME_S := $(shell uname -s)
|
||||
CPPFLAGS =
|
||||
LDFLAGS =
|
||||
EMS =
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
## EMSCRIPTEN OPTIONS
|
||||
##---------------------------------------------------------------------
|
||||
|
||||
# ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
|
||||
EMS += -s USE_SDL=2
|
||||
EMS += -s DISABLE_EXCEPTION_CATCHING=1
|
||||
LDFLAGS += -s WASM=1
|
||||
LDFLAGS += -s ALLOW_MEMORY_GROWTH=1
|
||||
LDFLAGS += -s ASYNCIFY=1
|
||||
LDFLAGS += -s NO_EXIT_RUNTIME=0
|
||||
LDFLAGS += -s ASSERTIONS=1
|
||||
|
||||
# (1) Using legacy WebGPU implementation (Emscripten < 4.0.10)
|
||||
#EMS += -DIMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
#LDFLAGS += -s USE_WEBGPU=1
|
||||
|
||||
# or (2) Using newer Dawn-based WebGPU port (Emscripten >= 4.0.10)
|
||||
EMS += --use-port=emdawnwebgpu
|
||||
LDFLAGS += --use-port=emdawnwebgpu
|
||||
|
||||
# Build as single file (binary text encoded in .html file)
|
||||
#LDFLAGS += -sSINGLE_FILE
|
||||
|
||||
# Emscripten allows preloading a file or folder to be accessible at runtime.
|
||||
# The Makefile for this example project suggests embedding the misc/fonts/ folder into our application, it will then be accessible as "/fonts"
|
||||
# See documentation for more details: https://emscripten.org/docs/porting/files/packaging_files.html
|
||||
# (Default value is 0. Set to 1 to enable file-system and include the misc/fonts/ folder as part of the build.)
|
||||
USE_FILE_SYSTEM ?= 0
|
||||
ifeq ($(USE_FILE_SYSTEM), 0)
|
||||
LDFLAGS += -s NO_FILESYSTEM=1
|
||||
CPPFLAGS += -DIMGUI_DISABLE_FILE_FUNCTIONS
|
||||
endif
|
||||
ifeq ($(USE_FILE_SYSTEM), 1)
|
||||
LDFLAGS += --no-heap-copy --preload-file ../../misc/fonts@/fonts
|
||||
endif
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
## FINAL BUILD FLAGS
|
||||
##---------------------------------------------------------------------
|
||||
|
||||
CPPFLAGS += -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
|
||||
#CPPFLAGS += -g
|
||||
CPPFLAGS += -Wall -Wformat -Os $(EMS)
|
||||
LDFLAGS += --shell-file ../libs/emscripten/shell_minimal.html
|
||||
LDFLAGS += $(EMS)
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
## BUILD RULES
|
||||
##---------------------------------------------------------------------
|
||||
|
||||
%.o:%.cpp
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
%.o:$(IMGUI_DIR)/%.cpp
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
%.o:$(IMGUI_DIR)/backends/%.cpp
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
all: $(EXE)
|
||||
@echo Build complete for $(EXE)
|
||||
|
||||
$(WEB_DIR):
|
||||
mkdir $@
|
||||
|
||||
serve: all
|
||||
python3 -m http.server -d $(WEB_DIR)
|
||||
|
||||
$(EXE): $(OBJS) $(WEB_DIR)
|
||||
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(EXE) $(OBJS) $(WEB_DIR)/*.js $(WEB_DIR)/*.wasm $(WEB_DIR)/*.wasm.pre
|
||||
167
examples/example_sdl2_wgpu/README.md
Normal file
167
examples/example_sdl2_wgpu/README.md
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
## How to Build
|
||||
|
||||
|
||||
---
|
||||
|
||||
### Using CMake
|
||||
#### Building for desktop (WebGPU-native) with Google Dawn:
|
||||
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_sdl2_wgpu[.exe]
|
||||
* build/example_sdl2_wgpu[.exe]
|
||||
|
||||
#### Building for desktop (WebGPU-Native) with WGPU:
|
||||
1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
|
||||
2. unzip the downloaded file in `your_preferred_folder`
|
||||
3. `cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder` ("full path" or "relative" starting from current directory)
|
||||
4. `cmake --build build`
|
||||
The resulting binary will be found at one of the following locations:
|
||||
* build/Debug/example_sdl2_wgpu[.exe]
|
||||
* build/example_sdl2_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`
|
||||
- (optional) `-DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py"`, see below
|
||||
4. `cmake --build build`
|
||||
|
||||
#### Sync Emscripten with latest Google Dawn:
|
||||
If you want to sync Emscripten with latest DAWN release it's necessary to download the `port-emdawnwgpu-package` (released daily by Google) here:
|
||||
https://github.com/google/dawn/releases
|
||||
Unpack it in your preferred folder and to replace the step 3 with:
|
||||
|
||||
3. `emcmake cmake -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py" -G Ninja -B build`
|
||||
|
||||
**N.B.**
|
||||
For the WASM code produced by Emscripten to work correctly, it will also be necessary to have the "corresponding" (or newer) version of Google Canary (nightly build for developers) that includes the latest changes
|
||||
|
||||
|
||||
---
|
||||
|
||||
### CMake by step
|
||||
|
||||
#### Generate Dawn Native:
|
||||
|
||||
- `cmake -G Ninja -DIMGUI_DAWN_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
- Using `IMGUI_DAWN_DIR` set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
|
||||
#### Generate WGPU Native:
|
||||
|
||||
- `cmake -G Ninja -DIMGUI_WGPU_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
|
||||
- Using `IMGUI_WGPU_DIR` set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten:
|
||||
|
||||
- `emcmake cmake -G Ninja -B where_to_build_dir`\
|
||||
CMake checks the EMSCRIPEN version then:
|
||||
- if EMS >= 4.0.10 uses `--use-port=emdawnwebgpu` flag to build
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
- if EMS < 4.0.10 uses `-sUSE_WEBGPU=1` flag to build
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten forcing `-sUSE_WEBGPU=1` deprecated flag even with EMS >= 4.0.10
|
||||
- `emcmake cmake -G Ninja -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="-sUSE_WEBGPU=1" -B where_to_build_dir`
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten using external WebGPU library (emdawnwebgpu_pkg)
|
||||
- `emcmake cmake -G Ninja -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path_to_emdawnwebgpu_pkg" -B where_to_build_dir`
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
- *To use external WebGPU library it's necessary to have EMS >= 4.0.10 or the minimum requirements specified by the package:*
|
||||
- https://github.com/google/dawn/releases
|
||||
|
||||
#### Build time
|
||||
|
||||
Once the procedure for the specific builder is generated, the build command is **always the same**:
|
||||
|
||||
- Build using CMake
|
||||
- `cmake --build where_to_build_dir`
|
||||
- It will use selected builder to build the example.
|
||||
|
||||
- Build explicitly:
|
||||
- `cd where_to_build_dir`
|
||||
- `ninja`
|
||||
- This is the builder chosen during the generation phase
|
||||
|
||||
---
|
||||
|
||||
### CMake useful options
|
||||
#### Generator types (alternative to **ninja** bulder):
|
||||
- `-G Ninja` to build with __ninja__ builder
|
||||
- `-G "Unix Makefiles"` to build with __make__ builder
|
||||
- `-G "Visual Studio 17 2022" -A x64` to create a VS 2022 solution (.sln) file, Windows only
|
||||
- **Native build only**
|
||||
- Not **officially** supported to build Google Dawn
|
||||
|
||||
Example:
|
||||
- using **make** instead **ninja**:
|
||||
- `cmake -G "Unix Makefiles" -DIMGUI_DAWN_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
|
||||
**Syntax is case sensitive and the "" are necessary in case of spaces between words*
|
||||
|
||||
#### Directories
|
||||
- The directory path can be absolute or relative (starting from the current directory)
|
||||
- It's necessary to use different `where_to_build_dir` for different CMake generations
|
||||
|
||||
|
||||
#### Build type
|
||||
The default build type is **Debug**
|
||||
It is possible to use a different build type using:
|
||||
- `-DCMAKE_BUILD_TYPE=Release`
|
||||
- `-DCMAKE_BUILD_TYPE=MinSizeRel`
|
||||
- `-DCMAKE_BUILD_TYPE=RelWithDebInfo`
|
||||
|
||||
Example:
|
||||
- building **Release**:
|
||||
- `cmake -G ninja -DIMGUI_WGPU_DIR=path_to_sdk_dir -DCMAKE_BUILD_TYPE=Release -B where_to_build_dir `
|
||||
|
||||
#### GLFW / SDL2 / SDL3 includes, libraries, search paths and package manager
|
||||
|
||||
Includes and libraries, by default, are searched in system/compiler paths (environment variables): you can add the path to your development tools to the environment variables without having to modify the `CMakeLists.txt` file.
|
||||
- e.g. CLang search in path specified from the following environment variables:
|
||||
- include files: CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH
|
||||
- library files: LIBRARY_PATH
|
||||
|
||||
If you are using a package manager (**vcpkg** / **conan** / ... ) you need/can to specify it, adding to cmake command:
|
||||
- `-DCMAKE_TOOLCHAIN_FILE=path/to/package_manager.cmake`
|
||||
|
||||
Examples:
|
||||
|
||||
- using **vcpkg** package manager it's necessary adding:
|
||||
- `-DCMAKE_TOOLCHAIN_FILE=<vcpkg_root_dir>/scripts/buildsystems/vcpkg.cmake`
|
||||
|
||||
- full cmake command using **vcpkg** package manager:
|
||||
- `cmake -G Ninja -DIMGUI_DAWN_DIR=path_to_sdk_dir -DCMAKE_TOOLCHAIN_FILE=<vcpkg_root_dir>/scripts/buildsystems/vcpkg.cmake -B where_to_build_dir`
|
||||
|
||||
---
|
||||
|
||||
|
||||
### Using makefile
|
||||
|
||||
- You need to install Emscripten from https://emscripten.org/docs/getting_started/downloads.html, and have the environment variables set, as described in https://emscripten.org/docs/getting_started/downloads.html#installation-instructions
|
||||
|
||||
- Depending on your configuration, in Windows you may need to run `emsdk/emsdk_env.bat` in your console to access the Emscripten command-line tools.
|
||||
|
||||
- You may also refer to our [Continuous Integration setup](https://github.com/ocornut/imgui/tree/master/.github/workflows) for Emscripten setup.
|
||||
|
||||
- Then build using `make -f Makefile.emscripten` while in the `example_glfw_wgpu/` directory.
|
||||
|
||||
- Requires recent Emscripten as WGPU is still a work-in-progress API.
|
||||
|
||||
---
|
||||
|
||||
## How to Run
|
||||
|
||||
To run on a local machine:
|
||||
- Make sure your browse supports WGPU and it is enabled. WGPU is still WIP not enabled by default in most browser.
|
||||
- `make serve` will use Python3 to spawn a local webserver, you can then browse http://localhost:8000 to access your build.
|
||||
- Otherwise, generally you will need a local webserver:
|
||||
- Quoting [https://emscripten.org/docs/getting_started](https://emscripten.org/docs/getting_started/Tutorial.html#generating-html):<br>
|
||||
_"Unfortunately several browsers (including Chrome, Safari, and Internet Explorer) do not support file:// [XHR](https://emscripten.org/docs/site/glossary.html#term-xhr) requests, and can’t load extra files needed by the HTML (like a .wasm file, or packaged file data as mentioned lower down). For these browsers you’ll need to serve the files using a [local webserver](https://emscripten.org/docs/getting_started/FAQ.html#faq-local-webserver) and then open http://localhost:8000/hello.html."_
|
||||
- Emscripten SDK has a handy `emrun` command: `emrun web/example_glfw_wgpu.html --browser firefox` which will spawn a temporary local webserver (in Firefox). See https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html for details.
|
||||
- You may use Python 3 builtin webserver: `python -m http.server -d web` (this is what `make serve` uses).
|
||||
- You may use Python 2 builtin webserver: `cd web && python -m SimpleHTTPServer`.
|
||||
- If you are accessing the files over a network, certain browsers, such as Firefox, will restrict Gamepad API access to secure contexts only (e.g. https only).
|
||||
541
examples/example_sdl2_wgpu/main.cpp
Normal file
541
examples/example_sdl2_wgpu/main.cpp
Normal file
|
|
@ -0,0 +1,541 @@
|
|||
// Dear ImGui: standalone example application for using SDL2 + 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:
|
||||
// - FAQ https://dearimgui.com/faq
|
||||
// - Getting Started https://dearimgui.com/getting-started
|
||||
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||
// - Introduction, links and more at the top of imgui.cpp
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl2.h"
|
||||
#include "imgui_impl_wgpu.h"
|
||||
#include <stdio.h>
|
||||
#include <SDL.h>
|
||||
|
||||
// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#include <emscripten/html5_webgpu.h>
|
||||
#endif
|
||||
#include "../libs/emscripten/emscripten_mainloop_stub.h"
|
||||
#endif
|
||||
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
#include <webgpu/webgpu_cpp.h>
|
||||
#endif
|
||||
|
||||
// Data
|
||||
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(SDL_Window* window);
|
||||
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window);
|
||||
|
||||
static void ResizeSurface(int width, int height)
|
||||
{
|
||||
wgpu_surface_configuration.width = wgpu_surface_width = width;
|
||||
wgpu_surface_configuration.height = wgpu_surface_height = height;
|
||||
wgpuSurfaceConfigure(wgpu_surface, (WGPUSurfaceConfiguration*)&wgpu_surface_configuration);
|
||||
}
|
||||
|
||||
// Main code
|
||||
int main(int, char**)
|
||||
{
|
||||
// Setup SDL
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
|
||||
|
||||
// Create window with graphics context
|
||||
float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
|
||||
SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE;
|
||||
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+WebGPU example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, wgpu_surface_width, wgpu_surface_height, window_flags);
|
||||
if (window == nullptr)
|
||||
{
|
||||
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize WGPU
|
||||
InitWGPU(window);
|
||||
|
||||
// Setup Dear ImGui context
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
//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
|
||||
ImGui_ImplSDL2_InitForOther(window);
|
||||
ImGui_ImplWGPU_InitInfo init_info;
|
||||
init_info.Device = wgpu_device;
|
||||
init_info.NumFramesInFlight = 3;
|
||||
init_info.RenderTargetFormat = wgpu_surface_configuration.format;
|
||||
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.
|
||||
// - 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).
|
||||
// - 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 \\ !
|
||||
// - 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;
|
||||
//io.Fonts->AddFontDefault();
|
||||
#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");
|
||||
//ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf");
|
||||
//IM_ASSERT(font != nullptr);
|
||||
#endif
|
||||
|
||||
// Our state
|
||||
bool show_demo_window = true;
|
||||
bool show_another_window = false;
|
||||
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
|
||||
// Main loop
|
||||
bool done = false;
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
|
||||
// You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
|
||||
io.IniFilename = nullptr;
|
||||
EMSCRIPTEN_MAINLOOP_BEGIN
|
||||
#else
|
||||
while (!done)
|
||||
#endif
|
||||
{
|
||||
// Poll and handle events (inputs, window resize, etc.)
|
||||
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||||
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||||
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||
if (event.type == SDL_QUIT)
|
||||
done = true;
|
||||
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
|
||||
done = true;
|
||||
}
|
||||
|
||||
// React to changes in screen size
|
||||
int width, height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
if (width != wgpu_surface_width || height != wgpu_surface_height)
|
||||
ResizeSurface(width, height);
|
||||
|
||||
// Check surface status for error. If texture is not optimal, try to reconfigure the surface.
|
||||
WGPUSurfaceTexture surface_texture;
|
||||
wgpuSurfaceGetCurrentTexture(wgpu_surface, &surface_texture);
|
||||
if (ImGui_ImplWGPU_IsSurfaceStatusError(surface_texture.status))
|
||||
{
|
||||
fprintf(stderr, "Unrecoverable Surface Texture status=%#.8x\n", surface_texture.status);
|
||||
abort();
|
||||
}
|
||||
if (ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(surface_texture.status))
|
||||
{
|
||||
if (surface_texture.texture)
|
||||
wgpuTextureRelease(surface_texture.texture);
|
||||
if (width > 0 && height > 0)
|
||||
ResizeSurface(width, height);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplWGPU_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
|
||||
if (show_demo_window)
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
|
||||
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
|
||||
{
|
||||
static float f = 0.0f;
|
||||
static int counter = 0;
|
||||
|
||||
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
|
||||
|
||||
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
|
||||
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
|
||||
ImGui::Checkbox("Another Window", &show_another_window);
|
||||
|
||||
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
||||
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
||||
|
||||
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
|
||||
counter++;
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("counter = %d", counter);
|
||||
|
||||
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
// 3. Show another simple window.
|
||||
if (show_another_window)
|
||||
{
|
||||
ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
|
||||
ImGui::Text("Hello from another window!");
|
||||
if (ImGui::Button("Close Me"))
|
||||
show_another_window = false;
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
// Rendering
|
||||
ImGui::Render();
|
||||
|
||||
WGPUTextureViewDescriptor view_desc = {};
|
||||
view_desc.format = wgpu_surface_configuration.format;
|
||||
view_desc.dimension = WGPUTextureViewDimension_2D;
|
||||
view_desc.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED;
|
||||
view_desc.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED;
|
||||
view_desc.aspect = WGPUTextureAspect_All;
|
||||
|
||||
WGPUTextureView texture_view = wgpuTextureCreateView(surface_texture.texture, &view_desc);
|
||||
|
||||
WGPURenderPassColorAttachment color_attachments = {};
|
||||
color_attachments.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
|
||||
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 = texture_view;
|
||||
|
||||
WGPURenderPassDescriptor render_pass_desc = {};
|
||||
render_pass_desc.colorAttachmentCount = 1;
|
||||
render_pass_desc.colorAttachments = &color_attachments;
|
||||
render_pass_desc.depthStencilAttachment = nullptr;
|
||||
|
||||
WGPUCommandEncoderDescriptor enc_desc = {};
|
||||
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(wgpu_device, &enc_desc);
|
||||
|
||||
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &render_pass_desc);
|
||||
ImGui_ImplWGPU_RenderDrawData(ImGui::GetDrawData(), pass);
|
||||
wgpuRenderPassEncoderEnd(pass);
|
||||
|
||||
WGPUCommandBufferDescriptor cmd_buffer_desc = {};
|
||||
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
|
||||
wgpuQueueSubmit(wgpu_queue, 1, &cmd_buffer);
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
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
|
||||
#endif
|
||||
wgpuTextureViewRelease(texture_view);
|
||||
wgpuRenderPassEncoderRelease(pass);
|
||||
wgpuCommandEncoderRelease(encoder);
|
||||
wgpuCommandBufferRelease(cmd_buffer);
|
||||
}
|
||||
#ifdef __EMSCRIPTEN__
|
||||
EMSCRIPTEN_MAINLOOP_END;
|
||||
#endif
|
||||
|
||||
// Cleanup
|
||||
ImGui_ImplWGPU_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
wgpuSurfaceUnconfigure(wgpu_surface);
|
||||
wgpuSurfaceRelease(wgpu_surface);
|
||||
wgpuQueueRelease(wgpu_queue);
|
||||
wgpuDeviceRelease(wgpu_device);
|
||||
wgpuInstanceRelease(wgpu_instance);
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
static WGPUAdapter RequestAdapter(wgpu::Instance& instance)
|
||||
{
|
||||
wgpu::Adapter acquired_adapter;
|
||||
wgpu::RequestAdapterOptions adapter_options;
|
||||
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;
|
||||
}
|
||||
acquired_adapter = std::move(adapter);
|
||||
};
|
||||
|
||||
// Synchronously (wait until) acquire Adapter
|
||||
wgpu::Future waitAdapterFunc { instance.RequestAdapter(&adapter_options, wgpu::CallbackMode::WaitAnyOnly, onRequestAdapter) };
|
||||
wgpu::WaitStatus waitStatusAdapter = instance.WaitAny(waitAdapterFunc, UINT64_MAX);
|
||||
IM_ASSERT(acquired_adapter != nullptr && waitStatusAdapter == wgpu::WaitStatus::Success && "Error on Adapter request");
|
||||
return acquired_adapter.MoveToCHandle();
|
||||
}
|
||||
|
||||
static WGPUDevice RequestDevice(wgpu::Instance& instance, wgpu::Adapter& adapter)
|
||||
{
|
||||
// Set device callback functions
|
||||
wgpu::DeviceDescriptor device_desc;
|
||||
device_desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowSpontaneous,
|
||||
[](const wgpu::Device&, wgpu::DeviceLostReason type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetDeviceLostReasonName((WGPUDeviceLostReason)type), msg.data); }
|
||||
);
|
||||
device_desc.SetUncapturedErrorCallback(
|
||||
[](const wgpu::Device&, wgpu::ErrorType type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetErrorTypeName((WGPUErrorType)type), msg.data); }
|
||||
);
|
||||
|
||||
wgpu::Device acquired_device;
|
||||
auto onRequestDevice = [&](wgpu::RequestDeviceStatus status, wgpu::Device local_device, wgpu::StringView message)
|
||||
{
|
||||
if (status != wgpu::RequestDeviceStatus::Success)
|
||||
{
|
||||
printf("Failed to get an device: %s\n", message.data);
|
||||
return;
|
||||
}
|
||||
acquired_device = std::move(local_device);
|
||||
};
|
||||
|
||||
// Synchronously (wait until) get Device
|
||||
wgpu::Future waitDeviceFunc { adapter.RequestDevice(&device_desc, wgpu::CallbackMode::WaitAnyOnly, onRequestDevice) };
|
||||
wgpu::WaitStatus waitStatusDevice = instance.WaitAny(waitDeviceFunc, UINT64_MAX);
|
||||
IM_ASSERT(acquired_device != nullptr && waitStatusDevice == wgpu::WaitStatus::Success && "Error on Device request");
|
||||
return acquired_device.MoveToCHandle();
|
||||
}
|
||||
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// 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 RequestAdapter(WGPUInstance& instance)
|
||||
{
|
||||
WGPURequestAdapterOptions adapter_options = {};
|
||||
|
||||
WGPUAdapter local_adapter;
|
||||
WGPURequestAdapterCallbackInfo adapterCallbackInfo = {};
|
||||
adapterCallbackInfo.callback = handle_request_adapter;
|
||||
adapterCallbackInfo.userdata1 = &local_adapter;
|
||||
|
||||
wgpuInstanceRequestAdapter(instance, &adapter_options, adapterCallbackInfo);
|
||||
IM_ASSERT(local_adapter && "Error on Adapter request");
|
||||
return local_adapter;
|
||||
}
|
||||
|
||||
static WGPUDevice RequestDevice(WGPUAdapter& adapter)
|
||||
{
|
||||
WGPUDevice local_device;
|
||||
WGPURequestDeviceCallbackInfo deviceCallbackInfo = {};
|
||||
deviceCallbackInfo.callback = handle_request_device;
|
||||
deviceCallbackInfo.userdata1 = &local_device;
|
||||
wgpuAdapterRequestDevice(adapter, nullptr, deviceCallbackInfo);
|
||||
IM_ASSERT(local_device && "Error on Device request");
|
||||
return local_device;
|
||||
}
|
||||
#endif // __EMSCRIPTEN__
|
||||
#endif // IMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
|
||||
static bool InitWGPU(SDL_Window* 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 instance_desc = {};
|
||||
static constexpr wgpu::InstanceFeatureName timedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
|
||||
instance_desc.requiredFeatureCount = 1;
|
||||
instance_desc.requiredFeatures = &timedWaitAny;
|
||||
wgpu::Instance instance = wgpu::CreateInstance(&instance_desc);
|
||||
|
||||
wgpu::Adapter adapter = RequestAdapter(instance);
|
||||
ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter.Get());
|
||||
|
||||
wgpu_device = RequestDevice(instance, adapter);
|
||||
|
||||
// Create the surface.
|
||||
#ifdef __EMSCRIPTEN__
|
||||
wgpu::EmscriptenSurfaceSourceCanvasHTMLSelector canvas_desc = {};
|
||||
canvas_desc.selector = "#canvas";
|
||||
|
||||
wgpu::SurfaceDescriptor surface_desc = {};
|
||||
surface_desc.nextInChain = &canvas_desc;
|
||||
wgpu::Surface surface = instance.CreateSurface(&surface_desc);
|
||||
#else
|
||||
wgpu::Surface surface = CreateWGPUSurface(instance.Get(), window);
|
||||
#endif
|
||||
if (!surface)
|
||||
return false;
|
||||
|
||||
// Moving Dawn objects into WGPU handles
|
||||
wgpu_instance = instance.MoveToCHandle();
|
||||
wgpu_surface = surface.MoveToCHandle();
|
||||
|
||||
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(
|
||||
[](WGPULogLevel level, WGPUStringView msg, void* userdata) { fprintf(stderr, "%s: %.*s\n", ImGui_ImplWGPU_GetLogLevelName(level), (int)msg.length, msg.data); }, nullptr
|
||||
);
|
||||
wgpuSetLogLevel(WGPULogLevel_Warn);
|
||||
|
||||
WGPUAdapter adapter = RequestAdapter(wgpu_instance);
|
||||
ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter);
|
||||
|
||||
wgpu_device = RequestDevice(adapter);
|
||||
|
||||
// Create the surface.
|
||||
wgpu_surface = CreateWGPUSurface(wgpu_instance, 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;
|
||||
}
|
||||
|
||||
// SDL2 helper to create a WebGPU surface (exclusively!) for Native/Desktop applications: available only together with WebGPU/WGPU backend
|
||||
// As of today (2025/10/31) there is no "official" support in SDL2 to create a surface for WebGPU backend.
|
||||
// This stub uses "low level" SDL2 calls to acquire information from a specific Window Manager.
|
||||
// Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
|
||||
#if !defined(__EMSCRIPTEN__) && (defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN))
|
||||
|
||||
#include <SDL_syswm.h>
|
||||
#undef Status // X11 headers are leaking this and also 'Success', 'Always', 'None', all used in DAWN api. Add #undef if necessary.
|
||||
|
||||
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window)
|
||||
{
|
||||
SDL_SysWMinfo sysWMInfo;
|
||||
SDL_VERSION(&sysWMInfo.version);
|
||||
SDL_GetWindowWMInfo(window, &sysWMInfo);
|
||||
|
||||
ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
|
||||
create_info.Instance = instance;
|
||||
#if defined(SDL_VIDEO_DRIVER_COCOA)
|
||||
{
|
||||
create_info.System = "cocoa";
|
||||
create_info.RawWindow = (void*)sysWMInfo.info.cocoa.window;
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#elif defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_X11)
|
||||
const char* sdl_driver = SDL_GetCurrentVideoDriver();
|
||||
if (sdl_driver && strcmp(sdl_driver, "wayland") == 0)
|
||||
{
|
||||
create_info.System = "wayland";
|
||||
create_info.RawDisplay = (void*)sysWMInfo.info.wl.display;
|
||||
create_info.RawSurface = (void*)sysWMInfo.info.wl.surface;
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
else
|
||||
{
|
||||
create_info.System = "x11";
|
||||
create_info.RawWindow = (void*)sysWMInfo.info.x11.window;
|
||||
create_info.RawDisplay = (void*)sysWMInfo.info.x11.display;
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#elif defined(SDL_VIDEO_DRIVER_WINDOWS)
|
||||
{
|
||||
create_info.System = "win32";
|
||||
create_info.RawWindow = (void*)sysWMInfo.info.win.window;
|
||||
create_info.RawInstance = (void*)sysWMInfo.info.win.hinstance;
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#else
|
||||
#error "Unsupported WebGPU native platform!"
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
207
examples/example_sdl3_wgpu/CMakeLists.txt
Normal file
207
examples/example_sdl3_wgpu/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
# Building for desktop (WebGPU-native) with Dawn:
|
||||
# 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_sdl3_wgpu[.exe]
|
||||
# * build/example_sdl3_wgpu[.exe]
|
||||
|
||||
# Building for desktop (WGPU-Native) with WGPU-Native:
|
||||
# 1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
|
||||
# 2. unzip the downloaded file in your_preferred_folder
|
||||
# 3. cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder ("full path" or "relative" starting from current directory)
|
||||
# 4. cmake --build build
|
||||
# The resulting binary will be found at one of the following locations:
|
||||
# * build/Debug/example_sdl3_wgpu[.exe]
|
||||
# * build/example_sdl3_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
|
||||
# (optional) -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py", see ReadMe.md
|
||||
# 4. cmake --build build
|
||||
# 5. emrun build/index.html
|
||||
|
||||
cmake_minimum_required(VERSION 3.22) # Dawn requires CMake >= 3.22
|
||||
project(imgui_example_sdl3_wgpu C CXX)
|
||||
|
||||
set(IMGUI_EXECUTABLE example_sdl3_wgpu)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17) # Dawn requires C++17
|
||||
|
||||
# Dear ImGui
|
||||
set(IMGUI_DIR ../../)
|
||||
|
||||
# ImGui example commons source files
|
||||
set(IMGUI_EXAMPLE_SOURCE_FILES
|
||||
main.cpp
|
||||
# backend files
|
||||
${IMGUI_DIR}/backends/imgui_impl_sdl3.cpp
|
||||
${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp
|
||||
# Dear ImGui files
|
||||
${IMGUI_DIR}/imgui.cpp
|
||||
${IMGUI_DIR}/imgui_draw.cpp
|
||||
${IMGUI_DIR}/imgui_demo.cpp
|
||||
${IMGUI_DIR}/imgui_tables.cpp
|
||||
${IMGUI_DIR}/imgui_widgets.cpp
|
||||
)
|
||||
|
||||
if(EMSCRIPTEN)
|
||||
if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.15")
|
||||
message(FATAL_ERROR "Using Emscripten with SDL3 needs Emscripten version >= 4.0.15")
|
||||
endif()
|
||||
if(NOT IMGUI_EMSCRIPTEN_WEBGPU_FLAG) # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG not used, set by current EMSCRIPTEN version
|
||||
if(EMSCRIPTEN_VERSION VERSION_GREATER_EQUAL "4.0.10")
|
||||
set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "--use-port=emdawnwebgpu" CACHE STRING "Choose between --use-port=emdawnwebgpu (Dawn implementation of EMSCRIPTEN) and -sUSE_WEBGPU=1 (WGPU implementation of EMSCRIPTEN, deprecated in 4.0.10): default to --use-port=emdawnwebgpu for EMSCRIPTEN >= 4.0.10")
|
||||
else()
|
||||
set(IMGUI_EMSCRIPTEN_WEBGPU_FLAG "-sUSE_WEBGPU=1" CACHE STRING "Use -sUSE_WEBGPU=1 for EMSCRIPTEN WGPU implementation")
|
||||
endif()
|
||||
else() # if IMGUI_EMSCRIPTEN_WEBGPU_FLAG used, check correct version
|
||||
if(EMSCRIPTEN_VERSION VERSION_LESS "4.0.10" AND "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
|
||||
# it's necessary EMSCRIPTEN >= v4.0.10 (although "--use-port=path/to/emdawnwebgpu.port.py" is supported/tested from v4.0.8)
|
||||
message(FATAL_ERROR "emdawnwebgpu needs EMSCRIPTEN version >= 4.0.10")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_compile_options(-sDISABLE_EXCEPTION_CATCHING=1 -DIMGUI_DISABLE_FILE_FUNCTIONS=1)
|
||||
else() # Native/Desktop build
|
||||
if(NOT IMGUI_DAWN_DIR AND NOT IMGUI_WGPU_DIR) # if it's Native/Desktop build, IMGUI_DAWN_DIR or IMGUI_WGPU_DIR must be specified
|
||||
message(FATAL_ERROR "Please specify the Dawn or WGPU base directory")
|
||||
endif()
|
||||
|
||||
if(IMGUI_DAWN_DIR AND IMGUI_WGPU_DIR) # both IMGUI_DAWN_DIR and IMGUI_WGPU_DIR cannot be set
|
||||
message(FATAL_ERROR "Please specify only one between Dawn / WGPU base directory")
|
||||
endif()
|
||||
|
||||
if(APPLE) # Add SDL3 module to get Surface, with libs and file property for MacOS build
|
||||
set_source_files_properties(${IMGUI_DIR}/backends/imgui_impl_wgpu.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
|
||||
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal -framework MetalKit -framework Cocoa")
|
||||
endif()
|
||||
|
||||
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
|
||||
|
||||
if(IMGUI_DAWN_DIR) # DAWN-Native build options
|
||||
list(APPEND CMAKE_PREFIX_PATH ${IMGUI_DAWN_DIR})
|
||||
find_package(Threads) # required from Dawn installation
|
||||
find_package(Dawn) # Search for a Dawn installation using IMGUI_DAWN_DIR in CMAKE_PREFIX_PATH
|
||||
if(Dawn_FOUND)
|
||||
message("Dawn Installation has been found!")
|
||||
set(LIBRARIES dawn::webgpu_dawn ${OS_LIBRARIES})
|
||||
else()
|
||||
set(IMGUI_DAWN_DIR CACHE PATH "Path to Dawn repository")
|
||||
|
||||
option(DAWN_USE_GLFW OFF) # disable buildin GLFW in DAWN when we use SDL2 / SDL3
|
||||
|
||||
option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" ON)
|
||||
set(DAWN_BUILD_MONOLITHIC_LIBRARY "STATIC" CACHE STRING "Build monolithic library: SHARED, STATIC, or OFF.")
|
||||
|
||||
# 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)
|
||||
option(TINT_BUILD_DOCS "Build documentation" OFF)
|
||||
option(TINT_BUILD_TESTS "Build tests" OFF)
|
||||
if(NOT APPLE)
|
||||
option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" OFF)
|
||||
endif()
|
||||
if(WIN32)
|
||||
option(DAWN_FORCE_SYSTEM_COMPONENT_LOAD "Allow system component fallback" ON)
|
||||
option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" OFF)
|
||||
option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
|
||||
option(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" OFF)
|
||||
option(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" OFF)
|
||||
option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
|
||||
option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
|
||||
endif()
|
||||
# check if WAYLAND is the current Session Type and enable DAWN_USE_WAYLAND Wayland option @compile time
|
||||
# You can override this using: cmake -DDAWN_USE_WAYLAND=X (X = ON | OFF)
|
||||
if(LINUX)
|
||||
if($ENV{XDG_SESSION_TYPE} MATCHES wayland)
|
||||
option(DAWN_USE_WAYLAND "Enable support for Wayland surface" ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_subdirectory("${IMGUI_DAWN_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/dawn" EXCLUDE_FROM_ALL)
|
||||
|
||||
set(LIBRARIES webgpu_dawn ${OS_LIBRARIES})
|
||||
endif()
|
||||
else() # WGPU-Native build options
|
||||
set(WGPU_NATIVE_LIB_DIR ${IMGUI_WGPU_DIR}/lib)
|
||||
find_library(WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native HINTS ${WGPU_NATIVE_LIB_DIR} REQUIRED)
|
||||
if(WIN32)
|
||||
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32 Propsys RuntimeObject)
|
||||
elseif(UNIX AND NOT APPLE)
|
||||
set(OS_LIBRARIES "-lm -ldl")
|
||||
endif()
|
||||
|
||||
set(LIBRARIES ${WGPU_LIBRARY} ${OS_LIBRARIES})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(${IMGUI_EXECUTABLE} ${IMGUI_EXAMPLE_SOURCE_FILES})
|
||||
|
||||
target_include_directories(${IMGUI_EXECUTABLE} PUBLIC
|
||||
${IMGUI_DIR}
|
||||
${IMGUI_DIR}/backends
|
||||
${SDL3_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_EXAMPLE_SDL3_WGPU")
|
||||
|
||||
# Enable warning level compiler option only for IMGUI_EXAMPLE_SOURCE_FILES
|
||||
if (MSVC)
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC /W4) # warning level 4
|
||||
else()
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC -Wall) # -Wextra -Wpedantic
|
||||
endif()
|
||||
|
||||
# IMGUI_IMPL_WEBGPU_BACKEND_DAWN/WGPU internal define is set according to:
|
||||
# EMSCRIPTEN: by used FLAG
|
||||
# --use-port=emdawnwebgpu --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled (+EMSCRIPTEN)
|
||||
# -sUSE_WEBGPU=1 --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled (+EMSCRIPTEN)
|
||||
# NATIVE: by used SDK installation directory
|
||||
# if IMGUI_DAWN_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_DAWN enabled
|
||||
# if IMGUI_WGPU_DIR is valid --> IMGUI_IMPL_WEBGPU_BACKEND_WGPU enabled
|
||||
|
||||
if(NOT EMSCRIPTEN) # WegGPU-Native settings
|
||||
if(IMGUI_DAWN_DIR)
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
|
||||
if(NOT Dawn_FOUND)
|
||||
target_link_libraries(${IMGUI_EXECUTABLE} INTERFACE webgpu_cpp)
|
||||
endif()
|
||||
else()
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
|
||||
target_include_directories(${IMGUI_EXECUTABLE} PUBLIC ${IMGUI_WGPU_DIR}/include)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${IMGUI_EXECUTABLE} PUBLIC ${LIBRARIES} SDL3::SDL3)
|
||||
else() # Emscripten settings
|
||||
set(CMAKE_EXECUTABLE_SUFFIX ".html")
|
||||
|
||||
if("${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}" MATCHES "emdawnwebgpu")
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}")
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_DAWN")
|
||||
else()
|
||||
target_compile_definitions(${IMGUI_EXECUTABLE} PUBLIC "IMGUI_IMPL_WEBGPU_BACKEND_WGPU")
|
||||
endif()
|
||||
message(STATUS "Using ${IMGUI_EMSCRIPTEN_WEBGPU_FLAG} WebGPU implementation")
|
||||
|
||||
target_compile_options(${IMGUI_EXECUTABLE} PUBLIC "-sUSE_SDL=3")
|
||||
target_link_options(${IMGUI_EXECUTABLE} PRIVATE
|
||||
"${IMGUI_EMSCRIPTEN_WEBGPU_FLAG}"
|
||||
"-sUSE_SDL=3"
|
||||
"-sWASM=1"
|
||||
"-sASYNCIFY=1"
|
||||
"-sALLOW_MEMORY_GROWTH=1"
|
||||
"-sNO_EXIT_RUNTIME=0"
|
||||
"-sASSERTIONS=1"
|
||||
"-sDISABLE_EXCEPTION_CATCHING=1"
|
||||
# "-sNO_FILESYSTEM=1"
|
||||
"--shell-file=${CMAKE_CURRENT_LIST_DIR}/../libs/emscripten/shell_minimal.html"
|
||||
)
|
||||
set_target_properties(${IMGUI_EXECUTABLE} PROPERTIES OUTPUT_NAME "index")
|
||||
endif()
|
||||
103
examples/example_sdl3_wgpu/Makefile.emscripten
Normal file
103
examples/example_sdl3_wgpu/Makefile.emscripten
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#
|
||||
# Makefile to use with emscripten
|
||||
# See https://emscripten.org/docs/getting_started/downloads.html
|
||||
# for installation instructions.
|
||||
#
|
||||
# This Makefile assumes you have loaded emscripten's environment.
|
||||
# (On Windows, you may need to execute emsdk_env.bat or encmdprompt.bat ahead)
|
||||
#
|
||||
# Running `make -f Makefile.emscripten` will produce three files:
|
||||
# - web/index.html
|
||||
# - web/index.js
|
||||
# - web/index.wasm
|
||||
#
|
||||
# All three are needed to run the demo.
|
||||
|
||||
CC = emcc
|
||||
CXX = em++
|
||||
WEB_DIR = web
|
||||
EXE = $(WEB_DIR)/index.html
|
||||
IMGUI_DIR = ../..
|
||||
SOURCES = main.cpp
|
||||
SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
|
||||
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl3.cpp $(IMGUI_DIR)/backends/imgui_impl_wgpu.cpp
|
||||
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
|
||||
UNAME_S := $(shell uname -s)
|
||||
CPPFLAGS =
|
||||
LDFLAGS =
|
||||
EMS =
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
## EMSCRIPTEN OPTIONS
|
||||
##---------------------------------------------------------------------
|
||||
|
||||
# ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
|
||||
EMS += -s USE_SDL=3
|
||||
EMS += -s DISABLE_EXCEPTION_CATCHING=1
|
||||
LDFLAGS += -s WASM=1
|
||||
LDFLAGS += -s ALLOW_MEMORY_GROWTH=1
|
||||
LDFLAGS += -s ASYNCIFY=1
|
||||
LDFLAGS += -s NO_EXIT_RUNTIME=0
|
||||
LDFLAGS += -s ASSERTIONS=1
|
||||
|
||||
# (1) Using legacy WebGPU implementation (Emscripten < 4.0.10)
|
||||
#EMS += -DIMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
#LDFLAGS += -s USE_WEBGPU=1
|
||||
|
||||
# or (2) Using newer Dawn-based WebGPU port (Emscripten >= 4.0.10)
|
||||
EMS += --use-port=emdawnwebgpu
|
||||
LDFLAGS += --use-port=emdawnwebgpu
|
||||
|
||||
# Build as single file (binary text encoded in .html file)
|
||||
#LDFLAGS += -sSINGLE_FILE
|
||||
|
||||
# Emscripten allows preloading a file or folder to be accessible at runtime.
|
||||
# The Makefile for this example project suggests embedding the misc/fonts/ folder into our application, it will then be accessible as "/fonts"
|
||||
# See documentation for more details: https://emscripten.org/docs/porting/files/packaging_files.html
|
||||
# (Default value is 0. Set to 1 to enable file-system and include the misc/fonts/ folder as part of the build.)
|
||||
USE_FILE_SYSTEM ?= 0
|
||||
ifeq ($(USE_FILE_SYSTEM), 0)
|
||||
LDFLAGS += -s NO_FILESYSTEM=1
|
||||
CPPFLAGS += -DIMGUI_DISABLE_FILE_FUNCTIONS
|
||||
endif
|
||||
ifeq ($(USE_FILE_SYSTEM), 1)
|
||||
LDFLAGS += --no-heap-copy --preload-file ../../misc/fonts@/fonts
|
||||
endif
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
## FINAL BUILD FLAGS
|
||||
##---------------------------------------------------------------------
|
||||
|
||||
CPPFLAGS += -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
|
||||
#CPPFLAGS += -g
|
||||
CPPFLAGS += -Wall -Wformat -Os $(EMS)
|
||||
LDFLAGS += --shell-file ../libs/emscripten/shell_minimal.html
|
||||
LDFLAGS += $(EMS)
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
## BUILD RULES
|
||||
##---------------------------------------------------------------------
|
||||
|
||||
%.o:%.cpp
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
%.o:$(IMGUI_DIR)/%.cpp
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
%.o:$(IMGUI_DIR)/backends/%.cpp
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
all: $(EXE)
|
||||
@echo Build complete for $(EXE)
|
||||
|
||||
$(WEB_DIR):
|
||||
mkdir $@
|
||||
|
||||
serve: all
|
||||
python3 -m http.server -d $(WEB_DIR)
|
||||
|
||||
$(EXE): $(OBJS) $(WEB_DIR)
|
||||
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(EXE) $(OBJS) $(WEB_DIR)/*.js $(WEB_DIR)/*.wasm $(WEB_DIR)/*.wasm.pre
|
||||
167
examples/example_sdl3_wgpu/README.md
Normal file
167
examples/example_sdl3_wgpu/README.md
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
## How to Build
|
||||
|
||||
|
||||
---
|
||||
|
||||
### Using CMake
|
||||
#### Building for desktop (WebGPU-native) with Google Dawn:
|
||||
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_sdl3_wgpu[.exe]
|
||||
* build/example_sdl3_wgpu[.exe]
|
||||
|
||||
#### Building for desktop (WebGPU-Native) with WGPU:
|
||||
1. download WGPU-Native autogenerated binary modules for your platform/compiler from: https://github.com/gfx-rs/wgpu-native/releases
|
||||
2. unzip the downloaded file in `your_preferred_folder`
|
||||
3. `cmake -B build -DIMGUI_WGPU_DIR=your_preferred_folder` ("full path" or "relative" starting from current directory)
|
||||
5. `cmake --build build`
|
||||
The resulting binary will be found at one of the following locations:
|
||||
* build/Debug/example_sdl3_wgpu[.exe]
|
||||
* build/example_sdl3_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`
|
||||
- (optional) `-DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py"`, see below
|
||||
4. `cmake --build build`
|
||||
|
||||
#### Sync Emscripten with latest Google Dawn:
|
||||
If you want to sync Emscripten with latest DAWN release it's necessary to download the `port-emdawnwgpu-package` (released daily by Google) here:
|
||||
https://github.com/google/dawn/releases
|
||||
Unpack it in your preferred folder and to replace the step 3 with:
|
||||
|
||||
3. `emcmake cmake -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path/to/emdawnwebgpu_package/emdawnwebgpu.port.py" -G Ninja -B build`
|
||||
|
||||
**N.B.**
|
||||
For the WASM code produced by Emscripten to work correctly, it will also be necessary to have the "corresponding" (or newer) version of Google Canary (nightly build for developers) that includes the latest changes
|
||||
|
||||
|
||||
---
|
||||
|
||||
### CMake by step
|
||||
|
||||
#### Generate Dawn Native:
|
||||
|
||||
- `cmake -G Ninja -DIMGUI_DAWN_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
- Using `IMGUI_DAWN_DIR` set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
|
||||
#### Generate WGPU Native:
|
||||
|
||||
- `cmake -G Ninja -DIMGUI_WGPU_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
|
||||
- Using `IMGUI_WGPU_DIR` set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten:
|
||||
|
||||
- `emcmake cmake -G Ninja -B where_to_build_dir`\
|
||||
CMake checks the EMSCRIPEN version then:
|
||||
- if EMS >= 4.0.10 uses `--use-port=emdawnwebgpu` flag to build
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
- if EMS < 4.0.10 uses `-sUSE_WEBGPU=1` flag to build
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten forcing `-sUSE_WEBGPU=1` deprecated flag even with EMS >= 4.0.10
|
||||
- `emcmake cmake -G Ninja -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="-sUSE_WEBGPU=1" -B where_to_build_dir`
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_WGPU` compiler define
|
||||
|
||||
#### Generate Emscripten using external WebGPU library (emdawnwebgpu_pkg)
|
||||
- `emcmake cmake -G Ninja -DIMGUI_EMSCRIPTEN_WEBGPU_FLAG="--use-port=path_to_emdawnwebgpu_pkg" -B where_to_build_dir`
|
||||
- it set `IMGUI_IMPL_WEBGPU_BACKEND_DAWN` compiler define
|
||||
- *To use external WebGPU library it's necessary to have EMS >= 4.0.10 or the minimum requirements specified by the package:*
|
||||
- https://github.com/google/dawn/releases
|
||||
|
||||
#### Build time
|
||||
|
||||
Once the procedure for the specific builder is generated, the build command is **always the same**:
|
||||
|
||||
- Build using CMake
|
||||
- `cmake --build where_to_build_dir`
|
||||
- It will use selected builder to build the example.
|
||||
|
||||
- Build explicitly:
|
||||
- `cd where_to_build_dir`
|
||||
- `ninja`
|
||||
- This is the builder chosen during the generation phase
|
||||
|
||||
---
|
||||
|
||||
### CMake useful options
|
||||
#### Generator types (alternative to **ninja** bulder):
|
||||
- `-G Ninja` to build with __ninja__ builder
|
||||
- `-G "Unix Makefiles"` to build with __make__ builder
|
||||
- `-G "Visual Studio 17 2022" -A x64` to create a VS 2022 solution (.sln) file, Windows only
|
||||
- **Native build only**
|
||||
- Not **officially** supported to build Google Dawn
|
||||
|
||||
Example:
|
||||
- using **make** instead **ninja**:
|
||||
- `cmake -G "Unix Makefiles" -DIMGUI_DAWN_DIR=path_to_sdk_dir -B where_to_build_dir`
|
||||
|
||||
**Syntax is case sensitive and the "" are necessary in case of spaces between words*
|
||||
|
||||
#### Directories
|
||||
- The directory path can be absolute or relative (starting from the current directory)
|
||||
- It's necessary to use different `where_to_build_dir` for different CMake generations
|
||||
|
||||
|
||||
#### Build type
|
||||
The default build type is **Debug**
|
||||
It is possible to use a different build type using:
|
||||
- `-DCMAKE_BUILD_TYPE=Release`
|
||||
- `-DCMAKE_BUILD_TYPE=MinSizeRel`
|
||||
- `-DCMAKE_BUILD_TYPE=RelWithDebInfo`
|
||||
|
||||
Example:
|
||||
- building **Release**:
|
||||
- `cmake -G ninja -DIMGUI_WGPU_DIR=path_to_sdk_dir -DCMAKE_BUILD_TYPE=Release -B where_to_build_dir `
|
||||
|
||||
#### GLFW / SDL2 / SDL3 includes, libraries, search paths and package manager
|
||||
|
||||
Includes and libraries, by default, are searched in system/compiler paths (environment variables): you can add the path to your development tools to the environment variables without having to modify the `CMakeLists.txt` file.
|
||||
- e.g. CLang search in path specified from the following environment variables:
|
||||
- include files: CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH
|
||||
- library files: LIBRARY_PATH
|
||||
|
||||
If you are using a package manager (**vcpkg** / **conan** / ... ) you need/can to specify it, adding to cmake command:
|
||||
- `-DCMAKE_TOOLCHAIN_FILE=path/to/package_manager.cmake`
|
||||
|
||||
Examples:
|
||||
|
||||
- using **vcpkg** package manager it's necessary adding:
|
||||
- `-DCMAKE_TOOLCHAIN_FILE=<vcpkg_root_dir>/scripts/buildsystems/vcpkg.cmake`
|
||||
|
||||
- full cmake command using **vcpkg** package manager:
|
||||
- `cmake -G Ninja -DIMGUI_DAWN_DIR=path_to_sdk_dir -DCMAKE_TOOLCHAIN_FILE=<vcpkg_root_dir>/scripts/buildsystems/vcpkg.cmake -B where_to_build_dir`
|
||||
|
||||
---
|
||||
|
||||
|
||||
### Using makefile
|
||||
|
||||
- You need to install Emscripten from https://emscripten.org/docs/getting_started/downloads.html, and have the environment variables set, as described in https://emscripten.org/docs/getting_started/downloads.html#installation-instructions
|
||||
|
||||
- Depending on your configuration, in Windows you may need to run `emsdk/emsdk_env.bat` in your console to access the Emscripten command-line tools.
|
||||
|
||||
- You may also refer to our [Continuous Integration setup](https://github.com/ocornut/imgui/tree/master/.github/workflows) for Emscripten setup.
|
||||
|
||||
- Then build using `make -f Makefile.emscripten` while in the `example_glfw_wgpu/` directory.
|
||||
|
||||
- Requires recent Emscripten as WGPU is still a work-in-progress API.
|
||||
|
||||
---
|
||||
|
||||
## How to Run
|
||||
|
||||
To run on a local machine:
|
||||
- Make sure your browse supports WGPU and it is enabled. WGPU is still WIP not enabled by default in most browser.
|
||||
- `make serve` will use Python3 to spawn a local webserver, you can then browse http://localhost:8000 to access your build.
|
||||
- Otherwise, generally you will need a local webserver:
|
||||
- Quoting [https://emscripten.org/docs/getting_started](https://emscripten.org/docs/getting_started/Tutorial.html#generating-html):<br>
|
||||
_"Unfortunately several browsers (including Chrome, Safari, and Internet Explorer) do not support file:// [XHR](https://emscripten.org/docs/site/glossary.html#term-xhr) requests, and can’t load extra files needed by the HTML (like a .wasm file, or packaged file data as mentioned lower down). For these browsers you’ll need to serve the files using a [local webserver](https://emscripten.org/docs/getting_started/FAQ.html#faq-local-webserver) and then open http://localhost:8000/hello.html."_
|
||||
- Emscripten SDK has a handy `emrun` command: `emrun web/example_glfw_wgpu.html --browser firefox` which will spawn a temporary local webserver (in Firefox). See https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html for details.
|
||||
- You may use Python 3 builtin webserver: `python -m http.server -d web` (this is what `make serve` uses).
|
||||
- You may use Python 2 builtin webserver: `cd web && python -m SimpleHTTPServer`.
|
||||
- If you are accessing the files over a network, certain browsers, such as Firefox, will restrict Gamepad API access to secure contexts only (e.g. https only).
|
||||
553
examples/example_sdl3_wgpu/main.cpp
Normal file
553
examples/example_sdl3_wgpu/main.cpp
Normal file
|
|
@ -0,0 +1,553 @@
|
|||
// Dear ImGui: standalone example application for using SDL3 + 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:
|
||||
// - FAQ https://dearimgui.com/faq
|
||||
// - Getting Started https://dearimgui.com/getting-started
|
||||
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
|
||||
// - Introduction, links and more at the top of imgui.cpp
|
||||
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl3.h"
|
||||
#include "imgui_impl_wgpu.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#include <emscripten/html5_webgpu.h>
|
||||
#endif
|
||||
#include "../libs/emscripten/emscripten_mainloop_stub.h"
|
||||
#endif
|
||||
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
#include <webgpu/webgpu_cpp.h>
|
||||
#endif
|
||||
|
||||
// Data
|
||||
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(SDL_Window* window);
|
||||
static WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window);
|
||||
|
||||
static void ResizeSurface(int width, int height)
|
||||
{
|
||||
wgpu_surface_configuration.width = wgpu_surface_width = width;
|
||||
wgpu_surface_configuration.height = wgpu_surface_height = height;
|
||||
wgpuSurfaceConfigure( wgpu_surface, (WGPUSurfaceConfiguration*)&wgpu_surface_configuration );
|
||||
}
|
||||
|
||||
// Main code
|
||||
int main(int, char**)
|
||||
{
|
||||
// Setup SDL
|
||||
// [If using SDL_MAIN_USE_CALLBACKS: all code below until the main loop starts would likely be your SDL_AppInit() function]
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
|
||||
{
|
||||
printf("Error: SDL_Init(): %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create SDL window graphics context
|
||||
float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
||||
SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE;
|
||||
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+WebGPU example", wgpu_surface_width, wgpu_surface_height, window_flags);
|
||||
if (window == nullptr)
|
||||
{
|
||||
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize WGPU
|
||||
InitWGPU(window);
|
||||
|
||||
// Setup Dear ImGui context
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
//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
|
||||
ImGui_ImplSDL3_InitForOther(window);
|
||||
|
||||
ImGui_ImplWGPU_InitInfo init_info;
|
||||
init_info.Device = wgpu_device;
|
||||
init_info.NumFramesInFlight = 3;
|
||||
init_info.RenderTargetFormat = wgpu_surface_configuration.format;
|
||||
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.
|
||||
// - 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).
|
||||
// - 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 \\ !
|
||||
// - 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;
|
||||
//io.Fonts->AddFontDefault();
|
||||
#ifndef IMGUI_DISABLE_FILE_FUNCTIONS
|
||||
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
|
||||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
|
||||
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
|
||||
//IM_ASSERT(font != nullptr);
|
||||
#endif
|
||||
|
||||
// Our state
|
||||
bool show_demo_window = true;
|
||||
bool show_another_window = false;
|
||||
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
|
||||
// Main loop
|
||||
bool done = false;
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
|
||||
// You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
|
||||
io.IniFilename = nullptr;
|
||||
EMSCRIPTEN_MAINLOOP_BEGIN
|
||||
#else
|
||||
while (!done)
|
||||
#endif
|
||||
{
|
||||
// Poll and handle events (inputs, window resize, etc.)
|
||||
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||||
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||||
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||
// [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function]
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
ImGui_ImplSDL3_ProcessEvent(&event);
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
done = true;
|
||||
if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
|
||||
done = true;
|
||||
}
|
||||
|
||||
// [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function]
|
||||
// React to changes in screen size
|
||||
int width, height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
if (width != wgpu_surface_width || height != wgpu_surface_height)
|
||||
ResizeSurface(width, height);
|
||||
|
||||
// Check surface status for error. If texture is not optimal, try to reconfigure the surface.
|
||||
WGPUSurfaceTexture surface_texture;
|
||||
wgpuSurfaceGetCurrentTexture(wgpu_surface, &surface_texture);
|
||||
if (ImGui_ImplWGPU_IsSurfaceStatusError(surface_texture.status))
|
||||
{
|
||||
fprintf(stderr, "Unrecoverable Surface Texture status=%#.8x\n", surface_texture.status);
|
||||
abort();
|
||||
}
|
||||
if (ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(surface_texture.status))
|
||||
{
|
||||
if (surface_texture.texture)
|
||||
wgpuTextureRelease(surface_texture.texture);
|
||||
if (width > 0 && height > 0)
|
||||
ResizeSurface(width, height);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplWGPU_NewFrame();
|
||||
ImGui_ImplSDL3_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
|
||||
if (show_demo_window)
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
|
||||
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
|
||||
{
|
||||
static float f = 0.0f;
|
||||
static int counter = 0;
|
||||
|
||||
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
|
||||
|
||||
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
|
||||
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
|
||||
ImGui::Checkbox("Another Window", &show_another_window);
|
||||
|
||||
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
||||
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
||||
|
||||
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
|
||||
counter++;
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("counter = %d", counter);
|
||||
|
||||
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
// 3. Show another simple window.
|
||||
if (show_another_window)
|
||||
{
|
||||
ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
|
||||
ImGui::Text("Hello from another window!");
|
||||
if (ImGui::Button("Close Me"))
|
||||
show_another_window = false;
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
// Rendering
|
||||
ImGui::Render();
|
||||
|
||||
WGPUTextureViewDescriptor view_desc = {};
|
||||
view_desc.format = wgpu_surface_configuration.format;
|
||||
view_desc.dimension = WGPUTextureViewDimension_2D;
|
||||
view_desc.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED;
|
||||
view_desc.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED;
|
||||
view_desc.aspect = WGPUTextureAspect_All;
|
||||
|
||||
WGPUTextureView texture_view = wgpuTextureCreateView(surface_texture.texture, &view_desc);
|
||||
|
||||
WGPURenderPassColorAttachment color_attachments = {};
|
||||
color_attachments.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
|
||||
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 = texture_view;
|
||||
|
||||
WGPURenderPassDescriptor render_pass_desc = {};
|
||||
render_pass_desc.colorAttachmentCount = 1;
|
||||
render_pass_desc.colorAttachments = &color_attachments;
|
||||
render_pass_desc.depthStencilAttachment = nullptr;
|
||||
|
||||
WGPUCommandEncoderDescriptor enc_desc = {};
|
||||
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(wgpu_device, &enc_desc);
|
||||
|
||||
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &render_pass_desc);
|
||||
ImGui_ImplWGPU_RenderDrawData(ImGui::GetDrawData(), pass);
|
||||
wgpuRenderPassEncoderEnd(pass);
|
||||
|
||||
WGPUCommandBufferDescriptor cmd_buffer_desc = {};
|
||||
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
|
||||
wgpuQueueSubmit(wgpu_queue, 1, &cmd_buffer);
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
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
|
||||
#endif
|
||||
wgpuTextureViewRelease(texture_view);
|
||||
wgpuRenderPassEncoderRelease(pass);
|
||||
wgpuCommandEncoderRelease(encoder);
|
||||
wgpuCommandBufferRelease(cmd_buffer);
|
||||
}
|
||||
#ifdef __EMSCRIPTEN__
|
||||
EMSCRIPTEN_MAINLOOP_END;
|
||||
#endif
|
||||
|
||||
// Cleanup
|
||||
// [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
|
||||
ImGui_ImplWGPU_Shutdown();
|
||||
ImGui_ImplSDL3_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
wgpuSurfaceUnconfigure(wgpu_surface);
|
||||
wgpuSurfaceRelease(wgpu_surface);
|
||||
wgpuQueueRelease(wgpu_queue);
|
||||
wgpuDeviceRelease(wgpu_device);
|
||||
wgpuInstanceRelease(wgpu_instance);
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
|
||||
static WGPUAdapter RequestAdapter(wgpu::Instance& instance)
|
||||
{
|
||||
wgpu::Adapter acquired_adapter;
|
||||
wgpu::RequestAdapterOptions adapter_options;
|
||||
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;
|
||||
}
|
||||
acquired_adapter = std::move(adapter);
|
||||
};
|
||||
|
||||
// Synchronously (wait until) acquire Adapter
|
||||
wgpu::Future waitAdapterFunc { instance.RequestAdapter(&adapter_options, wgpu::CallbackMode::WaitAnyOnly, onRequestAdapter) };
|
||||
wgpu::WaitStatus waitStatusAdapter = instance.WaitAny(waitAdapterFunc, UINT64_MAX);
|
||||
IM_ASSERT(acquired_adapter != nullptr && waitStatusAdapter == wgpu::WaitStatus::Success && "Error on Adapter request");
|
||||
return acquired_adapter.MoveToCHandle();
|
||||
}
|
||||
|
||||
static WGPUDevice RequestDevice(wgpu::Instance& instance, wgpu::Adapter& adapter)
|
||||
{
|
||||
// Set device callback functions
|
||||
wgpu::DeviceDescriptor device_desc;
|
||||
device_desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowSpontaneous,
|
||||
[](const wgpu::Device&, wgpu::DeviceLostReason type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetDeviceLostReasonName((WGPUDeviceLostReason)type), msg.data); }
|
||||
);
|
||||
device_desc.SetUncapturedErrorCallback(
|
||||
[](const wgpu::Device&, wgpu::ErrorType type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetErrorTypeName((WGPUErrorType)type), msg.data); }
|
||||
);
|
||||
|
||||
wgpu::Device acquired_device;
|
||||
auto onRequestDevice = [&](wgpu::RequestDeviceStatus status, wgpu::Device local_device, wgpu::StringView message)
|
||||
{
|
||||
if (status != wgpu::RequestDeviceStatus::Success)
|
||||
{
|
||||
printf("Failed to get an device: %s\n", message.data);
|
||||
return;
|
||||
}
|
||||
acquired_device = std::move(local_device);
|
||||
};
|
||||
|
||||
// Synchronously (wait until) get Device
|
||||
wgpu::Future waitDeviceFunc { adapter.RequestDevice(&device_desc, wgpu::CallbackMode::WaitAnyOnly, onRequestDevice) };
|
||||
wgpu::WaitStatus waitStatusDevice = instance.WaitAny(waitDeviceFunc, UINT64_MAX);
|
||||
IM_ASSERT(acquired_device != nullptr && waitStatusDevice == wgpu::WaitStatus::Success && "Error on Device request");
|
||||
return acquired_device.MoveToCHandle();
|
||||
}
|
||||
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// 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 RequestAdapter(WGPUInstance& instance)
|
||||
{
|
||||
WGPURequestAdapterOptions adapter_options = {};
|
||||
|
||||
WGPUAdapter local_adapter;
|
||||
WGPURequestAdapterCallbackInfo adapterCallbackInfo = {};
|
||||
adapterCallbackInfo.callback = handle_request_adapter;
|
||||
adapterCallbackInfo.userdata1 = &local_adapter;
|
||||
|
||||
wgpuInstanceRequestAdapter(instance, &adapter_options, adapterCallbackInfo);
|
||||
IM_ASSERT(local_adapter && "Error on Adapter request");
|
||||
return local_adapter;
|
||||
}
|
||||
|
||||
static WGPUDevice RequestDevice(WGPUAdapter& adapter)
|
||||
{
|
||||
WGPUDevice local_device;
|
||||
WGPURequestDeviceCallbackInfo deviceCallbackInfo = {};
|
||||
deviceCallbackInfo.callback = handle_request_device;
|
||||
deviceCallbackInfo.userdata1 = &local_device;
|
||||
wgpuAdapterRequestDevice(adapter, nullptr, deviceCallbackInfo);
|
||||
IM_ASSERT(local_device && "Error on Device request");
|
||||
return local_device;
|
||||
}
|
||||
#endif // __EMSCRIPTEN__
|
||||
#endif // IMGUI_IMPL_WEBGPU_BACKEND_WGPU
|
||||
|
||||
static bool InitWGPU(SDL_Window* 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 instance_desc = {};
|
||||
static constexpr wgpu::InstanceFeatureName timedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
|
||||
instance_desc.requiredFeatureCount = 1;
|
||||
instance_desc.requiredFeatures = &timedWaitAny;
|
||||
wgpu::Instance instance = wgpu::CreateInstance(&instance_desc);
|
||||
|
||||
wgpu::Adapter adapter = RequestAdapter(instance);
|
||||
ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter.Get());
|
||||
|
||||
wgpu_device = RequestDevice(instance, adapter);
|
||||
|
||||
// Create the surface.
|
||||
#ifdef __EMSCRIPTEN__
|
||||
wgpu::EmscriptenSurfaceSourceCanvasHTMLSelector canvas_desc = {};
|
||||
canvas_desc.selector = "#canvas";
|
||||
|
||||
wgpu::SurfaceDescriptor surface_desc = {};
|
||||
surface_desc.nextInChain = &canvas_desc;
|
||||
wgpu::Surface surface = instance.CreateSurface(&surface_desc);
|
||||
#else
|
||||
wgpu::Surface surface = CreateWGPUSurface(instance.Get(), window);
|
||||
#endif
|
||||
if (!surface)
|
||||
return false;
|
||||
|
||||
// Moving Dawn objects into WGPU handles
|
||||
wgpu_instance = instance.MoveToCHandle();
|
||||
wgpu_surface = surface.MoveToCHandle();
|
||||
|
||||
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();
|
||||
IM_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(
|
||||
[](WGPULogLevel level, WGPUStringView msg, void* userdata) { fprintf(stderr, "%s: %.*s\n", ImGui_ImplWGPU_GetLogLevelName(level), (int)msg.length, msg.data); }, nullptr
|
||||
);
|
||||
wgpuSetLogLevel(WGPULogLevel_Warn);
|
||||
|
||||
WGPUAdapter adapter = RequestAdapter(wgpu_instance);
|
||||
ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter);
|
||||
|
||||
wgpu_device = RequestDevice(adapter);
|
||||
|
||||
// Create the surface.
|
||||
wgpu_surface = CreateWGPUSurface(wgpu_instance, 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;
|
||||
}
|
||||
|
||||
// SDL3 helper to create a WebGPU surface (exclusively!) for Native/Desktop applications: available only together with WebGPU/WGPU backend
|
||||
// As of today (2025/10) there is no "official" support in SDL3 to create a surface for WebGPU backend
|
||||
// This stub uses "low level" SDL3 calls to acquire information from a specific Window Manager.
|
||||
// Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
|
||||
#if !defined(__EMSCRIPTEN__) && (defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN))
|
||||
|
||||
#if defined(SDL_PLATFORM_WIN32)
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
static WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window)
|
||||
{
|
||||
SDL_PropertiesID propertiesID = SDL_GetWindowProperties(window);
|
||||
|
||||
ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
|
||||
create_info.Instance = instance;
|
||||
#if defined(SDL_PLATFORM_MACOS)
|
||||
{
|
||||
create_info.System = "cocoa";
|
||||
create_info.RawWindow = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL);
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#elif defined(SDL_PLATFORM_LINUX)
|
||||
if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0)
|
||||
{
|
||||
create_info.System = "wayland";
|
||||
create_info.RawDisplay = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL);
|
||||
create_info.RawSurface = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL);
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
else if (!SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11"))
|
||||
{
|
||||
create_info.System = "x11";
|
||||
create_info.RawWindow = (void*)SDL_GetNumberProperty(propertiesID, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0);
|
||||
create_info.RawDisplay = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL);
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#elif defined(SDL_PLATFORM_WIN32)
|
||||
{
|
||||
create_info.System = "win32";
|
||||
create_info.RawWindow = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
|
||||
create_info.RawInstance = (void*)::GetModuleHandle(NULL);
|
||||
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
|
||||
}
|
||||
#else
|
||||
#error "Unsupported WebGPU native platform!"
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
41
imgui.cpp
41
imgui.cpp
|
|
@ -449,7 +449,7 @@ IMPLEMENTING SUPPORT for ImGuiBackendFlags_RendererHasTextures:
|
|||
ImFontConfig cfg2;
|
||||
cfg2.MergeMode = true;
|
||||
io.Fonts->AddFontFromFileTTF("FontAwesome4.ttf", 0.0f, &cfg2);
|
||||
- You can use `Metrics/Debugger->Fonts->Font->Input Glyphs Overlap Detection Tool` to see list of glyphs available in multiple font sources. This can facilitate unde
|
||||
- You can use `Metrics/Debugger->Fonts->Font->Input Glyphs Overlap Detection Tool` to see list of glyphs available in multiple font sources. This can facilitate understanding which font input is providing which glyph.
|
||||
- Fonts: **IMPORTANT** on Thread Safety:
|
||||
- A few functions such as font->CalcTextSizeA() were, by sheer luck (== accidentally) thread-safe even thou we had never provided that guarantee. They are definitively not thread-safe anymore as new glyphs may be loaded.
|
||||
- Fonts: ImFont::FontSize was removed and does not make sense anymore. ImFont::LegacySize is the size passed to AddFont().
|
||||
|
|
@ -4364,6 +4364,11 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
|
|||
memset(TempKeychordName, 0, sizeof(TempKeychordName));
|
||||
}
|
||||
|
||||
ImGuiContext::~ImGuiContext()
|
||||
{
|
||||
IM_ASSERT(Initialized == false && "Forgot to call DestroyContext()?");
|
||||
}
|
||||
|
||||
void ImGui::Initialize()
|
||||
{
|
||||
ImGuiContext& g = *GImGui;
|
||||
|
|
@ -6117,9 +6122,9 @@ void ImGui::EndFrame()
|
|||
if (g.PlatformIO.Platform_SetImeDataFn != NULL && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0)
|
||||
{
|
||||
ImGuiViewport* viewport = FindViewportByID(ime_data->ViewportId);
|
||||
IMGUI_DEBUG_LOG_IO("[io] Calling Platform_SetImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y);
|
||||
if (viewport == NULL)
|
||||
viewport = GetMainViewport();
|
||||
IMGUI_DEBUG_LOG_IO("[io] Calling Platform_SetImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f) for Viewport 0x%08X\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y, viewport->ID);
|
||||
g.PlatformIO.Platform_SetImeDataFn(&g, viewport, ime_data);
|
||||
}
|
||||
g.WantTextInputNextFrame = ime_data->WantTextInput ? 1 : 0;
|
||||
|
|
@ -8310,7 +8315,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
|
|||
|
||||
// Setup draw list and outer clipping rectangle
|
||||
IM_ASSERT(window->DrawList->CmdBuffer.Size == 1 && window->DrawList->CmdBuffer[0].ElemCount == 0);
|
||||
window->DrawList->PushTexture(g.Font->ContainerAtlas->TexRef);
|
||||
window->DrawList->PushTexture(g.Font->OwnerAtlas->TexRef);
|
||||
PushClipRect(host_rect.Min, host_rect.Max, false);
|
||||
|
||||
// Child windows can render their decoration (bg color, border, scrollbars, etc.) within their parent to save a draw call (since 1.71)
|
||||
|
|
@ -9526,7 +9531,7 @@ void ImGui::SetCurrentFont(ImFont* font, float font_size_before_scaling, float f
|
|||
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||
IM_ASSERT(font->Scale > 0.0f);
|
||||
#endif
|
||||
ImFontAtlas* atlas = font->ContainerAtlas;
|
||||
ImFontAtlas* atlas = font->OwnerAtlas;
|
||||
g.DrawListSharedData.FontAtlas = atlas;
|
||||
g.DrawListSharedData.Font = font;
|
||||
ImFontAtlasUpdateDrawListsSharedData(atlas);
|
||||
|
|
@ -11794,7 +11799,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu
|
|||
// If we crash on a NULL g.NavWindow we need to fix the bug elsewhere.
|
||||
if (!(g.LastItemData.ItemFlags & ImGuiItemFlags_NoNav))
|
||||
{
|
||||
// FIMXE-NAV: investigate changing the window tests into a simple 'if (g.NavFocusScopeId == g.CurrentFocusScopeId)' test.
|
||||
// FIXME-NAV: investigate changing the window tests into a simple 'if (g.NavFocusScopeId == g.CurrentFocusScopeId)' test.
|
||||
window->DC.NavLayersActiveMaskNext |= (1 << window->DC.NavLayerCurrent);
|
||||
if (g.NavId == id || g.NavAnyRequest)
|
||||
if (g.NavWindow->RootWindowForNav == window->RootWindowForNav)
|
||||
|
|
@ -12576,7 +12581,7 @@ bool ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags ext
|
|||
}
|
||||
|
||||
SetNextWindowBgAlpha(g.Style.Colors[ImGuiCol_PopupBg].w * 0.60f);
|
||||
//PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkboard has issue with transparent colors :(
|
||||
//PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkerboard has issue with transparent colors :(
|
||||
tooltip_flags |= ImGuiTooltipFlags_OverridePrevious;
|
||||
}
|
||||
|
||||
|
|
@ -13135,10 +13140,10 @@ ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& s
|
|||
// Combo Box policy (we want a connecting edge)
|
||||
if (policy == ImGuiPopupPositionPolicy_ComboBox)
|
||||
{
|
||||
const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Down, ImGuiDir_Right, ImGuiDir_Left, ImGuiDir_Up };
|
||||
const ImGuiDir dir_preferred_order[ImGuiDir_COUNT] = { ImGuiDir_Down, ImGuiDir_Right, ImGuiDir_Left, ImGuiDir_Up };
|
||||
for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++)
|
||||
{
|
||||
const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n];
|
||||
const ImGuiDir dir = (n == -1) ? *last_dir : dir_preferred_order[n];
|
||||
if (n != -1 && dir == *last_dir) // Already tried this direction?
|
||||
continue;
|
||||
ImVec2 pos;
|
||||
|
|
@ -13157,10 +13162,10 @@ ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& s
|
|||
// (Always first try the direction we used on the last frame, if any)
|
||||
if (policy == ImGuiPopupPositionPolicy_Tooltip || policy == ImGuiPopupPositionPolicy_Default)
|
||||
{
|
||||
const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left };
|
||||
const ImGuiDir dir_preferred_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left };
|
||||
for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++)
|
||||
{
|
||||
const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n];
|
||||
const ImGuiDir dir = (n == -1) ? *last_dir : dir_preferred_order[n];
|
||||
if (n != -1 && dir == *last_dir) // Already tried this direction?
|
||||
continue;
|
||||
|
||||
|
|
@ -15394,7 +15399,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags)
|
|||
ret = BeginTooltipHidden();
|
||||
else
|
||||
ret = BeginTooltip();
|
||||
IM_ASSERT(ret); // FIXME-NEWBEGIN: If this ever becomes false, we need to Begin("##Hidden", NULL, ImGuiWindowFlags_NoSavedSettings) + SetWindowHiddendAndSkipItemsForCurrentFrame().
|
||||
IM_ASSERT(ret); // FIXME-NEWBEGIN: If this ever becomes false, we need to Begin("##Hidden", NULL, ImGuiWindowFlags_NoSavedSettings) + SetWindowHiddenAndSkipItemsForCurrentFrame().
|
||||
IM_UNUSED(ret);
|
||||
}
|
||||
|
||||
|
|
@ -22141,7 +22146,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
|
|||
|
||||
BulletText("Table 0x%08X (%d columns, in '%s')", table->ID, table->ColumnsCount, table->OuterWindow->Name);
|
||||
if (IsItemHovered())
|
||||
GetForegroundDrawList()->AddRect(table->OuterRect.Min - ImVec2(1, 1), table->OuterRect.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f);
|
||||
GetForegroundDrawList(table->OuterWindow)->AddRect(table->OuterRect.Min - ImVec2(1, 1), table->OuterRect.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f);
|
||||
Indent();
|
||||
char buf[128];
|
||||
for (int rect_n = 0; rect_n < TRT_Count; rect_n++)
|
||||
|
|
@ -22156,7 +22161,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
|
|||
ImFormatString(buf, IM_ARRAYSIZE(buf), "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) Col %d %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), column_n, trt_rects_names[rect_n]);
|
||||
Selectable(buf);
|
||||
if (IsItemHovered())
|
||||
GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f);
|
||||
GetForegroundDrawList(table->OuterWindow)->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -22165,7 +22170,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
|
|||
ImFormatString(buf, IM_ARRAYSIZE(buf), "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), trt_rects_names[rect_n]);
|
||||
Selectable(buf);
|
||||
if (IsItemHovered())
|
||||
GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f);
|
||||
GetForegroundDrawList(table->OuterWindow)->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f);
|
||||
}
|
||||
}
|
||||
Unindent();
|
||||
|
|
@ -22988,7 +22993,7 @@ void ImGui::DebugNodeFont(ImFont* font)
|
|||
{
|
||||
ImGuiContext& g = *GImGui;
|
||||
ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig;
|
||||
ImFontAtlas* atlas = font->ContainerAtlas;
|
||||
ImFontAtlas* atlas = font->OwnerAtlas;
|
||||
bool opened = TreeNode(font, "Font: \"%s\": %d sources(s)", font->GetDebugName(), font->Sources.Size);
|
||||
|
||||
// Display preview text
|
||||
|
|
@ -23103,7 +23108,7 @@ void ImGui::DebugNodeFont(ImFont* font)
|
|||
for (int baked_n = 0; baked_n < atlas->Builder->BakedPool.Size; baked_n++)
|
||||
{
|
||||
ImFontBaked* baked = &atlas->Builder->BakedPool[baked_n];
|
||||
if (baked->ContainerFont != font)
|
||||
if (baked->OwnerFont != font)
|
||||
continue;
|
||||
PushID(baked_n);
|
||||
if (TreeNode("Glyphs", "Baked at { %.2fpx, d.%.2f }: %d glyphs%s", baked->Size, baked->RasterizerDensity, baked->Glyphs.Size, (baked->LastUsedFrame < atlas->Builder->FrameCount - 1) ? " *Unused*" : ""))
|
||||
|
|
@ -23194,7 +23199,7 @@ void ImGui::DebugNodeFontGlyph(ImFont* font, const ImFontGlyph* glyph)
|
|||
Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1);
|
||||
if (glyph->PackId >= 0)
|
||||
{
|
||||
ImTextureRect* r = ImFontAtlasPackGetRect(font->ContainerAtlas, glyph->PackId);
|
||||
ImTextureRect* r = ImFontAtlasPackGetRect(font->OwnerAtlas, glyph->PackId);
|
||||
Text("PackId: 0x%X (%dx%d rect at %d,%d)", glyph->PackId, r->w, r->h, r->x, r->y);
|
||||
}
|
||||
Text("SourceIdx: %d", glyph->SourceIdx);
|
||||
|
|
@ -23233,7 +23238,7 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label)
|
|||
if (!is_active) { PopStyleColor(); }
|
||||
if (is_active && IsItemHovered())
|
||||
{
|
||||
ImDrawList* draw_list = GetForegroundDrawList();
|
||||
ImDrawList* draw_list = GetForegroundDrawList(tab_bar->Window);
|
||||
draw_list->AddRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max, IM_COL32(255, 255, 0, 255));
|
||||
draw_list->AddLine(ImVec2(tab_bar->ScrollingRectMinX, tab_bar->BarRect.Min.y), ImVec2(tab_bar->ScrollingRectMinX, tab_bar->BarRect.Max.y), IM_COL32(0, 255, 0, 255));
|
||||
draw_list->AddLine(ImVec2(tab_bar->ScrollingRectMaxX, tab_bar->BarRect.Min.y), ImVec2(tab_bar->ScrollingRectMaxX, tab_bar->BarRect.Max.y), IM_COL32(0, 255, 0, 255));
|
||||
|
|
|
|||
24
imgui.h
24
imgui.h
|
|
@ -338,7 +338,7 @@ IM_MSVC_RUNTIME_CHECKS_RESTORE
|
|||
// - You may decide to store a higher-level structure containing texture, sampler, shader etc. with various
|
||||
// constructors if you like. You will need to implement ==/!= operators.
|
||||
// History:
|
||||
// - In v1.91.4 (2024/10/08): the default type for ImTextureID was changed from 'void*' to 'ImU64'. This allowed backends requirig 64-bit worth of data to build on 32-bit architectures. Use intermediary intptr_t cast and read FAQ if you have casting warnings.
|
||||
// - In v1.91.4 (2024/10/08): the default type for ImTextureID was changed from 'void*' to 'ImU64'. This allowed backends requiring 64-bit worth of data to build on 32-bit architectures. Use intermediary intptr_t cast and read FAQ if you have casting warnings.
|
||||
// - In v1.92.0 (2025/06/11): added ImTextureRef which carry either a ImTextureID either a pointer to internal texture atlas. All user facing functions taking ImTextureID changed to ImTextureRef
|
||||
#ifndef ImTextureID
|
||||
typedef ImU64 ImTextureID; // Default: store up to 64-bits (any pointer or integer). A majority of backends are ok with that.
|
||||
|
|
@ -746,7 +746,7 @@ namespace ImGui
|
|||
// Widgets: Trees
|
||||
// - TreeNode functions return true when the node is open, in which case you need to also call TreePop() when you are finished displaying the tree node contents.
|
||||
IMGUI_API bool TreeNode(const char* label);
|
||||
IMGUI_API bool TreeNode(const char* str_id, const char* fmt, ...) IM_FMTARGS(2); // helper variation to easily decorelate the id from the displayed string. Read the FAQ about why and how to use ID. to align arbitrary text at the same level as a TreeNode() you can use Bullet().
|
||||
IMGUI_API bool TreeNode(const char* str_id, const char* fmt, ...) IM_FMTARGS(2); // helper variation to easily decorrelate the id from the displayed string. Read the FAQ about why and how to use ID. to align arbitrary text at the same level as a TreeNode() you can use Bullet().
|
||||
IMGUI_API bool TreeNode(const void* ptr_id, const char* fmt, ...) IM_FMTARGS(2); // "
|
||||
IMGUI_API bool TreeNodeV(const char* str_id, const char* fmt, va_list args) IM_FMTLIST(2);
|
||||
IMGUI_API bool TreeNodeV(const void* ptr_id, const char* fmt, va_list args) IM_FMTLIST(2);
|
||||
|
|
@ -1320,7 +1320,7 @@ enum ImGuiInputTextFlags_
|
|||
// - It is much slower than regular text fields.
|
||||
// Ballpark estimate of cost on my 2019 desktop PC: for a 100 KB text buffer: +~0.3 ms (Optimized) / +~1.0 ms (Debug build).
|
||||
// The CPU cost is very roughly proportional to text length, so a 10 KB buffer should cost about ten times less.
|
||||
ImGuiInputTextFlags_WordWrap = 1 << 24, // InputTextMultine(): word-wrap lines that are too long.
|
||||
ImGuiInputTextFlags_WordWrap = 1 << 24, // InputTextMultiline(): word-wrap lines that are too long.
|
||||
|
||||
// Obsolete names
|
||||
//ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior
|
||||
|
|
@ -1347,7 +1347,7 @@ enum ImGuiTreeNodeFlags_
|
|||
ImGuiTreeNodeFlags_SpanAllColumns = 1 << 14, // Frame will span all columns of its container table (label will still fit in current column)
|
||||
ImGuiTreeNodeFlags_LabelSpanAllColumns = 1 << 15, // Label will span all columns of its container table
|
||||
//ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 16, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible
|
||||
ImGuiTreeNodeFlags_NavLeftJumpsToParent = 1 << 17, // Nav: left arrow moves back to parent. This is processed in TreePop() when there's an unfullfilled Left nav request remaining.
|
||||
ImGuiTreeNodeFlags_NavLeftJumpsToParent = 1 << 17, // Nav: left arrow moves back to parent. This is processed in TreePop() when there's an unfulfilled Left nav request remaining.
|
||||
ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog,
|
||||
|
||||
// [EXPERIMENTAL] Draw lines connecting TreeNode hierarchy. Discuss in GitHub issue #2920.
|
||||
|
|
@ -2374,7 +2374,7 @@ struct ImGuiStyle
|
|||
float ColumnsMinSpacing; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1).
|
||||
float ScrollbarSize; // Width of the vertical scrollbar, Height of the horizontal scrollbar.
|
||||
float ScrollbarRounding; // Radius of grab corners for scrollbar.
|
||||
float ScrollbarPadding; // Padding of scrollbar grab within its frame (same for both axises).
|
||||
float ScrollbarPadding; // Padding of scrollbar grab within its frame (same for both axes).
|
||||
float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar.
|
||||
float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
|
||||
float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
|
||||
|
|
@ -3538,7 +3538,7 @@ struct ImDrawData
|
|||
ImVec2 DisplaySize; // Size of the viewport to render (== GetMainViewport()->Size for the main viewport, == io.DisplaySize in most single-viewport applications)
|
||||
ImVec2 FramebufferScale; // Amount of pixels for each unit of DisplaySize. Copied from viewport->FramebufferScale (== io.DisplayFramebufferScale for main viewport). Generally (1,1) on normal display, (2,2) on OSX with Retina display.
|
||||
ImGuiViewport* OwnerViewport; // Viewport carrying the ImDrawData instance, might be of use to the renderer (generally not).
|
||||
ImVector<ImTextureData*>* Textures; // List of textures to update. Most of the times the list is shared by all ImDrawData, has only 1 texture and it doesn't need any update. This almost always points to ImGui::GetPlatformIO().Textures[]. May be overriden or set to NULL if you want to manually update textures.
|
||||
ImVector<ImTextureData*>* Textures; // List of textures to update. Most of the times the list is shared by all ImDrawData, has only 1 texture and it doesn't need any update. This almost always points to ImGui::GetPlatformIO().Textures[]. May be overridden or set to NULL if you want to manually update textures.
|
||||
|
||||
// Functions
|
||||
ImDrawData() { Clear(); }
|
||||
|
|
@ -3642,7 +3642,7 @@ struct ImFontConfig
|
|||
char Name[40]; // <auto> // Name (strictly to ease debugging, hence limited size buffer)
|
||||
void* FontData; // // TTF/OTF data
|
||||
int FontDataSize; // // TTF/OTF data size
|
||||
bool FontDataOwnedByAtlas; // true // TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself).
|
||||
bool FontDataOwnedByAtlas; // true // TTF/OTF data ownership taken by the owner ImFontAtlas (will delete memory itself).
|
||||
|
||||
// Options
|
||||
bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights.
|
||||
|
|
@ -3921,7 +3921,7 @@ struct ImFontBaked
|
|||
unsigned int LoadNoRenderOnLayout:1;// 0 // // Enable a two-steps mode where CalcTextSize() calls will load AdvanceX *without* rendering/packing glyphs. Only advantagous if you know that the glyph is unlikely to actually be rendered, otherwise it is slower because we'd do one query on the first CalcTextSize and one query on the first Draw.
|
||||
int LastUsedFrame; // 4 // // Record of that time this was bounds
|
||||
ImGuiID BakedId; // 4 // // Unique ID for this baked storage
|
||||
ImFont* ContainerFont; // 4-8 // in // Parent font
|
||||
ImFont* OwnerFont; // 4-8 // in // Parent font
|
||||
void* FontLoaderDatas; // 4-8 // // Font loader opaque storage (per baked font * sources): single contiguous buffer allocated by imgui, passed to loader.
|
||||
|
||||
// Functions
|
||||
|
|
@ -3952,7 +3952,7 @@ struct ImFont
|
|||
{
|
||||
// [Internal] Members: Hot ~12-20 bytes
|
||||
ImFontBaked* LastBaked; // 4-8 // Cache last bound baked. NEVER USE DIRECTLY. Use GetFontBaked().
|
||||
ImFontAtlas* ContainerAtlas; // 4-8 // What we have been loaded into.
|
||||
ImFontAtlas* OwnerAtlas; // 4-8 // What we have been loaded into.
|
||||
ImFontFlags Flags; // 4 // Font flags.
|
||||
float CurrentRasterizerDensity; // Current rasterizer density. This is a varying state of the font.
|
||||
|
||||
|
|
@ -3960,7 +3960,7 @@ struct ImFont
|
|||
// Conceptually Sources[] is the list of font sources merged to create this font.
|
||||
ImGuiID FontId; // Unique identifier for the font
|
||||
float LegacySize; // 4 // in // Font size passed to AddFont(). Use for old code calling PushFont() expecting to use that size. (use ImGui::GetFontBaked() to get font baked at current bound size).
|
||||
ImVector<ImFontConfig*> Sources; // 16 // in // List of sources. Pointers within ContainerAtlas->Sources[]
|
||||
ImVector<ImFontConfig*> Sources; // 16 // in // List of sources. Pointers within OwnerAtlas->Sources[]
|
||||
ImWchar EllipsisChar; // 2-4 // out // Character used for ellipsis rendering ('...').
|
||||
ImWchar FallbackChar; // 2-4 // out // Character used if a glyph isn't found (U+FFFD, '?')
|
||||
ImU8 Used8kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/8192/8]; // 1 bytes if ImWchar=ImWchar16, 16 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations across all used codepoints.
|
||||
|
|
@ -3974,7 +3974,7 @@ struct ImFont
|
|||
IMGUI_API ImFont();
|
||||
IMGUI_API ~ImFont();
|
||||
IMGUI_API bool IsGlyphInFont(ImWchar c);
|
||||
bool IsLoaded() const { return ContainerAtlas != NULL; }
|
||||
bool IsLoaded() const { return OwnerAtlas != NULL; }
|
||||
const char* GetDebugName() const { return Sources.Size ? Sources[0]->Name : "<unknown>"; } // Fill ImFontConfig::Name.
|
||||
|
||||
// [Internal] Don't use!
|
||||
|
|
@ -4401,7 +4401,7 @@ typedef ImFontAtlasRect ImFontAtlasCustomRect;
|
|||
//};
|
||||
|
||||
// RENAMED and MERGED both ImGuiKey_ModXXX and ImGuiModFlags_XXX into ImGuiMod_XXX (from September 2022)
|
||||
// RENAMED ImGuiKeyModFlags -> ImGuiModFlags in 1.88 (from April 2022). Exceptionally commented out ahead of obscolescence schedule to reduce confusion and because they were not meant to be used in the first place.
|
||||
// RENAMED ImGuiKeyModFlags -> ImGuiModFlags in 1.88 (from April 2022). Exceptionally commented out ahead of obsolescence schedule to reduce confusion and because they were not meant to be used in the first place.
|
||||
//typedef ImGuiKeyChord ImGuiModFlags; // == int. We generally use ImGuiKeyChord to mean "a ImGuiKey or-ed with any number of ImGuiMod_XXX value", so you may store mods in there.
|
||||
//enum ImGuiModFlags_ { ImGuiModFlags_None = 0, ImGuiModFlags_Ctrl = ImGuiMod_Ctrl, ImGuiModFlags_Shift = ImGuiMod_Shift, ImGuiModFlags_Alt = ImGuiMod_Alt, ImGuiModFlags_Super = ImGuiMod_Super };
|
||||
//typedef ImGuiKeyChord ImGuiKeyModFlags; // == int
|
||||
|
|
|
|||
|
|
@ -7115,7 +7115,7 @@ static void DemoWindowTables()
|
|||
// [2.3] Right-click in columns to open another custom popup
|
||||
HelpMarker(
|
||||
"Demonstrate mixing table context menu (over header), item context button (over button) "
|
||||
"and custom per-colunm context menu (over column body).");
|
||||
"and custom per-column context menu (over column body).");
|
||||
ImGuiTableFlags flags2 = ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders;
|
||||
if (ImGui::BeginTable("table_context_menu_2", COLUMNS_COUNT, flags2))
|
||||
{
|
||||
|
|
@ -8284,7 +8284,7 @@ void ImGui::ShowAboutWindow(bool* p_open)
|
|||
// - 16 is > strlen("((void)(_EXPR))") which we suggested in our imconfig.h template as a possible way to disable.
|
||||
int assert_runs_expression = 0;
|
||||
IM_ASSERT(++assert_runs_expression);
|
||||
int assert_expand_len = (int)strlen(IM_STRINGIFY(IM_ASSERT(true)));
|
||||
int assert_expand_len = (int)strlen(IM_STRINGIFY((IM_ASSERT(true))));
|
||||
bool assert_maybe_disabled = (!assert_runs_expression || assert_expand_len <= 16);
|
||||
ImGui::Text("IM_ASSERT: runs expression: %s. expand size: %s%s",
|
||||
assert_runs_expression ? "OK" : "KO", (assert_expand_len > 16) ? "OK" : "KO", assert_maybe_disabled ? " (MAYBE DISABLED?!)" : "");
|
||||
|
|
|
|||
|
|
@ -830,7 +830,7 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32
|
|||
const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX) && (fractional_thickness <= 0.00001f) && (AA_SIZE == 1.0f);
|
||||
|
||||
// We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTex unless ImFontAtlasFlags_NoBakedLines is off
|
||||
IM_ASSERT_PARANOID(!use_texture || !(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines));
|
||||
IM_ASSERT_PARANOID(!use_texture || !(_Data->Font->OwnerAtlas->Flags & ImFontAtlasFlags_NoBakedLines));
|
||||
|
||||
const int idx_count = use_texture ? (count * 6) : (thick_line ? count * 18 : count * 12);
|
||||
const int vtx_count = use_texture ? (points_count * 2) : (thick_line ? points_count * 4 : points_count * 3);
|
||||
|
|
@ -3248,7 +3248,7 @@ void ImFontAtlas::RemoveFont(ImFont* font)
|
|||
|
||||
ImFontAtlasBuildUpdatePointers(this);
|
||||
|
||||
font->ContainerAtlas = NULL;
|
||||
font->OwnerAtlas = NULL;
|
||||
IM_DELETE(font);
|
||||
|
||||
// Notify external systems
|
||||
|
|
@ -3626,7 +3626,7 @@ void ImFontAtlasFontSourceAddToFont(ImFontAtlas* atlas, ImFont* font, ImFontConf
|
|||
{
|
||||
font->ClearOutputData();
|
||||
//font->FontSize = src->SizePixels;
|
||||
font->ContainerAtlas = atlas;
|
||||
font->OwnerAtlas = atlas;
|
||||
IM_ASSERT(font->Sources[0] == src);
|
||||
}
|
||||
atlas->TexIsBuilt = false; // For legacy backends
|
||||
|
|
@ -3649,7 +3649,7 @@ void ImFontAtlasFontDestroySourceData(ImFontAtlas* atlas, ImFontConfig* src)
|
|||
// FIXME-NEWATLAS: This borrows too much from FontLoader's FontLoadGlyph() handlers and suggest that we should add further helpers.
|
||||
static ImFontGlyph* ImFontAtlasBuildSetupFontBakedEllipsis(ImFontAtlas* atlas, ImFontBaked* baked)
|
||||
{
|
||||
ImFont* font = baked->ContainerFont;
|
||||
ImFont* font = baked->OwnerFont;
|
||||
IM_ASSERT(font->EllipsisChar != 0);
|
||||
|
||||
const ImFontGlyph* dot_glyph = baked->FindGlyphNoFallback((ImWchar)'.');
|
||||
|
|
@ -3695,7 +3695,7 @@ static void ImFontAtlasBuildSetupFontBakedFallback(ImFontBaked* baked)
|
|||
{
|
||||
IM_ASSERT(baked->FallbackGlyphIndex == -1);
|
||||
IM_ASSERT(baked->FallbackAdvanceX == 0.0f);
|
||||
ImFont* font = baked->ContainerFont;
|
||||
ImFont* font = baked->OwnerFont;
|
||||
ImFontGlyph* fallback_glyph = NULL;
|
||||
if (font->FallbackChar != 0)
|
||||
fallback_glyph = baked->FindGlyphNoFallback(font->FallbackChar);
|
||||
|
|
@ -3705,7 +3705,7 @@ static void ImFontAtlasBuildSetupFontBakedFallback(ImFontBaked* baked)
|
|||
ImFontGlyph glyph;
|
||||
glyph.Codepoint = 0;
|
||||
glyph.AdvanceX = space_glyph ? space_glyph->AdvanceX : IM_ROUND(baked->Size * 0.40f);
|
||||
fallback_glyph = ImFontAtlasBakedAddFontGlyph(font->ContainerAtlas, baked, NULL, &glyph);
|
||||
fallback_glyph = ImFontAtlasBakedAddFontGlyph(font->OwnerAtlas, baked, NULL, &glyph);
|
||||
}
|
||||
baked->FallbackGlyphIndex = baked->Glyphs.index_from_ptr(fallback_glyph); // Storing index avoid need to update pointer on growth and simplify inner loop code
|
||||
baked->FallbackAdvanceX = fallback_glyph->AdvanceX;
|
||||
|
|
@ -3786,7 +3786,7 @@ ImFontBaked* ImFontAtlasBakedAdd(ImFontAtlas* atlas, ImFont* font, float font_si
|
|||
baked->Size = font_size;
|
||||
baked->RasterizerDensity = font_rasterizer_density;
|
||||
baked->BakedId = baked_id;
|
||||
baked->ContainerFont = font;
|
||||
baked->OwnerFont = font;
|
||||
baked->LastUsedFrame = atlas->Builder->FrameCount;
|
||||
|
||||
// Initialize backend data
|
||||
|
|
@ -3821,7 +3821,7 @@ ImFontBaked* ImFontAtlasBakedGetClosestMatch(ImFontAtlas* atlas, ImFont* font, f
|
|||
for (int baked_n = 0; baked_n < builder->BakedPool.Size; baked_n++)
|
||||
{
|
||||
ImFontBaked* baked = &builder->BakedPool[baked_n];
|
||||
if (baked->ContainerFont != font || baked->WantDestroy)
|
||||
if (baked->OwnerFont != font || baked->WantDestroy)
|
||||
continue;
|
||||
if (step_n == 0 && baked->RasterizerDensity != font_rasterizer_density) // First try with same density
|
||||
continue;
|
||||
|
|
@ -3877,7 +3877,7 @@ void ImFontAtlasFontDiscardBakes(ImFontAtlas* atlas, ImFont* font, int unused_fr
|
|||
ImFontBaked* baked = &builder->BakedPool[baked_n];
|
||||
if (baked->LastUsedFrame + unused_frames > atlas->Builder->FrameCount)
|
||||
continue;
|
||||
if (baked->ContainerFont != font || baked->WantDestroy)
|
||||
if (baked->OwnerFont != font || baked->WantDestroy)
|
||||
continue;
|
||||
ImFontAtlasBakedDiscard(atlas, font, baked);
|
||||
}
|
||||
|
|
@ -3892,9 +3892,9 @@ void ImFontAtlasBuildDiscardBakes(ImFontAtlas* atlas, int unused_frames)
|
|||
ImFontBaked* baked = &builder->BakedPool[baked_n];
|
||||
if (baked->LastUsedFrame + unused_frames > atlas->Builder->FrameCount)
|
||||
continue;
|
||||
if (baked->WantDestroy || (baked->ContainerFont->Flags & ImFontFlags_LockBakedSizes))
|
||||
if (baked->WantDestroy || (baked->OwnerFont->Flags & ImFontFlags_LockBakedSizes))
|
||||
continue;
|
||||
ImFontAtlasBakedDiscard(atlas, baked->ContainerFont, baked);
|
||||
ImFontAtlasBakedDiscard(atlas, baked->OwnerFont, baked);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -3915,6 +3915,11 @@ void ImFontAtlasRemoveDrawListSharedData(ImFontAtlas* atlas, ImDrawListSharedDat
|
|||
void ImFontAtlasUpdateDrawListsTextures(ImFontAtlas* atlas, ImTextureRef old_tex, ImTextureRef new_tex)
|
||||
{
|
||||
for (ImDrawListSharedData* shared_data : atlas->DrawListSharedDatas)
|
||||
{
|
||||
// If Context 2 uses font owned by Context 1 which already called EndFrame()/Render(), we don't want to mess with draw commands for Context 1
|
||||
if (shared_data->Context && !shared_data->Context->WithinFrameScope)
|
||||
continue;
|
||||
|
||||
for (ImDrawList* draw_list : shared_data->DrawLists)
|
||||
{
|
||||
// Replace in command-buffer
|
||||
|
|
@ -3928,6 +3933,7 @@ void ImFontAtlasUpdateDrawListsTextures(ImFontAtlas* atlas, ImTextureRef old_tex
|
|||
if (stacked_tex == old_tex)
|
||||
stacked_tex = new_tex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update texture coordinates in all draw list shared context
|
||||
|
|
@ -4427,8 +4433,8 @@ static void ImFontAtlas_FontHookRemapCodepoint(ImFontAtlas* atlas, ImFont* font,
|
|||
|
||||
static ImFontGlyph* ImFontBaked_BuildLoadGlyph(ImFontBaked* baked, ImWchar codepoint, float* only_load_advance_x)
|
||||
{
|
||||
ImFont* font = baked->ContainerFont;
|
||||
ImFontAtlas* atlas = font->ContainerAtlas;
|
||||
ImFont* font = baked->OwnerFont;
|
||||
ImFontAtlas* atlas = font->OwnerAtlas;
|
||||
if (atlas->Locked || (font->Flags & ImFontFlags_NoLoadGlyphs))
|
||||
{
|
||||
// Lazily load fallback glyph
|
||||
|
|
@ -4702,7 +4708,7 @@ static bool ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontC
|
|||
stbtt_MakeGlyphBitmapSubpixelPrefilter(&bd_font_data->FontInfo, bitmap_pixels, w, h, w,
|
||||
scale_for_raster_x, scale_for_raster_y, 0, 0, oversample_h, oversample_v, &sub_x, &sub_y, glyph_index);
|
||||
|
||||
const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
|
||||
const float ref_size = baked->OwnerFont->Sources[0]->SizePixels;
|
||||
const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
|
||||
float font_off_x = (src->GlyphOffset.x * offsets_scale);
|
||||
float font_off_y = (src->GlyphOffset.y * offsets_scale);
|
||||
|
|
@ -5099,7 +5105,7 @@ ImFont::~ImFont()
|
|||
|
||||
void ImFont::ClearOutputData()
|
||||
{
|
||||
if (ImFontAtlas* atlas = ContainerAtlas)
|
||||
if (ImFontAtlas* atlas = OwnerAtlas)
|
||||
ImFontAtlasFontDiscardBakes(atlas, this, 0);
|
||||
FallbackChar = EllipsisChar = 0;
|
||||
memset(Used8kPagesMap, 0, sizeof(Used8kPagesMap));
|
||||
|
|
@ -5144,7 +5150,7 @@ ImFontGlyph* ImFontAtlasBakedAddFontGlyph(ImFontAtlas* atlas, ImFontBaked* baked
|
|||
if (src != NULL)
|
||||
{
|
||||
// Clamp & recenter if needed
|
||||
const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
|
||||
const float ref_size = baked->OwnerFont->Sources[0]->SizePixels;
|
||||
const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
|
||||
float advance_x = ImClamp(glyph->AdvanceX, src->GlyphMinAdvanceX * offsets_scale, src->GlyphMaxAdvanceX * offsets_scale);
|
||||
if (advance_x != glyph->AdvanceX)
|
||||
|
|
@ -5170,7 +5176,7 @@ ImFontGlyph* ImFontAtlasBakedAddFontGlyph(ImFontAtlas* atlas, ImFontBaked* baked
|
|||
baked->IndexAdvanceX[codepoint] = glyph->AdvanceX;
|
||||
baked->IndexLookup[codepoint] = (ImU16)glyph_idx;
|
||||
const int page_n = codepoint / 8192;
|
||||
baked->ContainerFont->Used8kPagesMap[page_n >> 3] |= 1 << (page_n & 7);
|
||||
baked->OwnerFont->Used8kPagesMap[page_n >> 3] |= 1 << (page_n & 7);
|
||||
|
||||
return glyph;
|
||||
}
|
||||
|
|
@ -5182,7 +5188,7 @@ void ImFontAtlasBakedAddFontGlyphAdvancedX(ImFontAtlas* atlas, ImFontBaked* bake
|
|||
if (src != NULL)
|
||||
{
|
||||
// Clamp & recenter if needed
|
||||
const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
|
||||
const float ref_size = baked->OwnerFont->Sources[0]->SizePixels;
|
||||
const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
|
||||
advance_x = ImClamp(advance_x, src->GlyphMinAdvanceX * offsets_scale, src->GlyphMaxAdvanceX * offsets_scale);
|
||||
|
||||
|
|
@ -5204,7 +5210,7 @@ void ImFontAtlasBakedSetFontGlyphBitmap(ImFontAtlas* atlas, ImFontBaked* baked,
|
|||
ImTextureData* tex = atlas->TexData;
|
||||
IM_ASSERT(r->x + r->w <= tex->Width && r->y + r->h <= tex->Height);
|
||||
ImFontAtlasTextureBlockConvert(src_pixels, src_fmt, src_pitch, (unsigned char*)tex->GetPixelsAt(r->x, r->y), tex->Format, tex->GetPitch(), r->w, r->h);
|
||||
ImFontAtlasPostProcessData pp_data = { atlas, baked->ContainerFont, src, baked, glyph, tex->GetPixelsAt(r->x, r->y), tex->Format, tex->GetPitch(), r->w, r->h };
|
||||
ImFontAtlasPostProcessData pp_data = { atlas, baked->OwnerFont, src, baked, glyph, tex->GetPixelsAt(r->x, r->y), tex->Format, tex->GetPitch(), r->w, r->h };
|
||||
ImFontAtlasTextureBlockPostProcess(&pp_data);
|
||||
ImFontAtlasTextureBlockQueueUpload(atlas, tex, r->x, r->y, r->w, r->h);
|
||||
}
|
||||
|
|
@ -5262,7 +5268,7 @@ bool ImFontBaked::IsGlyphLoaded(ImWchar c)
|
|||
// This is not fast query
|
||||
bool ImFont::IsGlyphInFont(ImWchar c)
|
||||
{
|
||||
ImFontAtlas* atlas = ContainerAtlas;
|
||||
ImFontAtlas* atlas = OwnerAtlas;
|
||||
ImFontAtlas_FontHookRemapCodepoint(atlas, this, &c);
|
||||
for (ImFontConfig* src : Sources)
|
||||
{
|
||||
|
|
@ -5311,7 +5317,7 @@ ImFontBaked* ImFont::GetFontBaked(float size, float density)
|
|||
if (baked && baked->Size == size && baked->RasterizerDensity == density)
|
||||
return baked;
|
||||
|
||||
ImFontAtlas* atlas = ContainerAtlas;
|
||||
ImFontAtlas* atlas = OwnerAtlas;
|
||||
ImFontAtlasBuilder* builder = atlas->Builder;
|
||||
baked = ImFontAtlasBakedGetOrAdd(atlas, this, size, density);
|
||||
if (baked == NULL)
|
||||
|
|
@ -5332,7 +5338,7 @@ ImFontBaked* ImFontAtlasBakedGetOrAdd(ImFontAtlas* atlas, ImFont* font, float fo
|
|||
ImFontBaked* baked = *p_baked_in_map;
|
||||
if (baked != NULL)
|
||||
{
|
||||
IM_ASSERT(baked->Size == font_size && baked->ContainerFont == font && baked->BakedId == baked_id);
|
||||
IM_ASSERT(baked->Size == font_size && baked->OwnerFont == font && baked->BakedId == baked_id);
|
||||
return baked;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2754,6 +2754,7 @@ struct ImGuiContext
|
|||
char TempKeychordName[64];
|
||||
|
||||
ImGuiContext(ImFontAtlas* shared_font_atlas);
|
||||
~ImGuiContext();
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -2929,7 +2930,7 @@ struct IMGUI_API ImGuiWindow
|
|||
ImGuiWindow* RootWindowDockTree; // Point to ourself or first ancestor that is not a child window. Cross through dock nodes.
|
||||
ImGuiWindow* RootWindowForTitleBarHighlight; // Point to ourself or first ancestor which will display TitleBgActive color when this window is active.
|
||||
ImGuiWindow* RootWindowForNav; // Point to ourself or first ancestor which doesn't have the NavFlattened flag.
|
||||
ImGuiWindow* ParentWindowForFocusRoute; // Set to manual link a window to its logical parent so that Shortcut() chain are honoerd (e.g. Tool linked to Document)
|
||||
ImGuiWindow* ParentWindowForFocusRoute; // Set to manual link a window to its logical parent so that Shortcut() chain are honored (e.g. Tool linked to Document)
|
||||
|
||||
ImGuiWindow* NavLastChildNavWindow; // When going to the menu bar, we remember the child window we came from. (This could probably be made implicit if we kept g.Windows sorted by last focused including child window.)
|
||||
ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1)
|
||||
|
|
@ -3262,7 +3263,7 @@ struct IMGUI_API ImGuiTable
|
|||
bool IsContextPopupOpen; // Set when default context menu is open (also see: ContextPopupColumn, InstanceInteracted).
|
||||
bool DisableDefaultContextMenu; // Disable default context menu. You may submit your own using TableBeginContextMenuPopup()/EndPopup()
|
||||
bool IsSettingsRequestLoad;
|
||||
bool IsSettingsDirty; // Set when table settings have changed and needs to be reported into ImGuiTableSetttings data.
|
||||
bool IsSettingsDirty; // Set when table settings have changed and needs to be reported into ImGuiTableSettings data.
|
||||
bool IsDefaultDisplayOrder; // Set when display order is unchanged from default (DisplayOrder contains 0...Count-1)
|
||||
bool IsResetAllRequest;
|
||||
bool IsResetDisplayOrderRequest;
|
||||
|
|
@ -3626,7 +3627,7 @@ namespace ImGui
|
|||
// Legacy functions use ImGuiKeyOwner_Any meaning that they typically ignore ownership, unless a call to SetKeyOwner() explicitly used ImGuiInputFlags_LockThisFrame or ImGuiInputFlags_LockUntilRelease.
|
||||
// - Binding generators may want to ignore those for now, or suffix them with Ex() until we decide if this gets moved into public API.
|
||||
IMGUI_API bool IsKeyDown(ImGuiKey key, ImGuiID owner_id);
|
||||
IMGUI_API bool IsKeyPressed(ImGuiKey key, ImGuiInputFlags flags, ImGuiID owner_id = 0); // Important: when transitioning from old to new IsKeyPressed(): old API has "bool repeat = true", so would default to repeat. New API requiress explicit ImGuiInputFlags_Repeat.
|
||||
IMGUI_API bool IsKeyPressed(ImGuiKey key, ImGuiInputFlags flags, ImGuiID owner_id = 0); // Important: when transitioning from old to new IsKeyPressed(): old API has "bool repeat = true", so would default to repeat. New API requires explicit ImGuiInputFlags_Repeat.
|
||||
IMGUI_API bool IsKeyReleased(ImGuiKey key, ImGuiID owner_id);
|
||||
IMGUI_API bool IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID owner_id = 0);
|
||||
IMGUI_API bool IsMouseDown(ImGuiMouseButton button, ImGuiID owner_id);
|
||||
|
|
|
|||
|
|
@ -451,7 +451,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
|
|||
// But at this point we do NOT have a correct value for .Max.y (unless a height has been explicitly passed in). It will only be updated in EndTable().
|
||||
table->WorkRect = table->OuterRect = table->InnerRect = outer_rect;
|
||||
table->HasScrollbarYPrev = table->HasScrollbarYCurr = false;
|
||||
table->InnerWindow->DC.TreeDepth++; // This is designed to always linking ImGuiTreeNodeFlags_DrawLines linking accross a table
|
||||
table->InnerWindow->DC.TreeDepth++; // This is designed to always linking ImGuiTreeNodeFlags_DrawLines linking across a table
|
||||
}
|
||||
|
||||
// Push a standardized ID for both child-using and not-child-using tables
|
||||
|
|
@ -947,7 +947,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
|
|||
// (e.g. TextWrapped) too much. Otherwise what tends to happen is that TextWrapped would output a very
|
||||
// large height (= first frame scrollbar display very off + clipper would skip lots of items).
|
||||
// This is merely making the side-effect less extreme, but doesn't properly fixes it.
|
||||
// FIXME: Move this to ->WidthGiven to avoid temporary lossyless?
|
||||
// FIXME: Move this to ->WidthGiven to avoid temporary lossyness?
|
||||
// FIXME: This break IsPreserveWidthAuto from not flickering if the stored WidthAuto was smaller.
|
||||
if (column->AutoFitQueue > 0x01 && table->IsInitializing && !column->IsPreserveWidthAuto)
|
||||
column->WidthRequest = ImMax(column->WidthRequest, table->MinColumnWidth * 4.0f); // FIXME-TABLE: Another constant/scale?
|
||||
|
|
@ -1191,7 +1191,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
|
|||
}
|
||||
|
||||
// In case the table is visible (e.g. decorations) but all columns clipped, we keep a column visible.
|
||||
// Else if give no chance to a clipper-savy user to submit rows and therefore total contents height used by scrollbar.
|
||||
// Else if give no chance to a clipper-savvy user to submit rows and therefore total contents height used by scrollbar.
|
||||
if (has_at_least_one_column_requesting_output == false)
|
||||
{
|
||||
table->Columns[table->LeftMostEnabledColumn].IsRequestOutput = true;
|
||||
|
|
@ -2526,7 +2526,7 @@ void ImGui::TablePopColumnChannel()
|
|||
// - NoClip --> 2+D+1 channels: bg0/1 + bg2 + foreground (same clip rect == always 1 draw call)
|
||||
// - Clip --> 2+D+N channels
|
||||
// - FreezeRows --> 2+D+N*2 (unless scrolling value is zero)
|
||||
// - FreezeRows || FreezeColunns --> 3+D+N*2 (unless scrolling value is zero)
|
||||
// - FreezeRows || FreezeColumns --> 3+D+N*2 (unless scrolling value is zero)
|
||||
// Where D is 1 if any column is clipped or hidden (dummy channel) otherwise 0.
|
||||
void ImGui::TableSetupDrawChannels(ImGuiTable* table)
|
||||
{
|
||||
|
|
@ -3421,7 +3421,7 @@ void ImGui::TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label
|
|||
|
||||
// Left<>Right alignment
|
||||
float line_off_curr_x = flip_label ? (label_lines - 1) * line_off_step_x : 0.0f;
|
||||
float line_off_for_align_x = ImMax((((column->MaxX - column->MinX) - padding.x * 2.0f) - (label_lines * line_off_step_x)), 0.0f) * align.x;
|
||||
float line_off_for_align_x = ImFloor(ImMax((((column->MaxX - column->MinX) - padding.x * 2.0f) - (label_lines * line_off_step_x)), 0.0f) * align.x);
|
||||
line_off_curr_x += line_off_for_align_x - line_off_for_ascent_x;
|
||||
|
||||
// Register header width
|
||||
|
|
@ -4028,9 +4028,9 @@ void ImGui::DebugNodeTable(ImGuiTable* table)
|
|||
bool open = TreeNode(table, "Table 0x%08X (%d columns, in '%s')%s", table->ID, table->ColumnsCount, table->OuterWindow->Name, is_active ? "" : " *Inactive*");
|
||||
if (!is_active) { PopStyleColor(); }
|
||||
if (IsItemHovered())
|
||||
GetForegroundDrawList()->AddRect(table->OuterRect.Min, table->OuterRect.Max, IM_COL32(255, 255, 0, 255));
|
||||
GetForegroundDrawList(table->OuterWindow)->AddRect(table->OuterRect.Min, table->OuterRect.Max, IM_COL32(255, 255, 0, 255));
|
||||
if (IsItemVisible() && table->HoveredColumnBody != -1)
|
||||
GetForegroundDrawList()->AddRect(GetItemRectMin(), GetItemRectMax(), IM_COL32(255, 255, 0, 255));
|
||||
GetForegroundDrawList(table->OuterWindow)->AddRect(GetItemRectMin(), GetItemRectMax(), IM_COL32(255, 255, 0, 255));
|
||||
if (!open)
|
||||
return;
|
||||
if (table->InstanceCurrent > 0)
|
||||
|
|
@ -4084,7 +4084,7 @@ void ImGui::DebugNodeTable(ImGuiTable* table)
|
|||
if (IsItemHovered())
|
||||
{
|
||||
ImRect r(column->MinX, table->OuterRect.Min.y, column->MaxX, table->OuterRect.Max.y);
|
||||
GetForegroundDrawList()->AddRect(r.Min, r.Max, IM_COL32(255, 255, 0, 255));
|
||||
GetForegroundDrawList(table->OuterWindow)->AddRect(r.Min, r.Max, IM_COL32(255, 255, 0, 255));
|
||||
}
|
||||
}
|
||||
if (ImGuiTableSettings* settings = TableGetBoundSettings(table))
|
||||
|
|
|
|||
|
|
@ -9,5 +9,5 @@ imgui_scoped.h
|
|||
Try by merging: https://github.com/ocornut/imgui/pull/2197
|
||||
Discuss at: https://github.com/ocornut/imgui/issues/2096
|
||||
|
||||
See more C++ related extension (fmt, RAII, syntaxis sugar) on Wiki:
|
||||
See more C++ related extension (fmt, RAII, syntactic sugar) on Wiki:
|
||||
https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
// ImGui::InputText("my string", &my_string);
|
||||
// }
|
||||
|
||||
// See more C++ related extension (fmt, RAII, syntaxis sugar) on Wiki:
|
||||
// See more C++ related extension (fmt, RAII, syntactic sugar) on Wiki:
|
||||
// https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness
|
||||
|
||||
#include "imgui.h"
|
||||
|
|
|
|||
|
|
@ -423,7 +423,7 @@ static bool ImGui_ImplFreeType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* s
|
|||
IM_UNUSED(atlas);
|
||||
float size = baked->Size;
|
||||
if (src->MergeMode && src->SizePixels != 0.0f)
|
||||
size *= (src->SizePixels / baked->ContainerFont->Sources[0]->SizePixels);
|
||||
size *= (src->SizePixels / baked->OwnerFont->Sources[0]->SizePixels);
|
||||
|
||||
ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData;
|
||||
bd_font_data->BakedLastActivated = baked;
|
||||
|
|
@ -539,7 +539,7 @@ static bool ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConf
|
|||
uint32_t* temp_buffer = (uint32_t*)atlas->Builder->TempBuffer.Data;
|
||||
ImGui_ImplFreeType_BlitGlyph(ft_bitmap, temp_buffer, w);
|
||||
|
||||
const float ref_size = baked->ContainerFont->Sources[0]->SizePixels;
|
||||
const float ref_size = baked->OwnerFont->Sources[0]->SizePixels;
|
||||
const float offsets_scale = (ref_size != 0.0f) ? (baked->Size / ref_size) : 1.0f;
|
||||
float font_off_x = (src->GlyphOffset.x * offsets_scale);
|
||||
float font_off_y = (src->GlyphOffset.y * offsets_scale) + baked->Ascent;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue