Path: blob/master/servers/rendering/renderer_rd/shader_rd.h
22517 views
/**************************************************************************/1/* shader_rd.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/os/mutex.h"33#include "core/string/string_builder.h"34#include "core/templates/hash_map.h"35#include "core/templates/local_vector.h"36#include "core/templates/rid_owner.h"37#include "core/templates/self_list.h"38#include "servers/rendering/rendering_device.h"39#include "servers/rendering/rendering_server.h"4041class ShaderRD {42public:43struct VariantDefine {44int group = 0;45CharString text;46bool default_enabled = true;47VariantDefine() {}48VariantDefine(int p_group, const String &p_text, bool p_default_enabled) {49group = p_group;50default_enabled = p_default_enabled;51text = p_text.utf8();52}53};5455typedef Pair<ShaderRD *, RID> ShaderVersionPair;56typedef HashSet<ShaderVersionPair> ShaderVersionPairSet;5758private:59//versions60CharString general_defines;61Vector<VariantDefine> variant_defines;62Vector<bool> variants_enabled;63Vector<uint32_t> variant_to_group;64HashMap<int, LocalVector<int>> group_to_variant_map;65Vector<bool> group_enabled;6667Vector<RD::PipelineImmutableSampler> immutable_samplers;68Vector<uint64_t> dynamic_buffers;6970struct Version {71Mutex *mutex = nullptr;72CharString uniforms;73CharString vertex_globals;74CharString compute_globals;75CharString fragment_globals;76CharString raygen_globals;77CharString any_hit_globals;78CharString closest_hit_globals;79CharString miss_globals;80CharString intersection_globals;81HashMap<StringName, CharString> code_sections;82Vector<CharString> custom_defines;83Vector<WorkerThreadPool::GroupID> group_compilation_tasks;8485Vector<Vector<uint8_t>> variant_data;86Vector<RID> variants;8788bool valid;89bool dirty;90bool initialize_needed;91bool embedded;92};9394struct CompileData {95Version *version;96int group = 0;97};9899// Vector will have the size of SHADER_STAGE_MAX and unused stages will have empty strings.100void _compile_variant(uint32_t p_variant, CompileData p_data);101102void _initialize_version(Version *p_version);103void _clear_version(Version *p_version);104void _compile_version_start(Version *p_version, int p_group);105void _compile_version_end(Version *p_version, int p_group);106void _compile_ensure_finished(Version *p_version);107void _allocate_placeholders(Version *p_version, int p_group);108109RID_Owner<Version, true> version_owner;110Mutex versions_mutex;111HashMap<RID, Mutex *> version_mutexes;112113struct StageTemplate {114struct Chunk {115enum Type {116TYPE_VERSION_DEFINES,117TYPE_MATERIAL_UNIFORMS,118TYPE_VERTEX_GLOBALS,119TYPE_FRAGMENT_GLOBALS,120TYPE_COMPUTE_GLOBALS,121TYPE_RAYGEN_GLOBALS,122TYPE_ANY_HIT_GLOBALS,123TYPE_CLOSEST_HIT_GLOBALS,124TYPE_MISS_GLOBALS,125TYPE_INTERSECTION_GLOBALS,126TYPE_CODE,127TYPE_TEXT128};129130Type type;131StringName code;132CharString text;133};134LocalVector<Chunk> chunks;135};136137RD::PipelineType pipeline_type = RD::PIPELINE_TYPE_RASTERIZATION;138139String name;140141CharString base_compute_defines;142143String base_sha256;144LocalVector<String> group_sha256;145146static inline ShaderVersionPairSet shader_versions_embedded_set;147static inline Mutex shader_versions_embedded_set_mutex;148149static String shader_cache_user_dir;150static String shader_cache_res_dir;151static bool shader_cache_cleanup_on_start;152static bool shader_cache_save_compressed;153static bool shader_cache_save_compressed_zstd;154static bool shader_cache_save_debug;155bool shader_cache_user_dir_valid = false;156bool shader_cache_res_dir_valid = false;157158enum StageType {159STAGE_TYPE_VERTEX,160STAGE_TYPE_FRAGMENT,161STAGE_TYPE_COMPUTE,162STAGE_TYPE_RAYGEN,163STAGE_TYPE_ANY_HIT,164STAGE_TYPE_CLOSEST_HIT,165STAGE_TYPE_MISS,166STAGE_TYPE_INTERSECTION,167STAGE_TYPE_MAX,168};169170StageTemplate stage_templates[STAGE_TYPE_MAX];171172void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template);173Vector<String> _build_variant_stage_sources(uint32_t p_variant, CompileData p_data);174175void _add_stage(const char *p_code, StageType p_stage_type);176177String _version_get_sha1(Version *p_version) const;178String _get_cache_file_relative_path(Version *p_version, int p_group, const String &p_api_name);179String _get_cache_file_path(Version *p_version, int p_group, const String &p_api_name, bool p_user_dir);180bool _load_from_cache(Version *p_version, int p_group);181void _save_to_cache(Version *p_version, int p_group);182void _initialize_cache();183void _version_set(Version *p_version, const HashMap<String, String> &p_code, const Vector<String> &p_custom_defines);184185protected:186ShaderRD();187void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name);188void 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);189190public:191RID version_create(bool p_embedded = true);192193void 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);194void 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);195void 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);196197_FORCE_INLINE_ RID version_get_shader(RID p_version, int p_variant) {198ERR_FAIL_INDEX_V(p_variant, variant_defines.size(), RID());199ERR_FAIL_COND_V(!variants_enabled[p_variant], RID());200201Version *version = version_owner.get_or_null(p_version);202ERR_FAIL_NULL_V(version, RID());203204MutexLock lock(*version->mutex);205206if (version->dirty) {207_initialize_version(version);208for (int i = 0; i < group_enabled.size(); i++) {209if (!group_enabled[i]) {210_allocate_placeholders(version, i);211continue;212}213_compile_version_start(version, i);214}215}216217uint32_t group = variant_to_group[p_variant];218if (version->group_compilation_tasks[group] != 0) {219_compile_version_end(version, group);220}221222if (!version->valid) {223return RID();224}225226return version->variants[p_variant];227}228229bool version_is_valid(RID p_version);230231bool version_free(RID p_version);232233// Enable/disable variants for things that you know won't be used at engine initialization time .234void set_variant_enabled(int p_variant, bool p_enabled);235bool is_variant_enabled(int p_variant) const;236int64_t get_variant_count() const;237int get_variant_to_group(int p_variant) const;238239// Enable/disable groups for things that might be enabled at run time.240void enable_group(int p_group);241bool is_group_enabled(int p_group) const;242int64_t get_group_count() const;243const LocalVector<int> &get_group_to_variants(int p_group) const;244245const String &get_name() const;246247const Vector<uint64_t> &get_dynamic_buffers() const;248249static void shaders_embedded_set_lock();250static const ShaderVersionPairSet &shaders_embedded_set_get();251static void shaders_embedded_set_unlock();252253static void set_shader_cache_user_dir(const String &p_dir);254static const String &get_shader_cache_user_dir();255static void set_shader_cache_res_dir(const String &p_dir);256static const String &get_shader_cache_res_dir();257static void set_shader_cache_save_compressed(bool p_enable);258static void set_shader_cache_save_compressed_zstd(bool p_enable);259static void set_shader_cache_save_debug(bool p_enable);260261static Vector<RD::ShaderStageSPIRVData> compile_stages(const Vector<String> &p_stage_sources, const Vector<uint64_t> &p_dynamic_buffers);262static PackedByteArray save_shader_cache_bytes(const LocalVector<int> &p_variants, const Vector<Vector<uint8_t>> &p_variant_data);263264Vector<String> version_build_variant_stage_sources(RID p_version, int p_variant);265RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version);266String version_get_cache_file_relative_path(RID p_version, int p_group, const String &p_api_name);267268struct DynamicBuffer {269static uint64_t encode(uint32_t p_set_id, uint32_t p_binding) {270return uint64_t(p_set_id) << 32ul | uint64_t(p_binding);271}272};273274// 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 a276// uniform buffer and a dynamic uniform buffer. At shader level they're the same thing, but the PSO277// is created slightly differently and they're bound differently.278// On D3D12 the Root Layout is also different.279void 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>());280void 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>());281282virtual ~ShaderRD();283};284285286