-
Notifications
You must be signed in to change notification settings - Fork 870
Expand file tree
/
Copy pathDecalEntityManager.cs
More file actions
547 lines (465 loc) · 20.9 KB
/
DecalEntityManager.cs
File metadata and controls
547 lines (465 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine.Assertions;
using UnityEngine.Jobs;
namespace UnityEngine.Rendering.Universal
{
internal class DecalEntityIndexer
{
public struct DecalEntityItem
{
public int chunkIndex;
public int arrayIndex;
public int version;
}
private List<DecalEntityItem> m_Entities = new List<DecalEntityItem>();
private Queue<int> m_FreeIndices = new Queue<int>();
public bool IsValid(DecalEntity decalEntity)
{
if (m_Entities.Count <= decalEntity.index)
return false;
return m_Entities[decalEntity.index].version == decalEntity.version;
}
public DecalEntity CreateDecalEntity(int arrayIndex, int chunkIndex)
{
// Reuse
if (m_FreeIndices.Count != 0)
{
int entityIndex = m_FreeIndices.Dequeue();
int newVersion = m_Entities[entityIndex].version + 1;
m_Entities[entityIndex] = new DecalEntityItem()
{
arrayIndex = arrayIndex,
chunkIndex = chunkIndex,
version = newVersion,
};
return new DecalEntity()
{
index = entityIndex,
version = newVersion,
};
}
// Create new one
{
int entityIndex = m_Entities.Count;
int version = 1;
m_Entities.Add(new DecalEntityItem()
{
arrayIndex = arrayIndex,
chunkIndex = chunkIndex,
version = version,
});
return new DecalEntity()
{
index = entityIndex,
version = version,
};
}
}
public void DestroyDecalEntity(DecalEntity decalEntity)
{
Assert.IsTrue(IsValid(decalEntity));
m_FreeIndices.Enqueue(decalEntity.index);
// Update version that everything that points to it will have outdated version
var item = m_Entities[decalEntity.index];
item.version++;
m_Entities[decalEntity.index] = item;
}
public DecalEntityItem GetItem(DecalEntity decalEntity)
{
Assert.IsTrue(IsValid(decalEntity));
return m_Entities[decalEntity.index];
}
public void UpdateIndex(DecalEntity decalEntity, int newArrayIndex)
{
Assert.IsTrue(IsValid(decalEntity));
var item = m_Entities[decalEntity.index];
item.arrayIndex = newArrayIndex;
item.version = decalEntity.version;
m_Entities[decalEntity.index] = item;
}
public void RemapChunkIndices(List<int> remaper)
{
for (int i = 0; i < m_Entities.Count; ++i)
{
int newChunkIndex = remaper[m_Entities[i].chunkIndex];
var item = m_Entities[i];
item.chunkIndex = newChunkIndex;
m_Entities[i] = item;
}
}
public void Clear()
{
m_Entities.Clear();
m_FreeIndices.Clear();
}
}
internal struct DecalEntity
{
public int index;
public int version;
}
/// <summary>
/// Contains <see cref="DecalEntity"/> and shared material.
/// </summary>
internal class DecalEntityChunk : DecalChunk
{
public Material material;
public NativeArray<DecalEntity> decalEntities;
public DecalProjector[] decalProjectors;
public TransformAccessArray transformAccessArray;
public override void Push()
{
count++;
}
public override void RemoveAtSwapBack(int entityIndex)
{
RemoveAtSwapBack(ref decalEntities, entityIndex, count);
RemoveAtSwapBack(ref decalProjectors, entityIndex, count);
transformAccessArray.RemoveAtSwapBack(entityIndex);
count--;
}
public override void SetCapacity(int newCapacity)
{
decalEntities.ResizeArray(newCapacity);
ResizeNativeArray(ref transformAccessArray, decalProjectors, newCapacity);
ArrayExtensions.ResizeArray(ref decalProjectors, newCapacity);
capacity = newCapacity;
}
public override void Dispose()
{
if (capacity == 0)
return;
decalEntities.Dispose();
transformAccessArray.Dispose();
decalProjectors = null;
count = 0;
capacity = 0;
}
}
/// <summary>
/// Manages lifetime between <see cref="DecalProjector"></see> and <see cref="DecalEntity"/>.
/// Contains all <see cref="DecalChunk"/>.
/// </summary>
internal class DecalEntityManager : IDisposable
{
public List<DecalEntityChunk> entityChunks = new List<DecalEntityChunk>();
public List<DecalCachedChunk> cachedChunks = new List<DecalCachedChunk>();
public List<DecalCulledChunk> culledChunks = new List<DecalCulledChunk>();
public List<DecalDrawCallChunk> drawCallChunks = new List<DecalDrawCallChunk>();
public int chunkCount;
private ProfilingSampler m_AddDecalSampler;
private ProfilingSampler m_ResizeChunks;
private ProfilingSampler m_SortChunks;
private DecalEntityIndexer m_DecalEntityIndexer = new DecalEntityIndexer();
private Dictionary<Material, int> m_MaterialToChunkIndex = new Dictionary<Material, int>();
private struct CombinedChunks
{
public DecalEntityChunk entityChunk;
public DecalCachedChunk cachedChunk;
public DecalCulledChunk culledChunk;
public DecalDrawCallChunk drawCallChunk;
public int previousChunkIndex;
public bool valid;
}
private List<CombinedChunks> m_CombinedChunks = new List<CombinedChunks>();
private List<int> m_CombinedChunkRemmap = new List<int>();
private Material m_ErrorMaterial;
public Material errorMaterial
{
get
{
if (m_ErrorMaterial == null)
m_ErrorMaterial = CoreUtils.CreateEngineMaterial(Shader.Find("Hidden/InternalErrorShader"));
return m_ErrorMaterial;
}
}
private Mesh m_DecalProjectorMesh;
public Mesh decalProjectorMesh
{
get
{
if (m_DecalProjectorMesh == null)
m_DecalProjectorMesh = CoreUtils.CreateCubeMesh(new Vector4(-0.5f, -0.5f, -0.5f, 1.0f), new Vector4(0.5f, 0.5f, 0.5f, 1.0f));
return m_DecalProjectorMesh;
}
}
public DecalEntityManager()
{
m_AddDecalSampler = new ProfilingSampler("DecalEntityManager.CreateDecalEntity");
m_ResizeChunks = new ProfilingSampler("DecalEntityManager.ResizeChunks");
m_SortChunks = new ProfilingSampler("DecalEntityManager.SortChunks");
}
public bool IsValid(DecalEntity decalEntity)
{
return m_DecalEntityIndexer.IsValid(decalEntity);
}
public DecalEntity CreateDecalEntity(DecalProjector decalProjector)
{
var material = decalProjector.material;
if (material == null)
material = errorMaterial;
using (new ProfilingScope(m_AddDecalSampler))
{
int chunkIndex = CreateChunkIndex(material);
int entityIndex = entityChunks[chunkIndex].count;
DecalEntity entity = m_DecalEntityIndexer.CreateDecalEntity(entityIndex, chunkIndex);
DecalEntityChunk entityChunk = entityChunks[chunkIndex];
DecalCachedChunk cachedChunk = cachedChunks[chunkIndex];
DecalCulledChunk culledChunk = culledChunks[chunkIndex];
DecalDrawCallChunk drawCallChunk = drawCallChunks[chunkIndex];
// Make sure we have space to add new entity
if (entityChunks[chunkIndex].capacity == entityChunks[chunkIndex].count)
{
using (new ProfilingScope(m_ResizeChunks))
{
int newCapacity = entityChunks[chunkIndex].capacity + entityChunks[chunkIndex].capacity;
newCapacity = math.max(8, newCapacity);
entityChunk.SetCapacity(newCapacity);
cachedChunk.SetCapacity(newCapacity);
culledChunk.SetCapacity(newCapacity);
drawCallChunk.SetCapacity(newCapacity);
}
}
entityChunk.Push();
cachedChunk.Push();
culledChunk.Push();
drawCallChunk.Push();
entityChunk.decalProjectors[entityIndex] = decalProjector;
entityChunk.decalEntities[entityIndex] = entity;
entityChunk.transformAccessArray.Add(decalProjector.transform);
UpdateDecalEntityData(entity, decalProjector);
return entity;
}
}
private int CreateChunkIndex(Material material)
{
if (!m_MaterialToChunkIndex.TryGetValue(material, out int chunkIndex))
{
var propertyBlock = new MaterialPropertyBlock();
// In order instanced and non instanced rendering to work with _NormalToWorld and _DecalLayerMaskFromDecal
// We need to make sure the array are created with maximum size
propertyBlock.SetMatrixArray("_NormalToWorld", new Matrix4x4[DecalDrawSystem.MaxBatchSize]);
propertyBlock.SetFloatArray("_DecalLayerMaskFromDecal", new float[DecalDrawSystem.MaxBatchSize]);
entityChunks.Add(new DecalEntityChunk() { material = material });
cachedChunks.Add(new DecalCachedChunk()
{
propertyBlock = propertyBlock,
});
culledChunks.Add(new DecalCulledChunk());
drawCallChunks.Add(new DecalDrawCallChunk() { subCallCounts = new NativeArray<int>(1, Allocator.Persistent) });
m_CombinedChunks.Add(new CombinedChunks());
m_CombinedChunkRemmap.Add(0);
m_MaterialToChunkIndex.Add(material, chunkCount);
return chunkCount++;
}
return chunkIndex;
}
public void UpdateAllDecalEntitiesData()
{
foreach (var entityChunk in entityChunks)
{
for (int i = 0; i < entityChunk.count; ++i)
{
var decalProjector = entityChunk.decalProjectors[i];
if (decalProjector == null)
continue;
var entity = entityChunk.decalEntities[i];
if (!IsValid(entity))
continue;
UpdateDecalEntityData(entity, decalProjector);
}
}
}
public void UpdateDecalEntityData(DecalEntity decalEntity, DecalProjector decalProjector)
{
var decalItem = m_DecalEntityIndexer.GetItem(decalEntity);
int chunkIndex = decalItem.chunkIndex;
int arrayIndex = decalItem.arrayIndex;
DecalCachedChunk cachedChunk = cachedChunks[chunkIndex];
cachedChunk.sizeOffsets[arrayIndex] = Matrix4x4.Translate(decalProjector.decalOffset) * Matrix4x4.Scale(decalProjector.decalSize);
float drawDistance = decalProjector.drawDistance;
float fadeScale = decalProjector.fadeScale;
float startAngleFade = decalProjector.startAngleFade;
float endAngleFade = decalProjector.endAngleFade;
Vector4 uvScaleBias = decalProjector.uvScaleBias;
int layerMask = decalProjector.gameObject.layer;
#if UNITY_EDITOR
// instead of removing decal altogether, set scene culling mask to game view only if !visibleInScene
ulong sceneLayerMask = decalProjector.visibleInScene ? decalProjector.gameObject.sceneCullingMask :
UnityEditor.SceneManagement.SceneCullingMasks.GameViewObjects;
#else
ulong sceneLayerMask = decalProjector.gameObject.sceneCullingMask;
#endif
float fadeFactor = decalProjector.fadeFactor;
cachedChunk.drawDistances[arrayIndex] = new Vector2(drawDistance, fadeScale);
// In the shader to remap from cosine -1 to 1 to new range 0..1 (with 0 - 0 degree and 1 - 180 degree)
// we do 1.0 - (dot() * 0.5 + 0.5) => 0.5 * (1 - dot())
// we actually square that to get smoother result => x = (0.5 - 0.5 * dot())^2
// Do a remap in the shader. 1.0 - saturate((x - start) / (end - start))
// After simplification => saturate(a + b * dot() * (dot() - 2.0))
// a = 1.0 - (0.25 - start) / (end - start), y = - 0.25 / (end - start)
if (startAngleFade == 180.0f) // angle fade is disabled
{
cachedChunk.angleFades[arrayIndex] = new Vector2(0.0f, 0.0f);
}
else
{
float angleStart = startAngleFade / 180.0f;
float angleEnd = endAngleFade / 180.0f;
var range = Mathf.Max(0.0001f, angleEnd - angleStart);
cachedChunk.angleFades[arrayIndex] = new Vector2(1.0f - (0.25f - angleStart) / range, -0.25f / range);
}
cachedChunk.uvScaleBias[arrayIndex] = uvScaleBias;
cachedChunk.layerMasks[arrayIndex] = layerMask;
cachedChunk.sceneLayerMasks[arrayIndex] = sceneLayerMask;
cachedChunk.fadeFactors[arrayIndex] = fadeFactor;
cachedChunk.scaleModes[arrayIndex] = decalProjector.scaleMode;
cachedChunk.renderingLayerMasks[arrayIndex] = RenderingLayerUtils.ToValidRenderingLayers(decalProjector.renderingLayerMask);
cachedChunk.positions[arrayIndex] = decalProjector.transform.position;
cachedChunk.rotation[arrayIndex] = decalProjector.transform.rotation;
cachedChunk.scales[arrayIndex] = decalProjector.transform.lossyScale;
cachedChunk.dirty[arrayIndex] = true;
}
public void DestroyDecalEntity(DecalEntity decalEntity)
{
if (!m_DecalEntityIndexer.IsValid(decalEntity))
return;
var decalItem = m_DecalEntityIndexer.GetItem(decalEntity);
m_DecalEntityIndexer.DestroyDecalEntity(decalEntity);
int chunkIndex = decalItem.chunkIndex;
int arrayIndex = decalItem.arrayIndex;
DecalEntityChunk entityChunk = entityChunks[chunkIndex];
DecalCachedChunk cachedChunk = cachedChunks[chunkIndex];
DecalCulledChunk culledChunk = culledChunks[chunkIndex];
DecalDrawCallChunk drawCallChunk = drawCallChunks[chunkIndex];
int lastArrayIndex = entityChunk.count - 1;
if (arrayIndex != lastArrayIndex)
m_DecalEntityIndexer.UpdateIndex(entityChunk.decalEntities[lastArrayIndex], arrayIndex);
entityChunk.RemoveAtSwapBack(arrayIndex);
cachedChunk.RemoveAtSwapBack(arrayIndex);
culledChunk.RemoveAtSwapBack(arrayIndex);
drawCallChunk.RemoveAtSwapBack(arrayIndex);
}
public void Update()
{
using (new ProfilingScope(m_SortChunks))
{
for (int i = 0; i < chunkCount; ++i)
{
if (entityChunks[i].material == null)
entityChunks[i].material = errorMaterial;
}
// Combine chunks into single array
for (int i = 0; i < chunkCount; ++i)
{
m_CombinedChunks[i] = new CombinedChunks()
{
entityChunk = entityChunks[i],
cachedChunk = cachedChunks[i],
culledChunk = culledChunks[i],
drawCallChunk = drawCallChunks[i],
previousChunkIndex = i,
valid = entityChunks[i].count != 0,
};
}
// Sort
m_CombinedChunks.Sort((a, b) =>
{
if (a.valid && !b.valid)
return -1;
if (!a.valid && b.valid)
return 1;
if (a.cachedChunk.drawOrder < b.cachedChunk.drawOrder)
return -1;
if (a.cachedChunk.drawOrder > b.cachedChunk.drawOrder)
return 1;
return a.entityChunk.material.GetHashCode().CompareTo(b.entityChunk.material.GetHashCode());
});
// Early out if nothing changed
bool dirty = false;
for (int i = 0; i < chunkCount; ++i)
{
if (m_CombinedChunks[i].previousChunkIndex != i || !m_CombinedChunks[i].valid)
{
dirty = true;
break;
}
}
if (!dirty)
return;
// Update chunks
int count = 0;
m_MaterialToChunkIndex.Clear();
for (int i = 0; i < chunkCount; ++i)
{
var combinedChunk = m_CombinedChunks[i];
// Destroy invalid chunk for cleanup
if (!m_CombinedChunks[i].valid)
{
combinedChunk.entityChunk.currentJobHandle.Complete();
combinedChunk.cachedChunk.currentJobHandle.Complete();
combinedChunk.culledChunk.currentJobHandle.Complete();
combinedChunk.drawCallChunk.currentJobHandle.Complete();
combinedChunk.entityChunk.Dispose();
combinedChunk.cachedChunk.Dispose();
combinedChunk.culledChunk.Dispose();
combinedChunk.drawCallChunk.Dispose();
continue;
}
entityChunks[i] = combinedChunk.entityChunk;
cachedChunks[i] = combinedChunk.cachedChunk;
culledChunks[i] = combinedChunk.culledChunk;
drawCallChunks[i] = combinedChunk.drawCallChunk;
if (!m_MaterialToChunkIndex.ContainsKey(entityChunks[i].material))
m_MaterialToChunkIndex.Add(entityChunks[i].material, i);
m_CombinedChunkRemmap[combinedChunk.previousChunkIndex] = i;
count++;
}
// In case some chunks where destroyed resize the arrays
if (chunkCount > count)
{
entityChunks.RemoveRange(count, chunkCount - count);
cachedChunks.RemoveRange(count, chunkCount - count);
culledChunks.RemoveRange(count, chunkCount - count);
drawCallChunks.RemoveRange(count, chunkCount - count);
m_CombinedChunks.RemoveRange(count, chunkCount - count);
chunkCount = count;
}
// Remap entities chunk index with new sorted ones
m_DecalEntityIndexer.RemapChunkIndices(m_CombinedChunkRemmap);
}
}
public void Dispose()
{
CoreUtils.Destroy(m_ErrorMaterial);
CoreUtils.Destroy(m_DecalProjectorMesh);
foreach (var entityChunk in entityChunks)
entityChunk.currentJobHandle.Complete();
foreach (var cachedChunk in cachedChunks)
cachedChunk.currentJobHandle.Complete();
foreach (var culledChunk in culledChunks)
culledChunk.currentJobHandle.Complete();
foreach (var drawCallChunk in drawCallChunks)
drawCallChunk.currentJobHandle.Complete();
foreach (var entityChunk in entityChunks)
entityChunk.Dispose();
foreach (var cachedChunk in cachedChunks)
cachedChunk.Dispose();
foreach (var culledChunk in culledChunks)
culledChunk.Dispose();
foreach (var drawCallChunk in drawCallChunks)
drawCallChunk.Dispose();
m_DecalEntityIndexer.Clear();
m_MaterialToChunkIndex.Clear();
entityChunks.Clear();
cachedChunks.Clear();
culledChunks.Clear();
drawCallChunks.Clear();
m_CombinedChunks.Clear();
chunkCount = 0;
}
}
}