Search Unity

Is there any trick to enable or disable multithread-rendering at runtime?

Discussion in 'General Graphics' started by pippocao, Jan 15, 2016.

  1. pippocao

    pippocao

    Joined:
    Apr 10, 2014
    Posts:
    16
    Multithread-rendering brings a big performance advantage to our Android game. but we found it would crashes occasionally on some devices. So we want to disable it on some devices, but as we know, multitread-rendering is a option of building phase, and there is no API to enable or disable it at runtime.
    I was once to think that we can modify the "assets/bin/mainData" file to enable or disable it before the UnityPlayer Activity start. But sadly I found files under "assets" folder is read only, can not be modified.
    So, is there another method or trick to toggle multithread-rendering at runtime?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    I know that its not the preferred way of doing it: You can upload a second apk for specific devices for the Google play store if that what you are targeting. Then you can only list devices that have problems with the multithreading and ship to those devices the single thread version.
     
  3. pippocao

    pippocao

    Joined:
    Apr 10, 2014
    Posts:
    16
    Thx your reply, maybe it's a good solution. But we have hundreds download channels in Chinese android market which many of these can not specify devices, and very few people use Google Play in China.
     
  4. johnpine

    johnpine

    Joined:
    Sep 19, 2015
    Posts:
    26
    yes would be definitely a good option to switch this runtime. but i guess thats not gonna happen.
    multithreaded rendering is also causing other problems still ( unity version 5.3.5f1 ).
    like with vsync on gui sometimes flickers on android with multithreaded rendering.
    so basically this cant be enabled anyways. better to leave it off.
    it is impossible to test on all android devices if it works but if you get huge performance benefits then maybe
    you can trade it for the occasional flickering.
     
  5. Packedbox

    Packedbox

    Joined:
    Jun 20, 2013
    Posts:
    20
    Old thread still not possible in Unity 2019 to disable MTRendering for certain device, CPU Type, OS...
    But you can use this trick on IOS :

    Code (CSharp):
    1. In DeviceSettings.mm
    2.  
    3. bool reportFakeCPUcount = 0;
    4.  
    5. extern "C" int UnityDeviceCPUCount()
    6. {
    7.  
    8.     if (_DeviceCPUCount <= 0)
    9.     {
    10.         // maybe would be better to use HW_AVAILCPU
    11.         int     ctlName[]   = {CTL_HW, HW_NCPU};
    12.         size_t  dataLen     = sizeof(_DeviceCPUCount);
    13.  
    14.         ::sysctl(ctlName, 2, &_DeviceCPUCount, &dataLen, NULL, 0);
    15.     }
    16.     if(reportFakeCPUcount){
    17.         reportFakeCPUcount = 0;
    18.         return 1;
    19.     }
    20.     return _DeviceCPUCount;
    21. }
    Code (CSharp):
    1. In UnityAppController.mm
    2.  
    3. extern bool reportFakeCPUcount;
    4.  
    5.  
    6. - (void)startUnity:(UIApplication*)application
    7. {
    8.     NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");
    9.  
    10.    
    11.  
    12.  
    13.  
    14.     if(!UnityiOS110orNewer()){
    15.         NSLog(@"iOS is less than 11, disabling multithread rendering");
    16.         reportFakeCPUcount = 1;
    17.     }
    18.    
    19.    
    20.     UnityInitApplicationGraphics();
    21. ...
     
    AlejMC likes this.
  6. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    Woah, could something similar be done on android?
     
  7. tms_dev

    tms_dev

    Joined:
    Nov 26, 2018
    Posts:
    47
  8. CChong_at_Infinity

    CChong_at_Infinity

    Joined:
    Apr 7, 2020
    Posts:
    27
  9. tms_dev

    tms_dev

    Joined:
    Nov 26, 2018
    Posts:
    47
    It's in PlayerSettings, so it should work on runtime as far as I know. It says "Enable or disable multithreaded rendering option for mobile platform."
     
  10. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    PlayerSettings is in the UnityEditor namespace, from an editor assembly. Therefore, no, it is not usable at runtime in a player build.
     
  11. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    I know that on PC it can be toggled in the boot.config file (gfx-disable-mt-rendering=1). It's clear that you have to restart the program but maybe you could detect if the game crashed last time and then disable the flag for the next run. Not sure if this works on Android and it's very hacky, so I don't recommend it.

    Better option would be to provide different APKs for different devices (assuming you know on which devices it is unstable)