Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflection API : How to know if push constants are used by an entry point (using HLSL syntax) #5685

Open
PierreEVEN opened this issue Nov 26, 2024 · 0 comments

Comments

@PierreEVEN
Copy link

PierreEVEN commented Nov 26, 2024

Hi,

I would like to know if a push constant buffer is used by a given entry point.

I was initially using the hlsl syntax to declare my push constants:

struct PushConstsStruct
{
    float4x4 model;
};
[[vk::push_constant]] PushConstsStruct pc;

(Note : I replaced it with the slang syntax later, which led me to another issue : #5676)

With available reflection functions, the only way I found to know if a given parameter is used is :

slang::IMetadata* metadata;
program->getEntryPointMetadata(0, 0, &metadata, diagnostics.writeRef());
if (diagnostics)
    return result.push_error({static_cast<const char*>(diagnostics->getBufferPointer())});

StageData data;
slang::ProgramLayout* program_lay = program->getLayout();
for (unsigned par_i = 0; par_i < program_lay ->getParameterCount(); par_i++)
{
   // will always be set to false if it is a push constant
    bool b_is_used = true;
    slang::VariableLayoutReflection* parameter = program_lay ->getParameterByIndex(par_i);
    metadata->isParameterLocationUsed(static_cast<SlangParameterCategory>(parameter->getCategory()), 0, parameter->getBindingIndex(), b_is_used);
    ...
}

which doesn't works as it always return false.

I'm not sure that isParameterLocationUsed() is expected to works with push constants, but it's the only available method so I guess there is something missing.

here is the full shader : playground-link

And how I use the reflection Api :

slang::IModule* module = {...} // target is spirv_1.5

for (SlangInt32 ep_i = 0; ep_i < module->getDefinedEntryPointCount(); ++ep_i)
{
    Slang::ComPtr<slang::IEntryPoint> entry_point;
    if (SLANG_FAILED(module->getDefinedEntryPoint(ep_i, entry_point.writeRef())))
        return "error";

    std::vector<slang::IComponentType*> components;
    components.emplace_back(entry_point);
    Slang::ComPtr<slang::IComponentType> program;
    if (SLANG_FAILED(session->createCompositeComponentType(components.data(), components.size(), program.writeRef())))
        return "error";

    Slang::ComPtr<slang::IComponentType> linkedProgram;
    Slang::ComPtr<slang::IBlob>          diagnostics;
    program->link(linkedProgram.writeRef(), diagnostics.writeRef());
    if (diagnostics)
        return "error";

    slang::IMetadata* metadata;
    linkedProgram->getEntryPointMetadata(0, 0, &metadata, diagnostics.writeRef());
    if (diagnostics)
        return "error";

    StageData             data;
    slang::ProgramLayout* shaderReflection = linkedProgram->getLayout();
    for (unsigned par_i = 0; par_i < shaderReflection->getParameterCount(); par_i++)
    {
        bool                             b_is_used = true;
        slang::VariableLayoutReflection* parameter = shaderReflection->getParameterByIndex(par_i);
        metadata->isParameterLocationUsed(static_cast<SlangParameterCategory>(parameter->getCategory()), 0, parameter->getBindingIndex(), b_is_used);

        if (parameter->getCategory() == slang::PushConstantBuffer && !b_is_used)
                std::cout << std::format("ERROR : UNUSED PC !! {} \n", parameter->getName());
    }
}
@PierreEVEN PierreEVEN changed the title Reflection : How to know if push constants are used by an entry point (using HLSL syntax) Reflection API : How to know if push constants are used by an entry point (using HLSL syntax) Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant