文章目录
前言 一、我们先创建一个简单的Shader 二、编译这个Shader,并且打开 1、编译后注意事项 2、编译平台 和 编译指令数 3、顶点着色器用到的信息 4、顶点着色器计算的核心部分 5、片元着色器用到的信息 6、片元着色器核心部分前言
我们先读懂Shader编译后代码,才能对Shader进行合理的优化
一、我们先创建一个简单的Shader
Shader "MyShader/P2_3_6"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(i.uv,1,1);
}
ENDCG
}
}
}
二、编译这个Shader,并且打开
我们目前先只编译到 D3D 平台这是编译后的代码(我们来逐步分析):
// Compiled shader for custom platforms
//
//
// NOTE: This is *not* a valid shader file, the contents are provided just
// for information and for debugging purposes only.
//
//
// Skipping shader variants that would not be included into build of current scene.
Shader "MyShader/P2_3_6" {
SubShader {
LOD 100
Tags { "RenderType"="Opaque" }
// Stats for Vertex shader:
// d3d11: 8 math
Pass {
Tags { "RenderType"="Opaque" }
//
// //
// Compiled programs //
// //
//
//
Keywords: <none>
-- Hardware tier variant: Tier 1
-- Vertex shader for "d3d11":
// Stats: 8 math, 2 temp registers
Uses vertex data channel "Vertex"
Uses vertex data channel "TexCoord0"
Constant Buffer "UnityPerDraw" (176 bytes) on slot 0 {
Matrix4x4 unity_ObjectToWorld at 0
}
Constant Buffer "UnityPerFrame" (368 bytes) on slot 1 {
Matrix4x4 unity_MatrixVP at 272
}
Shader Disassembly:
//
// Generated by Microsoft (R) D3D Shader Disassembler
//
//
// Input signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION 0 xyzw 0 NONE float xyz
// TEXCOORD 0 xy 1 NONE float xy
//
//
// Output signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD 0 xy 0 NONE float xy
// SV_POSITION 0 xyzw 1 POS float xyzw
//
vs_4_0
dcl_constantbuffer CB0[4], immediateIndexed
dcl_constantbuffer CB1[21], immediateIndexed
dcl_input v0.xyz
dcl_input v1.xy
dcl_output o0.xy
dcl_output_siv o1.xyzw, position
dcl_temps 2
0: mov o0.xy, v1.xyxx
1: mul r0.xyzw, v0.yyyy, cb0[1].xyzw
2: mad r0.xyzw, cb0[0].xyzw, v0.xxxx, r0.xyzw
3: mad r0.xyzw, cb0[2].xyzw, v0.zzzz, r0.xyzw
4: add r0.xyzw, r0.xyzw, cb0[3].xyzw
5: mul r1.xyzw, r0.yyyy, cb1[18].xyzw
6: mad r1.xyzw, cb1[17].xyzw, r0.xxxx, r1.xyzw
7: mad r1.xyzw, cb1[19].xyzw, r0.zzzz, r1.xyzw
8: mad o1.xyzw, cb1[20].xyzw, r0.wwww, r1.xyzw
9: ret
// Approximately 0 instruction slots used
-- Hardware tier variant: Tier 1
-- Fragment shader for "d3d11":
Shader Disassembly:
//
// Generated by Microsoft (R) D3D Shader Disassembler
//
//
// Input signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD 0 xy 0 NONE float xy
// SV_POSITION 0 xyzw 1 POS float
//
//
// Output signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target 0 xyzw 0 TARGET float xyzw
//
ps_4_0
dcl_input_ps linear v0.xy
dcl_output o0.xyzw
0: mov o0.xy, v0.xyxx
1: mov o0.zw, l(0,0,1.000000,1.000000)
2: ret
// Approximately 0 instruction slots used
}
}
}
1、编译后注意事项
//
//
// NOTE: This is not a valid shader file, the contents are provided just
// for information and for debugging purposes only.
//
//
这不是一个有效的Shader,这个文本只是提供用于 Debug Shader 时使用
2、编译平台 和 编译指令数
// Stats for Vertex shader:
// d3d11: 8 math
代表编译后的平台是 DirectX 11,使用到了 8 条计算指令
3、顶点着色器用到的信息
Keywords:
– Hardware tier variant: Tier 1
– Vertex shader for “d3d11”:
// Stats: 8 math, 2 temp registers
Uses vertex data channel “Vertex”
Uses vertex data channel “TexCoord0”
Constant Buffer “UnityPerDraw” (176 bytes) on slot 0 {
Matrix4x4 unity_ObjectToWorld at 0
}
Constant Buffer “UnityPerFrame” (368 bytes) on slot 1 {
Matrix4x4 unity_MatrixVP at 272
}
4、顶点着色器计算的核心部分
Shader Disassembly:
//
// Generated by Microsoft (R) D3D Shader Disassembler
//
//
// Input signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION 0 xyzw 0 NONE float xyz
// TEXCOORD 0 xy 1 NONE float xy
//
//
// Output signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD 0 xy 0 NONE float xy
// SV_POSITION 0 xyzw 1 POS float xyzw
//
vs_4_0
dcl_constantbuffer CB0[4], immediateIndexed
dcl_constantbuffer CB1[21], immediateIndexed
dcl_input v0.xyz
dcl_input v1.xy
dcl_output o0.xy
dcl_output_siv o1.xyzw, position
dcl_temps 2
0: mov o0.xy, v1.xyxx
1: mul r0.xyzw, v0.yyyy, cb0[1].xyzw
2: mad r0.xyzw, cb0[0].xyzw, v0.xxxx, r0.xyzw
3: mad r0.xyzw, cb0[2].xyzw, v0.zzzz, r0.xyzw
4: add r0.xyzw, r0.xyzw, cb0[3].xyzw
5: mul r1.xyzw, r0.yyyy, cb1[18].xyzw
6: mad r1.xyzw, cb1[17].xyzw, r0.xxxx, r1.xyzw
7: mad r1.xyzw, cb1[19].xyzw, r0.zzzz, r1.xyzw
8: mad o1.xyzw, cb1[20].xyzw, r0.wwww, r1.xyzw
9: ret
// Approximately 0 instruction slots used
我们先看一下顶点着色器的注释(重要):
计算用到的指令
mov: 赋值运算
mul : 乘法
div : 除法
add : 加法 和 减法
mad: mul 和 add 的结合
ret : 返回
5、片元着色器用到的信息
– Hardware tier variant: Tier 1
– Fragment shader for “d3d11”:
6、片元着色器核心部分
Shader Disassembly:
//
// Generated by Microsoft (R) D3D Shader Disassembler
//
//
// Input signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD 0 xy 0 NONE float xy
// SV_POSITION 0 xyzw 1 POS float
//
//
// Output signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target 0 xyzw 0 TARGET float xyzw
//
ps_4_0
dcl_input_ps linear v0.xy
dcl_output o0.xyzw
0: mov o0.xy, v0.xyxx
1: mov o0.zw, l(0,0,1.000000,1.000000)
2: ret
// Approximately 0 instruction slots used
片元着色器部分的注释 和 顶点着色器 部分一样
ps_4_0 : 代表编译到的像素着色器(这里和片元着色器差不多,但是他两不一样)。在DirectX 平台下,这个代表编译到硬件 SM4.0
dcl_input_ps linear v0.xy:代表输入的变量,v 默认代表输入变量,0代表索引为0
dcl_output o0.xyzw:代表输出的变量,o默认代表输出变量,0代表索引为0
mov : 赋值
l :值类型
r : 代表 临时寄存器
cb : 代表 常量寄存器