2021-04-14 18:20:48 +00:00
|
|
|
//TODO: move this to a binary target once Rust supports
|
|
|
|
// binary-specific dependencies.
|
|
|
|
|
|
|
|
use std::{fs, path::PathBuf};
|
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
const BASE_DIR_IN: &str = "tests/in";
|
|
|
|
const BASE_DIR_OUT: &str = "tests/out";
|
2021-04-14 18:20:48 +00:00
|
|
|
|
2020-12-13 06:20:39 +00:00
|
|
|
bitflags::bitflags! {
|
2021-02-18 02:46:32 +00:00
|
|
|
struct Targets: u32 {
|
|
|
|
const IR = 0x1;
|
|
|
|
const ANALYSIS = 0x2;
|
|
|
|
const SPIRV = 0x4;
|
|
|
|
const METAL = 0x8;
|
|
|
|
const GLSL = 0x10;
|
2021-03-02 06:28:32 +00:00
|
|
|
const DOT = 0x20;
|
2021-04-11 15:36:26 +00:00
|
|
|
const HLSL = 0x40;
|
2021-04-19 13:19:03 +00:00
|
|
|
const WGSL = 0x80;
|
2020-12-13 06:20:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:56:10 +00:00
|
|
|
#[derive(Default, serde::Deserialize)]
|
2020-12-08 05:23:29 +00:00
|
|
|
struct Parameters {
|
2021-05-06 04:04:43 +00:00
|
|
|
#[serde(default)]
|
|
|
|
god_mode: bool,
|
2021-05-27 22:02:38 +00:00
|
|
|
|
|
|
|
// We can only deserialize `IndexBoundsCheckPolicy` values if `deserialize`
|
|
|
|
// feature was enabled, but features should not affect snapshot contents, so
|
|
|
|
// just take the policy as booleans instead.
|
|
|
|
#[serde(default)]
|
|
|
|
bounds_check_read_zero_skip_write: bool,
|
|
|
|
#[serde(default)]
|
|
|
|
bounds_check_restrict: bool,
|
|
|
|
|
2020-12-08 05:23:29 +00:00
|
|
|
#[cfg_attr(not(feature = "spv-out"), allow(dead_code))]
|
2021-02-14 05:37:54 +00:00
|
|
|
spv_version: (u8, u8),
|
|
|
|
#[cfg_attr(not(feature = "spv-out"), allow(dead_code))]
|
2021-05-19 05:18:27 +00:00
|
|
|
#[serde(default)]
|
2020-12-08 05:23:29 +00:00
|
|
|
spv_capabilities: naga::FastHashSet<spirv::Capability>,
|
2021-04-02 05:12:36 +00:00
|
|
|
#[cfg_attr(not(feature = "spv-out"), allow(dead_code))]
|
2021-05-06 04:04:43 +00:00
|
|
|
#[serde(default)]
|
2021-04-02 05:12:36 +00:00
|
|
|
spv_debug: bool,
|
|
|
|
#[cfg_attr(not(feature = "spv-out"), allow(dead_code))]
|
2021-05-06 04:04:43 +00:00
|
|
|
#[serde(default)]
|
2021-04-02 05:12:36 +00:00
|
|
|
spv_adjust_coordinate_space: bool,
|
2021-04-07 14:29:35 +00:00
|
|
|
#[cfg(all(feature = "deserialize", feature = "msl-out"))]
|
|
|
|
#[serde(default)]
|
|
|
|
msl: naga::back::msl::Options,
|
|
|
|
#[cfg(all(not(feature = "deserialize"), feature = "msl-out"))]
|
2021-05-06 04:04:43 +00:00
|
|
|
#[serde(default)]
|
2021-04-07 14:29:35 +00:00
|
|
|
msl_custom: bool,
|
2021-06-27 04:26:12 +00:00
|
|
|
#[cfg(all(feature = "deserialize", feature = "glsl-out"))]
|
|
|
|
#[serde(default)]
|
|
|
|
glsl: naga::back::glsl::Options,
|
|
|
|
#[cfg(all(not(feature = "deserialize"), feature = "glsl-out"))]
|
2021-04-15 18:53:41 +00:00
|
|
|
#[serde(default)]
|
2021-06-27 04:26:12 +00:00
|
|
|
glsl_custom: bool,
|
2021-06-18 22:38:47 +00:00
|
|
|
#[cfg_attr(not(feature = "glsl-out"), allow(dead_code))]
|
|
|
|
#[serde(default)]
|
|
|
|
glsl_vert_ep_name: Option<String>,
|
|
|
|
#[cfg_attr(not(feature = "glsl-out"), allow(dead_code))]
|
|
|
|
#[serde(default)]
|
|
|
|
glsl_frag_ep_name: Option<String>,
|
|
|
|
#[cfg_attr(not(feature = "glsl-out"), allow(dead_code))]
|
|
|
|
#[serde(default)]
|
|
|
|
glsl_comp_ep_name: Option<String>,
|
2020-12-08 05:23:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 14:12:14 +00:00
|
|
|
#[allow(dead_code, unused_variables)]
|
2021-02-18 02:46:32 +00:00
|
|
|
fn check_targets(module: &naga::Module, name: &str, targets: Targets) {
|
2021-04-14 18:20:48 +00:00
|
|
|
let root = env!("CARGO_MANIFEST_DIR");
|
2021-06-18 22:38:47 +00:00
|
|
|
let params = match fs::read_to_string(format!("{}/{}/{}.param.ron", root, BASE_DIR_IN, name)) {
|
2021-02-18 02:46:32 +00:00
|
|
|
Ok(string) => ron::de::from_str(&string).expect("Couldn't find param file"),
|
|
|
|
Err(_) => Parameters::default(),
|
|
|
|
};
|
2021-05-27 22:02:38 +00:00
|
|
|
if params.bounds_check_restrict && params.bounds_check_read_zero_skip_write {
|
|
|
|
panic!("select only one bounds check policy");
|
|
|
|
}
|
2021-05-06 04:04:43 +00:00
|
|
|
let capabilities = if params.god_mode {
|
|
|
|
naga::valid::Capabilities::all()
|
|
|
|
} else {
|
|
|
|
naga::valid::Capabilities::empty()
|
|
|
|
};
|
|
|
|
let info = naga::valid::Validator::new(naga::valid::ValidationFlags::all(), capabilities)
|
|
|
|
.validate(module)
|
|
|
|
.unwrap();
|
2021-02-18 02:46:32 +00:00
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
let dest = PathBuf::from(root).join(BASE_DIR_OUT);
|
2021-04-14 18:20:48 +00:00
|
|
|
|
2021-02-18 02:46:32 +00:00
|
|
|
#[cfg(feature = "serialize")]
|
|
|
|
{
|
|
|
|
if targets.contains(Targets::IR) {
|
|
|
|
let config = ron::ser::PrettyConfig::default().with_new_line("\n".to_string());
|
2021-04-14 18:20:48 +00:00
|
|
|
let string = ron::ser::to_string_pretty(module, config).unwrap();
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(dest.join(format!("ir/{}.ron", name)), string).unwrap();
|
2021-02-18 02:46:32 +00:00
|
|
|
}
|
|
|
|
if targets.contains(Targets::ANALYSIS) {
|
|
|
|
let config = ron::ser::PrettyConfig::default().with_new_line("\n".to_string());
|
2021-04-14 18:20:48 +00:00
|
|
|
let string = ron::ser::to_string_pretty(&info, config).unwrap();
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(dest.join(format!("analysis/{}.info.ron", name)), string).unwrap();
|
2021-02-18 02:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "spv-out")]
|
|
|
|
{
|
|
|
|
if targets.contains(Targets::SPIRV) {
|
2021-06-18 22:38:47 +00:00
|
|
|
write_output_spv(module, &info, &dest, name, ¶ms);
|
2021-02-18 02:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "msl-out")]
|
|
|
|
{
|
|
|
|
if targets.contains(Targets::METAL) {
|
2021-06-18 22:38:47 +00:00
|
|
|
write_output_msl(module, &info, &dest, name, ¶ms);
|
2021-02-18 02:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "glsl-out")]
|
|
|
|
{
|
|
|
|
if targets.contains(Targets::GLSL) {
|
2021-03-07 03:47:21 +00:00
|
|
|
for ep in module.entry_points.iter() {
|
2021-06-18 22:38:47 +00:00
|
|
|
write_output_glsl(module, &info, &dest, name, ep.stage, &ep.name, ¶ms);
|
2021-02-18 02:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 06:28:32 +00:00
|
|
|
#[cfg(feature = "dot-out")]
|
|
|
|
{
|
|
|
|
if targets.contains(Targets::DOT) {
|
2021-03-20 05:09:29 +00:00
|
|
|
let string = naga::back::dot::write(module, Some(&info)).unwrap();
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(dest.join(format!("dot/{}.dot", name)), string).unwrap();
|
2021-03-02 06:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 15:36:26 +00:00
|
|
|
#[cfg(feature = "hlsl-out")]
|
|
|
|
{
|
|
|
|
if targets.contains(Targets::HLSL) {
|
2021-06-18 22:38:47 +00:00
|
|
|
write_output_hlsl(module, &info, &dest, name);
|
2021-04-11 15:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 13:19:03 +00:00
|
|
|
#[cfg(feature = "wgsl-out")]
|
|
|
|
{
|
|
|
|
if targets.contains(Targets::WGSL) {
|
2021-06-18 22:38:47 +00:00
|
|
|
write_output_wgsl(module, &info, &dest, name);
|
2021-04-19 13:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-18 02:46:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 06:45:28 +00:00
|
|
|
#[cfg(feature = "spv-out")]
|
2021-06-18 22:38:47 +00:00
|
|
|
fn write_output_spv(
|
2021-02-17 19:55:46 +00:00
|
|
|
module: &naga::Module,
|
2021-03-20 05:09:29 +00:00
|
|
|
info: &naga::valid::ModuleInfo,
|
2021-04-14 18:20:48 +00:00
|
|
|
destination: &PathBuf,
|
2021-06-18 22:38:47 +00:00
|
|
|
file_name: &str,
|
2021-02-17 19:55:46 +00:00
|
|
|
params: &Parameters,
|
|
|
|
) {
|
2020-12-11 06:45:28 +00:00
|
|
|
use naga::back::spv;
|
|
|
|
use rspirv::binary::Disassemble;
|
|
|
|
|
2021-04-02 05:12:36 +00:00
|
|
|
let mut flags = spv::WriterFlags::empty();
|
|
|
|
if params.spv_debug {
|
|
|
|
flags |= spv::WriterFlags::DEBUG;
|
|
|
|
}
|
|
|
|
if params.spv_adjust_coordinate_space {
|
|
|
|
flags |= spv::WriterFlags::ADJUST_COORDINATE_SPACE;
|
|
|
|
}
|
2021-02-14 05:37:54 +00:00
|
|
|
let options = spv::Options {
|
|
|
|
lang_version: params.spv_version,
|
2021-04-02 05:12:36 +00:00
|
|
|
flags,
|
2021-05-19 05:18:27 +00:00
|
|
|
capabilities: if params.spv_capabilities.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(params.spv_capabilities.clone())
|
|
|
|
},
|
2021-05-27 22:02:38 +00:00
|
|
|
index_bounds_check_policy: if params.bounds_check_restrict {
|
|
|
|
naga::back::IndexBoundsCheckPolicy::Restrict
|
|
|
|
} else if params.bounds_check_read_zero_skip_write {
|
|
|
|
naga::back::IndexBoundsCheckPolicy::ReadZeroSkipWrite
|
|
|
|
} else {
|
|
|
|
naga::back::IndexBoundsCheckPolicy::UndefinedBehavior
|
|
|
|
},
|
|
|
|
..spv::Options::default()
|
2021-02-14 05:37:54 +00:00
|
|
|
};
|
|
|
|
|
2021-03-20 05:09:29 +00:00
|
|
|
let spv = spv::write_vec(module, info, &options).unwrap();
|
2020-12-11 06:45:28 +00:00
|
|
|
|
|
|
|
let dis = rspirv::dr::load_words(spv)
|
|
|
|
.expect("Produced invalid SPIR-V")
|
|
|
|
.disassemble();
|
2021-04-14 18:20:48 +00:00
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(destination.join(format!("spv/{}.spvasm", file_name)), dis).unwrap();
|
2020-12-11 06:45:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 05:23:29 +00:00
|
|
|
#[cfg(feature = "msl-out")]
|
2021-06-18 22:38:47 +00:00
|
|
|
fn write_output_msl(
|
2021-02-12 06:39:08 +00:00
|
|
|
module: &naga::Module,
|
2021-03-20 05:09:29 +00:00
|
|
|
info: &naga::valid::ModuleInfo,
|
2021-04-14 18:20:48 +00:00
|
|
|
destination: &PathBuf,
|
2021-06-18 22:38:47 +00:00
|
|
|
file_name: &str,
|
2021-02-12 06:39:08 +00:00
|
|
|
params: &Parameters,
|
|
|
|
) {
|
2020-12-08 05:23:29 +00:00
|
|
|
use naga::back::msl;
|
2020-12-11 06:45:28 +00:00
|
|
|
|
2021-04-07 14:29:35 +00:00
|
|
|
#[cfg_attr(feature = "deserialize", allow(unused_variables))]
|
|
|
|
let default_options = msl::Options::default();
|
|
|
|
#[cfg(feature = "deserialize")]
|
|
|
|
let options = ¶ms.msl;
|
|
|
|
#[cfg(not(feature = "deserialize"))]
|
|
|
|
let options = if params.msl_custom {
|
2021-05-01 02:40:32 +00:00
|
|
|
println!("Skipping {}", destination.display());
|
2021-04-07 14:29:35 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
&default_options
|
2021-04-07 05:06:51 +00:00
|
|
|
};
|
2021-04-07 14:29:35 +00:00
|
|
|
|
2021-04-08 02:44:41 +00:00
|
|
|
let pipeline_options = msl::PipelineOptions {
|
2021-04-02 03:49:17 +00:00
|
|
|
allow_point_size: true,
|
2020-12-08 05:23:29 +00:00
|
|
|
};
|
2020-12-11 06:45:28 +00:00
|
|
|
|
2021-04-30 17:38:20 +00:00
|
|
|
let (string, tr_info) = msl::write_string(module, info, options, &pipeline_options).unwrap();
|
|
|
|
|
|
|
|
for (ep, result) in module.entry_points.iter().zip(tr_info.entry_point_names) {
|
|
|
|
if let Err(error) = result {
|
|
|
|
panic!("Failed to translate '{}': {}", ep.name, error);
|
|
|
|
}
|
|
|
|
}
|
2021-02-07 23:01:58 +00:00
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(destination.join(format!("msl/{}.msl", file_name)), string).unwrap();
|
2020-12-08 05:23:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 06:45:28 +00:00
|
|
|
#[cfg(feature = "glsl-out")]
|
2021-06-18 22:38:47 +00:00
|
|
|
fn write_output_glsl(
|
2021-02-14 22:26:31 +00:00
|
|
|
module: &naga::Module,
|
2021-03-20 05:09:29 +00:00
|
|
|
info: &naga::valid::ModuleInfo,
|
2021-04-14 18:20:48 +00:00
|
|
|
destination: &PathBuf,
|
2021-06-18 22:38:47 +00:00
|
|
|
file_name: &str,
|
2021-02-14 22:26:31 +00:00
|
|
|
stage: naga::ShaderStage,
|
|
|
|
ep_name: &str,
|
2021-04-15 18:53:41 +00:00
|
|
|
params: &Parameters,
|
2021-02-14 22:26:31 +00:00
|
|
|
) {
|
2020-12-11 06:45:28 +00:00
|
|
|
use naga::back::glsl;
|
|
|
|
|
2021-06-27 04:26:12 +00:00
|
|
|
#[cfg_attr(feature = "deserialize", allow(unused_variables))]
|
|
|
|
let default_options = glsl::Options::default();
|
|
|
|
#[cfg(feature = "deserialize")]
|
|
|
|
let options = ¶ms.glsl;
|
|
|
|
#[cfg(not(feature = "deserialize"))]
|
|
|
|
let options = if params.glsl_custom {
|
|
|
|
println!("Skipping {}", destination.display());
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
&default_options
|
|
|
|
};
|
|
|
|
|
|
|
|
let pipeline_options = glsl::PipelineOptions {
|
2021-02-18 12:14:04 +00:00
|
|
|
shader_stage: stage,
|
|
|
|
entry_point: ep_name.to_string(),
|
2020-12-11 06:45:28 +00:00
|
|
|
};
|
|
|
|
|
2021-04-20 11:52:06 +00:00
|
|
|
let mut buffer = String::new();
|
2021-06-27 04:26:12 +00:00
|
|
|
let mut writer =
|
|
|
|
glsl::Writer::new(&mut buffer, module, info, &options, &pipeline_options).unwrap();
|
2020-12-11 06:45:28 +00:00
|
|
|
writer.write().unwrap();
|
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(
|
2021-06-23 16:47:49 +00:00
|
|
|
destination.join(format!("glsl/{}.{}.{:?}.glsl", file_name, ep_name, stage)),
|
2021-06-18 22:38:47 +00:00
|
|
|
buffer,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-12-08 05:23:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 15:36:26 +00:00
|
|
|
#[cfg(feature = "hlsl-out")]
|
2021-06-18 22:38:47 +00:00
|
|
|
fn write_output_hlsl(
|
|
|
|
module: &naga::Module,
|
|
|
|
info: &naga::valid::ModuleInfo,
|
|
|
|
destination: &PathBuf,
|
|
|
|
file_name: &str,
|
|
|
|
) {
|
2021-04-11 15:36:26 +00:00
|
|
|
use naga::back::hlsl;
|
2021-06-24 10:59:35 +00:00
|
|
|
let mut buffer = String::new();
|
2021-06-14 21:29:20 +00:00
|
|
|
let options = hlsl::Options::default();
|
2021-06-24 10:59:35 +00:00
|
|
|
let mut writer = hlsl::Writer::new(&mut buffer, &options);
|
|
|
|
let reflection_info = writer.write(module, info).unwrap();
|
2021-04-11 15:36:26 +00:00
|
|
|
|
2021-06-24 10:59:35 +00:00
|
|
|
fs::write(destination.join(format!("hlsl/{}.hlsl", file_name)), buffer).unwrap();
|
2021-04-14 18:20:48 +00:00
|
|
|
|
2021-06-14 21:29:20 +00:00
|
|
|
// We need a config file for validation script
|
|
|
|
// This file contains an info about profiles (shader stages) contains inside generated shader
|
|
|
|
// This info will be passed to dxc
|
|
|
|
let mut config_str = String::from("");
|
2021-07-05 13:35:48 +00:00
|
|
|
for (index, ep) in module.entry_points.iter().enumerate() {
|
|
|
|
let stage_str = match ep.stage {
|
2021-06-24 10:59:35 +00:00
|
|
|
naga::ShaderStage::Vertex => "vertex",
|
|
|
|
naga::ShaderStage::Fragment => "fragment",
|
|
|
|
naga::ShaderStage::Compute => "compute",
|
2021-06-14 21:29:20 +00:00
|
|
|
};
|
2021-06-16 07:04:40 +00:00
|
|
|
config_str = format!(
|
2021-07-12 03:14:07 +00:00
|
|
|
"{}{}={}_{}\n{}_name={}\n",
|
2021-06-24 10:59:35 +00:00
|
|
|
config_str,
|
|
|
|
stage_str,
|
2021-07-12 03:14:07 +00:00
|
|
|
ep.stage.to_hlsl_str(),
|
|
|
|
options.shader_model.to_str(),
|
2021-06-24 10:59:35 +00:00
|
|
|
stage_str,
|
2021-07-05 13:35:48 +00:00
|
|
|
&reflection_info.entry_points[index]
|
2021-06-16 07:04:40 +00:00
|
|
|
);
|
2021-06-14 21:29:20 +00:00
|
|
|
}
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(
|
|
|
|
destination.join(format!("hlsl/{}.hlsl.config", file_name)),
|
|
|
|
config_str,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-04-11 15:36:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 13:19:03 +00:00
|
|
|
#[cfg(feature = "wgsl-out")]
|
2021-06-18 22:38:47 +00:00
|
|
|
fn write_output_wgsl(
|
|
|
|
module: &naga::Module,
|
|
|
|
info: &naga::valid::ModuleInfo,
|
|
|
|
destination: &PathBuf,
|
|
|
|
file_name: &str,
|
|
|
|
) {
|
2021-04-19 13:19:03 +00:00
|
|
|
use naga::back::wgsl;
|
|
|
|
|
2021-04-20 10:02:13 +00:00
|
|
|
let string = wgsl::write_string(module, info).unwrap();
|
2021-04-19 13:19:03 +00:00
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
fs::write(destination.join(format!("wgsl/{}.wgsl", file_name)), string).unwrap();
|
2021-04-19 13:19:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 06:20:39 +00:00
|
|
|
#[cfg(feature = "wgsl-in")]
|
|
|
|
#[test]
|
2021-04-14 14:05:48 +00:00
|
|
|
fn convert_wgsl() {
|
2021-07-17 03:09:48 +00:00
|
|
|
let _ = env_logger::try_init();
|
|
|
|
|
2021-04-14 14:05:48 +00:00
|
|
|
let root = env!("CARGO_MANIFEST_DIR");
|
|
|
|
let inputs = [
|
|
|
|
(
|
|
|
|
"empty",
|
2021-04-19 13:19:03 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
|
2021-04-14 14:05:48 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"quad",
|
2021-06-14 21:29:20 +00:00
|
|
|
Targets::SPIRV
|
|
|
|
| Targets::METAL
|
|
|
|
| Targets::GLSL
|
|
|
|
| Targets::DOT
|
|
|
|
| Targets::HLSL
|
|
|
|
| Targets::WGSL,
|
2021-04-14 14:05:48 +00:00
|
|
|
),
|
2021-05-15 22:09:58 +00:00
|
|
|
(
|
|
|
|
"boids",
|
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::WGSL,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"skybox",
|
2021-07-18 02:38:04 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
|
2021-05-15 22:09:58 +00:00
|
|
|
),
|
2021-04-14 14:05:48 +00:00
|
|
|
(
|
|
|
|
"collatz",
|
2021-05-15 22:09:58 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::IR | Targets::ANALYSIS | Targets::WGSL,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"shadow",
|
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::WGSL,
|
2021-04-14 14:05:48 +00:00
|
|
|
),
|
2021-07-16 10:08:48 +00:00
|
|
|
(
|
|
|
|
"image",
|
|
|
|
Targets::SPIRV | Targets::METAL | Targets::HLSL | Targets::WGSL,
|
|
|
|
),
|
2021-05-15 22:09:58 +00:00
|
|
|
("extra", Targets::SPIRV | Targets::METAL | Targets::WGSL),
|
|
|
|
(
|
|
|
|
"operators",
|
2021-06-28 19:12:31 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
|
2021-05-15 22:09:58 +00:00
|
|
|
),
|
2021-04-16 20:16:15 +00:00
|
|
|
(
|
|
|
|
"interpolate",
|
2021-07-05 22:30:04 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
|
2021-04-16 20:16:15 +00:00
|
|
|
),
|
2021-05-15 22:09:58 +00:00
|
|
|
("access", Targets::SPIRV | Targets::METAL | Targets::WGSL),
|
2021-05-03 04:18:35 +00:00
|
|
|
(
|
|
|
|
"control-flow",
|
2021-07-08 20:55:14 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
|
2021-05-15 22:09:58 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"standard",
|
2021-06-28 17:06:53 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
|
2021-05-03 04:18:35 +00:00
|
|
|
),
|
2021-05-18 02:00:12 +00:00
|
|
|
//TODO: GLSL https://github.com/gfx-rs/naga/issues/874
|
2021-07-05 10:54:42 +00:00
|
|
|
(
|
|
|
|
"interface",
|
|
|
|
Targets::SPIRV | Targets::METAL | Targets::HLSL | Targets::WGSL,
|
|
|
|
),
|
2021-05-29 01:49:50 +00:00
|
|
|
(
|
|
|
|
"globals",
|
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::WGSL,
|
|
|
|
),
|
2021-05-27 22:02:38 +00:00
|
|
|
("bounds-check-zero", Targets::SPIRV),
|
2021-06-30 16:15:35 +00:00
|
|
|
(
|
|
|
|
"texture-arg",
|
2021-07-02 10:43:14 +00:00
|
|
|
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::WGSL,
|
2021-06-30 16:15:35 +00:00
|
|
|
),
|
2021-04-14 14:05:48 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for &(name, targets) in inputs.iter() {
|
|
|
|
println!("Processing '{}'", name);
|
2021-06-18 22:38:47 +00:00
|
|
|
// WGSL shaders lives in root dir as a privileged.
|
|
|
|
let file = fs::read_to_string(format!("{}/{}/{}.wgsl", root, BASE_DIR_IN, name))
|
2021-04-14 14:05:48 +00:00
|
|
|
.expect("Couldn't find wgsl file");
|
|
|
|
match naga::front::wgsl::parse_str(&file) {
|
|
|
|
Ok(module) => check_targets(&module, name, targets),
|
|
|
|
Err(e) => panic!("{}", e),
|
|
|
|
}
|
|
|
|
}
|
2021-02-07 02:02:49 +00:00
|
|
|
}
|
2021-02-08 17:30:21 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "spv-in")]
|
2021-04-02 04:16:11 +00:00
|
|
|
fn convert_spv(name: &str, adjust_coordinate_space: bool, targets: Targets) {
|
2021-07-17 03:09:48 +00:00
|
|
|
let _ = env_logger::try_init();
|
|
|
|
|
2021-04-14 18:20:48 +00:00
|
|
|
let root = env!("CARGO_MANIFEST_DIR");
|
2021-02-08 17:30:21 +00:00
|
|
|
let module = naga::front::spv::parse_u8_slice(
|
2021-06-18 22:38:47 +00:00
|
|
|
&fs::read(format!("{}/{}/spv/{}.spv", root, BASE_DIR_IN, name))
|
|
|
|
.expect("Couldn't find spv file"),
|
2021-04-02 04:16:11 +00:00
|
|
|
&naga::front::spv::Options {
|
|
|
|
adjust_coordinate_space,
|
2021-04-25 22:20:04 +00:00
|
|
|
strict_capabilities: false,
|
2021-04-02 04:16:11 +00:00
|
|
|
flow_graph_dump_prefix: None,
|
|
|
|
},
|
2021-02-08 17:30:21 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-18 02:46:32 +00:00
|
|
|
check_targets(&module, name, targets);
|
2021-05-05 21:52:50 +00:00
|
|
|
naga::valid::Validator::new(
|
|
|
|
naga::valid::ValidationFlags::all(),
|
|
|
|
naga::valid::Capabilities::empty(),
|
|
|
|
)
|
|
|
|
.validate(&module)
|
|
|
|
.unwrap();
|
2021-02-08 17:30:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 02:07:40 +00:00
|
|
|
#[cfg(feature = "spv-in")]
|
|
|
|
#[test]
|
|
|
|
fn convert_spv_quad_vert() {
|
2021-05-13 05:26:08 +00:00
|
|
|
convert_spv(
|
|
|
|
"quad-vert",
|
|
|
|
false,
|
2021-06-17 21:41:13 +00:00
|
|
|
Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
|
2021-05-13 05:26:08 +00:00
|
|
|
);
|
2021-03-27 02:07:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:30:21 +00:00
|
|
|
#[cfg(feature = "spv-in")]
|
|
|
|
#[test]
|
|
|
|
fn convert_spv_shadow() {
|
2021-04-02 04:16:11 +00:00
|
|
|
convert_spv("shadow", true, Targets::IR | Targets::ANALYSIS);
|
2021-02-08 17:30:21 +00:00
|
|
|
}
|
2021-02-11 19:43:37 +00:00
|
|
|
|
2021-05-27 22:02:38 +00:00
|
|
|
#[cfg(all(feature = "spv-in", feature = "spv-out"))]
|
|
|
|
#[test]
|
|
|
|
fn convert_spv_pointer_access() {
|
|
|
|
convert_spv("pointer-access", true, Targets::SPIRV);
|
|
|
|
}
|
|
|
|
|
2021-06-03 19:34:54 +00:00
|
|
|
#[cfg(feature = "glsl-in")]
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[test]
|
|
|
|
fn convert_glsl_folder() {
|
2021-07-17 03:09:48 +00:00
|
|
|
let _ = env_logger::try_init();
|
2021-07-14 16:42:02 +00:00
|
|
|
|
2021-06-03 19:34:54 +00:00
|
|
|
let root = env!("CARGO_MANIFEST_DIR");
|
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
for entry in std::fs::read_dir(format!("{}/{}/glsl", root, BASE_DIR_IN)).unwrap() {
|
2021-06-03 19:34:54 +00:00
|
|
|
let entry = entry.unwrap();
|
|
|
|
let file_name = entry.file_name().into_string().unwrap();
|
2021-07-14 15:55:58 +00:00
|
|
|
|
2021-06-18 22:38:47 +00:00
|
|
|
if file_name.ends_with(".ron") {
|
|
|
|
// No needed to validate ron files
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let params_path = format!(
|
|
|
|
"{}/{}/glsl/{}.params.ron",
|
|
|
|
root,
|
|
|
|
BASE_DIR_IN,
|
|
|
|
PathBuf::from(&file_name).with_extension("").display()
|
|
|
|
);
|
|
|
|
let is_params_used = PathBuf::from(¶ms_path).exists();
|
2021-06-03 19:34:54 +00:00
|
|
|
println!("Processing {}", file_name);
|
|
|
|
|
|
|
|
let mut entry_points = naga::FastHashMap::default();
|
2021-06-18 22:38:47 +00:00
|
|
|
if is_params_used {
|
|
|
|
let params: Parameters = match fs::read_to_string(¶ms_path) {
|
|
|
|
Ok(string) => ron::de::from_str(&string).expect("Couldn't find param file"),
|
|
|
|
Err(_) => panic!("Can't parse glsl params ron file: {:?}", ¶ms_path),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(vert) = params.glsl_vert_ep_name {
|
|
|
|
entry_points.insert(vert, naga::ShaderStage::Vertex);
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(frag) = params.glsl_frag_ep_name {
|
|
|
|
entry_points.insert(frag, naga::ShaderStage::Fragment);
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(comp) = params.glsl_comp_ep_name {
|
|
|
|
entry_points.insert(comp, naga::ShaderStage::Compute);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
let stage = match entry.path().extension().and_then(|s| s.to_str()).unwrap() {
|
|
|
|
"vert" => naga::ShaderStage::Vertex,
|
|
|
|
"frag" => naga::ShaderStage::Fragment,
|
|
|
|
"comp" => naga::ShaderStage::Compute,
|
|
|
|
ext => panic!("Unknown extension for glsl file {}", ext),
|
|
|
|
};
|
|
|
|
entry_points.insert("main".to_string(), stage);
|
|
|
|
}
|
2021-06-03 19:34:54 +00:00
|
|
|
|
2021-07-14 15:55:58 +00:00
|
|
|
let strip_unused_linkages = entry_points.len() > 1;
|
2021-06-03 19:34:54 +00:00
|
|
|
let module = naga::front::glsl::parse_str(
|
|
|
|
&fs::read_to_string(entry.path()).expect("Couldn't find glsl file"),
|
|
|
|
&naga::front::glsl::Options {
|
|
|
|
entry_points,
|
|
|
|
defines: Default::default(),
|
2021-07-14 15:55:58 +00:00
|
|
|
strip_unused_linkages: strip_unused_linkages,
|
2021-06-03 19:34:54 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let info = naga::valid::Validator::new(
|
|
|
|
naga::valid::ValidationFlags::all(),
|
|
|
|
naga::valid::Capabilities::all(),
|
|
|
|
)
|
|
|
|
.validate(&module)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-06-17 19:30:47 +00:00
|
|
|
#[cfg(feature = "wgsl-out")]
|
2021-06-18 22:38:47 +00:00
|
|
|
{
|
|
|
|
let dest = PathBuf::from(root).join(BASE_DIR_OUT);
|
|
|
|
write_output_wgsl(&module, &info, &dest, &file_name.replace(".", "-"));
|
|
|
|
}
|
2021-06-03 19:34:54 +00:00
|
|
|
}
|
|
|
|
}
|