build: make vulkan generation generic
This commit is contained in:
parent
472ebd5d7d
commit
fce250d233
5 changed files with 60 additions and 34 deletions
|
|
@ -1,17 +1,24 @@
|
||||||
mod hash;
|
mod hash;
|
||||||
|
|
||||||
use {
|
use {
|
||||||
crate::vulkan::hash::{ROOT, unchanged},
|
crate::vulkan::hash::{TREES, Tree, unchanged},
|
||||||
anyhow::bail,
|
anyhow::bail,
|
||||||
std::process::Command,
|
std::process::Command,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> anyhow::Result<()> {
|
pub fn main() -> anyhow::Result<()> {
|
||||||
println!("cargo:rerun-if-changed={}", ROOT);
|
for tree in TREES {
|
||||||
|
main_(tree)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main_(tree: &Tree) -> anyhow::Result<()> {
|
||||||
|
println!("cargo:rerun-if-changed={}", tree.root);
|
||||||
if !std::fs::exists("compile-shaders")? {
|
if !std::fs::exists("compile-shaders")? {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if unchanged() {
|
if unchanged(tree) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let code = Command::new("cargo")
|
let code = Command::new("cargo")
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,33 @@
|
||||||
use {std::fmt::Write, walkdir::WalkDir};
|
use {std::fmt::Write, walkdir::WalkDir};
|
||||||
|
|
||||||
pub const ROOT: &str = "src/gfx_apis/vulkan/shaders";
|
#[allow(dead_code)]
|
||||||
pub const HASH: &str = "src/gfx_apis/vulkan/shaders_hash.txt";
|
pub struct Tree {
|
||||||
|
pub root: &'static str,
|
||||||
|
pub hash: &'static str,
|
||||||
|
pub bin: &'static str,
|
||||||
|
pub shaders: &'static [&'static str],
|
||||||
|
}
|
||||||
|
|
||||||
fn calculate_hash() -> anyhow::Result<String> {
|
pub const TREES: &[Tree] = &[Tree {
|
||||||
let dir = WalkDir::new(ROOT);
|
root: "src/gfx_apis/vulkan/shaders",
|
||||||
|
hash: "src/gfx_apis/vulkan/shaders_hash.txt",
|
||||||
|
bin: "src/gfx_apis/vulkan/shaders_bin",
|
||||||
|
shaders: &[
|
||||||
|
"fill.frag",
|
||||||
|
"fill.vert",
|
||||||
|
"tex.vert",
|
||||||
|
"tex.frag",
|
||||||
|
"out.vert",
|
||||||
|
"out.frag",
|
||||||
|
"legacy/fill.frag",
|
||||||
|
"legacy/fill.vert",
|
||||||
|
"legacy/tex.vert",
|
||||||
|
"legacy/tex.frag",
|
||||||
|
],
|
||||||
|
}];
|
||||||
|
|
||||||
|
fn calculate_hash(tree: &Tree) -> anyhow::Result<String> {
|
||||||
|
let dir = WalkDir::new(tree.root);
|
||||||
let mut files = vec![];
|
let mut files = vec![];
|
||||||
for file in dir {
|
for file in dir {
|
||||||
let file = file?;
|
let file = file?;
|
||||||
|
|
@ -21,11 +44,11 @@ fn calculate_hash() -> anyhow::Result<String> {
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unchanged() -> bool {
|
pub fn unchanged(tree: &Tree) -> bool {
|
||||||
let Ok(actual) = std::fs::read_to_string(HASH) else {
|
let Ok(actual) = std::fs::read_to_string(tree.hash) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let Ok(expected) = calculate_hash() else {
|
let Ok(expected) = calculate_hash(tree) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
actual == expected
|
actual == expected
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,27 @@
|
||||||
use {
|
use {
|
||||||
anyhow::{Context, anyhow, bail},
|
anyhow::{Context, anyhow, bail},
|
||||||
compile_shaders_core::{BIN, ROOT, update_hash},
|
compile_shaders_core::{TREES, Tree, update_hash},
|
||||||
shaderc::{CompileOptions, ResolvedInclude},
|
shaderc::{CompileOptions, ResolvedInclude},
|
||||||
std::{fs::File, io::Write, path::Path},
|
std::{fs::File, io::Write, path::Path},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
compile("fill.frag")?;
|
for tree in TREES {
|
||||||
compile("fill.vert")?;
|
for shader in tree.shaders {
|
||||||
compile("tex.vert")?;
|
compile(tree, shader)?;
|
||||||
compile("tex.frag")?;
|
}
|
||||||
compile("out.vert")?;
|
update_hash(tree)?;
|
||||||
compile("out.frag")?;
|
}
|
||||||
compile("legacy/fill.frag")?;
|
|
||||||
compile("legacy/fill.vert")?;
|
|
||||||
compile("legacy/tex.vert")?;
|
|
||||||
compile("legacy/tex.frag")?;
|
|
||||||
update_hash()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile(name: &str) -> anyhow::Result<()> {
|
fn compile(tree: &Tree, name: &str) -> anyhow::Result<()> {
|
||||||
let out = format!("{name}.spv").replace("/", "_");
|
let out = format!("{name}.spv").replace("/", "_");
|
||||||
compile_shader(name, &out).with_context(|| name.to_string())
|
compile_shader(tree, name, &out).with_context(|| name.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_shader(name: &str, out: &str) -> anyhow::Result<()> {
|
fn compile_shader(tree: &Tree, name: &str, out: &str) -> anyhow::Result<()> {
|
||||||
let root = Path::new(ROOT).join(Path::new(name).parent().unwrap());
|
let root = Path::new(tree.root).join(Path::new(name).parent().unwrap());
|
||||||
let read = |path: &str| std::fs::read_to_string(root.join(path));
|
let read = |path: &str| std::fs::read_to_string(root.join(path));
|
||||||
let mut options = CompileOptions::new()?;
|
let mut options = CompileOptions::new()?;
|
||||||
options.set_include_callback(|name, _, _, _| {
|
options.set_include_callback(|name, _, _, _| {
|
||||||
|
|
@ -44,10 +39,10 @@ fn compile_shader(name: &str, out: &str) -> anyhow::Result<()> {
|
||||||
"vert" => shaderc::ShaderKind::Vertex,
|
"vert" => shaderc::ShaderKind::Vertex,
|
||||||
n => bail!("Unknown shader stage {}", n),
|
n => bail!("Unknown shader stage {}", n),
|
||||||
};
|
};
|
||||||
let src = std::fs::read_to_string(format!("{}/{}", ROOT, name))?;
|
let src = std::fs::read_to_string(format!("{}/{}", tree.root, name))?;
|
||||||
let compiler = shaderc::Compiler::new()?;
|
let compiler = shaderc::Compiler::new()?;
|
||||||
let binary = compiler.compile_into_spirv(&src, stage, name, "main", Some(&options))?;
|
let binary = compiler.compile_into_spirv(&src, stage, name, "main", Some(&options))?;
|
||||||
let mut file = File::create(Path::new(BIN).join(out))?;
|
let mut file = File::create(Path::new(tree.bin).join(out))?;
|
||||||
file.write_all(binary.as_binary_u8())?;
|
file.write_all(binary.as_binary_u8())?;
|
||||||
file.flush()?;
|
file.flush()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
include!("../../../build/vulkan/hash.rs");
|
include!("../../../build/vulkan/hash.rs");
|
||||||
|
|
||||||
pub const BIN: &str = "src/gfx_apis/vulkan/shaders_bin";
|
pub fn update_hash(tree: &Tree) -> anyhow::Result<()> {
|
||||||
|
std::fs::write(tree.hash, calculate_hash(tree)?)?;
|
||||||
pub fn update_hash() -> anyhow::Result<()> {
|
|
||||||
std::fs::write(HASH, calculate_hash()?)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
use compile_shaders_core::update_hash;
|
use compile_shaders_core::{update_hash, TREES};
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
update_hash()
|
for tree in TREES {
|
||||||
|
update_hash(tree)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue