Skip to content

Commit dc3be16

Browse files
alekseiunityEvergreen
authored andcommitted
Improved GPU Resident Drawer Burst Jobs Performance.
We have to disable native checks by default for the best burst jobs performance in GPU Resident Drawer. Additionally we never put NoAlias attribute along with native containers that required it which additionally allows compiler to improve performance. Performance Comparison: **Before:** ![CurrentCull](https://media.github.cds.internal.unity3d.com/user/1206/files/41e67802-3dc5-4a45-b877-c13ca9447422) ![CurrentInstanceData](https://media.github.cds.internal.unity3d.com/user/1206/files/39dbc9f4-898c-42fb-ac02-1c33e69ba240) **After:** ![NewCull](https://media.github.cds.internal.unity3d.com/user/1206/files/04dd8982-bcd5-4ce7-85be-b30f2fc7133c) ![NewInstanceData](https://media.github.cds.internal.unity3d.com/user/1206/files/9d9253ea-5a59-4996-b688-b465e83d89d7) 50K Instances UpdateRendererData: Before: 24.55ms After: **19.34ms** 50K Instances Culling: Before: 2.34ms + 0.42ms = 2.76ms After: 1.31ms + 0.32ms = **1.63ms**
1 parent b7ee57c commit dc3be16

File tree

7 files changed

+96
-102
lines changed

7 files changed

+96
-102
lines changed

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/InstanceCuller.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public int visibilityConfigCount
132132
}
133133
}
134134

135-
[BurstCompile]
135+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
136136
internal struct CullingJob : IJobParallelFor
137137
{
138138
public const int k_BatchSize = 32;
@@ -173,7 +173,7 @@ enum CrossFadeType
173173

174174
[ReadOnly] public CPUInstanceData.ReadOnly instanceData;
175175
[ReadOnly] public CPUSharedInstanceData.ReadOnly sharedInstanceData;
176-
[NativeDisableContainerSafetyRestriction] [ReadOnly] public NativeList<LODGroupCullingData> lodGroupCullingData;
176+
[NativeDisableContainerSafetyRestriction, NoAlias] [ReadOnly] public NativeList<LODGroupCullingData> lodGroupCullingData;
177177
[NativeDisableUnsafePtrRestriction] [ReadOnly] public IntPtr occlusionBuffer;
178178

179179
[NativeDisableParallelForRestriction][WriteOnly] public NativeArray<byte> rendererVisibilityMasks;
@@ -399,7 +399,7 @@ public void Execute(int instanceIndex)
399399
}
400400
}
401401

402-
[BurstCompile]
402+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
403403
internal unsafe struct AllocateBinsPerBatch : IJobParallelFor
404404
{
405405
[ReadOnly] public BinningConfig binningConfig;
@@ -409,15 +409,15 @@ internal unsafe struct AllocateBinsPerBatch : IJobParallelFor
409409
[ReadOnly] public CPUInstanceData.ReadOnly instanceData;
410410
[ReadOnly] public NativeArray<byte> rendererVisibilityMasks;
411411

412-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<int> batchBinAllocOffsets;
413-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<int> batchBinCounts;
412+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<int> batchBinAllocOffsets;
413+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<int> batchBinCounts;
414414

415-
[NativeDisableContainerSafetyRestriction] [DeallocateOnJobCompletion] public NativeArray<int> binAllocCounter;
416-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<short> binConfigIndices;
417-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<int> binVisibleInstanceCounts;
415+
[NativeDisableContainerSafetyRestriction, NoAlias] [DeallocateOnJobCompletion] public NativeArray<int> binAllocCounter;
416+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<short> binConfigIndices;
417+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<int> binVisibleInstanceCounts;
418418

419419
[ReadOnly] public int debugCounterIndexBase;
420-
[NativeDisableContainerSafetyRestriction] public NativeArray<int> splitDebugCounters;
420+
[NativeDisableContainerSafetyRestriction, NoAlias] public NativeArray<int> splitDebugCounters;
421421

422422
bool IsInstanceFlipped(int rendererIndex)
423423
{
@@ -535,7 +535,7 @@ unsafe public void Execute(int batchIndex)
535535
}
536536
}
537537

538-
[BurstCompile]
538+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
539539
internal unsafe struct PrefixSumDrawsAndInstances : IJob
540540
{
541541
[ReadOnly] public NativeList<DrawRange> drawRanges;
@@ -545,14 +545,14 @@ internal unsafe struct PrefixSumDrawsAndInstances : IJob
545545
[ReadOnly] public NativeArray<int> batchBinCounts;
546546
[ReadOnly] public NativeArray<int> binVisibleInstanceCounts;
547547

548-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<int> batchDrawCommandOffsets;
549-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<int> binVisibleInstanceOffsets;
548+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<int> batchDrawCommandOffsets;
549+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<int> binVisibleInstanceOffsets;
550550

551551
[NativeDisableUnsafePtrRestriction] public NativeArray<BatchCullingOutputDrawCommands> cullingOutput;
552552

553553
[ReadOnly] public IndirectBufferLimits indirectBufferLimits;
554-
[NativeDisableContainerSafetyRestriction] public NativeArray<IndirectBufferAllocInfo> indirectBufferAllocInfo;
555-
[NativeDisableContainerSafetyRestriction] public NativeArray<int> indirectAllocationCounters;
554+
[NativeDisableContainerSafetyRestriction, NoAlias] public NativeArray<IndirectBufferAllocInfo> indirectBufferAllocInfo;
555+
[NativeDisableContainerSafetyRestriction, NoAlias] public NativeArray<int> indirectAllocationCounters;
556556

557557
unsafe public void Execute()
558558
{
@@ -703,7 +703,7 @@ unsafe public void Execute()
703703
}
704704
}
705705

706-
[BurstCompile]
706+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
707707
internal unsafe struct DrawCommandOutputPerBatch : IJobParallelFor
708708
{
709709
[ReadOnly] public BinningConfig binningConfig;
@@ -731,9 +731,9 @@ internal unsafe struct DrawCommandOutputPerBatch : IJobParallelFor
731731
[ReadOnly] public IndirectBufferLimits indirectBufferLimits;
732732
[ReadOnly] public GraphicsBufferHandle visibleInstancesBufferHandle;
733733
[ReadOnly] public GraphicsBufferHandle indirectArgsBufferHandle;
734-
[NativeDisableContainerSafetyRestriction] public NativeArray<IndirectBufferAllocInfo> indirectBufferAllocInfo;
735-
[NativeDisableContainerSafetyRestriction] public NativeArray<IndirectDrawInfo> indirectDrawInfoGlobalArray;
736-
[NativeDisableContainerSafetyRestriction] public NativeArray<IndirectInstanceInfo> indirectInstanceInfoGlobalArray;
734+
[NativeDisableContainerSafetyRestriction, NoAlias] public NativeArray<IndirectBufferAllocInfo> indirectBufferAllocInfo;
735+
[NativeDisableContainerSafetyRestriction, NoAlias] public NativeArray<IndirectDrawInfo> indirectDrawInfoGlobalArray;
736+
[NativeDisableContainerSafetyRestriction, NoAlias] public NativeArray<IndirectInstanceInfo> indirectInstanceInfoGlobalArray;
737737

738738
unsafe int EncodeGPUInstanceIndexAndCrossFade(int rendererIndex, bool negateCrossFade)
739739
{
@@ -999,14 +999,14 @@ unsafe public void Execute(int batchIndex)
999999
}
10001000
}
10011001

1002-
[BurstCompile]
1002+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
10031003
internal unsafe struct CompactVisibilityMasksJob : IJobParallelForBatch
10041004
{
10051005
public const int k_BatchSize = 64;
10061006

10071007
[ReadOnly] public NativeArray<byte> rendererVisibilityMasks;
10081008

1009-
[NativeDisableContainerSafetyRestriction] public ParallelBitArray compactedVisibilityMasks;
1009+
[NativeDisableContainerSafetyRestriction, NoAlias] public ParallelBitArray compactedVisibilityMasks;
10101010

10111011
unsafe public void Execute(int startIndex, int count)
10121012
{
@@ -1032,7 +1032,7 @@ internal enum FilteringJobMode
10321032
Picking
10331033
}
10341034

1035-
[BurstCompile]
1035+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
10361036
internal unsafe struct DrawCommandOutputFiltering : IJob
10371037
{
10381038
[ReadOnly] public NativeParallelHashMap<uint, BatchID> batchIDs;
@@ -1183,7 +1183,7 @@ public void Execute()
11831183
}
11841184
}
11851185

1186-
[BurstCompile]
1186+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
11871187
internal struct CullSceneViewHiddenRenderersJob : IJobParallelFor
11881188
{
11891189
public const int k_BatchSize = 128;
@@ -1462,7 +1462,6 @@ public void MoveToDebugStatsAndClear(DebugRendererBatcherStats debugStats)
14621462
}
14631463
}
14641464

