Direct3D12 - 哪个 RootSignature 优先?

Direct3D12 - which RootSignature takes precedence?

我是 MSFT 和 DirectX 的新手,所以请放轻松..

如果 hlsl 着色器使用 [RootSignature(SignatureName)] 属性,它会被代码中使用 ID3D12RootSignature 定义的根签名覆盖吗?

例如在示例代码中,我在着色器包含的 .hlsli 文件中有这个:

    "RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT), " \
    "CBV(b0, visibility = SHADER_VISIBILITY_VERTEX), " \
    "CBV(b0, visibility = SHADER_VISIBILITY_PIXEL), " \
    "DescriptorTable(SRV(t0, numDescriptors = 6), visibility = SHADER_VISIBILITY_PIXEL)," \
    "DescriptorTable(SRV(t64, numDescriptors = 6), visibility = SHADER_VISIBILITY_PIXEL)," \
    "RootConstants(b1, num32BitConstants = 2, visibility = SHADER_VISIBILITY_VERTEX), " \
    "StaticSampler(s0, maxAnisotropy = 8, visibility = SHADER_VISIBILITY_PIXEL)," \
    "StaticSampler(s1, visibility = SHADER_VISIBILITY_PIXEL," \
        "addressU = TEXTURE_ADDRESS_CLAMP," \
        "addressV = TEXTURE_ADDRESS_CLAMP," \
        "addressW = TEXTURE_ADDRESS_CLAMP," \
        "comparisonFunc = COMPARISON_GREATER_EQUAL," \
        "filter = FILTER_MIN_MAG_LINEAR_MIP_POINT)"

在着色器中:

[RootSignature(ModelViewer_RootSig)]
VSOutput main(VSInput vsInput)
{
    VSOutput vsOutput;

    vsOutput.position = mul(modelToProjection, float4(vsInput.position, 1.0));
    vsOutput.worldPos = vsInput.position;
    vsOutput.texCoord = vsInput.texcoord0;
    vsOutput.viewDir = vsInput.position - ViewerPos;
 <etc>

这会覆盖还是被使用如下内容创建的根签名覆盖: ASSERT_SUCCEEDED( g_Device->CreateRootSignature(1, pOutBlob->GetBufferPointer(), pOutBlob->GetBufferSize(), MY_IID_PPV_ARGS(&m_Signature)) );

在 PC 上,如果您同时拥有“代码提供的”根签名和“着色器提供的”根签名,则代码优先。通常你应该使用其中之一,而不是两者都使用。

For DirectX Tool Kit for DX12 on GitHub I use both in violation of what I just said above, but there's good reasons for it. In general the "code-style" Root Signature seems easier to understand for 'newbies', and therefore I make use of it in the tool kit since it's primarily intended for learning projects, samples, etc. I do also support Xbox development, where the strong recommendation is to always use shader-based root signatures.

This is because on Xbox the shaders are 'fully precompiled' and any difference in the root signature triggers a 'recompile' at runtime. The Xbox version of the runtime generates debug output if my code and shader signatures are out-of-sync, so it's easy enough for me to maintain. That's not true for most people. It also provides a bunch of examples of the same root sig in both code and shader notation, which is a good thing for deved, but probably not worthwhile for most projects.