metal_device_properties.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #import <Metal/Metal.h>
  51. #import <MetalFX/MetalFX.h>
  52. #import <spirv_cross.hpp>
  53. #import <spirv_msl.hpp>
  54. // Common scaling multipliers.
  55. #define KIBI (1024)
  56. #define MEBI (KIBI * KIBI)
  57. #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MAX_ALLOWED < 140000) || (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED < 170000)
  58. #define MTLGPUFamilyApple9 (MTLGPUFamily)1009
  59. #endif
  60. API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0))
  61. MTLGPUFamily &operator--(MTLGPUFamily &p_family) {
  62. p_family = static_cast<MTLGPUFamily>(static_cast<int>(p_family) - 1);
  63. if (p_family < MTLGPUFamilyApple1) {
  64. p_family = MTLGPUFamilyApple9;
  65. }
  66. return p_family;
  67. }
  68. void MetalDeviceProperties::init_features(id<MTLDevice> p_device) {
  69. features = {};
  70. features.highestFamily = MTLGPUFamilyApple1;
  71. for (MTLGPUFamily family = MTLGPUFamilyApple9; family >= MTLGPUFamilyApple1; --family) {
  72. if ([p_device supportsFamily:family]) {
  73. features.highestFamily = family;
  74. break;
  75. }
  76. }
  77. if (@available(macOS 11, iOS 16.4, tvOS 16.4, *)) {
  78. features.supportsBCTextureCompression = p_device.supportsBCTextureCompression;
  79. } else {
  80. features.supportsBCTextureCompression = false;
  81. }
  82. #if TARGET_OS_OSX
  83. features.supportsDepth24Stencil8 = p_device.isDepth24Stencil8PixelFormatSupported;
  84. #endif
  85. if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
  86. features.supports32BitFloatFiltering = p_device.supports32BitFloatFiltering;
  87. features.supports32BitMSAA = p_device.supports32BitMSAA;
  88. }
  89. if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
  90. features.supports_gpu_address = true;
  91. }
  92. features.hostMemoryPageSize = sysconf(_SC_PAGESIZE);
  93. for (SampleCount sc = SampleCount1; sc <= SampleCount64; sc <<= 1) {
  94. if ([p_device supportsTextureSampleCount:sc]) {
  95. features.supportedSampleCounts |= sc;
  96. }
  97. }
  98. features.layeredRendering = [p_device supportsFamily:MTLGPUFamilyApple5];
  99. features.multisampleLayeredRendering = [p_device supportsFamily:MTLGPUFamilyApple7];
  100. features.tessellationShader = [p_device supportsFamily:MTLGPUFamilyApple3];
  101. features.imageCubeArray = [p_device supportsFamily:MTLGPUFamilyApple3];
  102. features.quadPermute = [p_device supportsFamily:MTLGPUFamilyApple4];
  103. features.simdPermute = [p_device supportsFamily:MTLGPUFamilyApple6];
  104. features.simdReduction = [p_device supportsFamily:MTLGPUFamilyApple7];
  105. features.argument_buffers_tier = p_device.argumentBuffersSupport;
  106. if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
  107. features.needs_arg_encoders = !([p_device supportsFamily:MTLGPUFamilyMetal3] && features.argument_buffers_tier == MTLArgumentBuffersTier2);
  108. }
  109. if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
  110. features.metal_fx_spatial = [MTLFXSpatialScalerDescriptor supportsDevice:p_device];
  111. features.metal_fx_temporal = [MTLFXTemporalScalerDescriptor supportsDevice:p_device];
  112. }
  113. MTLCompileOptions *opts = [MTLCompileOptions new];
  114. features.mslVersionEnum = opts.languageVersion; // By default, Metal uses the most recent language version.
  115. #define setMSLVersion(m_maj, m_min) \
  116. features.mslVersion = SPIRV_CROSS_NAMESPACE::CompilerMSL::Options::make_msl_version(m_maj, m_min)
  117. switch (features.mslVersionEnum) {
  118. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 150000 || __IPHONE_OS_VERSION_MAX_ALLOWED >= 180000 || __TV_OS_VERSION_MAX_ALLOWED >= 180000
  119. case MTLLanguageVersion3_2:
  120. setMSLVersion(3, 2);
  121. break;
  122. #endif
  123. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 140000 || __IPHONE_OS_VERSION_MAX_ALLOWED >= 170000 || __TV_OS_VERSION_MAX_ALLOWED >= 170000
  124. case MTLLanguageVersion3_1:
  125. setMSLVersion(3, 1);
  126. break;
  127. #endif
  128. case MTLLanguageVersion3_0:
  129. setMSLVersion(3, 0);
  130. break;
  131. case MTLLanguageVersion2_4:
  132. setMSLVersion(2, 4);
  133. break;
  134. case MTLLanguageVersion2_3:
  135. setMSLVersion(2, 3);
  136. break;
  137. case MTLLanguageVersion2_2:
  138. setMSLVersion(2, 2);
  139. break;
  140. case MTLLanguageVersion2_1:
  141. setMSLVersion(2, 1);
  142. break;
  143. case MTLLanguageVersion2_0:
  144. setMSLVersion(2, 0);
  145. break;
  146. case MTLLanguageVersion1_2:
  147. setMSLVersion(1, 2);
  148. break;
  149. case MTLLanguageVersion1_1:
  150. setMSLVersion(1, 1);
  151. break;
  152. #if TARGET_OS_IPHONE && !TARGET_OS_MACCATALYST
  153. case MTLLanguageVersion1_0:
  154. setMSLVersion(1, 0);
  155. break;
  156. #endif
  157. }
  158. }
  159. void MetalDeviceProperties::init_limits(id<MTLDevice> p_device) {
  160. using std::max;
  161. using std::min;
  162. // FST: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  163. // FST: Maximum number of layers per 1D texture array, 2D texture array, or 3D texture.
  164. limits.maxImageArrayLayers = 2048;
  165. if ([p_device supportsFamily:MTLGPUFamilyApple3]) {
  166. // FST: Maximum 2D texture width and height.
  167. limits.maxFramebufferWidth = 16384;
  168. limits.maxFramebufferHeight = 16384;
  169. limits.maxViewportDimensionX = 16384;
  170. limits.maxViewportDimensionY = 16384;
  171. // FST: Maximum 1D texture width.
  172. limits.maxImageDimension1D = 16384;
  173. // FST: Maximum 2D texture width and height.
  174. limits.maxImageDimension2D = 16384;
  175. // FST: Maximum cube map texture width and height.
  176. limits.maxImageDimensionCube = 16384;
  177. } else {
  178. // FST: Maximum 2D texture width and height.
  179. limits.maxFramebufferWidth = 8192;
  180. limits.maxFramebufferHeight = 8192;
  181. limits.maxViewportDimensionX = 8192;
  182. limits.maxViewportDimensionY = 8192;
  183. // FST: Maximum 1D texture width.
  184. limits.maxImageDimension1D = 8192;
  185. // FST: Maximum 2D texture width and height.
  186. limits.maxImageDimension2D = 8192;
  187. // FST: Maximum cube map texture width and height.
  188. limits.maxImageDimensionCube = 8192;
  189. }
  190. // FST: Maximum 3D texture width, height, and depth.
  191. limits.maxImageDimension3D = 2048;
  192. limits.maxThreadsPerThreadGroup = p_device.maxThreadsPerThreadgroup;
  193. // No effective limits.
  194. limits.maxComputeWorkGroupCount = { std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max() };
  195. // https://github.com/KhronosGroup/MoltenVK/blob/568cc3acc0e2299931fdaecaaa1fc3ec5b4af281/MoltenVK/MoltenVK/GPUObjects/MVKDevice.h#L85
  196. limits.maxBoundDescriptorSets = SPIRV_CROSS_NAMESPACE::kMaxArgumentBuffers;
  197. // FST: Maximum number of color render targets per render pass descriptor.
  198. limits.maxColorAttachments = 8;
  199. // Maximum number of textures the device can access, per stage, from an argument buffer.
  200. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  201. limits.maxTexturesPerArgumentBuffer = 1'000'000;
  202. } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
  203. limits.maxTexturesPerArgumentBuffer = 96;
  204. } else {
  205. limits.maxTexturesPerArgumentBuffer = 31;
  206. }
  207. // Maximum number of samplers the device can access, per stage, from an argument buffer.
  208. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  209. limits.maxSamplersPerArgumentBuffer = 1024;
  210. } else {
  211. limits.maxSamplersPerArgumentBuffer = 16;
  212. }
  213. // Maximum number of buffers the device can access, per stage, from an argument buffer.
  214. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  215. limits.maxBuffersPerArgumentBuffer = std::numeric_limits<uint64_t>::max();
  216. } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
  217. limits.maxBuffersPerArgumentBuffer = 96;
  218. } else {
  219. limits.maxBuffersPerArgumentBuffer = 31;
  220. }
  221. limits.minSubgroupSize = limits.maxSubgroupSize = 1;
  222. // These values were taken from MoltenVK.
  223. if (features.simdPermute) {
  224. limits.minSubgroupSize = 4;
  225. limits.maxSubgroupSize = 32;
  226. } else if (features.quadPermute) {
  227. limits.minSubgroupSize = limits.maxSubgroupSize = 4;
  228. }
  229. limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_COMPUTE_BIT);
  230. if (features.tessellationShader) {
  231. limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_TESSELATION_CONTROL_BIT);
  232. }
  233. limits.subgroupSupportedShaderStages.set_flag(RDD::ShaderStage::SHADER_STAGE_FRAGMENT_BIT);
  234. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_BASIC_BIT);
  235. if (features.simdPermute || features.quadPermute) {
  236. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_VOTE_BIT);
  237. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_BALLOT_BIT);
  238. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_SHUFFLE_BIT);
  239. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_SHUFFLE_RELATIVE_BIT);
  240. }
  241. if (features.simdReduction) {
  242. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_ARITHMETIC_BIT);
  243. }
  244. if (features.quadPermute) {
  245. limits.subgroupSupportedOperations.set_flag(RD::SubgroupOperations::SUBGROUP_QUAD_BIT);
  246. }
  247. limits.maxBufferLength = p_device.maxBufferLength;
  248. // FST: Maximum size of vertex descriptor layout stride.
  249. limits.maxVertexDescriptorLayoutStride = std::numeric_limits<uint64_t>::max();
  250. // Maximum number of viewports.
  251. if ([p_device supportsFamily:MTLGPUFamilyApple5]) {
  252. limits.maxViewports = 16;
  253. } else {
  254. limits.maxViewports = 1;
  255. }
  256. limits.maxPerStageBufferCount = 31;
  257. limits.maxPerStageSamplerCount = 16;
  258. if ([p_device supportsFamily:MTLGPUFamilyApple6]) {
  259. limits.maxPerStageTextureCount = 128;
  260. } else if ([p_device supportsFamily:MTLGPUFamilyApple4]) {
  261. limits.maxPerStageTextureCount = 96;
  262. } else {
  263. limits.maxPerStageTextureCount = 31;
  264. }
  265. limits.maxVertexInputAttributes = 31;
  266. limits.maxVertexInputBindings = 31;
  267. limits.maxVertexInputBindingStride = (2 * KIBI);
  268. limits.maxShaderVaryings = 31; // Accurate on Apple4 and above. See: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  269. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  270. limits.minUniformBufferOffsetAlignment = 64;
  271. #endif
  272. #if TARGET_OS_OSX
  273. // This is Apple Silicon specific.
  274. limits.minUniformBufferOffsetAlignment = 16;
  275. #endif
  276. limits.maxDrawIndexedIndexValue = std::numeric_limits<uint32_t>::max() - 1;
  277. if (@available(macOS 14.0, iOS 17.0, tvOS 17.0, *)) {
  278. limits.temporalScalerInputContentMinScale = (double)[MTLFXTemporalScalerDescriptor supportedInputContentMinScaleForDevice:p_device];
  279. limits.temporalScalerInputContentMaxScale = (double)[MTLFXTemporalScalerDescriptor supportedInputContentMaxScaleForDevice:p_device];
  280. } else {
  281. // Defaults taken from macOS 14+
  282. limits.temporalScalerInputContentMinScale = 1.0;
  283. limits.temporalScalerInputContentMaxScale = 3.0;
  284. }
  285. }
  286. MetalDeviceProperties::MetalDeviceProperties(id<MTLDevice> p_device) {
  287. init_features(p_device);
  288. init_limits(p_device);
  289. }
  290. MetalDeviceProperties::~MetalDeviceProperties() {
  291. }
  292. SampleCount MetalDeviceProperties::find_nearest_supported_sample_count(RenderingDevice::TextureSamples p_samples) const {
  293. SampleCount supported = features.supportedSampleCounts;
  294. if (supported & sample_count[p_samples]) {
  295. return sample_count[p_samples];
  296. }
  297. SampleCount requested_sample_count = sample_count[p_samples];
  298. // Find the nearest supported sample count.
  299. while (requested_sample_count > SampleCount1) {
  300. if (supported & requested_sample_count) {
  301. return requested_sample_count;
  302. }
  303. requested_sample_count = (SampleCount)(requested_sample_count >> 1);
  304. }
  305. return SampleCount1;
  306. }
  307. // region static members
  308. const SampleCount MetalDeviceProperties::sample_count[RenderingDevice::TextureSamples::TEXTURE_SAMPLES_MAX] = {
  309. SampleCount1,
  310. SampleCount2,
  311. SampleCount4,
  312. SampleCount8,
  313. SampleCount16,
  314. SampleCount32,
  315. SampleCount64,
  316. };
  317. // endregion