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;

    // load and compile the two shaders
    ID3DBlob *VS = nullptr, *PS = nullptr;
    ID3DBlob *errorBlob = nullptr;

    const D3D_SHADER_MACRO defines[] = {"EXAMPLE_DEFINE", "1", NULL, NULL};
#if defined( DEBUG ) || defined( _DEBUG )
    flags |= D3DCOMPILE_DEBUG;
    hr = D3DReadFileToBlob(L"shaders.cso", &VS);

    // encapsulate both shaders into shader objects
    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

    // set the shader objects
    devcon->VSSetShader(pVS, 0, 0);

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

    dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &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?

  • \$\begingroup\$ 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. \$\endgroup\$ Jul 1 '14 at 6:26
  • \$\begingroup\$ 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. \$\endgroup\$
    – John Judd
    Jul 3 '14 at 5:18
  • \$\begingroup\$ I came here only to realize that my binary shaders were full of 0s for some reason. \$\endgroup\$ Dec 31 '14 at 11:18

Browse other questions tagged or ask your own question.