11using System ;
22using System . Collections . Generic ;
3- using UnityEditor ;
3+ using System . Diagnostics . CodeAnalysis ;
44using UnityEditor . Build ;
55using UnityEditor . Build . Reporting ;
66using UnityEditor . Rendering . BuiltIn . ShaderGraph ;
77using UnityEngine ;
8- using UnityEngine . Profiling ;
98using UnityEngine . Rendering ;
109
1110namespace UnityEditor . Rendering . BuiltIn
@@ -89,6 +88,13 @@ internal class ShaderPreprocessor : IPreprocessShaders
8988 ShaderKeyword m_ScreenSpaceOcclusion = new ShaderKeyword ( ShaderKeywordStrings . ScreenSpaceOcclusion ) ;
9089 ShaderKeyword m_EditorVisualization = new ShaderKeyword ( ShaderKeywordStrings . EDITOR_VISUALIZATION ) ;
9190 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+ } ;
9298
9399 int m_TotalVariantsInputCount ;
94100 int m_TotalVariantsOutputCount ;
@@ -115,7 +121,42 @@ bool IsShaderGraphShader(Shader shader, ShaderSnippetData snippetData)
115121 var shaderGraphTag = serializedSubShader . FindTagValue ( m_ShaderGraphShaderTag ) ;
116122 if ( shaderGraphTag == ShaderTagId . none )
117123 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 ) ;
119160 }
120161
121162 bool StripUnusedPass ( ShaderFeatures features , ShaderSnippetData snippetData )
@@ -319,17 +360,26 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippetData, IList<
319360 Profiler . BeginSample ( k_ProcessShaderTag ) ;
320361#endif
321362
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).
324363 RenderPipelineAsset rpAsset = GraphicsSettings . currentRenderPipeline ;
325- if ( rpAsset != null || compilerDataList == null || compilerDataList . Count == 0 )
364+ if ( compilerDataList == null || compilerDataList . Count == 0 )
326365 return ;
327366
328367 double stripTimeMs = 0.0 ;
329368 int prevVariantCount = compilerDataList . Count ;
330369 using ( TimedScope . FromRef ( ref stripTimeMs ) )
331370 {
332371 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+
333383 for ( int i = 0 ; i < inputShaderVariantCount ; )
334384 {
335385 bool removeInput = true ;
0 commit comments