Search code examples
c++directx-11hlsl

Why are these shaders making my window crash?


I'm following a win32 directx 11 tutorial on DirectXtutorials.com on drawing a triangle. The program is able to render a Blue Background correctly, but when the shaders are created and set, and have their input layouts set, the window crashes about 1 second after creation. The code that I'm using is exactly the same as the one on their website, so I'm confused as to why it's failing.

I have commented out the region that I suspect causes the window to crash. (Removing this section entirely from the code renders a blue screen that doesn't crash).

void InitPipeline()
{
    // load and compile the two shaders
    ID3D10Blob *VS, *PS;
    D3DX11CompileFromFile("shaders.shader", 0, 0, "VShader", "vs_4_0", 0, 0, 0, &VS, 0, 0);
    D3DX11CompileFromFile("shaders.shader", 0, 0, "PShader", "ps_4_0", 0, 0, 0, &PS, 0, 0);

//-------------------------------------------STARTS FROM HERE---------------------

    // encapsulate both shaders into shader objects
    dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS); 
    dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);

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

    // 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);

//-------------------------------------------ENDS HERE------------------------
}

And this is the shader file:

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

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

    output.position = position;
    output.color = color;

    return output;
}


float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
    return color;
}

Could someone identify what might be causing my program to crash please? Thank you!


Solution

  • For anyone who may have similar issues with this tutorial, I tried using the error codes as Simon Mourier suggested, and did it in the following manner:

     HRESULT THING=D3DX11CompileFromFile("shaders.shader", NULL, NULL, "VShader", "vs_4_0", NULL, 0, NULL, &VS, NULL, NULL);
        _com_error err(THING);
        LPCTSTR errMsg = err.ErrorMessage();
        MessageBoxA(NULL, errMsg, errMsg, MB_OK);
    

    The resulting error code was

    The specified file could not be found."

    Changing the "shaders.shader" to the absolute path of your shader file instead (such as Users\\\User\\\Desktop\\\shaders.shader) will fix this issue!