From 693da0b59b6cb9c23671f98414e0f40fb04a277e Mon Sep 17 00:00:00 2001 From: Luke Street Date: Tue, 22 Feb 2022 01:28:28 -0500 Subject: [PATCH] aurora: Refactor Dawn private API hacks --- aurora/CMakeLists.txt | 5 +++++ aurora/lib/dawn/Hacks.cpp | 29 +++++++++++++++++++++++++++++ aurora/lib/dawn/Hacks.hpp | 10 ++++++++++ aurora/lib/gpu.cpp | 19 ++----------------- 4 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 aurora/lib/dawn/Hacks.cpp create mode 100644 aurora/lib/dawn/Hacks.hpp diff --git a/aurora/CMakeLists.txt b/aurora/CMakeLists.txt index 3f62a3f00..f01238a28 100644 --- a/aurora/CMakeLists.txt +++ b/aurora/CMakeLists.txt @@ -21,6 +21,7 @@ add_library(aurora STATIC lib/imgui.cpp lib/input.cpp lib/dawn/BackendBinding.cpp + lib/dawn/Hacks.cpp lib/gfx/common.cpp lib/gfx/texture.cpp lib/gfx/movie_player/shader.cpp @@ -33,6 +34,10 @@ add_library(aurora STATIC target_compile_definitions(aurora PRIVATE IMGUI_USER_CONFIG="imconfig_user.h") # IMGUI_USE_WCHAR32 target_include_directories(aurora PUBLIC include ../Runtime) target_include_directories(aurora PRIVATE ../imgui ../extern/imgui) +target_include_directories(aurora PRIVATE + ../extern/dawn/src + ../extern/dawn/third_party/abseil-cpp + ${CMAKE_CURRENT_BINARY_DIR}/dawn/gen/src) # for hacks :) target_link_libraries(aurora PRIVATE dawn_native dawncpp webgpu_dawn zeus logvisor SDL2-static xxhash) if (APPLE) target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_METAL) diff --git a/aurora/lib/dawn/Hacks.cpp b/aurora/lib/dawn/Hacks.cpp new file mode 100644 index 000000000..f07568b09 --- /dev/null +++ b/aurora/lib/dawn/Hacks.cpp @@ -0,0 +1,29 @@ +#include "Hacks.hpp" + +#include "dawn/native/Device.h" +#ifdef _WIN32 +#include "dawn/native/d3d12/AdapterD3D12.h" +#include "dawn/native/d3d12/BackendD3D12.h" +#include "dawn/native/d3d12/DeviceD3D12.h" +#endif + +namespace dawn::native { +class HackedDevice : public DeviceBase { +public: + void _ForceSetToggle(Toggle toggle, bool isEnabled) { ForceSetToggle(toggle, isEnabled); } +}; +} // namespace dawn::native + +namespace aurora::gpu::hacks { +void apply_toggles(WGPUDevice device) { + auto* hack = static_cast(static_cast(device)); + hack->_ForceSetToggle(dawn::native::Toggle::UseUserDefinedLabelsInBackend, true); +#if _WIN32 + hack->_ForceSetToggle(dawn::native::Toggle::UseDXC, true); + auto* backend = dawn::native::d3d12::ToBackend(hack->GetAdapter())->GetBackend(); + backend->EnsureDxcCompiler(); + backend->EnsureDxcLibrary(); + backend->EnsureDxcValidator(); +#endif +} +} // namespace aurora::gpu::hacks diff --git a/aurora/lib/dawn/Hacks.hpp b/aurora/lib/dawn/Hacks.hpp new file mode 100644 index 000000000..f8074dc07 --- /dev/null +++ b/aurora/lib/dawn/Hacks.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +/** + * Helpers to expose private Dawn APIs + */ +namespace aurora::gpu::hacks { +void apply_toggles(WGPUDevice device); +} // namespace aurora::gpu::hacks diff --git a/aurora/lib/gpu.cpp b/aurora/lib/gpu.cpp index ae6ebdac2..84141587b 100644 --- a/aurora/lib/gpu.cpp +++ b/aurora/lib/gpu.cpp @@ -8,18 +8,7 @@ #include #include "dawn/BackendBinding.hpp" - -// TODO this hack doesn't link on Windows? -#ifndef _WIN32 -// TODO HACK: dawn doesn't expose device toggles -#include "../extern/dawn/src/dawn/native/Toggles.h" -namespace dawn::native { -class DeviceBase { -public: - void SetToggle(Toggle toggle, bool isEnabled); -}; -} // namespace dawn::native -#endif +#include "dawn/Hacks.hpp" namespace aurora::gpu { static logvisor::Module Log("aurora::gpu"); @@ -151,11 +140,7 @@ void initialize(SDL_Window* window) { }; g_device = wgpu::Device::Acquire(g_Adapter.CreateDevice(&deviceDescriptor)); g_device.SetUncapturedErrorCallback(&error_callback, nullptr); -#ifndef _WIN32 - // TODO HACK: dawn doesn't expose device toggles - static_cast(static_cast(g_device.Get())) - ->SetToggle(dawn::native::Toggle::UseUserDefinedLabelsInBackend, true); -#endif + hacks::apply_toggles(g_device.Get()); } g_queue = g_device.GetQueue();