Skip to content

Commit c5b2a84

Browse files
BrutPittocornut
authored andcommitted
Backends: WebGPU: added smaller and debug helpers. (#8381, #8831, #8567, #8191, #7435)
1 parent d0e3b1d commit c5b2a84

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed

backends/imgui_impl_wgpu.cpp

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151

5252
#ifndef IMGUI_DISABLE
5353
#include "imgui_impl_wgpu.h"
54+
#include <limits.h>
55+
#include <stdio.h>
56+
#include <webgpu/webgpu.h>
5457

5558
// One of IMGUI_IMPL_WEBGPU_BACKEND_DAWN or IMGUI_IMPL_WEBGPU_BACKEND_WGPU must be provided. See imgui_impl_wgpu.h for more details.
5659
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) == defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
@@ -63,9 +66,6 @@
6366
#define IMGUI_IMPL_WEBGPU_BACKEND_WGPU_EMSCRIPTEN
6467
#endif
6568

66-
#include <limits.h>
67-
#include <webgpu/webgpu.h>
68-
6969
#ifdef IMGUI_IMPL_WEBGPU_BACKEND_DAWN
7070
// Dawn renamed WGPUProgrammableStageDescriptor to WGPUComputeState (see: https://github.com/webgpu-native/webgpu-headers/pull/413)
7171
// Using type alias until WGPU adopts the same naming convention (#8369)
@@ -920,6 +920,102 @@ void ImGui_ImplWGPU_NewFrame()
920920
// Those are currently used by our example applications.
921921
//-------------------------------------------------------------------------
922922

923+
bool ImGui_ImplWGPU_IsSurfaceStatusError(WGPUSurfaceGetCurrentTextureStatus status)
924+
{
925+
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
926+
return (status == WGPUSurfaceGetCurrentTextureStatus_Error);
927+
#else
928+
return (status == WGPUSurfaceGetCurrentTextureStatus_OutOfMemory || status == WGPUSurfaceGetCurrentTextureStatus_DeviceLost);
929+
#endif
930+
}
931+
932+
bool ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(WGPUSurfaceGetCurrentTextureStatus status)
933+
{
934+
#if defined(__EMSCRIPTEN__) && !defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
935+
return (status == WGPUSurfaceGetCurrentTextureStatus_Timeout || status == WGPUSurfaceGetCurrentTextureStatus_Outdated || status == WGPUSurfaceGetCurrentTextureStatus_Lost);
936+
#else
937+
return (status == WGPUSurfaceGetCurrentTextureStatus_Timeout || status == WGPUSurfaceGetCurrentTextureStatus_Outdated || status == WGPUSurfaceGetCurrentTextureStatus_Lost || status == WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal);
938+
#endif
939+
}
940+
941+
// Helpers to obtain a string
942+
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
943+
const char* ImGui_ImplWGPU_GetErrorTypeName(WGPUErrorType type)
944+
{
945+
switch (type)
946+
{
947+
case WGPUErrorType_Validation: return "Validation";
948+
case WGPUErrorType_OutOfMemory: return "OutOfMemory";
949+
case WGPUErrorType_Unknown: return "Unknown";
950+
case WGPUErrorType_Internal: return "Internal";
951+
default: return "Unknown";
952+
}
953+
}
954+
const char* ImGui_ImplWGPU_GetDeviceLostReasonName(WGPUDeviceLostReason type)
955+
{
956+
switch (type)
957+
{
958+
case WGPUDeviceLostReason_Unknown: return "Unknown";
959+
case WGPUDeviceLostReason_Destroyed: return "Destroyed";
960+
case WGPUDeviceLostReason_CallbackCancelled: return "CallbackCancelled";
961+
case WGPUDeviceLostReason_FailedCreation: return "FailedCreation";
962+
default: return "Unknown";
963+
}
964+
}
965+
#elif !defined(__EMSCRIPTEN__)
966+
const char* ImGui_ImplWGPU_GetLogLevelName(WGPULogLevel level)
967+
{
968+
switch (level)
969+
{
970+
case WGPULogLevel_Error: return "Error";
971+
case WGPULogLevel_Warn: return "Warn";
972+
case WGPULogLevel_Info: return "Info";
973+
case WGPULogLevel_Debug: return "Debug";
974+
case WGPULogLevel_Trace: return "Trace";
975+
default: return "Unknown";
976+
}
977+
}
978+
#endif
979+
980+
const char* ImGui_ImplWGPU_GetBackendTypeName(WGPUBackendType type)
981+
{
982+
switch (type)
983+
{
984+
case WGPUBackendType_WebGPU: return "WebGPU";
985+
case WGPUBackendType_D3D11: return "D3D11";
986+
case WGPUBackendType_D3D12: return "D3D12";
987+
case WGPUBackendType_Metal: return "Metal";
988+
case WGPUBackendType_Vulkan: return "Vulkan";
989+
case WGPUBackendType_OpenGL: return "OpenGL";
990+
case WGPUBackendType_OpenGLES: return "OpenGLES";
991+
default: return "Unknown";
992+
}
993+
}
994+
995+
const char* ImGui_ImplWGPU_GetAdapterTypeName(WGPUAdapterType type)
996+
{
997+
switch (type)
998+
{
999+
case WGPUAdapterType_DiscreteGPU: return "DiscreteGPU";
1000+
case WGPUAdapterType_IntegratedGPU: return "IntegratedGPU";
1001+
case WGPUAdapterType_CPU: return "CPU";
1002+
default: return "Unknown";
1003+
}
1004+
}
1005+
1006+
void ImGui_ImplWGPU_DebugPrintAdapterInfo(const WGPUAdapter& adapter)
1007+
{
1008+
WGPUAdapterInfo info = {};
1009+
wgpuAdapterGetInfo(adapter, &info);
1010+
printf("description: \"%.*s\"\n", (int)info.description.length, info.description.data);
1011+
printf("vendor: \"%.*s\", vendorID: %x\n", (int)info.vendor.length, info.vendor.data, info.vendorID);
1012+
printf("architecture: \"%.*s\"\n", (int) info.architecture.length, info.architecture.data);
1013+
printf("device: \"%.*s\", deviceID: %x\n", (int)info.device.length, info.device.data, info.deviceID);
1014+
printf("backendType: \"%s\"\n", ImGui_ImplWGPU_GetBackendTypeName(info.backendType));
1015+
printf("adapterType: \"%s\"\n", ImGui_ImplWGPU_GetAdapterTypeName(info.adapterType));
1016+
wgpuAdapterInfoFreeMembers(info);
1017+
}
1018+
9231019
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) && !defined(__EMSCRIPTEN__)
9241020

