| 1 |
#include "GLShaderProgram.h" |
| 2 |
#include "GLShader.h" |
| 3 |
#include "GLDebug.h" |
| 4 |
#include "../../BinaryShaderCache.h" |
| 5 |
#include "../../RenderResources.h" |
| 6 |
#include "../../RenderVaoPool.h" |
| 7 |
#include "../IRhiCapabilities.h" |
| 8 |
#include "../../../ServiceLocator.h" |
| 9 |
#include "../../../Base/StaticHashMapIterator.h" |
| 10 |
#include "../../../tracy.h" |
| 11 |
#include "../../../../Shaders/Generated/ShaderCompilerTypes.h" |
| 12 |
|
| 13 |
#include <cstring> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
namespace nCine::RHI::GL |
| 17 |
{ |
| 18 |
namespace |
| 19 |
{ |
| 20 |
GLenum UniformTypeToGL(ShaderCompiler::UniformType type) |
| 21 |
{ |
| 22 |
switch (type) { |
| 23 |
case ShaderCompiler::UniformType::Float: return GL_FLOAT; |
| 24 |
case ShaderCompiler::UniformType::Int: return GL_INT; |
| 25 |
case ShaderCompiler::UniformType::UInt: return GL_UNSIGNED_INT; |
| 26 |
#if !defined(DEATH_TARGET_VITA) |
| 27 |
case ShaderCompiler::UniformType::Bool: return GL_BOOL; |
| 28 |
#endif |
| 29 |
case ShaderCompiler::UniformType::Vec2: return GL_FLOAT_VEC2; |
| 30 |
case ShaderCompiler::UniformType::Vec3: return GL_FLOAT_VEC3; |
| 31 |
case ShaderCompiler::UniformType::Vec4: return GL_FLOAT_VEC4; |
| 32 |
case ShaderCompiler::UniformType::IVec2: return GL_INT_VEC2; |
| 33 |
case ShaderCompiler::UniformType::IVec3: return GL_INT_VEC3; |
| 34 |
case ShaderCompiler::UniformType::IVec4: return GL_INT_VEC4; |
| 35 |
#if !defined(DEATH_TARGET_VITA) // vitaGL declares none of the bool / unsigned-int vector types |
| 36 |
case ShaderCompiler::UniformType::UVec2: return GL_UNSIGNED_INT_VEC2; |
| 37 |
case ShaderCompiler::UniformType::UVec3: return GL_UNSIGNED_INT_VEC3; |
| 38 |
case ShaderCompiler::UniformType::UVec4: return GL_UNSIGNED_INT_VEC4; |
| 39 |
case ShaderCompiler::UniformType::BVec2: return GL_BOOL_VEC2; |
| 40 |
case ShaderCompiler::UniformType::BVec3: return GL_BOOL_VEC3; |
| 41 |
case ShaderCompiler::UniformType::BVec4: return GL_BOOL_VEC4; |
| 42 |
#endif |
| 43 |
case ShaderCompiler::UniformType::Mat2: return GL_FLOAT_MAT2; |
| 44 |
case ShaderCompiler::UniformType::Mat3: return GL_FLOAT_MAT3; |
| 45 |
case ShaderCompiler::UniformType::Mat4: return GL_FLOAT_MAT4; |
| 46 |
case ShaderCompiler::UniformType::Sampler2D: return GL_SAMPLER_2D; |
| 47 |
#if !defined(DEATH_TARGET_VITA) // GL_SAMPLER_3D is not declared by vitaGL |
| 48 |
case ShaderCompiler::UniformType::Sampler3D: return GL_SAMPLER_3D; |
| 49 |
#endif |
| 50 |
case ShaderCompiler::UniformType::SamplerCube: return GL_SAMPLER_CUBE; |
| 51 |
default: |
| 52 |
LOGW("No available case to handle reflected type: {}", std::uint32_t(type)); |
| 53 |
return GL_FLOAT; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
GLuint GLShaderProgram::_boundProgram = 0; |
| 59 |
|
| 60 |
GLShaderProgram::GLShaderProgram() |
| 61 |
: GLShaderProgram(QueryPhase::Immediate) |
| 62 |
{ |
| 63 |
} |
| 64 |
|
| 65 |
GLShaderProgram::GLShaderProgram(QueryPhase queryPhase) |
| 66 |
: _glHandle(0), _status(Status::NotLinked), _introspection(Introspection::Disabled), _queryPhase(queryPhase), _batchSize(DefaultBatchSize), _shouldLogOnErrors(true), _uniformsSize(0), _uniformBlocksSize(0), _reflection(nullptr) |
| 67 |
{ |
| 68 |
_glHandle = glCreateProgram(); |
| 69 |
|
| 70 |
_attachedShaders.reserve(AttachedShadersInitialSize); |
| 71 |
_uniforms.reserve(UniformsInitialSize); |
| 72 |
_uniformBlocks.reserve(UniformBlocksInitialSize); |
| 73 |
_attributes.reserve(AttributesInitialSize); |
| 74 |
|
| 75 |
#if defined(RHI_GL_PROFILE_ES2) |
| 76 |
// GL_PROGRAM_BINARY_RETRIEVABLE_HINT (and glProgramParameteri) is ES 3.0; the ES2-era |
| 77 |
// OES_get_program_binary path retrieves binaries without a hint |
| 78 |
#else |
| 79 |
if (RenderResources::GetBinaryShaderCache().IsAvailable()) { |
| 80 |
glProgramParameteri(_glHandle, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); |
| 81 |
} |
| 82 |
#endif |
| 83 |
} |
| 84 |
|
| 85 |
GLShaderProgram::GLShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase) |
| 86 |
: GLShaderProgram(queryPhase) |
| 87 |
{ |
| 88 |
const bool hasCompiledVS = AttachShaderFromFile(ShaderStage::Vertex, vertexFile); |
| 89 |
const bool hasCompiledFS = AttachShaderFromFile(ShaderStage::Fragment, fragmentFile); |
| 90 |
|
| 91 |
if (hasCompiledVS && hasCompiledFS) { |
| 92 |
Link(introspection); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
GLShaderProgram::GLShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection) |
| 97 |
: GLShaderProgram(vertexFile, fragmentFile, introspection, QueryPhase::Immediate) |
| 98 |
{ |
| 99 |
} |
| 100 |
|
| 101 |
GLShaderProgram::GLShaderProgram(StringView vertexFile, StringView fragmentFile) |
| 102 |
: GLShaderProgram(vertexFile, fragmentFile, Introspection::Enabled, QueryPhase::Immediate) |
| 103 |
{ |
| 104 |
} |
| 105 |
|
| 106 |
GLShaderProgram::~GLShaderProgram() |
| 107 |
{ |
| 108 |
if (_boundProgram == _glHandle) { |
| 109 |
glUseProgram(0); |
| 110 |
} |
| 111 |
|
| 112 |
glDeleteProgram(_glHandle); |
| 113 |
|
| 114 |
#if !defined(WITH_RHI_SOFTWARE) |
| 115 |
// The render pipeline is built against a single backend's `RHI::` aliases; when the software |
| 116 |
// backend is selected these GL-typed pipeline calls do not apply (this file is dead code there) |
| 117 |
RenderResources::RemoveCameraUniformData(this); |
| 118 |
#endif |
| 119 |
} |
| 120 |
|
| 121 |
bool GLShaderProgram::IsLinked() const |
| 122 |
{ |
| 123 |
return (_status == Status::Linked || |
| 124 |
_status == Status::LinkedWithDeferredQueries || |
| 125 |
_status == Status::LinkedWithIntrospection); |
| 126 |
} |
| 127 |
|
| 128 |
std::uint32_t GLShaderProgram::RetrieveInfoLogLength() const |
| 129 |
{ |
| 130 |
GLint length = 0; |
| 131 |
glGetProgramiv(_glHandle, GL_INFO_LOG_LENGTH, &length); |
| 132 |
return static_cast<std::uint32_t>(length); |
| 133 |
} |
| 134 |
|
| 135 |
void GLShaderProgram::RetrieveInfoLog(std::string& infoLog) const |
| 136 |
{ |
| 137 |
GLint length = 0; |
| 138 |
glGetProgramiv(_glHandle, GL_INFO_LOG_LENGTH, &length); |
| 139 |
|
| 140 |
if (length > 0 && infoLog.capacity() > 0) { |
| 141 |
const std::uint32_t capacity = (std::uint32_t)infoLog.capacity(); |
| 142 |
glGetProgramInfoLog(_glHandle, capacity, &length, infoLog.data()); |
| 143 |
infoLog.resize(static_cast<std::uint32_t>(length) < capacity - 1 ? static_cast<std::uint32_t>(length) : capacity - 1); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
bool GLShaderProgram::AttachShaderFromFile(ShaderStage stage, StringView filename) |
| 148 |
{ |
| 149 |
return AttachShaderFromStringsAndFile(stage, nullptr, filename); |
| 150 |
} |
| 151 |
|
| 152 |
bool GLShaderProgram::AttachShaderFromString(ShaderStage stage, StringView string) |
| 153 |
{ |
| 154 |
return AttachShaderFromStringsAndFile(stage, arrayView({ string }), {}); |
| 155 |
} |
| 156 |
|
| 157 |
bool GLShaderProgram::AttachShaderFromStrings(ShaderStage stage, ArrayView<const StringView> strings) |
| 158 |
{ |
| 159 |
return AttachShaderFromStringsAndFile(stage, strings, {}); |
| 160 |
} |
| 161 |
|
| 162 |
bool GLShaderProgram::AttachShaderFromStringsAndFile(ShaderStage stage, ArrayView<const StringView> strings, StringView filename) |
| 163 |
{ |
| 164 |
std::unique_ptr<GLShader> shader = std::make_unique<GLShader>(stage == ShaderStage::Vertex ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER); |
| 165 |
shader->LoadFromStringsAndFile(strings, filename); |
| 166 |
glAttachShader(_glHandle, shader->GetGLHandle()); |
| 167 |
|
| 168 |
const GLShader::ErrorChecking errorChecking = (_queryPhase == GLShaderProgram::QueryPhase::Immediate) |
| 169 |
? GLShader::ErrorChecking::Immediate |
| 170 |
: GLShader::ErrorChecking::Deferred; |
| 171 |
const bool hasCompiled = shader->Compile(errorChecking, _shouldLogOnErrors); |
| 172 |
|
| 173 |
if (hasCompiled) { |
| 174 |
_attachedShaders.push_back(std::move(shader)); |
| 175 |
} else { |
| 176 |
_status = Status::CompilationFailed; |
| 177 |
} |
| 178 |
return hasCompiled; |
| 179 |
} |
| 180 |
|
| 181 |
bool GLShaderProgram::Link(Introspection introspection) |
| 182 |
{ |
| 183 |
glLinkProgram(_glHandle); |
| 184 |
return FinalizeAfterLinking(introspection); |
| 185 |
} |
| 186 |
|
| 187 |
void GLShaderProgram::Use() |
| 188 |
{ |
| 189 |
if (_boundProgram != _glHandle) { |
| 190 |
ProcessDeferredQueries(); |
| 191 |
|
| 192 |
glUseProgram(_glHandle); |
| 193 |
_boundProgram = _glHandle; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
bool GLShaderProgram::Validate() |
| 198 |
{ |
| 199 |
#if defined(DEATH_TARGET_VITA) |
| 200 |
// vitaGL provides no glValidateProgram(), program validation is a debug-only aid, so assume success |
| 201 |
return true; |
| 202 |
#else |
| 203 |
glValidateProgram(_glHandle); |
| 204 |
GLint status; |
| 205 |
glGetProgramiv(_glHandle, GL_VALIDATE_STATUS, &status); |
| 206 |
return (status == GL_TRUE); |
| 207 |
#endif |
| 208 |
} |
| 209 |
|
| 210 |
bool GLShaderProgram::FinalizeAfterLinking(Introspection introspection) |
| 211 |
{ |
| 212 |
_introspection = introspection; |
| 213 |
|
| 214 |
if (_queryPhase == QueryPhase::Immediate) { |
| 215 |
_status = Status::NotLinked; |
| 216 |
const bool linkCheck = CheckLinking(); |
| 217 |
if (!linkCheck) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
// After linking, shader objects are not needed anymore |
| 222 |
#if !defined(DEATH_TARGET_VITA) |
| 223 |
// vitaGL has no glDetachShader(), the shader objects are released with the program instead |
| 224 |
for (auto& shader : _attachedShaders) { |
| 225 |
glDetachShader(_glHandle, shader->GetGLHandle()); |
| 226 |
} |
| 227 |
#endif |
| 228 |
|
| 229 |
_attachedShaders.clear(); |
| 230 |
|
| 231 |
PerformIntrospection(); |
| 232 |
return linkCheck; |
| 233 |
} else { |
| 234 |
_status = GLShaderProgram::Status::LinkedWithDeferredQueries; |
| 235 |
return true; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
GLVertexFormat::Attribute* GLShaderProgram::GetAttribute(const char* name) |
| 240 |
{ |
| 241 |
DEATH_ASSERT(name); |
| 242 |
GLVertexFormat::Attribute* vertexAttribute = nullptr; |
| 243 |
|
| 244 |
std::int32_t location = -1; |
| 245 |
const bool attributeFound = _attributeLocations.contains(name, location); |
| 246 |
if (attributeFound) { |
| 247 |
vertexAttribute = &_vertexFormat[location]; |
| 248 |
} |
| 249 |
return vertexAttribute; |
| 250 |
} |
| 251 |
|
| 252 |
void GLShaderProgram::DefineVertexFormat(const GLBufferObject* vbo, const GLBufferObject* ibo, std::uint32_t vboOffset) |
| 253 |
{ |
| 254 |
#if defined(RHI_GL_PROFILE_ES2) |
| 255 |
// ES2: the gl_VertexID-free sprite/full-screen shaders read their quad corner from the shared static |
| 256 |
// corner VBO (bound to aQuadCorner); any real geometry attributes (mesh sprites) come from the geometry |
| 257 |
// VBO. A VAO must be bound even when the sprite has no geometry VBO, so drive it off attribute presence. |
| 258 |
std::int32_t cornerLocation = -1; |
| 259 |
_attributeLocations.contains(Material::QuadCornerAttributeName, cornerLocation); |
| 260 |
const GLBufferObject* cornerVbo = static_cast<const GLBufferObject*>(RenderResources::GetQuadCornerVbo()); |
| 261 |
bool hasBoundAttribute = false; |
| 262 |
for (std::int32_t location : _attributeLocations) { |
| 263 |
if (location == cornerLocation) { |
| 264 |
if (cornerVbo != nullptr) { |
| 265 |
_vertexFormat[location].setVbo(cornerVbo); |
| 266 |
_vertexFormat[location].SetBaseOffset(0); |
| 267 |
hasBoundAttribute = true; |
| 268 |
} |
| 269 |
} else if (vbo != nullptr) { |
| 270 |
_vertexFormat[location].setVbo(vbo); |
| 271 |
_vertexFormat[location].SetBaseOffset(vboOffset); |
| 272 |
hasBoundAttribute = true; |
| 273 |
} |
| 274 |
} |
| 275 |
_vertexFormat.SetIbo(ibo); |
| 276 |
if (hasBoundAttribute) { |
| 277 |
RenderResources::GetVaoPool().BindVao(_vertexFormat); |
| 278 |
} |
| 279 |
#else |
| 280 |
if (vbo != nullptr) { |
| 281 |
for (std::int32_t location : _attributeLocations) { |
| 282 |
_vertexFormat[location].setVbo(vbo); |
| 283 |
_vertexFormat[location].SetBaseOffset(vboOffset); |
| 284 |
} |
| 285 |
_vertexFormat.SetIbo(ibo); |
| 286 |
|
| 287 |
#if !defined(WITH_RHI_SOFTWARE) |
| 288 |
RenderResources::GetVaoPool().BindVao(_vertexFormat); |
| 289 |
#endif |
| 290 |
} |
| 291 |
#endif |
| 292 |
} |
| 293 |
|
| 294 |
void GLShaderProgram::Reset() |
| 295 |
{ |
| 296 |
if (_status != Status::NotLinked && _status != Status::CompilationFailed) { |
| 297 |
_uniforms.clear(); |
| 298 |
_uniformBlocks.clear(); |
| 299 |
_attributes.clear(); |
| 300 |
|
| 301 |
_attributeLocations.clear(); |
| 302 |
_vertexFormat.Reset(); |
| 303 |
|
| 304 |
if (_boundProgram == _glHandle) { |
| 305 |
glUseProgram(0); |
| 306 |
} |
| 307 |
|
| 308 |
#if !defined(DEATH_TARGET_VITA) |
| 309 |
// vitaGL has no glDetachShader(), the shader objects are released with the program instead |
| 310 |
for (auto& shader : _attachedShaders) { |
| 311 |
glDetachShader(_glHandle, shader->GetGLHandle()); |
| 312 |
} |
| 313 |
#endif |
| 314 |
|
| 315 |
_attachedShaders.clear(); |
| 316 |
glDeleteProgram(_glHandle); |
| 317 |
|
| 318 |
#if !defined(WITH_RHI_SOFTWARE) |
| 319 |
RenderResources::RemoveCameraUniformData(this); |
| 320 |
RenderResources::UnregisterBatchedShader(this); |
| 321 |
#endif |
| 322 |
|
| 323 |
_glHandle = glCreateProgram(); |
| 324 |
} |
| 325 |
|
| 326 |
_status = Status::NotLinked; |
| 327 |
_batchSize = DefaultBatchSize; |
| 328 |
_reflection = nullptr; |
| 329 |
} |
| 330 |
|
| 331 |
void GLShaderProgram::SetObjectLabel(StringView label) |
| 332 |
{ |
| 333 |
GLDebug::SetObjectLabel(GLDebug::LabelTypes::Program, _glHandle, label); |
| 334 |
} |
| 335 |
|
| 336 |
bool GLShaderProgram::ProcessDeferredQueries() |
| 337 |
{ |
| 338 |
if (_status == GLShaderProgram::Status::LinkedWithDeferredQueries) { |
| 339 |
for (std::unique_ptr<GLShader>& attachedShader : _attachedShaders) { |
| 340 |
const bool compileCheck = attachedShader->CheckCompilation(_shouldLogOnErrors); |
| 341 |
if (!compileCheck) { |
| 342 |
return false; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
const bool linkCheck = CheckLinking(); |
| 347 |
if (!linkCheck) { |
| 348 |
return false; |
| 349 |
} |
| 350 |
|
| 351 |
// After linking, shader objects are not needed anymore |
| 352 |
#if !defined(DEATH_TARGET_VITA) |
| 353 |
// vitaGL has no glDetachShader(), the shader objects are released with the program instead |
| 354 |
for (auto& shader : _attachedShaders) { |
| 355 |
glDetachShader(_glHandle, shader->GetGLHandle()); |
| 356 |
} |
| 357 |
#endif |
| 358 |
|
| 359 |
_attachedShaders.clear(); |
| 360 |
|
| 361 |
PerformIntrospection(); |
| 362 |
} |
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
bool GLShaderProgram::CheckLinking() |
| 367 |
{ |
| 368 |
if (_status == Status::Linked || _status == Status::LinkedWithIntrospection) { |
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
GLint status; |
| 373 |
glGetProgramiv(_glHandle, GL_LINK_STATUS, &status); |
| 374 |
if (status == GL_FALSE) { |
| 375 |
#if defined(DEATH_TRACE) |
| 376 |
if (_shouldLogOnErrors) { |
| 377 |
GLint length = 0; |
| 378 |
glGetProgramiv(_glHandle, GL_INFO_LOG_LENGTH, &length); |
| 379 |
if (length > 0) { |
| 380 |
static char buffer[2048]; |
| 381 |
glGetProgramInfoLog(_glHandle, sizeof(buffer), &length, buffer); |
| 382 |
LOGW("{}", buffer); |
| 383 |
} |
| 384 |
} |
| 385 |
#endif |
| 386 |
_status = Status::LinkingFailed; |
| 387 |
return false; |
| 388 |
} |
| 389 |
|
| 390 |
_status = Status::Linked; |
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
void GLShaderProgram::PerformIntrospection() |
| 395 |
{ |
| 396 |
if (_introspection != Introspection::Disabled && _status != Status::LinkedWithIntrospection) { |
| 397 |
_uniformsSize = 0; |
| 398 |
_uniformBlocksSize = 0; |
| 399 |
|
| 400 |
if (_reflection != nullptr) { |
| 401 |
ImportReflection(); |
| 402 |
} else { |
| 403 |
const GLUniformBlock::DiscoverUniforms discover = (_introspection == Introspection::NoUniformsInBlocks) |
| 404 |
? GLUniformBlock::DiscoverUniforms::Disabled |
| 405 |
: GLUniformBlock::DiscoverUniforms::Enabled; |
| 406 |
|
| 407 |
DiscoverUniforms(); |
| 408 |
DiscoverUniformBlocks(discover); |
| 409 |
DiscoverAttributes(); |
| 410 |
} |
| 411 |
InitVertexFormat(); |
| 412 |
_status = Status::LinkedWithIntrospection; |
| 413 |
} |
| 414 |
// The reflection data is consumed by introspection and may not outlive the caller, so it is never kept |
| 415 |
_reflection = nullptr; |
| 416 |
} |
| 417 |
|
| 418 |
void GLShaderProgram::ImportReflection() |
| 419 |
{ |
| 420 |
ZoneScopedC(0x81A861); |
| 421 |
const ShaderCompiler::ProgramVariant& reflection = *_reflection; |
| 422 |
|
| 423 |
// Loose uniforms - the reflection keeps samplers in a separate list, but GL treats them as uniforms |
| 424 |
// (all samplers are 2D across the shader set; the reflected texture bindings carry no dimension) |
| 425 |
for (std::size_t i = 0; i < reflection.UniformCount; i++) { |
| 426 |
const ShaderCompiler::Uniform& u = reflection.Uniforms[i]; |
| 427 |
GLUniform& uniform = _uniforms.emplace_back(_glHandle, u.Name, UniformTypeToGL(u.Type), GLint(u.ArraySize)); |
| 428 |
_uniformsSize += uniform.GetMemorySize(); |
| 429 |
|
| 430 |
LOGD("Shader program {} - uniform {} : \"{}\" (reflected)", _glHandle, uniform.GetLocation(), uniform.GetName()); |
| 431 |
} |
| 432 |
for (std::size_t i = 0; i < reflection.TextureCount; i++) { |
| 433 |
const ShaderCompiler::TextureBinding& t = reflection.Textures[i]; |
| 434 |
GLUniform& uniform = _uniforms.emplace_back(_glHandle, t.Name, GL_SAMPLER_2D, 1); |
| 435 |
_uniformsSize += uniform.GetMemorySize(); |
| 436 |
|
| 437 |
LOGD("Shader program {} - sampler uniform {} : \"{}\" (reflected)", _glHandle, uniform.GetLocation(), uniform.GetName()); |
| 438 |
} |
| 439 |
|
| 440 |
for (std::size_t i = 0; i < reflection.BlockCount; i++) { |
| 441 |
const ShaderCompiler::UniformBlock& b = reflection.Blocks[i]; |
| 442 |
#if defined(RHI_GL_PROFILE_ES2) |
| 443 |
// ES2 (ESSL 100) has no uniform buffer objects: the block's members are plain loose uniforms in |
| 444 |
// the linked program. Keep the block in the reflection so BaseSprite/Material still address the |
| 445 |
// per-instance data through it, but resolve each member's real loose location below so that |
| 446 |
// GLShaderUniformBlocks::Bind() can push them with glUniform* instead of a UBO bind. There is no |
| 447 |
// glGetUniformBlockIndex() (ES 3.0) to call; the stored index stays GL_INVALID_INDEX and unused |
| 448 |
// (the ES2 path never binds a buffer range). |
| 449 |
const GLuint blockIndex = GL_INVALID_INDEX; |
| 450 |
#else |
| 451 |
const GLuint blockIndex = glGetUniformBlockIndex(_glHandle, b.Name); |
| 452 |
if (blockIndex == GL_INVALID_INDEX) { |
| 453 |
// The whole block was optimized out by the driver - GL introspection would not have listed it either |
| 454 |
LOGD("Shader program {} - uniform block \"{}\" is inactive and was skipped", _glHandle, b.Name); |
| 455 |
continue; |
| 456 |
} |
| 457 |
#endif |
| 458 |
|
| 459 |
// A BATCH_SIZE-sized instance array uses the explicitly set batch size, or the same 64 KB-based |
| 460 |
// fallback the in-shader "#ifndef BATCH_SIZE" defaults assume when no size is injected |
| 461 |
std::uint32_t effectiveBatchSize = 0; |
| 462 |
std::uint32_t dataSize = b.BaseSize; |
| 463 |
if (b.InstanceStride > 0) { |
| 464 |
effectiveBatchSize = (_batchSize != std::uint32_t(DefaultBatchSize) && _batchSize > 0) |
| 465 |
? _batchSize : (64 * 1024) / b.InstanceStride; |
| 466 |
dataSize += b.InstanceStride * effectiveBatchSize; |
| 467 |
} |
| 468 |
|
| 469 |
GLUniformBlock& uniformBlock = _uniformBlocks.emplace_back(_glHandle, b.Name, blockIndex, GLint(dataSize)); |
| 470 |
_uniformBlocksSize += uniformBlock.GetSize(); |
| 471 |
|
| 472 |
if (_introspection != Introspection::NoUniformsInBlocks) { |
| 473 |
for (std::size_t j = 0; j < b.MemberCount; j++) { |
| 474 |
const ShaderCompiler::BlockMember& m = b.Members[j]; |
| 475 |
if (m.Type == ShaderCompiler::UniformType::Struct) { |
| 476 |
// GL introspection reports flattened leaves of struct members instead - no engine code |
| 477 |
// accesses struct aggregates by name, so they are skipped here |
| 478 |
continue; |
| 479 |
} |
| 480 |
|
| 481 |
GLUniform blockUniform; |
| 482 |
std::size_t nameLength = strnlen(m.Name, GLUniform::MaxNameLength); |
| 483 |
DEATH_ASSERT(nameLength < GLUniform::MaxNameLength); |
| 484 |
std::memcpy(blockUniform._name, m.Name, nameLength); |
| 485 |
blockUniform._name[nameLength] = '\0'; |
| 486 |
blockUniform._blockIndex = GLint(blockIndex); |
| 487 |
blockUniform._type = UniformTypeToGL(m.Type); |
| 488 |
blockUniform._size = (m.ArraySize == ShaderCompiler::SymbolicArraySize) ? GLint(effectiveBatchSize) |
| 489 |
: (m.ArraySize > 0 ? GLint(m.ArraySize) : 1); |
| 490 |
blockUniform._offset = GLint(m.Offset); |
| 491 |
#if defined(RHI_GL_PROFILE_ES2) |
| 492 |
// The member is a real loose uniform on ES2 - resolve its location for the glUniform upload |
| 493 |
blockUniform._location = glGetUniformLocation(_glHandle, m.Name); |
| 494 |
#endif |
| 495 |
uniformBlock._blockUniforms[blockUniform._name] = blockUniform; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
LOGD("Shader program {} - uniform block {} : \"{}\" ({} bytes with {} align, reflected)", _glHandle, uniformBlock.GetIndex(), uniformBlock.GetName(), uniformBlock.GetSize(), uniformBlock.GetAlignAmount()); |
| 500 |
} |
| 501 |
|
| 502 |
#if defined(RHI_GL_PROFILE_ES2) |
| 503 |
// The reflected attributes describe the modern (gl_VertexID) source; the linked ES2 program has |
| 504 |
// different vertex inputs (aQuadCorner / aInstanceIndex replacing gl_VertexID), so query the real |
| 505 |
// active attributes from GL instead of the offline reflection. |
| 506 |
DiscoverAttributes(); |
| 507 |
#else |
| 508 |
for (std::size_t i = 0; i < reflection.AttributeCount; i++) { |
| 509 |
const ShaderCompiler::Attribute& a = reflection.Attributes[i]; |
| 510 |
DEATH_UNUSED GLAttribute& attribute = _attributes.emplace_back(_glHandle, a.Name, UniformTypeToGL(a.Type)); |
| 511 |
LOGD("Shader program {} - attribute {} : \"{}\" (reflected)", _glHandle, attribute.GetLocation(), attribute.GetName()); |
| 512 |
} |
| 513 |
#endif |
| 514 |
GL_LOG_ERRORS(); |
| 515 |
} |
| 516 |
|
| 517 |
void GLShaderProgram::DiscoverUniforms() |
| 518 |
{ |
| 519 |
ZoneScopedC(0x81A861); |
| 520 |
#if defined(RHI_GL_PROFILE_ES2) |
| 521 |
// ES2 has no uniform blocks, so every active uniform is a loose one - enumerate them directly |
| 522 |
// (glGetActiveUniformsiv() used below to filter out block members is ES 3.0) |
| 523 |
GLint uniformCount = 0; |
| 524 |
glGetProgramiv(_glHandle, GL_ACTIVE_UNIFORMS, &uniformCount); |
| 525 |
|
| 526 |
for (GLint i = 0; i < uniformCount; i++) { |
| 527 |
GLUniform& uniform = _uniforms.emplace_back(_glHandle, static_cast<GLuint>(i)); |
| 528 |
_uniformsSize += uniform.GetMemorySize(); |
| 529 |
|
| 530 |
LOGD("Shader program {} - uniform {} : \"{}\"", _glHandle, uniform.GetLocation(), uniform.GetName()); |
| 531 |
} |
| 532 |
#else |
| 533 |
static const std::uint32_t NumIndices = 512; |
| 534 |
static GLuint uniformIndices[NumIndices]; |
| 535 |
static GLint blockIndices[NumIndices]; |
| 536 |
|
| 537 |
GLint uniformCount = 0; |
| 538 |
glGetProgramiv(_glHandle, GL_ACTIVE_UNIFORMS, &uniformCount); |
| 539 |
|
| 540 |
if (uniformCount > 0) { |
| 541 |
std::uint32_t uniformsOutsideBlocks = 0; |
| 542 |
GLuint indices[MaxNumUniforms]; |
| 543 |
std::uint32_t remainingIndices = static_cast<std::uint32_t>(uniformCount); |
| 544 |
|
| 545 |
while (remainingIndices > 0) { |
| 546 |
const std::uint32_t uniformCountStep = (remainingIndices > NumIndices) ? NumIndices : remainingIndices; |
| 547 |
const std::uint32_t startIndex = static_cast<std::uint32_t>(uniformCount) - remainingIndices; |
| 548 |
|
| 549 |
for (std::uint32_t i = 0; i < uniformCountStep; i++) |
| 550 |
uniformIndices[i] = startIndex + i; |
| 551 |
|
| 552 |
glGetActiveUniformsiv(_glHandle, uniformCountStep, uniformIndices, GL_UNIFORM_BLOCK_INDEX, blockIndices); |
| 553 |
|
| 554 |
for (std::uint32_t i = 0; i < uniformCountStep; i++) { |
| 555 |
if (blockIndices[i] == -1) { |
| 556 |
indices[uniformsOutsideBlocks] = startIndex + i; |
| 557 |
uniformsOutsideBlocks++; |
| 558 |
} |
| 559 |
if (uniformsOutsideBlocks > MaxNumUniforms - 1) |
| 560 |
break; |
| 561 |
} |
| 562 |
|
| 563 |
remainingIndices -= uniformCountStep; |
| 564 |
} |
| 565 |
|
| 566 |
for (std::uint32_t i = 0; i < uniformsOutsideBlocks; i++) { |
| 567 |
GLUniform& uniform = _uniforms.emplace_back(_glHandle, indices[i]); |
| 568 |
_uniformsSize += uniform.GetMemorySize(); |
| 569 |
|
| 570 |
LOGD("Shader program {} - uniform {} : \"{}\"", _glHandle, uniform.GetLocation(), uniform.GetName()); |
| 571 |
} |
| 572 |
} |
| 573 |
#endif |
| 574 |
GL_LOG_ERRORS(); |
| 575 |
} |
| 576 |
|
| 577 |
void GLShaderProgram::DiscoverUniformBlocks(GLUniformBlock::DiscoverUniforms discover) |
| 578 |
{ |
| 579 |
#if defined(RHI_GL_PROFILE_ES2) |
| 580 |
// ES2 has no uniform blocks (GL_ACTIVE_UNIFORM_BLOCKS is ES 3.0); raw-string programs on this profile |
| 581 |
// (e.g. ImGui) only carry loose uniforms, which DiscoverUniforms() has already enumerated |
| 582 |
static_cast<void>(discover); |
| 583 |
#else |
| 584 |
ZoneScopedC(0x81A861); |
| 585 |
GLint count; |
| 586 |
glGetProgramiv(_glHandle, GL_ACTIVE_UNIFORM_BLOCKS, &count); |
| 587 |
|
| 588 |
for (std::int32_t i = 0; i < count; i++) { |
| 589 |
GLUniformBlock& uniformBlock = _uniformBlocks.emplace_back(_glHandle, i, discover); |
| 590 |
_uniformBlocksSize += uniformBlock.GetSize(); |
| 591 |
|
| 592 |
LOGD("Shader program {} - uniform block {} : \"{}\" ({} bytes with {} align)", _glHandle, uniformBlock.GetIndex(), uniformBlock.GetName(), uniformBlock.GetSize(), uniformBlock.GetAlignAmount()); |
| 593 |
} |
| 594 |
GL_LOG_ERRORS(); |
| 595 |
#endif |
| 596 |
} |
| 597 |
|
| 598 |
void GLShaderProgram::DiscoverAttributes() |
| 599 |
{ |
| 600 |
ZoneScopedC(0x81A861); |
| 601 |
GLint count; |
| 602 |
glGetProgramiv(_glHandle, GL_ACTIVE_ATTRIBUTES, &count); |
| 603 |
|
| 604 |
for (std::int32_t i = 0; i < count; i++) { |
| 605 |
DEATH_UNUSED GLAttribute& attribute = _attributes.emplace_back(_glHandle, i); |
| 606 |
LOGD("Shader program {} - attribute {} : \"{}\"", _glHandle, attribute.GetLocation(), attribute.GetName()); |
| 607 |
} |
| 608 |
GL_LOG_ERRORS(); |
| 609 |
} |
| 610 |
|
| 611 |
void GLShaderProgram::InitVertexFormat() |
| 612 |
{ |
| 613 |
ZoneScopedC(0x81A861); |
| 614 |
const std::uint32_t count = (std::uint32_t)_attributes.size(); |
| 615 |
if (count > GLVertexFormat::MaxAttributes) { |
| 616 |
LOGW("More active attributes ({}) than supported by the vertex format class ({})", count, GLVertexFormat::MaxAttributes); |
| 617 |
} |
| 618 |
for (std::uint32_t i = 0; i < _attributes.size(); i++) { |
| 619 |
const GLAttribute& attribute = _attributes[i]; |
| 620 |
const std::int32_t location = attribute.GetLocation(); |
| 621 |
if (location < 0) |
| 622 |
continue; |
| 623 |
|
| 624 |
_attributeLocations[attribute.GetName()] = location; |
| 625 |
_vertexFormat[location].Init(attribute.GetLocation(), attribute.GetComponentCount(), attribute.GetBasicType()); |
| 626 |
#if defined(RHI_GL_PROFILE_ES2) |
| 627 |
// The ES2 quad-corner attribute is fed from a tightly-packed static VBO (a plain vec2 stream), |
| 628 |
// not the geometry VBO; set its stride/offset once here (the VBO is bound in DefineVertexFormat). |
| 629 |
if (std::strcmp(attribute.GetName(), Material::QuadCornerAttributeName) == 0) { |
| 630 |
_vertexFormat[location].SetVboParameters(2 * sizeof(GLfloat), nullptr); |
| 631 |
} |
| 632 |
#endif |
| 633 |
} |
| 634 |
} |
| 635 |
} |