Struct naga::proc::constant_evaluator::ConstantEvaluator

source ·
pub struct ConstantEvaluator<'a> {
    behavior: Behavior<'a>,
    types: &'a mut UniqueArena<Type>,
    constants: &'a Arena<Constant>,
    overrides: &'a Arena<Override>,
    expressions: &'a mut Arena<Expression>,
    expression_kind_tracker: &'a mut ExpressionKindTracker,
}
Expand description

A context for evaluating constant expressions.

A ConstantEvaluator points at an expression arena to which it can append newly evaluated expressions: you pass try_eval_and_append whatever kind of Naga Expression you like, and if its value can be computed at compile time, try_eval_and_append appends an expression representing the computed value - a tree of Literal, Compose, ZeroValue, and Swizzle expressions - to the arena. See the try_eval_and_append method for details.

A ConstantEvaluator also holds whatever information we need to carry out that evaluation: types, other constants, and so on.

Fields§

§behavior: Behavior<'a>

Which language’s evaluation rules we should follow.

§types: &'a mut UniqueArena<Type>

The module’s type arena.

Because expressions like Splat contain type handles, we need to be able to add new types to produce those expressions.

§constants: &'a Arena<Constant>

The module’s constant arena.

§overrides: &'a Arena<Override>

The module’s override arena.

§expressions: &'a mut Arena<Expression>

The arena to which we are contributing expressions.

§expression_kind_tracker: &'a mut ExpressionKindTracker

Tracks the constness of expressions residing in Self::expressions

Implementations§

source§

impl<'a> ConstantEvaluator<'a>

source

pub fn for_wgsl_module( module: &'a mut Module, global_expression_kind_tracker: &'a mut ExpressionKindTracker, in_override_ctx: bool, ) -> Self

Return a ConstantEvaluator that will add expressions to module’s constant expression arena.

Report errors according to WGSL’s rules for constant evaluation.

source

pub fn for_glsl_module( module: &'a mut Module, global_expression_kind_tracker: &'a mut ExpressionKindTracker, ) -> Self

Return a ConstantEvaluator that will add expressions to module’s constant expression arena.

Report errors according to GLSL’s rules for constant evaluation.

source

