opengl - 如何将 "add"深度信息发送到主帧缓冲区

标签 opengl glsl

假设我有这个场景
enter image description here

我想从定制的片段着色器中添加深度信息。

现在要做的直观的事情是在我的茶壶上绘制一个四边形,而不启用深度测试,但使用 glDepthMask(1) 和 glColorMask(0, 0, 0, 0)。写一些片段 gl_FragDepth 并丢弃一些其他片段。

if ( gl_FragCoord.x < 100 )
    gl_FragDepth = 0.1;
else
    discard;

出于某种原因,在 NVidia Quadro 600 和 K5000 上它按预期工作,但在 NVidia K3000M 和 Firepro 上(不记得是哪一个),我丢弃的片段所覆盖的所有区域都被赋予了四边形的深度值。

我不能不修改丢弃的片段深度值吗?

编辑 我找到了解决我的问题的方法。事实证明,正如 Andon M. Coleman 和 Matt Fishman 指出的那样,我启用了 early_fragment_test,但不是因为我启用了它,而是因为我使用了 imageStore、imageLoad。

由于我不得不解决这个问题,我只是在“添加深度 channel ”之前将当前深度缓冲区的内容复制到纹理。将其分配给统一的 sampler2D。这是我的着色器中的代码:
if ( gl_FragCoord.x < 100 )
    gl_FragDepth = 0.1;
else
{
    gl_FragDepth = texture( depthTex, gl_PointCoord ).r;
    color = vec4(0.0,0.0,0.0,0.0);
    return;
}

这将写入一个具有不变 z 值的完全透明像素。

最佳答案

好吧,对我来说这听起来像是一个驱动程序错误——丢弃的片段不应该击中深度缓冲区。您可以将原始深度缓冲区绑定(bind)为纹理,使用 gl_FragCoord 对其进行采样, 然后写回结果而不是使用 discard .这会增加额外的纹理查找——但它可能是一个合适的解决方法。

编辑:来自 GLSL 4.40 Specification 的第 6.4 节:

The discard keyword is only allowed within fragment shaders. It can be used within a fragment shader to abandon the operation on the current fragment. This keyword causes the fragment to be discarded and no updates to any buffers will occur. Control flow exits the shader, and subsequent implicit or explicit derivatives are undefined when this exit is non-uniform. It would typically be used within a conditional statement, for example:

if (intensity < 0.0) discard;

A fragment shader may test a fragment’s alpha value and discard the fragment based on that test. However, it should be noted that coverage testing occurs after the fragment shader runs, and the coverage test can change the alpha value.if



(强调我的)

关于opengl - 如何将 "add"深度信息发送到主帧缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22871558/

相关文章:

c++ - GLM 角度轴错误?

linux - OpenGL - 渲染时间抖动使动画不稳定

optimization - 在GLSL中选择cubmap面的快速方法

opengl - 如何在 OpenGL GLSL 中导入库

glsl - 如何在版本 150 中获取 GLSL 的纹理坐标?

c++ - 法线贴图问题

c++ - 在计算着色器中计算帧缓冲区的颜色直方图

sprite-kit - 如何在 SpriteKit 中创建自定义混合模式

opengl - Windows 中的 MIDI 嗅探 + OpenGL

c++ - GLSL、Opengl 中的视差映射问题