mirror of
https://github.com/ocornut/imgui.git
synced 2026-01-11 00:04:24 +00:00
Examples: GLFW+WebGPU: moving blocks to simplify upcoming diff. (#8381)
This commit is contained in:
parent
71447b94fb
commit
4b1f5aba80
2 changed files with 33 additions and 37 deletions
|
|
@ -27,17 +27,16 @@
|
||||||
#include <webgpu/webgpu_cpp.h>
|
#include <webgpu/webgpu_cpp.h>
|
||||||
|
|
||||||
// Data
|
// Data
|
||||||
static WGPUInstance wgpu_instance = nullptr;
|
static WGPUInstance wgpu_instance = nullptr;
|
||||||
static WGPUDevice wgpu_device = nullptr;
|
static WGPUDevice wgpu_device = nullptr;
|
||||||
static WGPUSurface wgpu_surface = nullptr;
|
static WGPUSurface wgpu_surface = nullptr;
|
||||||
static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
|
static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
|
||||||
static WGPUSwapChain wgpu_swap_chain = nullptr;
|
static WGPUSwapChain wgpu_swap_chain = nullptr;
|
||||||
static int wgpu_surface_width = 1280;
|
static int wgpu_surface_width = 1280;
|
||||||
static int wgpu_surface_height = 800;
|
static int wgpu_surface_height = 800;
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
static bool InitWGPU(GLFWwindow* window);
|
static bool InitWGPU(GLFWwindow* window);
|
||||||
static void ResizeSurface(int width, int height);
|
|
||||||
|
|
||||||
static void glfw_error_callback(int error, const char* description)
|
static void glfw_error_callback(int error, const char* description)
|
||||||
{
|
{
|
||||||
|
|
@ -58,6 +57,21 @@ static void wgpu_error_callback(WGPUErrorType error_type, const char* message, v
|
||||||
printf("%s error: %s\n", error_type_lbl, message);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
// Main code
|
// Main code
|
||||||
int main(int, char**)
|
int main(int, char**)
|
||||||
{
|
{
|
||||||
|
|
@ -166,7 +180,7 @@ int main(int, char**)
|
||||||
glfwGetFramebufferSize((GLFWwindow*)window, &width, &height);
|
glfwGetFramebufferSize((GLFWwindow*)window, &width, &height);
|
||||||
if (width != wgpu_surface_width || height != wgpu_surface_height)
|
if (width != wgpu_surface_width || height != wgpu_surface_height)
|
||||||
{
|
{
|
||||||
ImGui_ImplWGPU_InvalidateDeviceObjects();
|
ImGui_ImplWGPU_InvalidateDeviceObjects(); // FIXME-OPT: Why doing this? This seems unnecessary and unoptimal.
|
||||||
ResizeSurface(width, height);
|
ResizeSurface(width, height);
|
||||||
ImGui_ImplWGPU_CreateDeviceObjects();
|
ImGui_ImplWGPU_CreateDeviceObjects();
|
||||||
}
|
}
|
||||||
|
|
@ -275,21 +289,21 @@ static WGPUAdapter RequestAdapter(WGPUInstance instance)
|
||||||
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, const char* message, void* pUserData)
|
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, const char* message, void* pUserData)
|
||||||
{
|
{
|
||||||
if (status == WGPURequestAdapterStatus_Success)
|
if (status == WGPURequestAdapterStatus_Success)
|
||||||
*(WGPUAdapter*)(pUserData) = adapter;
|
*(WGPUAdapter*)pUserData = adapter;
|
||||||
else
|
else
|
||||||
printf("Could not get WebGPU adapter: %s\n", message);
|
printf("Could not get WebGPU adapter: %s\n", message);
|
||||||
};
|
};
|
||||||
WGPUAdapter adapter;
|
WGPUAdapter adapter;
|
||||||
wgpuInstanceRequestAdapter(instance, nullptr, onAdapterRequestEnded, (void*)&adapter);
|
wgpuInstanceRequestAdapter(instance, nullptr, onAdapterRequestEnded, (void*)&adapter);
|
||||||
return adapter;
|
return adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
static WGPUDevice RequestDevice(WGPUAdapter& adapter)
|
static WGPUDevice RequestDevice(WGPUAdapter adapter)
|
||||||
{
|
{
|
||||||
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, const char* message, void* pUserData)
|
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, const char* message, void* pUserData)
|
||||||
{
|
{
|
||||||
if (status == WGPURequestDeviceStatus_Success)
|
if (status == WGPURequestDeviceStatus_Success)
|
||||||
*(WGPUDevice*)(pUserData) = device;
|
*(WGPUDevice*)pUserData = device;
|
||||||
else
|
else
|
||||||
printf("Could not get WebGPU device: %s\n", message);
|
printf("Could not get WebGPU device: %s\n", message);
|
||||||
};
|
};
|
||||||
|
|
@ -307,14 +321,7 @@ static bool InitWGPU(GLFWwindow* window)
|
||||||
wgpu_device = emscripten_webgpu_get_device();
|
wgpu_device = emscripten_webgpu_get_device();
|
||||||
if (!wgpu_device)
|
if (!wgpu_device)
|
||||||
return false;
|
return false;
|
||||||
#else
|
|
||||||
WGPUAdapter adapter = RequestAdapter(instance.Get());
|
|
||||||
if (!adapter)
|
|
||||||
return false;
|
|
||||||
wgpu_device = RequestDevice(adapter);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __EMSCRIPTEN__
|
|
||||||
wgpu::SurfaceDescriptorFromCanvasHTMLSelector canvas_desc = {};
|
wgpu::SurfaceDescriptorFromCanvasHTMLSelector canvas_desc = {};
|
||||||
canvas_desc.selector = "#canvas";
|
canvas_desc.selector = "#canvas";
|
||||||
wgpu::SurfaceDescriptor surface_desc = {};
|
wgpu::SurfaceDescriptor surface_desc = {};
|
||||||
|
|
@ -324,6 +331,11 @@ static bool InitWGPU(GLFWwindow* window)
|
||||||
wgpu::Adapter adapter = {};
|
wgpu::Adapter adapter = {};
|
||||||
wgpu_preferred_fmt = (WGPUTextureFormat)surface.GetPreferredFormat(adapter);
|
wgpu_preferred_fmt = (WGPUTextureFormat)surface.GetPreferredFormat(adapter);
|
||||||
#else
|
#else
|
||||||
|
WGPUAdapter adapter = RequestAdapter(instance.Get());
|
||||||
|
if (!adapter)
|
||||||
|
return false;
|
||||||
|
wgpu_device = RequestDevice(adapter);
|
||||||
|
|
||||||
wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
|
wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
|
||||||
if (!surface)
|
if (!surface)
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -335,21 +347,5 @@ static bool InitWGPU(GLFWwindow* window)
|
||||||
wgpu_surface = surface.MoveToCHandle();
|
wgpu_surface = surface.MoveToCHandle();
|
||||||
|
|
||||||
wgpuDeviceSetUncapturedErrorCallback(wgpu_device, wgpu_error_callback, nullptr);
|
wgpuDeviceSetUncapturedErrorCallback(wgpu_device, wgpu_error_callback, nullptr);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ int main(int, char**)
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_PropertiesID props = SDL_GetWindowProperties(window);
|
SDL_PropertiesID props = SDL_GetWindowProperties(window);
|
||||||
HWND hwnd = (HWND)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
|
HWND hwnd = (HWND)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
|
||||||
|
|
||||||
// Initialize Direct3D
|
// Initialize Direct3D
|
||||||
if (!CreateDeviceD3D(hwnd))
|
if (!CreateDeviceD3D(hwnd))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue