Where exactly depth and stencil buffers are?

So I want to grab depth AND stencil buffers from my camera.

I’ve found in documentation (Depth Layer | Learn PlayCanvas) and examples on how to do requesting depth buffer, but I seem to unable to replicate it, app.drawDepthBuffer also does not work for me.

To my understanding at least depth texture should be available in uniforms (however I cannot get it), but where is stencil buffer then, and can I get it as a texture somewhere?

Here is project that I’m failing to grab depth texture PlayCanvas 3D HTML5 Game Engine

okay, so I figured out the depth finally. I still don’t understand how stencil works and if I can get it somewhere as a texture buffer, which it should be.

Here’s an example using the scene depth in a custom shader:

When the shader is created, a shader chunk is added to it: pc.shaderChunks.screenDepthPS

Which is this file: https://github.com/playcanvas/engine/blob/main/src/graphics/program-lib/chunks/common/frag/screenDepth.js

see how it reads the depth from the uSceneDepthMap

In theory, if you use stencil buffer, it should be available as uSceneDepthMap.y, but I have not done any experiments to confirm.

1 Like

I was able to retrieve depth map in the end, but stencil buffer is empty. I checked every channel :frowning:

I will check later if I might be doing something wrong, since I’m not very familiar with how stencil operations work.

For now I’ll try to mask my post effect using alpha range in color buffer. Would be super great to have HDR, but I guess not currently, I’ve seen github issue where you described that pc render operates on LDR, and render texture format being 8 bit per channel hints at low resolution float.

You could implement HDR on platforms that support float / half float render targets though. You can create a texture with that format, and render your camera to it, while disabling gamma correction / tone mapping and whatever else is needed, to keep data in linear space. Then you’d need to implement postprocessing step that does it, as the final buffer that gets presented to the screen needs to be sRGB LDR.

Model viewer https://github.com/playcanvas/model-viewer does something similar when high quality mode is enabled I believe, see here: https://github.com/playcanvas/model-viewer/blob/main/src/multiframe.ts
Even though it uses float buffer to accumulate multiple frames, and not as an HDR solution.

regarding stencil, have you tried setting gl.STENCIL_BUFFER_BIT in the blit function?

I have not, but I don’t see how that might help in my case.

I’m getting depth texture via requestDepthBuffer, and then accessing said texture in uniform. To blit stencil, I would need to have stencil first, and where do I get it?

Thanks, I will look into that example!