metal_device_properties.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. /* Portions of this code were derived from MoltenVK. */
  33. /* */
  34. /* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. */
  35. /* (http://www.brenwill.com) */
  36. /* */
  37. /* Licensed under the Apache License, Version 2.0 (the "License"); */
  38. /* you may not use this file except in compliance with the License. */
  39. /* You may obtain a copy of the License at */
  40. /* */
  41. /* http://www.apache.org/licenses/LICENSE-2.0 */
  42. /* */
  43. /* Unless required by applicable law or agreed to in writing, software */
  44. /* distributed under the License is distributed on an "AS IS" BASIS, */
  45. /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
  46. /* implied. See the License for the specific language governing */
  47. /* permissions and limitations under the License. */
  48. /**************************************************************************/
  49. #import "metal_device_properties.h"
  50. #include "servers/rendering/renderer_rd/effects/metal_fx.h"
  51. #import <Metal/Metal.h>
  52. #import <MetalFX/MetalFX.h>
  53. #import <spirv_cross.hpp>
  54. #import <spirv_msl.hpp>
  55. // Common scaling multipliers.
  56. #define KIBI (1024)
  57. #define MEBI (KIBI * KIBI)
  58. #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MAX_ALLOWED < 140000) || (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED < 170000)
  59. #define MTLGPUFamilyApple9 (MTLGPUFamily)1009
  60. #endif
  61. API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0), visionos(1.0))
  62. MTLGPUFamily &operator--(MTLGPUFamily &p_family) {
  63. p_family = static_cast<MTLGPUFamily>(static_cast<int>(p_family) - 1);
  64. if (p_family < MTLGPUFamilyApple1) {
  65. p_family = MTLGPUFamilyApple9;
  66. }
  67. return p_family;
  68. }
  69. void MetalDeviceProperties::init_features(id<MTLDevice> p_device) {
  70. features = {};
  71. features.highestFamily = MTLGPUFamilyApple1;
  72. for (MTLGPUFamily family = MTLGPUFamilyApple9; family >= MTLGPUFamilyApple1; --family) {
  73. if ([p_device supportsFamily:family]) {
  74. features.highestFamily = family;
  75. break;
  76. }
  77. }
  78. if (@available(macOS 11, iOS 16.4, tvOS 16.4, *)) {
  79. features.supportsBCTextureCompression = p_device.supportsBCTextureCompression;
  80. } else {
  81. features.supportsBCTextureCompression = false;
  82. }
  83. #if TARGET_OS_OSX
  84. features.supportsDepth24Stencil8 = p_device.isDepth24Stencil8PixelFormatSupported;
  85. #endif
  86. if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
  87. features.supports32BitFloatFiltering = p_device.supports32BitFloatFiltering;
  88. features.supports32BitMSAA = p_device.supports32BitMSAA;
  89. }
  90. if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
  91. features.supports_gpu_address = true;
  92. }
  93. features.hostMemoryPageSize = sysconf(_SC_PAGESIZE);
  94. for (SampleCount sc = SampleCount1; sc <= SampleCount64; sc <<= 1) {
  95. if ([p_device supportsTextureSampleCount:sc]) {
  96. features.supportedSampleCounts |= sc;
  97. }
  98. }
  99. features.layeredRendering = [p_device supportsFamily:MTLGPUFamilyApple5];
  100. features.multisampleLayeredRendering = [p_device supportsFamily:MTLGPUFamilyApple7];
  101. features.tessellationShader = [p_device supportsFamily:MTLGPUFamilyApple3];
  102. features.imageCubeArray = [p_device supportsFamily:MTLGPUFamilyApple3];
  103. features.quadPermute = [p_device supportsFamily:MTLGPUFamilyApple4];
  104. features.simdPermute = [p_device supportsFamily:MTLGPUFamilyApple6];
  105. features.simdReduction = [p_device supportsFamily:MTLGPUFamilyApple7];
  106. features.argument_buffers_tier = p_device.argumentBuffersSupport;
  107. features.supports_image_atomic_32_bit = [p_device supportsFamily:MTLGPUFamilyApple6];
  108. features.supports_image_atomic_64_bit = [p_device supportsFamily:MTLGPUFamilyApple9] || ([p_device supportsFamily:MTLGPUFamilyApple8] && [p_device supportsFamily:MTLGPUFamilyMac2]);
  109. if (@available(macOS 14.0, iOS 17.0, tvOS 17.0, visionOS 1.0, *)) {
  110. features.supports_native_image_atomics = true;
  111. }
  112. if (OS::get_singleton()->get_environment("GODOT_MTL_DISABLE_IMAGE_ATOMICS") == "1") {
  113. features.supports_native_image_atomics = false;
  114. }
  115. if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
  116. features.needs_arg_encoders = !([p_device supportsFamily:MTLGPUFamilyMetal3] && features.argument_buffers_tier == MTLArgumentBuffersTier2);
  117. }
  118. if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
  119. features.metal_fx_spatial = [MTLFXSpatialScalerDescriptor supportsDevice:p_device];
  120. #ifdef METAL_MFXTEMPORAL_ENABLED
  121. features.metal_fx_temporal = [MTLFXTemporalScalerDescriptor supportsDevice:p_device];
  122. #else
  123. features.metal_fx_temporal = false;
  124. #endif
  125. }
  126. MTLCompileOptions *opts = [MTLCompileOptions new];
  127. features.mslVersionEnum = opts.languageVersion; // By default, Metal uses the most recent language version.
  128. features.mslVersionMajor = (opts.languageVersion >> 0x10) & 0xff;
  129. features.mslVersionMinor = (opts.languageVersion >> 0x00) & 0xff;
  130. }
  131. void MetalDeviceProperties::init_limits(id<MTLDevice> p_device) {
  132. using std::max;
  133. using std::min;
  134. // FST: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  135. // FST: Maximum number of layers per 1D texture array, 2D texture array, or 3D texture.
  136. limits.maxImageArrayLayers = 2048;
  137. if ([p_device supportsFamily:MTLGPUFamilyApple3]) {
  138. // FST: Maximum 2D texture width and height.
  139. limits.maxFramebufferWidth = 16384;
  140. limits.maxFramebufferHeight = 16384;
  141. limits.maxViewportDimensionX = 16384;
  142. limits.maxViewportDimensionY = 16384;
  143. // FST: Maximum 1D texture width.
  144. limits.maxImageDimension1D = 16384;
  145. // FST: Maximum 2D texture width and height.
  146. limits.maxImageDimension2D = 16384;
  147. // FST: Maximum cube map texture width and height.
  148. limits.maxImageDimensionCube = 16384;
  149. } else {
  150. // FST: Maximum 2D texture width and height.
  151. limits.maxFramebufferWidth = 8192;
  152. limits.maxFramebufferHeight = 8192;
  153. limits.maxViewportDimensionX = 8192;
  154. limits.maxViewportDimensionY = 8192;
  155. // FST: Maximum 1D texture width.
  156. limits.maxImageDimension1D = 8192;
  157. // FST: Maximum 2D texture width and height.
  158. limits.maxImageDimension2D = 8192;
  159. // FST: Maximum cube map texture width and height.
  160. limits.maxImageDimensionCube = 8192;
  161. }
  162. // FST: Maximum 3D texture width, height, and depth.
  163. limits.maxImageDimension3D = 2048;
  164. limits.maxThreadsPerThreadGroup = p_device.maxThreadsPerThreadgroup;
  165. // No effective limits.
  166. limits.maxComputeWorkGroupCount = { std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max() };
  167. // https://github.com/KhronosGroup/MoltenVK/blob/568cc3acc0e2299931fdaecaaa1fc3ec5b4af281/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h#L85
  168. limits.maxBoundDescriptorSets = SPIRV_CROSS_NAMESPACE::kMaxArgumentBuffers;
  169. // FST: Maximum number of color render targets per render pass descriptor.
  170. limits.maxColorAttachments = 8;
  171. // Maximum number of textures the device can access, per stage, from an argument buffer.
  172. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  173. limits.maxTexturesPerArgumentBuffer = 1'000'000;
  174. } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
  175. limits.maxTexturesPerArgumentBuffer = 96;
  176. } else {
  177. limits.maxTexturesPerArgumentBuffer = 31;
  178. }
  179. // Maximum number of samplers the device can access, per stage, from an argument buffer.
  180. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  181. limits.maxSamplersPerArgumentBuffer = 1024;
  182. } else {
  183. limits.maxSamplersPerArgumentBuffer = 16;
  184. }
  185. // Maximum number of buffers the device can access, per stage, from an argument buffer.
  186. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  187. limits.maxBuffersPerArgumentBuffer = std::numeric_limits<uint64_t>::max();
  188. } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
  189. limits.maxBuffersPerArgumentBuffer = 96;
  190. } else {
  191. limits.maxBuffersPerArgumentBuffer = 31;
  192. }
  193. limits.minSubgroupSize = limits.maxSubgroupSize = 1;
  194. // These values were taken from MoltenVK.
  195. if (features.simdPermute) {
  196. limits.minSubgroupSize = 4;
  197. limits.maxSubgroupSize = 32;
  198. } else if (features.quadPermute) {
  199. limits.minSubgroupSize = limits.maxSubgroupSize = 4;
  200. }
  201. limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_COMPUTE_BIT);
  202. if (features.tessellationShader) {
  203. limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_TESSELATION_CONTROL_BIT);
  204. }
  205. limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_FRAGMENT_BIT);
  206. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_BASIC_BIT);
  207. if (features.simdPermute || features.quadPermute) {
  208. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_VOTE_BIT);
  209. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_BALLOT_BIT);
  210. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_SHUFFLE_BIT);
  211. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_SHUFFLE_RELATIVE_BIT);
  212. }
  213. if (features.simdReduction) {
  214. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_ARITHMETIC_BIT);
  215. }
  216. if (features.quadPermute) {
  217. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_QUAD_BIT);
  218. }
  219. limits.maxBufferLength = p_device.maxBufferLength;
  220. // FST: Maximum size of vertex descriptor layout stride.
  221. limits.maxVertexDescriptorLayoutStride = std::numeric_limits<uint64_t>::max();
  222. // Maximum number of viewports.
  223. if ([p_device supportsFamily:MTLGPUFamilyApple5]) {
  224. limits.maxViewports = 16;
  225. } else {
  226. limits.maxViewports = 1;
  227. }
  228. limits.maxPerStageBufferCount = 31;
  229. limits.maxPerStageSamplerCount = 16;
  230. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  231. limits.maxPerStageTextureCount = 128;
  232. } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
  233. limits.maxPerStageTextureCount = 96;
  234. } else {
  235. limits.maxPerStageTextureCount = 31;
  236. }
  237. limits.maxVertexInputAttributes = 31;
  238. limits.maxVertexInputBindings = 31;
  239. limits.maxVertexInputBindingStride = (2 * KIBI);
  240. limits.maxShaderVaryings = 31; // Accurate on Apple4 and above. See: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  241. if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
  242. limits.maxThreadGroupMemoryAllocation = 32768;
  243. } else if ([p_device supportsFamily:MTLGPUFamilyApple3]) {
  244. limits.maxThreadGroupMemoryAllocation = 16384;
  245. } else {
  246. limits.maxThreadGroupMemoryAllocation = 16352;
  247. }
  248. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  249. limits.minUniformBufferOffsetAlignment = 64;
  250. #endif
  251. #if TARGET_OS_OSX
  252. // This is Apple Silicon specific.
  253. limits.minUniformBufferOffsetAlignment = 16;
  254. #endif
  255. limits.maxDrawIndexedIndexValue = std::numeric_limits<uint32_t>::max() - 1;
  256. #ifdef METAL_MFXTEMPORAL_ENABLED
  257. if (@available(macOS 14.0, iOS 17.0, tvOS 17.0, *)) {
  258. limits.temporalScalerInputContentMinScale = (double)[MTLFXTemporalScalerDescriptor supportedInputContentMinScaleForDevice:p_device];
  259. limits.temporalScalerInputContentMaxScale = (double)[MTLFXTemporalScalerDescriptor supportedInputContentMaxScaleForDevice:p_device];
  260. } else {
  261. // Defaults taken from macOS 14+
  262. limits.temporalScalerInputContentMinScale = 1.0;
  263. limits.temporalScalerInputContentMaxScale = 3.0;
  264. }
  265. #else
  266. // Defaults taken from macOS 14+
  267. limits.temporalScalerInputContentMinScale = 1.0;
  268. limits.temporalScalerInputContentMaxScale = 3.0;
  269. #endif
  270. }
  271. MetalDeviceProperties::MetalDeviceProperties(id<MTLDevice> p_device) {
  272. init_features(p_device);
  273. init_limits(p_device);
  274. }
  275. MetalDeviceProperties::~MetalDeviceProperties() {
  276. }
  277. SampleCount MetalDeviceProperties::find_nearest_supported_sample_count(RenderingDevice::TextureSamples p_samples) const {
  278. SampleCount supported = features.supportedSampleCounts;
  279. if (supported & sample_count[p_samples]) {
  280. return sample_count[p_samples];
  281. }
  282. SampleCount requested_sample_count = sample_count[p_samples];
  283. // Find the nearest supported sample count.
  284. while (requested_sample_count > SampleCount1) {
  285. if (supported & requested_sample_count) {
  286. return requested_sample_count;
  287. }
  288. requested_sample_count = (SampleCount)(requested_sample_count >> 1);
  289. }
  290. return SampleCount1;
  291. }
  292. // region static members
  293. const SampleCount MetalDeviceProperties::sample_count[RenderingDevice::TextureSamples::TEXTURE_SAMPLES_MAX] = {
  294. SampleCount1,
  295. SampleCount2,
  296. SampleCount4,
  297. SampleCount8,
  298. SampleCount16,
  299. SampleCount32,
  300. SampleCount64,
  301. };
  302. // endregion