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

Commit 2c04d49

Browse files
committed
Change stripping implementation so that no files of the PostProcessing package are modified
StrippingConfig asset is lazily created in Assets dir if it doesn't exist PostProcessLayer calls stripper on its resources instead of setting its default reference to a stripped resources asset.
1 parent aa0fb8e commit 2c04d49

12 files changed

+252
-59
lines changed
Lines changed: 106 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
using System;
22
using UnityEditor.Build;
33
using UnityEngine;
4+
using UnityEngine.SceneManagement;
45
using UnityEngine.Rendering.PostProcessing;
56

67
namespace UnityEditor.Rendering.PostProcessing
78
{
89
public sealed class PostProcessResourceStripper : ScriptableObject
910
{
10-
[SerializeField] private PostProcessResources resources;
11+
private PostProcessStrippingConfig stripping;
12+
private PostProcessStrippingConfig defaultConfig;
13+
1114
[SerializeField] private PostProcessResources unstrippedResources;
12-
[SerializeField] private PostProcessStrippingConfig stripping;
1315

1416
static PostProcessResourceStripper s_Instance;
1517

@@ -20,19 +22,25 @@ public static PostProcessResourceStripper instance
2022
if (s_Instance == null)
2123
{
2224
s_Instance = CreateInstance<PostProcessResourceStripper>();
23-
s_Instance.unstrippedResources.changeHandler = Update;
25+
s_Instance.defaultConfig = CreateInstance<PostProcessStrippingConfig>();
2426
}
2527

2628
return s_Instance;
2729
}
2830
}
2931

32+
void Awake()
33+
{
34+
SceneManager.sceneLoaded += OnSceneLoaded;
35+
36+
}
37+
3038
void OnDestroy()
3139
{
32-
unstrippedResources.changeHandler = null;
40+
SceneManager.sceneLoaded -= OnSceneLoaded;
3341
}
3442

35-
private void StripMultiScaleAO()
43+
static private void StripMultiScaleAO(PostProcessResources resources)
3644
{
3745
resources.computeShaders.multiScaleAODownsample1 = null;
3846
resources.computeShaders.multiScaleAODownsample2 = null;
@@ -41,13 +49,13 @@ private void StripMultiScaleAO()
4149
resources.shaders.multiScaleAO = null;
4250
}
4351

44-
private void StripScreenSpaceReflections()
52+
static private void StripScreenSpaceReflections(PostProcessResources resources)
4553
{
4654
resources.shaders.screenSpaceReflections = null;
4755
resources.computeShaders.gaussianDownsample = null;
4856
}
4957

50-
private void StripDebugShaders()
58+
static private void StripDebugShaders(PostProcessResources resources)
5159
{
5260
resources.shaders.lightMeter = null;
5361
resources.shaders.gammaHistogram = null;
@@ -60,44 +68,90 @@ private void StripDebugShaders()
6068
resources.computeShaders.vectorscope = null;
6169
}
6270

63-
private void Apply(BuildTarget target)
71+
private void LazyInitStrippingConfig()
6472
{
65-
if (resources == null)
73+
if (stripping != null)
6674
return;
6775

68-
if (unstrippedResources == null)
76+
var guids = AssetDatabase.FindAssets("t:PostProcessStrippingConfig", null);
77+
if (guids.Length > 0)
78+
{
79+
stripping = (PostProcessStrippingConfig) AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[0]), typeof(PostProcessStrippingConfig));
80+
}
81+
else
82+
{
83+
// Create a new config asset
84+
AssetDatabase.CreateAsset(defaultConfig, "Assets/PostProcessStrippingConfig.asset");
85+
AssetDatabase.SaveAssets();
86+
AssetDatabase.Refresh();
87+
}
88+
89+
if (stripping == null)
90+
stripping = defaultConfig;
91+
}
92+
93+
private void SetConfig(PostProcessStrippingConfig config)
94+
{
95+
if (config == stripping)
96+
return;
97+
98+
if (defaultConfig == null)
99+
return;
100+
101+
if (config == defaultConfig)
102+
return;
103+
104+
if (config == null)
105+
{
106+
stripping = defaultConfig;
107+
return;
108+
}
109+
110+
stripping = config;
111+
}
112+
113+
private void Apply(BuildTarget target, PostProcessResources resources)
114+
{
115+
if (defaultConfig == null)
69116
return;
70117

118+
LazyInitStrippingConfig();
71119
if (stripping == null)
72120
return;
73121

122+
if (unstrippedResources == null)
123+
return;
124+
125+
if (resources == null)
126+
return;
127+
74128
resources.computeShaders = unstrippedResources.computeShaders.Clone();
75129
resources.shaders = unstrippedResources.shaders.Clone();
76130

77131
// We don't support multi scale AO on mobile
78132
if (stripping.stripUnsupportedShaders &&
79133
(target == BuildTarget.Android || target == BuildTarget.iOS || target == BuildTarget.tvOS))
80134
{
81-
StripMultiScaleAO();
135+
StripMultiScaleAO(resources);
82136
}
83137

84138
if (stripping.stripDebugShaders)
85139
{
86-
StripDebugShaders();
140+
StripDebugShaders(resources);
87141
}
88142

89143
if (stripping.stripComputeShaders)
90144
{
91145
resources.computeShaders = new PostProcessResources.ComputeShaders();
92146
resources.shaders.autoExposure = null;
93-
StripScreenSpaceReflections();
94-
StripMultiScaleAO();
95-
StripDebugShaders();
147+
StripScreenSpaceReflections(resources);
148+
StripMultiScaleAO(resources);
149+
StripDebugShaders(resources);
96150
}
97151

98152
if (stripping.stripUnsupportedShaders && !RuntimeUtilities.supportsDeferredShading)
99153
{
100-
StripScreenSpaceReflections();
154+
StripScreenSpaceReflections(resources);
101155
resources.shaders.deferredFog = null;
102156
if (!RuntimeUtilities.supportsDepthNormals)
103157
resources.shaders.scalableAO = null;
@@ -112,14 +166,35 @@ private void Apply(BuildTarget target)
112166
}
113167
}
114168

