yuzu.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/logging/backend.h"
  12. #include "common/logging/log.h"
  13. #include "common/microprofile.h"
  14. #include "common/nvidia_flags.h"
  15. #include "common/scm_rev.h"
  16. #include "common/scope_exit.h"
  17. #include "common/settings.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/registered_cache.h"
  23. #include "core/file_sys/vfs_real.h"
  24. #include "core/hle/service/filesystem/filesystem.h"
  25. #include "core/loader/loader.h"
  26. #include "core/telemetry_session.h"
  27. #include "input_common/main.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. #include "yuzu_cmd/emu_window/emu_window_sdl2_vk.h"
  33. #ifdef _WIN32
  34. // windows.h needs to be included before shellapi.h
  35. #include <windows.h>
  36. #include <shellapi.h>
  37. #endif
  38. #undef _UNICODE
  39. #include <getopt.h>
  40. #ifndef _MSC_VER
  41. #include <unistd.h>
  42. #endif
  43. #ifdef _WIN32
  44. extern "C" {
  45. // tells Nvidia and AMD drivers to use the dedicated GPU by default on laptops with switchable
  46. // graphics
  47. __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
  48. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  49. }
  50. #endif
  51. static void PrintHelp(const char* argv0) {
  52. std::cout << "Usage: " << argv0
  53. << " [options] <filename>\n"
  54. "-f, --fullscreen Start in fullscreen mode\n"
  55. "-h, --help Display this help and exit\n"
  56. "-v, --version Output version information and exit\n"
  57. "-p, --program Pass following string as arguments to executable\n"
  58. "-c, --config Load the specified configuration file\n";
  59. }
  60. static void PrintVersion() {
  61. std::cout << "yuzu " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  62. }
  63. /// Application entry point
  64. int main(int argc, char** argv) {
  65. Common::Log::Initialize();
  66. Common::Log::SetColorConsoleBackendEnabled(true);
  67. Common::Log::Start();
  68. Common::DetachedTasks detached_tasks;
  69. int option_index = 0;
  70. #ifdef _WIN32
  71. int argc_w;
  72. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  73. if (argv_w == nullptr) {
  74. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  75. return -1;
  76. }
  77. #endif
  78. std::string filepath;
  79. std::optional<std::string> config_path;
  80. std::string program_args;
  81. bool fullscreen = false;
  82. static struct option long_options[] = {
  83. // clang-format off
  84. {"fullscreen", no_argument, 0, 'f'},
  85. {"help", no_argument, 0, 'h'},
  86. {"version", no_argument, 0, 'v'},
  87. {"program", optional_argument, 0, 'p'},
  88. {"config", required_argument, 0, 'c'},
  89. {0, 0, 0, 0},
  90. // clang-format on
  91. };
  92. while (optind < argc) {
  93. int arg = getopt_long(argc, argv, "g:fhvp::c:", long_options, &option_index);
  94. if (arg != -1) {
  95. switch (static_cast<char>(arg)) {
  96. case 'f':
  97. fullscreen = true;
  98. LOG_INFO(Frontend, "Starting in fullscreen mode...");
  99. break;
  100. case 'h':
  101. PrintHelp(argv[0]);
  102. return 0;
  103. case 'v':
  104. PrintVersion();
  105. return 0;
  106. case 'p':
  107. program_args = argv[optind];
  108. ++optind;
  109. break;
  110. case 'c':
  111. config_path = optarg;
  112. break;
  113. }
  114. } else {
  115. #ifdef _WIN32
  116. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  117. #else
  118. filepath = argv[optind];
  119. #endif
  120. optind++;
  121. }
  122. }
  123. Config config{config_path};
  124. if (!program_args.empty()) {
  125. Settings::values.program_args = program_args;
  126. }
  127. #ifdef _WIN32
  128. LocalFree(argv_w);
  129. #endif
  130. MicroProfileOnThreadCreate("EmuThread");
  131. SCOPE_EXIT({ MicroProfileShutdown(); });
  132. Common::ConfigureNvidiaEnvironmentFlags();
  133. if (filepath.empty()) {
  134. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  135. return -1;
  136. }
  137. Core::System system{};
  138. InputCommon::InputSubsystem input_subsystem{};
  139. // Apply the command line arguments
  140. system.ApplySettings();
  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, system, fullscreen);
  145. break;
  146. case Settings::RendererBackend::Vulkan:
  147. emu_window = std::make_unique<EmuWindow_SDL2_VK>(&input_subsystem, system, fullscreen);
  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::SystemResultStatus load_result{system.Load(*emu_window, filepath)};
  154. switch (load_result) {
  155. case Core::SystemResultStatus::ErrorGetLoader:
  156. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  157. return -1;
  158. case Core::SystemResultStatus::ErrorLoader:
  159. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  160. return -1;
  161. case Core::SystemResultStatus::ErrorNotInitialized:
  162. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  163. return -1;
  164. case Core::SystemResultStatus::ErrorVideoCore:
  165. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  166. return -1;
  167. case Core::SystemResultStatus::Success:
  168. break; // Expected case
  169. default:
  170. if (static_cast<u32>(load_result) >
  171. static_cast<u32>(Core::SystemResultStatus::ErrorLoader)) {
  172. const u16 loader_id = static_cast<u16>(Core::SystemResultStatus::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. if (Settings::values.use_disk_shader_cache.GetValue()) {
  185. system.Renderer().ReadRasterizer()->LoadDiskResources(
  186. system.GetCurrentProcessProgramID(), std::stop_token{},
  187. [](VideoCore::LoadCallbackStage, size_t value, size_t total) {});
  188. }
  189. system.RegisterExitCallback([&] {
  190. // Just exit right away.
  191. exit(0);
  192. });
  193. void(system.Run());
  194. if (system.DebuggerEnabled()) {
  195. system.InitializeDebugger();
  196. }
  197. while (emu_window->IsOpen()) {
  198. emu_window->WaitEvent();
  199. }
  200. system.DetachDebugger();
  201. void(system.Pause());
  202. system.Shutdown();
  203. detached_tasks.WaitForAllTasks();
  204. return 0;
  205. }