• Aucun résultat trouvé

Vertex Processing

Dans le document Real-Time Volume Graphics (Page 53-56)

GPU Programming

2.2 Vertex Processing

Compositing. The compositing step is the final step before the fragments are written into the frame buffer. Several tests are applied that finally determine whether the incoming fragment must be discarded (e.g., due to occlusion) or displayed on the screen. Frame buffer operations also decide how the color of the incoming fragment is combined with the color value stored in the frame buffer at the corresponding raster position.

In order to fully understand all the techniques explained in the follow-ing chapters, it is important to know the exact orderfollow-ing of operations in the graphics pipeline. Let us examine the different stages of the graphics pipeline in a little more detail.

2.2 Vertex Processing

The vertex processor performs the so-called per-vertex operations. These are operations that modify the incoming stream of vertices. It is impor-tant to note that the vertex processor can only modify existing vertices. It can neither discard vertices nor insert additional vertices into the stream.

Every vertex that enters the pipeline has a set of attributes, such as its position, the normal vector, and several texture coordinates and color val-ues. The vertex processor usually computes linear transformations of the position and the normal vector, such as translation, rotation, nonuniform scaling, and projection. Position and normal vectors in general are repre-sented by four-component vectors in homogeneous coordinates. The linear transformations are carried out by multiplying the vertices with 4×4 ma-trices, such as the well-known modelviewmatrix or theprojection matrix.

Remember that, in order to maintain consistency, normal vectors must be multiplied by the transposed inverse of themodelviewmatrix. This ensures that they stay perpendicular to the surface elements.

In the traditional fixed-function pipeline, local illumination is calculated for each vertex during geometry processing, and the illumination terms have been interpolated for each fragment (Gouraud or smooth shading).

This is the reason why the vertex processor has formerly been referred to as thetransform & lightunit (T&L). This term, however, is no longer appropriate, because in the programmable pipeline, local illumination is usually computed by the fragment processor (Phongshading).

2.2.1 Vertex Programs

Vertex programs are user-written microprograms that substitute major parts of the traditional fixed-function computation of the geometry pro-cessing unit. They are used to customize the vertex transformations and

i

allow almost arbitrary modifications of the vertex attributes. A specified vertex program is executedonce per vertex. Every time a vertex enters the pipeline, the vertex processor receives a set of vertex attributes, executes the vertex program, and finally emits the attributes for exactly one vertex.

The programmable vertex processor is outlined in Figure 2.2. The ver-tex program stored in the instruction memory of the verver-tex processor is executed for each vertex independently. At the beginning of the outer loop, an instruction is first fetched and decoded. The operands for the

Emit

Figure 2.2. The programmable vertex processing unit executes a vertex program stored in local video memory. The vertex texture fetch is only available on graphics boards that support Shader Model 3.0. (Image inspired by Mark Kilgard’s original diagram inThe Cg Tutorial[71].)

2.2 Vertex Processing 37 instruction are then read from input registers, which contain the original vertex attributes, or from temporary registers, which store intermediate results. Constant parameters in the program are usually declared as uni-form variables and are treated the same way as input parameters. Their values are specified by the programmer and cannot be changed during the execution. Most instructions are floating-point vector operations that are performed onxyzw components for homogeneous coordinates or the RGBA quadruplets for colors. Both notations are equivalent. Input mapping al-lows the programmer to specify, duplicate, and exchange the indices of the vector components (a process known asswizzling) and also to negate the re-spective values. After the operands are correctly mapped, the instruction is executed, and the result is eventually written to temporary or output registers. At the end of the loop, the vertex processor checks whether or not there are more instructions to be executed and decides to reenter the loop or terminate the program by emitting the output registers to the next stage in the pipeline. On modern GPUs that support loops and conditional branches in the vertex program, the next instruction to be executed does not have to be the next instruction in the command sequence.

A simple example of a vertex program in Cg is shown in Listing 2.1.

The parameters declared in the main function specify the input and output parameters of the function as well as uniform parameters. The input and output parameters in this example are the same: a vertex consists of a

po-// A simple vertex program in Cg void main( float4 Vertex : POSITION,

half3 Color : COLOR, half3 TexCoord : TEXCOORD0, uniform float4x4 matModelViewProj,

out float4 VertexOut : POSITION, out half3 ColorOut : COLOR, out half3 TexCoordOut : TEXCOORD0) {

// transform vertex into screen space VertexOut = mul(matModelViewProj, Vertex);

// hand over color and texture coordinate ColorOut = Color;

TexCoordOut = TexCoord;

return;

}

Listing 2.1. A simple example of a vertex program in Cg.

i i

i i

i i

i i

38 GPU Programming

sition in 3D space, a color value, and a texture coordinate. The compound modeling, viewing, and projection matrix is specified as a uniform param-eter in this program. The position of the incoming vertex is in local model coordinates and must be multiplied by this matrix to transform it into screen space. The color value and texture coordinates remain unchanged and are simply handed down the pipeline. This simple vertex program is all that we need for most of the rendering tasks described in this book. At the end of Chapter 3, we will see a couple of more sophisticated examples of vertex programs.

2.2.2 Vertex Textures

Until recently, only fragment programs were allowed to perform texture fetches. On graphics cards that support the Shader Model 3.0 specifica-tion, vertex programs can perform texture look-ups as well. In this case, there is a separate path in the vertex processing unit as shown in Fig-ure 2.2. If the active instruction is a textFig-ure fetch operation, the vertex shader computes the memory address of the texel1 from the given tex-ture coordinates. It then fetches the textex-ture samples that are required to compute the texel color. Depending on the underlying hardware, not all filtering methods available in the fragment processor may be supported by the vertex processor. Vertex texture fetches are often restricted to nearest-neighbor interpolation.

Dans le document Real-Time Volume Graphics (Page 53-56)