Graphics and GPU Programming

(Last modification: March 31st, 2002)

[size="5"]Preface

During this part of the introduction a very simple program that shows a rotating quad will evolve into a more sophisticated application showing a B?zier patch class with a diffuse and specular reflection model, featuring a point light source. The example applications are all build on each other in a way that most of the code of the previous example is re-used in the following example. This way the explanation of the features stayed focused on the advancements of the specific example.

[size="5"]RacorX

Figure 1 - RacorX

RacorX (see attached resource file) displays a green color, that is applied to the quad evenly. This example demonstrate the usage of the common file framework, provided with the DirectX 8.1 SDK and how to compile vertex shaders with the D3DXAssembleShader() function.

Like with all the upcoming examples, which are based on the Common Files, [lessthan]Alt>+[lessthan]Enter> switches between the windowed and full-screen mode, [lessthan]F2> gives you a selection of the usable drivers and [lessthan]Esc> will shutdown the application.

First let's take a look at the files you need to compile the program:

Figure 2- Directory Content

The source file is RacorX.cpp, the resource files are winmain.rc and resource.h. The icon file is directx.ico and the executable is RacorX.exe. The remaining files are for the use of the Visual C/C++ 6 IDE.

To compile this example, you should link it with the following *.lib files:
• d3d8.lib
• d3dx8dt.lib
• dxguid.lib
• d3dxof.lib
• winmm.lib
• gdi32.lib
• user32.lib
• kernel32.lib
• advapi32.lib Most of these *.lib files are COM wrappers. The d3dx8dt.lib is the debug version of the Direct3DX static link library.
[bquote]The release Direct3DX static link library is called d3dx8.lib. There is also a *.dll version of the debug build called d3dx8d.dll in the system32 directory. It is used by linking to the d3dx8d.lib COM wrapper.[/bquote]
All of these *.lib files have to be included in the <Object/libary modules:> entry field. This is located at [lessthan]Project->Settings> and there under the [lessthan]Link> tab:

Figure 3 - Project Settings

The provided Visual C/C++ 6 IDE workspace references the common files in a folder with the same name:

Figure 4 - Workspace

Figure 5 - Add Files to Project

[size="3"]The Common Files Framework

The common files framework helps getting up to speed, because:
• It helps to avoid how-tos for Direct3D in general, so that the focus of this text is the real stuff.
• It's common and tested foundation, which helps reduce the debug time.
• All of the Direct3D samples in the DirectX SDK use it. Learning time is very short.
• Its window mode makes debugging easier.
• Self-developed production code could be based on the common files, so knowing them is always a win. A high-level view of the Common Files shows 14 *.cpp files in

[font="Courier New"][color="#000080"] C:\DXSDK\samples\Multimedia\Common\src[/color][/font]

These files encapsulate the basic functionality you need to start programming a Direct3D application. The most important d3dapp.cpp contains the class CD3DApplication. It provides seven functions that can be overridden and that are used in the main *.cpp file of any project in this introduction:
• virtual HRESULT OneTimeSceneInit() { return S_OK; }
• virtual HRESULT InitDeviceObjects() { return S_OK; }
• virtual HRESULT RestoreDeviceObjects() { return S_OK; }
• virtual HRESULT DeleteDeviceObjects() { return S_OK; }
• virtual HRESULT Render() { return S_OK; }
• virtual HRESULT FrameMove( FLOAT ) { return S_OK; }
• virtual HRESULT FinalCleanup() { return S_OK; } All that has to be done, to create an application based on this framework code is to create a new project and new implementations of these overridable functions in the main source file. This is also shown in all Direct3D examples in the DirectX SDK.

RacorX uses these framework functions in RacorX.cpp. They can be called the public interface of the common files framework.

Figure 6 - Framework Public Interface

The following functions are called in the following order in racorx.cpp at startup:
• ConfirmDevice()
• OneTimeSceneInit()
• InitDeviceObjects()
• RestoreDeviceObjects() Now the application is running. While it is running, the framework calls
• FrameMove()
• Render() in a loop.

If the user resizes the window, the framework will call
• InvalidateDeviceObjects()
• RestoreDeviceObjects() If the user presses F2 or clicks [lessthan]File>->[lessthan]Change device> and changes the device by choosing for example another resolution or color quality, the framework will call
• InvalidateDeviceObjects()
• DeleteDeviceObjects()
• InitDeviceObjects()
• RestoreDeviceObjects() If the user quits the application, the framework will call
• InvalidateDeviceObjects()
• DeleteDeviceObjects()
• FinalCleanup() There are matching functional pairs. InvalidateDeviceObjects() destroys what RestoreDeviceObjects() has build up and DeleteDeviceObjects() destroys what InitDeviceObjects() has build up. The FinalCleanup() function destroys what OneTimeSceneInit() build up.

The idea is to give every functional pair its own tasks. The OneTimeSceneInit() / FinalCleanup() pair is called once at the beginning and the end of a life-cycle of the game. Both are used to load or delete data, which is not device dependant. A good candidate might be geometry data. The target of the InitDeviceObjects() / DeleteDeviceObjects() pair is, like the name implies, data that is device dependant. If the already loaded data has to be changed, when the device changes, it should be loaded here. The following examples will load, re-create or destroy their vertex buffer and index buffers and their textures in these functions.

The InvalidateDeviceObjects() / RestoreDeviceObjects() pair has to react on changes of the window size. So for example code that handles the projection matrix might be placed here. Additionally the following examples will set most of the render states in RestoreDeviceObjects().

Now back to RacorX. Like shown in part 1 of this introduction, the following list tracks the life-cycle of a vertex shader:
• Declaration of the vertex shader with the D3DVSD_* macros, to map vertex buffer streams to input registers
• Free vertex shader resources handled by the Direct3D engine with DeleteVertexShader() We will walk step-by-step through this list in the following pages.

The supported vertex shader version is checked in ConfirmDevice() in racorx.cpp:

HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format ) { if( (dwBehavior & D3DCREATE_HARDWARE_VERTEXPROCESSING ) || (dwBehavior & D3DCREATE_MIXED_VERTEXPROCESSING ) ) { if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) ) return E_FAIL; } return S_OK; }
If the framework has already initialized hardware or mixed vertex processing, the vertex shader version will be checked. If the framework initialized software vertex processing, the software-implementation provided by Intel and AMD jumps in and a check of the hardware capabilities is not needed.

The globally available pCaps capability data structure is filled with a call to GetDeviceCaps() by the framework. pCaps->VertexShaderVersion holds the vertex shader version in a DWORD. The macro D3DVS_VERSION helps checking the version number. For example the support of at least vs.2.0 in hardware will be checked with D3DVS_VERSION(2,0).

After checking the hardware capabilities for vertex shader support, the vertex shader has to be declared.

Declaring a vertex shader means mapping vertex data to specific vertex shader input registers, therefore the vertex shader declaration must reflect the vertex buffer layout, because the vertex buffer must transport the vertex data in the correct order. The one used in this example program is very simple. The vertex shader will get the position data via v0.

// shader decl DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // D3DVSDE_POSITION,0 D3DVSD_END() }; The corresponding layout of the vertex buffer looks like this: // vertex type struct VERTEX { FLOAT x, y, z; // The untransformed position for the vertex }; // Declare custom FVF macro. #define D3DFVF_VERTEX (D3DFVF_XYZ)
The position values will be stored in the vertex buffer and bound through the SetStreamSource() function to a device data stream port, that feed the primitive processing functions (this is the Higher-Order Surfaces (HOS) stage or directly the vertex shader, depending on the usage of HOS; see the Direct3D pipeline in part 1).

We do not use vertex color here, so no color values are declared.

[size="3"]Setting the Vertex Shader Constant Registers

The vertex shader constant registers have to be filled with a call to SetVertexShaderConstant(). We set the material color in RestoreDeviceObjects() in c8 in this example:

// set material color FLOAT fMaterial[4] = {0,1,0,0}; m_pd3dDevice->SetVertexShaderConstant(8, fMaterial, 1);

HRESULT SetVertexShaderConstant (DWORD Register, CONST void* pConstantData, DWORD ConstantCount);
The first parameter provides the number of the constant register that should be used. In this case 8. The second parameter stores the 128bit data in that constant register and the third parameter gives you the possibility to use the following registers as well. A 4x4 matrix can be stored with one SetVertexShaderConstant() call by providing the number four in ConstantCount. This is done for the clipping matrix in FrameMove():

// set the clip matrix ... m_pd3dDevice->SetVertexShaderConstant(4, matTemp, 4);
This way the c4, c5, c6 and c7 registers are used to store the matrix.

The vertex shader that is used by RacorX is very simple:

// reg c4-7 = WorldViewProj matrix // reg c8 = constant color // reg v0 = input register const char BasicVertexShader[] = "vs.1.1 //Shader version 1.1 \n"\ "dp4 oPos.x, v0, c4 //emit projected position \n"\ "dp4 oPos.y, v0, c5 //emit projected position \n"\ "dp4 oPos.z, v0, c6 //emit projected position \n"\ "dp4 oPos.w, v0, c7 //emit projected position \n"\ "mov oD0, c8 //material color = c8 \n";
It is used inline in a constant char array in RacorX.cpp. This vertex shader incorporates the vs.1.1 vertex shader implementation rules. It transforms from the concatenated and transposed world-, view- and projection-matrix to the clip matrix or clip space with the four dp4 instructions and kicks out into oD0 a green material color with mov.

As shown above, the values of the c4 - c7 constant registers are set in FrameMove(). These values are calculated by the following code snippet:

// rotates the object about the y-axis D3DXMatrixRotationY( &m_matWorld, m_fTime * 1.5f ); // set the clip matrix D3DXMATRIX matTemp; D3DXMatrixTranspose( &matTemp , &(m_matWorld * m_matView * m_matProj) ); m_pd3dDevice->SetVertexShaderConstant(4, matTemp, 4);
First the quad is rotated around the y-axis by the D3DMatrixRotationY() call, then the concatenated matrix is transposed and then stored in the constant registers c4 - c7. The source of the D3DMatrixRotationY() function might look like:

VOID D3DMatrixRotationY(D3DMATRIX * mat, FLOAT fRads) { D3DXMatrixIdentity(mat); mat._11 = cosf(fRads); mat._13 = -sinf(fRads); mat._31 = sinf(fRads); mat._33 = cosf(fRads); } = cosf(fRads) 0 -sinf(fRads) 0 0 0 0 0 sinf(fRads) 0 cosf(fRads) 0 0 0 0 0
So fRads equals the amount you want to rotate about the y-axis. After changing the values of the matrix this way, we transpose the matrix by using D3DXMatrixTranspose(), so that its columns are stored as rows. Why do we have to transpose the matrix?

A 4x4 matrix looks like this:
[bquote][font="Courier New"][color="#000080"]a b c d
e f g h
i j k l
m n o p[/color][/font][/bquote]
The formula for transforming a vector (v0) through the matrix is:

dest.x = (v0.x * a) + (v0.y * e) + (v0.z * i) + (v0.w * m) dest.y = (v0.x * b) + (v0.y * f) + (v0.z * j) + (v0.w * n) dest.z = (v0.x * c) + (v0.y * g) + (v0.z * k) + (v0.w * o) dest.w = (v0.x * d) + (v0.y * h) + (v0.z * l) + (v0.w * p)

So each column of the matrix should be multiplied with each component of the vector. Our vertex shader uses four dp4 instructions:

dest.w = (src1.x * src2.x) + (src1.y * src2.y) + (src1.z * src2.z) + (src1.w * src2.w) dest.x = dest.y = dest.z = unused
The dp4 instructions multiplies a row of a matrix with each component of the vector. Without transposing we would end up with:

dest.x = (v0.x * a) + (v0.y * b) + (v0.z * c) + (v0.w * d) dest.y = (v0.x * e) + (v0.y * f) + (v0.z * g) + (v0.w * h) dest.z = (v0.x * i) + (v0.y * j) + (v0.z * k) + (v0.w * l) dest.w = (v0.x * m) + (v0.y * n) + (v0.z * o) + (v0.w * p)
which is wrong. By transposing the matrix it looks like this in constant memory:
[bquote][font="Courier New"][color="#000080"]a e i m
b f j n
c g k o
d h l p[/color][/font][/bquote]
so the 4 dp4 operations would now yield:

dest.x = (v0.x * a) + (v0.y * e) + (v0.z * i) + (v0.w * m) dest.y = (v0.x * b) + (v0.y * f) + (v0.z * j) + (v0.w * n) dest.z = (v0.x * c) + (v0.y * g) + (v0.z * k) + (v0.w * o) dest.w = (v0.x * d) + (v0.y * h) + (v0.z * l) + (v0.w * p)
or

oPos.x = (v0.x * c4.x) + (v0.y * c4.y) + (v0.z * c4.z) + (v0.w * c4.w) oPos.y = (v0.x * c5.x) + (v0.y * c5.y) + (v0.z * c5.z) + (v0.w * c5.w) oPos.z = (v0.x * c6.x) + (v0.y * c6.y) + (v0.z * c6.z) + (v0.w * c6.w) oPos.w = (v0.x * c7.x) + (v0.y * c7.y) + (v0.z * c7.z) + (v0.w * c7.w)
which is exactly how the vector transformation should work.

dp4 gets the matrix values via the constant register c4 - c7 and the vertex position via the input register v0. Temporary registers are not used in this example. The dot product of the dp4 instructions is written to the oPos output register and the value of the constant register c8 is moved into the output register oD0, that is usually used to output diffuse color values.

The vertex shader that is stored in a char array is compiled with a call to the following code snippet in RestoreDeviceObjects():

// Assemble the shader rc = D3DXAssembleShader( BasicVertexShader , sizeof(BasicVertexShader) -1, 0 , NULL , &pVS , &pErrors ); if ( FAILED(rc) ) { OutputDebugString( "Failed to assemble the vertex shader, errors:\n" ); OutputDebugString( (char*)pErrors->GetBufferPointer() ); OutputDebugString( "\n" ); }
D3DXAssembleShader() creates a binary version of the shader in a buffer object via the ID3DXBuffer interface in pVS.

HRESULT D3DXAssembleShader( LPCVOID pSrcData, UINT SrcDataLen, DWORD Flags, LPD3DXBUFFER* ppConstants, LPD3DXBUFFER* ppCompiledShader, LPD3DXBUFFER* ppCompilationErrors );
The source data is provided in the first parameter and the size of the data length in bytes is provided in the second parameter. There are two possible flags for the third parameter called

#define D3DXASM_DEBUG 1 #define D3DXASM_SKIPVALIDATION 2
The first one inserts debug info as comments into the shader and the second one skips validation. This flag can be set for a working shader.

Via the fourth parameter a ID3DXBuffer interface can be exported, to get a vertex shader declaration fragment of the constants. To ignore this parameter, it is set to NULL here. In case of an error, the error explanation would be stored in a buffer object via the ID3DXBuffer interface in pErrors. To see the output of OutputDebugString() the debug process in the Visual C/C++ IDE must be started with [lessthan]F5>.

The vertex shader is validated and a handle for it is retrieved via a call to CreateVertexShader() in m_dwVertexShader: The following lines of code can be found in RestoreDeviceObjects():

// Create the vertex shader rc = m_pd3dDevice->CreateVertexShader( dwDecl, (DWORD*)pVS->GetBufferPointer(), &m_dwVertexShader, 0 ); if ( FAILED(rc) ) { OutputDebugString( "Failed to create the vertex shader, errors:\n" ); D3DXGetErrorStringA(rc,szBuffer,sizeof(szBuffer)); OutputDebugString( szBuffer ); OutputDebugString( "\n" ); }
CreateVertexShader() gets a pointer to the buffer with the binary version of the vertex shader via the ID3DXBuffer interface. This function gets the vertex shader declaration via dwDecl, that maps vertex data to specific vertex shader input registers. If an error occurs, its explanation is accessible via a pointer to a buffer object that is retrieved via the ID3DXBuffer interface in pVS->GetBufferPointer(). D3DXGetErrorStringA() interprets all Direct3D and Direct3DX HRESULTS and returns an error message in szBuffer.

It is possible to force the usage of software vertex processing with the last parameter by using the D3DUSAGE_SOFTWAREPROCESSING flag. It must be used when the D3DRS_SOFTWAREVERTEXPROCESSING member of the D3DRENDERSTATETYPE enumerated type is TRUE.

// set the vertex shader m_pd3dDevice->SetVertexShader( m_dwVertexShader );
The only parameter that must be provided is the handle to the vertex shader. This function executes the vertex shader as often as there are vertices.

Vertex shader resources must be freed with a call to

if ( m_dwVertexShader != 0xffffffff ) { m_pd3dDevice->DeleteVertexShader( m_dwVertexShader ); m_dwVertexShader = 0xffffffff; }
This example frees the vertex shader resources in the InvalidateDeviceObjects() framework function, because this has to happen in case of a change of the window size or a device.

The non-shader specific code of RacorX deals with setting render states and the handling of the vertex and index buffer. A few render states have to be set in RestoreDeviceObjects():

// z-buffer enabled m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); // Turn off D3D lighting, since we are providing our own vertex shader lighting m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); // Turn off culling, so we see the front and back of the quad m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
The first instructions enables the z-buffer (a corresponded flag has to be set in the constructor of the Direct3D framework class, so that the device is created with a z-buffer).

The fixed-function lighting is not needed, so it is switched off with the second statement. To be able to see both sides of the quad, backface culling is switched off with the third statement.

The vertex and index buffer is created in InitDeviceObjects():

// create and fill the vertex buffer // Initialize vertices for rendering a quad VERTEX Vertices[] = { // x y z { -1.0f,-1.0f, 0.0f, }, { 1.0f,-1.0f, 0.0f, }, { 1.0f, 1.0f, 0.0f, }, { -1.0f, 1.0f, 0.0f, }, }; m_dwSizeofVertices = sizeof (Vertices); // Create the vertex buffers with four vertices if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4 * sizeof(VERTEX), D3DUSAGE_WRITEONLY , sizeof(VERTEX), D3DPOOL_MANAGED, &m_pVB ) ) ) return E_FAIL; // lock and unlock the vertex buffer to fill it with memcpy VOID* pVertices; if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, Vertices, m_dwSizeofVertices); m_pVB->Unlock(); // create and fill the index buffer // indices WORD wIndices[]={0, 1, 2, 0, 2, 3}; m_wSizeofIndices = sizeof (wIndices); // create index buffer if(FAILED (m_pd3dDevice->CreateIndexBuffer(m_wSizeofIndices, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pIB))) return E_FAIL; // fill index buffer VOID *pIndices; if (FAILED(m_pIB->Lock(0, m_wSizeofIndices, (BYTE **)&pIndices, 0))) return E_FAIL; memcpy(pIndices, wIndices, m_wSizeofIndices); m_pIB->Unlock();
The four vertices of the quad are stored in a VERTEX structure, which holds for each vertex three FLOAT values for the position.

By using the flag D3DFMT_INDEX16 in CreateIndexBuffer(), 16-bit variables are used to store the indices into the wIndices structure. So the maximum number of available indices are 64 k. Both buffers use a managed memory pool with D3DPOOL_MANAGED, so they will be cached in the system memory.
[bquote] D3DPOOL_MANAGED resources are read from the system memory which is quite fast and they are written to the system memory and afterwards uploaded to wherever the non-system copy has to go (AGP or VIDEO memory). This upload happens when the resource is unlocked. So there are always two copies of a resource, one in the system and one in the AGP or VIDEO memory. This is a less efficient but bullet-proof way. It works for any class of driver and must be used with unified memory architecture boards. Handling resources with D3DPOOL_DEFAULT is more efficient. In this case the driver will choose the best place for the resource.

Why do we use a vertex buffer at all ? The vertex buffer can be stored in the memory of your graphic card or AGP, where it can be accessed very quickly by 3-D hardware. So a copy between system memory and the graphic card/AGP memory could be avoided. This is important for hardware that accelerates transformation and lighting. Without vertex buffers a lot of bus-traffic would happen by transforming and lighting the vertices.

Why do we use an index buffer ? You will get the maximum performance when you reduce the duplication in vertices transformed and sent across the bus to the rendering device. A nonindexed triangle list for example achieves no vertex sharing, so it's the least optimal method, because DrawPrimitive*() is called several times. Using indexed lists or strips reduce the call overhead of DrawPrimitive*() methods (Reducing DrawPrimitve*() methods is also called batching) and because of the reduction of vertices to send through the bus, it saves memory bandwidth. Indexed strips are more hardware-cache friendly on newer hardware than indexed lists. The performance of index processing operations depends heavily on where the index buffer exists in memory. At the time of this writing, the only graphic cards that supports index buffers in hardware are the RADEON 8x00 series.
[/bquote]
[size="3"]Summarize

