Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/shader_rd.h
21506 views
1
/**************************************************************************/
2
/* shader_rd.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/string_builder.h"
35
#include "core/templates/hash_map.h"
36
#include "core/templates/local_vector.h"
37
#include "core/templates/rid_owner.h"
38
#include "core/templates/self_list.h"
39
#include "servers/rendering/rendering_server.h"
40
41
class ShaderRD {
42
public:
43
struct VariantDefine {
44
int group = 0;
45
CharString text;
46
bool default_enabled = true;
47
VariantDefine() {}
48
VariantDefine(int p_group, const String &p_text, bool p_default_enabled) {
49
group = p_group;
50
default_enabled = p_default_enabled;
51
text = p_text.utf8();
52
}
53
};
54
55
typedef Pair<ShaderRD *, RID> ShaderVersionPair;
56
typedef HashSet<ShaderVersionPair> ShaderVersionPairSet;
57
58
private:
59
//versions
60
CharString general_defines;
61
Vector<VariantDefine> variant_defines;
62
Vector<bool> variants_enabled;
63
Vector<uint32_t> variant_to_group;
64
HashMap<int, LocalVector<int>> group_to_variant_map;
65
Vector<bool> group_enabled;
66
67
Vector<RD::PipelineImmutableSampler> immutable_samplers;
68
Vector<uint64_t> dynamic_buffers;
69
70
struct Version {
71
Mutex *mutex = nullptr;
72
CharString uniforms;
73
CharString vertex_globals;
74
CharString compute_globals;
75
CharString fragment_globals;
76
CharString raygen_globals;
77
CharString any_hit_globals;
78
CharString closest_hit_globals;
79
CharString miss_globals;
80
CharString intersection_globals;
81
HashMap<StringName, CharString> code_sections;
82
Vector<CharString> custom_defines;
83
Vector<WorkerThreadPool::GroupID> group_compilation_tasks;
84
85
Vector<Vector<uint8_t>> variant_data;
86
Vector<RID> variants;
87
88
bool valid;
89
bool dirty;
90
bool initialize_needed;
91
bool embedded;
92
};
93
94
struct CompileData {
95
Version *version;
96
int group = 0;
97
};
98
99
// Vector will have the size of SHADER_STAGE_MAX and unused stages will have empty strings.
100
void _compile_variant(uint32_t p_variant, CompileData p_data);
101
102
void _initialize_version(Version *p_version);
103
void _clear_version(Version *p_version);
104
void _compile_version_start(Version *p_version, int p_group);
105
void _compile_version_end(Version *p_version, int p_group);
106
void _compile_ensure_finished(Version *p_version);
107
void _allocate_placeholders(Version *p_version, int p_group);
108
109
RID_Owner<Version, true> version_owner;
110
Mutex versions_mutex;
111
HashMap<RID, Mutex *> version_mutexes;
112
113
struct StageTemplate {
114
struct Chunk {
115
enum Type {
116
TYPE_VERSION_DEFINES,
117
TYPE_MATERIAL_UNIFORMS,
118
TYPE_VERTEX_GLOBALS,
119
TYPE_FRAGMENT_GLOBALS,
120
TYPE_COMPUTE_GLOBALS,
121
TYPE_RAYGEN_GLOBALS,
122
TYPE_ANY_HIT_GLOBALS,
123
TYPE_CLOSEST_HIT_GLOBALS,
124
TYPE_MISS_GLOBALS,
125
TYPE_INTERSECTION_GLOBALS,
126
TYPE_CODE,
127
TYPE_TEXT
128
};
129
130
Type type;
131
StringName code;
132
CharString text;
133
};
134
LocalVector<Chunk> chunks;
135
};
136
137
RD::PipelineType pipeline_type = RD::PIPELINE_TYPE_RASTERIZATION;
138
139
String name;
140
141
CharString base_compute_defines;
142
143
String base_sha256;
144
LocalVector<String> group_sha256;
145
146
static inline ShaderVersionPairSet shader_versions_embedded_set;
147
static inline Mutex shader_versions_embedded_set_mutex;
148
149
static String shader_cache_user_dir;
150
static String shader_cache_res_dir;
151
static bool shader_cache_cleanup_on_start;
152
static bool shader_cache_save_compressed;
153
static bool shader_cache_save_compressed_zstd;
154
static bool shader_cache_save_debug;
155
bool shader_cache_user_dir_valid = false;
156
bool shader_cache_res_dir_valid = false;
157
158
enum StageType {
159
STAGE_TYPE_VERTEX,
160
STAGE_TYPE_FRAGMENT,
161
STAGE_TYPE_COMPUTE,
162
STAGE_TYPE_RAYGEN,
163
STAGE_TYPE_ANY_HIT,
164
STAGE_TYPE_CLOSEST_HIT,
165
STAGE_TYPE_MISS,
166
STAGE_TYPE_INTERSECTION,
167
STAGE_TYPE_MAX,
168
};
169
170
StageTemplate stage_templates[STAGE_TYPE_MAX];
171
172
void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template);
173
Vector<String> _build_variant_stage_sources(uint32_t p_variant, CompileData p_data);
174
175
void _add_stage(const char *p_code, StageType p_stage_type);
176
177
String _version_get_sha1(Version *p_version) const;
178
String _get_cache_file_relative_path(Version *p_version, int p_group, const String &p_api_name);
179
String _get_cache_file_path(Version *p_version, int p_group, const String &p_api_name, bool p_user_dir);
180
bool _load_from_cache(Version *p_version, int p_group);
181
void _save_to_cache(Version *p_version, int p_group);
182
void _initialize_cache();
183
void _version_set(Version *p_version, const HashMap<String, String> &p_code, const Vector<String> &p_custom_defines);
184
185
protected:
186
ShaderRD();
187
void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name);
188
void setup_raytracing(const char *p_raygen_code, const char *p_any_hit_code, const char *p_closest_hit_code, const char *p_miss_code, const char *p_intersection_code, const char *p_name);
189
190
public:
191
RID version_create(bool p_embedded = true);
192
193
void version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines);
194
void version_set_compute_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_compute_globals, const Vector<String> &p_custom_defines);
195
void version_set_raytracing_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_raygen_globals, const String &p_any_hit_globals, const String &p_closest_hit_globals, const String &p_miss_globals, const String &p_intersection_globals, const Vector<String> &p_custom_defines);
196
197
_FORCE_INLINE_ RID version_get_shader(RID p_version, int p_variant) {
198
ERR_FAIL_INDEX_V(p_variant, variant_defines.size(), RID());
199
ERR_FAIL_COND_V(!variants_enabled[p_variant], RID());
200
201
Version *version = version_owner.get_or_null(p_version);
202
ERR_FAIL_NULL_V(version, RID());
203
204
MutexLock lock(*version->mutex);
205
206
if (version->dirty) {
207
_initialize_version(version);
208
for (int i = 0; i < group_enabled.size(); i++) {
209
if (!group_enabled[i]) {
210
_allocate_placeholders(version, i);
211
continue;
212
}
213
_compile_version_start(version, i);
214
}
215
}
216
217
uint32_t group = variant_to_group[p_variant];
218
if (version->group_compilation_tasks[group] != 0) {
219
_compile_version_end(version, group);
220
}
221
222
if (!version->valid) {
223
return RID();
224
}
225
226
return version->variants[p_variant];
227
}
228
229
bool version_is_valid(RID p_version);
230
231
bool version_free(RID p_version);
232
233
// Enable/disable variants for things that you know won't be used at engine initialization time .
234
void set_variant_enabled(int p_variant, bool p_enabled);
235
bool is_variant_enabled(int p_variant) const;
236
int64_t get_variant_count() const;
237
int get_variant_to_group(int p_variant) const;
238
239
// Enable/disable groups for things that might be enabled at run time.
240
void enable_group(int p_group);
241
bool is_group_enabled(int p_group) const;
242
int64_t get_group_count() const;
243
const LocalVector<int> &get_group_to_variants(int p_group) const;
244
245
const String &get_name() const;
246
247
const Vector<uint64_t> &get_dynamic_buffers() const;
248
249
static void shaders_embedded_set_lock();
250
static const ShaderVersionPairSet &shaders_embedded_set_get();
251
static void shaders_embedded_set_unlock();
252
253
static void set_shader_cache_user_dir(const String &p_dir);
254
static const String &get_shader_cache_user_dir();
255
static void set_shader_cache_res_dir(const String &p_dir);
256
static const String &get_shader_cache_res_dir();
257
static void set_shader_cache_save_compressed(bool p_enable);
258
static void set_shader_cache_save_compressed_zstd(bool p_enable);
259
static void set_shader_cache_save_debug(bool p_enable);
260
261
static Vector<RD::ShaderStageSPIRVData> compile_stages(const Vector<String> &p_stage_sources, const Vector<uint64_t> &p_dynamic_buffers);
262
static PackedByteArray save_shader_cache_bytes(const LocalVector<int> &p_variants, const Vector<Vector<uint8_t>> &p_variant_data);
263
264
Vector<String> version_build_variant_stage_sources(RID p_version, int p_variant);
265
RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version);
266
String version_get_cache_file_relative_path(RID p_version, int p_group, const String &p_api_name);
267
268
struct DynamicBuffer {
269
static uint64_t encode(uint32_t p_set_id, uint32_t p_binding) {
270
return uint64_t(p_set_id) << 32ul | uint64_t(p_binding);
271
}
272
};
273
274
// Dynamic Buffers specifies Which buffers will be persistent/dynamic when used.
275
// See DynamicBuffer::encode. We need this argument because SPIR-V does not distinguish between a
276
// uniform buffer and a dynamic uniform buffer. At shader level they're the same thing, but the PSO
277
// is created slightly differently and they're bound differently.
278
// On D3D12 the Root Layout is also different.
279
void initialize(const Vector<String> &p_variant_defines, const String &p_general_defines = "", const Vector<RD::PipelineImmutableSampler> &p_immutable_samplers = Vector<RD::PipelineImmutableSampler>(), const Vector<uint64_t> &p_dynamic_buffers = Vector<uint64_t>());
280
void initialize(const Vector<VariantDefine> &p_variant_defines, const String &p_general_defines = "", const Vector<RD::PipelineImmutableSampler> &p_immutable_samplers = Vector<RD::PipelineImmutableSampler>(), const Vector<uint64_t> &p_dynamic_buffers = Vector<uint64_t>());
281
282
virtual ~ShaderRD();
283
};
284
285