问题
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?
回答1:
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 X
th vertex from the primitive in the geometry shader, rather than having to declare each individual input variable as an array.
回答2:
Is it possible to declare a shader variable as both input and output?
No. Names of variables are identifiers and 2 different variables have to have different identifiers.
See GLSL - The OpenGL Shading Language 4.6; 3.7 Identifiers; page 18
Identifiers are used for variable names, function names, structure names, and field selectors ...
... More generally, it is a compile-time error to redeclare a variable, ...
But of course it is possible to declare an interface block with an instance name. As described in the answer of Nicol Bolas. In this case the variable can be accessed by the instance name of the interface block and the name of the variable.
But it is still not possible to use an interface block without an instance name and to reuse a variable name inside the instance block.
See GLSL - The OpenGL Shading Language 4.6; 4.3.9 Interface Blocks; page 54
If an instance name (instance-name) is not used, the names declared inside the block are scoped at the global level and accessed as if they were declared outside the block. If an instance name (instance-name) is used, then it puts all the members inside a scope within its own name space, accessed with the field selector ( . ) operator (analogously to structures).
来源:https://stackoverflow.com/questions/48244561/is-it-possible-to-declare-a-shader-variable-as-both-input-and-output