Skip to content

Commit 44d490e

Browse files
urasmusEvergreen
authored andcommitted
Use current Defrag Count in Surface Cache
1 parent 252da96 commit 44d490e

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

Packages/com.unity.render-pipelines.core/Runtime/Lighting/SurfaceCache/SurfaceCache.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ internal class SurfaceCacheResourceSet
194194
internal int TemporalFilteringKernel;
195195
internal uint3 TemporalFilteringKernelGroupSize;
196196

197-
internal readonly uint SubGroupSize;
197+
internal readonly uint ComputeSubGroupSize;
198198

199-
internal SurfaceCacheResourceSet(uint subGroupSize)
199+
internal SurfaceCacheResourceSet(uint computeSubGroupSize)
200200
{
201-
SubGroupSize = subGroupSize;
201+
ComputeSubGroupSize = computeSubGroupSize;
202202
}
203203

204204
internal bool LoadFromRenderPipelineResources(RayTracingContext rtContext)
@@ -223,9 +223,9 @@ internal bool LoadFromRenderPipelineResources(RayTracingContext rtContext)
223223
TemporalFilteringKernel = TemporalFilteringShader.FindKernel("FilterTemporally");
224224
TemporalFilteringShader.GetKernelThreadGroupSizes(TemporalFilteringKernel, out TemporalFilteringKernelGroupSize.x, out TemporalFilteringKernelGroupSize.y, out TemporalFilteringKernelGroupSize.z);
225225

226-
Debug.Assert(SubGroupSize == 8 || SubGroupSize == 16 || SubGroupSize == 32 || SubGroupSize == 48 || SubGroupSize == 64);
226+
Debug.Assert(ComputeSubGroupSize == 8 || ComputeSubGroupSize == 16 || ComputeSubGroupSize == 32 || ComputeSubGroupSize == 48 || ComputeSubGroupSize == 64);
227227
DefragShader = rpResources.defragShader;
228-
var defragKeyword = "SUB_GROUP_SIZE_" + SubGroupSize;
228+
var defragKeyword = "SUB_GROUP_SIZE_" + ComputeSubGroupSize;
229229
DefragShader.EnableKeyword(defragKeyword);
230230
DefragKernel = DefragShader.FindKernel("Defrag");
231231
DefragShader.GetKernelThreadGroupSizes(DefragKernel, out DefragKernelGroupSize.x, out DefragKernelGroupSize.y, out DefragKernelGroupSize.z);
@@ -256,6 +256,7 @@ internal bool LoadFromRenderPipelineResources(RayTracingContext rtContext)
256256
internal class SurfaceCache : IDisposable
257257
{
258258
public const uint CascadeMax = 8;
259+
259260
private readonly GraphicsBuffer _punctualLightSamples;
260261
private readonly SurfaceCachePatchList _patches;
261262
private readonly SurfaceCacheVolume _volume;
@@ -267,7 +268,8 @@ internal class SurfaceCache : IDisposable
267268
private SurfaceCachePatchFilteringParameterSet _patchFilteringParams;
268269

269270
private float _shortHysteresis;
270-
readonly private uint _defragCount;
271+
private uint _defragCount = 1;
272+
private uint _defragOffset = 0;
271273
readonly private float _albedoBoost = 1.0f;
272274

273275
public GraphicsBuffer PunctualLightSamples => _punctualLightSamples;
@@ -323,7 +325,7 @@ private class DefragPassData
323325
internal GraphicsBuffer PatchStatistics;
324326
internal GraphicsBuffer CellPatchIndices;
325327
internal uint RingConfigStartFlipflop;
326-
internal uint EvenIterationPatchOffset;
328+
internal uint ComputeSubGroupSize;
327329
internal uint OddIterationPatchOffset;
328330
}
329331

@@ -442,7 +444,6 @@ internal static class ShaderIDs
442444

