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

Commit a61f862

Browse files
committed
Optimized AO render passes & fixed a serialization issue that resulted in a nullref
1 parent 36eac35 commit a61f862

File tree

6 files changed

+27
-49
lines changed

6 files changed

+27
-49
lines changed

PostProcessing/Runtime/Effects/MultiScaleVO.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ enum Pass
3838
{
3939
DepthCopy,
4040
CompositionDeferred,
41-
CompositeForward
41+
CompositionForward
4242
}
4343

4444
PropertySheet m_PropertySheet;
@@ -236,11 +236,9 @@ public bool IsSupported(PostProcessRenderContext context)
236236

237237
void DoLazyInitialization(PostProcessRenderContext context)
238238
{
239-
if (m_PropertySheet == null)
240-
{
241-
var shader = context.resources.shaders.multiScaleAO;
242-
m_PropertySheet = context.propertySheets.Get(shader);
243-
}
239+
var shader = context.resources.shaders.multiScaleAO;
240+
m_PropertySheet = context.propertySheets.Get(shader);
241+
m_PropertySheet.ClearKeywords();
244242

245243
// Render texture handles
246244
if (m_Result == null)
@@ -281,7 +279,6 @@ void RebuildCommandBuffers(PostProcessRenderContext context)
281279
context.height
282280
);
283281

284-
m_PropertySheet.ClearKeywords();
285282
m_PropertySheet.properties.SetVector(ShaderIDs.AOColor, Color.white - color);
286283

287284
#if !UNITY_2017_1_OR_NEWER
@@ -556,7 +553,7 @@ public void RenderAfterOpaque(PostProcessRenderContext context)
556553

557554
RebuildCommandBuffers(context);
558555
cmd.SetGlobalTexture(ShaderIDs.MSVOcclusionTexture, m_Result.id);
559-
cmd.BlitFullscreenTriangle(context.source, context.destination, sheet, (int)Pass.CompositeForward);
556+
cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, BuiltinRenderTextureType.CameraTarget, m_PropertySheet, (int)Pass.CompositionForward);
560557
cmd.EndSample("Ambient Occlusion");
561558
}
562559

@@ -573,20 +570,21 @@ public void CompositeAmbientOnly(PostProcessRenderContext context)
573570
{
574571
var cmd = context.command;
575572
cmd.BeginSample("Ambient Occlusion Composite");
576-
cmd.SetRenderTarget(m_MRT, BuiltinRenderTextureType.CameraTarget);
577573
cmd.SetGlobalTexture(ShaderIDs.MSVOcclusionTexture, m_Result.id);
578-
cmd.DrawProcedural(Matrix4x4.identity, m_PropertySheet.material, (int)Pass.CompositionDeferred, MeshTopology.Triangles, 3, 1, m_PropertySheet.properties);
574+
cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, m_MRT, BuiltinRenderTextureType.CameraTarget, m_PropertySheet, (int)Pass.CompositionDeferred);
579575
cmd.EndSample("Ambient Occlusion Composite");
580576
}
581577

582578
public void Release()
583579
{
584580
if (m_Result != null)
585581
{
582+
#if !UNITY_2017_1_OR_NEWER
586583
m_TiledDepth1.Destroy();
587584
m_TiledDepth2.Destroy();
588585
m_TiledDepth3.Destroy();
589586
m_TiledDepth4.Destroy();
587+
#endif
590588
m_Result.Destroy();
591589
}
592590

@@ -595,6 +593,7 @@ public void Release()
595593
m_TiledDepth3 = null;
596594
m_TiledDepth4 = null;
597595
m_Result = null;
596+
598597
}
599598
}
600599
}

PostProcessing/Runtime/Effects/ScalableAO.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public bool IsSupported(PostProcessRenderContext context)
6464

6565
void DoLazyInitialization(PostProcessRenderContext context)
6666
{
67-
if (m_PropertySheet == null)
68-
m_PropertySheet = context.propertySheets.Get(context.resources.shaders.scalableAO);
67+
m_PropertySheet = context.propertySheets.Get(context.resources.shaders.scalableAO);
6968

7069
bool reset = false;
7170

@@ -168,7 +167,7 @@ public void RenderAfterOpaque(PostProcessRenderContext context)
168167
cmd.BeginSample("Ambient Occlusion");
169168
Render(context, cmd, 0);
170169
cmd.SetGlobalTexture(ShaderIDs.SAOcclusionTexture, m_Result);
171-
cmd.BlitFullscreenTriangle(context.source, context.destination, m_PropertySheet, (int)Pass.CompositionForward);
170+
cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, BuiltinRenderTextureType.CameraTarget, m_PropertySheet, (int)Pass.CompositionForward);
172171
cmd.EndSample("Ambient Occlusion");
173172
}
174173

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ void OnPreCull()
310310
}
311311
else if (isAmbientOcclusionOpaque)
312312
{
313-
opaqueOnlyEffects++;
313+
context.command = m_LegacyCmdBufferOpaque;
314+
ambientOcclusion.Get().RenderAfterOpaque(context);
314315
}
315316

