2022-07-27 15:25:25 +00:00
|
|
|
#include "BackendBinding.hpp"
|
|
|
|
|
2023-05-27 15:44:36 +00:00
|
|
|
#include <SDL_syswm.h>
|
2023-05-27 18:57:24 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2023-06-29 00:02:15 +00:00
|
|
|
#if defined(DAWN_ENABLE_BACKEND_D3D11)
|
|
|
|
#include <dawn/native/D3D11Backend.h>
|
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
|
|
|
#include <dawn/native/D3D12Backend.h>
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
|
|
|
#include <dawn/native/MetalBackend.h>
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
|
|
|
#include <dawn/native/VulkanBackend.h>
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
|
|
|
|
#include <SDL_video.h>
|
|
|
|
#include <dawn/native/OpenGLBackend.h>
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
|
|
|
#include <dawn/native/NullBackend.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace aurora::webgpu::utils {
|
|
|
|
|
2022-08-29 21:07:21 +00:00
|
|
|
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
|
|
|
|
struct GLUserData {
|
|
|
|
SDL_Window* window;
|
|
|
|
SDL_GLContext context;
|
|
|
|
};
|
|
|
|
void GLMakeCurrent(void* userData) {
|
|
|
|
auto* data = static_cast<GLUserData*>(userData);
|
|
|
|
SDL_GL_MakeCurrent(data->window, data->context);
|
|
|
|
}
|
|
|
|
void GLDestroy(void* userData) {
|
|
|
|
auto* data = static_cast<GLUserData*>(userData);
|
|
|
|
SDL_GL_DeleteContext(data->context);
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-05-27 15:44:36 +00:00
|
|
|
bool DiscoverAdapter(dawn::native::Instance* instance, [[maybe_unused]] SDL_Window* window, wgpu::BackendType type) {
|
2022-07-27 15:25:25 +00:00
|
|
|
switch (type) {
|
2023-06-29 00:02:15 +00:00
|
|
|
#if defined(DAWN_ENABLE_BACKEND_D3D11)
|
|
|
|
case wgpu::BackendType::D3D11: {
|
|
|
|
dawn::native::d3d11::PhysicalDeviceDiscoveryOptions options;
|
|
|
|
return instance->DiscoverPhysicalDevices(&options);
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::BackendType::D3D12: {
|
2023-05-27 15:44:36 +00:00
|
|
|
dawn::native::d3d12::PhysicalDeviceDiscoveryOptions options;
|
|
|
|
return instance->DiscoverPhysicalDevices(&options);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::BackendType::Metal: {
|
2023-05-27 15:44:36 +00:00
|
|
|
dawn::native::metal::PhysicalDeviceDiscoveryOptions options;
|
|
|
|
return instance->DiscoverPhysicalDevices(&options);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::BackendType::Vulkan: {
|
2023-05-27 15:44:36 +00:00
|
|
|
dawn::native::vulkan::PhysicalDeviceDiscoveryOptions options;
|
|
|
|
return instance->DiscoverPhysicalDevices(&options);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::BackendType::OpenGL: {
|
2022-07-27 15:25:25 +00:00
|
|
|
SDL_GL_ResetAttributes();
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
|
2022-08-29 21:07:21 +00:00
|
|
|
SDL_GLContext context = SDL_GL_CreateContext(window);
|
2023-05-27 18:57:24 +00:00
|
|
|
dawn::native::opengl::PhysicalDeviceDiscoveryOptions options{WGPUBackendType_OpenGL};
|
|
|
|
options.getProc = SDL_GL_GetProcAddress;
|
|
|
|
options.makeCurrent = GLMakeCurrent;
|
|
|
|
options.destroy = GLDestroy;
|
|
|
|
options.userData = new GLUserData{
|
2022-08-29 21:07:21 +00:00
|
|
|
.window = window,
|
|
|
|
.context = context,
|
|
|
|
};
|
2023-05-27 18:57:24 +00:00
|
|
|
return instance->DiscoverPhysicalDevices(&options);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_OPENGLES)
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::BackendType::OpenGLES: {
|
2022-07-27 15:25:25 +00:00
|
|
|
SDL_GL_ResetAttributes();
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
2022-08-29 21:07:21 +00:00
|
|
|
SDL_GLContext context = SDL_GL_CreateContext(window);
|
2023-05-27 18:57:24 +00:00
|
|
|
dawn::native::opengl::PhysicalDeviceDiscoveryOptions options{WGPUBackendType_OpenGLES};
|
|
|
|
options.getProc = SDL_GL_GetProcAddress;
|
|
|
|
options.makeCurrent = GLMakeCurrent;
|
|
|
|
options.destroy = GLDestroy;
|
|
|
|
options.userData = new GLUserData{
|
2022-08-29 21:07:21 +00:00
|
|
|
.window = window,
|
|
|
|
.context = context,
|
|
|
|
};
|
2023-05-27 18:57:24 +00:00
|
|
|
return instance->DiscoverPhysicalDevices(&options);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::BackendType::Null:
|
2023-05-27 15:44:36 +00:00
|
|
|
instance->DiscoverDefaultPhysicalDevices();
|
2022-07-27 15:25:25 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 15:44:36 +00:00
|
|
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorCocoa(SDL_Window* window);
|
|
|
|
|
|
|
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptor(SDL_Window* window) {
|
2023-05-27 18:57:24 +00:00
|
|
|
#if defined(SDL_VIDEO_DRIVER_COCOA)
|
|
|
|
return SetupWindowAndGetSurfaceDescriptorCocoa(window);
|
|
|
|
#else
|
|
|
|
SDL_SysWMinfo wmInfo;
|
|
|
|
SDL_VERSION(&wmInfo.version);
|
|
|
|
if (SDL_GetWindowWMInfo(window, &wmInfo) == SDL_FALSE) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#if defined(SDL_VIDEO_DRIVER_WINDOWS)
|
2023-05-27 15:44:36 +00:00
|
|
|
std::unique_ptr<wgpu::SurfaceDescriptorFromWindowsHWND> desc =
|
|
|
|
std::make_unique<wgpu::SurfaceDescriptorFromWindowsHWND>();
|
2023-06-29 00:02:15 +00:00
|
|
|
desc->hwnd = wmInfo.info.win.window;
|
|
|
|
desc->hinstance = wmInfo.info.win.hinstance;
|
2023-05-27 15:44:36 +00:00
|
|
|
return std::move(desc);
|
2023-05-27 18:57:24 +00:00
|
|
|
#elif defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_X11)
|
|
|
|
#if defined(SDL_VIDEO_DRIVER_WAYLAND)
|
|
|
|
if (wmInfo.subsystem == SDL_SYSWM_WAYLAND) {
|
2023-05-27 15:44:36 +00:00
|
|
|
std::unique_ptr<wgpu::SurfaceDescriptorFromWaylandSurface> desc =
|
|
|
|
std::make_unique<wgpu::SurfaceDescriptorFromWaylandSurface>();
|
2023-05-27 18:57:24 +00:00
|
|
|
desc->display = wmInfo.info.wl.display;
|
|
|
|
desc->surface = wmInfo.info.wl.surface;
|
2023-05-27 15:44:36 +00:00
|
|
|
return std::move(desc);
|
2023-05-27 18:57:24 +00:00
|
|
|
}
|
2023-05-27 15:44:36 +00:00
|
|
|
#endif
|
2023-05-27 18:57:24 +00:00
|
|
|
#if defined(SDL_VIDEO_DRIVER_X11)
|
|
|
|
if (wmInfo.subsystem == SDL_SYSWM_X11) {
|
2023-05-27 15:44:36 +00:00
|
|
|
std::unique_ptr<wgpu::SurfaceDescriptorFromXlibWindow> desc =
|
|
|
|
std::make_unique<wgpu::SurfaceDescriptorFromXlibWindow>();
|
2023-05-27 18:57:24 +00:00
|
|
|
desc->display = wmInfo.info.x11.display;
|
|
|
|
desc->window = wmInfo.info.x11.window;
|
2023-05-27 15:44:36 +00:00
|
|
|
return std::move(desc);
|
|
|
|
}
|
|
|
|
#endif
|
2023-05-27 18:57:24 +00:00
|
|
|
return nullptr;
|
2023-05-27 15:44:36 +00:00
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2023-05-27 18:57:24 +00:00
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aurora::webgpu::utils
|