| 1 | /**************************************************************************/ |
| 2 | /* metal_device_properties.h */ |
| 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 | #pragma once |
| 32 | |
| 33 | /**************************************************************************/ |
| 34 | /* */ |
| 35 | /* Portions of this code were derived from MoltenVK. */ |
| 36 | /* */ |
| 37 | /* Copyright (c) 2015-2023 The Brenwill Workshop Ltd. */ |
| 38 | /* (http://www.brenwill.com) */ |
| 39 | /* */ |
| 40 | /* Licensed under the Apache License, Version 2.0 (the "License"); */ |
| 41 | /* you may not use this file except in compliance with the License. */ |
| 42 | /* You may obtain a copy of the License at */ |
| 43 | /* */ |
| 44 | /* http://www.apache.org/licenses/LICENSE-2.0 */ |
| 45 | /* */ |
| 46 | /* Unless required by applicable law or agreed to in writing, software */ |
| 47 | /* distributed under the License is distributed on an "AS IS" BASIS, */ |
| 48 | /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */ |
| 49 | /* implied. See the License for the specific language governing */ |
| 50 | /* permissions and limitations under the License. */ |
| 51 | /**************************************************************************/ |
| 52 | |
| 53 | #import "servers/rendering/rendering_device.h" |
| 54 | |
| 55 | #import <Foundation/Foundation.h> |
| 56 | #import <Metal/Metal.h> |
| 57 | |
| 58 | /** The buffer index to use for vertex content. */ |
| 59 | const static uint32_t VERT_CONTENT_BUFFER_INDEX = 0; |
| 60 | const static uint32_t MAX_COLOR_ATTACHMENT_COUNT = 8; |
| 61 | |
| 62 | typedef NS_OPTIONS(NSUInteger, SampleCount) { |
| 63 | SampleCount1 = (1UL << 0), |
| 64 | SampleCount2 = (1UL << 1), |
| 65 | SampleCount4 = (1UL << 2), |
| 66 | SampleCount8 = (1UL << 3), |
| 67 | SampleCount16 = (1UL << 4), |
| 68 | SampleCount32 = (1UL << 5), |
| 69 | SampleCount64 = (1UL << 6), |
| 70 | }; |
| 71 | |
| 72 | struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MetalFeatures { |
| 73 | /// Maximum version of the Metal Shading Language version available. |
| 74 | uint32_t msl_max_version = 0; |
| 75 | /*! @brief Target version of the Metal Shading Language used to translate shaders. |
| 76 | * |
| 77 | * This can be used to override the features used to generate shaders. Primarily |
| 78 | * for engine developers for testing. |
| 79 | */ |
| 80 | uint32_t msl_target_version = 0; |
| 81 | MTLGPUFamily highestFamily = MTLGPUFamilyApple4; |
| 82 | bool supportsBCTextureCompression = false; |
| 83 | bool supportsDepth24Stencil8 = false; |
| 84 | bool supports32BitFloatFiltering = false; |
| 85 | bool supports32BitMSAA = false; |
| 86 | bool supportsMac = TARGET_OS_OSX; |
| 87 | SampleCount supportedSampleCounts = SampleCount1; |
| 88 | long hostMemoryPageSize = 0; |
| 89 | bool layeredRendering = false; |
| 90 | bool multisampleLayeredRendering = false; |
| 91 | bool quadPermute = false; /**< If true, quadgroup permutation functions (vote, ballot, shuffle) are supported in shaders. */ |
| 92 | bool simdPermute = false; /**< If true, SIMD-group permutation functions (vote, ballot, shuffle) are supported in shaders. */ |
| 93 | bool simdReduction = false; /**< If true, SIMD-group reduction functions (arithmetic) are supported in shaders. */ |
| 94 | bool tessellationShader = false; /**< If true, tessellation shaders are supported. */ |
| 95 | bool imageCubeArray = false; /**< If true, image cube arrays are supported. */ |
| 96 | MTLArgumentBuffersTier argument_buffers_tier = MTLArgumentBuffersTier1; |
| 97 | bool needs_arg_encoders = true; /**< If true, argument encoders are required to encode arguments into an argument buffer. */ |
| 98 | bool use_argument_buffers = true; /**< If true, argument buffers are can be used instead of slot binding, if available. */ |
| 99 | bool metal_fx_spatial = false; /**< If true, Metal FX spatial functions are supported. */ |
| 100 | bool metal_fx_temporal = false; /**< If true, Metal FX temporal functions are supported. */ |
| 101 | bool supports_gpu_address = false; /**< If true, referencing a GPU address in a shader is supported. */ |
| 102 | bool supports_image_atomic_32_bit = false; /**< If true, 32-bit atomic operations on images are supported by the GPU. */ |
| 103 | bool supports_image_atomic_64_bit = false; /**< If true, 64-bit atomic operations on images are supported by the GPU. */ |
| 104 | bool supports_native_image_atomics = false; /**< If true, native image atomic operations are supported by the OS. */ |
| 105 | bool supports_residency_sets = false; /**< If true, residency sets (MTLResidencySet) are supported by the OS. */ |
| 106 | |
| 107 | /*! |
| 108 | * Check if argument buffers are fully supported, which requires tier 2 support and no need for argument encoders. |
| 109 | */ |
| 110 | _FORCE_INLINE_ bool argument_buffers_supported() const { |
| 111 | return argument_buffers_tier == MTLArgumentBuffersTier2 && needs_arg_encoders == false; |
| 112 | } |
| 113 | |
| 114 | /*! |
| 115 | * Check if argument buffers can be used, which requires that they are supported and that the user has enabled their use. |
| 116 | */ |
| 117 | _FORCE_INLINE_ bool argument_buffers_enabled() const { |
| 118 | return use_argument_buffers && argument_buffers_supported(); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | struct MetalLimits { |
| 123 | uint64_t maxImageArrayLayers; |
| 124 | uint64_t maxFramebufferHeight; |
| 125 | uint64_t maxFramebufferWidth; |
| 126 | uint64_t maxImageDimension1D; |
| 127 | uint64_t maxImageDimension2D; |
| 128 | uint64_t maxImageDimension3D; |
| 129 | uint64_t maxImageDimensionCube; |
| 130 | uint64_t maxViewportDimensionX; |
| 131 | uint64_t maxViewportDimensionY; |
| 132 | MTLSize maxThreadsPerThreadGroup; |
| 133 | MTLSize maxComputeWorkGroupCount; |
| 134 | uint64_t maxBoundDescriptorSets; |
| 135 | uint64_t maxColorAttachments; |
| 136 | uint64_t maxTexturesPerArgumentBuffer; |
| 137 | uint64_t maxSamplersPerArgumentBuffer; |
| 138 | uint64_t maxBuffersPerArgumentBuffer; |
| 139 | uint64_t maxBufferLength; |
| 140 | uint64_t minUniformBufferOffsetAlignment; |
| 141 | uint64_t maxVertexDescriptorLayoutStride; |
| 142 | uint16_t maxViewports; |
| 143 | uint32_t maxPerStageBufferCount; /**< The total number of per-stage Metal buffers available for shader uniform content and attributes. */ |
| 144 | uint32_t maxPerStageTextureCount; /**< The total number of per-stage Metal textures available for shader uniform content. */ |
| 145 | uint32_t maxPerStageSamplerCount; /**< The total number of per-stage Metal samplers available for shader uniform content. */ |
| 146 | uint32_t maxVertexInputAttributes; |
| 147 | uint32_t maxVertexInputBindings; |
| 148 | uint32_t maxVertexInputBindingStride; |
| 149 | uint32_t maxDrawIndexedIndexValue; |
| 150 | uint32_t maxShaderVaryings; |
| 151 | uint32_t maxThreadGroupMemoryAllocation; |
| 152 | |
| 153 | double temporalScalerInputContentMinScale; |
| 154 | double temporalScalerInputContentMaxScale; |
| 155 | |
| 156 | uint32_t minSubgroupSize; /**< The minimum number of threads in a SIMD-group. */ |
| 157 | uint32_t maxSubgroupSize; /**< The maximum number of threads in a SIMD-group. */ |
| 158 | BitField<RDD::ShaderStage> subgroupSupportedShaderStages; |
| 159 | BitField<RD::SubgroupOperations> subgroupSupportedOperations; /**< The subgroup operations supported by the device. */ |
| 160 | }; |
| 161 | |
| 162 | class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MetalDeviceProperties { |
| 163 | private: |
| 164 | void init_features(id<MTLDevice> p_device); |
| 165 | void init_limits(id<MTLDevice> p_device); |
| 166 | void init_os_props(); |
| 167 | |
| 168 | public: |
| 169 | MetalFeatures features; |
| 170 | MetalLimits limits; |
| 171 | |
| 172 | // maj * 10000 + min * 100 + patch |
| 173 | uint32_t os_version; |
| 174 | |
| 175 | SampleCount find_nearest_supported_sample_count(RenderingDevice::TextureSamples p_samples) const; |
| 176 | |
| 177 | MetalDeviceProperties(id<MTLDevice> p_device); |
| 178 | ~MetalDeviceProperties(); |
| 179 | |
| 180 | private: |
| 181 | static const SampleCount sample_count[RenderingDevice::TextureSamples::TEXTURE_SAMPLES_MAX]; |
| 182 | }; |