Allow for explicit definition of descriptor set on uniforms #4232
Replies: 3 comments 5 replies
-
For some additional information, I used Naga to transpile the fragment shader SPIR-V that struct PostprocessingParams_std140_ {
time: f32,
window_dimmensions: vec3<f32>,
}
struct GlobalParams_std140_ {
settings: PostprocessingParams_std140_,
}
var<private> inputuv_1: vec2<f32>;
@group(0) @binding(0)
var<uniform> globalParams: GlobalParams_std140_;
var<private> fragment_1: vec4<f32>;
fn fragment() {
let _e12 = inputuv_1;
let _e16 = globalParams.settings;
let _e21 = globalParams.settings;
fragment_1 = vec4<f32>((vec3<f32>(0f, 1f, 0f) * smoothstep(3f, 0f, (length((((_e12 * 2f) - vec2<f32>(1f, 1f)) * _e16.window_dimmensions.xy)) - fma(60f, sin(_e21.time), 240f)))), 1f);
return;
}
@fragment
fn main(@location(0) inputuv: vec2<f32>) -> @location(0) vec4<f32> {
inputuv_1 = inputuv;
fragment();
let _e3 = fragment_1;
return _e3;
} As expected, the uniform was bound to descriptor set 0, and the shader doesn't work. If I change the descriptor set to 2, the shader starts working fine, confirming the issue I described. |
Beta Was this translation helpful? Give feedback.
-
After digging a bit into the generated SPIR-V code, I discovered that, for some reason, struct VertexOutput {
float4 position : SV_Position;
float2 uv : LOC0;
};
[shader("vertex")]
VertexOutput vertex(uint vertex_index : SV_VertexID) {
VertexOutput output;
output.uv = float2(float(vertex_index >> 1u), float(vertex_index & 1u)) * 2.0;
output.position = float4(output.uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
return output;
}
struct PostprocessingParams {
float time;
float width;
float height;
float aspect;
};
[[vk::binding(2, 0)]]
ConstantBuffer<PostprocessingParams> settings;
[shader("fragment")]
float4 fragment(VertexOutput input) : SV_Target0 {
var test = float2(1.0, 1.0);
float2 uv = (input.uv * 2.0 - 1.0) * float2(settings.width, settings.height);
float circle_sdf = length(uv) - (240 + 60 * sin(settings.time));
float3 color = smoothstep(3, 0.0, circle_sdf) * float3(0.0, 1.0, 0.0);
return float4(color, 1.0);
} This works for bevy, since |
Beta Was this translation helpful? Give feedback.
-
@pedrotrschneider Packing all global uniforms into a single uniform buffer is the intended design. This aligns with the HLSL semantics and dxc behavior. Having a dedicated uniform buffer binding for a simple uniform value is wasteful and not performant so that isn't a desired behavior anyways. |
Beta Was this translation helpful? Give feedback.
-
Hi! I'm in the process of integrating slang into the Bevy game engine. It's all going super smoothly. I can compile to either GLSL or SPIR-V and it works flawlessly most of the time. My main problem is that there doesn't seem to exist a way to force slang to use a specific descriptor set when compiling, it always binds to descriptor set 0.
This is a problem for my goal because, in Bevy, user-defined uniforms default to descriptor set 2, with no easy way to change that. That is because descriptor sets 0 and 1 are reserved for data the engine passes automatically to the shader. Now, I can change the default descriptor set of my uniforms in Bevy to 0, but that ends up adding about 200 lines of code to my once 40-line codebase.
For reference, this is the slang shader I'm using:
This is a simple shader that, given 3 vertices, has the vertex shader construct a fullscreen triangle and map the UVs correctly, and, in the fragment shader, uses those UVs to draw a pulsating green circle in the center of the screen. I'm compiling the shader to SPIR-V using the following commands:
And my problem is that the uniform I defined doesn't get updated by Bevy because it defaults to descriptor set 0. For reference, this is the WGSL shader I use to make the same effect work:
As you can see, the uniform is explicitly bound to descriptor set 2.
With all of that being said, is there a way I can force slang to bind to descriptor set 2 instead of 0? And if not, is this something that could be added in a later release?
Beta Was this translation helpful? Give feedback.
All reactions