yuzu.cpp 7.9 KB

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