| 1 |
#pragma once |
| 2 |
|
| 3 |
#ifndef DOXYGEN_GENERATING_OUTPUT |
| 4 |
#define NCINE_INCLUDE_OPENGL |
| 5 |
#include "../../../CommonHeaders.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#include "../../../../Main.h" |
| 9 |
#include "../../../../Shaders/Generated/ShaderCompilerTypes.h" |
| 10 |
|
| 11 |
namespace nCine::RHI::GL |
| 12 |
{ |
| 13 |
/** |
| 14 |
@brief Stores information about an active shader uniform |
| 15 |
|
| 16 |
Holds the metadata queried for a single active uniform of a linked shader program, |
| 17 |
such as its location, array size, GL type, name and (when the uniform lives inside a |
| 18 |
uniform block) its owning block index and byte offset. It carries no value storage of |
| 19 |
its own and is mainly used to initialize a @ref GLUniformCache. |
| 20 |
*/ |
| 21 |
class GLUniform |
| 22 |
{ |
| 23 |
friend class GLUniformBlock; |
| 24 |
friend class GLShaderProgram; |
| 25 |
|
| 26 |
public: |
| 27 |
/** @brief Maximum length of a uniform name, including the terminating null character */ |
| 28 |
static constexpr std::uint32_t MaxNameLength = 48; |
| 29 |
|
| 30 |
/** @brief Creates an empty, unbound uniform */ |
| 31 |
GLUniform(); |
| 32 |
/** @brief Queries the active uniform at the specified index of a linked program */ |
| 33 |
GLUniform(GLuint program, GLuint index); |
| 34 |
/** @brief Creates a uniform from offline reflection data, querying only the location */ |
| 35 |
GLUniform(GLuint program, const char* name, GLenum type, GLint arraySize); |
| 36 |
|
| 37 |
/** @brief Returns the active uniform index within the program */ |
| 38 |
inline GLuint GetIndex() const { |
| 39 |
return _index; |
| 40 |
} |
| 41 |
/** @brief Returns the index of the owning uniform block, or -1 if the uniform is not in a block */ |
| 42 |
inline GLint GetBlockIndex() const { |
| 43 |
return _blockIndex; |
| 44 |
} |
| 45 |
/** @brief Returns the uniform location, or -1 if it has no location (e.g., block members) */ |
| 46 |
inline GLint GetLocation() const { |
| 47 |
return _location; |
| 48 |
} |
| 49 |
/** @brief Returns the number of array elements (1 for non-array uniforms) */ |
| 50 |
inline GLint GetSize() const { |
| 51 |
return _size; |
| 52 |
} |
| 53 |
/** @brief Returns the native GL type enum of the uniform (e.g., `GL_FLOAT_VEC4`) */ |
| 54 |
inline GLenum GetGLType() const { |
| 55 |
return _type; |
| 56 |
} |
| 57 |
/** @brief Returns the reflected (backend-neutral) type of the uniform, converted from the native GL type enum */ |
| 58 |
ShaderCompiler::UniformType GetType() const; |
| 59 |
/** @brief Returns the byte offset of the uniform within its uniform block */ |
| 60 |
inline GLint GetOffset() const { |
| 61 |
return _offset; |
| 62 |
} |
| 63 |
/** @brief Returns the uniform name */ |
| 64 |
inline const char* GetName() const { |
| 65 |
return _name; |
| 66 |
} |
| 67 |
/** @brief Returns the basic component type (`GL_FLOAT`, `GL_INT` or `GL_BOOL`) derived from the GL type */ |
| 68 |
GLenum GetBasicType() const; |
| 69 |
/** @brief Returns the number of components of the uniform type (e.g., 4 for a `vec4`, 16 for a `mat4`) */ |
| 70 |
std::uint32_t GetComponentCount() const; |
| 71 |
/** @brief Returns the size in bytes occupied by the uniform value */ |
| 72 |
inline std::uint32_t GetMemorySize() const { |
| 73 |
return GetSize() * GetComponentCount() * sizeof(GetBasicType()); |
| 74 |
} |
| 75 |
|
| 76 |
/** @brief Returns `true` if the uniform name starts with the reserved `gl_` prefix */ |
| 77 |
bool HasReservedPrefix() const; |
| 78 |
|
| 79 |
private: |
| 80 |
GLuint _index; |
| 81 |
// Active uniforms not in a block have a block index of -1 |
| 82 |
GLint _blockIndex; |
| 83 |
GLint _location; |
| 84 |
GLint _size; |
| 85 |
GLenum _type; |
| 86 |
GLint _offset; |
| 87 |
char _name[MaxNameLength]; |
| 88 |
}; |
| 89 |
|
| 90 |
} |