From 97a274daa3c247ba668d173d569a0f4fbfe0bc1b Mon Sep 17 00:00:00 2001 From: Jasper Bekkers Date: Fri, 26 Feb 2021 14:46:57 +0100 Subject: [PATCH 1/3] Safeness bug in StorageImage2d::write It's pretty easy to see why this is unsafe, if multiple threads write to the same coordinate race-conditions happen. Ultimately this should be addressed through something like #216 and some higher level abstractions on top of our buffer types, but since we don't have those for now, marking this as unsafe seems to be the only thing we can do for now. --- crates/spirv-std/src/textures.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/spirv-std/src/textures.rs b/crates/spirv-std/src/textures.rs index f8e54497a8..06482d6fd3 100644 --- a/crates/spirv-std/src/textures.rs +++ b/crates/spirv-std/src/textures.rs @@ -89,7 +89,7 @@ impl StorageImage2d { /// Write a texel to an image without a sampler. #[spirv_std_macros::gpu_only] - pub fn write(&self, coordinate: V, texels: V2) + pub unsafe fn write(&self, coordinate: V, texels: V2) where I: Integer, V: Vector, From 335aa917e8d1f99adb7e68e988c51ef9a514f4df Mon Sep 17 00:00:00 2001 From: Jasper Bekkers Date: Fri, 26 Feb 2021 15:16:48 +0100 Subject: [PATCH 2/3] Remove unsafe {} block for clippy --- crates/spirv-std/src/textures.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/spirv-std/src/textures.rs b/crates/spirv-std/src/textures.rs index 06482d6fd3..4873854ff4 100644 --- a/crates/spirv-std/src/textures.rs +++ b/crates/spirv-std/src/textures.rs @@ -95,16 +95,14 @@ impl StorageImage2d { V: Vector, V2: Vector, { - unsafe { - asm! { - "%image = OpLoad _ {this}", - "%coordinate = OpLoad _ {coordinate}", - "%texels = OpLoad _ {texels}", - "OpImageWrite %image %coordinate %texels", - this = in(reg) self, - coordinate = in(reg) &coordinate, - texels = in(reg) &texels, - } + asm! { + "%image = OpLoad _ {this}", + "%coordinate = OpLoad _ {coordinate}", + "%texels = OpLoad _ {texels}", + "OpImageWrite %image %coordinate %texels", + this = in(reg) self, + coordinate = in(reg) &coordinate, + texels = in(reg) &texels, } } } From b707ec284b3c4e64587f40d90d411b6d81f6411c Mon Sep 17 00:00:00 2001 From: Jasper Bekkers Date: Mon, 1 Mar 2021 15:51:23 +0100 Subject: [PATCH 3/3] Unsafe in test --- crates/spirv-builder/src/test/basic.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/spirv-builder/src/test/basic.rs b/crates/spirv-builder/src/test/basic.rs index 11abab3588..b37bb147b2 100644 --- a/crates/spirv-builder/src/test/basic.rs +++ b/crates/spirv-builder/src/test/basic.rs @@ -613,7 +613,9 @@ fn image_write() { #[spirv(fragment)] pub fn main(input: Input, image: UniformConstant) { let texels = *input; - image.write(glam::UVec2::new(0, 1), texels); + unsafe { + image.write(glam::UVec2::new(0, 1), texels); + } } "#); }