Can I read somewhere about optimizations that HLSL compiler performs?

For example, in some codes I have different variables that get a constant value (just to let myself adjust the shader with other values) and I use them later in some mathematical expressions, but actually it can be precalculated earlier. Does the compiler do such thing?

It's just an example, I would like to know most of these things.


I don't think any such spec exists publicly, but you can disassemble your compiled shaders using the D3DDisassemble API function, or using fxc /Fc at the command line. If you write some example shaders and look at the disassembly, this will give you some idea of the optimizations it does.

Note that this assembly is a machine-independent intermediate language (IL). Shaders in D3D get compiled to this IL first by Microsoft's compiler, then they get compiled again to the actual GPU machine instructions by the GPU vendor's compiler which is part of their D3D driver. The second stage requires vendor tools to get at, like AMD's ShaderAnalyzer, and NVIDIA's ShaderPerf (although it looks like it hasn't been updated in awhile - no support for the latest NVIDIA GPUs or for 5_0 profiles). Some further optimizations may be done by the second stage of compilation, and those tools will let you see it to some extent.


The compiler can do some very effective optimization. The best way to find out what it can do is to use a tool like GPU ShaderAnalyzer which will show you the disassembly for a shader.

The one thing that it doesn't do is to move calculations between shader code and CPU code.

  • \$\begingroup\$ +1 for mentioning optimizing by offloading stuff to the CPU. \$\endgroup\$ Jan 20 '13 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.