mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-17 15:03:25 +09:00
[Fix] (GL): validate program pipelines by the spec rules in glValidateProgramPipeline and on every pipeline draw
This commit is contained in:
@@ -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<GenericErrorInfo>("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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<SizeT>(ShaderStage::ShaderStageCount);
|
||||
const auto executes = [this](SizeT stage) {
|
||||
const auto& program = m_stagePrograms[stage];
|
||||
return program && program->HasLinkedShaderStage(static_cast<ShaderStage>(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<ShaderStage>(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<SizeT>(ShaderStage::TessControl)) ||
|
||||
executes(static_cast<SizeT>(ShaderStage::TessEval)) ||
|
||||
executes(static_cast<SizeT>(ShaderStage::Geometry));
|
||||
if (laterVertexProcessing && !executes(static_cast<SizeT>(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
|
||||
|
||||
@@ -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.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user