PDA

View Full Version : FBO render to texture



seelofo
09-06-2010, 02:55 AM
hey guys,

i have a question regarding FBO and rendering to texture.

i'm implementing volume visualization and i tried to use an fbo with a texture color attachment .

say i have a 1000 slices that i draw front -to- back and blend using an "under operator" and multiply the alpha in the shader
glBlendFunc(GL_ONE_MINUS_DST_ALPHA,GL_ONE);
gl_FragColor.rgb *= gl_FragColor.a;

it's working fine ,but i tried to add early ray termination to my shader by accessing the 2D texture that is the color attachment to the fbo and discarding if alpha is >0.99
if (fboColor.a > 0.99) discard;

this is not working.... the question is: is the 2D texture color attachment updated properly during rendering so that the value accessed is valid?

thanks in advance.

Alfonse Reinheart
09-06-2010, 11:27 AM
the question is: is the 2D texture color attachment updated properly during rendering so that the value accessed is valid?

No. The specification is very clear on this: you may not both render to and simultaneously read from the same mipmap level of the same texture. If you do so, you will get undefined results.

kyle_
09-06-2010, 12:28 PM
These guys
http://www.sci.utah.edu/~bavoil/research/kbuffer/
used this technique to implement k-buffer. Afaik that was done on NV, and needed some geometry preprocessing to get predictable results.

seelofo
09-07-2010, 01:48 AM
the question is: is the 2D texture color attachment updated properly during rendering so that the value accessed is valid?

No. The specification is very clear on this: you may not both render to and simultaneously read from the same mipmap level of the same texture. If you do so, you will get undefined results.

weird !, because ,as a test, i tried to disable opengl blending and apply my own blending function in the shader and it worked perfectly..... is there something i'm misunderstanding here?

another thing: i'm trying to access the render texture so that i may conclude that no further calculations is required at this position, that is , i'm trying to reduce the number of processed fragments.

if (i should process this fragment)
{
do complex gradient and light calculations...
}
else
{
discard;
}

didn't work even a little bit ... so as a test i tried this

if (int(gl_FragCoord.x)%4 ==0 && int(gl_FragCoord.y)%4 ==0)// don't process all fragments
{
do complex gradient and light calculations...
}
else
{
discard;
}
still the same fps .... any thoughts on what may be the problem here ?

thanks in advance.

Alfonse Reinheart
09-07-2010, 02:33 AM
weird !, because ,as a test, i tried to disable opengl blending and apply my own blending function in the shader and it worked perfectly..... is there something i'm misunderstanding here?

I said you would get undefined results. That it may actually work sometimes in certain cases on certain hardware under certain phases of the moon does not mean that you can expect it to work always, everwhere.

Undefined is not good, even if it happens to do what you want now.


still the same fps .... any thoughts on what may be the problem here ?

OpenGL does not specify performance, only behavior. And "discard" does not necessarily mean "stop processing the fragment". It may ultimately run through all of your code even after it gets the "discard" command.

Also, is your performance fragment shader bound? That is, do you know that if you lower your fragment processing, you get a performance increase?

seelofo
09-07-2010, 03:51 AM
Also, is your performance fragment shader bound? That is, do you know that if you lower your fragment processing, you get a performance increase?


yes... because when i zoom out the fps increases( smaller polygons and less fragments)

and i tried showing only half the fragments and there was a significant boost.

actually ,i have a question about this ,that i hope is relevant to my problem here:

if (condition)
{
do complex gradient and light calculations...
}
else
discard;

when this condition was
int(gl_FragCoord.x)%2 ==0 && int(gl_FragCoord.y)%2 ==0 there was no performance increase

but when it was
(gl_FragCoord.x/width) <0.5 &amp;&amp; (gl_FragCoord.y/height) <0.5 there was a significant performance increase.

why is that?

even though both of them discards 3/4 of the whole fragment count but the first is scattered all over the screen.

does it have any thing to do with which fragments are assigned for which fragment processor ?

kyle_
09-07-2010, 12:42 PM
Because fragment shader is evaluated for groups of fragments (at least 2x2 frags, and most probably more than that) - not for each fragment separately. Discarding single fragment from group does not save you anything. Only discarding the whole group will give you perf benefit.

In second case you discard ~3/4 of groups, in first you dont discard any (only a few fragments in each group).