97 lines
3.7 KiB
Rust
97 lines
3.7 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();
|
|
|
|
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 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 (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 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)
|
|
}
|