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

Commit 4f9ea06

Browse files
authored
Merge pull request #441 from Unity-Technologies/shader-stripping
Shader stripping
2 parents c0da412 + b4c5ac2 commit 4f9ea06

21 files changed

+515
-8
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
using UnityEditor.Build;
2+
using UnityEngine;
3+
using UnityEngine.SceneManagement;
4+
using UnityEngine.Rendering.PostProcessing;
5+
6+
namespace UnityEditor.Rendering.PostProcessing
7+
{
8+
public sealed class PostProcessResourceStripper : ScriptableObject
9+
{
10+
public const string DefaultStrippingConfigAssetPath = "Assets/PostProcessStrippingConfig.asset";
11+
12+
PostProcessStrippingConfig stripping;
13+
PostProcessStrippingConfig defaultConfig;
14+
15+
[SerializeField]
16+
PostProcessResources unstrippedResources;
17+
18+
static PostProcessResourceStripper s_Instance;
19+
20+
public static PostProcessResourceStripper instance
21+
{
22+
get
23+
{
24+
if (s_Instance == null)
25+
{
26+
s_Instance = CreateInstance<PostProcessResourceStripper>();
27+
s_Instance.defaultConfig = CreateInstance<PostProcessStrippingConfig>();
28+
}
29+
30+
return s_Instance;
31+
}
32+
}
33+
34+
void Awake()
35+
{
36+
SceneManager.sceneLoaded += OnSceneLoaded;
37+
}
38+
39+
void OnDestroy()
40+
{
41+
SceneManager.sceneLoaded -= OnSceneLoaded;
42+
}
43+
44+
static void StripMultiScaleAO(PostProcessResources resources)
45+
{
46+
resources.computeShaders.multiScaleAODownsample1 = null;
47+
resources.computeShaders.multiScaleAODownsample2 = null;
48+
resources.computeShaders.multiScaleAORender = null;
49+
resources.computeShaders.multiScaleAOUpsample = null;
50+
resources.shaders.multiScaleAO = null;
51+
}
52+
53+
static void StripScreenSpaceReflections(PostProcessResources resources)
54+
{
55+
resources.shaders.screenSpaceReflections = null;
56+
resources.computeShaders.gaussianDownsample = null;
57+
}
58+
59+
static void StripDebugShaders(PostProcessResources resources)
60+
{
61+
resources.shaders.lightMeter = null;
62+
resources.shaders.gammaHistogram = null;
63+
resources.shaders.waveform = null;
64+
resources.shaders.vectorscope = null;
65+
resources.shaders.debugOverlays = null;
66+
67+
resources.computeShaders.gammaHistogram = null;
68+
resources.computeShaders.waveform = null;
69+
resources.computeShaders.vectorscope = null;
70+
}
71+
72+
static string FindPostProcessStrippingConfigGUID()
73+
{
74+
var guids = AssetDatabase.FindAssets("t:PostProcessStrippingConfig", null);
75+
if (guids.Length > 0)
76+
return guids[0];
77+
else
78+
return null;
79+
}
80+
81+
static public void EnsurePostProcessStrippingConfigAssetExists()
82+
{
83+
var guid = FindPostProcessStrippingConfigGUID();
84+
if (guid != null)
85+
return;
86+
87+
AssetDatabase.CreateAsset(CreateInstance<PostProcessStrippingConfig>(), DefaultStrippingConfigAssetPath);
88+
AssetDatabase.SaveAssets();
89+
AssetDatabase.Refresh();
90+
}
91+
92+
void LazyLoadStrippingConfig()
93+
{
94+
if (stripping != null)
95+
return;
96+
97+
var guid = FindPostProcessStrippingConfigGUID();
98+
if (guid != null)
99+
{
100+
stripping = (PostProcessStrippingConfig) AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(PostProcessStrippingConfig));
101+
}
102+
103+
if (stripping == null)
104+
stripping = defaultConfig;
105+
}
106+
107+
void SetConfig(PostProcessStrippingConfig config)
108+
{
109+
if (config == stripping)
110+
return;
111+
112+
if (defaultConfig == null)
113+
return;
114+
115+
if (config == defaultConfig)
116+
return;
117+
118+
if (config == null)
119+
{
120+
stripping = defaultConfig;
121+
return;
122+
}
123+
124+
stripping = config;
125+
}
126+
127+
void Apply(BuildTarget target, PostProcessResources resources)
128+
{
129+
if (defaultConfig == null)
130+
return;
131+
132+
LazyLoadStrippingConfig();
133+
if (stripping == null)
134+
return;
135+
136+
if (unstrippedResources == null)
137+
return;
138+
139+
if (resources == null)
140+
return;
141+
142+
resources.computeShaders = unstrippedResources.computeShaders.Clone();
143+
resources.shaders = unstrippedResources.shaders.Clone();
144+
145+
// We don't support multi scale AO on mobile
146+
if (stripping.stripUnsupportedShaders &&
147+
(target == BuildTarget.Android || target == BuildTarget.iOS || target == BuildTarget.tvOS))
148+
{
149+
StripMultiScaleAO(resources);
150+
}
151+
152+
if (stripping.stripDebugShaders)
153+
{
154+
StripDebugShaders(resources);
155+
}
156+
157+
if (stripping.stripComputeShaders)
158+
{
159+
resources.computeShaders = new PostProcessResources.ComputeShaders();
160+
resources.shaders.autoExposure = null;
161+
StripScreenSpaceReflections(resources);
162+
StripMultiScaleAO(resources);
163+
StripDebugShaders(resources);
164+
}
165+
166+
if (stripping.stripUnsupportedShaders && !RuntimeUtilities.supportsDeferredShading)
167+
{
168+
StripScreenSpaceReflections(resources);
169+
resources.shaders.deferredFog = null;
170+
if (!RuntimeUtilities.supportsDepthNormals)
171+
resources.shaders.scalableAO = null;
172+
}
173+
174+
if (stripping.stripUnsupportedShaders && !SystemInfo.supportsMotionVectors)
175+
{
176+
resources.shaders.motionBlur = null;
177+
resources.shaders.temporalAntialiasing = null;
178+
resources.shaders.screenSpaceReflections = null;
179+
resources.computeShaders.gaussianDownsample = null;
180+
}
181+
}
182+
183+
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
184+
{
185+
StripAll();
186+
}
187+
188+
public static void Strip(PostProcessResources resources)
189+
{
190+
instance.Apply(EditorUserBuildSettings.activeBuildTarget, resources);
191+
}
192+
193+
public static void StripAll(BuildTarget target)
194+
{
195+
var allResources = PostProcessResourcesFactory.AllResources();
196+
if (allResources == null)
197+
return;
198+
199+
foreach (var resources in allResources)
200+
instance.Apply(EditorUserBuildSettings.activeBuildTarget, resources);
201+
}
202+
203+
public static void StripAll()
204+
{
205+
StripAll(EditorUserBuildSettings.activeBuildTarget);
206+
}
207+
208+
public static void StripAll(PostProcessStrippingConfig config)
209+
{
210+
instance.SetConfig(config);
211+
StripAll(EditorUserBuildSettings.activeBuildTarget);
212+
}
213+
}
214+
215+
#if UNITY_2017_1_OR_NEWER
216+
sealed class UpdateStrippingOnBuildTargetChange : IActiveBuildTargetChanged
217+
{
218+
public int callbackOrder { get { return 0; } }
219+
public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
220+
{
221+
PostProcessResourceStripper.StripAll(newTarget);
222+
}
223+
}
224+
225+
sealed class UpdateStrippingBeforeBuild : IPreprocessBuild
226+
{
227+
public int callbackOrder { get { return 0; } }
228+
229+
#if UNITY_2018_1_OR_NEWER
230+
public void OnPreprocessBuild(Build.Reporting.BuildReport report)
231+
{
232+
PostProcessResourceStripper.StripAll(report.summary.platform);
233+
}
234+
#else
235+
public void OnPreprocessBuild(BuildTarget target, string path)
236+
{
237+
PostProcessResourceStripper.StripAll(target);
238+
}
239+
#endif
240+
}
241+
#endif
242+
243+
[InitializeOnLoad]
244+
public class SetupStripping
245+
{
246+
static SetupStripping()
247+
{
248+
PostProcessResourceStripper.EnsurePostProcessStrippingConfigAssetExists();
249+
PostProcessResourcesFactory.Init(PostProcessResourceStripper.Strip);
250+
}
251+
}
252+
}

