The CG/HLSL semantics provided by Unity Shader Unity

Source: Internet
Author: User
The main reference is the "Unity Shader Essentials" book, plus some of your own summary
What is semantics
Semantics is actually a string assigned to the shader input and output, which expresses the meaning of this parameter. These semantics allow shader to know where to read data and where to send it. After DirectX10, there is a new semantic type, the system numerical semantics (system-value semantics). This type of semantics begins with the SV, and the SV represents the system value. These semantics have special meanings in the rendering pipeline, for example, in the previous article, we use the sv_position semantics to decorate the output variable pos of the vertex shader, which means that POS contains the vertex coordinates that can be used for rasterization, that is, the coordinates in the homogeneous clipping space.
Unity-supported semantics
The common semantics that unity supports when passing model data from the application phase to the vertex shader are as follows:


The common semantics supported by unity when passed from the vertex shader to the slice shader are as follows:


the common semantics supported by unity when the slice shader output is as follows:



How to debug unity Shader in Unity: 1, using false color imagesA false color image (False_color image) refers to an image generated by a false color technique. A false color image can be used to visualize some of the data, so how to use it to debug shader. The main idea is that we can map variables that need to be debugged to [0,1], output them as colors to the screen, and then determine whether the value is correct by the color of the pixels displayed on the screen. As an example, we will use false color images to visualize some of the model data, such as normals, tangents, texture coordinates, vertex colors, and the results of the operations between them, as shown in the following code:
Shader "Unity Shader book/chapter 5/false Color" {subshader{pass{cgprogram #pragma vertex vert #pragma frag
				ment frag #include "unitycg.cginc" struct v2f {float4 pos:sv_position;
			Fixed4 color:color0;
		
			};
				v2f Vert (Appdata_full v) {v2f o;

				O.pos = Unityobjecttoclippos (V.vertex);

				Visualize normal direction O.color = FIXED4 (v.normal * 0.5 + fixed3 (0.5, 0.5, 0.5), 1.0);

				Visualize tangent direction O.color = fixed4 (v.tangent * 0.5 + fixed3 (0.5,0.5,0.5), 1.0);
				Visual vice Normal direction fixed3 binormal = Cross (v.normal, v.tangent.xyz) * V.TANGENT.W;

				O.color = FIXED4 (binormal * 0.5 + fixed3 (0.5, 0.5, 0.5), 1.0);

				Visualize the first set of texture coordinates o.color = FIXED4 (v.texcoord.xy, 0.0, 1.0);

				Visualize the second set of texture coordinates o.color = FIXED4 (v.texcoord1.xy, 0.0, 1.0);
				Visualize the small part of the first set of texture coordinates o.color = frac (V.texcoord);
				if (Any (saturate (V.texcoord)-V.texcoord)) {o.color.b = 0.5;

				} O.COLOR.A = 1.0;
				Visualize the small part of the second set of texture coordinates o.color = frac (v.texcoord1); if (anY (Saturate (v.texcoord1)-V.texcoord1)) {o.color.b = 0.5;

				} O.COLOR.A = 1.0;

				Visualize vertex color o.color = v.color;
			return o;
			} fixed4 Frag (v2f i): sv_target{return i.color;
 } ENDCG}}}

In the above code, we used a struct-body--appdata_full built into the uniyt. You can find the definition of it in Unitycg.cginc:
struct Appdata_full {
    float4 vertex:position;
    FLOAT4 tangent:tangent;
    FLOAT3 Normal:normal;
    FLOAT4 texcoord:texcoord0;
    FLOAT4 Texcoord1:texcoord1;
    FLOAT4 Texcoord2:texcoord2;
    FLOAT4 Texcoord3:texcoord3;
    Fixed4 Color:color;
    unity_vertex_input_instance_id
};

As you can see, Appdata_full contains almost all of the model data.
We store the calculated false color in the color variable in the output structure body of the vertex shader--v2f, and output this color in the slice shader. We can make different comments about the code in it to see the effects of different operations and data.

The way of Shader regulation
1,float, half, fixed
In CG/HLSL, there are three types of precision values: float, half, fixed. These precision will determine the numerical range of the results, the accuracy range is as follows: The accuracy range above is not absolutely correct, and may vary on different platforms and GPUs. Use low-precision types whenever possible, as this optimizes the performance of shader, which is especially important on mobile platforms. From their broad range of ranges, we can use the fixed type to store color and unit vectors, and if you want to store a wider range of data you can choose the half type, and the worst-case option is to use float.
2. Avoid unnecessary calculations
If we do a lot of calculations in shader, especially in the slice shader, then you're likely to get a unity error. Different shader Target, different shader stages, the number of temporary registers and instructions we can use are different. As the following table:
3, use branching and looping statements with cautionIf we use a lot of process control statements in shader, the performance of this shader may be exponentially reduced. The solution is that we try to move the computation to the top of the pipeline, for example, to put the calculation in the element shader into the vertex shader, or to pre-calculate directly in the CPU, and then pass the result to shader. 4, don't divide by 0.Otherwise the result is unpredictable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.