Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 1bc44e2

Browse files
committed
Optimized lighting effect passes; removed useless usings; UI tweaks
1 parent 0d850a0 commit 1bc44e2

12 files changed

+110
-91
lines changed

PostProcessing/Editor/PostProcessLayerEditor.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public sealed class PostProcessLayerEditor : BaseEditor<PostProcessLayer>
3636
SerializedProperty m_DebugDisplay;
3737
SerializedProperty m_DebugMonitor;
3838
SerializedProperty m_DebugLightMeter;
39-
39+
40+
SerializedProperty m_ShowDebugLayer;
4041
SerializedProperty m_ShowToolkit;
4142
SerializedProperty m_ShowCustomSorter;
4243

@@ -83,6 +84,7 @@ void OnEnable()
8384
m_DebugMonitor = FindProperty(x => x.debugView.monitor);
8485
m_DebugLightMeter = FindProperty(x => x.debugView.lightMeter);
8586

87+
m_ShowDebugLayer = serializedObject.FindProperty("m_ShowDebugLayer");
8688
m_ShowToolkit = serializedObject.FindProperty("m_ShowToolkit");
8789
m_ShowCustomSorter = serializedObject.FindProperty("m_ShowCustomSorter");
8890

@@ -245,7 +247,7 @@ void DoFog(Camera camera)
245247
if (camera == null || camera.actualRenderingPath != RenderingPath.DeferredShading)
246248
return;
247249

248-
EditorGUILayout.LabelField(EditorUtilities.GetContent("Fog (Deferred)"), EditorStyles.boldLabel);
250+
EditorGUILayout.LabelField(EditorUtilities.GetContent("Deferred Fog"), EditorStyles.boldLabel);
249251
EditorGUI.indentLevel++;
250252
{
251253
EditorGUILayout.PropertyField(m_FogEnabled);
@@ -263,23 +265,32 @@ void DoFog(Camera camera)
263265

264266
void DoDebugLayer()
265267
{
266-
EditorGUILayout.LabelField(EditorUtilities.GetContent("Debug Layer"), EditorStyles.boldLabel);
267-
EditorGUI.indentLevel++;
268+
EditorUtilities.DrawSplitter();
269+
m_ShowDebugLayer.boolValue = EditorUtilities.DrawHeader("Debug Layer", m_ShowDebugLayer.boolValue);
270+
271+
if (m_ShowDebugLayer.boolValue)
268272
{
269-
EditorGUILayout.PropertyField(m_DebugDisplay, EditorUtilities.GetContent("Display|Toggle visibility of the debug layer on & off in the Game View."));
273+
GUILayout.Space(2);
270274

271-
if (m_DebugDisplay.boolValue)
275+
EditorGUI.indentLevel++;
272276
{
273-
if (!SystemInfo.supportsComputeShaders)
274-
EditorGUILayout.HelpBox("The debug layer only works on compute-shader enabled platforms.", MessageType.Info);
277+
EditorGUILayout.PropertyField(m_DebugDisplay, EditorUtilities.GetContent("Display|Toggle visibility of the debug layer on & off in the Game View."));
275278

276-
EditorGUILayout.PropertyField(m_DebugMonitor, EditorUtilities.GetContent("Monitor|The real-time monitor to display on the debug layer."));
277-
EditorGUILayout.PropertyField(m_DebugLightMeter, EditorUtilities.GetContent("HDR Light Meter|Light metering utility used to setup auto exposure. Note that it will only display correct values when using a full-HDR workflow (HDR camera, HDR/Custom color grading)."));
279+
using (new EditorGUI.DisabledScope(!m_DebugDisplay.boolValue))
280+
{
281+
EditorGUILayout.PropertyField(m_DebugMonitor, EditorUtilities.GetContent("Monitor|The real-time monitor to display on the debug layer."));
282+
EditorGUILayout.PropertyField(m_DebugLightMeter, EditorUtilities.GetContent("HDR Light Meter|Light metering utility used to setup auto exposure. Note that it will only display correct values when using a full-HDR workflow (HDR camera, HDR/Custom color grading)."));
283+
}
284+
285+
if (!SystemInfo.supportsComputeShaders)
286+
EditorGUILayout.HelpBox("The debug layer only works on compute-shader enabled platforms.", MessageType.Warning);
287+
288+
EditorGUILayout.HelpBox("This feature is still a work in progress.", MessageType.Info);
278289
}
279-
}
280-
EditorGUI.indentLevel--;
290+
EditorGUI.indentLevel--;
281291

282-
EditorGUILayout.Space();
292+
GUILayout.Space(3);
293+
}
283294
}
284295

