yuzu.cpp 7.3 KB

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