|
60 | 60 | # include "EngineFactoryMtl.h" |
61 | 61 | #endif |
62 | 62 |
|
| 63 | +#if WEBGPU_SUPPORTED |
| 64 | +# include "EngineFactoryWebGPU.h" |
| 65 | +#endif |
| 66 | + |
63 | 67 | #include "imgui.h" |
64 | 68 | #include "ImGuiImplDiligent.hpp" |
65 | 69 | #include "ImGuiUtils.hpp" |
66 | 70 |
|
| 71 | +#if PLATFORM_EMSCRIPTEN |
| 72 | +# include <emscripten/html5_webgpu.h> |
| 73 | +#endif |
| 74 | + |
67 | 75 | namespace Diligent |
68 | 76 | { |
69 | 77 |
|
@@ -136,7 +144,7 @@ void SampleApp::InitializeDiligentEngine(const NativeWindow* pWindow) |
136 | 144 |
|
137 | 145 | Uint32 NumImmediateContexts = 0; |
138 | 146 |
|
139 | | -#if D3D11_SUPPORTED || D3D12_SUPPORTED || VULKAN_SUPPORTED |
| 147 | +#if D3D11_SUPPORTED || D3D12_SUPPORTED || VULKAN_SUPPORTED || WEBGPU_SUPPORTED |
140 | 148 | auto FindAdapter = [this](auto* pFactory, Version GraphicsAPIVersion, GraphicsAdapterInfo& AdapterAttribs) { |
141 | 149 | Uint32 NumAdapters = 0; |
142 | 150 | pFactory->EnumerateAdapters(GraphicsAPIVersion, NumAdapters, nullptr); |
@@ -414,7 +422,6 @@ void SampleApp::InitializeDiligentEngine(const NativeWindow* pWindow) |
414 | 422 | break; |
415 | 423 | #endif |
416 | 424 |
|
417 | | - |
418 | 425 | #if METAL_SUPPORTED |
419 | 426 | case RENDER_DEVICE_TYPE_METAL: |
420 | 427 | { |
@@ -442,6 +449,50 @@ void SampleApp::InitializeDiligentEngine(const NativeWindow* pWindow) |
442 | 449 | break; |
443 | 450 | #endif |
444 | 451 |
|
| 452 | +#if WEBGPU_SUPPORTED |
| 453 | + case RENDER_DEVICE_TYPE_WEBGPU: |
| 454 | + { |
| 455 | +# if EXPLICITLY_LOAD_ENGINE_WEBGPU_DLL |
| 456 | + // Load the dll and import LoadGraphicsEngineWebGPU() function |
| 457 | + auto GetEngineFactoryWebGPU = LoadGraphicsEngineWebGPU(); |
| 458 | +# endif |
| 459 | + EngineWebGPUCreateInfo EngineCI; |
| 460 | + if (m_ValidationLevel >= 0) |
| 461 | + EngineCI.SetValidationLevel(static_cast<VALIDATION_LEVEL>(m_ValidationLevel)); |
| 462 | + |
| 463 | + auto* pFactoryWebGPU = GetEngineFactoryWebGPU(); |
| 464 | + m_pEngineFactory = pFactoryWebGPU; |
| 465 | + |
| 466 | + NumImmediateContexts = std::max(1u, EngineCI.NumImmediateContexts); |
| 467 | + ppContexts.resize(NumImmediateContexts + EngineCI.NumDeferredContexts); |
| 468 | + m_TheSample->ModifyEngineInitInfo({pFactoryWebGPU, m_DeviceType, EngineCI, m_SwapChainInitDesc}); |
| 469 | + |
| 470 | + if (EngineCI.NumDeferredContexts != 0) |
| 471 | + { |
| 472 | + LOG_WARNING_MESSAGE("Deferred contexts are not supported in WebGPU mode"); |
| 473 | + EngineCI.NumDeferredContexts = 0; |
| 474 | + } |
| 475 | + |
| 476 | +# if PLATFORM_EMSCRIPTEN |
| 477 | + (void)FindAdapter; |
| 478 | + WGPUDevice wgpuDevice = emscripten_webgpu_get_device(); |
| 479 | + WGPUInstance wgpuInstance = wgpuCreateInstance(nullptr); |
| 480 | + pFactoryWebGPU->AttachToWebGPUDevice(wgpuInstance, nullptr, wgpuDevice, EngineCI, &m_pDevice, ppContexts.data()); |
| 481 | +# else |
| 482 | + EngineCI.AdapterId = FindAdapter(pFactoryWebGPU, EngineCI.GraphicsAPIVersion, m_AdapterAttribs); |
| 483 | + pFactoryWebGPU->CreateDeviceAndContextsWebGPU(EngineCI, &m_pDevice, ppContexts.data()); |
| 484 | +# endif |
| 485 | + if (!m_pDevice) |
| 486 | + { |
| 487 | + LOG_ERROR_AND_THROW("Unable to initialize Diligent Engine in WebGPU mode. The API may not be available, " |
| 488 | + "or required features may not be supported by this GPU/driver/OS version."); |
| 489 | + } |
| 490 | + |
| 491 | + if (!m_pSwapChain && pWindow != nullptr) |
| 492 | + pFactoryWebGPU->CreateSwapChainWebGPU(m_pDevice, ppContexts[0], m_SwapChainInitDesc, *pWindow, &m_pSwapChain); |
| 493 | + } |
| 494 | + break; |
| 495 | +#endif |
445 | 496 | default: |
446 | 497 | LOG_ERROR_AND_THROW("Unknown device type"); |
447 | 498 | break; |
@@ -664,7 +715,8 @@ SampleApp::CommandLineStatus SampleApp::ProcessCommandLine(int argc, const char* |
664 | 715 | {"gl", RENDER_DEVICE_TYPE_GL}, |
665 | 716 | {"gles", RENDER_DEVICE_TYPE_GLES}, |
666 | 717 | {"vk", RENDER_DEVICE_TYPE_VULKAN}, |
667 | | - {"mtl", RENDER_DEVICE_TYPE_METAL} // |
| 718 | + {"mtl", RENDER_DEVICE_TYPE_METAL}, |
| 719 | + {"wgpu", RENDER_DEVICE_TYPE_WEBGPU} // |
668 | 720 | }; |
669 | 721 | return ArgsParser.ParseEnum("mode", 'm', DeviceTypeEnumVals, m_DeviceType); |
670 | 722 | }); |
@@ -711,6 +763,13 @@ SampleApp::CommandLineStatus SampleApp::ProcessCommandLine(int argc, const char* |
711 | 763 | LOG_ERROR_MESSAGE("Metal is not supported. Please select another device type"); |
712 | 764 | } |
713 | 765 | #endif |
| 766 | +#if !WEBGPU_SUPPORTED |
| 767 | + if (m_DeviceType == RENDER_DEVICE_TYPE_WEBGPU) |
| 768 | + { |
| 769 | + m_DeviceType = RENDER_DEVICE_TYPE_UNDEFINED; |
| 770 | + LOG_ERROR_MESSAGE("WebGPU is not supported. Please select another device type"); |
| 771 | + } |
| 772 | +#endif |
714 | 773 |
|
715 | 774 | if (ArgsParser.Parse("capture_path", m_ScreenCaptureInfo.Directory)) |
716 | 775 | m_ScreenCaptureInfo.AllowCapture = true; |
@@ -784,6 +843,8 @@ SampleApp::CommandLineStatus SampleApp::ProcessCommandLine(int argc, const char* |
784 | 843 | m_DeviceType = RENDER_DEVICE_TYPE_D3D11; |
785 | 844 | #elif GL_SUPPORTED || GLES_SUPPORTED |
786 | 845 | m_DeviceType = RENDER_DEVICE_TYPE_GL; |
| 846 | +#elif WEBGPU_SUPPORTED |
| 847 | + m_DeviceType = RENDER_DEVICE_TYPE_WEBGPU; |
787 | 848 | #endif |
788 | 849 | } |
789 | 850 | } |
|
0 commit comments