yuzu.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <iostream>
  5. #include <memory>
  6. #include <string>
  7. #include <thread>
  8. #include <fmt/ostream.h>
  9. #include "common/common_paths.h"
  10. #include "common/detached_tasks.h"
  11. #include "common/file_util.h"
  12. #include "common/logging/backend.h"
  13. #include "common/logging/filter.h"
  14. #include "common/logging/log.h"
  15. #include "common/microprofile.h"
  16. #include "common/scm_rev.h"
  17. #include "common/scope_exit.h"
  18. #include "common/string_util.h"
  19. #include "common/telemetry.h"
  20. #include "core/core.h"
  21. #include "core/crypto/key_manager.h"
  22. #include "core/file_sys/vfs_real.h"
  23. #include "core/hle/service/filesystem/filesystem.h"
  24. #include "core/loader/loader.h"
  25. #include "core/settings.h"
  26. #include "core/telemetry_session.h"
  27. #include "video_core/renderer_base.h"
  28. #include "yuzu_tester/config.h"
  29. #include "yuzu_tester/emu_window/emu_window_sdl2_hide.h"
  30. #include "yuzu_tester/service/yuzutest.h"
  31. #ifdef _WIN32
  32. // windows.h needs to be included before shellapi.h
  33. #include <windows.h>
  34. #include <shellapi.h>
  35. #endif
  36. #undef _UNICODE
  37. #include <getopt.h>
  38. #ifndef _MSC_VER
  39. #include <unistd.h>
  40. #endif
  41. #ifdef _WIN32
  42. extern "C" {
  43. // tells Nvidia and AMD drivers to use the dedicated GPU by default on laptops with switchable
  44. // graphics
  45. __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
  46. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  47. }
  48. #endif
  49. static void PrintHelp(const char* argv0) {
  50. std::cout << "Usage: " << argv0
  51. << " [options] <filename>\n"
  52. "-h, --help Display this help and exit\n"
  53. "-v, --version Output version information and exit\n"
  54. "-d, --datastring Pass following string as data to test service command #2\n"
  55. "-l, --log Log to console in addition to file (will log to file only "
  56. "by default)\n";
  57. }
  58. static void PrintVersion() {
  59. std::cout << "yuzu [Test Utility] " << Common::g_scm_branch << " " << Common::g_scm_desc
  60. << std::endl;
  61. }
  62. static void InitializeLogging(bool console) {
  63. Log::Filter log_filter(Log::Level::Debug);
  64. log_filter.ParseFilterString(Settings::values.log_filter);
  65. Log::SetGlobalFilter(log_filter);
  66. if (console)
  67. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  68. const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
  69. FileUtil::CreateFullPath(log_dir);
  70. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  71. #ifdef _WIN32
  72. Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
  73. #endif
  74. }
  75. /// Application entry point
  76. int main(int argc, char** argv) {
  77. Common::DetachedTasks detached_tasks;
  78. Config config;
  79. int option_index = 0;
  80. #ifdef _WIN32
  81. int argc_w;
  82. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  83. if (argv_w == nullptr) {
  84. std::cout << "Failed to get command line arguments" << std::endl;
  85. return -1;
  86. }
  87. #endif
  88. std::string filepath;
  89. static struct option long_options[] = {
  90. {"help", no_argument, 0, 'h'},
  91. {"version", no_argument, 0, 'v'},
  92. {"datastring", optional_argument, 0, 'd'},
  93. {"log", no_argument, 0, 'l'},
  94. {0, 0, 0, 0},
  95. };
  96. bool console_log = false;
  97. std::string datastring;
  98. while (optind < argc) {
  99. int arg = getopt_long(argc, argv, "hvdl::", long_options, &option_index);
  100. if (arg != -1) {
  101. switch (static_cast<char>(arg)) {
  102. case 'h':
  103. PrintHelp(argv[0]);
  104. return 0;
  105. case 'v':
  106. PrintVersion();
  107. return 0;
  108. case 'd':
  109. datastring = argv[optind];
  110. ++optind;
  111. break;
  112. case 'l':
  113. console_log = true;
  114. break;
  115. }
  116. } else {
  117. #ifdef _WIN32
  118. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  119. #else
  120. filepath = argv[optind];
  121. #endif
  122. optind++;
  123. }
  124. }
  125. InitializeLogging(console_log);
  126. #ifdef _WIN32
  127. LocalFree(argv_w);
  128. #endif
  129. MicroProfileOnThreadCreate("EmuThread");
  130. SCOPE_EXIT({ MicroProfileShutdown(); });
  131. if (filepath.empty()) {
  132. LOG_CRITICAL(Frontend, "Failed to load application: No application specified");
  133. std::cout << "Failed to load application: No application specified" << std::endl;
  134. PrintHelp(argv[0]);
  135. return -1;
  136. }
  137. Settings::values.use_gdbstub = false;
  138. Settings::Apply();
  139. std::unique_ptr<EmuWindow_SDL2_Hide> emu_window{std::make_unique<EmuWindow_SDL2_Hide>()};
  140. if (!Settings::values.use_multi_core) {
  141. // Single core mode must acquire OpenGL context for entire emulation session
  142. emu_window->MakeCurrent();
  143. }
  144. bool finished = false;
  145. int return_value = 0;
  146. const auto callback = [&finished,
  147. &return_value](std::vector<Service::Yuzu::TestResult> results) {
  148. finished = true;
  149. return_value = 0;
  150. // Find the minimum length needed to fully enclose all test names (and the header field) in
  151. // the fmt::format column by first finding the maximum size of any test name and comparing
  152. // that to 9, the string length of 'Test Name'
  153. const auto needed_length_name =
  154. std::max<u64>(std::max_element(results.begin(), results.end(),
  155. [](const auto& lhs, const auto& rhs) {
  156. return lhs.name.size() < rhs.name.size();
  157. })
  158. ->name.size(),
  159. 9ull);
  160. std::size_t passed = 0;
  161. std::size_t failed = 0;
  162. std::cout << fmt::format("Result [Res Code] | {:<{}} | Extra Data", "Test Name",
  163. needed_length_name)
  164. << std::endl;
  165. for (const auto& res : results) {
  166. const auto main_res = res.code == 0 ? "PASSED" : "FAILED";
  167. if (res.code == 0)
  168. ++passed;
  169. else
  170. ++failed;
  171. std::cout << fmt::format("{} [{:08X}] | {:<{}} | {}", main_res, res.code, res.name,
  172. needed_length_name, res.data)
  173. << std::endl;
  174. }
  175. std::cout << std::endl
  176. << fmt::format("{:4d} Passed | {:4d} Failed | {:4d} Total | {:2.2f} Passed Ratio",
  177. passed, failed, passed + failed,
  178. static_cast<float>(passed) / (passed + failed))
  179. << std::endl
  180. << (failed == 0 ? "PASSED" : "FAILED") << std::endl;
  181. if (failed > 0)
  182. return_value = -1;
  183. };
  184. Core::System& system{Core::System::GetInstance()};
  185. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  186. Service::FileSystem::CreateFactories(*system.GetFilesystem());
  187. SCOPE_EXIT({ system.Shutdown(); });
  188. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  189. switch (load_result) {
  190. case Core::System::ResultStatus::ErrorGetLoader:
  191. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  192. return -1;
  193. case Core::System::ResultStatus::ErrorLoader:
  194. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  195. return -1;
  196. case Core::System::ResultStatus::ErrorNotInitialized:
  197. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  198. return -1;
  199. case Core::System::ResultStatus::ErrorVideoCore:
  200. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  201. return -1;
  202. case Core::System::ResultStatus::Success:
  203. break; // Expected case
  204. default:
  205. if (static_cast<u32>(load_result) >
  206. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  207. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  208. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  209. LOG_CRITICAL(Frontend,
  210. "While attempting to load the ROM requested, an error occured. Please "
  211. "refer to the yuzu wiki for more information or the yuzu discord for "
  212. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  213. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  214. }
  215. }
  216. Service::Yuzu::InstallInterfaces(system.ServiceManager(), datastring, callback);
  217. system.TelemetrySession().AddField(Telemetry::FieldType::App, "Frontend", "SDLHideTester");
  218. system.Renderer().Rasterizer().LoadDiskResources();
  219. while (!finished) {
  220. system.RunLoop();
  221. }
  222. detached_tasks.WaitForAllTasks();
  223. return return_value;
  224. }