Skip to content

Improve Inter-Stage Shader Variable Validation #979

Description

@bjornbytes

From chat:

local Shader = lovr.graphics.newShader([[
out vec3 col1;
out vec3 col2;
vec4 lovrmain() {
  col1 = vec3(1, 0, 0);
  col2 = vec3(0, 0, 1);
  return DefaultPosition;
}
]], [[
in vec3 col2;
in vec3 col1;
vec4 lovrmain() {
  return vec4(col1, 1);  // col1 is blue
}
]])

I realized that omitting explicit location qualifiers can be dangerous. The generated locations depend on declaration order, while traditional (non-Vulkan) GLSL stage interfaces are order-independent. This means simply reordering interface variables can silently change the generated locations and break shader interface matching.
Would it be possible to assign locations based on variable names instead of declaration order?
Alternatively, could Lovr validate that fragment shader inputs match vertex shader outputs by name?

The hardest part is probably expanding spv.c to parse output variables (instead of attributes, it should be a more general inputs / outputs list probably). After that is added, lovrShaderCreate can add some logic to do the validation (or assign the locations better).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions