1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using UnityEditor ;
3
+ using System . Diagnostics . CodeAnalysis ;
4
4
using UnityEditor . Build ;
5
5
using UnityEditor . Build . Reporting ;
6
6
using UnityEditor . Rendering . BuiltIn . ShaderGraph ;
7
7
using UnityEngine ;
8
- using UnityEngine . Profiling ;
9
8
using UnityEngine . Rendering ;
10
9
11
10
namespace UnityEditor . Rendering . BuiltIn
@@ -89,6 +88,13 @@ internal class ShaderPreprocessor : IPreprocessShaders
89
88
ShaderKeyword m_ScreenSpaceOcclusion = new ShaderKeyword ( ShaderKeywordStrings . ScreenSpaceOcclusion ) ;
90
89
ShaderKeyword m_EditorVisualization = new ShaderKeyword ( ShaderKeywordStrings . EDITOR_VISUALIZATION ) ;
91
90
ShaderTagId m_ShaderGraphShaderTag = new ShaderTagId ( "ShaderGraphShader" ) ;
91
+ ShaderTagId m_ShaderGraphTargetIdTag = new ShaderTagId ( "ShaderGraphTargetId" ) ;
92
+
93
+ static List < string > SubTargetNames = new List < string >
94
+ {
95
+ typeof ( BuiltInLitSubTarget ) . Name ,
96
+ typeof ( BuiltInUnlitSubTarget ) . Name ,
97
+ } ;
92
98
93
99
int m_TotalVariantsInputCount ;
94
100
int m_TotalVariantsOutputCount ;
@@ -115,7 +121,42 @@ bool IsShaderGraphShader(Shader shader, ShaderSnippetData snippetData)
115
121
var shaderGraphTag = serializedSubShader . FindTagValue ( m_ShaderGraphShaderTag ) ;
116
122
if ( shaderGraphTag == ShaderTagId . none )
117
123
return false ;
118
- return true ;
124
+
125
+ var targetIdTag = serializedSubShader . FindTagValue ( m_ShaderGraphTargetIdTag ) ;
126
+ if ( targetIdTag == ShaderTagId . none )
127
+ return false ;
128
+
129
+ var targetIdString = targetIdTag . name ;
130
+ foreach ( var subTargetName in SubTargetNames )
131
+ {
132
+ if ( targetIdString == subTargetName )
133
+ return true ;
134
+ }
135
+ return false ;
136
+ }
137
+
138
+ private static readonly ShaderTagId s_RenderPipelineShaderTagId = new ShaderTagId ( "RenderPipeline" ) ;
139
+
140
+ bool IsBiRPShaderGraphVariant ( [ DisallowNull ] Shader shader , ShaderSnippetData shaderVariant )
141
+ {
142
+ if ( ! IsShaderGraphShader ( shader , shaderVariant ) )
143
+ return false ;
144
+
145
+ var shaderData = ShaderUtil . GetShaderData ( shader ) ;
146
+ if ( shaderData == null )
147
+ return false ;
148
+
149
+ int subshaderIndex = ( int ) shaderVariant . pass . SubshaderIndex ;
150
+ if ( subshaderIndex < 0 || subshaderIndex >= shader . subshaderCount )
151
+ return false ;
152
+
153
+ var subShader = shaderData . GetSerializedSubshader ( subshaderIndex ) ;
154
+ if ( subShader == null )
155
+ return false ;
156
+
157
+ // A non-existing or empty "RenderPipeline" tag means it's built-in.
158
+ var shaderTag = subShader . FindTagValue ( s_RenderPipelineShaderTagId ) ;
159
+ return string . IsNullOrEmpty ( shaderTag . name ) ;
119
160
}
120
161
121
162
bool StripUnusedPass ( ShaderFeatures features , ShaderSnippetData snippetData )
@@ -319,17 +360,26 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippetData, IList<
319
360
Profiler . BeginSample ( k_ProcessShaderTag ) ;
320
361
#endif
321
362
322
- // We only want to perform shader variant stripping if the built-in render pipeline
323
- // is the active render pipeline (i.e., there is no SRP asset in place).
324
363
RenderPipelineAsset rpAsset = GraphicsSettings . currentRenderPipeline ;
325
- if ( rpAsset != null || compilerDataList == null || compilerDataList . Count == 0 )
364
+ if ( compilerDataList == null || compilerDataList . Count == 0 )
326
365
return ;
327
366
328
367
double stripTimeMs = 0.0 ;
329
368
int prevVariantCount = compilerDataList . Count ;
330
369
using ( TimedScope . FromRef ( ref stripTimeMs ) )
331
370
{
332
371
var inputShaderVariantCount = compilerDataList . Count ;
372
+
373
+ // If the active render pipeline is not built-in, we want to strip all BiRP SG variants
374
+ // and completely ignore the rest (the other strippers will take care of those).
375
+ if ( rpAsset != null )
376
+ {
377
+ if ( IsBiRPShaderGraphVariant ( shader , snippetData ) )
378
+ inputShaderVariantCount = 0 ;
379
+ else
380
+ return ;
381
+ }
382
+
333
383
for ( int i = 0 ; i < inputShaderVariantCount ; )
334
384
{
335
385
bool removeInput = true ;
0 commit comments