Skip to content
This repository was archived by the owner on Oct 31, 2025. It is now read-only.

Commit d5dcee5

Browse files
committed
Add documentation and add ImageCoordinate trait
1 parent 199a453 commit d5dcee5

File tree

4 files changed

+60
-59
lines changed

4 files changed

+60
-59
lines changed

crates/rustc_codegen_spirv/src/abi.rs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -810,57 +810,45 @@ fn trans_image<'tcx>(
810810
// Hardcode to float for now
811811
let sampled_type = SpirvType::Float(32).def(span, cx);
812812

813-
macro_rules! type_from_variant_index {
813+
macro_rules! type_from_variant_discriminant {
814814
($(let $name:ident : $ty:ty = $exp:expr);+ ;) => {
815-
$(
816-
let $name = <$ty>::from_u64(
817-
cx.tcx
818-
.destructure_const(ParamEnv::reveal_all().and($exp))
819-
.variant
815+
$(let $name = {
816+
let const_ = $exp;
817+
let adt_def = const_.ty.ty_adt_def().unwrap();
818+
assert!(adt_def.is_enum());
819+
let destructured = cx.tcx.destructure_const(ParamEnv::reveal_all().and(const_));
820+
let idx = destructured.variant.unwrap();
821+
let value = const_.ty
822+
.discriminant_for_variant(cx.tcx, idx)
820823
.unwrap()
821-
.as_u32() as u64,
822-
)
823-
.unwrap();
824-
)+
824+
.val as u64;
825+
<$ty>::from_u64(value).unwrap()
826+
};)+
825827
}
826828
}
827829

828-
type_from_variant_index! {
830+
type_from_variant_discriminant! {
829831
let dim: spirv::Dim = substs.const_at(0);
830832
let depth: u32 = substs.const_at(1);
831833
let sampled: u32 = substs.const_at(4);
832834
let image_format: spirv::ImageFormat = substs.const_at(5);
833835
}
834836

835-
let arrayed: bool = substs
836-
.const_at(2)
837-
.val
838-
.try_to_value()
839-
.unwrap()
840-
.try_to_bool()
841-
.unwrap();
842-
let multisampled: bool = substs
843-
.const_at(3)
844-
.val
845-
.try_to_value()
846-
.unwrap()
847-
.try_to_bool()
848-
.unwrap();
837+
let arrayed: bool = substs.const_at(2).val.try_to_bool().unwrap();
838+
let multisampled: bool = substs.const_at(3).val.try_to_bool().unwrap();
849839
let access_qualifier = {
850840
let option = cx
851841
.tcx
852842
.destructure_const(ParamEnv::reveal_all().and(substs.const_at(6)));
853843

854844
match option.variant.map(|i| i.as_u32()).unwrap_or(0) {
855845
0 => None,
856-
1 => spirv::AccessQualifier::from_u64(
857-
option.fields[0]
858-
.val
859-
.try_to_scalar()
860-
.unwrap()
861-
.to_u64()
862-
.unwrap(),
863-
),
846+
1 => {
847+
type_from_variant_discriminant! {
848+
let access_qualifier: spirv::AccessQualifier = option.fields[0];
849+
}
850+
Some(access_qualifier)
851+
},
864852
_ => unreachable!(),
865853
}
866854
};

crates/spirv-std/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ pub use glam;
5353
pub use num_traits;
5454
pub use textures::*;
5555

56-
/// Marker trait for arguments that accept single scalar values or vectors
57-
/// of scalars.
58-
pub trait ScalarOrVector: sealed::Sealed {}
59-
60-
impl ScalarOrVector for f32 {}
61-
impl ScalarOrVector for glam::Vec2 {}
62-
impl ScalarOrVector for glam::Vec3 {}
63-
impl ScalarOrVector for glam::Vec3A {}
64-
impl ScalarOrVector for glam::Vec4 {}
65-
6656
#[cfg(all(not(test), target_arch = "spirv"))]
6757
#[panic_handler]
6858
fn panic(_: &core::panic::PanicInfo<'_>) -> ! {

crates/spirv-std/src/textures.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use glam::Vec4;
2-
3-
use crate::ScalarOrVector;
1+
use glam::{Vec2, Vec3, Vec3A, Vec4};
42

3+
/// The dimension of the image.
54
#[derive(Copy, Clone, PartialEq, Eq)]
65
pub enum Dimensionality {
76
OneD = 0,
@@ -13,20 +12,35 @@ pub enum Dimensionality {
1312
SubpassData = 6,
1413
}
1514

15+
/// Whether a given image contains [depth] information. **Note** Whether or not
16+
/// to perform depth comparisons is a property of the sampling code, not of this
17+
/// type declaration.
18+
///
19+
/// [depth]: https://en.wikipedia.org/wiki/Depth_map
1620
#[derive(Copy, Clone, PartialEq, Eq)]
1721
pub enum ImageDepth {
22+
/// Indicates that the image does not contain depth information.
1823
No = 0,
24+
/// Indicates that the image contains depth information.
1925
Yes = 1,
26+
/// Indicates that is not known ahead of time whether the image has depth
27+
/// information or not.
2028
Unknown = 2,
2129
}
2230

31+
/// Whether or not the image will be accessed in combination with a sampler.
2332
#[derive(Copy, Clone, PartialEq, Eq)]
2433
pub enum Sampled {
34+
/// Indicates that it is not known ahead of time whether the image will use
35+
/// a sampler or not.
2536
Unknown = 0,
37+
/// The image will be used with a sampler.
2638
Yes = 1,
39+
/// The image will not be used with a sampler.
2740
No = 2,
2841
}
2942

43+
/// The underlying internal representation of the image.
3044
#[derive(Copy, Clone, PartialEq, Eq)]
3145
pub enum ImageFormat {
3246
Unknown = 0,
@@ -71,23 +85,32 @@ pub enum ImageFormat {
7185
R8ui = 39,
7286
}
7387

88+
/// The access permissions for the image.
7489
#[derive(Copy, Clone, PartialEq, Eq)]
7590
pub enum AccessQualifier {
91+
/// A read only image.
7692
ReadOnly = 0,
93+
/// A write only image.
7794
WriteOnly = 1,
95+
/// A readable and writable image.
7896
ReadWrite = 2,
7997
}
8098

99+
/// Marker trait for arguments that accept single scalar values or vectors
100+
/// of scalars.
101+
pub trait ImageCoordinate<const DIM: Dimensionality>: crate::sealed::Sealed {}
102+
103+
impl ImageCoordinate<{ Dimensionality::OneD }> for f32 {}
104+
impl ImageCoordinate<{ Dimensionality::TwoD }> for Vec2 {}
105+
impl ImageCoordinate<{ Dimensionality::ThreeD }> for Vec3 {}
106+
impl ImageCoordinate<{ Dimensionality::ThreeD }> for Vec3A {}
107+
impl ImageCoordinate<{ Dimensionality::Rect }> for Vec2 {}
108+
impl ImageCoordinate<{ Dimensionality::Cube }> for Vec2 {}
109+
impl ImageCoordinate<{ Dimensionality::Buffer }> for f32 {}
110+
111+
/// An opaque image type. Corresponds to `OpTypeImage`.
81112
#[allow(unused_attributes)]
82-
#[spirv(image(
83-
// sampled_type is hardcoded to f32 for now
84-
dim = "Dim2D",
85-
depth = 0,
86-
arrayed = 0,
87-
multisampled = 0,
88-
sampled = 1,
89-
image_format = "Unknown"
90-
))]
113+
#[spirv(image)]
91114
#[derive(Copy, Clone)]
92115
pub struct Image<
93116
const DIM: Dimensionality,
@@ -120,7 +143,7 @@ impl<
120143
{ ACCESS_QUALIFIER },
121144
>
122145
{
123-
pub fn sample(&self, sampler: Sampler, coord: impl ScalarOrVector) -> Vec4 {
146+
pub fn sample(&self, sampler: Sampler, coord: impl ImageCoordinate<{ DIM }>) -> Vec4 {
124147
#[cfg(not(target_arch = "spirv"))]
125148
{
126149
let _ = sampler;
@@ -184,7 +207,7 @@ impl<
184207
>,
185208
>
186209
{
187-
pub fn sample(&self, coord: impl ScalarOrVector) -> Vec4 {
210+
pub fn sample(&self, coord: impl ImageCoordinate<{ DIM }>) -> Vec4 {
188211
#[cfg(not(target_arch = "spirv"))]
189212
{
190213
let _ = coord;

examples/shaders/sky-shader/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub extern crate spirv_std_macros;
1313

1414
use core::f32::consts::PI;
1515
use shared::*;
16-
use spirv_std::glam::{const_vec3, vec2, vec3, Vec2, Vec3, Vec4, Vec4Swizzles};
16+
use spirv_std::glam::{const_vec3, vec2, vec3, Vec2, Vec3, Vec4};
1717
use spirv_std::storage_class::{Input, Output, PushConstant, UniformConstant};
1818
use spirv_std::{Dimensionality, Image, ImageDepth, ImageFormat, Sampled, Sampler};
1919

@@ -160,7 +160,7 @@ pub fn fs(constants: &ShaderConstants, frag_coord: Vec2) -> Vec4 {
160160
#[allow(unused_attributes)]
161161
#[spirv(fragment)]
162162
pub fn main_fs(
163-
#[spirv(frag_coord)] in_frag_coord: Input<Vec4>,
163+
#[spirv(frag_coord)] in_frag_coord: Input<Vec2>,
164164
constants: PushConstant<ShaderConstants>,
165165
#[spirv(binding = 0)] img: UniformConstant<
166166
Image<

0 commit comments

Comments
 (0)