Created
April 26, 2017 12:51
-
-
Save fkaa/95135ab1e1e775cce3056984a5332b6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::path::Path; | |
use std::process::Command; | |
macro_rules! compile_hlsl { | |
( $( $file:expr => ($entry:ident, $target:ident) -> $output:expr),* ) => { | |
{ | |
let mut funcs = Vec::new(); | |
$( | |
println!("cargo:rerun-if-changed={}", $file); | |
funcs.push( | |
HlslFunction { | |
file: $file, | |
entry: stringify!($entry), | |
target: stringify!($target), | |
output: $output | |
} | |
); | |
)* | |
compile_hlsl(funcs); | |
}; | |
} | |
} | |
#[derive(Debug)] | |
struct HlslFunction { | |
file: &'static str, | |
entry: &'static str, | |
target: &'static str, | |
output: &'static str | |
} | |
fn compile_hlsl(functions: Vec<HlslFunction>) { | |
let compiler_path = env::var("DXSDK_DIR").unwrap() + "\\Utilities\\bin\\x64\\fxc.exe"; | |
let mut failed = false; | |
for func in functions { | |
let dir = Path::new(func.file).parent().unwrap().to_str().unwrap(); | |
use std::time::Instant; | |
let now = Instant::now(); | |
let output = | |
Command::new(&compiler_path) | |
.args(&[ | |
"-nologo", | |
"/T", func.target, | |
"/E", func.entry, | |
"/Fo", &(String::from(dir) + "/" + func.output), | |
func.file | |
]) | |
.output() | |
.expect("Failed to compile shader"); | |
let elapsed = now.elapsed(); | |
let sec = (elapsed.as_secs() as f64) + (elapsed.subsec_nanos() as f64 / 1000_000_000.0); | |
match output.status.success() { | |
true => { | |
println!("Compiled entrypoint {} in {} ({} s)", func.entry, func.file, sec); | |
}, | |
false => { | |
failed = true; | |
println!("{:?}\n{}", func, ::std::str::from_utf8(&output.stderr).unwrap()); | |
} | |
} | |
} | |
if failed { | |
panic!("Shader compilation failed, aborting build!"); | |
} | |
} | |
fn main() { | |
compile_hlsl! [ | |
"assets/shader/Sphere.hlsl" => (VS, vs_5_0) -> "sphere_vs.o", | |
"assets/shader/Sphere.hlsl" => (PS, ps_5_0) -> "sphere_ps.o" | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment