PDA

View Full Version : problem with return in shader



powerpad
07-29-2005, 09:34 AM
I have a small fragment program and I want to implement clipping.

if I use the following :


void clipRect(in float x0, in float y0, in float x1, in float y1, out vec2 e0c, out vec2 e1c)
{
e0c = vec2(1.0, 1.0);
e1c = vec2(1.0, 1.0);

bool bp0Accept = false;
bool bp1Accept = false;
bool earlyReturn= false;

if(x0 >= -1.0 &amp;&amp; x0 <= 1.0 &amp;&amp; y0 >= -1.0 &amp;&amp; y0 <= 1.0)
bp0Accept = true;
if(x1 >= -1.0 &amp;&amp; x1 <= 1.0 &amp;&amp; y1 >= -1.0 &amp;&amp; y1 <= 1.0)
bp1Accept = true;

return ;
}everything works fine. But if I insert a return statement before that, the fragment program does not work right.


void clipRect(in float x0, in float y0, in float x1, in float y1, out vec2 e0c, out vec2 e1c)
{
e0c = vec2(1.0, 1.0);
e1c = vec2(1.0, 1.0);

bool bp0Accept = false;
bool bp1Accept = false;
bool earlyReturn= false;

if(x0 >= -1.0 &amp;&amp; x0 <= 1.0 &amp;&amp; y0 >= -1.0 &amp;&amp; y0 <= 1.0)
bp0Accept = true;
if(x1 >= -1.0 &amp;&amp; x1 <= 1.0 &amp;&amp; y1 >= -1.0 &amp;&amp; y1 <= 1.0)
bp1Accept = true;

if(bp0Accept == true &amp;&amp; bp1Accept == true)
{
e0c = vec2(x0, y0);
e1c = vec2(x1, y1);
return ; //this return causes a problem
}

e0c = vec2(0.0, 0.0);
e1c = vec2(0.0, 0.0);

return ;
}I have left the rest of the shader out, since it seems to work.
Does anyone have some ideas what i did wrong or what might cause the problem here. I have the latest official drivers and a geforce 6600gt.

Korval
07-29-2005, 10:27 AM
See this (http://www.opengl.org/discussion_boards/cgi_directory/ultimatebb.cgi?ubb=get_topic;f=11;t=000838) thread.

Humus
07-29-2005, 01:57 PM
Sounds like nvidia has a driver bug. But you can probably work around it by doing like this instead:


if(bp0Accept == true &amp;&amp; bp1Accept == true){
e0c = vec2(x0, y0);
e1c = vec2(x1, y1);
} else {
e0c = vec2(0.0, 0.0);
e1c = vec2(0.0, 0.0);
}

powerpad
07-29-2005, 02:06 PM
this is just a short fragment out of a some more lines of code so this else won't do it (I left the rest out). I probably have to assign some variable indicating what to do :(

Peter_dup1
07-29-2005, 04:29 PM
I'd just put the rest of the code into another function


if(bp0Accept == true &amp;&amp; bp1Accept == true)
{
e0c = vec2(x0, y0);
e1c = vec2(x1, y1);
}
else
{
someFunction(...);
}
In my experience, 'if' statements usually aren't great for performance. If this is an issue, it might be faster if you do something like this;


vec4 input = vec4( x0, y0, x1, y1 );
if( dot(step( -1.0, input ), step( input, 1.0 )) == 4 )
{
e0c = vec2(x0, y0);
e1c = vec2(x1, y1);
}
else
{
someFunction(...);
}Of course, readability suffers.

Peter

powerpad
07-29-2005, 09:38 PM
this will be a problem since there are more than one if in my function, I would have to write several somefunction() functions :) . I will see what I can do..

thanks for your replies