-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathSurfaceCacheGIRendererFeature.cs
More file actions
1253 lines (1096 loc) · 73.4 KB
/
SurfaceCacheGIRendererFeature.cs
File metadata and controls
1253 lines (1096 loc) · 73.4 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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if SURFACE_CACHE
using System;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine.Experimental.Rendering;
using UnityEngine.PathTracing.Core;
using UnityEngine.Rendering.LiveGI;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering.UnifiedRayTracing;
using InstanceHandle = UnityEngine.PathTracing.Core.Handle<UnityEngine.Rendering.SurfaceCacheWorld.Instance>;
using LightHandle = UnityEngine.PathTracing.Core.Handle<UnityEngine.Rendering.SurfaceCacheWorld.Light>;
using MaterialHandle = UnityEngine.PathTracing.Core.Handle<UnityEngine.PathTracing.Core.MaterialPool.MaterialDescriptor>;
namespace UnityEngine.Rendering.Universal
{
[Serializable]
[SupportedOnRenderPipeline]
[Categorization.CategoryInfo(Name = "R: Surface Cache URP Integration", Order = 1000), HideInInspector]
sealed class SurfaceCacheRenderPipelineResourceSet : IRenderPipelineResources
{
[SerializeField, HideInInspector]
int m_Version = 6;
int IRenderPipelineGraphicsSettings.version => m_Version;
[ResourcePath("Runtime/RendererFeatures/SurfaceCacheGIRendererFeature/FallbackMaterial.mat")]
public Material m_FallbackMaterial;
[ResourcePath("Runtime/RendererFeatures/SurfaceCacheGIRendererFeature/PatchAllocation.compute")]
public ComputeShader m_AllocationShader;
[ResourcePath("Runtime/RendererFeatures/SurfaceCacheGIRendererFeature/ScreenResolveLookup.compute")]
public ComputeShader m_ScreenResolveLookupShader;
[ResourcePath("Runtime/RendererFeatures/SurfaceCacheGIRendererFeature/ScreenResolveUpsampling.compute")]
public ComputeShader m_ScreenResolveUpsamplingShader;
[ResourcePath("Runtime/RendererFeatures/SurfaceCacheGIRendererFeature/Debug.compute")]
public ComputeShader m_DebugShader;
[ResourcePath("Runtime/RendererFeatures/SurfaceCacheGIRendererFeature/FlatNormalResolution.compute")]
public ComputeShader m_FlatNormalResolutionShader;
public Material fallbackMaterial
{
get => m_FallbackMaterial;
set => this.SetValueAndNotify(ref m_FallbackMaterial, value, nameof(m_FallbackMaterial));
}
public ComputeShader allocationShader
{
get => m_AllocationShader;
set => this.SetValueAndNotify(ref m_AllocationShader, value, nameof(m_AllocationShader));
}
public ComputeShader screenResolveLookupShader
{
get => m_ScreenResolveLookupShader;
set => this.SetValueAndNotify(ref m_ScreenResolveLookupShader, value, nameof(m_ScreenResolveLookupShader));
}
public ComputeShader screenResolveUpsamplingShader
{
get => m_ScreenResolveUpsamplingShader;
set => this.SetValueAndNotify(ref m_ScreenResolveUpsamplingShader, value, nameof(m_ScreenResolveUpsamplingShader));
}
public ComputeShader debugShader
{
get => m_DebugShader;
set => this.SetValueAndNotify(ref m_DebugShader, value, nameof(m_DebugShader));
}
public ComputeShader flatNormalResolutionShader
{
get => m_FlatNormalResolutionShader;
set => this.SetValueAndNotify(ref m_FlatNormalResolutionShader, value, nameof(m_FlatNormalResolutionShader));
}
}
[DisallowMultipleRendererFeature("Surface Cache Global Illumination")]
public class SurfaceCacheGIRendererFeature : ScriptableRendererFeature
{
public enum DebugViewMode_
{
CellIndex,
StableIrradiance,
FastIrradiance,
CoefficientOfVariation,
Drift,
StdDev,
UpdateCount,
FlatNormal
}
// URP currently cannot render motion vectors properly in Scene View, so we disable it.
// https://jira.unity3d.com/browse/SRP-743
// When this is fixed, we probably want to enable this always.
static bool UseMotionVectorPatchSeeding(CameraType camType)
{
return camType == CameraType.Game;
}
internal static class ShaderIDs
{
public static readonly int _RingConfigBuffer = Shader.PropertyToID("_RingConfigBuffer");
public static readonly int _PatchIrradiances = Shader.PropertyToID("_PatchIrradiances");
public static readonly int _PatchStatistics = Shader.PropertyToID("_PatchStatistics");
public static readonly int _PatchGeometries = Shader.PropertyToID("_PatchGeometries");
public static readonly int _PatchIrradiances0 = Shader.PropertyToID("_PatchIrradiances0");
public static readonly int _PatchIrradiances1 = Shader.PropertyToID("_PatchIrradiances1");
public static readonly int _PatchCounterSets = Shader.PropertyToID("_PatchCounterSets");
public static readonly int _CellAllocationMarks = Shader.PropertyToID("_CellAllocationMarks");
public static readonly int _CellPatchIndices = Shader.PropertyToID("_CellPatchIndices");
public static readonly int _Result = Shader.PropertyToID("_Result");
public static readonly int _FullResDepths = Shader.PropertyToID("_FullResDepths");
public static readonly int _FullResIrradiances = Shader.PropertyToID("_FullResIrradiances");
public static readonly int _FullResFlatNormals = Shader.PropertyToID("_FullResFlatNormals");
public static readonly int _FullResShadedNormals = Shader.PropertyToID("_FullResShadedNormals");
public static readonly int _UseMotionVectorSeeding = Shader.PropertyToID("_UseMotionVectorSeeding");
public static readonly int _ResultL0 = Shader.PropertyToID("_ResultL0");
public static readonly int _ResultL10 = Shader.PropertyToID("_ResultL10");
public static readonly int _ResultL11 = Shader.PropertyToID("_ResultL11");
public static readonly int _ResultL12 = Shader.PropertyToID("_ResultL12");
public static readonly int _ResultNdcDepths = Shader.PropertyToID("_ResultNdcDepths");
public static readonly int _ScreenDepths = Shader.PropertyToID("_ScreenDepths");
public static readonly int _ScreenFlatNormals = Shader.PropertyToID("_ScreenFlatNormals");
public static readonly int _CurrentFullResScreenDepths = Shader.PropertyToID("_CurrentFullResScreenDepths");
public static readonly int _CurrentFullResScreenFlatNormals = Shader.PropertyToID("_CurrentFullResScreenFlatNormals");
public static readonly int _CurrentFullResScreenMotionVectors = Shader.PropertyToID("_CurrentFullResScreenMotionVectors");
public static readonly int _ScreenShadedNormals = Shader.PropertyToID("_ScreenShadedNormals");
public static readonly int _LowResIrradiancesL0 = Shader.PropertyToID("_LowResIrradiancesL0");
public static readonly int _LowResIrradiancesL10 = Shader.PropertyToID("_LowResIrradiancesL10");
public static readonly int _LowResIrradiancesL11 = Shader.PropertyToID("_LowResIrradiancesL11");
public static readonly int _LowResIrradiancesL12 = Shader.PropertyToID("_LowResIrradiancesL12");
public static readonly int _PreviousLowResScreenIrradiancesL0 = Shader.PropertyToID("_PreviousLowResScreenIrradiancesL0");
public static readonly int _PreviousLowResScreenIrradiancesL10 = Shader.PropertyToID("_PreviousLowResScreenIrradiancesL10");
public static readonly int _PreviousLowResScreenIrradiancesL11 = Shader.PropertyToID("_PreviousLowResScreenIrradiancesL11");
public static readonly int _PreviousLowResScreenIrradiancesL12 = Shader.PropertyToID("_PreviousLowResScreenIrradiancesL12");
public static readonly int _PreviousLowResScreenNdcDepths = Shader.PropertyToID("_PreviousLowResScreenNdcDepths");
public static readonly int _FilterRadius = Shader.PropertyToID("_FilterRadius");
public static readonly int _ViewMode = Shader.PropertyToID("_ViewMode");
public static readonly int _ShowSamplePosition = Shader.PropertyToID("_ShowSamplePosition");
public static readonly int _SampleCount = Shader.PropertyToID("_SampleCount");
public static readonly int _ClipToWorldTransform = Shader.PropertyToID("_ClipToWorldTransform");
public static readonly int _CurrentClipToWorldTransform = Shader.PropertyToID("_CurrentClipToWorldTransform");
public static readonly int _PreviousClipToWorldTransform = Shader.PropertyToID("_PreviousClipToWorldTransform");
public static readonly int _FrameIdx = Shader.PropertyToID("_FrameIdx");
public static readonly int _GridSize = Shader.PropertyToID("_GridSize");
public static readonly int _RingConfigOffset = Shader.PropertyToID("_RingConfigOffset");
public static readonly int _GridTargetPos = Shader.PropertyToID("_GridTargetPos");
public static readonly int _FullResPixelOffset = Shader.PropertyToID("_FullResPixelOffset");
public static readonly int _LowResScreenSize = Shader.PropertyToID("_LowResScreenSize");
public static readonly int _CascadeCount = Shader.PropertyToID("_CascadeCount");
public static readonly int _VoxelMinSize = Shader.PropertyToID("_VoxelMinSize");
public static readonly int _CascadeOffsets = Shader.PropertyToID("_CascadeOffsets");
public static readonly int _PatchCellIndices = Shader.PropertyToID("_PatchCellIndices");
}
class SurfaceCachePass : ScriptableRenderPass, IDisposable
{
private class WorldUpdatePassData
{
internal SurfaceCacheWorld World;
}
private class DebugPassData
{
internal ComputeShader Shader;
internal int KernelIndex;
internal uint3 ThreadGroupSize;
internal uint3 ThreadCount;
internal TextureHandle ScreenDepths;
internal TextureHandle ScreenShadedNormals;
internal TextureHandle ScreenFlatNormals;
internal TextureHandle ScreenIrradiances;
internal GraphicsBuffer CellPatchIndices;
internal GraphicsBuffer RingConfigBuffer;
internal GraphicsBuffer PatchIrradiances;
internal GraphicsBuffer PatchGeometries;
internal GraphicsBuffer PatchCellIndices;
internal GraphicsBuffer PatchStatistics;
internal GraphicsBuffer PatchCounterSets;
internal DebugViewMode_ ViewMode;
internal uint FrameIndex;
internal bool ShowSamplePosition;
internal uint GridSize;
internal float VoxelMinSize;
internal uint CascadeCount;
internal GraphicsBuffer CascadeOffsets;
internal uint RingConfigOffset;
internal Matrix4x4 ClipToWorldTransform;
internal Vector3 GridTargetPos;
}
private class FlatNormalResolutionPassData
{
internal ComputeShader Shader;
internal int KernelIndex;
internal uint3 ThreadGroupSize;
internal uint3 ThreadCount;
internal TextureHandle ScreenDepths;
internal TextureHandle ScreenFlatNormals;
internal Matrix4x4 ClipToWorldTransform;
}
private class PatchAllocationPassData
{
internal ComputeShader Shader;
internal int KernelIndex;
internal uint3 ThreadGroupSize;
internal uint3 ThreadCount;
internal TextureHandle ScreenDepths;
internal TextureHandle ScreenFlatNormals;
internal TextureHandle ScreenMotionVectors;
internal TextureHandle LowResScreenIrradiancesL0;
internal TextureHandle LowResScreenIrradiancesL10;
internal TextureHandle LowResScreenIrradiancesL11;
internal TextureHandle LowResScreenIrradiancesL12;
internal TextureHandle LowResScreenNdcDepths;
internal GraphicsBuffer CellAllocationMarks;
internal GraphicsBuffer CellPatchIndices;
internal GraphicsBuffer RingConfigBuffer;
internal GraphicsBuffer PatchIrradiances0;
internal GraphicsBuffer PatchIrradiances1;
internal GraphicsBuffer PatchGeometries;
internal GraphicsBuffer PatchCellIndices;
internal GraphicsBuffer PatchCounterSets;
internal uint FrameIdx;
internal uint GridSize;
internal uint CascadeCount;
internal uint RingConfigOffset;
internal bool UseMotionVectorSeeding;
internal GraphicsBuffer CascadeOffsets;
internal float VoxelMinSize;
internal Matrix4x4 CurrentClipToWorldTransform;
internal Matrix4x4 PreviousClipToWorldTransform;
internal Vector3 GridTargetPos;
internal uint2 FullResPixelOffset;
internal uint2 LowResScreenSize;
}
private class ScreenIrradianceLookupPassData
{
internal ComputeShader Shader;
internal int KernelIndex;
internal uint3 ThreadGroupSize;
internal uint3 ThreadCount;
internal TextureHandle FullResDepths;
internal TextureHandle FullResFlatNormals;
internal TextureHandle LowResScreenIrradiancesL0;
internal TextureHandle LowResScreenIrradiancesL10;
internal TextureHandle LowResScreenIrradiancesL11;
internal TextureHandle LowResScreenIrradiancesL12;
internal TextureHandle LowResScreenNdcDepths;
internal GraphicsBuffer CellPatchIndices;
internal GraphicsBuffer PatchIrradiances;
internal GraphicsBuffer PatchCounterSets;
internal GraphicsBuffer CascadeOffsets;
internal uint GridSize;
internal uint CascadeCount;
internal uint SampleCount;
internal uint FrameIndex;
internal float VoxelMinSize;
internal Matrix4x4 ClipToWorldTransform;
internal Vector3 GridTargetPos;
}
private class ScreenIrradianceUpsamplingPassData
{
internal ComputeShader Shader;
internal int KernelIndex;
internal uint3 ThreadGroupSize;
internal uint3 FullResThreadCount;
internal TextureHandle FullResDepths;
internal TextureHandle FullResShadedNormals;
internal TextureHandle FullResFlatNormals;
internal TextureHandle LowResScreenIrradiancesL0;
internal TextureHandle LowResScreenIrradiancesL10;
internal TextureHandle LowResScreenIrradiancesL11;
internal TextureHandle LowResScreenIrradiancesL12;
internal TextureHandle FullResIrradiances;
internal float FilterRadius;
internal uint SampleCount;
internal Matrix4x4 ClipToWorldTransform;
}
private RTHandle _fullResScreenIrradiances;
private RTHandle _fullResScreenFlatNormals;
private RTHandle _lowResScreenIrradiancesL0;
private RTHandle _lowResScreenIrradiancesL10;
private RTHandle _lowResScreenIrradiancesL11;
private RTHandle _lowResScreenIrradiancesL12;
private RTHandle _lowResScreenNdcDepths;
private GraphicsBuffer _worldUpdateScratch;
private readonly SceneUpdatesTracker _sceneTracker;
private readonly WorldAdapter _worldAdapter;
private readonly SurfaceCacheWorld _world;
private readonly Material _fallbackMaterial;
private readonly ComputeShader _screenResolveLookupShader;
private readonly ComputeShader _screenResolveUpsamplingShader;
private readonly ComputeShader _patchAllocationShader;
private readonly ComputeShader _debugShader;
private readonly ComputeShader _flatNormalResolutionShader;
private readonly RayTracingContext _rtContext;
private readonly int _screenResolveLookupKernel;
private readonly int _screenResolveUpsamplingKernel;
private readonly int _patchAllocationKernel;
private readonly int _debugKernel;
private readonly int _flatNormalResolutionKernel;
private uint3 _screenResolveLookupKernelGroupSize;
private uint3 _screenResolveUpsamplingKernelGroupSize;
private uint3 _patchAllocationKernelGroupSize;
private uint3 _debugKernelGroupSize;
private uint3 _flatNormalResolutionKernelGroupSize;
private uint _frameIdx;
private bool _cascadeMovement;
// Debug
private readonly bool _debugEnabled;
private readonly DebugViewMode_ _debugViewMode;
private readonly bool _debugShowSamplePosition;
// Screen Filtering
private readonly uint _lookupSampleCount;
private readonly float _upsamplingKernelSize;
private readonly uint _upsamplingSampleCount;
private SurfaceCache _cache;
private Matrix4x4 _prevClipToWorldTransform = Matrix4x4.identity;
public SurfaceCachePass(
RayTracingContext rtContext,
UnityEngine.Rendering.SurfaceCacheResourceSet resourceSet,
WorldResourceSet worldResources,
ComputeShader patchAllocationShader,
ComputeShader screenResolveLookupShader,
ComputeShader screenResolveUpsamplingShader,
ComputeShader debugShader,
ComputeShader flatNormalResolutionShader,
Material fallbackMaterial,
bool debugEnabled,
DebugViewMode_ debugViewMode,
bool debugShowSamplePosition,
uint lookupSampleCount,
float upsamplingKernelSize,
uint upsamplingSampleCount,
SurfaceCacheGridParameterSet gridParams,
SurfaceCacheEstimationParameterSet estimationParams,
SurfaceCachePatchFilteringParameterSet patchFilteringParams,
bool cascadeMovement)
{
Debug.Assert(gridParams.CascadeCount != 0);
Debug.Assert(gridParams.CascadeCount <= SurfaceCache.CascadeMax);
_screenResolveLookupShader = screenResolveLookupShader;
_screenResolveUpsamplingShader = screenResolveUpsamplingShader;
_debugShader = debugShader;
_flatNormalResolutionShader = flatNormalResolutionShader;
_patchAllocationShader = patchAllocationShader;
_rtContext = rtContext;
_screenResolveLookupKernel = _screenResolveLookupShader.FindKernel("Lookup");
_screenResolveUpsamplingKernel = _screenResolveUpsamplingShader.FindKernel("Upsample");
_patchAllocationKernel = _patchAllocationShader.FindKernel("Allocate");
_debugKernel = _debugShader.FindKernel("Visualize");
_flatNormalResolutionKernel = _flatNormalResolutionShader.FindKernel("ResolveFlatNormals");
_cascadeMovement = cascadeMovement;
_screenResolveLookupShader.GetKernelThreadGroupSizes(_screenResolveLookupKernel, out _screenResolveLookupKernelGroupSize.x, out _screenResolveLookupKernelGroupSize.y, out _screenResolveLookupKernelGroupSize.z);
_screenResolveUpsamplingShader.GetKernelThreadGroupSizes(_screenResolveUpsamplingKernel, out _screenResolveUpsamplingKernelGroupSize.x, out _screenResolveUpsamplingKernelGroupSize.y, out _screenResolveUpsamplingKernelGroupSize.z);
_patchAllocationShader.GetKernelThreadGroupSizes(_patchAllocationKernel, out _patchAllocationKernelGroupSize.x, out _patchAllocationKernelGroupSize.y, out _patchAllocationKernelGroupSize.z);
_debugShader.GetKernelThreadGroupSizes(_debugKernel, out _debugKernelGroupSize.x, out _debugKernelGroupSize.y, out _debugKernelGroupSize.z);
_flatNormalResolutionShader.GetKernelThreadGroupSizes(_flatNormalResolutionKernel, out _flatNormalResolutionKernelGroupSize.x, out _flatNormalResolutionKernelGroupSize.y, out _flatNormalResolutionKernelGroupSize.z);
_frameIdx = 0;
_debugEnabled = debugEnabled;
_debugViewMode = debugViewMode;
_debugShowSamplePosition = debugShowSamplePosition;
_upsamplingKernelSize = upsamplingKernelSize;
_upsamplingSampleCount = upsamplingSampleCount;
_lookupSampleCount = lookupSampleCount;
_cache = new SurfaceCache(resourceSet, gridParams, estimationParams, patchFilteringParams);
_sceneTracker = new SceneUpdatesTracker();
_world = new SurfaceCacheWorld();
_world.Init(_rtContext, worldResources);
_fallbackMaterial = fallbackMaterial;
_worldAdapter = new WorldAdapter(_world, _fallbackMaterial);
}
public void Dispose()
{
_fullResScreenIrradiances?.Release();
_fullResScreenFlatNormals?.Release();
_lowResScreenIrradiancesL0?.Release();
_lowResScreenIrradiancesL10?.Release();
_lowResScreenIrradiancesL11?.Release();
_lowResScreenIrradiancesL12?.Release();
_lowResScreenNdcDepths?.Release();
_sceneTracker.Dispose();
_cache.Dispose();
_world.Dispose();
_worldAdapter.Dispose();
_worldUpdateScratch?.Dispose();
}
const int k_UpscaleFactor = 4;
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
Debug.Assert(resourceData.cameraDepthTexture.IsValid());
Debug.Assert(resourceData.cameraNormalsTexture.IsValid());
Debug.Assert(resourceData.motionVectorColor.IsValid());
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
var worldToViewTransform = cameraData.GetViewMatrix();
var viewToClipTransform = cameraData.GetGPUProjectionMatrix(true);
var worldToClipTransform = viewToClipTransform * worldToViewTransform;
var clipToWorldTransform = worldToClipTransform.inverse;
var screenResolution = new int2(cameraData.pixelWidth, cameraData.pixelHeight);
// This avoids applying surface cache to e.g. preview cameras.
if (cameraData.cameraType != CameraType.Game && cameraData.cameraType != CameraType.SceneView)
return;
bool useMotionVectorPatchSeeding = UseMotionVectorPatchSeeding(cameraData.cameraType);
if (_fullResScreenIrradiances == null || _fullResScreenIrradiances.GetScaledSize() != new Vector2Int(screenResolution.x, screenResolution.y))
{
int lowResWidth = (screenResolution.x + k_UpscaleFactor - 1) / k_UpscaleFactor;
int lowResHeight = (screenResolution.y + k_UpscaleFactor - 1) / k_UpscaleFactor;
_fullResScreenIrradiances?.Release();
_fullResScreenIrradiances = RTHandles.Alloc((int)screenResolution.x, (int)screenResolution.y, 1, DepthBits.None, GraphicsFormat.R16G16B16A16_SFloat, FilterMode.Point, TextureWrapMode.Clamp, TextureDimension.Tex2D, true, name: "_fullResScreenIrradiances");
_fullResScreenFlatNormals?.Release();
_fullResScreenFlatNormals = RTHandles.Alloc((int)screenResolution.x, (int)screenResolution.y, 1, DepthBits.None, GraphicsFormat.R8G8B8A8_SNorm, FilterMode.Point, TextureWrapMode.Clamp, TextureDimension.Tex2D, true, name: "_fullResScreenFlatNormals");
var l0Format = GraphicsFormat.R16G16B16A16_SFloat;
var l1Format = GraphicsFormat.R8G8B8A8_UNorm;
_lowResScreenIrradiancesL0?.Release();
_lowResScreenIrradiancesL0 = RTHandles.Alloc(lowResWidth, lowResHeight, 1, DepthBits.None, l0Format, FilterMode.Point, TextureWrapMode.Clamp, TextureDimension.Tex2D, true, name: "_lowResScreenIrradiancesL0");
_lowResScreenIrradiancesL10?.Release();
_lowResScreenIrradiancesL10 = RTHandles.Alloc(lowResWidth, lowResHeight, 1, DepthBits.None, l1Format, FilterMode.Point, TextureWrapMode.Clamp, TextureDimension.Tex2D, true, name: "_lowResScreenIrradiancesL10");
_lowResScreenIrradiancesL11?.Release();
_lowResScreenIrradiancesL11 = RTHandles.Alloc(lowResWidth, lowResHeight, 1, DepthBits.None, l1Format, FilterMode.Point, TextureWrapMode.Clamp, TextureDimension.Tex2D, true, name: "_lowResScreenIrradiancesL11");
_lowResScreenIrradiancesL12?.Release();
_lowResScreenIrradiancesL12 = RTHandles.Alloc(lowResWidth, lowResHeight, 1, DepthBits.None, l1Format, FilterMode.Point, TextureWrapMode.Clamp, TextureDimension.Tex2D, true, name: "_lowResScreenIrradiancesL12");
_lowResScreenNdcDepths?.Release();
_lowResScreenNdcDepths = RTHandles.Alloc(lowResWidth, lowResHeight, 1, DepthBits.None, GraphicsFormat.R16_UNorm, FilterMode.Point, TextureWrapMode.Clamp, TextureDimension.Tex2D, true, name: "_lowResScreenNdcDepths");
}
if (_cascadeMovement || _frameIdx == 0)
{
_cache.Grid.TargetPos = cameraData.camera.transform.position;
}
_cache.RecordPreparation(renderGraph, _frameIdx);
var fullResScreenIrradiancesHandle = renderGraph.ImportTexture(_fullResScreenIrradiances);
var fullResScreenFlatNormalsHandle = renderGraph.ImportTexture(_fullResScreenFlatNormals);
var lowResScreenIrradiancesL0Handle = renderGraph.ImportTexture(_lowResScreenIrradiancesL0);
var lowResScreenIrradiancesL10Handle = renderGraph.ImportTexture(_lowResScreenIrradiancesL10);
var lowResScreenIrradiancesL11Handle = renderGraph.ImportTexture(_lowResScreenIrradiancesL11);
var lowResScreenIrradiancesL12Handle = renderGraph.ImportTexture(_lowResScreenIrradiancesL12);
var lowResScreenNdcDepthsHandle = renderGraph.ImportTexture(_lowResScreenNdcDepths);
var cellAllocationMarkHandle = renderGraph.ImportBuffer(_cache.Grid.CellAllocationMarks);
using (var builder = renderGraph.AddComputePass("Surface Cache Flat Normal Resolution", out FlatNormalResolutionPassData passData))
{
passData.ThreadCount = new uint3((uint)screenResolution.x, (uint)screenResolution.y, 1);
passData.Shader = _flatNormalResolutionShader;
passData.KernelIndex = _flatNormalResolutionKernel;
passData.ThreadGroupSize = _flatNormalResolutionKernelGroupSize;
passData.ScreenDepths = resourceData.cameraDepthTexture;
passData.ScreenFlatNormals = fullResScreenFlatNormalsHandle;
passData.ClipToWorldTransform = clipToWorldTransform;
builder.UseTexture(resourceData.cameraDepthTexture, AccessFlags.Read);
builder.UseTexture(fullResScreenFlatNormalsHandle, AccessFlags.Write);
builder.SetRenderFunc((FlatNormalResolutionPassData data, ComputeGraphContext cgContext) => ResolveFlatNormals(data, cgContext));
}
using (var builder = renderGraph.AddComputePass("Surface Cache Patch Allocation", out PatchAllocationPassData passData))
{
uint2 fullResScreenSize = new uint2((uint)screenResolution.x, (uint)screenResolution.y);
uint2 lowResScreenSize = DivUp(fullResScreenSize, new uint2(k_UpscaleFactor, k_UpscaleFactor));
passData.ThreadCount = new uint3(lowResScreenSize, 1);
passData.Shader = _patchAllocationShader;
passData.KernelIndex = _patchAllocationKernel;
passData.ThreadGroupSize = _patchAllocationKernelGroupSize;
passData.ScreenDepths = resourceData.cameraDepthTexture;
passData.ScreenFlatNormals = fullResScreenFlatNormalsHandle;
passData.ScreenMotionVectors = resourceData.motionVectorColor;
passData.LowResScreenIrradiancesL0 = lowResScreenIrradiancesL0Handle;
passData.LowResScreenIrradiancesL10 = lowResScreenIrradiancesL10Handle;
passData.LowResScreenIrradiancesL11 = lowResScreenIrradiancesL11Handle;
passData.LowResScreenIrradiancesL12 = lowResScreenIrradiancesL12Handle;
passData.LowResScreenNdcDepths = lowResScreenNdcDepthsHandle;
passData.CellAllocationMarks = _cache.Grid.CellAllocationMarks;
passData.CellPatchIndices = _cache.Grid.CellPatchIndices;
passData.RingConfigBuffer = _cache.RingConfig.Buffer;
passData.PatchIrradiances0 = _cache.PatchList.Irradiances[0];
passData.PatchIrradiances1 = _cache.PatchList.Irradiances[2];
passData.PatchGeometries = _cache.PatchList.Geometries;
passData.PatchCellIndices = _cache.PatchList.CellIndices;
passData.PatchCounterSets = _cache.PatchList.CounterSets;
passData.FrameIdx = _frameIdx;
passData.GridSize = _cache.Grid.GridSize;
passData.CascadeCount = _cache.Grid.CascadeCount;
passData.RingConfigOffset = _cache.RingConfig.OffsetA;
{
Debug.Assert(k_UpscaleFactor == 4);
uint cycleIndex = _frameIdx % 4;
uint shuffledCycleIndex = cycleIndex * 7 % 16;
passData.FullResPixelOffset = new uint2(shuffledCycleIndex / 4, shuffledCycleIndex % 4);
}
passData.LowResScreenSize = lowResScreenSize;
passData.UseMotionVectorSeeding = useMotionVectorPatchSeeding;
passData.CascadeOffsets = _cache.Grid.CascadeOffsetBuffer;
passData.VoxelMinSize = _cache.Grid.VoxelMinSize;
passData.CurrentClipToWorldTransform = clipToWorldTransform;
passData.PreviousClipToWorldTransform = _prevClipToWorldTransform;
passData.GridTargetPos = _cache.Grid.TargetPos;
builder.UseBuffer(cellAllocationMarkHandle, AccessFlags.Write);
builder.UseTexture(resourceData.cameraDepthTexture, AccessFlags.Read);
builder.UseTexture(fullResScreenFlatNormalsHandle, AccessFlags.Read);
builder.UseTexture(resourceData.motionVectorColor, AccessFlags.Read);
builder.UseTexture(lowResScreenIrradiancesL0Handle, AccessFlags.Read);
builder.UseTexture(lowResScreenIrradiancesL10Handle, AccessFlags.Read);
builder.UseTexture(lowResScreenIrradiancesL11Handle, AccessFlags.Read);
builder.UseTexture(lowResScreenIrradiancesL12Handle, AccessFlags.Read);
builder.UseTexture(lowResScreenNdcDepthsHandle, AccessFlags.Read);
builder.SetRenderFunc((PatchAllocationPassData data, ComputeGraphContext cgContext) => AllocatePatches(data, cgContext));
}
using (var builder = renderGraph.AddUnsafePass("Surface Cache World Update", out WorldUpdatePassData passData))
{
const bool filterBakedLights = true;
var changes = _sceneTracker.GetChanges(filterBakedLights);
_worldAdapter.UpdateMaterials(_world, changes.addedMaterials, changes.removedMaterials, changes.changedMaterials);
_worldAdapter.UpdateInstances(_world, changes.addedInstances, changes.changedInstances, changes.removedInstances, RenderedGameObjectsFilter.All, _fallbackMaterial);
const bool multiplyPunctualLightIntensityByPI = false;
_worldAdapter.UpdateLights(_world, changes.addedLights, changes.removedLights, changes.changedLights, multiplyPunctualLightIntensityByPI);
passData.World = _world;
_world.SetEnvironmentMaterial(RenderSettings.skybox);
builder.AllowGlobalStateModification(true);
builder.SetRenderFunc((WorldUpdatePassData data, UnsafeGraphContext graphCtx) => UpdateWorld(data, graphCtx, ref _worldUpdateScratch));
}
uint outputIrradianceBufferIdx = _cache.RecordPatchUpdate(renderGraph, _frameIdx, _world);
using (var builder = renderGraph.AddComputePass("Surface Cache Screen Lookup", out ScreenIrradianceLookupPassData data))
{
data.GridTargetPos = _cache.Grid.TargetPos;
data.ClipToWorldTransform = clipToWorldTransform;
data.ThreadCount = new uint3((uint)_lowResScreenIrradiancesL0.rt.width, (uint)_lowResScreenIrradiancesL0.rt.height, 1);
data.Shader = _screenResolveLookupShader;
data.KernelIndex = _screenResolveLookupKernel;
data.ThreadGroupSize = _screenResolveLookupKernelGroupSize;
data.FullResDepths = resourceData.cameraDepthTexture;
data.FullResFlatNormals = fullResScreenFlatNormalsHandle;
data.LowResScreenIrradiancesL0 = lowResScreenIrradiancesL0Handle;
data.LowResScreenIrradiancesL10 = lowResScreenIrradiancesL10Handle;
data.LowResScreenIrradiancesL11 = lowResScreenIrradiancesL11Handle;
data.LowResScreenIrradiancesL12 = lowResScreenIrradiancesL12Handle;
data.LowResScreenNdcDepths = lowResScreenNdcDepthsHandle;
data.CellPatchIndices = _cache.Grid.CellPatchIndices;
data.PatchIrradiances = _cache.PatchList.Irradiances[outputIrradianceBufferIdx];
data.PatchCounterSets = _cache.PatchList.CounterSets;
data.CascadeOffsets = _cache.Grid.CascadeOffsetBuffer;
data.GridSize = _cache.Grid.GridSize;
data.VoxelMinSize = _cache.Grid.VoxelMinSize;
data.CascadeCount = _cache.Grid.CascadeCount;
data.SampleCount = _lookupSampleCount;
data.FrameIndex = _frameIdx;
builder.UseBuffer(cellAllocationMarkHandle, AccessFlags.Read);
builder.UseTexture(resourceData.cameraDepthTexture, AccessFlags.Read);
builder.UseTexture(fullResScreenFlatNormalsHandle, AccessFlags.Read);
builder.UseTexture(resourceData.motionVectorColor, AccessFlags.Read);
builder.UseTexture(data.LowResScreenIrradiancesL0, AccessFlags.Write);
builder.UseTexture(data.LowResScreenIrradiancesL10, AccessFlags.Write);
builder.UseTexture(data.LowResScreenIrradiancesL11, AccessFlags.Write);
builder.UseTexture(data.LowResScreenIrradiancesL12, AccessFlags.Write);
builder.UseTexture(data.LowResScreenNdcDepths, AccessFlags.Write);
builder.SetRenderFunc((ScreenIrradianceLookupPassData data, ComputeGraphContext cgContext) => LookupScreenIrradiance(data, cgContext));
}
if (_debugEnabled)
{
using (var builder = renderGraph.AddComputePass("Surface Cache Debug", out DebugPassData passData))
{
passData.ThreadCount = new uint3((uint)screenResolution.x, (uint)screenResolution.y, 1);
passData.Shader = _debugShader;
passData.KernelIndex = _debugKernel;
passData.ThreadGroupSize = _debugKernelGroupSize;
passData.ScreenDepths = resourceData.cameraDepthTexture;
passData.ScreenShadedNormals = resourceData.cameraNormalsTexture;
passData.ScreenFlatNormals = fullResScreenFlatNormalsHandle;
passData.ScreenIrradiances = fullResScreenIrradiancesHandle;
passData.PatchCellIndices = _cache.PatchList.CellIndices;
passData.CellPatchIndices = _cache.Grid.CellPatchIndices;
passData.RingConfigBuffer = _cache.RingConfig.Buffer;
passData.RingConfigOffset = _cache.RingConfig.OffsetA;
passData.GridTargetPos = _cache.Grid.TargetPos;
passData.PatchIrradiances = _cache.PatchList.Irradiances[outputIrradianceBufferIdx];
passData.PatchGeometries = _cache.PatchList.Geometries;
passData.PatchStatistics = _cache.PatchList.Statistics;
passData.PatchCounterSets = _cache.PatchList.CounterSets;
passData.GridSize = _cache.Grid.GridSize;
passData.VoxelMinSize = _cache.Grid.VoxelMinSize;
passData.CascadeCount = _cache.Grid.CascadeCount;
passData.CascadeOffsets = _cache.Grid.CascadeOffsetBuffer;
passData.ViewMode = _debugViewMode;
passData.FrameIndex = _frameIdx;
passData.ShowSamplePosition = _debugShowSamplePosition;
passData.ClipToWorldTransform = clipToWorldTransform;
builder.UseTexture(resourceData.cameraDepthTexture, AccessFlags.Read);
builder.UseTexture(resourceData.cameraNormalsTexture, AccessFlags.Read);
builder.UseTexture(fullResScreenFlatNormalsHandle, AccessFlags.Read);
builder.UseTexture(passData.ScreenIrradiances, AccessFlags.Write);
builder.UseBuffer(cellAllocationMarkHandle, AccessFlags.Read);
builder.SetRenderFunc((DebugPassData data, ComputeGraphContext cgContext) => RenderDebug(data, cgContext));
}
}
else
{
using (var builder = renderGraph.AddComputePass("Surface Cache Screen Upsampling", out ScreenIrradianceUpsamplingPassData data))
{
data.ClipToWorldTransform = clipToWorldTransform;
data.FullResThreadCount = new uint3((uint)screenResolution.x, (uint)screenResolution.y, 1);
data.Shader = _screenResolveUpsamplingShader;
data.KernelIndex = _screenResolveUpsamplingKernel;
data.ThreadGroupSize = _screenResolveUpsamplingKernelGroupSize;
data.FullResDepths = resourceData.cameraDepthTexture;
data.FullResShadedNormals = resourceData.cameraNormalsTexture;
data.FullResFlatNormals = fullResScreenFlatNormalsHandle;
data.LowResScreenIrradiancesL0 = lowResScreenIrradiancesL0Handle;
data.LowResScreenIrradiancesL10 = lowResScreenIrradiancesL10Handle;
data.LowResScreenIrradiancesL11 = lowResScreenIrradiancesL11Handle;
data.LowResScreenIrradiancesL12 = lowResScreenIrradiancesL12Handle;
data.FullResIrradiances = fullResScreenIrradiancesHandle;
data.FilterRadius = _upsamplingKernelSize;
data.SampleCount = _upsamplingSampleCount;
builder.UseTexture(data.FullResIrradiances, AccessFlags.Write);
builder.UseTexture(resourceData.cameraDepthTexture, AccessFlags.Read);
builder.UseTexture(resourceData.cameraNormalsTexture, AccessFlags.Read);
builder.UseTexture(fullResScreenFlatNormalsHandle, AccessFlags.Read);
builder.UseTexture(resourceData.motionVectorColor, AccessFlags.Read);
builder.UseTexture(data.LowResScreenIrradiancesL0, AccessFlags.Read);
builder.UseTexture(data.LowResScreenIrradiancesL10, AccessFlags.Read);
builder.UseTexture(data.LowResScreenIrradiancesL11, AccessFlags.Read);
builder.UseTexture(data.LowResScreenIrradiancesL12, AccessFlags.Read);
builder.UseBuffer(cellAllocationMarkHandle, AccessFlags.Read);
builder.SetRenderFunc((ScreenIrradianceUpsamplingPassData data, ComputeGraphContext cgContext) => UpsampleScreenIrradiance(data, cgContext));
}
}
resourceData.irradianceTexture = fullResScreenIrradiancesHandle;
_frameIdx++;
_prevClipToWorldTransform = clipToWorldTransform;
}
static void UpdateWorld(WorldUpdatePassData data, UnsafeGraphContext graphCtx, ref GraphicsBuffer scratch)
{
var cmd = CommandBufferHelpers.GetNativeCommandBuffer(graphCtx.cmd);
data.World.Build(cmd, ref scratch);
}
static void LookupScreenIrradiance(ScreenIrradianceLookupPassData data, ComputeGraphContext cgContext)
{
var cmd = cgContext.cmd;
var shader = data.Shader;
var kernelIndex = data.KernelIndex;
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ResultL0, data.LowResScreenIrradiancesL0);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ResultL10, data.LowResScreenIrradiancesL10);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ResultL11, data.LowResScreenIrradiancesL11);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ResultL12, data.LowResScreenIrradiancesL12);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ResultNdcDepths, data.LowResScreenNdcDepths);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ScreenDepths, data.FullResDepths);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ScreenFlatNormals, data.FullResFlatNormals);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._CellPatchIndices, data.CellPatchIndices);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchIrradiances, data.PatchIrradiances);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._CascadeOffsets, data.CascadeOffsets);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchCounterSets, data.PatchCounterSets);
cmd.SetComputeIntParam(shader, ShaderIDs._GridSize, (int)data.GridSize);
cmd.SetComputeIntParam(shader, ShaderIDs._CascadeCount, (int)data.CascadeCount);
cmd.SetComputeIntParam(shader, ShaderIDs._SampleCount, (int)data.SampleCount);
cmd.SetComputeIntParam(shader, ShaderIDs._FrameIdx, (int)data.FrameIndex);
cmd.SetComputeFloatParam(shader, ShaderIDs._VoxelMinSize, data.VoxelMinSize);
cmd.SetComputeMatrixParam(shader, ShaderIDs._ClipToWorldTransform, data.ClipToWorldTransform);
cmd.SetComputeVectorParam(shader, ShaderIDs._GridTargetPos, data.GridTargetPos);
uint3 groupCount = DivUp(data.ThreadCount, data.ThreadGroupSize);
cmd.DispatchCompute(shader, kernelIndex, (int)groupCount.x, (int)groupCount.y, 1);
}
static void UpsampleScreenIrradiance(ScreenIrradianceUpsamplingPassData passData, ComputeGraphContext cgContext)
{
var cmd = cgContext.cmd;
var shader = passData.Shader;
var kernelIndex = passData.KernelIndex;
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._FullResIrradiances, passData.FullResIrradiances);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._FullResDepths, passData.FullResDepths);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._FullResFlatNormals, passData.FullResFlatNormals);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._FullResShadedNormals, passData.FullResShadedNormals);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._LowResIrradiancesL0, passData.LowResScreenIrradiancesL0);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._LowResIrradiancesL10, passData.LowResScreenIrradiancesL10);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._LowResIrradiancesL11, passData.LowResScreenIrradiancesL11);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._LowResIrradiancesL12, passData.LowResScreenIrradiancesL12);
cmd.SetComputeFloatParam(shader, ShaderIDs._FilterRadius, passData.FilterRadius);
cmd.SetComputeIntParam(shader, ShaderIDs._SampleCount, (int)passData.SampleCount);
cmd.SetComputeMatrixParam(shader, ShaderIDs._ClipToWorldTransform, passData.ClipToWorldTransform);
uint3 groupCount = DivUp(passData.FullResThreadCount, passData.ThreadGroupSize);
cmd.DispatchCompute(shader, kernelIndex, (int)groupCount.x, (int)groupCount.y, 1);
}
static void ResolveFlatNormals(FlatNormalResolutionPassData data, ComputeGraphContext cgContext)
{
var cmd = cgContext.cmd;
var shader = data.Shader;
var kernelIndex = data.KernelIndex;
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ScreenDepths, data.ScreenDepths);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ScreenFlatNormals, data.ScreenFlatNormals);
cmd.SetComputeMatrixParam(shader, ShaderIDs._ClipToWorldTransform, data.ClipToWorldTransform);
uint3 groupCount = DivUp(data.ThreadCount, data.ThreadGroupSize);
cmd.DispatchCompute(shader, kernelIndex, (int)groupCount.x, (int)groupCount.y, 1);
}
static void AllocatePatches(PatchAllocationPassData data, ComputeGraphContext cgContext)
{
var cmd = cgContext.cmd;
var shader = data.Shader;
var kernelIndex = data.KernelIndex;
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._CurrentFullResScreenDepths, data.ScreenDepths);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._CurrentFullResScreenFlatNormals, data.ScreenFlatNormals);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._CurrentFullResScreenMotionVectors, data.ScreenMotionVectors);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._PreviousLowResScreenIrradiancesL0, data.LowResScreenIrradiancesL0);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._PreviousLowResScreenIrradiancesL10, data.LowResScreenIrradiancesL10);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._PreviousLowResScreenIrradiancesL11, data.LowResScreenIrradiancesL11);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._PreviousLowResScreenIrradiancesL12, data.LowResScreenIrradiancesL12);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._PreviousLowResScreenNdcDepths, data.LowResScreenNdcDepths);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._CellAllocationMarks, data.CellAllocationMarks);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._CellPatchIndices, data.CellPatchIndices);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._RingConfigBuffer, data.RingConfigBuffer);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchIrradiances0, data.PatchIrradiances0);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchIrradiances1, data.PatchIrradiances1);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchGeometries, data.PatchGeometries);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchCellIndices, data.PatchCellIndices);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchCounterSets, data.PatchCounterSets);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._CascadeOffsets, data.CascadeOffsets);
cmd.SetComputeIntParam(shader, ShaderIDs._FrameIdx, (int)data.FrameIdx);
cmd.SetComputeIntParam(shader, ShaderIDs._GridSize, (int)data.GridSize);
cmd.SetComputeIntParam(shader, ShaderIDs._CascadeCount, (int)data.CascadeCount);
cmd.SetComputeIntParam(shader, ShaderIDs._RingConfigOffset, (int)data.RingConfigOffset);
cmd.SetComputeIntParams(shader, ShaderIDs._FullResPixelOffset, (int)data.FullResPixelOffset.x, (int)data.FullResPixelOffset.y);
cmd.SetComputeIntParams(shader, ShaderIDs._LowResScreenSize, (int)data.LowResScreenSize.x, (int)data.LowResScreenSize.y);
cmd.SetComputeIntParam(shader, ShaderIDs._UseMotionVectorSeeding, data.UseMotionVectorSeeding ? 1 : 0);
cmd.SetComputeFloatParam(shader, ShaderIDs._VoxelMinSize, data.VoxelMinSize);
cmd.SetComputeMatrixParam(shader, ShaderIDs._CurrentClipToWorldTransform, data.CurrentClipToWorldTransform);
cmd.SetComputeMatrixParam(shader, ShaderIDs._PreviousClipToWorldTransform, data.PreviousClipToWorldTransform);
cmd.SetComputeVectorParam(shader, ShaderIDs._GridTargetPos, data.GridTargetPos);
uint3 groupCount = DivUp(data.ThreadCount, data.ThreadGroupSize);
cmd.DispatchCompute(shader, kernelIndex, (int)groupCount.x, (int)groupCount.y, 1);
}
static void RenderDebug(DebugPassData data, ComputeGraphContext cgContext)
{
var cmd = cgContext.cmd;
var shader = data.Shader;
var kernelIndex = data.KernelIndex;
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._Result, data.ScreenIrradiances);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ScreenDepths, data.ScreenDepths);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ScreenShadedNormals, data.ScreenShadedNormals);
cmd.SetComputeTextureParam(shader, kernelIndex, ShaderIDs._ScreenFlatNormals, data.ScreenFlatNormals);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._CellPatchIndices, data.CellPatchIndices);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._RingConfigBuffer, data.RingConfigBuffer);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchIrradiances, data.PatchIrradiances);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchGeometries, data.PatchGeometries);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._CascadeOffsets, data.CascadeOffsets);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchCellIndices, data.PatchCellIndices);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchStatistics, data.PatchStatistics);
cmd.SetComputeBufferParam(shader, kernelIndex, ShaderIDs._PatchCounterSets, data.PatchCounterSets);
cmd.SetComputeIntParam(shader, ShaderIDs._GridSize, (int)data.GridSize);
cmd.SetComputeIntParam(shader, ShaderIDs._CascadeCount, (int)data.CascadeCount);
cmd.SetComputeIntParam(shader, ShaderIDs._ViewMode, (int)data.ViewMode);
cmd.SetComputeIntParam(shader, ShaderIDs._FrameIdx, (int)data.FrameIndex);
cmd.SetComputeIntParam(shader, ShaderIDs._ShowSamplePosition, data.ShowSamplePosition ? 1 : 0);
cmd.SetComputeFloatParam(shader, ShaderIDs._VoxelMinSize, data.VoxelMinSize);
cmd.SetComputeFloatParam(shader, ShaderIDs._RingConfigOffset, data.RingConfigOffset);
cmd.SetComputeVectorParam(shader, ShaderIDs._GridTargetPos, data.GridTargetPos);
cmd.SetComputeMatrixParam(shader, ShaderIDs._ClipToWorldTransform, data.ClipToWorldTransform);
uint3 groupCount = DivUp(data.ThreadCount, data.ThreadGroupSize);
cmd.DispatchCompute(shader, kernelIndex, (int)groupCount.x, (int)groupCount.y, 1);
}
private static uint3 DivUp(uint3 x, uint3 y) => (x + y - 1) / y;
private static uint2 DivUp(uint2 x, uint2 y) => (x + y - 1) / y;
}
class WorldAdapter : IDisposable
{
// This dictionary maps from Unity EntityID for MeshRenderer or Terrain, to corresponding InstanceHandle for accessing World.
private readonly Dictionary<EntityId, InstanceHandle> _entityIDToWorldInstanceHandles = new();
// Same as above but for Lights
private readonly Dictionary<EntityId, LightHandle> _entityIDToWorldLightHandles = new();
// Same as above but for Materials
private Dictionary<EntityId, MaterialHandle> _entityIDToWorldMaterialHandles = new();
// We also keep track of associated material descriptors, so we can free temporary temporary textures when a material is removed
private Dictionary<EntityId, MaterialPool.MaterialDescriptor> _entityIDToWorldMaterialDescriptors = new();
private MaterialPool.MaterialDescriptor _fallbackMaterialDescriptor;
private MaterialHandle _fallbackMaterialHandle;
public WorldAdapter(SurfaceCacheWorld world, Material fallbackMaterial)
{
_fallbackMaterialDescriptor = MaterialPool.ConvertUnityMaterialToMaterialDescriptor(fallbackMaterial);
_fallbackMaterialHandle = world.AddMaterial(in _fallbackMaterialDescriptor, UVChannel.UV0);
_entityIDToWorldMaterialHandles.Add(fallbackMaterial.GetEntityId(), _fallbackMaterialHandle);
_entityIDToWorldMaterialDescriptors.Add(fallbackMaterial.GetEntityId(), _fallbackMaterialDescriptor);
}
public void UpdateMaterials(SurfaceCacheWorld world, List<Material> addedMaterials, List<EntityId> removedMaterials, List<Material> changedMaterials)
{
UpdateMaterials(world, _entityIDToWorldMaterialHandles, _entityIDToWorldMaterialDescriptors, addedMaterials, removedMaterials, changedMaterials);
}
private static void UpdateMaterials(SurfaceCacheWorld world, Dictionary<EntityId, MaterialHandle> entityIDToHandle, Dictionary<EntityId, MaterialPool.MaterialDescriptor> entityIDToDescriptor, List<Material> addedMaterials, List<EntityId> removedMaterials, List<Material> changedMaterials)
{
static void DeleteTemporaryTextures(ref MaterialPool.MaterialDescriptor desc)
{
CoreUtils.Destroy(desc.Albedo);
CoreUtils.Destroy(desc.Emission);
CoreUtils.Destroy(desc.Transmission);
}
foreach (var entityID in removedMaterials)
{
// Clean up temporary textures in the descriptor
Debug.Assert(entityIDToDescriptor.ContainsKey(entityID));
var descriptor = entityIDToDescriptor[entityID];
DeleteTemporaryTextures(ref descriptor);
entityIDToDescriptor.Remove(entityID);
// Remove the material from the world
Debug.Assert(entityIDToHandle.ContainsKey(entityID));
world.RemoveMaterial(entityIDToHandle[entityID]);
entityIDToHandle.Remove(entityID);
}
foreach (var material in addedMaterials)
{
// Add material to the world
var descriptor = MaterialPool.ConvertUnityMaterialToMaterialDescriptor(material);
var handle = world.AddMaterial(in descriptor, UVChannel.UV0);
entityIDToHandle.Add(material.GetEntityId(), handle);
// Keep track of the descriptor
entityIDToDescriptor.Add(material.GetEntityId(), descriptor);
}
foreach (var material in changedMaterials)
{
// Clean up temporary textures in the old descriptor
Debug.Assert(entityIDToDescriptor.ContainsKey(material.GetEntityId()));
var oldDescriptor = entityIDToDescriptor[material.GetEntityId()];
DeleteTemporaryTextures(ref oldDescriptor);
// Update the material in the world using the new descriptor
Debug.Assert(entityIDToHandle.ContainsKey(material.GetEntityId()));
var newDescriptor = MaterialPool.ConvertUnityMaterialToMaterialDescriptor(material);
world.UpdateMaterial(entityIDToHandle[material.GetEntityId()], in newDescriptor, UVChannel.UV0);
entityIDToDescriptor[material.GetEntityId()] = newDescriptor;
}
}
internal void UpdateLights(SurfaceCacheWorld world, List<Light> addedLights, List<EntityId> removedLights,
List<Light> changedLights, bool multiplyPunctualLightIntensityByPI)
{
UpdateLights(world, _entityIDToWorldLightHandles, addedLights, removedLights, changedLights, multiplyPunctualLightIntensityByPI);
}
private static void UpdateLights(
SurfaceCacheWorld world,
Dictionary<EntityId, LightHandle> entityIDToHandle, List<Light> addedLights, List<EntityId> removedLights,
List<Light> changedLights,
bool multiplyPunctualLightIntensityByPI)
{
// Remove deleted lights
LightHandle[] handlesToRemove = new LightHandle[removedLights.Count];
for (int i = 0; i < removedLights.Count; i++)
{
int lightEntityID = removedLights[i];
handlesToRemove[i] = entityIDToHandle[lightEntityID];
entityIDToHandle.Remove(lightEntityID);
}
world.RemoveLights(handlesToRemove);
// Add new lights
var lightDescriptors = ConvertUnityLightsToLightDescriptors(addedLights.ToArray(), multiplyPunctualLightIntensityByPI);
LightHandle[] addedHandles = world.AddLights(lightDescriptors);
for (int i = 0; i < addedLights.Count; ++i)
entityIDToHandle.Add(addedLights[i].GetEntityId(), addedHandles[i]);
// Update changed lights
LightHandle[] handlesToUpdate = new LightHandle[changedLights.Count];
for (int i = 0; i < changedLights.Count; i++)
handlesToUpdate[i] = entityIDToHandle[changedLights[i].GetEntityId()];
world.UpdateLights(handlesToUpdate, ConvertUnityLightsToLightDescriptors(changedLights.ToArray(), multiplyPunctualLightIntensityByPI));
}
internal void UpdateInstances(
SurfaceCacheWorld world,
List<MeshRenderer> addedInstances,
List<InstanceChanges> changedInstances,
List<EntityId> removedInstances,
RenderedGameObjectsFilter renderedGameObjects,
Material fallbackMaterial)
{
UpdateInstances(world, _entityIDToWorldInstanceHandles, _entityIDToWorldMaterialHandles, addedInstances, changedInstances, removedInstances, renderedGameObjects, fallbackMaterial);
}
private static void UpdateInstances(
SurfaceCacheWorld world,
Dictionary<EntityId, InstanceHandle> entityIDToInstanceHandle,
Dictionary<EntityId, MaterialHandle> entityIDToMaterialHandle,
List<MeshRenderer> addedInstances,
List<InstanceChanges> changedInstances,
List<EntityId> removedInstances,
RenderedGameObjectsFilter renderedGameObjects,
Material fallbackMaterial)
{
foreach (var meshRendererEntityID in removedInstances)
{
Debug.Assert(entityIDToInstanceHandle.ContainsKey(meshRendererEntityID));
world.RemoveInstance(entityIDToInstanceHandle[meshRendererEntityID]);
entityIDToInstanceHandle.Remove(meshRendererEntityID);
}
foreach (var meshRenderer in addedInstances)
{
Debug.Assert(!meshRenderer.isPartOfStaticBatch);
var mesh = meshRenderer.GetComponent<MeshFilter>().sharedMesh;
var localToWorldMatrix = meshRenderer.transform.localToWorldMatrix;
var materials = Util.GetMaterials(meshRenderer);
var materialHandles = new MaterialHandle[materials.Length];
for (int i = 0; i < materials.Length; i++)
{
var matEntityId = materials[i] == null ? fallbackMaterial.GetEntityId() : materials[i].GetEntityId();
materialHandles[i] = entityIDToMaterialHandle[matEntityId];
}
uint[] masks = new uint[materials.Length];
for (int i = 0; i < masks.Length; i++)
{
masks[i] = materials[i] != null ? 1u : 0u;
}