Struct naga::back::glsl::Writer

source ·
pub struct Writer<'a, W> {
Show 17 fields module: &'a Module, info: &'a ModuleInfo, out: W, options: &'a Options, policies: BoundsCheckPolicies, features: FeaturesManager, namer: Namer, names: FastHashMap<NameKey, String>, reflection_names_globals: FastHashMap<Handle<GlobalVariable>, String>, entry_point: &'a EntryPoint, entry_point_idx: EntryPointIndex, block_id: IdGenerator, named_expressions: FastIndexMap<Handle<Expression>, String>, need_bake_expressions: NeedBakeExpressions, continue_ctx: ContinueCtx, multiview: Option<NonZeroU32>, varying: FastHashMap<String, VaryingLocation>,
}
Expand description

Writer responsible for all code generation.

Fields§

§module: &'a Module

The module being written.

§info: &'a ModuleInfo

The module analysis.

§out: W

The output writer.

§options: &'a Options

User defined configuration to be used.

§policies: BoundsCheckPolicies

The bound checking policies to be used

§features: FeaturesManager

Features manager used to store all the needed features and write them.

§namer: Namer§names: FastHashMap<NameKey, String>

A map with all the names needed for writing the module (generated by a Namer).

§reflection_names_globals: FastHashMap<Handle<GlobalVariable>, String>

A map with the names of global variables needed for reflections.

§entry_point: &'a EntryPoint

The selected entry point.

§entry_point_idx: EntryPointIndex

The index of the selected entry point.

§block_id: IdGenerator

A generator for unique block numbers.

§named_expressions: FastIndexMap<Handle<Expression>, String>

Set of expressions that have associated temporary variables.

§need_bake_expressions: NeedBakeExpressions

Set of expressions that need to be baked to avoid unnecessary repetition in output

§continue_ctx: ContinueCtx

Information about nesting of loops and switches.

Used for forwarding continue statements in switches that have been transformed to do {} while(false); loops.

§multiview: Option<NonZeroU32>

How many views to render to, if doing multiview rendering.

§varying: FastHashMap<String, VaryingLocation>

Mapping of varying variables to their location. Needed for reflections.

Implementations§

source§

impl<'a, W> Writer<'a, W>

source

pub(super) fn collect_required_features(&mut self) -> Result<(), Error>

Helper method that searches the module for all the needed Features

§Errors

If the version doesn’t support any of the needed Features a Error::MissingFeatures will be returned

source

fn scalar_required_features(&mut self, scalar: Scalar)

Helper method that checks the Features needed by a scalar

source

fn varying_required_features( &mut self, binding: Option<&Binding>, ty: Handle<Type>, )

source§

impl<'a, W: Write> Writer<'a, W>

source

pub fn new( out: W, module: &'a Module, info: &'a ModuleInfo, options: &'a Options, pipeline_options: &'a PipelineOptions, policies: BoundsCheckPolicies, ) -> Result<Self, Error>

Creates a new Writer instance.

§Errors
  • If the version specified is invalid or supported.
  • If the entry point couldn’t be found in the module.
  • If the version specified doesn’t support some used features.
source

pub fn write(&mut self) -> Result<ReflectionInfo, Error>

Writes the Module as glsl to the output

§Notes

If an error occurs while writing, the output might have been written partially

§Panics

Might panic if the module is invalid

source

fn write_array_size( &mut self, base: Handle<Type>, size: ArraySize, ) -> Result<(), Error>

source

fn write_value_type(&mut self, inner: &TypeInner) -> Result<(), Error>

Helper method used to write value types

§Notes

Adds no trailing or leading whitespace

source

fn write_type(&mut self, ty: Handle<Type>) -> Result<(), Error>

Helper method used to write non image/sampler types

§Notes

Adds no trailing or leading whitespace

source

fn write_image_type( &mut self, dim: ImageDimension, arrayed: bool, class: ImageClass, ) -> Result<(), Error>

Helper method to write a image type

§Notes

Adds no leading or trailing whitespace

source

fn write_global( &mut self, handle: Handle<GlobalVariable>, global: &GlobalVariable, ) -> Result<(), Error>

