Problem with return in shader

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 && x0 <= 1.0 && y0 >= -1.0 && y0 <= 1.0)
		bp0Accept = true;
	if(x1 >= -1.0 && x1 <= 1.0 && y1 >= -1.0 && 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 && x0 <= 1.0 && y0 >= -1.0 && y0 <= 1.0)
		bp0Accept = true;
	if(x1 >= -1.0 && x1 <= 1.0 && y1 >= -1.0 && y1 <= 1.0)
		bp1Accept = true;

	if(bp0Accept == true && 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.

See this thread.

Sounds like nvidia has a driver bug. But you can probably work around it by doing like this instead:

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

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 :frowning:

I’d just put the rest of the code into another function

if(bp0Accept == true && 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

this will be a problem since there are more than one if in my function, I would have to write several somefunction() functions :slight_smile: . I will see what I can do…

thanks for your replies

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.