Search Unity

"Lightmap Index" and Draw-Call Batching

Discussion in 'General Graphics' started by Peter77, Jun 3, 2019.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,610
    Looking at the Frame Debugger shows a lot of draw-calls are generated because of different lightmapIndex values in our game.

    I assumed objects with the same material, which are close to each other, would share the same lightmap(index) if there is enough space on the texture.

    However, it seems Unity does not try to assign the same lightmap to objects in close proximity, which would reduce draw-calls, because of less render state changes when rendering what's in the view.

    This is basic stuff, but I'm new to lightmaps and I must be missing something here. What can I do to achieve that Unity assigns the same lightmap to objects in close proximity?

    upload_2019-6-3_9-17-6.png

    upload_2019-6-3_9-17-56.png
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,756
    Peter77 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,610
    Many thanks for the quick answer!
     
    AcidArrow likes this.
  4. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Hi Peter,

    Currently Unity does not handle this for you automatically. However, you can do this manually by assigning equal "Baked Tags" to objects that you want to group into same lightmaps.

    See "Baked Tag" in the docs for more info.
     

    Attached Files:

    Peter77 and Adam-Bailey like this.
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,610
    Hi Rasmusn,

    many thanks for your answer. I was able to reduce draw-calls by 38% by using the "Baked Tag".

    Below I describe how I achieved this and would like to get some feedback on whether this is the approach you recommend, because it felt kinda odd in the way I used it.

    My initial assumption was that "Baked Tag" would be part of a Renderer. That would allow me just iterate over all renderers in a scene and assign a baked tag. However, there is no Baked Tag in a Renderer, but there is the LightmapParameters object.

    In order to assign a different Baked Tag to individual objects, I created a few hundred LightmapParameters assets in the project and assigned an unique bake tag to each of those assets.

    upload_2019-6-14_9-13-57.png

    Then I wrote an editor tool that spatially groups renderers ("world" is represented by several square cells/sectors) and assigns one of those LightmapParameters asset depending on what cell in the world-grid the renderer occupies. This makes sure all objects in a world-cell use the same baked tag.

    Before
    upload_2019-6-14_9-40-22.png

    After
    upload_2019-6-14_9-40-16.png

    A draw-call reduction of about 38%. That is great!

    However, the workflow of having those hundreds of LightmapParameters assets in the project feels like that's not the way you want your users to work with that. Specifically because it seems there is no way to only modify the baked tag. What if I want to keep all settings the same except for the baked tag?

    How would you assign the Baked Tag automatically to renderers without having to create hundreds of LightmapParameters?

    Thanks for your help!
     
    Last edited: Jun 14, 2019
    Torbach78, hippocoder and JamesArndt like this.
  6. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    Thank you for that detailed description and nice-looking screenshots. I think what you are doing is similar to my approach. Your analysis is spot on. The problem you point out is exactly what motivates us to do automatic spatially coherent lightmaps. I am afraid there is no better way at the moment. AFAIK bake tags were not originally introduced to solve the particular problem of lightmap batching, it just happens that you can use them like this. I hope this somewhat explains the suboptimal design. Since we have many other priorities I cannot promise you when/if a solution will be released. But we are aware and this issue is definitely something that we want to solve.
     
    Peter77, hippocoder and JamesArndt like this.
  7. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Any ETA when PLM will support automatic lightmap tag grouping?
     
    leandrovtd and Peter77 like this.
  8. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,610
    Any progress on this?
     
  9. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    No, unfortunately not. It is still on our radar and something that we want to do eventually, but I do not know currently whether and when it will happen.
     
  10. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Is there a reason this isn't prioritised? It seems like a relatively straightforward/backend change that would be a free performance win across the board for most developers. I am asking because I am wondering if this is not something Unity thinks is worth investing effort in, maybe it is not as big of a deal as it seems and I shouldn't invest time in it either.

    If not, could we have some reliable lightmapping callbacks (https://docs.unity3d.com/ScriptReference/Lightmapping-bakeStarted.html this doesn't work when Auto is enabled) and some way to manipulate lightmap params pre-bake via code?

    I was thinking I could hook into bakeStarted, collect renderers, group them, instantiate lightmapParams's for each cluster and assign them to their own lightmaps that way, marking them as DontSave https://docs.unity3d.com/ScriptReference/HideFlags.DontSave.html

    This seems like it'd work, but only if the user pressed Bake button, and I am not sure what happens if the bake is cancelled - do I get a bakeCompleted callback? I guess not, because it didn't complete? I have no reliable way to clean up.

    Not to mention it all feels very janky and gross.
     
    Peter77 likes this.
  11. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,756
    I don't know if it's a huge deal, but it's definitely a "nice to have".

    I think they have completely frozen the lightmapper feature set to be able to get it out of preview.

    I sincerely hope that once they get it out of preview it's not the end of PLM development, but rather the start, since there are a bunch of features that would be really nice to have (this one included), which should be reasonably easy (=a lot of work, but doable), once the basis is stable.

    So I'm willing to give them a pass... for now.
     
    Prodigga likes this.