| 1 |
#include "GLUniformBlockCache.h" |
| 2 |
#include "GLUniformBlock.h" |
| 3 |
#include "../../../../Main.h" |
| 4 |
|
| 5 |
#include <cstring> |
| 6 |
#include <vector> |
| 7 |
|
| 8 |
namespace nCine::RHI::GL |
| 9 |
{ |
| 10 |
GLUniformBlockCache::GLUniformBlockCache() |
| 11 |
: _uniformBlock(nullptr), _dataPointer(nullptr), _usedSize(0) |
| 12 |
{ |
| 13 |
} |
| 14 |
|
| 15 |
GLUniformBlockCache::GLUniformBlockCache(GLUniformBlock* uniformBlock) |
| 16 |
: _uniformBlock(uniformBlock), _dataPointer(nullptr), _usedSize(0) |
| 17 |
{ |
| 18 |
DEATH_ASSERT(uniformBlock); |
| 19 |
_usedSize = uniformBlock->GetSize(); |
| 20 |
|
| 21 |
static_assert(UniformHashSize >= GLUniformBlock::BlockUniformHashSize, "Uniform cache is smaller than the number of uniforms"); |
| 22 |
|
| 23 |
for (const GLUniform& uniform : uniformBlock->_blockUniforms) { |
| 24 |
GLUniformCache uniformCache(&uniform); |
| 25 |
_uniformCaches[uniform.GetName()] = uniformCache; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
GLuint GLUniformBlockCache::GetIndex() const |
| 30 |
{ |
| 31 |
GLuint index = 0; |
| 32 |
if (_uniformBlock != nullptr) { |
| 33 |
index = _uniformBlock->GetIndex(); |
| 34 |
} |
| 35 |
return index; |
| 36 |
} |
| 37 |
|
| 38 |
GLuint GLUniformBlockCache::GetBindingIndex() const |
| 39 |
{ |
| 40 |
GLuint bindingIndex = 0; |
| 41 |
if (_uniformBlock != nullptr) { |
| 42 |
bindingIndex = _uniformBlock->GetBindingIndex(); |
| 43 |
} |
| 44 |
return bindingIndex; |
| 45 |
} |
| 46 |
|
| 47 |
GLint GLUniformBlockCache::GetSize() const |
| 48 |
{ |
| 49 |
GLint size = 0; |
| 50 |
if (_uniformBlock != nullptr) { |
| 51 |
size = _uniformBlock->GetSize(); |
| 52 |
} |
| 53 |
return size; |
| 54 |
} |
| 55 |
|
| 56 |
std::uint8_t GLUniformBlockCache::GetAlignAmount() const |
| 57 |
{ |
| 58 |
std::uint8_t alignAmount = 0; |
| 59 |
if (_uniformBlock != nullptr) { |
| 60 |
alignAmount = _uniformBlock->GetAlignAmount(); |
| 61 |
} |
| 62 |
return alignAmount; |
| 63 |
} |
| 64 |
|
| 65 |
void GLUniformBlockCache::SetDataPointer(GLubyte* dataPointer) |
| 66 |
{ |
| 67 |
_dataPointer = dataPointer; |
| 68 |
|
| 69 |
for (GLUniformCache& uniformCache : _uniformCaches) { |
| 70 |
uniformCache.SetDataPointer(_dataPointer + uniformCache.GetUniform()->GetOffset()); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void GLUniformBlockCache::SetUsedSize(GLint usedSize) |
| 75 |
{ |
| 76 |
if (usedSize >= 0) { |
| 77 |
_usedSize = usedSize; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
bool GLUniformBlockCache::CopyData(std::uint32_t destIndex, const GLubyte* src, std::uint32_t numBytes) |
| 82 |
{ |
| 83 |
if (destIndex + numBytes > std::uint32_t(GetSize()) || numBytes == 0 || src == nullptr || _dataPointer == nullptr) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
std::memcpy(&_dataPointer[destIndex], src, numBytes); |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
GLUniformCache* GLUniformBlockCache::GetUniform(StringView name) |
| 91 |
{ |
| 92 |
return _uniformCaches.find(String::nullTerminatedView(name)); |
| 93 |
} |
| 94 |
|
| 95 |
void GLUniformBlockCache::SetBlockBinding(GLuint blockBinding) |
| 96 |
{ |
| 97 |
if (_uniformBlock) { |
| 98 |
_uniformBlock->SetBlockBinding(blockBinding); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#if defined(RHI_GL_PROFILE_ES2) |
| 103 |
void GLUniformBlockCache::CommitAsLooseUniforms() |
| 104 |
{ |
| 105 |
// Each member cache points into the host-side block buffer at the member's offset (SetDataPointer); |
| 106 |
// on ES2 the member is a real loose uniform, so push it directly with the matching glUniform* call |
| 107 |
// (the location was resolved in GLShaderProgram::ImportReflection). Mirrors GLUniformCache::CommitValue. |
| 108 |
// |
| 109 |
// The host data uses the std140 layout the reflection computed, but glUniform*v expects tightly packed |
| 110 |
// values. The layouts differ wherever std140 pads to 16-byte boundaries: array elements of scalar / |
| 111 |
// vec2 / vec3 types (stride 16 vs their tight size) and the columns of mat2 / mat3 (column stride 16 |
| 112 |
// vs 8 / 12) - repack those into a scratch buffer before uploading. vec4-sized rows (vec4, ivec4, |
| 113 |
// mat4 columns) are already dense. |
| 114 |
for (GLUniformCache& memberCache : _uniformCaches) { |
| 115 |
const GLUniform* uniform = memberCache.GetUniform(); |
| 116 |
if (uniform == nullptr) { |
| 117 |
continue; |
| 118 |
} |
| 119 |
const GLint location = uniform->GetLocation(); |
| 120 |
const GLubyte* data = memberCache.GetDataPointer(); |
| 121 |
if (location < 0 || data == nullptr) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
const GLsizei count = GLsizei(uniform->GetSize() > 0 ? uniform->GetSize() : 1); |
| 125 |
|
| 126 |
// Rows per element and tight bytes per row: mats are N column-vectors, everything else is 1 row |
| 127 |
std::int32_t rowsPerElement = 1; |
| 128 |
std::int32_t tightRowBytes = 0; |
| 129 |
switch (uniform->GetGLType()) { |
| 130 |
case GL_FLOAT: case GL_INT: tightRowBytes = 4; break; |
| 131 |
case GL_FLOAT_VEC2: case GL_INT_VEC2: tightRowBytes = 8; break; |
| 132 |
case GL_FLOAT_VEC3: case GL_INT_VEC3: tightRowBytes = 12; break; |
| 133 |
case GL_FLOAT_VEC4: case GL_INT_VEC4: tightRowBytes = 16; break; |
| 134 |
case GL_FLOAT_MAT2: rowsPerElement = 2; tightRowBytes = 8; break; |
| 135 |
case GL_FLOAT_MAT3: rowsPerElement = 3; tightRowBytes = 12; break; |
| 136 |
case GL_FLOAT_MAT4: rowsPerElement = 4; tightRowBytes = 16; break; |
| 137 |
default: continue; |
| 138 |
} |
| 139 |
// std140: each row (array element or matrix column) is padded to a vec4 boundary, except that a |
| 140 |
// non-array scalar/vec member is stored at its tight size with no trailing padding to skip |
| 141 |
const bool strided = (tightRowBytes < 16) && (count > 1 || rowsPerElement > 1); |
| 142 |
|
| 143 |
static std::vector<GLubyte> scratch; |
| 144 |
if (strided) { |
| 145 |
const std::int32_t totalRows = count * rowsPerElement; |
| 146 |
scratch.resize(std::size_t(totalRows) * tightRowBytes); |
| 147 |
for (std::int32_t row = 0; row < totalRows; row++) { |
| 148 |
std::memcpy(scratch.data() + std::size_t(row) * tightRowBytes, data + std::size_t(row) * 16, tightRowBytes); |
| 149 |
} |
| 150 |
data = scratch.data(); |
| 151 |
} |
| 152 |
|
| 153 |
const GLfloat* asFloat = reinterpret_cast<const GLfloat*>(data); |
| 154 |
const GLint* asInt = reinterpret_cast<const GLint*>(data); |
| 155 |
switch (uniform->GetGLType()) { |
| 156 |
case GL_FLOAT: glUniform1fv(location, count, asFloat); break; |
| 157 |
case GL_FLOAT_VEC2: glUniform2fv(location, count, asFloat); break; |
| 158 |
case GL_FLOAT_VEC3: glUniform3fv(location, count, asFloat); break; |
| 159 |
case GL_FLOAT_VEC4: glUniform4fv(location, count, asFloat); break; |
| 160 |
case GL_INT: glUniform1iv(location, count, asInt); break; |
| 161 |
case GL_INT_VEC2: glUniform2iv(location, count, asInt); break; |
| 162 |
case GL_INT_VEC3: glUniform3iv(location, count, asInt); break; |
| 163 |
case GL_INT_VEC4: glUniform4iv(location, count, asInt); break; |
| 164 |
case GL_FLOAT_MAT2: glUniformMatrix2fv(location, count, GL_FALSE, asFloat); break; |
| 165 |
case GL_FLOAT_MAT3: glUniformMatrix3fv(location, count, GL_FALSE, asFloat); break; |
| 166 |
case GL_FLOAT_MAT4: glUniformMatrix4fv(location, count, GL_FALSE, asFloat); break; |
| 167 |
default: break; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
#endif |
| 172 |
} |