Skip to content

Commit

Permalink
[APV] Graphics/core/apv/fix leak reduction
Browse files Browse the repository at this point in the history
revert the revert pr and fix the failing tests
https://github.cds.internal.unity3d.com/unity/unity/pull/49508
  • Loading branch information
adrien-de-tocqueville authored and Evergreen committed May 17, 2024
1 parent c0c514a commit e9b9d84
Show file tree
Hide file tree
Showing 27 changed files with 660 additions and 450 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal void FromSphericalHarmonicsShaderConstants(ProbeReferenceVolume.Cell ce
if (cellChunkData.skyShadingDirectionIndices.Length != 0)
{
int id = cellChunkData.skyShadingDirectionIndices[index];
var directions = DynamicSkyPrecomputedDirections.GetPrecomputedDirections();
var directions = ProbeVolumeConstantRuntimeResources.GetSkySamplingDirections();
SO_Direction = id == 255 ? Vector3.zero : directions[id];
}
}
Expand Down Expand Up @@ -171,7 +171,6 @@ public void Dispose()
static readonly int _ProbePositionsBuffer = Shader.PropertyToID("_ProbePositionsBuffer");
static readonly int _NeedDilating = Shader.PropertyToID("_NeedDilating");
static readonly int _DilationParameters = Shader.PropertyToID("_DilationParameters");
static readonly int _DilationParameters2 = Shader.PropertyToID("_DilationParameters2");
static readonly int _OutputProbes = Shader.PropertyToID("_OutputProbes");

