Path: blob/master/servers/rendering/rendering_shader_container.h
21052 views
/**************************************************************************/1/* rendering_shader_container.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/object/ref_counted.h"33#include "servers/rendering/rendering_device_commons.h"3435struct SpvReflectShaderModule;36struct SpvReflectDescriptorBinding;37struct SpvReflectSpecializationConstant;3839class RenderingShaderContainer : public RefCounted {40GDSOFTCLASS(RenderingShaderContainer, RefCounted);4142public:43static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;44static const uint32_t CONTAINER_VERSION = 2;4546protected:47using RDC = RenderingDeviceCommons;4849struct ContainerHeader {50uint32_t magic_number = 0;51uint32_t version = 0;52uint32_t format = 0;53uint32_t format_version = 0;54uint32_t shader_count = 0;55};5657struct ReflectionData {58uint64_t vertex_input_mask = 0;59uint32_t fragment_output_mask = 0;60uint32_t specialization_constants_count = 0;61RDC::PipelineType pipeline_type = RDC::PIPELINE_TYPE_RASTERIZATION;62uint32_t has_multiview = 0;63uint32_t has_dynamic_buffers = 0;64uint32_t compute_local_size[3] = {};65uint32_t set_count = 0;66uint32_t push_constant_size = 0;67uint32_t push_constant_stages_mask = 0;68uint32_t stage_count = 0;69uint32_t shader_name_len = 0;70};7172struct ReflectionBindingData {73uint32_t type = 0;74uint32_t binding = 0;75uint32_t stages = 0;76uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).77uint32_t writable = 0;7879bool operator<(const ReflectionBindingData &p_other) const {80return binding < p_other.binding;81}82};8384struct ReflectionSpecializationData {85uint32_t type = 0;86uint32_t constant_id = 0;87uint32_t int_value = 0;88uint32_t stage_flags = 0;89};9091struct ShaderHeader {92uint32_t shader_stage = 0;93uint32_t code_compressed_size = 0;94uint32_t code_compression_flags = 0;95uint32_t code_decompressed_size = 0;96};9798ReflectionData reflection_data;99Vector<uint32_t> reflection_binding_set_uniforms_count;100Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;101Vector<ReflectionSpecializationData> reflection_specialization_data;102Vector<RDC::ShaderStage> reflection_shader_stages;103104virtual uint32_t _format() const = 0;105virtual uint32_t _format_version() const = 0;106107// These methods will always be called with a valid pointer.108virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);109virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);110virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);111virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);112virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);113virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);114virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);115virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);116virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);117118// These methods will be called with a nullptr to retrieve the size of the data.119virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;120virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;121virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;122virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;123virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;124virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;125126template <class T>127struct ReflectSymbol {128static constexpr uint32_t STAGE_INDEX[RDC::SHADER_STAGE_MAX] = {1290, // SHADER_STAGE_VERTEX1301, // SHADER_STAGE_FRAGMENT1310, // SHADER_STAGE_TESSELATION_CONTROL1321, // SHADER_STAGE_TESSELATION_EVALUATION1330, // SHADER_STAGE_COMPUTE134};135136BitField<RDC::ShaderStage> stages = {};137138private:139const T *_spv_reflect[2] = { nullptr };140141public:142_FORCE_INLINE_ constexpr uint32_t get_index_for_stage(RDC::ShaderStage p_stage) const {143DEV_ASSERT(stages.has_flag((1 << p_stage)));144return STAGE_INDEX[p_stage];145}146147const T &get_spv_reflect(RDC::ShaderStage p_stage) const;148149/*! Returns the first valid stage if multiple stages are set.150*151* Crashes if no stages are set.152*/153const T &get_spv_reflect() const {154for (const T *d : _spv_reflect) {155if (d != nullptr) {156return *d;157}158}159CRASH_NOW_MSG("No stages set in ReflectSymbol");160}161void set_spv_reflect(RDC::ShaderStage p_stage, const T *p_spv);162};163164struct ReflectImageTraits {165RDC::DataFormat format = RDC::DATA_FORMAT_MAX;166};167168struct ReflectUniform : ReflectSymbol<SpvReflectDescriptorBinding> {169RDC::UniformType type = RDC::UniformType::UNIFORM_TYPE_MAX;170uint32_t binding = 0;171172ReflectImageTraits image;173174uint32_t length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).175bool writable = false;176177bool operator<(const ReflectUniform &p_other) const {178if (binding != p_other.binding) {179return binding < p_other.binding;180}181if (type != p_other.type) {182return type < p_other.type;183}184if (writable != p_other.writable) {185return writable < p_other.writable;186}187if (stages != p_other.stages) {188return stages < p_other.stages;189}190if (length != p_other.length) {191return length < p_other.length;192}193return false;194}195};196197struct ReflectSpecializationConstant : ReflectSymbol<SpvReflectSpecializationConstant> {198RDC::PipelineSpecializationConstantType type = {};199uint32_t constant_id = 0xffffffff;200union {201uint32_t int_value = 0;202float float_value;203bool bool_value;204};205206bool operator<(const ReflectSpecializationConstant &p_other) const { return constant_id < p_other.constant_id; }207};208209class ReflectShaderStage {210friend class RenderingShaderContainer;211212Vector<uint8_t> _spirv_data;213SpvReflectShaderModule *_module = nullptr;214215public:216RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;217const SpvReflectShaderModule &module() const;218const Span<uint32_t> spirv() const;219const Vector<uint8_t> spirv_data() const { return _spirv_data; }220221ReflectShaderStage();222~ReflectShaderStage();223};224225typedef LocalVector<ReflectUniform> ReflectDescriptorSet;226227struct ReflectShader {228uint64_t vertex_input_mask = 0;229uint32_t fragment_output_mask = 0;230uint32_t compute_local_size[3] = {};231uint32_t push_constant_size = 0;232bool has_multiview = false;233bool has_dynamic_buffers = false;234RDC::PipelineType pipeline_type = RDC::PIPELINE_TYPE_RASTERIZATION;235236LocalVector<ReflectShaderStage> shader_stages;237LocalVector<ReflectDescriptorSet> uniform_sets;238LocalVector<ReflectSymbol<SpvReflectDescriptorBinding>> reflect_uniforms;239LocalVector<ReflectSpecializationConstant> specialization_constants;240LocalVector<ReflectSymbol<SpvReflectSpecializationConstant>> reflect_specialization_constants;241LocalVector<RDC::ShaderStage> stages_vector;242BitField<RDC::ShaderStage> stages_bits = {};243BitField<RDC::ShaderStage> push_constant_stages = {};244245_FORCE_INLINE_ bool is_compute() const {246return stages_bits.has_flag(RDC::SHADER_STAGE_COMPUTE_BIT);247}248249/*! Returns the uniform at the specified global index.250*251* This is a flattened view of all uniform sets.252*/253ReflectUniform &uniform_at(uint32_t p_index) {254for (LocalVector<ReflectUniform> &set : uniform_sets) {255if (p_index < set.size()) {256return set[p_index];257}258p_index -= set.size();259}260CRASH_NOW_MSG(vformat("Uniform index %d out of range (total %d)", p_index, uniform_count()));261}262263uint32_t uniform_count() const {264uint32_t count = 0;265for (const LocalVector<ReflectUniform> &set : uniform_sets) {266count += set.size();267}268return count;269}270};271272// This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.273virtual void _set_from_shader_reflection_post(const ReflectShader &p_shader);274275// This method will be called when set_code_from_spirv() is called.276virtual bool _set_code_from_spirv(const ReflectShader &p_shader) = 0;277278void set_from_shader_reflection(const ReflectShader &p_reflection);279Error reflect_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv, ReflectShader &r_shader);280281public:282enum CompressionFlags {283COMPRESSION_FLAG_ZSTD = 0x1,284};285286struct Shader {287RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;288PackedByteArray code_compressed_bytes;289uint32_t code_compression_flags = 0;290uint32_t code_decompressed_size = 0;291};292293CharString shader_name;294Vector<Shader> shaders;295296bool set_code_from_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv);297RDC::ShaderReflection get_shader_reflection() const;298bool from_bytes(const PackedByteArray &p_bytes);299PackedByteArray to_bytes() const;300bool compress_code(const uint8_t *p_decompressed_bytes, uint32_t p_decompressed_size, uint8_t *p_compressed_bytes, uint32_t *r_compressed_size, uint32_t *r_compressed_flags) const;301bool decompress_code(const uint8_t *p_compressed_bytes, uint32_t p_compressed_size, uint32_t p_compressed_flags, uint8_t *p_decompressed_bytes, uint32_t p_decompressed_size) const;302RenderingShaderContainer();303virtual ~RenderingShaderContainer();304};305306class RenderingShaderContainerFormat : public RenderingDeviceCommons {307GDSOFTCLASS(RenderingShaderContainerFormat, RenderingDeviceCommons);308309public:310virtual Ref<RenderingShaderContainer> create_container() const = 0;311virtual ShaderLanguageVersion get_shader_language_version() const = 0;312virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;313};314315316