Depth buffer copy

Hello, I have 3d scene with 2d sprites. I want to render sprite to separate render target and apply some post effects to it (like blur and gloom). But I need somehow to pass Depth Buffer from main render target to post effect RT to apply depth mask and cut sprite.
I think easiest way to do it would be copy depth buffer from one RT to another, but I did find way to access it.
Please suggest me some solution to achieve it or should I create issue and request some feature for it?

You can create your depth buffer of the render target with the flag parameter set to render.TEXTURE_BIT:

…or if you are using the render target resource you can just enable the “depth texture storage” checkbox:

Now that the depth buffer is a texture you can bind it to a texture unit by using render.enable_texture :

1 Like

I do not get what to do with this texture after calling render.enable_texture. That means texture will be passed as sampler to the shader, not to the render targer depth buffer

What exactly do you want to do with the depth buffer? Do you need to use hardware depth testing to reject pixels? Otherwise you can just sample from th depth buffer in your post processing pass and do what ever you need with the values there

I want to hide parts of sprite that behind 3d objects and render to the buffer only visible part of the sprite

Alright in that case you can attach the depth buffer as a texture in the post processing pass and use discard to reject the samples.

I dont get how to do so. What is statment should be in this case?

vec2 duv = vec2(var_gl_position.xy + 1) / 2;
    
    float depth = texture2D(tex1, duv).r;

    if (gl_FragCoord.z > depth) 
    {
        discard;
    }

seems that works

1 Like