GLSL整合功能

问题描述:

关于如何在GLSL着色器中实现有效的积分功能(如SumX和SumY)的任何建议?

Any recommendation on how to implement efficient integral functions, like SumX and SumY, in GLSL shaders?

SumX(u)=关于x的积分= I(u0,y)+ I(u1,y)+ ... + I(uN,y); u =归一化的x坐标 SumY(v)=关于y的积分= I(x,v0)+ I(x,v1)+ ... + I(x,vN); v =归一化的y坐标

SumX(u) = Integration with respect to x = I(u0,y) + I(u1,y) +... + I(uN,y); u=normalized x coordinate SumY(v) = Integration with respect to y = I(x,v0) + I(x,v1) +... + I(x,vN); v=normalized y coordinate

例如,第一行的第五个像素将是第一行中所有五个像素的总和.最后一个像素将是所有先前像素的总和,包括最后一个像素本身.

For instance the 5th pixel of the first line would be the sum of all five pixels on the first line. And the last pixel would be the sum of all previous pixels including the last pixel itself.

您要的内容称为前缀总和总面积表(SAT)用于2D情况(只是这样,您可以更轻松地找到在线资源).

What you are asking for is called prefix sum or summed area table (SAT) for the 2D case (just so you find online resources more easily).

通过将多个并行前缀总和分解为 [ 1] [2] .
前缀总和可以通过使用本地内存存储中间部分总和来加速(请参阅OpenCL中的示例) CUDA中的示例,在原理上可以在OpenGL片段着色器中完成与可以很好地与图像加载存储一起使用,也可以在计算着色器中使用:

Summed area tables can be efficiently implemented on the GPU by decomposing into several parrallel prefix sum passes [1], [2].
The prefix sum can be accelerated by using local memory to store intermediate partial sums (see example in OpenCL or example in CUDA, the same can in principle be done in an OpenGL fragment shader as well with image load-store, or in a compute shader: OpenGL Super Bible example, similar example to be found in OpenGL Insights around page 280).

请注意,您可能会很快遇到精度问题,因为对于最右边(最下面)的像素,总和可能会变得很大.整数或fp16渲染目标很可能会由于溢出或精度不足而导致失败,fp32大部分时间都可以工作.

Note that you may quickly run into precision issues as the sum may get quite large for the rightmost (downmost) pixels. Integer or fp16 render targets will most likely result in failure due to overflow or lacking precision, fp32 will work most of the time.