2
0

main.cpp 23 KB

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