Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How does the 'Get font features at runtime' work?

Discussion in 'UGUI & TextMesh Pro' started by jeremy_gram, Nov 6, 2019.

  1. jeremy_gram

    jeremy_gram

    Joined:
    Jun 21, 2018
    Posts:
    14
    I am looking for more information about the 'Get font features at runtime' option, which appears to be enabled by default when using the Unity Package for TMP-1.4.1

    The only documentation I can find on this option is in this forum post: https://forum.unity.com/threads/ver...ic-sdf-for-unity-2018-3-now-available.622420/

    From what I can tell, this option doesn't seem to do anything other than cause a massic GC Alloc when generating many characters at the same time. I assume it does more than that?

    Here is our config: We are using a fallback font containing several non-Latin languages, with Atlas Population Mode set to Dynamic. Most of our users use Latin languages, so this is a bit of an optimization -- the character sets for these non-Latin languages would consume several atlases, but with the Dynamic setting we can get away with a single 1K atlas, which ultimately consumes much less memory at runtime (static + several atlases > dynamic + font asset + 1K atlas).

    There is one case when a large amount of unpredictable characters may be loaded at the same time. In the profiler I see something like ~500 characters added (TMP.TryAddCharacter) with an overhead of 50MB GC Alloc. This is coming almost entirely from TMP.UpdateGlyphAdjustmentRecords, which is only called when 'Get font features at runtime' is enabled. If I disable this feature then this GC Alloc is completely removed, and all features appear to continue working as expected.

    So I am wondering, what does 'Get font features at runtime' actually do?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    This function retrieves the potential glyph adjustment pairs (Kerning Pairs) for the newly added characters.

    I am currently making some improvements to this where performance should be significantly improved. Allocations will also reduced by a good amount but that will require an updated version of Unity.

    I'll try to post some Profilling results later tonight.
     
  3. jeremy_gram

    jeremy_gram

    Joined:
    Jun 21, 2018
    Posts:
    14
    That sounds great! But is this feature important? What do I lose when I disable it? Does kerning no longer work properly with dynamic SDF if I disabled this?
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    This feature fetches the kerning data for newly added glyphs so disabling it will no longer retrieve this data for those new glyphs.
     
  5. jeremy_gram

    jeremy_gram

    Joined:
    Jun 21, 2018
    Posts:
    14
    And in this case "new" means "the character I am trying to add dynamically at runtime" ? So if I disable this feature then kerning will not work anymore for dynamic SDF?
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Sort of ... Kerning will still work but since the newly added glyphs won't have any kerning data, their position will not be adjusted.

    P.S. I am doing some testing but should be able to include the performance improvements in the next package release which should be available tomorrow. The memory allocation improvements will require a new version of Unity.
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Here are profiling results in the Editor where I enable / disable a text object and clear the font asset data to force re-fetching glyph adjustment records for 62 glyphs.

    Results in version 1.5.0-preview.1
    upload_2019-11-8_0-10-57.png

    The top results is the firs time the object is enabled and bottom, the 2nd time. UpdateGlyphAdjustmentRecords is called once per glyph or 62 times.

    Here are the results with the changes included in version 1.5.0-preview.2 which should be available within 24 hours.
    upload_2019-11-8_0-17-28.png

    As you can see here the UpdateGlyphAdjustmentRecords is only called once both times. We can also observe a nice performance improvement and reduced allocations.

    I will be able to make additional performance improvements and further reduce allocations but those will require an updated version of Unity and new package with minor changes to the UpdateGlyphAdjustmentRecords function.

    Here is a preview of the result from this new version of Unity and package changes.
    upload_2019-11-8_1-33-8.png
     
  8. jeremy_gram

    jeremy_gram

    Joined:
    Jun 21, 2018
    Posts:
    14
    Thanks, this is great!
     
  9. FIFTYTWO

    FIFTYTWO

    Joined:
    Oct 21, 2014
    Posts:
    49
    Our current project takes 30-40 seconds to change play mode. Profiler says
    TextMeshProUGUI.OnValidate -> TMP.TryAddCharacter takes 20 seconds with allocation around 120 MB. We use Unity 2020.2.0f1. Can I tweak something to improve play mode change times?
     
  10. FIFTYTWO

    FIFTYTWO

    Joined:
    Oct 21, 2014
    Posts:
    49
    It seems I've found the issue. There's a file TMP_FontAsset.cs and a method TryAddCharacterInternal(). There is a case with unbalanced profiler API call which breaks profiler.

    Profiler.BeginSample("TMP.TryAddCharacter") is not balanced with Profiler.EndSample() in the case:
    if (FontEngine.LoadFontFace(sourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
    return;

    I tried to fix this in my local package and in my case TMP is not affecting play mode change time.
     
  11. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    I was made aware of that profiler issue last week which I have seen addressed.

    To make your change permanent (until a new version of the TMP package is available with that change), you will need to make the change to the relevant file in the Global Package Cache.
     
    FIFTYTWO likes this.
  12. CDUnityCoder

    CDUnityCoder

    Joined:
    Jan 10, 2021
    Posts:
    3
     

    Attached Files:

    • 123.png
      123.png
      File size:
      49.6 KB
      Views:
      303