diff --git a/Assets/_Scripts/ObjectLoader.cs b/Assets/_Scripts/ObjectLoader.cs index d2022172b27d7b29935939e1ae7979edd60fd8a7..69ee81450781fa3298d0267e199e4ba1596ec1e2 100644 --- a/Assets/_Scripts/ObjectLoader.cs +++ b/Assets/_Scripts/ObjectLoader.cs @@ -1,4 +1,4 @@ -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);