Debian Codegraph @

Sources/nCine/Graphics/RHI/GX/GxShaderProgram.h

raw
1 #pragma once
2
3 #include "GxShaderTypes.h"
4 #include "GxVertexFormat.h"
5 #include "../RhiTypes.h"
6
7 #include <cstdint>
8 #include <string>
9 #include <vector>
10
11 #include <Containers/ArrayView.h>
12 #include <Containers/String.h>
13 #include <Containers/StringView.h>
14
15 using namespace Death::Containers;
16
17 namespace ShaderCompiler
18 {
19 struct ProgramVariant;
20 }
21
22 namespace nCine::RHI::GX
23 {
24 class GxShaderUniforms;
25 class GxShaderUniformBlocks;
26 struct FixedFunctionGeneratedEffect;
27
28 /**
29 @brief Shader program of the GX backend (aliased as `RHI::ShaderProgram`)
30
31 Does not compile or link GLSL. Instead it carries the offline ShaderCompiler reflection (set with
32 @ref SetReflection() exactly like the OpenGL backend) from which it imports uniforms, uniform
33 blocks and attributes, and the true (program, variant) identity plumbed by the loaders with
34 @ref SetProgramIdentity(), from which the generated fixed-function effect of the program is
35 resolved (see @ref GetGeneratedEffect()) - the object label is kept for logging only. @ref Use()
36 records the program as current on the device; committed loose-uniform values are published back
37 here so the effect can read them.
38 */
39 class GxShaderProgram
40 {
41 friend class GxShaderUniforms;
42 friend class GxShaderUniformBlocks;
43
44 public:
45 enum class Introspection
46 {
47 Enabled,
48 NoUniformsInBlocks,
49 Disabled
50 };
51
52 enum class Status
53 {
54 NotLinked,
55 CompilationFailed,
56 LinkingFailed,
57 Linked,
58 LinkedWithDeferredQueries,
59 LinkedWithIntrospection
60 };
61
62 enum class QueryPhase
63 {
64 Immediate,
65 Deferred
66 };
67
68 /** @brief Default batch size, indicating the shader is not batched */
69 static constexpr std::int32_t DefaultBatchSize = -1;
70
71 GxShaderProgram();
72 explicit GxShaderProgram(QueryPhase queryPhase);
73 GxShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase);
74 GxShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection);
75 GxShaderProgram(StringView vertexFile, StringView fragmentFile);
76 ~GxShaderProgram();
77
78 GxShaderProgram(const GxShaderProgram&) = delete;
79 GxShaderProgram& operator=(const GxShaderProgram&) = delete;
80
81 /** @brief Returns a backend-neutral identifier uniquely identifying the program (feeds material sort keys) */
82 inline std::uint32_t GetUniqueId() const {
83 return _handle;
84 }
85 inline Status GetStatus() const {
86 return _status;
87 }
88 inline Introspection GetIntrospection() const {
89 return _introspection;
90 }
91 inline QueryPhase GetQueryPhase() const {
92 return _queryPhase;
93 }
94 inline std::uint32_t GetBatchSize() const {
95 return _batchSize;
96 }
97 inline void SetBatchSize(std::uint32_t value) {
98 _batchSize = value;
99 }
100
101 bool IsLinked() const;
102
103 std::uint32_t RetrieveInfoLogLength() const {
104 return 0;
105 }
106 void RetrieveInfoLog(std::string& infoLog) const {
107 static_cast<void>(infoLog);
108 }
109
110 inline std::uint32_t GetUniformsSize() const {
111 return _uniformsSize;
112 }
113 inline std::uint32_t GetUniformBlocksSize() const {
114 return _uniformBlocksSize;
115 }
116
117 bool AttachShaderFromFile(ShaderStage stage, StringView filename);
118 bool AttachShaderFromString(ShaderStage stage, StringView string);
119 bool AttachShaderFromStrings(ShaderStage stage, ArrayView<const StringView> strings);
120 bool AttachShaderFromStringsAndFile(ShaderStage stage, ArrayView<const StringView> strings, StringView filename);
121
122 /** @brief Sets the offline reflection consumed by @ref Link() to import uniforms/blocks/attributes */
123 inline void SetReflection(const ShaderCompiler::ProgramVariant* reflection) {
124 _reflection = reflection;
125 }
126 /**
127 @brief Records the true (program, variant) identity of the loaded shader
128
129 @p programName is the `.shader` program name and @p variantName the variant define it was
130 compiled with (empty/null for the base variant) - the exact key of the generated
131 fixed-function tables, plumbed by ContentResolver and RenderResources. Set it together
132 with @ref SetReflection(), before @ref Link(); the table lookup runs at link time.
133 */
134 void SetProgramIdentity(const char* programName, const char* variantName);
135
136 bool Link(Introspection introspection);
137 void Use();
138 bool Validate() {
139 return true;
140 }
141 bool FinalizeAfterLinking(Introspection introspection);
142
143 inline std::uint32_t GetAttributeCount() const {
144 return std::uint32_t(_attributes.size());
145 }
146 bool HasAttribute(const char* name) const;
147 GxVertexFormat::Attribute* GetAttribute(const char* name);
148
149 inline void DefineVertexFormat(const GxBuffer* vbo) {
150 DefineVertexFormat(vbo, nullptr, 0);
151 }
152 inline void DefineVertexFormat(const GxBuffer* vbo, const GxBuffer* ibo) {
153 DefineVertexFormat(vbo, ibo, 0);
154 }
155 void DefineVertexFormat(const GxBuffer* vbo, const GxBuffer* ibo, std::uint32_t vboOffset);
156 /** @brief Returns the vertex buffer last bound by @ref DefineVertexFormat(), or `nullptr` */
157 inline const GxBuffer* GetBoundVbo() const {
158 return _boundVbo;
159 }
160 /** @brief Returns the byte offset into @ref GetBoundVbo() the vertex data starts at */
161 inline std::uint32_t GetBoundVboOffset() const {
162 return _boundVboOffset;
163 }
164
165 void Reset();
166 void SetObjectLabel(StringView label);
167
168 /** @brief Returns whether the "unsupported effect" warning was already emitted for this program, and marks it emitted */
169 bool FetchUnsupportedWarned() {
170 bool value = _unsupportedWarned;
171 _unsupportedWarned = true;
172 return value;
173 }
174
175 inline bool GetLogOnErrors() const {
176 return _shouldLogOnErrors;
177 }
178 inline void SetLogOnErrors(bool shouldLogOnErrors) {
179 _shouldLogOnErrors = shouldLogOnErrors;
180 }
181
182 // -- GX backend extensions (used by the device and the effects) --
183
184 /**
185 @brief Returns the generated fixed-function effect of this program variant, or `nullptr`
186
187 Resolved once at link time from the (program, variant) identity set with
188 @ref SetProgramIdentity() against the table the ShaderCompiler transpiled from the
189 shaders' `fixed_function` blocks. The dispatch runs its function (or the backend
190 pipeline stage its `pipeline` intrinsic names); a null entry is skipped with a
191 one-time warning.
192 */
193 inline const FixedFunctionGeneratedEffect* GetGeneratedEffect() const {
194 return _generatedEffect;
195 }
196 /** @brief Returns the object label the program was tagged with (the shader name), or an empty string */
197 inline const char* GetObjectLabel() const {
198 return _label.data();
199 }
200 /** @brief Returns `true` if the program was compiled as the DITHER variant of its shader */
201 inline bool IsDitherVariant() const {
202 return _ditherVariant;
203 }
204 /** @brief Returns `true` if the program samples indexed textures through the palette texture (its reflection binds `uTexturePalette`) */
205 inline bool UsesPalette() const {
206 return _usesPalette;
207 }
208 /** @brief Returns the offline reflection last set on the program (kept for the effects to read) */
209 inline const ShaderCompiler::ProgramVariant* GetReflection() const {
210 return _effectReflection;
211 }
212 /** @brief Returns the imported metadata of the uniform block with the given name, or `nullptr` */
213 const GxUniformBlock* FindBlock(const char* name) const;
214 /** @brief Publishes a committed loose-uniform value pointer for the effects to read */
215 void SetResolvedUniform(const char* name, const std::uint8_t* data);
216 /** @brief Returns the last published value pointer of the named loose uniform, or `nullptr` */
217 const std::uint8_t* ResolveUniform(const char* name) const;
218 /** @brief Returns the last published `uProjectionMatrix` value pointer without the by-name scan (read per draw) */
219 inline const std::uint8_t* GetResolvedProjectionMatrix() const {
220 return _resolvedProjectionMatrix;
221 }
222 /** @brief Returns the last published `uViewMatrix` value pointer without the by-name scan (read per draw) */
223 inline const std::uint8_t* GetResolvedViewMatrix() const {
224 return _resolvedViewMatrix;
225 }
226
227 private:
228 static std::uint32_t _nextHandle;
229
230 std::uint32_t _handle;
231 Status _status;
232 Introspection _introspection;
233 QueryPhase _queryPhase;
234 std::uint32_t _batchSize;
235 bool _shouldLogOnErrors;
236 std::uint32_t _uniformsSize;
237 std::uint32_t _uniformBlocksSize;
238
239 std::vector<GxUniform> _uniforms;
240 std::vector<GxUniformBlock> _uniformBlocks;
241 std::vector<GxAttribute> _attributes;
242
243 const ShaderCompiler::ProgramVariant* _reflection;
244 // Kept after introspection so the effects can read member offsets/texture bindings at draw time
245 const ShaderCompiler::ProgramVariant* _effectReflection;
246 // The generated-table entry of this (program, variant) (see GetGeneratedEffect()); null when
247 // the shader has no fixed_function block (the draw is then skipped with a one-time warning)
248 const FixedFunctionGeneratedEffect* _generatedEffect;
249 bool _unsupportedWarned = false;
250 bool _ditherVariant;
251 bool _usesPalette;
252 // The true identity set by SetProgramIdentity(): the .shader program name and the variant
253 // define - the generated-table key. Copies, because runtime loaders hand in transient strings.
254 String _programName;
255 String _variantName;
256 // The shader name the program was tagged with (kept for log messages only)
257 String _label;
258
259 GxVertexFormat _vertexFormat;
260 const GxBuffer* _boundVbo;
261 std::uint32_t _boundVboOffset;
262 const GxBuffer* _boundIbo;
263
264 struct ResolvedUniform
265 {
266 String Name;
267 const std::uint8_t* Data;
268 };
269 std::vector<ResolvedUniform> _resolvedUniforms;
270 // The camera matrices are read by the device on every draw, so they bypass the by-name list scan
271 // above; recognized once in SetResolvedUniform() (see there)
272 const std::uint8_t* _resolvedProjectionMatrix = nullptr;
273 const std::uint8_t* _resolvedViewMatrix = nullptr;
274
275 void PerformIntrospection();
276 void ImportReflection();
277 };
278 }