main.cpp 23 KB

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