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

Deferred Lighting Performance

Discussion in 'Shaders' started by frosted, Sep 3, 2014.

  1. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    I know this isn't the correct forum, but generally you guys seem to really know your stuff and I can't get an answer anywhere else.

    I'm trying to better understand the performance considerations of Unity's deferred lighting system.

    From what I've read most deferred rendering systems work in something like a O(num lights * pixels), Unity's system doesn't seem to really follow that. Although the manual states: "The rendering overhead of realtime lights in deferred lighting is proportional to the number of pixels illuminated by the light and not dependent on scene complexity."

    I'm working on a desktop deployment with what I think is generally simple geometry on screen (200-300k tris/verts). The camera is most often top down, so that number is usually pretty steady.

    I've been getting better performance using forward rendering with a shader using fullforwardshadows on receivers to render shadows using point/spot. I'm trying to understand why forward rendering has been faster under these circumstances and under what circumstances that might change.

    Am I getting faster performance using forward rendering because of my generally simple screen geometry, is it because I'm only using a handful of dynamic pixel lights? Is it because those dynamic lights don't generally overlap much? If I generally cap the number of dynamic lights affecting a given area to 2-3 will deferred lighting ever be faster? Am I just missing the point?
     
    Last edited: Sep 3, 2014
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    I do not know exactly how it works, but I think that would be something like:
    If the surface is illuminated by many sources of light fills the entire screen is deferred faster if only a small part, the forward will be faster.
    With small number of light sources-forward faster.
    If a large portion of the screen is covered with surface with small number of light sources and objects is rendered in the correct order (close draw first), forward can be faster.
     
    Last edited: Sep 3, 2014
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    One thing to remember is that Unity's deferred renderer uses what is called pre-pass lighting, and is not actually fully deferred shading. The former uses a slimmer G-buffer (depth, normals and specular) in order to generate a light buffer, and then re-renders everything with the accumulated light. Deferred shading uses a fatter buffer to (depth, normals, all material properties) so that it can go straight to shading. No additional geometry passes are required.

    The benefit of pre-pass lighting is that it uses a slimmer G-buffer, but everything needs to be rendered at least twice. Depending on your scene complexity, lighting setup and performance bottlenecks, Unity's deferred lighting could be more than twice as expensive as deferred.

    Unity 5 will have a full deferred renderer, so you'll probably want to profile again when that's out.
     
  4. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Thank you both very much for your responses. I knew the shader forum was the only place I would hope to get a good answer :)

    My next question - (which may be very dumb) - when rendering under the pre-pass, is that some kind of shader agnostic calculation - or is it a full render?

    If it's a full render, then perhaps one of the reasons I'm seeing such a difference is actually because of the rendering overhead from specific shaders or even the textures used?
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Not shader-agnostic, but appropriately slimmed-down. If you want to see the pre pass versions of a surface shader, you can look at the generated code. It's unlikely that any specific shaders are making deferred rendering slower for you.
     
  6. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Thanks so much again.

    So the deferred pass will render the entire scene once, then accumulate lighting, then re-render with appropriate lighting.
    Forward will simply render each again and again for each pixel light that hits it.

    So in terms of raw performance alone, forward should always be faster than deferred when only using 2 lights right? Because deferred will always render twice anyway, and forward can skip a separate accumulation process by just rendering directly.

    This would mean that deferred only really becomes potentially faster once 3 or more lights are involved?

    Am I understanding this properly? Or is this also dependent on the 'slimmed down'ness of the shaders (I imagine forward would use a full pass each for each light whereas deferred could use a more slimmed down pass, since there is a solid differentiation between pre pass and final)
     
  7. Killersan

    Killersan

    Joined:
    May 27, 2014
    Posts:
    76
    I know that this thread is a bit old, but we are having some problems with performance in deferred rendering.
    In our case we have one directional light and about 100 point of lights and every time we put another light source in the scene performance goes down. Baking is not an option right now as it is outdoor scene like 20km2 and light baking on high performance server is taking like month, so it's not acceptable right now. Any clues why another light sources in deferred are so much expensive even if it stated that they shouldn't be ?

    Ohh, this problem occurs in Unity 5.4, 5.5 and 5.6
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Do note that it's light without shadows, those should be pretty cheap. Are you using shadows on all the lights?
     
    Lipoly likes this.
  9. Killersan

    Killersan

    Joined:
    May 27, 2014
    Posts:
    76
    On most of it, but I'll try to turn off shadows on all of them except directional one
     
  10. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    Also, make sure your objects are actually rendering as deferred by seeing if they show up in the gBuffer. If your shader, for instance, only provides a forward rendering pass, then unity will render that object with forward rendering instead of deferred.
     
    Lipoly, NeatWolf and Killersan like this.
  11. Killersan

    Killersan

    Joined:
    May 27, 2014
    Posts:
    76
    Thank You for pointing me a direction, I'll check if there are shaders which are not going to render in deferred.
     
    Lipoly likes this.