[Fix] (GL): fail the link when a stage exceeds its uniform or storage block limits

This commit is contained in:
2026-10-12 06:46:05 -04:00
parent 106190e2ff
commit ba6db60c09
5 changed files with 249 additions and 5 deletions
@@ -905,6 +905,89 @@ void main() {
}
}
// The same rule for blocks (GL 4.6 core 7.6.2 / 7.8): a stage using more shader storage
// blocks than GL_MAX_<STAGE>_SHADER_STORAGE_BLOCKS fails the link. glslang does not count
// them, so a fragment stage with eight blocks against an advertised four reported
// LINK_STATUS = TRUE and then failed inside the backend. Sized off the advertised limit.
TEST_F(ProgramInterfaceTest, StorageBlocksOverAStageLimitFailToLink) {
GLint maxFragmentBlocks = 0;
GLint maxCombinedBlocks = 0;
GetIntegerv(GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS, &maxFragmentBlocks);
GetIntegerv(GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS, &maxCombinedBlocks);
ClearErrors();
ASSERT_GT(maxFragmentBlocks, 0);
// Bindings wrap at the limit: two blocks may share a binding point, and a binding past
// GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS would fail for a different reason.
const auto linkWithFragmentBlocks = [maxFragmentBlocks](GLint count) {
std::string source = "#version 430\nout vec4 color;\n";
std::string sum = "0.0";
for (GLint i = 0; i < count; ++i) {
const std::string n = std::to_string(i);
source += "layout(std430, binding = " + std::to_string(i % maxFragmentBlocks) + ") buffer B" + n +
" { float v" + n + "; };\n";
sum += " + v" + n;
}
source += "void main() { color = vec4(" + sum + "); }\n";
return MakeProgram(kSimpleVs, source.c_str());
};
const GLuint over = linkWithFragmentBlocks(maxFragmentBlocks + 1);
LinkProgram(over);
GLint status = -1;
GetProgramiv(over, GL_LINK_STATUS, &status);
EXPECT_EQ(status, GL_FALSE);
char log[4096] = "";
GetProgramInfoLog(over, sizeof(log), nullptr, log);
EXPECT_NE(std::string(log).find("GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS"), std::string::npos)
<< "info log was: " << log;
ClearErrors();
if (maxFragmentBlocks <= maxCombinedBlocks) {
const GLuint atLimit = linkWithFragmentBlocks(maxFragmentBlocks);
LinkProgram(atLimit);
ExpectLinked(atLimit);
ClearErrors();
}
}
// Uniform blocks are counted the same way, and the synthesized block that carries the
// default-block uniforms is not one of them.
TEST_F(ProgramInterfaceTest, UniformBlocksOverAStageLimitFailToLink) {
GLint maxFragmentBlocks = 0;
GetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS, &maxFragmentBlocks);
ClearErrors();
ASSERT_GT(maxFragmentBlocks, 0);
const auto linkWithFragmentBlocks = [](GLint count) {
std::string source = "#version 430\nout vec4 color;\nuniform float loose;\n";
std::string sum = "loose";
for (GLint i = 0; i < count; ++i) {
const std::string n = std::to_string(i);
source += "layout(std140) uniform U" + n + " { float u" + n + "; };\n";
sum += " + u" + n;
}
source += "void main() { color = vec4(" + sum + "); }\n";
return MakeProgram(kSimpleVs, source.c_str());
};
const GLuint over = linkWithFragmentBlocks(maxFragmentBlocks + 1);
LinkProgram(over);
GLint status = -1;
GetProgramiv(over, GL_LINK_STATUS, &status);
EXPECT_EQ(status, GL_FALSE);
char log[4096] = "";
GetProgramInfoLog(over, sizeof(log), nullptr, log);
EXPECT_NE(std::string(log).find("GL_MAX_FRAGMENT_UNIFORM_BLOCKS"), std::string::npos)
<< "info log was: " << log;
ClearErrors();
const GLuint atLimit = linkWithFragmentBlocks(maxFragmentBlocks);
LinkProgram(atLimit);
ExpectLinked(atLimit);
ClearErrors();
}
// glGetProgramiv(GL_ACTIVE_ATOMIC_COUNTER_BUFFERS) and glGetActiveAtomicCounterBufferiv are
// the pre-4.3 spelling of the interface above, and the spec requires the two to agree.
// Neither did: the first counted glslang's atomic counter UNIFORMS - zero, because the
+44
View File
@@ -410,7 +410,51 @@ void main() {
// BUFFER block, so SPIRV-Reflect sees an ordinary storage descriptor for it and the
// backend's space has always contained it - even though GL's own GL_SHADER_STORAGE_BLOCK
// enumeration excludes it. The archive must follow the BACKEND's space, not GL's.
namespace {
// A limits-only backend: what the link checks against comes from GetDynamicParameters, and the
// no-backend defaults are the spec minimums, which give the vertex stage no storage blocks.
class StorageBlockLimitsBackend final : public MG_Backend::BackendObject {
public:
StorageBlockLimitsBackend() {
m_params.MaxVertexShaderStorageBlocks = 8;
m_params.MaxCombinedShaderStorageBlocks = 16;
m_params.MaxShaderStorageBufferBindings = 16;
}
void Initialize() override {}
Bool InitCapabilities() override { return true; }
Bool InitWindowSurface() override { return true; }
const RendererInfo& GetRendererInfo() const override {
static RendererInfo info = {};
return info;
}
String GetBackendAPIVersionString() const override { return {}; }
const MG_Backend::GlobalBackendFunctionsTable& GetBackendFunctions() const override {
static MG_Backend::GlobalBackendFunctionsTable table = {};
return table;
}
const MG_Backend::DynamicBackendParameters& GetDynamicParameters() const override { return m_params; }
BackendType GetBackendType() const override { return BackendType::Unknown; }
private:
MG_Backend::DynamicBackendParameters m_params{};
};
class ScopedLimitsBackend {
public:
ScopedLimitsBackend(): m_previous(Move(MG_Backend::pActiveBackendObject)) {
MG_Backend::pActiveBackendObject = MakeUnique<StorageBlockLimitsBackend>();
}
~ScopedLimitsBackend() { MG_Backend::pActiveBackendObject = Move(m_previous); }
private:
UniquePtr<MG_Backend::BackendObject> m_previous;
};
} // namespace
TEST_F(ProgramTest, TheArchivesStorageBlockOrderIsTheOneSpirvReflectProduces) {
// The vertex stage below uses storage blocks, which a link only accepts against a backend
// that advertises them.
const ScopedLimitsBackend limits;
char infoLog[1024] = "";
const char* vsSrc = R"(#version 460 core
layout(std430, binding = 3) buffer Blocks {