mirror of
https://github.com/encounter/aurora.git
synced 2025-07-07 13:46:08 +00:00
Update dawn & SDL part 1
This commit is contained in:
parent
ca822a7679
commit
3edd2c0e58
@ -60,7 +60,6 @@ endif ()
|
|||||||
target_link_libraries(aurora PRIVATE absl::btree absl::flat_hash_map)
|
target_link_libraries(aurora PRIVATE absl::btree absl::flat_hash_map)
|
||||||
if (DAWN_ENABLE_VULKAN)
|
if (DAWN_ENABLE_VULKAN)
|
||||||
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_VULKAN)
|
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_VULKAN)
|
||||||
target_sources(aurora PRIVATE lib/dawn/VulkanBinding.cpp)
|
|
||||||
endif ()
|
endif ()
|
||||||
if (DAWN_ENABLE_METAL)
|
if (DAWN_ENABLE_METAL)
|
||||||
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_METAL)
|
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_METAL)
|
||||||
@ -69,7 +68,6 @@ if (DAWN_ENABLE_METAL)
|
|||||||
endif ()
|
endif ()
|
||||||
if (DAWN_ENABLE_D3D12)
|
if (DAWN_ENABLE_D3D12)
|
||||||
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_D3D12)
|
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_D3D12)
|
||||||
target_sources(aurora PRIVATE lib/dawn/D3D12Binding.cpp)
|
|
||||||
endif ()
|
endif ()
|
||||||
if (DAWN_ENABLE_DESKTOP_GL OR DAWN_ENABLE_OPENGLES)
|
if (DAWN_ENABLE_DESKTOP_GL OR DAWN_ENABLE_OPENGLES)
|
||||||
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_OPENGL)
|
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_OPENGL)
|
||||||
@ -79,11 +77,9 @@ if (DAWN_ENABLE_DESKTOP_GL OR DAWN_ENABLE_OPENGLES)
|
|||||||
if (DAWN_ENABLE_OPENGLES)
|
if (DAWN_ENABLE_OPENGLES)
|
||||||
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_OPENGLES)
|
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_OPENGLES)
|
||||||
endif ()
|
endif ()
|
||||||
target_sources(aurora PRIVATE lib/dawn/OpenGLBinding.cpp)
|
|
||||||
endif ()
|
endif ()
|
||||||
if (DAWN_ENABLE_NULL)
|
if (DAWN_ENABLE_NULL)
|
||||||
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_NULL)
|
target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_NULL)
|
||||||
target_sources(aurora PRIVATE lib/dawn/NullBinding.cpp)
|
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
add_library(aurora_main STATIC lib/main.cpp)
|
add_library(aurora_main STATIC lib/main.cpp)
|
||||||
|
2
extern/SDL
vendored
2
extern/SDL
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 34458fe9f4a11a5b15ced6193e4c5d7ef97cca36
|
Subproject commit 9b3d146ea1949831d11eaf04c38d167f4efc6549
|
2
extern/dawn
vendored
2
extern/dawn
vendored
@ -1 +1 @@
|
|||||||
Subproject commit db82c79db65e502dd226f5cb4e2c03d85f8ffd2d
|
Subproject commit 0bd776df12d2f747b17437c6501751fdee70f14d
|
@ -1,5 +1,6 @@
|
|||||||
#include "BackendBinding.hpp"
|
#include "BackendBinding.hpp"
|
||||||
|
|
||||||
|
#include <SDL_syswm.h>
|
||||||
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
||||||
#include <dawn/native/D3D12Backend.h>
|
#include <dawn/native/D3D12Backend.h>
|
||||||
#endif
|
#endif
|
||||||
@ -19,24 +20,6 @@
|
|||||||
|
|
||||||
namespace aurora::webgpu::utils {
|
namespace aurora::webgpu::utils {
|
||||||
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
|
||||||
BackendBinding* CreateD3D12Binding(SDL_Window* window, WGPUDevice device);
|
|
||||||
#endif
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
|
||||||
BackendBinding* CreateMetalBinding(SDL_Window* window, WGPUDevice device);
|
|
||||||
#endif
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
|
||||||
BackendBinding* CreateNullBinding(SDL_Window* window, WGPUDevice device);
|
|
||||||
#endif
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
|
|
||||||
BackendBinding* CreateOpenGLBinding(SDL_Window* window, WGPUDevice device);
|
|
||||||
#endif
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
|
||||||
BackendBinding* CreateVulkanBinding(SDL_Window* window, WGPUDevice device);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
BackendBinding::BackendBinding(SDL_Window* window, WGPUDevice device) : m_window(window), m_device(device) {}
|
|
||||||
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
|
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
|
||||||
struct GLUserData {
|
struct GLUserData {
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
@ -53,24 +36,24 @@ void GLDestroy(void* userData) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool DiscoverAdapter(dawn::native::Instance* instance, SDL_Window* window, wgpu::BackendType type) {
|
bool DiscoverAdapter(dawn::native::Instance* instance, [[maybe_unused]] SDL_Window* window, wgpu::BackendType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
||||||
case wgpu::BackendType::D3D12: {
|
case wgpu::BackendType::D3D12: {
|
||||||
dawn::native::d3d12::AdapterDiscoveryOptions options;
|
dawn::native::d3d12::PhysicalDeviceDiscoveryOptions options;
|
||||||
return instance->DiscoverAdapters(&options);
|
return instance->DiscoverPhysicalDevices(&options);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
||||||
case wgpu::BackendType::Metal: {
|
case wgpu::BackendType::Metal: {
|
||||||
dawn::native::metal::AdapterDiscoveryOptions options;
|
dawn::native::metal::PhysicalDeviceDiscoveryOptions options;
|
||||||
return instance->DiscoverAdapters(&options);
|
return instance->DiscoverPhysicalDevices(&options);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
||||||
case wgpu::BackendType::Vulkan: {
|
case wgpu::BackendType::Vulkan: {
|
||||||
dawn::native::vulkan::AdapterDiscoveryOptions options;
|
dawn::native::vulkan::PhysicalDeviceDiscoveryOptions options;
|
||||||
return instance->DiscoverAdapters(&options);
|
return instance->DiscoverPhysicalDevices(&options);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
|
#if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
|
||||||
@ -111,7 +94,7 @@ bool DiscoverAdapter(dawn::native::Instance* instance, SDL_Window* window, wgpu:
|
|||||||
#endif
|
#endif
|
||||||
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
||||||
case wgpu::BackendType::Null:
|
case wgpu::BackendType::Null:
|
||||||
instance->DiscoverDefaultAdapters();
|
instance->DiscoverDefaultPhysicalDevices();
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
@ -119,35 +102,43 @@ bool DiscoverAdapter(dawn::native::Instance* instance, SDL_Window* window, wgpu:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BackendBinding* CreateBinding(wgpu::BackendType type, SDL_Window* window, WGPUDevice device) {
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorCocoa(SDL_Window* window);
|
||||||
switch (type) {
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptor(SDL_Window* window) {
|
||||||
case wgpu::BackendType::D3D12:
|
#if _WIN32
|
||||||
return CreateD3D12Binding(window, device);
|
std::unique_ptr<wgpu::SurfaceDescriptorFromWindowsHWND> desc =
|
||||||
|
std::make_unique<wgpu::SurfaceDescriptorFromWindowsHWND>();
|
||||||
|
desc->hwnd = glfwGetWin32Window(window);
|
||||||
|
desc->hinstance = GetModuleHandle(nullptr);
|
||||||
|
return std::move(desc);
|
||||||
|
#elif defined(DAWN_ENABLE_BACKEND_METAL)
|
||||||
|
return SetupWindowAndGetSurfaceDescriptorCocoa(window);
|
||||||
|
#elif defined(DAWN_USE_WAYLAND) || defined(DAWN_USE_X11)
|
||||||
|
#if defined(GLFW_PLATFORM_WAYLAND) && defined(DAWN_USE_WAYLAND)
|
||||||
|
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) {
|
||||||
|
std::unique_ptr<wgpu::SurfaceDescriptorFromWaylandSurface> desc =
|
||||||
|
std::make_unique<wgpu::SurfaceDescriptorFromWaylandSurface>();
|
||||||
|
desc->display = glfwGetWaylandDisplay();
|
||||||
|
desc->surface = glfwGetWaylandWindow(window);
|
||||||
|
return std::move(desc);
|
||||||
|
} else // NOLINT(readability/braces)
|
||||||
#endif
|
#endif
|
||||||
#if defined(DAWN_ENABLE_BACKEND_METAL)
|
#if defined(DAWN_USE_X11)
|
||||||
case wgpu::BackendType::Metal:
|
{
|
||||||
return CreateMetalBinding(window, device);
|
std::unique_ptr<wgpu::SurfaceDescriptorFromXlibWindow> desc =
|
||||||
#endif
|
std::make_unique<wgpu::SurfaceDescriptorFromXlibWindow>();
|
||||||
#if defined(DAWN_ENABLE_BACKEND_NULL)
|
desc->display = glfwGetX11Display();
|
||||||
case wgpu::BackendType::Null:
|
desc->window = glfwGetX11Window(window);
|
||||||
return CreateNullBinding(window, device);
|
return std::move(desc);
|
||||||
#endif
|
}
|
||||||
#if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
|
#else
|
||||||
case wgpu::BackendType::OpenGL:
|
{
|
||||||
return CreateOpenGLBinding(window, device);
|
|
||||||
#endif
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_OPENGLES)
|
|
||||||
case wgpu::BackendType::OpenGLES:
|
|
||||||
return CreateOpenGLBinding(window, device);
|
|
||||||
#endif
|
|
||||||
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
|
|
||||||
case wgpu::BackendType::Vulkan:
|
|
||||||
return CreateVulkanBinding(window, device);
|
|
||||||
#endif
|
|
||||||
default:
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
return nullptr;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aurora::webgpu::utils
|
} // namespace aurora::webgpu::utils
|
||||||
|
@ -7,21 +7,7 @@ struct SDL_Window;
|
|||||||
|
|
||||||
namespace aurora::webgpu::utils {
|
namespace aurora::webgpu::utils {
|
||||||
|
|
||||||
class BackendBinding {
|
|
||||||
public:
|
|
||||||
virtual ~BackendBinding() = default;
|
|
||||||
|
|
||||||
virtual uint64_t GetSwapChainImplementation() = 0;
|
|
||||||
virtual WGPUTextureFormat GetPreferredSwapChainTextureFormat() = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
BackendBinding(SDL_Window* window, WGPUDevice device);
|
|
||||||
|
|
||||||
SDL_Window* m_window = nullptr;
|
|
||||||
WGPUDevice m_device = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool DiscoverAdapter(dawn::native::Instance* instance, SDL_Window* window, wgpu::BackendType type);
|
bool DiscoverAdapter(dawn::native::Instance* instance, SDL_Window* window, wgpu::BackendType type);
|
||||||
BackendBinding* CreateBinding(wgpu::BackendType type, SDL_Window* window, WGPUDevice device);
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptor(SDL_Window* window);
|
||||||
|
|
||||||
} // namespace aurora::webgpu::utils
|
} // namespace aurora::webgpu::utils
|
||||||
|
@ -1,108 +1,13 @@
|
|||||||
#include "BackendBinding.hpp"
|
#include "BackendBinding.hpp"
|
||||||
|
|
||||||
#include <SDL_metal.h>
|
#include <SDL_metal.h>
|
||||||
#include <dawn/native/MetalBackend.h>
|
|
||||||
|
|
||||||
#import <QuartzCore/CAMetalLayer.h>
|
|
||||||
|
|
||||||
template <typename T> DawnSwapChainImplementation CreateSwapChainImplementation(T *swapChain) {
|
|
||||||
DawnSwapChainImplementation impl = {};
|
|
||||||
impl.userData = swapChain;
|
|
||||||
impl.Init = [](void *userData, void *wsiContext) {
|
|
||||||
auto *ctx = static_cast<typename T::WSIContext *>(wsiContext);
|
|
||||||
reinterpret_cast<T *>(userData)->Init(ctx);
|
|
||||||
};
|
|
||||||
impl.Destroy = [](void *userData) { delete reinterpret_cast<T *>(userData); };
|
|
||||||
impl.Configure = [](void *userData, WGPUTextureFormat format, WGPUTextureUsage allowedUsage, uint32_t width,
|
|
||||||
uint32_t height) {
|
|
||||||
return static_cast<T *>(userData)->Configure(format, allowedUsage, width, height);
|
|
||||||
};
|
|
||||||
impl.GetNextTexture = [](void *userData, DawnSwapChainNextTexture *nextTexture) {
|
|
||||||
return static_cast<T *>(userData)->GetNextTexture(nextTexture);
|
|
||||||
};
|
|
||||||
impl.Present = [](void *userData) { return static_cast<T *>(userData)->Present(); };
|
|
||||||
return impl;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace aurora::webgpu::utils {
|
namespace aurora::webgpu::utils {
|
||||||
class SwapChainImplMTL {
|
std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorCocoa(SDL_Window* window) {
|
||||||
public:
|
SDL_MetalView view = SDL_Metal_CreateView(window);
|
||||||
using WSIContext = DawnWSIContextMetal;
|
std::unique_ptr<wgpu::SurfaceDescriptorFromMetalLayer> desc =
|
||||||
|
std::make_unique<wgpu::SurfaceDescriptorFromMetalLayer>();
|
||||||
explicit SwapChainImplMTL(SDL_Window *window) : m_view(SDL_Metal_CreateView(window)) {}
|
desc->layer = SDL_Metal_GetLayer(view);
|
||||||
|
return std::move(desc);
|
||||||
~SwapChainImplMTL() { SDL_Metal_DestroyView(m_view); }
|
}
|
||||||
|
|
||||||
void Init(DawnWSIContextMetal *ctx) {
|
|
||||||
mMtlDevice = ctx->device;
|
|
||||||
mCommandQueue = ctx->queue;
|
|
||||||
}
|
|
||||||
|
|
||||||
DawnSwapChainError Configure(WGPUTextureFormat format, WGPUTextureUsage usage, uint32_t width, uint32_t height) {
|
|
||||||
if (format != WGPUTextureFormat_BGRA8Unorm) {
|
|
||||||
return "unsupported format";
|
|
||||||
}
|
|
||||||
assert(width > 0);
|
|
||||||
assert(height > 0);
|
|
||||||
|
|
||||||
CGSize size = {};
|
|
||||||
size.width = width;
|
|
||||||
size.height = height;
|
|
||||||
|
|
||||||
mLayer = (__bridge CAMetalLayer *)(SDL_Metal_GetLayer(m_view));
|
|
||||||
[mLayer setDevice:mMtlDevice];
|
|
||||||
[mLayer setPixelFormat:MTLPixelFormatBGRA8Unorm];
|
|
||||||
[mLayer setDrawableSize:size];
|
|
||||||
|
|
||||||
constexpr uint32_t kFramebufferOnlyTextureUsages = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_Present;
|
|
||||||
bool hasOnlyFramebufferUsages = (usage & (~kFramebufferOnlyTextureUsages)) == 0u;
|
|
||||||
if (hasOnlyFramebufferUsages) {
|
|
||||||
[mLayer setFramebufferOnly:YES];
|
|
||||||
}
|
|
||||||
|
|
||||||
return DAWN_SWAP_CHAIN_NO_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
DawnSwapChainError GetNextTexture(DawnSwapChainNextTexture *nextTexture) {
|
|
||||||
mCurrentDrawable = [mLayer nextDrawable];
|
|
||||||
mCurrentTexture = mCurrentDrawable.texture;
|
|
||||||
nextTexture->texture.ptr = (__bridge void *)(mCurrentTexture);
|
|
||||||
return DAWN_SWAP_CHAIN_NO_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
DawnSwapChainError Present() {
|
|
||||||
id<MTLCommandBuffer> commandBuffer = [mCommandQueue commandBuffer];
|
|
||||||
[commandBuffer presentDrawable:mCurrentDrawable];
|
|
||||||
[commandBuffer commit];
|
|
||||||
return DAWN_SWAP_CHAIN_NO_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
SDL_MetalView m_view = nil;
|
|
||||||
id<MTLDevice> mMtlDevice = nil;
|
|
||||||
id<MTLCommandQueue> mCommandQueue = nil;
|
|
||||||
|
|
||||||
CAMetalLayer *mLayer = nullptr;
|
|
||||||
id<CAMetalDrawable> mCurrentDrawable = nil;
|
|
||||||
id<MTLTexture> mCurrentTexture = nil;
|
|
||||||
};
|
|
||||||
|
|
||||||
class MetalBinding : public BackendBinding {
|
|
||||||
public:
|
|
||||||
MetalBinding(SDL_Window *window, WGPUDevice device) : BackendBinding(window, device) {}
|
|
||||||
|
|
||||||
uint64_t GetSwapChainImplementation() override {
|
|
||||||
if (m_swapChainImpl.userData == nullptr) {
|
|
||||||
m_swapChainImpl = CreateSwapChainImplementation(new SwapChainImplMTL(m_window));
|
|
||||||
}
|
|
||||||
return reinterpret_cast<uint64_t>(&m_swapChainImpl);
|
|
||||||
}
|
|
||||||
|
|
||||||
WGPUTextureFormat GetPreferredSwapChainTextureFormat() override { return WGPUTextureFormat_BGRA8Unorm; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
DawnSwapChainImplementation m_swapChainImpl{};
|
|
||||||
};
|
|
||||||
|
|
||||||
BackendBinding *CreateMetalBinding(SDL_Window *window, WGPUDevice device) { return new MetalBinding(window, device); }
|
|
||||||
} // namespace aurora::webgpu::utils
|
} // namespace aurora::webgpu::utils
|
||||||
|
@ -342,6 +342,77 @@ static void pipeline_worker() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load serialized pipeline cache
|
||||||
|
void load_pipeline_cache() {
|
||||||
|
ByteBuffer pipelineCache;
|
||||||
|
u32 pipelineCacheCount = 0;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::string path = std::string{g_config.configPath} + "/pipeline_cache.bin";
|
||||||
|
std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate);
|
||||||
|
if (file) {
|
||||||
|
const auto size = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
constexpr size_t headerSize = sizeof(pipelineCacheCount);
|
||||||
|
if (size != -1 && size > headerSize) {
|
||||||
|
pipelineCache.append_zeroes(size_t(size) - headerSize);
|
||||||
|
file.read(reinterpret_cast<char*>(&pipelineCacheCount), headerSize);
|
||||||
|
file.read(reinterpret_cast<char*>(pipelineCache.data()), size_t(size) - headerSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pipelineCacheCount > 0) {
|
||||||
|
size_t offset = 0;
|
||||||
|
while (offset < pipelineCache.size()) {
|
||||||
|
ShaderType type = *reinterpret_cast<const ShaderType*>(pipelineCache.data() + offset);
|
||||||
|
offset += sizeof(ShaderType);
|
||||||
|
u32 size = *reinterpret_cast<const u32*>(pipelineCache.data() + offset);
|
||||||
|
offset += sizeof(u32);
|
||||||
|
switch (type) {
|
||||||
|
case ShaderType::Stream: {
|
||||||
|
if (size != sizeof(stream::PipelineConfig)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const auto config = *reinterpret_cast<const stream::PipelineConfig*>(pipelineCache.data() + offset);
|
||||||
|
if (config.version != gx::GXPipelineConfigVersion) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
find_pipeline(
|
||||||
|
type, config, [=]() { return stream::create_pipeline(g_state.stream, config); }, true);
|
||||||
|
} break;
|
||||||
|
case ShaderType::Model: {
|
||||||
|
if (size != sizeof(model::PipelineConfig)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const auto config = *reinterpret_cast<const model::PipelineConfig*>(pipelineCache.data() + offset);
|
||||||
|
if (config.version != gx::GXPipelineConfigVersion) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
find_pipeline(
|
||||||
|
type, config, [=]() { return model::create_pipeline(g_state.model, config); }, true);
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
Log.report(LOG_WARNING, FMT_STRING("Unknown pipeline type {}"), static_cast<int>(type));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
offset += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write serialized pipelines to file
|
||||||
|
void save_pipeline_cache() {
|
||||||
|
const auto path = std::string{g_config.configPath} + "pipeline_cache.bin";
|
||||||
|
std::ofstream file(path, std::ios::out | std::ios::trunc | std::ios::binary);
|
||||||
|
if (file) {
|
||||||
|
file.write(reinterpret_cast<const char*>(&g_serializedPipelineCount), sizeof(g_serializedPipelineCount));
|
||||||
|
file.write(reinterpret_cast<const char*>(g_serializedPipelines.data()), g_serializedPipelines.size());
|
||||||
|
}
|
||||||
|
g_serializedPipelines.clear();
|
||||||
|
g_serializedPipelineCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void initialize() {
|
void initialize() {
|
||||||
// No async pipelines for OpenGL (ES)
|
// No async pipelines for OpenGL (ES)
|
||||||
if (webgpu::g_backendType == wgpu::BackendType::OpenGL || webgpu::g_backendType == wgpu::BackendType::OpenGLES ||
|
if (webgpu::g_backendType == wgpu::BackendType::OpenGL || webgpu::g_backendType == wgpu::BackendType::OpenGLES ||
|
||||||
@ -385,58 +456,7 @@ void initialize() {
|
|||||||
g_state.stream = stream::construct_state();
|
g_state.stream = stream::construct_state();
|
||||||
g_state.model = model::construct_state();
|
g_state.model = model::construct_state();
|
||||||
|
|
||||||
{
|
load_pipeline_cache();
|
||||||
// Load serialized pipeline cache
|
|
||||||
std::string path = std::string{g_config.configPath} + "/pipeline_cache.bin";
|
|
||||||
std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate);
|
|
||||||
if (file) {
|
|
||||||
const auto size = file.tellg();
|
|
||||||
file.seekg(0, std::ios::beg);
|
|
||||||
constexpr size_t headerSize = sizeof(g_serializedPipelineCount);
|
|
||||||
if (size != -1 && size > headerSize) {
|
|
||||||
g_serializedPipelines.append_zeroes(size_t(size) - headerSize);
|
|
||||||
file.read(reinterpret_cast<char*>(&g_serializedPipelineCount), headerSize);
|
|
||||||
file.read(reinterpret_cast<char*>(g_serializedPipelines.data()), size_t(size) - headerSize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (g_serializedPipelineCount > 0) {
|
|
||||||
size_t offset = 0;
|
|
||||||
while (offset < g_serializedPipelines.size()) {
|
|
||||||
ShaderType type = *reinterpret_cast<const ShaderType*>(g_serializedPipelines.data() + offset);
|
|
||||||
offset += sizeof(ShaderType);
|
|
||||||
u32 size = *reinterpret_cast<const u32*>(g_serializedPipelines.data() + offset);
|
|
||||||
offset += sizeof(u32);
|
|
||||||
switch (type) {
|
|
||||||
case ShaderType::Stream: {
|
|
||||||
if (size != sizeof(stream::PipelineConfig)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const auto config = *reinterpret_cast<const stream::PipelineConfig*>(g_serializedPipelines.data() + offset);
|
|
||||||
if (config.version != gx::GXPipelineConfigVersion) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
find_pipeline(
|
|
||||||
type, config, [=]() { return stream::create_pipeline(g_state.stream, config); }, false);
|
|
||||||
} break;
|
|
||||||
case ShaderType::Model: {
|
|
||||||
if (size != sizeof(model::PipelineConfig)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const auto config = *reinterpret_cast<const model::PipelineConfig*>(g_serializedPipelines.data() + offset);
|
|
||||||
if (config.version != gx::GXPipelineConfigVersion) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
find_pipeline(
|
|
||||||
type, config, [=]() { return model::create_pipeline(g_state.model, config); }, false);
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
Log.report(LOG_WARNING, FMT_STRING("Unknown pipeline type {}"), static_cast<int>(type));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
offset += size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void shutdown() {
|
void shutdown() {
|
||||||
@ -446,17 +466,7 @@ void shutdown() {
|
|||||||
g_pipelineThread.join();
|
g_pipelineThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
save_pipeline_cache();
|
||||||
// Write serialized pipelines to file
|
|
||||||
const auto path = std::string{g_config.configPath} + "pipeline_cache.bin";
|
|
||||||
std::ofstream file(path, std::ios::out | std::ios::trunc | std::ios::binary);
|
|
||||||
if (file) {
|
|
||||||
file.write(reinterpret_cast<const char*>(&g_serializedPipelineCount), sizeof(g_serializedPipelineCount));
|
|
||||||
file.write(reinterpret_cast<const char*>(g_serializedPipelines.data()), g_serializedPipelines.size());
|
|
||||||
}
|
|
||||||
g_serializedPipelines.clear();
|
|
||||||
g_serializedPipelineCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
gx::shutdown();
|
gx::shutdown();
|
||||||
|
|
||||||
|
@ -155,8 +155,8 @@ struct TextureRef;
|
|||||||
using TextureHandle = std::shared_ptr<TextureRef>;
|
using TextureHandle = std::shared_ptr<TextureRef>;
|
||||||
|
|
||||||
enum class ShaderType {
|
enum class ShaderType {
|
||||||
Stream,
|
Stream = 'STRM',
|
||||||
Model,
|
Model = 'CMDL',
|
||||||
};
|
};
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
|
@ -688,21 +688,21 @@ static wgpu::AddressMode wgpu_address_mode(GXTexWrapMode mode) {
|
|||||||
return wgpu::AddressMode::MirrorRepeat;
|
return wgpu::AddressMode::MirrorRepeat;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static std::pair<wgpu::FilterMode, wgpu::FilterMode> wgpu_filter_mode(GXTexFilter filter) {
|
static std::pair<wgpu::FilterMode, wgpu::MipmapFilterMode> wgpu_filter_mode(GXTexFilter filter) {
|
||||||
switch (filter) {
|
switch (filter) {
|
||||||
DEFAULT_FATAL("invalid filter mode {}", static_cast<int>(filter));
|
DEFAULT_FATAL("invalid filter mode {}", static_cast<int>(filter));
|
||||||
case GX_NEAR:
|
case GX_NEAR:
|
||||||
return {wgpu::FilterMode::Nearest, wgpu::FilterMode::Linear};
|
return {wgpu::FilterMode::Nearest, wgpu::MipmapFilterMode::Linear};
|
||||||
case GX_LINEAR:
|
case GX_LINEAR:
|
||||||
return {wgpu::FilterMode::Linear, wgpu::FilterMode::Linear};
|
return {wgpu::FilterMode::Linear, wgpu::MipmapFilterMode::Linear};
|
||||||
case GX_NEAR_MIP_NEAR:
|
case GX_NEAR_MIP_NEAR:
|
||||||
return {wgpu::FilterMode::Nearest, wgpu::FilterMode::Nearest};
|
return {wgpu::FilterMode::Nearest, wgpu::MipmapFilterMode::Nearest};
|
||||||
case GX_LIN_MIP_NEAR:
|
case GX_LIN_MIP_NEAR:
|
||||||
return {wgpu::FilterMode::Linear, wgpu::FilterMode::Nearest};
|
return {wgpu::FilterMode::Linear, wgpu::MipmapFilterMode::Nearest};
|
||||||
case GX_NEAR_MIP_LIN:
|
case GX_NEAR_MIP_LIN:
|
||||||
return {wgpu::FilterMode::Nearest, wgpu::FilterMode::Linear};
|
return {wgpu::FilterMode::Nearest, wgpu::MipmapFilterMode::Linear};
|
||||||
case GX_LIN_MIP_LIN:
|
case GX_LIN_MIP_LIN:
|
||||||
return {wgpu::FilterMode::Linear, wgpu::FilterMode::Linear};
|
return {wgpu::FilterMode::Linear, wgpu::MipmapFilterMode::Linear};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static u16 wgpu_aniso(GXAnisotropy aniso) {
|
static u16 wgpu_aniso(GXAnisotropy aniso) {
|
||||||
@ -725,7 +725,7 @@ wgpu::SamplerDescriptor TextureBind::get_descriptor() const noexcept {
|
|||||||
.addressModeW = wgpu::AddressMode::Repeat,
|
.addressModeW = wgpu::AddressMode::Repeat,
|
||||||
.magFilter = wgpu::FilterMode::Nearest,
|
.magFilter = wgpu::FilterMode::Nearest,
|
||||||
.minFilter = wgpu::FilterMode::Nearest,
|
.minFilter = wgpu::FilterMode::Nearest,
|
||||||
.mipmapFilter = wgpu::FilterMode::Nearest,
|
.mipmapFilter = wgpu::MipmapFilterMode::Nearest,
|
||||||
.lodMinClamp = 0.f,
|
.lodMinClamp = 0.f,
|
||||||
.lodMaxClamp = 1000.f,
|
.lodMaxClamp = 1000.f,
|
||||||
.maxAnisotropy = 1,
|
.maxAnisotropy = 1,
|
||||||
|
@ -78,6 +78,7 @@ void process_event(const SDL_Event& event) noexcept {
|
|||||||
void new_frame(const AuroraWindowSize& size) noexcept {
|
void new_frame(const AuroraWindowSize& size) noexcept {
|
||||||
if (g_useSdlRenderer) {
|
if (g_useSdlRenderer) {
|
||||||
ImGui_ImplSDLRenderer_NewFrame();
|
ImGui_ImplSDLRenderer_NewFrame();
|
||||||
|
g_scale = size.scale;
|
||||||
} else {
|
} else {
|
||||||
if (g_scale != size.scale) {
|
if (g_scale != size.scale) {
|
||||||
if (g_scale > 0.f) {
|
if (g_scale > 0.f) {
|
||||||
|
@ -438,7 +438,7 @@ static void ImGui_ImplWGPU_CreateFontsTexture()
|
|||||||
WGPUSamplerDescriptor sampler_desc = {};
|
WGPUSamplerDescriptor sampler_desc = {};
|
||||||
sampler_desc.minFilter = WGPUFilterMode_Linear;
|
sampler_desc.minFilter = WGPUFilterMode_Linear;
|
||||||
sampler_desc.magFilter = WGPUFilterMode_Linear;
|
sampler_desc.magFilter = WGPUFilterMode_Linear;
|
||||||
sampler_desc.mipmapFilter = WGPUFilterMode_Linear;
|
sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Linear;
|
||||||
sampler_desc.addressModeU = WGPUAddressMode_Repeat;
|
sampler_desc.addressModeU = WGPUAddressMode_Repeat;
|
||||||
sampler_desc.addressModeV = WGPUAddressMode_Repeat;
|
sampler_desc.addressModeV = WGPUAddressMode_Repeat;
|
||||||
sampler_desc.addressModeW = WGPUAddressMode_Repeat;
|
sampler_desc.addressModeW = WGPUAddressMode_Repeat;
|
||||||
|
@ -35,11 +35,10 @@ wgpu::BindGroup g_CopyBindGroup;
|
|||||||
#ifdef WEBGPU_DAWN
|
#ifdef WEBGPU_DAWN
|
||||||
static std::unique_ptr<dawn::native::Instance> g_dawnInstance;
|
static std::unique_ptr<dawn::native::Instance> g_dawnInstance;
|
||||||
static dawn::native::Adapter g_adapter;
|
static dawn::native::Adapter g_adapter;
|
||||||
static std::unique_ptr<utils::BackendBinding> g_backendBinding;
|
|
||||||
#else
|
#else
|
||||||
wgpu::Instance g_instance;
|
|
||||||
static wgpu::Adapter g_adapter;
|
static wgpu::Adapter g_adapter;
|
||||||
#endif
|
#endif
|
||||||
|
wgpu::Instance g_instance;
|
||||||
static wgpu::Surface g_surface;
|
static wgpu::Surface g_surface;
|
||||||
static wgpu::AdapterProperties g_adapterProperties;
|
static wgpu::AdapterProperties g_adapterProperties;
|
||||||
|
|
||||||
@ -81,7 +80,7 @@ TextureWithSampler create_render_texture(bool multisampled) {
|
|||||||
.addressModeW = wgpu::AddressMode::ClampToEdge,
|
.addressModeW = wgpu::AddressMode::ClampToEdge,
|
||||||
.magFilter = wgpu::FilterMode::Linear,
|
.magFilter = wgpu::FilterMode::Linear,
|
||||||
.minFilter = wgpu::FilterMode::Linear,
|
.minFilter = wgpu::FilterMode::Linear,
|
||||||
.mipmapFilter = wgpu::FilterMode::Linear,
|
.mipmapFilter = wgpu::MipmapFilterMode::Linear,
|
||||||
.lodMinClamp = 0.f,
|
.lodMinClamp = 0.f,
|
||||||
.lodMaxClamp = 1000.f,
|
.lodMaxClamp = 1000.f,
|
||||||
.maxAnisotropy = 1,
|
.maxAnisotropy = 1,
|
||||||
@ -130,7 +129,7 @@ static TextureWithSampler create_depth_texture() {
|
|||||||
.addressModeW = wgpu::AddressMode::ClampToEdge,
|
.addressModeW = wgpu::AddressMode::ClampToEdge,
|
||||||
.magFilter = wgpu::FilterMode::Linear,
|
.magFilter = wgpu::FilterMode::Linear,
|
||||||
.minFilter = wgpu::FilterMode::Linear,
|
.minFilter = wgpu::FilterMode::Linear,
|
||||||
.mipmapFilter = wgpu::FilterMode::Linear,
|
.mipmapFilter = wgpu::MipmapFilterMode::Linear,
|
||||||
.lodMinClamp = 0.f,
|
.lodMinClamp = 0.f,
|
||||||
.lodMaxClamp = 1000.f,
|
.lodMaxClamp = 1000.f,
|
||||||
.maxAnisotropy = 1,
|
.maxAnisotropy = 1,
|
||||||
@ -344,7 +343,7 @@ bool initialize(AuroraBackend auroraBackend) {
|
|||||||
g_instance = {}; // TODO use wgpuCreateInstance when supported
|
g_instance = {}; // TODO use wgpuCreateInstance when supported
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
wgpu::BackendType backend = to_wgpu_backend(auroraBackend);
|
const wgpu::BackendType backend = to_wgpu_backend(auroraBackend);
|
||||||
#ifdef EMSCRIPTEN
|
#ifdef EMSCRIPTEN
|
||||||
if (backend != wgpu::BackendType::WebGPU) {
|
if (backend != wgpu::BackendType::WebGPU) {
|
||||||
Log.report(LOG_WARNING, FMT_STRING("Backend type {} unsupported"), magic_enum::enum_name(backend));
|
Log.report(LOG_WARNING, FMT_STRING("Backend type {} unsupported"), magic_enum::enum_name(backend));
|
||||||
@ -388,6 +387,12 @@ bool initialize(AuroraBackend auroraBackend) {
|
|||||||
}
|
}
|
||||||
g_adapter = *adapterIt;
|
g_adapter = *adapterIt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto chainedDescriptor = utils::SetupWindowAndGetSurfaceDescriptor(window);
|
||||||
|
wgpu::SurfaceDescriptor surfaceDescriptor;
|
||||||
|
surfaceDescriptor.nextInChain = chainedDescriptor.get();
|
||||||
|
g_surface = g_instance.CreateSurface(&surfaceDescriptor);
|
||||||
|
ASSERT(g_surface, "Failed to initialize surface");
|
||||||
#else
|
#else
|
||||||
const WGPUSurfaceDescriptorFromCanvasHTMLSelector canvasDescriptor{
|
const WGPUSurfaceDescriptorFromCanvasHTMLSelector canvasDescriptor{
|
||||||
.chain = {.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector},
|
.chain = {.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector},
|
||||||
@ -476,9 +481,9 @@ bool initialize(AuroraBackend auroraBackend) {
|
|||||||
"disable_symbol_renaming",
|
"disable_symbol_renaming",
|
||||||
/* clang-format on */
|
/* clang-format on */
|
||||||
};
|
};
|
||||||
wgpu::DawnTogglesDeviceDescriptor togglesDescriptor{};
|
wgpu::DawnTogglesDescriptor togglesDescriptor{};
|
||||||
togglesDescriptor.forceEnabledTogglesCount = enableToggles.size();
|
togglesDescriptor.enabledTogglesCount = enableToggles.size();
|
||||||
togglesDescriptor.forceEnabledToggles = enableToggles.data();
|
togglesDescriptor.enabledToggles = enableToggles.data();
|
||||||
#endif
|
#endif
|
||||||
const wgpu::DeviceDescriptor deviceDescriptor{
|
const wgpu::DeviceDescriptor deviceDescriptor{
|
||||||
#ifdef WEBGPU_DAWN
|
#ifdef WEBGPU_DAWN
|
||||||
@ -508,12 +513,7 @@ bool initialize(AuroraBackend auroraBackend) {
|
|||||||
g_queue = g_device.GetQueue();
|
g_queue = g_device.GetQueue();
|
||||||
|
|
||||||
#if WEBGPU_DAWN
|
#if WEBGPU_DAWN
|
||||||
g_backendBinding =
|
auto swapChainFormat = wgpu::TextureFormat::BGRA8UnormSrgb; // TODO
|
||||||
std::unique_ptr<utils::BackendBinding>(utils::CreateBinding(g_backendType, window, g_device.Get()));
|
|
||||||
if (!g_backendBinding) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto swapChainFormat = static_cast<wgpu::TextureFormat>(g_backendBinding->GetPreferredSwapChainTextureFormat());
|
|
||||||
#else
|
#else
|
||||||
auto swapChainFormat = g_surface.GetPreferredFormat(g_adapter);
|
auto swapChainFormat = g_surface.GetPreferredFormat(g_adapter);
|
||||||
#endif
|
#endif
|
||||||
@ -532,9 +532,6 @@ bool initialize(AuroraBackend auroraBackend) {
|
|||||||
.width = size.fb_width,
|
.width = size.fb_width,
|
||||||
.height = size.fb_height,
|
.height = size.fb_height,
|
||||||
.presentMode = wgpu::PresentMode::Fifo,
|
.presentMode = wgpu::PresentMode::Fifo,
|
||||||
#ifdef WEBGPU_DAWN
|
|
||||||
.implementation = g_backendBinding->GetSwapChainImplementation(),
|
|
||||||
#endif
|
|
||||||
},
|
},
|
||||||
.depthFormat = wgpu::TextureFormat::Depth32Float,
|
.depthFormat = wgpu::TextureFormat::Depth32Float,
|
||||||
.msaaSamples = g_config.msaa,
|
.msaaSamples = g_config.msaa,
|
||||||
@ -556,11 +553,10 @@ void shutdown() {
|
|||||||
wgpuQueueRelease(g_queue.Release());
|
wgpuQueueRelease(g_queue.Release());
|
||||||
wgpuDeviceDestroy(g_device.Release());
|
wgpuDeviceDestroy(g_device.Release());
|
||||||
g_adapter = {};
|
g_adapter = {};
|
||||||
|
g_surface = {};
|
||||||
#ifdef WEBGPU_DAWN
|
#ifdef WEBGPU_DAWN
|
||||||
g_backendBinding.reset();
|
|
||||||
g_dawnInstance.reset();
|
g_dawnInstance.reset();
|
||||||
#else
|
#else
|
||||||
g_surface = {};
|
|
||||||
g_instance = {};
|
g_instance = {};
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -572,15 +568,7 @@ void resize_swapchain(uint32_t width, uint32_t height, bool force) {
|
|||||||
}
|
}
|
||||||
g_graphicsConfig.swapChainDescriptor.width = width;
|
g_graphicsConfig.swapChainDescriptor.width = width;
|
||||||
g_graphicsConfig.swapChainDescriptor.height = height;
|
g_graphicsConfig.swapChainDescriptor.height = height;
|
||||||
#ifdef WEBGPU_DAWN
|
|
||||||
if (!g_swapChain) {
|
|
||||||
g_swapChain = g_device.CreateSwapChain(g_surface, &g_graphicsConfig.swapChainDescriptor);
|
g_swapChain = g_device.CreateSwapChain(g_surface, &g_graphicsConfig.swapChainDescriptor);
|
||||||
}
|
|
||||||
g_swapChain.Configure(g_graphicsConfig.swapChainDescriptor.format, g_graphicsConfig.swapChainDescriptor.usage, width,
|
|
||||||
height);
|
|
||||||
#else
|
|
||||||
g_swapChain = g_device.CreateSwapChain(g_surface, &g_graphicsConfig.swapChainDescriptor);
|
|
||||||
#endif
|
|
||||||
g_frameBuffer = create_render_texture(true);
|
g_frameBuffer = create_render_texture(true);
|
||||||
g_frameBufferResolved = create_render_texture(false);
|
g_frameBufferResolved = create_render_texture(false);
|
||||||
g_depthBuffer = create_depth_texture();
|
g_depthBuffer = create_depth_texture();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user