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;
|
||||
|
||||
use {
|
||||
crate::vulkan::hash::{ROOT, unchanged},
|
||||
crate::vulkan::hash::{TREES, Tree, unchanged},
|
||||
anyhow::bail,
|
||||
std::process::Command,
|
||||
};
|
||||
|
||||
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")? {
|
||||
return Ok(());
|
||||
}
|
||||
if unchanged() {
|
||||
if unchanged(tree) {
|
||||
return Ok(());
|
||||
}
|
||||
let code = Command::new("cargo")
|
||||
|
|
|
|||
|
|
@ -1,10 +1,33 @@
|
|||
use {std::fmt::Write, walkdir::WalkDir};
|
||||
|
||||
pub const ROOT: &str = "src/gfx_apis/vulkan/shaders";
|
||||
pub const HASH: &str = "src/gfx_apis/vulkan/shaders_hash.txt";
|
||||
#[allow(dead_code)]
|
||||
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> {
|
||||
let dir = WalkDir::new(ROOT);
|
||||
pub const TREES: &[Tree] = &[Tree {
|
||||
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![];
|
||||
for file in dir {
|
||||
let file = file?;
|
||||
|
|
@ -21,11 +44,11 @@ fn calculate_hash() -> anyhow::Result<String> {
|
|||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn unchanged() -> bool {
|
||||
let Ok(actual) = std::fs::read_to_string(HASH) else {
|
||||
pub fn unchanged(tree: &Tree) -> bool {
|
||||
let Ok(actual) = std::fs::read_to_string(tree.hash) else {
|
||||
return false;
|
||||
};
|
||||
let Ok(expected) = calculate_hash() else {
|
||||
let Ok(expected) = calculate_hash(tree) else {
|
||||
return false;
|
||||
};
|
||||
actual == expected
|
||||
|
|
|
|||
|
|
@ -1,32 +1,27 @@
|
|||
use {
|
||||
anyhow::{Context, anyhow, bail},
|
||||
compile_shaders_core::{BIN, ROOT, update_hash},
|
||||
compile_shaders_core::{TREES, Tree, update_hash},
|
||||
shaderc::{CompileOptions, ResolvedInclude},
|
||||
std::{fs::File, io::Write, path::Path},
|
||||
};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
compile("fill.frag")?;
|
||||
compile("fill.vert")?;
|
||||
compile("tex.vert")?;
|
||||
compile("tex.frag")?;
|
||||
compile("out.vert")?;
|
||||
compile("out.frag")?;
|
||||
compile("legacy/fill.frag")?;
|
||||
compile("legacy/fill.vert")?;
|
||||
compile("legacy/tex.vert")?;
|
||||
compile("legacy/tex.frag")?;
|
||||
update_hash()?;
|
||||
for tree in TREES {
|
||||
for shader in tree.shaders {
|
||||
compile(tree, shader)?;
|
||||
}
|
||||
update_hash(tree)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compile(name: &str) -> anyhow::Result<()> {
|
||||
fn compile(tree: &Tree, name: &str) -> anyhow::Result<()> {
|
||||
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<()> {
|
||||
let root = Path::new(ROOT).join(Path::new(name).parent().unwrap());
|
||||
fn compile_shader(tree: &Tree, name: &str, out: &str) -> anyhow::Result<()> {
|
||||
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 mut options = CompileOptions::new()?;
|
||||
options.set_include_callback(|name, _, _, _| {
|
||||
|
|
@ -44,10 +39,10 @@ fn compile_shader(name: &str, out: &str) -> anyhow::Result<()> {
|
|||
"vert" => shaderc::ShaderKind::Vertex,
|
||||
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 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.flush()?;
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
include!("../../../build/vulkan/hash.rs");
|
||||
|
||||
pub const BIN: &str = "src/gfx_apis/vulkan/shaders_bin";
|
||||
|
||||
pub fn update_hash() -> anyhow::Result<()> {
|
||||
std::fs::write(HASH, calculate_hash()?)?;
|
||||
pub fn update_hash(tree: &Tree) -> anyhow::Result<()> {
|
||||
std::fs::write(tree.hash, calculate_hash(tree)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
use compile_shaders_core::update_hash;
|
||||
use compile_shaders_core::{update_hash, TREES};
|
||||
|
||||
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