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