Skip to content

Commit 657ddb6

Browse files
adrien-de-tocquevilleEvergreen
authored andcommitted
[HDRP] Fix flickering when using baked gi node and TAA
The position SS wasn't passed to the APV evaluation function resulting in wrong jittering for TAA
1 parent a957d70 commit 657ddb6

File tree

4 files changed

+93
-45
lines changed

4 files changed

+93
-45
lines changed

Packages/com.unity.render-pipelines.high-definition/Runtime/Material/BuiltinGIUtilities.hlsl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ void SampleBakedGI(
163163
#elif (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
164164
if (needToIncludeAPV)
165165
{
166-
EvaluateAdaptiveProbeVolume(GetAbsolutePositionWS(posInputs.positionWS), normalWS, backNormalWS, GetWorldSpaceNormalizeViewDir(posInputs.positionWS), 0.0, bakeDiffuseLighting, backBakeDiffuseLighting);
166+
EvaluateAdaptiveProbeVolume(GetAbsolutePositionWS(posInputs.positionWS),
167+
normalWS,
168+
backNormalWS,
169+
GetWorldSpaceNormalizeViewDir(posInputs.positionWS),
170+
posInputs.positionSS,
171+
bakeDiffuseLighting,
172+
backBakeDiffuseLighting);
167173
}
168174
#elif !(defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2)) // With APV if we aren't a lightmap we do nothing. We will default to Ambient Probe in lightloop code if APV is disabled
169175
EvaluateLightProbeBuiltin(positionRWS, normalWS, backNormalWS, bakeDiffuseLighting, backBakeDiffuseLighting);
@@ -191,14 +197,15 @@ void SampleBakedGI(
191197
SampleBakedGI(posInputs, normalWS, backNormalWS, renderingLayers, uvStaticLightmap, uvDynamicLightmap, needToIncludeAPV, bakeDiffuseLighting, backBakeDiffuseLighting);
192198
}
193199

194-
float3 SampleBakedGI(float3 positionRWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool needToIncludeAPV = false)
200+
float3 SampleBakedGI(float3 positionRWS, float3 normalWS, uint2 positionSS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool needToIncludeAPV = false)
195201
{
196-
// Need PositionInputs for indexing probe volume clusters, but they are not availbile from the current SampleBakedGI() function signature.
202+
// Need PositionInputs for indexing probe volume clusters, but they are not available from the current SampleBakedGI() function signature.
197203
// Reconstruct.
198204
uint renderingLayers = 0;
199205
PositionInputs posInputs;
200206
ZERO_INITIALIZE(PositionInputs, posInputs);
201207
posInputs.positionWS = positionRWS;
208+
posInputs.positionSS = positionSS;
202209

203210
const float3 backNormalWSUnused = 0.0;
204211
float3 bakeDiffuseLighting;

Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderGraphFunctions.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ float3 shadergraph_HDSampleSceneColor(float2 uv)
4242
return float3(0, 0, 0);
4343
}
4444

45-
float3 shadergraph_HDBakedGI(float3 positionWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool applyScaling)
45+
float3 shadergraph_HDBakedGI(float3 positionRWS, float3 normalWS, uint2 positionSS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool applyScaling)
4646
{
4747
#if defined(__BUILTINGIUTILITIES_HLSL__)
4848
bool needToIncludeAPV = true;
49-
return SampleBakedGI(positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap, needToIncludeAPV);
49+
return SampleBakedGI(positionRWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap, needToIncludeAPV);
5050
#else
5151
return 0;
5252
#endif
@@ -89,7 +89,7 @@ float3 shadergraph_HDMainLightDirection()
8989
#ifdef SHADERGRAPH_BAKED_GI
9090
#undef SHADERGRAPH_BAKED_GI
9191
#endif
92-
#define SHADERGRAPH_BAKED_GI(positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap, applyScaling) shadergraph_HDBakedGI(positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap, applyScaling)
92+
#define SHADERGRAPH_BAKED_GI(positionWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap, applyScaling) shadergraph_HDBakedGI(positionWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap, applyScaling)
9393

9494

9595
#ifdef SHADERGRAPH_MAIN_LIGHT_DIRECTION

Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#define SHADERGRAPH_SAMPLE_SCENE_DEPTH(uv) shadergraph_LWSampleSceneDepth(uv)
55
#define SHADERGRAPH_SAMPLE_SCENE_COLOR(uv) shadergraph_LWSampleSceneColor(uv)
66
#define SHADERGRAPH_SAMPLE_SCENE_NORMAL(uv) shadergraph_LWSampleSceneNormals(uv)
7-
#define SHADERGRAPH_BAKED_GI(positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap, applyScaling) shadergraph_LWBakedGI(positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap, applyScaling)
7+
#define SHADERGRAPH_BAKED_GI(positionWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap, applyScaling) shadergraph_LWBakedGI(positionWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap, applyScaling)
88
#define SHADERGRAPH_REFLECTION_PROBE(viewDir, normalOS, lod) shadergraph_LWReflectionProbe(viewDir, normalOS, lod)
99
#define SHADERGRAPH_FOG(position, color, density) shadergraph_LWFog(position, color, density)
1010
#define SHADERGRAPH_AMBIENT_SKY unity_AmbientSky
@@ -53,7 +53,7 @@ float3 shadergraph_LWSampleSceneNormals(float2 uv)
5353
#endif
5454
}
5555

56-
float3 shadergraph_LWBakedGI(float3 positionWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool applyScaling)
56+
float3 shadergraph_LWBakedGI(float3 positionWS, float3 normalWS, uint2 positionSS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool applyScaling)
5757
{
5858
#ifdef LIGHTMAP_ON
5959
if (applyScaling)
@@ -71,7 +71,7 @@ float3 shadergraph_LWBakedGI(float3 positionWS, float3 normalWS, float2 uvStatic
7171
if (_EnableProbeVolumes)
7272
{
7373
float3 bakeDiffuseLighting;
74-
EvaluateAdaptiveProbeVolume(positionWS, normalWS, GetWorldSpaceNormalizeViewDir(positionWS), 0.0, bakeDiffuseLighting);
74+
EvaluateAdaptiveProbeVolume(positionWS, normalWS, GetWorldSpaceNormalizeViewDir(positionWS), positionSS, bakeDiffuseLighting);
7575
return bakeDiffuseLighting;
7676
}
7777
else
Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System.Reflection;
21
using UnityEngine;
32
using UnityEditor.Graphing;
43
using UnityEditor.ShaderGraph.Drawing.Controls;
4+
using UnityEditor.ShaderGraph.Internal;
55

66
namespace UnityEditor.ShaderGraph
77
{
88
[FormerName("UnityEditor.ShaderGraph.BakedGAbstractMaterialNode")]
99
[FormerName("UnityEditor.ShaderGraph.LightProbeNode")]
1010
[Title("Input", "Lighting", "Baked GI")]
11-
class BakedGINode : CodeFunctionNode
11+
class BakedGINode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequirePixelPosition, IMayRequirePosition, IMayRequireNormal, IMayRequireMeshUV
1212
{
1313
public override bool hasPreview { get { return false; } }
1414

@@ -18,14 +18,6 @@ public BakedGINode()
1818
synonyms = new string[] { "global illumination" };
1919
}
2020

21-
protected override MethodInfo GetFunctionToConvert()
22-
{
23-
if (applyScaling.isOn)
24-
return GetType().GetMethod("Unity_BakedGIScale", BindingFlags.Static | BindingFlags.NonPublic);
25-
else
26-
return GetType().GetMethod("Unity_BakedGI", BindingFlags.Static | BindingFlags.NonPublic);
27-
}
28-
2921
[SerializeField]
3022
private bool m_ApplyScaling = true;
3123

@@ -42,36 +34,85 @@ public ToggleData applyScaling
4234
}
4335
}
4436

45-
static string Unity_BakedGI(
46-
[Slot(2, Binding.WorldSpacePosition)] Vector3 Position,
47-
[Slot(0, Binding.WorldSpaceNormal)] Vector3 Normal,
48-
[Slot(3, Binding.MeshUV1)] Vector2 StaticUV,
49-
[Slot(4, Binding.MeshUV2)] Vector2 DynamicUV,
50-
[Slot(1, Binding.None)] out Vector3 Out)
37+
const int kNormalWSInputSlotId = 0;
38+
const string kNormalWSInputSlotName = "NormalWS";
39+
40+
const int kOutputSlotId = 1;
41+
const string kOutputSlotName = "Out";
42+
43+
const int kPositionWSInputSlotId = 2;
44+
const string kPositionWSInputSlotName = "PositionWS";
45+
46+
const int kStaticUVInputSlotId = 3;
47+
const string kStaticUVInputSlotName = "StaticUV";
48+
49+
const int kDynamicUVInputSlotId = 4;
50+
const string kDynamicUVInputSlotName = "DynamicUV";
51+
52+
public sealed override void UpdateNodeAfterDeserialization()
5153
{
52-
Out = Vector3.one;
53-
return
54-
@"
55-
{
56-
Out = SHADERGRAPH_BAKED_GI(Position, Normal, StaticUV, DynamicUV, false);
57-
}
58-
";
54+
// Input
55+
AddSlot(new NormalMaterialSlot(kNormalWSInputSlotId, kNormalWSInputSlotName, kNormalWSInputSlotName, CoordinateSpace.World));
56+
AddSlot(new PositionMaterialSlot(kPositionWSInputSlotId, kPositionWSInputSlotName, kPositionWSInputSlotName, CoordinateSpace.World));
57+
AddSlot(new UVMaterialSlot(kStaticUVInputSlotId, kStaticUVInputSlotName, kStaticUVInputSlotName, UVChannel.UV1));
58+
AddSlot(new UVMaterialSlot(kDynamicUVInputSlotId, kDynamicUVInputSlotName, kDynamicUVInputSlotName, UVChannel.UV2));
59+
60+
// Output
61+
AddSlot(new Vector3MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector3.zero));
62+
63+
RemoveSlotsNameNotMatching(new[]
64+
{
65+
// Input
66+
kNormalWSInputSlotId,
67+
kPositionWSInputSlotId,
68+
kStaticUVInputSlotId,
69+
kDynamicUVInputSlotId,
70+
71+
// Output
72+
kOutputSlotId,
73+
});
5974
}
6075

61-
static string Unity_BakedGIScale(
62-
[Slot(2, Binding.WorldSpacePosition)] Vector3 Position,
63-
[Slot(0, Binding.WorldSpaceNormal)] Vector3 Normal,
64-
[Slot(3, Binding.MeshUV1)] Vector2 StaticUV,
65-
[Slot(4, Binding.MeshUV2)] Vector2 DynamicUV,
66-
[Slot(1, Binding.None)] out Vector3 Out)
76+
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
6777
{
68-
Out = Vector3.one;
69-
return
70-
@"
71-
{
72-
Out = SHADERGRAPH_BAKED_GI(Position, Normal, StaticUV, DynamicUV, true);
73-
}
74-
";
78+
if (generationMode == GenerationMode.ForReals)
79+
{
80+
sb.AppendLine("$precision3 {6} = SHADERGRAPH_BAKED_GI({0}, {1}, IN.{2}.xy, {3}, {4}, {5});",
81+
GetSlotValue(kPositionWSInputSlotId, generationMode),
82+
GetSlotValue(kNormalWSInputSlotId, generationMode),
83+
ShaderGeneratorNames.PixelPosition,
84+
GetSlotValue(kStaticUVInputSlotId, generationMode),
85+
GetSlotValue(kDynamicUVInputSlotId, generationMode),
86+
applyScaling.isOn ? "true" : "false",
87+
GetVariableNameForSlot(kOutputSlotId));
88+
}
89+
else
90+
{
91+
// Output zeros
92+
sb.AppendLine("$precision3 {0} = 0.0;",
93+
GetVariableNameForSlot(kOutputSlotId));
94+
}
95+
}
96+
97+
public bool RequiresPixelPosition(ShaderStageCapability stageCapability = ShaderStageCapability.All)
98+
{
99+
return true; // needed for APV sampling noise when TAA is used
100+
}
101+
102+
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability = ShaderStageCapability.All)
103+
{
104+
return FindSlot<PositionMaterialSlot>(kPositionWSInputSlotId).RequiresPosition();
105+
}
106+
107+
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability = ShaderStageCapability.All)
108+
{
109+
return FindSlot<NormalMaterialSlot>(kNormalWSInputSlotId).RequiresNormal();
110+
}
111+
112+
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability = ShaderStageCapability.All)
113+
{
114+
return FindSlot<UVMaterialSlot>(kStaticUVInputSlotId).RequiresMeshUV(channel) ||
115+
FindSlot<UVMaterialSlot>(kDynamicUVInputSlotId).RequiresMeshUV(channel);
75116
}
76117
}
77118
}

0 commit comments

Comments
 (0)