- Home /
Read depth buffer on the cpu
I need to read the depth information of the scene on the cpu every frame, which I use to generate a new texture that gets sent to a final image effect shader. How can I read the depth data outside of the shader? I think I have to render it to a rendertexture, but will I be able to read the data from a rendertexture (no GetPixel() function in RenderTexture, only Texture2D)?
be sure you need to do this. any reading of the GPU output by the CPU will incur a GPU pipeline stall, possibly hurting the frame-rate. that might be fine in your situation, but it might not be.
Answer by tanoshimi · Aug 02, 2016 at 10:59 AM
You have to copy the Rendertexture to a Texture2d first, which you can do with ReadPixels():
RenderTexture.active = yourRenderTexture;
yourTexture2D.ReadPixels(new Rect(0, 0, yourRenderTexture.width, yourRenderTexture.height), 0, 0);
yourTexture2D.Apply();
You can then use GetPixels() as normal, but, as @elenzil points out, this is slow to do every frame. If you're sending this back to an image effect anyway, do you really have to retrieve it to the CPU? Can you do all the remaining processing on the GPU side?
I need the depth info on the cpu because I can't set neighbor pixel values in the shader. I could use multiple passes on the gpu, but I would probably need about ten passes (im guessing its faster to just do it on the cpu, I will have to test it).
Your answer
Follow this Question
Related Questions
How to get the depth values of the camera view? 1 Answer
sampler2d object has no methods 1 Answer
Complex Depth Shader 0 Answers
How to get depth texture and render texture from one camera 0 Answers
Render scene depth to a texture 4 Answers