Path: blob/master/servers/rendering/rendering_shader_container.h
11352 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;3637class RenderingShaderContainer : public RefCounted {38GDSOFTCLASS(RenderingShaderContainer, RefCounted);3940public:41static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;42static const uint32_t CONTAINER_VERSION = 2;4344protected:45struct ContainerHeader {46uint32_t magic_number = 0;47uint32_t version = 0;48uint32_t format = 0;49uint32_t format_version = 0;50uint32_t shader_count = 0;51};5253struct ReflectionData {54uint64_t vertex_input_mask = 0;55uint32_t fragment_output_mask = 0;56uint32_t specialization_constants_count = 0;57uint32_t is_compute = 0;58uint32_t has_multiview = 0;59uint32_t compute_local_size[3] = {};60uint32_t set_count = 0;61uint32_t push_constant_size = 0;62uint32_t push_constant_stages_mask = 0;63uint32_t stage_count = 0;64uint32_t shader_name_len = 0;65};6667struct ReflectionBindingData {68uint32_t type = 0;69uint32_t binding = 0;70uint32_t stages = 0;71uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).72uint32_t writable = 0;7374bool operator<(const ReflectionBindingData &p_other) const {75return binding < p_other.binding;76}77};7879struct ReflectionSpecializationData {80uint32_t type = 0;81uint32_t constant_id = 0;82uint32_t int_value = 0;83uint32_t stage_flags = 0;84};8586struct ShaderHeader {87uint32_t shader_stage = 0;88uint32_t code_compressed_size = 0;89uint32_t code_compression_flags = 0;90uint32_t code_decompressed_size = 0;91};9293ReflectionData reflection_data;94Vector<uint32_t> reflection_binding_set_uniforms_count;95Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;96Vector<ReflectionSpecializationData> reflection_specialization_data;97Vector<RenderingDeviceCommons::ShaderStage> reflection_shader_stages;9899virtual uint32_t _format() const = 0;100virtual uint32_t _format_version() const = 0;101102// These methods will always be called with a valid pointer.103virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);104virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);105virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);106virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);107virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);108virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);109virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);110virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);111virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);112113// These methods will be called with a nullptr to retrieve the size of the data.114virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;115virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;116virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;117virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;118virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;119virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;120121// This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.122virtual void _set_from_shader_reflection_post(const RenderingDeviceCommons::ShaderReflection &p_reflection);123124class ReflectedShaderStage {125friend class RenderingShaderContainer;126127Vector<uint8_t> _spirv_data;128SpvReflectShaderModule *_module = nullptr;129130public:131RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;132const SpvReflectShaderModule &module() const;133const Span<uint32_t> spirv() const;134const Vector<uint8_t> spirv_data() const { return _spirv_data; }135136ReflectedShaderStage();137~ReflectedShaderStage();138};139140// This method will be called when set_code_from_spirv() is called.141virtual bool _set_code_from_spirv(Span<ReflectedShaderStage> p_spirv) = 0;142143void set_from_shader_reflection(const RenderingDeviceCommons::ShaderReflection &p_reflection);144Error reflect_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv, LocalVector<ReflectedShaderStage> &r_refl);145146public:147enum CompressionFlags {148COMPRESSION_FLAG_ZSTD = 0x1,149};150151struct Shader {152RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;153PackedByteArray code_compressed_bytes;154uint32_t code_compression_flags = 0;155uint32_t code_decompressed_size = 0;156};157158CharString shader_name;159Vector<Shader> shaders;160161bool set_code_from_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv);162RenderingDeviceCommons::ShaderReflection get_shader_reflection() const;163bool from_bytes(const PackedByteArray &p_bytes);164PackedByteArray to_bytes() const;165bool 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;166bool 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;167RenderingShaderContainer();168virtual ~RenderingShaderContainer();169};170171class RenderingShaderContainerFormat : public RenderingDeviceCommons {172GDSOFTCLASS(RenderingShaderContainerFormat, RenderingDeviceCommons);173174public:175virtual Ref<RenderingShaderContainer> create_container() const = 0;176virtual ShaderLanguageVersion get_shader_language_version() const = 0;177virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;178};179180181