macro.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <cstring>
  4. #include <fstream>
  5. #include <optional>
  6. #include <span>
  7. #include <boost/container_hash/hash.hpp>
  8. #include "common/assert.h"
  9. #include "common/fs/fs.h"
  10. #include "common/fs/path_util.h"
  11. #include "common/settings.h"
  12. #include "video_core/macro/macro.h"
  13. #include "video_core/macro/macro_hle.h"
  14. #include "video_core/macro/macro_interpreter.h"
  15. #include "video_core/macro/macro_jit_x64.h"
  16. namespace Tegra {
  17. static void Dump(u64 hash, std::span<const u32> code) {
  18. const auto base_dir{Common::FS::GetYuzuPath(Common::FS::YuzuPath::DumpDir)};
  19. const auto macro_dir{base_dir / "macros"};
  20. if (!Common::FS::CreateDir(base_dir) || !Common::FS::CreateDir(macro_dir)) {
  21. LOG_ERROR(Common_Filesystem, "Failed to create macro dump directories");
  22. return;
  23. }
  24. const auto name{macro_dir / fmt::format("{:016x}.macro", hash)};
  25. std::fstream macro_file(name, std::ios::out | std::ios::binary);
  26. if (!macro_file) {
  27. LOG_ERROR(Common_Filesystem, "Unable to open or create file at {}",
  28. Common::FS::PathToUTF8String(name));
  29. return;
  30. }
  31. macro_file.write(reinterpret_cast<const char*>(code.data()), code.size_bytes());
  32. }
  33. MacroEngine::MacroEngine(Engines::Maxwell3D& maxwell3d)
  34. : hle_macros{std::make_unique<Tegra::HLEMacro>(maxwell3d)} {}
  35. MacroEngine::~MacroEngine() = default;
  36. void MacroEngine::AddCode(u32 method, u32 data) {
  37. uploaded_macro_code[method].push_back(data);
  38. }
  39. void MacroEngine::ClearCode(u32 method) {
  40. macro_cache.erase(method);
  41. uploaded_macro_code.erase(method);
  42. }
  43. void MacroEngine::Execute(u32 method, const std::vector<u32>& parameters) {
  44. auto compiled_macro = macro_cache.find(method);
  45. if (compiled_macro != macro_cache.end()) {
  46. const auto& cache_info = compiled_macro->second;
  47. if (cache_info.has_hle_program) {
  48. cache_info.hle_program->Execute(parameters, method);
  49. } else {
  50. cache_info.lle_program->Execute(parameters, method);
  51. }
  52. } else {
  53. // Macro not compiled, check if it's uploaded and if so, compile it
  54. std::optional<u32> mid_method;
  55. const auto macro_code = uploaded_macro_code.find(method);
  56. if (macro_code == uploaded_macro_code.end()) {
  57. for (const auto& [method_base, code] : uploaded_macro_code) {
  58. if (method >= method_base && (method - method_base) < code.size()) {
  59. mid_method = method_base;
  60. break;
  61. }
  62. }
  63. if (!mid_method.has_value()) {
  64. UNREACHABLE_MSG("Macro 0x{0:x} was not uploaded", method);
  65. return;
  66. }
  67. }
  68. auto& cache_info = macro_cache[method];
  69. if (!mid_method.has_value()) {
  70. cache_info.lle_program = Compile(macro_code->second);
  71. cache_info.hash = boost::hash_value(macro_code->second);
  72. if (Settings::values.dump_macros) {
  73. Dump(cache_info.hash, macro_code->second);
  74. }
  75. } else {
  76. const auto& macro_cached = uploaded_macro_code[mid_method.value()];
  77. const auto rebased_method = method - mid_method.value();
  78. auto& code = uploaded_macro_code[method];
  79. code.resize(macro_cached.size() - rebased_method);
  80. std::memcpy(code.data(), macro_cached.data() + rebased_method,
  81. code.size() * sizeof(u32));
  82. cache_info.hash = boost::hash_value(code);
  83. cache_info.lle_program = Compile(code);
  84. if (Settings::values.dump_macros) {
  85. Dump(cache_info.hash, code);
  86. }
  87. }
  88. if (auto hle_program = hle_macros->GetHLEProgram(cache_info.hash)) {
  89. cache_info.has_hle_program = true;
  90. cache_info.hle_program = std::move(hle_program);
  91. cache_info.hle_program->Execute(parameters, method);
  92. } else {
  93. cache_info.lle_program->Execute(parameters, method);
  94. }
  95. }
  96. }
  97. std::unique_ptr<MacroEngine> GetMacroEngine(Engines::Maxwell3D& maxwell3d) {
  98. if (Settings::values.disable_macro_jit) {
  99. return std::make_unique<MacroInterpreter>(maxwell3d);
  100. }
  101. #ifdef ARCHITECTURE_x86_64
  102. return std::make_unique<MacroJITx64>(maxwell3d);
  103. #else
  104. return std::make_unique<MacroInterpreter>(maxwell3d);
  105. #endif
  106. }
  107. } // namespace Tegra