我需要在着色器中为每个片段输出24个索引。我已经达到了最大数量的rendertargets,因为我正在为我的gbuffer使用其他四个rendertargets。所以我尝试用SSBO输出数据,用像素的gl_FragCoord索引它。问题是,它需要深度正确。所以我尝试使用layout(early_fragment_tests) in;
并观察指数。我现在可以在某些位置看到奇怪的每像素错误,看起来下面的三角形中的索引正在通过加上当我将相机移近那些点时停止。
我仔细检查了ssbo的索引,它是正确的+整个三角形的索引应该是相同的,但闪烁是每个像素。所以我认为深度测试仅适用于栅格化的每像素输出,而不适用于整个片段着色器代码。可能是问题还是有人知道深度测试是否应该停止片段的整个处理?如果情况并非如此,即使是单独的深度预通也无法帮助我。
这是一个片段着色器示例:
#version 440 core
layout(early_fragment_tests) in;
layout(location = 0) uniform sampler2D texSampler;
#include "../Header/MaterialData.glslh"
#include "../Header/CameraUBO.glslh"
layout(location = 3) uniform uint screenWidth;//horizontal screen resolution in pixel
layout(location = 0) out vec4 fsout_color;
layout(location = 1) out vec4 fsout_normal;
layout(location = 2) out vec4 fsout_material;
coherent layout(std430, binding = 3) buffer frameCacheIndexBuffer
{
uvec4 globalCachesIndices[];
};
in vec3 gsout_normal;
in vec2 gsout_texCoord;
flat in uvec4 gsout_cacheIndices[6];
flat in uint gsout_instanceIndex;
void main()
{
uint frameBufferIndex = 6 * (uint(gl_FragCoord.x) + uint(gl_FragCoord.y) * screenWidth);
for(uint i = 0; i < 6; i++)
{
globalCachesIndices[frameBufferIndex + i] = gsout_cacheIndices[i];//only the closest fragment should output
}
fsout_normal = vec4(gsout_normal * 0.5f + 0.5f, 0);
fsout_color = vec4(texture(texSampler, gsout_texCoord));
MaterialData thisMaterial = material[materialIndex[gsout_instanceIndex]];
fsout_material = vec4(thisMaterial.diffuseStrength,
thisMaterial.specularStrength,
thisMaterial.ambientStrength,
thisMaterial.specularExponent);
}