Debian Codegraph @

Sources/nCine/Graphics/RHI/GS/GsShaderProgram.h

raw
1 #pragma once
2
3 #include "GsShaderTypes.h"
4 #include "GsVertexFormat.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::GS
23 {
24 class GsShaderUniforms;
25 class GsShaderUniformBlocks;
26 struct FixedFunctionGeneratedEffect;
27
28 /**
29 @brief Shader program of the GS backend (aliased as `RHI::ShaderProgram`)
30
31 Does not compile or link GLSL - the Graphics Synthesizer has no programmable stage. Instead it carries
32 the offline ShaderCompiler reflection (set with @ref SetReflection() exactly like the OpenGL backend)
33 from which it imports uniforms, uniform blocks and attributes, and the true (program, variant) identity
34 plumbed by the loaders with @ref SetProgramIdentity(), from which the generated fixed-function effect of
35 the program is resolved (see @ref GetGeneratedEffect()) - the object label is kept for logging only.
36 @ref Use() records the program as current on the device; committed loose-uniform values are published
37 back here so the effect can read them by name.
38 */
39 class GsShaderProgram
40 {
41 friend class GsShaderUniforms;
42 friend class GsShaderUniformBlocks;
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 GsShaderProgram();
72 explicit GsShaderProgram(QueryPhase queryPhase);
73 GsShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase);
74 GsShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection);
75 GsShaderProgram(StringView vertexFile, StringView fragmentFile);
76 ~GsShaderProgram();
77
78 GsShaderProgram(const GsShaderProgram&) = delete;
79 GsShaderProgram& operator=(const GsShaderProgram&) = 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 compiled
130 with (empty/null for the base variant) - the exact key of the generated fixed-function tables,
131 plumbed by ContentResolver and RenderResources. Set it together with @ref SetReflection(), before
132 @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 GsVertexFormat::Attribute* GetAttribute(const char* name);
148
149 inline void DefineVertexFormat(const GsBuffer* vbo) {
150 DefineVertexFormat(vbo, nullptr, 0);
151 }
152 inline void DefineVertexFormat(const GsBuffer* vbo, const GsBuffer* ibo) {
153 DefineVertexFormat(vbo, ibo, 0);
154 }
155 void DefineVertexFormat(const GsBuffer* vbo, const GsBuffer* ibo, std::uint32_t vboOffset);
156 /** @brief Returns the vertex buffer last bound by @ref DefineVertexFormat(), or `nullptr` */
157 inline const GsBuffer* 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 // -- GS 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 shaders'
189 `fixed_function` blocks. The dispatch runs its function (or the backend pipeline stage its
190 `pipeline` intrinsic names); a null entry is skipped with a one-time warning.
191 */
192 inline const FixedFunctionGeneratedEffect* GetGeneratedEffect() const {
193 return _generatedEffect;
194 }
195 /** @brief Returns the object label the program was tagged with (the shader name), or an empty string */
196 inline const char* GetObjectLabel() const {
197 return _label.data();
198 }
199 /** @brief Returns `true` if the program was compiled as the DITHER variant of its shader */
200 inline bool IsDitherVariant() const {
201 return _ditherVariant;
202 }
203 /** @brief Returns `true` if the program samples indexed textures through the palette texture (its reflection binds `uTexturePalette`) */
204 inline bool UsesPalette() const {
205 return _usesPalette;
206 }
207 /** @brief Returns the offline reflection last set on the program (kept for the effects to read) */
208 inline const ShaderCompiler::ProgramVariant* GetReflection() const {
209 return _effectReflection;
210 }
211 /** @brief Returns the imported metadata of the uniform block with the given name, or `nullptr` */
212 const GsUniformBlock* FindBlock(const char* name) const;
213 /** @brief Publishes a committed loose-uniform value pointer for the effects to read */
214 void SetResolvedUniform(const char* name, const std::uint8_t* data);
215 /** @brief Returns the last published value pointer of the named loose uniform, or `nullptr` */
216 const std::uint8_t* ResolveUniform(const char* name) const;
217 /** @brief Returns the published `uProjectionMatrix` value without the by-name scan (it runs per draw) */
218 inline const std::uint8_t* GetResolvedProjection() const {
219 return _resolvedProjection;
220 }
221 /** @brief Returns the published `uViewMatrix` value without the by-name scan (it runs per draw) */
222 inline const std::uint8_t* GetResolvedView() const {
223 return _resolvedView;
224 }
225
226 private:
227 static std::uint32_t _nextHandle;
228
229 std::uint32_t _handle;
230 Status _status;
231 Introspection _introspection;
232 QueryPhase _queryPhase;
233 std::uint32_t _batchSize;
234 bool _shouldLogOnErrors;
235 std::uint32_t _uniformsSize;
236 std::uint32_t _uniformBlocksSize;
237
238 std::vector<GsUniform> _uniforms;
239 std::vector<GsUniformBlock> _uniformBlocks;
240 std::vector<GsAttribute> _attributes;
241
242 const ShaderCompiler::ProgramVariant* _reflection;
243 // Kept after introspection so the effects can read member offsets/texture bindings at draw time
244 const ShaderCompiler::ProgramVariant* _effectReflection;
245 // The generated-table entry of this (program, variant) (see GetGeneratedEffect()); null when the
246 // shader has no fixed_function block (the draw is then skipped with a one-time warning)
247 const FixedFunctionGeneratedEffect* _generatedEffect;
248 bool _unsupportedWarned = false;
249 bool _ditherVariant;
250 bool _usesPalette;
251 // The true identity set by SetProgramIdentity(): the .shader program name and the variant define -
252 // the generated-table key. Copies, because runtime loaders hand in transient strings.
253 String _programName;
254 String _variantName;
255 // The shader name the program was tagged with (kept for log messages only)
256 String _label;
257
258 GsVertexFormat _vertexFormat;
259 const GsBuffer* _boundVbo;
260 std::uint32_t _boundVboOffset;
261 const GsBuffer* _boundIbo;
262
263 struct ResolvedUniform
264 {
265 String Name;
266 const std::uint8_t* Data;
267 };
268 std::vector<ResolvedUniform> _resolvedUniforms;
269 // The two names every dispatch reads are kept as direct pointers, bypassing the by-name scan
270 const std::uint8_t* _resolvedProjection = nullptr;
271 const std::uint8_t* _resolvedView = nullptr;
272
273 void PerformIntrospection();
274 void ImportReflection();
275 };
276 }