Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL "in" structs

Tags:

opengl

glsl

I'm not allowed to use a struct with the in keyword, am I? For example:

struct Rect {
    float x,y,width,height;
};

layout(location = 7) in Rect TexSrc;

It's difficult to Google, but I get an error when I try to use my GLSL shader (INVALID_OPERATION). It appears I'm allowed to use float[4] however.

If I'm not allowed to use a struct, what do you suggest I do? Construct the struct out of a float[4], or suck it up, forgo the nice API, and use indices 0-3 everywhere?

(A vec4 would work here as well, I know, but z and w don't have quite the same meaning as "width" and "height", and this is only one example)

I suppose you can only specify a single type per location with glVertexAttribPointer, so I can see why a struct would be disallowed... I'm just trying to think of a way to keep my code clean and ledgible without compromising performance.

like image 532
mpen Avatar asked Feb 14 '12 02:02

mpen


1 Answers

You cannot have vertex shader inputs of structs.

The correct way to do this is to just stick them in a vec4. If you use a float[4], this will require four attributes instead of just one. Arrays always take up one attribute per element. So if you do layout(location = 7) in float[4] TexSrc;, this will take up the attribute indices 7, 8, 9, and 10. That's an awful waste of attributes, and it could degrade performance.

Just use a vec4 like everyone else. People pack inputs in vec4s all the time; they use comments to explain any semantic oddities.

BTW, if you're getting an invalid operation, that probably means you didn't actually check to see if your shader compiled and linked properly. You should always do that.

New stuff

OpenGL 4.4 or the ARB_enhanced_layouts extension, allow you to do something like this.

As previously stated, the main problem with layout(location = 7) in float[4] TexSrc; is that it takes up 4 input locations. So if you have 4 floats that aren't really a 4D vector, you still have to pack them into a vec4 for shipping into the shader:

layout(location = 7) in vec4 rect;

This makes it a lot harder to know what rect.z means in the shader, since it doesn't have any explicit meaning.

4.4/enhanced_layouts allow you to pack multiple variables in a single location, so long as their components don't overlap. So if each location is conceptually a vec4, we can split that into 2 vec2s:

layout(location = 7, component = 0) in vec2 rectPos;
layout(location = 7, component = 2) in vec2 rectSize;

Now we get some semantic information. rectPos is the position of the rectangle, while rectSize is its size. And it still only takes up one location.

The enhanced layouts feature allows this to work for any inputs and outputs, not just vertex shaders.

like image 55
Nicol Bolas Avatar answered Sep 22 '22 18:09

Nicol Bolas