From beecb48e39c03739cf8d6d27ff6e93f0098a79c3 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Fri, 17 Mar 2023 21:17:49 +0200 Subject: [PATCH] spirv-std: add `#[repr(C)]` (and extra comments) to opaque handle types. --- crates/spirv-std/src/image.rs | 6 +++++- crates/spirv-std/src/ray_tracing.rs | 12 ++++++++++-- crates/spirv-std/src/runtime_array.rs | 7 +++++-- crates/spirv-std/src/sampler.rs | 6 +++++- tests/ui/image/gather_err.stderr | 8 ++++---- tests/ui/image/query/query_levels_err.stderr | 4 ++-- tests/ui/image/query/query_lod_err.stderr | 4 ++-- tests/ui/image/query/query_size_err.stderr | 4 ++-- tests/ui/image/query/query_size_lod_err.stderr | 4 ++-- 9 files changed, 37 insertions(+), 18 deletions(-) diff --git a/crates/spirv-std/src/image.rs b/crates/spirv-std/src/image.rs index b73167208a..76f1670a34 100644 --- a/crates/spirv-std/src/image.rs +++ b/crates/spirv-std/src/image.rs @@ -94,6 +94,8 @@ pub type Cubemap = crate::Image!(cube, type=f32, sampled, __crate_root=crate); /// See SPIR-V OpTypeImage specification for the meaning of integer parameters. #[spirv(generic_image_type)] #[derive(Copy, Clone)] +// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation. +#[repr(C)] pub struct Image< SampledType: SampleType, const DIM: u32, // Dimensionality, @@ -103,7 +105,9 @@ pub struct Image< const SAMPLED: u32, // Sampled, const FORMAT: u32, // ImageFormat, > { - _x: u32, + // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way + // or another, before `#[spirv(generic_image_type)]` can special-case it). + _anti_zst_padding: core::mem::MaybeUninit, _marker: core::marker::PhantomData, } diff --git a/crates/spirv-std/src/ray_tracing.rs b/crates/spirv-std/src/ray_tracing.rs index 1998e0078b..f86371ebbf 100644 --- a/crates/spirv-std/src/ray_tracing.rs +++ b/crates/spirv-std/src/ray_tracing.rs @@ -7,8 +7,12 @@ use core::arch::asm; /// acceleration structure handle as defined in the client API specification. #[spirv(acceleration_structure)] #[derive(Copy, Clone)] +// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation. +#[repr(C)] pub struct AccelerationStructure { - pub(crate) _private: u32, + // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way + // or another, before `#[spirv(acceleration_structure)]` can special-case it). + _anti_zst_padding: core::mem::MaybeUninit, } impl AccelerationStructure { @@ -186,8 +190,12 @@ pub enum CommittedIntersection { /// A ray query type which is an opaque object representing a ray traversal. #[spirv(ray_query)] +// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation. +#[repr(C)] pub struct RayQuery { - _private: u32, + // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way + // or another, before `#[spirv(ray_query)]` can special-case it). + _anti_zst_padding: core::mem::MaybeUninit, } /// Constructs an uninitialized ray query variable. Using the syntax diff --git a/crates/spirv-std/src/runtime_array.rs b/crates/spirv-std/src/runtime_array.rs index 36a8668914..0241acc79e 100644 --- a/crates/spirv-std/src/runtime_array.rs +++ b/crates/spirv-std/src/runtime_array.rs @@ -7,9 +7,12 @@ use core::marker::PhantomData; /// Hence, this type represents something very similar to a slice, but with no way of knowing its /// length. #[spirv(runtime_array)] +// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation. +#[repr(C)] pub struct RuntimeArray { - // spooky! this field does not exist, so if it's referenced in rust code, things will explode - _do_not_touch: u32, + // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way + // or another, before `#[spirv(runtime_array)]` can special-case it). + _anti_zst_padding: core::mem::MaybeUninit, _phantom: PhantomData, } diff --git a/crates/spirv-std/src/sampler.rs b/crates/spirv-std/src/sampler.rs index ef04a61642..6fb7ee8510 100644 --- a/crates/spirv-std/src/sampler.rs +++ b/crates/spirv-std/src/sampler.rs @@ -2,6 +2,10 @@ /// sample an image. #[spirv(sampler)] #[derive(Copy, Clone)] +// HACK(eddyb) avoids "transparent newtype of `_anti_zst_padding`" misinterpretation. +#[repr(C)] pub struct Sampler { - _x: u32, + // HACK(eddyb) avoids the layout becoming ZST (and being elided in one way + // or another, before `#[spirv(sampler)]` can special-case it). + _anti_zst_padding: core::mem::MaybeUninit, } diff --git a/tests/ui/image/gather_err.stderr b/tests/ui/image/gather_err.stderr index d4c0db36ec..0859eb76d8 100644 --- a/tests/ui/image/gather_err.stderr +++ b/tests/ui/image/gather_err.stderr @@ -9,9 +9,9 @@ error[E0277]: the trait bound `Image: HasGather` is not s Image Image note: required by a bound in `Image::::gather` - --> $SPIRV_STD_SRC/image.rs:163:15 + --> $SPIRV_STD_SRC/image.rs:167:15 | -163 | Self: HasGather, +167 | Self: HasGather, | ^^^^^^^^^ required by this bound in `Image::::gather` error[E0277]: the trait bound `Image: HasGather` is not satisfied @@ -25,9 +25,9 @@ error[E0277]: the trait bound `Image: HasGather` is not s Image Image note: required by a bound in `Image::::gather` - --> $SPIRV_STD_SRC/image.rs:163:15 + --> $SPIRV_STD_SRC/image.rs:167:15 | -163 | Self: HasGather, +167 | Self: HasGather, | ^^^^^^^^^ required by this bound in `Image::::gather` error: aborting due to 2 previous errors diff --git a/tests/ui/image/query/query_levels_err.stderr b/tests/ui/image/query/query_levels_err.stderr index 727f811ef4..7309e4c789 100644 --- a/tests/ui/image/query/query_levels_err.stderr +++ b/tests/ui/image/query/query_levels_err.stderr @@ -10,9 +10,9 @@ error[E0277]: the trait bound `Image: HasQueryLevels` is Image Image note: required by a bound in `Image::::query_levels` - --> $SPIRV_STD_SRC/image.rs:813:15 + --> $SPIRV_STD_SRC/image.rs:817:15 | -813 | Self: HasQueryLevels, +817 | Self: HasQueryLevels, | ^^^^^^^^^^^^^^ required by this bound in `Image::::query_levels` error: aborting due to previous error diff --git a/tests/ui/image/query/query_lod_err.stderr b/tests/ui/image/query/query_lod_err.stderr index efc521c338..2bb3f58573 100644 --- a/tests/ui/image/query/query_lod_err.stderr +++ b/tests/ui/image/query/query_lod_err.stderr @@ -10,9 +10,9 @@ error[E0277]: the trait bound `Image: HasQueryLevels` is Image Image note: required by a bound in `Image::::query_lod` - --> $SPIRV_STD_SRC/image.rs:839:15 + --> $SPIRV_STD_SRC/image.rs:843:15 | -839 | Self: HasQueryLevels, +843 | Self: HasQueryLevels, | ^^^^^^^^^^^^^^ required by this bound in `Image::::query_lod` error: aborting due to previous error diff --git a/tests/ui/image/query/query_size_err.stderr b/tests/ui/image/query/query_size_err.stderr index 39a30c274f..0e56d8efaf 100644 --- a/tests/ui/image/query/query_size_err.stderr +++ b/tests/ui/image/query/query_size_err.stderr @@ -15,9 +15,9 @@ error[E0277]: the trait bound `Image: HasQuerySize` is no Image and 6 others note: required by a bound in `Image::::query_size` - --> $SPIRV_STD_SRC/image.rs:870:15 + --> $SPIRV_STD_SRC/image.rs:874:15 | -870 | Self: HasQuerySize, +874 | Self: HasQuerySize, | ^^^^^^^^^^^^ required by this bound in `Image::::query_size` error: aborting due to previous error diff --git a/tests/ui/image/query/query_size_lod_err.stderr b/tests/ui/image/query/query_size_lod_err.stderr index 0b92123ca8..490eab595f 100644 --- a/tests/ui/image/query/query_size_lod_err.stderr +++ b/tests/ui/image/query/query_size_lod_err.stderr @@ -10,9 +10,9 @@ error[E0277]: the trait bound `Image: HasQuerySizeLod` is Image Image note: required by a bound in `Image::::query_size_lod` - --> $SPIRV_STD_SRC/image.rs:903:15 + --> $SPIRV_STD_SRC/image.rs:907:15 | -903 | Self: HasQuerySizeLod, +907 | Self: HasQuerySizeLod, | ^^^^^^^^^^^^^^^ required by this bound in `Image::::query_size_lod` error: aborting due to previous error