yuzu.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/detached_tasks.h"
  11. #include "common/fs/fs.h"
  12. #include "common/fs/fs_paths.h"
  13. #include "common/fs/path_util.h"
  14. #include "common/logging/backend.h"
  15. #include "common/logging/filter.h"
  16. #include "common/logging/log.h"
  17. #include "common/microprofile.h"
  18. #include "common/nvidia_flags.h"
  19. #include "common/scm_rev.h"
  20. #include "common/scope_exit.h"
  21. #include "common/settings.h"
  22. #include "common/string_util.h"
  23. #include "common/telemetry.h"
  24. #include "core/core.h"
  25. #include "core/crypto/key_manager.h"
  26. #include "core/file_sys/registered_cache.h"
  27. #include "core/file_sys/vfs_real.h"
  28. #include "core/hle/kernel/k_process.h"
  29. #include "core/hle/service/filesystem/filesystem.h"
  30. #include "core/loader/loader.h"
  31. #include "core/telemetry_session.h"
  32. #include "input_common/main.h"
  33. #include "video_core/renderer_base.h"
  34. #include "yuzu_cmd/config.h"
  35. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  36. #include "yuzu_cmd/emu_window/emu_window_sdl2_gl.h"
  37. #include "yuzu_cmd/emu_window/emu_window_sdl2_vk.h"
  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. "-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. using namespace Common;
  69. Log::Filter log_filter(Log::Level::Debug);
  70. log_filter.ParseFilterString(static_cast<std::string>(Settings::values.log_filter));
  71. Log::SetGlobalFilter(log_filter);
  72. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  73. const auto& log_dir = FS::GetYuzuPath(FS::YuzuPath::LogDir);
  74. void(FS::CreateDir(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. InitializeLogging();
  86. #ifdef _WIN32
  87. int argc_w;
  88. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  89. if (argv_w == nullptr) {
  90. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  91. return -1;
  92. }
  93. #endif
  94. std::string filepath;
  95. bool fullscreen = false;
  96. static struct option long_options[] = {
  97. {"fullscreen", no_argument, 0, 'f'},
  98. {"help", no_argument, 0, 'h'},
  99. {"version", no_argument, 0, 'v'},
  100. {"program", optional_argument, 0, 'p'},
  101. {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 'f':
  108. fullscreen = true;
  109. LOG_INFO(Frontend, "Starting in fullscreen mode...");
  110. break;
  111. case 'h':
  112. PrintHelp(argv[0]);
  113. return 0;
  114. case 'v':
  115. PrintVersion();
  116. return 0;
  117. case 'p':
  118. Settings::values.program_args = argv[optind];
  119. ++optind;
  120. break;
  121. }
  122. } else {
  123. #ifdef _WIN32
  124. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  125. #else
  126. filepath = argv[optind];
  127. #endif
  128. optind++;
  129. }
  130. }
  131. #ifdef _WIN32
  132. LocalFree(argv_w);
  133. #endif
  134. MicroProfileOnThreadCreate("EmuThread");
  135. SCOPE_EXIT({ MicroProfileShutdown(); });
  136. Common::ConfigureNvidiaEnvironmentFlags();
  137. if (filepath.empty()) {
  138. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  139. return -1;
  140. }
  141. auto& system{Core::System::GetInstance()};
  142. InputCommon::InputSubsystem input_subsystem;
  143. // Apply the command line arguments
  144. system.ApplySettings();
  145. std::unique_ptr<EmuWindow_SDL2> emu_window;
  146. switch (Settings::values.renderer_backend.GetValue()) {
  147. case Settings::RendererBackend::OpenGL:
  148. emu_window = std::make_unique<EmuWindow_SDL2_GL>(&input_subsystem, fullscreen);
  149. break;
  150. case Settings::RendererBackend::Vulkan:
  151. emu_window = std::make_unique<EmuWindow_SDL2_VK>(&input_subsystem);
  152. break;
  153. }
  154. system.SetContentProvider(std::make_unique<FileSys::ContentProviderUnion>());
  155. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  156. system.GetFileSystemController().CreateFactories(*system.GetFilesystem());
  157. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  158. switch (load_result) {
  159. case Core::System::ResultStatus::ErrorGetLoader:
  160. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  161. return -1;
  162. case Core::System::ResultStatus::ErrorLoader:
  163. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  164. return -1;
  165. case Core::System::ResultStatus::ErrorNotInitialized:
  166. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  167. return -1;
  168. case Core::System::ResultStatus::ErrorVideoCore:
  169. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  170. return -1;
  171. case Core::System::ResultStatus::Success:
  172. break; // Expected case
  173. default:
  174. if (static_cast<u32>(load_result) >
  175. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  176. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  177. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  178. LOG_CRITICAL(Frontend,
  179. "While attempting to load the ROM requested, an error occurred. Please "
  180. "refer to the yuzu wiki for more information or the yuzu discord for "
  181. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  182. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  183. }
  184. }
  185. system.TelemetrySession().AddField(Common::Telemetry::FieldType::App, "Frontend", "SDL");
  186. // Core is loaded, start the GPU (makes the GPU contexts current to this thread)
  187. system.GPU().Start();
  188. system.Renderer().ReadRasterizer()->LoadDiskResources(
  189. system.CurrentProcess()->GetTitleID(), std::stop_token{},
  190. [](VideoCore::LoadCallbackStage, size_t value, size_t total) {});
  191. void(system.Run());
  192. while (emu_window->IsOpen()) {
  193. emu_window->WaitEvent();
  194. }
  195. void(system.Pause());
  196. system.Shutdown();
  197. detached_tasks.WaitForAllTasks();
  198. return 0;
  199. }