443445
public SurfaceCache(
444446
SurfaceCacheResourceSet resources,
445-
uint defragCount,
446447
SurfaceCacheVolumeParameterSet volParams)
447448
{
448449
Debug.Assert(volParams.CascadeCount != 0);
@@ -457,23 +458,26 @@ public SurfaceCache(
457458
_ringConfig = new SurfaceCacheRingConfig();
458459
_patches = new SurfaceCachePatchList(patchCapacity);
459460
_punctualLightSamples = new GraphicsBuffer(GraphicsBuffer.Target.Structured, (int)punctualLightSampleCount, sizeof(float) * 17);
460-
461-
_defragCount = defragCount;
462461
}
463462

464463
public void SetEstimationParams(SurfaceCacheEstimationParameterSet estimationParams)
465464
{
466465
_estimationParams = estimationParams;
467466
}
468467

468+
public void SetDefragCount(uint count)
469+
{
470+
_defragCount = count;
471+
}
472+
469473
public void SetPatchFilteringParams(SurfaceCachePatchFilteringParameterSet patchFilteringParams)
470474
{
471475
Debug.Assert(0.0f <= patchFilteringParams.TemporalSmoothing && patchFilteringParams.TemporalSmoothing <= 1.0f);
472476
_patchFilteringParams = patchFilteringParams;
473477
_shortHysteresis = Mathf.Lerp(0.75f, 0.95f, patchFilteringParams.TemporalSmoothing);
474478
}
475479

476-
public void UpdateVolumeSize(float size)
480+
public void SetVolumeSize(float size)
477481
{
478482
_volume.VoxelMinSize = size / (_volume.SpatialResolution * (float)(1u << (int)(_volume.CascadeCount - 1u)));
479483
}
@@ -495,7 +499,7 @@ private void RecordDefragmentation(RenderGraph renderGraph, uint frameIdx)
495499
{
496500
using (var builder = renderGraph.AddComputePass("Surface Cache Defrag", out DefragPassData passData))
497501
{
498-
passData.IterationOffset = frameIdx * _defragCount;
502+
passData.IterationOffset = _defragOffset;
499503
passData.IterationCount = _defragCount;
500504
passData.Shader = _resources.DefragShader;
501505
passData.Keyword = _resources.DefragKeyword;
@@ -510,15 +514,16 @@ private void RecordDefragmentation(RenderGraph renderGraph, uint frameIdx)
510514
passData.PatchGeometries = Patches.Geometries;
511515
passData.PatchStatistics = Patches.Statistics;
512516
passData.CellPatchIndices = Volume.CellPatchIndices;
513-
passData.EvenIterationPatchOffset = 0;
514-
passData.OddIterationPatchOffset = _resources.SubGroupSize / 2;
517+
passData.ComputeSubGroupSize = _resources.ComputeSubGroupSize;
515518

516519
builder.AllowGlobalStateModification(true); // Set to ensure ordering.
517520
builder.SetRenderFunc((DefragPassData data, ComputeGraphContext cgContext) => Defrag(data, cgContext));
518521

519522
if (_defragCount % 2 == 1)
520523
RingConfig.Flip();
521524
}
525+
526+
_defragOffset += _defragCount;
522527
}
523528

524529
private void RecordEviction(RenderGraph renderGraph, uint frameIdx)
@@ -770,7 +775,7 @@ static void Defrag(DefragPassData data, ComputeGraphContext cgContext)
770775
{
771776
uint readOffset = SurfaceCacheRingConfig.GetOffsetA(flipflop);
772777
uint writeOffset = SurfaceCacheRingConfig.GetOffsetB(flipflop);
773-
uint patchOffset = iterationIndex % 2 == 0 ? data.EvenIterationPatchOffset : data.OddIterationPatchOffset;
778+
uint patchOffset = iterationIndex % 2 * (data.ComputeSubGroupSize / 2);
774779

775780
cmd.SetComputeIntParam(shader, ShaderIDs._RingConfigReadOffset, (int)readOffset);
776781
cmd.SetComputeIntParam(shader, ShaderIDs._RingConfigWriteOffset, (int)writeOffset);

Packages/com.unity.render-pipelines.universal/Runtime/RendererFeatures/SurfaceCacheGIRendererFeature/SurfaceCacheGIRendererFeature.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ private class ScreenIrradianceUpsamplingPassData
356356

357357
// Stored for runtime cache recreation when resolution or cascade count changes
358358
private readonly Rendering.SurfaceCacheResourceSet _coreResources;
359-
private uint _defragCount;
360359

361360
public SurfaceCachePass(
362361
RayTracingContext rtContext,
@@ -371,7 +370,6 @@ public SurfaceCachePass(
371370
bool debugEnabled,
372371
DebugViewMode_ debugViewMode,
373372
bool debugShowSamplePosition,
374-
uint defragCount,
375373
SurfaceCacheVolumeParameterSet volParams)
376374
{
377375
Debug.Assert(volParams.CascadeCount != 0);
@@ -403,9 +401,8 @@ public SurfaceCachePass(
403401
_debugShowSamplePosition = debugShowSamplePosition;
404402

405403
_coreResources = resourceSet;
406-
_defragCount = defragCount;
407404

408-
_cache = new SurfaceCache(resourceSet, defragCount, volParams);
405+
_cache = new SurfaceCache(resourceSet, volParams);
409406
_sceneTracker = new SceneUpdatesTracker();
410407

411408
_world = new SurfaceCacheWorld();
@@ -521,7 +518,6 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
521518
var stack = VolumeManager.instance.stack;
522519
var volume = stack.GetComponent<SurfaceCacheGIVolumeOverride>();
523520
var volumeParams = GetVolumeParametersOrDefaults(volume);
524-
_defragCount = volumeParams.DefragCount;
525521

526522
// Detect structural changes that require buffer reallocation.
527523
if (volumeParams.VolumeResolution != _cache.Volume.SpatialResolution || volumeParams.VolumeCascadeCount != _cache.Volume.CascadeCount)
@@ -537,13 +533,14 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
537533
Size = volumeParams.VolumeSize,
538534
CascadeCount = newCascadeCount
539535
};
540-
_cache = new SurfaceCache(_coreResources, _defragCount, newVolParams);
536+
_cache = new SurfaceCache(_coreResources, newVolParams);
541537
_frameIdx = 0;
542538
}
543539

544540
_cache.SetEstimationParams(volumeParams.EstimationParams);
545541
_cache.SetPatchFilteringParams(volumeParams.PatchFilteringParams);
546-
_cache.UpdateVolumeSize(volumeParams.VolumeSize);
542+
_cache.SetVolumeSize(volumeParams.VolumeSize);
543+
_cache.SetDefragCount(volumeParams.DefragCount);
547544

548545
bool useMotionVectorPatchSeeding = UseMotionVectorPatchSeeding(cameraData.cameraType);
549546

@@ -1037,7 +1034,6 @@ public override void Create()
10371034
_parameterSet.DebugEnabled,
10381035
_parameterSet.DebugViewMode,
10391036
_parameterSet.DebugShowSamplePosition,
1040-
defragCount: DEFAULT_DEFRAG_COUNT,
10411037
volParams);
10421038

10431039
_pass.renderPassEvent = RenderPassEvent.AfterRenderingPrePasses + 1;

0 commit comments

Comments
 (0)