1

On my NVIDIA GPU these 2 shaders work fine:

// VS
#version 450
layout(location = 0) in vec4 inPosition;
layout(location = 0) out struct {
  vec2 dummy;
  uvec4 variableInStruct;
} testStruct;

void main(void)
{
  gl_Position = inPosition;
  testStruct.variableInStruct = uvec4(4, 16, 64, 256);
}


// PS
#version 450
layout(location = 0) out vec4 fragColor;
layout(location = 0) in flat struct {
  vec2 dummy;
  uvec2 variableInStruct;
} testStruct;

void main(void)
{
  float result = 0.0;
  result = float(testStruct.variableInStruct.x == 4) *
           float(testStruct.variableInStruct.y == 16);
  fragColor = vec4(result);
}

In the program PS input { <2 x float>, <2 x i32> } is only a subset of VS output { <2 x float>, <4 x i32> }, and it works fine on some hardware.

Did GLSL/Vulkan spec mention anything (legality, code examples) about such cases?

1 Answer 1

3

Assuming that you are using direct linking of the shader stages, the Spec demands that there is an exact match for all outputs/inputs between stages.

The OpenGL 4.6 Spec, Section 7.4.1 states:

At an interface between program objects, the set of inputs and outputs are considered to match exactly if and only if:

  • Every declared input block or variable has a matching output, as described above. However, the intrinsically declared gl_PerVertex shader interface block mustberedeclared, and all members of the redeclared gl_PerVertex shader interface block, including the gl_PointSize member if present in the redeclaration, must match exactly in name, type, qualification and declaration order.
  • There are no output blocks or user-defined output variables declared without a matching input block or variable declaration.

When the set of inputs and outputs on an interface between programs matches exactly, all inputs are well-defined except when the corresponding outputs were not written in the previous shader. However, any mismatch between inputs and outputs results in all inputs being undefined ...

For separate programs, the rules are slightly relaxed. For example, it is allowed to have ouputs that do not match an input as long as the output uses a layout qualifier. For details see OpenGL Wiki, Shader Compilation - Separate Program Matching

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.