Helper method used to write non images/sampler globals

§Notes

Adds a newline

§Panics

If the global has type sampler

source

fn write_simple_global( &mut self, handle: Handle<GlobalVariable>, global: &GlobalVariable, ) -> Result<(), Error>

source

fn write_interface_block( &mut self, handle: Handle<GlobalVariable>, global: &GlobalVariable, ) -> Result<(), Error>

Write an interface block for a single Naga global.

Write block_name { members }. Since block_name must be unique between blocks and structs, we add _block_ID where ID is a IdGenerator generated number. Write members in the same way we write a struct’s members.

source

fn update_expressions_to_bake(&mut self, func: &Function, info: &FunctionInfo)

Helper method used to find which expressions of a given function require baking

§Notes

Clears need_bake_expressions set before adding to it

source

fn get_global_name( &self, handle: Handle<GlobalVariable>, global: &GlobalVariable, ) -> String

Helper method used to get a name for a global

Globals have different naming schemes depending on their binding:

  • Globals without bindings use the name from the Namer
  • Globals with resource binding are named _group_X_binding_Y where X is the group and Y is the binding
source

fn write_global_name( &mut self, handle: Handle<GlobalVariable>, global: &GlobalVariable, ) -> Result<(), Error>

Helper method used to write a name for a global without additional heap allocation

source

fn write_varying( &mut self, binding: Option<&Binding>, ty: Handle<Type>, output: bool, ) -> Result<(), Error>

Write a GLSL global that will carry a Naga entry point’s argument or return value.

A Naga entry point’s arguments and return value are rendered in GLSL as variables at global scope with the in and out storage qualifiers. The code we generate for main loads from all the in globals into appropriately named locals. Before it returns, main assigns the components of its return value into all the out globals.

This function writes a declaration for one such GLSL global, representing a value passed into or returned from self.entry_point that has a Location binding. The global’s name is generated based on the location index and the shader stages being connected; see VaryingName. This means we don’t need to know the names of arguments, just their types and bindings.

Emit nothing for entry point arguments or return values with BuiltIn bindings; main will read from or assign to the appropriate GLSL special variable; these are pre-declared. As an exception, we do declare gl_Position or gl_FragCoord with the invariant qualifier if needed.

Use output together with self.entry_point.stage to determine which shader stages are being connected, and choose the in or out storage qualifier.

source

fn write_function( &mut self, ty: FunctionType, func: &Function, info: &FunctionInfo, ) -> Result<(), Error>

Helper method used to write functions (both entry points and regular functions)

§Notes

Adds a newline

source

