[Unity]Shader变体以及使用IPreprocessShaders剥离
2020/04
25
20:04
建立测试Shader
Shader "Unlit/NewUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
Name "MyShader"
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ A1 A2 A3
#pragma multi_compile _ B1 B2 B3
#pragma shader_feature _ C1 C2
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
#if defined(COLOR_1)
col.a *= 1.5;
#endif
#if defined(COLOR_2)
col.a *= 0.7;
#endif
#if defined(COLOR_3)
col.a *= 0.7;
#endif
#if defined(COLOR_4)
col.a *= 0.7;
#endif
#if defined(COLOR_5)
col.a *= 0.7;
#endif
#if defined(COLOR_6)
col.a *= 0.7;
#endif
#if defined(COLOR_7)
col.a *= 0.7;
#endif
#if defined(COLOR_8)
col.a *= 0.7;
#endif
#if defined(COLOR_9)
col.a *= 0.7;
#endif
#if defined(COLOR_10)
col.a *= 0.7;
#endif
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
添加处理:
using System.Collections.Generic;
using UnityEditor.Build;
using UnityEditor.Rendering;
using UnityEngine;
public class ShaderVarBuildProcess: IPreprocessShaders
{
public int callbackOrder { get { return 0; } }
public UwShaderVarBuildProcess()
{
}
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data)
{
if("Unlit/NewUnlitShader" != shader.name)
{
return;
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("shader={3}, passType={0}, passName={1}, shaderType={2}\n",
snippet.passType, snippet.passName, snippet.shaderType, shader.name);
for(int i=0;i<data.Count; ++i)
{
var pdata = data[i];
sb.AppendFormat("{0}.{1},{2}: ", i, pdata.graphicsTier, pdata.shaderCompilerPlatform);
var ks = pdata.shaderKeywordSet.GetShaderKeywords();
foreach(var k in ks)
{
sb.AppendFormat("{0}, ", k.GetKeywordName());
}
sb.Append("\n");
}
Debug.Log(sb.ToString());
}
}
输出以下日志:
shader=Unlit/NewUnlitShader, passType=ForwardBase, passName=MyShader, shaderType=Vertex
0.Tier1,GLES20:
1.Tier2,GLES20:
2.Tier3,GLES20:
3.Tier1,GLES3x:
4.Tier2,GLES3x:
5.Tier3,GLES3x:
6.Tier1,GLES20: B1,
7.Tier2,GLES20: B1,
8.Tier3,GLES20: B1,
9.Tier1,GLES3x: B1,
10.Tier2,GLES3x: B1,
11.Tier3,GLES3x: B1,
12.Tier1,GLES20: B2,
13.Tier2,GLES20: B2,
14.Tier3,GLES20: B2,
15.Tier1,GLES3x: B2,
16.Tier2,GLES3x: B2,
17.Tier3,GLES3x: B2,
18.Tier1,GLES20: B3,
19.Tier2,GLES20: B3,
20.Tier3,GLES20: B3,
21.Tier1,GLES3x: B3,
22.Tier2,GLES3x: B3,
23.Tier3,GLES3x: B3,
24.Tier1,GLES20: A1,
25.Tier2,GLES20: A1,
26.Tier3,GLES20: A1,
27.Tier1,GLES3x: A1,
28.Tier2,GLES3x: A1,
29.Tier3,GLES3x: A1,
30.Tier1,GLES20: A1, B1,
31.Tier2,GLES20: A1, B1,
32.Tier3,GLES20: A1, B1,
33.Tier1,GLES3x: A1, B1,
34.Tier2,GLES3x: A1, B1,
35.Tier3,GLES3x: A1, B1,
36.Tier1,GLES20: A1, B2,
37.Tier2,GLES20: A1, B2,
38.Tier3,GLES20: A1, B2,
39.Tier1,GLES3x: A1, B2,
40.Tier2,GLES3x: A1, B2,
41.Tier3,GLES3x: A1, B2,
42.Tier1,GLES20: A1, B3,
43.Tier2,GLES20: A1, B3,
44.Tier3,GLES20: A1, B3,
45.Tier1,GLES3x: A1, B3,
46.Tier2,GLES3x: A1, B3,
47.Tier3,GLES3x: A1, B3,
48.Tier1,GLES20: A2,
49.Tier2,GLES20: A2,
50.Tier3,GLES20: A2,
51.Tier1,GLES3x: A2,
52.Tier2,GLES3x: A2,
53.Tier3,GLES3x: A2,
54.Tier1,GLES20: A2, B1,
55.Tier2,GLES20: A2, B1,
56.Tier3,GLES20: A2, B1,
57.Tier1,GLES3x: A2, B1,
58.Tier2,GLES3x: A2, B1,
59.Tier3,GLES3x: A2, B1,
60.Tier1,GLES20: A2, B2,
61.Tier2,GLES20: A2, B2,
62.Tier3,GLES20: A2, B2,
63.Tier1,GLES3x: A2, B2,
64.Tier2,GLES3x: A2, B2,
65.Tier3,GLES3x: A2, B2,
66.Tier1,GLES20: A2, B3,
67.Tier2,GLES20: A2, B3,
68.Tier3,GLES20: A2, B3,
69.Tier1,GLES3x: A2, B3,
70.Tier2,GLES3x: A2, B3,
71.Tier3,GLES3x: A2, B3,
72.Tier1,GLES20: A3,
73.Tier2,GLES20: A3,
74.Tier3,GLES20: A3,
75.Tier1,GLES3x: A3,
76.Tier2,GLES3x: A3,
77.Tier3,GLES3x: A3,
78.Tier1,GLES20: A3, B1,
79.Tier2,GLES20: A3, B1,
80.Tier3,GLES20: A3, B1,
81.Tier1,GLES3x: A3, B1,
82.Tier2,GLES3x: A3, B1,
83.Tier3,GLES3x: A3, B1,
84.Tier1,GLES20: A3, B2,
85.Tier2,GLES20: A3, B2,
86.Tier3,GLES20: A3, B2,
87.Tier1,GLES3x: A3, B2,
88.Tier2,GLES3x: A3, B2,
89.Tier3,GLES3x: A3, B2,
90.Tier1,GLES20: A3, B3,
91.Tier2,GLES20: A3, B3,
92.Tier3,GLES20: A3, B3,
93.Tier1,GLES3x: A3, B3,
94.Tier2,GLES3x: A3, B3,
95.Tier3,GLES3x: A3, B3,
shader=Unlit/NewUnlitShader, passType=ForwardBase, passName=MyShader, shaderType=Fragment
0.Tier1,GLES20:
1.Tier2,GLES20:
2.Tier3,GLES20:
3.Tier1,GLES3x:
4.Tier2,GLES3x:
5.Tier3,GLES3x:
6.Tier1,GLES20: B1,
7.Tier2,GLES20: B1,
8.Tier3,GLES20: B1,
9.Tier1,GLES3x: B1,
10.Tier2,GLES3x: B1,
11.Tier3,GLES3x: B1,
12.Tier1,GLES20: B2,
13.Tier2,GLES20: B2,
14.Tier3,GLES20: B2,
15.Tier1,GLES3x: B2,
16.Tier2,GLES3x: B2,
17.Tier3,GLES3x: B2,
18.Tier1,GLES20: B3,
19.Tier2,GLES20: B3,
20.Tier3,GLES20: B3,
21.Tier1,GLES3x: B3,
22.Tier2,GLES3x: B3,
23.Tier3,GLES3x: B3,
24.Tier1,GLES20: A1,
25.Tier2,GLES20: A1,
26.Tier3,GLES20: A1,
27.Tier1,GLES3x: A1,
28.Tier2,GLES3x: A1,
29.Tier3,GLES3x: A1,
30.Tier1,GLES20: A1, B1,
31.Tier2,GLES20: A1, B1,
32.Tier3,GLES20: A1, B1,
33.Tier1,GLES3x: A1, B1,
34.Tier2,GLES3x: A1, B1,
35.Tier3,GLES3x: A1, B1,
36.Tier1,GLES20: A1, B2,
37.Tier2,GLES20: A1, B2,
38.Tier3,GLES20: A1, B2,
39.Tier1,GLES3x: A1, B2,
40.Tier2,GLES3x: A1, B2,
41.Tier3,GLES3x: A1, B2,
42.Tier1,GLES20: A1, B3,
43.Tier2,GLES20: A1, B3,
44.Tier3,GLES20: A1, B3,
45.Tier1,GLES3x: A1, B3,
46.Tier2,GLES3x: A1, B3,
47.Tier3,GLES3x: A1, B3,
48.Tier1,GLES20: A2,
49.Tier2,GLES20: A2,
50.Tier3,GLES20: A2,
51.Tier1,GLES3x: A2,
52.Tier2,GLES3x: A2,
53.Tier3,GLES3x: A2,
54.Tier1,GLES20: A2, B1,
55.Tier2,GLES20: A2, B1,
56.Tier3,GLES20: A2, B1,
57.Tier1,GLES3x: A2, B1,
58.Tier2,GLES3x: A2, B1,
59.Tier3,GLES3x: A2, B1,
60.Tier1,GLES20: A2, B2,
61.Tier2,GLES20: A2, B2,
62.Tier3,GLES20: A2, B2,
63.Tier1,GLES3x: A2, B2,
64.Tier2,GLES3x: A2, B2,
65.Tier3,GLES3x: A2, B2,
66.Tier1,GLES20: A2, B3,
67.Tier2,GLES20: A2, B3,
68.Tier3,GLES20: A2, B3,
69.Tier1,GLES3x: A2, B3,
70.Tier2,GLES3x: A2, B3,
71.Tier3,GLES3x: A2, B3,
72.Tier1,GLES20: A3,
73.Tier2,GLES20: A3,
74.Tier3,GLES20: A3,
75.Tier1,GLES3x: A3,
76.Tier2,GLES3x: A3,
77.Tier3,GLES3x: A3,
78.Tier1,GLES20: A3, B1,
79.Tier2,GLES20: A3, B1,
80.Tier3,GLES20: A3, B1,
81.Tier1,GLES3x: A3, B1,
82.Tier2,GLES3x: A3, B1,
83.Tier3,GLES3x: A3, B1,
84.Tier1,GLES20: A3, B2,
85.Tier2,GLES20: A3, B2,
86.Tier3,GLES20: A3, B2,
87.Tier1,GLES3x: A3, B2,
88.Tier2,GLES3x: A3, B2,
89.Tier3,GLES3x: A3, B2,
90.Tier1,GLES20: A3, B3,
91.Tier2,GLES20: A3, B3,
92.Tier3,GLES20: A3, B3,
93.Tier1,GLES3x: A3, B3,
94.Tier2,GLES3x: A3, B3,
95.Tier3,GLES3x: A3, B3,
4个multi_compile,2组,一共16个情况
3档,2个平台,6个情况,总计16*6=96个变体
CopyRights: The Post by BY-NC-SA For Authorization,Original If Not Noted,Reprint Please Indicate From 老刘@开发笔记
Post Link: [Unity]Shader变体以及使用IPreprocessShaders剥离
Post Link: [Unity]Shader变体以及使用IPreprocessShaders剥离