PostProcessing/Editor/PostProcessResourceStripper.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEditor.Rendering.PostProcessing;
4+
using UnityEngine.Rendering.PostProcessing;
5+
6+
namespace UnityEditor.Rendering.PostProcessing
7+
{
8+
public sealed class PostProcessStrippingConfig : ScriptableObject
9+
{
10+
public bool stripUnsupportedShaders = true;
11+
public bool stripDebugShaders = false;
12+
public bool stripComputeShaders = false;
13+
14+
[Serializable]
15+
public sealed class Result
16+
{
17+
public PostProcessResources.Shaders shaders;
18+
public PostProcessResources.ComputeShaders computeShaders;
19+
}
20+
21+
public Result result;
22+
23+
private PostProcessResources strippedResources;
24+
25+
public void Awake()
26+
{
27+
PostProcessResourceStripper.StripAll(this);
28+
UpdateResult();
29+
}
30+
31+
public void OnValidate()
32+
{
33+
PostProcessResourceStripper.StripAll(this);
34+
UpdateResult();
35+
}
36+
37+
private void UpdateResult()
38+
{
39+
if (result == null)
40+
result = new Result();
41+
42+
PostProcessResources resources = PostProcessResourcesFactory.StrippedDefaultResources();
43+
result.shaders = resources.shaders;
44+
result.computeShaders = resources.computeShaders;
45+
DestroyImmediate(resources);
46+
}
47+
}
48+
49+
}

