yuzu.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <iostream>
  6. #include <memory>
  7. #include <string>
  8. #include <thread>
  9. #include <fmt/ostream.h>
  10. #include "common/common_paths.h"
  11. #include "common/detached_tasks.h"
  12. #include "common/file_util.h"
  13. #include "common/logging/backend.h"
  14. #include "common/logging/filter.h"
  15. #include "common/logging/log.h"
  16. #include "common/microprofile.h"
  17. #include "common/scm_rev.h"
  18. #include "common/scope_exit.h"
  19. #include "common/string_util.h"
  20. #include "common/telemetry.h"
  21. #include "core/core.h"
  22. #include "core/crypto/key_manager.h"
  23. #include "core/file_sys/registered_cache.h"
  24. #include "core/file_sys/vfs_real.h"
  25. #include "core/gdbstub/gdbstub.h"
  26. #include "core/hle/kernel/process.h"
  27. #include "core/hle/service/filesystem/filesystem.h"
  28. #include "core/loader/loader.h"
  29. #include "core/settings.h"
  30. #include "core/telemetry_session.h"
  31. #include "input_common/main.h"
  32. #include "video_core/renderer_base.h"
  33. #include "yuzu_cmd/config.h"
  34. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  35. #include "yuzu_cmd/emu_window/emu_window_sdl2_gl.h"
  36. #ifdef HAS_VULKAN
  37. #include "yuzu_cmd/emu_window/emu_window_sdl2_vk.h"
  38. #endif
  39. #ifdef _WIN32
  40. // windows.h needs to be included before shellapi.h
  41. #include <windows.h>
  42. #include <shellapi.h>
  43. #endif
  44. #undef _UNICODE
  45. #include <getopt.h>
  46. #ifndef _MSC_VER
  47. #include <unistd.h>
  48. #endif
  49. #ifdef _WIN32
  50. extern "C" {
  51. // tells Nvidia and AMD drivers to use the dedicated GPU by default on laptops with switchable
  52. // graphics
  53. __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
  54. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  55. }
  56. #endif
  57. static void PrintHelp(const char* argv0) {
  58. std::cout << "Usage: " << argv0
  59. << " [options] <filename>\n"
  60. "-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n"
  61. "-f, --fullscreen Start in fullscreen mode\n"
  62. "-h, --help Display this help and exit\n"
  63. "-v, --version Output version information and exit\n"
  64. "-p, --program Pass following string as arguments to executable\n";
  65. }
  66. static void PrintVersion() {
  67. std::cout << "yuzu " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  68. }
  69. static void InitializeLogging() {
  70. Log::Filter log_filter(Log::Level::Debug);
  71. log_filter.ParseFilterString(Settings::values.log_filter);
  72. Log::SetGlobalFilter(log_filter);
  73. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  74. const std::string& log_dir = Common::FS::GetUserPath(Common::FS::UserPath::LogDir);
  75. Common::FS::CreateFullPath(log_dir);
  76. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  77. #ifdef _WIN32
  78. Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
  79. #endif
  80. }
  81. /// Application entry point
  82. int main(int argc, char** argv) {
  83. Common::DetachedTasks detached_tasks;
  84. Config config;
  85. int option_index = 0;
  86. bool use_gdbstub = Settings::values.use_gdbstub;
  87. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  88. InitializeLogging();
  89. char* endarg;
  90. #ifdef _WIN32
  91. int argc_w;
  92. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  93. if (argv_w == nullptr) {
  94. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  95. return -1;
  96. }
  97. #endif
  98. std::string filepath;
  99. bool fullscreen = false;
  100. static struct option long_options[] = {
  101. {"gdbport", required_argument, 0, 'g'}, {"fullscreen", no_argument, 0, 'f'},
  102. {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'},
  103. {"program", optional_argument, 0, 'p'}, {0, 0, 0, 0},
  104. };
  105. while (optind < argc) {
  106. int arg = getopt_long(argc, argv, "g:fhvp::", long_options, &option_index);
  107. if (arg != -1) {
  108. switch (static_cast<char>(arg)) {
  109. case 'g':
  110. errno = 0;
  111. gdb_port = strtoul(optarg, &endarg, 0);
  112. use_gdbstub = true;
  113. if (endarg == optarg)
  114. errno = EINVAL;
  115. if (errno != 0) {
  116. perror("--gdbport");
  117. exit(1);
  118. }
  119. break;
  120. case 'f':
  121. fullscreen = true;
  122. LOG_INFO(Frontend, "Starting in fullscreen mode...");
  123. break;
  124. case 'h':
  125. PrintHelp(argv[0]);
  126. return 0;
  127. case 'v':
  128. PrintVersion();
  129. return 0;
  130. case 'p':
  131. Settings::values.program_args = argv[optind];
  132. ++optind;
  133. break;
  134. }
  135. } else {
  136. #ifdef _WIN32
  137. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  138. #else
  139. filepath = argv[optind];
  140. #endif
  141. optind++;
  142. }
  143. }
  144. #ifdef _WIN32
  145. LocalFree(argv_w);
  146. #endif
  147. MicroProfileOnThreadCreate("EmuThread");
  148. SCOPE_EXIT({ MicroProfileShutdown(); });
  149. if (filepath.empty()) {
  150. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  151. return -1;
  152. }
  153. // Apply the command line arguments
  154. Settings::values.gdbstub_port = gdb_port;
  155. Settings::values.use_gdbstub = use_gdbstub;
  156. Settings::Apply();
  157. Core::System& system{Core::System::GetInstance()};
  158. InputCommon::InputSubsystem input_subsystem;
  159. std::unique_ptr<EmuWindow_SDL2> emu_window;
  160. switch (Settings::values.renderer_backend.GetValue()) {
  161. case Settings::RendererBackend::OpenGL:
  162. emu_window = std::make_unique<EmuWindow_SDL2_GL>(&input_subsystem, fullscreen);
  163. break;
  164. case Settings::RendererBackend::Vulkan:
  165. #ifdef HAS_VULKAN
  166. emu_window = std::make_unique<EmuWindow_SDL2_VK>(&input_subsystem);
  167. break;
  168. #else
  169. LOG_CRITICAL(Frontend, "Vulkan backend has not been compiled!");
  170. return 1;
  171. #endif
  172. }
  173. system.SetContentProvider(std::make_unique<FileSys::ContentProviderUnion>());
  174. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  175. system.GetFileSystemController().CreateFactories(*system.GetFilesystem());
  176. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  177. switch (load_result) {
  178. case Core::System::ResultStatus::ErrorGetLoader:
  179. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  180. return -1;
  181. case Core::System::ResultStatus::ErrorLoader:
  182. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  183. return -1;
  184. case Core::System::ResultStatus::ErrorNotInitialized:
  185. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  186. return -1;
  187. case Core::System::ResultStatus::ErrorVideoCore:
  188. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  189. return -1;
  190. case Core::System::ResultStatus::Success:
  191. break; // Expected case
  192. default:
  193. if (static_cast<u32>(load_result) >
  194. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  195. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  196. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  197. LOG_CRITICAL(Frontend,
  198. "While attempting to load the ROM requested, an error occured. Please "
  199. "refer to the yuzu wiki for more information or the yuzu discord for "
  200. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  201. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  202. }
  203. }
  204. system.TelemetrySession().AddField(Common::Telemetry::FieldType::App, "Frontend", "SDL");
  205. // Core is loaded, start the GPU (makes the GPU contexts current to this thread)
  206. system.GPU().Start();
  207. system.Renderer().Rasterizer().LoadDiskResources(
  208. system.CurrentProcess()->GetTitleID(), false,
  209. [](VideoCore::LoadCallbackStage, size_t value, size_t total) {});
  210. system.Run();
  211. while (emu_window->IsOpen()) {
  212. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  213. }
  214. system.Pause();
  215. system.Shutdown();
  216. detached_tasks.WaitForAllTasks();
  217. return 0;
  218. }