mirror of
https://github.com/ocornut/imgui.git
synced 2026-01-10 15:23:12 +00:00
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user