-
Notifications
You must be signed in to change notification settings - Fork 159
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
[Vulkan] Incorrect descriptor set when compiling from single slang shader? #669
Comments
The Short AnswerThis behavior is Slang working as intended. The Slightly Longer AnswerAnything you declare outside of an explicit The Even Longer AnswerThe right way to think about how Slang assigns Take an example where I put resources inside a constant buffer: cbuffer C
{
Texture2D t;
float4 a;
} If we imagine how the compiler scans this code to assign bindings, then when it sees the The compiler does not put off allocating the binding for the cbuffer C
{
Texture2D t;
} Now, the same basic logic applies to struct Stuff
{
ParameterBlock<MoreStuff> more;
Texture2D t;
};
ParameterBlock<Stuff> stuff; When the compiler sees the declaration of (Caveat: nesting of parameter blocks like this is intended to work, but hasn't been adopted by users, so I cannot promise that it won't break) If the With all that background, it is hopefully easy to see what Slang is doing to your overall shader. The right way to look at it is that Slang "opens" a fresh parameter block (for the "default" block) at the top of your file, and then scans your global declarations as the fields of that block. Ordinary resources/buffers/etc. become Unrelated NotesFrom the examples you've been posting in your bug reports, it seems like you are mostly using declarations like: ParameterBlock<X> myStuff; as an alternative to ordinary constant buffer declarations: cbuffer MyStuff
{
X myStuff;
} So far all of your examples have only put pure "uniform" data inside of If you want to write something closer to idiomatic HLSL, you may want to just use ordinary cbuffer U
{
UBOData ubo;
}
SamplerState textureSampler;
Texture2D textureSource; That code should work with any HLSL front-end, and not just Slang. Slang would put all of those declarations into the same Alternatively, a more idiomatic Slang way of doing things would be to declare a single struct MyParams
{
/* ... contents of UBOData go here ... */
SamplerState textureSampler;
Texture2D textureSource;
};
ParameterBlock<MyParams> myParams; In this second example we've encapsulated all the parameters of a single effect (which might be your top-level shader, or it might be some effect defined in a library you In this example all of the resources that get declared based on that The features of Slang are geared more toward this second approach, where you encapsulate together related shader parameters whether they are uniform data or resources, rather than the more traditional GLSL/HLSL style that tries to enforce a harder mental separation before resources and things that are "just data." |
@vasumahesh1 Does my response answer your questions? If so, I'd like to close this issue. If you think the behavior of the compiler should be different in cases like this, I'd be interested to hear what you'd like the rules to be. |
Oh sorry, was a bit MIA for a while. Thanks for answering my doubts. I had a few other things to verify. So say I wanted the Sampler and the Texture image on the same set but different from the set of the UBO.
|
EDIT: For Part 1 It does work. import Common;
ParameterBlock<UBOData> ubo;
ParameterBlock<SamplerBlock> samplerBlock;
float4 main(PixelShaderInput input) : SV_Target
{
float4 color = float4(samplerBlock.g_txDiffuse.Sample(samplerBlock.g_sampler, input.uv).rgb, 1.0);
return color;
} even though UBO doesn't get used, Slang assigns the SamplerBlock to set #line 25 0
layout(binding = 0, set = 1)
uniform sampler samplerBlock_g_sampler_0;
#line 8 1
layout(binding = 1, set = 1)
uniform texture2D samplerBlock_g_txDiffuse_0; |
For that example, it should, yes.
The big picture here is that Slang expects/needs to have a global view of your shader code. If you compile entry points one at a time - a vertex shader here, and then a fragment shader there, etc. - then somewhat obviously Slang can't take the resource bindings in your vertex shader into account when compiling the fragment shader. There are three main ways around this: 1. Put all the entry points that will be used together in one fileIf you currently have 2. Use separate files, but have one
|
To be clear, one of the most important design decisions in Slang is that we never take into account whether a parameter gets used or not when doing layout. We think this is the right option, especially for applications that implement "hot reload" of shaders, because you don't want the location of a shader parameter to change when you comment out some random line of code and then hot-reload. Some existing applications depend on compilers to eliminate all their unused parameters to make them fit within API/driver limits, and those applications might have a hard time porting to Slang. |
This is something I ended up with after trying some stuff out. The idea is now very clear that we need to expose the descriptors in the correct order for slang to understand all of the layout correctly.
Yes that is very true. I believe this clears out a lot of things. Many thanks. I will be trying out my D3D12 implementation in a few weeks. You can close this issue. 👍 |
Hi,
It seems I have another descriptor set issue. And a doubt too.
Say if I have:
And I have both my pixel shader and vertex shader functions in the same file. But
ubo
is not used in PS and vice versa for the texture & sampler.Do I expect ubo to be assigned to set 0 and texture sampler to set 1 when I get my compiled output? Or Do I expect both of them to be set 0 (Because the unused variables get compiled out)?
Because I have neither the case and ubo gets assigned to set 1 and the texture+sampler gets set 0 (and correct bindings). Here is the source I used
Output for GLSL:
Tested on 0.11.1
The text was updated successfully, but these errors were encountered: