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

Commit 170dd41

Browse files
authored
Merge pull request #476 from Unity-Technologies/shader-stripping
Shader stripping (should fix #467, #474 and #476)
2 parents eacff27 + a75b6b7 commit 170dd41

10 files changed

+101
-260
lines changed
Lines changed: 69 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
using System;
12
using UnityEditor.Build;
23
using UnityEngine;
3-
using UnityEngine.SceneManagement;
44
using UnityEngine.Rendering.PostProcessing;
55

66
namespace UnityEditor.Rendering.PostProcessing
77
{
88
public sealed class PostProcessResourceStripper : ScriptableObject
99
{
10-
public const string DefaultStrippingConfigAssetPath = "Assets/PostProcessStrippingConfig.asset";
11-
12-
PostProcessStrippingConfig stripping;
13-
PostProcessStrippingConfig defaultConfig;
10+
[SerializeField] private PostProcessResources resources;
11+
[SerializeField] private PostProcessResources unstrippedResources;
12+
[SerializeField] private PostProcessStrippingConfig stripping;
1413

15-
[SerializeField]
16-
PostProcessResources unstrippedResources;
14+
public const string DefaultStrippingConfigAssetPath = "Assets/PostProcessStrippingConfig.asset";
15+
bool enabled = true;
1716

1817
static PostProcessResourceStripper s_Instance;
1918

@@ -24,52 +23,14 @@ public static PostProcessResourceStripper instance
2423
if (s_Instance == null)
2524
{
2625
s_Instance = CreateInstance<PostProcessResourceStripper>();
27-
s_Instance.defaultConfig = CreateInstance<PostProcessStrippingConfig>();
26+
s_Instance.unstrippedResources.changeHandler = Update;
2827
}
2928

3029
return s_Instance;
3130
}
3231
}
3332

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()
33+
static private string FindPostProcessStrippingConfigGUID()
7334
{
7435
var guids = AssetDatabase.FindAssets("t:PostProcessStrippingConfig", null);
7536
if (guids.Length > 0)
@@ -78,65 +39,82 @@ static string FindPostProcessStrippingConfigGUID()
7839
return null;
7940
}
8041

81-
static public void EnsurePostProcessStrippingConfigAssetExists()
42+
static public string EnsurePostProcessStrippingConfigAssetExists()
8243
{
8344
var guid = FindPostProcessStrippingConfigGUID();
8445
if (guid != null)
85-
return;
46+
return guid;
8647

48+
bool wasEnabled = instance.enabled;
49+
instance.enabled = false;
8750
AssetDatabase.CreateAsset(CreateInstance<PostProcessStrippingConfig>(), DefaultStrippingConfigAssetPath);
8851
AssetDatabase.SaveAssets();
8952
AssetDatabase.Refresh();
53+
instance.enabled = wasEnabled;
54+
return FindPostProcessStrippingConfigGUID();
9055
}
9156

92-
void LazyLoadStrippingConfig()
57+
private void LazyLoadStrippingConfig()
9358
{
9459
if (stripping != null)
9560
return;
9661

97-
var guid = FindPostProcessStrippingConfigGUID();
62+
var guid = EnsurePostProcessStrippingConfigAssetExists();
9863
if (guid != null)
9964
{
100-
stripping = (PostProcessStrippingConfig) AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(PostProcessStrippingConfig));
65+
bool wasEnabled = instance.enabled;
66+
instance.enabled = false;
67+
stripping = (PostProcessStrippingConfig)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(PostProcessStrippingConfig));
68+
instance.enabled = wasEnabled;
10169
}
102-
103-
if (stripping == null)
104-
stripping = defaultConfig;
10570
}
10671

107-
void SetConfig(PostProcessStrippingConfig config)
72+
void OnDestroy()
10873
{
109-
if (config == stripping)
110-
return;
74+
unstrippedResources.changeHandler = null;
75+
}
11176

112-
if (defaultConfig == null)
113-
return;
77+
private void StripMultiScaleAO()
78+
{
79+
resources.computeShaders.multiScaleAODownsample1 = null;
80+
resources.computeShaders.multiScaleAODownsample2 = null;
81+
resources.computeShaders.multiScaleAORender = null;
82+
resources.computeShaders.multiScaleAOUpsample = null;
83+
resources.shaders.multiScaleAO = null;
84+
}
11485

115-
if (config == defaultConfig)
116-
return;
86+
private void StripScreenSpaceReflections()
87+
{
88+
resources.shaders.screenSpaceReflections = null;
89+
resources.computeShaders.gaussianDownsample = null;
90+
}
11791

118-
if (config == null)
119-
{
120-
stripping = defaultConfig;
121-
return;
122-
}
92+
private void StripDebugShaders()
93+
{
94+
resources.shaders.lightMeter = null;
95+
resources.shaders.gammaHistogram = null;
96+
resources.shaders.waveform = null;
97+
resources.shaders.vectorscope = null;
98+
resources.shaders.debugOverlays = null;
12399

124-
stripping = config;
100+
resources.computeShaders.gammaHistogram = null;
101+
resources.computeShaders.waveform = null;
102+
resources.computeShaders.vectorscope = null;
125103
}
126104

