早期退出GPU上的循环值得做什么?

nro*_*rob 3 shader gpu opengl-es glsl webgl

我们编写了GLSL着色器代码,使用GPU进行光线跟踪可视化.在光线行进循环中提前退出中断似乎是非常标准的,因此如果光线熄灭,则循环中断.

但根据我对GPU代码的了解,每次渲染都需要与最长的循环运行一样长.所以我的问题是:是否值得提前退出?

例如

for(int i = 0; i < MAX_STEPS; i++){
        //Get the voxel intensity value from the 3D texture.    
        dataRGBA = getRGBAfromDataTex(dataTexture, currentPosition, dataShape, textureShape);

        // get contribution from the light
        lightRayPathRGBA = getPathRGBA(currentPosition, light.position, steps, tex); // this is the light absorbed so we need to take 1.0- to get the light transmitted
        lightRayRGBA = (vec4(1.0) - lightRayPathRGBA) * vec4(light.color, light.intensity);

        apparentRGB = (1.0 - accumulatedAlpha) * dataRGBA.rgb * lightRayRGBA.rgb * dataRGBA.a * lightRayRGBA.a;
        //apparentRGB = (1.0 - accumulatedAlpha) * dataRGBA.rgb * dataRGBA.a * lightRayRGBA.a;

        //Perform the composition.
        accumulatedColor += apparentRGB;
        //Store the alpha accumulated so far.
        accumulatedAlpha += dataRGBA.a;

        //Adva      nce the ray.
        currentPosition += deltaDirection;
        accumulatedLength += deltaDirectionLength;

        //If the length traversed is more than the ray length, or if the alpha accumulated reaches 1.0 then exit.
        if(accumulatedLength >= rayLength || accumulatedAlpha >= 1.0 ){
            break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

use*_*016 8

GPU的调度单元是warp/wavefront.通常是32或64个线程的连续组.warp的执行时间是该warp中所有线程的执行时间的最大值.

因此,如果您提前退出可以使整个warp更快终止(例如,如果线程0到31 采用提前退出),那么是的,这是值得的,因为硬件可以安排另一个warp来执行,这会降低整体内核运行时间.否则,它可能不是,因为即使线程1到31采用提前退出,warp仍然占用硬件,直到线程0完成.

  • @nrob:Cicada 的完美回答和评论。本质上可以归结为,GPU 将处理 n×m 片段的块(扭曲/波前)中的像素(n 和 m 因 GPU 而异,但对于当前硬件来说通常都在 8 到 32 之间)。对于该块中的每个片段,程序流程都是相同的,并且在扭曲/波前内出现分歧的情况下,将采用任一路径。因此,如果扭曲/波前中只有一个片段提前终止,那么您赚得很少。但如果你能让整个扭曲/波前提前终止,你就可以释放 GPU 核心和周期。 (2认同)
  • @nrob:旁注:人们在学习 GPU 原子计数器时要做的第一件事就是可视化光栅化模式(即 GPU 如何将片段调度到扭曲/波前)。请参阅这篇文章:http://www.geeks3d.com/20120309/opengl-4-2-atomic-counter-demo-rendering-order-of-fragments/ (2认同)