FBO texture as color rendering target & pass it as as texture input into depth shader

Hello :slight_smile:

I would like to create a texture with one 16bit integer component, attach it to FBO as color rendering target and pass it as texture input into depth shader.

I am aware that it is Sampling and Rendering to the Same Texture is not possible.

Any hints to solved this in the best way?

Thanks a lot

I would like to create a texture with one 16bit integer component, attach it to FBO as color rendering target and pass it as texture input into depth shader.

You can’t do that directly; you cannot alias color and depth format textures with each other. You also cannot copy the texel values between color and depth formats.

I am aware that it is Sampling and Rendering to the Same Texture is not possible.

That’s not the problem. You can read from and render to the same texture. Just not at the same time. And even then, you can render to and read from different parts of it if you have GL 4.5/ARB/NV_texture_barrier. The problem is your desire to treat a color texture as a depth texture.

The only reason I can come up with as to why you need this to be a depth value (besides actually using this for some kind of depth buffering operation in addition to the texture read) is if you’re trying to take advantage of shadow samplers and depth comparison sampling modes. If that’s true, here are your options, from best to worst:

1: Use purely integer textures, then use texture gather functions and do the shadow comparisons and PCF computations yourself in the shader. If you don’t have access to texture gather functions, you’ll have to use texelFetch and so forth.

2: Use PBOs or some similar operation to copy from your integer texture to the depth texture.