metal_device_properties.mm 13 KB

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