yuzu.cpp 6.9 KB

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