Skip to content
0.2.0-dev Download ZIP
170 lines ·5.8 KB Raw
1/**************************************************************************/
2/* metal_device_profile.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#include "core/os/mutex.h"
34#include "core/string/ustring.h"
35#include "core/templates/hash_map.h"
36#include "core/typedefs.h"
37
38class MinOsVersion {
39 uint32_t version;
40
41public:
42 String to_compiler_os_version() const;
43 bool is_null() const { return version == UINT32_MAX; }
44 bool is_valid() const { return version != UINT32_MAX; }
45
46 MinOsVersion(const String &p_version);
47 constexpr explicit MinOsVersion(uint32_t p_version) :
48 version(p_version) {}
49 constexpr MinOsVersion(uint32_t p_major, uint32_t p_minor, uint32_t p_patch = 0) :
50 version(p_major * 10000 + p_minor * 100 + p_patch) {}
51 constexpr MinOsVersion() :
52 version(UINT32_MAX) {}
53
54 bool operator>(uint32_t p_other) {
55 return version > p_other;
56 }
57 constexpr operator uint32_t() const { return version; }
58};
59
60namespace os_version {
61
62constexpr MinOsVersion MACOS_26_0(26'00'00);
63constexpr MinOsVersion MACOS_15_0(15'00'00);
64constexpr MinOsVersion MACOS_14_0(14'00'00);
65constexpr MinOsVersion MACOS_13_0(13'00'00);
66constexpr MinOsVersion MACOS_12_0(12'00'00);
67constexpr MinOsVersion MACOS_11_0(11'00'00);
68
69constexpr MinOsVersion IOS_26_0(26'00'00);
70constexpr MinOsVersion IOS_18_0(18'00'00);
71constexpr MinOsVersion IOS_17_0(17'00'00);
72constexpr MinOsVersion IOS_16_0(16'00'00);
73constexpr MinOsVersion IOS_15_0(15'00'00);
74
75constexpr MinOsVersion VISIONOS_26_0(26'00'00);
76constexpr MinOsVersion VISIONOS_02_4(02'04'00);
77
78} //namespace os_version
79
80/// @brief A minimal structure that defines a device profile for Metal.
81///
82/// This structure is used by the `RenderingShaderContainerMetal` class to
83/// determine options for compiling SPIR-V to Metal source. It currently only
84/// contains the minimum properties required to transform shaders from SPIR-V to Metal
85/// and potentially compile to a `.metallib`.
86struct MetalDeviceProfile {
87 enum class Platform : uint32_t {
88 macOS = 0,
89 iOS = 1,
90 visionOS = 2,
91 };
92
93 /*! @brief The GPU family.
94 *
95 * NOTE: These values match Apple's MTLGPUFamily
96 */
97 enum class GPU : uint32_t {
98 Apple1 = 1001,
99 Apple2 = 1002,
100 Apple3 = 1003,
101 Apple4 = 1004,
102 Apple5 = 1005,
103 Apple6 = 1006,
104 Apple7 = 1007,
105 Apple8 = 1008,
106 Apple9 = 1009,
107 };
108
109 enum class ArgumentBuffersTier : uint32_t {
110 Tier1 = 0,
111 Tier2 = 1,
112 };
113
114 struct Features {
115 uint32_t msl_version = 0;
116 bool use_argument_buffers = false;
117 bool simdPermute = false;
118 };
119
120 Platform platform = Platform::macOS;
121 GPU gpu = GPU::Apple4;
122 MinOsVersion min_os_version;
123 Features features;
124
125 struct MinimumRequirements {
126 GPU gpu = GPU::Apple1;
127 uint32_t msl_version = 0;
128
129 bool operator>(const MinimumRequirements &p_other) const {
130 return gpu > p_other.gpu || msl_version > p_other.msl_version;
131 }
132 };
133
134 MinimumRequirements get_minimum_requirements() const {
135 return { gpu, features.msl_version };
136 }
137
138 static const MetalDeviceProfile *get_profile(Platform p_platform, GPU p_gpu, MinOsVersion p_min_os_version);
139
140 MetalDeviceProfile() = default;
141
142private:
143 static Mutex profiles_lock; ///< Mutex to protect access to the profiles map.
144
145 struct ProfileKey {
146 friend struct HashMapHasherDefaultImpl<ProfileKey>;
147 union {
148 struct {
149 uint32_t min_os_version;
150 uint16_t platform;
151 uint16_t gpu;
152 };
153 uint64_t value = 0;
154 };
155
156 ProfileKey() = default;
157 ProfileKey(MinOsVersion p_min_os_version, Platform p_platform, GPU p_gpu) :
158 min_os_version(p_min_os_version), platform((uint16_t)p_platform), gpu((uint16_t)p_gpu) {}
159
160 _FORCE_INLINE_ uint32_t hash() const {
161 return hash_one_uint64(value);
162 }
163
164 bool operator==(const ProfileKey &p_other) const {
165 return value == p_other.value;
166 }
167 };
168
169 static HashMap<ProfileKey, MetalDeviceProfile> profiles;
170};