CG in Two Pages

zhaozj2021-02-11  190

CG in Two Pages

Mark J.Kilgard Nvidia Corporation Austin, Texas January 16, 2003

Game Engine Development Network Gong Minmin Translation

CG use case

CG is a language for GPU programming. The CG program looks like a C program. There is a CG vertex program here:

Void SimpleTransform (Float4 ObjectPosition: position,

Float4 Color: Color,

FLOAT4 DECALCOORD: Texcoord0,

FLOAT4 LIGHTMAPCOORD: TEXCOORD1,

Out float4 clipposition: position,

Out float4 Ocolor: Color,

Out float4 Odecalcoord: Texcoord0,

Out float4 Olightmapcoord: Texcoord1,

Uniform Float Brightness,

Uniform float4x4 modelviewprojection

{

Clipposition = MUL (ModelViewProjection, ObjectPosition);

Ocolor = Brightness * Color;

ODECALCOORD = DECALCOORD;

Olightmapcoord = lightmapcoord;

}

1.1 Popular program explanation

This program converts a vertex object space through a 4x4 matrix that contains world, observation and projection. The resulting result vector is the position of the vertex in the clamping space. The color of each vertex is configured by a floating point parameter before the output. Similarly, two texture coordinate sets naturally passed.

CG supports scalar data type, such as float, but also supports vector data types well. FLOAT4 indicates the vector containing four floating point numbers. Float4x4 represents a matrix. MUL is a standard library function, complete matrix and vector multiplication. The CG provides a function overload like C ; MUL is an overload function, so it can be used for all combinations of vector and matrix multiplied.

CG provides an operator like CG. However, unlike C, CG operators can accept and return scales and vectors. For example, the scalar Brightness is contracted by the vector Color, just like you expect.

In CG, a parameter is declared with UNIFORM modifiers indicating that its value is initialized by the external data source, and remains unchanged in the process of given this batch. From this point of view, the Uniform modifier in the CG is different from the Uniform modifier in the renderman, but the use occasion is used. In fact, the external data source is some OpenGL or Direct3D status that your application is loaded. For example, your program must provide the ModelViewProjection matrix and the Brightness scalar. The CG run library provides an API to transform the status of your program to compile the appropriate API status.

The position, color, texcoord0 and texcoord1 markers behind ObjectPosition, Color, Decalcoord and Lightmapcoord parameters are called input semantics. They indicate how the parameters are initialized by data different from each vertex. In OpenGL, the GlvertEX command requires the position to enter semantics; the GLColor command requires the Color semantic; the GLMULTITEXCOORD command requires Texcoordn semantics.

OUT modifiers indicate that Clipposition, Ocolor, ODecalcoord and OLightmapcoord parameters are output by this program. The semantics followed behind the parameters are output semantics. These semantics indicate that the program outputs a converted clamping space position and color amount. Similarly, the two texture coordinate sets also passed. The resulting result vertices are provided to the primary assembler to generate a rasterized element. Compiling this program requires the program source code, to compile the entry function name and the Profile name (vs_1_1).

The CG compiler can then compile the above CG to DirectX 8 Vertex Shader:

VS.1.1

MOV OT0, V7

MOV OT1, V8

DP4 Opos.x, C1, V0

DP4 OPOS.Y, C2, V0

DP4 OPOS.Z, C3, V0

DP4 OPOS.W, C4, V0

MUL OD0, C0.x, V5

PROFILE is used to indicate that the program is to be compiled as which API running environment. The same program can be compiled as a DirectX 9 Vertex Shader Profile (VS_2_0), multi-vendor OpenGL vertex program extension (ARBVP1) or NVIDIA private OpenGL extensions (VP20 and VP30).

The process of compiling the CG program can be performed when you initialize the program of CG. The CG runtime contains a CG compiler, which contains a very simple process in the API, allowing you to configure compiled programs for OpenGL or Direct3D.

1.2 explanation of the block

In addition to writing procedures for writing the vertex, you can write procedures for handling the block. There is a CG block program here:

Float4 BrightlightMapDecal (Float4 Color: Color,

FLOAT4 DECALCOORD: Texcoord0,

FLOAT4 LIGHTMAPCOORD: TEXCOORD1,

Uniform Sampler2D Decal,

Uniform Sampler2D LightMap: Color

{

FLOAT4 D = TEX2DPROJ (DECAL, DECALCOORD);

FLOAT4 LM = TEX2DPROJ (Lightmap, Lightmapcoord);

RETURN 2.0 * Color * D * LM;

}

The input parameters are color interpolation and two texture coordinate sets, which are defined by their input semantic definitions.

The Sampler2D type corresponds to a 2D texture unit. The CG standard library example tex2dproj can perform 2D texture mapping lookup. Two tex2dproj calls were sampled for DECAL and Light Map textures, and then assigned the results to local variables D and LM, respectively.

This program multiplies two texture results, color interpolation, and constant 2.0, and draws the result of RGBA color. This program returns a FLOAT4 and the return value is the semantics of Color, that is, the final color of the block.

When compiled as an ARBFP1 multi-vendor OpenGL Piece PROFILE, the CG compiler converts BrightlightMapDecal to the following code:

!! ARBFP1.0

PARAM C0 = {2, 2, 2, 2}; Temp R0; Temp R1; Temp R2;

TXP R0, FRAGMENT.TEXCOORD [0], TEXTURE [0], 2D;

TXP R1, FRAGMENT.TEXCOORD [1], Texture [1], 2D;

Mul R2, C0.x, Fragment.color.primary;

Mul R0, R2, R0;

Mul Result.color, R0, R1;

End

The same program can be compiled as Profile (PS_1_3 and PS_2_x) or NVIDIA private OpenGL extensions (FP20 and FP30) for DirectX 8 or 9. 2. Other CG features

2.1 Features from C

CG provides structures and arrays, including multi-dimensional arrays. CG provides all CMT schools ( , *, /, etc.). CG provides a Boolean type and those related to Boolean types (||, && ,!, etc.). CG provides increment / decrement ( / -) operator, conditional expression operator (? :), assignment expressions ( =, etc.), even C comma operators.

CG provides a user-defined function (except for defined standard library functions), but the recursive function is not allowed. CG provides a subset of CG control flow structures (Do, While, For, IF, Break, Continue); other structures such as GOTO and SWITCH are not supported in current CG implementation, but retains the necessary keywords.

Just like C, CG's accuracy and scope of data types is not subject to restrictions. In fact, the selection of PROFILE used to compile determines the specific performance of each data type. FLOAT, HALF and DOUBLE are used to represent a continuous value, and the ideal floating point, but this depends on Profile. HALF refers to a 16-bit semi-precision floating point data type (NVIDIA's CINEFX architecture provides such a data type). INT is an integer and is usually used for loops and indexes. Fixed is an additional data type that is used to indicate a fixed point of the floating point number.

CG provides # include, # define, # ifdef, etc., and the pre-processing of C. CG supports C and C comment styles.

2.2 C no additional characteristics

CG provides built-in constructor to the vector data type (similar to C , but is not customized by user):

Float4 vec1 = float4 (4.0, -2.0, 5.0, 3.0);

SWIZZLING is a method for restructuring or constructing or constructing shorter or longer vector. The example is as follows:

FLOAT2 VEC2 = vec1.yx; // vec2 = (-2.0, 4.0)

FLOAT SCALAR = vec1.w; // scalar = 3.0

Float3 vec3 = scalar.xxx; // vec3 = (3.0, 3.0, 3.0)

Matrices can be used with more complex Swizzling syntax. Vector and matrix elements can also be accessed by standard array index syntax.

The write mask can limit only the part assigned to the vector. The example is as follows:

Vec1.xw = vec3; // vec1 = (3.0, -2.0, 5.0, 3.0)

Use the .xyzw or .RGBA suffix to form swizzling and write masks.

The CG standard library contains a large number of built-in mathematics (ABS, DOT, LOG2, REFLECT, RSQRT, etc.) and texture access functions (Texcube, Tex3DProj, etc.). The standard library has widely used function overload (similar to C ) to support vector and different data types of different lengths. You don't have to use #include in C to include the prototype of the standard library case, and the CG standard library example is automatically generated.

In addition to the OUT modifier for the call-by-result parametery, the inout modifier makes the parameters both the call-by-value input parameter is also a call-by-result output parameter.

Keyword Discard is similar to return, but does not return to the conversion of good chiefs are terminated. 2.3 Unsupported features

CG currently does not support pointers and bit operations (but, the necessary C operators and keywords are reserved for these purposes). CG (current) does not support the union of the consortium and function.

CG does not have C "large programming" features, such as classes, templates, operator overload, exception handling, and namespace.

The CG standard library has no functional comprehension, such as string processing, file input / output, and memory configuration, which exceeds CG's proprietary field.

However, CG retains all C and C keywords, which can be incorporated from these languages ​​to the future CG implementation guarantee.

3. Profile dependence

When you compile a C or C program, it will not be compiled because the program is too large (within a certain premise) or because of the use of this program. Programs that are correct for CGs, a syntax and semantics may still be compiled because it is limited to the Profile you compile this program.

For example, when you use the vertex profile, if you access textures, an error is generated. Future Vertex Profile may allow texture to access, but now PROFILE is not. Other errors are more intrinsic. For example, a piece of PROFILE cannot output a parameter in a TexCoord0 semantic. Other errors may be because the current GPU capability range, such as exceeding the maximum number of instructions or the number of texture units available.

To know that these Profile dependencies do not reflect the restrictions of the CG language, but the current restriction of CG implementation or the potential hardware limit of your target GPU.

4. Compatibility and transplantability

NVIDIA's CG implementation is very similar to Microsoft's Advanced Coloring Language (HLSL) because they are developed. HLSL is integrated with DirectX 9 and Windows operating systems. CG provides support for multi-API (OpenGL, DirectX 8, and DirectX 9) and multi-operating systems (Windows, Linux, and Mac OS X). Because CG has an interface to multi-vendor API, CG can run on multiple vendors' GPUs.

5. More information

You can see "The CG Tutorial: The Definitive Guide to Programmable Real-Time Graphics" (ISBN 0321194969), published by Addison-Wesley in March 2003. See http://www.awprofessional.com/titles/0321194969.

转载请注明原文地址:https://www.9cbs.com/read-4790.html

New Post(0)