Unity Shader the foundation for Learning 5--CG language

Source: Internet
Author: User
Tags array length arrays bool constant versions

We know that there are three shader lab languages for shader programming: OpenGL GLSL, Direct3D HLSL, and Navidia's CG language.

Since the CG program can run on both OpenGL and Direct3D without any processing , I chose CG as the programming language for developing shader, and then I came to understand some of the basic features of CG.


First, the compilation method of CG:

1. Compiling:

Compile the program:

A computer can only understand and execute a machine language consisting of 0, 1 sequences (voltage sequences), so assembly language and high-level language programs need to be translated to be understood by the computer, and the program that takes on this task is called a language processing program, often referred to as a compiler program.

Static Compilation:

Once compiled, you do not need to recompile unless you change the program code, which is called Static compilation (compilation). The most important feature of static compilation is that once it is compiled into an executable file, the source information is no longer needed during the execution of the executable.

Dynamic Compilation:

Both the compiler and the source code are involved in the process of running the program.


CG usually uses dynamic compilation (CG also supports static compilation), that is, when the host program runs, the CG runtime (CG Runtimer Library) to dynamically compile the CG code. Using the method of dynamic compilation, the CG program can be used as a script, ready to modify at any time, save time, in the Ogre graphics engine in this way.


2.Cg Compiler:

The CG compiler first translates the CG program into a form that can be accepted by the graphics API (OpenGL and Direct3D), and then the application uses the appropriate OpenGL and Direct3D commands to pass the translated CG program to the graphics processor, OpenGL and Dire The Ct3d driver finally translates it into the hardware executable format required by the graphics processor. The Cg compiler provided by NVIDIA is Cgc.exe.

After installing CG Toolkit, Cgc.exe files can be found under the Navidia Corporation\cg\bin folder. Various commands:

CG Program compiled commands: CGC [options] File

--[options] Represents an optional configuration item, file represents the CG program filename.


For example, a typical compilation method: Cgc-profile glslv-entry Main_v TEST.CG

The GLSLV is the profile name, the name of the profile that is currently used, the entry function names configuration entry for the-entry shader, and Main_v is the name of the entry functions for the vertex shader program; TEST.CG Is the current shader file name (must have a suffix name).


convert a shader written by the CG language to a program written using GLSL or HLSL:

cgc–profile glslv–o direct.glsl–entry main_v TEST.CG

--Represents the vertex shader in the compiled file TEST.CG, the entry function is named Main_v, and the vertex shader program is converted to a GLSL program, and then saved as a file DIRECT.GLSL.


remark: GPU programming, a shader program that cannot track debug shaders, syntax errors can be found by the compiler, but logic errors in code can only be considered for lookups.


3.Cg Profiles:

The compilation of CG program not only relies on the three-dimensional programming interface used by the host program, but also relies on the graphics hardware environment, because the graphics hardware own limitations, not necessarily support some kind of Cg statement.

A subset of CG languages supported by a particular graphics hardware environment or AIP, known as CG Profiles . Profile is divided into: the profile of the vertex program and the profile of the fragment program, so the vertex shader must be compiled with the current graphics hardware support of the vertex profile, in the same vein, the compilation fragment shader must choose the current graphics hardware support fragment profile. Vertex profile and fragment profiles are divided into versions based on different versions or extensions of OpenGL and DirectX.

The profiles currently supported by Cg compiler are:

OpenGL ARB Vertex Programs
Runtime PROFILE:CG_PROFILE_ARBVP1
Compiler option: _profile ARBVP1
OpenGL ARB Fragment Programs
Runtime PROFILE:CG_PROFILE_ARBFP1
Compiler option: _profile ARBFP1
......

ii. CG data types:

1. Basic Data type: CG supports 7 basic data types, namely:

