Cg (C for Graphic) language semantic words and semantic bindings in detail

Source: Internet
Author: User
Tags bind
excerpt "GPU Programming and CG Language Primer 1rd Edition" Chinese name "GPU Programming and CG language Spring snow lowbrow" semantic Word (Semantic) and semantic binding (binding semantics)

Semantic words, which represent the data meaning of the input entities (location information or normal vector information), also indicate 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 (bind semantics). 7.5.1 The difference between input semantics and input semantics

The semantic concept is related to the working mechanism of graphic pipelining. As can be seen from the previous GPU processing process, one phase processes the data and then transmits it to the next stage, so how is the interface between each phase determined? For example, the input data of the vertex processor is the vertex data (position, normal vector) in the model space, the output is the projection coordinates and the illumination color, and the fragment processor wants to make the light color input, the question is "How does the fragment processor know where the light color value is stored".

In high-level languages, where data flows from one end of the interface to the other, it is because the memory location (usually pointer information) where the data resides is provided, because the CG language does not support the pointer mechanism, and in the process of graphics hardware processing, the data is usually present in registers, so in the CG language, by introducing the semantic binding ( binding semantics) mechanism that specifies where the data is stored is actually a mapping between input/output data and registers (in OpenGL CG profiles, but not in directx-based CG profiles) mapping relationships). Based on the input semantics, the graphics processor takes data from a register, and then the processed data, based on the output semantics, is placed into the specified register.

Remember this: Semantics is the bridge between the input/output data and registers between the two processing stages (vertex programs, fragment programs), and semantics often represent the meaning of the data, such as POSITION, which generally means that the data stored in the parameter species is the vertex position.

Semantics, which are only meaningful for input/output data in two processing stages, meaning that semantics are valid only in the entry function, and are ignored in an intrinsic function (an internal handler for a stage and no data-passing relationship at the next stage) (semantics attached to Parameters to Non-main functions is ignored (261 pages));

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.

Description: In OpenGL CG profiles, semantic binding specifies the correspondence between the input/output data and the graphics hardware registers, but not in DirectX CG profiles. On page 25th of the literature "3", write:

In the OpenGL Cg profiles, binding semantics implicitly specify the mapping of varying inputs to particular hardware Regis Ters. However, in DirectX based Cg profiles There is no such implied.

on page No. 260 of the literature "3", write:

Often, these predefined names correspond to the names of hardware registers or API

Resources

7.5.2 input semantics for vertex shader programs

Figure 13 08d0c9ea79f9bace118c8200aa004ba90b02000000080000000e0000005f005200650066003200340034003700360037003200350037000000 The set of binding semantic keywords shown is supported by all vertex profiles in the Cg language, and some profiles support additional semantic words.

POSITION Blendweight

NORMAL TANGENT

Binormal PSIZE

Blendindices TEXCOORD0---TEXCOORD7

Figure SEQ Graph/* ARABIC 13 fixed-point shader input semantic word

Semantic word POSITION0 is equivalent to POSITION, and other semantic words have similar equivalence relations. To illustrate the meaning of semantic words, examples are as follows:

In Float4 modelpos:position

The data in this parameter is the vertex position coordinate (usually in model space), the input parameter, and the semantic word POSITION is the input semantics, if in OpenGL it corresponds to the register (on the graphics hardware) that receives the vertex data passed by the application.

In Float4 Modelnormal:normal

Indicates that the data in this parameter is a vertex normal vector coordinate (usually in model space), is an input parameter, and the semantic word NORMAL is the input semantics, if in OpenGL it corresponds to a register (on the graphics hardware) of the vertex normal vector passed by the application.

Note that the above parameters are declared as a four-ary vector, usually we are involved in the application of the vertex position and the normal vector is ternary vector, as to why the ternary vector is a four-yuan vector, also known as the sub-coordinates, see Appendix A for details. The vertex position coordinates are converted into four-tuple vectors in the incoming vertex shader program, the last meta-data is 1, and the vertex-normal vector is converted to a four-element vector in the vertex-shader program, and the last meta-data is 0. 7.5.3 The output semantics of the vertex shader program

The output data of the vertex program is passed into the fragment program, so the output semantic word of the vertex shader program is usually the input semantic word of the fragment program, except the semantic word POSITION.

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-texco ORD7.

The vertex shader must declare an output variable and bind the POSITION semantic word, and the data in that variable will be used and used only for rasterization. If you do not declare an output variable that is bound to a POSITION semantic word, the code is as follows:

void Main_v (Float4 position:position,

Out Float4 Oposition:position,

Uniform float4x4 modelviewproj)

{

Oposition = Mul (modelviewproj,position);

}

Error messages are prompted when compiling with Vp20 and VP30: "Error c6014:required output ' Hpos ' not written. Error messages are prompted when compiling with Vs_2_0 and vs_3_0: "Error c6014:required output ' POSITION ' not written.

In order to maintain consistency between the vertex program output semantics and the fragment program input semantics, it is very convenient to use the same struct type data as the transfer between the two. For example:

struct Vertexin

{

FLOAT4 position:position;

FLOAT4 Normal:normal;

};

struct Vertexscreen

{

FLOAT4 oposition:position;

FLOAT4 objectpos:texcoord0;

FLOAT4 Objectnormal:texcoord1;

};

Note: When using member variable binding semantics in a struct struct, it is important to note that the POSITION semantic words used in the vertex shader program are not used by the fragment program.

If you need to pass data from a vertex shader program to a fragment program, such as vertex projection coordinates, illumination information, and so on, you can declare additional parameters that are bound to the Texcoord series of semantic words for data transfer, in fact the semantic words of the Texcoord series are often used to pass data from the vertex program to the fragment program

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.