任务背景
我试图在OGLDev Tutorial 45之后实现SSAO,该教程基于 John Chapman的教程。OGLDev 教程使用一种高度简化的方法,该方法在片段位置周围的半径内随机采样点,并根据有多少采样点的深度大于存储在该位置的实际表面深度(位置越多)来提高 AO 因子片段周围位于其前面,遮挡越大)。
我使用的“引擎”没有 OGLDev 那样的模块化延迟着色,但基本上它首先将整个屏幕颜色渲染到带有纹理附件和深度渲染缓冲区附件的帧缓冲区。为了比较深度,片段视图空间位置被渲染到另一个带有纹理附件的帧缓冲区。然后这些纹理由 SSAO 着色器进行后处理,并将结果绘制到屏幕填充四边形。两种纹理都可以很好地绘制到四边形,着色器输入制服似乎也可以,所以这就是为什么我没有包含任何引擎代码的原因。
片段着色器几乎相同,如下所示。我已经包含了一些符合我个人理解的评论。
#version 330 core
in vec2 texCoord;
layout(location = 0) out vec4 outColor;
const int RANDOM_VECTOR_ARRAY_MAX_SIZE = 128; // reference uses 64
const float SAMPLE_RADIUS = 1.5f; // TODO: play with this value, reference uses 1.5
uniform sampler2D screenColorTexture; // the whole rendered screen
uniform sampler2D viewPosTexture; // interpolated vertex positions in view space
uniform mat4 projMat;
// we use a uniform buffer object for better performance
layout (std140) uniform RandomVectors
{
vec3 randomVectors[RANDOM_VECTOR_ARRAY_MAX_SIZE];
};
void main()
{
vec4 screenColor = texture(screenColorTexture, texCoord).rgba;
vec3 viewPos = texture(viewPosTexture, texCoord).xyz;
float AO = 0.0;
// sample random points to compare depths around the view space position.
// the more sampled points lie in front of the actual depth at the sampled position,
// the higher the probability of the surface point to be occluded.
for (int i = 0; i < RANDOM_VECTOR_ARRAY_MAX_SIZE; ++i) {
// take a random sample point.
vec3 samplePos = viewPos + randomVectors[i];
// project sample point onto near clipping plane
// to find the depth value (i.e. actual surface geometry)
// at the given view space position for which to compare depth
vec4 offset = vec4(samplePos, 1.0);
offset = projMat * offset; // project onto near clipping plane
offset.xy /= offset.w; // perform perspective divide
offset.xy = offset.xy * 0.5 + vec2(0.5); // transform to [0,1] range
float sampleActualSurfaceDepth = texture(viewPosTexture, offset.xy).z;
// compare depth of random sampled point to actual depth at sampled xy position:
// the function step(edge, value) returns 1 if value > edge, else 0
// thus if the random sampled point's depth is greater (lies behind) of the actual surface depth at that point,
// the probability of occlusion increases.
// note: if the actual depth at the sampled position is too far off from the depth at the fragment position,
// i.e. the surface has a sharp ridge/crevice, it doesnt add to the occlusion, to avoid artifacts.
if (abs(viewPos.z - sampleActualSurfaceDepth) < SAMPLE_RADIUS) {
AO += step(sampleActualSurfaceDepth, samplePos.z);
}
}
// normalize the ratio of sampled points lying behind the surface to a probability in [0,1]
// the occlusion factor should make the color darker, not lighter, so we invert it.
AO = 1.0 - AO / float(RANDOM_VECTOR_ARRAY_MAX_SIZE);
///
outColor = screenColor + mix(vec4(0.2), vec4(pow(AO, 2.0)), 1.0);
/*/
outColor = vec4(viewPos, 1); // DEBUG: draw view space positions
//*/
}
什么有效?
- 片段颜色纹理是正确的。
- 纹理坐标是我们绘制并转换为 [0, 1] 的屏幕填充四边形的坐标。它们产生等效的结果
vec2 texCoord = gl_FragCoord.xy / textureSize(screenColorTexture, 0);
- (透视)投影矩阵是相机使用的矩阵,它适用于该目的。无论如何,这似乎不是问题。
- 正如预期的那样,随机样本向量分量在 [-1, 1] 范围内。
- 片段视图空间位置纹理似乎没问题:
怎么了?
当我将片段着色器底部的 AO 混合因子设置为 0 时,它会平稳运行到 fps 上限(即使仍在执行计算,至少我猜编译器不会优化它:D)。但是当 AO 混合在一起时,每帧绘制最多需要 80 毫秒(随着时间的推移变得越来越慢,好像缓冲区被填满了一样),结果非常有趣和令人困惑:
显然映射看起来很遥远,闪烁的噪声看起来非常随机,就好像它直接对应于随机样本向量。我发现最有趣的是,绘制时间仅在添加 AO 因子时大幅增加,而不是由于遮挡计算。绘制缓冲区有问题吗?