Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL fold/reduce implementation?

I've figure out how to translate the higher order functions map and filter into OpenGL code by using transform feedback (or by rendering to a texture). I'd also like to be able to use fold, but I have no idea how this would work. Let's assume that the operation is associative, so I don't care if it's a left fold or a right fold or some nondeterministic mix.

Examples of fold operations:

  • Calculate the smallest AABB containing a set of points (fold over a vertex array).
  • Calculate the brightest value in a texture (fold over a texture).

Or is this not feasible without OpenCL or CUDA?

like image 305
Dietrich Epp Avatar asked Oct 29 '12 19:10

Dietrich Epp


1 Answers

If your operation is associative you can reduce/fold your data by repeatedly rendering to a smaller texture. In each pass you combine a number of texels from the previous pass. You read the data from the previous pass by binding it as a texture for your fragment shader.

For example if you wanted to compute the average of an image of 128x128 values you could first render to a texture of 64x64 where you average 4 texels in the input texture for each texel in the target texture.

A typical example is blurring an image.

like image 150
ThomasD Avatar answered Sep 28 '22 04:09

ThomasD