Skip to content

Commit

Permalink
[naga wgsl] Implement local const declarations (#6156)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagudev authored Aug 30, 2024
1 parent 4454cbf commit 34bb9e4
Show file tree
Hide file tree
Showing 26 changed files with 769 additions and 127 deletions.
3 changes: 2 additions & 1 deletion naga/src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub use features::Features;

use crate::{
back::{self, Baked},
proc::{self, NameKey},
proc::{self, ExpressionKindTracker, NameKey},
valid, Handle, ShaderStage, TypeInner,
};
use features::FeaturesManager;
Expand Down Expand Up @@ -1584,6 +1584,7 @@ impl<'a, W: Write> Writer<'a, W> {
info,
expressions: &func.expressions,
named_expressions: &func.named_expressions,
expr_kind_tracker: ExpressionKindTracker::from_arena(&func.expressions),
};

self.named_expressions.clear();
Expand Down
4 changes: 3 additions & 1 deletion naga/src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
};
use crate::{
back::{self, Baked},
proc::{self, NameKey},
proc::{self, ExpressionKindTracker, NameKey},
valid, Handle, Module, Scalar, ScalarKind, ShaderStage, TypeInner,
};
use std::{fmt, mem};
Expand Down Expand Up @@ -346,6 +346,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
info,
expressions: &function.expressions,
named_expressions: &function.named_expressions,
expr_kind_tracker: ExpressionKindTracker::from_arena(&function.expressions),
};
let name = self.names[&NameKey::Function(handle)].clone();

Expand Down Expand Up @@ -386,6 +387,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
info,
expressions: &ep.function.expressions,
named_expressions: &ep.function.named_expressions,
expr_kind_tracker: ExpressionKindTracker::from_arena(&ep.function.expressions),
};

self.write_wrapped_functions(module, &ctx)?;
Expand Down
4 changes: 4 additions & 0 deletions naga/src/back/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Backend functions that export shader [`Module`](super::Module)s into binary and
*/
#![allow(dead_code)] // can be dead if none of the enabled backends need it

use crate::proc::ExpressionKindTracker;

#[cfg(dot_out)]
pub mod dot;
#[cfg(glsl_out)]
Expand Down Expand Up @@ -118,6 +120,8 @@ pub struct FunctionCtx<'a> {
pub expressions: &'a crate::Arena<crate::Expression>,
/// Map of expressions that have associated variable names
pub named_expressions: &'a crate::NamedExpressions,
/// For constness checks
pub expr_kind_tracker: ExpressionKindTracker,
}

impl FunctionCtx<'_> {
Expand Down
1 change: 1 addition & 0 deletions naga/src/back/pipeline_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ fn process_function(
&mut local_expression_kind_tracker,
&mut emitter,
&mut block,
false,
);

for (old_h, mut expr, span) in expressions.drain() {
Expand Down
12 changes: 10 additions & 2 deletions naga/src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Error;
use crate::{
back::{self, Baked},
proc::{self, NameKey},
proc::{self, ExpressionKindTracker, NameKey},
valid, Handle, Module, ShaderStage, TypeInner,
};
use std::fmt::Write;
Expand Down Expand Up @@ -166,6 +166,7 @@ impl<W: Write> Writer<W> {
info: fun_info,
expressions: &function.expressions,
named_expressions: &function.named_expressions,
expr_kind_tracker: ExpressionKindTracker::from_arena(&function.expressions),
};

// Write the function
Expand Down Expand Up @@ -193,6 +194,7 @@ impl<W: Write> Writer<W> {
info: info.get_entry_point(index),
expressions: &ep.function.expressions,
named_expressions: &ep.function.named_expressions,
expr_kind_tracker: ExpressionKindTracker::from_arena(&ep.function.expressions),
};
self.write_function(module, &ep.function, &func_ctx)?;

Expand Down Expand Up @@ -1115,8 +1117,14 @@ impl<W: Write> Writer<W> {
func_ctx: &back::FunctionCtx,
name: &str,
) -> BackendResult {
// Some functions are marked as const, but are not yet implemented as constant expression
let quantifier = if func_ctx.expr_kind_tracker.is_impl_const(handle) {
"const"
} else {
"let"
};
// Write variable name
write!(self.out, "let {name}")?;
write!(self.out, "{quantifier} {name}")?;
if self.flags.contains(WriterFlags::EXPLICIT_TYPES) {
write!(self.out, ": ")?;
let ty = &func_ctx.info[handle].ty;
Expand Down
Loading

0 comments on commit 34bb9e4

Please sign in to comment.