Skip to content

Commit

Permalink
Write locals as const
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
  • Loading branch information
sagudev committed Feb 25, 2025
1 parent 2c8a3fc commit 94544a7
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions naga/src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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<crate::Expression>) -> BackendResult {
fn write_global_const_expr(&mut self, expr: Handle<crate::Expression>) -> 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<crate::Expression>,
arena: &crate::Arena<crate::Expression>,
) -> 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),
)
}

Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 94544a7

Please sign in to comment.