Search Unity

Question IndexOutOfRangeException in Entities 0.51 and 2021 LTS within HybridV2Culling.cs

Discussion in 'Entity Component System' started by Tony1974, Jul 11, 2022.

  1. Tony1974

    Tony1974

    Joined:
    May 26, 2018
    Posts:
    11
    Hi,

    I originally posted this within the following thread https://forum.unity.com/threads/experimental-entities-0-51-is-available.1281233/
    However, it was suggested it might be worth its own thread.

    I updated to 0.51 and 2021.3.6 LTS and during game play I constantly receive errors similar to the following:

    Code (CSharp):
    1. IndexOutOfRangeException: Index 15 is out of range of '15' Length.
    2. Unity.Collections.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at <4a31731933e0419ca5a995305014ad37>:0)
    3. Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at <4a31731933e0419ca5a995305014ad37>:0)
    4. Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at <4a31731933e0419ca5a995305014ad37>:0)
    5. Unity.Rendering.SimpleCullingJob.Execute (Unity.Entities.ArchetypeChunk archetypeChunk, System.Int32 chunkIndex, System.Int32 firstEntityIndex) (at Library/PackageCache/com.unity.rendering.hybrid@0.51.0-preview.32/Unity.Rendering.Hybrid/HybridV2Culling.cs:351)
    6. Unity.Entities.JobChunkExtensions+JobChunkProducer`1[T].ExecuteInternal (Unity.Entities.JobChunkExtensions+JobChunkWrapper`1[T]& jobWrapper, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@0.51.0-preview.32/Unity.Entities/IJobChunk.cs:401)
    7. Unity.Entities.JobChunkExtensions+JobChunkProducer`1[T].Execute (Unity.Entities.JobChunkExtensions+JobChunkWrapper`1[T]& jobWrapper, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@0.51.0-preview.32/Unity.Entities/IJobChunk.cs:368)
    Since these errors are all occurring within the Entities/HybridV2 packages and the stack trace does not lead back to my code I have no idea what is causing it or how to fix it.

    The issue appears to be due to an invalid index reference for the variable finalindex into the chunkInstanceBounds array within the SimpleCullingJob struct of HybridV2Culling.cs. See below:

    Code (CSharp):
    1. var finalIndex = (j << 6) + bitIndex;
    2.  
    3.                                 scratch[instanceOutputOffset] = processedInstanceCount + finalIndex;
    4.                                 // IndexList[batchOutputOffset + batchOutputCount] = processedInstanceCount + finalIndex;
    5.                                 // Debug.Log($"DEBUGCULLING Partial {externalBatchIndex}: [{batchOutputOffset + batchOutputCount}] = {processedInstanceCount + finalIndex}");
    6.  
    7.                                 int advance = FrustumPlanes.Intersect2(Planes, chunkInstanceBounds[finalIndex].Value) !=
    8.                                     FrustumPlanes.IntersectResult.Out
    9.                                     ? 1
    10.                                     : 0;

    Not sure if this is a bug or something I am simply doing wrong.

    Has anyone else had this issue and were able to fix?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,423
    Yes but a new thread in the DOTS forums I suspect.

    Want me to move your post there?
     
  3. Tony1974

    Tony1974

    Joined:
    May 26, 2018
    Posts:
    11
    Yes please. Couldn't find a DOTs forum myself.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,423
    It was the one you linked. I'll move your post.
     
  5. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    284
    Hi, you took a nice detour :) Welcome to the DOTS forum.

    If everything worked before the upgrade this might not be your issue, but seeing hybridV2Culling.cs in your stacktrace it might be worth a shot. If you are using LODs.

    In the past there was an issue with LODs when moving backwards. And the error also says something about Index out of range: https://forum.unity.com/threads/hybrid-renderer-v2-0-4-0.847546/page-10

    The fix was to change in HybritV2Culling.cs (think its line 159 now):
    Code (CSharp):
    1. float graceDistance = float.MaxValue;
    2. to
    3. float graceDistance = 0f;
    Unity will try to convert back changes to code from packages, so the most stable way to do this is to move the Package from the PackageCache to the Package folder. Before making the change.
    (Updating this package in the future would require you to remove the package from the package folder, update the package, place it back in the folder, re-apply the fix.)

    PackageCache folder is in your projects Library folder, and the Package folder is in the same folder as your Library folder.
     
    BigRookGames likes this.
  6. Tony1974

    Tony1974

    Joined:
    May 26, 2018
    Posts:
    11
    Hey thanks for getting back to me.

    Yeah haven't really been active on the forums and obviously a bit of a newbie here. Stumbling my way through. :(

    Yes, everything did work before the upgrade and I have had to downgrade the project back to Entities 0.17 to get it working again.

    However I did keep a copy of the later version and will try as you suggested.

    I didn't know you could modify the Unity packages like that so that is very useful information too.

    Thanks again. I'll post back to let you know how I went.
     
  7. Tony1974

    Tony1974

    Joined:
    May 26, 2018
    Posts:
    11
    Ok. So I applied this fix and it did the trick.

    My Project is now up and running again. Thanks heaps for your help.
     
    Arnold_2013 likes this.