Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AccessQualifiers to Image, the Image! macro and codegen #1116

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ fn trans_intrinsic_type<'tcx>(
let multisampled = const_int_value(cx, args.const_at(4))?;
let sampled = const_int_value(cx, args.const_at(5))?;
let image_format = const_int_value(cx, args.const_at(6))?;
let access_qualifier = const_int_value(cx, args.const_at(7))?;

let ty = SpirvType::Image {
sampled_type,
Expand All @@ -886,6 +887,7 @@ fn trans_intrinsic_type<'tcx>(
multisampled,
sampled,
image_format,
access_qualifier,
};
Ok(ty.def(span, cx))
}
Expand Down
1 change: 1 addition & 0 deletions crates/rustc_codegen_spirv/src/builder/spirv_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {
multisampled: inst.operands[4].unwrap_literal_int32(),
sampled: inst.operands[5].unwrap_literal_int32(),
image_format: inst.operands[6].unwrap_image_format(),
access_qualifier: inst.operands[7].unwrap_access_qualifier()
}
.def(self.span(), self),
Op::TypeSampledImage => SpirvType::SampledImage {
Expand Down
12 changes: 10 additions & 2 deletions crates/rustc_codegen_spirv/src/spirv_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::builder_spirv::SpirvValue;
use crate::codegen_cx::CodegenCx;
use indexmap::IndexSet;
use rspirv::dr::Operand;
use rspirv::spirv::{Capability, Decoration, Dim, ImageFormat, StorageClass, Word};
use rspirv::spirv::{Capability, Decoration, Dim, ImageFormat, StorageClass, Word, AccessQualifier};
use rustc_data_structures::fx::FxHashMap;
use rustc_middle::span_bug;
use rustc_span::def_id::DefId;
Expand Down Expand Up @@ -74,6 +74,7 @@ pub enum SpirvType<'tcx> {
multisampled: u32,
sampled: u32,
image_format: ImageFormat,
access_qualifier: AccessQualifier,
},
Sampler,
SampledImage {
Expand Down Expand Up @@ -236,6 +237,7 @@ impl SpirvType<'_> {
multisampled,
sampled,
image_format,
access_qualifier,
} => cx.emit_global().type_image_id(
id,
sampled_type,
Expand All @@ -245,7 +247,7 @@ impl SpirvType<'_> {
multisampled,
sampled,
image_format,
None,
Some(access_qualifier)
),
Self::Sampler => cx.emit_global().type_sampler_id(id),
Self::AccelerationStructureKhr => {
Expand Down Expand Up @@ -413,6 +415,7 @@ impl SpirvType<'_> {
multisampled,
sampled,
image_format,
access_qualifier
} => SpirvType::Image {
sampled_type,
dim,
Expand All @@ -421,6 +424,7 @@ impl SpirvType<'_> {
multisampled,
sampled,
image_format,
access_qualifier,
},
SpirvType::Sampler => SpirvType::Sampler,
SpirvType::SampledImage { image_type } => SpirvType::SampledImage { image_type },
Expand Down Expand Up @@ -576,6 +580,7 @@ impl fmt::Debug for SpirvTypePrinter<'_, '_> {
multisampled,
sampled,
image_format,
access_qualifier,
} => f
.debug_struct("Image")
.field("id", &self.id)
Expand All @@ -586,6 +591,7 @@ impl fmt::Debug for SpirvTypePrinter<'_, '_> {
.field("multisampled", &multisampled)
.field("sampled", &sampled)
.field("image_format", &image_format)
.field("access_qualifier", &access_qualifier)
.finish(),
SpirvType::Sampler => f.debug_struct("Sampler").field("id", &self.id).finish(),
SpirvType::SampledImage { image_type } => f
Expand Down Expand Up @@ -731,6 +737,7 @@ impl SpirvTypePrinter<'_, '_> {
multisampled,
sampled,
image_format,
access_qualifier
} => f
.debug_struct("Image")
.field("sampled_type", &self.cx.debug_type(sampled_type))
Expand All @@ -740,6 +747,7 @@ impl SpirvTypePrinter<'_, '_> {
.field("multisampled", &multisampled)
.field("sampled", &sampled)
.field("image_format", &image_format)
.field("access_qualifier", &access_qualifier)
.finish(),
SpirvType::Sampler => f.write_str("Sampler"),
SpirvType::SampledImage { image_type } => f
Expand Down
43 changes: 43 additions & 0 deletions crates/spirv-std/macros/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct ImageType {
sampled: Sampled,
sampled_type: SampledType,
components: u32,
access_qualifier: AccessQualifier,
}

impl Parse for ImageType {
Expand All @@ -47,6 +48,7 @@ impl Parse for ImageType {
let mut sampled: Option<Sampled> = None;
let mut crate_root = None;
let mut components = None;
let mut access_qualifier = None;

let starting_span = input.span();

Expand Down Expand Up @@ -160,6 +162,22 @@ impl Parse for ImageType {
)),
}
);
} else if ident == "access" {
let value = peek_and_eat_value!(syn::Ident);

if value.is_none() {
return Err(syn::Error::new(
ident.span(),
"Expected argument for `access`.",
));
}
let value = params::access_qualifier_from_str(&value.unwrap().to_string());

if let Err(err) = value {
return Err(syn::Error::new(ident.span(), err));
}

access_qualifier = value.ok();
} else if ident == "__crate_root" {
input.parse::<syn::Token![=]>()?;
crate_root = Some(input.parse::<syn::Path>()?);
Expand Down Expand Up @@ -366,6 +384,7 @@ impl Parse for ImageType {
let multisampled = multisampled.unwrap_or(Multisampled::False);
let sampled = sampled.unwrap_or(Sampled::Unknown);
let components = components.unwrap_or(4);
let access_qualifier = access_qualifier.unwrap_or(AccessQualifier::ReadOnly);

Ok(Self {
arrayed,
Expand All @@ -377,6 +396,7 @@ impl Parse for ImageType {
sampled,
sampled_type,
components,
access_qualifier,
})
}
}
Expand All @@ -400,6 +420,7 @@ impl quote::ToTokens for ImageType {
let sampled = params::sampled_to_tokens(&self.sampled);
let sampled_type = &self.sampled_type;
let components = self.components;
let access_qualifier = params::access_qualifier_to_tokens(&self.access_qualifier);

tokens.append_all(quote::quote! {
#crate_root::image::Image<
Expand All @@ -410,6 +431,7 @@ impl quote::ToTokens for ImageType {
{ #crate_root::image::#multisampled as u32 },
{ #crate_root::image::#sampled as u32 },
{ #crate_root::image::#format as u32 },
{ #crate_root::image::#access_qualifier as u32 },
{ #components as u32 },
>
});
Expand Down Expand Up @@ -471,6 +493,19 @@ mod params {
})
}

pub fn access_qualifier_from_str(s: &str) -> Result<AccessQualifier, &'static str> {
Ok(match s {
"readonly" => AccessQualifier::ReadOnly,
"writeonly" => AccessQualifier::WriteOnly,
"readwrite" => AccessQualifier::ReadWrite,
_ => {
return Err(
"Unknown specified access qualifier. Use `access=<access>` instead if this is intentional.",
);
}
})
}

/// The sampled type of an unknown image format.
pub enum SampledType {
U8,
Expand Down Expand Up @@ -598,4 +633,12 @@ mod params {

quote!(ImageFormat::#variant)
}

pub fn access_qualifier_to_tokens(access: &AccessQualifier) -> proc_macro2::TokenStream {
match access {
AccessQualifier::ReadOnly => quote!(AccessQualifier::ReadOnly),
AccessQualifier::WriteOnly => quote!(AccessQualifier::WriteOnly),
AccessQualifier::ReadWrite => quote!(AccessQualifier::ReadWrite),
}
}
}
Loading