yuzu.cpp 7.0 KB

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