I tried to use layer rendering to create shadow map but it gave me some problem.
The following geometry shader will only render the last layer (layer 3,gl_Layer = 2) correct.
#version 150 core
precision highp int;
layout(triangles) in;
layout(triangle_strip, max_vertices = 9) out;
uniform mat4 cameraMatrix[3];
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix[3];
out vec4 vertexPosition;
void main()
{
for(int layerIndex = 0; layerIndex < 3;layerIndex++)
{
gl_Layer = layerIndex;
mat4 cameraModelMatrix = cameraMatrix[layerIndex]*modelMatrix;
for(int i = 0; i < gl_in.length();i++)
{
gl_Position = projectionMatrix[layerIndex]*cameraModelMatrix*gl_in[i].gl_Position;
vertexPosition = cameraModelMatrix*gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}
}
if I change “layerIndex < 3” to “layerIndex < 2” only the second layer will be correctly rendered.
any layer written before the last layer get mangled like it had been over written with some garbage.
I also try to unroll the layer loop and using integer to specified layer/uniform matrix index but it didn’t help.
All the uniform matrix are uploaded correctly(since with testing above the last layer will alway correctly render).
The layered FBO are create with the code below with out any error
void FBO::initFBOForVarianceShadowMapLayered(int width,int height,int shadowCount){
this->finalizeFBO();
this->width = width;
this->height = height;
glGenFramebuffers(1, &fboID);
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
//create and attach depth array
glGenTextures(1, &this->depthBufferTexID);
glBindTexture(GL_TEXTURE_2D_ARRAY, this->depthBufferTexID);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT24, this->width,this->height,shadowCount, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,depthBufferTexID,0);
//create texture array of color buffer
this->colorBufferTexID.resize(1);
glGenTextures(1, &this->colorBufferTexID[0]);
glBindTexture(GL_TEXTURE_2D_ARRAY, this->colorBufferTexID[0]);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RG32F, this->width,this->height,shadowCount, 0, GL_RG, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,this->colorBufferTexID[0],0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status == GL_FRAMEBUFFER_COMPLETE){
printf("Create layered frame buffert succssful
");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
this->isFBOInitialize = true;
getchar();
}
and have a shader compile with out error or warning
**** UPDATE ****
with out depth texture attach the layer rendering are correct (But with out any depth testing).
So the error might be where I attach depth buffer to FBO or I didn’t clear the depth buffer right.
I just call this code to clear selected draw buffer (buffer 0)
//activate FBO
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
glViewport(0,0,width, height);
//select draw buffer
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
If you need more info just ask me.