PDA

View Full Version : Xfer feedback variable optimized away



john_connor
12-08-2016, 05:06 AM
hi everyone,

i'm trying to make a little application with transform feedback, i want to capture all (transformed) vertex attributes and want to reuse them the next frame (double-buffered)
capturing variables already works, but i assume the problem is the glsl compiler that "optimizes" the vertex shader and discarts unused variables

the working example:
vertex shader:

#version 450 core

in layout (location = 0) vec3 in_position;
in layout (location = 1) vec4 in_color;

uniform layout (location = 0) mat4 MVP = mat4(1);

layout (xfb_buffer = 0, xfb_offset = 0, xfb_stride = 28) out vec3 position;
layout (xfb_buffer = 0, xfb_offset = 12, xfb_stride = 28) out vec4 color;

void main ()
{
gl_Position = MVP * vec4(in_position, 1);
position = (MVP * vec4(in_position, 1)).xyz;
color = in_color;
}
fragment shader:

#version 450 core

in vec3 position;
in vec4 color;

out layout (location = 0) vec4 out_color;

void main ()
{
out_color = color + vec4(position, 0);
}

as you can see, the fragment shader does some "weird" stuff, i just added a position to "persuay" the compiler to not discard the variable, but that's just for testing purposes

of course i want to have this:
fragment shader:

#version 450 core

in vec4 color;

out layout (location = 0) vec4 out_color;

void main ()
{
out_color = color;
}

but when i do that, the application definitely wont capture the "position" attribute anymore
(i retrieved all transform feedback varyings and the captured data to test this)


what can i do ??
thanks in advance!

Dark Photon
12-08-2016, 06:30 AM
i'm trying to make a little application with transform feedback, i want to capture all (transformed) vertex attributes...

i assume the problem is the glsl compiler that "optimizes" the vertex shader and discarts unused variables ...

the working example:
vertex shader:

...
layout (xfb_buffer = 0, xfb_offset = 0, xfb_stride = 28) out vec3 position;
layout (xfb_buffer = 0, xfb_offset = 12, xfb_stride = 28) out vec4 color;
...

fragment shader:

...
out_color = color + vec4(position, 0);
...


as you can see, the fragment shader does some "weird" stuff, i just added a position to "[persuade]" the compiler to not discard the variable, but that's just for testing purposes

Hmm, ok. Since you're doing transform feedback, why do you even have a fragment shader? I'd get rid of that (that is, only link a vertex shader into your shader program). See if the linker removes position then.

If so, you might consider using glTransformFeedbackVaryings to set the TF outputs instead of the layout qualifiers. Just be sure to call this "before" you link your program. See this: Transform Feedback (https://www.opengl.org/wiki/Transform_Feedback) (OpenGL Wiki)

Probably not related, but you are setting rasterizer discard (https://www.opengl.org/wiki/Primitive_Assembly#Rasterizer_discard), right?

john_connor
12-08-2016, 07:11 AM
thanks for your reply!


Hmm, ok. Since you're doing transform feedback, why do you even have a fragment shader? I'd get rid of that (that is, only link a vertex shader into your shader program). See if the linker removes position then.

i tried that --> it works!
but the thing is i want to do a kind of "buffer pingponging"
that means WHILE rendering a triangle, i want to capture the transformed vertex in buffer 2
when done, i use buffer 2 as GL_ARRAY_BUFFER and feed the vertices into the same program again, capturing every thing into buffer 1 (ping-pong)
so the point is i need the fragmentshader (beside 2 VAO, 2 VBO, 2 Xfer feedback objects)



If so, you might consider using glTransformFeedbackVaryings to set the TF outputs instead of the layout qualifiers. Just be sure to call this "before" you link your program. See this: Transform Feedback (https://www.opengl.org/wiki/Transform_Feedback) (OpenGL Wiki)

glTransformFeedbackVaryings(...) is exactly what i want to avoid ;)
(used it before, works perfectly)




Probably not related, but you are setting rasterizer discard (https://www.opengl.org/wiki/Primitive_Assembly#Rasterizer_discard), right?

nope

i also tried to set

#version 450 core\n
#pragma optimize(off)\n
in both shaders (vertex / fragment) --> doesnt work

by the way: doesnt work in my code pieces ?!? why ?

[highlight=cpp]void main() {
int test = 2;
}

GClements
12-08-2016, 02:03 PM
by the way: [highlight=cpp] doesnt work in my code pieces ?!? why ?

You need to use [highlight] instead of [code], not inside it.

Dark Photon
12-08-2016, 06:05 PM
...WHILE rendering a triangle, i want to capture the transformed vertex in buffer 2...so the point is i need the fragmentshader (beside 2 VAO, 2 VBO, 2 Xfer feedback objects)

Ok. Well as far as I know what you're doing is fine. What GPU/driver/OS is this?

I'd suggest that you double-check the spec just to verify that your/my understanding that this is valid is correct. And assuming so, file a bug report with the company/organization that maintains the drivers you're using.

For now I guess you can continue to use a work-around similar to what you are using, but just change the expression so it can have no real effect on the resulting color, and yet won't allow the compiler to optimize it away.

john_connor
12-09-2016, 03:39 AM
What GPU/driver/OS is this?

a usual desktop pc:
-- windows 10 home (x64)
-- NVIDIA GeForce GT 640
-- driver 369.09 (outdated)
-- driver 376.19

OpenGL 4.5 core profile and GLSL 450 core
my driver 369.09 was outdated, but that doesnt matter, my example doesnt work neither with 369.09, nor with 376.19
(i've just installed 376.19)



I'd suggest that you double-check the spec just to verify that your/my understanding that this is valid is correct. And assuming so, file a bug report with the company/organization that maintains the drivers you're using.

triple-checked it ... i dont violate any GLSL 450 core rule
https://www.opengl.org/registry/doc/GLSLangSpec.4.50.pdf

i played around, used interface blocks, etc .. nothing worked
(until i pass the "position" attribute to the fragmentshader and use it there anyhow)

i'll report NVIDIA the bug

Silence
12-09-2016, 04:03 AM
a usual desktop pc:
-- windows 10 home (x64)

parenthesis_open()

Is this an usual desktop PC ?

parenthesis_close()

Dark Photon
12-09-2016, 05:48 AM
From what you've indicated, seems pretty typical to me.