0

Having programmed for OpenGL ES and XNA for some time, I am now updating some of my content to DirectXTK for the XBOX One. Sadly, I just ran into another issue - namely the integration of custom shader files.

In particular, I have the following Vertex Shader:

cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
    matrix model;
    matrix view;
    matrix projection;
};

struct VertexShaderInput
{
    float3 pos : POSITION;
};

struct PixelShaderInput
{
    float4 pos : SV_POSITION;
};

PixelShaderInput main(VertexShaderInput input)
{
    PixelShaderInput output;
    float4 pos = float4(input.pos, 1.0f);

    pos = mul(pos, model);
    pos = mul(pos, view);
    pos = mul(pos, projection);
    output.pos = pos;

    return output;
}

Loading it into the IEffectFactory implementation works fine to far. In the first step, I use LoadAsync to load the compiled hlsl file:

void SUS3DApp1Main::SUSCreateDeviceResources()
{
    auto loadVSTask = DX::ReadDataAsync(L"SampleVertexShader.cso");
    auto loadPSTask = DX::ReadDataAsync(L"SamplePixelShader.cso");


    auto createVSTask = loadVSTask.then([this](const std::vector<byte>& fileData) {
        DX::ThrowIfFailed(
            m_deviceResources->GetD3DDevice()->CreateVertexShader(
                &fileData[0],
                fileData.size(),
                nullptr,
                &myVertexShader
            )
        );

        myVShaderCode = fileData.data();
        myVShaderCodeSize = fileData.size();
        myFlagVSUp = true;
    }); //VS hoch
    ...

The two variables myVShaderCode and mVShaderCodeSize then get passed on into the actual implementation of iEffect, which uses them like this:

void SUS3DApp1::SUSEffect::GetVertexShaderBytecode(void const ** pShaderByteCode, size_t * pByteCodeLength)
{
    (*pByteCodeLength) = myVShaderCodeSize;
    (*pShaderByteCode) = myVShaderCode;
}

Sadly, this does not work at runtime. Instead, an error of the type Unhandled exception at 0x603520B5 (ucrtbased.dll) in SUS3DApp1.exe: An invalid parameter was passed to a function that considers invalid parameters fatal. is thrown. The call stack looks like this:

>   SUS3DApp1.exe!DirectX::ThrowIfFailed(HRESULT hr) Line 48    C++
    SUS3DApp1.exe!CreateInputLayout(ID3D11Device * device, DirectX::IEffect * effect, ID3D11InputLayout * * pInputLayout, bool skinning) Line 202   C++
    SUS3DApp1.exe!DirectX::Model::CreateFromCMO(ID3D11Device * d3dDevice, const unsigned char * meshData, unsigned int dataSize, DirectX::IEffectFactory & fxFactory, bool ccw, bool pmalpha) Line 796  C++
    SUS3DApp1.exe!DirectX::Model::CreateFromCMO(ID3D11Device * d3dDevice, const wchar_t * szFileName, DirectX::IEffectFactory & fxFactory, bool ccw, bool pmalpha) Line 848 C++
    SUS3DApp1.exe!SUSModel::SUSModel(ID3D11Device * _dev, std::basic_string<char,std::char_traits<char>,std::allocator<char> > & resource, const unsigned char * _myVShaderCode, int _myVShaderCodeSize) Line 15    C++
    SUS3DApp1.exe!SUS3DApp1::SUS3DApp1Main::SetUpShaderDependentResources() Line 62 C++
    SUS3DApp1.exe!SUS3DApp1::SUS3DApp1Main::Render() Line 148   C++

Ideas, anyone? I already checked the validity of the member variables in GetVertexShaderBytecode...

Update: I now changed the shader, and also the loading of the code. Strangely, this part of the code now passes the CreateInputLayout stage inside of SUSCreateDeviceResources: void SUS3DApp1Main::SUSCreateDeviceResources() { auto loadVSTask = DX::ReadDataAsync(L"SampleVertexShader.cso"); auto loadPSTask = DX::ReadDataAsync(L"SamplePixelShader.cso");

    auto createVSTask = loadVSTask.then([this](const std::vector<byte>& fileData) {
        DX::ThrowIfFailed(
            m_deviceResources->GetD3DDevice()->CreateVertexShader(
                &fileData[0],
                fileData.size(),
                nullptr,
                &myVertexShader
            )
        );



        myVShaderCode = &fileData[0];
        myVShaderCodeSize = fileData.size();
        Microsoft::WRL::ComPtr<ID3D11InputLayout>       il;
        DX::ThrowIfFailed(
            m_deviceResources->GetD3DDevice()->CreateInputLayout(VertexPositionNormal::InputElements,
                VertexPositionNormal::InputElementCount,
                myVShaderCode, myVShaderCodeSize,
                &il)
        );
        myFlagVSUp = true;

    }); //VS hoch

The problem then occurs during the creation of the model, where it is once again a invalid parameter. This is very strange, as the creation worked well before. Could it be that the value of myVShaderCode is lost?

Tam HANNA
  • 51
  • 6
  • Put a break-point inside the ``CreateInputLayout`` function to see why it's throwing the exception. My guess is that your shader is failing to bind with the provided model vertex format. – Chuck Walbourn Jan 12 '17 at 07:29
  • Hello, sorry for being away so long. Things got a little out of hand. I already found that issue - it was my own idiocy, as the content of fileData did not get persisted after the line marked with //vs hoch. – Tam HANNA Jan 29 '17 at 17:57

0 Answers0