// Can definitively be optimized later on.
Expand Down Expand Up @@ -296,8 +295,7 @@ static void PerformDilation(ProbeReferenceVolume.Cell cell, ProbeVolumeBakingSet
// There's an upper limit on the number of bricks supported inside a single cell
int probeCount = Mathf.Min(cell.data.probePositions.Length, ushort.MaxValue * ProbeBrickPool.kBrickProbeCountTotal);

cmd.SetComputeVectorParam(dilationShader, _DilationParameters, new Vector4(probeCount, settings.dilationValidityThreshold, settings.dilationDistance, ProbeReferenceVolume.instance.MinBrickSize()));
cmd.SetComputeVectorParam(dilationShader, _DilationParameters2, new Vector4(settings.squaredDistWeighting ? 1 : 0, bakingSet.skyOcclusion ? 1 : 0, bakingSet.skyOcclusionShadingDirection ? 1 : 0, 0));
cmd.SetComputeVectorParam(dilationShader, _DilationParameters, new Vector4(probeCount, settings.dilationValidityThreshold, settings.dilationDistance, settings.squaredDistWeighting ? 1 : 0));

var refVolume = ProbeReferenceVolume.instance;
ProbeReferenceVolume.RuntimeResources rr = refVolume.GetRuntimeResources();
Expand Down Expand Up @@ -330,12 +328,11 @@ static void PerformDilation(ProbeReferenceVolume.Cell cell, ProbeVolumeBakingSet
parameters.samplingNoise = 0;
parameters.weight = 1f;
parameters.leakReductionMode = APVLeakReductionMode.None;
parameters.minValidNormalWeight = 0.0f;
parameters.frameIndexForNoise = 0;
parameters.reflNormalizationLowerClamp = 0.1f;
parameters.reflNormalizationUpperClamp = 1.0f;
parameters.skyOcclusionIntensity = 0.0f;
parameters.skyOcclusionShadingDirection = false;
parameters.skyOcclusionIntensity = bakingSet.skyOcclusion ? 1 : 0;
parameters.skyOcclusionShadingDirection = bakingSet.skyOcclusionShadingDirection ? true : false;
parameters.regionCount = 1;
parameters.regionLayerMasks = 1;
ProbeReferenceVolume.instance.UpdateConstantBuffer(cmd, parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ static void AnalyzeBrickForIndirectionEntries(ref BakingCell cell)
cell.indirectionEntryInfo[i].positionInBricks = cellPosInBricks + new Vector3Int(x, y, z) * indirectionEntrySizeInBricks;
cell.indirectionEntryInfo[i].hasOnlyBiggerBricks = minSubdiv > entrySubdivLevel && touchedBrick;

ProbeBrickIndex.IndirectionEntryUpdateInfo unused = new ProbeBrickIndex.IndirectionEntryUpdateInfo();
int brickCount = ProbeReferenceVolume.instance.GetNumberOfBricksAtSubdiv(cell.indirectionEntryInfo[i], ref unused);
prv.ComputeEntryMinMax(ref cell.indirectionEntryInfo[i], cell.bricks);
int brickCount = ProbeReferenceVolume.GetNumberOfBricksAtSubdiv(cell.indirectionEntryInfo[i]);

totalIndexChunks += Mathf.CeilToInt((float)brickCount / ProbeBrickIndex.kIndexChunkSize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal static NativeArray<uint> EncodeShadingDirection(NativeArray<Vector3> di
var cs = GraphicsSettings.GetRenderPipelineSettings<ProbeVolumeBakingResources>().skyOcclusionCS;
int kernel = cs.FindKernel("EncodeShadingDirection");

DynamicSkyPrecomputedDirections.Initialize();
ProbeVolumeConstantRuntimeResources.Initialize();
var precomputedShadingDirections = ProbeReferenceVolume.instance.GetRuntimeResources().SkyPrecomputedDirections;

int probeCount = directions.Length;
Expand Down Expand Up @@ -93,7 +93,7 @@ internal static NativeArray<uint> EncodeShadingDirection(NativeArray<Vector3> di

internal static uint EncodeSkyShadingDirection(Vector3 direction)
{
var precomputedDirections = DynamicSkyPrecomputedDirections.GetPrecomputedDirections();
var precomputedDirections = ProbeVolumeConstantRuntimeResources.GetSkySamplingDirections();

uint indexMax = 255;
float bestDot = -10.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static class Styles
// Probe Invalidity section
public static readonly GUIContent resetDilation = new GUIContent("Reset Dilation Settings");
public static readonly GUIContent resetVirtualOffset = new GUIContent("Reset Virtual Offset Settings");
public static readonly GUIContent renderingLayerMasks = new GUIContent("Rendering Layer Masks", "When enabled, geometry in a Rendering Layer will only receive lighting from probes which see Rendering Layers in the same Rendering Layer Mask. This can be used to prevent leaking across boundaries.\nGeometry not belonging to a Rendering Layer Mask will continue to sample all probes.");
public static readonly GUIContent renderingLayerMasks = new GUIContent("Rendering Layer Masks", "When enabled, geometry in a Rendering Layer will only receive lighting from probes which see Rendering Layers in the same Rendering Layer Mask. This can be used to prevent leaking across boundaries.\nGeometry not belonging to a Rendering Layer Mask will continue to sample all probes. Requires Leak Reduction Mode to be enabled.");
public static readonly string maskTooltip = "The Rendering Layers for this mask.";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ CBUFFER_END
#define _ProbeCount (uint)_DilationParameters.x
#define _ValidityThreshold _DilationParameters.y
#define _SearchRadius _DilationParameters.z
#define _MinBrickSizeForDilation _DilationParameters.w

#define _SquaredDistanceWeight (_DilationParameters2.x > 0)
#define _EnableSkyOcclusion (_DilationParameters2.y > 0)
#define _EnableSkyDirection (_DilationParameters2.z > 0)
#define _SquaredDistanceWeight (_DilationParameters.w > 0)

StructuredBuffer<int> _NeedDilating;
StructuredBuffer<float3> _ProbePositionsBuffer;
Expand Down Expand Up @@ -60,17 +56,17 @@ void AddProbeSample(APVResources apvRes, float3 uvw, inout DilatedProbe probe, f
}

// Sky occlusion data
if (_EnableSkyOcclusion)
if (_APVSkyOcclusionWeight > 0)
{
float4 SO_L0L1 = SAMPLE_TEXTURE3D_LOD(apvRes.SkyOcclusionL0L1, s_linear_clamp_sampler, uvw, 0).rgba * weight;

if (SO_L0L1.x >= 0.0001f)
{
probe.SO_L0L1 += SO_L0L1 * weight;

if (_EnableSkyDirection)
if (_APVSkyDirectionWeight > 0)
{
int3 texCoord = uvw * _PoolDim - 0.5f; // No interpolation for sky shading indices
int3 texCoord = uvw * _APVPoolDim - 0.5f; // No interpolation for sky shading indices
uint index = LOAD_TEXTURE3D(apvRes.SkyShadingDirectionIndices, texCoord).x * 255.0;

probe.SO_Direction = index == 255 ? float3(0, 0, 0) : apvRes.SkyPrecomputedDirections[index].rgb * weight;
Expand Down Expand Up @@ -103,18 +99,18 @@ void DilateCell(uint3 id : SV_DispatchThreadID)

if (_NeedDilating[probeIdx] > 0)
{
float3 centralPosition = _ProbePositionsBuffer[probeIdx] - _WorldOffset;
float3 centralPosition = _ProbePositionsBuffer[probeIdx] - _APVWorldOffset;
DilatedProbe probe = (DilatedProbe)0;

float3 uvw;
uint subdiv;
float shWeight = 0, soWeight = 0;
float3 uvwDelta = rcp(_PoolDim);
float3 uvwDelta = rcp(_APVPoolDim);

float3 biasedPosWS;
if (TryToGetPoolUVWAndSubdiv(FillAPVResources(), centralPosition, 0, 0, uvw, subdiv, biasedPosWS))
{
float stepSize = _MinBrickSizeForDilation / 3.0f;
float stepSize = _APVMinBrickSize / 3.0f;

// Inflate search radius a bit.
float radius = 1.5f * _SearchRadius;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ sealed class ProbeVolumesOptionsEditor : VolumeComponentEditor
SerializedDataParameter m_ScaleBiasMinProbeDistance;
SerializedDataParameter m_SamplingNoise;
SerializedDataParameter m_LeakReductionMode;
SerializedDataParameter m_MinValidDotProdValue;
SerializedDataParameter m_AnimateNoise;
SerializedDataParameter m_OcclusionOnlyNormalization;

Expand All @@ -26,7 +25,6 @@ public override void OnEnable()
m_ScaleBiasMinProbeDistance = Unpack(o.Find(x => x.scaleBiasWithMinProbeDistance));
m_SamplingNoise = Unpack(o.Find(x => x.samplingNoise));
m_LeakReductionMode = Unpack(o.Find(x => x.leakReductionMode));
m_MinValidDotProdValue = Unpack(o.Find(x => x.minValidDotProductValue));
m_AnimateNoise = Unpack(o.Find(x => x.animateSamplingNoise));
m_OcclusionOnlyNormalization = Unpack(o.Find(x => x.occlusionOnlyReflectionNormalization));

Expand All @@ -44,15 +42,6 @@ public override void OnInspectorGUI()
PropertyField(m_SamplingNoise);
PropertyField(m_AnimateNoise);
PropertyField(m_LeakReductionMode);
if (m_LeakReductionMode.value.intValue == (int)APVLeakReductionMode.ValidityBased)
{
}
else if (m_LeakReductionMode.value.intValue == (int)APVLeakReductionMode.ValidityAndNormalBased)
{
using (new IndentLevelScope())
PropertyField(m_MinValidDotProdValue);
}

PropertyField(m_OcclusionOnlyNormalization);

PropertyField(m_IntensityMultiplier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void DoCull(inout v2f o)
// snappedProbePosition_WS : worldspace position of main probe (a corner of the 8 probes cube)
// samplingPosition_WS : worldspace sampling position after applying 'NormalBias' and 'ViewBias' and 'ValidityAndNormalBased Leak Reduction'
// normalizedOffset : normalized offset between sampling position and snappedProbePosition
void FindSamplingData(float3 posWS, float3 normalWS, uint renderingLayer, out float3 snappedProbePosition_WS, out float3 samplingPosition_WS, out float3 samplingPositionNoAntiLeak_WS, out float probeDistance, out float3 normalizedOffset, out float validityWeights[8])
void FindSamplingData(float3 posWS, float3 normalWS, uint renderingLayer, out float3 snappedProbePosition_WS, out float3 samplingPositionNoAntiLeak_WS, out float probeDistance, out float3 normalizedOffset, out float validityWeights[8])
{
float3 cameraPosition_WS = _WorldSpaceCameraPos;
float3 viewDir_WS = normalize(cameraPosition_WS - posWS);
Expand All @@ -115,33 +115,43 @@ void FindSamplingData(float3 posWS, float3 normalWS, uint renderingLayer, out fl
posWS = AddNoiseToSamplingPosition(posWS, posSS, viewDir_WS);
}

posWS -= _WorldOffset;
posWS -= _APVWorldOffset;

// uvw
APVResources apvRes = FillAPVResources();
float3 uvw;
uint subdiv;
float3 biasedPosWS;
bool valid = TryToGetPoolUVWAndSubdiv(apvRes, posWS, normalWS, viewDir_WS, uvw, subdiv, biasedPosWS);

probeDistance = ProbeDistance(subdiv);
snappedProbePosition_WS = GetSnappedProbePosition(biasedPosWS, subdiv);

WarpUVWLeakReduction(apvRes, posWS, normalWS, renderingLayer, subdiv, biasedPosWS, uvw, normalizedOffset, validityWeights);

biasedPosWS += _WorldOffset;
snappedProbePosition_WS += _WorldOffset;
samplingPositionNoAntiLeak_WS = biasedPosWS;

if (_LeakReductionMode != 0)
// Validity mask
float3 texCoord = uvw * _APVPoolDim - .5f;
float3 texFrac = frac(texCoord);
uint validityMask = LoadValidityMask(apvRes, renderingLayer, texCoord);
for (uint i = 0; i < 8; i++)
{
samplingPosition_WS = snappedProbePosition_WS + (normalizedOffset*probeDistance);
int3 probeCoord = GetSampleOffset(i);
float validityWeight = ((probeCoord.x == 1) ? texFrac.x : 1.0f - texFrac.x) *
((probeCoord.y == 1) ? texFrac.y : 1.0f - texFrac.y) *
((probeCoord.z == 1) ? texFrac.z : 1.0f - texFrac.z);
validityWeights[i] = validityWeight * GetValidityWeight(i, validityMask);
}
else

// Sample position
normalizedOffset = texFrac;
if (_APVLeakReductionMode == APVLEAKREDUCTIONMODE_PERFORMANCE)
{
normalizedOffset = (biasedPosWS - snappedProbePosition_WS) / probeDistance;
samplingPosition_WS = biasedPosWS;
float3 warped = uvw;
WarpUVWLeakReduction(apvRes, renderingLayer, warped);
normalizedOffset += (warped - uvw) * _APVPoolDim;
}

// stuff
biasedPosWS += _APVWorldOffset;
samplingPositionNoAntiLeak_WS = biasedPosWS;

probeDistance = ProbeDistance(subdiv);
snappedProbePosition_WS = GetSnappedProbePosition(biasedPosWS, subdiv);
}

// Return probe sampling weight
Expand Down Expand Up @@ -293,7 +303,7 @@ float3 CalculateDiffuseLighting(v2f i)
float3 skyShadingDirection = normal;
if (_ShadingMode == DEBUGPROBESHADINGMODE_SKY_DIRECTION)
{
if (_EnableSkyOcclusionShadingDirection > 0)
if (_APVSkyDirectionWeight > 0)
{
float value = 1.0f / GetCurrentExposureMultiplier();

Expand All @@ -314,8 +324,8 @@ float3 CalculateDiffuseLighting(v2f i)
}
else
{
float skyOcclusion = 0.0f;
if (_SkyOcclusionIntensity > 0)
float3 skyOcclusion = _DebugEmptyProbeData.xyz;
if (_APVSkyOcclusionWeight > 0)
{
// L0 L1
float4 temp = float4(kSHBasis0, kSHBasis1 * normal.x, kSHBasis1 * normal.y, kSHBasis1 * normal.z);
Expand All @@ -324,10 +334,7 @@ float3 CalculateDiffuseLighting(v2f i)

if (_ShadingMode == DEBUGPROBESHADINGMODE_SKY_OCCLUSION_SH)
{
if(_SkyOcclusionIntensity > 0)
return skyOcclusion / GetCurrentExposureMultiplier();
else
return _DebugEmptyProbeData.xyz / GetCurrentExposureMultiplier();
return skyOcclusion / GetCurrentExposureMultiplier();
}
else
{
Expand Down Expand Up @@ -355,7 +362,7 @@ float3 CalculateDiffuseLighting(v2f i)

bakeDiffuseLighting += EvalL2(L0, L2_R, L2_G, L2_B, L2_C, normal);
#endif
if (_SkyOcclusionIntensity > 0)
if (_APVSkyOcclusionWeight > 0)
bakeDiffuseLighting += skyOcclusion * EvaluateAmbientProbe(skyShadingDirection);

return bakeDiffuseLighting;
Expand Down
Loading

0 comments on commit e9b9d84

Please sign in to comment.