[spv-in] add a flag to adjust the coordinate space

This commit is contained in:
Dzmitry Malyshau
2021-04-02 00:16:11 -04:00
committed by Dzmitry Malyshau
parent 100c2f42ed
commit ead052b773
5 changed files with 51 additions and 30 deletions

View File

@@ -5,6 +5,8 @@ use std::{env, error::Error, path::Path};
#[derive(Default)]
struct Parameters {
#[cfg(feature = "spv-in")]
spv_adjust_coordinate_space: bool,
#[cfg(feature = "spv-in")]
spv_flow_dump_prefix: Option<String>,
#[cfg(feature = "spv-out")]
@@ -102,6 +104,7 @@ fn main() {
#[cfg(feature = "spv-in")]
"spv" => {
let options = naga::front::spv::Options {
adjust_coordinate_space: params.spv_adjust_coordinate_space,
flow_graph_dump_prefix: params.spv_flow_dump_prefix.map(std::path::PathBuf::from),
};
let input = fs::read(input_path).unwrap();

View File

@@ -279,31 +279,34 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
}
}
let position_index = members.iter().position(|member| match member.binding {
Some(crate::Binding::BuiltIn(crate::BuiltIn::Position)) => true,
_ => false,
});
if let Some(component_index) = position_index {
let old_len = function.expressions.len();
let global_expr = components[component_index];
let access_expr = function.expressions.append(crate::Expression::AccessIndex {
base: global_expr,
index: 1,
});
let load_expr = function.expressions.append(crate::Expression::Load {
pointer: access_expr,
});
let neg_expr = function.expressions.append(crate::Expression::Unary {
op: crate::UnaryOperator::Negate,
expr: load_expr,
});
function.body.push(crate::Statement::Emit(
function.expressions.range_from(old_len),
));
function.body.push(crate::Statement::Store {
pointer: access_expr,
value: neg_expr,
if self.options.adjust_coordinate_space {
let position_index = members.iter().position(|member| match member.binding {
Some(crate::Binding::BuiltIn(crate::BuiltIn::Position)) => true,
_ => false,
});
if let Some(component_index) = position_index {
// The IR is Y-up, while SPIR-V is Y-down.
let old_len = function.expressions.len();
let global_expr = components[component_index];
let access_expr = function.expressions.append(crate::Expression::AccessIndex {
base: global_expr,
index: 1,
});
let load_expr = function.expressions.append(crate::Expression::Load {
pointer: access_expr,
});
let neg_expr = function.expressions.append(crate::Expression::Unary {
op: crate::UnaryOperator::Negate,
expr: load_expr,
});
function.body.push(crate::Statement::Emit(
function.expressions.range_from(old_len),
));
function.body.push(crate::Statement::Store {
pointer: access_expr,
value: neg_expr,
});
}
}
let old_len = function.expressions.len();

View File

@@ -312,11 +312,24 @@ enum ExtendedClass {
Output,
}
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
pub struct Options {
/// The IR coordinate space matches all the APIs except SPIR-V,
/// so by default we flip the Y coordinate of the `BuiltIn::Position`.
/// This flag can be used to avoid this.
pub adjust_coordinate_space: bool,
pub flow_graph_dump_prefix: Option<PathBuf>,
}
impl Default for Options {
fn default() -> Self {
Options {
adjust_coordinate_space: true,
flow_graph_dump_prefix: None,
}
}
}
pub struct Parser<I> {
data: I,
state: ModuleState,

View File

@@ -69,7 +69,6 @@ vertex main2Output main2(
a_uv = a_uv1;
a_pos = a_pos1;
main1(v_uv, a_uv, _, a_pos);
_.gl_Position.y = -_.gl_Position.y;
const auto _tmp = type10 {v_uv, _.gl_Position, _.gl_PointSize, {_.gl_ClipDistance[0]}};
return main2Output { _tmp.member, _tmp.gl_Position1, _tmp.gl_PointSize1, {_tmp.gl_ClipDistance1[0]} };
}

View File

@@ -279,10 +279,13 @@ fn convert_wgsl_texture_array() {
}
#[cfg(feature = "spv-in")]
fn convert_spv(name: &str, targets: Targets) {
fn convert_spv(name: &str, adjust_coordinate_space: bool, targets: Targets) {
let module = naga::front::spv::parse_u8_slice(
&std::fs::read(format!("tests/in/{}{}", name, ".spv")).expect("Couldn't find spv file"),
&Default::default(),
&naga::front::spv::Options {
adjust_coordinate_space,
flow_graph_dump_prefix: None,
},
)
.unwrap();
check_targets(&module, name, targets);
@@ -294,13 +297,13 @@ fn convert_spv(name: &str, targets: Targets) {
#[cfg(feature = "spv-in")]
#[test]
fn convert_spv_quad_vert() {
convert_spv("quad-vert", Targets::METAL);
convert_spv("quad-vert", false, Targets::METAL);
}
#[cfg(feature = "spv-in")]
#[test]
fn convert_spv_shadow() {
convert_spv("shadow", Targets::IR | Targets::ANALYSIS);
convert_spv("shadow", true, Targets::IR | Targets::ANALYSIS);
}
#[cfg(feature = "glsl-in")]