我在我的应用程序中使用deferred渲染,我正在尝试创建一个包含深度和模板的纹理.
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0,
???, GL_FLOAT, 0);
Run Code Online (Sandbox Code Playgroud)
现在opengl想要这个特定纹理的enum格式是什么.我尝试了一对,并为所有人都犯了错误
另外,访问纹理的深度和模板部分的正确glsl语法是什么.据我所知深度纹理通常是均匀的sampler2Dshadow.但我该怎么做
float depth = texture(depthstenciltex,uv).r;// <- first bit ? all 32 bit ? 24 bit ?
float stencil = texture(depthstenciltex,uv).a;
Run Code Online (Sandbox Code Playgroud)
现在opengl想要这个特定纹理的enum格式是什么.
您遇到的问题是Depth + Stencil是一个完全奇怪的数据组合.前24位(深度)是定点,其余8位(模板)是无符号整数.这需要特殊的打包数据类型:GL_UNSIGNED_INT_24_8
另外,访问纹理的深度和模板部分的正确glsl语法是什么.据我所知深度纹理通常是均匀的sampler2Dshadow.
你实际上永远无法使用相同的采样器制服对这两件事进行采样,这就是为什么:
OpenGL着色语言4.50规范 - 8.9纹理函数 - p.158
对于深度/模板纹理,采样器类型应与通过OpenGL API设置的组件匹配.当深度/模板纹理模式设置
GL_DEPTH_COMPONENT为时,应使用浮点采样器类型.当深度/模板纹理模式设置GL_STENCIL_INDEX为时,应使用无符号整数采样器类型.使用不受支持的组合执行纹理查找将返回未定义的值.
这意味着如果要在着色器中同时使用深度和模板,则必须使用纹理视图(OpenGL 4.2+)并将这些纹理绑定到两个不同的纹理图像单元(每个视图具有不同的状态GL_DEPTH_STENCIL_TEXTURE_MODE).这两件事一起意味着你至少需要一个OpenGL 4.4实现.
#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);
}
Run Code Online (Sandbox Code Playgroud)
// 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`
Run Code Online (Sandbox Code Playgroud)
// 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);
Run Code Online (Sandbox Code Playgroud)