opengl创建一个depth_stencil纹理以供阅读 [英] opengl create a depth_stencil texture for reading

查看:610
本文介绍了opengl创建一个depth_stencil纹理以供阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用了延迟渲染,并且试图创建一个既包含深度又包含模板的纹理.

I'm using defered rendering in my application and i'm trying to create a texture that will contain both the depth and the stencil.

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0,
???, GL_FLOAT, 0);

现在,opengl希望此特定纹理使用哪种格式的枚举.我尝试了几次,但所有人都出错了

Now what format enum does opengl want for this particular texture. I tried a couple and got error for all of them

此外,访问纹理的深度和模板部分的正确glsl语法是什么.我了解深度纹理通常是均匀的sampler2Dshadow.但是我可以吗

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. But do I do

float depth = texture(depthstenciltex,uv).r;// <- first bit ? all 32 bit ? 24 bit ?
float stencil = texture(depthstenciltex,uv).a;

推荐答案

现在,opengl希望此特定纹理使用哪种格式的枚举.

Now what format enum does opengl want for this particular texture.

您遇到的问题是,深度+模板是数据的完全奇怪的组合.前24位(深度)是定点数,其余8位(模板)是无符号整数.这需要特殊的打包数据类型:GL_UNSIGNED_INT_24_8

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

此外,访问纹理的深度和模板部分的正确glsl语法是什么.我了解深度纹理通常是均匀的sampler2Dshadow.

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:

OpenGL着色语言4.50规范  -  8.9纹理函数 -n. 158

OpenGL Shading Language 4.50 Specification  -  8.9 Texture Functions  -  p. 158

对于深度/模板纹理,采样器类型应与通过OpenGL API设置的要访问的组件相匹配.当深度/模板纹理模式设置为GL_DEPTH_COMPONENT时,应使用浮点采样器类型.当深度/模板纹理模式设置为GL_STENCIL_INDEX时,应使用无符号整数采样器类型.使用不受支持的组合进行纹理查找将返回未定义的值.

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.

这意味着,如果要在着色器中同时使用深度和模具,则必须使用纹理视图(OpenGL 4.2+)并将这些纹理绑定到两个不同的纹理图像单元(每个视图的状态不同) GL_DEPTH_STENCIL_TEXTURE_MODE).这两件事共同意味着您至少需要一个OpenGL 4.4实现.

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.

#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);
}

创建模具视图纹理:

// 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状态设置:

// 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);

这篇关于opengl创建一个depth_stencil纹理以供阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