RacorX shows a simple vertex shader together with its infrastructure. The shader is inlined in racorx.cpp and compiled with D3DXAssembleShader(). It uses four dp4 instructions for the transformation of the quad and only one material color.

The upcoming examples are build on this example and only the functional additions will be shown on the next pages.

[size="5"]RacorX2

The main difference between RacorX and RacorX2 (see attached resource file) is the compilation of the vertex shader with NVASM. Whereas the first example compiles the vertex shader with D3DXAssembleShader() while the application starts up, RacorX2 uses a pre-compiled vertex shader.

To add the NVIDIA vertex and pixel shader assembler, you have to do the following steps:
• Create a directory, for example [lessthan]C:\NVASM>: and unzip nvasm.exe and the documentation into it
• Show your Visual C++ IDE the path to this exe with
[lessthan]Tools->Options->Directories>
and choose from the drop down menu [lessthan]Show directories for:>
[lessthan]Executable files>
[lessthan]C:\NVASM>
Now the dialog box should look like this:

Figure 7 - Integrating NVASM into Visual C/C++

• Additionally you have to tell every vertex shader file in the IDE, that it has to be compiled with NVASM. The easiest way to do that is looking into the example RacorX2. Just fire up your Visual C++ IDE by clicking on the [lessthan]RacorX.dsp> in the RacorX2 directory. Click on the [lessthan]FileView> tab of the Workspace dialog and there on [lessthan]Shaders> to view the available shader files. A right-click on the file [lessthan]basic.vsh> should show you a popup. Click on [lessthan]Settings...>. The project settings of your project might look like:

Figure 8 - Custom Build Options Vertex Shader Files

• The entry in the entry field called "Commands" is:

nvasm.exe $(InputPath) shaders\$(InputName).vso

For the entry field named "Outputs", I use the input name as the name for the output file with an *.vso extension. The output directory should be the shaders directory. ShaderX author Kenneth Hurley is the author of NVASM. Read more in his paper [Hurley]. The output of NVASM in the build window of your Visual C/C++ IDE should look like this:

Figure 9 - NVASM Output

The vertex shader is provided in its own ASCII file called basic.vsh. After compilation a binary object file with the name basic.vso is created in the directory [lessthan]shaders>:

Figure 10 - Directory Content RacorX2

Because of the already compiled vertex shader, the creation of the vertex shader has to be done in a different way than in the previous example. This happens in the InitDeviceObjects() function:

//shader decl DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register 0 D3DVSD_END() }; // loads a *.vso binary file, already compiled with NVASM and // creates a vertex shader if (FAILED(CreateVSFromCompiledFile (m_pd3dDevice, dwDecl,"shaders/basic.vso", &m_dwVertexShader))) return E_FAIL;
CreateVSFromCompiledFile() opens and reads in the binary vertex shader file and creates a vertex shader. The source of this function can be found at the end of the file racorx.cpp in the RacorX2 directory:

//---------------------------------------------------------------------------- // Name: CreateVSFromBinFile // Desc: loads a binary *.vso file that was compiled by NVASM // and creates a vertex shader //---------------------------------------------------------------------------- HRESULT CMyD3DApplication::CreateVSFromCompiledFile (IDirect3DDevice8* m_pd3dDevice, DWORD* dwDeclaration, TCHAR* strVSPath, DWORD* m_dwVS) { char szBuffer[128]; // debug output DWORD* dwpVS; // pointer to address space of the calling process HANDLE hFile, hMap; // handle file and handle mapped file TCHAR tempVSPath[512]; // temporary file path HRESULT hr; // error if( FAILED( hr = DXUtil_FindMediaFile( tempVSPath, strVSPath ) ) ) return D3DAPPERR_MEDIANOTFOUND; hFile = CreateFile(tempVSPath, GENERIC_READ,0,0,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0); if(hFile != INVALID_HANDLE_VALUE) { if(GetFileSize(hFile,0) > 0) hMap = CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0); else { CloseHandle(hFile); return E_FAIL; } } else return E_FAIL; // maps a view of a file into the address space of the calling process dwpVS = (DWORD *)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); // Create the vertex shader hr = m_pd3dDevice->CreateVertexShader( dwDeclaration, dwpVS, m_dwVS, 0 ); if ( FAILED(hr) ) { OutputDebugString( "Failed to create Vertex Shader, errors:\n" ); D3DXGetErrorStringA(hr,szBuffer,sizeof(szBuffer)); OutputDebugString( szBuffer ); OutputDebugString( "\n" ); return hr; } UnmapViewOfFile(dwpVS); CloseHandle(hMap); CloseHandle(hFile); return S_OK; }
DXUtil_FindMediaFile(), a helper function located in the framework file dxutil.cpp, returns the path to the already compiled vertex shader file. CreateFile() opens and reads the existing file:

HANDLE CreateFile( LPCTSTR lpFileName, // file name DWORD dwDesiredAccess, // access mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD DWORD dwCreationDisposition, // how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to template file );
Its first parameter is the path to the file. The flag GENERIC_READ specifies read access to the file in the second parameter. The following two parameters are not used, because file sharing should not happen and the fourth parameter is not used, because the file should not be inherited by a child process. The fifth parameter is set to OPEN_EXISTING. This way, the function fails if the file does not exist. Setting the sixt parameter to FILE_ATTRIBUTE_NORMAL indicates, that the file has no other attributes. A template file is not used here, so the last parameter is set to 0. Please consult the Platform SDK help file for more information.

CreateFileMapping() creates or opens a named or unnamed file-mapping object for the specified file:

HANDLE CreateFileMapping( HANDLE hFile, // handle to file LPSECURITY_ATTRIBUTES lpAttributes, // security DWORD flProtect, // protection DWORD dwMaximumSizeHigh, // high-order DWORD of size DWORD dwMaximumSizeLow, // low-order DWORD of size LPCTSTR lpName // object name );
The first parameter is a handle to the file from which to create a mapping object. The file must be opened with an access mode compatible with the protection flags specified by the flProtect parameter. We have opened the file in CreateFile() with GENERIC_READ, therefore we use PAGE_READONLY here. Other features of CreateFileMapping() are not needed, therefore we set the rest of the parameters to 0.

MapViewOfFile() function maps a view of a file into the address space of the calling process:

LPVOID MapViewOfFile( HANDLE hFileMappingObject, // handle to file-mapping object DWORD dwDesiredAccess, // access mode DWORD dwFileOffsetHigh, // high-order DWORD of offset DWORD dwFileOffsetLow, // low-order DWORD of offset SIZE_T dwNumberOfBytesToMap // number of bytes to map );
This function only gets the handle to the file-mapping object from CreateFileMapping() and in the second parameter the access mode FILE_MAP_READ. The access mode parameter specifies the type of access to the file view and, therefore, the protection of the pages mapped by the file. More features are not needed, therefore the rest of the parameters are set to 0.

CreateVertexShader() is used to create and validate a vertex shader. It takes the vertex shader declaration (which maps vertex buffer streams to different vertex input registers) in its first parameter as a pointer and returns the shader handle in the third parameter. The second parameter gets the vertex shader instructions of the binary code pre-compiled by a vertex shader assembler. With the fourth parameter you can force software vertex processing with D3DUSAGE_SOFTWAREPROCESSING.

As in the previous example OutputDebugString() shows the complete error message in the output debug window of the Visual C/C++ IDE and D3DXGetErrorStringA() interprets all Direct3D and Direct3DX HRESULTS and returns an error message in szBuffer.

[size="3"]Summarize

This example showed the integration of NVASM to pre-compile a vertex shader and how to open and read a binary vertex shader file.

[size="5"]RacorX3

The main improvement of RacorX3 (see attached resource file) over RacorX2 is the addition of a per-vertex diffuse reflection model in the vertex shader. This is one of the simplest lighting calculations, which outputs the color based on the dot product of the vertex normal with the light vector.

RacorX3 uses a light positioned at (0.0, 0.0, 1.0) and a green color.

Figure 11 - RacorX3

As usual we are tracking the life-cycle of the vertex shader.

The vertex shader declaration has to map vertex data to specific vertex shader input registers. Additionally to the previous examples, we need to map a normal vector to the input register v3:

// vertex shader declaration DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register #1 D3DVSD_REG(3, D3DVSDT_FLOAT3 ), // normal in input register #4 D3DVSD_END() };
The corresponding layout of the vertex buffer looks like this:

struct VERTICES { FLOAT x, y, z; // The untransformed position for the vertex FLOAT nx, ny, nz; // the normal }; // Declare custom FVF macro. #define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
Each vertex consists of three position floating point values and three normal floating point values in the vertex buffer. The vertex shader gets the position and normal values from the vertex buffer via v0 and v3.

[size="3"]Setting the Vertex Shader Constant Registers

The vertex shader constants are set in FrameMove() and RestoreDeviceObjects(). This example uses a more elegant way to handle the constant registers. The file const.h that is included in racorx.cpp and diffuse.vsh, gives the constant registers an easier to remember name:

#define CLIP_MATRIX 0 #define CLIP_MATRIX_1 1 #define CLIP_MATRIX_2 2 #define CLIP_MATRIX_3 3 #define INVERSE_WORLD_MATRIX 4 #define INVERSE_WORLD_MATRIX_1 5 #define INVERSE_WORLD_MATRIX_2 6 #define LIGHT_POSITION 11 #define DIFFUSE_COLOR 14 #define LIGHT_COLOR 15
In FrameMove() a clipping matrix and an inversed world matrix are set into the constant registers:

HRESULT CMyD3DApplication::FrameMove() { // rotates the object about the y-axis D3DXMatrixRotationY( &m_matWorld, m_fTime * 1.5f ); // set the clip matrix m_pd3dDevice->SetVertexShaderConstant(CLIP_MATRIX, &(m_matWorld * m_matView * m_matProj), 4); D3DXMATRIX matWorldInverse; D3DXMatrixInverse(&matWorldInverse, NULL, &m_matWorld); m_pd3dDevice->SetVertexShaderConstant(INVERSE_WORLD_MATRIX, &matWorldInverse,3); return S_OK; } 
Contrary to the previous examples, the concatenated world-, view- and projection matrix, which is used to rotate the quad, is not transposed here. This is because the matrix will be transposed in the vertex shader as shown below.

To transform the normal, an inverse 4x3 matrix is send to the vertex shader via c4 -c6.

The vertex shader is a little bit more complex, than the one used in the previous examples:

; per-vertex diffuse lighting #include "const.h" vs.1.1 ; transpose and transform to clip space mul r0, v0.x, c[CLIP_MATRIX] mad r0, v0.y, c[CLIP_MATRIX_1], r0 mad r0, v0.z, c[CLIP_MATRIX_2], r0 add oPos, c[CLIP_MATRIX_3], r0 ; transform normal dp3 r1.x, v3, c[INVERSE_WORLD_MATRIX] dp3 r1.y, v3, c[INVERSE_WORLD_MATRIX_1] dp3 r1.z, v3, c[INVERSE_WORLD_MATRIX_2] ; renormalize it dp3 r1.w, r1, r1 rsq r1.w, r1.w mul r1, r1, r1.w ; N dot L ; we need L vector towards the light, thus negate sign dp3 r0, r1, -c[LIGHT_POSITION] mul r0, r0, c[LIGHT_COLOR] ; modulate against light color mul oD0, r0, c[DIFFUSE_COLOR] ; modulate against material
The mul, mad and add instructions transpose and transform the matrix provided in c0 - c3 to clip space. As such they are nearly functionally equivalent to the transposition of the matrix and the four dp4 instructions shown in the previous examples. There are two caveats to bear in mind: The complex matrix instructions like m4x4 might be faster in software emulation mode and v0.w is not used here. oPos.w is automatically filled with 1. These instructions should save the CPU cycles used for transposing.

The normals are transformed in the following three dp3 instructions and then renormalized with the dp3, rsq and mul instructions.

You can think of a normal transform in the following way: Normal vectors (unlike position vectors) are simply directions in space, and as such they should not get squished in magnitude, and translation doesn't change their direction. They should simply be rotated in some fashion to reflect the change in orientation of the surface. This change in orientation is a result of rotating and squishing the object, but not moving it. The information for rotating a normal can be extracted from the 4x4 transformation matrix by doing transpose and inversion. A more math-related explanation is given in [Haines/M?ller][Turkowski].

So the bullet-proof way to use normals, is to transform the transpose of the inverse of the matrix, that is used to transform the object. If the matrix used to transform the object is called M, then we must use the matrix, N, below to transform the normals of this object.
[bquote][font="Courier New"][color="#000080"]N = transpose( inverse(M) )[/color][/font]

The normal can be transformed with the transformation matrix (usually the world matrix), that is used to transform the object in the following cases:

• Matrix formed from rotations (orthogonal matrix), because the inverse of an orthogonal matrix is its transpose
• Matrix formed from rotations and translation (rigid-body transforms), because translations do not affect vector direction
• Matrix formed from rotations and translation and uniform scalings, because such scalings affect only the length of the transformed normal, not its direction. A uniform scaling is simply a matrix which uniformly increases or decreases the object's size, vs. a non-uniform scaling, which can stretch or squeeze an object. If uniform scalings are used, then the normals do have to be renormalized. Therefore using the world matrix would be sufficient in this example.[/bquote]
That's exactly, what the source is doing. The inverse world matrix is delivered to the vertex shader via c4 - c6. The dp3 instruction handles the matrix in a similar way as dp4.

By multiplying a matrix with a vector, each column of the matrix should be multiplied with each component of the vector. dp3 and dp4 are only capable to multiply each row of the matrix with each component of the vector. In case of the position data, the matrix is transposed to get the right results.

In case of the normals, no transposition is done. So dp3 calculates the dot product by multiplying the rows of the matrix with the components of the vector. This is like using a transposed matrix.

The normal is re-normalized with the dp3, rsq and mul instructions. Re-normalizing a vector means align its length to 1. That's because we need a unit vector to calculate our diffuse lighting effect.

To calculate a unit vector, divide the vector by its magnitude or length. The magnitude of vectors is calculated by using the Pythagorean theorem:

x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup] = m[sup]2[/sup]

The length of the vector is retrieved by

||A|| = sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])

The magnitude of a vector has a special symbol in mathematics. It is a capital letter designated with two vertical bars: ||A||. So dividing the vector by its magnitude is:

UnitVector = Vector / sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])

The lines of code in the vertex shader, that handles the calculation of the unit vector looks like this:

; renormalize it dp3 r1.w, r1, r1 ; (src1.x * src2.x) + (src1.y * src2.y) + (src1.z * src2.z) rsq r1.w, r1.w ; if (v != 0 && v != 1.0) v = (float)(1.0f / sqrt(v)) mul r1, r1, r1.w ; r1 * r1.w
dp3 squares the x, y and z components of the temporary register r1, adds them and returns the result in r1.w. rsq divides 1 by the result in r1.w and stores the result in r1.w. mul multiplies all components of r1 with r1.w. Afterwards, the result in r1.w is not used anymore in the vertex shader.

The underlying calculation of these three instructions can be represented by the following formula, which is mostly identical to the formula postualted above:

UnitVector = Vector * 1/sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])

Lighting is calculated with the following three instruction:

dp3 r0, r1, -c[LIGHT_POSITION] mul r0, r0, c[LIGHT_COLOR] ; modulate against light color mul oD0, r0, c[DIFFUSE_COLOR] ; modulate against diffuse color
Nowadays the lighting models used in current games are not based on much physical theory. Game programmers use approximations that try to simulate the way photons are reflected from objects in a rough but efficient manner.

One differentiates usually between different kind of light sources and different reflection models. The common lighting sources are called directional, point light and spotlight. The most common reflections models are ambient, diffuse and specular lighting.

This example uses a directional light source with an ambient and a diffuse reflection model.

Directional Light

RacorX3 uses a light source in an infinite distance. This simulates the long distance the light beams have to travel from the sun. We treat this light beams as beeing parallel. This kind of light source is called directional light source.

Diffuse Reflection

Whereas ambient light is considered to be uniform from any direction, diffuse light simulates the emission of an object by a particular light source. Therefore you are able to see that light falls onto the surface of an object from a particular direction by using the diffuse lighting model.

It is based on the assumption that light is reflected equally well in all directions, so the appearance of the reflection does not depend on the position of the observer. The intensity of the light reflected in any direction depends only on how much light falls onto the surface.

If the surface of the object is facing the light source, which means is perpendicular to the direction of the light, the density of the incident light is the highest. If the surface is facing the light source under some angle smaller than 90 degrees, the density is proportionally smaller.

The diffuse reflection model is based on a law of physics called Lambert's Law, which states that for ideally diffuse (totally matte) surfaces, the reflected light is determined by the cosine between the surface normal N and the light vector L.

Figure 12 - Diffuse Lighting

The left figure shows a geometric interpretation of Lambert's Law (see also [RTR]). The middle figure shows the light rays hitting the surface perpendicularly in a distance d apart. The intensity of the light is related to this distance. It decreases as d becomes greater. This is shown by the right figure. The light rays make an angle ? with the normal of the plane. This illustrates that the same amount of light that passes through one side of a right-angle triangle is reflected from the region of the surface corresponding to the triangles hypotenuse. Due to the relationships that hold in a right-angle triangle, the length of the hypotenuse is d/cos ? of the length of the considered side. Thus you can deduce that if the intensity of the incident light is Idirected, the amount of light reflected from a unit surface is Idirected cos ?. Adjusting this with a coefficient that describes reflection properties of the matter leads to the following equation (see also [Savchenko]):

Ireflected = Cdiffuse * Idirected cos ?

This equation demonstrates that the reflection is at its peak for surfaces that are perpendicular to the direction of light and diminishes for smaller angles, because the cosinus value is very large. The light is obscured by the surface if the angles is more than 180 or less than 0 degrees, because the cosinus value is small. You will obtain negative intensity of the reflected light, which will be clamped by the output registers.

In an implementation of this model, you have to find a way to compute cos ?. By definition the dot or scalar product of the light and normal vector can be expressed as

N dot L = ||N|| ||L||cos ?

where ||N|| and ||L|| are the lengths of the vectors. If both vectors are unit length, you can compute cos ? as the scalar or dot product of the light and normal vector. Thus the expression is

Ireflected = Cdiffuse * Idirected(N dot L)

So (N dot L) is the same as the cosine of the angle between N and L, therefore as the angle decrease, the resulting diffuse value is higher. This is exactly what the dp3 instruction and the first mul instruction are doing. Here is the source with the relevant part of constant.h:

#define LIGHT_POSITION 11 #define MATERIAL_COLOR 14 ----- dp3 r0, r1, -c[LIGHT_POSITION] mul r0, r0, c[LIGHT_COLOR] ; modulate against light color mul oD0, r0, c[DIFFUSE_COLOR] ; modulate against material
So the vertex shader registers are involved in the following way:

r0 = (r1 dot -c11) * c14
This example modulates additionally against the blue light color in c15:

r0 = (c15 * (r1 dot -c11)) * c14
[size="3"]Summarize

RacorX3 shows the usage of an include file to give constants a name that can be remembered in a better way. It shows how to normalize vectors and it just strive the problem of transforming normals, but shows a bullet-proof method to do it.

The example introduces an optimization technique, that eliminates the need to transpose the clip space matrix with the help of the CPU and it shows the usage of a simple diffuse reflection model, that lights the quad on a per-vertex basis.

[size="5"]RacorX4

RacorX4 (see attached resource file) has gotten a few additional features compared to RacorX3. First of all this example will not use a plain quad anymore, it uses instead a B?zier patch class, that shows a round surface with a texture attached to it. To simulate light reflections, a combined diffuse and specular reflection model is used.

Figure 13 - RacorX4

RacorX4 uses the trackball class provided with the Common files framework, to rotate and move the object. You can choose different specular colors with the [lessthan]C> key and zoom in and out with the mouse wheel.

Compared to RacorX3, additionally the texture coordinates will be mapped to input register v7.

// shader decl DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register v0 D3DVSD_REG(3, D3DVSDT_FLOAT3 ), // normal in input register v3 D3DVSD_REG(7, D3DVSDT_FLOAT2), // tex coordinates D3DVSD_END() };
The corresponding layout of the vertex buffer in BPatch.h looks like:

struct VERTICES { D3DXVECTOR3 vPosition; D3DXVECTOR3 vNormal; D3DXVECTOR2 uv; }; // Declare custom FVF macro. #define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
The third flag used in the custom FVF macro indicates the usage of one texture coordinate pair. This macro is provided to CreateVertexBuffer(). The vertex shader gets the position values from the vertex buffer via v0, the normal values via v3 and the two texture coordinates via v7.

The vertex shader constants are set in FrameMove() and RestoreDeviceObjects(). The file const.h holds the following defines:

