【问题标题】:OpenGL OGLDev SSAO Tutorial Implementation Fragment Shader yields NoiseOpenGL OGLDev SSAO 教程实现片段着色器产生噪声
【发布时间】: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


【解决方案1】:

问题似乎与所选纹理类型有关。

需要将带有句柄viewPosTexture 的纹理显式定义为浮点纹理格式GL_RGB16FGL_RGBA32F,而不仅仅是GL_RGB。有趣的是,单独的纹理绘制得很好,只有组合出现了问题。

// generate screen color texture
// note: GL_NEAREST interpolation is ok since there is no subpixel sampling anyway
glGenTextures(1, &screenColorTexture);
glBindTexture(GL_TEXTURE_2D, screenColorTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, windowWidth, windowHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, NULL);

// generate depth renderbuffer. without this, depth testing wont work.
// we use a renderbuffer since we wont have to sample this, opengl uses it directly.
glGenRenderbuffers(1, &screenDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, screenDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, windowWidth, windowHeight);

// generate vertex view space position texture
glGenTextures(1, &viewPosTexture);
glBindTexture(GL_TEXTURE_2D, viewPosTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, windowWidth, windowHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

绘制缓慢可能是由 GLSL mix function 引起的。将对此进行进一步调查。

闪烁是由于在每一帧中重新生成和传递新的随机向量。只需传递足够多的随机向量就可以解决问题。否则可能有助于模糊 SSAO 结果。

基本上,SSAO 现在可以工作了!现在它只是或多或少明显的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多