- Home /
Job System without using the main thread
I never did something with threading before, but I try to create an endless terrain with the Unity Job System.
I use JobHandle.ScheduleBatchedJobs();
, but sometimes the job gets calculated on the main thread. Is there any way to make it run in the background, so the game doesn't lag?
AFAICT Job System actively avoids the Main unity thread. If you see a frame drop it is usually not from the job execution necessarily, but something else going wrong.
For example, when I see terrain being mentioned it immediately suggest what is a probable cause: terrain.terrainData.SyncHeightmap();
But you shared no Profiler's Timeline data, not even a line of code, so there is no way to tell for sure.
Thats what my Profiler shows, when my game freezes. Most of the time there are some very smal spikes, because of the generation of the new meshes, but some times there is one big freezes and I thought thats because of the Job System using the main thread.
Well yes the job system has to use the main thread here and there to generate/schedule new jobs. Afaik this cannot be done from other threads. You also always have to return to the main thread to gather the results from all jobs and to render/dispatch a new frame.
Can you perhaps show us the detailed profiler stack of calls so that we mgiht see what takes up all the performance? perhaps there is an easy improvement to be found for this.
It might also be intresting to know how many Jobs you are trying to schedule/generate here. For Burst to need 3 seconds seems like you are up to something big here which you might have to be spread over multiple frames?
If you see the Main thread being this significantly involved in a job execution then it means that you called jobHandle.Complete()
somewhere (explicitly or via dependency chain) thus causing this "all hands on deck" situation.
Also 3.7s is a lot. You need to optimize this job and/or spread it's execution across many frames (>4s) as @Captain_Pineapple suggested
Yes, the Job is very hard so process and need some optimizations. It gives out the vertices and triangles of a 3DPerlinNoise with a Marching Cubes algorithm. The job is called around 41-170 times at once. It makes sense to use the main thread, but is there any option to queue all the jobs, so the main thread doesnt need to be included in the process?
In this example, the job gets called 41 times. I use an IEnumerator to check if every single job is finished. Maybe thats also slowing everything down?
Many calculations take around 200 frames in the workers thread. How do I spread it over multiple frames?
Iam sorry, I am very a noob in this topic.
can you take a look at the alternative view here? so instead of the timeline look at the detailied view. That is a little dropdown in your image right above "Main Thread"
Oh wow, 10s-long stall on 8 threads.
I think that sharing the sources for this GenerateChunkJob
+ managing class might help you here. Especially if you're new to this, sb may even spot mistakes & help you fix it faster (or ever).
public static Action<Vector3, int[], Vector3[]> OnChunkGenerated;
public void GenerateChunk(Vector3 position, Vector3 Size, float resolution, NoiseData data)
{
NativeList<int> Nativetriangles = new NativeList<int>(Allocator.Persistent);
NativeList<float3> NativeVertices = new NativeList<float3>(Allocator.Persistent);
NativeArray<int> NativeTriTable = GetOneDimensialArrayTriTable();
NativeArray<float> Nativecave_thresholdCurveAsArray = new NativeArray<float>(data.cave_thresholdCurveAsArray, Allocator.Persistent);
GenerateChunkJob job = new GenerateChunkJob()
{
position = new float3(position.x, position.y, position.z),
cave_scale = data.cave_scale,
Size = new float3(Size.x, Size.y, Size.z),
cave_threshold = data.cave_threshold,
triangles = Nativetriangles,
vertices = NativeVertices,
resolution = resolution,
overworldHeight = data.overworldHeight,
overworldScale = data.overworldScale,
overworldSeed = data.overworldSeed,
cave_seed = data.cave_seed,
triTable = NativeTriTable,
cave_thresholdcurve = Nativecave_thresholdCurveAsArray,
maxHeight = data.maxHeight,
minHeight = data.minHeight,
};
JobHandle handle = job.Schedule();
JobHandle.ScheduleBatchedJobs();
StartCoroutine(WaitForGenerateChunk(handle, job, position, Nativetriangles, NativeVertices, NativeTriTable, Nativecave_thresholdCurveAsArray));
}
IEnumerator WaitForGenerateChunk(JobHandle handle, GenerateChunkJob job, Vector3 position, NativeList<int> Nativetriangles, NativeList<float3> NativeVertices, NativeArray<int> NativeTriTable, NativeArray<float> Nativecave_thresholdCurceAsArray)
{
yield return new WaitUntil(() => handle.IsCompleted);
int[] triangles = job.triangles.ToArray();
Vector3[] vertices = new Vector3[job.vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = NativeVertices[i];
}
NativeVertices.Dispose();
Nativetriangles.Dispose();
NativeTriTable.Dispose();
Nativecave_thresholdCurceAsArray.Dispose();
OnChunkGenerated?.Invoke(position, triangles, vertices);
}
Answer by Pangamini · Apr 12 at 04:05 PM
AFAIK job can be scheduled to run on the main thread, but only if you called Job.Complete() before it was actually completed (that way, it simply utilizes all available threads, since the main thread is blocked by Complete() anyway). Try to first check if it's actually done (by using IsCompleted), and wait for the next frame if it's not. It's perfectly OK to have the job running during multiple frames.