#define CLIP_MATRIX 0 #define CLIP_MATRIX_1 1 #define CLIP_MATRIX_2 2 #define CLIP_MATRIX_3 3 #define INVERSE_WORLD_MATRIX 8 #define INVERSE_WORLD_MATRIX_1 9 #define INVERSE_WORLD_MATRIX_2 10 #define LIGHT_VECTOR 11 #define EYE_VECTOR 12 #define SPEC_POWER 13 #define SPEC_COLOR 14
In FrameMove() a clipping matrix, the inversed world matrix, an eye vector and a specular color are set:

// set the clip matrix m_pd3dDevice->SetVertexShaderConstant(CLIP_MATRIX, &(m_matWorld * m_matView * m_matProj),4); // set the world inverse matrix D3DXMATRIX matWorldInverse; D3DXMatrixInverse(&matWorldInverse, NULL, &m_matWorld); m_pd3dDevice->SetVertexShaderConstant(INVERSE_WORLD_MATRIX, matWorldInverse,3); // stuff for specular lighting // set eye vector E m_pd3dDevice->SetVertexShaderConstant(EYE_VECTOR, vEyePt,1); // specular color if(m_bKey['C']) { m_bKey['C']=0; ++m_dwCurrentColor; if(m_dwCurrentColor >= 3) m_dwCurrentColor=0; } m_pd3dDevice->SetVertexShaderConstant(SPEC_COLOR, m_vLightColor[m_dwCurrentColor], 1);
Like in the previous example the concatenated world-, view- and projection matrix are set into c0 - c3, to get transposed in the vertex shader and the inverse 4x3 world matrix is send to vertex shader, to transform the normal.

The eye vector (EYE_VECTOR) that is used to build up the view matrix is stored in constant register c12. As shown below, this vector is helpful to build up the specular reflection model used in this upcoming examples.

The user can pick one of the following specular colors with the [lessthan]C> key:

m_vLightColor[0]=D3DXVECTOR4(0.3f,0.1f,0.1f,1.0f); m_vLightColor[1]=D3DXVECTOR4(0.1f,0.5f,0.1f,1.0f); m_vLightColor[2]=D3DXVECTOR4(0.0f,0.1f,0.4f,1.0f);
In RestoreDeviceObjects() the specular power, the light vector and the diffuse color are set:

// specular power m_pd3dDevice->SetVertexShaderConstant(SPEC_POWER, D3DXVECTOR4(0,10,25,50),1); // light direction D3DXVECTOR3 vLight(0,0,1); m_pd3dDevice->SetVertexShaderConstant(LIGHT_VECTOR, vLight,1); D3DXCOLOR matDiffuse(0.9f, 0.9f, 0.9f, 1.0f); m_pd3dDevice->SetVertexShaderConstant(DIFFUSE_COLOR, &matDiffuse, 1);
As in the previous examples, the light is positioned at (0.0, 0.0, 1.0). There are four specular power values, from which one will be used in the vertex shader.
[bquote]To optimize the usage of SetVertexShaderConstant(), the specular color could be set into the fourth component of the light vector, which only used three of its components.[/bquote]

The vertex shader handles a combined diffuse and specular reflection model:

; diffuse and specular vertex lighting #include "const.h" vs.1.1 ; transpose and transform to clip space mul r0, v0.x, c[CLIP_MATRIX] mad r0, v0.y, c[CLIP_MATRIX_1], r0 mad r0, v0.z, c[CLIP_MATRIX_2], r0 add oPos, c[CLIP_MATRIX_3], r0 ; output texture coords mov oT0, v7 ; transform normal dp3 r1.x, v3, c[INVERSE_WORLD_MATRIX] dp3 r1.y, v3, c[INVERSE_WORLD_MATRIX_1] dp3 r1.z, v3, c[INVERSE_WORLD_MATRIX_2] ; renormalize it dp3 r1.w, r1, r1 rsq r1.w, r1.w mul r1, r1, r1.w ; light vector L ; we need L towards the light, thus negate sign mov r5, -c[LIGHT_VECTOR] ; N dot L dp3 r0.x, r1, r5 ; compute normalized half vector H = L + V add r2, c[EYE_VECTOR], r5 ; L + V ; renormalize H dp3 r2.w, r2, r2 rsq r2.w, r2.w mul r2, r2, r2.w ; N dot H dp3 r0.y, r1, r2 ; compute specular and clamp values (lit) ; r0.x - N dot L ; r0.y - N dot H ; r0.w - specular power n mov r0.w, c[SPEC_POWER].y ; n must be between -128.0 and 128.0 lit r4, r0 ; diffuse color * diffuse intensity(r4.y) mul oD0, c[DIFFUSE_COLOR], r4.y ; specular color * specular intensity(r4.z) mul oD1, c[SPEC_COLOR], r4.z
Compared to the vertex shader in the previous example program, this shader maps the texture coordinates to texture stage 0 with the following instruction:

; output texture coords mov oT0, v7
The corresponding texture stage states set for the shading of the pixels in the multitexturing unit will be shown below in the section named "Non-Shader specific code". The instructions that transform the normal and calculate the diffuse reflection were already discussed along with the previous example.

The real new functionality in this shader is the calculation of the specular reflection, which happens in the code lines starting with the add instruction.

Specular Reflection

Compared to the diffuse reflection model, the appearance of the reflection depends in the specular reflection model on the position of the viewer. When the direction of the viewing coincides, or nearly coincides, with the direction of specular reflection, a bright highlight is observed. This simulates the reflection of a light source by a smooth, shiny and polished surface.

To describe reflection from shiny surfaces an approximation is commonly used, which is called the Phong illumination model (not to be confused with Pong Shading), named after its creator Phong Bui Tong [Foley]. According to this model, a specular highlight is seen when the viewer is close to the direction of reflection. The intensity of light falls off sharply when the viewer moves away from the direction of the specular reflection.

Figure 14 - Vectors for Specular Reflection

A model describing this effect has to be aware of at least the location of the light source L, the location of the viewer V, and the orientation of the surface N. Additionally a vector R that describes the direction of the reflection might be useful. The half way vector H, that halfs the angle between L and V will be introduced below.

The original Phong formula approximates the falloff of the intensity. It looks like this:

kspecular cos[sup]n[/sup](ss)

where kspecular is a scalar coefficient showing the percentage of the incident light reflected. ss describes the angle between R and V. The exponent n characterizes the shiny properties of the surface and ranges from one to infinity. Objects that are matte require a small exponent, since they produce a large, dim, specular highlight with a gentle falloff. Shiny surfaces should have a sharp highlight that is modeled by a very large exponent, making the intensity falloff very steep.

Together with the diffuse reflection model shown above, the Phong illumination model can be expressed in the following way:

Ireflected = Idirected((N dot L) + kspecular cos[sup]n[/sup](ss))

cos[sup]n[/sup](ss) can be replaced by using the mechanism of the dot or scalar product of the unit vectors R and V:

Ireflected = Idirected((N dot L) + kspecular (R dot V)[sup]n[/sup])

This is the generally accepted phong reflection equation. As the angle between V and R decreases, the specularity will rise.

Because it is expensive to calculate the reflection vector R (mirror of light incidence around the surface normal), James F. Blinn [Blinn] introduced a way to do this using an imaginary vector H, which is defined as halfway between L and V. H is therefore:

H = (L + V) / 2

When H coincides with N, the direction of the reflection coincides with the viewing direction V and a specular hightlight is observed. So the original formula

(R dot V)[sup]n[/sup]

is expanded to

(N dot ((L + V) / 2))[sup]n[/sup]

or

(N dot H)[sup]n[/sup]

The complete Blinn-Phong model formula looks like:

Ireflected = Idirected((N dot L) + kspecular (N dot H)[sup]n[/sup])

Now back to the relevant code piece that calculates the specular reflection:

; compute normalized half vector H = L + V add r2, c[EYE_VECTOR], r5 ; L + V ; renormalize H dp3 r2.w, r2, r2 rsq r2.w, r2.w mul r2, r2, r2.w ; N
 
 0   Report Article 
 Sign in to follow this   Followers 0 Go to articles Graphics and GPU Programming 
 User Feedback 0 Comments 0 Reviews There are no comments to display. Create an account or sign in to comment You need to be a member in order to leave a comment Create an account Sign up for a new account in our community. It's easy! Register a new account Sign in Already have an account? Sign in here. Sign In Now 
•  
 
 Advertisement 
 Advertisement Categories 29Audio 29Music and Sound FX 200Business 125Business and Law 36Career Development 39Production and Management 192Game Design 185Game Design and Theory 3Writing for Games 3UX for Games 233Industry 172Interviews 61Event Coverage 1165Programming 55Artificial Intelligence 616General and Gameplay Programming 322Graphics and GPU Programming 56Engines and Middleware (and 2 more) 65Visual Arts 34Archive Show all categories   Advertisement GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Category This Article More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Articles Programming Graphics and GPU Programming Introduction to Shader Programming Part II: Programming Vertex Shaders 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Chat GDNet+ Membership Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search var ipsDebug=false;var CKEDITOR_BASEPATH='//www.gamedev.net/applications/core/interface/ckeditor/ckeditor/';var ipsSettings={cookie_path:"/",cookie_prefix:"ips4_",cookie_ssl:true,upload_imgURL:"",message_imgURL:"",notification_imgURL:"",baseURL:"//www.gamedev.net/",jsURL:"//www.gamedev.net/applications/core/interface/js/js.php",csrfKey:"6aeb278a596e3f2cee4743d51eedd6d6",antiCache:"2a361a8a02",disableNotificationSounds:false,useCompiledFiles:true,links_external:true,memberID:0,analyticsProvider:"ga",viewProfiles:true,mapProvider:'google',mapApiKey:"AIzaSyAeT7tk3vnWWmbgVISkLpbhkQvekG19rHM",}; ips.setSetting('date_format',jQuery.parseJSON('"mm\/dd\/yy"'));ips.setSetting('date_first_day',jQuery.parseJSON('0'));ips.setSetting('remote_image_proxy',jQuery.parseJSON('1'));ips.setSetting('ipb_url_filter_option',jQuery.parseJSON('"none"'));ips.setSetting('url_filter_any_action',jQuery.parseJSON('"allow"'));ips.setSetting('bypass_profanity',jQuery.parseJSON('0'));ips.setSetting('emoji_style',jQuery.parseJSON('"native"'));ips.setSetting('emoji_shortcodes',jQuery.parseJSON('"1"'));ips.setSetting('emoji_ascii',jQuery.parseJSON('"1"'));ips.setSetting('emoji_cache',jQuery.parseJSON('"1"'));ips.setSetting('quickSearchDefault',jQuery.parseJSON('"all"'));ips.setSetting('quickSearchMinimum',jQuery.parseJSON('3'));ips.setSetting('quickSearchShowAdv',jQuery.parseJSON('true'));ips.setSetting('quickSearchIn',jQuery.parseJSON('"title"')); { "@context": "http://schema.org", "@type": "Article", "url": "https://www.gamedev.net/articles/programming/graphics/introduction-to-shader-programming-part-ii-pro-r1807/", "discussionUrl": "https://www.gamedev.net/articles/programming/graphics/introduction-to-shader-programming-part-ii-pro-r1807/", "mainEntityOfPage": "https://www.gamedev.net/articles/programming/graphics/introduction-to-shader-programming-part-ii-pro-r1807/", "name": "Introduction to Shader Programming Part II: Programming Vertex Shaders", "headline": "Introduction to Shader Programming Part II: Programming Vertex Shaders", "text": "(Editor\u0027s Note - this article requires additional formatting work and is incomplete)\n(Last modification: March 31st, 2002)\n [size=\"5\"]Preface\nDuring this part of the introduction a very simple program that shows a rotating quad will evolve into a more sophisticated application showing a B?zier patch class with a diffuse and specular reflection model, featuring a point light source. The example applications are all build on each other in a way that most of the code of the previous example is re-used in the following example. This way the explanation of the features stayed focused on the advancements of the specific example.\n[size=\"5\"]RacorX\nFigure 1 - RacorX\n\n RacorX (see attached resource file) displays a green color, that is applied to the quad evenly. This example demonstrate the usage of the common file framework, provided with the DirectX 8.1 SDK and how to compile vertex shaders with the D3DXAssembleShader() function.\n Like with all the upcoming examples, which are based on the Common Files, [lessthan]Alt\u0026gt;+[lessthan]Enter\u0026gt; switches between the windowed and full-screen mode, [lessthan]F2\u0026gt; gives you a selection of the usable drivers and [lessthan]Esc\u0026gt; will shutdown the application.\n First let\u0027s take a look at the files you need to compile the program: \nFigure 2- Directory Content\n\n The source file is RacorX.cpp, the resource files are winmain.rc and resource.h. The icon file is directx.ico and the executable is RacorX.exe. The remaining files are for the use of the Visual C/C++ 6 IDE.\n To compile this example, you should link it with the following *.lib files:\nd3d8.lib \nd3dx8dt.lib \ndxguid.lib \nd3dxof.lib \nwinmm.lib \ngdi32.lib \nuser32.lib \nkernel32.lib \nadvapi32.lib Most of these *.lib files are COM wrappers. The d3dx8dt.lib is the debug version of the Direct3DX static link library.[bquote]The release Direct3DX static link library is called d3dx8.lib. There is also a *.dll version of the debug build called d3dx8d.dll in the system32 directory. It is used by linking to the d3dx8d.lib COM wrapper.[/bquote] All of these *.lib files have to be included in the Object/libary modules:\u0026gt; entry field. This is located at [lessthan]Project-\u0026gt;Settings\u0026gt; and there under the [lessthan]Link\u0026gt; tab: \nFigure 3 - Project Settings\n\n The provided Visual C/C++ 6 IDE workspace references the common files in a folder with the same name: \nFigure 4 - Workspace\n\n They were added to the project with Project-\u0026gt;Add to the Project-\u0026gt;Files: \nFigure 5 - Add Files to Project\n\n[size=\"3\"]The Common Files Framework\nThe common files framework helps getting up to speed, because:\nIt helps to avoid how-tos for Direct3D in general, so that the focus of this text is the real stuff. \nIt\u0027s common and tested foundation, which helps reduce the debug time. \nAll of the Direct3D samples in the DirectX SDK use it. Learning time is very short. \nIts window mode makes debugging easier. \nSelf-developed production code could be based on the common files, so knowing them is always a win. A high-level view of the Common Files shows 14 *.cpp files in\n[font=\"Courier New\"][color=\"#000080\"] C:\\DXSDK\\samples\\Multimedia\\Common\\src[/color][/font]\n These files encapsulate the basic functionality you need to start programming a Direct3D application. The most important d3dapp.cpp contains the class CD3DApplication. It provides seven functions that can be overridden and that are used in the main *.cpp file of any project in this introduction:\nvirtual HRESULT OneTimeSceneInit() { return S_OK; } \nvirtual HRESULT InitDeviceObjects() { return S_OK; } \nvirtual HRESULT RestoreDeviceObjects() { return S_OK; } \nvirtual HRESULT DeleteDeviceObjects() { return S_OK; } \nvirtual HRESULT Render() { return S_OK; } \nvirtual HRESULT FrameMove( FLOAT ) { return S_OK; } \nvirtual HRESULT FinalCleanup() { return S_OK; } All that has to be done, to create an application based on this framework code is to create a new project and new implementations of these overridable functions in the main source file. This is also shown in all Direct3D examples in the DirectX SDK.\n RacorX uses these framework functions in RacorX.cpp. They can be called the public interface of the common files framework. \nFigure 6 - Framework Public Interface\n\n The following functions are called in the following order in racorx.cpp at startup:\nConfirmDevice() \nOneTimeSceneInit() \nInitDeviceObjects() \nRestoreDeviceObjects() Now the application is running. While it is running, the framework calls\nFrameMove() \nRender() in a loop.\n If the user resizes the window, the framework will call\nInvalidateDeviceObjects() \nRestoreDeviceObjects() If the user presses F2 or clicks [lessthan]File\u0026gt;-\u0026gt;[lessthan]Change device\u0026gt; and changes the device by choosing for example another resolution or color quality, the framework will call\nInvalidateDeviceObjects() \nDeleteDeviceObjects() \nInitDeviceObjects() \nRestoreDeviceObjects() If the user quits the application, the framework will call\nInvalidateDeviceObjects() \nDeleteDeviceObjects() \nFinalCleanup() There are matching functional pairs. InvalidateDeviceObjects() destroys what RestoreDeviceObjects() has build up and DeleteDeviceObjects() destroys what InitDeviceObjects() has build up. The FinalCleanup() function destroys what OneTimeSceneInit() build up.\n The idea is to give every functional pair its own tasks. The OneTimeSceneInit() / FinalCleanup() pair is called once at the beginning and the end of a life-cycle of the game. Both are used to load or delete data, which is not device dependant. A good candidate might be geometry data. The target of the InitDeviceObjects() / DeleteDeviceObjects() pair is, like the name implies, data that is device dependant. If the already loaded data has to be changed, when the device changes, it should be loaded here. The following examples will load, re-create or destroy their vertex buffer and index buffers and their textures in these functions.\n The InvalidateDeviceObjects() / RestoreDeviceObjects() pair has to react on changes of the window size. So for example code that handles the projection matrix might be placed here. Additionally the following examples will set most of the render states in RestoreDeviceObjects().\n Now back to RacorX. Like shown in part 1 of this introduction, the following list tracks the life-cycle of a vertex shader:\nCheck for vertex shader support by checking the D3DCAPS8::VertexShaderVersion field \nDeclaration of the vertex shader with the D3DVSD_* macros, to map vertex buffer streams to input registers \nSetting the vertex shader constant registers with SetVertexShaderConstant()\n \nCompiling an already written vertex shader with D3DXAssembleShader*() (Alternatively: could be pre-compiled using a Shader Assembler) \nCreating a vertex shader handle with CreateVertexShader()\n \nSetting a vertex shader with SetVertexShader() for a specific object \nFree vertex shader resources handled by the Direct3D engine with DeleteVertexShader() We will walk step-by-step through this list in the following pages.\n [size=\"3\"]Check for Vertex Shader Support\nThe supported vertex shader version is checked in ConfirmDevice() in racorx.cpp:HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps,\n DWORD dwBehavior,\n D3DFORMAT Format )\n{\n if( (dwBehavior \u0026amp; D3DCREATE_HARDWARE_VERTEXPROCESSING ) ||\n (dwBehavior \u0026amp; D3DCREATE_MIXED_VERTEXPROCESSING ) )\n {\n if( pCaps-\u0026gt;VertexShaderVersion \n \treturn E_FAIL;\n }\n return S_OK;\n} If the framework has already initialized hardware or mixed vertex processing, the vertex shader version will be checked. If the framework initialized software vertex processing, the software-implementation provided by Intel and AMD jumps in and a check of the hardware capabilities is not needed.\n The globally available pCaps capability data structure is filled with a call to GetDeviceCaps() by the framework. pCaps-\u0026gt;VertexShaderVersion holds the vertex shader version in a DWORD. The macro D3DVS_VERSION helps checking the version number. For example the support of at least vs.2.0 in hardware will be checked with D3DVS_VERSION(2,0).\n After checking the hardware capabilities for vertex shader support, the vertex shader has to be declared.\n [size=\"3\"]Vertex Shader Declaration\nDeclaring a vertex shader means mapping vertex data to specific vertex shader input registers, therefore the vertex shader declaration must reflect the vertex buffer layout, because the vertex buffer must transport the vertex data in the correct order. The one used in this example program is very simple. The vertex shader will get the position data via v0.// shader decl\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // D3DVSDE_POSITION,0\n D3DVSD_END()\n};\nThe corresponding layout of the vertex buffer looks like this:\n// vertex type\nstruct VERTEX\n{\n FLOAT x, y, z; // The untransformed position for the vertex\n};\n// Declare custom FVF macro.\n#define D3DFVF_VERTEX (D3DFVF_XYZ) The position values will be stored in the vertex buffer and bound through the SetStreamSource() function to a device data stream port, that feed the primitive processing functions (this is the Higher-Order Surfaces (HOS) stage or directly the vertex shader, depending on the usage of HOS; see the Direct3D pipeline in part 1).\n We do not use vertex color here, so no color values are declared.\n [size=\"3\"]Setting the Vertex Shader Constant Registers\nThe vertex shader constant registers have to be filled with a call to SetVertexShaderConstant(). We set the material color in RestoreDeviceObjects() in c8 in this example:// set material color\nFLOAT fMaterial[4] = {0,1,0,0};\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(8, fMaterial, 1); SetVertexShaderConstant() is declared like:HRESULT SetVertexShaderConstant (DWORD Register,\n \tCONST void* pConstantData,\n \tDWORD ConstantCount); The first parameter provides the number of the constant register that should be used. In this case 8. The second parameter stores the 128bit data in that constant register and the third parameter gives you the possibility to use the following registers as well. A 4x4 matrix can be stored with one SetVertexShaderConstant() call by providing the number four in ConstantCount. This is done for the clipping matrix in FrameMove():// set the clip matrix\n...\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(4, matTemp, 4); This way the c4, c5, c6 and c7 registers are used to store the matrix.\n [size=\"3\"]The Vertex Shader\nThe vertex shader that is used by RacorX is very simple:// reg c4-7 = WorldViewProj matrix\n// reg c8 = constant color\n// reg v0 = input register\nconst char BasicVertexShader[] =\n\"vs.1.1 \t//Shader version 1.1 \\n\"\\\n\"dp4 oPos.x, v0, c4 //emit projected position \\n\"\\\n\"dp4 oPos.y, v0, c5 //emit projected position \\n\"\\\n\"dp4 oPos.z, v0, c6 //emit projected position \\n\"\\\n\"dp4 oPos.w, v0, c7 //emit projected position \\n\"\\\n\"mov oD0, c8 //material color = c8 \\n\"; It is used inline in a constant char array in RacorX.cpp. This vertex shader incorporates the vs.1.1 vertex shader implementation rules. It transforms from the concatenated and transposed world-, view- and projection-matrix to the clip matrix or clip space with the four dp4 instructions and kicks out into oD0 a green material color with mov.\n As shown above, the values of the c4 - c7 constant registers are set in FrameMove(). These values are calculated by the following code snippet:// rotates the object about the y-axis\nD3DXMatrixRotationY( \u0026amp;m_matWorld, m_fTime * 1.5f );\n// set the clip matrix\nD3DXMATRIX matTemp;\nD3DXMatrixTranspose( \u0026amp;matTemp , \u0026amp;(m_matWorld * m_matView * m_matProj) );\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(4, matTemp, 4); First the quad is rotated around the y-axis by the D3DMatrixRotationY() call, then the concatenated matrix is transposed and then stored in the constant registers c4 - c7. The source of the D3DMatrixRotationY() function might look like:VOID D3DMatrixRotationY(D3DMATRIX * mat, FLOAT fRads)\n{\n D3DXMatrixIdentity(mat);\n mat._11 = cosf(fRads);\n mat._13 = -sinf(fRads);\n mat._31 = sinf(fRads);\n mat._33 = cosf(fRads);\n}\n=\ncosf(fRads) 0 -sinf(fRads) 0\n0 \t0 0 \t0\nsinf(fRads) 0 cosf(fRads) 0\n0 \t0 0 \t0 So fRads equals the amount you want to rotate about the y-axis. After changing the values of the matrix this way, we transpose the matrix by using D3DXMatrixTranspose(), so that its columns are stored as rows. Why do we have to transpose the matrix?\n A 4x4 matrix looks like this:[bquote][font=\"Courier New\"][color=\"#000080\"]a b c d\ne f g h\ni j k l\nm n o p[/color][/font][/bquote] The formula for transforming a vector (v0) through the matrix is:\n dest.x = (v0.x * a) + (v0.y * e) + (v0.z * i) + (v0.w * m) dest.y = (v0.x * b) + (v0.y * f) + (v0.z * j) + (v0.w * n) dest.z = (v0.x * c) + (v0.y * g) + (v0.z * k) + (v0.w * o) dest.w = (v0.x * d) + (v0.y * h) + (v0.z * l) + (v0.w * p) \nSo each column of the matrix should be multiplied with each component of the vector. Our vertex shader uses four dp4 instructions:dest.w = (src1.x * src2.x) + (src1.y * src2.y) + \n \t(src1.z * src2.z) + (src1.w * src2.w)\ndest.x = dest.y = dest.z = unused The dp4 instructions multiplies a row of a matrix with each component of the vector. Without transposing we would end up with:dest.x = (v0.x * a) + (v0.y * b) + (v0.z * c) + (v0.w * d)\ndest.y = (v0.x * e) + (v0.y * f) + (v0.z * g) + (v0.w * h)\ndest.z = (v0.x * i) + (v0.y * j) + (v0.z * k) + (v0.w * l)\ndest.w = (v0.x * m) + (v0.y * n) + (v0.z * o) + (v0.w * p) which is wrong. By transposing the matrix it looks like this in constant memory:[bquote][font=\"Courier New\"][color=\"#000080\"]a e i m\nb f j n\nc g k o\nd h l p[/color][/font][/bquote] so the 4 dp4 operations would now yield:dest.x = (v0.x * a) + (v0.y * e) + (v0.z * i) + (v0.w * m)\ndest.y = (v0.x * b) + (v0.y * f) + (v0.z * j) + (v0.w * n)\ndest.z = (v0.x * c) + (v0.y * g) + (v0.z * k) + (v0.w * o)\ndest.w = (v0.x * d) + (v0.y * h) + (v0.z * l) + (v0.w * p) oroPos.x = (v0.x * c4.x) + (v0.y * c4.y) + (v0.z * c4.z) + (v0.w * c4.w)\noPos.y = (v0.x * c5.x) + (v0.y * c5.y) + (v0.z * c5.z) + (v0.w * c5.w)\noPos.z = (v0.x * c6.x) + (v0.y * c6.y) + (v0.z * c6.z) + (v0.w * c6.w)\noPos.w = (v0.x * c7.x) + (v0.y * c7.y) + (v0.z * c7.z) + (v0.w * c7.w) which is exactly how the vector transformation should work.dp4 gets the matrix values via the constant register c4 - c7 and the vertex position via the input register v0. Temporary registers are not used in this example. The dot product of the dp4 instructions is written to the oPos output register and the value of the constant register c8 is moved into the output register oD0, that is usually used to output diffuse color values.\n [size=\"3\"]Compiling a Vertex Shader\nThe vertex shader that is stored in a char array is compiled with a call to the following code snippet in RestoreDeviceObjects():// Assemble the shader\nrc = D3DXAssembleShader( BasicVertexShader , sizeof(BasicVertexShader) -1,\n \t0 , NULL , \u0026amp;pVS , \u0026amp;pErrors );\nif ( FAILED(rc) )\n{\n OutputDebugString( \"Failed to assemble the vertex shader, errors:\\n\" );\n OutputDebugString( (char*)pErrors-\u0026gt;GetBufferPointer() );\n OutputDebugString( \"\\n\" );\n} D3DXAssembleShader() creates a binary version of the shader in a buffer object via the ID3DXBuffer interface in pVS.HRESULT D3DXAssembleShader(\n LPCVOID pSrcData,\n UINT SrcDataLen,\n DWORD Flags,\n LPD3DXBUFFER* ppConstants,\n LPD3DXBUFFER* ppCompiledShader,\n LPD3DXBUFFER* ppCompilationErrors\n); The source data is provided in the first parameter and the size of the data length in bytes is provided in the second parameter. There are two possible flags for the third parameter called#define D3DXASM_DEBUG 1\n#define D3DXASM_SKIPVALIDATION 2 The first one inserts debug info as comments into the shader and the second one skips validation. This flag can be set for a working shader.\n Via the fourth parameter a ID3DXBuffer interface can be exported, to get a vertex shader declaration fragment of the constants. To ignore this parameter, it is set to NULL here. In case of an error, the error explanation would be stored in a buffer object via the ID3DXBuffer interface in pErrors. To see the output of OutputDebugString() the debug process in the Visual C/C++ IDE must be started with [lessthan]F5\u0026gt;.\n [size=\"3\"]Creating a Vertex Shader\nThe vertex shader is validated and a handle for it is retrieved via a call to CreateVertexShader() in m_dwVertexShader: The following lines of code can be found in RestoreDeviceObjects():// Create the vertex shader\nrc = m_pd3dDevice-\u0026gt;CreateVertexShader( dwDecl, (DWORD*)pVS-\u0026gt;GetBufferPointer(),\n \u0026amp;m_dwVertexShader, 0 );\nif ( FAILED(rc) )\n{\n OutputDebugString( \"Failed to create the vertex shader, errors:\\n\" );\n D3DXGetErrorStringA(rc,szBuffer,sizeof(szBuffer));\n OutputDebugString( szBuffer );\n OutputDebugString( \"\\n\" );\n} CreateVertexShader() gets a pointer to the buffer with the binary version of the vertex shader via the ID3DXBuffer interface. This function gets the vertex shader declaration via dwDecl, that maps vertex data to specific vertex shader input registers. If an error occurs, its explanation is accessible via a pointer to a buffer object that is retrieved via the ID3DXBuffer interface in pVS-\u0026gt;GetBufferPointer(). D3DXGetErrorStringA() interprets all Direct3D and Direct3DX HRESULTS and returns an error message in szBuffer.\n It is possible to force the usage of software vertex processing with the last parameter by using the D3DUSAGE_SOFTWAREPROCESSING flag. It must be used when the D3DRS_SOFTWAREVERTEXPROCESSING member of the D3DRENDERSTATETYPE enumerated type is TRUE.\n [size=\"3\"]Setting a Vertex Shader\nThe vertex shader is set via SetVertexShader() in the Render() function:// set the vertex shader\nm_pd3dDevice-\u0026gt;SetVertexShader( m_dwVertexShader ); The only parameter that must be provided is the handle to the vertex shader. This function executes the vertex shader as often as there are vertices.\n [size=\"3\"]Free Vertex Shader Resources\nVertex shader resources must be freed with a call toif ( m_dwVertexShader != 0xffffffff )\n{\n m_pd3dDevice-\u0026gt;DeleteVertexShader( m_dwVertexShader );\n m_dwVertexShader = 0xffffffff;\n} This example frees the vertex shader resources in the InvalidateDeviceObjects() framework function, because this has to happen in case of a change of the window size or a device.\n [size=\"3\"]Non-Shader specific Code\nThe non-shader specific code of RacorX deals with setting render states and the handling of the vertex and index buffer. A few render states have to be set in RestoreDeviceObjects():// z-buffer enabled\nm_pd3dDevice-\u0026gt;SetRenderState( D3DRS_ZENABLE, TRUE );\n// Turn off D3D lighting, since we are providing our own vertex shader lighting\nm_pd3dDevice-\u0026gt;SetRenderState( D3DRS_LIGHTING, FALSE );\n// Turn off culling, so we see the front and back of the quad\nm_pd3dDevice-\u0026gt;SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); The first instructions enables the z-buffer (a corresponded flag has to be set in the constructor of the Direct3D framework class, so that the device is created with a z-buffer).\n The fixed-function lighting is not needed, so it is switched off with the second statement. To be able to see both sides of the quad, backface culling is switched off with the third statement.\n The vertex and index buffer is created in InitDeviceObjects():// create and fill the vertex buffer\n// Initialize vertices for rendering a quad\nVERTEX Vertices[] =\n{\n// x y z\n{ -1.0f,-1.0f, 0.0f, },\n{ 1.0f,-1.0f, 0.0f, },\n{ 1.0f, 1.0f, 0.0f, },\n{ -1.0f, 1.0f, 0.0f, },\n};\nm_dwSizeofVertices = sizeof (Vertices);\n// Create the vertex buffers with four vertices\nif( FAILED( m_pd3dDevice-\u0026gt;CreateVertexBuffer( 4 * sizeof(VERTEX),\n \tD3DUSAGE_WRITEONLY , sizeof(VERTEX), D3DPOOL_MANAGED, \u0026amp;m_pVB ) ) )\n return E_FAIL;\n// lock and unlock the vertex buffer to fill it with memcpy\nVOID* pVertices;\nif( FAILED( m_pVB-\u0026gt;Lock( 0, m_dwSizeofVertices, (BYTE**)\u0026amp;pVertices, 0 ) ) )\n return E_FAIL;\nmemcpy( pVertices, Vertices, m_dwSizeofVertices);\nm_pVB-\u0026gt;Unlock();\n// create and fill the index buffer\n// indices\nWORD wIndices[]={0, 1, 2, 0, 2, 3};\nm_wSizeofIndices = sizeof (wIndices);\n// create index buffer\nif(FAILED (m_pd3dDevice-\u0026gt;CreateIndexBuffer(m_wSizeofIndices, 0,\n \tD3DFMT_INDEX16, D3DPOOL_MANAGED, \u0026amp;m_pIB)))\n return E_FAIL;\n// fill index buffer\nVOID *pIndices;\nif (FAILED(m_pIB-\u0026gt;Lock(0, m_wSizeofIndices, (BYTE **)\u0026amp;pIndices, 0)))\n return E_FAIL;\nmemcpy(pIndices, wIndices, m_wSizeofIndices);\nm_pIB-\u0026gt;Unlock(); The four vertices of the quad are stored in a VERTEX structure, which holds for each vertex three FLOAT values for the position.\n By using the flag D3DFMT_INDEX16 in CreateIndexBuffer(), 16-bit variables are used to store the indices into the wIndices structure. So the maximum number of available indices are 64 k. Both buffers use a managed memory pool with D3DPOOL_MANAGED, so they will be cached in the system memory.[bquote] D3DPOOL_MANAGED resources are read from the system memory which is quite fast and they are written to the system memory and afterwards uploaded to wherever the non-system copy has to go (AGP or VIDEO memory). This upload happens when the resource is unlocked. So there are always two copies of a resource, one in the system and one in the AGP or VIDEO memory. This is a less efficient but bullet-proof way. It works for any class of driver and must be used with unified memory architecture boards. Handling resources with D3DPOOL_DEFAULT is more efficient. In this case the driver will choose the best place for the resource.\n Why do we use a vertex buffer at all ? The vertex buffer can be stored in the memory of your graphic card or AGP, where it can be accessed very quickly by 3-D hardware. So a copy between system memory and the graphic card/AGP memory could be avoided. This is important for hardware that accelerates transformation and lighting. Without vertex buffers a lot of bus-traffic would happen by transforming and lighting the vertices.\n Why do we use an index buffer ? You will get the maximum performance when you reduce the duplication in vertices transformed and sent across the bus to the rendering device. A nonindexed triangle list for example achieves no vertex sharing, so it\u0027s the least optimal method, because DrawPrimitive*() is called several times. Using indexed lists or strips reduce the call overhead of DrawPrimitive*() methods (Reducing DrawPrimitve*() methods is also called batching) and because of the reduction of vertices to send through the bus, it saves memory bandwidth. Indexed strips are more hardware-cache friendly on newer hardware than indexed lists. The performance of index processing operations depends heavily on where the index buffer exists in memory. At the time of this writing, the only graphic cards that supports index buffers in hardware are the RADEON 8x00 series.[/bquote]\n [size=\"3\"]Summarize\nRacorX shows a simple vertex shader together with its infrastructure. The shader is inlined in racorx.cpp and compiled with D3DXAssembleShader(). It uses four dp4 instructions for the transformation of the quad and only one material color.\n The upcoming examples are build on this example and only the functional additions will be shown on the next pages.\n[size=\"5\"]RacorX2\nThe main difference between RacorX and RacorX2 (see attached resource file) is the compilation of the vertex shader with NVASM. Whereas the first example compiles the vertex shader with D3DXAssembleShader() while the application starts up, RacorX2 uses a pre-compiled vertex shader.\n To add the NVIDIA vertex and pixel shader assembler, you have to do the following steps:\nCreate a directory, for example [lessthan]C:\\NVASM\u0026gt;: and unzip nvasm.exe and the documentation into it \nShow your Visual C++ IDE the path to this exe with\n[lessthan]Tools-\u0026gt;Options-\u0026gt;Directories\u0026gt;\nand choose from the drop down menu [lessthan]Show directories for:\u0026gt;\n[lessthan]Executable files\u0026gt;\nAdd the path to NVASM\n [lessthan]C:\\NVASM\u0026gt;\nNow the dialog box should look like this: \nFigure 7 - Integrating NVASM into Visual C/C++\n \nAdditionally you have to tell every vertex shader file in the IDE, that it has to be compiled with NVASM. The easiest way to do that is looking into the example RacorX2. Just fire up your Visual C++ IDE by clicking on the [lessthan]RacorX.dsp\u0026gt; in the RacorX2 directory. Click on the [lessthan]FileView\u0026gt; tab of the Workspace dialog and there on [lessthan]Shaders\u0026gt; to view the available shader files. A right-click on the file [lessthan]basic.vsh\u0026gt; should show you a popup. Click on [lessthan]Settings...\u0026gt;. The project settings of your project might look like: \nFigure 8 - Custom Build Options Vertex Shader Files\n \nThe entry in the entry field called \"Commands\" is:\nnvasm.exe $(InputPath) shaders\\$(InputName).vso\nFor the entry field named \"Outputs\", I use the input name as the name for the output file with an *.vso extension. The output directory should be the shaders directory. ShaderX author Kenneth Hurley is the author of NVASM. Read more in his paper [Hurley]. The output of NVASM in the build window of your Visual C/C++ IDE should look like this: \nFigure 9 - NVASM Output\n\n The vertex shader is provided in its own ASCII file called basic.vsh. After compilation a binary object file with the name basic.vso is created in the directory [lessthan]shaders\u0026gt;: \nFigure 10 - Directory Content RacorX2\n\n [size=\"3\"]Creating a Vertex Shader\nBecause of the already compiled vertex shader, the creation of the vertex shader has to be done in a different way than in the previous example. This happens in the InitDeviceObjects() function://shader decl\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register 0\n D3DVSD_END()\n};\n// loads a *.vso binary file, already compiled with NVASM and\n// creates a vertex shader\nif (FAILED(CreateVSFromCompiledFile (m_pd3dDevice, dwDecl,\"shaders/basic.vso\",\n \t\u0026amp;m_dwVertexShader)))\n return E_FAIL; CreateVSFromCompiledFile() opens and reads in the binary vertex shader file and creates a vertex shader. The source of this function can be found at the end of the file racorx.cpp in the RacorX2 directory://----------------------------------------------------------------------------\n// Name: CreateVSFromBinFile\n// Desc: loads a binary *.vso file that was compiled by NVASM\n// and creates a vertex shader\n//----------------------------------------------------------------------------\nHRESULT CMyD3DApplication::CreateVSFromCompiledFile (IDirect3DDevice8* m_pd3dDevice,\n \tDWORD* dwDeclaration,\n \tTCHAR* strVSPath,\n \tDWORD* m_dwVS)\n{\n char szBuffer[128]; // debug output\n DWORD* dwpVS; // pointer to address space of the calling process\n HANDLE hFile, hMap; // handle file and handle mapped file\n TCHAR tempVSPath[512]; // temporary file path\n HRESULT hr; // error\nif( FAILED( hr = DXUtil_FindMediaFile( tempVSPath, strVSPath ) ) )\n return D3DAPPERR_MEDIANOTFOUND;\nhFile = CreateFile(tempVSPath, GENERIC_READ,0,0,OPEN_EXISTING,\n \tFILE_ATTRIBUTE_NORMAL,0);\nif(hFile != INVALID_HANDLE_VALUE)\n{\n if(GetFileSize(hFile,0) \u0026gt; 0)\n hMap = CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0);\n else\n {\n CloseHandle(hFile);\n return E_FAIL;\n }\n}\nelse\n return E_FAIL;\n// maps a view of a file into the address space of the calling process\ndwpVS = (DWORD *)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);\n// Create the vertex shader\nhr = m_pd3dDevice-\u0026gt;CreateVertexShader( dwDeclaration, dwpVS, m_dwVS, 0 );\nif ( FAILED(hr) )\n{\n OutputDebugString( \"Failed to create Vertex Shader, errors:\\n\" );\n D3DXGetErrorStringA(hr,szBuffer,sizeof(szBuffer));\n OutputDebugString( szBuffer );\n OutputDebugString( \"\\n\" );\nreturn hr;\n}\n UnmapViewOfFile(dwpVS);\n CloseHandle(hMap);\n CloseHandle(hFile);\n return S_OK;\n} DXUtil_FindMediaFile(), a helper function located in the framework file dxutil.cpp, returns the path to the already compiled vertex shader file. CreateFile() opens and reads the existing file:HANDLE CreateFile(\n LPCTSTR lpFileName, // file name\n DWORD dwDesiredAccess, // access mode\n DWORD dwShareMode, // share mode\n LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD\n DWORD dwCreationDisposition, // how to create\n DWORD dwFlagsAndAttributes, // file attributes\n HANDLE hTemplateFile // handle to template file\n); Its first parameter is the path to the file. The flag GENERIC_READ specifies read access to the file in the second parameter. The following two parameters are not used, because file sharing should not happen and the fourth parameter is not used, because the file should not be inherited by a child process. The fifth parameter is set to OPEN_EXISTING. This way, the function fails if the file does not exist. Setting the sixt parameter to FILE_ATTRIBUTE_NORMAL indicates, that the file has no other attributes. A template file is not used here, so the last parameter is set to 0. Please consult the Platform SDK help file for more information.CreateFileMapping() creates or opens a named or unnamed file-mapping object for the specified file:HANDLE CreateFileMapping(\n HANDLE hFile, // handle to file\n LPSECURITY_ATTRIBUTES lpAttributes, // security\n DWORD flProtect, // protection\n DWORD dwMaximumSizeHigh, // high-order DWORD of size\n DWORD dwMaximumSizeLow, // low-order DWORD of size\n LPCTSTR lpName // object name\n); The first parameter is a handle to the file from which to create a mapping object. The file must be opened with an access mode compatible with the protection flags specified by the flProtect parameter. We have opened the file in CreateFile() with GENERIC_READ, therefore we use PAGE_READONLY here. Other features of CreateFileMapping() are not needed, therefore we set the rest of the parameters to 0.MapViewOfFile() function maps a view of a file into the address space of the calling process:LPVOID MapViewOfFile(\n HANDLE hFileMappingObject, // handle to file-mapping object \n DWORD dwDesiredAccess, // access mode\n DWORD dwFileOffsetHigh, // high-order DWORD of offset\n DWORD dwFileOffsetLow, // low-order DWORD of offset\n SIZE_T dwNumberOfBytesToMap // number of bytes to map\n); This function only gets the handle to the file-mapping object from CreateFileMapping() and in the second parameter the access mode FILE_MAP_READ. The access mode parameter specifies the type of access to the file view and, therefore, the protection of the pages mapped by the file. More features are not needed, therefore the rest of the parameters are set to 0.CreateVertexShader() is used to create and validate a vertex shader. It takes the vertex shader declaration (which maps vertex buffer streams to different vertex input registers) in its first parameter as a pointer and returns the shader handle in the third parameter. The second parameter gets the vertex shader instructions of the binary code pre-compiled by a vertex shader assembler. With the fourth parameter you can force software vertex processing with D3DUSAGE_SOFTWAREPROCESSING.\n As in the previous example OutputDebugString() shows the complete error message in the output debug window of the Visual C/C++ IDE and D3DXGetErrorStringA() interprets all Direct3D and Direct3DX HRESULTS and returns an error message in szBuffer.\n [size=\"3\"]Summarize\nThis example showed the integration of NVASM to pre-compile a vertex shader and how to open and read a binary vertex shader file.\n[size=\"5\"]RacorX3\nThe main improvement of RacorX3 (see attached resource file) over RacorX2 is the addition of a per-vertex diffuse reflection model in the vertex shader. This is one of the simplest lighting calculations, which outputs the color based on the dot product of the vertex normal with the light vector.\n RacorX3 uses a light positioned at (0.0, 0.0, 1.0) and a green color. \nFigure 11 - RacorX3\n\n As usual we are tracking the life-cycle of the vertex shader.\n [size=\"3\"]Vertex Shader Declaration\nThe vertex shader declaration has to map vertex data to specific vertex shader input registers. Additionally to the previous examples, we need to map a normal vector to the input register v3:// vertex shader declaration\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register #1\n D3DVSD_REG(3, D3DVSDT_FLOAT3 ), // normal in input register #4\n D3DVSD_END()\n}; The corresponding layout of the vertex buffer looks like this:struct VERTICES\n{\n FLOAT x, y, z; // The untransformed position for the vertex\n FLOAT nx, ny, nz; // the normal\n};\n// Declare custom FVF macro.\n#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL) Each vertex consists of three position floating point values and three normal floating point values in the vertex buffer. The vertex shader gets the position and normal values from the vertex buffer via v0 and v3.\n [size=\"3\"]Setting the Vertex Shader Constant Registers\nThe vertex shader constants are set in FrameMove() and RestoreDeviceObjects(). This example uses a more elegant way to handle the constant registers. The file const.h that is included in racorx.cpp and diffuse.vsh, gives the constant registers an easier to remember name:#define CLIP_MATRIX 0\n#define CLIP_MATRIX_1 1\n#define CLIP_MATRIX_2 2\n#define CLIP_MATRIX_3 3\n#define INVERSE_WORLD_MATRIX 4\n#define INVERSE_WORLD_MATRIX_1 5\n#define INVERSE_WORLD_MATRIX_2 6\n#define LIGHT_POSITION 11\n#define DIFFUSE_COLOR 14\n#define LIGHT_COLOR 15 In FrameMove() a clipping matrix and an inversed world matrix are set into the constant registers:HRESULT CMyD3DApplication::FrameMove()\n{\n // rotates the object about the y-axis\n D3DXMatrixRotationY( \u0026amp;m_matWorld, m_fTime * 1.5f );\n // set the clip matrix\n m_pd3dDevice-\u0026gt;SetVertexShaderConstant(CLIP_MATRIX, \u0026amp;(m_matWorld *\n m_matView * m_matProj), 4);\n D3DXMATRIX matWorldInverse;\n D3DXMatrixInverse(\u0026amp;matWorldInverse, NULL, \u0026amp;m_matWorld);\n m_pd3dDevice-\u0026gt;SetVertexShaderConstant(INVERSE_WORLD_MATRIX,\n \u0026amp;matWorldInverse,3);\n return S_OK;\n} Contrary to the previous examples, the concatenated world-, view- and projection matrix, which is used to rotate the quad, is not transposed here. This is because the matrix will be transposed in the vertex shader as shown below.\n To transform the normal, an inverse 4x3 matrix is send to the vertex shader via c4 -c6.\n [size=\"3\"]The Vertex Shader\nThe vertex shader is a little bit more complex, than the one used in the previous examples:; per-vertex diffuse lighting\n#include \"const.h\"\nvs.1.1\n; transpose and transform to clip space\nmul r0, v0.x, c[CLIP_MATRIX]\nmad r0, v0.y, c[CLIP_MATRIX_1], r0\nmad r0, v0.z, c[CLIP_MATRIX_2], r0\nadd oPos, c[CLIP_MATRIX_3], r0\n; transform normal\ndp3 r1.x, v3, c[INVERSE_WORLD_MATRIX]\ndp3 r1.y, v3, c[INVERSE_WORLD_MATRIX_1]\ndp3 r1.z, v3, c[INVERSE_WORLD_MATRIX_2]\n; renormalize it\ndp3 r1.w, r1, r1\nrsq r1.w, r1.w\nmul r1, r1, r1.w\n; N dot L\n; we need L vector towards the light, thus negate sign\ndp3 r0, r1, -c[LIGHT_POSITION]\nmul r0, r0, c[LIGHT_COLOR] ; modulate against light color\nmul oD0, r0, c[DIFFUSE_COLOR] ; modulate against material The mul, mad and add instructions transpose and transform the matrix provided in c0 - c3 to clip space. As such they are nearly functionally equivalent to the transposition of the matrix and the four dp4 instructions shown in the previous examples. There are two caveats to bear in mind: The complex matrix instructions like m4x4 might be faster in software emulation mode and v0.w is not used here. oPos.w is automatically filled with 1. These instructions should save the CPU cycles used for transposing.\n The normals are transformed in the following three dp3 instructions and then renormalized with the dp3, rsq and mul instructions.\n You can think of a normal transform in the following way: Normal vectors (unlike position vectors) are simply directions in space, and as such they should not get squished in magnitude, and translation doesn\u0027t change their direction. They should simply be rotated in some fashion to reflect the change in orientation of the surface. This change in orientation is a result of rotating and squishing the object, but not moving it. The information for rotating a normal can be extracted from the 4x4 transformation matrix by doing transpose and inversion. A more math-related explanation is given in [Haines/M?ller][Turkowski].\n So the bullet-proof way to use normals, is to transform the transpose of the inverse of the matrix, that is used to transform the object. If the matrix used to transform the object is called M, then we must use the matrix, N, below to transform the normals of this object.[bquote][font=\"Courier New\"][color=\"#000080\"]N = transpose( inverse(M) )[/color][/font]\nThe normal can be transformed with the transformation matrix (usually the world matrix), that is used to transform the object in the following cases:\nMatrix formed from rotations (orthogonal matrix), because the inverse of an orthogonal matrix is its transpose \nMatrix formed from rotations and translation (rigid-body transforms), because translations do not affect vector direction \n\nMatrix formed from rotations and translation and uniform scalings, because such scalings affect only the length of the transformed normal, not its direction. A uniform scaling is simply a matrix which uniformly increases or decreases the object\u0027s size, vs. a non-uniform scaling, which can stretch or squeeze an object. If uniform scalings are used, then the normals do have to be renormalized. Therefore using the world matrix would be sufficient in this example.[/bquote] \n\n That\u0027s exactly, what the source is doing. The inverse world matrix is delivered to the vertex shader via c4 - c6. The dp3 instruction handles the matrix in a similar way as dp4.\n By multiplying a matrix with a vector, each column of the matrix should be multiplied with each component of the vector. dp3 and dp4 are only capable to multiply each row of the matrix with each component of the vector. In case of the position data, the matrix is transposed to get the right results.\n In case of the normals, no transposition is done. So dp3 calculates the dot product by multiplying the rows of the matrix with the components of the vector. This is like using a transposed matrix.\n The normal is re-normalized with the dp3, rsq and mul instructions. Re-normalizing a vector means align its length to 1. That\u0027s because we need a unit vector to calculate our diffuse lighting effect.\n To calculate a unit vector, divide the vector by its magnitude or length. The magnitude of vectors is calculated by using the Pythagorean theorem:\n x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup] = m[sup]2[/sup]\n The length of the vector is retrieved by\n ||A|| = sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])\n The magnitude of a vector has a special symbol in mathematics. It is a capital letter designated with two vertical bars: ||A||. So dividing the vector by its magnitude is:\n UnitVector = Vector / sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])\n The lines of code in the vertex shader, that handles the calculation of the unit vector looks like this:; renormalize it\ndp3 r1.w, r1, r1 ; (src1.x * src2.x) + (src1.y * src2.y) + (src1.z * src2.z)\nrsq r1.w, r1.w ; if (v != 0 \u0026amp;\u0026amp; v != 1.0) v = (float)(1.0f / sqrt(v))\nmul r1, r1, r1.w ; r1 * r1.w dp3 squares the x, y and z components of the temporary register r1, adds them and returns the result in r1.w. rsq divides 1 by the result in r1.w and stores the result in r1.w. mul multiplies all components of r1 with r1.w. Afterwards, the result in r1.w is not used anymore in the vertex shader.\n The underlying calculation of these three instructions can be represented by the following formula, which is mostly identical to the formula postualted above:\n UnitVector = Vector * 1/sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])\n Lighting is calculated with the following three instruction:dp3 r0, r1, -c[LIGHT_POSITION]\nmul r0, r0, c[LIGHT_COLOR] ; modulate against light color\nmul oD0, r0, c[DIFFUSE_COLOR] ; modulate against diffuse color Nowadays the lighting models used in current games are not based on much physical theory. Game programmers use approximations that try to simulate the way photons are reflected from objects in a rough but efficient manner.\n One differentiates usually between different kind of light sources and different reflection models. The common lighting sources are called directional, point light and spotlight. The most common reflections models are ambient, diffuse and specular lighting.\n This example uses a directional light source with an ambient and a diffuse reflection model.Directional Light\n RacorX3 uses a light source in an infinite distance. This simulates the long distance the light beams have to travel from the sun. We treat this light beams as beeing parallel. This kind of light source is called directional light source.Diffuse Reflection\n Whereas ambient light is considered to be uniform from any direction, diffuse light simulates the emission of an object by a particular light source. Therefore you are able to see that light falls onto the surface of an object from a particular direction by using the diffuse lighting model.\n It is based on the assumption that light is reflected equally well in all directions, so the appearance of the reflection does not depend on the position of the observer. The intensity of the light reflected in any direction depends only on how much light falls onto the surface.\n If the surface of the object is facing the light source, which means is perpendicular to the direction of the light, the density of the incident light is the highest. If the surface is facing the light source under some angle smaller than 90 degrees, the density is proportionally smaller.\n The diffuse reflection model is based on a law of physics called Lambert\u0027s Law, which states that for ideally diffuse (totally matte) surfaces, the reflected light is determined by the cosine between the surface normal N and the light vector L. \nFigure 12 - Diffuse Lighting\n\n The left figure shows a geometric interpretation of Lambert\u0027s Law (see also [RTR]). The middle figure shows the light rays hitting the surface perpendicularly in a distance d apart. The intensity of the light is related to this distance. It decreases as d becomes greater. This is shown by the right figure. The light rays make an angle ? with the normal of the plane. This illustrates that the same amount of light that passes through one side of a right-angle triangle is reflected from the region of the surface corresponding to the triangles hypotenuse. Due to the relationships that hold in a right-angle triangle, the length of the hypotenuse is d/cos ? of the length of the considered side. Thus you can deduce that if the intensity of the incident light is Idirected, the amount of light reflected from a unit surface is Idirected cos ?. Adjusting this with a coefficient that describes reflection properties of the matter leads to the following equation (see also [Savchenko]):\n Ireflected = Cdiffuse * Idirected cos ?\n This equation demonstrates that the reflection is at its peak for surfaces that are perpendicular to the direction of light and diminishes for smaller angles, because the cosinus value is very large. The light is obscured by the surface if the angles is more than 180 or less than 0 degrees, because the cosinus value is small. You will obtain negative intensity of the reflected light, which will be clamped by the output registers.\n In an implementation of this model, you have to find a way to compute cos ?. By definition the dot or scalar product of the light and normal vector can be expressed as\n N dot L = ||N|| ||L||cos ?\n where ||N|| and ||L|| are the lengths of the vectors. If both vectors are unit length, you can compute cos ? as the scalar or dot product of the light and normal vector. Thus the expression is\n Ireflected = Cdiffuse * Idirected(N dot L)\n So (N dot L) is the same as the cosine of the angle between N and L, therefore as the angle decrease, the resulting diffuse value is higher. This is exactly what the dp3 instruction and the first mul instruction are doing. Here is the source with the relevant part of constant.h:#define LIGHT_POSITION 11\n#define MATERIAL_COLOR 14\n-----\ndp3 r0, r1, -c[LIGHT_POSITION]\nmul r0, r0, c[LIGHT_COLOR] ; modulate against light color\nmul oD0, r0, c[DIFFUSE_COLOR] ; modulate against material So the vertex shader registers are involved in the following way:r0 = (r1 dot -c11) * c14 This example modulates additionally against the blue light color in c15:r0 = (c15 * (r1 dot -c11)) * c14 [size=\"3\"]Summarize\nRacorX3 shows the usage of an include file to give constants a name that can be remembered in a better way. It shows how to normalize vectors and it just strive the problem of transforming normals, but shows a bullet-proof method to do it.\n The example introduces an optimization technique, that eliminates the need to transpose the clip space matrix with the help of the CPU and it shows the usage of a simple diffuse reflection model, that lights the quad on a per-vertex basis.\n[size=\"5\"]RacorX4\nRacorX4 (see attached resource file) has gotten a few additional features compared to RacorX3. First of all this example will not use a plain quad anymore, it uses instead a B?zier patch class, that shows a round surface with a texture attached to it. To simulate light reflections, a combined diffuse and specular reflection model is used. \nFigure 13 - RacorX4\n\n RacorX4 uses the trackball class provided with the Common files framework, to rotate and move the object. You can choose different specular colors with the [lessthan]C\u0026gt; key and zoom in and out with the mouse wheel.\n [size=\"3\"]Vertex Shader Declaration\nCompared to RacorX3, additionally the texture coordinates will be mapped to input register v7.// shader decl\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register v0\n D3DVSD_REG(3, D3DVSDT_FLOAT3 ), // normal in input register v3\n D3DVSD_REG(7, D3DVSDT_FLOAT2), // tex coordinates\n D3DVSD_END()\n}; The corresponding layout of the vertex buffer in BPatch.h looks like:struct VERTICES {\n D3DXVECTOR3 vPosition;\n D3DXVECTOR3 vNormal;\n D3DXVECTOR2 uv;\n};\n// Declare custom FVF macro.\n#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1) The third flag used in the custom FVF macro indicates the usage of one texture coordinate pair. This macro is provided to CreateVertexBuffer(). The vertex shader gets the position values from the vertex buffer via v0, the normal values via v3 and the two texture coordinates via v7.\n [size=\"3\"]Setting the Vertex Shader Constants\nThe vertex shader constants are set in FrameMove() and RestoreDeviceObjects(). The file const.h holds the following defines:#define CLIP_MATRIX 0\n#define CLIP_MATRIX_1 1\n#define CLIP_MATRIX_2 2\n#define CLIP_MATRIX_3 3\n#define INVERSE_WORLD_MATRIX 8\n#define INVERSE_WORLD_MATRIX_1 9\n#define INVERSE_WORLD_MATRIX_2 10\n#define LIGHT_VECTOR 11\n#define EYE_VECTOR 12\n#define SPEC_POWER 13\n#define SPEC_COLOR 14 In FrameMove() a clipping matrix, the inversed world matrix, an eye vector and a specular color are set:// set the clip matrix\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(CLIP_MATRIX,\n \t\u0026amp;(m_matWorld * m_matView * m_matProj),4);\n// set the world inverse matrix\nD3DXMATRIX matWorldInverse;\nD3DXMatrixInverse(\u0026amp;matWorldInverse, NULL, \u0026amp;m_matWorld);\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(INVERSE_WORLD_MATRIX, matWorldInverse,3);\n// stuff for specular lighting\n// set eye vector E\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(EYE_VECTOR, vEyePt,1);\n// specular color\nif(m_bKey[\u0027C\u0027])\n{\n m_bKey[\u0027C\u0027]=0;\n ++m_dwCurrentColor;\n if(m_dwCurrentColor \u0026gt;= 3)\n m_dwCurrentColor=0;\n}\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(SPEC_COLOR, m_vLightColor[m_dwCurrentColor], 1); Like in the previous example the concatenated world-, view- and projection matrix are set into c0 - c3, to get transposed in the vertex shader and the inverse 4x3 world matrix is send to vertex shader, to transform the normal.\n The eye vector (EYE_VECTOR) that is used to build up the view matrix is stored in constant register c12. As shown below, this vector is helpful to build up the specular reflection model used in this upcoming examples.\n The user can pick one of the following specular colors with the [lessthan]C\u0026gt; key:m_vLightColor[0]=D3DXVECTOR4(0.3f,0.1f,0.1f,1.0f);\nm_vLightColor[1]=D3DXVECTOR4(0.1f,0.5f,0.1f,1.0f);\nm_vLightColor[2]=D3DXVECTOR4(0.0f,0.1f,0.4f,1.0f); In RestoreDeviceObjects() the specular power, the light vector and the diffuse color are set:// specular power\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(SPEC_POWER, D3DXVECTOR4(0,10,25,50),1);\n// light direction\nD3DXVECTOR3 vLight(0,0,1);\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(LIGHT_VECTOR, vLight,1);\nD3DXCOLOR matDiffuse(0.9f, 0.9f, 0.9f, 1.0f);\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(DIFFUSE_COLOR, \u0026amp;matDiffuse, 1); As in the previous examples, the light is positioned at (0.0, 0.0, 1.0). There are four specular power values, from which one will be used in the vertex shader.[bquote]To optimize the usage of SetVertexShaderConstant(), the specular color could be set into the fourth component of the light vector, which only used three of its components.[/bquote] [size=\"3\"]The Vertex Shader\nThe vertex shader handles a combined diffuse and specular reflection model:; diffuse and specular vertex lighting\n#include \"const.h\"\nvs.1.1\n; transpose and transform to clip space\nmul r0, v0.x, c[CLIP_MATRIX]\nmad r0, v0.y, c[CLIP_MATRIX_1], r0\nmad r0, v0.z, c[CLIP_MATRIX_2], r0\nadd oPos, c[CLIP_MATRIX_3], r0\n; output texture coords\nmov oT0, v7\n; transform normal\ndp3 r1.x, v3, c[INVERSE_WORLD_MATRIX]\ndp3 r1.y, v3, c[INVERSE_WORLD_MATRIX_1]\ndp3 r1.z, v3, c[INVERSE_WORLD_MATRIX_2]\n; renormalize it\ndp3 r1.w, r1, r1\nrsq r1.w, r1.w\nmul r1, r1, r1.w\n; light vector L\n; we need L towards the light, thus negate sign\nmov r5, -c[LIGHT_VECTOR]\n; N dot L\ndp3 r0.x, r1, r5\n; compute normalized half vector H = L + V\nadd r2, c[EYE_VECTOR], r5 ; L + V\n; renormalize H\ndp3 r2.w, r2, r2\nrsq r2.w, r2.w\nmul r2, r2, r2.w\n; N dot H\ndp3 r0.y, r1, r2\n; compute specular and clamp values (lit)\n; r0.x - N dot L\n; r0.y - N dot H\n; r0.w - specular power n\nmov r0.w, c[SPEC_POWER].y ; n must be between -128.0 and 128.0\nlit r4, r0\n; diffuse color * diffuse intensity(r4.y)\nmul oD0, c[DIFFUSE_COLOR], r4.y\n; specular color * specular intensity(r4.z)\nmul oD1, c[SPEC_COLOR], r4.z Compared to the vertex shader in the previous example program, this shader maps the texture coordinates to texture stage 0 with the following instruction:; output texture coords\nmov oT0, v7 The corresponding texture stage states set for the shading of the pixels in the multitexturing unit will be shown below in the section named \"Non-Shader specific code\". The instructions that transform the normal and calculate the diffuse reflection were already discussed along with the previous example.\n The real new functionality in this shader is the calculation of the specular reflection, which happens in the code lines starting with the add instruction.Specular Reflection\n Compared to the diffuse reflection model, the appearance of the reflection depends in the specular reflection model on the position of the viewer. When the direction of the viewing coincides, or nearly coincides, with the direction of specular reflection, a bright highlight is observed. This simulates the reflection of a light source by a smooth, shiny and polished surface.\n To describe reflection from shiny surfaces an approximation is commonly used, which is called the Phong illumination model (not to be confused with Pong Shading), named after its creator Phong Bui Tong [Foley]. According to this model, a specular highlight is seen when the viewer is close to the direction of reflection. The intensity of light falls off sharply when the viewer moves away from the direction of the specular reflection. \nFigure 14 - Vectors for Specular Reflection\n\n A model describing this effect has to be aware of at least the location of the light source L, the location of the viewer V, and the orientation of the surface N. Additionally a vector R that describes the direction of the reflection might be useful. The half way vector H, that halfs the angle between L and V will be introduced below.\n The original Phong formula approximates the falloff of the intensity. It looks like this:\n kspecular cos[sup]n[/sup](ss)\n where kspecular is a scalar coefficient showing the percentage of the incident light reflected. ss describes the angle between R and V. The exponent n characterizes the shiny properties of the surface and ranges from one to infinity. Objects that are matte require a small exponent, since they produce a large, dim, specular highlight with a gentle falloff. Shiny surfaces should have a sharp highlight that is modeled by a very large exponent, making the intensity falloff very steep.\n Together with the diffuse reflection model shown above, the Phong illumination model can be expressed in the following way:\n Ireflected = Idirected((N dot L) + kspecular cos[sup]n[/sup](ss))\n cos[sup]n[/sup](ss) can be replaced by using the mechanism of the dot or scalar product of the unit vectors R and V:\n Ireflected = Idirected((N dot L) + kspecular (R dot V)[sup]n[/sup])\n This is the generally accepted phong reflection equation. As the angle between V and R decreases, the specularity will rise.\n Because it is expensive to calculate the reflection vector R (mirror of light incidence around the surface normal), James F. Blinn [Blinn] introduced a way to do this using an imaginary vector H, which is defined as halfway between L and V. H is therefore:\n H = (L + V) / 2\n When H coincides with N, the direction of the reflection coincides with the viewing direction V and a specular hightlight is observed. So the original formula\n (R dot V)[sup]n[/sup]\n is expanded to\n (N dot ((L + V) / 2))[sup]n[/sup]\n or\n (N dot H)[sup]n[/sup]\n The complete Blinn-Phong model formula looks like:\n Ireflected = Idirected((N dot L) + kspecular (N dot H)[sup]n[/sup])\n Now back to the relevant code piece that calculates the specular reflection:; compute normalized half vector H = L + V\nadd r2, c[EYE_VECTOR], r5 ; L + V\n; renormalize H\ndp3 r2.w, r2, r2\nrsq r2.w, r2.w\nmul r2, r2, r2.w\n; N\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n", "articleBody": "(Editor\u0027s Note - this article requires additional formatting work and is incomplete)\n(Last modification: March 31st, 2002)\n [size=\"5\"]Preface\nDuring this part of the introduction a very simple program that shows a rotating quad will evolve into a more sophisticated application showing a B?zier patch class with a diffuse and specular reflection model, featuring a point light source. The example applications are all build on each other in a way that most of the code of the previous example is re-used in the following example. This way the explanation of the features stayed focused on the advancements of the specific example.\n[size=\"5\"]RacorX\nFigure 1 - RacorX\n\n RacorX (see attached resource file) displays a green color, that is applied to the quad evenly. This example demonstrate the usage of the common file framework, provided with the DirectX 8.1 SDK and how to compile vertex shaders with the D3DXAssembleShader() function.\n Like with all the upcoming examples, which are based on the Common Files, [lessthan]Alt\u0026gt;+[lessthan]Enter\u0026gt; switches between the windowed and full-screen mode, [lessthan]F2\u0026gt; gives you a selection of the usable drivers and [lessthan]Esc\u0026gt; will shutdown the application.\n First let\u0027s take a look at the files you need to compile the program: \nFigure 2- Directory Content\n\n The source file is RacorX.cpp, the resource files are winmain.rc and resource.h. The icon file is directx.ico and the executable is RacorX.exe. The remaining files are for the use of the Visual C/C++ 6 IDE.\n To compile this example, you should link it with the following *.lib files:\nd3d8.lib \nd3dx8dt.lib \ndxguid.lib \nd3dxof.lib \nwinmm.lib \ngdi32.lib \nuser32.lib \nkernel32.lib \nadvapi32.lib Most of these *.lib files are COM wrappers. The d3dx8dt.lib is the debug version of the Direct3DX static link library.[bquote]The release Direct3DX static link library is called d3dx8.lib. There is also a *.dll version of the debug build called d3dx8d.dll in the system32 directory. It is used by linking to the d3dx8d.lib COM wrapper.[/bquote] All of these *.lib files have to be included in the Object/libary modules:\u0026gt; entry field. This is located at [lessthan]Project-\u0026gt;Settings\u0026gt; and there under the [lessthan]Link\u0026gt; tab: \nFigure 3 - Project Settings\n\n The provided Visual C/C++ 6 IDE workspace references the common files in a folder with the same name: \nFigure 4 - Workspace\n\n They were added to the project with Project-\u0026gt;Add to the Project-\u0026gt;Files: \nFigure 5 - Add Files to Project\n\n[size=\"3\"]The Common Files Framework\nThe common files framework helps getting up to speed, because:\nIt helps to avoid how-tos for Direct3D in general, so that the focus of this text is the real stuff. \nIt\u0027s common and tested foundation, which helps reduce the debug time. \nAll of the Direct3D samples in the DirectX SDK use it. Learning time is very short. \nIts window mode makes debugging easier. \nSelf-developed production code could be based on the common files, so knowing them is always a win. A high-level view of the Common Files shows 14 *.cpp files in\n[font=\"Courier New\"][color=\"#000080\"] C:\\DXSDK\\samples\\Multimedia\\Common\\src[/color][/font]\n These files encapsulate the basic functionality you need to start programming a Direct3D application. The most important d3dapp.cpp contains the class CD3DApplication. It provides seven functions that can be overridden and that are used in the main *.cpp file of any project in this introduction:\nvirtual HRESULT OneTimeSceneInit() { return S_OK; } \nvirtual HRESULT InitDeviceObjects() { return S_OK; } \nvirtual HRESULT RestoreDeviceObjects() { return S_OK; } \nvirtual HRESULT DeleteDeviceObjects() { return S_OK; } \nvirtual HRESULT Render() { return S_OK; } \nvirtual HRESULT FrameMove( FLOAT ) { return S_OK; } \nvirtual HRESULT FinalCleanup() { return S_OK; } All that has to be done, to create an application based on this framework code is to create a new project and new implementations of these overridable functions in the main source file. This is also shown in all Direct3D examples in the DirectX SDK.\n RacorX uses these framework functions in RacorX.cpp. They can be called the public interface of the common files framework. \nFigure 6 - Framework Public Interface\n\n The following functions are called in the following order in racorx.cpp at startup:\nConfirmDevice() \nOneTimeSceneInit() \nInitDeviceObjects() \nRestoreDeviceObjects() Now the application is running. While it is running, the framework calls\nFrameMove() \nRender() in a loop.\n If the user resizes the window, the framework will call\nInvalidateDeviceObjects() \nRestoreDeviceObjects() If the user presses F2 or clicks [lessthan]File\u0026gt;-\u0026gt;[lessthan]Change device\u0026gt; and changes the device by choosing for example another resolution or color quality, the framework will call\nInvalidateDeviceObjects() \nDeleteDeviceObjects() \nInitDeviceObjects() \nRestoreDeviceObjects() If the user quits the application, the framework will call\nInvalidateDeviceObjects() \nDeleteDeviceObjects() \nFinalCleanup() There are matching functional pairs. InvalidateDeviceObjects() destroys what RestoreDeviceObjects() has build up and DeleteDeviceObjects() destroys what InitDeviceObjects() has build up. The FinalCleanup() function destroys what OneTimeSceneInit() build up.\n The idea is to give every functional pair its own tasks. The OneTimeSceneInit() / FinalCleanup() pair is called once at the beginning and the end of a life-cycle of the game. Both are used to load or delete data, which is not device dependant. A good candidate might be geometry data. The target of the InitDeviceObjects() / DeleteDeviceObjects() pair is, like the name implies, data that is device dependant. If the already loaded data has to be changed, when the device changes, it should be loaded here. The following examples will load, re-create or destroy their vertex buffer and index buffers and their textures in these functions.\n The InvalidateDeviceObjects() / RestoreDeviceObjects() pair has to react on changes of the window size. So for example code that handles the projection matrix might be placed here. Additionally the following examples will set most of the render states in RestoreDeviceObjects().\n Now back to RacorX. Like shown in part 1 of this introduction, the following list tracks the life-cycle of a vertex shader:\nCheck for vertex shader support by checking the D3DCAPS8::VertexShaderVersion field \nDeclaration of the vertex shader with the D3DVSD_* macros, to map vertex buffer streams to input registers \nSetting the vertex shader constant registers with SetVertexShaderConstant()\n \nCompiling an already written vertex shader with D3DXAssembleShader*() (Alternatively: could be pre-compiled using a Shader Assembler) \nCreating a vertex shader handle with CreateVertexShader()\n \nSetting a vertex shader with SetVertexShader() for a specific object \nFree vertex shader resources handled by the Direct3D engine with DeleteVertexShader() We will walk step-by-step through this list in the following pages.\n [size=\"3\"]Check for Vertex Shader Support\nThe supported vertex shader version is checked in ConfirmDevice() in racorx.cpp:HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps,\n DWORD dwBehavior,\n D3DFORMAT Format )\n{\n if( (dwBehavior \u0026amp; D3DCREATE_HARDWARE_VERTEXPROCESSING ) ||\n (dwBehavior \u0026amp; D3DCREATE_MIXED_VERTEXPROCESSING ) )\n {\n if( pCaps-\u0026gt;VertexShaderVersion \n \treturn E_FAIL;\n }\n return S_OK;\n} If the framework has already initialized hardware or mixed vertex processing, the vertex shader version will be checked. If the framework initialized software vertex processing, the software-implementation provided by Intel and AMD jumps in and a check of the hardware capabilities is not needed.\n The globally available pCaps capability data structure is filled with a call to GetDeviceCaps() by the framework. pCaps-\u0026gt;VertexShaderVersion holds the vertex shader version in a DWORD. The macro D3DVS_VERSION helps checking the version number. For example the support of at least vs.2.0 in hardware will be checked with D3DVS_VERSION(2,0).\n After checking the hardware capabilities for vertex shader support, the vertex shader has to be declared.\n [size=\"3\"]Vertex Shader Declaration\nDeclaring a vertex shader means mapping vertex data to specific vertex shader input registers, therefore the vertex shader declaration must reflect the vertex buffer layout, because the vertex buffer must transport the vertex data in the correct order. The one used in this example program is very simple. The vertex shader will get the position data via v0.// shader decl\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // D3DVSDE_POSITION,0\n D3DVSD_END()\n};\nThe corresponding layout of the vertex buffer looks like this:\n// vertex type\nstruct VERTEX\n{\n FLOAT x, y, z; // The untransformed position for the vertex\n};\n// Declare custom FVF macro.\n#define D3DFVF_VERTEX (D3DFVF_XYZ) The position values will be stored in the vertex buffer and bound through the SetStreamSource() function to a device data stream port, that feed the primitive processing functions (this is the Higher-Order Surfaces (HOS) stage or directly the vertex shader, depending on the usage of HOS; see the Direct3D pipeline in part 1).\n We do not use vertex color here, so no color values are declared.\n [size=\"3\"]Setting the Vertex Shader Constant Registers\nThe vertex shader constant registers have to be filled with a call to SetVertexShaderConstant(). We set the material color in RestoreDeviceObjects() in c8 in this example:// set material color\nFLOAT fMaterial[4] = {0,1,0,0};\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(8, fMaterial, 1); SetVertexShaderConstant() is declared like:HRESULT SetVertexShaderConstant (DWORD Register,\n \tCONST void* pConstantData,\n \tDWORD ConstantCount); The first parameter provides the number of the constant register that should be used. In this case 8. The second parameter stores the 128bit data in that constant register and the third parameter gives you the possibility to use the following registers as well. A 4x4 matrix can be stored with one SetVertexShaderConstant() call by providing the number four in ConstantCount. This is done for the clipping matrix in FrameMove():// set the clip matrix\n...\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(4, matTemp, 4); This way the c4, c5, c6 and c7 registers are used to store the matrix.\n [size=\"3\"]The Vertex Shader\nThe vertex shader that is used by RacorX is very simple:// reg c4-7 = WorldViewProj matrix\n// reg c8 = constant color\n// reg v0 = input register\nconst char BasicVertexShader[] =\n\"vs.1.1 \t//Shader version 1.1 \\n\"\\\n\"dp4 oPos.x, v0, c4 //emit projected position \\n\"\\\n\"dp4 oPos.y, v0, c5 //emit projected position \\n\"\\\n\"dp4 oPos.z, v0, c6 //emit projected position \\n\"\\\n\"dp4 oPos.w, v0, c7 //emit projected position \\n\"\\\n\"mov oD0, c8 //material color = c8 \\n\"; It is used inline in a constant char array in RacorX.cpp. This vertex shader incorporates the vs.1.1 vertex shader implementation rules. It transforms from the concatenated and transposed world-, view- and projection-matrix to the clip matrix or clip space with the four dp4 instructions and kicks out into oD0 a green material color with mov.\n As shown above, the values of the c4 - c7 constant registers are set in FrameMove(). These values are calculated by the following code snippet:// rotates the object about the y-axis\nD3DXMatrixRotationY( \u0026amp;m_matWorld, m_fTime * 1.5f );\n// set the clip matrix\nD3DXMATRIX matTemp;\nD3DXMatrixTranspose( \u0026amp;matTemp , \u0026amp;(m_matWorld * m_matView * m_matProj) );\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(4, matTemp, 4); First the quad is rotated around the y-axis by the D3DMatrixRotationY() call, then the concatenated matrix is transposed and then stored in the constant registers c4 - c7. The source of the D3DMatrixRotationY() function might look like:VOID D3DMatrixRotationY(D3DMATRIX * mat, FLOAT fRads)\n{\n D3DXMatrixIdentity(mat);\n mat._11 = cosf(fRads);\n mat._13 = -sinf(fRads);\n mat._31 = sinf(fRads);\n mat._33 = cosf(fRads);\n}\n=\ncosf(fRads) 0 -sinf(fRads) 0\n0 \t0 0 \t0\nsinf(fRads) 0 cosf(fRads) 0\n0 \t0 0 \t0 So fRads equals the amount you want to rotate about the y-axis. After changing the values of the matrix this way, we transpose the matrix by using D3DXMatrixTranspose(), so that its columns are stored as rows. Why do we have to transpose the matrix?\n A 4x4 matrix looks like this:[bquote][font=\"Courier New\"][color=\"#000080\"]a b c d\ne f g h\ni j k l\nm n o p[/color][/font][/bquote] The formula for transforming a vector (v0) through the matrix is:\n dest.x = (v0.x * a) + (v0.y * e) + (v0.z * i) + (v0.w * m) dest.y = (v0.x * b) + (v0.y * f) + (v0.z * j) + (v0.w * n) dest.z = (v0.x * c) + (v0.y * g) + (v0.z * k) + (v0.w * o) dest.w = (v0.x * d) + (v0.y * h) + (v0.z * l) + (v0.w * p) \nSo each column of the matrix should be multiplied with each component of the vector. Our vertex shader uses four dp4 instructions:dest.w = (src1.x * src2.x) + (src1.y * src2.y) + \n \t(src1.z * src2.z) + (src1.w * src2.w)\ndest.x = dest.y = dest.z = unused The dp4 instructions multiplies a row of a matrix with each component of the vector. Without transposing we would end up with:dest.x = (v0.x * a) + (v0.y * b) + (v0.z * c) + (v0.w * d)\ndest.y = (v0.x * e) + (v0.y * f) + (v0.z * g) + (v0.w * h)\ndest.z = (v0.x * i) + (v0.y * j) + (v0.z * k) + (v0.w * l)\ndest.w = (v0.x * m) + (v0.y * n) + (v0.z * o) + (v0.w * p) which is wrong. By transposing the matrix it looks like this in constant memory:[bquote][font=\"Courier New\"][color=\"#000080\"]a e i m\nb f j n\nc g k o\nd h l p[/color][/font][/bquote] so the 4 dp4 operations would now yield:dest.x = (v0.x * a) + (v0.y * e) + (v0.z * i) + (v0.w * m)\ndest.y = (v0.x * b) + (v0.y * f) + (v0.z * j) + (v0.w * n)\ndest.z = (v0.x * c) + (v0.y * g) + (v0.z * k) + (v0.w * o)\ndest.w = (v0.x * d) + (v0.y * h) + (v0.z * l) + (v0.w * p) oroPos.x = (v0.x * c4.x) + (v0.y * c4.y) + (v0.z * c4.z) + (v0.w * c4.w)\noPos.y = (v0.x * c5.x) + (v0.y * c5.y) + (v0.z * c5.z) + (v0.w * c5.w)\noPos.z = (v0.x * c6.x) + (v0.y * c6.y) + (v0.z * c6.z) + (v0.w * c6.w)\noPos.w = (v0.x * c7.x) + (v0.y * c7.y) + (v0.z * c7.z) + (v0.w * c7.w) which is exactly how the vector transformation should work.dp4 gets the matrix values via the constant register c4 - c7 and the vertex position via the input register v0. Temporary registers are not used in this example. The dot product of the dp4 instructions is written to the oPos output register and the value of the constant register c8 is moved into the output register oD0, that is usually used to output diffuse color values.\n [size=\"3\"]Compiling a Vertex Shader\nThe vertex shader that is stored in a char array is compiled with a call to the following code snippet in RestoreDeviceObjects():// Assemble the shader\nrc = D3DXAssembleShader( BasicVertexShader , sizeof(BasicVertexShader) -1,\n \t0 , NULL , \u0026amp;pVS , \u0026amp;pErrors );\nif ( FAILED(rc) )\n{\n OutputDebugString( \"Failed to assemble the vertex shader, errors:\\n\" );\n OutputDebugString( (char*)pErrors-\u0026gt;GetBufferPointer() );\n OutputDebugString( \"\\n\" );\n} D3DXAssembleShader() creates a binary version of the shader in a buffer object via the ID3DXBuffer interface in pVS.HRESULT D3DXAssembleShader(\n LPCVOID pSrcData,\n UINT SrcDataLen,\n DWORD Flags,\n LPD3DXBUFFER* ppConstants,\n LPD3DXBUFFER* ppCompiledShader,\n LPD3DXBUFFER* ppCompilationErrors\n); The source data is provided in the first parameter and the size of the data length in bytes is provided in the second parameter. There are two possible flags for the third parameter called#define D3DXASM_DEBUG 1\n#define D3DXASM_SKIPVALIDATION 2 The first one inserts debug info as comments into the shader and the second one skips validation. This flag can be set for a working shader.\n Via the fourth parameter a ID3DXBuffer interface can be exported, to get a vertex shader declaration fragment of the constants. To ignore this parameter, it is set to NULL here. In case of an error, the error explanation would be stored in a buffer object via the ID3DXBuffer interface in pErrors. To see the output of OutputDebugString() the debug process in the Visual C/C++ IDE must be started with [lessthan]F5\u0026gt;.\n [size=\"3\"]Creating a Vertex Shader\nThe vertex shader is validated and a handle for it is retrieved via a call to CreateVertexShader() in m_dwVertexShader: The following lines of code can be found in RestoreDeviceObjects():// Create the vertex shader\nrc = m_pd3dDevice-\u0026gt;CreateVertexShader( dwDecl, (DWORD*)pVS-\u0026gt;GetBufferPointer(),\n \u0026amp;m_dwVertexShader, 0 );\nif ( FAILED(rc) )\n{\n OutputDebugString( \"Failed to create the vertex shader, errors:\\n\" );\n D3DXGetErrorStringA(rc,szBuffer,sizeof(szBuffer));\n OutputDebugString( szBuffer );\n OutputDebugString( \"\\n\" );\n} CreateVertexShader() gets a pointer to the buffer with the binary version of the vertex shader via the ID3DXBuffer interface. This function gets the vertex shader declaration via dwDecl, that maps vertex data to specific vertex shader input registers. If an error occurs, its explanation is accessible via a pointer to a buffer object that is retrieved via the ID3DXBuffer interface in pVS-\u0026gt;GetBufferPointer(). D3DXGetErrorStringA() interprets all Direct3D and Direct3DX HRESULTS and returns an error message in szBuffer.\n It is possible to force the usage of software vertex processing with the last parameter by using the D3DUSAGE_SOFTWAREPROCESSING flag. It must be used when the D3DRS_SOFTWAREVERTEXPROCESSING member of the D3DRENDERSTATETYPE enumerated type is TRUE.\n [size=\"3\"]Setting a Vertex Shader\nThe vertex shader is set via SetVertexShader() in the Render() function:// set the vertex shader\nm_pd3dDevice-\u0026gt;SetVertexShader( m_dwVertexShader ); The only parameter that must be provided is the handle to the vertex shader. This function executes the vertex shader as often as there are vertices.\n [size=\"3\"]Free Vertex Shader Resources\nVertex shader resources must be freed with a call toif ( m_dwVertexShader != 0xffffffff )\n{\n m_pd3dDevice-\u0026gt;DeleteVertexShader( m_dwVertexShader );\n m_dwVertexShader = 0xffffffff;\n} This example frees the vertex shader resources in the InvalidateDeviceObjects() framework function, because this has to happen in case of a change of the window size or a device.\n [size=\"3\"]Non-Shader specific Code\nThe non-shader specific code of RacorX deals with setting render states and the handling of the vertex and index buffer. A few render states have to be set in RestoreDeviceObjects():// z-buffer enabled\nm_pd3dDevice-\u0026gt;SetRenderState( D3DRS_ZENABLE, TRUE );\n// Turn off D3D lighting, since we are providing our own vertex shader lighting\nm_pd3dDevice-\u0026gt;SetRenderState( D3DRS_LIGHTING, FALSE );\n// Turn off culling, so we see the front and back of the quad\nm_pd3dDevice-\u0026gt;SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); The first instructions enables the z-buffer (a corresponded flag has to be set in the constructor of the Direct3D framework class, so that the device is created with a z-buffer).\n The fixed-function lighting is not needed, so it is switched off with the second statement. To be able to see both sides of the quad, backface culling is switched off with the third statement.\n The vertex and index buffer is created in InitDeviceObjects():// create and fill the vertex buffer\n// Initialize vertices for rendering a quad\nVERTEX Vertices[] =\n{\n// x y z\n{ -1.0f,-1.0f, 0.0f, },\n{ 1.0f,-1.0f, 0.0f, },\n{ 1.0f, 1.0f, 0.0f, },\n{ -1.0f, 1.0f, 0.0f, },\n};\nm_dwSizeofVertices = sizeof (Vertices);\n// Create the vertex buffers with four vertices\nif( FAILED( m_pd3dDevice-\u0026gt;CreateVertexBuffer( 4 * sizeof(VERTEX),\n \tD3DUSAGE_WRITEONLY , sizeof(VERTEX), D3DPOOL_MANAGED, \u0026amp;m_pVB ) ) )\n return E_FAIL;\n// lock and unlock the vertex buffer to fill it with memcpy\nVOID* pVertices;\nif( FAILED( m_pVB-\u0026gt;Lock( 0, m_dwSizeofVertices, (BYTE**)\u0026amp;pVertices, 0 ) ) )\n return E_FAIL;\nmemcpy( pVertices, Vertices, m_dwSizeofVertices);\nm_pVB-\u0026gt;Unlock();\n// create and fill the index buffer\n// indices\nWORD wIndices[]={0, 1, 2, 0, 2, 3};\nm_wSizeofIndices = sizeof (wIndices);\n// create index buffer\nif(FAILED (m_pd3dDevice-\u0026gt;CreateIndexBuffer(m_wSizeofIndices, 0,\n \tD3DFMT_INDEX16, D3DPOOL_MANAGED, \u0026amp;m_pIB)))\n return E_FAIL;\n// fill index buffer\nVOID *pIndices;\nif (FAILED(m_pIB-\u0026gt;Lock(0, m_wSizeofIndices, (BYTE **)\u0026amp;pIndices, 0)))\n return E_FAIL;\nmemcpy(pIndices, wIndices, m_wSizeofIndices);\nm_pIB-\u0026gt;Unlock(); The four vertices of the quad are stored in a VERTEX structure, which holds for each vertex three FLOAT values for the position.\n By using the flag D3DFMT_INDEX16 in CreateIndexBuffer(), 16-bit variables are used to store the indices into the wIndices structure. So the maximum number of available indices are 64 k. Both buffers use a managed memory pool with D3DPOOL_MANAGED, so they will be cached in the system memory.[bquote] D3DPOOL_MANAGED resources are read from the system memory which is quite fast and they are written to the system memory and afterwards uploaded to wherever the non-system copy has to go (AGP or VIDEO memory). This upload happens when the resource is unlocked. So there are always two copies of a resource, one in the system and one in the AGP or VIDEO memory. This is a less efficient but bullet-proof way. It works for any class of driver and must be used with unified memory architecture boards. Handling resources with D3DPOOL_DEFAULT is more efficient. In this case the driver will choose the best place for the resource.\n Why do we use a vertex buffer at all ? The vertex buffer can be stored in the memory of your graphic card or AGP, where it can be accessed very quickly by 3-D hardware. So a copy between system memory and the graphic card/AGP memory could be avoided. This is important for hardware that accelerates transformation and lighting. Without vertex buffers a lot of bus-traffic would happen by transforming and lighting the vertices.\n Why do we use an index buffer ? You will get the maximum performance when you reduce the duplication in vertices transformed and sent across the bus to the rendering device. A nonindexed triangle list for example achieves no vertex sharing, so it\u0027s the least optimal method, because DrawPrimitive*() is called several times. Using indexed lists or strips reduce the call overhead of DrawPrimitive*() methods (Reducing DrawPrimitve*() methods is also called batching) and because of the reduction of vertices to send through the bus, it saves memory bandwidth. Indexed strips are more hardware-cache friendly on newer hardware than indexed lists. The performance of index processing operations depends heavily on where the index buffer exists in memory. At the time of this writing, the only graphic cards that supports index buffers in hardware are the RADEON 8x00 series.[/bquote]\n [size=\"3\"]Summarize\nRacorX shows a simple vertex shader together with its infrastructure. The shader is inlined in racorx.cpp and compiled with D3DXAssembleShader(). It uses four dp4 instructions for the transformation of the quad and only one material color.\n The upcoming examples are build on this example and only the functional additions will be shown on the next pages.\n[size=\"5\"]RacorX2\nThe main difference between RacorX and RacorX2 (see attached resource file) is the compilation of the vertex shader with NVASM. Whereas the first example compiles the vertex shader with D3DXAssembleShader() while the application starts up, RacorX2 uses a pre-compiled vertex shader.\n To add the NVIDIA vertex and pixel shader assembler, you have to do the following steps:\nCreate a directory, for example [lessthan]C:\\NVASM\u0026gt;: and unzip nvasm.exe and the documentation into it \nShow your Visual C++ IDE the path to this exe with\n[lessthan]Tools-\u0026gt;Options-\u0026gt;Directories\u0026gt;\nand choose from the drop down menu [lessthan]Show directories for:\u0026gt;\n[lessthan]Executable files\u0026gt;\nAdd the path to NVASM\n [lessthan]C:\\NVASM\u0026gt;\nNow the dialog box should look like this: \nFigure 7 - Integrating NVASM into Visual C/C++\n \nAdditionally you have to tell every vertex shader file in the IDE, that it has to be compiled with NVASM. The easiest way to do that is looking into the example RacorX2. Just fire up your Visual C++ IDE by clicking on the [lessthan]RacorX.dsp\u0026gt; in the RacorX2 directory. Click on the [lessthan]FileView\u0026gt; tab of the Workspace dialog and there on [lessthan]Shaders\u0026gt; to view the available shader files. A right-click on the file [lessthan]basic.vsh\u0026gt; should show you a popup. Click on [lessthan]Settings...\u0026gt;. The project settings of your project might look like: \nFigure 8 - Custom Build Options Vertex Shader Files\n \nThe entry in the entry field called \"Commands\" is:\nnvasm.exe $(InputPath) shaders\\$(InputName).vso\nFor the entry field named \"Outputs\", I use the input name as the name for the output file with an *.vso extension. The output directory should be the shaders directory. ShaderX author Kenneth Hurley is the author of NVASM. Read more in his paper [Hurley]. The output of NVASM in the build window of your Visual C/C++ IDE should look like this: \nFigure 9 - NVASM Output\n\n The vertex shader is provided in its own ASCII file called basic.vsh. After compilation a binary object file with the name basic.vso is created in the directory [lessthan]shaders\u0026gt;: \nFigure 10 - Directory Content RacorX2\n\n [size=\"3\"]Creating a Vertex Shader\nBecause of the already compiled vertex shader, the creation of the vertex shader has to be done in a different way than in the previous example. This happens in the InitDeviceObjects() function://shader decl\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register 0\n D3DVSD_END()\n};\n// loads a *.vso binary file, already compiled with NVASM and\n// creates a vertex shader\nif (FAILED(CreateVSFromCompiledFile (m_pd3dDevice, dwDecl,\"shaders/basic.vso\",\n \t\u0026amp;m_dwVertexShader)))\n return E_FAIL; CreateVSFromCompiledFile() opens and reads in the binary vertex shader file and creates a vertex shader. The source of this function can be found at the end of the file racorx.cpp in the RacorX2 directory://----------------------------------------------------------------------------\n// Name: CreateVSFromBinFile\n// Desc: loads a binary *.vso file that was compiled by NVASM\n// and creates a vertex shader\n//----------------------------------------------------------------------------\nHRESULT CMyD3DApplication::CreateVSFromCompiledFile (IDirect3DDevice8* m_pd3dDevice,\n \tDWORD* dwDeclaration,\n \tTCHAR* strVSPath,\n \tDWORD* m_dwVS)\n{\n char szBuffer[128]; // debug output\n DWORD* dwpVS; // pointer to address space of the calling process\n HANDLE hFile, hMap; // handle file and handle mapped file\n TCHAR tempVSPath[512]; // temporary file path\n HRESULT hr; // error\nif( FAILED( hr = DXUtil_FindMediaFile( tempVSPath, strVSPath ) ) )\n return D3DAPPERR_MEDIANOTFOUND;\nhFile = CreateFile(tempVSPath, GENERIC_READ,0,0,OPEN_EXISTING,\n \tFILE_ATTRIBUTE_NORMAL,0);\nif(hFile != INVALID_HANDLE_VALUE)\n{\n if(GetFileSize(hFile,0) \u0026gt; 0)\n hMap = CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0);\n else\n {\n CloseHandle(hFile);\n return E_FAIL;\n }\n}\nelse\n return E_FAIL;\n// maps a view of a file into the address space of the calling process\ndwpVS = (DWORD *)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);\n// Create the vertex shader\nhr = m_pd3dDevice-\u0026gt;CreateVertexShader( dwDeclaration, dwpVS, m_dwVS, 0 );\nif ( FAILED(hr) )\n{\n OutputDebugString( \"Failed to create Vertex Shader, errors:\\n\" );\n D3DXGetErrorStringA(hr,szBuffer,sizeof(szBuffer));\n OutputDebugString( szBuffer );\n OutputDebugString( \"\\n\" );\nreturn hr;\n}\n UnmapViewOfFile(dwpVS);\n CloseHandle(hMap);\n CloseHandle(hFile);\n return S_OK;\n} DXUtil_FindMediaFile(), a helper function located in the framework file dxutil.cpp, returns the path to the already compiled vertex shader file. CreateFile() opens and reads the existing file:HANDLE CreateFile(\n LPCTSTR lpFileName, // file name\n DWORD dwDesiredAccess, // access mode\n DWORD dwShareMode, // share mode\n LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD\n DWORD dwCreationDisposition, // how to create\n DWORD dwFlagsAndAttributes, // file attributes\n HANDLE hTemplateFile // handle to template file\n); Its first parameter is the path to the file. The flag GENERIC_READ specifies read access to the file in the second parameter. The following two parameters are not used, because file sharing should not happen and the fourth parameter is not used, because the file should not be inherited by a child process. The fifth parameter is set to OPEN_EXISTING. This way, the function fails if the file does not exist. Setting the sixt parameter to FILE_ATTRIBUTE_NORMAL indicates, that the file has no other attributes. A template file is not used here, so the last parameter is set to 0. Please consult the Platform SDK help file for more information.CreateFileMapping() creates or opens a named or unnamed file-mapping object for the specified file:HANDLE CreateFileMapping(\n HANDLE hFile, // handle to file\n LPSECURITY_ATTRIBUTES lpAttributes, // security\n DWORD flProtect, // protection\n DWORD dwMaximumSizeHigh, // high-order DWORD of size\n DWORD dwMaximumSizeLow, // low-order DWORD of size\n LPCTSTR lpName // object name\n); The first parameter is a handle to the file from which to create a mapping object. The file must be opened with an access mode compatible with the protection flags specified by the flProtect parameter. We have opened the file in CreateFile() with GENERIC_READ, therefore we use PAGE_READONLY here. Other features of CreateFileMapping() are not needed, therefore we set the rest of the parameters to 0.MapViewOfFile() function maps a view of a file into the address space of the calling process:LPVOID MapViewOfFile(\n HANDLE hFileMappingObject, // handle to file-mapping object \n DWORD dwDesiredAccess, // access mode\n DWORD dwFileOffsetHigh, // high-order DWORD of offset\n DWORD dwFileOffsetLow, // low-order DWORD of offset\n SIZE_T dwNumberOfBytesToMap // number of bytes to map\n); This function only gets the handle to the file-mapping object from CreateFileMapping() and in the second parameter the access mode FILE_MAP_READ. The access mode parameter specifies the type of access to the file view and, therefore, the protection of the pages mapped by the file. More features are not needed, therefore the rest of the parameters are set to 0.CreateVertexShader() is used to create and validate a vertex shader. It takes the vertex shader declaration (which maps vertex buffer streams to different vertex input registers) in its first parameter as a pointer and returns the shader handle in the third parameter. The second parameter gets the vertex shader instructions of the binary code pre-compiled by a vertex shader assembler. With the fourth parameter you can force software vertex processing with D3DUSAGE_SOFTWAREPROCESSING.\n As in the previous example OutputDebugString() shows the complete error message in the output debug window of the Visual C/C++ IDE and D3DXGetErrorStringA() interprets all Direct3D and Direct3DX HRESULTS and returns an error message in szBuffer.\n [size=\"3\"]Summarize\nThis example showed the integration of NVASM to pre-compile a vertex shader and how to open and read a binary vertex shader file.\n[size=\"5\"]RacorX3\nThe main improvement of RacorX3 (see attached resource file) over RacorX2 is the addition of a per-vertex diffuse reflection model in the vertex shader. This is one of the simplest lighting calculations, which outputs the color based on the dot product of the vertex normal with the light vector.\n RacorX3 uses a light positioned at (0.0, 0.0, 1.0) and a green color. \nFigure 11 - RacorX3\n\n As usual we are tracking the life-cycle of the vertex shader.\n [size=\"3\"]Vertex Shader Declaration\nThe vertex shader declaration has to map vertex data to specific vertex shader input registers. Additionally to the previous examples, we need to map a normal vector to the input register v3:// vertex shader declaration\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register #1\n D3DVSD_REG(3, D3DVSDT_FLOAT3 ), // normal in input register #4\n D3DVSD_END()\n}; The corresponding layout of the vertex buffer looks like this:struct VERTICES\n{\n FLOAT x, y, z; // The untransformed position for the vertex\n FLOAT nx, ny, nz; // the normal\n};\n// Declare custom FVF macro.\n#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL) Each vertex consists of three position floating point values and three normal floating point values in the vertex buffer. The vertex shader gets the position and normal values from the vertex buffer via v0 and v3.\n [size=\"3\"]Setting the Vertex Shader Constant Registers\nThe vertex shader constants are set in FrameMove() and RestoreDeviceObjects(). This example uses a more elegant way to handle the constant registers. The file const.h that is included in racorx.cpp and diffuse.vsh, gives the constant registers an easier to remember name:#define CLIP_MATRIX 0\n#define CLIP_MATRIX_1 1\n#define CLIP_MATRIX_2 2\n#define CLIP_MATRIX_3 3\n#define INVERSE_WORLD_MATRIX 4\n#define INVERSE_WORLD_MATRIX_1 5\n#define INVERSE_WORLD_MATRIX_2 6\n#define LIGHT_POSITION 11\n#define DIFFUSE_COLOR 14\n#define LIGHT_COLOR 15 In FrameMove() a clipping matrix and an inversed world matrix are set into the constant registers:HRESULT CMyD3DApplication::FrameMove()\n{\n // rotates the object about the y-axis\n D3DXMatrixRotationY( \u0026amp;m_matWorld, m_fTime * 1.5f );\n // set the clip matrix\n m_pd3dDevice-\u0026gt;SetVertexShaderConstant(CLIP_MATRIX, \u0026amp;(m_matWorld *\n m_matView * m_matProj), 4);\n D3DXMATRIX matWorldInverse;\n D3DXMatrixInverse(\u0026amp;matWorldInverse, NULL, \u0026amp;m_matWorld);\n m_pd3dDevice-\u0026gt;SetVertexShaderConstant(INVERSE_WORLD_MATRIX,\n \u0026amp;matWorldInverse,3);\n return S_OK;\n} Contrary to the previous examples, the concatenated world-, view- and projection matrix, which is used to rotate the quad, is not transposed here. This is because the matrix will be transposed in the vertex shader as shown below.\n To transform the normal, an inverse 4x3 matrix is send to the vertex shader via c4 -c6.\n [size=\"3\"]The Vertex Shader\nThe vertex shader is a little bit more complex, than the one used in the previous examples:; per-vertex diffuse lighting\n#include \"const.h\"\nvs.1.1\n; transpose and transform to clip space\nmul r0, v0.x, c[CLIP_MATRIX]\nmad r0, v0.y, c[CLIP_MATRIX_1], r0\nmad r0, v0.z, c[CLIP_MATRIX_2], r0\nadd oPos, c[CLIP_MATRIX_3], r0\n; transform normal\ndp3 r1.x, v3, c[INVERSE_WORLD_MATRIX]\ndp3 r1.y, v3, c[INVERSE_WORLD_MATRIX_1]\ndp3 r1.z, v3, c[INVERSE_WORLD_MATRIX_2]\n; renormalize it\ndp3 r1.w, r1, r1\nrsq r1.w, r1.w\nmul r1, r1, r1.w\n; N dot L\n; we need L vector towards the light, thus negate sign\ndp3 r0, r1, -c[LIGHT_POSITION]\nmul r0, r0, c[LIGHT_COLOR] ; modulate against light color\nmul oD0, r0, c[DIFFUSE_COLOR] ; modulate against material The mul, mad and add instructions transpose and transform the matrix provided in c0 - c3 to clip space. As such they are nearly functionally equivalent to the transposition of the matrix and the four dp4 instructions shown in the previous examples. There are two caveats to bear in mind: The complex matrix instructions like m4x4 might be faster in software emulation mode and v0.w is not used here. oPos.w is automatically filled with 1. These instructions should save the CPU cycles used for transposing.\n The normals are transformed in the following three dp3 instructions and then renormalized with the dp3, rsq and mul instructions.\n You can think of a normal transform in the following way: Normal vectors (unlike position vectors) are simply directions in space, and as such they should not get squished in magnitude, and translation doesn\u0027t change their direction. They should simply be rotated in some fashion to reflect the change in orientation of the surface. This change in orientation is a result of rotating and squishing the object, but not moving it. The information for rotating a normal can be extracted from the 4x4 transformation matrix by doing transpose and inversion. A more math-related explanation is given in [Haines/M?ller][Turkowski].\n So the bullet-proof way to use normals, is to transform the transpose of the inverse of the matrix, that is used to transform the object. If the matrix used to transform the object is called M, then we must use the matrix, N, below to transform the normals of this object.[bquote][font=\"Courier New\"][color=\"#000080\"]N = transpose( inverse(M) )[/color][/font]\nThe normal can be transformed with the transformation matrix (usually the world matrix), that is used to transform the object in the following cases:\nMatrix formed from rotations (orthogonal matrix), because the inverse of an orthogonal matrix is its transpose \nMatrix formed from rotations and translation (rigid-body transforms), because translations do not affect vector direction \n\nMatrix formed from rotations and translation and uniform scalings, because such scalings affect only the length of the transformed normal, not its direction. A uniform scaling is simply a matrix which uniformly increases or decreases the object\u0027s size, vs. a non-uniform scaling, which can stretch or squeeze an object. If uniform scalings are used, then the normals do have to be renormalized. Therefore using the world matrix would be sufficient in this example.[/bquote] \n\n That\u0027s exactly, what the source is doing. The inverse world matrix is delivered to the vertex shader via c4 - c6. The dp3 instruction handles the matrix in a similar way as dp4.\n By multiplying a matrix with a vector, each column of the matrix should be multiplied with each component of the vector. dp3 and dp4 are only capable to multiply each row of the matrix with each component of the vector. In case of the position data, the matrix is transposed to get the right results.\n In case of the normals, no transposition is done. So dp3 calculates the dot product by multiplying the rows of the matrix with the components of the vector. This is like using a transposed matrix.\n The normal is re-normalized with the dp3, rsq and mul instructions. Re-normalizing a vector means align its length to 1. That\u0027s because we need a unit vector to calculate our diffuse lighting effect.\n To calculate a unit vector, divide the vector by its magnitude or length. The magnitude of vectors is calculated by using the Pythagorean theorem:\n x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup] = m[sup]2[/sup]\n The length of the vector is retrieved by\n ||A|| = sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])\n The magnitude of a vector has a special symbol in mathematics. It is a capital letter designated with two vertical bars: ||A||. So dividing the vector by its magnitude is:\n UnitVector = Vector / sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])\n The lines of code in the vertex shader, that handles the calculation of the unit vector looks like this:; renormalize it\ndp3 r1.w, r1, r1 ; (src1.x * src2.x) + (src1.y * src2.y) + (src1.z * src2.z)\nrsq r1.w, r1.w ; if (v != 0 \u0026amp;\u0026amp; v != 1.0) v = (float)(1.0f / sqrt(v))\nmul r1, r1, r1.w ; r1 * r1.w dp3 squares the x, y and z components of the temporary register r1, adds them and returns the result in r1.w. rsq divides 1 by the result in r1.w and stores the result in r1.w. mul multiplies all components of r1 with r1.w. Afterwards, the result in r1.w is not used anymore in the vertex shader.\n The underlying calculation of these three instructions can be represented by the following formula, which is mostly identical to the formula postualted above:\n UnitVector = Vector * 1/sqrt(x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup])\n Lighting is calculated with the following three instruction:dp3 r0, r1, -c[LIGHT_POSITION]\nmul r0, r0, c[LIGHT_COLOR] ; modulate against light color\nmul oD0, r0, c[DIFFUSE_COLOR] ; modulate against diffuse color Nowadays the lighting models used in current games are not based on much physical theory. Game programmers use approximations that try to simulate the way photons are reflected from objects in a rough but efficient manner.\n One differentiates usually between different kind of light sources and different reflection models. The common lighting sources are called directional, point light and spotlight. The most common reflections models are ambient, diffuse and specular lighting.\n This example uses a directional light source with an ambient and a diffuse reflection model.Directional Light\n RacorX3 uses a light source in an infinite distance. This simulates the long distance the light beams have to travel from the sun. We treat this light beams as beeing parallel. This kind of light source is called directional light source.Diffuse Reflection\n Whereas ambient light is considered to be uniform from any direction, diffuse light simulates the emission of an object by a particular light source. Therefore you are able to see that light falls onto the surface of an object from a particular direction by using the diffuse lighting model.\n It is based on the assumption that light is reflected equally well in all directions, so the appearance of the reflection does not depend on the position of the observer. The intensity of the light reflected in any direction depends only on how much light falls onto the surface.\n If the surface of the object is facing the light source, which means is perpendicular to the direction of the light, the density of the incident light is the highest. If the surface is facing the light source under some angle smaller than 90 degrees, the density is proportionally smaller.\n The diffuse reflection model is based on a law of physics called Lambert\u0027s Law, which states that for ideally diffuse (totally matte) surfaces, the reflected light is determined by the cosine between the surface normal N and the light vector L. \nFigure 12 - Diffuse Lighting\n\n The left figure shows a geometric interpretation of Lambert\u0027s Law (see also [RTR]). The middle figure shows the light rays hitting the surface perpendicularly in a distance d apart. The intensity of the light is related to this distance. It decreases as d becomes greater. This is shown by the right figure. The light rays make an angle ? with the normal of the plane. This illustrates that the same amount of light that passes through one side of a right-angle triangle is reflected from the region of the surface corresponding to the triangles hypotenuse. Due to the relationships that hold in a right-angle triangle, the length of the hypotenuse is d/cos ? of the length of the considered side. Thus you can deduce that if the intensity of the incident light is Idirected, the amount of light reflected from a unit surface is Idirected cos ?. Adjusting this with a coefficient that describes reflection properties of the matter leads to the following equation (see also [Savchenko]):\n Ireflected = Cdiffuse * Idirected cos ?\n This equation demonstrates that the reflection is at its peak for surfaces that are perpendicular to the direction of light and diminishes for smaller angles, because the cosinus value is very large. The light is obscured by the surface if the angles is more than 180 or less than 0 degrees, because the cosinus value is small. You will obtain negative intensity of the reflected light, which will be clamped by the output registers.\n In an implementation of this model, you have to find a way to compute cos ?. By definition the dot or scalar product of the light and normal vector can be expressed as\n N dot L = ||N|| ||L||cos ?\n where ||N|| and ||L|| are the lengths of the vectors. If both vectors are unit length, you can compute cos ? as the scalar or dot product of the light and normal vector. Thus the expression is\n Ireflected = Cdiffuse * Idirected(N dot L)\n So (N dot L) is the same as the cosine of the angle between N and L, therefore as the angle decrease, the resulting diffuse value is higher. This is exactly what the dp3 instruction and the first mul instruction are doing. Here is the source with the relevant part of constant.h:#define LIGHT_POSITION 11\n#define MATERIAL_COLOR 14\n-----\ndp3 r0, r1, -c[LIGHT_POSITION]\nmul r0, r0, c[LIGHT_COLOR] ; modulate against light color\nmul oD0, r0, c[DIFFUSE_COLOR] ; modulate against material So the vertex shader registers are involved in the following way:r0 = (r1 dot -c11) * c14 This example modulates additionally against the blue light color in c15:r0 = (c15 * (r1 dot -c11)) * c14 [size=\"3\"]Summarize\nRacorX3 shows the usage of an include file to give constants a name that can be remembered in a better way. It shows how to normalize vectors and it just strive the problem of transforming normals, but shows a bullet-proof method to do it.\n The example introduces an optimization technique, that eliminates the need to transpose the clip space matrix with the help of the CPU and it shows the usage of a simple diffuse reflection model, that lights the quad on a per-vertex basis.\n[size=\"5\"]RacorX4\nRacorX4 (see attached resource file) has gotten a few additional features compared to RacorX3. First of all this example will not use a plain quad anymore, it uses instead a B?zier patch class, that shows a round surface with a texture attached to it. To simulate light reflections, a combined diffuse and specular reflection model is used. \nFigure 13 - RacorX4\n\n RacorX4 uses the trackball class provided with the Common files framework, to rotate and move the object. You can choose different specular colors with the [lessthan]C\u0026gt; key and zoom in and out with the mouse wheel.\n [size=\"3\"]Vertex Shader Declaration\nCompared to RacorX3, additionally the texture coordinates will be mapped to input register v7.// shader decl\nDWORD dwDecl[] =\n{\n D3DVSD_STREAM(0),\n D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // input register v0\n D3DVSD_REG(3, D3DVSDT_FLOAT3 ), // normal in input register v3\n D3DVSD_REG(7, D3DVSDT_FLOAT2), // tex coordinates\n D3DVSD_END()\n}; The corresponding layout of the vertex buffer in BPatch.h looks like:struct VERTICES {\n D3DXVECTOR3 vPosition;\n D3DXVECTOR3 vNormal;\n D3DXVECTOR2 uv;\n};\n// Declare custom FVF macro.\n#define D3DFVF_VERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1) The third flag used in the custom FVF macro indicates the usage of one texture coordinate pair. This macro is provided to CreateVertexBuffer(). The vertex shader gets the position values from the vertex buffer via v0, the normal values via v3 and the two texture coordinates via v7.\n [size=\"3\"]Setting the Vertex Shader Constants\nThe vertex shader constants are set in FrameMove() and RestoreDeviceObjects(). The file const.h holds the following defines:#define CLIP_MATRIX 0\n#define CLIP_MATRIX_1 1\n#define CLIP_MATRIX_2 2\n#define CLIP_MATRIX_3 3\n#define INVERSE_WORLD_MATRIX 8\n#define INVERSE_WORLD_MATRIX_1 9\n#define INVERSE_WORLD_MATRIX_2 10\n#define LIGHT_VECTOR 11\n#define EYE_VECTOR 12\n#define SPEC_POWER 13\n#define SPEC_COLOR 14 In FrameMove() a clipping matrix, the inversed world matrix, an eye vector and a specular color are set:// set the clip matrix\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(CLIP_MATRIX,\n \t\u0026amp;(m_matWorld * m_matView * m_matProj),4);\n// set the world inverse matrix\nD3DXMATRIX matWorldInverse;\nD3DXMatrixInverse(\u0026amp;matWorldInverse, NULL, \u0026amp;m_matWorld);\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(INVERSE_WORLD_MATRIX, matWorldInverse,3);\n// stuff for specular lighting\n// set eye vector E\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(EYE_VECTOR, vEyePt,1);\n// specular color\nif(m_bKey[\u0027C\u0027])\n{\n m_bKey[\u0027C\u0027]=0;\n ++m_dwCurrentColor;\n if(m_dwCurrentColor \u0026gt;= 3)\n m_dwCurrentColor=0;\n}\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(SPEC_COLOR, m_vLightColor[m_dwCurrentColor], 1); Like in the previous example the concatenated world-, view- and projection matrix are set into c0 - c3, to get transposed in the vertex shader and the inverse 4x3 world matrix is send to vertex shader, to transform the normal.\n The eye vector (EYE_VECTOR) that is used to build up the view matrix is stored in constant register c12. As shown below, this vector is helpful to build up the specular reflection model used in this upcoming examples.\n The user can pick one of the following specular colors with the [lessthan]C\u0026gt; key:m_vLightColor[0]=D3DXVECTOR4(0.3f,0.1f,0.1f,1.0f);\nm_vLightColor[1]=D3DXVECTOR4(0.1f,0.5f,0.1f,1.0f);\nm_vLightColor[2]=D3DXVECTOR4(0.0f,0.1f,0.4f,1.0f); In RestoreDeviceObjects() the specular power, the light vector and the diffuse color are set:// specular power\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(SPEC_POWER, D3DXVECTOR4(0,10,25,50),1);\n// light direction\nD3DXVECTOR3 vLight(0,0,1);\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(LIGHT_VECTOR, vLight,1);\nD3DXCOLOR matDiffuse(0.9f, 0.9f, 0.9f, 1.0f);\nm_pd3dDevice-\u0026gt;SetVertexShaderConstant(DIFFUSE_COLOR, \u0026amp;matDiffuse, 1); As in the previous examples, the light is positioned at (0.0, 0.0, 1.0). There are four specular power values, from which one will be used in the vertex shader.[bquote]To optimize the usage of SetVertexShaderConstant(), the specular color could be set into the fourth component of the light vector, which only used three of its components.[/bquote] [size=\"3\"]The Vertex Shader\nThe vertex shader handles a combined diffuse and specular reflection model:; diffuse and specular vertex lighting\n#include \"const.h\"\nvs.1.1\n; transpose and transform to clip space\nmul r0, v0.x, c[CLIP_MATRIX]\nmad r0, v0.y, c[CLIP_MATRIX_1], r0\nmad r0, v0.z, c[CLIP_MATRIX_2], r0\nadd oPos, c[CLIP_MATRIX_3], r0\n; output texture coords\nmov oT0, v7\n; transform normal\ndp3 r1.x, v3, c[INVERSE_WORLD_MATRIX]\ndp3 r1.y, v3, c[INVERSE_WORLD_MATRIX_1]\ndp3 r1.z, v3, c[INVERSE_WORLD_MATRIX_2]\n; renormalize it\ndp3 r1.w, r1, r1\nrsq r1.w, r1.w\nmul r1, r1, r1.w\n; light vector L\n; we need L towards the light, thus negate sign\nmov r5, -c[LIGHT_VECTOR]\n; N dot L\ndp3 r0.x, r1, r5\n; compute normalized half vector H = L + V\nadd r2, c[EYE_VECTOR], r5 ; L + V\n; renormalize H\ndp3 r2.w, r2, r2\nrsq r2.w, r2.w\nmul r2, r2, r2.w\n; N dot H\ndp3 r0.y, r1, r2\n; compute specular and clamp values (lit)\n; r0.x - N dot L\n; r0.y - N dot H\n; r0.w - specular power n\nmov r0.w, c[SPEC_POWER].y ; n must be between -128.0 and 128.0\nlit r4, r0\n; diffuse color * diffuse intensity(r4.y)\nmul oD0, c[DIFFUSE_COLOR], r4.y\n; specular color * specular intensity(r4.z)\nmul oD1, c[SPEC_COLOR], r4.z Compared to the vertex shader in the previous example program, this shader maps the texture coordinates to texture stage 0 with the following instruction:; output texture coords\nmov oT0, v7 The corresponding texture stage states set for the shading of the pixels in the multitexturing unit will be shown below in the section named \"Non-Shader specific code\". The instructions that transform the normal and calculate the diffuse reflection were already discussed along with the previous example.\n The real new functionality in this shader is the calculation of the specular reflection, which happens in the code lines starting with the add instruction.Specular Reflection\n Compared to the diffuse reflection model, the appearance of the reflection depends in the specular reflection model on the position of the viewer. When the direction of the viewing coincides, or nearly coincides, with the direction of specular reflection, a bright highlight is observed. This simulates the reflection of a light source by a smooth, shiny and polished surface.\n To describe reflection from shiny surfaces an approximation is commonly used, which is called the Phong illumination model (not to be confused with Pong Shading), named after its creator Phong Bui Tong [Foley]. According to this model, a specular highlight is seen when the viewer is close to the direction of reflection. The intensity of light falls off sharply when the viewer moves away from the direction of the specular reflection. \nFigure 14 - Vectors for Specular Reflection\n\n A model describing this effect has to be aware of at least the location of the light source L, the location of the viewer V, and the orientation of the surface N. Additionally a vector R that describes the direction of the reflection might be useful. The half way vector H, that halfs the angle between L and V will be introduced below.\n The original Phong formula approximates the falloff of the intensity. It looks like this:\n kspecular cos[sup]n[/sup](ss)\n where kspecular is a scalar coefficient showing the percentage of the incident light reflected. ss describes the angle between R and V. The exponent n characterizes the shiny properties of the surface and ranges from one to infinity. Objects that are matte require a small exponent, since they produce a large, dim, specular highlight with a gentle falloff. Shiny surfaces should have a sharp highlight that is modeled by a very large exponent, making the intensity falloff very steep.\n Together with the diffuse reflection model shown above, the Phong illumination model can be expressed in the following way:\n Ireflected = Idirected((N dot L) + kspecular cos[sup]n[/sup](ss))\n cos[sup]n[/sup](ss) can be replaced by using the mechanism of the dot or scalar product of the unit vectors R and V:\n Ireflected = Idirected((N dot L) + kspecular (R dot V)[sup]n[/sup])\n This is the generally accepted phong reflection equation. As the angle between V and R decreases, the specularity will rise.\n Because it is expensive to calculate the reflection vector R (mirror of light incidence around the surface normal), James F. Blinn [Blinn] introduced a way to do this using an imaginary vector H, which is defined as halfway between L and V. H is therefore:\n H = (L + V) / 2\n When H coincides with N, the direction of the reflection coincides with the viewing direction V and a specular hightlight is observed. So the original formula\n (R dot V)[sup]n[/sup]\n is expanded to\n (N dot ((L + V) / 2))[sup]n[/sup]\n or\n (N dot H)[sup]n[/sup]\n The complete Blinn-Phong model formula looks like:\n Ireflected = Idirected((N dot L) + kspecular (N dot H)[sup]n[/sup])\n Now back to the relevant code piece that calculates the specular reflection:; compute normalized half vector H = L + V\nadd r2, c[EYE_VECTOR], r5 ; L + V\n; renormalize H\ndp3 r2.w, r2, r2\nrsq r2.w, r2.w\nmul r2, r2, r2.w\n; N\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n", "dateCreated": "2002-04-24T23:15:45+0000", "datePublished": "2002-04-24T23:15:00+0000", "dateModified": "2002-04-24T23:15:00+0000", "pageStart": 1, "pageEnd": 1, "author": { "@type": "Person", "name": "Myopic Rhino", "image": "https://secure.gravatar.com/avatar/16f4f23aa8088c2c27c829623874b42e?d=https://www.gamedev.net/uploads/monthly_2017_08/M.png.3d2e4bcade48bd80a1d6554f9c036e99.png", "url": "https://www.gamedev.net/profile/6-dave-astle/" }, "publisher": { "@type": "Organization", "name": "Myopic Rhino", "image": "https://secure.gravatar.com/avatar/16f4f23aa8088c2c27c829623874b42e?d=https://www.gamedev.net/uploads/monthly_2017_08/M.png.3d2e4bcade48bd80a1d6554f9c036e99.png", "logo": { "@type": "ImageObject", "url": "https://secure.gravatar.com/avatar/16f4f23aa8088c2c27c829623874b42e?d=https://www.gamedev.net/uploads/monthly_2017_08/M.png.3d2e4bcade48bd80a1d6554f9c036e99.png" }, "url": "https://www.gamedev.net/profile/6-dave-astle/" }, "interactionStatistic": [ { "@type": "InteractionCounter", "interactionType": "http://schema.org/ViewAction", "userInteractionCount": 9085 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/FollowAction", "userInteractionCount": 3 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/ReviewAction", "userInteractionCount": 0 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/CommentAction", "userInteractionCount": 0 } ], "image": { "@type": "ImageObject", "url": "https://secure.gravatar.com/avatar/16f4f23aa8088c2c27c829623874b42e?d=https://www.gamedev.net/uploads/monthly_2017_08/M.png.3d2e4bcade48bd80a1d6554f9c036e99.png", "width": "500", "height": "500" }, "aggregateRating": { "@type": "AggregateRating", "ratingValue": 5, "ratingCount": 1, "reviewCount": 0, "bestRating": "5" }, "commentCount": 0 } { "@context": "http://www.schema.org", "@type": "WebSite", "name": "GameDev.net", "url": "https://www.gamedev.net/", "potentialAction": { "type": "SearchAction", "query-input": "required name=query", "target": "https://www.gamedev.net/search/?q={query}" }, "inLanguage": [ { "@type": "Language", "name": "English (USA)", "alternateName": "en-US" } ] } { "@context": "http://www.schema.org", "@type": "Organization", "name": "GameDev.net", "url": "https://www.gamedev.net/", "logo": "https://www.gamedev.net/uploads/themes/monthly_2017_04/gamedev-logo-2017-368x76.png.e986e41d566ff6c90485a5304985ed5f.png", "address": { "@type": "PostalAddress", "streetAddress": "", "addressLocality": null, "addressRegion": null, "postalCode": null, "addressCountry": null } } { "@context": "http://schema.org", "@type": "BreadcrumbList", "itemListElement": [ { "@type": "ListItem", "position": 1, "item": { "@id": "https://www.gamedev.net/articles/", "name": "Articles" } }, { "@type": "ListItem", "position": 2, "item": { "@id": "https://www.gamedev.net/articles/programming/", "name": "Programming" } }, { "@type": "ListItem", "position": 3, "item": { "@id": "https://www.gamedev.net/articles/programming/graphics/", "name": "Graphics and GPU Programming" } } ] } { "@context": "http://schema.org", "@type": "ContactPage", "url": "https://www.gamedev.net/contact/" } Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept We are the game development community. Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up! Sign me up!