Search code examples
directxd3dx

Getting shader bytecode from ID3D11VertexShader or anything


I usually get vertex shader's bytecodes by D3DX11CompileFromFile, but is there a way to get the bytecodes from ID3D11VertexShader? or other interfaces.

I've tried other D3DX11Compile* functions, such as D3DX11CompileFromMemory, D3DX11CompileFromResource but the functions were not designed for this problem.

ID3DBlob* VertexBlob;

D3DX11CompileFromFile(Resource->GetPath(), NULL, NULL, "VS", "vs_5_0", 
D3DCOMPILE_ENABLE_STRICTNESS, 0, NULL, &VertexBlob, &ErrorBlob, &hr);

As I said, I usually initialize shader bytecodes(ID3DBlob*) via D3DX11CompileFromFile function, but i don't use it anymore.

I've tried to find other ways to solve this problem. but i failed.

Is it possible to get shader bytecodes without using D3DX11Compile* functions?


Solution

  • In order to create Direct3D 11 vertex shader object, pixel shader object, etc. you have to provide the compiled shader binary which is what the shader blob contains.

    The Direct3D 11 function will fail if the provided compiled shader binary is invalid or if the signature is not correct for the runtime. Therefore, the binary has to come from the HLSL Direct3D compiler in the first place. Usually this is done offline and then loaded directly from disk as binary data, a cache of such binary blobs created during runtime of the game while it was in production, or as is common in older samples built from shader source at runtime via the Direct3D Compile API.

    Note that the D3DX11Compile* functions are merely wrappers around the D3D Compile API. D3DX11 is deprecated, so you should use D3DCompile directly instead.

    See this blog post