[Fix, Test] (GLState): keep named-block members out of the GL uniform location pool

This commit is contained in:
2026-08-21 06:36:47 -04:00
parent b32c35a113
commit 3cc6b88767
2 changed files with 126 additions and 13 deletions
@@ -1023,6 +1023,12 @@ namespace MobileGL::MG_State::GLState {
return uniform.index >= 0 && uniform.index < static_cast<Int>(artifacts.tProgramBlockIndexToGl.size()) &&
artifacts.tProgramBlockIndexToGl[uniform.index] < 0;
};
// Member of a block GL can see - a named uniform block, a buffer block, or the
// synthesized atomic-counter block. GL locations are a property of the DEFAULT uniform
// block alone (GL 4.6 core 7.6.1), so these take none.
const auto isNamedBlockMember = [&isGlobalUboMember](const glslang::TObjectReflection& uniform) {
return uniform.index >= 0 && !isGlobalUboMember(uniform);
};
for (Int i = 0; i < tProgramUniformCount; i++) {
const auto& uniform = artifacts.program->getUniform(i);
if (isGlobalUboMember(uniform) && uniform.stages == 0) {
@@ -1069,8 +1075,7 @@ namespace MobileGL::MG_State::GLState {
for (const Int i : artifacts.glUniformIndexToTProgram) {
const auto& uniform = artifacts.program->getUniform(i);
const glslang::TType* type = uniform.getType();
const Bool inNamedBlock = uniform.index >= 0 && !isGlobalUboMember(uniform);
if (inNamedBlock) continue; // block members never take glUniform locations
if (isNamedBlockMember(uniform)) continue; // block members never take glUniform locations
if (const Int* explicitLocation = findExplicitLocation(uniform.name)) {
effectiveLocation[i] = static_cast<Uint>(*explicitLocation);
@@ -1145,20 +1150,23 @@ namespace MobileGL::MG_State::GLState {
in.externalIndex, uniform.name.c_str(), location, location + locationSpan - 1);
}
// Counts ONLY default-block uniforms, which is the whole of what a GL uniform location
// is and the whole of what GL_MAX_UNIFORM_LOCATIONS bounds (GL 4.6 core 7.6.1). A
// named-block member used to be counted here too and used to be handed a location by the
// first-fit pass below, which is a spec violation twice over: glGetUniformLocation must
// answer -1 for it (glGetProgramResourceLocation already did), and every slot it took
// pushed a real default-block uniform one location further up. On a program with a
// buffer block that is exactly how a location EQUAL to the advertised maximum got minted
// - the table's ceiling is raised to hold this count, so one extra block member raised it
// to MAX and the first-fit pass then filled the last slot
// (KHR-GL43.explicit_uniform_location.uniform-loc-mix-with-implicit-max, whose compute
// program carries an SSBO; its -max-array sibling ran the pool out and failed to link).
Int requiredUniformLocations = deadReservedLocationCount;
// The same count restricted to DEFAULT-BLOCK uniforms, which is the only thing
// GL_MAX_UNIFORM_LOCATIONS bounds. requiredUniformLocations cannot serve: it also carries
// named-block members, which take a slot in this allocator's table (an implementation
// detail) but consume no GL uniform location at all, so a big UBO array would otherwise
// fail a link the spec allows.
Int defaultBlockLocationDemand = deadReservedLocationCount;
for (const Int i : artifacts.glUniformIndexToTProgram) {
auto& uniform = artifacts.program->getUniform(i);
const Uint location = effectiveLocation[i];
const Int locationSpan = GetUniformLocationSpan(uniform);
requiredUniformLocations += locationSpan;
const Bool inNamedBlock = uniform.index >= 0 && !isGlobalUboMember(uniform);
if (!inNamedBlock) defaultBlockLocationDemand += locationSpan;
if (!isNamedBlockMember(uniform)) requiredUniformLocations += locationSpan;
if (location != kNoLocation) {
artifacts.maxUniformLocation = std::max(artifacts.maxUniformLocation, location + locationSpan - 1);
}
@@ -1177,11 +1185,11 @@ namespace MobileGL::MG_State::GLState {
// (KHR-GL43.explicit_uniform_location.uniform-loc-negative-link-max-num-of-locations).
// A single uniform whose own span passes the ceiling was already rejected above; this is
// the aggregate half of the same rule.
if (defaultBlockLocationDemand > static_cast<Int>(kMaxUniformLocations)) {
if (requiredUniformLocations > static_cast<Int>(kMaxUniformLocations)) {
artifacts.infoLog =
std::format("Uniform locations exhausted: the default-block uniforms need {} locations but "
"GL_MAX_UNIFORM_LOCATIONS is {}.",
defaultBlockLocationDemand, kMaxUniformLocations);
requiredUniformLocations, kMaxUniformLocations);
DeferLog(std::format("ProgramObject {}: Link failed - {}", in.externalIndex, artifacts.infoLog));
ProgramObject::ResetLinkArtifacts(artifacts);
return false;
@@ -1254,6 +1262,10 @@ namespace MobileGL::MG_State::GLState {
// is demoted to the first-fit pass below instead of failing the link.
for (const Int i : artifacts.glUniformIndexToTProgram) {
auto& uniform = artifacts.program->getUniform(i);
// Same rule the effective-location loop applies: a block member has no GL location,
// so it must not reach the first-fit pass either. Its uniformLocations entry stays
// at kNoLocation, which glGetUniformLocation reads back as the -1 the spec wants.
if (isNamedBlockMember(uniform)) continue;
if (locationIsSourceExplicit[i]) continue;
const Uint location = effectiveLocation[i];
if (location == kNoLocation) {