Skip to content

Commit 8b2c9d2

Browse files
AndrewSaraevUnityDeployer User
authored andcommitted
DGI fallback radiance baking for low end settings (#92)
* Added the way to copy prepared radiance into baked data of a Probe Volume and use it at runtime as a fallback for when DGI is disabled. * Changed the way of updating and tracking data version for Probe Volume assets to not reupload it on every asset reimport and validation (needed to maintain propagated radiance while baking reflections and saving assets).
1 parent e49409d commit 8b2c9d2

File tree

9 files changed

+337
-61
lines changed

9 files changed

+337
-61
lines changed

com.unity.render-pipelines.high-definition/Editor/Lighting/ProbeVolume/ProbeVolumeUI.Drawer.cs

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@ enum Expandable
1616
{
1717
Volume = 1 << 0,
1818
Probes = 1 << 1,
19-
Baking = 1 << 2
19+
Baking = 1 << 2,
20+
DynamicBaking = 1 << 3
21+
}
22+
23+
enum DynamicGIBakingStage
24+
{
25+
Neighborhood,
26+
MixedLights,
27+
FallbackRadiance,
2028
}
2129

2230
readonly static ExpandedState<Expandable, ProbeVolume> k_ExpandedStateVolume = new ExpandedState<Expandable, ProbeVolume>(Expandable.Volume, "HDRP");
2331
readonly static ExpandedState<Expandable, ProbeVolume> k_ExpandedStateProbes = new ExpandedState<Expandable, ProbeVolume>(Expandable.Probes, "HDRP");
2432
readonly static ExpandedState<Expandable, ProbeVolume> k_ExpandedStateBaking = new ExpandedState<Expandable, ProbeVolume>(Expandable.Baking, "HDRP");
33+
readonly static ExpandedState<Expandable, ProbeVolume> k_ExpandedStateDynamicBaking = new ExpandedState<Expandable, ProbeVolume>(Expandable.DynamicBaking, "HDRP");
2534

2635
internal static readonly CED.IDrawer Inspector = CED.Group(
2736
CED.Group(
@@ -55,6 +64,13 @@ enum Expandable
5564
Expandable.Baking,
5665
k_ExpandedStateBaking,
5766
Drawer_BakeToolBar
67+
),
68+
CED.space,
69+
CED.FoldoutGroup(
70+
Styles.k_DynamicBakingHeader,
71+
Expandable.DynamicBaking,
72+
k_ExpandedStateDynamicBaking,
73+
Drawer_DynamicBakeToolBar
5874
)
5975
)
6076
)
@@ -101,38 +117,108 @@ static void Drawer_BakeToolBar(SerializedProbeVolume serialized, Editor owner)
101117
EditorGUILayout.Slider(serialized.backfaceTolerance, 0.0f, 1.0f, Styles.s_BackfaceToleranceLabel);
102118
EditorGUILayout.PropertyField(serialized.dilationIterations, Styles.s_DilationIterationLabel);
103119

