I have been trying to get a tutorial Direct X 11 application running for some time now. I'm being stymied when I try to create the vertex shader. Setting the Direct X Debug setting reveals that I am getting this error.

D3D11 ERROR: ID3D11Device::CreateVertexShader: Encoded Vertex Shader size doesn't match specified size. [ STATE_CREATION ERROR #166: CREATEVERTEXSHADER_INVALIDSHADERBYTECODE]


I have compiled the hlsl shader code using Visual Studio 2012 Express into "shaders.cso" and that appears to load okay. According to the HRESULT return from D3DReadFileToBlob. However, the CreateVertexShader call returns "E_INVALIDARG One or more arguments are invalid."

After this the program fails on a Draw attempt.

I have checked the size of the blob buffer and it is the same size as the shaders.cso file.

I have included the code which sets this up below.

void InitPipeline()
{
HRESULT hr;

ID3DBlob *VS = nullptr, *PS = nullptr;
ID3DBlob *errorBlob = nullptr;

const D3D_SHADER_MACRO defines[] = {"EXAMPLE_DEFINE", "1", NULL, NULL};
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
flags |= D3DCOMPILE_DEBUG;
#endif

auto vbs = VS->GetBufferSize();
auto vbp = VS->GetBufferPointer();

hr = dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), nullptr, &pVS); // This function returns "E_INVALIDARG One or more arguments are invalid." and pVS is returned as NULL

// create the input layout object
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devcon->IASetInputLayout(pLayout);
}


For reference this is the shader code.

cbuffer ConstantBuffer
{
float4x4 matFinal;
}

struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};

VOut main(float4 position : POSITION, float4 color : COLOR)
{
VOut output;

output.position = mul(matFinal, position);
output.color = color;

return output;
}


I'll admit to not knowing much about shader code. Honestly, I'd prefer to do without it at this stage of re-learning D3D, but it appears to be required to use DX11.

Can anyone shed any light on this?

• Note I don't really have a good suggestion as to why you are getting this weird result. Are you mixing the use of Windows 8 and the legacy DirectX SDK in this project? You may find that using DirectX Tool Kit a bit easier as it provides a number of ready-to-use stock shaders. – Chuck Walbourn Jul 1 '14 at 6:26
• Thanks Chuck. I gave up on this some time ago, but thanks for the tip on the DX toolkit, I'll check it out when I get a bit of time. Cheers. – John Judd Jul 3 '14 at 5:18
• I came here only to realize that my binary shaders were full of 0s for some reason. – RelativeGames Dec 31 '14 at 11:18