Float, 32-bit floating point data, one sign bit. Floating-point data types are supported by all profiles half,16 for floating-point data int,32 bit shaping data, and some profiles use the int type as a float type fixed,12 bit fixed point number, which is supported by all fragment profiles BOOL, Boolean data, usually used for the IF and conditional operators (?:), the Boolean data type is
Profiles supports simpler*, a handle to a texture object (the handle to a texture object), divided into 6 categories:
Sampler, sampler1d, sampler2d, Sampler3d, Samplercube, and Samplerrect. DirectX profiles does not support the Samplerrect type, except that these types are supported by all Pixelprofiles and NV40 vertex program profiles (cgusersmanual 30 pages). Thus, in the near future, the vertex program will also widely support texture manipulation string, character type, the type is not supported by the current existing profile, there is no need to use the CG program in the character type, but you can declare the type variable through the CG runtime API, and assign a value Therefore, the type variable can hold information about the Cg file.
The first 6 types are common types, and string types are hardly used. In addition, CG also provides a built-inVector data Types(built-in vector data types), the built-in vector datatype is based on the underlying data type. For example: FLOAT4, which represents a 4-tuple vector of type float, and Bool4, which represents a bool type 4-element vector.Note:The vector can not exceed 4 yuan, that is, in the CG program, the FLOAT1, Float2, FLOAT3, FLOAT4 types of array variables, but cannot declare more than 4 yuan of the vector. The vector initialization method is generally:

    FLOAT4 array = FLOAT4 (1.0, 2.0, 3.0, 4.0);

Longer vectors can also be built with shorter vectors:
    Float2 A = FLOAT2 (1.0, 1.0);
    FLOAT4 B = float4 (A, 0.0, 0.0);
In addition, CG also provides matrix data types, but the maximum dimensions cannot exceed the 4*4 order, for example:
    float1x1 matrix1;//is equivalent to float matirx1; X is a character, not a multiplication sign.
    the float2x3 matrix2;//represents a 2*3-order matrix that contains 6 float data
    float4x2 matrix3;//represents a 4*2-order matrix with 8 float-type data
    float4x4 ma The trix4;//represents the 4*4-order matrix, which is the maximum number of dimensions

Matrix initialization:
   float2x3 matrix5 = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

in CG, vectors, matrices and arrays are completely different, vectors and matrices are built-in data types, and data is a structure.
2. Arrays:The role of the array data type in CG: As a function of formal parameters, for a large number of data transfer, such as: Vertex parameter array, illumination parameter data. One-dimensional arrays:
    Float a[10];//declares an array that contains 10 float type data
    float a[4] = {1.0, 2.0, 3.0, 4.0};//initialize an array
    int length = a.length;//Get array Length

Multidimensional Arrays:
    Float B[2][3] = {{0.0, 0.0, 0.0},{1.0, 1.0, 1.0}};
    int length1 = B.length; The Length1 value is 2
    int length2 = b[0].length;//Length2 value is 3
3. Structure:A struct's declaration begins with a keyword struct, followed by the struct's name, followed by a curly brace, and terminated with a semicolon (don't forget the semicolon). Braces are definitions of structs, divided into two main categories: member variables and member functions. For example:
    struct Myadd
    {
        float val;
        float Add (float x)
        {
            return val + x;
        }
    };

    Myadd s;

Use the symbol "." member variables and member functions that reference struct bodies:
    float a = S.value;
    Float B = S.add (a);

In general, the source code of the Cg will define two structures in the file header, respectively, to define the type of the input and output, the two struct definition differs from the normal C structure definition, in addition to defining the data type of the struct member, defines the binding semantic type (binding semantics) of the member. The so-called binding semantic types are designed to recognize different data types when exchanging data with the hosting environment. Currently, the types of binding semantics supported by CG include postion position, color, normal (normal vector), texcoord (texture coordinate), and so on.
4. Type conversion:Type conversions in Cg are similar to type conversions in the C language. Type conversions in C can be either coercion or implicit conversions, or, if the latter, the data type is converted from low to high precision. This is true in the Cg language as well.
    float A = 1.0;
    Half B = 2.0;
    float C = a+b; Equivalent to float c = a + (float) b;

When there is a type variable and no type constant data for the operation, the constant data does not do type conversion, for example:
    float A = 1.0;
    Float B = A + 2.0; 2.0 is untyped constant data, compile-time as float type

In Cg language, for constant data, you can add a type suffix that represents the type of the data, for example:
    float A = 1.0;
    Float B = A + 2.0h; 2.0h for half type constant data, operation is required to do type conversion

There are 3 types of constant suffixes: F: Indicates float h: Half x: Indicates fixed

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.