Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/rendering_shader_container.h
10277 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
class RenderingShaderContainer : public RefCounted {
37
GDSOFTCLASS(RenderingShaderContainer, RefCounted);
38
39
public:
40
static const uint32_t CONTAINER_MAGIC_NUMBER = 0x43535247;
41
static const uint32_t CONTAINER_VERSION = 2;
42
43
protected:
44
struct ContainerHeader {
45
uint32_t magic_number = 0;
46
uint32_t version = 0;
47
uint32_t format = 0;
48
uint32_t format_version = 0;
49
uint32_t shader_count = 0;
50
};
51
52
struct ReflectionData {
53
uint64_t vertex_input_mask = 0;
54
uint32_t fragment_output_mask = 0;
55
uint32_t specialization_constants_count = 0;
56
uint32_t is_compute = 0;
57
uint32_t has_multiview = 0;
58
uint32_t compute_local_size[3] = {};
59
uint32_t set_count = 0;
60
uint32_t push_constant_size = 0;
61
uint32_t push_constant_stages_mask = 0;
62
uint32_t stage_count = 0;
63
uint32_t shader_name_len = 0;
64
};
65
66
struct ReflectionBindingData {
67
uint32_t type = 0;
68
uint32_t binding = 0;
69
uint32_t stages = 0;
70
uint32_t length = 0; // Size of arrays (in total elements), or UBOs (in bytes * total elements).
71
uint32_t writable = 0;
72
73
bool operator<(const ReflectionBindingData &p_other) const {
74
return binding < p_other.binding;
75
}
76
};
77
78
struct ReflectionSpecializationData {
79
uint32_t type = 0;
80
uint32_t constant_id = 0;
81
uint32_t int_value = 0;
82
uint32_t stage_flags = 0;
83
};
84
85
struct ShaderHeader {
86
uint32_t shader_stage = 0;
87
uint32_t code_compressed_size = 0;
88
uint32_t code_compression_flags = 0;
89
uint32_t code_decompressed_size = 0;
90
};
91
92
ReflectionData reflection_data;
93
Vector<uint32_t> reflection_binding_set_uniforms_count;
94
Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;
95
Vector<ReflectionSpecializationData> reflection_specialization_data;
96
Vector<RenderingDeviceCommons::ShaderStage> reflection_shader_stages;
97
98
virtual uint32_t _format() const = 0;
99
virtual uint32_t _format_version() const = 0;
100
101
// These methods will always be called with a valid pointer.
102
virtual uint32_t _from_bytes_header_extra_data(const uint8_t *p_bytes);
103
virtual uint32_t _from_bytes_reflection_extra_data(const uint8_t *p_bytes);
104
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes);
105
virtual uint32_t _from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index);
106
virtual uint32_t _from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes);
107
virtual uint32_t _from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index);
108
virtual uint32_t _from_bytes_shader_extra_data_start(const uint8_t *p_bytes);
109
virtual uint32_t _from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index);
110
virtual uint32_t _from_bytes_footer_extra_data(const uint8_t *p_bytes);
111
112
// These methods will be called with a nullptr to retrieve the size of the data.
113
virtual uint32_t _to_bytes_header_extra_data(uint8_t *p_bytes) const;
114
virtual uint32_t _to_bytes_reflection_extra_data(uint8_t *p_bytes) const;
115
virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
116
virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
117
virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
118
virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;
119
120
// This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.
121
virtual void _set_from_shader_reflection_post(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection);
122
123
// This method will be called when set_code_from_spirv() is called.
124
virtual bool _set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) = 0;
125
126
public:
127
enum CompressionFlags {
128
COMPRESSION_FLAG_ZSTD = 0x1,
129
};
130
131
struct Shader {
132
RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;
133
PackedByteArray code_compressed_bytes;
134
uint32_t code_compression_flags = 0;
135
uint32_t code_decompressed_size = 0;
136
};
137
138
CharString shader_name;
139
Vector<Shader> shaders;
140
141
void set_from_shader_reflection(const String &p_shader_name, const RenderingDeviceCommons::ShaderReflection &p_reflection);
142
bool set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv);
143
RenderingDeviceCommons::ShaderReflection get_shader_reflection() const;
144
bool from_bytes(const PackedByteArray &p_bytes);
145
PackedByteArray to_bytes() const;
146
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;
147
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;
148
RenderingShaderContainer();
149
virtual ~RenderingShaderContainer();
150
};
151
152
class RenderingShaderContainerFormat : public RenderingDeviceCommons {
153
public:
154
virtual Ref<RenderingShaderContainer> create_container() const = 0;
155
virtual ShaderLanguageVersion get_shader_language_version() const = 0;
156
virtual ShaderSpirvVersion get_shader_spirv_version() const = 0;
157
};
158
159