| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "GuShaderTypes.h" |
| 4 |
#include "GuVertexFormat.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::GU |
| 23 |
{ |
| 24 |
class GuShaderUniforms; |
| 25 |
class GuShaderUniformBlocks; |
| 26 |
struct FixedFunctionGeneratedEffect; |
| 27 |
|
| 28 |
/** |
| 29 |
@brief Shader program of the GU 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 GuShaderProgram |
| 40 |
{ |
| 41 |
friend class GuShaderUniforms; |
| 42 |
friend class GuShaderUniformBlocks; |
| 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 |
GuShaderProgram(); |
| 72 |
explicit GuShaderProgram(QueryPhase queryPhase); |
| 73 |
GuShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase); |
| 74 |
GuShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection); |
| 75 |
GuShaderProgram(StringView vertexFile, StringView fragmentFile); |
| 76 |
~GuShaderProgram(); |
| 77 |
|
| 78 |
GuShaderProgram(const GuShaderProgram&) = delete; |
| 79 |
GuShaderProgram& operator=(const GuShaderProgram&) = 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 |
GuVertexFormat::Attribute* GetAttribute(const char* name); |
| 148 |
|
| 149 |
inline void DefineVertexFormat(const GuBuffer* vbo) { |
| 150 |
DefineVertexFormat(vbo, nullptr, 0); |
| 151 |
} |
| 152 |
inline void DefineVertexFormat(const GuBuffer* vbo, const GuBuffer* ibo) { |
| 153 |
DefineVertexFormat(vbo, ibo, 0); |
| 154 |
} |
| 155 |
void DefineVertexFormat(const GuBuffer* vbo, const GuBuffer* ibo, std::uint32_t vboOffset); |
| 156 |
/** @brief Returns the vertex buffer last bound by @ref DefineVertexFormat(), or `nullptr` */ |
| 157 |
inline const GuBuffer* 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 |
// -- GU 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 GuUniformBlock* 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 published `uProjectionMatrix` value without the by-name scan (it runs per draw) */ |
| 219 |
inline const std::uint8_t* GetResolvedProjection() const { |
| 220 |
return _resolvedProjection; |
| 221 |
} |
| 222 |
/** @brief Returns the published `uViewMatrix` value without the by-name scan (it runs per draw) */ |
| 223 |
inline const std::uint8_t* GetResolvedView() const { |
| 224 |
return _resolvedView; |
| 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<GuUniform> _uniforms; |
| 240 |
std::vector<GuUniformBlock> _uniformBlocks; |
| 241 |
std::vector<GuAttribute> _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 |
GuVertexFormat _vertexFormat; |
| 260 |
const GuBuffer* _boundVbo; |
| 261 |
std::uint32_t _boundVboOffset; |
| 262 |
const GuBuffer* _boundIbo; |
| 263 |
|
| 264 |
struct ResolvedUniform |
| 265 |
{ |
| 266 |
String Name; |
| 267 |
const std::uint8_t* Data; |
| 268 |
}; |
| 269 |
std::vector<ResolvedUniform> _resolvedUniforms; |
| 270 |
// The two names every dispatch reads are kept as direct pointers, bypassing the by-name scan |
| 271 |
const std::uint8_t* _resolvedProjection = nullptr; |
| 272 |
const std::uint8_t* _resolvedView = nullptr; |
| 273 |
|
| 274 |
void PerformIntrospection(); |
| 275 |
void ImportReflection(); |
| 276 |
}; |
| 277 |
} |