Debian Codegraph @

drivers/metal/metal_device_properties.mm

raw
1 /**************************************************************************/
2 /* metal_device_properties.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 /**************************************************************************/
32 /* */
33 /* Portions of this code were derived from MoltenVK. */
34 /* */
35 /* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. */
36 /* (http://www.brenwill.com) */
37 /* */
38 /* Licensed under the Apache License, Version 2.0 (the "License"); */
39 /* you may not use this file except in compliance with the License. */
40 /* You may obtain a copy of the License at */
41 /* */
42 /* http://www.apache.org/licenses/LICENSE-2.0 */
43 /* */
44 /* Unless required by applicable law or agreed to in writing, software */
45 /* distributed under the License is distributed on an "AS IS" BASIS, */
46 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
47 /* implied. See the License for the specific language governing */
48 /* permissions and limitations under the License. */
49 /**************************************************************************/
50
51 #import "metal_device_properties.h"
52
53 #import "metal_utils.h"
54
55 #import "servers/rendering/renderer_rd/effects/metal_fx.h"
56
57 #import <Metal/Metal.h>
58 #import <MetalFX/MetalFX.h>
59 #import <spirv_cross.hpp>
60 #import <spirv_msl.hpp>
61
62 // Common scaling multipliers.
63 #define KIBI (1024)
64 #define MEBI (KIBI * KIBI)
65
66 #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MAX_ALLOWED < 140000) || (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED < 170000)
67 #define MTLGPUFamilyApple9 (MTLGPUFamily)1009
68 #endif
69
70 API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0), visionos(1.0))
71 MTLGPUFamily &operator--(MTLGPUFamily &p_family) {
72 p_family = static_cast<MTLGPUFamily>(static_cast<int>(p_family) - 1);
73 if (p_family < MTLGPUFamilyApple1) {
74 p_family = MTLGPUFamilyApple9;
75 }
76
77 return p_family;
78 }
79
80 void MetalDeviceProperties::init_features(id<MTLDevice> p_device) {
81 features = {};
82
83 MTLCompileOptions *opts = [MTLCompileOptions new];
84 features.msl_max_version = make_msl_version((opts.languageVersion >> 0x10) & 0xff, (opts.languageVersion >> 0x00) & 0xff);
85 features.msl_target_version = features.msl_max_version;
86 if (String version = OS::get_singleton()->get_environment("GODOT_MTL_TARGET_VERSION"); !version.is_empty()) {
87 if (version != "max") {
88 Vector<String> parts = version.split(".", true, 2);
89 if (parts.size() == 2) {
90 uint32_t major = parts[0].to_int();
91 uint32_t minor = parts[1].to_int();
92 uint32_t msl_version = make_msl_version(major, minor);
93 if (msl_version < MSL_VERSION_23 || msl_version > MSL_VERSION_40) {
94 WARN_PRINT(vformat("GODOT_MTL_TARGET_VERSION: invalid MSL version '%d.%d'", major, minor));
95 } else {
96 print_line(vformat("Override: Targeting Metal version %d.%d", major, minor));
97 features.msl_target_version = msl_version;
98 }
99 } else {
100 WARN_PRINT("GODOT_MTL_TARGET_VERSION: invalid version string format. Expected major.minor or 'max'.");
101 }
102 }
103 }
104
105 features.highestFamily = MTLGPUFamilyApple1;
106 for (MTLGPUFamily family = MTLGPUFamilyApple9; family >= MTLGPUFamilyApple1; --family) {
107 if ([p_device supportsFamily:family]) {
108 features.highestFamily = family;
109 break;
110 }
111 }
112
113 if (@available(macOS 11, iOS 16.4, tvOS 16.4, *)) {
114 features.supportsBCTextureCompression = p_device.supportsBCTextureCompression;
115 } else {
116 features.supportsBCTextureCompression = false;
117 }
118
119 #if TARGET_OS_OSX
120 features.supportsDepth24Stencil8 = p_device.isDepth24Stencil8PixelFormatSupported;
121 #endif
122
123 if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
124 features.supports32BitFloatFiltering = p_device.supports32BitFloatFiltering;
125 features.supports32BitMSAA = p_device.supports32BitMSAA;
126 }
127
128 if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
129 features.supports_gpu_address = true;
130 }
131
132 features.hostMemoryPageSize = sysconf(_SC_PAGESIZE);
133
134 for (SampleCount sc = SampleCount1; sc <= SampleCount64; sc <<= 1) {
135 if ([p_device supportsTextureSampleCount:sc]) {
136 features.supportedSampleCounts |= sc;
137 }
138 }
139
140 features.layeredRendering = [p_device supportsFamily:MTLGPUFamilyApple5];
141 features.multisampleLayeredRendering = [p_device supportsFamily:MTLGPUFamilyApple7];
142 features.tessellationShader = [p_device supportsFamily:MTLGPUFamilyApple3];
143 features.imageCubeArray = [p_device supportsFamily:MTLGPUFamilyApple3];
144 features.quadPermute = [p_device supportsFamily:MTLGPUFamilyApple4];
145 features.simdPermute = [p_device supportsFamily:MTLGPUFamilyApple6];
146 features.simdReduction = [p_device supportsFamily:MTLGPUFamilyApple7];
147 features.argument_buffers_tier = p_device.argumentBuffersSupport;
148 features.supports_image_atomic_32_bit = [p_device supportsFamily:MTLGPUFamilyApple6];
149 features.supports_image_atomic_64_bit = [p_device supportsFamily:MTLGPUFamilyApple9] || ([p_device supportsFamily:MTLGPUFamilyApple8] && [p_device supportsFamily:MTLGPUFamilyMac2]);
150
151 if (features.msl_target_version >= MSL_VERSION_31) {
152 // Native atomics are only supported on 3.1 and above.
153 if (@available(macOS 14.0, iOS 17.0, tvOS 17.0, visionOS 1.0, *)) {
154 features.supports_native_image_atomics = true;
155 }
156 }
157
158 if (OS::get_singleton()->get_environment("GODOT_MTL_DISABLE_IMAGE_ATOMICS") == "1") {
159 features.supports_native_image_atomics = false;
160 }
161
162 if (@available(macOS 15.0, iOS 18.0, tvOS 18.0, visionOS 2.0, *)) {
163 features.supports_residency_sets = true;
164 } else {
165 features.supports_residency_sets = false;
166 }
167
168 if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
169 features.needs_arg_encoders = !([p_device supportsFamily:MTLGPUFamilyMetal3] && features.argument_buffers_tier == MTLArgumentBuffersTier2);
170 }
171
172 if (String v = OS::get_singleton()->get_environment("GODOT_MTL_DISABLE_ARGUMENT_BUFFERS"); v == "1") {
173 features.use_argument_buffers = false;
174 }
175
176 if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
177 features.metal_fx_spatial = [MTLFXSpatialScalerDescriptor supportsDevice:p_device];
178 #ifdef METAL_MFXTEMPORAL_ENABLED
179 features.metal_fx_temporal = [MTLFXTemporalScalerDescriptor supportsDevice:p_device];
180 #else
181 features.metal_fx_temporal = false;
182 #endif
183 }
184 }
185
186 void MetalDeviceProperties::init_limits(id<MTLDevice> p_device) {
187 using std::max;
188 using std::min;
189
190 // FST: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
191
192 // FST: Maximum number of layers per 1D texture array, 2D texture array, or 3D texture.
193 limits.maxImageArrayLayers = 2048;
194 if ([p_device supportsFamily:MTLGPUFamilyApple3]) {
195 // FST: Maximum 2D texture width and height.
196 limits.maxFramebufferWidth = 16384;
197 limits.maxFramebufferHeight = 16384;
198 limits.maxViewportDimensionX = 16384;
199 limits.maxViewportDimensionY = 16384;
200 // FST: Maximum 1D texture width.
201 limits.maxImageDimension1D = 16384;
202 // FST: Maximum 2D texture width and height.
203 limits.maxImageDimension2D = 16384;
204 // FST: Maximum cube map texture width and height.
205 limits.maxImageDimensionCube = 16384;
206 } else {
207 // FST: Maximum 2D texture width and height.
208 limits.maxFramebufferWidth = 8192;
209 limits.maxFramebufferHeight = 8192;
210 limits.maxViewportDimensionX = 8192;
211 limits.maxViewportDimensionY = 8192;
212 // FST: Maximum 1D texture width.
213 limits.maxImageDimension1D = 8192;
214 // FST: Maximum 2D texture width and height.
215 limits.maxImageDimension2D = 8192;
216 // FST: Maximum cube map texture width and height.
217 limits.maxImageDimensionCube = 8192;
218 }
219 // FST: Maximum 3D texture width, height, and depth.
220 limits.maxImageDimension3D = 2048;
221
222 limits.maxThreadsPerThreadGroup = p_device.maxThreadsPerThreadgroup;
223 // No effective limits.
224 limits.maxComputeWorkGroupCount = { std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max() };
225 // https://github.com/KhronosGroup/MoltenVK/blob/568cc3acc0e2299931fdaecaaa1fc3ec5b4af281/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h#L85
226 limits.maxBoundDescriptorSets = SPIRV_CROSS_NAMESPACE::kMaxArgumentBuffers;
227 // FST: Maximum number of color render targets per render pass descriptor.
228 limits.maxColorAttachments = 8;
229
230 // Maximum number of textures the device can access, per stage, from an argument buffer.
231 if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
232 limits.maxTexturesPerArgumentBuffer = 1'000'000;
233 } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
234 limits.maxTexturesPerArgumentBuffer = 96;
235 } else {
236 limits.maxTexturesPerArgumentBuffer = 31;
237 }
238
239 // Maximum number of samplers the device can access, per stage, from an argument buffer.
240 if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
241 limits.maxSamplersPerArgumentBuffer = 1024;
242 } else {
243 limits.maxSamplersPerArgumentBuffer = 16;
244 }
245
246 // Maximum number of buffers the device can access, per stage, from an argument buffer.
247 if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
248 limits.maxBuffersPerArgumentBuffer = std::numeric_limits<uint64_t>::max();
249 } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
250 limits.maxBuffersPerArgumentBuffer = 96;
251 } else {
252 limits.maxBuffersPerArgumentBuffer = 31;
253 }
254
255 limits.minSubgroupSize = limits.maxSubgroupSize = 1;
256 // These values were taken from MoltenVK.
257 if (features.simdPermute) {
258 limits.minSubgroupSize = 4;
259 limits.maxSubgroupSize = 32;
260 } else if (features.quadPermute) {
261 limits.minSubgroupSize = limits.maxSubgroupSize = 4;
262 }
263
264 limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_COMPUTE_BIT);
265 if (features.tessellationShader) {
266 limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_TESSELATION_CONTROL_BIT);
267 }
268 limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_FRAGMENT_BIT);
269
270 limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_BASIC_BIT);
271 if (features.simdPermute || features.quadPermute) {
272 limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_VOTE_BIT);
273 limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_BALLOT_BIT);
274 limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_SHUFFLE_BIT);
275 limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_SHUFFLE_RELATIVE_BIT);
276 }
277
278 if (features.simdReduction) {
279 limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_ARITHMETIC_BIT);
280 }
281
282 if (features.quadPermute) {
283 limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_QUAD_BIT);
284 }
285
286 limits.maxBufferLength = p_device.maxBufferLength;
287
288 // FST: Maximum size of vertex descriptor layout stride.
289 limits.maxVertexDescriptorLayoutStride = std::numeric_limits<uint64_t>::max();
290
291 // Maximum number of viewports.
292 if ([p_device supportsFamily:MTLGPUFamilyApple5]) {
293 limits.maxViewports = 16;
294 } else {
295 limits.maxViewports = 1;
296 }
297
298 limits.maxPerStageBufferCount = 31;
299 limits.maxPerStageSamplerCount = 16;
300 if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
301 limits.maxPerStageTextureCount = 128;
302 } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
303 limits.maxPerStageTextureCount = 96;
304 } else {
305 limits.maxPerStageTextureCount = 31;
306 }
307
308 limits.maxVertexInputAttributes = 31;
309 limits.maxVertexInputBindings = 31;
310 limits.maxVertexInputBindingStride = (2 * KIBI);
311 limits.maxShaderVaryings = 31; // Accurate on Apple4 and above. See: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
312
313 if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
314 limits.maxThreadGroupMemoryAllocation = 32768;
315 } else if ([p_device supportsFamily:MTLGPUFamilyApple3]) {
316 limits.maxThreadGroupMemoryAllocation = 16384;
317 } else {
318 limits.maxThreadGroupMemoryAllocation = 16352;
319 }
320
321 #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
322 limits.minUniformBufferOffsetAlignment = 64;
323 #endif
324
325 #if TARGET_OS_OSX
326 // This is Apple Silicon specific.
327 limits.minUniformBufferOffsetAlignment = 16;
328 #endif
329
330 limits.maxDrawIndexedIndexValue = std::numeric_limits<uint32_t>::max() - 1;
331
332 #ifdef METAL_MFXTEMPORAL_ENABLED
333 if (@available(macOS 14.0, iOS 17.0, tvOS 17.0, *)) {
334 limits.temporalScalerInputContentMinScale = (double)[MTLFXTemporalScalerDescriptor supportedInputContentMinScaleForDevice:p_device];
335 limits.temporalScalerInputContentMaxScale = (double)[MTLFXTemporalScalerDescriptor supportedInputContentMaxScaleForDevice:p_device];
336 } else {
337 // Defaults taken from macOS 14+
338 limits.temporalScalerInputContentMinScale = 1.0;
339 limits.temporalScalerInputContentMaxScale = 3.0;
340 }
341 #else
342 // Defaults taken from macOS 14+
343 limits.temporalScalerInputContentMinScale = 1.0;
344 limits.temporalScalerInputContentMaxScale = 3.0;
345 #endif
346 }
347
348 void MetalDeviceProperties::init_os_props() {
349 NSOperatingSystemVersion ver = NSProcessInfo.processInfo.operatingSystemVersion;
350 os_version = (uint32_t)ver.majorVersion * 10000 + (uint32_t)ver.minorVersion * 100 + (uint32_t)ver.patchVersion;
351 }
352
353 MetalDeviceProperties::MetalDeviceProperties(id<MTLDevice> p_device) {
354 init_features(p_device);
355 init_limits(p_device);
356 init_os_props();
357 }
358
359 MetalDeviceProperties::~MetalDeviceProperties() {
360 }
361
362 SampleCount MetalDeviceProperties::find_nearest_supported_sample_count(RenderingDevice::TextureSamples p_samples) const {
363 SampleCount supported = features.supportedSampleCounts;
364 if (supported & sample_count[p_samples]) {
365 return sample_count[p_samples];
366 }
367
368 SampleCount requested_sample_count = sample_count[p_samples];
369 // Find the nearest supported sample count.
370 while (requested_sample_count > SampleCount1) {
371 if (supported & requested_sample_count) {
372 return requested_sample_count;
373 }
374 requested_sample_count = (SampleCount)(requested_sample_count >> 1);
375 }
376
377 return SampleCount1;
378 }
379
380 // region static members
381
382 const SampleCount MetalDeviceProperties::sample_count[RenderingDevice::TextureSamples::TEXTURE_SAMPLES_MAX] = {
383 SampleCount1,
384 SampleCount2,
385 SampleCount4,
386 SampleCount8,
387 SampleCount16,
388 SampleCount32,
389 SampleCount64,
390 };
391
392 // endregion