fn write_workgroup_variables_initialization( &mut self, ctx: &FunctionCtx<'_>, ) -> Result<(), Error>

source

fn write_slice<T, F: FnMut(&mut Self, u32, &T) -> Result<(), Error>>( &mut self, data: &[T], f: F, ) -> Result<(), Error>

Write a list of comma separated T values using a writer function F.

The writer function F receives a mutable reference to self that if needed won’t cause borrow checker issues (using for example a closure with self will cause issues), the second argument is the 0 based index of the element on the list, and the last element is a reference to the element T being written

§Notes
  • Adds no newlines or leading/trailing whitespace
  • The last element won’t have a trailing ,
source

fn write_global_constant( &mut self, handle: Handle<Constant>, ) -> Result<(), Error>

Helper method used to write global constants

source

fn write_dot_product( &mut self, arg: Handle<Expression>, arg1: Handle<Expression>, size: usize, ctx: &FunctionCtx<'_>, ) -> Result<(), Error>

Helper method used to output a dot product as an arithmetic expression

source

fn write_struct_body( &mut self, handle: Handle<Type>, members: &[StructMember], ) -> Result<(), Error>

Helper method used to write structs

§Notes

Ends in a newline

source

fn write_stmt( &mut self, sta: &Statement, ctx: &FunctionCtx<'_>, level: Level, ) -> Result<(), Error>

Helper method used to write statements

§Notes

Always adds a newline

source

fn write_const_expr(&mut self, expr: Handle<Expression>) -> Result<(), Error>

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

source

fn write_possibly_const_expr<'w, I, E>( &'w mut self, expr: Handle<Expression>, expressions: &Arena<Expression>, info: I, write_expression: E, ) -> Result<(), Error>

Write Expression variants that can occur in both runtime and const expressions.

Write expr, a handle to an Expression in the arena expressions, as as GLSL expression. This must be one of the Expression variants that is allowed to occur in constant expressions.

Use write_expression to write subexpressions.

This is the common code for write_expr, which handles arbitrary runtime expressions, and write_const_expr, which only handles const-expressions. Each of those callers passes itself (essentially) as the write_expression callback, so that subexpressions are restricted to the appropriate variants.

§Notes

Adds no newlines or leading/trailing whitespace

source

fn write_expr( &mut self, expr: Handle<Expression>, ctx: &FunctionCtx<'_>, ) -> Result<(), Error>

Helper method to write expressions

§Notes

Doesn’t add any newlines or leading/trailing spaces

source

fn write_clamped_lod( &mut self, ctx: &FunctionCtx<'_>, expr: Handle<Expression>, image: Handle<Expression>, level_expr: Handle<Expression>, ) -> Result<(), Error>

Helper function to write the local holding the clamped lod

source

fn get_coordinate_vector_size(&self, dim: ImageDimension, arrayed: bool) -> u8

source

fn write_texture_coord( &mut self, ctx: &FunctionCtx<'_>, vector_size: u8, coordinate: Handle<Expression>, array_index: Option<Handle<Expression>>, tex_1d_hack: bool, ) -> Result<(), Error>

Helper method to write the coordinate vector for image operations

source

fn write_image_store( &mut self, ctx: &FunctionCtx<'_>, image: Handle<Expression>, coordinate: Handle<Expression>, array_index: Option<Handle<Expression>>, value: Handle<Expression>, ) -> Result<(), Error>

Helper method to write the ImageStore statement

source

fn write_image_load( &mut self, handle: Handle<Expression>, ctx: &FunctionCtx<'_>, image: Handle<Expression>, coordinate: Handle<Expression>, array_index: Option<Handle<Expression>>, sample: Option<Handle<Expression>>, level: Option<Handle<Expression>>, ) -> Result<(), Error>

Helper method for writing an ImageLoad expression.

source

fn write_named_expr( &mut self, handle: Handle<Expression>, name: String, named: Handle<Expression>, ctx: &FunctionCtx<'_>, ) -> Result<(), Error>

source

fn write_zero_init_value(&mut self, ty: Handle<Type>) -> Result<(), Error>

Helper function that write string with default zero initialization for supported types

source

fn write_zero_init_scalar(&mut self, kind: ScalarKind) -> Result<(), Error>

Helper function that write string with zero initialization for scalar

source

fn write_barrier(&mut self, flags: Barrier, level: Level) -> Result<(), Error>

Issue a memory barrier. Please note that to ensure visibility, OpenGL always requires a call to the barrier() function after a memoryBarrier*()

source

fn write_storage_access( &mut self, storage_access: StorageAccess, ) -> Result<(), Error>

Helper function that return the glsl storage access string of StorageAccess

glsl allows adding both readonly and writeonly but this means that they can only be used to query information about the resource which isn’t what we want here so when storage access is both LOAD and STORE add no modifiers

source

fn collect_reflection_info(&mut self) -> Result<ReflectionInfo, Error>

Helper method used to produce the reflection info that’s returned to the user

source

fn collect_push_constant_items( &mut self, ty: Handle<Type>, segments: &mut Vec<String>, layouter: &Layouter, offset: &mut u32, items: &mut Vec<PushConstantItem>, )

Auto Trait Implementations§

§

impl<'a, W> Freeze for Writer<'a, W>
where W: Freeze,

§

impl<'a, W> RefUnwindSafe for Writer<'a, W>
where W: RefUnwindSafe,

§

impl<'a, W> !Send for Writer<'a, W>

§

impl<'a, W> !Sync for Writer<'a, W>

§

impl<'a, W> Unpin for Writer<'a, W>
where W: Unpin,

§

impl<'a, W> UnwindSafe for Writer<'a, W>
where W: UnwindSafe,

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.