1
0
Fork 0
forked from wry/wry

vulkan: include precompiled shaders

This commit is contained in:
Julian Orth 2025-09-16 09:40:52 +02:00
parent 2dc9695621
commit 51575fce39
31 changed files with 425 additions and 122 deletions

1
compile-shaders/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

196
compile-shaders/Cargo.lock generated Normal file
View file

@ -0,0 +1,196 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anyhow"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100"
[[package]]
name = "arrayref"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb"
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "blake3"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3888aaa89e4b2a40fca9848e400f6a658a5a3978de7be858e209cafa8be9a4a0"
dependencies = [
"arrayref",
"arrayvec",
"cc",
"cfg-if",
"constant_time_eq",
]
[[package]]
name = "cc"
version = "1.2.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65193589c6404eb80b450d618eaf9a2cafaaafd57ecce47370519ef674a7bd44"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "cmake"
version = "0.1.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0"
dependencies = [
"cc",
]
[[package]]
name = "compile-shaders-build-rs"
version = "0.1.0"
dependencies = [
"anyhow",
"compile-shaders-core",
]
[[package]]
name = "compile-shaders-compile"
version = "0.1.0"
dependencies = [
"anyhow",
"compile-shaders-core",
"shaderc",
]
[[package]]
name = "compile-shaders-core"
version = "0.1.0"
dependencies = [
"anyhow",
"blake3",
"walkdir",
]
[[package]]
name = "constant_time_eq"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6"
[[package]]
name = "find-msvc-tools"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fd99930f64d146689264c637b5af2f0233a933bef0d8570e2526bf9e083192d"
[[package]]
name = "libc"
version = "0.2.175"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543"
[[package]]
name = "link-cplusplus"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f78c730aaa7d0b9336a299029ea49f9ee53b0ed06e9202e8cb7db9bae7b8c82"
dependencies = [
"cc",
]
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "roxmltree"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "shaderc"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ab2a6e36d1c1e2320c87e2b806a3e7b0dffaa67b82c14a39dad6cf7637208ae"
dependencies = [
"libc",
"shaderc-sys",
]
[[package]]
name = "shaderc-sys"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdceb85b2c6d2c27b95ffe2d341063dfded0aca8046f7f60c544bbeaeaf8bcae"
dependencies = [
"cmake",
"libc",
"link-cplusplus",
"pkg-config",
"roxmltree",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "winapi-util"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-link"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45e46c0661abb7180e7b9c281db115305d49ca1709ab8242adf09666d2173c65"
[[package]]
name = "windows-sys"
version = "0.61.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e201184e40b2ede64bc2ea34968b28e33622acdbbf37104f0e4a33f7abe657aa"
dependencies = [
"windows-link",
]

View file

@ -0,0 +1,3 @@
[workspace]
resolver = "3"
members = ["build-rs", "compile", "core"]

View file

@ -0,0 +1,8 @@
[package]
name = "compile-shaders-build-rs"
version = "0.1.0"
edition = "2024"
[dependencies]
compile-shaders-core = { path = "../core" }
anyhow = "1.0.99"

View file

@ -0,0 +1,25 @@
use {
anyhow::bail,
compile_shaders_core::{ROOT, unchanged},
std::process::Command,
};
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed={}", ROOT);
if unchanged() {
return Ok(());
}
let code = Command::new("cargo")
.args([
"run",
"--manifest-path",
"compile-shaders/Cargo.toml",
"-p",
"compile-shaders-compile",
])
.status()?;
if !code.success() {
bail!("compile-shaders failed");
}
Ok(())
}

View file

@ -0,0 +1,9 @@
[package]
name = "compile-shaders-compile"
version = "0.1.0"
edition = "2024"
[dependencies]
compile-shaders-core = { path = "../core" }
anyhow = "1.0.79"
shaderc = "0.10.1"

View file

@ -0,0 +1,54 @@
use {
anyhow::{Context, anyhow, bail},
compile_shaders_core::{BIN, ROOT, 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()?;
Ok(())
}
fn compile(name: &str) -> anyhow::Result<()> {
let out = format!("{name}.spv").replace("/", "_");
compile_shader(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());
let read = |path: &str| std::fs::read_to_string(root.join(path));
let mut options = CompileOptions::new()?;
options.set_include_callback(|name, _, _, _| {
Ok(ResolvedInclude {
resolved_name: name.to_string(),
content: read(name).map_err(|e| anyhow!(e).to_string())?,
})
});
let stage = match Path::new(name)
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
{
"frag" => shaderc::ShaderKind::Fragment,
"vert" => shaderc::ShaderKind::Vertex,
n => bail!("Unknown shader stage {}", n),
};
let src = std::fs::read_to_string(format!("{}/{}", 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))?;
file.write_all(binary.as_binary_u8())?;
file.flush()?;
Ok(())
}

View file

@ -0,0 +1,9 @@
[package]
name = "compile-shaders-core"
version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = "1.0.79"
walkdir = "2.5.0"
blake3 = "1.8.2"

View file

@ -0,0 +1,38 @@
use {std::fmt::Write, walkdir::WalkDir};
pub const ROOT: &str = "src/gfx_apis/vulkan/shaders";
pub const BIN: &str = "src/gfx_apis/vulkan/shaders_bin";
pub const HASH: &str = "src/gfx_apis/vulkan/shaders_hash.txt";
fn calculate_hash() -> anyhow::Result<String> {
let dir = WalkDir::new(ROOT);
let mut files = vec![];
for file in dir {
let file = file?;
if file.file_type().is_file() {
files.push(file.path().to_path_buf());
}
}
files.sort();
let mut out = String::new();
for file in files {
let data = std::fs::read(&file)?;
writeln!(out, "{} {}", blake3::hash(&data).to_hex(), file.display())?;
}
Ok(out)
}
pub fn unchanged() -> bool {
let Ok(actual) = std::fs::read_to_string(HASH) else {
return false;
};
let Ok(expected) = calculate_hash() else {
return false;
};
actual == expected
}
pub fn update_hash() -> anyhow::Result<()> {
std::fs::write(HASH, calculate_hash()?)?;
Ok(())
}

View file

@ -0,0 +1,5 @@
use compile_shaders_core::update_hash;
fn main() -> anyhow::Result<()> {
update_hash()
}