mirror of
https://github.com/vulkano-rs/vulkano.git
synced 2024-11-25 08:14:20 +00:00
Add easier shaders compilation path
This commit is contained in:
parent
41831c30cb
commit
ddcb8cf402
@ -3,5 +3,5 @@ name = "vulkano-shaders"
|
||||
version = "0.1.0"
|
||||
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
|
||||
|
||||
[dev-dependencies]
|
||||
[dependencies]
|
||||
glsl-to-spirv = { path = "../glsl-to-spirv" }
|
||||
|
@ -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<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>
|
||||
where R: Read
|
||||
{
|
||||
|
@ -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]
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user