fn for_module( behavior: Behavior<'a>, module: &'a mut Module, global_expression_kind_tracker: &'a mut ExpressionKindTracker, ) -> Self

source

pub fn for_wgsl_function( module: &'a mut Module, expressions: &'a mut Arena<Expression>, local_expression_kind_tracker: &'a mut ExpressionKindTracker, emitter: &'a mut Emitter, block: &'a mut Block, is_const: bool, ) -> Self

Return a ConstantEvaluator that will add expressions to function’s expression arena.

Report errors according to WGSL’s rules for constant evaluation.

source

pub fn for_glsl_function( module: &'a mut Module, expressions: &'a mut Arena<Expression>, local_expression_kind_tracker: &'a mut ExpressionKindTracker, emitter: &'a mut Emitter, block: &'a mut Block, ) -> Self

Return a ConstantEvaluator that will add expressions to function’s expression arena.

Report errors according to GLSL’s rules for constant evaluation.

source

pub fn to_ctx(&self) -> GlobalCtx<'_>

source

fn check(&self, expr: Handle<Expression>) -> Result<(), ConstantEvaluatorError>

source

fn check_and_get( &mut self, expr: Handle<Expression>, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

pub fn try_eval_and_append( &mut self, expr: Expression, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Try to evaluate expr at compile time.

The expr argument can be any sort of Naga Expression you like. If we can determine its value at compile time, we append an expression representing its value - a tree of Literal, Compose, ZeroValue, and Swizzle expressions - to the expression arena self contributes to.

If expr’s value cannot be determined at compile time, and self is contributing to some function’s expression arena, then append expr to that arena unchanged (and thus unevaluated). Otherwise, self must be contributing to the module’s constant expression arena; since expr’s value is not a constant, return an error.

We only consider expr itself, without recursing into its operands. Its operands must all have been produced by prior calls to try_eval_and_append, to ensure that they have already been reduced to an evaluated form if possible.

source

const fn is_global_arena(&self) -> bool

Is the Self::expressions arena the global module expression arena?

source

const fn function_local_data(&self) -> Option<&FunctionLocalData<'a>>

source

fn try_eval_and_append_impl( &mut self, expr: &Expression, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn splat( &mut self, value: Handle<Expression>, size: VectorSize, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Splat value to size, without using Splat expressions.

This constructs Compose or ZeroValue expressions to build a vector with the given size whose components are all value.

Use span as the span of the inserted expressions and resulting types.

source

fn swizzle( &mut self, size: VectorSize, span: Span, src_constant: Handle<Expression>, pattern: [SwizzleComponent; 4], ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn math( &mut self, arg: Handle<Expression>, arg1: Option<Handle<Expression>>, arg2: Option<Handle<Expression>>, arg3: Option<Handle<Expression>>, fun: MathFunction, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn array_length( &mut self, array: Handle<Expression>, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn access( &mut self, base: Handle<Expression>, index: usize, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn constant_index( &self, expr: Handle<Expression>, ) -> Result<usize, ConstantEvaluatorError>

source

fn eval_zero_value_and_splat( &mut self, expr: Handle<Expression>, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Lower ZeroValue and Splat expressions to Literal and Compose expressions.

source

fn eval_zero_value( &mut self, expr: Handle<Expression>, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Lower ZeroValue expressions to Literal and Compose expressions.

source

fn eval_zero_value_impl( &mut self, ty: Handle<Type>, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Lower ZeroValue expressions to Literal and Compose expressions.

source

pub fn cast( &mut self, expr: Handle<Expression>, target: Scalar, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Convert the scalar components of expr to target.

Treat span as the location of the resulting expression.

source

pub fn cast_array( &mut self, expr: Handle<Expression>, target: Scalar, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Convert the scalar leaves of expr to target, handling arrays.

expr must be a Compose expression whose type is a scalar, vector, matrix, or nested arrays of such.

This is basically the same as the cast method, except that that should only handle Naga As expressions, which cannot convert arrays.

Treat span as the location of the resulting expression.

source

fn unary_op( &mut self, op: UnaryOperator, expr: Handle<Expression>, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn binary_op( &mut self, op: BinaryOperator, left: Handle<Expression>, right: Handle<Expression>, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn binary_op_vector( &mut self, op: BinaryOperator, size: VectorSize, components: &[(Handle<Expression>, Handle<Expression>)], left_ty: Handle<Type>, span: Span, ) -> Result<Expression, ConstantEvaluatorError>

source

fn copy_from( &mut self, expr: Handle<Expression>, expressions: &Arena<Expression>, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

Deep copy expr from expressions into self.expressions.

Return the root of the new copy.

This is used when we’re evaluating expressions in a function’s expression arena that refer to a constant: we need to copy the constant’s value into the function’s arena so we can operate on it.

source

fn register_evaluated_expr( &mut self, expr: Expression, span: Span, ) -> Result<Handle<Expression>, ConstantEvaluatorError>

source

fn append_expr( &mut self, expr: Expression, span: Span, expr_type: ExpressionKind, ) -> Handle<Expression>

source

fn resolve_type( &self, expr: Handle<Expression>, ) -> Result<TypeResolution, ConstantEvaluatorError>

Trait Implementations§

source§

impl<'a> Debug for ConstantEvaluator<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for ConstantEvaluator<'a>

§

impl<'a> RefUnwindSafe for ConstantEvaluator<'a>

§

impl<'a> Send for ConstantEvaluator<'a>

§

impl<'a> Sync for ConstantEvaluator<'a>

§

impl<'a> Unpin for ConstantEvaluator<'a>

§

impl<'a> !UnwindSafe for ConstantEvaluator<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.