Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug BatchRendererGroup crash when change material properties

Discussion in 'Graphics for ECS' started by furm, Dec 14, 2023.

  1. furm

    furm

    Joined:
    Apr 24, 2016
    Posts:
    13
    Last week I reported this issue. but I didnt find a good way to reproduce at that time. Since no one keep watching that thread, I have to open a new one.
    The problem is ,when I use Meshrenderer.material.setxxx(id,value) with new BatchRendererGroup , there is a probability of crashing

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using Unity.Jobs;
    4. using UnityEngine;
    5. using UnityEngine.Rendering;
    6. using Random = UnityEngine.Random;
    7.  
    8. public class test : MonoBehaviour
    9. {
    10.     public GameObject cube;
    11.     private List<MeshRenderer> mrs;
    12.  
    13.     private const int cap = 5000;
    14.     private int id = Shader.PropertyToID("_BaseColor");
    15.     private MaterialPropertyBlock mpb;
    16.  
    17.     private BatchRendererGroup m_BatchRendererGroup;
    18.     void Start()
    19.     {
    20.         m_BatchRendererGroup = new BatchRendererGroup(OnPerformCulling, IntPtr.Zero);
    21.      
    22.         mpb = new MaterialPropertyBlock();
    23.         mrs = new List<MeshRenderer>(cap);
    24.         for (int i = 0; i < cap; i++)
    25.         {
    26.             var obj = Instantiate(cube,transform);
    27.             obj.transform.position = cube.transform.position + new Vector3(Random.Range(-5f, 5f),Random.Range(-5f, 5f), Random.Range(-5f, 5f));
    28.  
    29.             mrs.Add(obj.GetComponent<MeshRenderer>());
    30.         }
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         for (int i = 0; i < cap; i++)
    37.         {
    38.             var mr = mrs[i];
    39.             // mr.GetPropertyBlock(mpb);
    40.             var randColor = new Color(Random.value,Random.value,Random.value);
    41.             mr.material.SetColor(id,randColor);
    42.             // mpb.SetColor(id,randColor);
    43.             // mr.SetPropertyBlock(mpb);
    44.         }
    45.     }
    46.  
    47.     public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCullingContext cullingContext, BatchCullingOutput cullingOutput, IntPtr userContext)
    48.     {
    49.         return new JobHandle();
    50.     }
    51. }
    52.  
    Try the code above, build with a development build( higher probability to crash) , apk will first freeze in a while, and then with memory increased ,it will finally crash.

    Use mpb instead can avoid crash (however if you keep create new MaterialPropertyBlock,there is still a chance to crash),

    I wonder if change material with brg is allowed? if not ,what should I do? if it is a bug,Please fix it asap, Thx.
     
    Last edited: Dec 16, 2023
  2. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    352
    Modifying the material should work, it should not cause a crash.

    Is the Material registered with the BRG? Your code snippet does not contain any RegisterMaterial calls.

    Which platforms does the problem happen on? Does it only happen on Android, or also on some other platform?
     
  3. furm

    furm

    Joined:
    Apr 24, 2016
    Posts:
    13
    No, no Materail registered with the BRG. My test case shows that only new BatchRendererGroup(OnPerformCulling, IntPtr.Zero) is enough. Add detail logic like register mat or fill OnPerformCulling function does not make any difference.

    When I dispose BatchRendererGroup or dont add BatchRendererGroup at all. no crash.

    My test platform is android and ios, both crashed. pc is ok for now.
     
  4. furm

    furm

    Joined:
    Apr 24, 2016
    Posts:
    13
    Another hint is , if I keep dispose BatchRendererGroup and recreate it in Update(), crash disappeared. So I doubt if there is some unexcepted memory access?
     
  5. furm

    furm

    Joined:
    Apr 24, 2016
    Posts:
    13
  6. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    352
    When you observe the crash on Android, do you see something like this in your logcat output?
    Code (CSharp):
    1. D/Unity: Could not allocate memory: System out of memory!
    2.     Trying to allocate: 1073741824B with 16 alignment. MemoryLabel: DynamicArray
    3.  
    Specifically, "trying to allocate" with some ridiculously large number.

    If yes, then this looks like it's another appearance of an ARM specific bug in an internal software component, which BRG happens to use in its implementation.
     
  7. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    352
    After more investigation, I think it seems very likely this is caused by an ARM specific internal bug which causes a threading race condition. I have forwarded the bug to the appropriate team.
     
  8. furm

    furm

    Joined:
    Apr 24, 2016
    Posts:
    13
    I upload the full log from open app to crash. The error part is here.

    Code (CSharp):
    1. 2023-12-19 11:30:50.601 19633-29852 qdgralloc               com.xxxxxxx.Yyyyyyyyyyyyyyy          E  GetGpuPixelFormat: No map for format: 0x38
    2. 2023-12-19 11:30:50.601 19633-29852 AdrenoUtils             com.xxxxxxx.Yyyyyyyyyyyyyyy          E  <validate_memory_layout_input_parmas:1984>: Unknown Format 0
    3. 2023-12-19 11:30:50.601 19633-29852 AdrenoUtils             com.xxxxxxx.Yyyyyyyyyyyyyyy          E  <adreno_init_memory_layout:4723>: Memory Layout input parameter validation failed!
    4. 2023-12-19 11:30:50.601 19633-29852 qdgralloc               com.xxxxxxx.Yyyyyyyyyyyyyyy          E  GetGpuResourceSizeAndDimensions Graphics metadata init failed
    5. 2023-12-19 11:30:50.601 19633-29852 Gralloc4                com.xxxxxxx.Yyyyyyyyyyyyyyy          E  isSupported(1, 1, 56, 1, ...) failed with 1
    6. 2023-12-19 11:30:50.601 19633-29852 GraphicBufferAllocator  com.xxxxxxx.Yyyyyyyyyyyyyyy          E  Failed to allocate (4 x 4) layerCount 1 format 56 usage b00: 1
    7. 2023-12-19 11:30:50.601 19633-29852 AHardwareBuffer         com.xxxxxxx.Yyyyyyyyyyyyyyy          E  GraphicBuffer(w=4, h=4, lc=1) failed (Unknown error -1), handle=0x0
    8. 2023-12-19 11:30:50.601 19633-29852 qdgralloc               com.xxxxxxx.Yyyyyyyyyyyyyyy          E  GetGpuPixelFormat: No map for format: 0x38
    9. 2023-12-19 11:30:50.601 19633-29852 AdrenoUtils             com.xxxxxxx.Yyyyyyyyyyyyyyy          E  <validate_memory_layout_input_parmas:1984>: Unknown Format 0
    10. 2023-12-19 11:30:50.601 19633-29852 AdrenoUtils             com.xxxxxxx.Yyyyyyyyyyyyyyy          E  <adreno_init_memory_layout:4723>: Memory Layout input parameter validation failed!
    11. 2023-12-19 11:30:50.601 19633-29852 qdgralloc               com.xxxxxxx.Yyyyyyyyyyyyyyy          E  GetGpuResourceSizeAndDimensions Graphics metadata init failed
    12. 2023-12-19 11:30:50.601 19633-29852 Gralloc4                com.xxxxxxx.Yyyyyyyyyyyyyyy          E  isSupported(1, 1, 56, 1, ...) failed with 1
    13. 2023-12-19 11:30:50.601 19633-29852 GraphicBufferAllocator  com.xxxxxxx.Yyyyyyyyyyyyyyy          E  Failed to allocate (4 x 4) layerCount 1 format 56 usage b00: 1
    14. 2023-12-19 11:30:50.601 19633-29852 AHardwareBuffer         com.xxxxxxx.Yyyyyyyyyyyyyyy          E  GraphicBuffer(w=4, h=4, lc=1) failed (Unknown error -1), handle=0x0
    I am not sure if its the same issue as you mentioned.
     

    Attached Files:

  9. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    352
    Unfortunately there doesn't seem to be anything specific to this error in the log, but your earlier callstack from the other thread fully matches what I would expect from the error I suspect this to be, so I think it's very likely your issue is caused by that.
     
  10. furm

    furm

    Joined:
    Apr 24, 2016
    Posts:
    13
    OK, Thank you for the reply. Wonder when will this bug be fixed? Is there a plan for this issue in the future release? Anyway, if the bug is fixed, please inform me here, thx.
     
  11. zzermanli

    zzermanli

    Joined:
    Sep 1, 2021
    Posts:
    2
    It seems we got the same out-of-memory crash.
    https://forum.unity.com/threads/uni...fatal-out-of-memory-crash-on-android.1528546/
    It's very easy for me to reproduce this crash.
    Literally you can reproduce it by adding Entities.Graphics to any project.
    Then build an Android app, run it, wait 30 seconds, if not crashing, kill the process and re-run it.
    It could crash with about 5% chance. All 2022 LTS version could occur.
     
  12. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    352
    Unity 2023.3.0b5 and 2022.3.19f1 should contain a fix to this issue.
     
  13. btsjamin

    btsjamin

    Joined:
    May 10, 2022
    Posts:
    1
    Do you know when these versions will be released?
     
  14. zhoutaosheng-elex

    zhoutaosheng-elex

    Joined:
    Mar 31, 2021
    Posts:
    12
    I encountered a similar issue when I tested with the official sample "boids". After running more than ten minutes, I received a "System out of memory" error in the log
    upload_2024-1-29_10-32-37.png
    and it was clearly a low memory kill event.

    upload_2024-1-29_10-35-21.png

    Has this issue also been fixed in 2022.3.19f1?
     
  15. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    352
    This is likely the same issue (the "trying to allocate" is an extremely large number, because of a threading problem), so it should be fixed.
     
  16. furm

    furm

    Joined:
    Apr 24, 2016
    Posts:
    13
    Thank you. We tested on several devices, no crash in 2022.3.19f1.
     
  17. IAmChiagozie

    IAmChiagozie

    Joined:
    Jun 26, 2017
    Posts:
    49
    I think I encounter the same issue with BatchRendererGroup on the editor. But this is happening with Entities Graphics.
    Here's my crash log.
    upload_2024-6-14_10-5-24.png