Skip to content

Commit 4e5f347

Browse files
authored
Add SampledImage::sample_by_lod (#755)
* Add 'SampledImage::sample_by_lod' * Add a compiletest * Format the compiletest, but manually because running cargo fmt doesn't seem to do anything 🤷 * Run rustfmt
1 parent b692ab5 commit 4e5f347

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

crates/spirv-std/src/image.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl<
951951
Image<SampledType, DIM, DEPTH, ARRAYED, { Multisampled::False as u32 }, SAMPLED, FORMAT>,
952952
>
953953
{
954-
/// Sample texels at `coord` from the sampled image.
954+
/// Sample texels at `coord` from the sampled image with an implicit lod.
955955
///
956956
/// # Safety
957957
/// Sampling with a type (`S`) that doesn't match the image's image format
@@ -974,6 +974,36 @@ impl<
974974
);
975975
result
976976
}
977+
978+
/// Sample texels at `coord` from the sampled image with an explicit lod.
979+
///
980+
/// # Safety
981+
/// Sampling with a type (`S`) that doesn't match the image's image format
982+
/// will result in undefined behaviour.
983+
#[crate::macros::gpu_only]
984+
pub unsafe fn sample_by_lod<F, V>(
985+
&self,
986+
coord: impl ImageCoordinate<F, DIM, ARRAYED>,
987+
lod: f32,
988+
) -> V
989+
where
990+
F: Float,
991+
V: Vector<SampledType, 4>,
992+
{
993+
let mut result = Default::default();
994+
asm!(
995+
"%sampledImage = OpLoad typeof*{1} {1}",
996+
"%coord = OpLoad typeof*{2} {2}",
997+
"%lod = OpLoad typeof*{3} {3}",
998+
"%result = OpImageSampleExplicitLod typeof*{0} %sampledImage %coord Lod %lod",
999+
"OpStore {0} %result",
1000+
in(reg) &mut result,
1001+
in(reg) self,
1002+
in(reg) &coord,
1003+
in(reg) &lod,
1004+
);
1005+
result
1006+
}
9771007
}
9781008

9791009
/// This is a marker trait to represent the constraints on `OpImageGather` too complex to be

tests/ui/image/sample_lod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
// Test `OpImageSampleExplicitLod` Lod
22
// build-pass
33

4-
use spirv_std::{arch, Image, Sampler};
4+
use spirv_std::{arch, image::SampledImage, Image, Sampler};
55

66
#[spirv(fragment)]
77
pub fn main(
88
#[spirv(descriptor_set = 0, binding = 0)] image2d: &Image!(2D, type=f32, sampled),
99
#[spirv(descriptor_set = 1, binding = 1)] image2d_array: &Image!(2D, type=f32, arrayed, sampled),
1010
#[spirv(descriptor_set = 2, binding = 2)] cubemap: &Image!(3D, type=f32, sampled),
1111
#[spirv(descriptor_set = 3, binding = 3)] sampler: &Sampler,
12+
#[spirv(descriptor_set = 4, binding = 4)] sampled_image: &SampledImage<
13+
Image!(2D, type=f32, sampled),
14+
>,
1215
output: &mut glam::Vec4,
1316
) {
1417
let v2 = glam::Vec2::new(0.0, 1.0);
1518
let v3 = glam::Vec3::new(0.0, 1.0, 0.5);
1619
let r1: glam::Vec4 = image2d.sample_by_lod(*sampler, v2, 0.0);
1720
let r2: glam::Vec4 = image2d_array.sample_by_lod(*sampler, v3, 0.0);
1821
let r3: glam::Vec4 = cubemap.sample_by_lod(*sampler, v3, 0.0);
19-
*output = r1 + r2 + r3;
22+
let r4: glam::Vec4 = unsafe { sampled_image.sample_by_lod(v2, 0.0) };
23+
*output = r1 + r2 + r3 + r4;
2024
}

0 commit comments

Comments
 (0)