macro.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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::Execute(u32 method, const std::vector<u32>& parameters) {
  40. auto compiled_macro = macro_cache.find(method);
  41. if (compiled_macro != macro_cache.end()) {
  42. const auto& cache_info = compiled_macro->second;
  43. if (cache_info.has_hle_program) {
  44. cache_info.hle_program->Execute(parameters, method);
  45. } else {
  46. cache_info.lle_program->Execute(parameters, method);
  47. }
  48. } else {
  49. // Macro not compiled, check if it's uploaded and if so, compile it
  50. std::optional<u32> mid_method;
  51. const auto macro_code = uploaded_macro_code.find(method);
  52. if (macro_code == uploaded_macro_code.end()) {
  53. for (const auto& [method_base, code] : uploaded_macro_code) {
  54. if (method >= method_base && (method - method_base) < code.size()) {
  55. mid_method = method_base;
  56. break;
  57. }
  58. }
  59. if (!mid_method.has_value()) {
  60. UNREACHABLE_MSG("Macro 0x{0:x} was not uploaded", method);
  61. return;
  62. }
  63. }
  64. auto& cache_info = macro_cache[method];
  65. if (!mid_method.has_value()) {
  66. cache_info.lle_program = Compile(macro_code->second);
  67. cache_info.hash = boost::hash_value(macro_code->second);
  68. if (Settings::values.dump_macros) {
  69. Dump(cache_info.hash, macro_code->second);
  70. }
  71. } else {
  72. const auto& macro_cached = uploaded_macro_code[mid_method.value()];
  73. const auto rebased_method = method - mid_method.value();
  74. auto& code = uploaded_macro_code[method];
  75. code.resize(macro_cached.size() - rebased_method);
  76. std::memcpy(code.data(), macro_cached.data() + rebased_method,
  77. code.size() * sizeof(u32));
  78. cache_info.hash = boost::hash_value(code);
  79. cache_info.lle_program = Compile(code);
  80. if (Settings::values.dump_macros) {
  81. Dump(cache_info.hash, code);
  82. }
  83. }
  84. if (auto hle_program = hle_macros->GetHLEProgram(cache_info.hash)) {
  85. cache_info.has_hle_program = true;
  86. cache_info.hle_program = std::move(hle_program);
  87. cache_info.hle_program->Execute(parameters, method);
  88. } else {
  89. cache_info.lle_program->Execute(parameters, method);
  90. }
  91. }
  92. }
  93. std::unique_ptr<MacroEngine> GetMacroEngine(Engines::Maxwell3D& maxwell3d) {
  94. if (Settings::values.disable_macro_jit) {
  95. return std::make_unique<MacroInterpreter>(maxwell3d);
  96. }
  97. #ifdef ARCHITECTURE_x86_64
  98. return std::make_unique<MacroJITx64>(maxwell3d);
  99. #else
  100. return std::make_unique<MacroInterpreter>(maxwell3d);
  101. #endif
  102. }
  103. } // namespace Tegra