0
\$\begingroup\$

From Microsoft Docs, I can specify the root signature of my shader in HLSL. How do I use it though?

Since at the beginning of the command list, root signature is undefined. I need to use ID3D12GraphicsCommandList::SetComputeRootSignature(ID3D12RootSignature*) or ID3D12GraphicsCommandList::SetGraphicsRootSignature(ID3D12RootSignature*) set root signature, but having specified in HLSL instead of C++ code, I do not have a pointer to my root signature objects, how should it be done then?

\$\endgroup\$
1
\$\begingroup\$

You can provide any compiled shader blob that contains the desired root signature defined in HLSL when creating the root signature object:

Microsoft::WRL::ComPtr<ID3D12RootSignature> m_rootSignature

// Create root signature.
auto vertexShaderBlob = DX::ReadData(L"VertexShader.cso");

DX::ThrowIfFailed(
    device->CreateRootSignature(0, vertexShaderBlob.data(), vertexShaderBlob.size(),
        IID_PPV_ARGS(m_rootSignature.ReleaseAndGetAddressOf())));

…

// Use root signature
commandList->SetGraphicsRootSignature(m_rootSignature.Get());

The DX::ReadData function just loads the binary blob from disk (source here). The DX::ThrowIfFailed helper is explained here.

If you are using FXC.EXE you can also build a shader with the HLSL defined root signature using the rootsig_1_0 or rootsig_1_1 profile and the resulting compiled binary object will just have the root signature in it.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That worked! I was used to using D3D12SerializeVersionedRootSignature to produce the blob and didn't know the blob from compiling the shader can be used for it as well. Thank you. \$\endgroup\$ – klWu Jan 17 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.