Debian Codegraph @

Source/Core/VideoBackends/Metal/MTLMain.mm

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