Skip to content

Commit 0f1d475

Browse files
urasmusEvergreen
authored andcommitted
Address uninitialized memory issue in Surface Cache
1 parent a862a7c commit 0f1d475

File tree

14 files changed

+66
-82
lines changed

14 files changed

+66
-82
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
RWStructuredBuffer<uint> _RingConfigBuffer;
3434
RWStructuredBuffer<uint> _CellPatchIndices;
3535
RWStructuredBuffer<uint> _PatchCellIndices;
36-
RWStructuredBuffer<PatchUtil::PatchCounterSet> _PatchCounterSets;
3736
RWStructuredBuffer<SphericalHarmonics::RGBL1> _PatchIrradiances0;
3837
RWStructuredBuffer<SphericalHarmonics::RGBL1> _PatchIrradiances1;
3938
RWStructuredBuffer<PatchUtil::PatchGeometry> _PatchGeometries;
@@ -80,7 +79,6 @@ void Defrag(Threading::Group group)
8079

8180
const uint newPatchIdx = (patchIdx + rightReclaimableCount) % patchCapacity;
8281
_PatchCellIndices[newPatchIdx] = oldPatchCellIndex;
83-
_PatchCounterSets[newPatchIdx] = _PatchCounterSets[patchIdx];
8482
_PatchIrradiances0[newPatchIdx] = _PatchIrradiances0[patchIdx];
8583
_PatchIrradiances1[newPatchIdx] = _PatchIrradiances1[patchIdx];
8684
_PatchGeometries[newPatchIdx] = _PatchGeometries[patchIdx];

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@
22
#include "PatchUtil.hlsl"
33
#include "TemporalFiltering.hlsl"
44

5-
void ProcessAndStoreRadianceSample(RWStructuredBuffer<SphericalHarmonics::RGBL1> patchIrradiances, RWStructuredBuffer<PatchUtil::PatchStatisticsSet> patchStatistics, RWStructuredBuffer<PatchUtil::PatchCounterSet> patchCounterSets, uint patchIdx, SphericalHarmonics::RGBL1 radianceSample, float shortHysteresis)
5+
void ProcessAndStoreRadianceSample(RWStructuredBuffer<SphericalHarmonics::RGBL1> patchIrradiances, RWStructuredBuffer<PatchUtil::PatchStatisticsSet> patchStatistics, uint patchIdx, SphericalHarmonics::RGBL1 radianceSample, float shortHysteresis)
66
{
77
SphericalHarmonics::CosineConvolve(radianceSample);
88

99
PatchUtil::PatchStatisticsSet oldStats = patchStatistics[patchIdx];
10+
const uint oldUpdateCount = PatchUtil::GetUpdateCount(oldStats.patchCounters);
11+
const uint newUpdateCount = min(oldUpdateCount + 1, PatchUtil::updateMax);
1012

1113
const SphericalHarmonics::RGBL1 oldIrradiance = patchIrradiances[patchIdx];
12-
const float3 newL0ShortIrradiance = lerp(radianceSample.l0, oldStats.mean, shortHysteresis);
13-
const float3 varianceSample = (radianceSample.l0 - newL0ShortIrradiance) * (radianceSample.l0 - oldStats.mean);
14-
const float3 newVariance = lerp(varianceSample, oldStats.variance, shortHysteresis);
1514

16-
const PatchUtil::PatchCounterSet oldCounterSet = patchCounterSets[patchIdx];
17-
const uint oldUpdateCount = PatchUtil::GetUpdateCount(patchCounterSets[patchIdx]);
18-
const uint newUpdateCount = min(oldUpdateCount + 1, PatchUtil::updateMax);
15+
float shortIrradianceUpdateWeight;
16+
if (oldUpdateCount == 0)
17+
shortIrradianceUpdateWeight = 0;
18+
else
19+
shortIrradianceUpdateWeight = min(1.0f - rcp(oldUpdateCount), shortHysteresis);
1920

20-
PatchUtil::PatchCounterSet newCounterSet = oldCounterSet;
21-
PatchUtil::SetUpdateCount(newCounterSet, newUpdateCount);
21+
const float3 newL0ShortIrradiance = lerp(radianceSample.l0, oldStats.mean, shortIrradianceUpdateWeight);
22+
const float3 varianceSample = (radianceSample.l0 - newL0ShortIrradiance) * (radianceSample.l0 - oldStats.mean);
23+
const float3 newVariance = lerp(varianceSample, oldStats.variance, shortHysteresis);
2224

2325
SphericalHarmonics::RGBL1 output = FilterTemporallyVarianceGuided(shortHysteresis, newUpdateCount, newVariance, newL0ShortIrradiance, radianceSample, oldIrradiance);
2426

2527
patchIrradiances[patchIdx] = output;
26-
if (!PatchUtil::IsEqual(oldCounterSet, newCounterSet))
27-
patchCounterSets[patchIdx] = newCounterSet;
2828

2929
PatchUtil::PatchStatisticsSet newStats;
3030
newStats.mean = newL0ShortIrradiance;
3131
newStats.variance = newVariance;
32+
newStats.patchCounters = oldStats.patchCounters;
33+
PatchUtil::SetUpdateCount(newStats.patchCounters, newUpdateCount);
3234
patchStatistics[patchIdx] = newStats;
3335
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "PatchUtil.hlsl"
66

77
StructuredBuffer<uint> _RingConfigBuffer;
8-
StructuredBuffer<PatchUtil::PatchCounterSet> _PatchCounterSets;
8+
StructuredBuffer<PatchUtil::PatchStatisticsSet> _PatchStatistics;
99
RWStructuredBuffer<uint> _CellAllocationMarks;
1010
RWStructuredBuffer<uint> _CellPatchIndices;
1111
RWStructuredBuffer<uint> _PatchCellIndices;
@@ -29,7 +29,7 @@ void Evict(uint patchIdx : SV_DispatchThreadID)
2929
if (cellIdx == PatchUtil::invalidCellIndex)
3030
return;
3131

32-
const PatchUtil::PatchCounterSet counterSet = _PatchCounterSets[patchIdx];
32+
const PatchUtil::PatchCounterSet counterSet = _PatchStatistics[patchIdx].patchCounters;
3333
const uint lastAccessFrameIdx = PatchUtil::GetLastAccessFrame(counterSet);
3434

3535
// Here we take into account that last frame access index is in [0, 2^16-1].

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ namespace PatchUtil
3838
float3 normal;
3939
};
4040

41-
struct PatchStatisticsSet
41+
struct PatchCounterSet
4242
{
43-
float3 mean;
44-
float3 variance;
43+
uint data;
4544
};
4645

47-
struct PatchCounterSet
46+
struct PatchStatisticsSet
4847
{
49-
uint data;
48+
float3 mean;
49+
float3 variance;
50+
PatchCounterSet patchCounters;
5051
};
5152

5253
void Reset(inout PatchCounterSet set)
@@ -79,11 +80,11 @@ namespace PatchUtil
7980
return a.data == b.data;
8081
}
8182

82-
void WriteLastFrameAccess(RWStructuredBuffer<PatchUtil::PatchCounterSet> counterSets, uint patchIdx, uint frameIdx)
83+
void WriteLastFrameAccess(RWStructuredBuffer<PatchUtil::PatchStatisticsSet> statisticsSets, uint patchIdx, uint frameIdx)
8384
{
84-
PatchCounterSet counterSet = counterSets[patchIdx];
85+
PatchCounterSet counterSet = statisticsSets[patchIdx].patchCounters;
8586
SetLastAccessFrame(counterSet, frameIdx);
86-
counterSets[patchIdx] = counterSet;
87+
statisticsSets[patchIdx].patchCounters = counterSet;
8788
}
8889

8990
float GetVoxelSize(float voxelMinSize, uint cascadeIdx)
@@ -306,12 +307,12 @@ namespace PatchUtil
306307
}
307308
}
308309

