Skip to content

Instantly share code, notes, and snippets.

@rygorous
Last active December 31, 2015 04:39
Show Gist options
  • Save rygorous/7936047 to your computer and use it in GitHub Desktop.
Save rygorous/7936047 to your computer and use it in GitHub Desktop.
Using Win8 d3dcompiler without using Platform SDK headers for everything
// Okay, the Win8 version of d3dcompiler.dll is *substantially* faster to compile
// shaders, but switching to the Win8 SDK outright is painful if you need to
// support older compilers (the new headers don't work with old VC++ versions)
// or use D3DX.
//
// However, there's a dead simple solution that works just fine.
// Just link against June 2010 DX SDK d3dcompiler.lib and use its header files,
// then do this:
typedef HRESULT (__stdcall d3d_compile_func)(LPCVOID pSrcData, SIZE_T SrcDataSize, LPCSTR pSourceName, const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude, LPCSTR pEntrypoint, LPCSTR pTarget, UINT Flags1, UINT Flags2, ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs);
static d3d_compile_func *D3DCompileFunc = D3DCompile;
static void try_load_win8_compiler()
{
HMODULE dll = LoadLibraryA("d3dcompiler_46.dll");
if (!dll)
{
printf("shadergen: Couldn't load d3dcompiler_46.dll; will use old D3D compiler instead.\n"
" This means complicated shader compiles will take *significantly* longer (often\n"
" minutes instead of seconds). It's recommended you grab d3dcompiler_46.dll\n"
" from the Win8 SDK (32-bit version) and copy that in shadergen's path instead.\n\n");
}
else
{
void *func = GetProcAddress(dll, "D3DCompile");
if (!func)
codegen_error("Something is wrong with d3dcompiler_46.dll");
D3DCompileFunc = (d3d_compile_func *)func;
}
}
// actual shader compilation:
//
// hr = D3DCompileFunc(code, codelen, "source", NULL, NULL, "main", profile,
// D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE_ENABLE_STRICTNESS, 0, &blob, &errors);
//
// TL;DR: Call try_load_win8_compiler at startup and replace D3DCompile() calls with
// D3DCompileFunc(). That's it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment