hlsldirectx-12

Shader signature reflection


I am trying to get the shader signature for my vertex shader in HLSL using DirectX 12 and I cannot figure out how to get the data types out of the parameters.

vs.hlsl

#include "rootsig.hlsl"

struct VS_OUT
{
    float4 pos : SV_POSITION;
    float3 color : COLOR;
};

struct CameraTransform
{
    matrix viewProjection;
};

struct ModelTransform
{
    matrix transform;
};

ConstantBuffer<ModelTransform> ModelTrans : register(b1);
ConstantBuffer<ModelTransform> CameraTrans: register(b0);

[RootSignature(ROOTSIG)]
VS_OUT main(float3 pos : Position, float3 color : Color )
{
    VS_OUT vsout;
    vsout.color = color;
    matrix transform = mul(CameraTrans.viewProjection, ModelTrans.transform);
    vsout.pos = mul(transform, float4(pos, 1.0f));
    return vsout;
}

I can only get that the position is a float and I cannot find how to get the float3 out.

I have been searching through the documentation and I have not been able to find the issue. I have tried using the GetInputParameterDesc in the ID3D12ShaderReflection interface


Solution

  • I think something like this should work:

    //assuming vertex_shader_reflection is ID3D12ShaderReflection*
    D3D12_SHADER_DESC shader_desc;
    CHECK_HR(vertex_shader_reflection->GetDesc(&shader_desc));
    
    D3D12_SIGNATURE_PARAMETER_DESC signature_param_desc{};
    std::vector<D3D12_INPUT_ELEMENT_DESC> input_element_descs(shader_desc.InputParameters);
    for (Uint32 i = 0; i < shader_desc.InputParameters; i++)
    {
        vertex_shader_reflection->GetInputParameterDesc(i, &signature_param_desc);
        input_element_descs[i].SemanticName = signature_param_desc.SemanticName;
        input_element_descs[i].SemanticIndex = signature_param_desc.SemanticIndex;
        input_element_descs[i].AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT;
        input_element_descs[i].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
        input_element_descs[i].InputSlot = 0;
    
        if (signature_param_desc.Mask == 1)
        {
            if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_UINT32)        input_element_descs[i].Format = DXGI_FORMAT_R32_UINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_SINT32)  input_element_descs[i].Format = DXGI_FORMAT_R32_SINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) input_element_descs[i].Format = DXGI_FORMAT_R32_FLOAT;
        }
        else if (signature_param_desc.Mask <= 3)
        {
            if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_UINT32)        input_element_descs[i].Format = DXGI_FORMAT_R32G32_UINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_SINT32)  input_element_descs[i].Format = DXGI_FORMAT_R32G32_SINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) input_element_descs[i].Format = DXGI_FORMAT_R32G32_FLOAT;
        }
        else if (signature_param_desc.Mask <= 7)
        {
            if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_UINT32)        input_element_descs[i].Format = DXGI_FORMAT_R32G32B32_UINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_SINT32)  input_element_descs[i].Format = DXGI_FORMAT_R32G32B32_SINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) input_element_descs[i].Format = DXGI_FORMAT_R32G32B32_FLOAT;
        }
        else if (signature_param_desc.Mask <= 15)
        {
            if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_UINT32)        input_element_descs[i].Format = DXGI_FORMAT_R32G32B32A32_UINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_SINT32)  input_element_descs[i].Format = DXGI_FORMAT_R32G32B32A32_SINT;
            else if (signature_param_desc.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) input_element_descs[i].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
        }
    }