PostProcessing/Editor/PostProcessStrippingConfig.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
613 Bytes
Binary file not shown.

PostProcessing/Runtime/Effects/AmbientOcclusion.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,20 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
7171

7272
if (mode.value == AmbientOcclusionMode.ScalableAmbientObscurance)
7373
{
74-
state &= !RuntimeUtilities.scriptableRenderPipelineActive;
74+
state &= !RuntimeUtilities.scriptableRenderPipelineActive
75+
&& context.resources.shaders.scalableAO
76+
&& context.resources.shaders.scalableAO.isSupported;
7577
}
7678
else if (mode.value == AmbientOcclusionMode.MultiScaleVolumetricObscurance)
7779
{
7880
#if UNITY_2017_1_OR_NEWER
7981
state &= SystemInfo.supportsComputeShaders
82+
&& context.resources.shaders.multiScaleAO
83+
&& context.resources.shaders.multiScaleAO.isSupported
84+
&& context.resources.computeShaders.multiScaleAODownsample1
85+
&& context.resources.computeShaders.multiScaleAODownsample2
86+
&& context.resources.computeShaders.multiScaleAORender
87+
&& context.resources.computeShaders.multiScaleAOUpsample
8088
&& RenderTextureFormat.RFloat.IsSupported()
8189
&& RenderTextureFormat.RHalf.IsSupported()
8290
&& RenderTextureFormat.R8.IsSupported();

PostProcessing/Runtime/Effects/AutoExposure.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
4040
{
4141
return enabled.value
4242
&& SystemInfo.supportsComputeShaders
43-
&& RenderTextureFormat.RFloat.IsSupported();
43+
&& RenderTextureFormat.RFloat.IsSupported()
44+
&& context.resources.shaders.autoExposure
45+
&& context.resources.shaders.autoExposure.isSupported
46+
&& context.resources.computeShaders.exposureHistogram;
4447
}
4548
}
4649

PostProcessing/Runtime/Effects/ColorGrading.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public override void Render(PostProcessRenderContext context)
176176
var gradingMode = settings.gradingMode.value;
177177
var supportComputeTex3D = SystemInfo.supports3DRenderTextures
178178
&& SystemInfo.supportsComputeShaders
179+
&& context.resources.computeShaders.lut3DBaker != null
179180
&& SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLCore
180181
&& SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES3;
181182

PostProcessing/Runtime/Effects/Fog.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ internal bool IsEnabledAndSupported(PostProcessRenderContext context)
2121
return enabled
2222
&& RenderSettings.fog
2323
&& !RuntimeUtilities.scriptableRenderPipelineActive
24+
&& context.resources.shaders.deferredFog
25+
&& context.resources.shaders.deferredFog.isSupported
2426
&& context.camera.actualRenderingPath == RenderingPath.DeferredShading; // In forward fog is already done at shader level
2527
}
2628

PostProcessing/Runtime/Effects/ScreenSpaceReflections.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public override bool IsEnabledAndSupported(PostProcessRenderContext context)
5252
&& context.camera.actualRenderingPath == RenderingPath.DeferredShading
5353
&& SystemInfo.supportsMotionVectors
5454
&& SystemInfo.supportsComputeShaders
55-
&& SystemInfo.copyTextureSupport > CopyTextureSupport.None;
55+
&& SystemInfo.copyTextureSupport > CopyTextureSupport.None
56+
&& context.resources.shaders.screenSpaceReflections
57+
&& context.resources.shaders.screenSpaceReflections.isSupported
58+
&& context.resources.computeShaders.gaussianDownsample;
5659
}
5760
}
5861

0 commit comments

Comments
 (0)