Search code examples
openglglslshader

Is it possible to declare a shader variable as both input and output?


I'm using both a vertex shader and a geometry shader. My vertex shader does nothing more than forward its input to the geometry shader.

#version 330 core
layout (location = 0) in uint xy;
layout (location = 1) in uint znt;

out uint out_xy;
out uint out_znt;

void main()
{
    out_xy = xy;
    out_znt = znt;
}

Is it possible to declare xy and znt as both an input and an output, so that I don't need to rename them?


Solution

  • You cannot "declare" them that way, but you can use interface blocks, which can give you essentially the same thing:

    //Vertex shader:
    
    layout (location = 0) in uint xy;
    layout (location = 1) in uint znt;
    
    out VS
    {
       uint xy;
       uint znt;
    } to_gs;
    
    void main()
    {
        to_gs.xy = xy;
        to_gs.znt = znt;
    }
    
    //Geometry shader:
    
    in VS
    {
       uint xy;
       uint znt;
    } from_vs[];
    
    void main()
    {
    }
    

    You have to use instance names on these blocks so that GLSL knows what variables you're talking about. But otherwise, they have the same name. This also allows you to use from_vs[X] to select the Xth vertex from the primitive in the geometry shader, rather than having to declare each individual input variable as an array.