0

Assuming I have a few hundred varying size adjoining ranges 0-100,101-300,301-1000,1001-2000 etc. What would be the fastest way for me to find which range a given integer falls into using HLSL/GLSL?

The ranges will be stored in a constant buffer and I need to find the range from within a vertex shader.

The current brute force approach I am using is far too slow.

int index = 0;
int count = Lookup[index].count;
while (integer > count){
    count += Lookup[index++].count;
}
return index;

Anyone have any ideas?

Dean North
  • 3,741
  • 2
  • 29
  • 30
  • First thing come in my mind: How about binary search instead of linear? The only condition would be that your ranges are sorted and it would be much faster. http://en.wikipedia.org/wiki/Binary_search_algorithm – Gnietschow Jul 11 '14 at 17:04
  • That's exactly what I was after. Thanks! Post your comment as an answer and I will accept it. – Dean North Jul 14 '14 at 18:49

1 Answers1

1

I would suppose to use binary search instead of linear. This would improve the speed from O(n) to O(log n). The only condition would be that your ranges are sorted and it would be much faster.

Wikipedia-Entry for binary search

Gnietschow
  • 3,070
  • 1
  • 18
  • 28