From d7225b4eb625f0a9ea0d89decb3c1443979229da Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Mon, 25 Jan 2021 21:03:45 -0330 Subject: [PATCH] [glsl-out] Improve formatting of whitespace --- src/back/glsl/mod.rs | 69 ++++++++++--------- .../snapshots__empty-Compute.glsl.snap | 6 +- .../snapshots__quad-Fragment.glsl.snap | 11 ++- .../snapshots__quad-Vertex.glsl.snap | 13 ++-- .../snapshots__skybox-Fragment.glsl.snap | 16 ++--- .../snapshots__skybox-Vertex.glsl.snap | 35 +++++----- 6 files changed, 73 insertions(+), 77 deletions(-) diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index ddb1c6c6c0..a5fb8f27b3 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -38,7 +38,7 @@ // // The only thing that glsl removes that makes a difference are pointers. // -// Addititions that are relevant for the backend are the discard keyword, the introduction of +// Additions that are relevant for the backend are the discard keyword, the introduction of // vector, matrices, samplers, image types and functions that provide common shader operations pub use features::Features; @@ -71,6 +71,7 @@ mod keywords; pub const SUPPORTED_CORE_VERSIONS: &[u16] = &[330, 400, 410, 420, 430, 440, 450]; /// List of supported es glsl versions pub const SUPPORTED_ES_VERSIONS: &[u16] = &[300, 310, 320]; +const INDENT: &str = " "; /// glsl version #[derive(Debug, Copy, Clone, PartialEq)] @@ -341,7 +342,7 @@ impl<'a, W: Write> Writer<'a, W> { /// # Panics /// Might panic if the module is invalid pub fn write(&mut self) -> Result, Error> { - // We use `writeln!(self.out)` troughout the write to add newlines + // We use `writeln!(self.out)` throughout the write to add newlines // to make the output more readable let es = self.options.version.is_es(); @@ -355,12 +356,13 @@ impl<'a, W: Write> Writer<'a, W> { // extensions to appear before being used, even though extensions are part of the // preprocessor not the processor ¯\_(ツ)_/¯ self.features.write(self.options.version, &mut self.out)?; - writeln!(self.out)?; // glsl es requires a precision to be specified for floats // TODO: Should this be user configurable? if es { - writeln!(self.out, "precision highp float;\n")?; + writeln!(self.out)?; + writeln!(self.out, "precision highp float;")?; + writeln!(self.out)?; } // Enable early depth tests if needed @@ -378,10 +380,9 @@ impl<'a, W: Write> Writer<'a, W> { } )?; } + writeln!(self.out)?; } - writeln!(self.out)?; - // Write all structs // // This are always ordered because of the IR is structured in a way that you can't make a @@ -396,8 +397,6 @@ impl<'a, W: Write> Writer<'a, W> { } } - writeln!(self.out)?; - // Write the globals // // We filter all globals that aren't used by the selected entry point as they might be @@ -460,7 +459,9 @@ impl<'a, W: Write> Writer<'a, W> { self.out, " {};", self.names[&NameKey::GlobalVariable(handle)] - )? + )?; + + writeln!(self.out)?; } // glsl has no concept of samplers so we just ignore it TypeInner::Sampler { .. } => continue, @@ -469,8 +470,6 @@ impl<'a, W: Write> Writer<'a, W> { } } - writeln!(self.out)?; - // Sort the graph topologically so that functions calls are valid // It's impossible for this to panic because the IR forbids cycles let functions = petgraph::algo::toposort(&self.call_graph, None).unwrap(); @@ -589,8 +588,8 @@ impl<'a, W: Write> Writer<'a, W> { // Write the block members for (idx, member) in members.iter().enumerate() { - // Add a tab for identation (readability only) - writeln!(self.out, "\t")?; + // Add a tab for indentation (readability only) + write!(self.out, "{}", INDENT)?; // Write the member type self.write_type(member.ty)?; @@ -714,6 +713,7 @@ impl<'a, W: Write> Writer<'a, W> { // Finally write the global name and end the global with a `;` and a newline // Leading space is important writeln!(self.out, " {};", self.get_global_name(handle, global))?; + writeln!(self.out)?; Ok(()) } @@ -730,7 +730,7 @@ impl<'a, W: Write> Writer<'a, W> { match global.binding { Some(Binding::Location(location)) => { format!( - " _location_{}{}", + "_location_{}{}", location, match (self.options.entry_point.0, global.class) { (ShaderStage::Fragment, StorageClass::Input) => "_vs", @@ -740,7 +740,7 @@ impl<'a, W: Write> Writer<'a, W> { ) } Some(Binding::Resource { group, binding }) => { - format!(" _group_{}_binding_{}", group, binding) + format!("_group_{}_binding_{}", group, binding) } Some(Binding::BuiltIn(built_in)) => glsl_built_in(built_in).to_string(), None => self.names[&NameKey::GlobalVariable(handle)].clone(), @@ -824,9 +824,9 @@ impl<'a, W: Write> Writer<'a, W> { // // Always adds a newline for (handle, local) in func.local_variables.iter() { - // Write identation (only for readability) and the type + // Write indentation (only for readability) and the type // `write_type` adds no trailing space - write!(self.out, "\t")?; + write!(self.out, "{}", INDENT)?; self.write_type(local.ty)?; // Write the local name @@ -848,8 +848,6 @@ impl<'a, W: Write> Writer<'a, W> { writeln!(self.out, ";")? } - writeln!(self.out)?; - // Write the function body (statement list) for sta in func.body.iter() { // Write a statement, the indentation should always be 1 when writing the function body @@ -949,8 +947,8 @@ impl<'a, W: Write> Writer<'a, W> { writeln!(self.out, "struct {} {{", self.names[&NameKey::Type(handle)])?; for (idx, member) in members.iter().enumerate() { - // The identation is only for readability - write!(self.out, "\t")?; + // The indentation is only for readability + write!(self.out, "{}", INDENT)?; // Write the member type // Adds no trailing space @@ -967,6 +965,8 @@ impl<'a, W: Write> Writer<'a, W> { } writeln!(self.out, "}};")?; + writeln!(self.out)?; + Ok(()) } @@ -981,7 +981,7 @@ impl<'a, W: Write> Writer<'a, W> { indent: usize, ) -> BackendResult { // The indentation is only for readability - write!(self.out, "{}", "\t".repeat(indent))?; + write!(self.out, "{}", INDENT.repeat(indent))?; match sta { // Blocks are simple we just need to write the block statements between braces @@ -993,7 +993,7 @@ impl<'a, W: Write> Writer<'a, W> { // Increase the indentation to help with readability self.write_stmt(sta, ctx, indent + 1)? } - writeln!(self.out, "{}}}", "\t".repeat(indent))? + writeln!(self.out, "{}}}", INDENT.repeat(indent))? } // Ifs are written as in C: // ``` @@ -1020,15 +1020,15 @@ impl<'a, W: Write> Writer<'a, W> { // If there are no statements in the reject block we skip writing it // This is only for readability if !reject.is_empty() { - writeln!(self.out, "{}}} else {{", "\t".repeat(indent))?; + writeln!(self.out, "{}}} else {{", INDENT.repeat(indent))?; for sta in reject { - // Increase identation to help with readability + // Increase indentation to help with readability self.write_stmt(sta, ctx, indent + 1)?; } } - writeln!(self.out, "{}}}", "\t".repeat(indent))? + writeln!(self.out, "{}}}", INDENT.repeat(indent))? } // Switch are written as in C: // ``` @@ -1058,7 +1058,12 @@ impl<'a, W: Write> Writer<'a, W> { // Write all cases for case in cases { - writeln!(self.out, "{}case {}:", "\t".repeat(indent + 1), case.value)?; + writeln!( + self.out, + "{}case {}:", + INDENT.repeat(indent + 1), + case.value + )?; for sta in case.body.iter() { self.write_stmt(sta, ctx, indent + 2)?; @@ -1066,21 +1071,21 @@ impl<'a, W: Write> Writer<'a, W> { // Write `break;` if the block isn't fallthrough if case.fall_through { - writeln!(self.out, "{}break;", "\t".repeat(indent + 2))?; + writeln!(self.out, "{}break;", INDENT.repeat(indent + 2))?; } } // Only write the default block if the block isn't empty // Writing default without a block is valid but it's more readable this way if !default.is_empty() { - writeln!(self.out, "{}default:", "\t".repeat(indent + 1))?; + writeln!(self.out, "{}default:", INDENT.repeat(indent + 1))?; for sta in default { self.write_stmt(sta, ctx, indent + 2)?; } } - writeln!(self.out, "{}}}", "\t".repeat(indent))? + writeln!(self.out, "{}}}", INDENT.repeat(indent))? } // Loops in naga IR are based on wgsl loops, glsl can emulate the behaviour by using a // while true loop and appending the continuing block to the body resulting on: @@ -1097,7 +1102,7 @@ impl<'a, W: Write> Writer<'a, W> { self.write_stmt(sta, ctx, indent + 1)?; } - writeln!(self.out, "{}}}", "\t".repeat(indent))? + writeln!(self.out, "{}}}", INDENT.repeat(indent))? } // Break, continue and return as written as in C // `break;` @@ -1342,7 +1347,7 @@ impl<'a, W: Write> Writer<'a, W> { // "~" - for `Not` if it's an integer // "!" - for `Not` if it's a boolean // - // We also wrap the everything in parantheses to avoid precedence issues + // We also wrap the everything in parentheses to avoid precedence issues Expression::Unary { op, expr } => { write!( self.out, diff --git a/tests/snapshots/snapshots__empty-Compute.glsl.snap b/tests/snapshots/snapshots__empty-Compute.glsl.snap index 9fb9bfbaa1..2a8a2b72e1 100644 --- a/tests/snapshots/snapshots__empty-Compute.glsl.snap +++ b/tests/snapshots/snapshots__empty-Compute.glsl.snap @@ -6,11 +6,7 @@ expression: string precision highp float; - - - void main() { - - return; + return; } diff --git a/tests/snapshots/snapshots__quad-Fragment.glsl.snap b/tests/snapshots/snapshots__quad-Fragment.glsl.snap index 75d517f2fa..d61405935d 100644 --- a/tests/snapshots/snapshots__quad-Fragment.glsl.snap +++ b/tests/snapshots/snapshots__quad-Fragment.glsl.snap @@ -6,15 +6,14 @@ expression: string precision highp float; +in vec2 _location_0_vs; - -in vec2 _location_0_vs; uniform sampler2D u_texture; -out vec4 _location_0; + +out vec4 _location_0; void main() { - - _location_0 = texture( _group_0_binding_0, vec2( _location_0_vs)); - return; + _location_0 = texture(_group_0_binding_0, vec2(_location_0_vs)); + return; } diff --git a/tests/snapshots/snapshots__quad-Vertex.glsl.snap b/tests/snapshots/snapshots__quad-Vertex.glsl.snap index d79cd676a2..63e133f4bf 100644 --- a/tests/snapshots/snapshots__quad-Vertex.glsl.snap +++ b/tests/snapshots/snapshots__quad-Vertex.glsl.snap @@ -6,16 +6,15 @@ expression: string precision highp float; +in vec2 _location_0; +in vec2 _location_1; -in vec2 _location_0; -in vec2 _location_1; -out vec2 _location_0_vs; +out vec2 _location_0_vs; void main() { - - _location_0_vs = _location_1; - gl_Position = vec4((1.2 * _location_0), 0.0, 1.0); - return; + _location_0_vs = _location_1; + gl_Position = vec4((1.2 * _location_0), 0.0, 1.0); + return; } diff --git a/tests/snapshots/snapshots__skybox-Fragment.glsl.snap b/tests/snapshots/snapshots__skybox-Fragment.glsl.snap index 24583ee9fb..175ebf3ce0 100644 --- a/tests/snapshots/snapshots__skybox-Fragment.glsl.snap +++ b/tests/snapshots/snapshots__skybox-Fragment.glsl.snap @@ -6,19 +6,19 @@ expression: string precision highp float; - struct Data { - mat4x4 proj_inv; - mat4x4 view; + mat4x4 proj_inv; + mat4x4 view; }; uniform samplerCube r_texture; -in vec3 _location_0_vs; -out vec4 _location_0; + +in vec3 _location_0_vs; + +out vec4 _location_0; void main() { - - _location_0 = texture( _group_0_binding_1, vec3( _location_0_vs)); - return; + _location_0 = texture(_group_0_binding_1, vec3(_location_0_vs)); + return; } diff --git a/tests/snapshots/snapshots__skybox-Vertex.glsl.snap b/tests/snapshots/snapshots__skybox-Vertex.glsl.snap index 651626a5f3..c5bd31b67e 100644 --- a/tests/snapshots/snapshots__skybox-Vertex.glsl.snap +++ b/tests/snapshots/snapshots__skybox-Vertex.glsl.snap @@ -6,30 +6,27 @@ expression: string precision highp float; - struct Data { - mat4x4 proj_inv; - mat4x4 view; + mat4x4 proj_inv; + mat4x4 view; }; -out vec3 _location_0_vs; +out vec3 _location_0_vs; + uniform Data_block_0 { - -mat4x4 proj_inv; - -mat4x4 view; -} _group_0_binding_0; + mat4x4 proj_inv; + mat4x4 view; +} _group_0_binding_0; void main() { - int tmp1_; - int tmp2_; - vec4 unprojected; - - tmp1_ = (int(gl_VertexID) / 2); - tmp2_ = (int(gl_VertexID) & 1); - unprojected = ( _group_0_binding_0.proj_inv * vec4(((float(tmp1_) * 4.0) - 1.0), ((float(tmp2_) * 4.0) - 1.0), 0.0, 1.0)); - _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(unprojected[0], unprojected[1], unprojected[2])); - gl_Position = vec4(((float(tmp1_) * 4.0) - 1.0), ((float(tmp2_) * 4.0) - 1.0), 0.0, 1.0); - return; + int tmp1_; + int tmp2_; + vec4 unprojected; + tmp1_ = (int(gl_VertexID) / 2); + tmp2_ = (int(gl_VertexID) & 1); + unprojected = (_group_0_binding_0.proj_inv * vec4(((float(tmp1_) * 4.0) - 1.0), ((float(tmp2_) * 4.0) - 1.0), 0.0, 1.0)); + _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(unprojected[0], unprojected[1], unprojected[2])); + gl_Position = vec4(((float(tmp1_) * 4.0) - 1.0), ((float(tmp2_) * 4.0) - 1.0), 0.0, 1.0); + return; }