0.2.0-dev Download ZIP
201 lines ·10.0 KB Raw
| 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 | #include "servers/rendering/rendering_device_driver.h" |
| 54 | |
| 55 | #include <Metal/Metal.hpp> |
| 56 | |
| 57 | #include <cstddef> |
| 58 | |
| 59 | /** The buffer index to use for vertex content. */ |
| 60 | const static uint32_t VERT_CONTENT_BUFFER_INDEX = 0; |
| 61 | const static uint32_t MAX_COLOR_ATTACHMENT_COUNT = 8; |
| 62 | |
| 63 | enum SampleCount : NS::UInteger { |
| 64 | SampleCount1 = (1UL << 0), |
| 65 | SampleCount2 = (1UL << 1), |
| 66 | SampleCount4 = (1UL << 2), |
| 67 | SampleCount8 = (1UL << 3), |
| 68 | SampleCount16 = (1UL << 4), |
| 69 | SampleCount32 = (1UL << 5), |
| 70 | SampleCount64 = (1UL << 6), |
| 71 | }; |
| 72 | |
| 73 | _FORCE_INLINE_ SampleCount operator|(SampleCount a, SampleCount b) { |
| 74 | return static_cast<SampleCount>(static_cast<NS::UInteger>(a) | static_cast<NS::UInteger>(b)); |
| 75 | } |
| 76 | |
| 77 | _FORCE_INLINE_ SampleCount &operator|=(SampleCount &a, SampleCount b) { |
| 78 | return a = a | b; |
| 79 | } |
| 80 | |
| 81 | _FORCE_INLINE_ SampleCount operator<<(SampleCount a, int shift) { |
| 82 | return static_cast<SampleCount>(static_cast<NS::UInteger>(a) << shift); |
| 83 | } |
| 84 | |
| 85 | _FORCE_INLINE_ SampleCount &operator<<=(SampleCount &a, int shift) { |
| 86 | return a = a << shift; |
| 87 | } |
| 88 | |
| 89 | struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MetalFeatures { |
| 90 | /// Maximum version of the Metal Shading Language version available. |
| 91 | uint32_t msl_max_version = 0; |
| 92 | /*! @brief Target version of the Metal Shading Language used to translate shaders. |
| 93 | * |
| 94 | * This can be used to override the features used to generate shaders. Primarily |
| 95 | * for engine developers for testing. |
| 96 | */ |
| 97 | uint32_t msl_target_version = 0; |
| 98 | MTL::GPUFamily highestFamily = MTL::GPUFamilyApple4; |
| 99 | bool supportsBCTextureCompression = false; |
| 100 | bool supportsDepth24Stencil8 = false; |
| 101 | bool supports32BitFloatFiltering = false; |
| 102 | bool supports32BitMSAA = false; |
| 103 | bool supportsMac = TARGET_OS_OSX; |
| 104 | SampleCount supportedSampleCounts = SampleCount1; |
| 105 | long hostMemoryPageSize = 0; |
| 106 | bool layeredRendering = false; |
| 107 | bool multisampleLayeredRendering = false; |
| 108 | bool quadPermute = false; /**< If true, quadgroup permutation functions (vote, ballot, shuffle) are supported in shaders. */ |
| 109 | bool simdPermute = false; /**< If true, SIMD-group permutation functions (vote, ballot, shuffle) are supported in shaders. */ |
| 110 | bool simdReduction = false; /**< If true, SIMD-group reduction functions (arithmetic) are supported in shaders. */ |
| 111 | bool tessellationShader = false; /**< If true, tessellation shaders are supported. */ |
| 112 | bool imageCubeArray = false; /**< If true, image cube arrays are supported. */ |
| 113 | MTL::ArgumentBuffersTier argument_buffers_tier = MTL::ArgumentBuffersTier1; |
| 114 | bool needs_arg_encoders = true; /**< If true, argument encoders are required to encode arguments into an argument buffer. */ |
| 115 | bool use_argument_buffers = true; /**< If true, argument buffers are can be used instead of slot binding, if available. */ |
| 116 | bool metal_fx_spatial = false; /**< If true, Metal FX spatial functions are supported. */ |
| 117 | bool metal_fx_temporal = false; /**< If true, Metal FX temporal functions are supported. */ |
| 118 | bool supports_gpu_address = false; /**< If true, referencing a GPU address in a shader is supported. */ |
| 119 | bool supports_image_atomic_32_bit = false; /**< If true, 32-bit atomic operations on images are supported by the GPU. */ |
| 120 | bool supports_image_atomic_64_bit = false; /**< If true, 64-bit atomic operations on images are supported by the GPU. */ |
| 121 | bool supports_native_image_atomics = false; /**< If true, native image atomic operations are supported by the OS. */ |
| 122 | bool supports_border_color = false; /**< If true, sampler border color (clamp-to-border) is supported. Requires Apple7+. */ |
| 123 | bool supports_residency_sets = false; /**< If true, residency sets (MTLResidencySet) are supported by the OS. */ |
| 124 | bool supports_msaa_depth_resolve = false; |
| 125 | |
| 126 | /*! |
| 127 | * Check if argument buffers are fully supported, which requires tier 2 support and no need for argument encoders. |
| 128 | */ |
| 129 | _FORCE_INLINE_ bool argument_buffers_supported() const { |
| 130 | return argument_buffers_tier == MTL::ArgumentBuffersTier2 && needs_arg_encoders == false; |
| 131 | } |
| 132 | |
| 133 | /*! |
| 134 | * Check if argument buffers can be used, which requires that they are supported and that the user has enabled their use. |
| 135 | */ |
| 136 | _FORCE_INLINE_ bool argument_buffers_enabled() const { |
| 137 | return use_argument_buffers && argument_buffers_supported(); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | struct MetalLimits { |
| 142 | uint64_t maxImageArrayLayers; |
| 143 | uint64_t maxFramebufferHeight; |
| 144 | uint64_t maxFramebufferWidth; |
| 145 | uint64_t maxImageDimension1D; |
| 146 | uint64_t maxImageDimension2D; |
| 147 | uint64_t maxImageDimension3D; |
| 148 | uint64_t maxImageDimensionCube; |
| 149 | uint64_t maxViewportDimensionX; |
| 150 | uint64_t maxViewportDimensionY; |
| 151 | MTL::Size maxThreadsPerThreadGroup; |
| 152 | MTL::Size maxComputeWorkGroupCount; |
| 153 | uint64_t maxBoundDescriptorSets; |
| 154 | uint64_t maxColorAttachments; |
| 155 | uint64_t maxTexturesPerArgumentBuffer; |
| 156 | uint64_t maxSamplersPerArgumentBuffer; |
| 157 | uint64_t maxBuffersPerArgumentBuffer; |
| 158 | uint64_t maxBufferLength; |
| 159 | uint64_t minUniformBufferOffsetAlignment; |
| 160 | uint64_t maxVertexDescriptorLayoutStride; |
| 161 | uint16_t maxViewports; |
| 162 | uint32_t maxPerStageBufferCount; /**< The total number of per-stage Metal buffers available for shader uniform content and attributes. */ |
| 163 | uint32_t maxPerStageTextureCount; /**< The total number of per-stage Metal textures available for shader uniform content. */ |
| 164 | uint32_t maxPerStageSamplerCount; /**< The total number of per-stage Metal samplers available for shader uniform content. */ |
| 165 | uint32_t maxVertexInputAttributes; |
| 166 | uint32_t maxVertexInputBindings; |
| 167 | uint32_t maxVertexInputBindingStride; |
| 168 | uint32_t maxDrawIndexedIndexValue; |
| 169 | uint32_t maxShaderVaryings; |
| 170 | uint32_t maxThreadGroupMemoryAllocation; |
| 171 | |
| 172 | double temporalScalerInputContentMinScale; |
| 173 | double temporalScalerInputContentMaxScale; |
| 174 | |
| 175 | uint32_t minSubgroupSize; /**< The minimum number of threads in a SIMD-group. */ |
| 176 | uint32_t maxSubgroupSize; /**< The maximum number of threads in a SIMD-group. */ |
| 177 | BitField<RDD::ShaderStage> subgroupSupportedShaderStages; |
| 178 | BitField<RDD::SubgroupOperations> subgroupSupportedOperations; /**< The subgroup operations supported by the device. */ |
| 179 | }; |
| 180 | |
| 181 | class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MetalDeviceProperties { |
| 182 | private: |
| 183 | void init_features(MTL::Device *p_device); |
| 184 | void init_limits(MTL::Device *p_device); |
| 185 | void init_os_props(); |
| 186 | |
| 187 | public: |
| 188 | MetalFeatures features; |
| 189 | MetalLimits limits; |
| 190 | |
| 191 | // maj * 10000 + min * 100 + patch |
| 192 | uint32_t os_version; |
| 193 | |
| 194 | SampleCount find_nearest_supported_sample_count(RDD::TextureSamples p_samples) const; |
| 195 | |
| 196 | MetalDeviceProperties(MTL::Device *p_device); |
| 197 | ~MetalDeviceProperties(); |
| 198 | |
| 199 | private: |
| 200 | static const SampleCount sample_count[RDD::TextureSamples::TEXTURE_SAMPLES_MAX]; |
| 201 | }; |