bevy_crevice_derive/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
mod glsl;
mod layout;

use bevy_macro_utils::BevyManifest;
use proc_macro::TokenStream as CompilerTokenStream;

use syn::{parse_macro_input, DeriveInput, Path};

#[proc_macro_derive(AsStd140)]
pub fn derive_as_std140(input: CompilerTokenStream) -> CompilerTokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let expanded = layout::emit(input, "Std140", "std140", 16);

    CompilerTokenStream::from(expanded)
}

#[proc_macro_derive(AsStd430)]
pub fn derive_as_std430(input: CompilerTokenStream) -> CompilerTokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let expanded = layout::emit(input, "Std430", "std430", 0);

    CompilerTokenStream::from(expanded)
}

#[proc_macro_derive(GlslStruct)]
pub fn derive_glsl_struct(input: CompilerTokenStream) -> CompilerTokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let expanded = glsl::emit(input);

    CompilerTokenStream::from(expanded)
}

const BEVY: &str = "bevy";
const BEVY_CREVICE: &str = "bevy_crevice";
const BEVY_RENDER: &str = "bevy_render";

fn bevy_crevice_path() -> Path {
    let bevy_manifest = BevyManifest::default();
    bevy_manifest
        .maybe_get_path(crate::BEVY)
        .map(|bevy_path| {
            let mut segments = bevy_path.segments;
            segments.push(BevyManifest::parse_str("render"));
            Path {
                leading_colon: None,
                segments,
            }
        })
        .or_else(|| bevy_manifest.maybe_get_path(crate::BEVY_RENDER))
        .map(|bevy_render_path| {
            let mut segments = bevy_render_path.segments;
            segments.push(BevyManifest::parse_str("render_resource"));
            Path {
                leading_colon: None,
                segments,
            }
        })
        .unwrap_or_else(|| bevy_manifest.get_path(crate::BEVY_CREVICE))
}