| 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 "../../../Base/StaticHashMapIterator.h" |
| 10 |
|
| 11 |
namespace nCine::RHI::GL |
| 12 |
{ |
| 13 |
class GLShaderProgram; |
| 14 |
|
| 15 |
/** |
| 16 |
@brief Stores information about an active uniform block |
| 17 |
|
| 18 |
Holds the metadata queried for a single active uniform block of a linked shader program, |
| 19 |
such as its index, byte size (padded to the uniform buffer offset alignment), current |
| 20 |
binding index and the @ref GLUniform members it contains. Member uniforms are discovered |
| 21 |
only when @ref DiscoverUniforms::Enabled is requested. |
| 22 |
*/ |
| 23 |
class GLUniformBlock |
| 24 |
{ |
| 25 |
friend class GLUniformBlockCache; |
| 26 |
friend class GLShaderProgram; |
| 27 |
|
| 28 |
public: |
| 29 |
/** @brief Maximum length of a uniform block name, including the terminating null character */ |
| 30 |
static constexpr std::uint32_t MaxNameLength = 48; |
| 31 |
|
| 32 |
/** @brief Whether the member uniforms of a block should be queried on construction */ |
| 33 |
enum class DiscoverUniforms |
| 34 |
{ |
| 35 |
Enabled, |
| 36 |
Disabled |
| 37 |
}; |
| 38 |
|
| 39 |
/** @brief Creates an empty, unbound uniform block */ |
| 40 |
GLUniformBlock(); |
| 41 |
/** @brief Queries the active uniform block at the specified index, discovering members as requested */ |
| 42 |
GLUniformBlock(GLuint program, GLuint blockIndex, DiscoverUniforms discover); |
| 43 |
/** @brief Queries the active uniform block at the specified index, discovering its member uniforms */ |
| 44 |
GLUniformBlock(GLuint program, GLuint blockIndex); |
| 45 |
/** @brief Creates a uniform block from offline reflection data (name, queried index and unaligned data size), without member discovery */ |
| 46 |
GLUniformBlock(GLuint program, const char* name, GLuint blockIndex, GLint dataSize); |
| 47 |
|
| 48 |
/** @brief Returns the active uniform block index within the program */ |
| 49 |
inline GLuint GetIndex() const { |
| 50 |
return _index; |
| 51 |
} |
| 52 |
/** @brief Returns the current binding index of the block, or -1 if not bound */ |
| 53 |
inline GLint GetBindingIndex() const { |
| 54 |
return _bindingIndex; |
| 55 |
} |
| 56 |
/** @brief Returns the block size in bytes, padded to the uniform buffer offset alignment */ |
| 57 |
inline GLint GetSize() const { |
| 58 |
return _size; |
| 59 |
} |
| 60 |
/** @brief Returns the number of padding bytes added to the original size for offset alignment */ |
| 61 |
inline std::uint8_t GetAlignAmount() const { |
| 62 |
return _alignAmount; |
| 63 |
} |
| 64 |
/** @brief Returns the uniform block name */ |
| 65 |
inline const char* GetName() const { |
| 66 |
return _name; |
| 67 |
} |
| 68 |
|
| 69 |
/** @brief Returns the member uniform with the specified name, or `nullptr` if not found */ |
| 70 |
inline GLUniform* GetUniform(const char* name) { |
| 71 |
return _blockUniforms.find(String::nullTerminatedView(name)); |
| 72 |
} |
| 73 |
/** @brief Sets the binding index of the block, skipping the GL call if it is unchanged */ |
| 74 |
void SetBlockBinding(GLuint blockBinding); |
| 75 |
|
| 76 |
private: |
| 77 |
// Max number of discoverable uniforms per block |
| 78 |
static const std::int32_t MaxNumBlockUniforms = 16; |
| 79 |
|
| 80 |
static const std::uint32_t BlockUniformHashSize = 8; |
| 81 |
StaticHashMap<String, GLUniform, BlockUniformHashSize> _blockUniforms; |
| 82 |
|
| 83 |
GLuint _program; |
| 84 |
GLuint _index; |
| 85 |
// Offset aligned size for `glBindBufferRange()` calls |
| 86 |
GLint _size; |
| 87 |
// Uniform buffer offset alignment added to `_size` |
| 88 |
std::uint8_t _alignAmount; |
| 89 |
// Current binding index for the uniform block. Negative if not bound. |
| 90 |
GLint _bindingIndex; |
| 91 |
char _name[MaxNameLength]; |
| 92 |
}; |
| 93 |
} |