PDA

View Full Version : Use stencil attachment apart from depth attachment



antoniopan
08-10-2014, 11:19 PM
It seems that if we would like to apply depth test and stencil test together, we could only attach a single texture or render buffer with a base internal format of DEPTH_STENCIL to the DEPTH_STENCIL_ATTACHMENT channel to the frame buffer object.
When I create a texture with base internal format of DEPTH_COMPONENT and a render buffer with base internal format of STENCIL_INDEX, attached to the DEPTH_ATTACHMENT and STENCIL_ATTACHMENT respectively, an error occurs indicating FRAMEBUFFER INCOMPLETENESS when writing to the frame buffer object.

Here is the sample code indicating the problem:

// code which works
glGenFramebuffers(1, &uiFbo);
glGenTextures(3, uiTex);
glGenRenderbuffers(2, uiRbo);

glBindTexture(GL_TEXTURE_2D, uiTex[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 400, 400, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, uiTex[2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 400, 400, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, uiFbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, uiTex[0], 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, uiTex[2], 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

// code which also works
glGenFramebuffers(1, &uiFbo);
glGenTextures(3, uiTex);
glGenRenderbuffers(2, uiRbo);

glBindTexture(GL_TEXTURE_2D, uiTex[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 400, 400, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, uiTex[2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 400, 400, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, uiFbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, uiTex[0], 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, uiTex[2], 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, uiTex[2], 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

// code which does not work
glGenFramebuffers(1, &uiFbo);
glGenTextures(3, uiTex);
glGenRenderbuffers(2, uiRbo);

glBindTexture(GL_TEXTURE_2D, uiTex[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 400, 400, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, uiTex[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 400, 400, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);

glBindRenderbuffer(GL_RENDERBUFFER, uiRbo[0]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, 400, 400);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, uiFbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, uiTex[0], 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, uiTex[1], 0);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, uiRbo[0]);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

From the sample code, is it to say that the DEPTH_ATTACHMENT and the STENCIL_ATTACHMENT need to be sticked together in storage, in the same word, they should be the same object. But I could not find anything which mention this tricky usage of frame buffer object in the latest OpenGL specification. Or this is an implementation dependent problem?

Yandersen
08-11-2014, 01:34 PM
I am not sure where have I seen that, but the separate stencil and depth buffer attachments are not supported. Strange that the "second code" works, though. Probably the actual drawing targets are not allowed to be the separate objects, and taken it is the same texture it works.

antoniopan
08-11-2014, 06:01 PM
Thanks.
I'm just considering that if the DEPTH_STENCIL texture works as sampler2DShadow in shadow mapping.