1465-
[BurstCompile]
14661465
internal struct InstanceCuller : IDisposable
14671466
{
14681467
//@ Move this in CPUInstanceData.

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/InstanceCullingBatcher.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal struct MeshProceduralInfo
4949
public uint indexCount;
5050
}
5151

52-
[BurstCompile(DisableSafetyChecks = true)]
52+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
5353
internal struct PrefixSumDrawInstancesJob : IJob
5454
{
5555
[ReadOnly] public NativeParallelHashMap<RangeKey, int> rangeHash;
@@ -105,18 +105,18 @@ public void Execute()
105105
}
106106
}
107107

108-
[BurstCompile(DisableSafetyChecks = true)]
108+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
109109
internal unsafe struct BuildDrawListsJob : IJobParallelFor
110110
{
111111
public const int k_BatchSize = 128;
112112
public const int k_IntsPerCacheLine = JobsUtility.CacheLineSize / sizeof(int);
113113

114114
[ReadOnly] public NativeParallelHashMap<DrawKey, int> batchHash;
115-
[NativeDisableContainerSafetyRestriction] [ReadOnly] public NativeList<DrawInstance> drawInstances;
116-
[NativeDisableContainerSafetyRestriction] [ReadOnly] public NativeList<DrawBatch> drawBatches;
115+
[NativeDisableContainerSafetyRestriction, NoAlias] [ReadOnly] public NativeList<DrawInstance> drawInstances;
116+
[NativeDisableContainerSafetyRestriction, NoAlias] [ReadOnly] public NativeList<DrawBatch> drawBatches;
117117

118-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<int> internalDrawIndex;
119-
[NativeDisableContainerSafetyRestriction] [WriteOnly] public NativeArray<int> drawInstanceIndices;
118+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<int> internalDrawIndex;
119+
[NativeDisableContainerSafetyRestriction, NoAlias] [WriteOnly] public NativeArray<int> drawInstanceIndices;
120120

121121
private unsafe static int IncrementCounter(int* counter)
122122
{
@@ -136,13 +136,13 @@ public void Execute(int index)
136136
}
137137
}
138138

139-
[BurstCompile(DisableSafetyChecks = true)]
139+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
140140
internal unsafe struct FindDrawInstancesJob : IJobParallelForBatch
141141
{
142142
public const int k_BatchSize = 128;
143143

144144
[ReadOnly] public NativeArray<InstanceHandle> instancesSorted;
145-
[NativeDisableContainerSafetyRestriction] [ReadOnly] public NativeList<DrawInstance> drawInstances;
145+
[NativeDisableContainerSafetyRestriction, NoAlias] [ReadOnly] public NativeList<DrawInstance> drawInstances;
146146

147147
[WriteOnly] public NativeList<int>.ParallelWriter outDrawInstanceIndicesWriter;
148148

@@ -163,13 +163,13 @@ public void Execute(int startIndex, int count)
163163
}
164164
}
165165

166-
[BurstCompile(DisableSafetyChecks = true)]
166+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
167167
internal unsafe struct FindMaterialDrawInstancesJob : IJobParallelForBatch
168168
{
169169
public const int k_BatchSize = 128;
170170

171171
[ReadOnly] public NativeArray<uint> materialsSorted;
172-
[NativeDisableContainerSafetyRestriction] [ReadOnly] public NativeList<DrawInstance> drawInstances;
172+
[NativeDisableContainerSafetyRestriction, NoAlias] [ReadOnly] public NativeList<DrawInstance> drawInstances;
173173

174174
[WriteOnly] public NativeList<int>.ParallelWriter outDrawInstanceIndicesWriter;
175175

@@ -190,7 +190,7 @@ public void Execute(int startIndex, int count)
190190
}
191191
}
192192

193-
[BurstCompile(DisableSafetyChecks = true)]
193+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
194194
internal struct FindNonRegisteredInstancesJob<T> : IJobParallelForBatch where T : unmanaged
195195
{
196196
public const int k_BatchSize = 128;
@@ -217,7 +217,7 @@ public unsafe void Execute(int startIndex, int count)
217217
}
218218
}
219219

220-
[BurstCompile(DisableSafetyChecks = true)]
220+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
221221
internal struct RegisterNewInstancesJob<T> : IJobParallelFor where T : unmanaged
222222
{
223223
public const int k_BatchSize = 128;
@@ -233,18 +233,17 @@ public unsafe void Execute(int index)
233233
}
234234
}
235235

