Use include_bytes! in shader! for automatic recompilation (#1523)

This commit is contained in:
mvilim 2021-03-31 03:35:35 -05:00 committed by GitHub
parent eb21fea03b
commit fc2e470d14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View File

@ -191,6 +191,7 @@ pub(super) fn reflect(
name: &str,
spirv: &[u32],
types_meta: TypesMeta,
full_path: Option<&str>,
dump: bool,
) -> Result<TokenStream, Error> {
let struct_name = Ident::new(&name, Span::call_site());
@ -255,6 +256,12 @@ pub(super) fn reflect(
}
}
let include_bytes = full_path.map(|s| quote! {
// using include_bytes here ensures that changing the shader will force recompilation.
// The bytes themselves can be optimized out by the compiler as they are unused.
let _bytes = ::std::include_bytes!( #s );
}).unwrap_or(TokenStream::new());
let structs = structs::write_structs(&doc, &types_meta);
let specialization_constants = spec_consts::write_specialization_constants(&doc, &types_meta);
let uses = &types_meta.uses;
@ -308,6 +315,8 @@ pub(super) fn reflect(
pub fn load(device: ::std::sync::Arc<::vulkano::device::Device>)
-> Result<#struct_name, ::vulkano::OomError>
{
#include_bytes
#( #cap_checks )*
static WORDS: &[u32] = &[ #( #spirv ),* ];

View File

@ -574,23 +574,27 @@ pub fn shader(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
"Shader",
unsafe { from_raw_parts(bytes.as_slice().as_ptr() as *const u32, bytes.len() / 4) },
input.types_meta,
None,
input.dump,
)
.unwrap()
.into()
} else {
let (path, source_code) = match input.source_kind {
SourceKind::Src(source) => (None, source),
SourceKind::Path(path) => (Some(path.clone()), {
let (path, full_path, source_code) = match input.source_kind {
SourceKind::Src(source) => (None, None, source),
SourceKind::Path(path) => {
let full_path = root_path.join(&path);
if full_path.is_file() {
(Some(path.clone()),
Some(full_path.to_str()
.expect("Path {:?} could not be converted to UTF-8").to_owned()),
read_file_to_string(&full_path)
.expect(&format!("Error reading source from {:?}", path))
.expect(&format!("Error reading source from {:?}", path)))
} else {
panic!("File {:?} was not found ; note that the path must be relative to your Cargo.toml", path);
}
}),
}
SourceKind::Bytes(_) => unreachable!(),
};
@ -617,7 +621,7 @@ pub fn shader(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Err(e) => panic!("{}", e.replace("(s): ", "(s):\n")),
};
codegen::reflect("Shader", content.as_binary(), input.types_meta, input.dump)
codegen::reflect("Shader", content.as_binary(), input.types_meta, full_path.as_deref(), input.dump)
.unwrap()
.into()
}