tonemap hdr captures on the gpu
firefox playing hdr youtube renders the desktop in linear scrgb, and capturing it as bgra8 clamps the >1.0 highlights to pure white. capture in r16g16b16a16_float instead and run a hable filmic + bt709 pixel shader directly on the captured d3d11 texture (which windows-capture already exposes via frame.as_raw_texture), then read back bgra8 so the ffmpeg pipe stays unchanged from the sdr path. the shader is compiled offline with fxc at build time and embedded. the fullscreen-triangle draw needs an explicit cull-none rasterizer state since the default back-face cull silently drops it.
This commit is contained in:
+88
-1
@@ -1,3 +1,90 @@
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
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() {
|
||||
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR"));
|
||||
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)
|
||||
);
|
||||
let _ = std::fs::write(&dst, []);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("cargo:warning=could not run fxc for {entry}: {e}");
|
||||
let _ = 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.
|
||||
let _ = 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() {
|
||||
if best.as_ref().map_or(true, |(b, _)| name.as_str() > b.as_str()) {
|
||||
best = Some((name, fxc));
|
||||
}
|
||||
}
|
||||
}
|
||||
best.map(|(_, p)| p)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user