diff --git a/vulkano-shaders/Cargo.toml b/vulkano-shaders/Cargo.toml index b0d38f18..736eb3b8 100644 --- a/vulkano-shaders/Cargo.toml +++ b/vulkano-shaders/Cargo.toml @@ -3,5 +3,5 @@ name = "vulkano-shaders" version = "0.1.0" authors = ["Pierre Krieger "] -[dev-dependencies] +[dependencies] glsl-to-spirv = { path = "../glsl-to-spirv" } diff --git a/vulkano-shaders/src/lib.rs b/vulkano-shaders/src/lib.rs index 5b04c738..cd3dc7b0 100644 --- a/vulkano-shaders/src/lib.rs +++ b/vulkano-shaders/src/lib.rs @@ -1,12 +1,48 @@ +extern crate glsl_to_spirv; + use std::collections::HashSet; +use std::env; +use std::fs; +use std::fs::File; use std::io::Error as IoError; use std::io::Read; +use std::io::Write; +use std::path::Path; pub use parse::ParseError; +pub use glsl_to_spirv::ShaderType; mod enums; mod parse; +pub fn build_glsl_shaders<'a, I>(shaders: I) + where I: IntoIterator +{ + let dest = env::var("OUT_DIR").unwrap(); + let dest = Path::new(&dest); + + for (shader, ty) in shaders { + println!("cargo:rerun-if-changed={}", shader); + let shader = Path::new(shader); + + let shader_content = { + let mut s = String::new(); + File::open(shader).expect("failed to open shader").read_to_string(&mut s) + .expect("failed to read shader content"); + s + }; + + let out_file = dest.join("shaders").join(shader); + fs::create_dir_all(&dest.join("shaders")).unwrap(); + let mut file_output = File::create(&dest.join("shaders").join(shader)) + .expect("failed to open shader output"); + + let content = glsl_to_spirv::compile(&shader_content, ty).unwrap(); + let output = reflect("Shader", content).unwrap(); + write!(file_output, "{}", output).unwrap(); + } +} + pub fn reflect(name: &str, mut spirv: R) -> Result where R: Read { diff --git a/vulkano/Cargo.toml b/vulkano/Cargo.toml index 553182f9..282abf84 100644 --- a/vulkano/Cargo.toml +++ b/vulkano/Cargo.toml @@ -11,7 +11,6 @@ lazy_static = "0.1.15" [build-dependencies] vk-sys = { path = "../vk-sys" } -glsl-to-spirv = { path = "../glsl-to-spirv" } vulkano-shaders = { path = "../vulkano-shaders" } [dev-dependencies] diff --git a/vulkano/build.rs b/vulkano/build.rs index fc8b33b9..3dc71c92 100644 --- a/vulkano/build.rs +++ b/vulkano/build.rs @@ -1,4 +1,3 @@ -extern crate glsl_to_spirv; extern crate vulkano_shaders; extern crate vk_sys; @@ -17,46 +16,13 @@ fn main() { let mut file_output = File::create(&dest.join("vk_bindings.rs")).unwrap(); vk_sys::write_bindings(&mut file_output).unwrap(); - write_examples(); -} - -fn write_examples() { - let dest = env::var("OUT_DIR").unwrap(); - let dest = Path::new(&dest); - - let mut file_output = File::create(&dest.join("examples-triangle_vs.rs")).unwrap(); - println!("cargo:rerun-if-changed=examples/triangle_vs.glsl"); - let content = glsl_to_spirv::compile(include_str!("examples/triangle_vs.glsl"), glsl_to_spirv::ShaderType::Vertex).unwrap(); - let output = vulkano_shaders::reflect("TriangleShader", content).unwrap(); - write!(file_output, "{}", output).unwrap(); - - let mut file_output = File::create(&dest.join("examples-triangle_fs.rs")).unwrap(); - println!("cargo:rerun-if-changed=examples/triangle_fs.glsl"); - let content = glsl_to_spirv::compile(include_str!("examples/triangle_fs.glsl"), glsl_to_spirv::ShaderType::Fragment).unwrap(); - let output = vulkano_shaders::reflect("TriangleShader", content).unwrap(); - write!(file_output, "{}", output).unwrap(); - - let mut file_output = File::create(&dest.join("examples-teapot_vs.rs")).unwrap(); - println!("cargo:rerun-if-changed=examples/teapot_vs.glsl"); - let content = glsl_to_spirv::compile(include_str!("examples/teapot_vs.glsl"), glsl_to_spirv::ShaderType::Vertex).unwrap(); - let output = vulkano_shaders::reflect("TeapotShader", content).unwrap(); - write!(file_output, "{}", output).unwrap(); - - let mut file_output = File::create(&dest.join("examples-teapot_fs.rs")).unwrap(); - println!("cargo:rerun-if-changed=examples/teapot_fs.glsl"); - let content = glsl_to_spirv::compile(include_str!("examples/teapot_fs.glsl"), glsl_to_spirv::ShaderType::Fragment).unwrap(); - let output = vulkano_shaders::reflect("TeapotShader", content).unwrap(); - write!(file_output, "{}", output).unwrap(); - - let mut file_output = File::create(&dest.join("examples-image_vs.rs")).unwrap(); - println!("cargo:rerun-if-changed=examples/image_vs.glsl"); - let content = glsl_to_spirv::compile(include_str!("examples/image_vs.glsl"), glsl_to_spirv::ShaderType::Vertex).unwrap(); - let output = vulkano_shaders::reflect("ImageShader", content).unwrap(); - write!(file_output, "{}", output).unwrap(); - - let mut file_output = File::create(&dest.join("examples-image_fs.rs")).unwrap(); - println!("cargo:rerun-if-changed=examples/image_fs.glsl"); - let content = glsl_to_spirv::compile(include_str!("examples/image_fs.glsl"), glsl_to_spirv::ShaderType::Fragment).unwrap(); - let output = vulkano_shaders::reflect("ImageShader", content).unwrap(); - write!(file_output, "{}", output).unwrap(); + // building the shaders used in the examples + vulkano_shaders::build_glsl_shaders([ + ("examples/triangle_vs.glsl", vulkano_shaders::ShaderType::Vertex), + ("examples/triangle_fs.glsl", vulkano_shaders::ShaderType::Fragment), + ("examples/teapot_vs.glsl", vulkano_shaders::ShaderType::Vertex), + ("examples/teapot_fs.glsl", vulkano_shaders::ShaderType::Fragment), + ("examples/image_vs.glsl", vulkano_shaders::ShaderType::Vertex), + ("examples/image_fs.glsl", vulkano_shaders::ShaderType::Fragment), + ].iter().cloned()); } diff --git a/vulkano/examples/image.rs b/vulkano/examples/image.rs index cc5f0e34..5837acfb 100644 --- a/vulkano/examples/image.rs +++ b/vulkano/examples/image.rs @@ -80,10 +80,10 @@ fn main() { } - mod vs { include!{concat!(env!("OUT_DIR"), "/examples-image_vs.rs")} } - let vs = vs::ImageShader::load(&device).expect("failed to create shader module"); - mod fs { include!{concat!(env!("OUT_DIR"), "/examples-image_fs.rs")} } - let fs = fs::ImageShader::load(&device).expect("failed to create shader module"); + mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/image_vs.glsl")} } + let vs = vs::Shader::load(&device).expect("failed to create shader module"); + mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/image_fs.glsl")} } + let fs = fs::Shader::load(&device).expect("failed to create shader module"); let renderpass = single_pass_renderpass!{ device: &device, diff --git a/vulkano/examples/teapot.rs b/vulkano/examples/teapot.rs index 2f31c7b1..a5716709 100644 --- a/vulkano/examples/teapot.rs +++ b/vulkano/examples/teapot.rs @@ -119,10 +119,10 @@ fn main() { mapping.1 = proj.into(); } - mod vs { include!{concat!(env!("OUT_DIR"), "/examples-teapot_vs.rs")} } - let vs = vs::TeapotShader::load(&device).expect("failed to create shader module"); - mod fs { include!{concat!(env!("OUT_DIR"), "/examples-teapot_fs.rs")} } - let fs = fs::TeapotShader::load(&device).expect("failed to create shader module"); + mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/teapot_vs.glsl")} } + let vs = vs::Shader::load(&device).expect("failed to create shader module"); + mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/teapot_fs.glsl")} } + let fs = fs::Shader::load(&device).expect("failed to create shader module"); let images = images.into_iter().map(|image| { let image = image.transition(vulkano::image::Layout::PresentSrc, &cb_pool, diff --git a/vulkano/examples/triangle.rs b/vulkano/examples/triangle.rs index 6fd5eeef..77de7fc2 100644 --- a/vulkano/examples/triangle.rs +++ b/vulkano/examples/triangle.rs @@ -130,10 +130,10 @@ fn main() { // can now use to load the shader. // // Because of some restrictions with the `include!` macro, we need to use a module. - mod vs { include!{concat!(env!("OUT_DIR"), "/examples-triangle_vs.rs")} } - let vs = vs::TriangleShader::load(&device).expect("failed to create shader module"); - mod fs { include!{concat!(env!("OUT_DIR"), "/examples-triangle_fs.rs")} } - let fs = fs::TriangleShader::load(&device).expect("failed to create shader module"); + mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/triangle_vs.glsl")} } + let vs = vs::Shader::load(&device).expect("failed to create shader module"); + mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/triangle_fs.glsl")} } + let fs = fs::Shader::load(&device).expect("failed to create shader module"); // At this point, OpenGL initialization would be finished. However in Vulkan it is not. OpenGL // implicitely does a lot of computation whenever you draw. In Vulkan, you have to do all this