BasicHLSL Sample

Description
This sample shows a simple example of the High-Level Shader Language (HLSL) using an effect interface.



Path
Source: DX90SDK\Samples\C++\Direct3D\BasicHLSL
Executable: DX90SDK\Samples\C++\Direct3D\Bin

Sample Overview
This sample simply loads a mesh, creates an effect from a file, and then uses the effect to render the mesh. The effect that is used is a simple vertex shader that animates the vertices based on time.

How does the sample work?
First the sample loads the geometry with D3DXLoadMeshFromX() and calls D3DXComputeNormals() to create mesh normals if there aren't any. It then calls OptimizeInplace() to optimize the mesh for the vertex cache. It then loads the textures using D3DXCreateTextureFromFile(). It then calls D3DXCreateEffectFromFile() to compile the effect text file to a ID3DXEffect* interface called m_pEffect.

Note that the D3DXCreateEffectFromFile() call takes flags which are needed to debug shaders with the Microsoft Visual Studio .NET shader debugger. Debugging vertex shaders requires either REF or software vertex processing, and debugging pixel shaders requires REF. The D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the shader debugger. It enables source level debugging, prevents instruction reordering, prevents dead code elimination, and forces the compiler to compile against the next higher available software target, which ensures that the unoptimized shaders do not exceed the shader model limitations. Setting these flags will cause slower rendering since the shaders will be unoptimized and forced into software. All that is needed to turn these flags on with the sample code is to uncomment the #define DEBUG_VS or #define DEBUG_PS line from near the top of the file. This will also force the device into software processing by adding some code to CMyD3DApplication::ConfirmDevices(). See the DirectX documentation for more information about using the shader debugger.

In CMyD3DApplication::Render() it sets the technique to use with a call to m_pEffect->SetTechnique(). It passes a D3DXHANDLE to the technique that was obtained upon init with a call to m_pEffect->GetTechniqueByName(). It is possible to pass a string name instead of a handle, but using a handle improves performance since this high frequency call does not have to spend time on a string compare.

It then sets variables used by the technique such as the World*View*Projection matrix and the time variable with various ID3DXEffect::Set*() calls. To render it calls m_pEffect->Begin() which returns the number of passes required by the technique, then it loops through each pass calling m_pEffect->Pass(iPass) and rendering the mesh with m_pMesh->DrawSubset(). When all the passes are done, it calls m_pEffect->End().





Copyright (c) Microsoft Corporation. All rights reserved.