gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.4k stars 908 forks source link

Disallow write-only storage buffers #4370

Open soltanmm opened 2 years ago

soltanmm commented 2 years ago

AFAICT, according to Resource Layout Compatibility, WGSL does not consider write-only storage buffers to be part of the resource interface.

Naga 0.8.5 appears to validate the following fragment of module-scope WGSL:

[[group(2), binding(0)]] var<storage, write> my_struct_global_variable: MyStruct;

Behavior on master-as-of-this-post is similar. Inserting the following module-scope WGSL fragment in tests/in/access.wgsl results in a passing cargo test:

struct Baz {
  a: u32,
};
@group(1) @binding(0)
var<storage,write> baz: Baz;

But trying to actually use such a binding through wgpu results in errors like:

thread 'main' panicked at 'wgpu error: Validation Error

Caused by:
    In Device::create_compute_pipeline
    error matching shader requirements against the pipeline
    shader global ResourceBinding { group: 2, binding: 0 } is not available in the layout pipeline layout
    storage class Storage { access: LOAD | STORE } doesn't match the shader Storage { access: STORE }

'

Because it's making the reasonable assumption that the shader wanted something readable.

kvark commented 2 years ago

This is a bit of a gray issue. From the point of shader translation - it doesn't care. The lack of readable texture storages can be seen as a limitation from the pipeline interface side, and all those validations happen on wgpu side. However, from the practical point of view, it would be better to get an error at shader module creation instead of the pipeline creation. So we should add this check to Naga.