![]() |
![]() |
![]() |
BasicHLSL Direct3D Sample |
Language: |
This sample shows a simple example of the high-level shader language (HLSL) using an effect interface.
Source: | (SDK root)\Samples\Managed\Direct3D\BasicHLSL |
---|---|
Executable: | (SDK root)\Samples\Managed\Direct3D\bin\csBasicHLSL.exe |
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, along with the possibility to have multiple directional lights.
First the sample loads the geometry with Mesh.FromFile and calls ComputeNormals() 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 CreateTextureFromFile(). Finally, it calls CreateEffectFromFile() to compile the effect text file into an Effect object called effect.
Note that the CreateEffectFromFile()call takes flags which are needed to debug shaders with the Microsoft® Visual Studio® .NET shader debugger. Debugging vertex shaders requires either Refernce or software vertex processing, and debugging pixel shaders requires Refernce. The ShaderFlags.Force*SoftwareNoOptimizations 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 ModifyDeviceSettings.
In OnFrameRender() it sets the technique to use with a call to effect.Technique. It passes an EffectHandle (in this case a string) to the technique. It is possible to pass a handle instead of a string, and 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 effect.SetArrayRange calls. To render it calls effect.BeginPass() which returns the number of passes required by the technique, then it loops through each pass calling effect.BeginPass(iPass) and rendering the mesh with mesh.DrawSubset. After each pass, effect.EndPass is called. When all the passes are done, it calls effect.End.
Feedback? Please provide us with your comments on this topic.
For more help, visit the DirectX Developer Center