Now what format enum does opengl want for this particular texture.
The problem you are running into is that Depth+Stencil is a totally oddball combination of data. The first 24-bits (depth) are fixed-point and the remaining 8-bits (stencil) are unsigned integer. This requires a special packed data type: GL_UNSIGNED_INT_24_8
Also, what is the correct glsl syntax to access the depth and stencil part of the texture. I understand that depth texture are usually uniform sampler2Dshadow.
You will actually never be able to sample both of those things using the same sampler uniform and here is why:
For depth/stencil textures, the sampler type should match the component being accessed as set through the OpenGL API. When the depth/stencil texture mode is set to GL_DEPTH_COMPONENT
, a floating-point sampler type should be used. When the depth/stencil texture mode is set to GL_STENCIL_INDEX
, an unsigned integer sampler type should be used. Doing a texture lookup with an unsupported combination will return undefined values.
This means if you want to use both the depth and stencil in a shader you are going to have to use texture views (OpenGL 4.2+) and bind those texture to two different Texture Image Units (each view have a different state for GL_DEPTH_STENCIL_TEXTURE_MODE
). Both of these things together mean you are going to need at least an OpenGL 4.4 implementation.
Fragment shader that samples depth and stencil:
#version 440
// Sampling the stencil index of a depth+stencil texture became core in OpenGL 4.4
layout (binding=0) uniform sampler2D depth_tex;
layout (binding=1) uniform usampler2D stencil_tex;
in vec2 uv;
void main (void) {
float depth = texture (depth_tex, uv);
uint stencil = texture (stencil_tex, uv);
}
Create a stencil view texture:
// Alternate view of the image data in `depth_stencil_texture`
GLuint stencil_view;
glGenTextures (&stencil_view, 1);
glTextureView (stencil_view, GL_TEXTURE_2D, depth_stencil_tex,
GL_DEPTH24_STENCIL8, 0, 1, 0, 1);
// ^^^ This requires `depth_stencil_tex` be allocated using `glTexStorage2D (...)`
// to satisfy `GL_TEXTURE_IMMUTABLE_FORMAT` == `GL_TRUE`
OpenGL state setup for this shader:
// Texture Image Unit 0 will treat it as a depth texture
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, depth_stencil_tex);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
// Texture Image Unit 1 will treat the stencil view of depth_stencil_tex accordingly
glActiveTexture (GL_TEXTURE1);
glBindTexture (GL_TEXTURE_2D, stencil_view);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
GL_DEPTH_TEXTURE_STENCIL_MODE
for the view. – Dekker