88using Unity . Burst ;
99using UnityEngine . Profiling ;
1010
11- [ assembly: RegisterGenericJobType ( typeof ( UnityEngine . Rendering . RegisterNewInstancesJob < UnityEngine . Rendering . BatchMeshID > ) ) ]
12- [ assembly: RegisterGenericJobType ( typeof ( UnityEngine . Rendering . RegisterNewInstancesJob < UnityEngine . Rendering . BatchMaterialID > ) ) ]
13- [ assembly: RegisterGenericJobType ( typeof ( UnityEngine . Rendering . FindNonRegisteredInstancesJob < UnityEngine . Rendering . BatchMeshID > ) ) ]
14- [ assembly: RegisterGenericJobType ( typeof ( UnityEngine . Rendering . FindNonRegisteredInstancesJob < UnityEngine . Rendering . BatchMaterialID > ) ) ]
15-
1611namespace UnityEngine . Rendering
1712{
1813 internal delegate void OnCullingCompleteCallback ( JobHandle jobHandle , in BatchCullingContext cullingContext , in BatchCullingOutput cullingOutput ) ;
@@ -191,48 +186,109 @@ public void Execute(int startIndex, int count)
191186 }
192187
193188 [ BurstCompile ( DisableSafetyChecks = true , OptimizeFor = OptimizeFor . Performance ) ]
194- internal struct FindNonRegisteredInstancesJob < T > : IJobParallelForBatch where T : unmanaged
189+ internal struct FindNonRegisteredMeshesJob : IJobParallelForBatch
195190 {
196191 public const int k_BatchSize = 128 ;
197192
198193 [ ReadOnly ] public NativeArray < int > instanceIDs ;
199- [ ReadOnly ] public NativeParallelHashMap < int , T > hashMap ;
194+ [ ReadOnly ] public NativeParallelHashMap < int , BatchMeshID > hashMap ;
200195
201196 [ WriteOnly ] public NativeList < int > . ParallelWriter outInstancesWriter ;
202197
203198 public unsafe void Execute ( int startIndex , int count )
204199 {
205- int * notFoundinstanceIDs = stackalloc int [ k_BatchSize ] ;
206- int length = 0 ;
200+ int * notFoundinstanceIDsPtr = stackalloc int [ k_BatchSize ] ;
201+ var notFoundinstanceIDs = new UnsafeList < int > ( notFoundinstanceIDsPtr , k_BatchSize ) ;
202+
203+ notFoundinstanceIDs . Length = 0 ;
207204
208205 for ( int i = startIndex ; i < startIndex + count ; ++ i )
209206 {
210207 int instanceID = instanceIDs [ i ] ;
211208
212209 if ( ! hashMap . ContainsKey ( instanceID ) )
213- notFoundinstanceIDs [ length ++ ] = instanceID ;
210+ notFoundinstanceIDs . AddNoResize ( instanceID ) ;
214211 }
215212
216- outInstancesWriter . AddRangeNoResize ( notFoundinstanceIDs , length ) ;
213+ outInstancesWriter . AddRangeNoResize ( notFoundinstanceIDsPtr , notFoundinstanceIDs . Length ) ;
217214 }
218215 }
219216
220217 [ BurstCompile ( DisableSafetyChecks = true , OptimizeFor = OptimizeFor . Performance ) ]
221- internal struct RegisterNewInstancesJob < T > : IJobParallelFor where T : unmanaged
218+ internal struct FindNonRegisteredMaterialsJob : IJobParallelForBatch
222219 {
223220 public const int k_BatchSize = 128 ;
224221
225222 [ ReadOnly ] public NativeArray < int > instanceIDs ;
226- [ ReadOnly ] public NativeArray < T > batchIDs ;
223+ [ ReadOnly ] public NativeArray < GPUDrivenPackedMaterialData > packedMaterialDatas ;
224+ [ ReadOnly ] public NativeParallelHashMap < int , BatchMaterialID > hashMap ;
227225
228- [ WriteOnly ] public NativeParallelHashMap < int , T > . ParallelWriter hashMap ;
226+ [ WriteOnly ] public NativeList < int > . ParallelWriter outInstancesWriter ;
227+ [ WriteOnly ] public NativeList < GPUDrivenPackedMaterialData > . ParallelWriter outPackedMaterialDatasWriter ;
229228
230- public unsafe void Execute ( int index )
229+ public unsafe void Execute ( int startIndex , int count )
230+ {
231+ int * notFoundinstanceIDsPtr = stackalloc int [ k_BatchSize ] ;
232+ var notFoundinstanceIDs = new UnsafeList < int > ( notFoundinstanceIDsPtr , k_BatchSize ) ;
233+
234+ GPUDrivenPackedMaterialData * notFoundPackedMaterialDatasPtr = stackalloc GPUDrivenPackedMaterialData [ k_BatchSize ] ;
235+ var notFoundPackedMaterialDatas = new UnsafeList < GPUDrivenPackedMaterialData > ( notFoundPackedMaterialDatasPtr , k_BatchSize ) ;
236+
237+ notFoundinstanceIDs . Length = 0 ;
238+ notFoundPackedMaterialDatas . Length = 0 ;
239+
240+ for ( int i = startIndex ; i < startIndex + count ; ++ i )
241+ {
242+ int instanceID = instanceIDs [ i ] ;
243+
244+ if ( ! hashMap . ContainsKey ( instanceID ) )
245+ {
246+ notFoundinstanceIDs . AddNoResize ( instanceID ) ;
247+ notFoundPackedMaterialDatas . AddNoResize ( packedMaterialDatas [ i ] ) ;
248+ }
249+ }
250+
251+ outInstancesWriter . AddRangeNoResize ( notFoundinstanceIDsPtr , notFoundinstanceIDs . Length ) ;
252+ outPackedMaterialDatasWriter . AddRangeNoResize ( notFoundPackedMaterialDatasPtr , notFoundPackedMaterialDatas . Length ) ;
253+ }
254+ }
255+
256+ [ BurstCompile ( DisableSafetyChecks = true , OptimizeFor = OptimizeFor . Performance ) ]
257+ internal struct RegisterNewMeshesJob : IJobParallelFor
258+ {
259+ public const int k_BatchSize = 128 ;
260+
261+ [ ReadOnly ] public NativeArray < int > instanceIDs ;
262+ [ ReadOnly ] public NativeArray < BatchMeshID > batchIDs ;
263+
264+ [ WriteOnly ] public NativeParallelHashMap < int , BatchMeshID > . ParallelWriter hashMap ;
265+
266+ public void Execute ( int index )
231267 {
232268 hashMap . TryAdd ( instanceIDs [ index ] , batchIDs [ index ] ) ;
233269 }
234270 }
235271
272+ [ BurstCompile ( DisableSafetyChecks = true , OptimizeFor = OptimizeFor . Performance ) ]
273+ internal struct RegisterNewMaterialsJob : IJobParallelFor
274+ {
275+ public const int k_BatchSize = 128 ;
276+
277+ [ ReadOnly ] public NativeArray < int > instanceIDs ;
278+ [ ReadOnly ] public NativeArray < GPUDrivenPackedMaterialData > packedMaterialDatas ;
279+ [ ReadOnly ] public NativeArray < BatchMaterialID > batchIDs ;
280+
281+ [ WriteOnly ] public NativeParallelHashMap < int , BatchMaterialID > . ParallelWriter batchMaterialHashMap ;
282+ [ WriteOnly ] public NativeParallelHashMap < int , GPUDrivenPackedMaterialData > . ParallelWriter packedMaterialHashMap ;
283+
284+ public void Execute ( int index )
285+ {
286+ var instanceID = instanceIDs [ index ] ;
287+ batchMaterialHashMap . TryAdd ( instanceID , batchIDs [ index ] ) ;
288+ packedMaterialHashMap . TryAdd ( instanceID , packedMaterialDatas [ index ] ) ;
289+ }
290+ }
291+
236292 [ BurstCompile ( DisableSafetyChecks = true , OptimizeFor = OptimizeFor . Performance ) ]
237293 internal struct RemoveDrawInstanceIndicesJob : IJob
238294 {
@@ -468,7 +524,7 @@ public void ProcessRenderer(int i)
468524 {
469525 var materialID = rendererData . materialID [ materialIndex ] ;
470526 bool isFound = packedMaterialDataHash . TryGetValue ( materialID , out packedMaterialData ) ;
471- Assert . IsTrue ( isFound ) ;
527+ Assert . IsTrue ( isFound , "Packed material data not found." ) ;
472528 }
473529 supportsIndirect &= packedMaterialData . isIndirectSupported ;
474530
@@ -1011,41 +1067,45 @@ public void PostCullBeginCameraRendering(RenderRequestBatcherContext context)
10111067 private void RegisterBatchMeshes ( NativeArray < int > meshIDs )
10121068 {
10131069 var newMeshIDs = new NativeList < int > ( meshIDs . Length , Allocator . TempJob ) ;
1014- new FindNonRegisteredInstancesJob < BatchMeshID >
1070+ new FindNonRegisteredMeshesJob
10151071 {
10161072 instanceIDs = meshIDs ,
10171073 hashMap = m_BatchMeshHash ,
10181074 outInstancesWriter = newMeshIDs . AsParallelWriter ( )
10191075 }
1020- . ScheduleBatch ( meshIDs . Length , FindNonRegisteredInstancesJob < BatchMeshID > . k_BatchSize ) . Complete ( ) ;
1076+ . ScheduleBatch ( meshIDs . Length , FindNonRegisteredMeshesJob . k_BatchSize ) . Complete ( ) ;
10211077 var newBatchMeshIDs = new NativeArray < BatchMeshID > ( newMeshIDs . Length , Allocator . TempJob , NativeArrayOptions . UninitializedMemory ) ;
10221078 m_BRG . RegisterMeshes ( newMeshIDs . AsArray ( ) , newBatchMeshIDs ) ;
10231079
10241080 int totalMeshesNum = m_BatchMeshHash . Count ( ) + newBatchMeshIDs . Length ;
10251081 m_BatchMeshHash . Capacity = Math . Max ( m_BatchMeshHash . Capacity , Mathf . CeilToInt ( totalMeshesNum / 1023.0f ) * 1024 ) ;
10261082
1027- new RegisterNewInstancesJob < BatchMeshID >
1083+ new RegisterNewMeshesJob
10281084 {
10291085 instanceIDs = newMeshIDs . AsArray ( ) ,
10301086 batchIDs = newBatchMeshIDs ,
10311087 hashMap = m_BatchMeshHash . AsParallelWriter ( )
10321088 }
1033- . Schedule ( newMeshIDs . Length , RegisterNewInstancesJob < BatchMeshID > . k_BatchSize ) . Complete ( ) ;
1089+ . Schedule ( newMeshIDs . Length , RegisterNewMeshesJob . k_BatchSize ) . Complete ( ) ;
10341090
10351091 newMeshIDs . Dispose ( ) ;
10361092 newBatchMeshIDs . Dispose ( ) ;
10371093 }
10381094
1039- private void RegisterBatchMaterials ( in NativeArray < int > usedMaterialIDs )
1095+ private void RegisterBatchMaterials ( in NativeArray < int > usedMaterialIDs , in NativeArray < GPUDrivenPackedMaterialData > usedPackedMaterialDatas )
10401096 {
1097+ Debug . Assert ( usedMaterialIDs . Length == usedPackedMaterialDatas . Length , "Each material ID should correspond to one packed material data." ) ;
10411098 var newMaterialIDs = new NativeList < int > ( usedMaterialIDs . Length , Allocator . TempJob ) ;
1042- new FindNonRegisteredInstancesJob < BatchMaterialID >
1099+ var newPackedMaterialDatas = new NativeList < GPUDrivenPackedMaterialData > ( usedMaterialIDs . Length , Allocator . TempJob ) ;
1100+ new FindNonRegisteredMaterialsJob
10431101 {
10441102 instanceIDs = usedMaterialIDs ,
1103+ packedMaterialDatas = usedPackedMaterialDatas ,
10451104 hashMap = m_BatchMaterialHash ,
1046- outInstancesWriter = newMaterialIDs . AsParallelWriter ( )
1105+ outInstancesWriter = newMaterialIDs . AsParallelWriter ( ) ,
1106+ outPackedMaterialDatasWriter = newPackedMaterialDatas . AsParallelWriter ( )
10471107 }
1048- . ScheduleBatch ( usedMaterialIDs . Length , FindNonRegisteredInstancesJob < BatchMaterialID > . k_BatchSize ) . Complete ( ) ;
1108+ . ScheduleBatch ( usedMaterialIDs . Length , FindNonRegisteredMaterialsJob . k_BatchSize ) . Complete ( ) ;
10491109
10501110 var newBatchMaterialIDs = new NativeArray < BatchMaterialID > ( newMaterialIDs . Length , Allocator . TempJob , NativeArrayOptions . UninitializedMemory ) ;
10511111 m_BRG . RegisterMaterials ( newMaterialIDs . AsArray ( ) , newBatchMaterialIDs ) ;
@@ -1054,15 +1114,18 @@ private void RegisterBatchMaterials(in NativeArray<int> usedMaterialIDs)
10541114 m_BatchMaterialHash . Capacity = Math . Max ( m_BatchMaterialHash . Capacity , Mathf . CeilToInt ( totalMaterialsNum / 1023.0f ) * 1024 ) ;
10551115 m_PackedMaterialHash . Capacity = m_BatchMaterialHash . Capacity ;
10561116
1057- new RegisterNewInstancesJob < BatchMaterialID >
1117+ new RegisterNewMaterialsJob
10581118 {
10591119 instanceIDs = newMaterialIDs . AsArray ( ) ,
1120+ packedMaterialDatas = newPackedMaterialDatas . AsArray ( ) ,
10601121 batchIDs = newBatchMaterialIDs ,
1061- hashMap = m_BatchMaterialHash . AsParallelWriter ( )
1122+ batchMaterialHashMap = m_BatchMaterialHash . AsParallelWriter ( ) ,
1123+ packedMaterialHashMap = m_PackedMaterialHash . AsParallelWriter ( )
10621124 }
1063- . Schedule ( newMaterialIDs . Length , RegisterNewInstancesJob < BatchMaterialID > . k_BatchSize ) . Complete ( ) ;
1125+ . Schedule ( newMaterialIDs . Length , RegisterNewMaterialsJob . k_BatchSize ) . Complete ( ) ;
10641126
10651127 newMaterialIDs . Dispose ( ) ;
1128+ newPackedMaterialDatas . Dispose ( ) ;
10661129 newBatchMaterialIDs . Dispose ( ) ;
10671130 }
10681131
@@ -1078,15 +1141,13 @@ public JobHandle SchedulePackedMaterialCacheUpdate(NativeArray<int> materialIDs,
10781141
10791142 public void BuildBatch (
10801143 NativeArray < InstanceHandle > instances ,
1081- NativeArray < int > usedMaterialIDs ,
1082- NativeArray < int > usedMeshIDs ,
10831144 in GPUDrivenRendererGroupData rendererData ,
10841145 bool registerMaterialsAndMeshes )
10851146 {
10861147 if ( registerMaterialsAndMeshes )
10871148 {
1088- RegisterBatchMaterials ( usedMaterialIDs ) ;
1089- RegisterBatchMeshes ( usedMeshIDs ) ;
1149+ RegisterBatchMaterials ( rendererData . materialID , rendererData . packedMaterialData ) ;
1150+ RegisterBatchMeshes ( rendererData . meshID ) ;
10901151 }
10911152
10921153 new CreateDrawBatchesJob
0 commit comments