yuzu.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/hle/kernel/process.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. #include "yuzu_cmd/emu_window/emu_window_sdl2_vk.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. "-f, --fullscreen Start in fullscreen mode\n"
  58. "-h, --help Display this help and exit\n"
  59. "-v, --version Output version information and exit\n"
  60. "-p, --program Pass following string as arguments to executable\n";
  61. }
  62. static void PrintVersion() {
  63. std::cout << "yuzu " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  64. }
  65. static void InitializeLogging() {
  66. Log::Filter log_filter(Log::Level::Debug);
  67. log_filter.ParseFilterString(Settings::values.log_filter);
  68. Log::SetGlobalFilter(log_filter);
  69. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  70. const std::string& log_dir = Common::FS::GetUserPath(Common::FS::UserPath::LogDir);
  71. Common::FS::CreateFullPath(log_dir);
  72. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  73. #ifdef _WIN32
  74. Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
  75. #endif
  76. }
  77. /// Application entry point
  78. int main(int argc, char** argv) {
  79. Common::DetachedTasks detached_tasks;
  80. Config config;
  81. int option_index = 0;
  82. InitializeLogging();
  83. #ifdef _WIN32
  84. int argc_w;
  85. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  86. if (argv_w == nullptr) {
  87. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  88. return -1;
  89. }
  90. #endif
  91. std::string filepath;
  92. bool fullscreen = false;
  93. static struct option long_options[] = {
  94. {"fullscreen", no_argument, 0, 'f'},
  95. {"help", no_argument, 0, 'h'},
  96. {"version", no_argument, 0, 'v'},
  97. {"program", optional_argument, 0, 'p'},
  98. {0, 0, 0, 0},
  99. };
  100. while (optind < argc) {
  101. int arg = getopt_long(argc, argv, "g:fhvp::", long_options, &option_index);
  102. if (arg != -1) {
  103. switch (static_cast<char>(arg)) {
  104. case 'f':
  105. fullscreen = true;
  106. LOG_INFO(Frontend, "Starting in fullscreen mode...");
  107. break;
  108. case 'h':
  109. PrintHelp(argv[0]);
  110. return 0;
  111. case 'v':
  112. PrintVersion();
  113. return 0;
  114. case 'p':
  115. Settings::values.program_args = argv[optind];
  116. ++optind;
  117. break;
  118. }
  119. } else {
  120. #ifdef _WIN32
  121. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  122. #else
  123. filepath = argv[optind];
  124. #endif
  125. optind++;
  126. }
  127. }
  128. #ifdef _WIN32
  129. LocalFree(argv_w);
  130. #endif
  131. MicroProfileOnThreadCreate("EmuThread");
  132. SCOPE_EXIT({ MicroProfileShutdown(); });
  133. if (filepath.empty()) {
  134. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  135. return -1;
  136. }
  137. auto& system{Core::System::GetInstance()};
  138. InputCommon::InputSubsystem input_subsystem;
  139. // Apply the command line arguments
  140. Settings::Apply(system);
  141. std::unique_ptr<EmuWindow_SDL2> emu_window;
  142. switch (Settings::values.renderer_backend.GetValue()) {
  143. case Settings::RendererBackend::OpenGL:
  144. emu_window = std::make_unique<EmuWindow_SDL2_GL>(&input_subsystem, fullscreen);
  145. break;
  146. case Settings::RendererBackend::Vulkan:
  147. emu_window = std::make_unique<EmuWindow_SDL2_VK>(&input_subsystem);
  148. break;
  149. }
  150. system.SetContentProvider(std::make_unique<FileSys::ContentProviderUnion>());
  151. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  152. system.GetFileSystemController().CreateFactories(*system.GetFilesystem());
  153. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  154. switch (load_result) {
  155. case Core::System::ResultStatus::ErrorGetLoader:
  156. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  157. return -1;
  158. case Core::System::ResultStatus::ErrorLoader:
  159. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  160. return -1;
  161. case Core::System::ResultStatus::ErrorNotInitialized:
  162. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  163. return -1;
  164. case Core::System::ResultStatus::ErrorVideoCore:
  165. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  166. return -1;
  167. case Core::System::ResultStatus::Success:
  168. break; // Expected case
  169. default:
  170. if (static_cast<u32>(load_result) >
  171. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  172. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  173. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  174. LOG_CRITICAL(Frontend,
  175. "While attempting to load the ROM requested, an error occurred. Please "
  176. "refer to the yuzu wiki for more information or the yuzu discord for "
  177. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  178. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  179. }
  180. }
  181. system.TelemetrySession().AddField(Common::Telemetry::FieldType::App, "Frontend", "SDL");
  182. // Core is loaded, start the GPU (makes the GPU contexts current to this thread)
  183. system.GPU().Start();
  184. system.Renderer().Rasterizer().LoadDiskResources(
  185. system.CurrentProcess()->GetTitleID(), false,
  186. [](VideoCore::LoadCallbackStage, size_t value, size_t total) {});
  187. void(system.Run());
  188. while (emu_window->IsOpen()) {
  189. emu_window->WaitEvent();
  190. }
  191. void(system.Pause());
  192. system.Shutdown();
  193. detached_tasks.WaitForAllTasks();
  194. return 0;
  195. }