RaycastCommand.ScheduleBatch assertion failed when using large buffers

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):

Script

using Unity.Collections;
using Unity.Jobs;
using UnityEngine;

public class RaycastCommandExample : MonoBehaviour{

    public int raycastCount = 2048*2048;
 
    [ReadOnly] private NativeArray<RaycastHit> results;
    [ReadOnly] private NativeArray<RaycastCommand> commands;

    void Start() {

        results = new NativeArray<RaycastHit>(raycastCount, Allocator.TempJob);
        commands = new NativeArray<RaycastCommand>(raycastCount, Allocator.TempJob);

        for (int i = 0; i < raycastCount; i++) {
            commands[i] = new RaycastCommand(Vector3.zero, Vector3.forward, 1);
        }

        JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 32, default(JobHandle));
        handle.Complete();
    
        //...do something with results array entries

        commands.Dispose();
        results.Dispose();
    }
}

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:

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

Does anybody know what is happening?

Thanks in advance,
Francesco

1 Like

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.

1 Like

I’ve tried different versions (2018.1, 2018.2, 2018.3) and all have this problem.
Still didn’t figure out the cause…

1 Like

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?

Everything works fine. All the rays in the batch do what they should.

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.

  protected override JobHandle OnUpdate(JobHandle handle)
  {
    for (int i = 0; i < groups.Length; ++i)
    {
      int Length = groups[i].NumberOfRaysOnGroup;
   
      NativeArray<RaycastCommand> raycastCommands = new NativeArray<RaycastCommand>(
        Length, Allocator.TempJob, NativeArrayOptions.ClearMemory
      );
      NativeArray<RaycastHit> raycastHits = new NativeArray<RaycastHit>(
        Length, Allocator.TempJob, NativeArrayOptions.ClearMemory
      );

      InitializeRaycastCommandJob setupRaycastsJob = new InitializeRaycastCommandJob()
      {
        Raycasts = raycastCommands,
        Tracer = Rays_LUT[i]
      };
      //Schedule the commands
      JobHandle setupDependency = setupRaycastsJob.Schedule(Length,
        Length / Job_Settings.InitializeRayCastCommandJobDivider, handle);
      //Schedule the rays on batch
      JobHandle raycastDependency = RaycastCommand.ScheduleBatch(raycastCommands,
        raycastHits, Length / Job_Settings.MinimumRaysPerJobDivider, setupDependency);
    }
  }
1 Like

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…

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.

1 Like