Skip to content

Commit 2f4c67e

Browse files
Safeness bug in StorageImage2d::write (#453)
* 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. * Remove unsafe {} block for clippy
1 parent 2034141 commit 2f4c67e

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

crates/spirv-builder/src/test/basic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,9 @@ fn image_write() {
613613
#[spirv(fragment)]
614614
pub fn main(input: Input<glam::Vec2>, image: UniformConstant<StorageImage2d>) {
615615
let texels = *input;
616-
image.write(glam::UVec2::new(0, 1), texels);
616+
unsafe {
617+
image.write(glam::UVec2::new(0, 1), texels);
618+
}
617619
}
618620
"#);
619621
}

crates/spirv-std/src/textures.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,20 @@ impl StorageImage2d {
8989

9090
/// Write a texel to an image without a sampler.
9191
#[spirv_std_macros::gpu_only]
92-
pub fn write<I, V, V2, const N: usize>(&self, coordinate: V, texels: V2)
92+
pub unsafe fn write<I, V, V2, const N: usize>(&self, coordinate: V, texels: V2)
9393
where
9494
I: Integer,
9595
V: Vector<I, 2>,
9696
V2: Vector<f32, N>,
9797
{
98-
unsafe {
99-
asm! {
100-
"%image = OpLoad _ {this}",
101-
"%coordinate = OpLoad _ {coordinate}",
102-
"%texels = OpLoad _ {texels}",
103-
"OpImageWrite %image %coordinate %texels",
104-
this = in(reg) self,
105-
coordinate = in(reg) &coordinate,
106-
texels = in(reg) &texels,
107-
}
98+
asm! {
99+
"%image = OpLoad _ {this}",
100+
"%coordinate = OpLoad _ {coordinate}",
101+
"%texels = OpLoad _ {texels}",
102+
"OpImageWrite %image %coordinate %texels",
103+
this = in(reg) self,
104+
coordinate = in(reg) &coordinate,
105+
texels = in(reg) &texels,
108106
}
109107
}
110108
}

0 commit comments

Comments
 (0)