diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index fccbb5eec7..daca40b1db 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -1252,7 +1252,7 @@ impl<'a, W: Write> Writer<'a, W> { if global.space.initializable() && is_value_init_supported(self.module, global.ty) { write!(self.out, " = ")?; if let Some(init) = global.init { - self.write_const_expr(init)?; + self.write_global_const_expr(init)?; } else { self.write_zero_init_value(global.ty)?; } @@ -1788,6 +1788,13 @@ impl<'a, W: Write> Writer<'a, W> { // Write indentation (only for readability) and the type // `write_type` adds no trailing space write!(self.out, "{}", back::INDENT)?; + + if let Some(init) = local.init { + if ctx.expr_kind_tracker.is_const(init) { + write!(self.out, "const ")?; + } + } + self.write_type(local.ty)?; // Write the local name @@ -1897,7 +1904,7 @@ impl<'a, W: Write> Writer<'a, W> { self.write_array_size(base, size)?; } write!(self.out, " = ")?; - self.write_const_expr(constant.init)?; + self.write_global_const_expr(constant.init)?; writeln!(self.out, ";")?; Ok(()) } @@ -2648,12 +2655,30 @@ impl<'a, W: Write> Writer<'a, W> { /// /// [`Expression`]: crate::Expression /// [`Module`]: crate::Module - fn write_const_expr(&mut self, expr: Handle) -> BackendResult { + fn write_global_const_expr(&mut self, expr: Handle) -> BackendResult { + self.write_const_expr(expr, &self.module.global_expressions) + } + + /// Write a const expression. + /// + /// Write `expr`, a handle to an [`Expression`] in the current [`Module`]'s + /// constant expression arena, as GLSL expression. + /// + /// # Notes + /// Adds no newlines or leading/trailing whitespace + /// + /// [`Expression`]: crate::Expression + /// [`Module`]: crate::Module + fn write_const_expr( + &mut self, + expr: Handle, + arena: &crate::Arena, + ) -> BackendResult { self.write_possibly_const_expr( expr, - &self.module.global_expressions, + arena, |expr| &self.info[expr], - |writer, expr| writer.write_const_expr(expr), + |writer, expr| writer.write_const_expr(expr, arena), ) } @@ -2720,7 +2745,7 @@ impl<'a, W: Write> Writer<'a, W> { if constant.name.is_some() { write!(self.out, "{}", self.names[&NameKey::Constant(handle)])?; } else { - self.write_const_expr(constant.init)?; + self.write_global_const_expr(constant.init)?; } } Expression::ZeroValue(ty) => {