Files
bytesize/src-tauri/build.rs
T

109 lines
4.2 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. Without the SDK the .cso
/// files are emitted empty so the build still succeeds, and there is no runtime
/// compile path: capture.rs refuses to start on empty bytecode and says why.
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.
#[allow(
clippy::expect_used,
reason = "a build script without OUT_DIR cannot produce a usable build"
)]
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();
// (output name, entry point, profile, defines). The pixel shader is built
// twice: an SDR output skips the HDR curve rather than being darkened by it.
let variants: &[(&str, &str, &str, &[&str])] = &[
("fullscreen_vs", "fullscreen_vs", "vs_5_0", &[]),
("tonemap_ps", "tonemap_ps", "ps_5_0", &["TONEMAP=1"]),
("tonemap_ps_sdr", "tonemap_ps", "ps_5_0", &["TONEMAP=0"]),
];
for (name, entry, profile, defines) in variants {
let src = shader_dir.join("tonemap.hlsl");
let dst = out_dir.join(format!("{name}.cso"));
match &fxc {
Some(fxc) => {
let mut cmd = Command::new(fxc);
cmd.arg(format!("/T{profile}"))
.arg(format!("/E{entry}"))
.arg("/O3");
for define in *defines {
cmd.arg(format!("/D{define}"));
}
let r = cmd.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 {name}: {}",
String::from_utf8_lossy(&o.stderr)
);
drop(std::fs::write(&dst, []));
}
Err(e) => {
println!("cargo:warning=could not run fxc for {name}: {e}");
drop(std::fs::write(&dst, []));
}
}
}
None => {
// No SDK on the build machine - emit an empty .cso so the build
// still succeeds; capture.rs rejects it at startup and points at
// the SDK rather than failing opaquely on the first frame.
drop(std::fs::write(&dst, []));
}
}
}
if fxc.is_none() {
println!("cargo:warning=fxc.exe not found - shaders cannot be compiled, capture will refuse to start");
}
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, in which case the build still succeeds but capture
/// refuses to start until it is rebuilt with the SDK present.
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 Ok(name) = entry.file_name().into_string() else {
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)
}