| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
HashMap<StringName, CharString> code_sections; |
| 77 |
Vector<CharString> custom_defines; |
| 78 |
Vector<WorkerThreadPool::GroupID> group_compilation_tasks; |
| 79 |
|
| 80 |
Vector<Vector<uint8_t>> variant_data; |
| 81 |
Vector<RID> variants; |
| 82 |
|
| 83 |
bool valid; |
| 84 |
bool dirty; |
| 85 |
bool initialize_needed; |
| 86 |
bool embedded; |
| 87 |
}; |
| 88 |
|
| 89 |
struct CompileData { |
| 90 |
Version *version; |
| 91 |
int group = 0; |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
void _compile_variant(uint32_t p_variant, CompileData p_data); |
| 96 |
|
| 97 |
void _initialize_version(Version *p_version); |
| 98 |
void _clear_version(Version *p_version); |
| 99 |
void _compile_version_start(Version *p_version, int p_group); |
| 100 |
void _compile_version_end(Version *p_version, int p_group); |
| 101 |
void _compile_ensure_finished(Version *p_version); |
| 102 |
void _allocate_placeholders(Version *p_version, int p_group); |
| 103 |
|
| 104 |
RID_Owner<Version, true> version_owner; |
| 105 |
Mutex versions_mutex; |
| 106 |
HashMap<RID, Mutex *> version_mutexes; |
| 107 |
|
| 108 |
struct StageTemplate { |
| 109 |
struct Chunk { |
| 110 |
enum Type { |
| 111 |
TYPE_VERSION_DEFINES, |
| 112 |
TYPE_MATERIAL_UNIFORMS, |
| 113 |
TYPE_VERTEX_GLOBALS, |
| 114 |
TYPE_FRAGMENT_GLOBALS, |
| 115 |
TYPE_COMPUTE_GLOBALS, |
| 116 |
TYPE_CODE, |
| 117 |
TYPE_TEXT |
| 118 |
}; |
| 119 |
|
| 120 |
Type type; |
| 121 |
StringName code; |
| 122 |
CharString text; |
| 123 |
}; |
| 124 |
LocalVector<Chunk> chunks; |
| 125 |
}; |
| 126 |
|
| 127 |
bool is_compute = false; |
| 128 |
|
| 129 |
String name; |
| 130 |
|
| 131 |
CharString base_compute_defines; |
| 132 |
|
| 133 |
String base_sha256; |
| 134 |
LocalVector<String> group_sha256; |
| 135 |
|
| 136 |
static inline ShaderVersionPairSet shader_versions_embedded_set; |
| 137 |
static inline Mutex shader_versions_embedded_set_mutex; |
| 138 |
|
| 139 |
static String shader_cache_user_dir; |
| 140 |
static String shader_cache_res_dir; |
| 141 |
static bool shader_cache_cleanup_on_start; |
| 142 |
static bool shader_cache_save_compressed; |
| 143 |
static bool shader_cache_save_compressed_zstd; |
| 144 |
static bool shader_cache_save_debug; |
| 145 |
bool shader_cache_user_dir_valid = false; |
| 146 |
bool shader_cache_res_dir_valid = false; |
| 147 |
|
| 148 |
enum StageType { |
| 149 |
STAGE_TYPE_VERTEX, |
| 150 |
STAGE_TYPE_FRAGMENT, |
| 151 |
STAGE_TYPE_COMPUTE, |
| 152 |
STAGE_TYPE_MAX, |
| 153 |
}; |
| 154 |
|
| 155 |
StageTemplate stage_templates[STAGE_TYPE_MAX]; |
| 156 |
|
| 157 |
void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template); |
| 158 |
Vector<String> _build_variant_stage_sources(uint32_t p_variant, CompileData p_data); |
| 159 |
|
| 160 |
void _add_stage(const char *p_code, StageType p_stage_type); |
| 161 |
|
| 162 |
String _version_get_sha1(Version *p_version) const; |
| 163 |
String _get_cache_file_relative_path(Version *p_version, int p_group, const String &p_api_name); |
| 164 |
String _get_cache_file_path(Version *p_version, int p_group, const String &p_api_name, bool p_user_dir); |
| 165 |
bool _load_from_cache(Version *p_version, int p_group); |
| 166 |
void _save_to_cache(Version *p_version, int p_group); |
| 167 |
void _initialize_cache(); |
| 168 |
|
| 169 |
protected: |
| 170 |
ShaderRD(); |
| 171 |
void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name); |
| 172 |
|
| 173 |
public: |
| 174 |
RID version_create(bool p_embedded = true); |
| 175 |
|
| 176 |
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); |
| 177 |
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); |
| 178 |
|
| 179 |
_FORCE_INLINE_ RID version_get_shader(RID p_version, int p_variant) { |
| 180 |
ERR_FAIL_INDEX_V(p_variant, variant_defines.size(), RID()); |
| 181 |
ERR_FAIL_COND_V(!variants_enabled[p_variant], RID()); |
| 182 |
|
| 183 |
Version *version = version_owner.get_or_null(p_version); |
| 184 |
ERR_FAIL_NULL_V(version, RID()); |
| 185 |
|
| 186 |
MutexLock lock(*version->mutex); |
| 187 |
|
| 188 |
if (version->dirty) { |
| 189 |
_initialize_version(version); |
| 190 |
for (int i = 0; i < group_enabled.size(); i++) { |
| 191 |
if (!group_enabled[i]) { |
| 192 |
_allocate_placeholders(version, i); |
| 193 |
continue; |
| 194 |
} |
| 195 |
_compile_version_start(version, i); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
uint32_t group = variant_to_group[p_variant]; |
| 200 |
if (version->group_compilation_tasks[group] != 0) { |
| 201 |
_compile_version_end(version, group); |
| 202 |
} |
| 203 |
|
| 204 |
if (!version->valid) { |
| 205 |
return RID(); |
| 206 |
} |
| 207 |
|
| 208 |
return version->variants[p_variant]; |
| 209 |
} |
| 210 |
|
| 211 |
bool version_is_valid(RID p_version); |
| 212 |
|
| 213 |
bool version_free(RID p_version); |
| 214 |
|
| 215 |
|
| 216 |
void set_variant_enabled(int p_variant, bool p_enabled); |
| 217 |
bool is_variant_enabled(int p_variant) const; |
| 218 |
int64_t get_variant_count() const; |
| 219 |
int get_variant_to_group(int p_variant) const; |
| 220 |
|
| 221 |
|
| 222 |
void enable_group(int p_group); |
| 223 |
bool is_group_enabled(int p_group) const; |
| 224 |
int64_t get_group_count() const; |
| 225 |
const LocalVector<int> &get_group_to_variants(int p_group) const; |
| 226 |
|
| 227 |
const String &get_name() const; |
| 228 |
|
| 229 |
const Vector<uint64_t> &get_dynamic_buffers() const; |
| 230 |
|
| 231 |
static void shaders_embedded_set_lock(); |
| 232 |
static const ShaderVersionPairSet &shaders_embedded_set_get(); |
| 233 |
static void shaders_embedded_set_unlock(); |
| 234 |
|
| 235 |
static void set_shader_cache_user_dir(const String &p_dir); |
| 236 |
static const String &get_shader_cache_user_dir(); |
| 237 |
static void set_shader_cache_res_dir(const String &p_dir); |
| 238 |
static const String &get_shader_cache_res_dir(); |
| 239 |
static void set_shader_cache_save_compressed(bool p_enable); |
| 240 |
static void set_shader_cache_save_compressed_zstd(bool p_enable); |
| 241 |
static void set_shader_cache_save_debug(bool p_enable); |
| 242 |
|
| 243 |
static Vector<RD::ShaderStageSPIRVData> compile_stages(const Vector<String> &p_stage_sources, const Vector<uint64_t> &p_dynamic_buffers); |
| 244 |
static PackedByteArray save_shader_cache_bytes(const LocalVector<int> &p_variants, const Vector<Vector<uint8_t>> &p_variant_data); |
| 245 |
|
| 246 |
Vector<String> version_build_variant_stage_sources(RID p_version, int p_variant); |
| 247 |
RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version); |
| 248 |
String version_get_cache_file_relative_path(RID p_version, int p_group, const String &p_api_name); |
| 249 |
|
| 250 |
struct DynamicBuffer { |
| 251 |
static uint64_t encode(uint32_t p_set_id, uint32_t p_binding) { |
| 252 |
return uint64_t(p_set_id) << 32ul | uint64_t(p_binding); |
| 253 |
} |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
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>()); |
| 262 |
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>()); |
| 263 |
|
| 264 |
virtual ~ShaderRD(); |
| 265 |
}; |