Skip to content

Commit 56002b2

Browse files
committed
Add more shader stripping option for passes
Further Improve shader compile logging
1 parent 81c22f9 commit 56002b2

4 files changed

Lines changed: 103 additions & 17 deletions

File tree

Assets/Exporter Scripts/Editor/AssetBundleUnity3DEditor.cs

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.Common;
34
using System.IO;
45
using System.Linq;
56
using System.Runtime.InteropServices;
@@ -144,6 +145,8 @@ public override void OnInspectorGUI()
144145
"Search folders recursively");
145146
EditorGUI.EndDisabledGroup();
146147

148+
ToggleUI(ref script.DetailedLogging,
149+
"Enable Detailed Logging");
147150
ToggleUI(ref script.VerboseLogging,
148151
"Enable Verbose Logging");
149152

@@ -246,7 +249,10 @@ public override void OnInspectorGUI()
246249

247250
ToggleUI(ref script.StripInstancing,
248251
"Strip Instancing variants",
249-
"Should be safe to strip if you know that no material will use geometry instancing.");
252+
"Should be safe to strip if you know that no material will ever use geometry instancing.");
253+
ToggleUI(ref script.StripNonInstancing,
254+
"Strip Non-Instancing variants",
255+
"Should be safe to strip if you know that materials will always use geometry instancing.");
250256

251257
GUILayout.Space(8);
252258

@@ -263,6 +269,25 @@ public override void OnInspectorGUI()
263269
ToggleUI(ref script.StripLightmapSubtractive,
264270
"Strip Baked Subtractive Lightmap variants");
265271

272+
GUILayout.Space(8);
273+
274+
ToggleUI(ref script.StripLightForDeferred,
275+
"Strip Deferred vs Shadow Light Probe");
276+
277+
EditorGUI.BeginDisabledGroup(
278+
script.StripLightForDeferred);
279+
ToggleUI(ref script.StripLightprobeOn,
280+
"Strip Light Probe On variants");
281+
ToggleUI(ref script.StripLightprobeOff,
282+
"Strip Light Probe Off variants");
283+
EditorGUI.EndDisabledGroup();
284+
285+
GUILayout.Space(8);
286+
287+
ToggleUI(ref script.StripUnityHdrOn,
288+
"Strip Unity HDR On variants");
289+
ToggleUI(ref script.StripUnityHdrOff,
290+
"Strip Unity HDR Off variants");
266291
}
267292

