Q: DX12 图形管线状态创建错误

Q: DX12 Error in creation of graphics pipeline state

按照 Microsoft 文档创建 PSO,我收到以下 D3D12 错误。

Error code

考虑到第一个错误指出编码的签名大小与指定的大小不匹配。 我比较了着色器和签名。

    D3D12_INPUT_ELEMENT_DESC inputElementDesc[] =
    {{
      "POSITION",
      0,
      DXGI_FORMAT_R32G32B32_FLOAT,
      0,
      0,
      D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
      0
    }};

    D3D12_INPUT_LAYOUT_DESC inputLayoutDesc;
    inputLayoutDesc.pInputElementDescs = inputElementDesc;
    inputLayoutDesc.NumElements = ARRAYSIZE(inputElementDesc);
struct VSIn 
{
    float3 pos : POSITION;
};

struct VSOut
{
    float4 pos : SV_POSITION;
};

VSOut vs_main( VSIn input, uint index : SV_VertexID )
{
    VSOut output = (VSOut)0;
    output.pos = float4(input.pos, 1.0f);
    return output;
}

我似乎找不到任何不当行为。 所以我想也许错误在于我编译着色器的方式。 为此,我使用了 IDxcCompiler 库。 调试时我没有从编译中得到任何错误。

    ComPtr<IDxcOperationResult> result = nullptr;
    ComPtr<IDxcBlobEncoding> vertexBlob = nullptr;
    std::vector<LPCWSTR> compileArguments;
    compileArguments.push_back(L"/Gis");
    ThrowIfFailed(library->CreateBlobFromFile(L"VertexShader.hlsl", nullptr, &vertexBlob));
    ThrowIfFailed(compiler->Compile(
        vertexBlob.Get(),
        L"VertexShader.hlsl",
        L"vs_main",
        L"vs_6_4",
        compileArguments.data(),
        (UINT)compileArguments.size(),
        NULL,
        NULL,
        NULL,
        &result
    ));

而且 blob 似乎已经收到了它的信息

D3D12_GRAPHICS_PIPELINE_STATE_DESC gPipelineStateDesc;
gPipelineStateDesc.pRootSignature = this->gRootSignature.Get();
gPipelineStateDesc.InputLayout = inputLayoutDesc;
gPipelineStateDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;

ComPtr<IDxcBlobEncoding> test16;
gPipelineStateDesc.VS.pShaderBytecode = reinterpret_cast<void*>(vertexBlob->GetBufferPointer());
library->GetBlobAsUtf16(vertexBlob.Get(), &test16);
OutputDebugStringW(reinterpret_cast<LPCWSTR>(test16->GetBufferPointer()));
gPipelineStateDesc.VS.BytecodeLength = vertexBlob->GetBufferSize();

gPipelineStateDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
gPipelineStateDesc.NumRenderTargets = 1;
gPipelineStateDesc.SampleDesc.Count = 1;
gPipelineStateDesc.SampleMask = UINT_MAX;

gPipelineStateDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
gPipelineStateDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;

D3D12_RENDER_TARGET_BLEND_DESC RTBlendDesc = {
    false,
    false,
    D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
    D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
    D3D12_LOGIC_OP_NOOP, D3D12_COLOR_WRITE_ENABLE_ALL
};
for (UINT i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i)
{
    gPipelineStateDesc.BlendState.RenderTarget[i] = RTBlendDesc;
}
ThrowIfFailed(this->gDevice->CreateGraphicsPipelineState(&gPipelineStateDesc, IID_PPV_ARGS(&this->gPipelineState)));

Output Results

缓冲区 size/byte 代码长度为 241 字节。 有人知道哪里出了问题吗?

Chuck Walbourn 帮我找到了解决方案。 blob 没有得到类似于 D3DCompileFromFile() 的编译结果。 通过在编译器的最后一个参数中使用 IDxcOperationResult。 您正在像这样从编译器中获取结果:

    ComPtr<IDxcOperationResult> result = nullptr;
    ....
    ...
    ..
    ComPtr<IDxcBlob> vertexShader = nullptr;
    result->GetResult(vertexShader);

这样 ->GetBufferPointer() 应该可以正常工作。 其余错误是由于忘记用“={};”初始化管道状态描述。