[glsl\wgsl out] Remove unnecessary clones (#1492)

* [glsl-out] Remove unnecessary clones

* [wgsl-out] Remove unnecessary clones
This commit is contained in:
Igor Shaposhnik
2021-10-28 20:54:32 +03:00
committed by GitHub
parent d1cfdd15a0
commit 9d2b357214
2 changed files with 41 additions and 22 deletions

View File

@@ -617,17 +617,10 @@ impl<'a, W: Write> Writer<'a, W> {
continue; continue;
} }
// We also `clone` to satisfy the borrow checker
let name = self.names[&NameKey::Function(handle)].clone();
let fun_info = &self.info[handle]; let fun_info = &self.info[handle];
// Write the function // Write the function
self.write_function( self.write_function(back::FunctionType::Function(handle), function, fun_info)?;
back::FunctionType::Function(handle),
function,
fun_info,
&name,
)?;
writeln!(self.out)?; writeln!(self.out)?;
} }
@@ -636,7 +629,6 @@ impl<'a, W: Write> Writer<'a, W> {
back::FunctionType::EntryPoint(self.entry_point_idx), back::FunctionType::EntryPoint(self.entry_point_idx),
&self.entry_point.function, &self.entry_point.function,
ep_info, ep_info,
"main",
)?; )?;
// Add newline at the end of file // Add newline at the end of file
@@ -882,8 +874,8 @@ impl<'a, W: Write> Writer<'a, W> {
// Finally write the global name and end the global with a `;` and a newline // Finally write the global name and end the global with a `;` and a newline
// Leading space is important // Leading space is important
let global_name = self.get_global_name(handle, global); write!(self.out, " ")?;
write!(self.out, " {}", global_name)?; self.write_global_name(handle, global)?;
if let TypeInner::Array { size, .. } = self.module.types[global.ty].inner { if let TypeInner::Array { size, .. } = self.module.types[global.ty].inner {
self.write_array_size(size)?; self.write_array_size(size)?;
} }
@@ -920,6 +912,24 @@ impl<'a, W: Write> Writer<'a, W> {
} }
} }
/// Helper method used to write a name for a global without additional heap allocation
fn write_global_name(
&mut self,
handle: Handle<crate::GlobalVariable>,
global: &crate::GlobalVariable,
) -> BackendResult {
match global.binding {
Some(ref br) => write!(self.out, "_group_{}_binding_{}", br.group, br.binding)?,
None => write!(
self.out,
"{}",
&self.names[&NameKey::GlobalVariable(handle)]
)?,
}
Ok(())
}
/// Writes the varying declaration. /// Writes the varying declaration.
fn write_varying( fn write_varying(
&mut self, &mut self,
@@ -1013,7 +1023,6 @@ impl<'a, W: Write> Writer<'a, W> {
ty: back::FunctionType, ty: back::FunctionType,
func: &crate::Function, func: &crate::Function,
info: &valid::FunctionInfo, info: &valid::FunctionInfo,
name: &str,
) -> BackendResult { ) -> BackendResult {
// Create a function context for the function being written // Create a function context for the function being written
let ctx = back::FunctionCtx { let ctx = back::FunctionCtx {
@@ -1047,7 +1056,11 @@ impl<'a, W: Write> Writer<'a, W> {
} }
// Write the function name and open parentheses for the argument list // Write the function name and open parentheses for the argument list
write!(self.out, " {}(", name)?; let function_name = match ctx.ty {
back::FunctionType::Function(handle) => &self.names[&NameKey::Function(handle)],
back::FunctionType::EntryPoint(_) => "main",
};
write!(self.out, " {}(", function_name)?;
// Write the comma separated argument list // Write the comma separated argument list
// //
@@ -1609,9 +1622,6 @@ impl<'a, W: Write> Writer<'a, W> {
stage: ep.stage, stage: ep.stage,
output: true, output: true,
}; };
let field_name = self.names
[&NameKey::StructMember(result.ty, index as u32)]
.clone();
write!(self.out, "{} = ", varying_name)?; write!(self.out, "{} = ", varying_name)?;
if let Some(struct_name) = temp_struct_name { if let Some(struct_name) = temp_struct_name {
@@ -1620,7 +1630,13 @@ impl<'a, W: Write> Writer<'a, W> {
self.write_expr(value, ctx)?; self.write_expr(value, ctx)?;
} }
writeln!(self.out, ".{};", field_name)?; // Write field name
writeln!(
self.out,
".{};",
&self.names
[&NameKey::StructMember(result.ty, index as u32)]
)?;
write!(self.out, "{}", level)?; write!(self.out, "{}", level)?;
} }
} }
@@ -1872,7 +1888,7 @@ impl<'a, W: Write> Writer<'a, W> {
// `get_global_name` does the work for us // `get_global_name` does the work for us
Expression::GlobalVariable(handle) => { Expression::GlobalVariable(handle) => {
let global = &self.module.global_variables[handle]; let global = &self.module.global_variables[handle];
write!(self.out, "{}", self.get_global_name(handle, global))? self.write_global_name(handle, global)?
} }
// A local is written as it's name // A local is written as it's name
Expression::LocalVariable(handle) => { Expression::LocalVariable(handle) => {

View File

@@ -1652,7 +1652,6 @@ impl<W: Write> Writer<W> {
global: &crate::GlobalVariable, global: &crate::GlobalVariable,
handle: Handle<crate::GlobalVariable>, handle: Handle<crate::GlobalVariable>,
) -> BackendResult { ) -> BackendResult {
let name = self.names[&NameKey::GlobalVariable(handle)].clone();
// Write group and dinding attributes if present // Write group and dinding attributes if present
if let Some(ref binding) = global.binding { if let Some(ref binding) = global.binding {
self.write_attributes( self.write_attributes(
@@ -1675,7 +1674,11 @@ impl<W: Write> Writer<W> {
} }
write!(self.out, ">")?; write!(self.out, ">")?;
} }
write!(self.out, " {}: ", name)?; write!(
self.out,
" {}: ",
&self.names[&NameKey::GlobalVariable(handle)]
)?;
// Write global type // Write global type
self.write_type(module, global.ty)?; self.write_type(module, global.ty)?;
@@ -1765,7 +1768,7 @@ impl<W: Write> Writer<W> {
width: _, width: _,
ref value, ref value,
} => { } => {
let name = self.names[&NameKey::Constant(handle)].clone(); let name = &self.names[&NameKey::Constant(handle)];
// First write only constant name // First write only constant name
write!(self.out, "let {}: ", name)?; write!(self.out, "let {}: ", name)?;
// Next write constant type and value // Next write constant type and value
@@ -1789,7 +1792,7 @@ impl<W: Write> Writer<W> {
writeln!(self.out, ";")?; writeln!(self.out, ";")?;
} }
crate::ConstantInner::Composite { ty, ref components } => { crate::ConstantInner::Composite { ty, ref components } => {
let name = self.names[&NameKey::Constant(handle)].clone(); let name = &self.names[&NameKey::Constant(handle)];
// First write only constant name // First write only constant name
write!(self.out, "let {}: ", name)?; write!(self.out, "let {}: ", name)?;
// Next write constant type // Next write constant type