Skip to content

Commit

Permalink
feat: added image access qualifier, fixes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
schell committed Oct 11, 2024
1 parent 18b9f75 commit c9ba1f7
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 58 deletions.
106 changes: 53 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 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: Option<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 = None;

let starting_span = input.span();

Expand Down Expand Up @@ -122,6 +124,16 @@ impl Parse for ImageType {
.map(syn::LitBool::value)
.map_or(ImageDepth::True, From::from)
);
} else if ident == "access" {
let value = peek_and_eat_value!(syn::Ident);
let value = params::image_access_from_str(
value
.map(|id| id.to_string())
.as_deref()
.unwrap_or("unknown"),
)
.map_err(|e| syn::Error::new(ident.span(), e))?;
access = Some(value);
} else if ident == "format" {
let value = peek_and_eat_value!(syn::Ident);

Expand Down Expand Up @@ -377,6 +389,7 @@ impl Parse for ImageType {
sampled,
sampled_type,
components,
access,
})
}
}
Expand All @@ -400,6 +413,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 = params::image_access_to_tokens(self.access);

tokens.append_all(quote::quote! {
#crate_root::image::Image<
Expand All @@ -411,6 +425,7 @@ impl quote::ToTokens for ImageType {
{ #crate_root::image::#sampled as u32 },
{ #crate_root::image::#format as u32 },
{ #components as u32 },
#crate_root::image::__private::#access
>
});
}
Expand All @@ -420,6 +435,20 @@ mod params {
use super::*;
use proc_macro2::TokenStream;

pub fn image_access_from_str(s: &str) -> Result<AccessQualifier, &'static str> {
Ok(match s {
"readonly" => AccessQualifier::ReadOnly,
"writeonly" => AccessQualifier::WriteOnly,
"readwrite" => AccessQualifier::ReadWrite,
_ => {
return Err(
"Unknown specified image access. Must be `readonly`, `writeonly`, \
`readwrite`, or must be omitted.",
);
}
})
}

pub fn image_format_from_str(s: &str) -> Result<ImageFormat, &'static str> {
Ok(match s {
"rgba32f" => ImageFormat::Rgba32f,
Expand Down Expand Up @@ -544,6 +573,15 @@ mod params {
}
}

pub fn image_access_to_tokens(access: Option<AccessQualifier>) -> proc_macro2::TokenStream {
match access {
Some(AccessQualifier::ReadOnly) => quote!(ImageAccessReadOnly),
Some(AccessQualifier::WriteOnly) => quote!(ImageAccessWriteOnly),
Some(AccessQualifier::ReadWrite) => quote!(ImageAccessReadWrite),
None => quote!(ImageAccessUnknown),
}
}

pub fn image_format_to_tokens(format: &ImageFormat) -> proc_macro2::TokenStream {
let variant = {
let variant = match format {
Expand Down
Loading

0 comments on commit c9ba1f7

Please sign in to comment.