2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-05-29 06:53:27 +02:00
|
|
|
|
2022-01-25 20:09:59 +01:00
|
|
|
#include <cstring>
|
2022-03-26 14:03:02 +01:00
|
|
|
#include <fstream>
|
2020-06-30 07:32:24 +02:00
|
|
|
#include <optional>
|
2022-03-26 14:03:02 +01:00
|
|
|
#include <span>
|
2022-01-25 20:09:59 +01:00
|
|
|
|
2020-06-04 17:42:19 +02:00
|
|
|
#include <boost/container_hash/hash.hpp>
|
2022-01-25 20:09:59 +01:00
|
|
|
|
2022-03-26 14:03:02 +01:00
|
|
|
#include <fstream>
|
2020-05-29 06:53:27 +02:00
|
|
|
#include "common/assert.h"
|
2022-03-26 14:03:02 +01:00
|
|
|
#include "common/fs/fs.h"
|
|
|
|
#include "common/fs/path_util.h"
|
2022-11-18 00:21:13 +01:00
|
|
|
#include "common/microprofile.h"
|
2021-04-15 01:07:40 +02:00
|
|
|
#include "common/settings.h"
|
2022-03-05 08:01:13 +01:00
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2020-05-29 06:53:27 +02:00
|
|
|
#include "video_core/macro/macro.h"
|
2020-06-04 17:42:19 +02:00
|
|
|
#include "video_core/macro/macro_hle.h"
|
2020-05-29 06:53:27 +02:00
|
|
|
#include "video_core/macro/macro_interpreter.h"
|
2022-11-06 22:45:36 +01:00
|
|
|
|
|
|
|
#ifdef ARCHITECTURE_x86_64
|
2020-05-29 06:53:27 +02:00
|
|
|
#include "video_core/macro/macro_jit_x64.h"
|
2022-11-06 22:45:36 +01:00
|
|
|
#endif
|
2020-05-29 06:53:27 +02:00
|
|
|
|
2023-01-03 16:01:25 +01:00
|
|
|
MICROPROFILE_DEFINE(MacroHLE, "GPU", "Execute macro HLE", MP_RGB(128, 192, 192));
|
2022-11-18 00:21:13 +01:00
|
|
|
|
2020-05-29 06:53:27 +02:00
|
|
|
namespace Tegra {
|
|
|
|
|
2022-03-26 14:03:02 +01:00
|
|
|
static void Dump(u64 hash, std::span<const u32> code) {
|
|
|
|
const auto base_dir{Common::FS::GetYuzuPath(Common::FS::YuzuPath::DumpDir)};
|
|
|
|
const auto macro_dir{base_dir / "macros"};
|
|
|
|
if (!Common::FS::CreateDir(base_dir) || !Common::FS::CreateDir(macro_dir)) {
|
|
|
|
LOG_ERROR(Common_Filesystem, "Failed to create macro dump directories");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto name{macro_dir / fmt::format("{:016x}.macro", hash)};
|
|
|
|
std::fstream macro_file(name, std::ios::out | std::ios::binary);
|
|
|
|
if (!macro_file) {
|
|
|
|
LOG_ERROR(Common_Filesystem, "Unable to open or create file at {}",
|
|
|
|
Common::FS::PathToUTF8String(name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
macro_file.write(reinterpret_cast<const char*>(code.data()), code.size_bytes());
|
|
|
|
}
|
|
|
|
|
2022-03-05 08:01:13 +01:00
|
|
|
MacroEngine::MacroEngine(Engines::Maxwell3D& maxwell3d_)
|
|
|
|
: hle_macros{std::make_unique<Tegra::HLEMacro>(maxwell3d_)}, maxwell3d{maxwell3d_} {}
|
2020-06-04 17:42:19 +02:00
|
|
|
|
2020-06-05 05:09:52 +02:00
|
|
|
MacroEngine::~MacroEngine() = default;
|
2020-06-04 17:42:19 +02:00
|
|
|
|
2020-05-29 06:53:27 +02:00
|
|
|
void MacroEngine::AddCode(u32 method, u32 data) {
|
|
|
|
uploaded_macro_code[method].push_back(data);
|
|
|
|
}
|
|
|
|
|
2022-05-10 23:07:21 +02:00
|
|
|
void MacroEngine::ClearCode(u32 method) {
|
|
|
|
macro_cache.erase(method);
|
|
|
|
uploaded_macro_code.erase(method);
|
|
|
|
}
|
|
|
|
|
2022-01-25 19:41:35 +01:00
|
|
|
void MacroEngine::Execute(u32 method, const std::vector<u32>& parameters) {
|
2020-05-29 06:53:27 +02:00
|
|
|
auto compiled_macro = macro_cache.find(method);
|
|
|
|
if (compiled_macro != macro_cache.end()) {
|
2020-06-04 17:42:19 +02:00
|
|
|
const auto& cache_info = compiled_macro->second;
|
|
|
|
if (cache_info.has_hle_program) {
|
2022-11-18 00:21:13 +01:00
|
|
|
MICROPROFILE_SCOPE(MacroHLE);
|
2020-06-04 17:42:19 +02:00
|
|
|
cache_info.hle_program->Execute(parameters, method);
|
|
|
|
} else {
|
2022-03-05 08:01:13 +01:00
|
|
|
maxwell3d.RefreshParameters();
|
2020-06-04 17:42:19 +02:00
|
|
|
cache_info.lle_program->Execute(parameters, method);
|
|
|
|
}
|
2020-05-29 06:53:27 +02:00
|
|
|
} else {
|
|
|
|
// Macro not compiled, check if it's uploaded and if so, compile it
|
2020-09-22 23:31:53 +02:00
|
|
|
std::optional<u32> mid_method;
|
2020-06-30 07:32:24 +02:00
|
|
|
const auto macro_code = uploaded_macro_code.find(method);
|
2020-05-29 06:53:27 +02:00
|
|
|
if (macro_code == uploaded_macro_code.end()) {
|
2020-06-30 07:32:24 +02:00
|
|
|
for (const auto& [method_base, code] : uploaded_macro_code) {
|
|
|
|
if (method >= method_base && (method - method_base) < code.size()) {
|
|
|
|
mid_method = method_base;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!mid_method.has_value()) {
|
2022-06-07 23:02:29 +02:00
|
|
|
ASSERT_MSG(false, "Macro 0x{0:x} was not uploaded", method);
|
2020-06-30 07:32:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-05-29 06:53:27 +02:00
|
|
|
}
|
2020-06-04 17:42:19 +02:00
|
|
|
auto& cache_info = macro_cache[method];
|
2020-06-30 07:32:24 +02:00
|
|
|
|
|
|
|
if (!mid_method.has_value()) {
|
|
|
|
cache_info.lle_program = Compile(macro_code->second);
|
|
|
|
cache_info.hash = boost::hash_value(macro_code->second);
|
2022-03-26 14:03:02 +01:00
|
|
|
if (Settings::values.dump_macros) {
|
|
|
|
Dump(cache_info.hash, macro_code->second);
|
|
|
|
}
|
2020-06-30 07:32:24 +02:00
|
|
|
} else {
|
|
|
|
const auto& macro_cached = uploaded_macro_code[mid_method.value()];
|
|
|
|
const auto rebased_method = method - mid_method.value();
|
|
|
|
auto& code = uploaded_macro_code[method];
|
|
|
|
code.resize(macro_cached.size() - rebased_method);
|
|
|
|
std::memcpy(code.data(), macro_cached.data() + rebased_method,
|
|
|
|
code.size() * sizeof(u32));
|
|
|
|
cache_info.hash = boost::hash_value(code);
|
|
|
|
cache_info.lle_program = Compile(code);
|
2022-03-26 14:03:02 +01:00
|
|
|
if (Settings::values.dump_macros) {
|
|
|
|
Dump(cache_info.hash, code);
|
|
|
|
}
|
2020-06-30 07:32:24 +02:00
|
|
|
}
|
2020-06-04 17:42:19 +02:00
|
|
|
|
2023-01-04 20:56:52 +01:00
|
|
|
auto hle_program = hle_macros->GetHLEProgram(cache_info.hash);
|
|
|
|
if (!hle_program || Settings::values.disable_macro_hle) {
|
|
|
|
maxwell3d.RefreshParameters();
|
|
|
|
cache_info.lle_program->Execute(parameters, method);
|
|
|
|
} else {
|
2020-06-04 17:42:19 +02:00
|
|
|
cache_info.has_hle_program = true;
|
2022-01-25 19:50:10 +01:00
|
|
|
cache_info.hle_program = std::move(hle_program);
|
2022-11-18 00:21:13 +01:00
|
|
|
MICROPROFILE_SCOPE(MacroHLE);
|
2020-06-04 17:42:19 +02:00
|
|
|
cache_info.hle_program->Execute(parameters, method);
|
|
|
|
}
|
2020-05-29 06:53:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MacroEngine> GetMacroEngine(Engines::Maxwell3D& maxwell3d) {
|
|
|
|
if (Settings::values.disable_macro_jit) {
|
|
|
|
return std::make_unique<MacroInterpreter>(maxwell3d);
|
|
|
|
}
|
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
return std::make_unique<MacroJITx64>(maxwell3d);
|
|
|
|
#else
|
|
|
|
return std::make_unique<MacroInterpreter>(maxwell3d);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Tegra
|