2727#include < webgpu/webgpu_cpp.h>
2828
2929// Data
30- static WGPUInstance wgpu_instance = nullptr ;
31- static WGPUDevice wgpu_device = nullptr ;
32- static WGPUSurface wgpu_surface = nullptr ;
33- static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
34- static WGPUSwapChain wgpu_swap_chain = nullptr ;
35- static int wgpu_surface_width = 1280 ;
36- static int wgpu_surface_height = 800 ;
30+ static WGPUInstance wgpu_instance = nullptr ;
31+ static WGPUDevice wgpu_device = nullptr ;
32+ static WGPUSurface wgpu_surface = nullptr ;
33+ static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm;
34+ static WGPUSwapChain wgpu_swap_chain = nullptr ;
35+ static int wgpu_surface_width = 1280 ;
36+ static int wgpu_surface_height = 800 ;
3737
3838// Forward declarations
3939static bool InitWGPU (GLFWwindow* window);
40- static void ResizeSurface (int width, int height);
4140
4241static void glfw_error_callback (int error, const char * description)
4342{
@@ -58,6 +57,21 @@ static void wgpu_error_callback(WGPUErrorType error_type, const char* message, v
5857 printf (" %s error: %s\n " , error_type_lbl, message);
5958}
6059
60+ static void ResizeSurface (int width, int height)
61+ {
62+ if (wgpu_swap_chain)
63+ wgpuSwapChainRelease (wgpu_swap_chain);
64+ wgpu_surface_width = width;
65+ wgpu_surface_height = height;
66+ WGPUSwapChainDescriptor swap_chain_desc = {};
67+ swap_chain_desc.usage = WGPUTextureUsage_RenderAttachment;
68+ swap_chain_desc.format = wgpu_preferred_fmt;
69+ swap_chain_desc.width = width;
70+ swap_chain_desc.height = height;
71+ swap_chain_desc.presentMode = WGPUPresentMode_Fifo;
72+ wgpu_swap_chain = wgpuDeviceCreateSwapChain (wgpu_device, wgpu_surface, &swap_chain_desc);
73+ }
74+
6175// Main code
6276int main (int , char **)
6377{
@@ -166,7 +180,7 @@ int main(int, char**)
166180 glfwGetFramebufferSize ((GLFWwindow*)window, &width, &height);
167181 if (width != wgpu_surface_width || height != wgpu_surface_height)
168182 {
169- ImGui_ImplWGPU_InvalidateDeviceObjects ();
183+ ImGui_ImplWGPU_InvalidateDeviceObjects (); // FIXME-OPT: Why doing this? This seems unnecessary and unoptimal.
170184 ResizeSurface (width, height);
171185 ImGui_ImplWGPU_CreateDeviceObjects ();
172186 }
@@ -275,21 +289,21 @@ static WGPUAdapter RequestAdapter(WGPUInstance instance)
275289 auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, const char * message, void * pUserData)
276290 {
277291 if (status == WGPURequestAdapterStatus_Success)
278- *(WGPUAdapter*)( pUserData) = adapter;
292+ *(WGPUAdapter*)pUserData = adapter;
279293 else
280294 printf (" Could not get WebGPU adapter: %s\n " , message);
281- };
295+ };
282296 WGPUAdapter adapter;
283297 wgpuInstanceRequestAdapter (instance, nullptr , onAdapterRequestEnded, (void *)&adapter);
284298 return adapter;
285299}
286300
287- static WGPUDevice RequestDevice (WGPUAdapter& adapter)
301+ static WGPUDevice RequestDevice (WGPUAdapter adapter)
288302{
289303 auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, const char * message, void * pUserData)
290304 {
291305 if (status == WGPURequestDeviceStatus_Success)
292- *(WGPUDevice*)( pUserData) = device;
306+ *(WGPUDevice*)pUserData = device;
293307 else
294308 printf (" Could not get WebGPU device: %s\n " , message);
295309 };
@@ -307,14 +321,7 @@ static bool InitWGPU(GLFWwindow* window)
307321 wgpu_device = emscripten_webgpu_get_device ();
308322 if (!wgpu_device)
309323 return false ;
310- #else
311- WGPUAdapter adapter = RequestAdapter (instance.Get ());
312- if (!adapter)
313- return false ;
314- wgpu_device = RequestDevice (adapter);
315- #endif
316324
317- #ifdef __EMSCRIPTEN__
318325 wgpu::SurfaceDescriptorFromCanvasHTMLSelector canvas_desc = {};
319326 canvas_desc.selector = " #canvas" ;
320327 wgpu::SurfaceDescriptor surface_desc = {};
@@ -324,6 +331,11 @@ static bool InitWGPU(GLFWwindow* window)
324331 wgpu::Adapter adapter = {};
325332 wgpu_preferred_fmt = (WGPUTextureFormat)surface.GetPreferredFormat (adapter);
326333#else
334+ WGPUAdapter adapter = RequestAdapter (instance.Get ());
335+ if (!adapter)
336+ return false ;
337+ wgpu_device = RequestDevice (adapter);
338+
327339 wgpu::Surface surface = wgpu::glfw::CreateSurfaceForWindow (instance, window);
328340 if (!surface)
329341 return false ;
@@ -335,21 +347,5 @@ static bool InitWGPU(GLFWwindow* window)
335347 wgpu_surface = surface.MoveToCHandle ();
336348
337349 wgpuDeviceSetUncapturedErrorCallback (wgpu_device, wgpu_error_callback, nullptr );
338-
339350 return true ;
340351}
341-
342- static void ResizeSurface (int width, int height)
343- {
344- if (wgpu_swap_chain)
345- wgpuSwapChainRelease (wgpu_swap_chain);
346- wgpu_surface_width = width;
347- wgpu_surface_height = height;
348- WGPUSwapChainDescriptor swap_chain_desc = {};
349- swap_chain_desc.usage = WGPUTextureUsage_RenderAttachment;
350- swap_chain_desc.format = wgpu_preferred_fmt;
351- swap_chain_desc.width = width;
352- swap_chain_desc.height = height;
353- swap_chain_desc.presentMode = WGPUPresentMode_Fifo;
354- wgpu_swap_chain = wgpuDeviceCreateSwapChain (wgpu_device, wgpu_surface, &swap_chain_desc);
355- }
0 commit comments