Skip to content

Commit c53c351

Browse files
authored
feat: add image fetch (#480)
1 parent 2d6a677 commit c53c351

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,14 @@ pub fn main(input: Input<glam::Vec2>, image: UniformConstant<StorageImage2d>) {
587587
}
588588
"#);
589589
}
590+
591+
#[test]
592+
fn image_fetch() {
593+
val(r#"
594+
#[spirv(fragment)]
595+
pub fn main(image: UniformConstant<Image2d>, mut output: Output<glam::Vec4>) {
596+
let texel = image.fetch(glam::IVec2::new(0, 1));
597+
*output = texel;
598+
}
599+
"#);
600+
}

crates/spirv-std/src/textures.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ impl Image2d {
4242
result
4343
}
4444
}
45+
/// Fetch a single texel with a sampler set at compile time
46+
#[spirv_std_macros::gpu_only]
47+
pub fn fetch<I>(&self, coordinate: impl Vector<I>) -> Vec4
48+
where
49+
I: Integer,
50+
{
51+
let mut result = Vec4::default();
52+
unsafe {
53+
asm! {
54+
"%image = OpLoad _ {this}",
55+
"%coordinate = OpLoad _ {coordinate}",
56+
"%result = OpImageFetch typeof*{result} %image %coordinate",
57+
"OpStore {result} %result",
58+
this = in(reg) self,
59+
coordinate = in(reg) &coordinate,
60+
result = in(reg) &mut result,
61+
}
62+
}
63+
64+
result
65+
}
4566
}
4667

4768
#[spirv(image_type(

0 commit comments

Comments
 (0)