How to use ID3D12ShaderReflection::GetVariableByName method

0

I have the following shader compiled using D3DCompiler:

struct vertex_in
    {
        float3 position : POSITION;
        float2 tex_coord : TEXCOORD;
    };

    struct vertex_out
    {
        float4 position : SV_POSITION;
        float2 tex_coord : TEXCOORD;
    };

    Texture2D g_texture : register(t0);
    SamplerState g_sampler : register(s0);

    vertex_out VS(vertex_in vin)
    {
        vertex_out vout;
        vout.position  = float4(vin.position , 1.0f);
        vout.tex_coord = vin.tex_coord;
        return vout;
    }

    float4 PS(vertex_out pin) : SV_Target
    {
        float2 test_var;
        test_var.x = 0.0f;
        test_var.y = 1.0f;
        return g_texture.Sample(g_sampler, test_var);
    }

I would like to use the ID3D12ShaderReflection interface in order to get information about the variable called test_var in the pixel shader above. This is what I am currently trying:

com_ptr<ID3D12ShaderReflection> pixel_shader_reflector;
D3DReflect(pixel_shader->GetBufferPointer(), pixel_shader->GetBufferSize(), winrt::guid_of<ID3D12ShaderReflection>(), pixel_shader_reflector.put_void());
std::string test_var = "test_var";
ID3D12ShaderReflectionVariable* sr_variable = pixel_shader_reflector->GetVariableByName(test_var.c_str());
D3D12_SHADER_VARIABLE_DESC shader_var_desc = {};
sr_variable->GetDesc(&shader_var_desc);

However I get the following errors:

Exception thrown at 0x00007FFBF7AB5299 (KernelBase.dll) in wzrd_editor.exe: WinRT originate error - 0x80004005 : 'Unspecified error'.
Exception thrown at 0x00007FFBF7AB5299 in wzrd_editor.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x00000053DE1FB058.
reflection
directx
shader
hlsl
directx-12
asked on Stack Overflow Feb 7, 2019 by mbl

1 Answer

1

The HLSL shader reflection API is designed to expose metadata about global variables, not internal local variables that are probably optimized out in individual shaders. See Microsoft Docs.

You are getting an exception because you didn't check for a nullptr coming back from GetVariableByName.

answered on Stack Overflow Feb 10, 2019 by Chuck Walbourn

User contributions licensed under CC BY-SA 3.0