Bug 1728659 - Output struct field names for single-component swizzles in glsl-to-cxx. r=jrmuizel

Differential Revision: https://phabricator.services.mozilla.com/D124254
This commit is contained in:
Lee Salzman 2021-09-01 21:34:04 +00:00
parent c196398803
commit aac91caa52
2 changed files with 25 additions and 28 deletions

View File

@ -1252,7 +1252,7 @@ pub struct Expr {
pub ty: Type,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FieldSet {
Rgba,
Xyzw,
@ -1336,9 +1336,9 @@ impl SwizzleSelector {
}
}
pub fn to_string(&self) -> String {
pub fn to_field_set(&self, field_set: FieldSet) -> String {
let mut s = String::new();
let fs = match self.field_set {
let fs = match field_set {
FieldSet::Rgba => ['r', 'g', 'b', 'a'],
FieldSet::Xyzw => ['x', 'y', 'z', 'w'],
FieldSet::Stpq => ['s', 't', 'p', 'q'],
@ -1348,6 +1348,10 @@ impl SwizzleSelector {
}
s
}
pub fn to_string(&self) -> String {
self.to_field_set(self.field_set)
}
}
/// The most general form of an expression. As you can see if you read the variant list, in GLSL, an

View File

@ -9,7 +9,7 @@ mod hir;
use glsl::parser::Parse;
use glsl::syntax;
use glsl::syntax::{TranslationUnit, UnaryOp};
use hir::{Statement, SwizzleSelector, Type};
use hir::{Statement, Type};
use std::cell::{Cell, RefCell};
use std::collections::{BTreeMap, HashMap};
use std::io::Read;
@ -1465,25 +1465,6 @@ pub fn show_double(state: &OutputState, x: f64) {
}
}
trait SwizzelSelectorExt {
fn to_args(&self) -> String;
}
impl SwizzelSelectorExt for SwizzleSelector {
fn to_args(&self) -> String {
let mut s = Vec::new();
let fs = match self.field_set {
hir::FieldSet::Rgba => ["R", "G", "B", "A"],
hir::FieldSet::Xyzw => ["X", "Y", "Z", "W"],
hir::FieldSet::Stpq => ["S", "T", "P", "Q"],
};
for i in &self.components {
s.push(fs[*i as usize])
}
s.join(", ")
}
}
fn expr_run_class(state: &OutputState, expr: &hir::Expr) -> hir::RunClass {
match &expr.kind {
hir::ExprKind::Variable(i) => symbol_run_class(&state.hir.sym(*i).decl, state.vector_mask),
@ -2018,13 +1999,25 @@ pub fn show_hir_expr_inner(state: &OutputState, expr: &hir::Expr, top_level: boo
}
state.write("(");
show_hir_expr(state, &e);
if state.is_lval.get() && s.components.len() > 1 {
state.write(").lsel(");
state.write(").");
if s.components.len() == 1 {
// For single component swizzles, output a field access to
// avoid stressing inlining of sel().
state.write(&s.to_field_set(hir::FieldSet::Xyzw));
} else {
state.write(").sel(");
if state.is_lval.get() && s.components.len() > 1 {
state.write("lsel(");
} else {
state.write("sel(");
}
for (i, c) in s.to_string().chars().enumerate() {
if i > 0 {
state.write(",");
}
write!(state, "{}", c.to_uppercase());
}
state.write(")");
}
state.write(&s.to_args());
state.write(")");
} else {
state.write("(");
show_hir_expr(state, &e);