yuzu.cpp 8.0 KB

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