Move NeedBakeExpressions into backends (#1716)

This commit is contained in:
Dzmitry Malyshau
2022-02-04 14:31:48 -05:00
committed by GitHub
parent f3e61530cf
commit ca58d413b7
4 changed files with 13 additions and 24 deletions

View File

@@ -418,7 +418,7 @@ pub struct Writer<'a, W> {
/// Set of expressions that have associated temporary variables.
named_expressions: crate::NamedExpressions,
/// Set of expressions that need to be baked to avoid unnecessary repetition in output
need_bake_expressions: crate::NeedBakeExpressions,
need_bake_expressions: back::NeedBakeExpressions,
}
impl<'a, W: Write> Writer<'a, W> {
@@ -469,8 +469,8 @@ impl<'a, W: Write> Writer<'a, W> {
entry_point_idx: ep_idx as u16,
block_id: IdGenerator::default(),
named_expressions: crate::NamedExpressions::default(),
need_bake_expressions: crate::NeedBakeExpressions::default(),
named_expressions: Default::default(),
need_bake_expressions: Default::default(),
};
// Find all features required to print this module
@@ -1561,7 +1561,7 @@ impl<'a, W: Write> Writer<'a, W> {
// Also, we use sanitized names! It defense backend from generating variable with name from reserved keywords.
Some(self.namer.call(name))
} else if self.need_bake_expressions.contains(&handle) {
Some(format!("{}{}", super::BAKE_PREFIX, handle.index()))
Some(format!("{}{}", back::BAKE_PREFIX, handle.index()))
} else {
None
};
@@ -1886,7 +1886,7 @@ impl<'a, W: Write> Writer<'a, W> {
} => {
write!(self.out, "{}", level)?;
if let Some(expr) = result {
let name = format!("{}{}", super::BAKE_PREFIX, expr.index());
let name = format!("{}{}", back::BAKE_PREFIX, expr.index());
let result = self.module.functions[function].result.as_ref().unwrap();
self.write_type(result.ty)?;
write!(self.out, " {} = ", name)?;
@@ -1914,7 +1914,7 @@ impl<'a, W: Write> Writer<'a, W> {
result,
} => {
write!(self.out, "{}", level)?;
let res_name = format!("{}{}", super::BAKE_PREFIX, result.index());
let res_name = format!("{}{}", back::BAKE_PREFIX, result.index());
let res_ty = ctx.info[result].ty.inner_with(&self.module.types);
self.write_value_type(res_ty)?;
write!(self.out, " {} = ", res_name)?;
@@ -2478,7 +2478,7 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, "(")?;
self.write_expr(left, ctx)?;
write!(self.out, " {} ", super::binary_operation_str(op))?;
write!(self.out, " {} ", back::binary_operation_str(op))?;
self.write_expr(right, ctx)?;
write!(self.out, ")")?;

View File

@@ -1,6 +1,7 @@
/*!
Backend functions that export shader [`Module`](super::Module)s into binary and text formats.
*/
#![allow(dead_code)] // can be dead if none of the enabled backends need it
#[cfg(feature = "dot-out")]
pub mod dot;
@@ -15,25 +16,21 @@ pub mod spv;
#[cfg(feature = "wgsl-out")]
pub mod wgsl;
#[allow(dead_code)]
const COMPONENTS: &[char] = &['x', 'y', 'z', 'w'];
#[allow(dead_code)]
const INDENT: &str = " ";
#[allow(dead_code)]
const BAKE_PREFIX: &str = "_e";
type NeedBakeExpressions = crate::FastHashSet<crate::Handle<crate::Expression>>;
#[derive(Clone, Copy)]
#[allow(dead_code)]
struct Level(usize);
#[allow(dead_code)]
impl Level {
fn next(&self) -> Self {
Level(self.0 + 1)
}
}
#[allow(dead_code)]
impl std::fmt::Display for Level {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
(0..self.0).try_for_each(|_| formatter.write_str(INDENT))
@@ -43,7 +40,6 @@ impl std::fmt::Display for Level {
/// Stores the current function type (either a regular function or an entry point)
///
/// Also stores data needed to identify it (handle for a regular function or index for an entry point)
#[allow(dead_code)]
enum FunctionType {
/// A regular function and it's handle
Function(crate::Handle<crate::Function>),
@@ -52,7 +48,6 @@ enum FunctionType {
}
/// Helper structure that stores data needed when writing the function
#[allow(dead_code)]
struct FunctionCtx<'a> {
/// The current function being written
ty: FunctionType,
@@ -64,7 +59,6 @@ struct FunctionCtx<'a> {
named_expressions: &'a crate::NamedExpressions,
}
#[allow(dead_code)]
impl<'a> FunctionCtx<'_> {
/// Helper method that generates a [`NameKey`](crate::proc::NameKey) for a local in the current function
fn name_key(&self, local: crate::Handle<crate::LocalVariable>) -> crate::proc::NameKey {
@@ -134,7 +128,6 @@ impl crate::Expression {
/// See the [module-level documentation][emit] for details.
///
/// [emit]: index.html#expression-evaluation-time
#[allow(dead_code)]
fn bake_ref_count(&self) -> usize {
match *self {
// accesses are never cached, only loads are
@@ -156,7 +149,6 @@ impl crate::Expression {
/// Helper function that returns the string corresponding to the [`BinaryOperator`](crate::BinaryOperator)
/// # Notes
/// Used by `glsl-out`, `msl-out`, `wgsl-out`, `hlsl-out`.
#[allow(dead_code)]
fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
use crate::BinaryOperator as Bo;
match op {
@@ -184,7 +176,6 @@ fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
/// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize)
/// # Notes
/// Used by `msl-out`, `wgsl-out`, `hlsl-out`.
#[allow(dead_code)]
fn vector_size_str(size: crate::VectorSize) -> &'static str {
match size {
crate::VectorSize::Bi => "2",
@@ -194,7 +185,6 @@ fn vector_size_str(size: crate::VectorSize) -> &'static str {
}
impl crate::TypeInner {
#[allow(unused)]
fn is_handle(&self) -> bool {
match *self {
crate::TypeInner::Image { .. } | crate::TypeInner::Sampler { .. } => true,

View File

@@ -310,7 +310,7 @@ pub struct Writer<W> {
names: FastHashMap<NameKey, String>,
named_expressions: crate::NamedExpressions,
/// Set of expressions that need to be baked to avoid unnecessary repetition in output
need_bake_expressions: crate::NeedBakeExpressions,
need_bake_expressions: back::NeedBakeExpressions,
namer: proc::Namer,
#[cfg(test)]
put_expression_stack_pointers: FastHashSet<*const ()>,
@@ -526,8 +526,8 @@ impl<W: Write> Writer<W> {
Writer {
out,
names: FastHashMap::default(),
named_expressions: crate::NamedExpressions::default(),
need_bake_expressions: crate::NeedBakeExpressions::default(),
named_expressions: Default::default(),
need_bake_expressions: Default::default(),
namer: proc::Namer::default(),
#[cfg(test)]
put_expression_stack_pointers: Default::default(),

View File

@@ -233,7 +233,6 @@ pub type FastHashSet<K> = rustc_hash::FxHashSet<K>;
/// Map of expressions that have associated variable names
pub(crate) type NamedExpressions = FastHashMap<Handle<Expression>, String>;
pub(crate) type NeedBakeExpressions = FastHashSet<Handle<Expression>>;
/// Early fragment tests.
///