Debian Codegraph @

Sources/nCine/Graphics/RHI/Software/SwShaderProgram.h

raw
1 #pragma once
2
3 #include "SwShaderTypes.h"
4 #include "SwVertexFormat.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::Software
23 {
24 class SwShaderUniforms;
25 class SwShaderUniformBlocks;
26
27 /**
28 @brief The hand-written C++ effect a software program dispatches to at draw time
29
30 The software backend does not run GLSL — instead each program is tagged with the effect that
31 reproduces its shader, derived from the program's object label. New effects (palette remap, tinted,
32 outline, ...) are added here as the backend grows; @ref Unknown programs log a skipped draw.
33 */
34 enum class SwEffect
35 {
36 Unknown, /**< No matching C++ effect — draws are skipped with a log message */
37 DefaultSprite, /**< `texture(uTexture, vTexCoords) * vColor` over a single instance */
38 DefaultBatchedSprites, /**< The same effect over an array of batched instances */
39 DefaultSpriteNoTexture, /**< Solid-colour sprite (`vColor`, no texture) over a single instance */
40 DefaultBatchedSpritesNoTexture, /**< The solid-colour sprite over an array of batched instances */
41 TexturedBackground, /**< The animated, per-pixel-warped menu/level background (planar tunnel) */
42 TexturedBackgroundCircle, /**< The circular ("tube") variant of the textured background */
43 PaletteRemap, /**< An R8/RG8 index sprite recolored through the shared palette texture */
44 BatchedPaletteRemap, /**< The palette-remap effect over an array of batched instances */
45 Combine /**< The viewport compositor (scene + lighting + blur + ambient) */
46 };
47
48 /**
49 @brief Shader program of the software backend (aliased as `RHI::ShaderProgram`)
50
51 Does not compile or link GLSL. Instead it carries the offline ShaderCompiler reflection (set with
52 @ref SetReflection() exactly like the OpenGL backend) from which it imports uniforms, uniform
53 blocks and attributes, and a @ref SwEffect identity derived from its object label that tells the
54 device which hand-written C++ effect to run. @ref Use() records the program as current on the
55 device; committed loose-uniform values are published back here so the effect can read them.
56 */
57 class SwShaderProgram
58 {
59 friend class SwShaderUniforms;
60 friend class SwShaderUniformBlocks;
61
62 public:
63 enum class Introspection
64 {
65 Enabled,
66 NoUniformsInBlocks,
67 Disabled
68 };
69
70 enum class Status
71 {
72 NotLinked,
73 CompilationFailed,
74 LinkingFailed,
75 Linked,
76 LinkedWithDeferredQueries,
77 LinkedWithIntrospection
78 };
79
80 enum class QueryPhase
81 {
82 Immediate,
83 Deferred
84 };
85
86 /** @brief Default batch size, indicating the shader is not batched */
87 static constexpr std::int32_t DefaultBatchSize = -1;
88
89 SwShaderProgram();
90 explicit SwShaderProgram(QueryPhase queryPhase);
91 SwShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase);
92 SwShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection);
93 SwShaderProgram(StringView vertexFile, StringView fragmentFile);
94 ~SwShaderProgram();
95
96 SwShaderProgram(const SwShaderProgram&) = delete;
97 SwShaderProgram& operator=(const SwShaderProgram&) = delete;
98
99 /** @brief Returns a backend-neutral identifier uniquely identifying the program (feeds material sort keys) */
100 inline std::uint32_t GetUniqueId() const {
101 return _handle;
102 }
103 inline Status GetStatus() const {
104 return _status;
105 }
106 inline Introspection GetIntrospection() const {
107 return _introspection;
108 }
109 inline QueryPhase GetQueryPhase() const {
110 return _queryPhase;
111 }
112 inline std::uint32_t GetBatchSize() const {
113 return _batchSize;
114 }
115 inline void SetBatchSize(std::uint32_t value) {
116 _batchSize = value;
117 }
118
119 bool IsLinked() const;
120
121 std::uint32_t RetrieveInfoLogLength() const {
122 return 0;
123 }
124 void RetrieveInfoLog(std::string& infoLog) const {
125 static_cast<void>(infoLog);
126 }
127
128 inline std::uint32_t GetUniformsSize() const {
129 return _uniformsSize;
130 }
131 inline std::uint32_t GetUniformBlocksSize() const {
132 return _uniformBlocksSize;
133 }
134
135 bool AttachShaderFromFile(ShaderStage stage, StringView filename);
136 bool AttachShaderFromString(ShaderStage stage, StringView string);
137 bool AttachShaderFromStrings(ShaderStage stage, ArrayView<const StringView> strings);
138 bool AttachShaderFromStringsAndFile(ShaderStage stage, ArrayView<const StringView> strings, StringView filename);
139
140 /** @brief Sets the offline reflection consumed by @ref Link() to import uniforms/blocks/attributes */
141 inline void SetReflection(const ShaderCompiler::ProgramVariant* reflection) {
142 _reflection = reflection;
143 }
144 /**
145 @brief Records the true (program, variant) identity of the loaded shader
146
147 Only the fixed-function console backends consume it (they resolve their generated
148 effect tables from this identity instead of the object label); the software backend
149 still keys its fast paths and generated fragments on the object label and ignores it.
150 */
151 inline void SetProgramIdentity(const char* programName, const char* variantName) {
152 static_cast<void>(programName);
153 static_cast<void>(variantName);
154 }
155
156
157 bool Link(Introspection introspection);
158 void Use();
159 bool Validate() {
160 return true;
161 }
162 bool FinalizeAfterLinking(Introspection introspection);
163
164 inline std::uint32_t GetAttributeCount() const {
165 return std::uint32_t(_attributes.size());
166 }
167 bool HasAttribute(const char* name) const;
168 SwVertexFormat::Attribute* GetAttribute(const char* name);
169
170 inline void DefineVertexFormat(const SwBuffer* vbo) {
171 DefineVertexFormat(vbo, nullptr, 0);
172 }
173 inline void DefineVertexFormat(const SwBuffer* vbo, const SwBuffer* ibo) {
174 DefineVertexFormat(vbo, ibo, 0);
175 }
176 void DefineVertexFormat(const SwBuffer* vbo, const SwBuffer* ibo, std::uint32_t vboOffset);
177
178 void Reset();
179 void SetObjectLabel(StringView label);
180
181 inline bool GetLogOnErrors() const {
182 return _shouldLogOnErrors;
183 }
184 inline void SetLogOnErrors(bool shouldLogOnErrors) {
185 _shouldLogOnErrors = shouldLogOnErrors;
186 }
187
188 // -- Software backend extensions (used by the device and the effects) --
189
190 /** @brief Returns the C++ effect this program dispatches to */
191 inline SwEffect GetEffect() const {
192 return _effect;
193 }
194 /** @brief Returns the object label the program was tagged with (the shader name), or an empty string */
195 inline const char* GetObjectLabel() const {
196 return _label.data();
197 }
198 /** @brief Returns `true` if the program is the dithering variant of its effect (derived from the label) */
199 inline bool IsDitherVariant() const {
200 return _ditherVariant;
201 }
202 /** @brief Returns the offline reflection last set on the program (kept for the effects to read) */
203 inline const ShaderCompiler::ProgramVariant* GetReflection() const {
204 return _effectReflection;
205 }
206 /** @brief Returns the imported metadata of the uniform block with the given name, or `nullptr` */
207 const SwUniformBlock* FindBlock(const char* name) const;
208 /** @brief Publishes a committed loose-uniform value pointer for the effects to read */
209 void SetResolvedUniform(const char* name, const std::uint8_t* data);
210 /** @brief Returns the last published value pointer of the named loose uniform, or `nullptr` */
211 const std::uint8_t* ResolveUniform(const char* name) const;
212
213 private:
214 static std::uint32_t _nextHandle;
215
216 std::uint32_t _handle;
217 Status _status;
218 Introspection _introspection;
219 QueryPhase _queryPhase;
220 std::uint32_t _batchSize;
221 bool _shouldLogOnErrors;
222 std::uint32_t _uniformsSize;
223 std::uint32_t _uniformBlocksSize;
224
225 std::vector<SwUniform> _uniforms;
226 std::vector<SwUniformBlock> _uniformBlocks;
227 std::vector<SwAttribute> _attributes;
228
229 const ShaderCompiler::ProgramVariant* _reflection;
230 // Kept after introspection so the effects can read member offsets/texture bindings at draw time
231 const ShaderCompiler::ProgramVariant* _effectReflection;
232 SwEffect _effect;
233 bool _ditherVariant;
234 // The shader name the program was tagged with (used to look up an offline-transpiled generated fragment)
235 String _label;
236
237 SwVertexFormat _vertexFormat;
238 const SwBuffer* _boundVbo;
239 const SwBuffer* _boundIbo;
240
241 struct ResolvedUniform
242 {
243 String Name;
244 const std::uint8_t* Data;
245 };
246 std::vector<ResolvedUniform> _resolvedUniforms;
247
248 void PerformIntrospection();
249 void ImportReflection();
250 };
251 }