236-
[BurstCompile(DisableSafetyChecks = true)]
236+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
237237
internal struct RemoveDrawInstanceIndicesJob : IJob
238238
{
239-
[NativeDisableContainerSafetyRestriction] [ReadOnly] public NativeArray<int> drawInstanceIndices;
239+
[NativeDisableContainerSafetyRestriction, NoAlias] [ReadOnly] public NativeArray<int> drawInstanceIndices;
240240

241241
public NativeList<DrawInstance> drawInstances;
242242
public NativeParallelHashMap<RangeKey, int> rangeHash;
243243
public NativeParallelHashMap<DrawKey, int> batchHash;
244244
public NativeList<DrawRange> drawRanges;
245245
public NativeList<DrawBatch> drawBatches;
246246

247-
[BurstCompile]
248247
public void RemoveDrawRange(in RangeKey key)
249248
{
250249
int drawRangeIndex = rangeHash[key];
@@ -256,7 +255,6 @@ public void RemoveDrawRange(in RangeKey key)
256255
drawRanges.RemoveAtSwapBack(drawRangeIndex);
257256
}
258257

259-
[BurstCompile]
260258
public void RemoveDrawBatch(in DrawKey key)
261259
{
262260
int drawBatchIndex = batchHash[key];
@@ -303,7 +301,7 @@ public unsafe void Execute()
303301
}
304302
}
305303

306-
[BurstCompile(DisableSafetyChecks = true)]
304+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
307305
internal struct CreateDrawBatchesJob : IJob
308306
{
309307
[ReadOnly] public bool implicitInstanceIndices;
@@ -319,7 +317,6 @@ internal struct CreateDrawBatchesJob : IJob
319317

320318
[WriteOnly] public NativeList<DrawInstance> drawInstances;
321319

322-
[BurstCompile]
323320
private ref DrawRange EditDrawRange(in RangeKey key)
324321
{
325322
int drawRangeIndex;
@@ -338,7 +335,6 @@ private ref DrawRange EditDrawRange(in RangeKey key)
338335
return ref data;
339336
}
340337

341-
[BurstCompile]
342338
private ref DrawBatch EditDrawBatch(in DrawKey key, in SubMeshDescriptor subMeshDescriptor)
343339
{
344340
var procInfo = new MeshProceduralInfo();
@@ -531,7 +527,6 @@ public void Execute()
531527
}
532528
}
533529

534-
[BurstCompile]
535530
internal class CPUDrawInstanceData
536531
{
537532
public NativeList<DrawInstance> drawInstances => m_DrawInstances;

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/InstanceData/GPUInstanceDataBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public void CPUInstanceArrayToGPUInstanceArray(NativeArray<InstanceHandle> insta
204204
}
205205
}
206206

207-
[BurstCompile]
207+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
208208
struct ConvertCPUInstancesToGPUInstancesJob : IJobParallelFor
209209
{
210210
public const int k_BatchSize = 512;

Packages/com.unity.render-pipelines.core/Runtime/GPUDriven/InstanceData/GPUInstanceDataUploader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public void Dispose()
425425
m_DummyArray.Dispose();
426426
}
427427

428-
[BurstCompile(DisableSafetyChecks = true)]
428+
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
429429
internal struct WriteInstanceDataParameterJob : IJobParallelFor
430430
{
431431
public const int k_BatchSize = 512;
@@ -436,9 +436,9 @@ internal struct WriteInstanceDataParameterJob : IJobParallelFor
436436
[ReadOnly] public int uintPerInstance;
437437
[ReadOnly] public NativeArray<int> componentDataIndex;
438438
[ReadOnly] public NativeArray<int> gatherIndices;
439-
[NativeDisableContainerSafetyRestriction][ReadOnly] public NativeArray<uint> instanceData;
439+
[NativeDisableContainerSafetyRestriction, NoAlias][ReadOnly] public NativeArray<uint> instanceData;
440440

441-
[NativeDisableContainerSafetyRestriction][WriteOnly] public NativeArray<uint> tmpDataBuffer;
441+
[NativeDisableContainerSafetyRestriction, NoAlias][WriteOnly] public NativeArray<uint> tmpDataBuffer;
442442

443443
public unsafe void Execute(int index)
444444
{

0 commit comments

Comments
 (0)