104-
GUILayout.BeginHorizontal();
105-
if (GUILayout.Button(Styles.k_BakeSelectedText))
120+
var bakeButtonRect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(false));
121+
if (GUI.Button(bakeButtonRect, Styles.k_BakeSelectedText))
106122
{
107123
ProbeVolumeManager.BakeSelected();
108124
}
109-
if (GUILayout.Button(Styles.k_BakeDynamicGIOnlyText))
125+
}
126+
127+
static void Drawer_DynamicBakeToolBar(SerializedProbeVolume serialized, Editor owner)
128+
{
129+
DynamicGIBakingStage dynamicGIBakingStage;
130+
if (ProbeVolume.preparingMixedLights)
131+
dynamicGIBakingStage = DynamicGIBakingStage.MixedLights;
132+
else if (ProbeVolume.preparingForBake)
133+
dynamicGIBakingStage = DynamicGIBakingStage.FallbackRadiance;
134+
else
135+
dynamicGIBakingStage = DynamicGIBakingStage.Neighborhood;
136+
137+
EditorGUI.BeginChangeCheck();
138+
dynamicGIBakingStage = (DynamicGIBakingStage)EditorGUILayout.EnumPopup(Styles.k_DynamicBakingStageLabel, dynamicGIBakingStage);
139+
if (EditorGUI.EndChangeCheck())
140+
{
141+
ProbeVolume.preparingMixedLights = dynamicGIBakingStage == DynamicGIBakingStage.MixedLights;
142+
ProbeVolume.preparingForBake = dynamicGIBakingStage == DynamicGIBakingStage.FallbackRadiance;
143+
}
144+
145+
EditorGUILayout.Space();
146+
147+
var targets = serialized.GetTargetObjects();
148+
149+
if (dynamicGIBakingStage == DynamicGIBakingStage.Neighborhood)
110150
{
111-
var targets = serialized.GetTargetObjects();
112-
for (int i = 0; i < targets.Length; i++)
151+
var dynamicBakeButtonRect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(false));
152+
if (GUI.Button(dynamicBakeButtonRect, Styles.k_DynamicBakeNeighborhoodLabel))
113153
{
114-
var probeVolume = (ProbeVolume)targets[i];
115-
EditorUtility.DisplayProgressBar("Baking Dynamic GI", $"{i + 1}/{targets.Length} {probeVolume.name}", (i + 0.5f) / targets.Length);
116-
probeVolume.BakeDynamicGIOnly();
154+
for (int i = 0; i < targets.Length; i++)
155+
{
156+
var probeVolume = (ProbeVolume)targets[i];
157+
EditorUtility.DisplayProgressBar("Baking Dynamic GI", $"{i + 1}/{targets.Length} {probeVolume.name}", (i + 0.5f) / targets.Length);
158+
probeVolume.BakeDynamicGIOnly();
159+
}
160+
EditorUtility.ClearProgressBar();
117161
}
118-
EditorUtility.ClearProgressBar();
119162
}
120-
GUILayout.EndHorizontal();
121-
122-
EditorGUILayout.Space();
123-
124-
ProbeVolume.preparingMixedLights = EditorGUILayout.Toggle(Styles.k_PrepareMixedLightsText, ProbeVolume.preparingMixedLights);
125-
GUI.enabled = ProbeVolume.preparingMixedLights;
163+
else
164+
{
165+
GUI.enabled = !CheckAndWarnNoNeighborhoodBaked(targets);
166+
167+
var dynamicBakeButtonRect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(false));
168+
if (dynamicGIBakingStage == DynamicGIBakingStage.MixedLights)
169+
{
170+
if (GUI.Button(dynamicBakeButtonRect, Styles.k_DynamicBakeMixedLightsLabel))
171+
{
172+
foreach (var target in targets)
173+
{
174+
var probeVolume = (ProbeVolume)target;
175+
probeVolume.CopyDirectLightingToMixed();
176+
}
177+
}
178+
}
179+
else if (dynamicGIBakingStage == DynamicGIBakingStage.FallbackRadiance)
180+
{
181+
if (GUI.Button(dynamicBakeButtonRect, Styles.k_DynamicBakeFallbackRadianceLabel))
182+
{
183+
foreach (var target in targets)
184+
{
185+
var probeVolume = (ProbeVolume)target;
186+
probeVolume.CopyDynamicSHToAsset();
187+
}
188+
}
189+
}
126190

127-
if (GUILayout.Button(Styles.k_BakeMixedLightsText))
191+
GUI.enabled = true;
192+
193+
EditorGUILayout.Space();
194+
195+
EditorGUILayout.HelpBox(Styles.k_DynamicPipelineOverridesWarning, MessageType.Warning);
196+
var resetButtonRect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(false));
197+
if (GUI.Button(resetButtonRect, Styles.k_DynamicResetPipelineOverridesLabel))
198+
{
199+
ProbeVolume.preparingMixedLights = false;
200+
ProbeVolume.preparingForBake = false;
201+
}
202+
}
203+
}
204+
205+
static bool CheckAndWarnNoNeighborhoodBaked(Object[] targets)
206+
{
207+
var noNeighborhoodBaked = false;
208+
foreach (var target in targets)
128209
{
129-
var targets = serialized.GetTargetObjects();
130-
for (int i = 0; i < targets.Length; i++)
210+
var probeVolume = (ProbeVolume)target;
211+
if (probeVolume.probeVolumeAsset == null)
131212
{
132-
var probeVolume = (ProbeVolume)targets[i];
133-
probeVolume.CopyDirectLightingToMixed();
213+
noNeighborhoodBaked = true;
214+
break;
134215
}
135216
}
217+
218+
if (noNeighborhoodBaked)
219+
EditorGUILayout.HelpBox(Styles.k_DynamicNoNeighborhoodWarning, MessageType.Error);
220+
221+
return noNeighborhoodBaked;
136222
}
137223

