WebGL上的快速纹理乒乓实现

时间:2014-08-02 10:34:06

标签: opengl-es webgl gpgpu render-to-texture

我试图在WebGL上实现GPGPU计算,我需要存储中间结果。整个计算并不适合一个片段着色器,因此我必须将其分成几轮。

我已经实现了纹理乒乓技术,我有两个纹理,可以交换每个渲染。

我有数千个渲染回合来获取结果数据,其中新数据依赖于之前的(不可能并行执行)。我有一个很大的纹理来存储所有数据,但计算需要在每一轮的几个像素上(每轮8-20像素,纹理是1024x1024)。

我的典型片段着色器代码如下:

void main () {
    vec4 c = gl_FragCoord - 0.5;
    float position = (c.y * TEXTURE_SIZE) + c.x;
    float offset = mod(position, BLOCK_SIZE);
    float block = floor(position / BLOCK_SIZE);

    if ( offset >= (TMP_WORK_OFFSET) && offset < (TMP_WORK_OFFSET + WORKS_PER_ROUND)) {
         //Do the computation here
    } else {
         //Just return the pixel from the texture
         gl_FragColor = texture2D(uSampler, vTextCoord.st);
    }
}

目前,我每一轮渲染整个帧,我想配置顶点以仅渲染所需的像素以进行优化。第一个想法如下:

1. Set full frame squad vertecies.
2. Switch to simpleCopy shader program.
3. Copy texture to the framebuffer.
4. Switch to shader program to needed for computations.
5. Set vertices only for needed pixels.
6. Render computations to the framebuffer.
7. Swap textures and go to the #1

如您所见,计算本身之前有5个步骤。我担心,每一轮改变着色器程序和顶点(你记得,我有成千上万的短轮来完成工作)会产生巨大的开销。

另一个问题,它会不仅仅是:

1. Render whole frame with needed complex shader program (which returns the origin pixel on unused pixels);
2. Swap textures and go to the #2.  

我可以做哪些其他优化?

0 个答案:

没有答案
相关问题