Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to access rendered depth buffer properly?

Discussion in 'Shaders' started by valyard, Nov 12, 2012.

Thread Status:
Not open for further replies.
  1. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    291
    I want to save depth buffer of rendered image for later use. How do I do this?

    I don't get this weird convention with DepthTextureMode.Depth and _CameraDepthTexture in a shader.
    Is there a way to use actual RenderTexture.depthBuffer? I don't see if it's possible to do anything with RenderBuffers at all.
     
  2. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    291
    Anyone?
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You can not access the buffers from CPU at all (that means you can not read its content in script).
    Be it the color or depth buffer.
    RenderTextures exist on the GPU only (they are Unity wrapper around Render Targets, FBO, ...)

    To get it out there you would need write a shader that visualizes the depth buffer and use Texture2D.ReadPixels when this render texture is the currently active one on your camera so you can grab the data into a Texture2D which you can use in your further scripting. Just be aware that texture readback from gpu is not exactly fast and depending on your need it might not even make sense to go there
     
    AbleArcher likes this.
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    The builtin depth texture in unity is actually just a "shader replacement" shader. If you got lost with the builtin values and scripts, just check the builtin shaders source (can be found in one of the top sticky posts in shaders section) and there you learn how you can render your very own depth texture yourself.
    Although, you may have problems when using deferred or forward rendering, as in deferred unity already renders the depth anyways. So if you custom render it in a different shader, it will be some unnecessary extra work.

    Briefly, your rendered texture of depth is already there if you just enable
    Camera.depthTextureMode = DepthTextureMode.Depth;in one of your scripts awake or start function.

    And the rendertexture in RenderTextureFormat.Depth format will be available to all your shaders as it is automatically set as a global uniform property with the name _CameraDepthTexture

    I hope it was clear, sorry english is not my main language.

    Oh, by the way if i misunderstood the question, and if you want to save the depth image of a previous frame and carry it over to next frame(s), you need to do it custom way.
     
    Last edited: Nov 14, 2012
  5. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    291
    Thanks.
    I need depth later in a compute shader. It doesn't have to be transfered to CPU just to be accessible at GPU again.
    Just to copy data from current depth buffer to another buffer somehow.
     
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    So you want to save the current frames buffer, and use this in a later frame doing kind of a motion blur thing right?
    As, the rendertexture _CameraDepthTexture is not exposed in scripts but only to shaders, you must reproduce it yourself with a custom replacement shader.
    Check the example project "Replacement Shaders" in unity site. There is a full depth texture shader example there.
    Doing custom shader also helps you get full control on what is to write on the buffer. You can even write transparent objects if you find a smart usage for it.
     
  7. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    291
    Ended up with rendering depth buffer into a texture with a separate render using a special shader.
    Doesn't seem optimal.
     
  8. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    What else did you expect, you are trying to use a past frames depth info inside the current frame. There is no other way to do it in any other engine or whatever.
     
  9. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    291
    yeah, but depth buffer is still a buffer on GPU. though, it's not possible in Unity to grab it after the frame is rendered.
     
    boyplayunity likes this.
  10. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Depth "buffer" in unity works quite different than what you expect. Read the documentation about the camera depth image in the manual.
     
  11. valyard

    valyard

    Unity Technologies

    Joined:
    Jun 4, 2010
    Posts:
    291
    I know. Might also try multiple rendering targets for that.
     
  12. HumphreyZhu

    HumphreyZhu

    Joined:
    Sep 19, 2015
    Posts:
    2
    Hi, how did you solve this at last?
    I've came to the same question recently. I tried to render some particles in another RenderTexture while using the main camera's depth buffer for the right occlusion. Here's the code snippet:
    void OnPostRender()
    {
    bool bFog = RenderSettings.fog;
    RenderSettings.fog = false;

    offscreenCamera.CopyFrom(main);
    offscreenCamera.enabled = false;

    offscreenCamera.depthTextureMode = DepthTextureMode.None;

    offscreenCamera.cullingMask = layerMask;

    offscreenCamera.clearFlags = CameraClearFlags.Nothing;
    offscreenCamera.backgroundColor = backColor;
    offscreenCamera.targetTexture = offscreenRT;

    //Graphics.SetRenderTarget(offscreenRT.colorBuffer, Graphics.activeDepthBuffer);
    //GL.Clear(false, true, backColor);

    offscreenCamera.RenderWithShader(combinedShader, "RenderType");

    RenderSettings.fog = bFog;
    }

    I also tried the code commented while not set the camera's targetTexture, it turns out that the offscreenCamera created a rt and a depth buffer on it's own.

    I'm wondering is there any way to share the main camera's depth?
     
  13. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    I haven't found a way - I'm releasing an off screen particle system next week (render at 1/4 screen size, nearest depth upsample to the screen), and currently I have to do the z-test in the particle shaders, which while not too expensive (especially when rendering at low res) makes it harder for novices to write new shaders for the system.
     
  14. Primerist

    Primerist

    Joined:
    Sep 19, 2016
    Posts:
    5
    I tried using ReadPixels to sample values of various depth pixels in an image effect and it was as slow as expected. If we do not need the whole depth buffer necessarily as a texture, we could just get selective values and output those.

    Could we get the depth buffer in a compute shader and then output data from it as arrays of values? Has anyone done something like that?
     
  15. yu_yang

    yu_yang

    Joined:
    May 3, 2015
    Posts:
    85
    I'm stuck this now :-(
     
  16. drcrck

    drcrck

    Joined:
    May 23, 2017
    Posts:
    328
    2012 - 2019
    we still have no access to the original depth texture
    maybe it will be available in HDRP v12 in 2030 or so...
     
  17. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    229
    Graphics.activeDepthBuffer doesn't seem to work :)
    BuiltinRenderTextureType.Depth also ends up as "UnityDefault2D".
    This is nice
     
  18. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    why is a unity tech asking a question ???
     
  19. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Because that was 7 years ago and they weren’t a Unity employee at the time?
     
  20. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    38
    Just to show no-one knows the answer?
     
    Egad_McDad likes this.
  21. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    seems weird either way, like why keep the moniker on the account before they were in?
    Why use the same account?
    Why not answer the question/issue after being hired?
     
  22. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Why not?

    No idea when he got hired, but how often do most people go back and answer forum posts they made once they find the answer? Most people certainly don't unless it's a day or two later. Most likely it was several years between him posting and being hired. And once you're hired you're probably busy actually working. :p

    This is a community forum first and foremost. The Unity employees that respond do so mostly by their own volition, ignoring the specific posts requesting feedback like the preview and beta forums.
     
  23. okluskyond

    okluskyond

    Joined:
    Dec 6, 2017
    Posts:
    38
    The only thing that we have figured out is to use NativeRenderPlugin for each platform you are targeting, and copy DepthBuffer from there. But it's not much comfy solution (and on mobile it's highly risky as everything new in graphics API :))
     
  24. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    Then its anarchy here. No answer is an answer and Unity staff have no obligation for support is what i'm getting. Not a good way to promote a product for production.

    What are they hired to do if not support the product and its users then? Just make their own app in a vacuum and bug you to buy the forreals tech hotline? In that case it would be better as a paid product with no free offer.

    Seems odd simply to not just stick someone on the docs for a week and hammer out stuff. Thats all that is really needed. Grind away some XP share. Dont need wild example projects, just more example clip and paste pages. They do a good job when they do make those but MORE is needed.

    I have already voiced this to their direct hotline, but I get the same here that its not a priority not even ONE person. Its the same reasoning that Apple does not have an official Ipad calculator app, and google dropped their own rss app for examples. No body cared internally to focus on basics
     
  25. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The original post is from over seven ... years ... ago.

    Unity 3.0 was still the main version of that was out, Unity 4.0 wasn't officially released until a few days after this (though I suspect this was actually Unity 4.0 related). The specific issue here is thread necro. It's more amazing that any of the information in this thread is at all still relevant, though that's in part because people keep posting to. But if you search the forum for more recent posts both of the main questions asked about in this thread have been answered elsewhere, often with example code and/or simple projects. Old threads are old.


    More direct official support comes with the paid tiers of Unity. Most staff are not being paid or trained to offer support because it is not their job to do so, and shouldn't be. A programmer should not be required to know how to interface with customers anymore than community managers should be required to write code. Unity does have a dedicated support staff, as well a documentation staff, and there are official ways to make comments and note when data is wrong or missing through the documentation itself.

    Write code? As far as I know actual tasks usually come from internal vetting, like they do for any large company. If they didn't it really would be anarchy. The OP appears to have been hired about 2 years ago, and appears to be one of the people you might get when you pay for a support contract. But again... this is a seven year old post that people keep necro bumping.

    If you find something that's wrong or you think needs more information, every page has a "leave feedback" button you can use. I know many of the documentation staff are hungry for feedback. Unity's official documentation is surprisingly complete compared to many other tools out there, as annoying and occasionally outdated some of it is, it's still better than almost any other tool I've used. The documentation staff is also a little swamped right now working on the URP / HDRP related stuff that Unity's been pumping out.

    Need both. Copy & paste are good for simple and common use cases, but there are so many possible situations that there's no way to cover all of them. This particular thread is about making a copy of the depth buffer for use in later frames. This is something very, very few people need, and generally if you know enough about the rendering systems to need it you'll likely already know the way to do it. The forums have a relatively healthy community, and if you look around enough you will usually find answers the questions you have, or you can make a new thread and ask and frequently people will respond with answers or at least point you in the right direction.
     
    hippocoder likes this.
  26. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Many people using Unity go on, years later, to be hired by Unity. That happens often on these forums. And no, it's not your place to criticise them if they want to use the same name or avatar.

    That's called freedom of personal choice and you're the first person to have a problem with that since forever, so I recommend you chill big time.

    Also you necro'd this thread and posted offtopic remarks. Those are against forum rules.
     
    luisbazan and SirGhoul like this.
Thread Status:
Not open for further replies.