309-
uint FindPatchIndexAndUpdateLastAccess(float3 volumeTargetPos, StructuredBuffer<uint> cellPatchIndices, uint spatialResolution, StructuredBuffer<int3> cascadeOffsets, RWStructuredBuffer<PatchUtil::PatchCounterSet> patchCounterSets, uint cascadeCount, float voxelMinSize, float3 worldPosition, float3 worldNormal, uint frameIdx)
310+
uint FindPatchIndexAndUpdateLastAccess(float3 volumeTargetPos, StructuredBuffer<uint> cellPatchIndices, uint spatialResolution, StructuredBuffer<int3> cascadeOffsets, RWStructuredBuffer<PatchUtil::PatchStatisticsSet> patchStatisticSets, uint cascadeCount, float voxelMinSize, float3 worldPosition, float3 worldNormal, uint frameIdx)
310311
{
311312
const uint patchIdx = FindPatchIndex(volumeTargetPos, cellPatchIndices, spatialResolution, cascadeOffsets, cascadeCount, voxelMinSize,worldPosition, worldNormal);
312313
if (patchIdx != invalidPatchIndex)
313314
{
314-
WriteLastFrameAccess(patchCounterSets, patchIdx, frameIdx);
315+
WriteLastFrameAccess(patchStatisticSets, patchIdx, frameIdx);
315316
}
316317
return patchIdx;
317318
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ StructuredBuffer<Realization> _PatchRealizations;
1212
StructuredBuffer<PatchUtil::PatchGeometry> _PatchGeometries;
1313
RWStructuredBuffer<SphericalHarmonics::RGBL1> _PatchIrradiances;
1414
RWStructuredBuffer<PatchUtil::PatchStatisticsSet> _PatchStatistics;
15-
RWStructuredBuffer<PatchUtil::PatchCounterSet> _PatchCounterSets;
1615

1716
uint _RingConfigOffset;
1817
float _ShortHysteresis;
@@ -44,6 +43,6 @@ void Estimate(uint patchIdx : SV_DispatchThreadID)
4443
estimate.l1s[1] = realization.sample.radiance * SphericalHarmonics::y1Constant * rayDirection.z;
4544
estimate.l1s[2] = realization.sample.radiance * SphericalHarmonics::y1Constant * rayDirection.x;
4645
SphericalHarmonics::MulMut(estimate, realization.weight);
47-
ProcessAndStoreRadianceSample(_PatchIrradiances, _PatchStatistics, _PatchCounterSets, patchIdx, estimate, _ShortHysteresis);
46+
ProcessAndStoreRadianceSample(_PatchIrradiances, _PatchStatistics, patchIdx, estimate, _ShortHysteresis);
4847
}
4948
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ StructuredBuffer<uint> _RingConfigBuffer;
3535
RWStructuredBuffer<SphericalHarmonics::RGBL1> _PatchIrradiances;
3636
RWStructuredBuffer<PatchUtil::PatchStatisticsSet> _PatchStatistics;
3737
StructuredBuffer<PatchUtil::PatchGeometry> _PatchGeometries;
38-
RWStructuredBuffer<PatchUtil::PatchCounterSet> _PatchCounterSets;
3938
StructuredBuffer<uint> _CellPatchIndices;
4039
StructuredBuffer<int3> _CascadeOffsets;
4140
RWStructuredBuffer<SphericalHarmonics::ScalarL2> _PatchAccumulatedLuminances;
@@ -185,6 +184,6 @@ void Estimate(UnifiedRT::DispatchInfo dispatchInfo)
185184
ProcessAndStoreLuminanceSample(_PatchAccumulatedLuminances, patchIdx, luminanceEstimate, _TargetFunctionUpdateWeight);
186185

187186
const SphericalHarmonics::RGBL1 estimate = EstimateFromSampleAndWeight(radianceSample, reservoir.sample.direction, outputWeight);
188-
ProcessAndStoreRadianceSample(_PatchIrradiances, _PatchStatistics, _PatchCounterSets, patchIdx, estimate, _ShortHysteresis);
187+
ProcessAndStoreRadianceSample(_PatchIrradiances, _PatchStatistics, patchIdx, estimate, _ShortHysteresis);
189188
}
190189
}

0 commit comments

Comments
 (0)