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

Commit 7a5e1e0

Browse files
authored
Merge pull request #475 from Unity-Technologies/more-mobile-optims
Optimizations
2 parents f4421bf + 775ae9c commit 7a5e1e0

File tree

7 files changed

+131
-58
lines changed

7 files changed

+131
-58
lines changed

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ void OnPreRender()
309309
void BuildCommandBuffers()
310310
{
311311
var context = m_CurrentContext;
312-
var sourceFormat = m_Camera.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;
312+
var sourceFormat = m_Camera.allowHDR ? RuntimeUtilities.defaultHDRRenderTextureFormat : RenderTextureFormat.Default;
313+
314+
if (!RuntimeUtilities.isFloatingPointFormat(sourceFormat))
315+
m_NaNKilled = true;
313316

314317
context.Reset();
315318
context.camera = m_Camera;
@@ -425,7 +428,8 @@ void BuildCommandBuffers()
425428
int tempRt = m_TargetPool.Get();
426429
context.GetScreenSpaceTemporaryRT(m_LegacyCmdBuffer, tempRt, 0, sourceFormat, RenderTextureReadWrite.sRGB);
427430
m_LegacyCmdBuffer.Blit(cameraTarget, tempRt, RuntimeUtilities.copyStdMaterial, stopNaNPropagation ? 1 : 0);
428-
m_NaNKilled = stopNaNPropagation;
431+
if (!m_NaNKilled)
432+
m_NaNKilled = stopNaNPropagation;
429433

430434
context.command = m_LegacyCmdBuffer;
431435
context.source = tempRt;
@@ -781,6 +785,19 @@ void RenderList(List<SerializedBundleRef> list, PostProcessRenderContext context
781785
cmd.EndSample(marker);
782786
}
783787

788+
void ApplyFlip(PostProcessRenderContext context, MaterialPropertyBlock properties)
789+
{
790+
if (context.flip && !context.isSceneView)
791+
properties.SetVector(ShaderIDs.UVTransform, new Vector4(1.0f, 1.0f, 0.0f, 0.0f));
792+
else
793+
ApplyDefaultFlip(properties);
794+
}
795+
796+
void ApplyDefaultFlip(MaterialPropertyBlock properties)
797+
{
798+
properties.SetVector(ShaderIDs.UVTransform, SystemInfo.graphicsUVStartsAtTop ? new Vector4(1.0f, -1.0f, 0.0f, 1.0f) : new Vector4(1.0f, 1.0f, 0.0f, 0.0f));
799+
}
800+
784801
int RenderBuiltins(PostProcessRenderContext context, bool isFinalPass, int releaseTargetAfterUse = -1)
785802
{
786803
var uberSheet = context.propertySheets.Get(context.resources.shaders.uber);
@@ -833,18 +850,18 @@ int RenderBuiltins(PostProcessRenderContext context, bool isFinalPass, int relea
833850
if (!breakBeforeColorGrading)
834851
RenderEffect<ColorGrading>(context);
835852

836-
int pass = 0;
837-
838853
if (isFinalPass)
839854
{
840855
uberSheet.EnableKeyword("FINALPASS");
841856
dithering.Render(context);
842-
843-
if (context.flip && !context.isSceneView)
844-
pass = 1;
857+
ApplyFlip(context, uberSheet.properties);
858+
}
859+
else
860+
{
861+
ApplyDefaultFlip(uberSheet.properties);
845862
}
846863

847-
cmd.BlitFullscreenTriangle(context.source, context.destination, uberSheet, pass);
864+
cmd.BlitFullscreenTriangle(context.source, context.destination, uberSheet, 0);
848865

849866
context.source = context.destination;
850867
context.destination = finalDestination;
@@ -901,7 +918,8 @@ void RenderFinalPass(PostProcessRenderContext context, int releaseTargetAfterUse
901918

902919
dithering.Render(context);
903920

904-
cmd.BlitFullscreenTriangle(context.source, context.destination, uberSheet, (context.flip && !context.isSceneView) ? 1 : 0);
921+
ApplyFlip(context, uberSheet.properties);
922+
cmd.BlitFullscreenTriangle(context.source, context.destination, uberSheet, 0);
905923

906924
if (tempTarget > -1)
907925
cmd.ReleaseTemporaryRT(tempTarget);

PostProcessing/Runtime/Utils/RuntimeUtilities.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using System.Text;
77
using UnityEngine.Assertions;
88

9+
#if UNITY_EDITOR
10+
using UnityEditor;
11+
#endif
12+
913
namespace UnityEngine.Rendering.PostProcessing
1014
{
1115
using SceneManagement;
@@ -283,6 +287,32 @@ public static bool isAndroidOpenGL
283287
get { return Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan; }
284288
}
285289

290+
public static RenderTextureFormat defaultHDRRenderTextureFormat
291+
{
292+
get
293+
{
294+
#if UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_SWITCH || UNITY_EDITOR
295+
RenderTextureFormat format = RenderTextureFormat.RGB111110Float;
296+
# if UNITY_EDITOR
297+
var target = EditorUserBuildSettings.activeBuildTarget;
298+
if (target != BuildTarget.Android && target != BuildTarget.iOS && target != BuildTarget.tvOS && target != BuildTarget.Switch)
299+
return RenderTextureFormat.DefaultHDR;
300+
# endif // UNITY_EDITOR
301+
if (format.IsSupported())
302+
return format;
303+
#endif // UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_SWITCH || UNITY_EDITOR
304+
return RenderTextureFormat.DefaultHDR;
305+
}
306+
}
307+
308+
public static bool isFloatingPointFormat(RenderTextureFormat format)
309+
{
310+
return format == RenderTextureFormat.DefaultHDR || format == RenderTextureFormat.ARGBHalf || format == RenderTextureFormat.ARGBFloat ||
311+
format == RenderTextureFormat.RGFloat || format == RenderTextureFormat.RGHalf ||
312+
format == RenderTextureFormat.RFloat || format == RenderTextureFormat.RHalf ||
313+
format == RenderTextureFormat.RGB111110Float;
314+
}
315+
286316
public static void Destroy(UnityObject obj)
287317
{
288318
if (obj != null)

PostProcessing/Runtime/Utils/ShaderIDs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,7 @@ static class ShaderIDs
146146
internal static readonly int VectorscopeBuffer = Shader.PropertyToID("_VectorscopeBuffer");
147147

148148
internal static readonly int RenderViewportScaleFactor = Shader.PropertyToID("_RenderViewportScaleFactor");
149+
150+
internal static readonly int UVTransform = Shader.PropertyToID("_UVTransform");
149151
}
150152
}

PostProcessing/Shaders/Builtins/FinalPass.shader

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ Shader "Hidden/PostProcessing/FinalPass"
2121
#endif
2222

2323
#if FXAA_LOW
24-
#define FXAA_QUALITY__PRESET 28
24+
#define FXAA_QUALITY__PRESET 12
2525
#define FXAA_QUALITY_SUBPIX 1.0
26-
#define FXAA_QUALITY_EDGE_THRESHOLD 0.125
26+
#define FXAA_QUALITY_EDGE_THRESHOLD 0.166
2727
#define FXAA_QUALITY_EDGE_THRESHOLD_MIN 0.0625
2828
#else
29-
#define FXAA_QUALITY__PRESET 39
29+
#define FXAA_QUALITY__PRESET 28
3030
#define FXAA_QUALITY_SUBPIX 1.0
3131
#define FXAA_QUALITY_EDGE_THRESHOLD 0.063
3232
#define FXAA_QUALITY_EDGE_THRESHOLD_MIN 0.0312
@@ -97,18 +97,7 @@ Shader "Hidden/PostProcessing/FinalPass"
9797
{
9898
HLSLPROGRAM
9999

100-
#pragma vertex VertDefault
101-
#pragma fragment Frag
102-
#pragma target 5.0
103-
104-
ENDHLSL
105-
}
106-
107-
Pass
108-
{
109-
HLSLPROGRAM
110-
111-
#pragma vertex VertDefaultNoFlip
100+
#pragma vertex VertUVTransform
112101
#pragma fragment Frag
113102
#pragma target 5.0
114103

@@ -124,18 +113,7 @@ Shader "Hidden/PostProcessing/FinalPass"
124113
{
125114
HLSLPROGRAM
126115

127-
#pragma vertex VertDefault
128-
#pragma fragment Frag
129-
#pragma target 3.0
130-
131-
ENDHLSL
132-
}
133-
134-
Pass
135-
{
136-
HLSLPROGRAM
137-
138-
#pragma vertex VertDefaultNoFlip
116+
#pragma vertex VertUVTransform
139117
#pragma fragment Frag
140118
#pragma target 3.0
141119

PostProcessing/Shaders/Builtins/Uber.shader

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Shader "Hidden/PostProcessing/Uber"
1111
#pragma multi_compile __ VIGNETTE
1212
#pragma multi_compile __ GRAIN
1313
#pragma multi_compile __ FINALPASS
14-
14+
1515
#include "../StdLib.hlsl"
1616
#include "../Colors.hlsl"
1717
#include "../Sampling.hlsl"
@@ -148,6 +148,7 @@ Shader "Hidden/PostProcessing/Uber"
148148

149149
#if VIGNETTE
150150
{
151+
UNITY_BRANCH
151152
if (_Vignette_Mode < 0.5)
152153
{
153154
half2 d = abs(uv - _Vignette_Center) * _Vignette_Settings.x;
@@ -243,20 +244,11 @@ Shader "Hidden/PostProcessing/Uber"
243244
{
244245
HLSLPROGRAM
245246

246-
#pragma vertex VertDefault
247+
#pragma vertex VertUVTransform
247248
#pragma fragment FragUber
248249

249250
ENDHLSL
250251
}
251252

252-
Pass
253-
{
254-
HLSLPROGRAM
255-
256-
#pragma vertex VertDefaultNoFlip
257-
#pragma fragment FragUber
258-
259-
ENDHLSL
260-
}
261253
}
262254
}

PostProcessing/Shaders/Colors.hlsl

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,39 @@
77
#define LUT_SPACE_ENCODE(x) LinearToLogC(x)
88
#define LUT_SPACE_DECODE(x) LogCToLinear(x)
99

10-
// Set to 1 to use more precise but more expensive log/linear conversions. I haven't found a proper
11-
// use case for the high precision version yet so I'm leaving this to 0.
12-
#define USE_PRECISE_LOGC 0
10+
#ifndef USE_PRECISE_LOGC
11+
// Set to 1 to use more precise but more expensive log/linear conversions. I haven't found a proper
12+
// use case for the high precision version yet so I'm leaving this to 0.
13+
#define USE_PRECISE_LOGC 0
14+
#endif
15+
16+
#ifndef TONEMAPPING_USE_FULL_ACES
17+
// Set to 1 to use the full reference ACES tonemapper. This should only be used for research
18+
// purposes as it's quite heavy and generally overkill.
19+
#define TONEMAPPING_USE_FULL_ACES 0
20+
#endif
21+
22+
#ifndef DEFAULT_MAX_PQ
23+
// PQ ST.2048 max value
24+
// 1.0 = 100nits, 100.0 = 10knits
25+
#define DEFAULT_MAX_PQ 100.0
26+
#endif
1327

14-
// Set to 1 to use the full reference ACES tonemapper. This should only be used for research
15-
// purposes as it's quite heavy and generally overkill.
16-
#define TONEMAPPING_USE_FULL_ACES 0
28+
#ifndef USE_VERY_FAST_SRGB
29+
#if defined(SHADER_API_MOBILE)
30+
#define USE_VERY_FAST_SRGB 1
31+
#else
32+
#define USE_VERY_FAST_SRGB 0
33+
#endif
34+
#endif
1735

18-
// PQ ST.2048 max value
19-
// 1.0 = 100nits, 100.0 = 10knits
20-
#define DEFAULT_MAX_PQ 100.0
36+
#ifndef USE_FAST_SRGB
37+
#if defined(SHADER_API_CONSOLE)
38+
#define USE_FAST_SRGB 1
39+
#else
40+
#define USE_FAST_SRGB 0
41+
#endif
42+
#endif
2143

2244
//
2345
// Alexa LogC converters (El 1000)
@@ -132,21 +154,34 @@ float3 PQToLinear(float3 x)
132154

133155
//
134156
// sRGB transfer functions
157+
// Fast path ref: http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
135158
//
136159
half SRGBToLinear(half c)
137160
{
161+
#if USE_VERY_FAST_SRGB
162+
return c * c;
163+
#elif USE_FAST_SRGB
164+
return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
165+
#else
138166
half linearRGBLo = c / 12.92;
139167
half linearRGBHi = PositivePow((c + 0.055) / 1.055, 2.4);
140168
half linearRGB = (c <= 0.04045) ? linearRGBLo : linearRGBHi;
141169
return linearRGB;
170+
#endif
142171
}
143172

144173
half3 SRGBToLinear(half3 c)
145174
{
175+
#if USE_VERY_FAST_SRGB
176+
return c * c;
177+
#elif USE_FAST_SRGB
178+
return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
179+
#else
146180
half3 linearRGBLo = c / 12.92;
147181
half3 linearRGBHi = PositivePow((c + 0.055) / 1.055, half3(2.4, 2.4, 2.4));
148182
half3 linearRGB = (c <= 0.04045) ? linearRGBLo : linearRGBHi;
149183
return linearRGB;
184+
#endif
150185
}
151186

152187
half4 SRGBToLinear(half4 c)
@@ -156,18 +191,30 @@ half4 SRGBToLinear(half4 c)
156191

157192
half LinearToSRGB(half c)
158193
{
194+
#if USE_VERY_FAST_SRGB
195+
return sqrt(c);
196+
#elif USE_FAST_SRGB
197+
return max(1.055 * PositivePow(c, 0.416666667) - 0.055, 0.0);
198+
#else
159199
half sRGBLo = c * 12.92;
160200
half sRGBHi = (PositivePow(c, 1.0 / 2.4) * 1.055) - 0.055;
161201
half sRGB = (c <= 0.0031308) ? sRGBLo : sRGBHi;
162202
return sRGB;
203+
#endif
163204
}
164205

165206
half3 LinearToSRGB(half3 c)
166207
{
208+
#if USE_VERY_FAST_SRGB
209+
return sqrt(c);
210+
#elif USE_FAST_SRGB
211+
return max(1.055 * PositivePow(c, 0.416666667) - 0.055, 0.0);
212+
#else
167213
half3 sRGBLo = c * 12.92;
168214
half3 sRGBHi = (PositivePow(c, half3(1.0 / 2.4, 1.0 / 2.4, 1.0 / 2.4)) * 1.055) - 0.055;
169215
half3 sRGB = (c <= 0.0031308) ? sRGBLo : sRGBHi;
170216
return sRGB;
217+
#endif
171218
}
172219

173220
half4 LinearToSRGB(half4 c)

PostProcessing/Shaders/StdLib.hlsl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include "API/OpenGL.hlsl"
2828
#endif
2929

30+
#if defined(SHADER_API_PSSL) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_SWITCH)
31+
#define SHADER_API_CONSOLE
32+
#endif
33+
3034
// -----------------------------------------------------------------------------
3135
// Constants
3236

@@ -278,11 +282,13 @@ VaryingsDefault VertDefault(AttributesDefault v)
278282
return o;
279283
}
280284

281-
VaryingsDefault VertDefaultNoFlip(AttributesDefault v)
285+
float4 _UVTransform; // xy: scale, wz: translate
286+
287+
VaryingsDefault VertUVTransform(AttributesDefault v)
282288
{
283289
VaryingsDefault o;
284290
o.vertex = float4(v.vertex.xy, 0.0, 1.0);
285-
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
291+
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy) * _UVTransform.xy + _UVTransform.zw;
286292
o.texcoordStereo = TransformStereoScreenSpaceTex(o.texcoord, 1.0);
287293
return o;
288294
}

0 commit comments

Comments
 (0)