From e98c5b4a6a9e224572845c39a9791a5fb8120c88 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Mon, 12 Oct 2026 08:05:12 -0400 Subject: [PATCH] [Fix] (GL): validate program pipelines by the spec rules in glValidateProgramPipeline and on every pipeline draw --- .../MG_Impl/GLImpl/Drawing/GL_Drawing.cpp | 17 +++++ .../GLImpl/Program/GL_ProgramPipeline.cpp | 6 +- .../ProgramState/ProgramPipelineObject.h | 75 +++++++++++++++++++ .../Program/ProgramPipelineCompositeTest.cpp | 68 +++++++++++++++++ 4 files changed, 163 insertions(+), 3 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Drawing/GL_Drawing.cpp b/MobileGL/MG_Impl/GLImpl/Drawing/GL_Drawing.cpp index 1852fbe1..c9b649ef 100644 --- a/MobileGL/MG_Impl/GLImpl/Drawing/GL_Drawing.cpp +++ b/MobileGL/MG_Impl/GLImpl/Drawing/GL_Drawing.cpp @@ -113,6 +113,23 @@ namespace MobileGL::MG_Impl::GLImpl { // DRAW FRAMEBUFFER binding, so it has to run after the caller's framebuffer state is settled // and before the backend consumes the program's UBO content version. static Bool PrepareCurrentProgramForDraw(const char* functionName) { + // GL 4.6 core 11.1.3.11: a draw through a program pipeline that would fail + // glValidateProgramPipeline is INVALID_OPERATION. Only when no glUseProgram program + // overrides the pipeline, and BEFORE the composite is resolved: the composite takes from + // each stage program only the shaders of the stages it is bound to, so a program bound + // to fewer stages than it was linked with no longer shows in it. + if (!MG_State::pGLContext->GetCurrentProgram() && MG_State::pGLContext->GetBoundProgramPipelineName() != 0) { + if (const auto& pipeline = MG_State::pGLContext->GetBoundProgramPipeline()) { + const String reason = pipeline->FindValidationError(/*requireAnyStage=*/false); + if (!reason.empty()) { + MG_State::pGLContext->RecordError( + ErrorCode::InvalidOperation, + MakeUnique("MG_Impl/GLImpl", functionName, + "The bound program pipeline is not valid: " + reason)); + return false; + } + } + } const auto& currentProgram = MG_State::pGLContext->GetProgramForDraw(); if (!ValidateResolvedProgramForDraw(currentProgram, functionName)) return false; PublishDrawFramebufferSampleCount(currentProgram); diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_ProgramPipeline.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_ProgramPipeline.cpp index f9c461e4..3e7fe3e0 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_ProgramPipeline.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_ProgramPipeline.cpp @@ -243,8 +243,8 @@ namespace MobileGL::MG_Impl::GLImpl { void ValidateProgramPipeline(GLuint pipeline) { const auto* pipelineObject = TryGetPipeline(pipeline, __func__); if (!pipelineObject) return; - // Nothing here can fail today: MobileGL links each stage program on its own, so there is no - // cross-stage interface to re-check at validation time. The log stays empty, which GL allows. - (*pipelineObject)->SetValidateStatus(true); + String reason = (*pipelineObject)->FindValidationError(/*requireAnyStage=*/true); + (*pipelineObject)->SetValidateStatus(reason.empty()); + (*pipelineObject)->SetInfoLog(Move(reason)); } } // namespace MobileGL::MG_Impl::GLImpl diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramPipelineObject.h b/MobileGL/MG_State/GLState/ProgramState/ProgramPipelineObject.h index 981b55ac..8522e1c7 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramPipelineObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramPipelineObject.h @@ -40,6 +40,81 @@ namespace MobileGL { Uint GetExternalIndex() const { return m_externalIndex; } + // GL 4.6 core 11.1.3.11, the pipeline validation rules - the ONE copy, read by + // glValidateProgramPipeline and by every draw made through the pipeline (where a + // failure is INVALID_OPERATION). Returns why the pipeline is not valid, or an empty + // string when it is. + // + // `requireAnyStage`: a pipeline with no program in any stage fails VALIDATION, but a + // draw through one is merely undefined and is dropped elsewhere without an error. + // + // Every question is asked of a program's EXECUTABLE (HasLinkedShaderStage and the + // separable flag as last linked), never of its live attach list. + String FindValidationError(Bool requireAnyStage) const { + constexpr SizeT kStageCount = static_cast(ShaderStage::ShaderStageCount); + const auto executes = [this](SizeT stage) { + const auto& program = m_stagePrograms[stage]; + return program && program->HasLinkedShaderStage(static_cast(stage)); + }; + + Bool anyStage = false; + for (SizeT stage = 0; stage < kStageCount; ++stage) { + const auto& program = m_stagePrograms[stage]; + if (!program) continue; + anyStage = true; + // A stage program whose last relink failed, or that was relinked as not + // separable, can no longer serve as a stage (glUseProgramStages checked + // both, but only at the time of the call). + if (!program->GetLinkStatus()) { + return std::format("The program in stage {} is not successfully linked.", stage); + } + if (!program->GetLinkedSeparable()) { + return std::format("The program in stage {} was relinked as not separable.", stage); + } + // "A program object is active for at least one, but not all of the shader + // stages that were present when the program was linked." + for (SizeT other = 0; other < kStageCount; ++other) { + if (program->HasLinkedShaderStage(static_cast(other)) && + m_stagePrograms[other] != program) { + return std::format("A program is active for stage {} but not for its linked stage {}.", + stage, other); + } + } + } + if (!anyStage) { + return requireAnyStage ? String("The pipeline has no program in any stage.") : String(); + } + + // "One program object is active for at least two shader stages and a second + // program is active for a shader stage between two stages for which the first + // program was active." Over the graphics stages, in pipeline order. + for (SizeT first = 0; first < kGraphicsStageCount; ++first) { + if (!executes(first)) continue; + for (SizeT last = first + 2; last < kGraphicsStageCount; ++last) { + if (!executes(last) || m_stagePrograms[last] != m_stagePrograms[first]) continue; + for (SizeT between = first + 1; between < last; ++between) { + if (executes(between) && m_stagePrograms[between] != m_stagePrograms[first]) { + return std::format("The program in stage {} sits between two stages of another " + "program.", + between); + } + } + } + } + + // "There is an active program for tessellation control, tessellation evaluation, + // or geometry stages with corresponding executable shader, but there is no + // active program with an executable vertex shader." + const Bool laterVertexProcessing = + executes(static_cast(ShaderStage::TessControl)) || + executes(static_cast(ShaderStage::TessEval)) || + executes(static_cast(ShaderStage::Geometry)); + if (laterVertexProcessing && !executes(static_cast(ShaderStage::Vertex))) { + return "A tessellation or geometry stage is active but no program supplies a vertex shader."; + } + return {}; + } + // glIsProgramPipeline's answer, and NOT the same question as "does this object // exist" (GL 4.6 core 7.4: a GenProgramPipelines name "acquires program pipeline // state only when first bound"). The object is materialized by any of the diff --git a/MobileGL/MG_Test/Program/ProgramPipelineCompositeTest.cpp b/MobileGL/MG_Test/Program/ProgramPipelineCompositeTest.cpp index 1d9a7eae..e3f7ab50 100644 --- a/MobileGL/MG_Test/Program/ProgramPipelineCompositeTest.cpp +++ b/MobileGL/MG_Test/Program/ProgramPipelineCompositeTest.cpp @@ -605,6 +605,74 @@ void main() { gl_Position = vec4(0.0); EmitVertex(); EndPrimitive(); } EXPECT_EQ(GetError(), GL_NO_ERROR); } +// GL 4.6 core 11.1.3.11, the shapes es31cSeparateShaderObjsTests' PipelineApi and +// StateInteraction check: glValidateProgramPipeline and a draw through the pipeline must agree, +// and a missing stage is NOT one of the failures. +TEST_F(ProgramPipelineCompositeTest, PipelineValidationAndDrawsAgreeOnTheSpecRules) { + const char* kVf = R"(#version 430 core +out gl_PerVertex { vec4 gl_Position; }; +void main() { gl_Position = vec4(0.0); } +)"; + // One separable program carrying BOTH the vertex and the fragment stage. + const GLuint both = CreateProgram(); + ProgramParameteri(both, GL_PROGRAM_SEPARABLE, GL_TRUE); + for (const auto& [stage, source] : {std::pair{GL_VERTEX_SHADER, kVf}, std::pair{GL_FRAGMENT_SHADER, kSharedUniformFs}}) { + const GLuint shader = CreateShader(stage); + ShaderSource(shader, 1, &source, nullptr); + CompileShader(shader); + AttachShader(both, shader); + } + LinkProgram(both); + GLint linked = GL_FALSE; + GetProgramiv(both, GL_LINK_STATUS, &linked); + ASSERT_EQ(linked, GL_TRUE); + const GLuint fs = MakeSeparableProgram(GL_FRAGMENT_SHADER, kSharedUniformFs); + + GLuint pipeline = 0; + GenProgramPipelines(1, &pipeline); + BindProgramPipeline(pipeline); + const auto validates = [pipeline] { + ValidateProgramPipeline(pipeline); + GLint status = -1; + GetProgramPipelineiv(pipeline, GL_VALIDATE_STATUS, &status); + return status == GL_TRUE; + }; + + EXPECT_FALSE(validates()) << "an empty pipeline does not validate"; + GLint logLength = 0; + GetProgramPipelineiv(pipeline, GL_INFO_LOG_LENGTH, &logLength); + EXPECT_GT(logLength, 0) << "a failed validation says why"; + + UseProgramStages(pipeline, GL_VERTEX_SHADER_BIT | GL_FRAGMENT_SHADER_BIT, both); + EXPECT_TRUE(validates()); + + // Bound to only one of the two stages it was linked with. + UseProgramStages(pipeline, GL_FRAGMENT_SHADER_BIT, fs); + EXPECT_FALSE(validates()); + ASSERT_EQ(GetError(), GL_NO_ERROR); + DrawArrays(GL_TRIANGLES, 0, 3); + EXPECT_EQ(GetError(), GL_INVALID_OPERATION) << "a draw through an invalid pipeline"; + + // A missing stage is fine: no vertex stage at all, only a fragment program. + UseProgramStages(pipeline, GL_VERTEX_SHADER_BIT, 0); + EXPECT_TRUE(validates()); + + // A stage program relinked as NOT separable can no longer serve. + UseProgramStages(pipeline, GL_VERTEX_SHADER_BIT | GL_FRAGMENT_SHADER_BIT, both); + ASSERT_TRUE(validates()); + ProgramParameteri(both, GL_PROGRAM_SEPARABLE, GL_FALSE); + LinkProgram(both); + EXPECT_FALSE(validates()); + ASSERT_EQ(GetError(), GL_NO_ERROR); + DrawArrays(GL_TRIANGLES, 0, 3); + EXPECT_EQ(GetError(), GL_INVALID_OPERATION); + + BindProgramPipeline(0); + DeleteProgramPipelines(1, &pipeline); + for (Int drained = 0; drained < 16 && GetError() != GL_NO_ERROR; ++drained) { + } +} + // --------------------------------------------------------------------------------------------- // The composite's transform-feedback capture list. //