Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My shader creation code looks like this.

std::shared_ptr<IRenderUtility::IVertexShader> D3D11RenderUtility::CreateVertexShader(IShader::INIT_DESC & desc) const
    HRESULT result;
    ID3D10Blob * errorMessage;
    ID3D10Blob * vertexShaderBuffer;
    ID3D11VertexShader * vertShader;
    D3D11VertexShader::INIT_DESC vertShaderDesc;
    char errorMsg[255];

    result = D3DX11CompileFromFileA(desc.fileName.c_str(), NULL, NULL, desc.entryPoint.c_str(), "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &vertexShaderBuffer, &errorMessage, NULL);
            OutputErrorBlob(errorMessage, desc.fileName.c_str());

        sprintf_s(errorMsg, "D3D11_Renderer | D3D11RenderUtility::CreateVertexShader \n\tfailed to compile shader file %s %i", desc.fileName, __LINE__);
        throw std::exception(errorMsg);

    result = m_Device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &vertShader);
        sprintf_s(errorMsg, "D3D11_Renderer | D3D11RenderUtility::CreateVertexShader \n\tfailed to create the vertex shader %i", __LINE__);
        throw std::exception(errorMsg);

    vertexShaderBuffer = 0;

    vertShaderDesc.shader = vertShader;

    return std::shared_ptr<IVertexShader>(new D3D11VertexShader(vertShaderDesc));

IRenderUtility, D3D11RenderUtility, IVertexShader, D3D11VertexShader, and IShader are my stuff. My question only pertains to the Shader creation code. At a later point, after the shader is made, i would like to create an input layout, but without the blob to get a pointer to the compiled shader, i cannot. Is there any way to obtain a pointer to the compiled shader from ID3D11VertexShader after it is created, or should i keep the blob around, storing it in my VertexShader object? How big is it?

share|improve this question
"How big is it?" You can answer that question yourself by calling GetBufferSize(). I wouldn't expect the shader bytecode to be more than a few KB - maybe into the tens of KB for a big, complex shader - so keeping the bytecode around shouldn't be an issue. – Nathan Reed Apr 25 '13 at 18:25

In general, and because vertex shaders and input layouts are so tightly coupled, I prefer to create both at the same time, i.e. something like this (not the exact code, just for illustration):

HRESULT CreateVertexShaderAndInputLayout (ID3D11VertexShader **vs, ID3D11InputLayout **il, otherParams...)

This ensures that your layout is valid for the shader and that you have no blobs left hanging around that you may forget to Release. It comes at the expense of potentially having multiple duplicate input layouts, of course, but I don't consider it that big a deal.

Another option is to use shader reflection to get at the vertex shader input, but that of course won't get you the per-instance attributes of the layout, if you use them, so I don't consider it viable.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.