unsafe void UpdateDynamicInstanceRenderer() { if (m_DynamicChunks.Length == 0) { return; } Profiler.BeginSample("Gather Types"); var localToWorldType = GetArchetypeChunkComponentType <LocalToWorld>(true); var visibleLocalToWorldType = GetArchetypeChunkComponentType <VisibleLocalToWorld>(false); var meshInstanceRendererType = GetArchetypeChunkSharedComponentType <MeshInstanceRenderer>(); var meshInstanceFlippedTagType = GetArchetypeChunkComponentType <MeshInstanceFlippedWindingTag>(); var worldMeshRenderBoundsType = GetArchetypeChunkComponentType <WorldMeshRenderBounds>(true); var meshLODComponentType = GetArchetypeChunkComponentType <MeshLODComponent>(true); var activeLODGroupMask = GetComponentDataFromEntity <ActiveLODGroupMask>(true); Profiler.EndSample(); Profiler.BeginSample("Allocate Temp Data"); var chunkVisibleCount = new NativeArray <int>(m_DynamicChunks.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var packedChunkIndices = new NativeArray <int>(m_DynamicChunks.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); Profiler.EndSample(); var cullLODToVisibleJob = new CullLODToVisible { Chunks = m_DynamicChunks, ActiveLODGroupMask = activeLODGroupMask, MeshLODComponentType = meshLODComponentType, LocalToWorldType = localToWorldType, WorldMeshRenderBoundsType = worldMeshRenderBoundsType, ChunkBounds = null, Planes = m_Planes, VisibleLocalToWorldType = visibleLocalToWorldType, ChunkVisibleCount = chunkVisibleCount, }; var cullLODToVisibleJobHandle = cullLODToVisibleJob.Schedule(m_DynamicChunks.Length, 64); var packedChunkCount = 0; var packVisibleChunkIndicesJob = new PackVisibleChunkIndices { Chunks = m_DynamicChunks, ChunkVisibleCount = chunkVisibleCount, PackedChunkIndices = packedChunkIndices, PackedChunkCount = &packedChunkCount }; var packVisibleChunkIndicesJobHandle = packVisibleChunkIndicesJob.Schedule(cullLODToVisibleJobHandle); packVisibleChunkIndicesJobHandle.Complete(); Profiler.BeginSample("Process DrawMeshInstanced"); var drawCount = 0; var lastRendererIndex = -1; var batchCount = 0; var flippedWinding = false; for (int i = 0; i < packedChunkCount; i++) { var chunkIndex = packedChunkIndices[i]; var chunk = m_DynamicChunks[chunkIndex]; var rendererIndex = chunk.GetSharedComponentIndex(meshInstanceRendererType); var activeCount = chunkVisibleCount[chunkIndex]; var rendererChanged = rendererIndex != lastRendererIndex; var fullBatch = ((batchCount + activeCount) > 1023); var visibleTransforms = chunk.GetNativeArray(visibleLocalToWorldType); var newFlippedWinding = chunk.Has(meshInstanceFlippedTagType); if ((fullBatch || rendererChanged || (newFlippedWinding != flippedWinding)) && (batchCount > 0)) { RenderBatch(lastRendererIndex, batchCount); drawCount++; batchCount = 0; } CopyTo(visibleTransforms, activeCount, m_MatricesArray, batchCount); flippedWinding = newFlippedWinding; batchCount += activeCount; lastRendererIndex = rendererIndex; } if (batchCount > 0) { RenderBatch(lastRendererIndex, batchCount); drawCount++; } Profiler.EndSample(); packedChunkIndices.Dispose(); chunkVisibleCount.Dispose(); }
private unsafe void UpdateInstanceRenderers(NativeArray <ArchetypeChunk> chunks, WorldRenderBounds *worldRenderBounds) { Profiler.BeginSample("Gather Types"); var localToWorldType = this.GetArchetypeChunkComponentType <LocalToWorld>(true); var visibleLocalToWorldType = this.GetArchetypeChunkComponentType <VisibleLocalToWorld>(); var visibleIndexType = this.GetArchetypeChunkComponentType <VisibleIndex>(); var worldRenderBoundsType = this.GetArchetypeChunkComponentType <WorldRenderBounds>(true); var meshLODComponentType = this.GetArchetypeChunkComponentType <MeshLODComponent>(true); var activeLODGroupMask = this.GetComponentDataFromEntity <ActiveLODGroupMask>(true); var renderMeshType = this.GetArchetypeChunkSharedComponentType <RenderMesh>(); var flippedWindingTagType = this.GetArchetypeChunkComponentType <RenderMeshFlippedWindingTag>(); Profiler.EndSample(); Profiler.BeginSample("Allocate Temp Data"); var chunkVisibleCount = new NativeArray <int>(chunks.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); var packedChunkIndices = new NativeArray <int>(chunks.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); Profiler.EndSample(); var cullLODToVisibleJob = new CullLODToVisible { Chunks = chunks, ActiveLODGroupMask = activeLODGroupMask, MeshLODComponentType = meshLODComponentType, WorldRenderBoundsType = worldRenderBoundsType, VisibleIndexType = visibleIndexType, ChunkBounds = worldRenderBounds, Planes = this.m_Planes, ChunkVisibleCount = chunkVisibleCount, LocalToWorldType = localToWorldType, VisibleLocalToWorldType = visibleLocalToWorldType, }; var cullLODToVisibleJobHandle = cullLODToVisibleJob.Schedule(chunks.Length, 64); var packedChunkCount = 0; var packVisibleChunkIndicesJob = new PackVisibleChunkIndices { Chunks = chunks, ChunkVisibleCount = chunkVisibleCount, PackedChunkIndices = packedChunkIndices, PackedChunkCount = &packedChunkCount, }; var packVisibleChunkIndicesJobHandle = packVisibleChunkIndicesJob.Schedule(cullLODToVisibleJobHandle); packVisibleChunkIndicesJobHandle.Complete(); Profiler.BeginSample("Process DrawMeshInstanced"); var drawCount = 0; var lastRendererIndex = -1; var batchCount = 0; var flippedWinding = false; for (int i = 0; i < packedChunkCount; i++) { var chunkIndex = packedChunkIndices[i]; var chunk = chunks[chunkIndex]; var rendererIndex = chunk.GetSharedComponentIndex(renderMeshType); var activeCount = chunkVisibleCount[chunkIndex]; var rendererChanged = rendererIndex != lastRendererIndex; var fullBatch = (batchCount + activeCount) > 1023; var visibleTransforms = chunk.GetNativeArray(visibleLocalToWorldType); var indices = chunk.GetNativeArray(visibleIndexType); var newFlippedWinding = chunk.Has(flippedWindingTagType); if ((fullBatch || rendererChanged || (newFlippedWinding != flippedWinding)) && (batchCount > 0)) { this.RenderBatch(lastRendererIndex, batchCount); drawCount++; batchCount = 0; } this.GenerateMaterialPropertyBlocks(chunk, indices, rendererIndex, rendererChanged, batchCount, activeCount); CopyTo(visibleTransforms, activeCount, this.m_MatricesArray, batchCount); flippedWinding = newFlippedWinding; batchCount += activeCount; lastRendererIndex = rendererIndex; } if (batchCount > 0) { this.RenderBatch(lastRendererIndex, batchCount); drawCount++; } Profiler.EndSample(); packedChunkIndices.Dispose(); chunkVisibleCount.Dispose(); }