268293
GUILayout.Space(12);
@@ -326,7 +351,7 @@ public override void OnInspectorGUI()
326351
foreach (var obj in script.Objects)
327352
CollectAssets(obj, ref assets,
328353
script.SearchFoldersRecursively);
329-
if (script.VerboseLogging)
354+
if (script.DetailedLogging)
330355
foreach (var asset in assets)
331356
Debug.LogFormat(
332357
" found {0}\n of type {1}\n",
@@ -462,7 +487,8 @@ private void ExportAssetBundle(UnityEngine.Object[] exports,
462487
var oldFogKeepLinear = gfxCfg.FindProperty("m_FogKeepLinear").boolValue;
463488
var oldFogKeepExp = gfxCfg.FindProperty("m_FogKeepExp").boolValue;
464489
var oldFogKeepExp2 = gfxCfg.FindProperty("m_FogKeepExp2").boolValue;
465-
var oldFogStripInstancing = gfxCfg.FindProperty("m_InstancingStripping").intValue;
490+
var oldStripInstancing = gfxCfg.FindProperty("m_InstancingStripping").intValue;
491+
// var oldStripNonInstancing = gfxCfg.FindProperty("m_NonInstancingStripping").intValue;
466492
var oldLightmapStripping = gfxCfg.FindProperty("m_LightmapStripping").boolValue;
467493
var oldLightmapKeepPlain = gfxCfg.FindProperty("m_LightmapKeepPlain").boolValue;
468494
var oldLightmapKeepDirCombined = gfxCfg.FindProperty("m_LightmapKeepDirCombined").boolValue;
@@ -472,11 +498,12 @@ private void ExportAssetBundle(UnityEngine.Object[] exports,
472498
var oldLightmapKeepSubtractive = gfxCfg.FindProperty("m_LightmapKeepSubtractive").boolValue;
473499
// Always enable shader variant stripping for best results
474500
// Otherwise we are depending on the actual project setting
475-
GraphicsSettings.logWhenShaderIsCompiled = script.VerboseLogging;
501+
GraphicsSettings.logWhenShaderIsCompiled = script.DetailedLogging;
476502
gfxCfg.FindProperty("m_FogStripping").boolValue = true;
477503
gfxCfg.FindProperty("m_LightmapStripping").boolValue = true;
478504
// Either fully strip instancing or keep it (not sure if "unused" makes much sense)
479505
gfxCfg.FindProperty("m_InstancingStripping").intValue = script.StripInstancing ? 1 : 2;
506+
// gfxCfg.FindProperty("m_NonInstancingStripping").intValue = script.StripNonInstancing ? 1 : 2;
480507
gfxCfg.FindProperty("m_FogKeepLinear").boolValue = !script.StripFogLinear;
481508
gfxCfg.FindProperty("m_FogKeepExp").boolValue = !script.StripFogExp;
482509
gfxCfg.FindProperty("m_FogKeepExp2").boolValue = !script.StripFogExp2;
@@ -494,7 +521,7 @@ private void ExportAssetBundle(UnityEngine.Object[] exports,
494521
try
495522
{
496523
// Give verbose log message to report use APIs
497-
if (script.VerboseLogging) Debug.LogFormat(
524+
if (script.DetailedLogging) Debug.LogFormat(
498525
"Create {0} with graphics API: {1}", Path.GetFileName(path),
499526
string.Join(", ", Array.ConvertAll(apis, x => x.ToString())));
500527
// Call the actual exporting functionality
@@ -516,8 +543,9 @@ private void ExportAssetBundle(UnityEngine.Object[] exports,
516543
gfxCfg.FindProperty("m_FogKeepLinear").boolValue = oldFogKeepLinear;
517544
gfxCfg.FindProperty("m_FogKeepExp").boolValue = oldFogKeepExp;
518545
gfxCfg.FindProperty("m_FogKeepExp2").boolValue = oldFogKeepExp2;
519-
gfxCfg.FindProperty("m_InstancingStripping").intValue = oldFogStripInstancing;
520546
gfxCfg.FindProperty("m_LightmapStripping").boolValue = oldLightmapStripping;
547+
gfxCfg.FindProperty("m_InstancingStripping").intValue = oldStripInstancing;
548+
// gfxCfg.FindProperty("m_NonInstancingStripping").intValue = oldStripNonInstancing;
521549
gfxCfg.FindProperty("m_LightmapKeepPlain").boolValue = oldLightmapKeepPlain;
522550
gfxCfg.FindProperty("m_LightmapKeepDirCombined").boolValue = oldLightmapKeepDirCombined;
523551
gfxCfg.FindProperty("m_LightmapKeepDynamicPlain").boolValue = oldLightmapKeepDynamicPlain;
@@ -644,6 +672,9 @@ class StripShaderVariants : IPreprocessShaders
644672
static ShaderKeyword FOG_EXP = new ShaderKeyword("FOG_EXP");
645673
static ShaderKeyword FOG_EXP2 = new ShaderKeyword("FOG_EXP2");
646674
static ShaderKeyword FOG_LINEAR = new ShaderKeyword("FOG_LINEAR");
675+
static ShaderKeyword INSTANCING = new ShaderKeyword("INSTANCING_ON");
676+
static ShaderKeyword UNITY_HDR_ON = new ShaderKeyword("UNITY_HDR_ON");
677+
static ShaderKeyword LIGHTPROBE_SH = new ShaderKeyword("LIGHTPROBE_SH");
647678

648679
// Do the processing of shaders to potentially alter the shader variants to be compiled
649680
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data)
@@ -656,30 +687,58 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<Shad
656687
StripShaderVariantCounter.ShadersBefore = data.Count;
657688
// Strip certain pass variants in case render path is known to be unused
658689
if (Cfg.StripMetaPass && snippet.passType == PassType.Meta) data.Clear();
659-
else if (Cfg.StripDeferredPass && snippet.passType == PassType.Deferred) data.Clear();
660-
else if (Cfg.StripForwardBasePass && snippet.passType == PassType.ForwardBase) data.Clear();
661-
else if (Cfg.StripForwardAddPass && snippet.passType == PassType.ForwardAdd) data.Clear();
662-
else if (Cfg.StripShadowCasterPass && snippet.passType == PassType.ShadowCaster) data.Clear();
690+
if (Cfg.StripDeferredPass && snippet.passType == PassType.Deferred) data.Clear();
691+
if (Cfg.StripForwardBasePass && snippet.passType == PassType.ForwardBase) data.Clear();
692+
if (Cfg.StripForwardAddPass && snippet.passType == PassType.ForwardAdd) data.Clear();
693+
if (Cfg.StripShadowCasterPass && snippet.passType == PassType.ShadowCaster) data.Clear();
663694
// Process the variants array from behind
664695
for (int i = data.Count - 1; i != -1; --i)
665696
{
666697
bool remove = false;
667698
bool isFogExp = data[i].shaderKeywordSet.IsEnabled(FOG_EXP);
668699
bool isFogExp2 = data[i].shaderKeywordSet.IsEnabled(FOG_EXP2);
669700
bool isFogLinear = data[i].shaderKeywordSet.IsEnabled(FOG_LINEAR);
701+
bool isInstanced = data[i].shaderKeywordSet.IsEnabled(INSTANCING);
702+
bool isUnityHdrOn = data[i].shaderKeywordSet.IsEnabled(UNITY_HDR_ON);
703+
bool isLightprobeSH = data[i].shaderKeywordSet.IsEnabled(LIGHTPROBE_SH);
670704
// Debug.Log(shader.name + " Keywords " + string.Join(", ",
671705
// data[i].shaderKeywordSet.GetShaderKeywords()));
672706
bool isFog = isFogExp | isFogExp2 | isFogLinear;
673707
if (Cfg.StripFogNone && !isFog) remove = true;
674-
else if (Cfg.StripFogExp && isFogExp) remove = true;
675-
else if (Cfg.StripFogExp2 && isFogExp2) remove = true;
676-
else if (Cfg.StripFogLinear && isFogLinear) remove = true;
708+
if (Cfg.StripFogExp && isFogExp) remove = true;
709+
if (Cfg.StripFogExp2 && isFogExp2) remove = true;
710+
if (Cfg.StripFogLinear && isFogLinear) remove = true;
711+
if (Cfg.StripUnityHdrOn && isUnityHdrOn) remove = true;
712+
if (Cfg.StripUnityHdrOff && !isUnityHdrOn) remove = true;
713+
if (Cfg.StripInstancing && isInstanced) remove = true;
714+
if (Cfg.StripNonInstancing && !isInstanced) remove = true;
715+
// Do light stripping according to shader stage
716+
// Deferred needs light, shadow caster does not
717+
if (Cfg.StripLightForDeferred == false) {
718+
if (Cfg.StripLightprobeOn && isLightprobeSH) remove = true;
719+
if (Cfg.StripLightprobeOff && !isLightprobeSH) remove = true;
720+
}
721+
else {
722+
if (snippet.passType == PassType.Deferred)
723+
remove |= !isLightprobeSH;
724+
if (snippet.passType == PassType.ShadowCaster)
725+
remove |= isLightprobeSH;
726+
}
727+
677728
if (remove) data.RemoveAt(i);
678729
}
679730
// Print a log if verbose logging is enabled
680-
if (Cfg.VerboseLogging) Debug.Log(string.Format(
681-
"{0}: {1} variants stripped: {3}, compiling: {2}",
682-
shader.name, snippet.passName, data.Count,
731+
for (int i = 0; i < data.Count; i++)
732+
{
733+
if (Cfg.VerboseLogging) Debug.Log(string.Format(
734+
"{0}: {1} {2} Shader for {3} on {4} [{5}]",
735+
i, snippet.passType, snippet.shaderType,
736+
data[i].shaderCompilerPlatform, data[i].buildTarget,
737+
string.Join(", ", data[i].shaderKeywordSet.GetShaderKeywords())));
738+
}
739+
if (Cfg.DetailedLogging) Debug.Log(string.Format(
740+
"{0}: {1} {2} variants stripped: {4}, compiling: {3}",
741+
shader.name, snippet.passType, snippet.shaderType, data.Count,
683742
StripShaderVariantCounter.ShadersBefore - data.Count));
684743
}
685744
}

Assets/Exporter Scripts/Runtime/AssetBundleUnity3D.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class AssetBundleUnity3D : ScriptableObject
2828

2929
[HideInInspector]
3030
public bool VerboseLogging = false;
31+
[HideInInspector]
32+
public bool DetailedLogging = false;
3133

3234
[HideInInspector]
3335
public bool ShowPlatformTargets = false;
@@ -77,6 +79,8 @@ public class AssetBundleUnity3D : ScriptableObject
7779

7880
[HideInInspector]
7981
public bool StripInstancing = false;
82+
[HideInInspector]
83+
public bool StripNonInstancing = false;
8084

8185
[HideInInspector]
8286
public bool StripLightmapPlain = true;
@@ -90,6 +94,16 @@ public class AssetBundleUnity3D : ScriptableObject
9094
public bool StripLightmapShadowMask = true;
9195
[HideInInspector]
9296
public bool StripLightmapSubtractive = true;
97+
[HideInInspector]
98+
public bool StripLightprobeOn = false;
99+
[HideInInspector]
100+
public bool StripLightprobeOff = false;
101+
[HideInInspector]
102+
public bool StripLightForDeferred = true;
103+
[HideInInspector]
104+
public bool StripUnityHdrOn = false;
105+
[HideInInspector]
106+
public bool StripUnityHdrOff = true;
93107

94108
[Space(10)]
95109

Assets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ch.ocbnet.unityassetexporter",
33
"displayName": "Unity3D Bundle Exporter",
4-
"version": "0.6.5",
4+
"version": "0.8.1",
55
"unity": "2018.3",
66
"description": "Easily create and export unity3d resource bundles with multiple assets."
77
}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ shaders programmatically.
106106

107107
## Changelog
108108

109+
### Version 0.9.0
110+
111+
- Add more shader stripping option for passes
112+
- Further Improve shader compile logging
113+
114+
### Version 0.8.1
115+
116+
- Switch package version for master to 9999.0.0
117+
118+
### Version 0.8.0
119+
120+
- Add more shader stripping option for passes
121+
109122
### Version 0.7.2
110123

111124
- Improve shader compile logging

0 commit comments

Comments
 (0)