Merge pull request #82 from wheremyfoodat/toml

Add configs
This commit is contained in:
wheremyfoodat 2023-07-08 16:58:35 +03:00 committed by GitHub
commit a12b11cd67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 91 additions and 9 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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

View file

@ -1,5 +1,7 @@
#pragma once
#include <array>
#include "config.hpp"
#include "helpers.hpp"
#include "logger.hpp"
#include "memory.hpp"
@ -16,6 +18,7 @@ class GPU {
using Registers = std::array<u32, regNum>;
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(); }

10
include/config.hpp Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#include <filesystem>
// Remember to initialize every 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);
};

View file

@ -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();

View file

@ -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;

55
src/config.cpp Normal file
View file

@ -0,0 +1,55 @@
#include "config.hpp"
#include <fstream>
#include "helpers.hpp"
#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
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<toml::value>(data.at("GPU"));
if (gpuResult.is_ok()) {
auto gpu = gpuResult.unwrap();
shaderJitEnabled = toml::find_or<toml::boolean>(gpu, "EnableShaderJIT", false);
}
}
}
void EmulatorConfig::save(const std::filesystem::path& path) {
toml::basic_value<toml::preserve_comments> data;
if (std::filesystem::exists(path)) {
try {
data = toml::parse<toml::preserve_comments>(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();
}

View file

@ -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<true, true>();
else
drawArrays<true, false>();
} else {
if constexpr (ShaderJIT::isAvailable() && shaderJITEnabled)
if (shaderJITEnabled)
drawArrays<false, true>();
else
drawArrays<false, false>();

View file

@ -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();

1
third_party/toml11 vendored Submodule

@ -0,0 +1 @@
Subproject commit 1340692442b530ada93a63f4b76a1421b3c139fe