DirectXTK:将hlsl文件集成到模型和效果系统

时间:2017-01-11 17:52:36

标签: uwp directx directx-11 directxtk

已经为OpenGL ES和XNA编程了一段时间,我现在正将一些内容更新为DirectXTK用于XBOX One。可悲的是,我刚遇到另一个问题 - 即自定义着色器文件的集成。

特别是,我有以下顶点着色器:

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;
}

将其加载到IEffectFactory实现中可以正常工作。在第一步中,我使用LoadAsync加载已编译的hlsl文件:

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
    ...

然后将两个变量myVShaderCode和mVShaderCodeSize传递给iEffect的实际实现,iEffect使用它们如下:

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

可悲的是,这在运行时不起作用。而是,SUS3DApp1.exe中0x603520B5(ucrtbased.dll)上的未处理异常类型的错误:将无效参数传递给认为无效参数致命的函数。被扔了。 调用堆栈如下所示:

>   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++

想法,有人吗?我已经检查了GetVertexShaderBytecode中的成员变量的有效性......

更新:我现在更改了着色器,还加载了代码。奇怪的是,这部分代码现在通过了SUSCreateDeviceResources中的CreateInputLayout阶段:     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

然后在创建模型期间出现问题,它再次成为无效参数。这很奇怪,因为之前创作很好。可能是myVShaderCode的值丢失了吗?

0 个答案:

没有答案