Add easier shaders compilation path

This commit is contained in:
Pierre Krieger 2016-02-25 08:52:22 +01:00
parent 41831c30cb
commit ddcb8cf402
7 changed files with 58 additions and 57 deletions

View File

@ -3,5 +3,5 @@ name = "vulkano-shaders"
version = "0.1.0" version = "0.1.0"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"] authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
[dev-dependencies] [dependencies]
glsl-to-spirv = { path = "../glsl-to-spirv" } glsl-to-spirv = { path = "../glsl-to-spirv" }

View File

@ -1,12 +1,48 @@
extern crate glsl_to_spirv;
use std::collections::HashSet; use std::collections::HashSet;
use std::env;
use std::fs;
use std::fs::File;
use std::io::Error as IoError; use std::io::Error as IoError;
use std::io::Read; use std::io::Read;
use std::io::Write;
use std::path::Path;
pub use parse::ParseError; pub use parse::ParseError;
pub use glsl_to_spirv::ShaderType;
mod enums; mod enums;
mod parse; mod parse;
pub fn build_glsl_shaders<'a, I>(shaders: I)
where I: IntoIterator<Item = (&'a str, ShaderType)>
{
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<R>(name: &str, mut spirv: R) -> Result<String, Error> pub fn reflect<R>(name: &str, mut spirv: R) -> Result<String, Error>
where R: Read where R: Read
{ {

View File

@ -11,7 +11,6 @@ lazy_static = "0.1.15"
[build-dependencies] [build-dependencies]
vk-sys = { path = "../vk-sys" } vk-sys = { path = "../vk-sys" }
glsl-to-spirv = { path = "../glsl-to-spirv" }
vulkano-shaders = { path = "../vulkano-shaders" } vulkano-shaders = { path = "../vulkano-shaders" }
[dev-dependencies] [dev-dependencies]

View File

@ -1,4 +1,3 @@
extern crate glsl_to_spirv;
extern crate vulkano_shaders; extern crate vulkano_shaders;
extern crate vk_sys; extern crate vk_sys;
@ -17,46 +16,13 @@ fn main() {
let mut file_output = File::create(&dest.join("vk_bindings.rs")).unwrap(); let mut file_output = File::create(&dest.join("vk_bindings.rs")).unwrap();
vk_sys::write_bindings(&mut file_output).unwrap(); vk_sys::write_bindings(&mut file_output).unwrap();
write_examples(); // building the shaders used in the examples
} vulkano_shaders::build_glsl_shaders([
("examples/triangle_vs.glsl", vulkano_shaders::ShaderType::Vertex),
fn write_examples() { ("examples/triangle_fs.glsl", vulkano_shaders::ShaderType::Fragment),
let dest = env::var("OUT_DIR").unwrap(); ("examples/teapot_vs.glsl", vulkano_shaders::ShaderType::Vertex),
let dest = Path::new(&dest); ("examples/teapot_fs.glsl", vulkano_shaders::ShaderType::Fragment),
("examples/image_vs.glsl", vulkano_shaders::ShaderType::Vertex),
let mut file_output = File::create(&dest.join("examples-triangle_vs.rs")).unwrap(); ("examples/image_fs.glsl", vulkano_shaders::ShaderType::Fragment),
println!("cargo:rerun-if-changed=examples/triangle_vs.glsl"); ].iter().cloned());
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();
} }

View File

@ -80,10 +80,10 @@ fn main() {
} }
mod vs { include!{concat!(env!("OUT_DIR"), "/examples-image_vs.rs")} } mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/image_vs.glsl")} }
let vs = vs::ImageShader::load(&device).expect("failed to create shader module"); let vs = vs::Shader::load(&device).expect("failed to create shader module");
mod fs { include!{concat!(env!("OUT_DIR"), "/examples-image_fs.rs")} } mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/image_fs.glsl")} }
let fs = fs::ImageShader::load(&device).expect("failed to create shader module"); let fs = fs::Shader::load(&device).expect("failed to create shader module");
let renderpass = single_pass_renderpass!{ let renderpass = single_pass_renderpass!{
device: &device, device: &device,

View File

@ -119,10 +119,10 @@ fn main() {
mapping.1 = proj.into(); mapping.1 = proj.into();
} }
mod vs { include!{concat!(env!("OUT_DIR"), "/examples-teapot_vs.rs")} } mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/teapot_vs.glsl")} }
let vs = vs::TeapotShader::load(&device).expect("failed to create shader module"); let vs = vs::Shader::load(&device).expect("failed to create shader module");
mod fs { include!{concat!(env!("OUT_DIR"), "/examples-teapot_fs.rs")} } mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/teapot_fs.glsl")} }
let fs = fs::TeapotShader::load(&device).expect("failed to create shader module"); let fs = fs::Shader::load(&device).expect("failed to create shader module");
let images = images.into_iter().map(|image| { let images = images.into_iter().map(|image| {
let image = image.transition(vulkano::image::Layout::PresentSrc, &cb_pool, let image = image.transition(vulkano::image::Layout::PresentSrc, &cb_pool,

View File

@ -130,10 +130,10 @@ fn main() {
// can now use to load the shader. // can now use to load the shader.
// //
// Because of some restrictions with the `include!` macro, we need to use a module. // 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")} } mod vs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/triangle_vs.glsl")} }
let vs = vs::TriangleShader::load(&device).expect("failed to create shader module"); let vs = vs::Shader::load(&device).expect("failed to create shader module");
mod fs { include!{concat!(env!("OUT_DIR"), "/examples-triangle_fs.rs")} } mod fs { include!{concat!(env!("OUT_DIR"), "/shaders/examples/triangle_fs.glsl")} }
let fs = fs::TriangleShader::load(&device).expect("failed to create shader module"); 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 // 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 // implicitely does a lot of computation whenever you draw. In Vulkan, you have to do all this