main.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // Dear ImGui: standalone example application for using SDL3 + WebGPU
  2. // - Emscripten is supported for publishing on web. See https://emscripten.org.
  3. // - Dawn is used as a WebGPU implementation on desktop.
  4. // Learn about Dear ImGui:
  5. // - FAQ https://dearimgui.com/faq
  6. // - Getting Started https://dearimgui.com/getting-started
  7. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  8. // - Introduction, links and more at the top of imgui.cpp
  9. #include "imgui.h"
  10. #include "imgui_impl_sdl3.h"
  11. #include "imgui_impl_wgpu.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <SDL3/SDL.h>
  15. // This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
  16. #ifdef __EMSCRIPTEN__
  17. #include <emscripten.h>
  18. #include <emscripten/html5.h>
  19. #if defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
  20. #include <emscripten/html5_webgpu.h>
  21. #endif
  22. #include "../libs/emscripten/emscripten_mainloop_stub.h"
  23. #endif
  24. #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
  25. #include <webgpu/webgpu_cpp.h>
  26. #endif
  27. // Data
  28. static WGPUInstance wgpu_instance = nullptr;
  29. static WGPUDevice wgpu_device = nullptr;
  30. static WGPUSurface wgpu_surface = nullptr;
  31. static WGPUQueue wgpu_queue = nullptr;
  32. static WGPUSurfaceConfiguration wgpu_surface_configuration = {};
  33. static int wgpu_surface_width = 1280;
  34. static int wgpu_surface_height = 800;
  35. // Forward declarations
  36. static bool InitWGPU(SDL_Window* window);
  37. static WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window);
  38. static void ResizeSurface(int width, int height)
  39. {
  40. wgpu_surface_configuration.width = wgpu_surface_width = width;
  41. wgpu_surface_configuration.height = wgpu_surface_height = height;
  42. wgpuSurfaceConfigure( wgpu_surface, (WGPUSurfaceConfiguration*)&wgpu_surface_configuration );
  43. }
  44. // Main code
  45. int main(int, char**)
  46. {
  47. // Setup SDL
  48. // [If using SDL_MAIN_USE_CALLBACKS: all code below until the main loop starts would likely be your SDL_AppInit() function]
  49. if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
  50. {
  51. printf("Error: SDL_Init(): %s\n", SDL_GetError());
  52. return 1;
  53. }
  54. // Create SDL window graphics context
  55. float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
  56. SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE;
  57. SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+WebGPU example", wgpu_surface_width, wgpu_surface_height, window_flags);
  58. if (window == nullptr)
  59. {
  60. printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
  61. return 1;
  62. }
  63. // Initialize WGPU
  64. InitWGPU(window);
  65. // Setup Dear ImGui context
  66. IMGUI_CHECKVERSION();
  67. ImGui::CreateContext();
  68. ImGuiIO& io = ImGui::GetIO(); (void)io;
  69. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  70. io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  71. // Setup Dear ImGui style
  72. ImGui::StyleColorsDark();
  73. //ImGui::StyleColorsLight();
  74. // Setup scaling
  75. ImGuiStyle& style = ImGui::GetStyle();
  76. 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)
  77. style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
  78. // Setup Platform/Renderer backends
  79. ImGui_ImplSDL3_InitForOther(window);
  80. ImGui_ImplWGPU_InitInfo init_info;
  81. init_info.Device = wgpu_device;
  82. init_info.NumFramesInFlight = 3;
  83. init_info.RenderTargetFormat = wgpu_surface_configuration.format;
  84. init_info.DepthStencilFormat = WGPUTextureFormat_Undefined;
  85. ImGui_ImplWGPU_Init(&init_info);
  86. // Load Fonts
  87. // - 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.
  88. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  89. // - 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).
  90. // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
  91. // - 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!
  92. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  93. // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
  94. //style.FontSizeBase = 20.0f;
  95. //io.Fonts->AddFontDefault();
  96. #ifndef IMGUI_DISABLE_FILE_FUNCTIONS
  97. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
  98. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
  99. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
  100. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
  101. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
  102. //IM_ASSERT(font != nullptr);
  103. #endif
  104. // Our state
  105. bool show_demo_window = true;
  106. bool show_another_window = false;
  107. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  108. // Main loop
  109. bool done = false;
  110. #ifdef __EMSCRIPTEN__
  111. // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
  112. // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
  113. io.IniFilename = nullptr;
  114. EMSCRIPTEN_MAINLOOP_BEGIN
  115. #else
  116. while (!done)
  117. #endif
  118. {
  119. // Poll and handle events (inputs, window resize, etc.)
  120. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  121. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  122. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  123. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  124. // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function]
  125. SDL_Event event;
  126. while (SDL_PollEvent(&event))
  127. {
  128. ImGui_ImplSDL3_ProcessEvent(&event);
  129. if (event.type == SDL_EVENT_QUIT)
  130. done = true;
  131. if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
  132. done = true;
  133. }
  134. // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function]
  135. // React to changes in screen size
  136. int width, height;
  137. SDL_GetWindowSize(window, &width, &height);
  138. if (width != wgpu_surface_width || height != wgpu_surface_height)
  139. ResizeSurface(width, height);
  140. // Check surface status for error. If texture is not optimal, try to reconfigure the surface.
  141. WGPUSurfaceTexture surface_texture;
  142. wgpuSurfaceGetCurrentTexture(wgpu_surface, &surface_texture);
  143. if (ImGui_ImplWGPU_IsSurfaceStatusError(surface_texture.status))
  144. {
  145. fprintf(stderr, "Unrecoverable Surface Texture status=%#.8x\n", surface_texture.status);
  146. abort();
  147. }
  148. if (ImGui_ImplWGPU_IsSurfaceStatusSubOptimal(surface_texture.status))
  149. {
  150. if (surface_texture.texture)
  151. wgpuTextureRelease(surface_texture.texture);
  152. if (width > 0 && height > 0)
  153. ResizeSurface(width, height);
  154. continue;
  155. }
  156. // Start the Dear ImGui frame
  157. ImGui_ImplWGPU_NewFrame();
  158. ImGui_ImplSDL3_NewFrame();
  159. ImGui::NewFrame();
  160. // 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!).
  161. if (show_demo_window)
  162. ImGui::ShowDemoWindow(&show_demo_window);
  163. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
  164. {
  165. static float f = 0.0f;
  166. static int counter = 0;
  167. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  168. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  169. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  170. ImGui::Checkbox("Another Window", &show_another_window);
  171. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  172. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  173. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  174. counter++;
  175. ImGui::SameLine();
  176. ImGui::Text("counter = %d", counter);
  177. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  178. ImGui::End();
  179. }
  180. // 3. Show another simple window.
  181. if (show_another_window)
  182. {
  183. 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)
  184. ImGui::Text("Hello from another window!");
  185. if (ImGui::Button("Close Me"))
  186. show_another_window = false;
  187. ImGui::End();
  188. }
  189. // Rendering
  190. ImGui::Render();
  191. WGPUTextureViewDescriptor view_desc = {};
  192. view_desc.format = wgpu_surface_configuration.format;
  193. view_desc.dimension = WGPUTextureViewDimension_2D;
  194. view_desc.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED;
  195. view_desc.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED;
  196. view_desc.aspect = WGPUTextureAspect_All;
  197. WGPUTextureView texture_view = wgpuTextureCreateView(surface_texture.texture, &view_desc);
  198. WGPURenderPassColorAttachment color_attachments = {};
  199. color_attachments.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
  200. color_attachments.loadOp = WGPULoadOp_Clear;
  201. color_attachments.storeOp = WGPUStoreOp_Store;
  202. color_attachments.clearValue = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
  203. color_attachments.view = texture_view;
  204. WGPURenderPassDescriptor render_pass_desc = {};
  205. render_pass_desc.colorAttachmentCount = 1;
  206. render_pass_desc.colorAttachments = &color_attachments;
  207. render_pass_desc.depthStencilAttachment = nullptr;
  208. WGPUCommandEncoderDescriptor enc_desc = {};
  209. WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(wgpu_device, &enc_desc);
  210. WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &render_pass_desc);
  211. ImGui_ImplWGPU_RenderDrawData(ImGui::GetDrawData(), pass);
  212. wgpuRenderPassEncoderEnd(pass);
  213. WGPUCommandBufferDescriptor cmd_buffer_desc = {};
  214. WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(encoder, &cmd_buffer_desc);
  215. wgpuQueueSubmit(wgpu_queue, 1, &cmd_buffer);
  216. #ifndef __EMSCRIPTEN__
  217. wgpuSurfacePresent(wgpu_surface);
  218. // Tick needs to be called in Dawn to display validation errors
  219. #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
  220. wgpuDeviceTick(wgpu_device);
  221. #endif
  222. #endif
  223. wgpuTextureViewRelease(texture_view);
  224. wgpuRenderPassEncoderRelease(pass);
  225. wgpuCommandEncoderRelease(encoder);
  226. wgpuCommandBufferRelease(cmd_buffer);
  227. }
  228. #ifdef __EMSCRIPTEN__
  229. EMSCRIPTEN_MAINLOOP_END;
  230. #endif
  231. // Cleanup
  232. // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
  233. ImGui_ImplWGPU_Shutdown();
  234. ImGui_ImplSDL3_Shutdown();
  235. ImGui::DestroyContext();
  236. wgpuSurfaceUnconfigure(wgpu_surface);
  237. wgpuSurfaceRelease(wgpu_surface);
  238. wgpuQueueRelease(wgpu_queue);
  239. wgpuDeviceRelease(wgpu_device);
  240. wgpuInstanceRelease(wgpu_instance);
  241. SDL_DestroyWindow(window);
  242. SDL_Quit();
  243. return 0;
  244. }
  245. #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
  246. static WGPUAdapter RequestAdapter(wgpu::Instance& instance)
  247. {
  248. wgpu::Adapter acquired_adapter;
  249. wgpu::RequestAdapterOptions adapter_options;
  250. auto onRequestAdapter = [&](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter, wgpu::StringView message)
  251. {
  252. if (status != wgpu::RequestAdapterStatus::Success)
  253. {
  254. printf("Failed to get an adapter: %s\n", message.data);
  255. return;
  256. }
  257. acquired_adapter = std::move(adapter);
  258. };
  259. // Synchronously (wait until) acquire Adapter
  260. wgpu::Future waitAdapterFunc { instance.RequestAdapter(&adapter_options, wgpu::CallbackMode::WaitAnyOnly, onRequestAdapter) };
  261. wgpu::WaitStatus waitStatusAdapter = instance.WaitAny(waitAdapterFunc, UINT64_MAX);
  262. IM_ASSERT(acquired_adapter != nullptr && waitStatusAdapter == wgpu::WaitStatus::Success && "Error on Adapter request");
  263. return acquired_adapter.MoveToCHandle();
  264. }
  265. static WGPUDevice RequestDevice(wgpu::Instance& instance, wgpu::Adapter& adapter)
  266. {
  267. // Set device callback functions
  268. wgpu::DeviceDescriptor device_desc;
  269. device_desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowSpontaneous,
  270. [](const wgpu::Device&, wgpu::DeviceLostReason type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetDeviceLostReasonName((WGPUDeviceLostReason)type), msg.data); }
  271. );
  272. device_desc.SetUncapturedErrorCallback(
  273. [](const wgpu::Device&, wgpu::ErrorType type, wgpu::StringView msg) { fprintf(stderr, "%s error: %s\n", ImGui_ImplWGPU_GetErrorTypeName((WGPUErrorType)type), msg.data); }
  274. );
  275. wgpu::Device acquired_device;
  276. auto onRequestDevice = [&](wgpu::RequestDeviceStatus status, wgpu::Device local_device, wgpu::StringView message)
  277. {
  278. if (status != wgpu::RequestDeviceStatus::Success)
  279. {
  280. printf("Failed to get an device: %s\n", message.data);
  281. return;
  282. }
  283. acquired_device = std::move(local_device);
  284. };
  285. // Synchronously (wait until) get Device
  286. wgpu::Future waitDeviceFunc { adapter.RequestDevice(&device_desc, wgpu::CallbackMode::WaitAnyOnly, onRequestDevice) };
  287. wgpu::WaitStatus waitStatusDevice = instance.WaitAny(waitDeviceFunc, UINT64_MAX);
  288. IM_ASSERT(acquired_device != nullptr && waitStatusDevice == wgpu::WaitStatus::Success && "Error on Device request");
  289. return acquired_device.MoveToCHandle();
  290. }
  291. #elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
  292. #ifdef __EMSCRIPTEN__
  293. // Adapter and device initialization via JS
  294. EM_ASYNC_JS( void, getAdapterAndDeviceViaJS, (),
  295. {
  296. if (!navigator.gpu)
  297. throw Error("WebGPU not supported.");
  298. const adapter = await navigator.gpu.requestAdapter();
  299. const device = await adapter.requestDevice();
  300. Module.preinitializedWebGPUDevice = device;
  301. } );
  302. #else // __EMSCRIPTEN__
  303. static void handle_request_adapter(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, void* userdata1, void* userdata2)
  304. {
  305. if (status == WGPURequestAdapterStatus_Success)
  306. {
  307. WGPUAdapter* extAdapter = (WGPUAdapter*)userdata1;
  308. *extAdapter = adapter;
  309. }
  310. else
  311. {
  312. printf("Request_adapter status=%#.8x message=%.*s\n", status, (int)message.length, message.data);
  313. }
  314. }
  315. static void handle_request_device(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, void* userdata1, void* userdata2)
  316. {
  317. if (status == WGPURequestDeviceStatus_Success)
  318. {
  319. WGPUDevice* extDevice = (WGPUDevice*)userdata1;
  320. *extDevice = device;
  321. }
  322. else
  323. {
  324. printf("Request_device status=%#.8x message=%.*s\n", status, (int)message.length, message.data);
  325. }
  326. }
  327. static WGPUAdapter RequestAdapter(WGPUInstance& instance)
  328. {
  329. WGPURequestAdapterOptions adapter_options = {};
  330. WGPUAdapter local_adapter;
  331. WGPURequestAdapterCallbackInfo adapterCallbackInfo = {};
  332. adapterCallbackInfo.callback = handle_request_adapter;
  333. adapterCallbackInfo.userdata1 = &local_adapter;
  334. wgpuInstanceRequestAdapter(instance, &adapter_options, adapterCallbackInfo);
  335. IM_ASSERT(local_adapter && "Error on Adapter request");
  336. return local_adapter;
  337. }
  338. static WGPUDevice RequestDevice(WGPUAdapter& adapter)
  339. {
  340. WGPUDevice local_device;
  341. WGPURequestDeviceCallbackInfo deviceCallbackInfo = {};
  342. deviceCallbackInfo.callback = handle_request_device;
  343. deviceCallbackInfo.userdata1 = &local_device;
  344. wgpuAdapterRequestDevice(adapter, nullptr, deviceCallbackInfo);
  345. IM_ASSERT(local_device && "Error on Device request");
  346. return local_device;
  347. }
  348. #endif // __EMSCRIPTEN__
  349. #endif // IMGUI_IMPL_WEBGPU_BACKEND_WGPU
  350. static bool InitWGPU(SDL_Window* window)
  351. {
  352. WGPUTextureFormat preferred_fmt = WGPUTextureFormat_Undefined; // acquired from SurfaceCapabilities
  353. // Google DAWN backend: Adapter and Device acquisition, Surface creation
  354. #if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
  355. wgpu::InstanceDescriptor instance_desc = {};
  356. static constexpr wgpu::InstanceFeatureName timedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
  357. instance_desc.requiredFeatureCount = 1;
  358. instance_desc.requiredFeatures = &timedWaitAny;
  359. wgpu::Instance instance = wgpu::CreateInstance(&instance_desc);
  360. wgpu::Adapter adapter = RequestAdapter(instance);
  361. ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter.Get());
  362. wgpu_device = RequestDevice(instance, adapter);
  363. // Create the surface.
  364. #ifdef __EMSCRIPTEN__
  365. wgpu::EmscriptenSurfaceSourceCanvasHTMLSelector canvas_desc = {};
  366. canvas_desc.selector = "#canvas";
  367. wgpu::SurfaceDescriptor surface_desc = {};
  368. surface_desc.nextInChain = &canvas_desc;
  369. wgpu::Surface surface = instance.CreateSurface(&surface_desc);
  370. #else
  371. wgpu::Surface surface = CreateWGPUSurface(instance.Get(), window);
  372. #endif
  373. if (!surface)
  374. return false;
  375. // Moving Dawn objects into WGPU handles
  376. wgpu_instance = instance.MoveToCHandle();
  377. wgpu_surface = surface.MoveToCHandle();
  378. WGPUSurfaceCapabilities surface_capabilities = {};
  379. wgpuSurfaceGetCapabilities(wgpu_surface, adapter.Get(), &surface_capabilities);
  380. preferred_fmt = surface_capabilities.formats[0];
  381. // WGPU backend: Adapter and Device acquisition, Surface creation
  382. #elif defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU)
  383. wgpu_instance = wgpuCreateInstance(nullptr);
  384. #ifdef __EMSCRIPTEN__
  385. getAdapterAndDeviceViaJS();
  386. wgpu_device = emscripten_webgpu_get_device();
  387. IM_ASSERT(wgpu_device != nullptr && "Error creating the Device");
  388. WGPUSurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = {};
  389. html_surface_desc.chain.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector;
  390. html_surface_desc.selector = "#canvas";
  391. WGPUSurfaceDescriptor surface_desc = {};
  392. surface_desc.nextInChain = &html_surface_desc.chain;
  393. // Create the surface.
  394. wgpu_surface = wgpuInstanceCreateSurface(wgpu_instance, &surface_desc);
  395. preferred_fmt = wgpuSurfaceGetPreferredFormat(wgpu_surface, {} /* adapter */);
  396. #else // __EMSCRIPTEN__
  397. wgpuSetLogCallback(
  398. [](WGPULogLevel level, WGPUStringView msg, void* userdata) { fprintf(stderr, "%s: %.*s\n", ImGui_ImplWGPU_GetLogLevelName(level), (int)msg.length, msg.data); }, nullptr
  399. );
  400. wgpuSetLogLevel(WGPULogLevel_Warn);
  401. WGPUAdapter adapter = RequestAdapter(wgpu_instance);
  402. ImGui_ImplWGPU_DebugPrintAdapterInfo(adapter);
  403. wgpu_device = RequestDevice(adapter);
  404. // Create the surface.
  405. wgpu_surface = CreateWGPUSurface(wgpu_instance, window);
  406. if (!wgpu_surface)
  407. return false;
  408. WGPUSurfaceCapabilities surface_capabilities = {};
  409. wgpuSurfaceGetCapabilities(wgpu_surface, adapter, &surface_capabilities);
  410. preferred_fmt = surface_capabilities.formats[0];
  411. #endif // __EMSCRIPTEN__
  412. #endif // IMGUI_IMPL_WEBGPU_BACKEND_WGPU
  413. wgpu_surface_configuration.presentMode = WGPUPresentMode_Fifo;
  414. wgpu_surface_configuration.alphaMode = WGPUCompositeAlphaMode_Auto;
  415. wgpu_surface_configuration.usage = WGPUTextureUsage_RenderAttachment;
  416. wgpu_surface_configuration.width = wgpu_surface_width;
  417. wgpu_surface_configuration.height = wgpu_surface_height;
  418. wgpu_surface_configuration.device = wgpu_device;
  419. wgpu_surface_configuration.format = preferred_fmt;
  420. wgpuSurfaceConfigure(wgpu_surface, &wgpu_surface_configuration);
  421. wgpu_queue = wgpuDeviceGetQueue(wgpu_device);
  422. return true;
  423. }
  424. // SDL3 helper to create a WebGPU surface (exclusively!) for Native/Desktop applications: available only together with WebGPU/WGPU backend
  425. // As of today (2025/10) there is no "official" support in SDL3 to create a surface for WebGPU backend
  426. // This stub uses "low level" SDL3 calls to acquire information from a specific Window Manager.
  427. // Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
  428. #ifndef __EMSCRIPTEN__
  429. #if defined(SDL_PLATFORM_WIN32)
  430. #ifndef WIN32_LEAN_AND_MEAN
  431. #define WIN32_LEAN_AND_MEAN 1
  432. #endif
  433. #include <windows.h>
  434. #endif
  435. static WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window)
  436. {
  437. SDL_PropertiesID propertiesID = SDL_GetWindowProperties(window);
  438. ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
  439. create_info.Instance = instance;
  440. #if defined(SDL_PLATFORM_MACOS)
  441. {
  442. create_info.System = "cocoa";
  443. create_info.RawWindow = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL);
  444. return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
  445. }
  446. #elif defined(SDL_PLATFORM_LINUX)
  447. if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0)
  448. {
  449. create_info.System = "wayland";
  450. create_info.RawDisplay = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL);
  451. create_info.RawSurface = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL);
  452. return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
  453. }
  454. else if (!SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11"))
  455. {
  456. create_info.System = "x11";
  457. create_info.RawWindow = (void*)SDL_GetNumberProperty(propertiesID, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0);
  458. create_info.RawDisplay = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL);
  459. return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
  460. }
  461. #elif defined(SDL_PLATFORM_WIN32)
  462. {
  463. create_info.System = "win32";
  464. create_info.RawWindow = (void*)SDL_GetPointerProperty(propertiesID, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
  465. create_info.RawInstance = (void*)::GetModuleHandle(NULL);
  466. return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
  467. }
  468. #else
  469. #error "Unsupported WebGPU native platform!"
  470. #endif
  471. return nullptr;
  472. }
  473. #endif // #ifndef __EMSCRIPTEN__