Unity Shader Learning Note (eight) semantic word and semantic binding

Source: Internet
Author: User
Unity Shader Learning Note (eight) semantic word and semantic binding
input \ Output rhetorical characters (in\out\inout) parameter PassingParameter passing refers to the process by which a function invokes an argument value to initialize a function parameter. In C\c++, if the change of the parameter value will result in the change of the actual parameter value, the argument passing is divided into "value passing (pass-by-value)"
and "reference passing (pass-by-reference)". When passed by value, the function does not access the currently invoked argument, and the function body processes the copy of the argument, which is the formal parameter, so the change of the parameter value does not affect the actual parameter value;
When a reference is passed, the function receives the address where the argument is stored, and the value of the argument is changed in the body of the function. C\c++ takes a pointer mechanism to construct a reference pass, so typically reference passing is also referred to as "pointer passing."

the way of parameter passing in Cg languageThe method of parameter passing in CG language is also divided into "value passing" and "reference passing", but the pointer mechanism is not supported by GPU hardware, so CG language uses different grammatical rhetoric characters to distinguish "value passing" and "reference passing". These rhetorical characters are:
1. inch: Rhetoric A formal parameter is used only for input, is initialized when it enters the function body, and the change of the parameter value does not affect the actual parameter value, which is a typical way of value passing.
2. out: Rhetoric A formal parameter is used only for output, and is not initialized when entering the function body, and this type of formal parameter is generally the result of a function operation;
3. inout: Rhetoric A formal parameter is used for both input and output, which is a typical reference pass.
Examples are as follows:
void MyFunction (out float x); Parameter x, just for output
void MyFunction (inout float x); Parameter x, which is used for input initialization, also for output data
void MyFunction (in float x); Parameter x, just for input
void MyFunction (float x); Equivalent to in float x, this usage is exactly the same as c\c++ and you can use the return statement instead of the use of out rhetoric. Input \ Output Rhetorical characters are usually used together with semantic words to represent the input and output of vertex shader programs and fragment shaders.
The concept of semantic word and semantic binding Semantic WordsThat represents the data meaning of the input entities (positional or normal), as well as the hardware resources (registers or texture buffers) that these metadata data holds. The input of the varying inputs type in the vertex shader and fragment shader programs must be bound to a semantic word, which is called binding Semantics(Binding semantics).

Semantics is divided into input semantics and input semantics, and input semantics and output semantics are different. While some parameters often use the same binding semantics, such as the input parameters of vertex shader, position refers to the vertex position that the application passes in, and the output parameter uses position semantics to indicate the clipping space position to be fed back to the hardware rasterizer, and the rasterizer position As a location information. Although both semantics are named position, they correspond to different registers on the graphics pipeline.

input semantics for vertex shader programsThe following set of binding semantic keywords are supported by all vertex profiles in the CG language
POSITION Blendweight
NORMAL
TANGENT
Binormal
PSIZE
Blendindices
TEXCOORD0---TEXCOORD7

Examples are as follows: in FLOAT4 modelpos:position
In Float4 Modelnormal:normal

The following semantic terms apply to all CG vertex profiles as input semantics for output semantics and CG fragment profiles: POSITION PSIZE FOG Color0-color1
Texcoord0-texcoord7

input semantics for fragment shader programsFragment shaders have fewer output semantic words, usually color. This is because after the fragment shader has finished running, it is basically at the end of the GPU pipeline. Fragment program must declare an out vector
(ternary or four), binding semantic word color, which will be used as the final color value for the fragment.

Semantic Binding methods 1. The binding semantics are placed after the parameter declaration of the function's argument list: [const] [in | out | inout]<type><identifier> [:
<binding-semantic>][=<initializer>]
In which, const as an optional, rhetorical parameter data; In, out, and inout as optional, indicating how the data is invoked; Type is a required option, declaring data types; identifier is a required option, the form parametric name; a colon ":" plus a binding semantics, which is optional The last is the initialization parameter, which is optional. As shown in the following code. Parameters in the parameter list are bound to input semantics, parameter three, parameter four bound to the output semantics, although the parameter is the same as the binding semantic Word of parameter 3, but the former is the input semantics, the latter is the output semantics, so the two parameter data corresponding to the hardware location is different.

void Mian_v (Float4 position_obj:position,
float3 normal_obj:normal, out
float4 oposition:position, out
F LOAT4 Ocolor:color,
uniform float4x4 modelviewproj)
{..........
}



2. The binding semantics can be placed behind the member variables of the struct (struct): struct <struct-tag>
{
<type><identifier> [: <binding-semantic >];
};


For example, the 2 member variables in structure C2E1V_OUTPU are bound to the semantic position, respectively
and color, and then output in the C2e1v_green Vertex program entry function, so C2E1V_OUTPU
The semantics in is output semantics.
struct C2e1v_output {
float4 position:position;
FLOAT3 color:color;
};
C2e1v_output C2e1v_green (float2 position:position)
{
c2e1v_output out;
Out.position = FLOAT4 (position,0,1);
Out.color = FLOAT3 (0,1,0);
return out;
}


3. Binding semantic words can be placed behind function declarations in the form of:
<type> <identifier> (<parameter-list>) [: <binding-semantic]
{
<body>
}
As shown in the following code, the vertex entry function is declared with a "COLOR" semantic word, indicating that the letter
The number needs to feed back a color value, so the return type of the function is FLOAT4, and the function body must also return
Statement ends.
Float4 Main_v (Float4 position:position, out
float4 oposition:position,
uniform float4x4 modelviewproj): COLOR
{
oposition = Mul (modelviewproj,position);
FLOAT4 Ocolor = FLOAT4 (1.0,0,0,0);
return ocolor;
}


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.