MTLMain.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2022 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/Metal/VideoBackend.h"
  4. // This must be included before we use any TARGET_OS_* macros.
  5. #include <TargetConditionals.h>
  6. #if TARGET_OS_OSX
  7. #include <AppKit/AppKit.h>
  8. #endif
  9. #include <Metal/Metal.h>
  10. #include <QuartzCore/QuartzCore.h>
  11. #include "Common/Common.h"
  12. #include "Common/MsgHandler.h"
  13. #include "VideoBackends/Metal/MTLBoundingBox.h"
  14. #include "VideoBackends/Metal/MTLGfx.h"
  15. #include "VideoBackends/Metal/MTLObjectCache.h"
  16. #include "VideoBackends/Metal/MTLPerfQuery.h"
  17. #include "VideoBackends/Metal/MTLStateTracker.h"
  18. #include "VideoBackends/Metal/MTLUtil.h"
  19. #include "VideoBackends/Metal/MTLVertexManager.h"
  20. #include "VideoCommon/AbstractGfx.h"
  21. #include "VideoCommon/FramebufferManager.h"
  22. #include "VideoCommon/VideoCommon.h"
  23. #include "VideoCommon/VideoConfig.h"
  24. std::string Metal::VideoBackend::GetName() const
  25. {
  26. return NAME;
  27. }
  28. std::string Metal::VideoBackend::GetDisplayName() const
  29. {
  30. // i18n: Apple's Metal graphics API (https://developer.apple.com/metal/)
  31. return _trans("Metal");
  32. }
  33. std::optional<std::string> Metal::VideoBackend::GetWarningMessage() const
  34. {
  35. if (Util::GetAdapterList().empty())
  36. {
  37. return _trans("No Metal-compatible GPUs were found. "
  38. "Use the OpenGL backend or upgrade your computer/GPU");
  39. }
  40. return std::nullopt;
  41. }
  42. static bool WindowSystemTypeSupportsMetal(WindowSystemType type)
  43. {
  44. switch (type)
  45. {
  46. case WindowSystemType::MacOS:
  47. case WindowSystemType::Headless:
  48. return true;
  49. default:
  50. return false;
  51. }
  52. }
  53. bool Metal::VideoBackend::Initialize(const WindowSystemInfo& wsi)
  54. {
  55. @autoreleasepool
  56. {
  57. const bool surface_ok = wsi.type == WindowSystemType::Headless || wsi.render_surface;
  58. if (!WindowSystemTypeSupportsMetal(wsi.type) || !surface_ok)
  59. {
  60. PanicAlertFmt("Bad WindowSystemInfo for Metal renderer.");
  61. return false;
  62. }
  63. auto devs = Util::GetAdapterList();
  64. if (devs.empty())
  65. {
  66. PanicAlertFmt("No Metal GPUs detected.");
  67. return false;
  68. }
  69. Util::PopulateBackendInfo(&g_Config);
  70. Util::PopulateBackendInfoAdapters(&g_Config, devs);
  71. // Since we haven't called InitializeShared yet, iAdapter may be out of range,
  72. // so we have to check it ourselves.
  73. size_t selected_adapter_index = static_cast<size_t>(g_Config.iAdapter);
  74. if (selected_adapter_index >= devs.size())
  75. {
  76. WARN_LOG_FMT(VIDEO, "Metal adapter index out of range, selecting default adapter.");
  77. selected_adapter_index = 0;
  78. }
  79. MRCOwned<id<MTLDevice>> adapter = std::move(devs[selected_adapter_index]);
  80. Util::PopulateBackendInfoFeatures(&g_Config, adapter);
  81. #if TARGET_OS_OSX
  82. // This should be available on all macOS 13.3+ systems – but when using OCLP drivers, some devices
  83. // fail with "Unrecognized selector -[MTLIGAccelDevice setShouldMaximizeConcurrentCompilation:]"
  84. //
  85. // This concerns Intel Ivy Bridge, Haswell and Nvidia Kepler on macOS 13.3 or newer.
  86. // (See
  87. // https://github.com/dortania/OpenCore-Legacy-Patcher/blob/34676702f494a2a789c514cc76dba19b8b7206b1/docs/PATCHEXPLAIN.md?plain=1#L354C1-L354C83)
  88. //
  89. // Perform the feature detection dynamically instead.
  90. #pragma clang diagnostic push
  91. #pragma clang diagnostic ignored "-Wunguarded-availability"
  92. if ([adapter respondsToSelector:@selector(setShouldMaximizeConcurrentCompilation:)])
  93. {
  94. [adapter setShouldMaximizeConcurrentCompilation:YES];
  95. }
  96. #pragma clang diagnostic pop
  97. #endif
  98. UpdateActiveConfig();
  99. MRCOwned<CAMetalLayer*> layer = MRCRetain(static_cast<CAMetalLayer*>(wsi.render_surface));
  100. [layer setDevice:adapter];
  101. if (Util::ToAbstract([layer pixelFormat]) == AbstractTextureFormat::Undefined)
  102. [layer setPixelFormat:MTLPixelFormatBGRA8Unorm];
  103. ObjectCache::Initialize(std::move(adapter));
  104. g_state_tracker = std::make_unique<StateTracker>();
  105. return InitializeShared(
  106. std::make_unique<Metal::Gfx>(std::move(layer)), std::make_unique<Metal::VertexManager>(),
  107. std::make_unique<Metal::PerfQuery>(), std::make_unique<Metal::BoundingBox>());
  108. }
  109. }
  110. void Metal::VideoBackend::Shutdown()
  111. {
  112. ShutdownShared();
  113. g_state_tracker.reset();
  114. ObjectCache::Shutdown();
  115. }
  116. void Metal::VideoBackend::InitBackendInfo(const WindowSystemInfo& wsi)
  117. {
  118. @autoreleasepool
  119. {
  120. Util::PopulateBackendInfo(&g_Config);
  121. auto adapters = Util::GetAdapterList();
  122. Util::PopulateBackendInfoAdapters(&g_Config, adapters);
  123. if (!adapters.empty())
  124. {
  125. // Use the selected adapter, or the first to fill features.
  126. size_t index = static_cast<size_t>(g_Config.iAdapter);
  127. if (index >= adapters.size())
  128. index = 0;
  129. Util::PopulateBackendInfoFeatures(&g_Config, adapters[index]);
  130. }
  131. }
  132. }
  133. void Metal::VideoBackend::PrepareWindow(WindowSystemInfo& wsi)
  134. {
  135. #if TARGET_OS_OSX
  136. if (wsi.type != WindowSystemType::MacOS)
  137. return;
  138. NSView* view = static_cast<NSView*>(wsi.render_surface);
  139. CAMetalLayer* layer = [CAMetalLayer layer];
  140. Util::PopulateBackendInfo(&g_Config);
  141. if (g_Config.backend_info.bSupportsHDROutput && g_Config.bHDR)
  142. {
  143. [layer setWantsExtendedDynamicRangeContent:YES];
  144. [layer setPixelFormat:MTLPixelFormatRGBA16Float];
  145. const CFStringRef name = kCGColorSpaceExtendedLinearSRGB;
  146. CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(name);
  147. [layer setColorspace:colorspace];
  148. CGColorSpaceRelease(colorspace);
  149. }
  150. [view setWantsLayer:YES];
  151. [view setLayer:layer];
  152. wsi.render_surface = layer;
  153. #endif
  154. }