-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.rs
More file actions
100 lines (83 loc) · 3.75 KB
/
Copy pathbuild.rs
File metadata and controls
100 lines (83 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use cargo_gpu::spirv_builder::{Capability, MetadataPrintout, SpirvBuilderError, SpirvMetadata};
use std::error::Error;
use std::path::PathBuf;
use std::{env, fs, thread};
fn main() -> Result<(), Box<dyn Error>> {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("Always set by Cargo; qed");
if target_arch != "spirv" {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("Always set by Cargo; qed"));
// Skip compilation under Clippy, it doesn't work for some reason and isn't really needed
// anyway. Same about Miri and rustdoc.
if ["CLIPPY_ARGS", "MIRI_SYSROOT", "RUSTDOCFLAGS"]
.iter()
.any(|var| env::var(var).is_ok())
{
let empty_file = out_dir.join("empty.bin");
fs::write(&empty_file, [])?;
println!(
"cargo::rustc-env=SHADER_PATH_FALLBACK={}",
empty_file.display()
);
println!(
"cargo::rustc-env=SHADER_PATH_MODERN={}",
empty_file.display()
);
return Ok(());
}
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Always set by Cargo; qed");
let profile = env::var("PROFILE").expect("Always set by Cargo; qed");
let shader_crate = PathBuf::from(cargo_manifest_dir);
let backend = cargo_gpu::Install::from_shader_crate(shader_crate.clone()).run()?;
let spirv_builder = backend
.to_spirv_builder(shader_crate, "spirv-unknown-vulkan1.2")
.print_metadata(MetadataPrintout::DependencyOnly)
.spirv_metadata(if profile == "debug" {
SpirvMetadata::NameVariables
} else {
SpirvMetadata::None
})
.release(profile != "debug")
// TODO: This should not be needed: https://github.com/Rust-GPU/rust-gpu/discussions/385
.capability(Capability::GroupNonUniformArithmetic);
thread::scope(|scope| -> Result<(), Box<dyn Error>> {
// Compile SPIR-V shader for GPU that only supports baseline Vulkan features
let handle_fallback = scope.spawn(|| {
let compile_result = spirv_builder
.clone()
// Avoid Cargo deadlock, customize target
.target_dir_path(out_dir.join("fallback").to_string_lossy().to_string())
.build()?;
let path_to_spv = compile_result.module.unwrap_single();
println!(
"cargo::rustc-env=SHADER_PATH_FALLBACK={}",
path_to_spv.display()
);
Ok::<(), SpirvBuilderError>(())
});
// Compile SPIR-V shader for GPUs that supports modern Vulkan features
let handle_modern = scope.spawn(|| {
let compile_result = spirv_builder
.clone()
.shader_crate_features(["__modern-gpu".to_string()])
// Avoid Cargo deadlock, customize target
.target_dir_path(out_dir.join("modern").to_string_lossy().to_string())
.capability(Capability::Int64)
.build()?;
let path_to_spv = compile_result.module.unwrap_single();
println!(
"cargo::rustc-env=SHADER_PATH_MODERN={}",
path_to_spv.display()
);
Ok::<(), SpirvBuilderError>(())
});
handle_fallback
.join()
.expect("Spawning threads must succeed")?;
handle_modern
.join()
.expect("Spawning threads must succeed")?;
Ok(())
})?;
}
Ok(())
}