CompiledEffect Sample

Description
This sample shows how an ID3DXEffect object can be compiled when the project is built and loaded directly as a binary file at run time.



Path
Source: DX90SDK\Samples\C++\Direct3D\CompiledEffect
Executable: DX90SDK\Samples\C++\Direct3D\Bin\CompiledEffect.exe


Sample Overview
Large-scale projects may contain dozens or even hundreds of shader and effect files. Compiling these files at run time can cause a noticeable delay during initialization, and this delay is incurred each and every time the project is run; instead, these files can be compiled as part of your project build process, eliminating the need for any further compilation during program execution. Since these files will only need to be compiled when changes are made, no additional build time is spent on effect files which have already been compiled. Note that when developing or debugging effect files it's still beneficial to compile at run time, which allows more debugging flexibility. Note that the process illustrated in this sample is identical whether compiling a complete effect object or individual shaders. All that would change would be the target profile command line passed to the compiler.


How does the sample work?
The code is virtually identical to the BasicHLSL sample except for a couple important differences:
  • The call to D3DXCreateEffectFromFile is passed the filename of the compiled effect (CompiledEffect.fxo) instead of the filename of the effect source (CompiledEffect.fx).
  • No debugging flags are passed to the D3DXCreateEffectFromFile function. Instead, flags are passed to the effect compiler to enable shader debugging. Which compiler flags are used depends on which Visual Studio build configuration is currently selected.

The program used to compile shaders at build time is fxc. This command-line utility ships with the SDK, and wraps the ID3DXEffectCompiler calls used to perform the compilation. It's interesting to note that you could create your own shader compiler fairly easily using the ID3DXEffectCompiler interface, but fxc should be flexible enough for most needs. The commands used to build release and debug versions of a hypothetical MyEffect.fx file are:

Release: fxc /Tfx_2_0 /FoMyEffect.fxo MyEffect.fx
Debug: fxc /Od /Zi /Tfx_2_0 /FoMyEffect.fxo MyEffect.fx

Run fxc without any arguments for usage information.


Build Environment Configuration
Here are the steps taken to configure the build environment. These steps should be performed for every effect file you wish to be compiled as part of the build process:

Visual Studio.NET:
  1. Open the CompiledEffect sample project inside Visual Studio.NET
  2. From within the Solution Explorer window, right click on CompiledEffect.fx and select "Properties" from the context menu.
  3. Select "Configuration Properties->Custom Build Step" to view the custom build step directives. The specific compile command issued depends on the project build configuration, as you can verify by scrolling through the "Configuration" combo box.

Visual C++ 6.0:
  1. Open the CompiledEffect sample project inside Visual C++ 6.0
  2. From within the Window window, right click on CompiledEffect.fx and select "Settings" from the context menu.
  3. Select the "Custom Build" tab to view the custom build step directives. The specific compile command issued depends on the project build configuration, as you can verify by scrolling through the "Settings For" combo box.






Copyright (c) Microsoft Corporation. All rights reserved.