| 1 |
#pragma once |
| 2 |
|
| 3 |
#ifndef DOXYGEN_GENERATING_OUTPUT |
| 4 |
#define NCINE_INCLUDE_OPENGL |
| 5 |
#include "../../../CommonHeaders.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#include "GLUniform.h" |
| 9 |
#include "GLUniformBlock.h" |
| 10 |
#include "GLAttribute.h" |
| 11 |
#include "GLVertexFormat.h" |
| 12 |
#include "../RhiTypes.h" |
| 13 |
#include "../../../Base/StaticHashMap.h" |
| 14 |
#include "../../../../Main.h" |
| 15 |
|
| 16 |
#include <string> |
| 17 |
|
| 18 |
#include <Containers/SmallVector.h> |
| 19 |
|
| 20 |
using namespace Death::Containers; |
| 21 |
|
| 22 |
namespace ShaderCompiler |
| 23 |
{ |
| 24 |
struct ProgramVariant; |
| 25 |
} |
| 26 |
|
| 27 |
namespace nCine::RHI::GL |
| 28 |
{ |
| 29 |
class GLShader; |
| 30 |
|
| 31 |
/** |
| 32 |
@brief Wraps an OpenGL shader program object |
| 33 |
|
| 34 |
Manages the lifetime of a shader program, attaches @ref GLShader objects, links and optionally |
| 35 |
validates them, and binds the program for rendering (caching the currently bound program to |
| 36 |
avoid redundant `glUseProgram()` calls). After linking it can introspect the program to discover |
| 37 |
active uniforms, uniform blocks and vertex attributes, and to build the associated vertex format. |
| 38 |
Compilation and linking queries can be performed immediately or deferred until the program is first used. |
| 39 |
*/ |
| 40 |
class GLShaderProgram |
| 41 |
{ |
| 42 |
friend class GLShaderUniforms; |
| 43 |
friend class GLShaderUniformBlocks; |
| 44 |
|
| 45 |
public: |
| 46 |
/** @brief How much of the program is introspected after linking */ |
| 47 |
enum class Introspection |
| 48 |
{ |
| 49 |
Enabled, /**< Discover all uniforms, including those inside uniform blocks */ |
| 50 |
NoUniformsInBlocks, /**< Discover uniforms and uniform blocks, but not the uniforms inside the blocks */ |
| 51 |
Disabled /**< Perform no introspection */ |
| 52 |
}; |
| 53 |
|
| 54 |
/** @brief Lifecycle status of the shader program */ |
| 55 |
enum class Status |
| 56 |
{ |
| 57 |
NotLinked, /**< Not linked yet */ |
| 58 |
CompilationFailed, /**< Compilation of an attached shader failed */ |
| 59 |
LinkingFailed, /**< Linking failed */ |
| 60 |
Linked, /**< Linked successfully */ |
| 61 |
LinkedWithDeferredQueries, /**< Linked, but the status checks and introspection are still pending */ |
| 62 |
LinkedWithIntrospection /**< Linked and introspected */ |
| 63 |
}; |
| 64 |
|
| 65 |
/** @brief When compilation and linking status are queried */ |
| 66 |
enum class QueryPhase |
| 67 |
{ |
| 68 |
Immediate, /**< Status is checked and introspection is performed right after linking */ |
| 69 |
Deferred /**< Status checks and introspection are postponed until the program is first used */ |
| 70 |
}; |
| 71 |
|
| 72 |
/** @brief Default batch size, indicating the shader is not batched */ |
| 73 |
static constexpr std::int32_t DefaultBatchSize = -1; |
| 74 |
|
| 75 |
GLShaderProgram(); |
| 76 |
explicit GLShaderProgram(QueryPhase queryPhase); |
| 77 |
GLShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase); |
| 78 |
GLShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection); |
| 79 |
GLShaderProgram(StringView vertexFile, StringView fragmentFile); |
| 80 |
~GLShaderProgram(); |
| 81 |
|
| 82 |
GLShaderProgram(const GLShaderProgram&) = delete; |
| 83 |
GLShaderProgram& operator=(const GLShaderProgram&) = delete; |
| 84 |
|
| 85 |
/** @brief Returns the OpenGL handle of the shader program object */ |
| 86 |
inline GLuint GetGLHandle() const { |
| 87 |
return _glHandle; |
| 88 |
} |
| 89 |
/** @brief Returns a backend-neutral identifier uniquely identifying the program (feeds material sort keys) */ |
| 90 |
inline std::uint32_t GetUniqueId() const { |
| 91 |
return _glHandle; |
| 92 |
} |
| 93 |
/** @brief Returns the current lifecycle status */ |
| 94 |
inline Status GetStatus() const { |
| 95 |
return _status; |
| 96 |
} |
| 97 |
/** @brief Returns the introspection level used when linking */ |
| 98 |
inline Introspection GetIntrospection() const { |
| 99 |
return _introspection; |
| 100 |
} |
| 101 |
/** @brief Returns when compilation and linking status are queried */ |
| 102 |
inline QueryPhase GetQueryPhase() const { |
| 103 |
return _queryPhase; |
| 104 |
} |
| 105 |
/** @brief Returns the batch size, or @ref DefaultBatchSize if the shader is not batched */ |
| 106 |
inline std::uint32_t GetBatchSize() const { |
| 107 |
return _batchSize; |
| 108 |
} |
| 109 |
/** @brief Sets the batch size used for batched rendering */ |
| 110 |
inline void SetBatchSize(std::uint32_t value) { |
| 111 |
_batchSize = value; |
| 112 |
} |
| 113 |
|
| 114 |
/** @brief Returns `true` if the program has been linked successfully */ |
| 115 |
bool IsLinked() const; |
| 116 |
|
| 117 |
/** @brief Returns the length of the information log, including the null terminator */ |
| 118 |
std::uint32_t RetrieveInfoLogLength() const; |
| 119 |
/** |
| 120 |
* @brief Retrieves the information log |
| 121 |
* |
| 122 |
* Copies the program information log into the provided string, up to its current capacity. |
| 123 |
*/ |
| 124 |
void RetrieveInfoLog(std::string& infoLog) const; |
| 125 |
|
| 126 |
/** @brief Returns the total memory needed for all uniforms outside of blocks */ |
| 127 |
inline std::uint32_t GetUniformsSize() const { |
| 128 |
return _uniformsSize; |
| 129 |
} |
| 130 |
/** @brief Returns the total memory needed for all uniforms inside of blocks */ |
| 131 |
inline std::uint32_t GetUniformBlocksSize() const { |
| 132 |
return _uniformBlocksSize; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @brief Attaches a shader stage compiled from the specified file |
| 137 |
* |
| 138 |
* @param stage The programmable stage the source targets |
| 139 |
* @param filename Path of the file containing the shader source |
| 140 |
* @return `true` if the shader compiled successfully (or when checks are deferred) |
| 141 |
*/ |
| 142 |
bool AttachShaderFromFile(ShaderStage stage, StringView filename); |
| 143 |
/** |
| 144 |
* @brief Attaches a shader stage compiled from the specified source string |
| 145 |
* |
| 146 |
* @param stage The programmable stage the source targets |
| 147 |
* @param string The shader source |
| 148 |
* @return `true` if the shader compiled successfully (or when checks are deferred) |
| 149 |
*/ |
| 150 |
bool AttachShaderFromString(ShaderStage stage, StringView string); |
| 151 |
/** |
| 152 |
* @brief Attaches a shader stage compiled from the specified source strings |
| 153 |
* |
| 154 |
* @param stage The programmable stage the source targets |
| 155 |
* @param strings The shader source fragments, concatenated in order |
| 156 |
* @return `true` if the shader compiled successfully (or when checks are deferred) |
| 157 |
*/ |
| 158 |
bool AttachShaderFromStrings(ShaderStage stage, ArrayView<const StringView> strings); |
| 159 |
/** |
| 160 |
* @brief Attaches a shader stage compiled from the specified source strings and file |
| 161 |
* |
| 162 |
* @param stage The programmable stage the source targets |
| 163 |
* @param strings The shader source fragments, concatenated in order |
| 164 |
* @param filename Path of a file whose source is appended after the strings |
| 165 |
* @return `true` if the shader compiled successfully (or when checks are deferred) |
| 166 |
*/ |
| 167 |
bool AttachShaderFromStringsAndFile(ShaderStage stage, ArrayView<const StringView> strings, StringView filename); |
| 168 |
/** |
| 169 |
* @brief Sets offline reflection data to be used instead of GL introspection after linking |
| 170 |
* |
| 171 |
* When set before @ref Link() (or @ref FinalizeAfterLinking()), uniforms, uniform blocks and |
| 172 |
* attributes are imported from the ShaderCompiler reflection with targeted location queries only, |
| 173 |
* instead of being enumerated with `glGetActiveUniform*()`. The pointer must stay valid until |
| 174 |
* introspection runs (it is consumed and cleared afterwards, and cleared by @ref Reset()). |
| 175 |
*/ |
| 176 |
inline void SetReflection(const ShaderCompiler::ProgramVariant* reflection) { |
| 177 |
_reflection = reflection; |
| 178 |
} |
| 179 |
/** |
| 180 |
@brief Records the true (program, variant) identity of the loaded shader |
| 181 |
|
| 182 |
Only the fixed-function console backends consume it (they resolve their generated |
| 183 |
effect tables from this identity instead of the object label); this backend runs |
| 184 |
real shaders and ignores it. |
| 185 |
*/ |
| 186 |
inline void SetProgramIdentity(const char* programName, const char* variantName) { |
| 187 |
static_cast<void>(programName); |
| 188 |
static_cast<void>(variantName); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* @brief Links the attached shaders into the program |
| 194 |
* |
| 195 |
* @param introspection The introspection level to use after linking |
| 196 |
* @return `true` on success, or when the status check is deferred |
| 197 |
*/ |
| 198 |
bool Link(Introspection introspection); |
| 199 |
/** |
| 200 |
* @brief Binds the program for rendering |
| 201 |
* |
| 202 |
* Processes any deferred queries on first use and calls `glUseProgram()`, skipping it if the |
| 203 |
* program is already bound. |
| 204 |
*/ |
| 205 |
void Use(); |
| 206 |
/** @brief Validates the program and returns `true` if validation succeeded */ |
| 207 |
bool Validate(); |
| 208 |
|
| 209 |
/** |
| 210 |
* @brief Finalizes the program after an external link step |
| 211 |
* |
| 212 |
* When queries are immediate, checks the link status, detaches the shader objects and performs |
| 213 |
* introspection; otherwise marks the program for deferred queries. |
| 214 |
* |
| 215 |
* @param introspection The introspection level to use |
| 216 |
* @return `true` on success, or when the status check is deferred |
| 217 |
*/ |
| 218 |
bool FinalizeAfterLinking(Introspection introspection); |
| 219 |
|
| 220 |
/** @brief Returns the number of active vertex attributes discovered by introspection */ |
| 221 |
inline std::uint32_t GetAttributeCount() const { |
| 222 |
return _attributeLocations.size(); |
| 223 |
} |
| 224 |
/** @brief Returns `true` if the program has an active vertex attribute with the given name */ |
| 225 |
inline bool HasAttribute(const char* name) const { |
| 226 |
return (_attributeLocations.find(String::nullTerminatedView(name)) != nullptr); |
| 227 |
} |
| 228 |
/** @brief Returns the vertex format attribute with the given name, or `nullptr` if not found */ |
| 229 |
GLVertexFormat::Attribute* GetAttribute(const char* name); |
| 230 |
|
| 231 |
/** @brief Defines the vertex format from a vertex buffer object */ |
| 232 |
inline void DefineVertexFormat(const GLBufferObject* vbo) { |
| 233 |
DefineVertexFormat(vbo, nullptr, 0); |
| 234 |
} |
| 235 |
/** @brief Defines the vertex format from a vertex buffer object and an index buffer object */ |
| 236 |
inline void DefineVertexFormat(const GLBufferObject* vbo, const GLBufferObject* ibo) { |
| 237 |
DefineVertexFormat(vbo, ibo, 0); |
| 238 |
} |
| 239 |
/** |
| 240 |
* @brief Defines the vertex format from the given buffer objects |
| 241 |
* |
| 242 |
* Assigns the vertex and index buffers to the introspected attributes and binds a matching VAO. |
| 243 |
* |
| 244 |
* @param vbo The vertex buffer object providing attribute data |
| 245 |
* @param ibo The index buffer object, or `nullptr` if unused |
| 246 |
* @param vboOffset Base offset into the vertex buffer applied to every attribute |
| 247 |
*/ |
| 248 |
void DefineVertexFormat(const GLBufferObject* vbo, const GLBufferObject* ibo, std::uint32_t vboOffset); |
| 249 |
|
| 250 |
/** |
| 251 |
* @brief Resets the program so that new shaders can be attached |
| 252 |
* |
| 253 |
* Clears the introspection state, deletes the current OpenGL program and creates a fresh one. |
| 254 |
*/ |
| 255 |
void Reset(); |
| 256 |
|
| 257 |
/** @brief Sets an OpenGL object label for the program, for debugging */ |
| 258 |
void SetObjectLabel(StringView label); |
| 259 |
|
| 260 |
/** @brief Returns the automatic log on errors flag */ |
| 261 |
inline bool GetLogOnErrors() const { |
| 262 |
return _shouldLogOnErrors; |
| 263 |
} |
| 264 |
/** |
| 265 |
* @brief Sets the automatic log on errors flag |
| 266 |
* |
| 267 |
* When `true`, the shader program automatically logs compilation and linking errors. |
| 268 |
*/ |
| 269 |
inline void SetLogOnErrors(bool shouldLogOnErrors) { |
| 270 |
_shouldLogOnErrors = shouldLogOnErrors; |
| 271 |
} |
| 272 |
|
| 273 |
/** @brief Drops the cached bound program so the next @ref Use() re-applies (external code changed it) */ |
| 274 |
static void InvalidateCachedBinding() { |
| 275 |
_boundProgram = ~0u; |
| 276 |
} |
| 277 |
|
| 278 |
private: |
| 279 |
/** @brief Maximum number of discoverable uniforms outside of blocks */ |
| 280 |
static constexpr std::uint32_t MaxNumUniforms = 32; |
| 281 |
|
| 282 |
static constexpr std::int32_t AttachedShadersInitialSize = 2; |
| 283 |
static constexpr std::int32_t UniformsInitialSize = 8; |
| 284 |
static constexpr std::int32_t UniformBlocksInitialSize = 4; |
| 285 |
static constexpr std::int32_t AttributesInitialSize = 4; |
| 286 |
|
| 287 |
static GLuint _boundProgram; |
| 288 |
|
| 289 |
GLuint _glHandle; |
| 290 |
|
| 291 |
SmallVector<std::unique_ptr<GLShader>, AttachedShadersInitialSize> _attachedShaders; |
| 292 |
Status _status; |
| 293 |
Introspection _introspection; |
| 294 |
QueryPhase _queryPhase; |
| 295 |
std::uint32_t _batchSize; |
| 296 |
|
| 297 |
/** @brief Whether the shader program should automatically log errors (the information log) */ |
| 298 |
bool _shouldLogOnErrors; |
| 299 |
|
| 300 |
std::uint32_t _uniformsSize; |
| 301 |
std::uint32_t _uniformBlocksSize; |
| 302 |
|
| 303 |
SmallVector<GLUniform, 0> _uniforms; |
| 304 |
SmallVector<GLUniformBlock, 0> _uniformBlocks; |
| 305 |
SmallVector<GLAttribute, 0> _attributes; |
| 306 |
|
| 307 |
/** @brief Offline reflection used instead of GL introspection, consumed by PerformIntrospection() */ |
| 308 |
const ShaderCompiler::ProgramVariant* _reflection; |
| 309 |
|
| 310 |
StaticHashMap<String, std::int32_t, GLVertexFormat::MaxAttributes> _attributeLocations; |
| 311 |
GLVertexFormat _vertexFormat; |
| 312 |
|
| 313 |
bool ProcessDeferredQueries(); |
| 314 |
bool CheckLinking(); |
| 315 |
void PerformIntrospection(); |
| 316 |
|
| 317 |
void ImportReflection(); |
| 318 |
void DiscoverUniforms(); |
| 319 |
void DiscoverUniformBlocks(GLUniformBlock::DiscoverUniforms discover); |
| 320 |
void DiscoverAttributes(); |
| 321 |
void InitVertexFormat(); |
| 322 |
}; |
| 323 |
} |