Skip to content

Commit 95740fd

Browse files
urasmusEvergreen
authored andcommitted
[Backport] Fix wrong APV Sky Occlusion on Vulkan
1 parent 703a3e2 commit 95740fd

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/DynamicGI/DynamicGISkyOcclusion.urtshader

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
UNIFIED_RT_DECLARE_ACCEL_STRUCT(_AccelStruct);
1313

14-
1514
int _SampleCount;
1615
int _SampleId;
1716
int _MaxBounces;
@@ -34,11 +33,13 @@ void RayGenExecute(UnifiedRT::DispatchInfo dispatchInfo)
3433
QrngSobol rngState;
3534
rngState.Init(uint2((uint)probeId, 0), _SampleId);
3635

37-
if (_SampleId==0)
36+
float4 skyOcclusionEstimate = 0;
37+
float3 skyShadingEstimate = 0;
38+
if (_SampleId != 0)
3839
{
39-
_SkyOcclusionOut[probeId] = float4(0,0,0,0);
40+
skyOcclusionEstimate = _SkyOcclusionOut[probeId];
4041
if (_BakeSkyShadingDirection > 0)
41-
_SkyShadingOut[probeId] = float3(0,0,0);
42+
skyShadingEstimate = _SkyShadingOut[probeId];
4243
}
4344

4445
UnifiedRT::RayTracingAccelStruct accelStruct = UNIFIED_RT_GET_ACCEL_STRUCT(_AccelStruct);
@@ -107,47 +108,50 @@ void RayGenExecute(UnifiedRT::DispatchInfo dispatchInfo)
107108
rayFirstDirection.y * norm * kSHBasis1,
108109
rayFirstDirection.z * norm * kSHBasis1);
109110

110-
_SkyOcclusionOut[probeId] += tempSH;
111+
skyOcclusionEstimate += tempSH;
112+
111113
if(_BakeSkyShadingDirection > 0)
112-
_SkyShadingOut[probeId] += ray.direction / _SampleCount;
114+
skyShadingEstimate += ray.direction / _SampleCount;
113115

114-
// break the loop;
115-
bounceIndex = _MaxBounces + 2;
116+
break;
116117
}
117118
}
118119

119120
// Last sample
120121
if (_SampleId == _SampleCount - 1)
121122
{
122123
// Window L1 coefficients to make sure no value is negative when sampling SH, layout is DC, x, y, z
123-
float4 SHData = _SkyOcclusionOut[probeId];
124124
// find main direction for light
125125
float3 mainDir;
126-
mainDir.x = SHData.y;
127-
mainDir.y = SHData.z;
128-
mainDir.z = SHData.w;
126+
mainDir.x = skyOcclusionEstimate.y;
127+
mainDir.y = skyOcclusionEstimate.z;
128+
mainDir.z = skyOcclusionEstimate.w;
129129
mainDir = normalize(mainDir);
130130

131131
// find the value in the opposite direction, which is the lowest value in the SH
132132
float4 temp2 = float4(kSHBasis0, kSHBasis1 * -mainDir.x, kSHBasis1 * -mainDir.y, kSHBasis1 * -mainDir.z);
133-
float value = dot(temp2, SHData);
133+
float value = dot(temp2, skyOcclusionEstimate);
134134
float windowL1 = 1.0f;
135135

136136
if (value < 0.0f)
137137
{
138138
// find the L1 factor for this value to be null instead of negative
139-
windowL1 = -(temp2.x * SHData.x) / dot(temp2.yzw, SHData.yzw);
139+
windowL1 = -(temp2.x * skyOcclusionEstimate.x) / dot(temp2.yzw, skyOcclusionEstimate.yzw);
140140
windowL1 = saturate(windowL1);
141141
}
142142

143-
_SkyOcclusionOut[probeId].yzw *= windowL1;
143+
skyOcclusionEstimate.yzw *= windowL1;
144144

145145
float radianceToIrradianceFactor = 2.0f / 3.0f;
146146
// This is a hacky solution for mitigating the radianceToIrradianceFactor based on the previous windowing operation.
147147
// The 1.125f exponent comes from experimental testing. It's the value that works the best when trying to match a bake and deringing done with the lightmapper, but it has no theoretical explanation.
148148
// In the future, we should replace these custom windowing and deringing operations with the ones used in the lightmapper to implement a more academical solution.
149-
_SkyOcclusionOut[probeId].yzw *= lerp(1.0f, radianceToIrradianceFactor, pow(windowL1, 1.125f));
149+
skyOcclusionEstimate.yzw *= lerp(1.0f, radianceToIrradianceFactor, pow(windowL1, 1.125f));
150150
}
151+
152+
_SkyOcclusionOut[probeId] = skyOcclusionEstimate;
153+
if(_BakeSkyShadingDirection > 0)
154+
_SkyShadingOut[probeId] = skyShadingEstimate;
151155
}
152156

153157
#ifdef UNIFIED_RT_BACKEND_COMPUTE

0 commit comments

Comments
 (0)