Skip to content

Commit 8718ff5

Browse files
committed
Add query_size_lod and query_size methods to SampledImage
- Add query_size_lod for non-multisampled sampled images (Sampled=1) - Add query_size for multisampled sampled images - Add missing HasQuerySize trait implementations for Sampled::Yes - Add tests for both methods
1 parent fbe8893 commit 8718ff5

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

crates/spirv-std/src/image.rs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,94 @@ impl<
11171117
}
11181118
result.truncate_into()
11191119
}
1120+
1121+
/// Query the dimensions of the image at the specified level of detail.
1122+
#[crate::macros::gpu_only]
1123+
#[doc(alias = "OpImageQuerySizeLod")]
1124+
pub fn query_size_lod<Size: ImageCoordinate<u32, DIM, ARRAYED> + Default>(
1125+
&self,
1126+
lod: u32,
1127+
) -> Size
1128+
where
1129+
Image<
1130+
SampledType,
1131+
DIM,
1132+
DEPTH,
1133+
ARRAYED,
1134+
{ Multisampled::False as u32 },
1135+
SAMPLED,
1136+
FORMAT,
1137+
COMPONENTS,
1138+
>: HasQuerySizeLod,
1139+
{
1140+
let mut result: Size = Default::default();
1141+
unsafe {
1142+
asm! {
1143+
"%sampledImage = OpLoad _ {this}",
1144+
"%image = OpImage _ %sampledImage",
1145+
"%result = OpImageQuerySizeLod typeof*{result} %image {lod}",
1146+
"OpStore {result} %result",
1147+
this = in(reg) self,
1148+
lod = in(reg) lod,
1149+
result = in(reg) &mut result,
1150+
}
1151+
}
1152+
result
1153+
}
1154+
}
1155+
1156+
impl<
1157+
SampledType: SampleType<FORMAT, COMPONENTS>,
1158+
const DIM: u32,
1159+
const DEPTH: u32,
1160+
const ARRAYED: u32,
1161+
const SAMPLED: u32,
1162+
const FORMAT: u32,
1163+
const COMPONENTS: u32,
1164+
>
1165+
SampledImage<
1166+
Image<
1167+
SampledType,
1168+
DIM,
1169+
DEPTH,
1170+
ARRAYED,
1171+
{ Multisampled::True as u32 },
1172+
SAMPLED,
1173+
FORMAT,
1174+
COMPONENTS,
1175+
>,
1176+
>
1177+
{
1178+
/// Query the dimensions of the image, with no level of detail.
1179+
/// Available only for multisampled images.
1180+
#[crate::macros::gpu_only]
1181+
#[doc(alias = "OpImageQuerySize")]
1182+
pub fn query_size<Size: ImageCoordinate<u32, DIM, ARRAYED> + Default>(&self) -> Size
1183+
where
1184+
Image<
1185+
SampledType,
1186+
DIM,
1187+
DEPTH,
1188+
ARRAYED,
1189+
{ Multisampled::True as u32 },
1190+
SAMPLED,
1191+
FORMAT,
1192+
COMPONENTS,
1193+
>: HasQuerySize,
1194+
{
1195+
let mut result: Size = Default::default();
1196+
unsafe {
1197+
asm! {
1198+
"%sampledImage = OpLoad _ {this}",
1199+
"%image = OpImage _ %sampledImage",
1200+
"%result = OpImageQuerySize typeof*{result} %image",
1201+
"OpStore {result} %result",
1202+
this = in(reg) self,
1203+
result = in(reg) &mut result,
1204+
}
1205+
}
1206+
result
1207+
}
11201208
}
11211209

