Sign in to follow this  

GLSL: Branching performance problems

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a strange problem using this function underneath in a fragment shader. The functions is looking for the index 'i' of the beginning of the interval [t_i<= u < t_j] I does a binary search on a row of a texture The input is a 32bit RGBA floating point texture which looks like this an array is encapsulated into an array of vec4s 0 0 0 0 1 2 2 2 2 -> [0 0 0 0][1 2 2 2][2 0 0 0] If i pass a parameter u >= 2, this function becomes horribly slow. The variable j counts the number of texture lookups. The strange thing is, the algorithm works correctly, but the output of return i; is pretty slow. outputting return j; works just fine and I have no clue why Is there something wrong with the code underneath? I can t reproduce the performance leaps when commenting in & out "return i;" This only happens if u>= the largest value of the input array, for values < max value it works just fine thx in advance P.S.: I am working on a 8800GT Suse 10.0
  1 int find_interval(float u, int start, int end, int row, float uoffset, float voffset, sampler2D tex)
  2 {
  3         bool done=false;
  4         ivec2 range = ivec2(start,end/4);
  5         vec4 elem_a = vec4(-1.0);
  6         vec4 elem_b = vec4(-1.0);
  7         int i;
  8         int j=0;
  9         while(true)
 10         {
 11                 ++j;
 12                 i=(range.x+range.y)/2;
 13                 elem_a = element(i,row,uoffset,voffset,tex);
 14                 if(u<elem_a.x)
 15                 {
 16                         range.y=i;
 17                         if(range.x==range.y)
 18                                 break;
 19                 }
 20                 else if(u>=elem_a.w)
 21                 {
 22                         elem_b = element(i+1,row,uoffset,voffset,tex);
 23                         ++j;
 24                         if(u<elem_b.x) {i=i*4+3; done=true; break;}
 25
 26                         range.x = i+1;
 27                         if(range.x==range.y)
 28                         {
 29                                 if(elem_b.x<=u && u<elem_b.y)           {i=i*4+4+0; done=true; break;}
 30                                 else if(elem_b.y<=u && u<elem_b.z)      {i=i*4+4+1; done=true; break;}
 31                                 else if(elem_b.z<=u && u<elem_b.w)      {i=i*4+4+2; done=true; break;}
 32                                 else
 33                                 {
 34                                         break;
 35                                 }
 36                         }
 37                 }
 38                 else if(elem_a.x<=u && u<elem_a.y) {i=i*4+0; done=true; break;}
 39                 else if(elem_a.y<=u && u<elem_a.z) {i=i*4+1; done=true; break;}
 40                 else if(elem_a.z<=u && u<elem_a.w) {i=i*4+2; done=true; break;}
 41         }
 42         if(i>=end)
 43                 i=-1;
 44
 45         if(done==false)
 46                 i=-1;
 47
 48         return i;
 49         //return j;
 50 };



Share this post


Link to post
Share on other sites
Hm
just in the moment I posted this I thought about a little optimization

I did i=-1 at the breaks where the search fails instead and now it seems to work just fine.

strange anyways


1 int find_interval(float u, int start, int end, int row, float uoffset, float voffset, sampler2D tex)
2 {
3 bool done=false;
4 ivec2 range = ivec2(start,end/4);
5 vec4 elem_a = vec4(-1.0);
6 vec4 elem_b = vec4(-1.0);
7 int i;
8 int j=0;
9 while(true)
10 {
11 ++j;
12 i=(range.x+range.y)/2;
13 elem_a = element(i,row,uoffset,voffset,tex);
14 if(u<elem_a.x)
15 {
16 range.y=i;
17 if(range.x==range.y)
18 {
19 i=-1;
20 break;
21 }
22 }
23 else if(u>=elem_a.w)
24 {
25 elem_b = element(i+1,row,uoffset,voffset,tex);
26 ++j;
27 if(u<elem_b.x) {i=i*4+3; done=true; break;}
28
29 range.x = i+1;
30 if(range.x==range.y)
31 {
32 if(elem_b.x<=u && u<elem_b.y) {i=i*4+4+0; done=true; break;}
33 else if(elem_b.y<=u && u<elem_b.z) {i=i*4+4+1; done=true; break;}
34 else if(elem_b.z<=u && u<elem_b.w) {i=i*4+4+2; done=true; break;}
35 else
36 {
37 i=-1;
38 break;
39 }
40 }
41 }
42 else if(elem_a.x<=u && u<elem_a.y) {i=i*4+0; done=true; break;}
43 else if(elem_a.y<=u && u<elem_a.z) {i=i*4+1; done=true; break;}
44 else if(elem_a.z<=u && u<elem_a.w) {i=i*4+2; done=true; break;}
45 }
46 if(i>=end)
47 i=-1;
48
49
50 return i;
51 //return j;
52 };

Share this post


Link to post
Share on other sites

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this