127-
void Apply(BuildTarget target, PostProcessResources resources)
105+
private void Apply(BuildTarget target)
128106
{
129-
if (defaultConfig == null)
107+
if (!enabled)
130108
return;
131109

132-
LazyLoadStrippingConfig();
133-
if (stripping == null)
110+
if (resources == null)
134111
return;
135112

136113
if (unstrippedResources == null)
137114
return;
138115

139-
if (resources == null)
116+
LazyLoadStrippingConfig();
117+
if (stripping == null)
140118
return;
141119

142120
resources.computeShaders = unstrippedResources.computeShaders.Clone();
@@ -146,26 +124,26 @@ void Apply(BuildTarget target, PostProcessResources resources)
146124
if (stripping.stripUnsupportedShaders &&
147125
(target == BuildTarget.Android || target == BuildTarget.iOS || target == BuildTarget.tvOS))
148126
{
149-
StripMultiScaleAO(resources);
127+
StripMultiScaleAO();
150128
}
151129

152130
if (stripping.stripDebugShaders)
153131
{
154-
StripDebugShaders(resources);
132+
StripDebugShaders();
155133
}
156134

157135
if (stripping.stripComputeShaders)
158136
{
159137
resources.computeShaders = new PostProcessResources.ComputeShaders();
160138
resources.shaders.autoExposure = null;
161-
StripScreenSpaceReflections(resources);
162-
StripMultiScaleAO(resources);
163-
StripDebugShaders(resources);
139+
StripScreenSpaceReflections();
140+
StripMultiScaleAO();
141+
StripDebugShaders();
164142
}
165143

166144
if (stripping.stripUnsupportedShaders && !RuntimeUtilities.supportsDeferredShading)
167145
{
168-
StripScreenSpaceReflections(resources);
146+
StripScreenSpaceReflections();
169147
resources.shaders.deferredFog = null;
170148
if (!RuntimeUtilities.supportsDepthNormals)
171149
resources.shaders.scalableAO = null;
@@ -180,35 +158,14 @@ void Apply(BuildTarget target, PostProcessResources resources)
180158
}
181159
}
182160

183-
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
161+
public static void Update()
184162
{
185-
StripAll();
163+
Update(EditorUserBuildSettings.activeBuildTarget);
186164
}
187165

188-
public static void Strip(PostProcessResources resources)
166+
public static void Update(BuildTarget target)
189167
{
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);
168+
instance.Apply(EditorUserBuildSettings.activeBuildTarget);
212169
}
213170
}
214171

@@ -218,7 +175,7 @@ sealed class UpdateStrippingOnBuildTargetChange : IActiveBuildTargetChanged
218175
public int callbackOrder { get { return 0; } }
219176
public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
220177
{
221-
PostProcessResourceStripper.StripAll(newTarget);
178+
PostProcessResourceStripper.Update(newTarget);
222179
}
223180
}
224181

@@ -229,24 +186,26 @@ sealed class UpdateStrippingBeforeBuild : IPreprocessBuild
229186
#if UNITY_2018_1_OR_NEWER
230187
public void OnPreprocessBuild(Build.Reporting.BuildReport report)
231188
{
232-
PostProcessResourceStripper.StripAll(report.summary.platform);
189+
PostProcessResourceStripper.Update(report.summary.platform);
233190
}
191+
234192
#else
235193
public void OnPreprocessBuild(BuildTarget target, string path)
236194
{
237-
PostProcessResourceStripper.StripAll(target);
195+
PostProcessResourceStripper.Update(target);
238196
}
197+
239198
#endif
240199
}
241200
#endif
242201

202+
243203
[InitializeOnLoad]
244-
public class SetupStripping
204+
public class SetupStrippingConfig
245205
{
246-
static SetupStripping()
206+
static SetupStrippingConfig()
247207
{
248208
PostProcessResourceStripper.EnsurePostProcessStrippingConfigAssetExists();
249-
PostProcessResourcesFactory.Init(PostProcessResourceStripper.Strip);
250209
}
251210
}
252211
}

PostProcessing/Editor/PostProcessResourceStripper.cs.meta

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
21
using UnityEngine;
32
using UnityEditor.Rendering.PostProcessing;
4-
using UnityEngine.Rendering.PostProcessing;
53

64
namespace UnityEditor.Rendering.PostProcessing
75
{
@@ -11,39 +9,14 @@ public sealed class PostProcessStrippingConfig : ScriptableObject
119
public bool stripDebugShaders = false;
1210
public bool stripComputeShaders = false;
1311

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-
2512
public void Awake()
2613
{
27-
PostProcessResourceStripper.StripAll(this);
28-
UpdateResult();
14+
PostProcessResourceStripper.Update();
2915
}
3016

3117
public void OnValidate()
3218
{
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);
19+
PostProcessResourceStripper.Update();
4620
}
4721
}
48-
4922
}
-605 Bytes
Binary file not shown.
Binary file not shown.

PostProcessing/PostProcessResourcesUnstripped.asset.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ void InitLegacy()
145145

146146
public void Init(PostProcessResources resources)
147147
{
148-
#if UNITY_EDITOR
149-
m_Resources = PostProcessResourcesFactory.Stripped(m_Resources);
150-
#endif
151148
if (resources != null) m_Resources = resources;
152149

153150
RuntimeUtilities.CreateIfNull(ref temporalAntialiasing);

0 commit comments

Comments
 (0)