【发布时间】:2015-05-21 20:02:39
【问题描述】:
任务背景
我正在尝试在OGLDev Tutorial 45 之后实现SSAO,它基于Tutorial by John Chapman。 OGLDev 教程使用了一种高度简化的方法,它在片段位置周围的半径范围内对随机点进行采样,并根据有多少采样点的深度大于存储在该位置的实际表面深度(位置越多)来提高 AO 因子片段周围位于其前面,遮挡越大)。
我使用的“引擎”没有 OGLDev 那样的模块化延迟着色,但基本上它首先将整个屏幕颜色渲染到带有纹理附件和深度渲染缓冲区附件的帧缓冲区。为了比较深度,片段视图空间位置被渲染到另一个带有纹理附件的帧缓冲区。 然后这些纹理由 SSAO 着色器进行后处理,并将结果绘制到屏幕填充四边形。 两种纹理本身都可以很好地绘制到四边形,着色器输入制服似乎也不错,所以这就是我没有包含任何引擎代码的原因。
片段着色器几乎相同,如下所示。我已经包含了一些符合我个人理解的 cmets。
#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 因子时大幅增加,而不是由于遮挡计算。绘制缓冲区有问题吗?
【问题讨论】:
-
我想这个问题太小众了,如果没有简单的“工作”示例和动手调试,任何人都无法回答。根据我的经验,轻弹“图像”是由于未绑定纹理或在上传纹理时使用了随机数据。我总是有一个例程将单个缓冲区绘制到屏幕上(例如深度缓冲区或法线贴图),这有助于我在视觉上确定一切正常。另一件事是将值与您期望的值进行比较,并在错误时发出红色像素,在通过时发出绿色像素。此外,您的编译器可能对“删除未使用的代码”非常严格。
-
感谢您的评论!如前所述,使用的两个输入纹理在单独渲染时工作正常。所以不幸的是,这不是问题......关于编译器:它必须进行非常复杂的词法分析来优化我上面提到的代码——以确定当 mix 函数的因子为 0 时没有绘制任何内容。跨度>
-
请给出拒绝投票的理由。批评很有帮助!
标签: c++ opengl glsl ssao deferred-shading