-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathNativeRenderPass.cs
More file actions
675 lines (549 loc) · 33.9 KB
/
NativeRenderPass.cs
File metadata and controls
675 lines (549 loc) · 33.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Collections;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Rendering.Universal
{
public partial class ScriptableRenderer
{
private const int kRenderPassMapSize = 10;
private const int kRenderPassMaxCount = 20;
// used to keep track of the index of the last pass when we called BeginSubpass
private int m_LastBeginSubpassPassIndex = 0;
private Dictionary<Hash128, int[]> m_MergeableRenderPassesMap = new Dictionary<Hash128, int[]>(kRenderPassMapSize);
// static array storing all the mergeableRenderPassesMap arrays. This is used to remove any GC allocs during the frame which would have been introduced by using a dynamic array to store the mergeablePasses per RenderPass
private int[][] m_MergeableRenderPassesMapArrays;
private Hash128[] m_PassIndexToPassHash = new Hash128[kRenderPassMaxCount];
private Dictionary<Hash128, int> m_RenderPassesAttachmentCount = new Dictionary<Hash128, int>(kRenderPassMapSize);
AttachmentDescriptor[] m_ActiveColorAttachmentDescriptors = new AttachmentDescriptor[]
{
RenderingUtils.emptyAttachment, RenderingUtils.emptyAttachment, RenderingUtils.emptyAttachment,
RenderingUtils.emptyAttachment, RenderingUtils.emptyAttachment, RenderingUtils.emptyAttachment,
RenderingUtils.emptyAttachment, RenderingUtils.emptyAttachment
};
AttachmentDescriptor m_ActiveDepthAttachmentDescriptor;
bool[] m_IsActiveColorAttachmentTransient = new bool[]
{
false, false, false, false, false, false, false, false
};
internal RenderBufferStoreAction[] m_FinalColorStoreAction = new RenderBufferStoreAction[]
{
RenderBufferStoreAction.Store, RenderBufferStoreAction.Store, RenderBufferStoreAction.Store, RenderBufferStoreAction.Store,
RenderBufferStoreAction.Store, RenderBufferStoreAction.Store, RenderBufferStoreAction.Store, RenderBufferStoreAction.Store
};
internal RenderBufferStoreAction m_FinalDepthStoreAction = RenderBufferStoreAction.Store;
private static partial class Profiling
{
public static readonly ProfilingSampler setMRTAttachmentsList = new ProfilingSampler($"NativeRenderPass {nameof(SetNativeRenderPassMRTAttachmentList)}");
public static readonly ProfilingSampler setAttachmentList = new ProfilingSampler($"NativeRenderPass {nameof(SetNativeRenderPassAttachmentList)}");
public static readonly ProfilingSampler execute = new ProfilingSampler($"NativeRenderPass {nameof(ExecuteNativeRenderPass)}");
public static readonly ProfilingSampler setupFrameData = new ProfilingSampler($"NativeRenderPass {nameof(SetupNativeRenderPassFrameData)}");
}
internal struct RenderPassDescriptor
{
internal int w, h, samples, depthID;
internal RenderPassDescriptor(int width, int height, int sampleCount, int rtID)
{
w = width;
h = height;
samples = sampleCount;
depthID = rtID;
}
}
internal void ResetNativeRenderPassFrameData()
{
if (m_MergeableRenderPassesMapArrays == null)
m_MergeableRenderPassesMapArrays = new int[kRenderPassMapSize][];
for (int i = 0; i < kRenderPassMapSize; ++i)
{
if (m_MergeableRenderPassesMapArrays[i] == null)
m_MergeableRenderPassesMapArrays[i] = new int[kRenderPassMaxCount];
for (int j = 0; j < kRenderPassMaxCount; ++j)
{
m_MergeableRenderPassesMapArrays[i][j] = -1;
}
}
}
internal void SetupNativeRenderPassFrameData(ref CameraData cameraData, bool isRenderPassEnabled)
{
//TODO: edge cases to detect that should affect possible passes to merge
// - total number of color attachment > 8
// Go through all the passes and mark the final one as last pass
using (new ProfilingScope(null, Profiling.setupFrameData))
{
int lastPassIndex = m_ActiveRenderPassQueue.Count - 1;
// Make sure the list is already sorted!
m_MergeableRenderPassesMap.Clear();
m_RenderPassesAttachmentCount.Clear();
uint currentHashIndex = 0;
// reset all the passes last pass flag
for (int i = 0; i < m_ActiveRenderPassQueue.Count; ++i)
{
var renderPass = m_ActiveRenderPassQueue[i];
renderPass.renderPassQueueIndex = i;
bool RPEnabled = IsRenderPassEnabled(renderPass);
if (!RPEnabled)
continue;
var rpDesc = InitializeRenderPassDescriptor(ref cameraData, renderPass);
Hash128 hash = CreateRenderPassHash(rpDesc, currentHashIndex);
m_PassIndexToPassHash[i] = hash;
if (!m_MergeableRenderPassesMap.ContainsKey(hash))
{
m_MergeableRenderPassesMap.Add(hash, m_MergeableRenderPassesMapArrays[m_MergeableRenderPassesMap.Count]);
m_RenderPassesAttachmentCount.Add(hash, 0);
}
else if (m_MergeableRenderPassesMap[hash][GetValidPassIndexCount(m_MergeableRenderPassesMap[hash]) - 1] != (i - 1))
{
// if the passes are not sequential we want to split the current mergeable passes list. So we increment the hashIndex and update the hash
currentHashIndex++;
hash = CreateRenderPassHash(rpDesc, currentHashIndex);
m_PassIndexToPassHash[i] = hash;
m_MergeableRenderPassesMap.Add(hash, m_MergeableRenderPassesMapArrays[m_MergeableRenderPassesMap.Count]);
m_RenderPassesAttachmentCount.Add(hash, 0);
}
m_MergeableRenderPassesMap[hash][GetValidPassIndexCount(m_MergeableRenderPassesMap[hash])] = i;
}
for (int i = 0; i < m_ActiveRenderPassQueue.Count; ++i)
{
m_ActiveRenderPassQueue[i].m_ColorAttachmentIndices = new NativeArray<int>(8, Allocator.Temp);
m_ActiveRenderPassQueue[i].m_InputAttachmentIndices = new NativeArray<int>(8, Allocator.Temp);
}
}
}
internal void UpdateFinalStoreActions(int[] currentMergeablePasses, ref CameraData cameraData)
{
for (int i = 0; i < m_FinalColorStoreAction.Length; ++i)
m_FinalColorStoreAction[i] = RenderBufferStoreAction.Store;
m_FinalDepthStoreAction = RenderBufferStoreAction.Store;
foreach (var passIdx in currentMergeablePasses)
{
if (!m_UseOptimizedStoreActions)
break;
if (passIdx == -1)
break;
ScriptableRenderPass pass = m_ActiveRenderPassQueue[passIdx];
var samples = pass.overrideCameraTarget ? GetFirstAllocatedRTHandle(pass).rt.descriptor.msaaSamples :
(cameraData.targetTexture != null ? cameraData.targetTexture.descriptor.msaaSamples : cameraData.cameraTargetDescriptor.msaaSamples);
// only override existing non destructive actions
for (int i = 0; i < m_FinalColorStoreAction.Length; ++i)
{
if (m_FinalColorStoreAction[i] == RenderBufferStoreAction.Store || m_FinalColorStoreAction[i] == RenderBufferStoreAction.StoreAndResolve || pass.overriddenColorStoreActions[i])
m_FinalColorStoreAction[i] = pass.colorStoreActions[i];
if (samples > 1)
{
if (m_FinalColorStoreAction[i] == RenderBufferStoreAction.Store)
m_FinalColorStoreAction[i] = RenderBufferStoreAction.StoreAndResolve;
else if (m_FinalColorStoreAction[i] == RenderBufferStoreAction.DontCare)
m_FinalColorStoreAction[i] = RenderBufferStoreAction.Resolve;
}
}
// only override existing store
if (m_FinalDepthStoreAction == RenderBufferStoreAction.Store || (m_FinalDepthStoreAction == RenderBufferStoreAction.StoreAndResolve && pass.depthStoreAction == RenderBufferStoreAction.Resolve) || pass.overriddenDepthStoreAction)
m_FinalDepthStoreAction = pass.depthStoreAction;
}
}
internal void SetNativeRenderPassMRTAttachmentList(ScriptableRenderPass renderPass, ref CameraData cameraData, bool needCustomCameraColorClear, ClearFlag cameraClearFlag)
{
using (new ProfilingScope(null, Profiling.setMRTAttachmentsList))
{
int currentPassIndex = renderPass.renderPassQueueIndex;
Hash128 currentPassHash = m_PassIndexToPassHash[currentPassIndex];
int[] currentMergeablePasses = m_MergeableRenderPassesMap[currentPassHash];
// Not the first pass
if (currentMergeablePasses.First() != currentPassIndex)
return;
m_RenderPassesAttachmentCount[currentPassHash] = 0;
UpdateFinalStoreActions(currentMergeablePasses, ref cameraData);
int currentAttachmentIdx = 0;
bool hasInput = false;
foreach (var passIdx in currentMergeablePasses)
{
if (passIdx == -1)
break;
ScriptableRenderPass pass = m_ActiveRenderPassQueue[passIdx];
for (int i = 0; i < pass.m_ColorAttachmentIndices.Length; ++i)
pass.m_ColorAttachmentIndices[i] = -1;
for (int i = 0; i < pass.m_InputAttachmentIndices.Length; ++i)
pass.m_InputAttachmentIndices[i] = -1;
uint validColorBuffersCount = RenderingUtils.GetValidColorBufferCount(pass.colorAttachmentHandles);
for (int i = 0; i < validColorBuffersCount; ++i)
{
AttachmentDescriptor currentAttachmentDescriptor =
new AttachmentDescriptor(pass.renderTargetFormat[i] != GraphicsFormat.None ? pass.renderTargetFormat[i] : UniversalRenderPipeline.MakeRenderTextureGraphicsFormat(cameraData.isHdrEnabled, cameraData.hdrColorBufferPrecision, Graphics.preserveFramebufferAlpha));
var colorHandle = pass.overrideCameraTarget ? pass.colorAttachmentHandles[i] : m_CameraColorTarget.handle;
int existingAttachmentIndex = FindAttachmentDescriptorIndexInList(colorHandle.nameID, m_ActiveColorAttachmentDescriptors);
if (m_UseOptimizedStoreActions)
currentAttachmentDescriptor.storeAction = m_FinalColorStoreAction[i];
if (existingAttachmentIndex == -1)
{
// add a new attachment
m_ActiveColorAttachmentDescriptors[currentAttachmentIdx] = currentAttachmentDescriptor;
bool passHasClearColor = (pass.clearFlag & ClearFlag.Color) != 0;
m_ActiveColorAttachmentDescriptors[currentAttachmentIdx].ConfigureTarget(colorHandle.nameID, !passHasClearColor, true);
if (pass.colorAttachmentHandles[i] == m_CameraColorTarget.handle && needCustomCameraColorClear && (cameraClearFlag & ClearFlag.Color) != 0)
m_ActiveColorAttachmentDescriptors[currentAttachmentIdx].ConfigureClear(cameraData.backgroundColor, 1.0f, 0);
else if (passHasClearColor)
m_ActiveColorAttachmentDescriptors[currentAttachmentIdx].ConfigureClear(CoreUtils.ConvertSRGBToActiveColorSpace(pass.clearColor), 1.0f, 0);
pass.m_ColorAttachmentIndices[i] = currentAttachmentIdx;
currentAttachmentIdx++;
m_RenderPassesAttachmentCount[currentPassHash]++;
}
else
{
// attachment was already present
pass.m_ColorAttachmentIndices[i] = existingAttachmentIndex;
}
}
if (PassHasInputAttachments(pass))
{
hasInput = true;
SetupInputAttachmentIndices(pass);
}
// TODO: this is redundant and is being setup for each attachment. Needs to be done only once per mergeable pass list (we need to make sure mergeable passes use the same depth!)
m_ActiveDepthAttachmentDescriptor = new AttachmentDescriptor(SystemInfo.GetGraphicsFormat(DefaultFormat.DepthStencil));
bool passHasClearDepth = (cameraClearFlag & ClearFlag.DepthStencil) != 0;
m_ActiveDepthAttachmentDescriptor.ConfigureTarget(pass.overrideCameraTarget ? pass.depthAttachmentHandle.nameID : m_CameraDepthTarget.nameID, !passHasClearDepth, true);
if (passHasClearDepth)
m_ActiveDepthAttachmentDescriptor.ConfigureClear(Color.black, 1.0f, 0);
if (m_UseOptimizedStoreActions)
m_ActiveDepthAttachmentDescriptor.storeAction = m_FinalDepthStoreAction;
}
if (hasInput)
SetupTransientInputAttachments(m_RenderPassesAttachmentCount[currentPassHash]);
}
}
bool IsDepthOnlyRenderTexture(RenderTexture t)
{
return t.graphicsFormat == GraphicsFormat.None;
}
internal void SetNativeRenderPassAttachmentList(ScriptableRenderPass renderPass, ref CameraData cameraData, RTHandle passColorAttachment, RTHandle passDepthAttachment, ClearFlag finalClearFlag, Color finalClearColor)
{
using (new ProfilingScope(null, Profiling.setAttachmentList))
{
int currentPassIndex = renderPass.renderPassQueueIndex;
Hash128 currentPassHash = m_PassIndexToPassHash[currentPassIndex];
int[] currentMergeablePasses = m_MergeableRenderPassesMap[currentPassHash];
// Skip if not the first pass
if (currentMergeablePasses.First() != currentPassIndex)
return;
m_RenderPassesAttachmentCount[currentPassHash] = 0;
UpdateFinalStoreActions(currentMergeablePasses, ref cameraData);
int currentAttachmentIdx = 0;
foreach (var passIdx in currentMergeablePasses)
{
if (passIdx == -1)
break;
ScriptableRenderPass pass = m_ActiveRenderPassQueue[passIdx];
for (int i = 0; i < pass.m_ColorAttachmentIndices.Length; ++i)
pass.m_ColorAttachmentIndices[i] = -1;
AttachmentDescriptor currentAttachmentDescriptor;
var usesTargetTexture = cameraData.targetTexture != null;
var depthOnly = (pass.colorAttachmentHandle.rt != null && IsDepthOnlyRenderTexture(pass.colorAttachmentHandle.rt)) || (usesTargetTexture && IsDepthOnlyRenderTexture(cameraData.targetTexture));
int samples;
RenderTargetIdentifier colorAttachmentTarget;
// We are not rendering to Backbuffer so we have the RT and the information with it
// while also creating a new RenderTargetIdentifier to ignore the current depth slice (which might get bypassed in XR setup eventually)
if (new RenderTargetIdentifier(passColorAttachment.nameID, 0, depthSlice: 0) != BuiltinRenderTextureType.CameraTarget)
{
currentAttachmentDescriptor = new AttachmentDescriptor(depthOnly ? passColorAttachment.rt.descriptor.depthStencilFormat : passColorAttachment.rt.descriptor.graphicsFormat);
samples = passColorAttachment.rt.descriptor.msaaSamples;
colorAttachmentTarget = passColorAttachment.nameID;
}
else // In this case we might be rendering the the targetTexture or the Backbuffer, so less information is available
{
currentAttachmentDescriptor = new AttachmentDescriptor(pass.renderTargetFormat[0] != GraphicsFormat.None ? pass.renderTargetFormat[0] : UniversalRenderPipeline.MakeRenderTextureGraphicsFormat(cameraData.isHdrEnabled, cameraData.hdrColorBufferPrecision, Graphics.preserveFramebufferAlpha));
samples = cameraData.cameraTargetDescriptor.msaaSamples;
colorAttachmentTarget = usesTargetTexture ? new RenderTargetIdentifier(cameraData.targetTexture) : BuiltinRenderTextureType.CameraTarget;
}
currentAttachmentDescriptor.ConfigureTarget(colorAttachmentTarget, ((uint)finalClearFlag & (uint)ClearFlag.Color) == 0, true);
if (PassHasInputAttachments(pass))
SetupInputAttachmentIndices(pass);
// TODO: this is redundant and is being setup for each attachment. Needs to be done only once per mergeable pass list (we need to make sure mergeable passes use the same depth!)
m_ActiveDepthAttachmentDescriptor = new AttachmentDescriptor(SystemInfo.GetGraphicsFormat(DefaultFormat.DepthStencil));
m_ActiveDepthAttachmentDescriptor.ConfigureTarget(passDepthAttachment.nameID != BuiltinRenderTextureType.CameraTarget ? passDepthAttachment.nameID :
(usesTargetTexture ? new RenderTargetIdentifier(cameraData.targetTexture.depthBuffer) : BuiltinRenderTextureType.Depth),
((uint)finalClearFlag & (uint)ClearFlag.Depth) == 0, true);
if (finalClearFlag != ClearFlag.None)
{
// We don't clear color for Overlay render targets, however pipeline set's up depth only render passes as color attachments which we do need to clear
if ((cameraData.renderType != CameraRenderType.Overlay || depthOnly && ((uint)finalClearFlag & (uint)ClearFlag.Color) != 0))
currentAttachmentDescriptor.ConfigureClear(finalClearColor, 1.0f, 0);
if (((uint)finalClearFlag & (uint)ClearFlag.Depth) != 0)
m_ActiveDepthAttachmentDescriptor.ConfigureClear(Color.black, 1.0f, 0);
}
// resolving to the implicit color target's resolve surface TODO: handle m_CameraResolveTarget if present?
if (samples > 1)
{
currentAttachmentDescriptor.ConfigureResolveTarget(colorAttachmentTarget);
if (RenderingUtils.MultisampleDepthResolveSupported())
m_ActiveDepthAttachmentDescriptor.ConfigureResolveTarget(m_ActiveDepthAttachmentDescriptor.loadStoreTarget);
}
if (m_UseOptimizedStoreActions)
{
currentAttachmentDescriptor.storeAction = m_FinalColorStoreAction[0];
m_ActiveDepthAttachmentDescriptor.storeAction = m_FinalDepthStoreAction;
}
int existingAttachmentIndex = FindAttachmentDescriptorIndexInList(currentAttachmentIdx,
currentAttachmentDescriptor, m_ActiveColorAttachmentDescriptors);
if (existingAttachmentIndex == -1)
{
// add a new attachment
pass.m_ColorAttachmentIndices[0] = currentAttachmentIdx;
m_ActiveColorAttachmentDescriptors[currentAttachmentIdx] = currentAttachmentDescriptor;
currentAttachmentIdx++;
m_RenderPassesAttachmentCount[currentPassHash]++;
}
else
{
// attachment was already present
pass.m_ColorAttachmentIndices[0] = existingAttachmentIndex;
}
}
}
}
internal void ExecuteNativeRenderPass(ScriptableRenderContext context, ScriptableRenderPass renderPass, ref CameraData cameraData, ref RenderingData renderingData)
{
using (new ProfilingScope(null, Profiling.execute))
{
int currentPassIndex = renderPass.renderPassQueueIndex;
Hash128 currentPassHash = m_PassIndexToPassHash[currentPassIndex];
int[] currentMergeablePasses = m_MergeableRenderPassesMap[currentPassHash];
int validColorBuffersCount = m_RenderPassesAttachmentCount[currentPassHash];
var depthOnly = (renderPass.colorAttachmentHandle.rt != null && IsDepthOnlyRenderTexture(renderPass.colorAttachmentHandle.rt)) || (cameraData.targetTexture != null && IsDepthOnlyRenderTexture(cameraData.targetTexture));
bool useDepth = depthOnly || (!renderPass.overrideCameraTarget || (renderPass.overrideCameraTarget && renderPass.depthAttachmentHandle.nameID != BuiltinRenderTextureType.CameraTarget));// &&
var attachments =
new NativeArray<AttachmentDescriptor>(useDepth && !depthOnly ? validColorBuffersCount + 1 : 1, Allocator.Temp);
for (int i = 0; i < validColorBuffersCount; ++i)
attachments[i] = m_ActiveColorAttachmentDescriptors[i];
if (useDepth && !depthOnly)
attachments[validColorBuffersCount] = m_ActiveDepthAttachmentDescriptor;
var rpDesc = InitializeRenderPassDescriptor(ref cameraData, renderPass);
int validPassCount = GetValidPassIndexCount(currentMergeablePasses);
var attachmentIndicesCount = GetSubPassAttachmentIndicesCount(renderPass);
var attachmentIndices = new NativeArray<int>(!depthOnly ? (int)attachmentIndicesCount : 0, Allocator.Temp);
if (!depthOnly)
{
for (int i = 0; i < attachmentIndicesCount; ++i)
{
attachmentIndices[i] = renderPass.m_ColorAttachmentIndices[i];
}
}
if (validPassCount == 1 || currentMergeablePasses[0] == currentPassIndex) // Check if it's the first pass
{
if (PassHasInputAttachments(renderPass))
Debug.LogWarning("First pass in a RenderPass should not have input attachments.");
context.BeginRenderPass(rpDesc.w, rpDesc.h, Math.Max(rpDesc.samples, 1), attachments,
useDepth ? (!depthOnly ? validColorBuffersCount : 0) : -1);
attachments.Dispose();
context.BeginSubPass(attachmentIndices);
m_LastBeginSubpassPassIndex = currentPassIndex;
}
else
{
// Regarding input attachments, currently we always recreate a new subpass if it contains input attachments
// This might not the most optimal way though and it should be investigated in the future
// Whether merging subpasses with matching input attachments is a more viable option
if (!AreAttachmentIndicesCompatible(m_ActiveRenderPassQueue[m_LastBeginSubpassPassIndex], m_ActiveRenderPassQueue[currentPassIndex]))
{
context.EndSubPass();
if (PassHasInputAttachments(m_ActiveRenderPassQueue[currentPassIndex]))
context.BeginSubPass(attachmentIndices, m_ActiveRenderPassQueue[currentPassIndex].m_InputAttachmentIndices);
else
context.BeginSubPass(attachmentIndices);
m_LastBeginSubpassPassIndex = currentPassIndex;
}
else if (PassHasInputAttachments(m_ActiveRenderPassQueue[currentPassIndex]))
{
context.EndSubPass();
context.BeginSubPass(attachmentIndices, m_ActiveRenderPassQueue[currentPassIndex].m_InputAttachmentIndices);
m_LastBeginSubpassPassIndex = currentPassIndex;
}
}
attachmentIndices.Dispose();
renderPass.Execute(context, ref renderingData);
// Need to execute it immediately to avoid sync issues between context and cmd buffer
context.ExecuteCommandBuffer(renderingData.commandBuffer);
renderingData.commandBuffer.Clear();
if (validPassCount == 1 || currentMergeablePasses[validPassCount - 1] == currentPassIndex) // Check if it's the last pass
{
context.EndSubPass();
context.EndRenderPass();
m_LastBeginSubpassPassIndex = 0;
}
for (int i = 0; i < m_ActiveColorAttachmentDescriptors.Length; ++i)
{
m_ActiveColorAttachmentDescriptors[i] = RenderingUtils.emptyAttachment;
m_IsActiveColorAttachmentTransient[i] = false;
}
m_ActiveDepthAttachmentDescriptor = RenderingUtils.emptyAttachment;
}
}
internal void SetupInputAttachmentIndices(ScriptableRenderPass pass)
{
var validInputBufferCount = GetValidInputAttachmentCount(pass);
pass.m_InputAttachmentIndices = new NativeArray<int>(validInputBufferCount, Allocator.Temp);
for (int i = 0; i < validInputBufferCount; i++)
{
pass.m_InputAttachmentIndices[i] = FindAttachmentDescriptorIndexInList(pass.m_InputAttachments[i], m_ActiveColorAttachmentDescriptors);
if (pass.m_InputAttachmentIndices[i] == -1)
{
Debug.LogWarning("RenderPass Input attachment not found in the current RenderPass");
continue;
}
// Only update it as long as it has default value - if it was changed once, we assume it'll be memoryless in the whole RenderPass
if (!m_IsActiveColorAttachmentTransient[pass.m_InputAttachmentIndices[i]])
{
m_IsActiveColorAttachmentTransient[pass.m_InputAttachmentIndices[i]] = pass.IsInputAttachmentTransient(i);
}
}
}
internal void SetupTransientInputAttachments(int attachmentCount)
{
for (int i = 0; i < attachmentCount; ++i)
{
if (!m_IsActiveColorAttachmentTransient[i])
continue;
m_ActiveColorAttachmentDescriptors[i].loadAction = RenderBufferLoadAction.DontCare;
m_ActiveColorAttachmentDescriptors[i].storeAction = RenderBufferStoreAction.DontCare;
// We change the target of the descriptor for it to be initialized engine-side as a transient resource.
m_ActiveColorAttachmentDescriptors[i].loadStoreTarget = BuiltinRenderTextureType.None;
}
}
internal static uint GetSubPassAttachmentIndicesCount(ScriptableRenderPass pass)
{
uint numValidAttachments = 0;
foreach (var attIdx in pass.m_ColorAttachmentIndices)
{
if (attIdx >= 0)
++numValidAttachments;
}
return numValidAttachments;
}
internal static bool AreAttachmentIndicesCompatible(ScriptableRenderPass lastSubPass, ScriptableRenderPass currentSubPass)
{
uint lastSubPassAttCount = GetSubPassAttachmentIndicesCount(lastSubPass);
uint currentSubPassAttCount = GetSubPassAttachmentIndicesCount(currentSubPass);
if (currentSubPassAttCount > lastSubPassAttCount)
return false;
uint numEqualAttachments = 0;
for (int currPassIdx = 0; currPassIdx < currentSubPassAttCount; ++currPassIdx)
{
for (int lastPassIdx = 0; lastPassIdx < lastSubPassAttCount; ++lastPassIdx)
{
if (currentSubPass.m_ColorAttachmentIndices[currPassIdx] == lastSubPass.m_ColorAttachmentIndices[lastPassIdx])
numEqualAttachments++;
}
}
return (numEqualAttachments == currentSubPassAttCount);
}
internal static uint GetValidColorAttachmentCount(AttachmentDescriptor[] colorAttachments)
{
uint nonNullColorBuffers = 0;
if (colorAttachments != null)
{
foreach (var attachment in colorAttachments)
{
if (attachment != RenderingUtils.emptyAttachment)
++nonNullColorBuffers;
}
}
return nonNullColorBuffers;
}
internal static int GetValidInputAttachmentCount(ScriptableRenderPass renderPass)
{
var length = renderPass.m_InputAttachments.Length;
if (length != 8) // overriden, there are attachments
return length;
else
{
for (int i = 0; i < length; ++i)
{
if (renderPass.m_InputAttachments[i] == null)
return i;
}
return length;
}
}
internal static int FindAttachmentDescriptorIndexInList(int attachmentIdx, AttachmentDescriptor attachmentDescriptor, AttachmentDescriptor[] attachmentDescriptors)
{
int existingAttachmentIndex = -1;
for (int i = 0; i <= attachmentIdx; ++i)
{
AttachmentDescriptor att = attachmentDescriptors[i];
if (att.loadStoreTarget == attachmentDescriptor.loadStoreTarget && att.graphicsFormat == attachmentDescriptor.graphicsFormat)
{
existingAttachmentIndex = i;
break;
}
}
return existingAttachmentIndex;
}
internal static int FindAttachmentDescriptorIndexInList(RenderTargetIdentifier target, AttachmentDescriptor[] attachmentDescriptors)
{
for (int i = 0; i < attachmentDescriptors.Length; i++)
{
AttachmentDescriptor att = attachmentDescriptors[i];
if (att.loadStoreTarget == target)
return i;
}
return -1;
}
internal static int GetValidPassIndexCount(int[] array)
{
if (array == null)
return 0;
for (int i = 0; i < array.Length; ++i)
{
if (array[i] == -1)
return i;
}
return array.Length - 1;
}
internal static RTHandle GetFirstAllocatedRTHandle(ScriptableRenderPass pass)
{
for (int i = 0; i < pass.colorAttachmentHandles.Length; ++i)
{
if (pass.colorAttachmentHandles[i].rt != null)
return pass.colorAttachmentHandles[i];
}
return pass.colorAttachmentHandles[0];
}
internal static bool PassHasInputAttachments(ScriptableRenderPass renderPass)
{
return renderPass.m_InputAttachments.Length != 8 || renderPass.m_InputAttachments[0] != null;
}
internal static Hash128 CreateRenderPassHash(int width, int height, int depthID, int sample, uint hashIndex)
{
return new Hash128((uint)(width << 4) + (uint)height, (uint)depthID, (uint)sample, hashIndex);
}
internal static Hash128 CreateRenderPassHash(RenderPassDescriptor desc, uint hashIndex)
{
return CreateRenderPassHash(desc.w, desc.h, desc.depthID, desc.samples, hashIndex);
}
private RenderPassDescriptor InitializeRenderPassDescriptor(ref CameraData cameraData, ScriptableRenderPass renderPass)
{
RenderTextureDescriptor targetRT;
if (!renderPass.overrideCameraTarget || (renderPass.colorAttachmentHandle.rt == null && renderPass.depthAttachmentHandle.rt == null))
{
targetRT = cameraData.cameraTargetDescriptor;
// In this case we want to rely on the pixelWidth/Height as the texture could be scaled from a script later and etc.
// and it's new dimensions might not be reflected on the targetTexture. This also applies to camera stacks rendering to a target texture.
if (cameraData.targetTexture != null)
{
targetRT.width = cameraData.pixelWidth;
targetRT.height = cameraData.pixelHeight;
}
}
else
{
var handle = GetFirstAllocatedRTHandle(renderPass);
targetRT = handle.rt != null ? handle.rt.descriptor : renderPass.depthAttachmentHandle.rt.descriptor;
}
var depthTarget = renderPass.overrideCameraTarget ? renderPass.depthAttachmentHandle : cameraDepthTargetHandle;
var depthID = (targetRT.graphicsFormat == GraphicsFormat.None && targetRT.depthStencilFormat != GraphicsFormat.None) ? renderPass.colorAttachmentHandle.GetHashCode() : depthTarget.GetHashCode();
return new RenderPassDescriptor(targetRT.width, targetRT.height, targetRT.msaaSamples, depthID);
}
}
}