From ca6a64cb43a74943ee1a648a886959121b2d1dcf Mon Sep 17 00:00:00 2001 From: Cai Bear Date: Sun, 10 Aug 2025 17:44:24 -0700 Subject: [PATCH 1/2] Add Image::fetch_with_level. --- crates/spirv-std/src/image.rs | 29 +++++++++++++++++++ .../compiletests/ui/image/fetch_with_level.rs | 13 +++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/compiletests/ui/image/fetch_with_level.rs diff --git a/crates/spirv-std/src/image.rs b/crates/spirv-std/src/image.rs index 361cd1cf29..f110a1dfba 100644 --- a/crates/spirv-std/src/image.rs +++ b/crates/spirv-std/src/image.rs @@ -162,6 +162,35 @@ impl< } result.truncate_into() } + + /// Fetch a single texel at a mipmap `level` with a sampler set at compile time + #[crate::macros::gpu_only] + #[doc(alias = "OpImageFetch")] + pub fn fetch_with_level( + &self, + coordinate: impl ImageCoordinate, + level: u32, + ) -> SampledType::SampleResult + where + I: Integer, + { + let mut result = SampledType::Vec4::default(); + unsafe { + asm! { + "OpDecorate %image NonUniform", + "OpDecorate %result NonUniform", + "%image = OpLoad _ {this}", + "%coordinate = OpLoad _ {coordinate}", + "%result = OpImageFetch typeof*{result} %image %coordinate Lod {level}", + "OpStore {result} %result", + result = in(reg) &mut result, + this = in(reg) self, + coordinate = in(reg) &coordinate, + level = in(reg) level, + } + } + result.truncate_into() + } } impl< diff --git a/tests/compiletests/ui/image/fetch_with_level.rs b/tests/compiletests/ui/image/fetch_with_level.rs new file mode 100644 index 0000000000..34ddb41ace --- /dev/null +++ b/tests/compiletests/ui/image/fetch_with_level.rs @@ -0,0 +1,13 @@ +// build-pass + +use spirv_std::spirv; +use spirv_std::{Image, arch}; + +#[spirv(fragment)] +pub fn main( + #[spirv(descriptor_set = 0, binding = 0)] image: &Image!(2D, type=f32, sampled), + output: &mut glam::Vec4, +) { + let texel = image.fetch_with_level(glam::IVec2::new(0, 1), 0); + *output = texel; +} From ba4bfe7a6250ff81a94a68710cec7377fedee360 Mon Sep 17 00:00:00 2001 From: Cai Bear Date: Mon, 11 Aug 2025 16:41:59 -0700 Subject: [PATCH 2/2] Rename level to lod to match spirv and glsl naming convention. --- crates/spirv-std/src/image.rs | 10 +++++----- .../image/{fetch_with_level.rs => fetch_with_lod.rs} | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) rename tests/compiletests/ui/image/{fetch_with_level.rs => fetch_with_lod.rs} (78%) diff --git a/crates/spirv-std/src/image.rs b/crates/spirv-std/src/image.rs index f110a1dfba..f3414084d1 100644 --- a/crates/spirv-std/src/image.rs +++ b/crates/spirv-std/src/image.rs @@ -163,13 +163,13 @@ impl< result.truncate_into() } - /// Fetch a single texel at a mipmap `level` with a sampler set at compile time + /// Fetch a single texel at a mipmap `lod` with a sampler set at compile time #[crate::macros::gpu_only] #[doc(alias = "OpImageFetch")] - pub fn fetch_with_level( + pub fn fetch_with_lod( &self, coordinate: impl ImageCoordinate, - level: u32, + lod: u32, ) -> SampledType::SampleResult where I: Integer, @@ -181,12 +181,12 @@ impl< "OpDecorate %result NonUniform", "%image = OpLoad _ {this}", "%coordinate = OpLoad _ {coordinate}", - "%result = OpImageFetch typeof*{result} %image %coordinate Lod {level}", + "%result = OpImageFetch typeof*{result} %image %coordinate Lod {lod}", "OpStore {result} %result", result = in(reg) &mut result, this = in(reg) self, coordinate = in(reg) &coordinate, - level = in(reg) level, + lod = in(reg) lod, } } result.truncate_into() diff --git a/tests/compiletests/ui/image/fetch_with_level.rs b/tests/compiletests/ui/image/fetch_with_lod.rs similarity index 78% rename from tests/compiletests/ui/image/fetch_with_level.rs rename to tests/compiletests/ui/image/fetch_with_lod.rs index 34ddb41ace..1b2662de46 100644 --- a/tests/compiletests/ui/image/fetch_with_level.rs +++ b/tests/compiletests/ui/image/fetch_with_lod.rs @@ -8,6 +8,6 @@ pub fn main( #[spirv(descriptor_set = 0, binding = 0)] image: &Image!(2D, type=f32, sampled), output: &mut glam::Vec4, ) { - let texel = image.fetch_with_level(glam::IVec2::new(0, 1), 0); + let texel = image.fetch_with_lod(glam::IVec2::new(0, 1), 0); *output = texel; }