Path: blob/master/servers/rendering/rendering_shader_container.h
10277 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"3435class RenderingShaderContainer : public RefCounted {36GDSOFTCLASS(RenderingShaderContainer, RefCounted);3738public:39static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;40static const uint32_t CONTAINER_VERSION = 2;4142protected:43struct ContainerHeader {44uint32_t magic_number = 0;45uint32_t version = 0;46uint32_t format = 0;47uint32_t format_version = 0;48uint32_t shader_count = 0;49};5051struct ReflectionData {52uint64_t vertex_input_mask = 0;53uint32_t fragment_output_mask = 0;54uint32_t specialization_constants_count = 0;55uint32_t is_compute = 0;56uint32_t has_multiview = 0;57uint32_t compute_local_size[3] = {};58uint32_t set_count = 0;59uint32_t push_constant_size = 0;60uint32_t push_constant_stages_mask = 0;61uint32_t stage_count = 0;62uint32_t shader_name_len = 0;63};6465struct ReflectionBindingData {66uint32_t type = 0;67uint32_t binding = 0;68uint32_t stages = 0;69uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).70uint32_t writable = 0;7172bool operator<(const ReflectionBindingData &p_other) const {73return binding < p_other.binding;74}75};7677struct ReflectionSpecializationData {78uint32_t type = 0;79uint32_t constant_id = 0;80uint32_t int_value = 0;81uint32_t stage_flags = 0;82};8384struct ShaderHeader {85uint32_t shader_stage = 0;86uint32_t code_compressed_size = 0;87uint32_t code_compression_flags = 0;88uint32_t code_decompressed_size = 0;89};9091ReflectionData reflection_data;92Vector<uint32_t> reflection_binding_set_uniforms_count;93Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;94Vector<ReflectionSpecializationData> reflection_specialization_data;95Vector<RenderingDeviceCommons::ShaderStage> reflection_shader_stages;9697virtual uint32_t _format() const = 0;98virtual uint32_t _format_version() const = 0;99100// These methods will always be called with a valid pointer.101virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);102virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);103virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);104virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);105virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);106virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);107virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);108virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);109virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);110111// These methods will be called with a nullptr to retrieve the size of the data.112virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;113virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;114virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;115virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;116virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;117virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;118119// This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.120virtual void _set_from_shader_reflection_post(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection);121122// This method will be called when set_code_from_spirv() is called.123virtual bool _set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) = 0;124125public:126enum CompressionFlags {127COMPRESSION_FLAG_ZSTD = 0x1,128};129130struct Shader {131RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;132PackedByteArray code_compressed_bytes;133uint32_t code_compression_flags = 0;134uint32_t code_decompressed_size = 0;135};136137CharString shader_name;138Vector<Shader> shaders;139140void set_from_shader_reflection(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection);141bool set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv);142RenderingDeviceCommons::ShaderReflection get_shader_reflection() const;143bool from_bytes(const PackedByteArray &p_bytes);144PackedByteArray to_bytes() const;145bool 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;146bool 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;147RenderingShaderContainer();148virtual ~RenderingShaderContainer();149};150151class RenderingShaderContainerFormat : public RenderingDeviceCommons {152public:153virtual Ref<RenderingShaderContainer> create_container() const = 0;154virtual ShaderLanguageVersion get_shader_language_version() const = 0;155virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;156};157158159