11221210
/// Helper trait that defines all `*_with` methods on an `Image` that use the extra image operands,
@@ -1633,6 +1721,25 @@ impl<
16331721
>
16341722
{
16351723
}
1724+
impl<
1725+
SampledType: SampleType<FORMAT, COMPONENTS>,
1726+
const DEPTH: u32,
1727+
const FORMAT: u32,
1728+
const ARRAYED: u32,
1729+
const COMPONENTS: u32,
1730+
> HasQuerySize
1731+
for Image<
1732+
SampledType,
1733+
{ Dimensionality::OneD as u32 },
1734+
DEPTH,
1735+
ARRAYED,
1736+
{ Multisampled::False as u32 },
1737+
{ Sampled::Yes as u32 },
1738+
FORMAT,
1739+
COMPONENTS,
1740+
>
1741+
{
1742+
}
16361743
impl<
16371744
SampledType: SampleType<FORMAT, COMPONENTS>,
16381745
const DEPTH: u32,
@@ -1691,6 +1798,25 @@ impl<
16911798
>
16921799
{
16931800
}
1801+
impl<
1802+
SampledType: SampleType<FORMAT, COMPONENTS>,
1803+
const DEPTH: u32,
1804+
const FORMAT: u32,
1805+
const ARRAYED: u32,
1806+
const COMPONENTS: u32,
1807+
> HasQuerySize
1808+
for Image<
1809+
SampledType,
1810+
{ Dimensionality::TwoD as u32 },
1811+
DEPTH,
1812+
ARRAYED,
1813+
{ Multisampled::False as u32 },
1814+
{ Sampled::Yes as u32 },
1815+
FORMAT,
1816+
COMPONENTS,
1817+
>
1818+
{
1819+
}
16941820
impl<
16951821
SampledType: SampleType<FORMAT, COMPONENTS>,
16961822
const DEPTH: u32,
@@ -1749,6 +1875,25 @@ impl<
17491875
>
17501876
{
17511877
}
1878+
impl<
1879+
SampledType: SampleType<FORMAT, COMPONENTS>,
1880+
const DEPTH: u32,
1881+
const FORMAT: u32,
1882+
const ARRAYED: u32,
1883+
const COMPONENTS: u32,
1884+
> HasQuerySize
1885+
for Image<
1886+
SampledType,
1887+
{ Dimensionality::ThreeD as u32 },
1888+
DEPTH,
1889+
ARRAYED,
1890+
{ Multisampled::False as u32 },
1891+
{ Sampled::Yes as u32 },
1892+
FORMAT,
1893+
COMPONENTS,
1894+
>
1895+
{
1896+
}
17521897
impl<
17531898
SampledType: SampleType<FORMAT, COMPONENTS>,
17541899
const DEPTH: u32,
@@ -1807,6 +1952,25 @@ impl<
18071952
>
18081953
{
18091954
}
1955+
impl<
1956+
SampledType: SampleType<FORMAT, COMPONENTS>,
1957+
const DEPTH: u32,
1958+
const FORMAT: u32,
1959+
const ARRAYED: u32,
1960+
const COMPONENTS: u32,
1961+
> HasQuerySize
1962+
for Image<
1963+
SampledType,
1964+
{ Dimensionality::Cube as u32 },
1965+
DEPTH,
1966+
ARRAYED,
1967+
{ Multisampled::False as u32 },
1968+
{ Sampled::Yes as u32 },
1969+
FORMAT,
1970+
COMPONENTS,
1971+
>
1972+
{
1973+
}
18101974
impl<
18111975
SampledType: SampleType<FORMAT, COMPONENTS>,
18121976
const DEPTH: u32,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Test `OpImageQuerySize` on multisampled `SampledImage`
2+
// build-pass
3+
// compile-flags: -C target-feature=+ImageQuery
4+
5+
use spirv_std::spirv;
6+
use spirv_std::{Image, arch, image::SampledImage};
7+
8+
#[spirv(fragment)]
9+
pub fn main(
10+
#[spirv(descriptor_set = 0, binding = 0)] sampled_image2d_ms: &SampledImage<
11+
Image!(2D, type=f32, multisampled, sampled),
12+
>,
13+
output: &mut glam::UVec2,
14+
) {
15+
// Multisampled sampled images can use query_size directly
16+
*output = sampled_image2d_ms.query_size();
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Test `OpImageQuerySizeLod` on `SampledImage`
2+
// build-pass
3+
// compile-flags: -C target-feature=+ImageQuery
4+
5+
use spirv_std::spirv;
6+
use spirv_std::{Image, arch, image::SampledImage};
7+
8+
#[spirv(fragment)]
9+
pub fn main(
10+
#[spirv(descriptor_set = 0, binding = 0)] sampled_image2d: &SampledImage<
11+
Image!(2D, type=f32, sampled),
12+
>,
13+
#[spirv(descriptor_set = 1, binding = 1)] sampled_image2d_array: &SampledImage<
14+
Image!(2D, type=f32, arrayed, sampled),
15+
>,
16+
#[spirv(descriptor_set = 2, binding = 2)] sampled_image3d: &SampledImage<
17+
Image!(3D, type=f32, sampled),
18+
>,
19+
output: &mut glam::UVec3,
20+
) {
21+
// For sampled images, we need to use query_size_lod
22+
let size_2d: glam::UVec2 = sampled_image2d.query_size_lod(0);
23+
let size_2d_array: glam::UVec3 = sampled_image2d_array.query_size_lod(0);
24+
let size_3d: glam::UVec3 = sampled_image3d.query_size_lod(0);
25+
26+
*output = glam::UVec3::new(size_2d.x, size_2d_array.y, size_3d.z);
27+
}

0 commit comments

Comments
 (0)