Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Just a small performance question

Discussion in 'Shaders' started by flogelz, Dec 17, 2021.

  1. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    141
    So I want to sample the depthmap for my sunflare effect at 5 different points near the sun to check if something is in front of it or not. I can think of a few ways of how to do this, but am a bit curious about what would be more expensive to do? (if the difference is even measurable)

    Method 1: Sampling the depth 5 times in the shader of the flare sprites (per pixel then, cause it would sample there). This probably is fast because it's on the gpu, but it's basically useless sampling, because i'd check the same 5 places for every pixel of the sprite on screen, eventhough the end result will always be the same. (This could probably be optimized by just doing it in the vertex shader of the sprite. 4 vertices = just 4 times checking instead of for every pixel in the pixel shader).

    Method 2: Doing a GetPixel in a script and sending it over as a shader variable. Now this would be sampling wise the best solution, but I also read about the poor performance of that method.

    UPDATE
    Method 3: Just read that you can't read from the depthbuffer directly on the cpu, so another method would be to call it in a small shader that blits the result in a 1 pixel rendertexture, which i could then read the value from.

    Would be interesting to hear your opinions about this. Or if that even would be a concern to think about at all.
     
    Last edited: Dec 17, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Yeah, do this per vertex. Inside does it this way, but with like 16 spots per vertex, and a different set of points for each, so glows fade out as they go behind solid objects. Only look at other options if it becomes a performance issue… which it won’t.

    edit: I’m wrong, they only do one sample per vertex!
    https://loopit.dk/rendering_inside.pdf starting on page 172

    Dear flog no. Even if you could read the depth texture from the CPU (which you can do by copying the data to another render texture) you’d always be a frame behind in the best case, or adding nearly doubling the frame time in the worst. Copying data back to the CPU is slow.

    This might be a good idea if you plan on having a lot of different flares, or a lot of elements for your flare. If not, it’s overkill.
     
    Last edited: Dec 17, 2021
    flogelz likes this.
  3. flogelz

    flogelz

    Joined:
    Aug 10, 2018
    Posts:
    141
    Thanks for the quick answer! (and also that Inside talk is a super helpful ressource!) Gonna do it like that ouo)b
    Also I don't think you're wrong tho? Atleast in the slides they say they do 32 samples per vertex. (Which seems quite a lot for just a fade. 16 should probably enough)
    xD Noted for the future!
     
    bgolus likes this.