Skip to content

Commit 353bb96

Browse files
authored
Add gpu_only proc macro (#392)
1 parent 19cf332 commit 353bb96

File tree

6 files changed

+39
-25
lines changed

6 files changed

+39
-25
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spirv-std-macros/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ repository = "https://github.com/EmbarkStudios/rust-gpu"
88

99
[lib]
1010
proc-macro = true
11+
12+
[dependencies]
13+
quote = "1.0.8"
14+
syn = { version = "1.0.58", features=["full"] }

crates/spirv-std-macros/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,28 @@ pub fn spirv(_attr: TokenStream, item: TokenStream) -> TokenStream {
2929
}
3030
tokens.into_iter().collect()
3131
}
32+
33+
#[proc_macro_attribute]
34+
pub fn gpu_only(_attr: TokenStream, item: TokenStream) -> TokenStream {
35+
let syn::ItemFn {
36+
attrs,
37+
vis,
38+
sig,
39+
block,
40+
} = syn::parse_macro_input!(item as syn::ItemFn);
41+
42+
let fn_name = sig.ident.clone();
43+
44+
let output = quote::quote! {
45+
// Don't warn on unused arguments on the CPU side.
46+
#[cfg_attr(not(target_arch = "spirv"), allow(unused_variables))]
47+
#(#attrs)* #vis #sig {
48+
#[cfg(target_arch="spirv")] { #block }
49+
#[cfg(not(target_arch="spirv"))] {
50+
unimplemented!(concat!("`", stringify!(#fn_name), "` is only available on SPIR-V platforms."))
51+
}
52+
}
53+
};
54+
55+
output.into()
56+
}

crates/spirv-std/src/derivative.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ macro_rules! deriv_caps {
5656

5757
macro_rules! deriv_fn {
5858
($name:ident, $inst:ident, $needs_caps:tt) => {
59+
#[spirv_std_macros::gpu_only]
5960
fn $name(self) -> Self {
60-
#[cfg(not(target_arch = "spirv"))]
61-
panic!(concat!(stringify!($name), " is not supported on the CPU"));
62-
#[cfg(target_arch = "spirv")]
6361
unsafe {
6462
let mut o = Default::default();
6563
deriv_caps!($needs_caps);

crates/spirv-std/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub use num_traits;
5252
pub use textures::*;
5353

5454
/// Calls the `OpDemoteToHelperInvocationEXT` instruction, which corresponds to discard() in HLSL
55+
#[spirv_std_macros::gpu_only]
5556
pub fn demote_to_helper_invocation() {
56-
#[cfg(target_arch = "spirv")]
5757
unsafe {
5858
asm!(
5959
"OpExtension \"SPV_EXT_demote_to_helper_invocation\"",
@@ -64,8 +64,8 @@ pub fn demote_to_helper_invocation() {
6464
}
6565

6666
/// Calls the `OpKill` instruction, which corresponds to discard() in GLSL
67+
#[spirv_std_macros::gpu_only]
6768
pub fn discard() {
68-
#[cfg(target_arch = "spirv")]
6969
unsafe {
7070
asm!("OpKill", "%unused = OpLabel");
7171
}

crates/spirv-std/src/textures.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,8 @@ pub struct Image2d {
2323
}
2424

2525
impl Image2d {
26+
#[spirv_std_macros::gpu_only]
2627
pub fn sample(&self, sampler: Sampler, coord: Vec2) -> Vec4 {
27-
#[cfg(not(target_arch = "spirv"))]
28-
{
29-
let _ = sampler;
30-
let _ = coord;
31-
panic!("Image sampling not supported on CPU");
32-
}
33-
#[cfg(target_arch = "spirv")]
3428
unsafe {
3529
let mut result = Default::default();
3630
asm!(
@@ -66,14 +60,8 @@ pub struct Image2dArray {
6660
}
6761

6862
impl Image2dArray {
63+
#[spirv_std_macros::gpu_only]
6964
pub fn sample(&self, sampler: Sampler, coord: Vec3A) -> Vec4 {
70-
#[cfg(not(target_arch = "spirv"))]
71-
{
72-
let _ = sampler;
73-
let _ = coord;
74-
panic!("Image sampling not supported on CPU");
75-
}
76-
#[cfg(target_arch = "spirv")]
7765
unsafe {
7866
let mut result = Default::default();
7967
asm!(
@@ -101,13 +89,8 @@ pub struct SampledImage<I> {
10189
}
10290

10391
impl SampledImage<Image2d> {
92+
#[spirv_std_macros::gpu_only]
10493
pub fn sample(&self, coord: Vec2) -> Vec4 {
105-
#[cfg(not(target_arch = "spirv"))]
106-
{
107-
let _ = coord;
108-
panic!("Image sampling not supported on CPU");
109-
}
110-
#[cfg(target_arch = "spirv")]
11194
unsafe {
11295
let mut result = Default::default();
11396
asm!(

0 commit comments

Comments
 (0)