Files
bytesize/src-tauri/build.rs
T

93 lines
3.5 KiB
Rust

use std::path::PathBuf;
use std::process::Command;
fn main() {
tauri_build::build();
compile_shaders();
}
/// Compile the HDR tonemap HLSL to bytecode at build time so we can embed it
/// via `include_bytes`! and avoid a runtime dependency on `d3dcompiler_47.dll`.
/// Looks for fxc.exe in the installed Windows SDK. If fxc is unavailable the
/// .cso files are emitted empty and capture.rs falls back to runtime
/// `D3DCompile` (so a clean checkout without the SDK still builds).
fn compile_shaders() {
// Cargo always sets OUT_DIR for a build script; if it is missing the build
// is broken in a way we cannot paper over, so say so plainly.
let out_dir = PathBuf::from(
std::env::var("OUT_DIR").expect("OUT_DIR is set by cargo for build scripts"),
);
let shader_dir = PathBuf::from("shaders");
let fxc = find_fxc();
for (entry, profile) in &[("fullscreen_vs", "vs_5_0"), ("tonemap_ps", "ps_5_0")] {
let src = shader_dir.join("tonemap.hlsl");
let dst = out_dir.join(format!("{entry}.cso"));
match &fxc {
Some(fxc) => {
let r = Command::new(fxc)
.arg(format!("/T{profile}"))
.arg(format!("/E{entry}"))
.arg("/O3")
.arg("/Fo")
.arg(&dst)
.arg(&src)
.output();
match r {
Ok(o) if o.status.success() => {
println!("cargo:rerun-if-changed={}", src.display());
}
Ok(o) => {
println!(
"cargo:warning=fxc failed for {entry}: {}",
String::from_utf8_lossy(&o.stderr)
);
drop(std::fs::write(&dst, []));
}
Err(e) => {
println!("cargo:warning=could not run fxc for {entry}: {e}");
drop(std::fs::write(&dst, []));
}
}
}
None => {
// No SDK on the build machine - emit an empty .cso; capture.rs
// detects the zero size and compiles the HLSL at runtime.
drop(std::fs::write(&dst, []));
}
}
}
if fxc.is_none() {
println!("cargo:warning=fxc.exe not found - shaders will be compiled at runtime");
}
println!("cargo:rerun-if-changed=shaders/tonemap.hlsl");
}
/// Locate the newest fxc.exe under the Windows Kits SDK. Returns None if the
/// SDK isn't installed (build still succeeds via the runtime-compile fallback).
fn find_fxc() -> Option<PathBuf> {
let root = PathBuf::from(r"C:\Program Files (x86)\Windows Kits\10\bin");
let Ok(entries) = std::fs::read_dir(&root) else {
return None;
};
// Pick the highest-versioned SDK with an x64 fxc.exe.
let mut best: Option<(String, PathBuf)> = None;
for entry in entries.flatten() {
let name = match entry.file_name().into_string() {
Ok(s) => s,
Err(_) => continue,
};
// Skip non-version dirs (arm64, x64, x86 at the root level).
if !name.starts_with("10.") {
continue;
}
let fxc = entry.path().join("x64").join("fxc.exe");
if fxc.is_file() && best.as_ref().is_none_or(|(b, _)| name.as_str() > b.as_str()) {
best = Some((name, fxc));
}
}
best.map(|(_, p)| p)
}