From 90a88eee398aec6f48c261198dc5a8b77b91b768 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 8 Jul 2023 15:00:55 +0300 Subject: [PATCH 1/4] Switch if to if constexpr --- include/renderer_gl/surface_cache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/renderer_gl/surface_cache.hpp b/include/renderer_gl/surface_cache.hpp index c5c98f79..1a7b87f8 100644 --- a/include/renderer_gl/surface_cache.hpp +++ b/include/renderer_gl/surface_cache.hpp @@ -56,7 +56,7 @@ public: // Adds a surface object to the cache and returns it SurfaceType& add(const SurfaceType& surface) { if (size >= capacity) { - if (evictOnOverflow) { // Do a ring buffer if evictOnOverflow is true + if constexpr (evictOnOverflow) { // Do a ring buffer if evictOnOverflow is true auto& e = buffer[evictionIndex]; evictionIndex = (evictionIndex + 1) % capacity; From bc3377ac785d6f6c472f0c38069d566cdd1f5c37 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 8 Jul 2023 16:16:00 +0300 Subject: [PATCH 2/4] Add config class --- .gitmodules | 3 +++ CMakeLists.txt | 7 ++++-- include/config.hpp | 10 +++++++++ include/emulator.hpp | 3 +++ src/config.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++ third_party/toml11 | 1 + 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 include/config.hpp create mode 100644 src/config.cpp create mode 160000 third_party/toml11 diff --git a/.gitmodules b/.gitmodules index 4305ba4d..f8c67366 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "third_party/xbyak"] path = third_party/xbyak url = https://github.com/herumi/xbyak +[submodule "third_party/toml11"] + path = third_party/toml11 + url = https://github.com/ToruNiina/toml11 diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ce91389..8d446ba7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,9 @@ set(SDL_STATIC ON CACHE BOOL "" FORCE) set(SDL_SHARED OFF CACHE BOOL "" FORCE) add_subdirectory(third_party/SDL2) add_subdirectory(third_party/glad) +add_subdirectory(third_party/toml11) include_directories(${SDL2_INCLUDE_DIR}) +include_directories(third_party/toml11) set(BOOST_ROOT "${CMAKE_SOURCE_DIR}/third_party/boost") set(Boost_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/third_party/boost") @@ -83,8 +85,8 @@ else() message(FATAL_ERROR "Currently unsupported CPU architecture") endif() -set(SOURCE_FILES src/main.cpp src/emulator.cpp src/io_file.cpp src/gl_state.cpp src/core/CPU/cpu_dynarmic.cpp - src/core/CPU/dynarmic_cycles.cpp src/core/memory.cpp +set(SOURCE_FILES src/main.cpp src/emulator.cpp src/io_file.cpp src/gl_state.cpp src/config.cpp + src/core/CPU/cpu_dynarmic.cpp src/core/CPU/dynarmic_cycles.cpp src/core/memory.cpp ) set(CRYPTO_SOURCE_FILES src/core/crypto/aes_engine.cpp) set(KERNEL_SOURCE_FILES src/core/kernel/kernel.cpp src/core/kernel/resource_limits.cpp @@ -139,6 +141,7 @@ set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/opengl.hpp inc include/result/result_common.hpp include/result/result_fs.hpp include/result/result_fnd.hpp include/result/result_gsp.hpp include/result/result_kernel.hpp include/result/result_os.hpp include/crypto/aes_engine.hpp include/metaprogramming.hpp include/PICA/pica_vertex.hpp include/gl_state.hpp + include/config.hpp ) set(THIRD_PARTY_SOURCE_FILES third_party/imgui/imgui.cpp diff --git a/include/config.hpp b/include/config.hpp new file mode 100644 index 00000000..51584b9b --- /dev/null +++ b/include/config.hpp @@ -0,0 +1,10 @@ +#pragma once +#include + +// Remember to initialize everything field here to its default value otherwise bad things will happen +struct EmulatorConfig { + bool shaderJitEnabled = false; + + void load(const std::filesystem::path& path); + void save(const std::filesystem::path& path); +}; \ No newline at end of file diff --git a/include/emulator.hpp b/include/emulator.hpp index 309b5db6..33f1ae6e 100644 --- a/include/emulator.hpp +++ b/include/emulator.hpp @@ -9,6 +9,7 @@ #include "PICA/gpu.hpp" #include "cpu.hpp" +#include "config.hpp" #include "crypto/aes_engine.hpp" #include "io_file.hpp" #include "memory.hpp" @@ -24,6 +25,7 @@ class Emulator { Crypto::AESEngine aesEngine; GLStateManager gl; + EmulatorConfig config; SDL_Window* window; SDL_GLContext glContext; SDL_GameController* gameController = nullptr; @@ -49,6 +51,7 @@ class Emulator { public: Emulator(); + ~Emulator(); void step(); void render(); diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 00000000..845774d9 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,53 @@ +#include "config.hpp" + +#include + +#include "helpers.hpp" +#include "toml.hpp" + +// Largely based on https://github.com/nba-emu/NanoBoyAdvance/blob/master/src/platform/core/src/config.cpp + +void EmulatorConfig::load(const std::filesystem::path& path) { + // If the configuration file does not exist, create it and return + if (!std::filesystem::exists(path)) { + save(path); + return; + } + + toml::value data; + + try { + data = toml::parse(path); + } catch (std::exception& ex) { + Helpers::warn("Got exception trying to load config file. Exception: %s\n", ex.what()); + return; + } + + if (data.contains("GPU")) { + auto gpuResult = toml::expect(data.at("GPU")); + if (gpuResult.is_ok()) { + auto gpu = gpuResult.unwrap(); + + shaderJitEnabled = toml::find_or(gpu, "EnableShaderJIT", false); + } + } +} + +void EmulatorConfig::save(const std::filesystem::path& path) { + toml::basic_value data; + + if (std::filesystem::exists(path)) { + try { + data = toml::parse(path); + } catch (std::exception& ex) { + Helpers::warn("Got exception trying to save config file. Exception: %s\n", ex.what()); + return; + } + } + + data["GPU"]["EnableShaderJIT"] = shaderJitEnabled; + + std::ofstream file(path, std::ios::out); + file << data; + file.close(); +} \ No newline at end of file diff --git a/third_party/toml11 b/third_party/toml11 new file mode 160000 index 00000000..13406924 --- /dev/null +++ b/third_party/toml11 @@ -0,0 +1 @@ +Subproject commit 1340692442b530ada93a63f4b76a1421b3c139fe From 1037f93f617b8214bb8f4cd0f95eb36fcc2da4c0 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 8 Jul 2023 16:23:20 +0300 Subject: [PATCH 3/4] Hook shader JIT to config file --- include/PICA/gpu.hpp | 5 ++++- src/core/PICA/gpu.cpp | 8 ++++---- src/emulator.cpp | 6 +++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/PICA/gpu.hpp b/include/PICA/gpu.hpp index 5bc06c47..5f74bdcc 100644 --- a/include/PICA/gpu.hpp +++ b/include/PICA/gpu.hpp @@ -1,5 +1,7 @@ #pragma once #include + +#include "config.hpp" #include "helpers.hpp" #include "logger.hpp" #include "memory.hpp" @@ -16,6 +18,7 @@ class GPU { using Registers = std::array; Memory& mem; + EmulatorConfig& config; ShaderUnit shaderUnit; ShaderJIT shaderJIT; // Doesn't do anything if JIT is disabled or not supported @@ -81,7 +84,7 @@ class GPU { // Set to false by the renderer when the lighting_lut is uploaded ot the GPU bool lightingLUTDirty = false; - GPU(Memory& mem, GLStateManager& gl); + GPU(Memory& mem, GLStateManager& gl, EmulatorConfig& config); void initGraphicsContext() { renderer.initGraphicsContext(); } void getGraphicsContext() { renderer.getGraphicsContext(); } void display() { renderer.display(); } diff --git a/src/core/PICA/gpu.cpp b/src/core/PICA/gpu.cpp index 51e9ab69..ed67067c 100644 --- a/src/core/PICA/gpu.cpp +++ b/src/core/PICA/gpu.cpp @@ -12,7 +12,7 @@ using namespace Floats; // Note: For when we have multiple backends, the GL state manager can stay here and have the constructor for the Vulkan-or-whatever renderer ignore it // Thus, our GLStateManager being here does not negatively impact renderer-agnosticness -GPU::GPU(Memory& mem, GLStateManager& gl) : mem(mem), renderer(*this, gl, regs) { +GPU::GPU(Memory& mem, GLStateManager& gl, EmulatorConfig& config) : mem(mem), renderer(*this, gl, regs), config(config) { vram = new u8[vramSize]; mem.setVRAM(vram); // Give the bus a pointer to our VRAM } @@ -47,15 +47,15 @@ void GPU::reset() { // Call the correct version of drawArrays based on whether this is an indexed draw (first template parameter) // And whether we are going to use the shader JIT (second template parameter) void GPU::drawArrays(bool indexed) { - constexpr bool shaderJITEnabled = false; // TODO: Make a configurable option + const bool shaderJITEnabled = ShaderJIT::isAvailable() && config.shaderJitEnabled; if (indexed) { - if constexpr (ShaderJIT::isAvailable() && shaderJITEnabled) + if (shaderJITEnabled) drawArrays(); else drawArrays(); } else { - if constexpr (ShaderJIT::isAvailable() && shaderJITEnabled) + if (shaderJITEnabled) drawArrays(); else drawArrays(); diff --git a/src/emulator.cpp b/src/emulator.cpp index 24dfc0b5..7ca9e26d 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -10,7 +10,7 @@ _declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1; } #endif -Emulator::Emulator() : kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory, gl), memory(cpu.getTicksRef()) { +Emulator::Emulator() : kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory, gl, config), memory(cpu.getTicksRef()) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) { Helpers::panic("Failed to initialize SDL2"); } @@ -50,9 +50,13 @@ Emulator::Emulator() : kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory } } + config.load(std::filesystem::current_path() / "config.toml"); + reset(); } +Emulator::~Emulator() { config.save(std::filesystem::current_path() / "config.toml"); } + void Emulator::reset() { cpu.reset(); gpu.reset(); From c4878ec4b40388950ff6122d01d1eabec23d42b0 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 8 Jul 2023 16:38:44 +0300 Subject: [PATCH 4/4] Update comments Co-Authored-By: Mireille <13669774+fleroviux@users.noreply.github.com> --- include/config.hpp | 2 +- src/config.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/config.hpp b/include/config.hpp index 51584b9b..bdb697bf 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -1,7 +1,7 @@ #pragma once #include -// Remember to initialize everything field here to its default value otherwise bad things will happen +// Remember to initialize every field here to its default value otherwise bad things will happen struct EmulatorConfig { bool shaderJitEnabled = false; diff --git a/src/config.cpp b/src/config.cpp index 845774d9..9964c1c7 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -6,6 +6,8 @@ #include "toml.hpp" // Largely based on https://github.com/nba-emu/NanoBoyAdvance/blob/master/src/platform/core/src/config.cpp +// We are legally allowed, as per the author's wish, to use the above code without any licensing restrictions +// However we still want to follow the license as closely as possible and offer the proper attributions. void EmulatorConfig::load(const std::filesystem::path& path) { // If the configuration file does not exist, create it and return