Path: blob/master/servers/rendering/renderer_rd/shader_rd.h
21506 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_server.h"3940class ShaderRD {41public:42struct VariantDefine {43int group = 0;44CharString text;45bool default_enabled = true;46VariantDefine() {}47VariantDefine(int p_group, const String &p_text, bool p_default_enabled) {48group = p_group;49default_enabled = p_default_enabled;50text = p_text.utf8();51}52};5354typedef Pair<ShaderRD *, RID> ShaderVersionPair;55typedef HashSet<ShaderVersionPair> ShaderVersionPairSet;5657private:58//versions59CharString general_defines;60Vector<VariantDefine> variant_defines;61Vector<bool> variants_enabled;62Vector<uint32_t> variant_to_group;63HashMap<int, LocalVector<int>> group_to_variant_map;64Vector<bool> group_enabled;6566Vector<RD::PipelineImmutableSampler> immutable_samplers;67Vector<uint64_t> dynamic_buffers;6869struct Version {70Mutex *mutex = nullptr;71CharString uniforms;72CharString vertex_globals;73CharString compute_globals;74CharString fragment_globals;75CharString raygen_globals;76CharString any_hit_globals;77CharString closest_hit_globals;78CharString miss_globals;79CharString intersection_globals;80HashMap<StringName, CharString> code_sections;81Vector<CharString> custom_defines;82Vector<WorkerThreadPool::GroupID> group_compilation_tasks;8384Vector<Vector<uint8_t>> variant_data;85Vector<RID> variants;8687bool valid;88bool dirty;89bool initialize_needed;90bool embedded;91};9293struct CompileData {94Version *version;95int group = 0;96};9798// Vector will have the size of SHADER_STAGE_MAX and unused stages will have empty strings.99void _compile_variant(uint32_t p_variant, CompileData p_data);100101void _initialize_version(Version *p_version);102void _clear_version(Version *p_version);103void _compile_version_start(Version *p_version, int p_group);104void _compile_version_end(Version *p_version, int p_group);105void _compile_ensure_finished(Version *p_version);106void _allocate_placeholders(Version *p_version, int p_group);107108RID_Owner<Version, true> version_owner;109Mutex versions_mutex;110HashMap<RID, Mutex *> version_mutexes;111112struct StageTemplate {113struct Chunk {114enum Type {115TYPE_VERSION_DEFINES,116TYPE_MATERIAL_UNIFORMS,117TYPE_VERTEX_GLOBALS,118TYPE_FRAGMENT_GLOBALS,119TYPE_COMPUTE_GLOBALS,120TYPE_RAYGEN_GLOBALS,121TYPE_ANY_HIT_GLOBALS,122TYPE_CLOSEST_HIT_GLOBALS,123TYPE_MISS_GLOBALS,124TYPE_INTERSECTION_GLOBALS,125TYPE_CODE,126TYPE_TEXT127};128129Type type;130StringName code;131CharString text;132};133LocalVector<Chunk> chunks;134};135136RD::PipelineType pipeline_type = RD::PIPELINE_TYPE_RASTERIZATION;137138String name;139140CharString base_compute_defines;141142String base_sha256;143LocalVector<String> group_sha256;144145static inline ShaderVersionPairSet shader_versions_embedded_set;146static inline Mutex shader_versions_embedded_set_mutex;147148static String shader_cache_user_dir;149static String shader_cache_res_dir;150static bool shader_cache_cleanup_on_start;151static bool shader_cache_save_compressed;152static bool shader_cache_save_compressed_zstd;153static bool shader_cache_save_debug;154bool shader_cache_user_dir_valid = false;155bool shader_cache_res_dir_valid = false;156157enum StageType {158STAGE_TYPE_VERTEX,159STAGE_TYPE_FRAGMENT,160STAGE_TYPE_COMPUTE,161STAGE_TYPE_RAYGEN,162STAGE_TYPE_ANY_HIT,163STAGE_TYPE_CLOSEST_HIT,164STAGE_TYPE_MISS,165STAGE_TYPE_INTERSECTION,166STAGE_TYPE_MAX,167};168169StageTemplate stage_templates[STAGE_TYPE_MAX];170171void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template);172Vector<String> _build_variant_stage_sources(uint32_t p_variant, CompileData p_data);173174void _add_stage(const char *p_code, StageType p_stage_type);175176String _version_get_sha1(Version *p_version) const;177String _get_cache_file_relative_path(Version *p_version, int p_group, const String &p_api_name);178String _get_cache_file_path(Version *p_version, int p_group, const String &p_api_name, bool p_user_dir);179bool _load_from_cache(Version *p_version, int p_group);180void _save_to_cache(Version *p_version, int p_group);181void _initialize_cache();182void _version_set(Version *p_version, const HashMap<String, String> &p_code, const Vector<String> &p_custom_defines);183184protected:185ShaderRD();186void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name);187void 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);188189public:190RID version_create(bool p_embedded = true);191192void 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);193void 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);194void 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);195196_FORCE_INLINE_ RID version_get_shader(RID p_version, int p_variant) {197ERR_FAIL_INDEX_V(p_variant, variant_defines.size(), RID());198ERR_FAIL_COND_V(!variants_enabled[p_variant], RID());199200Version *version = version_owner.get_or_null(p_version);201ERR_FAIL_NULL_V(version, RID());202203MutexLock lock(*version->mutex);204205if (version->dirty) {206_initialize_version(version);207for (int i = 0; i < group_enabled.size(); i++) {208if (!group_enabled[i]) {209_allocate_placeholders(version, i);210continue;211}212_compile_version_start(version, i);213}214}215216uint32_t group = variant_to_group[p_variant];217if (version->group_compilation_tasks[group] != 0) {218_compile_version_end(version, group);219}220221if (!version->valid) {222return RID();223}224225return version->variants[p_variant];226}227228bool version_is_valid(RID p_version);229230bool version_free(RID p_version);231232// Enable/disable variants for things that you know won't be used at engine initialization time .233void set_variant_enabled(int p_variant, bool p_enabled);234bool is_variant_enabled(int p_variant) const;235int64_t get_variant_count() const;236int get_variant_to_group(int p_variant) const;237238// Enable/disable groups for things that might be enabled at run time.239void enable_group(int p_group);240bool is_group_enabled(int p_group) const;241int64_t get_group_count() const;242const LocalVector<int> &get_group_to_variants(int p_group) const;243244const String &get_name() const;245246const Vector<uint64_t> &get_dynamic_buffers() const;247248static void shaders_embedded_set_lock();249static const ShaderVersionPairSet &shaders_embedded_set_get();250static void shaders_embedded_set_unlock();251252static void set_shader_cache_user_dir(const String &p_dir);253static const String &get_shader_cache_user_dir();254static void set_shader_cache_res_dir(const String &p_dir);255static const String &get_shader_cache_res_dir();256static void set_shader_cache_save_compressed(bool p_enable);257static void set_shader_cache_save_compressed_zstd(bool p_enable);258static void set_shader_cache_save_debug(bool p_enable);259260static Vector<RD::ShaderStageSPIRVData> compile_stages(const Vector<String> &p_stage_sources, const Vector<uint64_t> &p_dynamic_buffers);261static PackedByteArray save_shader_cache_bytes(const LocalVector<int> &p_variants, const Vector<Vector<uint8_t>> &p_variant_data);262263Vector<String> version_build_variant_stage_sources(RID p_version, int p_variant);264RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version);265String version_get_cache_file_relative_path(RID p_version, int p_group, const String &p_api_name);266267struct DynamicBuffer {268static uint64_t encode(uint32_t p_set_id, uint32_t p_binding) {269return uint64_t(p_set_id) << 32ul | uint64_t(p_binding);270}271};272273// Dynamic Buffers specifies Which buffers will be persistent/dynamic when used.274// See DynamicBuffer::encode. We need this argument because SPIR-V does not distinguish between a275// uniform buffer and a dynamic uniform buffer. At shader level they're the same thing, but the PSO276// is created slightly differently and they're bound differently.277// On D3D12 the Root Layout is also different.278void 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>());279void 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>());280281virtual ~ShaderRD();282};283284285