3
\$\begingroup\$

In Direct3D 11 there is a two-stage shader compilation process; you first pass the HLSL source code into D3DCompile, which outputs a "blob" of bytecode. You then take this blob and call CreatePixelShader or another of the CreateWhateverShader methods to finally get an object that can be bound to the D3D pipeline and used.

My question is: is the bytecode produced by the first stage machine-independent or is it specific to the GPU on which it was compiled? I could not find a straight answer to this in the MSDN docs. I heard somewhere that HLSL compiles to machine-independent bytecode which is then translated into GPU-specific machine code by the driver, but I have not been able to confirm this.

\$\endgroup\$
  • \$\begingroup\$ Thumbs up for making me wonder where to find what I already thought to be true in written form. Not sure I would have stumbled across that AMD presentation otherwise. \$\endgroup\$ – Koarl Jan 25 '12 at 1:18
3
\$\begingroup\$

This Presentation by AMD should answer your question about shader compilation in DirectX and intermediate representations of shader code:

http://developer.amd.com/media/gpu_assets/Dark_Secrets_of_shader_Dev-Mojo.pdf

I have only skimmed over it, but HLSL shader object code seems to be hardware independent indeed.

Also, from my own (albeit small) experience: I compile shaders using fxc.exe and distribute the shader object files in an archive just as any other type of game asset. During run-time, I just read those object files into ordinary byte buffers and pass them to ID3D11Device::CreateVertexShader() for instance. How would that ever work if shader object files were not platform independent?

EDIT: Sorry, I only just noticed that you were specifically asking whether the output of D3DCompile was platform independent. Since that output can (and must) be fed to Create...Shader just as fxc-output can, I suppose it's pretty much the same thing, added to the library for convenience.

\$\endgroup\$
  • \$\begingroup\$ Cool. Yeah, fxc.exe is just a wrapper around D3DCompile as far as I know. Thanks for the reference, although I still wish I could find it in MSDN. :) \$\endgroup\$ – Nathan Reed Jan 25 '12 at 1:58
1
\$\begingroup\$

I'm not sure where in MSDN docs it is written, but HLSL bytecode is independent of GPU. HLSL shader compilation is happening in D3D API, not on driver level like in OpenGL case.

\$\endgroup\$

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.