【发布时间】:2014-07-15 16:48:47
【问题描述】:
我正在实现平铺延迟着色,为此我需要计算平铺的最小/最大深度值。我正在为每个图块渲染 1 个像素,并在嵌套的 for 循环中收集深度值,如下所示:
float minDepth = 1.0;
float maxDepth = 0.0;
ivec2 clampMax = ivec2(screenSize) - 1;
// Iterate over each pixel in this tile
for (int x = 0; x < 32; x++) {
for (int y = 0; y < 32; y++) {
ivec2 newCoord = screenCoord + ivec2(x,y);
newCoord = min(newCoord, clampMax);
// Fetch the depth for that coordinate
float currentDepth = texelFetch(depth, newCoord, 0).r;
minDepth = min(minDepth, currentDepth);
maxDepth = max(maxDepth, currentDepth);
}
}
到目前为止,这工作正常,但查看生成的程序集,纹理查找得到如下内容:
// R2.xy contains 'newCoord'
MOV.S R2.z, {0, 0, 0, 0}.x;
TXF.F R1.x, R2.xyzz, handle(D0.x), 2D;
基本上等于:
vec3 coordinate;
coordinate.xy = newCoord;
coordinate.z = 0;
result = texelFetch(depth, coordinate);
因此它为纹理查找生成了一条额外的不必要指令,在这样的循环中总结了很多。我的猜测是,NVIDIA 内部将 texelFetch 实现为
texelFetch(sampler2D sampler, ivec3 coord)
回到问题:你会如何优化这个循环?
我在 Windows 上使用带有最新驱动程序的 GTX 670。
【问题讨论】:
标签: opengl glsl depth-buffer