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

Question about Parameter Block Reflection API (Vulkan) #3782

Closed
Jebbly opened this issue Mar 17, 2024 · 5 comments
Closed

Question about Parameter Block Reflection API (Vulkan) #3782

Jebbly opened this issue Mar 17, 2024 · 5 comments

Comments

@Jebbly
Copy link

Jebbly commented Mar 17, 2024

Hello, I'm trying to use the Reflection API to extract information from Slang shaders, but I'm running into some confusion with the API. According to Issue #669, my understanding is that different parameter blocks should be allocated into different descriptor sets. For example, by slightly extending the hello world example:

struct Buffers {
    float constant;
    StructuredBuffer<float> buffer0;
    StructuredBuffer<float> buffer1;
    RWStructuredBuffer<float> output;
};

ParameterBlock<Buffers> buffers;
ParameterBlock<Buffers> buffers2;

[shader("compute")]
[numthreads(1, 1, 1)]
void compute_main(
    uint3 threadId: SV_DispatchThreadID) {
    uint index = threadId.x;
    buffers.output[index] = buffers.buffer0[index] + buffers.buffer1[index] + buffers.constant;
    buffers2.output[index] = buffers2.buffer0[index] + buffers2.buffer1[index] + buffers2.constant;
}

Here, I'd expect buffers and buffers2 to occupy sets 0 and 1 respectively (since there are no non-ParameterBlock arguments, so the default set would be empty). This seems to be the case when I manually use slangc to compile to GLSL:

#version 450
layout(row_major) uniform;
layout(row_major) buffer;

#line 1 0
struct Buffers_0
{
    float constant_0;
};


#line 5
layout(binding = 0)
layout(std140) uniform _S1
{
    float constant_0;
}buffers_0;

#line 1
layout(std430, binding = 1) readonly buffer StructuredBuffer_float_t_0 {
    float _data[];
} buffers_buffer0_0;

#line 1
layout(std430, binding = 2) readonly buffer StructuredBuffer_float_t_1 {
    float _data[];
} buffers_buffer1_0;

#line 16
layout(std430, binding = 3) buffer StructuredBuffer_float_t_2 {
    float _data[];
} buffers_output_0;

#line 16
layout(binding = 0, set = 1)
layout(std140) uniform _S2
{
    float constant_0;
}buffers2_0;

#line 16
layout(std430, binding = 1, set = 1) readonly buffer StructuredBuffer_float_t_3 {
    float _data[];
} buffers2_buffer0_0;

#line 16
layout(std430, binding = 2, set = 1) readonly buffer StructuredBuffer_float_t_4 {
    float _data[];
} buffers2_buffer1_0;

#line 16
layout(std430, binding = 3, set = 1) buffer StructuredBuffer_float_t_5 {
    float _data[];
} buffers2_output_0;

#line 13
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main()
{

#line 15
    uint index_0 = gl_GlobalInvocationID.x;
    buffers_output_0._data[index_0] = buffers_buffer0_0._data[index_0] + buffers_buffer1_0._data[index_0] + buffers_0.constant_0;
    buffers2_output_0._data[index_0] = buffers2_buffer0_0._data[index_0] + buffers2_buffer1_0._data[index_0] + buffers2_0.constant_0;
    return;
}

However, when I instead query for the layout reflection using the API using the following loop,

for (uint32_t parameter_index = 0; parameter_index < num_parameters; parameter_index++) {
    slang::VariableLayoutReflection* variable = layout->getParameterByIndex(parameter_index);
    std::cout << "set " << variable->getBindingSpace() << ", binding " << variable->getBindingIndex()
        << ": " << variable->getType()->getName() << " " << variable->getName() << std::endl;
}

I instead see the following:

set 0, binding 0: ParameterBlock buffers 
set 0, binding 1: ParameterBlock buffers2   

I'm referring to the (Slang API User's Guide)[https://shader-slang.com/slang/api-users-guide.html], although it seems to be a bit outdated - did the convention change at some point? I can't seem to find much on reflection in (the newer (?) documentation)[https://shader-slang.com/slang/user-guide/compiling.html#using-the-compilation-api], so any clarification would be much appreciated.

@Jebbly
Copy link
Author

Jebbly commented Mar 17, 2024

When I add more parameters as follows:

uniform float a;
uniform float b;
ParameterBlock<Buffers> buffers;
ParameterBlock<Buffers> buffers2;

The GLSL is again what I'd expect (with a and b being located in the default set 0 now, and the parameter blocks occupying sets 1 and 2 respectively. However, again using the same loop as before, I see the following being printed:

set 0, binding 0: float a                                                                                               
set 0, binding 4: float b                                                                                               
set 0, binding 1: ParameterBlock buffers                                                                                
set 0, binding 2: ParameterBlock buffers2      

For some more context, I'm directly using Slang as a submodule (at the latest commit as of right now), and I'm using this as the target description:

slang::TargetDesc target_info = {
    .format = SLANG_SPIRV,
    .profile = global_session_->findProfile("glsl_450"),
    .flags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY,
};

@csyonghe
Copy link
Collaborator

You also need to add the result from calling variable->getOffset(SLANG_PARAMETER_CATEGORY_REGISTER_SPACE).

@csyonghe
Copy link
Collaborator

I updated the documentation. Removed out of date doc on the API. Please check the user guide for the compilation and reflection API.

@Jebbly
Copy link
Author

Jebbly commented Mar 18, 2024

Thank you for the quick response! I found that SLANG_PARAMETER_CATEGORY_SUB_ELEMENT_REGISTER_SPACE seems to provide the expected result over SLANG_PARAMETER_CATEGORY_REGISTER_SPACE. Or is it necessary to add both of them together?

In both cases, I'm still seeing the uniform float b having an index of 4 even though I'd expect it to have an index of 1 in the default set. Is there also an offset I should be adding to the index as well?

EDIT: I see the documentation mentions that the index is actually the byte offset in its parent if the type is a Uniform. Will all "ordinary" resources be bound to the same uniform?

@csyonghe
Copy link
Collaborator

You are right, it should be SUB_ELEMENT_REGISTER_SPACE. I have updated the documentation here: https://shader-slang.com/slang/user-guide/reflection.html

You are right that the "binding" for ordinary uniforms are byte offsets for that uniform in the constant buffer. All global uniforms are placed in a default constant buffer that has the same binding. You can query the binding with getGlobalConstantBufferBinding.

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

2 participants