Cg - The Cg Tutorial Ch2. The Simplest Programs 筆記
1. 不同於一般用其他高階語言撰寫的程式,可以執行很多不同種類的工作
Cg programs are limited to outputting a bundle of values.
2. float4 is not a reserved word in Cg; it is a standard type definition in the Cg Standard Library.
Unlike in C/C++, there is no need to specify a preprocessor statement (such as #include) to
iclude declarations for Cg's Standard Library. Instead, Cg automatically includes the declarations
needed by most Cg programs.
3. For example, float x[4] is not the same declaration as float4 x. These vector types are packed arrays,
which are often just called vectors. If two input vectors are stored in packed form, programmable
graphics hardware typically performs three or four-component math operations—such as multiplications
and additions—in a single instruction. 即兩個vectors可以直接做運算
For example : float4 v1, v2, result;
result = v1 + v2;
4. Semantics are the glue that binds a Cg program to the rest of the graphics pipeline. For example,
it is the use of the POSITION semantic after the member name—not the name of the member itself
—that makes the rasterizer treat the position member as a position.
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;
}
5. Input and output semantics are not the same, even though some have the same names. For example,
for an input parameter to a vertex program, POSITION refers to the application-specified position
assigned by the application when it sends a vertex to the GPU. However, an output structure element
with the POSITION semantic represents the clip-space position that is fed to the hardware rasterizer.
上述程式中, POSITION為input samentic; POSITION為output samentic
6. When you use the float4 or similar vector type name like a function (for example, float4(0, 1, 0, 1)),
this is called a constructor. Constructors in Cg are provided for vectors and matrices.
7. CGprofile會去選擇一個適合自己顯示卡的profile來使用,當一個program在不同的computer上跑,
或者是使用不同的programming interface(OpenGL or DirectX),則必須根據不同的environment來
選擇不同的profile來使用
8. There are two classes of compilation errors for Cg programs: conventional and profile-dependent.
① Conventional errors are caused either by incorrect syntax, usually due to typos, or by incorrect
semantics, such as calling a function with the wrong number of parameters.
② Profile-dependent errors result from using Cg in a way that is syntactically and semantically correct
but not supported by your specified profile. General-purpose programming languages do not have
this type of error.
9. The COLOR input semantic for a fragment program is the color of the fragment interpolated by the rasterizer,
based on the primitive's assigned vertex colors.
{
C2E2f_Output OUT;
OUT.color = color;
return OUT;
}
10. In Cg, you do not invoke a program that runs until it terminates, as you would in C/C++. Instead, the
Cg compiler translates your program into a form that your 3D programming interface can download to
hardware. It is up to your application to call the necessary Cg runtime and 3D programming interface
routines to download and configure your program for use by the GPU. When the application feeds a
vertex to the GPU, the vertex processor runs that program every time.

Compiling and Loading a Cg Program into the GPU
11. The vertex program passes the specified 2D position of each vertex to the rasterizer. The rasterizer
expects positions to be specified as coordinates in clip space. Clip space defines what is visible from
the current viewpoint. The GPU then transforms clip space automatically (by a simple scale and bias)
into window space coordinates for the rasterizer.
浙公网安备 33010602011771号