0xbadf00d
0xbadf00d

Reputation: 18178

Using input/output structs in GLSL-Shaders

In HLSL I can write:

struct vertex_in
{
    float3 pos    : POSITION;
    float3 normal : NORMAL;
    float2 tex    : TEXCOORD;
};

and use this struct as an input of a vertex shader:

vertex_out VS(vertex_in v) { ... }

Is there something similar in GLSL? Or do I need to write something like:

layout(location = 0) in vec4 aPosition;
layout(location = 1) in vec4 aNormal;
...

Upvotes: 3

Views: 7625

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

What you are looking for are known as interface blocks in GLSL.

Unfortunately, they are not supported for the input to the Vertex Shader stage or for the output from the Fragment Shader stage. Those must have explicitly bound data locations at program link-time (or they will be automatically assigned, and you can query them later).

Regarding the use of HLSL semantics like : POSITION, : NORMAL, : TEXCOORD, modern GLSL does not have anything like that. Vertex attributes are bound to a generic (numbered) location by name either using glBindAttribLocation (...) prior to linking or as in your example, layout (location = ...).

For input / output between shader stages, that is matched entirely on the basis of variable / interface block name during GLSL program linking (except if you use the relatively new Separate Shader Objects extension). In no case will you have constant pre-defined named semantics like POSITION, NORMAL, etc. though; locations are all application- or linker-defined numbers.

Upvotes: 6

Related Questions