First,I have to apologize for my bad english.
Actually I try to do deferred shading optimization using stencil.
Trying to read attached FBO’s depth as texture (for vertex reconstruction) and do stencil test on the attached FBO at the same time give me a strange visually error(even when depth testing turn off).
The stencil test appear to be working , performance is increased but there are strange scanline running on the picture.
So I think I could fix this by having another copy of depth texture to use for vertex reconstruction.
Since it look like seperate attachment of depth and stencil
didn’t wok on ATI HD4670 card (dont know about NVIDIA) I have to use the pack version instead.
But I have a problem trying to copy depth/stencil data between two FBO,
which had GL_DEPTH24_STENCIL8_EXT attach as a depth texture.
this is a code to set up the two FBO
void FBO::initFBOForDeferredWithStencil(int width,int height){
this->width = width;
this->height = height;
glGenFramebuffersEXT(1, &fboID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glGenTextures(1, &depthBufferTexID);
glBindTexture(GL_TEXTURE_2D, depthBufferTexID);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH24_STENCIL8_EXT, width, height, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D,depthBufferTexID, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D,depthBufferTexID, 0);
this->colorBufferTexID.resize(4);
for(int i=0;i<4;i++){
glGenTextures(1, &colorBufferTexID[i]);
glBindTexture(GL_TEXTURE_2D, colorBufferTexID[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+i, GL_TEXTURE_2D,colorBufferTexID[i], 0);
}
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
if(status == GL_FRAMEBUFFER_COMPLETE_EXT){
printf("Frame buffer for FBO with stencil complete
");
}
this->isFBOInitialize = true;
}
void FBO::initFBODepthStencil(int width,int height){
this->width = width;
this->height = height;
glGenFramebuffersEXT(1, &fboID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glGenTextures(1, &depthBufferTexID);
glBindTexture(GL_TEXTURE_2D, depthBufferTexID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D,depthBufferTexID, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D,depthBufferTexID, 0);
glDrawBuffer(GL_NONE);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status == GL_FRAMEBUFFER_COMPLETE_EXT){
printf("Frame buffer depth stencil complete
");
}
else{
printf("Frame buffer depth stencil error
");
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
this->isFBOInitialize = true;
}
This is the code use to copy depth data between two FBO.
fbo1 is initialize using function 1.
fbo2 is initialize using function 2.
both have the same depth stencil attachment.
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT,fbo1.fboID);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo2.fboID);
glBlitFramebufferEXT( 0, 0,fbo1.width,fbo1. height,
0, 0,fbo2.width,fbo2. height,
GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);