Skip to content

Commit ca6a64c

Browse files
committed
Add Image::fetch_with_level.
1 parent df1628a commit ca6a64c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

crates/spirv-std/src/image.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ impl<
162162
}
163163
result.truncate_into()
164164
}
165+
166+
/// Fetch a single texel at a mipmap `level` with a sampler set at compile time
167+
#[crate::macros::gpu_only]
168+
#[doc(alias = "OpImageFetch")]
169+
pub fn fetch_with_level<I>(
170+
&self,
171+
coordinate: impl ImageCoordinate<I, DIM, ARRAYED>,
172+
level: u32,
173+
) -> SampledType::SampleResult
174+
where
175+
I: Integer,
176+
{
177+
let mut result = SampledType::Vec4::default();
178+
unsafe {
179+
asm! {
180+
"OpDecorate %image NonUniform",
181+
"OpDecorate %result NonUniform",
182+
"%image = OpLoad _ {this}",
183+
"%coordinate = OpLoad _ {coordinate}",
184+
"%result = OpImageFetch typeof*{result} %image %coordinate Lod {level}",
185+
"OpStore {result} %result",
186+
result = in(reg) &mut result,
187+
this = in(reg) self,
188+
coordinate = in(reg) &coordinate,
189+
level = in(reg) level,
190+
}
191+
}
192+
result.truncate_into()
193+
}
165194
}
166195

167196
impl<
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// build-pass
2+
3+
use spirv_std::spirv;
4+
use spirv_std::{Image, arch};
5+
6+
#[spirv(fragment)]
7+
pub fn main(
8+
#[spirv(descriptor_set = 0, binding = 0)] image: &Image!(2D, type=f32, sampled),
9+
output: &mut glam::Vec4,
10+
) {
11+
let texel = image.fetch_with_level(glam::IVec2::new(0, 1), 0);
12+
*output = texel;
13+
}

0 commit comments

Comments
 (0)