Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/rendering_shader_container_metal.h
10277 views
1
/**************************************************************************/
2
/* rendering_shader_container_metal.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
#import "sha256_digest.h"
34
35
#import "servers/rendering/rendering_device_driver.h"
36
#import "servers/rendering/rendering_shader_container.h"
37
38
constexpr uint32_t R32UI_ALIGNMENT_CONSTANT_ID = 65535;
39
/// Metal buffer index for the view mask when rendering multi-view.
40
const uint32_t VIEW_MASK_BUFFER_INDEX = 24;
41
42
class RenderingShaderContainerFormatMetal;
43
44
class MinOsVersion {
45
uint32_t version;
46
47
public:
48
String to_compiler_os_version() const;
49
bool is_null() const { return version == UINT32_MAX; }
50
bool is_valid() const { return version != UINT32_MAX; }
51
52
MinOsVersion(const String &p_version);
53
explicit MinOsVersion(uint32_t p_version) :
54
version(p_version) {}
55
MinOsVersion() :
56
version(UINT32_MAX) {}
57
58
bool operator>(uint32_t p_other) {
59
return version > p_other;
60
}
61
};
62
63
/// @brief A minimal structure that defines a device profile for Metal.
64
///
65
/// This structure is used by the `RenderingShaderContainerMetal` class to
66
/// determine options for compiling SPIR-V to Metal source. It currently only
67
/// contains the minimum properties required to transform shaders from SPIR-V to Metal
68
/// and potentially compile to a `.metallib`.
69
struct MetalDeviceProfile {
70
enum class Platform : uint32_t {
71
macOS = 0,
72
iOS = 1,
73
};
74
75
/*! @brief The GPU family.
76
*
77
* NOTE: These values match Apple's MTLGPUFamily
78
*/
79
enum class GPU : uint32_t {
80
Apple1 = 1001,
81
Apple2 = 1002,
82
Apple3 = 1003,
83
Apple4 = 1004,
84
Apple5 = 1005,
85
Apple6 = 1006,
86
Apple7 = 1007,
87
Apple8 = 1008,
88
Apple9 = 1009,
89
};
90
91
enum class ArgumentBuffersTier : uint32_t {
92
Tier1 = 0,
93
Tier2 = 1,
94
};
95
96
struct Features {
97
uint32_t mslVersionMajor = 0;
98
uint32_t mslVersionMinor = 0;
99
ArgumentBuffersTier argument_buffers_tier = ArgumentBuffersTier::Tier1;
100
bool simdPermute = false;
101
};
102
103
Platform platform = Platform::macOS;
104
GPU gpu = GPU::Apple4;
105
Features features;
106
107
static const MetalDeviceProfile *get_profile(Platform p_platform, GPU p_gpu);
108
109
MetalDeviceProfile() = default;
110
111
private:
112
static Mutex profiles_lock; ///< Mutex to protect access to the profiles map.
113
static HashMap<uint32_t, MetalDeviceProfile> profiles;
114
};
115
116
class RenderingShaderContainerMetal : public RenderingShaderContainer {
117
GDSOFTCLASS(RenderingShaderContainerMetal, RenderingShaderContainer);
118
119
public:
120
struct HeaderData {
121
enum Flags : uint32_t {
122
NONE = 0,
123
NEEDS_VIEW_MASK_BUFFER = 1 << 0,
124
USES_ARGUMENT_BUFFERS = 1 << 1,
125
};
126
127
/// The base profile that was used to generate this shader.
128
MetalDeviceProfile profile;
129
130
/// The Metal language version specified when compiling SPIR-V to MSL.
131
/// Format is major * 10000 + minor * 100 + patch.
132
uint32_t msl_version = UINT32_MAX;
133
/*! @brief The minimum supported OS version for shaders baked to a `.metallib`.
134
*
135
* NOTE: This property is only valid when shaders are baked to a .metalllib
136
*
137
* Format is major * 10000 + minor * 100 + patch.
138
*/
139
MinOsVersion os_min_version;
140
uint32_t flags = NONE;
141
142
/// @brief Returns `true` if the shader is compiled with multi-view support.
143
bool needs_view_mask_buffer() const {
144
return flags & NEEDS_VIEW_MASK_BUFFER;
145
}
146
147
void set_needs_view_mask_buffer(bool p_value) {
148
if (p_value) {
149
flags |= NEEDS_VIEW_MASK_BUFFER;
150
} else {
151
flags &= ~NEEDS_VIEW_MASK_BUFFER;
152
}
153
}
154
155
/// @brief Returns `true` if the shader was compiled with argument buffer support.
156
bool uses_argument_buffers() const {
157
return flags & USES_ARGUMENT_BUFFERS;
158
}
159
160
void set_uses_argument_buffers(bool p_value) {
161
if (p_value) {
162
flags |= USES_ARGUMENT_BUFFERS;
163
} else {
164
flags &= ~USES_ARGUMENT_BUFFERS;
165
}
166
}
167
};
168
169
struct StageData {
170
uint32_t vertex_input_binding_mask = 0;
171
uint32_t is_position_invariant = 0; ///< <c>true</c> if the position output is invariant
172
uint32_t supports_fast_math = 0;
173
SHA256Digest hash; ///< SHA 256 hash of the shader code
174
uint32_t source_size = 0; ///< size of the source code in the returned bytes
175
uint32_t library_size = 0; ///< size of the compiled library in the returned bytes, 0 if it is not compiled
176
uint32_t push_constant_binding = UINT32_MAX; ///< Metal binding slot for the push constant data
177
};
178
179
struct BindingInfoData {
180
uint32_t shader_stage = UINT32_MAX; ///< The shader stage this binding is used in, or UINT32_MAX if not used.
181
uint32_t data_type = 0; // MTLDataTypeNone
182
uint32_t index = 0;
183
uint32_t access = 0; // MTLBindingAccessReadOnly
184
uint32_t usage = 0; // MTLResourceUsage (none)
185
uint32_t texture_type = 2; // MTLTextureType2D
186
uint32_t image_format = 0;
187
uint32_t array_length = 0;
188
uint32_t is_multisampled = 0;
189
};
190
191
struct UniformData {
192
/// Specifies the index into the `bindings` array for the shader stage.
193
///
194
/// For example, a vertex and fragment shader use slots 0 and 1 of the bindings and bindings_secondary arrays.
195
static constexpr uint32_t STAGE_INDEX[RenderingDeviceCommons::SHADER_STAGE_MAX] = {
196
0, // SHADER_STAGE_VERTEX
197
1, // SHADER_STAGE_FRAGMENT
198
0, // SHADER_STAGE_TESSELATION_CONTROL
199
1, // SHADER_STAGE_TESSELATION_EVALUATION
200
0, // SHADER_STAGE_COMPUTE
201
};
202
203
/// Specifies the stages the uniform data is
204
/// used by the Metal shader.
205
uint32_t active_stages = 0;
206
/// The primary binding information for the uniform data.
207
///
208
/// A maximum of two stages is expected for any given pipeline, such as a vertex and fragment, so
209
/// the array size is fixed to 2.
210
BindingInfoData bindings[2];
211
/// The secondary binding information for the uniform data.
212
///
213
/// This is typically a sampler for an image-sampler uniform
214
BindingInfoData bindings_secondary[2];
215
216
_FORCE_INLINE_ constexpr uint32_t get_index_for_stage(RenderingDeviceCommons::ShaderStage p_stage) const {
217
return STAGE_INDEX[p_stage];
218
}
219
220
_FORCE_INLINE_ BindingInfoData &get_binding_for_stage(RenderingDeviceCommons::ShaderStage p_stage) {
221
BindingInfoData &info = bindings[get_index_for_stage(p_stage)];
222
DEV_ASSERT(info.shader_stage == UINT32_MAX || info.shader_stage == p_stage); // make sure this uniform isn't used in the other stage
223
info.shader_stage = p_stage;
224
return info;
225
}
226
227
_FORCE_INLINE_ BindingInfoData &get_secondary_binding_for_stage(RenderingDeviceCommons::ShaderStage p_stage) {
228
BindingInfoData &info = bindings_secondary[get_index_for_stage(p_stage)];
229
DEV_ASSERT(info.shader_stage == UINT32_MAX || info.shader_stage == p_stage); // make sure this uniform isn't used in the other stage
230
info.shader_stage = p_stage;
231
return info;
232
}
233
};
234
235
struct SpecializationData {
236
uint32_t used_stages = 0;
237
};
238
239
HeaderData mtl_reflection_data; // compliment to reflection_data
240
Vector<StageData> mtl_shaders; // compliment to shaders
241
242
private:
243
struct ToolchainProperties {
244
MinOsVersion os_version_min_required;
245
uint32_t metal_version = UINT32_MAX;
246
247
_FORCE_INLINE_ bool is_null() const { return os_version_min_required.is_null() || metal_version == UINT32_MAX; }
248
_FORCE_INLINE_ bool is_valid() const { return !is_null(); }
249
};
250
251
ToolchainProperties compiler_props;
252
253
void _initialize_toolchain_properties();
254
255
private:
256
const MetalDeviceProfile *device_profile = nullptr;
257
bool export_mode = false;
258
MinOsVersion min_os_version;
259
260
Vector<UniformData> mtl_reflection_binding_set_uniforms_data; // compliment to reflection_binding_set_uniforms_data
261
Vector<SpecializationData> mtl_reflection_specialization_data; // compliment to reflection_specialization_data
262
263
Error compile_metal_source(const char *p_source, const StageData &p_stage_data, Vector<uint8_t> &r_binary_data);
264
265
public:
266
static constexpr uint32_t FORMAT_VERSION = 1;
267
268
void set_export_mode(bool p_export_mode) { export_mode = p_export_mode; }
269
void set_device_profile(const MetalDeviceProfile *p_device_profile) { device_profile = p_device_profile; }
270
void set_min_os_version(const MinOsVersion p_min_os_version) { min_os_version = p_min_os_version; }
271
272
struct MetalShaderReflection {
273
Vector<Vector<UniformData>> uniform_sets;
274
Vector<SpecializationData> specialization_constants;
275
};
276
277
MetalShaderReflection get_metal_shader_reflection() const;
278
279
protected:
280
virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes) override;
281
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) override;
282
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
283
virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) override;
284
virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
285
virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes) override;
286
virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index) override;
287
288
virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const override;
289
virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
290
virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
291
virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
292
293
virtual uint32_t _format() const override;
294
virtual uint32_t _format_version() const override;
295
virtual bool _set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) override;
296
};
297
298
class RenderingShaderContainerFormatMetal : public RenderingShaderContainerFormat {
299
bool export_mode = false;
300
MinOsVersion min_os_version;
301
302
const MetalDeviceProfile *device_profile = nullptr;
303
304
public:
305
virtual Ref<RenderingShaderContainer> create_container() const override;
306
virtual ShaderLanguageVersion get_shader_language_version() const override;
307
virtual ShaderSpirvVersion get_shader_spirv_version() const override;
308
RenderingShaderContainerFormatMetal(const MetalDeviceProfile *p_device_profile, bool p_export = false, const MinOsVersion p_min_os_version = MinOsVersion());
309
virtual ~RenderingShaderContainerFormatMetal() = default;
310
};
311
312