PDA

View Full Version : Passing integer from vertex to fragment shader?



Yongjin Kim
11-29-2010, 07:22 PM
When I tried to pass an integer from a vertex shader to a fragment shader, i get "invalid operation". I can expect it may have some trouble with interpolation performed in the rasterization stage. What I want to know is whether this is actually a prohibited operation or not. I think if integer passing is available, item buffer generation would be much easier.

My code is as follows.

//----- vertex shader -----
#version 150 compatibility

out vec4 pos;
out vec3 normal;
out uint id;

void main() {
pos = gl_Vertex;
normal = gl_Normal;
id = 123u;
gl_Position = ftransform();
}


//----- fragment shader -----
#version 150 compatibility

in vec4 pos;
in vec3 normal;
in uint id;

out vec4 fragPosition;
out vec4 fragNormal;
out uvec4 fragID;

void main()
{
fragPosition = pos;
fragNormal = vec4(normal,1);
fragID = uvec4(id,0,0,1);
}

Alfonse Reinheart
11-29-2010, 08:03 PM
When I tried to pass an integer from a vertex shader to a fragment shader, i get "invalid operation".

Did the program link successfully? If there was a problem with the shader, it should have failed to link. So are you sure that the problem is with the integer value?


I think if integer passing is available, item buffer generation would be much easier.

What is an "item buffer?"

Yongjin Kim
11-29-2010, 08:38 PM
Link was successful, but I get errors when I do glUseProgram(program).

Item buffer is an image where each pixel has an unique id of its primitive. If we generate an item buffer of a cube, then pixels of 3 visible faces will have IDs of faces. It is used for ray tracing acceleration and line visibility testing.
- WEGHORST, H., HOOPER, G., AND GREENBERG, D. P. 1984.
Improved computational methods for ray tracing. ACM Transactions
on Graphics 3, 1 (Jan.), 5269.
- MARKOSIAN, L., KOWALSKI, M. A., GOLDSTEIN, D.,
TRYCHIN, S. J., HUGHES, J. F., AND BOURDEV, L. D. 1997.
Real-time nonphotorealistic rendering. In SIGGRAPH 97: Proceedings
of the 24th annual conference on Computer graphics
and interactive techniques, 415420

Alfonse Reinheart
11-29-2010, 11:13 PM
but I get errors when I do glUseProgram(program).

glUseProgram only generates errors if the program object is not a valid program object (ie: not returned by glCreateProgram) or if it was not successfully linked. So it looks like there's something going on there.

Yongjin Kim
11-29-2010, 11:42 PM
Thank you for your answer.
It was my mistake. My program only displayed compile errors but link errors printing part was commented out. The link error said
"error C5215: Integer varying id must be flat"
I added 'flat' in front of 'out' like

flat out uint id;

and the problem solved.

Thank you!!

Alfonse Reinheart
11-30-2010, 12:12 AM
"error C5215: Integer varying id must be flat"

Wow, I had to dig deep into the spec to find that one. I found it in the section on clipping of all things. I'd never have guessed it (but it does make some sense), but apparently this is true.