feat: add image fetch (#480)

This commit is contained in:
Jesse 2021-03-10 17:17:07 +09:00 committed by GitHub
parent 2d6a677563
commit c53c351987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -587,3 +587,14 @@ pub fn main(input: Input<glam::Vec2>, image: UniformConstant<StorageImage2d>) {
}
"#);
}
#[test]
fn image_fetch() {
val(r#"
#[spirv(fragment)]
pub fn main(image: UniformConstant<Image2d>, mut output: Output<glam::Vec4>) {
let texel = image.fetch(glam::IVec2::new(0, 1));
*output = texel;
}
"#);
}

View File

@ -42,6 +42,27 @@ impl Image2d {
result
}
}
/// Fetch a single texel with a sampler set at compile time
#[spirv_std_macros::gpu_only]
pub fn fetch<I>(&self, coordinate: impl Vector<I>) -> Vec4
where
I: Integer,
{
let mut result = Vec4::default();
unsafe {
asm! {
"%image = OpLoad _ {this}",
"%coordinate = OpLoad _ {coordinate}",
"%result = OpImageFetch typeof*{result} %image %coordinate",
"OpStore {result} %result",
this = in(reg) self,
coordinate = in(reg) &coordinate,
result = in(reg) &mut result,
}
}
result
}
}
#[spirv(image_type(