9251021
#if defined(__APPLE__)

backends/imgui_impl_wgpu.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
#endif
4242

4343
#include <webgpu/webgpu.h>
44+
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
45+
#include <webgpu/webgpu_cpp.h> // for wgpu::Device, wgpu::DeviceLostReason, wgpu::ErrorType used by validation layer callbacks.
46+
#elif !defined(__EMSCRIPTEN__)
47+
#include <webgpu/wgpu.h> // WGPULogLevel
48+
#endif
4449

4550
// Initialization data, for ImGui_ImplWGPU_Init()
4651
struct ImGui_ImplWGPU_InitInfo
@@ -86,6 +91,21 @@ struct ImGui_ImplWGPU_RenderState
8691
// Those are currently used by our example applications.
8792
//-------------------------------------------------------------------------
8893

94+
// (Optional) Helper to wrap some of the Dawn/WGPU/Emscripten quirks
95+
bool ImGui_ImplWGPU_IsSurfaceStatusError(WGPUSurfaceGetCurrentTextureStatus status);
96+
bool ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(WGPUSurfaceGetCurrentTextureStatus status); // Return whether the texture is suboptimal and may need to be recreated.
97+
98+
// (Optional) Helper for debugging/logging
99+
void ImGui_ImplWGPU_DebugPrintAdapterInfo(const WGPUAdapter& adapter);
100+
const char* ImGui_ImplWGPU_GetBackendTypeName(WGPUBackendType type);
101+
const char* ImGui_ImplWGPU_GetAdapterTypeName(WGPUAdapterType type);
102+
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
103+
const char* ImGui_ImplWGPU_GetDeviceLostReasonName(WGPUDeviceLostReason type);
104+
const char* ImGui_ImplWGPU_GetErrorTypeName(WGPUErrorType type);
105+
#elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) && !defined(__EMSCRIPTEN__)
106+
const char* ImGui_ImplWGPU_GetLogLevelName(WGPULogLevel level);
107+
#endif
108+
89109
// (Optional) Helper to create a surface on macOS/Wayland/X11/Window
90110
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN) && !defined(__EMSCRIPTEN__)
91111
struct ImGui_ImplWGPU_CreateSurfaceInfo

docs/CHANGELOG.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ Other Changes:
8585
- WebGPU: added various internal/optional helpers to wrap some of the
8686
Dawn/WGPU/Emscripten debacle quirks: (#8381) [@brutpitt]
8787
- ImGui_ImplWGPU_CreateWGPUSurfaceHelper().
88+
- ImGui_ImplWGPU_IsSurfaceStatusError(), ImGui_ImplWGPU_IsSurfaceStatusSubOptimal().
89+
- ImGui_ImplWGPU_DebugPrintAdapterInfo(),
90+
- ImGui_ImplWGPU_GetBackendTypeName(), ImGui_ImplWGPU_GetAdapterTypeName(),
91+
ImGui_ImplWGPU_GetDeviceLostReasonName(), ImGui_ImplWGPU_GetErrorTypeName(),
92+
ImGui_ImplWGPU_GetLogLevelName().
8893
- WebGPU: update to compile with Dawn and Emscripten's 4.0.10+
8994
'--use-port=emdawnwebgpu' ports. (#8381, #8898) [@brutpitt, @trbabb]
9095
When using Emscripten 4.0.10+, backend now defaults to IMGUI_IMPL_WEBGPU_BACKEND_DAWN

0 commit comments

Comments
 (0)