Commit ffecf24a authored by Per-Morten Straume's avatar Per-Morten Straume
Browse files

Dispose of objectsDispose as early as possible

objectsDispose was originally disposed of at the end of the 
LoadFile function. However, we can dispose of it as soon as the
MakeFileObjectSoAJob is finished.

Also added an objectCount variable for clarity, and to avoid using 
objects.Length after scheduling objectDispose.Dispose.
parent bbbe4efc
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
......@@ -53,10 +53,11 @@ public class ObjectLoader : MonoBehaviour
var fileContents = System.IO.File.ReadAllText(filepath);
var objectsList = JsonUtility.FromJson<FileObjects>(fileContents).Objects;
var objectCount = objectsList.Count;
var objectsDispose = objectsList.ViewAsNativeArray(out var objects);
var transforms = new NativeArray<float4x4>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
var meshTypes = new NativeArray<GraphicsSystem.MeshType>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
var materialIndices = new NativeArray<UInt16>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
var transforms = new NativeArray<float4x4>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
var meshTypes = new NativeArray<GraphicsSystem.MeshType>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
var materialIndices = new NativeArray<UInt16>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
JobHandle handle;
handle = new MakeFileObjectSoAJob
......@@ -66,9 +67,11 @@ public class ObjectLoader : MonoBehaviour
MeshTypes = meshTypes,
Transforms = transforms,
}
.Schedule(objects.Length, 64);
.Schedule(objectCount, 64);
var sortingIndices = new NativeArray<int>(objects.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
handle = objectsDispose.Dispose(handle);
var sortingIndices = new NativeArray<int>(objectCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
handle = JobAlgorithms.ScheduleConsecutiveFill(sortingIndices, handle);
handle = new PartitionByMaterial
{
......@@ -84,7 +87,7 @@ public class ObjectLoader : MonoBehaviour
handle = JobAlgorithms.ScheduleShuffle(meshTypes.Reinterpret<byte>(), sortingIndices, handle);
handle = JobAlgorithms.ScheduleShuffle(materialIndices, sortingIndices, handle);
var materialRanges = new NativeList<RangeInt>(materialIndices.Length, Allocator.Persistent);
var materialRanges = new NativeList<RangeInt>(objectCount, Allocator.Persistent);
handle = new RangeifyByMaterial
{
MaterialIndices = materialIndices,
......@@ -122,7 +125,7 @@ public class ObjectLoader : MonoBehaviour
handle = materialRanges.Dispose(handle);
handle = sortingIndices.Dispose(handle);
var finalRangesList = new NativeList<RangeInt>(materialIndices.Length, Allocator.Persistent);
var finalRangesList = new NativeList<RangeInt>(objectCount, Allocator.Persistent);
handle = new RangeifyByMaterialAndMesh
{
MaterialIndices = materialIndices,
......@@ -131,13 +134,12 @@ public class ObjectLoader : MonoBehaviour
}
.Schedule(handle);
handle = objectsDispose.Dispose(handle);
// Need to wait for the jobs to complete here because we need to copy finalRangesList to ranges
JobHandle.ScheduleBatchedJobs();
while (!handle.IsCompleted)
yield return null;
handle.Complete();
var ranges = new NativeArray<RangeInt>(finalRangesList, Allocator.Persistent);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment