public unsafe ComponentChanges GatherComponentChangesAsync(EntityQuery query, Allocator allocator, out JobHandle jobHandle) { using (k_GatherComponentChangesAsync.Auto()) { var chunks = query.CreateArchetypeChunkArrayAsync(Allocator.TempJob, out var chunksJobHandle); k_GatherComponentChangesAsyncBufferAlloc.Begin(); m_AllocatedShadowChunksForTheFrame.Clear(); m_AllocatedShadowChunksForTheFrame.ResizeUninitialized(chunks.Length); m_GatheredChanges.Clear(); m_GatheredChanges.Resize(chunks.Length, NativeArrayOptions.ClearMemory); m_RemovedChunkBuffer.Clear(); m_RemovedChunkEntities.Clear(); var result = new NativeReference <Result>(allocator); k_GatherComponentChangesAsyncBufferAlloc.End(); k_GatherComponentChangesAsyncScheduling.Begin(); var changesJobHandle = new GatherComponentChangesJob { TypeIndex = m_TypeIndex, ComponentSize = m_ComponentSize, Chunks = chunks, ShadowChunksBySequenceNumber = m_PreviousChunksBySequenceNumber, GatheredChanges = (ChangesCollector *)m_GatheredChanges.GetUnsafeList()->Ptr }.Schedule(chunks.Length, 1, chunksJobHandle); var allocateNewShadowChunksJobHandle = new AllocateNewShadowChunksJob { TypeIndex = m_TypeIndex, ComponentSize = m_ComponentSize, Chunks = chunks, ShadowChunksBySequenceNumber = m_PreviousChunksBySequenceNumber, AllocatedShadowChunks = (ShadowChunk *)m_AllocatedShadowChunksForTheFrame.GetUnsafeList()->Ptr }.Schedule(chunks.Length, 1, chunksJobHandle); var copyJobHandle = new CopyComponentDataJob { TypeIndex = m_TypeIndex, ComponentSize = m_ComponentSize, Chunks = chunks, ShadowChunksBySequenceNumber = m_PreviousChunksBySequenceNumber, AllocatedShadowChunks = (ShadowChunk *)m_AllocatedShadowChunksForTheFrame.GetUnsafeList()->Ptr, RemovedChunkComponentDataBuffer = m_RemovedChunkBuffer, RemovedChunkEntities = m_RemovedChunkEntities }.Schedule(JobHandle.CombineDependencies(changesJobHandle, allocateNewShadowChunksJobHandle)); var concatResultJobHandle = new ConcatResultJob { ComponentSize = m_ComponentSize, Allocator = allocator, GatheredChanges = m_GatheredChanges.AsDeferredJobArray(), RemovedChunkComponentDataBuffer = m_RemovedChunkBuffer.AsDeferredJobArray(), RemovedChunkEntities = m_RemovedChunkEntities.AsDeferredJobArray(), Result = result }.Schedule(copyJobHandle); jobHandle = JobHandle.CombineDependencies(chunks.Dispose(copyJobHandle), concatResultJobHandle); k_GatherComponentChangesAsyncScheduling.End(); return(new ComponentChanges(m_TypeIndex, result)); } }
public unsafe ComponentChanges GatherComponentChangesAsync(EntityQuery query, Allocator allocator, out JobHandle jobHandle) { var chunks = query.CreateArchetypeChunkArrayAsync(Allocator.TempJob, out var chunksJobHandle); var allocatedShadowChunksForTheFrame = new NativeArray <ShadowChunk>(chunks.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var gatheredChanges = new NativeArray <ChangesCollector>(chunks.Length, Allocator.TempJob); var removedChunkBuffer = new NativeList <byte>(Allocator.TempJob); var removedChunkEntities = new NativeList <Entity>(Allocator.TempJob); var buffer = new NativeList <byte>(allocator); var addedComponents = new NativeList <Entity>(allocator); var removedComponents = new NativeList <Entity>(allocator); var changesJobHandle = new GatherComponentChangesJob { TypeIndex = m_TypeIndex, ComponentSize = m_ComponentSize, Chunks = chunks, ShadowChunksBySequenceNumber = m_PreviousChunksBySequenceNumber, GatheredChanges = (ChangesCollector *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(gatheredChanges) }.Schedule(chunks.Length, 1, chunksJobHandle); var allocateNewShadowChunksJobHandle = new AllocateNewShadowChunksJob { TypeIndex = m_TypeIndex, ComponentSize = m_ComponentSize, Chunks = chunks, ShadowChunksBySequenceNumber = m_PreviousChunksBySequenceNumber, AllocatedShadowChunks = (ShadowChunk *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(allocatedShadowChunksForTheFrame) }.Schedule(chunks.Length, 1, chunksJobHandle); var copyJobHandle = new CopyComponentDataJob { TypeIndex = m_TypeIndex, ComponentSize = m_ComponentSize, Chunks = chunks, ShadowChunksBySequenceNumber = m_PreviousChunksBySequenceNumber, AllocatedShadowChunks = (ShadowChunk *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(allocatedShadowChunksForTheFrame), RemovedChunkComponentDataBuffer = removedChunkBuffer, RemovedChunkEntities = removedChunkEntities }.Schedule(JobHandle.CombineDependencies(changesJobHandle, allocateNewShadowChunksJobHandle)); var concatResultJobHandle = new ConcatResultJob { ComponentSize = m_ComponentSize, GatheredChanges = gatheredChanges, RemovedChunkComponentDataBuffer = removedChunkBuffer.AsDeferredJobArray(), RemovedChunkEntities = removedChunkEntities.AsDeferredJobArray(), ComponentDataBuffer = buffer, AddedComponents = addedComponents, RemovedComponents = removedComponents }.Schedule(copyJobHandle); var handles = new NativeArray <JobHandle>(5, Allocator.Temp) { [0] = chunks.Dispose(copyJobHandle), [1] = gatheredChanges.Dispose(concatResultJobHandle), [2] = removedChunkBuffer.Dispose(concatResultJobHandle), [3] = removedChunkEntities.Dispose(concatResultJobHandle), [4] = allocatedShadowChunksForTheFrame.Dispose(copyJobHandle) }; jobHandle = JobHandle.CombineDependencies(handles); handles.Dispose(); return(new ComponentChanges(m_TypeIndex, buffer, addedComponents, removedComponents)); }