115-
public static void Update()
169+
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
116170
{
117-
Update(EditorUserBuildSettings.activeBuildTarget);
171+
StripAll();
118172
}
119173

120-
public static void Update(BuildTarget target)
174+
public static void Strip(PostProcessResources resources)
121175
{
122-
instance.Apply(EditorUserBuildSettings.activeBuildTarget);
176+
instance.Apply(EditorUserBuildSettings.activeBuildTarget, resources);
177+
}
178+
179+
public static void StripAll(BuildTarget target)
180+
{
181+
var allResources = PostProcessResourcesFactory.AllResources();
182+
if (allResources == null)
183+
return;
184+
185+
foreach (var resources in allResources)
186+
instance.Apply(EditorUserBuildSettings.activeBuildTarget, resources);
187+
}
188+
189+
public static void StripAll()
190+
{
191+
StripAll(EditorUserBuildSettings.activeBuildTarget);
192+
}
193+
194+
public static void StripAll(PostProcessStrippingConfig config)
195+
{
196+
instance.SetConfig(config);
197+
StripAll(EditorUserBuildSettings.activeBuildTarget);
123198
}
124199
}
125200

@@ -129,7 +204,7 @@ sealed class UpdateStrippingOnBuildTargetChange : IActiveBuildTargetChanged
129204
public int callbackOrder { get { return 0; } }
130205
public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
131206
{
132-
PostProcessResourceStripper.Update(newTarget);
207+
PostProcessResourceStripper.StripAll(newTarget);
133208
}
134209
}
135210

@@ -140,15 +215,23 @@ sealed class UpdateStrippingBeforeBuild : IPreprocessBuild
140215
#if UNITY_2018_1_OR_NEWER
141216
public void OnPreprocessBuild(Build.Reporting.BuildReport report)
142217
{
143-
PostProcessResourceStripper.Update(report.summary.platform);
218+
PostProcessResourceStripper.StripAll(report.summary.platform);
144219
}
145220
#else
146221
public void OnPreprocessBuild(BuildTarget target, string path)
147222
{
148-
PostProcessResourceStripper.Update(target);
223+
PostProcessResourceStripper.StripAll(target);
149224
}
150225
#endif
151226
}
152227
#endif
153228

229+
[InitializeOnLoad]
230+
public class SetupStripping {
231+
static SetupStripping()
232+
{
233+
PostProcessResourcesFactory.Init(PostProcessResourceStripper.Strip);
234+
}
235+
}
236+
154237
}

PostProcessing/Editor/PostProcessResourceStripper.cs.meta

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

46
namespace UnityEditor.Rendering.PostProcessing
57
{
@@ -9,14 +11,39 @@ public sealed class PostProcessStrippingConfig : ScriptableObject
911
public bool stripDebugShaders = false;
1012
public bool stripComputeShaders = false;
1113

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+
1225
public void Awake()
1326
{
14-
PostProcessResourceStripper.Update();
27+
PostProcessResourceStripper.StripAll(this);
28+
UpdateResult();
1529
}
1630

1731
public void OnValidate()
1832
{
19-
PostProcessResourceStripper.Update();
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);
2046
}
2147
}
48+
2249
}
605 Bytes
Binary file not shown.
Binary file not shown.

PostProcessing/PostProcessResourcesUnstripped.asset.meta

Lines changed: 0 additions & 10 deletions
This file was deleted.
-4.11 KB
Binary file not shown.

PostProcessing/PostProcessStrippingConfig.asset.meta

Lines changed: 0 additions & 10 deletions
This file was deleted.

PostProcessing/Runtime/PostProcessLayer.cs

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

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

150153
RuntimeUtilities.CreateIfNull(ref temporalAntialiasing);

PostProcessing/Runtime/PostProcessResources.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ public sealed class SMAALuts
7777
public Shaders shaders;
7878
public ComputeShaders computeShaders;
7979

80-
#if UNITY_EDITOR
81-
public delegate void ChangeHandler();
82-
public ChangeHandler changeHandler;
83-
84-
void OnValidate()
80+
public PostProcessResources StrippableClone()
8581
{
86-
if (changeHandler != null)
87-
changeHandler();
82+
PostProcessResources clone = CreateInstance<PostProcessResources>();
83+
clone.blueNoise64 = (Texture2D[]) blueNoise64.Clone();
84+
clone.blueNoise256 = (Texture2D[]) blueNoise256.Clone();
85+
clone.smaaLuts = smaaLuts;
86+
clone.shaders = shaders.Clone();
87+
clone.computeShaders = computeShaders.Clone();
88+
return clone;
8889
}
89-
#endif
9090
}
9191

9292
}

0 commit comments

Comments
 (0)