Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/rendering_shader_container.h
11352 views
1
/**************************************************************************/
2
/* rendering_shader_container.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/object/ref_counted.h"
34
#include "servers/rendering/rendering_device_commons.h"
35
36
struct SpvReflectShaderModule;
37
38
class RenderingShaderContainer : public RefCounted {
39
GDSOFTCLASS(RenderingShaderContainer, RefCounted);
40
41
public:
42
static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;
43
static const uint32_t CONTAINER_VERSION = 2;
44
45
protected:
46
struct ContainerHeader {
47
uint32_t magic_number = 0;
48
uint32_t version = 0;
49
uint32_t format = 0;
50
uint32_t format_version = 0;
51
uint32_t shader_count = 0;
52
};
53
54
struct ReflectionData {
55
uint64_t vertex_input_mask = 0;
56
uint32_t fragment_output_mask = 0;
57
uint32_t specialization_constants_count = 0;
58
uint32_t is_compute = 0;
59
uint32_t has_multiview = 0;
60
uint32_t compute_local_size[3] = {};
61
uint32_t set_count = 0;
62
uint32_t push_constant_size = 0;
63
uint32_t push_constant_stages_mask = 0;
64
uint32_t stage_count = 0;
65
uint32_t shader_name_len = 0;
66
};
67
68
struct ReflectionBindingData {
69
uint32_t type = 0;
70
uint32_t binding = 0;
71
uint32_t stages = 0;
72
uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).
73
uint32_t writable = 0;
74
75
bool operator<(const ReflectionBindingData &p_other) const {
76
return binding < p_other.binding;
77
}
78
};
79
80
struct ReflectionSpecializationData {
81
uint32_t type = 0;
82
uint32_t constant_id = 0;
83
uint32_t int_value = 0;
84
uint32_t stage_flags = 0;
85
};
86
87
struct ShaderHeader {
88
uint32_t shader_stage = 0;
89
uint32_t code_compressed_size = 0;
90
uint32_t code_compression_flags = 0;
91
uint32_t code_decompressed_size = 0;
92
};
93
94
ReflectionData reflection_data;
95
Vector<uint32_t> reflection_binding_set_uniforms_count;
96
Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;
97
Vector<ReflectionSpecializationData> reflection_specialization_data;
98
Vector<RenderingDeviceCommons::ShaderStage> reflection_shader_stages;
99
100
virtual uint32_t _format() const = 0;
101
virtual uint32_t _format_version() const = 0;
102
103
// These methods will always be called with a valid pointer.
104
virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);
105
virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);
106
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);
107
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);
108
virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);
109
virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);
110
virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);
111
virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);
112
virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);
113
114
// These methods will be called with a nullptr to retrieve the size of the data.
115
virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;
116
virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;
117
virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
118
virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
119
virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
120
virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;
121
122
// This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.
123
virtual void _set_from_shader_reflection_post(const RenderingDeviceCommons::ShaderReflection &p_reflection);
124
125
class ReflectedShaderStage {
126
friend class RenderingShaderContainer;
127
128
Vector<uint8_t> _spirv_data;
129
SpvReflectShaderModule *_module = nullptr;
130
131
public:
132
RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;
133
const SpvReflectShaderModule &module() const;
134
const Span<uint32_t> spirv() const;
135
const Vector<uint8_t> spirv_data() const { return _spirv_data; }
136
137
ReflectedShaderStage();
138
~ReflectedShaderStage();
139
};
140
141
// This method will be called when set_code_from_spirv() is called.
142
virtual bool _set_code_from_spirv(Span<ReflectedShaderStage> p_spirv) = 0;
143
144
void set_from_shader_reflection(const RenderingDeviceCommons::ShaderReflection &p_reflection);
145
Error reflect_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv, LocalVector<ReflectedShaderStage> &r_refl);
146
147
public:
148
enum CompressionFlags {
149
COMPRESSION_FLAG_ZSTD = 0x1,
150
};
151
152
struct Shader {
153
RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;
154
PackedByteArray code_compressed_bytes;
155
uint32_t code_compression_flags = 0;
156
uint32_t code_decompressed_size = 0;
157
};
158
159
CharString shader_name;
160
Vector<Shader> shaders;
161
162
bool set_code_from_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv);
163
RenderingDeviceCommons::ShaderReflection get_shader_reflection() const;
164
bool from_bytes(const PackedByteArray &p_bytes);
165
PackedByteArray to_bytes() const;
166
bool 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;
167
bool 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;
168
RenderingShaderContainer();
169
virtual ~RenderingShaderContainer();
170
};
171
172
class RenderingShaderContainerFormat : public RenderingDeviceCommons {
173
GDSOFTCLASS(RenderingShaderContainerFormat, RenderingDeviceCommons);
174
175
public:
176
virtual Ref<RenderingShaderContainer> create_container() const = 0;
177
virtual ShaderLanguageVersion get_shader_language_version() const = 0;
178
virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;
179
};
180
181