I am looking for an easy solution for passing information between shader stages when adding additional shader stages. Right now I am using the "out" keyword to pass data from for example vertex to fragment shader. On the fragment shader of cause I am using "in" to receive the data.
The problem I have is the following: The variable name MUST be the same. So "out vec3 test;" can only be received by variable "in vec3 test;". When optionally plug in a geometry shader to duplicate triangles, I would like to receive the outputting data from the vertex shader "in vec3 test" and pass it to the fragment shader "out vec3 test". And here the program crashes... because both variables names are "test"
Here the Vertex shader code:
#version 430
layout(location = 0) in vec3 vertexPosition;
out vec3 test;
void main()
{
gl_Position = vec4(vertexPosition, 1.0f);
test = vec3(1.0f, 2.0f, 3.0f); //this is just a test!
}
The Fragment shader code:
#version 430
in vec3 test;
out vec4 color;
void main()
{
color = vec4(test, 1.0f);
}
So the last shader (geometry shader) should be able to plug in if wanted, without changing the vertex or fragment shader.
The Geometry shader code:
#version 430 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 30) out;
in vec3 test[];
out vec3 test; // this does not work (name already exists)
void main()
{
vec4 position0 = gl_Position[0];
vec4 position1 = gl_Position[1];
vec4 position2 = gl_Position[2];
for(uint i = 0; i < 10; ++i)
{
gl_Position = position0 + vec4(i,0,0,0);
test = test[0]; //this of cause does not work... (I just want to pass this information)
EmitVertex();
gl_Position = position1 + vec4(i,0,0,0);
test = test[1]; //this of cause does not work...
EmitVertex();
gl_Position = position2 + vec4(i,0,0,0);
test = test[2]; //this of cause does not work...
EmitVertex();
EndPrimitive();
}
}
This geometry shader should copy the input triangle 10 times and pass the "varying" data from vertex to fragment shader.
So the goal is that the vertex and fragment shader do not need to be changed when adding or leaving out the geometry shader.
We would like to do stuff like that for example to enable/disable layered rendering, adding a tessellation shader when wanted. Right now we need to write several shaders for the same thing. For example when having the option of enable/disable layered rendering and enable/disable tessellation there are 2^2 = 4 possibilities. This means we need to write the same shader 4 times. With this approach we only need to write it once and just plug in the shader stages when needed.
I am also interested in dynamically read out type and values of passed data. So ask within the geometry shader which variables are passed and then forward them to the next stage. With this approach we would be able to write one geometry shader which can be plugged between several other shaders.
What possibilities do we have to generate such a system?