Skip to content

Commit b320baf

Browse files
authored
Add Image::fetch_with_lod
Fixes #360 I used the terminology level since that's what WGSL used, but it would also make sense to use lod since that's what SPIR-V and GLSL's texelFetch use.
1 parent 9cf4ea4 commit b320baf

File tree

8 files changed

+52
-23
lines changed

8 files changed

+52
-23
lines changed

crates/spirv-std/src/image.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ impl<
162162
}
163163
result.truncate_into()
164164
}
165+
166+
/// Fetch a single texel at a mipmap `lod` with a sampler set at compile time
167+
///
168+
/// `lod` is also known as `level` in WGSL's `textureLoad`
169+
#[crate::macros::gpu_only]
170+
#[doc(alias = "OpImageFetch")]
171+
pub fn fetch_with_lod<I>(
172+
&self,
173+
coordinate: impl ImageCoordinate<I, DIM, ARRAYED>,
174+
lod: u32,
175+
) -> SampledType::SampleResult
176+
where
177+
I: Integer,
178+
{
179+
self.fetch_with(coordinate, sample_with::lod(lod))
180+
}
165181
}
166182

167183
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_lod(glam::IVec2::new(0, 1), 0);
12+
*output = texel;
13+
}

tests/compiletests/ui/image/gather_err.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ error[E0277]: the trait bound `Image<f32, 0, 2, 0, 0, 1, 0, 4>: HasGather` is no
99
Image<SampledType, 3, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
1010
Image<SampledType, 4, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
1111
note: required by a bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#1}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>::gather`
12-
--> $SPIRV_STD_SRC/image.rs:199:15
12+
--> $SPIRV_STD_SRC/image.rs:215:15
1313
|
14-
192 | pub fn gather<F>(
14+
208 | pub fn gather<F>(
1515
| ------ required by a bound in this associated function
1616
...
17-
199 | Self: HasGather,
17+
215 | Self: HasGather,
1818
| ^^^^^^^^^ required by this bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#1}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>::gather`
1919

2020
error[E0277]: the trait bound `Image<f32, 2, 2, 0, 0, 1, 0, 4>: HasGather` is not satisfied
@@ -28,12 +28,12 @@ error[E0277]: the trait bound `Image<f32, 2, 2, 0, 0, 1, 0, 4>: HasGather` is no
2828
Image<SampledType, 3, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
2929
Image<SampledType, 4, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
3030
note: required by a bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#1}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>::gather`
31-
--> $SPIRV_STD_SRC/image.rs:199:15
31+
--> $SPIRV_STD_SRC/image.rs:215:15
3232
|
33-
192 | pub fn gather<F>(
33+
208 | pub fn gather<F>(
3434
| ------ required by a bound in this associated function
3535
...
36-
199 | Self: HasGather,
36+
215 | Self: HasGather,
3737
| ^^^^^^^^^ required by this bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#1}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>::gather`
3838

3939
error: aborting due to 2 previous errors

tests/compiletests/ui/image/query/query_levels_err.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ error[E0277]: the trait bound `Image<f32, 4, 2, 0, 0, 1, 0, 4>: HasQueryLevels`
1010
Image<SampledType, 2, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>
1111
Image<SampledType, 3, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>
1212
note: required by a bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>::query_levels`
13-
--> $SPIRV_STD_SRC/image.rs:951:15
13+
--> $SPIRV_STD_SRC/image.rs:967:15
1414
|
15-
949 | pub fn query_levels(&self) -> u32
15+
965 | pub fn query_levels(&self) -> u32
1616
| ------------ required by a bound in this associated function
17-
950 | where
18-
951 | Self: HasQueryLevels,
17+
966 | where
18+
967 | Self: HasQueryLevels,
1919
| ^^^^^^^^^^^^^^ required by this bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>::query_levels`
2020

2121
error: aborting due to 1 previous error

tests/compiletests/ui/image/query/query_lod_err.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ error[E0277]: the trait bound `Image<f32, 4, 2, 0, 0, 1, 0, 4>: HasQueryLevels`
1010
Image<SampledType, 2, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>
1111
Image<SampledType, 3, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>
1212
note: required by a bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>::query_lod`
13-
--> $SPIRV_STD_SRC/image.rs:980:15
13+
--> $SPIRV_STD_SRC/image.rs:996:15
1414
|
15-
974 | pub fn query_lod(
15+
990 | pub fn query_lod(
1616
| --------- required by a bound in this associated function
1717
...
18-
980 | Self: HasQueryLevels,
18+
996 | Self: HasQueryLevels,
1919
| ^^^^^^^^^^^^^^ required by this bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>::query_lod`
2020

2121
error: aborting due to 1 previous error

tests/compiletests/ui/image/query/query_size_err.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ error[E0277]: the trait bound `Image<f32, 1, 2, 0, 0, 1, 0, 4>: HasQuerySize` is
1515
Image<SampledType, 2, DEPTH, ARRAYED, 0, 2, FORMAT, COMPONENTS>
1616
and 6 others
1717
note: required by a bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>::query_size`
18-
--> $SPIRV_STD_SRC/image.rs:1015:15
18+
--> $SPIRV_STD_SRC/image.rs:1031:15
1919
|
20-
1013 | pub fn query_size<Size: ImageSizeQuery<u32, DIM, ARRAYED> + Default>(&self) -> Size
20+
1029 | pub fn query_size<Size: ImageSizeQuery<u32, DIM, ARRAYED> + Default>(&self) -> Size
2121
| ---------- required by a bound in this associated function
22-
1014 | where
23-
1015 | Self: HasQuerySize,
22+
1030 | where
23+
1031 | Self: HasQuerySize,
2424
| ^^^^^^^^^^^^ required by this bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, MULTISAMPLED, SAMPLED, FORMAT, COMPONENTS>::query_size`
2525

2626
error: aborting due to 1 previous error

tests/compiletests/ui/image/query/query_size_lod_err.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ error[E0277]: the trait bound `Image<f32, 4, 2, 0, 0, 1, 0, 4>: HasQuerySizeLod`
1010
Image<SampledType, 2, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
1111
Image<SampledType, 3, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
1212
note: required by a bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#7}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>::query_size_lod`
13-
--> $SPIRV_STD_SRC/image.rs:1061:15
13+
--> $SPIRV_STD_SRC/image.rs:1077:15
1414
|
15-
1056 | pub fn query_size_lod<Size: ImageSizeQuery<u32, DIM, ARRAYED> + Default>(
15+
1072 | pub fn query_size_lod<Size: ImageSizeQuery<u32, DIM, ARRAYED> + Default>(
1616
| -------------- required by a bound in this associated function
1717
...
18-
1061 | Self: HasQuerySizeLod,
18+
1077 | Self: HasQuerySizeLod,
1919
| ^^^^^^^^^^^^^^^ required by this bound in `Image::<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#7}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>::query_size_lod`
2020

2121
error: aborting due to 1 previous error

tests/compiletests/ui/image/query/sampled_image_rect_query_size_lod_err.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ error[E0277]: the trait bound `Image<f32, 4, 2, 0, 0, 1, 0, 4>: HasQuerySizeLod`
1010
Image<SampledType, 2, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
1111
Image<SampledType, 3, DEPTH, ARRAYED, 0, SAMPLED, FORMAT, COMPONENTS>
1212
note: required by a bound in `SampledImage::<Image<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#9}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>>::query_size_lod`
13-
--> /image.rs:1226:12
13+
--> /image.rs:1242:12
1414
|
15-
1212 | pub fn query_size_lod<Size: ImageSizeQuery<u32, DIM, ARRAYED> + Default>(
15+
1228 | pub fn query_size_lod<Size: ImageSizeQuery<u32, DIM, ARRAYED> + Default>(
1616
| -------------- required by a bound in this associated function
1717
...
18-
1226 | >: HasQuerySizeLod,
18+
1242 | >: HasQuerySizeLod,
1919
| ^^^^^^^^^^^^^^^ required by this bound in `SampledImage::<Image<SampledType, DIM, DEPTH, ARRAYED, spirv_std::::image::{impl#9}::{constant#0}, SAMPLED, FORMAT, COMPONENTS>>::query_size_lod`
2020

2121
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)