Search Unity

RaycastCommand.ScheduleBatch assertion failed when using large buffers

Discussion in 'Entity Component System' started by fra3point, Jan 13, 2019.

  1. fra3point

    fra3point

    Joined:
    Aug 20, 2012
    Posts:
    269
    Hello,

    I'm using RaycastCommands to gain a bit of performance when casting millions of rays in Unity 2018.3.1, but I'm getting a strange message if (and only if) I use large buffers.

    Here's the code (you can just attach it to some object and press play):

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Jobs;
    3. using UnityEngine;
    4.  
    5. public class RaycastCommandExample : MonoBehaviour{
    6.  
    7.     public int raycastCount = 2048*2048;
    8.  
    9.     [ReadOnly] private NativeArray<RaycastHit> results;
    10.     [ReadOnly] private NativeArray<RaycastCommand> commands;
    11.  
    12.     void Start() {
    13.  
    14.         results = new NativeArray<RaycastHit>(raycastCount, Allocator.TempJob);
    15.         commands = new NativeArray<RaycastCommand>(raycastCount, Allocator.TempJob);
    16.  
    17.         for (int i = 0; i < raycastCount; i++) {
    18.             commands[i] = new RaycastCommand(Vector3.zero, Vector3.forward, 1);
    19.         }
    20.  
    21.         JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 32, default(JobHandle));
    22.         handle.Complete();
    23.    
    24.         //...do something with results array entries
    25.  
    26.         commands.Dispose();
    27.         results.Dispose();
    28.     }
    29. }

    When raycastCount is lower than a certain value (that in my case happens to be 1568529), I get no messages.
    But when I use a higher value I get this:

    upload_2019-1-13_12-4-19.png


    Does anybody know what is happening?

    Thanks in advance,
    Francesco
     
    Last edited: Jan 13, 2019
    CityGen3D likes this.
  2. CityGen3D

    CityGen3D

    Joined:
    Nov 23, 2012
    Posts:
    682
    I have exactly the same issue.
    Anyone know if this is a new problem?
    I'm new to the job system so don't know if this has always been the case. I may try a slightly earlier Unity version to test.
     
    fra3point likes this.
  3. fra3point

    fra3point

    Joined:
    Aug 20, 2012
    Posts:
    269
    I've tried different versions (2018.1, 2018.2, 2018.3) and all have this problem.
    Still didn't figure out the cause...
     
    CityGen3D likes this.
  4. CityGen3D

    CityGen3D

    Joined:
    Nov 23, 2012
    Posts:
    682
    No not sure either.
    As far as I can tell my code still works as intended with all batched raycasts completed, so I think it may just be an erroneous assert.
    Does your code still work as intended, or you can see other issues in addition to the error message?
     
  5. fra3point

    fra3point

    Joined:
    Aug 20, 2012
    Posts:
    269
    Everything works fine. All the rays in the batch do what they should.
     
  6. Joouur

    Joouur

    Joined:
    Feb 18, 2016
    Posts:
    10
    A wild guess but I think they probably limit the amount of ray according to what the max size a batch buffer can handle.

    I ran into this issue before, I decided to divide by groups the rays then use a job to initialized the commands, then batch the raycast, something like this.

    Code (CSharp):
    1.   protected override JobHandle OnUpdate(JobHandle handle)
    2.   {
    3.     for (int i = 0; i < groups.Length; ++i)
    4.     {
    5.       int Length = groups[i].NumberOfRaysOnGroup;
    6.    
    7.       NativeArray<RaycastCommand> raycastCommands = new NativeArray<RaycastCommand>(
    8.         Length, Allocator.TempJob, NativeArrayOptions.ClearMemory
    9.       );
    10.       NativeArray<RaycastHit> raycastHits = new NativeArray<RaycastHit>(
    11.         Length, Allocator.TempJob, NativeArrayOptions.ClearMemory
    12.       );
    13.  
    14.       InitializeRaycastCommandJob setupRaycastsJob = new InitializeRaycastCommandJob()
    15.       {
    16.         Raycasts = raycastCommands,
    17.         Tracer = Rays_LUT[i]
    18.       };
    19.       //Schedule the commands
    20.       JobHandle setupDependency = setupRaycastsJob.Schedule(Length,
    21.         Length / Job_Settings.InitializeRayCastCommandJobDivider, handle);
    22.       //Schedule the rays on batch
    23.       JobHandle raycastDependency = RaycastCommand.ScheduleBatch(raycastCommands,
    24.         raycastHits, Length / Job_Settings.MinimumRaysPerJobDivider, setupDependency);
    25.     }
    26.   }
     
    Last edited: Jan 29, 2019
    fra3point likes this.
  7. fra3point

    fra3point

    Joined:
    Aug 20, 2012
    Posts:
    269
    Good workaround! Thanks for sharing!!
    Do you think the max batch size is something hard coded? 'Cause to me only lengths higher than 1568529 produce this error. This is not a common number...
     
  8. Joouur

    Joouur

    Joined:
    Feb 18, 2016
    Posts:
    10
    I don't think is a hard coded value, more of like how much can your memory handle, mostly because for you 1568529 but for me is 1569748.
     
    fra3point likes this.