Unused bindings throw error
Rus-Abd opened this issue · comments
struct Uniforms { resolution: vec2f, time: f32 };
@binding(0) @group(0) var<uniform> U : Uniforms;
@binding(1) @group(0) var mySampler: sampler;
@binding(2) @group(0) var myTexture: texture_2d<f32>;
struct out{
position: vec4f,
color: vec3f
}
@fragment
fn main(@builtin(position) fragCoord : vec4f,
@location(0) color: vec3f) -> @location(0) vec4<f32>
{
var uv = fragCoord.xy/U.resolution ;//* 2. -1.;
//uv*= vec2f(1, -1);
var col:vec4f = textureSample(myTexture,mySampler,uv);
return vec4<f32>(color,1.);
}
Everything works fine until I comment this line
var col:vec4f = textureSample(myTexture,mySampler,uv);
I receive this error
Number of entries (3) did not match the number of entries (1) specified in [BindGroupLayout].
Expected layout: [{ binding: 0, visibility: ShaderStage::(Vertex|Fragment), buffer: { type: BufferBindingType::Uniform, hasDynamicOffset: 0, minBindingSize: 16 } }]
- While validating [BindGroupDescriptor] against [BindGroupLayout]
- While calling [Device].CreateBindGroup([BindGroupDescriptor]).1
I'm getting an error for not using these bindings, am I forced to use them?
I'll point out that this issue list is for problems with the specs, not questions about using the API.
That being said, this is intended WGSL behavior. The workaround is to use a phony assignment to indicate to WGSL that the variable is still "in use".
@fragment
fn main(@builtin(position) fragCoord : vec4f,
@location(0) color: vec3f) -> @location(0) vec4<f32>
{
var uv = fragCoord.xy/U.resolution ;//* 2. -1.;
//uv*= vec2f(1, -1);
// Do a phony assignment to ensure that these variables are not removed from the autogenerated bind group layout.
_ = myTexture;
_ = mySampler;
//var col:vec4f = textureSample(myTexture,mySampler,uv);
return vec4<f32>(color,1.);
}
The other way to work around this is to stop using layout: 'auto'
when creating your pipelines. By explicitly specifying the BindGroupLayout
you can consistently create bind groups using every specified entry regardless of if any given shader makes use of all of them.