Skip to content

Commit

Permalink
Make shader write&read storage buffers match non readonly layouts (#3893
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fornwall committed Jun 29, 2023
1 parent 88f18ed commit 17143c1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Bottom level categories:

- Fix order of arguments to glPolygonOffset by @komadori in [#3783](https://github.com/gfx-rs/wgpu/pull/3783).
- Fix OpenGL/EGL backend not respecting non-sRGB texture formats in `SurfaceConfiguration`. by @liquidev in [#3817](https://github.com/gfx-rs/wgpu/pull/3817)
- Make write- and read-only marked buffers match non-readonly layouts. by @fornwall in [#3893](https://github.com/gfx-rs/wgpu/pull/3893)

#### Metal

Expand Down
71 changes: 70 additions & 1 deletion wgpu-core/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl Resource {
)
}
};
if self.class != class {
if !address_space_matches(self.class, class) {
return Err(BindingError::WrongAddressSpace {
binding: class,
shader: self.class,
Expand Down Expand Up @@ -1237,3 +1237,72 @@ impl Interface {
Ok(outputs)
}
}

fn address_space_matches(shader: naga::AddressSpace, binding: naga::AddressSpace) -> bool {
match (shader, binding) {
(
naga::AddressSpace::Storage {
access: access_shader,
},
naga::AddressSpace::Storage {
access: access_pipeline,
},
) => {
// Allow read- and write-only usages to match read-write layouts:
(access_shader & access_pipeline) == access_shader
}
(a, b) => a == b,
}
}

#[cfg(test)]
mod test {
use super::address_space_matches;

#[test]
fn address_space_matches_correctly() {
assert!(address_space_matches(
naga::AddressSpace::Uniform,
naga::AddressSpace::Uniform
));

assert!(!address_space_matches(
naga::AddressSpace::Uniform,
naga::AddressSpace::Storage {
access: naga::StorageAccess::LOAD
}
));

let test_cases = [
(naga::StorageAccess::LOAD, naga::StorageAccess::LOAD, true),
(naga::StorageAccess::STORE, naga::StorageAccess::LOAD, false),
(naga::StorageAccess::LOAD, naga::StorageAccess::STORE, false),
(naga::StorageAccess::STORE, naga::StorageAccess::STORE, true),
(
naga::StorageAccess::LOAD | naga::StorageAccess::STORE,
naga::StorageAccess::LOAD | naga::StorageAccess::STORE,
true,
),
(
naga::StorageAccess::STORE,
naga::StorageAccess::LOAD | naga::StorageAccess::STORE,
true,
),
(
naga::StorageAccess::LOAD,
naga::StorageAccess::LOAD | naga::StorageAccess::STORE,
true,
),
];

for (shader, binding, expect_match) in test_cases {
assert_eq!(
expect_match,
address_space_matches(
naga::AddressSpace::Storage { access: shader },
naga::AddressSpace::Storage { access: binding }
)
);
}
}
}

0 comments on commit 17143c1

Please sign in to comment.