[glsl-out] Fix statement indentation

This commit is contained in:
Igor Shaposhnik 2021-03-04 17:47:48 +03:00 committed by Dzmitry Malyshau
parent 52b34eb51b
commit 50e5904add
5 changed files with 24 additions and 34 deletions

View File

@ -519,6 +519,9 @@ impl<'a, W: Write> Writer<'a, W> {
"main",
)?;
// Add newline at the end of file
writeln!(self.out)?;
// Collect all of the texture mappings and return them to the user
self.collect_texture_mapping()
}
@ -997,38 +1000,29 @@ impl<'a, W: Write> Writer<'a, W> {
ctx: &FunctionCtx<'_, '_>,
indent: usize,
) -> BackendResult {
// The indentation is only for readability
write!(self.out, "{}", INDENT.repeat(indent))?;
match *sta {
// This is where we can generate intermediate constants for some expression types.
Statement::Emit(ref range) => {
let mut indented = true;
for handle in range.clone() {
if let Ok(ty_handle) = ctx.typifier.get_handle(handle) {
let min_ref_count = ctx.expressions[handle].bake_ref_count();
if min_ref_count <= ctx.info[handle].ref_count {
if !indented {
write!(self.out, "{}", INDENT.repeat(indent))?;
}
write!(self.out, "{}", INDENT.repeat(indent))?;
let name = format!("_expr{}", handle.index());
self.write_type(ty_handle)?;
write!(self.out, " {} = ", name)?;
self.write_expr(handle, ctx)?;
writeln!(self.out, ";")?;
self.cached_expressions.insert(handle, name);
indented = false;
}
}
}
if indented {
writeln!(self.out, ";")?;
}
}
// Blocks are simple we just need to write the block statements between braces
// We could also just print the statements but this is more readable and maps more
// closely to the IR
Statement::Block(ref block) => {
write!(self.out, "{}", INDENT.repeat(indent))?;
writeln!(self.out, "{{")?;
for sta in block.iter() {
// Increase the indentation to help with readability
@ -1049,6 +1043,7 @@ impl<'a, W: Write> Writer<'a, W> {
ref accept,
ref reject,
} => {
write!(self.out, "{}", INDENT.repeat(indent))?;
write!(self.out, "if(")?;
self.write_expr(condition, ctx)?;
writeln!(self.out, ") {{")?;
@ -1093,6 +1088,7 @@ impl<'a, W: Write> Writer<'a, W> {
ref default,
} => {
// Start the switch
write!(self.out, "{}", INDENT.repeat(indent))?;
write!(self.out, "switch(")?;
self.write_expr(selector, ctx)?;
writeln!(self.out, ") {{")?;
@ -1140,6 +1136,7 @@ impl<'a, W: Write> Writer<'a, W> {
ref body,
ref continuing,
} => {
write!(self.out, "{}", INDENT.repeat(indent))?;
writeln!(self.out, "while(true) {{")?;
for sta in body.iter().chain(continuing.iter()) {
@ -1150,11 +1147,18 @@ impl<'a, W: Write> Writer<'a, W> {
}
// Break, continue and return as written as in C
// `break;`
Statement::Break => writeln!(self.out, "break;")?,
Statement::Break => {
write!(self.out, "{}", INDENT.repeat(indent))?;
writeln!(self.out, "break;")?
}
// `continue;`
Statement::Continue => writeln!(self.out, "continue;")?,
Statement::Continue => {
write!(self.out, "{}", INDENT.repeat(indent))?;
writeln!(self.out, "continue;")?
}
// `return expr;`, `expr` is optional
Statement::Return { value } => {
write!(self.out, "{}", INDENT.repeat(indent))?;
write!(self.out, "return")?;
// Write the expression to be returned if needed
if let Some(expr) = value {
@ -1166,9 +1170,13 @@ impl<'a, W: Write> Writer<'a, W> {
// This is one of the places were glsl adds to the syntax of C in this case the discard
// keyword which ceases all further processing in a fragment shader, it's called OpKill
// in spir-v that's why it's called `Statement::Kill`
Statement::Kill => writeln!(self.out, "discard;")?,
Statement::Kill => {
write!(self.out, "{}", INDENT.repeat(indent))?;
writeln!(self.out, "discard;")?
}
// Stores in glsl are just variable assignments written as `pointer = value;`
Statement::Store { pointer, value } => {
write!(self.out, "{}", INDENT.repeat(indent))?;
self.write_expr(pointer, ctx)?;
write!(self.out, " = ")?;
self.write_expr(value, ctx)?;
@ -1181,6 +1189,7 @@ impl<'a, W: Write> Writer<'a, W> {
array_index,
value,
} => {
write!(self.out, "{}", INDENT.repeat(indent))?;
// This will only panic if the module is invalid
let dim = match *ctx.typifier.get(image, &self.module.types) {
TypeInner::Image { dim, .. } => dim,
@ -1201,6 +1210,7 @@ impl<'a, W: Write> Writer<'a, W> {
ref arguments,
result,
} => {
write!(self.out, "{}", INDENT.repeat(indent))?;
if let Some(expr) = result {
let name = format!("_expr{}", expr.index());
let ty = self.module.functions[function].return_type.unwrap();

View File

@ -13,8 +13,6 @@ uniform sampler2D _group_0_binding_0;
out vec4 _location_0;
void main() {
;
_location_0 = texture(_group_0_binding_0, vec2(_location_0_vs));
return;
}

View File

@ -13,11 +13,7 @@ in vec2 _location_1;
out vec2 _location_0_vs;
void main() {
;
_location_0_vs = _location_1;
;
;
gl_Position = vec4((1.2 * _location_0), 0.0, 1.0);
return;
}

View File

@ -18,8 +18,6 @@ in vec3 _location_0_vs;
out vec4 _location_0;
void main() {
;
_location_0 = texture(_group_0_binding_1, vec3(_location_0_vs));
return;
}

View File

@ -22,24 +22,12 @@ void main() {
int tmp1_;
int tmp2_;
vec4 unprojected;
;
;
tmp1_ = (int(gl_VertexID) / 2);
;
;
tmp2_ = (int(gl_VertexID) & 1);
;
;
;
;
;
vec4 _expr28 = vec4(((float(tmp1_) * 4.0) - 1.0), ((float(tmp2_) * 4.0) - 1.0), 0.0, 1.0);
;
;
unprojected = (_group_0_binding_0.proj_inv * _expr28);
vec4 _expr56 = unprojected;
_location_0_vs = (transpose(mat3x3(vec3(_group_0_binding_0.view[0][0], _group_0_binding_0.view[0][1], _group_0_binding_0.view[0][2]), vec3(_group_0_binding_0.view[1][0], _group_0_binding_0.view[1][1], _group_0_binding_0.view[1][2]), vec3(_group_0_binding_0.view[2][0], _group_0_binding_0.view[2][1], _group_0_binding_0.view[2][2]))) * vec3(_expr56[0], _expr56[1], _expr56[2]));
gl_Position = _expr28;
return;
}