I am rendering a scene using three.js that needs multiple post-processing passes. Most of those steps need the depth buffer. My plan was to first render all visible objects to obtain the color and depth and then render all post-processing passes using 2 framebuffers which are alternating read and write framebuffers. The passes are just examples:
- Render Objects -> FB0
- DistortionPass, taking FB0 as input -> FB1
- GodrayPass, taking FB1 as input -> FB0
- SSAOPass, taking FB0 as input -> screen
The GodrayPass needs to read the depth of the first render pass, therefore I need to bind the depth texture and it should not be the one bound to FB0, otherwise it would lead to a feedback loop, since that shader is writing to FB0.
I think it would make sense to copy the depth into a separate texture after rendering the objects, so I can bind this texture whenever needed in a pass without worrying about a feedback loop.
However,
- copyTexImage2D seem to not support copying from the depth buffer.
- using a shader to pack the depth buffer after the first pass into an RGBA8 texture would require every pass to "unpack" the float again
- render all objects again using a shader that output the depth to the color buffer? Would require packing or precision loss again.
What is the best practice here? Am I on the right path?
I can make use of WebGL 2.0 (OpenGL ES 3.0), but would like to avoid using unpopular extensions.