move to GLSL (rip rust-gpu 😭)
This commit is contained in:
parent
c1df7bf365
commit
d4623ab21f
14 changed files with 457 additions and 486 deletions
|
|
@ -1,33 +1,71 @@
|
|||
use shaderc::{Compiler, ShaderKind};
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{Read, Write},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use spirv_builder::{MetadataPrintout, SpirvBuilder};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Tell Cargo to rerun this script if the shaders crate or its contents change
|
||||
println!("cargo:rerun-if-changed=../shaders/src");
|
||||
println!("cargo:rerun-if-changed=../shaders/Cargo.toml");
|
||||
// Tell Cargo to rerun if shaders directory changes
|
||||
println!("cargo:rerun-if-changed=../../shaders");
|
||||
|
||||
SpirvBuilder::new("../shaders/", "spirv-unknown-vulkan1.2")
|
||||
.print_metadata(MetadataPrintout::None)
|
||||
.multimodule(true)
|
||||
.build()?
|
||||
.module
|
||||
.unwrap_multi()
|
||||
.iter()
|
||||
.for_each(|(name, path)| {
|
||||
let mut data = vec![];
|
||||
File::open(path).unwrap().read_to_end(&mut data).unwrap();
|
||||
let shader_dir = Path::new("../../shaders");
|
||||
let cache_dir = Path::new("../../shader-cache");
|
||||
|
||||
fs::create_dir_all("./shaders/").unwrap();
|
||||
// Create shader cache directory if it doesn't exist
|
||||
fs::create_dir_all(cache_dir)?;
|
||||
|
||||
File::create(format!("./shaders/{name}.spv"))
|
||||
.unwrap()
|
||||
.write_all(&data)
|
||||
.unwrap();
|
||||
});
|
||||
let compiler = Compiler::new().expect("Failed to create shader compiler");
|
||||
|
||||
// Compile all .vert and .frag files
|
||||
for entry in fs::read_dir(shader_dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
if let Some(extension) = path.extension() {
|
||||
let kind = match extension.to_str() {
|
||||
Some("vert") => ShaderKind::Vertex,
|
||||
Some("frag") => ShaderKind::Fragment,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
let source = fs::read_to_string(&path)?;
|
||||
let file_name = path.file_name().unwrap().to_str().unwrap();
|
||||
|
||||
// Create output path
|
||||
let spirv_path = cache_dir.join(format!("{}.spv", file_name));
|
||||
|
||||
// Check if we need to recompile
|
||||
if should_compile(&path, &spirv_path) {
|
||||
println!("Compiling shader: {}", file_name);
|
||||
|
||||
let compiled =
|
||||
compiler.compile_into_spirv(&source, kind, file_name, "main", None)?;
|
||||
|
||||
let mut file = File::create(&spirv_path)?;
|
||||
file.write_all(compiled.as_binary_u8())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn should_compile(source_path: &Path, output_path: &PathBuf) -> bool {
|
||||
// If output doesn't exist, we need to compile
|
||||
if !output_path.exists() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get modification times
|
||||
let source_modified = fs::metadata(source_path)
|
||||
.and_then(|m| m.modified())
|
||||
.unwrap_or(std::time::SystemTime::UNIX_EPOCH);
|
||||
|
||||
let output_modified = fs::metadata(output_path)
|
||||
.and_then(|m| m.modified())
|
||||
.unwrap_or(std::time::SystemTime::UNIX_EPOCH);
|
||||
|
||||
// Compile if source is newer than output
|
||||
source_modified > output_modified
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue