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

Use include_bytes! in shader! for automatic recompilation #1523

Merged
Merged
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
9 changes: 9 additions & 0 deletions vulkano-shaders/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ),* ];

Expand Down
16 changes: 10 additions & 6 deletions vulkano-shaders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
};

Expand All @@ -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()
}
Expand Down