E_INVALIDARG 调用 CreateGraphicsPipelineState 时

E_INVALIDARG when calling CreateGraphicsPipelineState

我在调用 CreateGraphicsPipelineState() 时遇到了一个奇怪的错误。 函数returnsE_INVALIDARG虽然描述都设置好了

之前的描述有效,我尝试将 indexbuffers 添加到我的管道中,我什至没有接触任何 PSO 或着色器的代码,现在 PSO 的创建都一团糟。

问题是在启用调试层时我没有从驱动程序收到任何 DX 错误消息。我只得到这个

"Microsoft C++ exception: _com_error at memory location

当我单步执行函数时。

感觉是指针错误之类的,但我想不通是什么错误。也许你们中的任何人都能看出我犯了一个明显的错误?

这是我的代码:

CGraphicsPSO* pso = new CGraphicsPSO();

    D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};


    // Input Layout
    std::vector<D3D12_INPUT_ELEMENT_DESC> elements;
    if (aPSODesc.inputLayout != nullptr)
    {
        auto& ilData = aPSODesc.inputLayout->desc;
        for (auto& element : ilData)
        {
            // All Data here is correct when breaking
            D3D12_INPUT_ELEMENT_DESC elementDesc;
            elementDesc.SemanticName = element.mySemanticName;
            elementDesc.SemanticIndex = element.mySemanticIndex;
            elementDesc.InputSlot = element.myInputSlot;
            elementDesc.AlignedByteOffset = element.myAlignedByteOffset;
            elementDesc.InputSlotClass = _ConvertInputClassificationDX12(element.myInputSlotClass);
            elementDesc.Format = _ConvertFormatDX12(element.myFormat);
            elementDesc.InstanceDataStepRate = element.myInstanceDataStepRate;

            elements.push_back(elementDesc);
        }
        D3D12_INPUT_LAYOUT_DESC inputLayout = {};
        inputLayout.NumElements = (UINT)elements.size();
        inputLayout.pInputElementDescs = elements.data();
        psoDesc.InputLayout = inputLayout;
    }   
    // TOPOLOGY
    switch (aPSODesc.topology)
    {
    default:
    case EPrimitiveTopology::TriangleList:
        psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; // <--- Always this option
        break;
    case EPrimitiveTopology::PointList:
        psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT;
        break;
    case EPrimitiveTopology::LineList:
        psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE;
        break;
        //case EPrimitiveTopology::Patch:
        //  psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_PATCH;
        //  break;
    }

    // Shaders
    if (aPSODesc.vs != nullptr)
    {
        D3D12_SHADER_BYTECODE vertexShaderBytecode = {};
        vertexShaderBytecode.BytecodeLength = aPSODesc.vs->myByteCodeSize;
        vertexShaderBytecode.pShaderBytecode = aPSODesc.vs->myByteCode;
        psoDesc.VS = vertexShaderBytecode;
    }
    if (aPSODesc.ps != nullptr)
    {
        D3D12_SHADER_BYTECODE pixelShaderBytecode = {};
        pixelShaderBytecode.BytecodeLength = aPSODesc.ps->myByteCodeSize;
        pixelShaderBytecode.pShaderBytecode = aPSODesc.ps->myByteCode;
        psoDesc.PS = pixelShaderBytecode;
    }

    psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; // format of the render target

    DXGI_SAMPLE_DESC sampleDesc = {};
    sampleDesc.Count = 1;
    sampleDesc.Quality = 0;

    psoDesc.DepthStencilState.DepthEnable = FALSE;
    psoDesc.DepthStencilState.StencilEnable = FALSE;

    psoDesc.SampleDesc = sampleDesc; // must be the same sample description as the swapchain and depth/stencil buffer
    psoDesc.SampleMask = UINT_MAX; // sample mask has to do with multi-sampling. 0xffffffff means point sampling is done
    psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT); // a default rasterizer state.
    psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT); // a default blent state.
    psoDesc.NumRenderTargets = 1; // we are only binding one render target
    psoDesc.pRootSignature = myGraphicsRootSignature;
    psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;

    ID3D12PipelineState* pipelineState;
    HRESULT hr = myDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&pipelineState));
    pso->myPipelineState = pipelineState;
    if (FAILED(hr))
    {
        delete pso;
        return nullptr;
    }

    return pso;

所以我刚刚发现错误。 似乎我为我的输入布局解析语义的方式给了我一个无效的指针。 因此,该地址的内存无效,并给 DX12 设备提供了不正确的描述。

所以我所做的是将语义名称本地存储在我的 CreatePSO 函数中,直到创建 PSO,现在一切正常。

在我看来,您承诺的存储指针超出了范围。

..
  D3D12_INPUT_LAYOUT_DESC inputLayout = {};
..
  psoDesc.InputLayout = inputLayout;
}