316317
opaqueOnlyEffects += isFogActive ? 1 : 0;
@@ -340,15 +341,6 @@ void OnPreCull()
340341
}
341342
else context.destination = cameraTarget;
342343

343-
if (isAmbientOcclusionOpaque)
344-
{
345-
ambientOcclusion.Get().RenderAfterOpaque(context);
346-
opaqueOnlyEffects--;
347-
var prevSource = context.source;
348-
context.source = context.destination;
349-
context.destination = opaqueOnlyEffects == 1 ? cameraTarget : prevSource;
350-
}
351-
352344
// TODO: Insert SSR here
353345

354346
if (isFogActive)

PostProcessing/Shaders/Builtins/MultiScaleVO.shader

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,18 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
55
#include "../StdLib.hlsl"
66
#include "Fog.hlsl"
77

8+
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
89
TEXTURE2D_SAMPLER2D(_MSVOcclusionTexture, sampler_MSVOcclusionTexture);
910
float3 _AOColor;
1011

11-
// Full screen triangle with procedural draw
12-
// This can't be used when the destination can be the back buffer because
13-
// this doesn't support the situations that requires vertical flipping.
14-
VaryingsDefault VertProcedural(uint vid : SV_VertexID)
12+
VaryingsDefault Vert(AttributesDefault v)
1513
{
16-
float x = vid == 1 ? 2 : 0;
17-
float y = vid > 1 ? 2 : 0;
18-
1914
VaryingsDefault o;
20-
o.vertex = float4(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
15+
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
16+
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
2117

2218
#if UNITY_UV_STARTS_AT_TOP
23-
o.texcoord = float2(x, y);
24-
#else
25-
o.texcoord = float2(x, 1.0 - y);
19+
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
2620
#endif
2721

2822
o.texcoord = TransformStereoScreenSpaceTex(o.texcoord, 1);
@@ -40,11 +34,9 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
4034
{
4135
HLSLPROGRAM
4236

43-
#pragma vertex VertProcedural
37+
#pragma vertex Vert
4438
#pragma fragment Frag
4539

46-
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
47-
4840
float4 Frag(VaryingsDefault i) : SV_Target
4941
{
5042
return SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord);
@@ -60,10 +52,9 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
6052

6153
HLSLPROGRAM
6254

63-
#pragma vertex VertProcedural
55+
#pragma vertex Vert
6456
#pragma fragment Frag
6557

66-
6758
struct Output
6859
{
6960
float4 gbuffer0 : SV_Target0;
@@ -85,15 +76,14 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
8576
// 2 - Composite to the frame buffer
8677
Pass
8778
{
79+
Blend Zero OneMinusSrcColor, Zero OneMinusSrcAlpha
80+
8881
HLSLPROGRAM
8982

9083
#pragma multi_compile _ FOG_LINEAR FOG_EXP FOG_EXP2
91-
#pragma vertex VertDefault
84+
#pragma vertex Vert
9285
#pragma fragment Frag
9386

94-
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
95-
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
96-
9787
float4 Frag(VaryingsDefault i) : SV_Target
9888
{
9989
float2 texcoord = TransformStereoScreenSpaceTex(i.texcoord, 1);
@@ -106,9 +96,7 @@ Shader "Hidden/PostProcessing/MultiScaleVO"
10696
ao *= ComputeFog(d);
10797
#endif
10898

109-
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, texcoord);
110-
color.rgb *= 1.0 - ao * _AOColor;
111-
return color;
99+
return float4(ao * _AOColor, 0.0);
112100
}
113101

114102
ENDHLSL

PostProcessing/Shaders/Builtins/ScalableAO.hlsl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,7 @@ float4 FragComposition(VaryingsDefault i) : SV_Target
385385
{
386386
float2 delta = _MainTex_TexelSize.xy / DOWNSAMPLE;
387387
half ao = BlurSmall(TEXTURE2D_PARAM(_SAOcclusionTexture, sampler_SAOcclusionTexture), i.texcoord, delta);
388-
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord));
389-
color.rgb *= 1.0 - EncodeAO(ao) * _AOColor;
390-
return color;
388+
return float4(EncodeAO(ao) * _AOColor, 0.0);
391389
}
392390

393391
#if !SHADER_API_GLES // Excluding the MRT pass under GLES2

PostProcessing/Shaders/Builtins/ScalableAO.shader

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Shader "Hidden/PostProcessing/ScalableAO"
8585
// 6 - Final composition
8686
Pass
8787
{
88+
Blend Zero OneMinusSrcColor, Zero OneMinusSrcAlpha
89+
8890
HLSLPROGRAM
8991

9092
#pragma vertex VertDefault

0 commit comments

Comments
 (0)