如何使用ID3D12ShaderReflection :: GetVariableByName方法

问题描述 投票:0回答:1

我有使用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);
    }

我想用ID3D12ShaderReflection接口,以获取有关以上像素着色器称为test_var变量信息。这是我目前正在:

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);

不过,我得到以下错误:

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
1个回答
1
投票

该HLSL着色反射API旨在揭露有关全局变量,这很可能在个别着色器优化掉了而不是内部的局部变量的元数据。见Microsoft Docs

你得到一个例外,因为你没有检查从nullptr回来一个GetVariableByName

© www.soinside.com 2019 - 2024. All rights reserved.