285296
void DoToolkit()

PostProcessing/Runtime/Effects/AmbientOcclusion.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using UnityEngine.Rendering;
32

43
namespace UnityEngine.Rendering.PostProcessing
54
{
@@ -58,9 +57,10 @@ internal DepthTextureMode GetCameraFlags()
5857

5958
internal bool IsAmbientOnly(PostProcessRenderContext context)
6059
{
60+
var camera = context.camera;
6161
return ambientOnly
62-
&& context.camera.actualRenderingPath == RenderingPath.DeferredShading
63-
&& context.camera.allowHDR;
62+
&& camera.actualRenderingPath == RenderingPath.DeferredShading
63+
&& camera.allowHDR;
6464
}
6565

6666
internal bool IsEnabledAndSupported(PostProcessRenderContext context)

PostProcessing/Runtime/Effects/AutoExposure.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using UnityEngine.Rendering;
32

43
namespace UnityEngine.Rendering.PostProcessing
54
{

PostProcessing/Runtime/Effects/ColorGrading.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using UnityEngine.Rendering;
32

43
namespace UnityEngine.Rendering.PostProcessing
54
{

PostProcessing/Runtime/Effects/DepthOfField.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using UnityEngine.Rendering;
32

43
namespace UnityEngine.Rendering.PostProcessing
54
{

PostProcessing/Runtime/Effects/Grain.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using UnityEngine.Rendering;
32

43
namespace UnityEngine.Rendering.PostProcessing
54
{

PostProcessing/Runtime/Effects/MotionBlur.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using UnityEngine.Rendering;
32

43
namespace UnityEngine.Rendering.PostProcessing
54
{

PostProcessing/Runtime/Effects/TemporalAntialiasing.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using UnityEngine.Rendering;
32

43
namespace UnityEngine.Rendering.PostProcessing
54
{

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 83 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using UnityEngine.Assertions;
5-
using UnityEngine.Rendering;
65

76
namespace UnityEngine.Rendering.PostProcessing
87
{
@@ -40,11 +39,9 @@ public enum Antialiasing
4039
PostProcessResources m_Resources;
4140

4241
// UI states
43-
[SerializeField]
44-
bool m_ShowToolkit;
45-
46-
[SerializeField]
47-
bool m_ShowCustomSorter;
42+
[SerializeField] bool m_ShowDebugLayer;
43+
[SerializeField] bool m_ShowToolkit;
44+
[SerializeField] bool m_ShowCustomSorter;
4845

4946
// Will stop applying post-processing effects just before color grading is applied
5047
// Currently used to export to exr without color grading
@@ -243,7 +240,6 @@ void OnPreCull()
243240

244241
var context = m_CurrentContext;
245242
var sourceFormat = m_Camera.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;
246-
int tempRt = m_TargetPool.Get();
247243

248244
context.Reset();
249245
context.camera = m_Camera;
@@ -255,27 +251,92 @@ void OnPreCull()
255251

256252
SetupContext(context);
257253

258-
// We need to use the internal Blit method to copy the camera target or it'll fail on
259-
// tiled GPU as it won't be able to resolve
260-
261-
RenderLightingEffects(context);
254+
// Lighting & opaque-only effects
255+
int opaqueOnlyEffects = 0;
256+
bool hasCustomOpaqueOnlyEffects = HasOpaqueOnlyEffects(context);
257+
bool isAmbientOcclusionDeferred = ambientOcclusion.IsEnabledAndSupported(context) && ambientOcclusion.IsAmbientOnly(context);
258+
bool isAmbientOcclusionOpaque = ambientOcclusion.IsEnabledAndSupported(context) && !ambientOcclusion.IsAmbientOnly(context);
259+
bool isFogActive = fog.IsEnabledAndSupported(context);
262260

263-
if (HasOpaqueOnlyEffects(context))
261+
// Ambient-only AO is done in a separate command buffer, before reflections
262+
if (isAmbientOcclusionDeferred)
263+
{
264+
context.command = m_LegacyCmdBufferBeforeReflections;
265+
ambientOcclusion.RenderAmbientOnly(context);
266+
}
267+
else if (isAmbientOcclusionOpaque)
264268
{
265-
m_LegacyCmdBufferOpaque.GetTemporaryRT(tempRt, m_Camera.pixelWidth, m_Camera.pixelHeight, 24, FilterMode.Bilinear, sourceFormat);
266-
m_LegacyCmdBufferOpaque.Blit(BuiltinRenderTextureType.CameraTarget, tempRt);
267-
context.command = m_LegacyCmdBufferOpaque;
268-
context.source = new RenderTargetIdentifier(tempRt);
269-
context.destination = BuiltinRenderTextureType.CameraTarget;
270-
RenderOpaqueOnly(context);
271-
m_LegacyCmdBufferOpaque.ReleaseTemporaryRT(tempRt);
269+
opaqueOnlyEffects++;
272270
}
271+
272+
opaqueOnlyEffects += isFogActive ? 1 : 0;
273+
opaqueOnlyEffects += hasCustomOpaqueOnlyEffects ? 1 : 0;
274+
275+
var cameraTarget = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
276+
277+
if (opaqueOnlyEffects > 0)
278+
{
279+
var cmd = m_LegacyCmdBufferOpaque;
280+
context.command = cmd;
281+
282+
// We need to use the internal Blit method to copy the camera target or it'll fail
283+
// on tiled GPU as it won't be able to resolve
284+
int tempTarget0 = m_TargetPool.Get();
285+
cmd.GetTemporaryRT(tempTarget0, context.width, context.height, 24, FilterMode.Bilinear, sourceFormat);
286+
cmd.Blit(cameraTarget, tempTarget0);
287+
context.source = tempTarget0;
288+
289+
int tempTarget1 = -1;
290+
291+
if (opaqueOnlyEffects > 1)
292+
{
293+
tempTarget1 = m_TargetPool.Get();
294+
cmd.GetTemporaryRT(tempTarget1, context.width, context.height, 24, FilterMode.Bilinear, sourceFormat);
295+
context.destination = tempTarget1;
296+
}
297+
else
298+
{
299+
context.destination = cameraTarget;
300+
}
273301

274-
m_LegacyCmdBuffer.GetTemporaryRT(tempRt, m_Camera.pixelWidth, m_Camera.pixelHeight, 24, FilterMode.Bilinear, sourceFormat);
275-
m_LegacyCmdBuffer.Blit(BuiltinRenderTextureType.CameraTarget, tempRt);
302+
if (isAmbientOcclusionOpaque)
303+
{
304+
ambientOcclusion.RenderAfterOpaque(context);
305+
opaqueOnlyEffects--;
306+
var prevSource = context.source;
307+
context.source = context.destination;
308+
context.destination = opaqueOnlyEffects == 1 ? cameraTarget : prevSource;
309+
}
310+
311+
// TODO: Insert SSR here
312+
313+
if (isFogActive)
314+
{
315+
fog.Render(context);
316+
opaqueOnlyEffects--;
317+
var prevSource = context.source;
318+
context.source = context.destination;
319+
context.destination = opaqueOnlyEffects == 1 ? cameraTarget : prevSource;
320+
}
321+
322+
if (hasCustomOpaqueOnlyEffects)
323+
{
324+
RenderOpaqueOnly(context);
325+
}
326+
327+
if (opaqueOnlyEffects > 1)
328+
cmd.ReleaseTemporaryRT(tempTarget1);
329+
330+
cmd.ReleaseTemporaryRT(tempTarget0);
331+
}
332+
333+
// Post-transparency stack
334+
int tempRt = m_TargetPool.Get();
335+
m_LegacyCmdBuffer.GetTemporaryRT(tempRt, context.width, context.height, 24, FilterMode.Bilinear, sourceFormat);
336+
m_LegacyCmdBuffer.Blit(cameraTarget, tempRt);
276337
context.command = m_LegacyCmdBuffer;
277338
context.source = new RenderTargetIdentifier(tempRt);
278-
context.destination = BuiltinRenderTextureType.CameraTarget;
339+
context.destination = cameraTarget;
279340
Render(context);
280341
m_LegacyCmdBuffer.ReleaseTemporaryRT(tempRt);
281342
}
@@ -408,49 +469,6 @@ void UpdateSettingsIfNeeded(PostProcessRenderContext context)
408469
m_SettingsUpdateNeeded = false;
409470
}
410471

411-
// Only used in vanilla render pipelines - fog & ambient occlusion
412-
// Returns true if an effect was rendered in the opaque-only command buffer
413-
void RenderLightingEffects(PostProcessRenderContext context)
414-
{
415-
// TODO: Optimize this, lots of useless passes and resolve going on here
416-
//>>>
417-
bool isAmbientOcclusionActive = ambientOcclusion.IsEnabledAndSupported(context);
418-
bool isAmbientOcclusionDeferred = ambientOcclusion.IsAmbientOnly(context);
419-
bool isFogActive = fog.IsEnabledAndSupported(context);
420-
421-
if (isAmbientOcclusionActive && isAmbientOcclusionDeferred)
422-
{
423-
context.command = m_LegacyCmdBufferBeforeReflections;
424-
ambientOcclusion.RenderAmbientOnly(context);
425-
}
426-
else if (isAmbientOcclusionActive)
427-
{
428-
var cmd = m_LegacyCmdBufferOpaque;
429-
int tempRt = m_TargetPool.Get();
430-
cmd.GetTemporaryRT(tempRt, context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
431-
cmd.Blit(BuiltinRenderTextureType.CameraTarget, tempRt);
432-
context.command = cmd;
433-
context.source = tempRt;
434-
context.destination = BuiltinRenderTextureType.CameraTarget;
435-
ambientOcclusion.RenderAfterOpaque(context);
436-
cmd.ReleaseTemporaryRT(tempRt);
437-
}
438-
439-
if (isFogActive)
440-
{
441-
var cmd = m_LegacyCmdBufferOpaque;
442-
int tempRt = m_TargetPool.Get();
443-
cmd.GetTemporaryRT(tempRt, context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
444-
cmd.Blit(BuiltinRenderTextureType.CameraTarget, tempRt);
445-
context.command = cmd;
446-
context.source = tempRt;
447-
context.destination = BuiltinRenderTextureType.CameraTarget;
448-
fog.Render(context);
449-
cmd.ReleaseTemporaryRT(tempRt);
450-
}
451-
//<<<
452-
}
453-
454472
// Renders before-transparent effects.
455473
// Make sure you check `HasOpaqueOnlyEffects()` before calling this method as it won't
456474
// automatically blit source into destination if no opaque effects are active.

PostProcessing/Runtime/PostProcessRenderContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using UnityEngine.Rendering;
2-
31
namespace UnityEngine.Rendering.PostProcessing
42
{
53
// Context object passed around all post-fx in a frame

0 commit comments

Comments
 (0)