From 46a93166ef96bc521fd7784f0953181312af2c5e Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Wed, 8 Jun 2022 10:58:38 -0700 Subject: [PATCH] Restore graphicsApi, msaa, and textureAniso cvars, fix a few deserialization bugs in CVar.cpp --- Runtime/CMain.cpp | 4 +- Runtime/ConsoleVariables/CVar.cpp | 6 +- Runtime/ImGuiEntitySupport.cpp | 15 ++++- Runtime/Weapon/CPlayerGun.hpp | 2 + aurora/CMakeLists.txt | 2 +- aurora/include/aurora/aurora.hpp | 16 +++-- aurora/lib/aurora.cpp | 108 +++++++++++++++++++++++++++--- aurora/lib/gfx/common.cpp | 7 +- aurora/lib/gpu.cpp | 7 +- aurora/lib/gpu.hpp | 2 +- 10 files changed, 137 insertions(+), 32 deletions(-) diff --git a/Runtime/CMain.cpp b/Runtime/CMain.cpp index 31f0ca59b..6a5b377e7 100644 --- a/Runtime/CMain.cpp +++ b/Runtime/CMain.cpp @@ -568,7 +568,9 @@ int main(int argc, char** argv) { .width = icon.width, .height = icon.height, }; - aurora::app_run(std::move(app), std::move(data), argc, argv, fileMgr.getStoreRoot()); + aurora::app_run(std::move(app), std::move(data), argc, argv, fileMgr.getStoreRoot(), + aurora::translate_backend(cvarCmns.getGraphicsApi()), cvarCmns.getSamples(), + cvarCmns.getAnisotropy()); return 0; } #endif diff --git a/Runtime/ConsoleVariables/CVar.cpp b/Runtime/ConsoleVariables/CVar.cpp index d8cf81d5d..81e1fa6f4 100644 --- a/Runtime/ConsoleVariables/CVar.cpp +++ b/Runtime/ConsoleVariables/CVar.cpp @@ -434,7 +434,7 @@ void CVar::dispatch() { bool isReal(std::string_view v) { char* p; std::strtod(v.data(), &p); - return *p == 0; + return p != nullptr && *p == 0; } bool isReal(const std::vector& v) { for (auto& s : v) { @@ -455,10 +455,10 @@ bool CVar::isValidInput(std::string_view input) const { } case EType::Signed: std::strtol(input.data(), &p, 0); - return p == nullptr; + return p != nullptr && *p == 0; case EType::Unsigned: std::strtoul(input.data(), &p, 0); - return p == nullptr; + return p != nullptr && *p == 0; case EType::Real: { bool size = parts.size() == 1; bool ret = isReal(input); diff --git a/Runtime/ImGuiEntitySupport.cpp b/Runtime/ImGuiEntitySupport.cpp index 4975e3a3f..52ffd588f 100644 --- a/Runtime/ImGuiEntitySupport.cpp +++ b/Runtime/ImGuiEntitySupport.cpp @@ -751,7 +751,20 @@ IMGUI_ENTITY_INSPECT(MP1::CBouncyGrenade, CPhysicsActor, BouncyGrenade, {}) IMGUI_ENTITY_INSPECT(CCollisionActor, CPhysicsActor, CollisionActor, {}) IMGUI_ENTITY_INSPECT(MP1::CGrenadeLauncher, CPhysicsActor, GrenadeLauncher, {}) IMGUI_ENTITY_INSPECT(MP1::CMetroidPrimeExo::CPhysicsDummy, CPhysicsActor, MetroidPrimeExoPhysicsDummy, {}) -IMGUI_ENTITY_INSPECT(CPlayer, CPhysicsActor, Player, {}) +IMGUI_ENTITY_INSPECT(CPlayer, CPhysicsActor, Player, { + if (ImGui::CollapsingHeader("Player Gun")) { + auto* gun = GetPlayerGun(); + ImGui::Text("Last Fire Button States: 0x%08X", gun->x2ec_lastFireButtonStates); + ImGui::Text("Pressed Fire Button States: 0x%08X", gun->x2f0_pressedFireButtonStates); + ImGui::Text("Fire Button States: 0x%08X", gun->x2f4_fireButtonStates); + ImGui::Text("State Flags: 0x%08X", gun->x2f8_stateFlags); + ImGui::Text("Fidget Anim Bits: 0x%08X", gun->x2fc_fidgetAnimBits); + ImGui::Text("Remaining Missiles: %i", gun->x300_remainingMissiles); + ImGui::Text("Bomb Count: %i", gun->x308_bombCount); + ImGui::Text("Current Beam: %s", magic_enum::enum_name(gun->x310_currentBeam).data()); + ImGui::Text("Next Beam: %s", magic_enum::enum_name(gun->x314_nextBeam).data()); + } +}) IMGUI_ENTITY_INSPECT(CScriptActor, CPhysicsActor, ScriptActor, { if (ImGui::Button("Edit Damage Vulnerability")) { m_editingDamageVulnerability = true; diff --git a/Runtime/Weapon/CPlayerGun.hpp b/Runtime/Weapon/CPlayerGun.hpp index 0882150eb..dde92cfa8 100644 --- a/Runtime/Weapon/CPlayerGun.hpp +++ b/Runtime/Weapon/CPlayerGun.hpp @@ -31,6 +31,8 @@ namespace metaforce { struct CFinalInput; class CPlayerGun { + // For ImGuiEntitySupport + friend class CPlayer; public: static float skTractorBeamFactor; enum class EMissileMode { Inactive, Active }; diff --git a/aurora/CMakeLists.txt b/aurora/CMakeLists.txt index 40498711c..4a05a078a 100644 --- a/aurora/CMakeLists.txt +++ b/aurora/CMakeLists.txt @@ -36,7 +36,7 @@ if (DAWN_ENABLE_D3D12) target_sources(aurora PRIVATE lib/dawn/D3D12Binding.cpp) endif () if (DAWN_ENABLE_DESKTOP_GL) - target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_OPENGL DAWN_ENABLE_BACKEND_DESKTOP_GL) + target_compile_definitions(aurora PRIVATE DAWN_ENABLE_BACKEND_OPENGL DAWN_ENABLE_BACKEND_DESKTOP_GL DAWN_ENABLE_BACKEND_OPENGLES) target_sources(aurora PRIVATE lib/dawn/OpenGLBinding.cpp) endif () if (DAWN_ENABLE_NULL) diff --git a/aurora/include/aurora/aurora.hpp b/aurora/include/aurora/aurora.hpp index f2fab8578..e56af2e86 100644 --- a/aurora/include/aurora/aurora.hpp +++ b/aurora/include/aurora/aurora.hpp @@ -191,17 +191,17 @@ enum class MouseButton { ENABLE_BITWISE_ENUM(MouseButton); enum class Backend : uint8_t { - Invalid, - Vulkan, - Metal, - D3D12, + Null, + WebGPU, D3D11, + D3D12, + Metal, + Vulkan, OpenGL, OpenGLES, - WebGPU, + Invalid = 0xFF, }; -struct App; struct AppDelegate { AppDelegate() = default; virtual ~AppDelegate() noexcept = default; @@ -242,12 +242,14 @@ struct AppDelegate { virtual void onControllerAxis(uint32_t which, ControllerAxis axis, int16_t value) noexcept = 0; }; -void app_run(std::unique_ptr app, Icon icon, int argc, char** argv, std::string_view configPath) noexcept; +void app_run(std::unique_ptr app, Icon icon, int argc, char** argv, std::string_view configPath, + Backend desiredBackend = Backend::Invalid, uint32_t msaa = 1, uint16_t aniso = 16) noexcept; [[nodiscard]] std::vector get_args() noexcept; [[nodiscard]] WindowSize get_window_size() noexcept; void set_window_title(zstring_view title) noexcept; [[nodiscard]] Backend get_backend() noexcept; [[nodiscard]] std::string_view get_backend_string() noexcept; +[[nodiscard]] Backend translate_backend(std::string_view name); void set_fullscreen(bool fullscreen) noexcept; [[nodiscard]] uint32_t get_which_controller_for_player(int32_t index) noexcept; [[nodiscard]] int32_t get_controller_player_index(uint32_t which) noexcept; diff --git a/aurora/lib/aurora.cpp b/aurora/lib/aurora.cpp index 9c4ef91a5..0579a4741 100644 --- a/aurora/lib/aurora.cpp +++ b/aurora/lib/aurora.cpp @@ -1,10 +1,12 @@ #include + #include "gfx/common.hpp" #include "gfx/gx.hpp" #include "gpu.hpp" #include "input.hpp" #include "imgui.hpp" +#include #include #include #include @@ -253,7 +255,29 @@ static SDL_Window* create_window(wgpu::BackendType type) { return SDL_CreateWindow("Metaforce", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 960, flags); } -void app_run(std::unique_ptr app, Icon icon, int argc, char** argv, std::string_view configPath) noexcept { +wgpu::BackendType to_wgpu_backend(Backend backend) { + switch (backend) { + case Backend::WebGPU: + return wgpu::BackendType::WebGPU; + case Backend::D3D11: + return wgpu::BackendType::D3D11; + case Backend::D3D12: + return wgpu::BackendType::D3D12; + case Backend::Metal: + return wgpu::BackendType::Metal; + case Backend::Vulkan: + return wgpu::BackendType::Vulkan; + case Backend::OpenGL: + return wgpu::BackendType::OpenGL; + case Backend::OpenGLES: + return wgpu::BackendType::OpenGLES; + default: + case Backend::Null: + return wgpu::BackendType::Null; + } +} +void app_run(std::unique_ptr app, Icon icon, int argc, char** argv, std::string_view configPath, + Backend desiredBackend, uint32_t msaa, uint16_t aniso) noexcept { g_AppDelegate = std::move(app); /* Lets gather arguments skipping the program filename */ for (size_t i = 1; i < argc; ++i) { @@ -280,18 +304,31 @@ void app_run(std::unique_ptr app, Icon icon, int argc, char** argv, /* TODO: Make this an option rather than hard coding it */ SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); - for (const auto backendType : gpu::PreferredBackendOrder) { - auto* window = create_window(backendType); - if (window == nullptr) { - continue; + /* Attempt to create a window using the calling application's desired backend */ + if (desiredBackend != Backend::Invalid) { + auto wgpuBackend = to_wgpu_backend(desiredBackend); + g_window = create_window(wgpuBackend); + if (g_window != nullptr && !gpu::initialize(g_window, wgpuBackend, msaa, aniso)) { + g_window = nullptr; + SDL_DestroyWindow(g_window); } - g_window = window; - if (gpu::initialize(window, backendType)) { - break; - } - g_window = nullptr; - SDL_DestroyWindow(window); } + + if (g_window == nullptr) { + for (const auto backendType : gpu::PreferredBackendOrder) { + auto* window = create_window(backendType); + if (window == nullptr) { + continue; + } + g_window = window; + if (gpu::initialize(window, backendType, msaa, aniso)) { + break; + } + g_window = nullptr; + SDL_DestroyWindow(window); + } + } + if (g_window == nullptr) { Log.report(logvisor::Fatal, FMT_STRING("Error creating window: {}"), SDL_GetError()); unreachable(); @@ -438,6 +475,55 @@ Backend get_backend() noexcept { std::string_view get_backend_string() noexcept { return magic_enum::enum_name(gpu::g_backendType); } +Backend translate_backend(std::string_view backend) { + if (backend.empty()) { + return Backend::Invalid; + } + std::string tmp = backend.data(); + std::transform(tmp.begin(), tmp.end(), tmp.begin(), [](unsigned char c) { return std::tolower(c); }); +#if DAWN_ENABLE_BACKEND_WEBGPU + if (tmp == "webgpu" || tmp == "wgpu") { + return Backend::WebGPU; + } +#endif +#if DAWN_ENABLE_BACKEND_D3D11 + if (tmp == "d3d11") { + return Backend::D3D11; + } +#endif +#if DAWN_ENABLE_BACKEND_D3D12 + if (tmp == "d3d12") { + return Backend::D3D12; + } +#endif +#if DAWN_ENABLE_BACKEND_METAL + if (tmp == "metal") { + return Backend::Metal; + } +#endif +#if DAWN_ENABLE_BACKEND_DESKTOP_GL || DAWN_ENABLE_BACKEND_OPENGL + if (tmp == "gl" || tmp == "opengl") { + return Backend::OpenGL; + } +#endif +#if DAWN_ENABLE_BACKEND_OPENGLES + if (tmp == "gles" || tmp == "opengles") { + return Backend::OpenGLES; + } +#endif +#if DAWN_ENABLE_BACKEND_VULKAN + if (tmp == "vulkan") { + return Backend::Vulkan; + } +#endif +#if DAWN_ENABLE_BACKEND_NULL + if (tmp == "null") { + return Backend::Null; + } +#endif + return Backend::Invalid; +} + void set_fullscreen(bool fullscreen) noexcept { SDL_SetWindowFullscreen(g_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0); } diff --git a/aurora/lib/gfx/common.cpp b/aurora/lib/gfx/common.cpp index 2b4a008e5..003ca97ea 100644 --- a/aurora/lib/gfx/common.cpp +++ b/aurora/lib/gfx/common.cpp @@ -313,7 +313,8 @@ static void pipeline_worker() { void initialize() { // No async pipelines for OpenGL (ES) - if (gpu::g_backendType != wgpu::BackendType::OpenGL && gpu::g_backendType != wgpu::BackendType::OpenGLES) { + if (gpu::g_backendType != wgpu::BackendType::OpenGL && gpu::g_backendType != wgpu::BackendType::OpenGLES && + gpu::g_backendType != wgpu::BackendType::Vulkan) { g_pipelineThread = std::thread(pipeline_worker); g_hasPipelineThread = true; } @@ -758,9 +759,7 @@ const wgpu::Sampler& sampler_ref(const wgpu::SamplerDescriptor& descriptor) { return it->second; } -uint32_t align_uniform(uint32_t value) { - return ALIGN(value, g_cachedLimits.limits.minUniformBufferOffsetAlignment); -} +uint32_t align_uniform(uint32_t value) { return ALIGN(value, g_cachedLimits.limits.minUniformBufferOffsetAlignment); } void push_debug_group(zstring_view label) noexcept { #ifdef AURORA_GFX_DEBUG_GROUPS diff --git a/aurora/lib/gpu.cpp b/aurora/lib/gpu.cpp index cb5f2a3c1..3ad5403ae 100644 --- a/aurora/lib/gpu.cpp +++ b/aurora/lib/gpu.cpp @@ -240,7 +240,7 @@ static void device_callback(WGPURequestDeviceStatus status, WGPUDevice device, c *static_cast(userdata) = true; } -bool initialize(SDL_Window* window, wgpu::BackendType backendType) { +bool initialize(SDL_Window* window, wgpu::BackendType backendType, uint32_t msaa, uint16_t aniso) { if (!g_Instance) { Log.report(logvisor::Info, FMT_STRING("Creating Dawn instance")); g_Instance = std::make_unique(); @@ -343,6 +343,7 @@ bool initialize(SDL_Window* window, wgpu::BackendType backendType) { } g_device.SetUncapturedErrorCallback(&error_callback, nullptr); } + g_device.SetDeviceLostCallback(nullptr, nullptr); g_queue = g_device.GetQueue(); g_BackendBinding = @@ -372,8 +373,8 @@ bool initialize(SDL_Window* window, wgpu::BackendType backendType) { .height = size.fb_height, .colorFormat = swapChainFormat, .depthFormat = wgpu::TextureFormat::Depth32Float, - .msaaSamples = 1, - .textureAnisotropy = 16, + .msaaSamples = msaa, + .textureAnisotropy = aniso, }; create_copy_pipeline(); resize_swapchain(size.fb_width, size.fb_height); diff --git a/aurora/lib/gpu.hpp b/aurora/lib/gpu.hpp index fb6f85639..ba1653652 100644 --- a/aurora/lib/gpu.hpp +++ b/aurora/lib/gpu.hpp @@ -63,7 +63,7 @@ extern TextureWithSampler g_depthBuffer; extern wgpu::RenderPipeline g_CopyPipeline; extern wgpu::BindGroup g_CopyBindGroup; -bool initialize(SDL_Window* window, wgpu::BackendType backendType); +bool initialize(SDL_Window* window, wgpu::BackendType backendType, uint32_t msaa, uint16_t aniso); void shutdown(); void resize_swapchain(uint32_t width, uint32_t height); TextureWithSampler create_render_texture(bool multisampled);