138224
static void Drawer_ToolBar(SerializedProbeVolume serialized, Editor owner)

com.unity.render-pipelines.high-definition/Editor/Lighting/ProbeVolume/ProbeVolumeUI.Skin.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ internal static class Styles
1515
internal const string k_ProbesHeader = "Probes";
1616
internal const string k_BakingHeader = "Baking";
1717
internal const string k_BakeSelectedText = "Bake Selected";
18-
internal const string k_BakeDynamicGIOnlyText = "Bake Dynamic GI Only";
19-
internal const string k_PrepareMixedLightsText = "Prepare Mixed Lights";
20-
internal const string k_BakeMixedLightsText = "Bake Mixed Lights";
18+
internal const string k_DynamicBakingHeader = "Dynamic GI Baking";
19+
internal const string k_DynamicBakingStageLabel = "Baking Stage";
20+
internal const string k_DynamicBakeNeighborhoodLabel = "Bake Neighborhood";
21+
internal const string k_DynamicBakeMixedLightsLabel = "Bake Mixed Lights";
22+
internal const string k_DynamicNoNeighborhoodWarning = "Please bake the neighborhood before any other stage.";
23+
internal const string k_DynamicBakeFallbackRadianceLabel = "Bake Fallback Radiance";
24+
internal const string k_DynamicPipelineOverridesWarning = "Some render pipeline settings are globally overriden for baking purposes. Please reset them after you finish baking.";
25+
internal const string k_DynamicResetPipelineOverridesLabel = "Reset Pipeline Overrides";
2126

2227
internal static readonly GUIContent[] s_Toolbar_Contents = new GUIContent[]
2328
{

com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDProcessedVisibleLightsBuilder.Jobs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public void Execute(int index)
263263
lightRenderData.affectDynamicGI;
264264

265265
#if UNITY_EDITOR
266-
if (dynamicGIPreparingMixedLights)
266+
if (dynamicGIPreparingMixedLights || dynamicGIPreparingForBake)
267267
affectsDynamicGI &= lightRenderData.mixedDynamicGI;
268268
#endif
269269

com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoop.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,11 +2112,13 @@ bool PrepareLightsForGPU(CommandBuffer cmd, HDCamera hdCamera, CullingResults cu
21122112
bool processVisibleLights = cullResults.visibleLights.Length != 0;
21132113

21142114
bool dynamicGIEnabled = hdCamera.frameSettings.IsEnabled(FrameSettingsField.ProbeVolumeDynamicGI);
2115+
bool dynamicGIMixedOnly = hdCamera.frameSettings.probeVolumeDynamicGIMixedLightMode == ProbeVolumeDynamicGIMixedLightMode.MixedOnly;
21152116
bool dynamicGINeedsLights =
21162117
#if UNITY_EDITOR
2117-
ProbeVolume.preparingMixedLights ||
2118+
ProbeVolume.preparingMixedLights || ProbeVolume.preparingForBake ||
21182119
#endif
2119-
hdCamera.frameSettings.probeVolumeDynamicGIMixedLightMode != ProbeVolumeDynamicGIMixedLightMode.MixedOnly;
2120+
!dynamicGIMixedOnly;
2121+
21202122
bool processDynamicGI = dynamicGIEnabled && dynamicGINeedsLights;
21212123

21222124
PreprocessLights(cmd, hdCamera, cullResults, debugDisplaySettings, aovRequest, processDynamicGI);

com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/DynamicGI/ProbeDynamicGI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ProbeDynamicGI : VolumeComponent
4242
public DynamicGINeighboringVolumePropagationModeParameter neighborVolumePropagationMode = new DynamicGINeighboringVolumePropagationModeParameter(DynamicGINeighboringVolumePropagationMode.SampleNeighborsDirectionOnly);
4343

4444
[Tooltip("Debug Contribution control for mixing in baked indirect lighting")]
45-
public ClampedFloatParameter bakeAmount = new ClampedFloatParameter(1.0f, 0.0f, 1.0f);
45+
public ClampedFloatParameter fallbackAmount = new ClampedFloatParameter(0.0f, 0.0f, 1.0f);
4646
[Tooltip("Debug Contribution control for mixing in dynamic indirect lighting")]
4747
public ClampedFloatParameter dynamicAmount = new ClampedFloatParameter(1.0f, 0.0f, 1.0f);
4848

0 commit comments

Comments
 (0)