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<W> Writer<'_, W>
impl<W> Writer<'_, W>
sourcepub(super) fn collect_required_features(&mut self) -> Result<(), Error>
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
sourcefn scalar_required_features(&mut self, scalar: Scalar)
fn scalar_required_features(&mut self, scalar: Scalar)
Helper method that checks the Features
needed by a scalar
fn varying_required_features( &mut self, binding: Option<&Binding>, ty: Handle<Type>, )
source§impl<'a, W: Write> Writer<'a, W>
impl<'a, W: Write> Writer<'a, W>
sourcepub fn new(
out: W,
module: &'a Module,
info: &'a ModuleInfo,
options: &'a Options,
pipeline_options: &'a PipelineOptions,
policies: BoundsCheckPolicies,
) -> Result<Self, Error>
pub fn new( out: W, module: &'a Module, info: &'a ModuleInfo, options: &'a Options, pipeline_options: &'a PipelineOptions, policies: BoundsCheckPolicies, ) -> Result<Self, Error>
sourcepub fn write(&mut self) -> Result<ReflectionInfo, Error>
pub fn write(&mut self) -> Result<ReflectionInfo, Error>
fn write_array_size( &mut self, base: Handle<Type>, size: ArraySize, ) -> Result<(), Error>
sourcefn write_image_type(
&mut self,
dim: ImageDimension,
arrayed: bool,
class: ImageClass,
) -> Result<(), Error>
fn write_image_type( &mut self, dim: ImageDimension, arrayed: bool, class: ImageClass, ) -> Result<(), Error>
sourcefn write_global(
&mut self,
handle: Handle<GlobalVariable>,
global: &GlobalVariable,
) -> Result<(), Error>
fn write_global( &mut self, handle: Handle<GlobalVariable>, global: &GlobalVariable, ) -> Result<(), Error>
fn write_simple_global( &mut self, handle: Handle<GlobalVariable>, global: &GlobalVariable, ) -> Result<(), Error>
sourcefn write_interface_block(
&mut self,
handle: Handle<GlobalVariable>,
global: &GlobalVariable,
) -> Result<(), Error>
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.
sourcefn update_expressions_to_bake(&mut self, func: &Function, info: &FunctionInfo)
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
sourcefn get_global_name(
&self,
handle: Handle<GlobalVariable>,
global: &GlobalVariable,
) -> String
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
whereX
is the group andY
is the binding
sourcefn write_global_name(
&mut self,
handle: Handle<GlobalVariable>,
global: &GlobalVariable,
) -> Result<(), Error>
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
sourcefn write_varying(
&mut self,
binding: Option<&Binding>,
ty: Handle<Type>,
output: bool,
) -> Result<(), Error>
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.
sourcefn write_function(
&mut self,
ty: FunctionType,
func: &Function,
info: &FunctionInfo,
) -> Result<(), Error>
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
fn write_workgroup_variables_initialization( &mut self, ctx: &FunctionCtx<'_>, ) -> Result<(), Error>
sourcefn write_slice<T, F: FnMut(&mut Self, u32, &T) -> Result<(), Error>>(
&mut self,
data: &[T],
f: F,
) -> Result<(), Error>
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
,
sourcefn write_global_constant(
&mut self,
handle: Handle<Constant>,
) -> Result<(), Error>
fn write_global_constant( &mut self, handle: Handle<Constant>, ) -> Result<(), Error>
Helper method used to write global constants
sourcefn write_dot_product(
&mut self,
arg: Handle<Expression>,
arg1: Handle<Expression>,
size: usize,
ctx: &FunctionCtx<'_>,
) -> Result<(), Error>
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
sourcefn write_struct_body(
&mut self,
handle: Handle<Type>,
members: &[StructMember],
) -> Result<(), Error>
fn write_struct_body( &mut self, handle: Handle<Type>, members: &[StructMember], ) -> Result<(), Error>
sourcefn write_stmt(
&mut self,
sta: &Statement,
ctx: &FunctionCtx<'_>,
level: Level,
) -> Result<(), Error>
fn write_stmt( &mut self, sta: &Statement, ctx: &FunctionCtx<'_>, level: Level, ) -> Result<(), Error>
sourcefn write_const_expr(&mut self, expr: Handle<Expression>) -> Result<(), Error>
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
sourcefn write_possibly_const_expr<'w, I, E>(
&'w mut self,
expr: Handle<Expression>,
expressions: &Arena<Expression>,
info: I,
write_expression: E,
) -> Result<(), Error>where
I: Fn(Handle<Expression>) -> &'w TypeResolution,
E: Fn(&mut Self, Handle<Expression>) -> Result<(), Error>,
fn write_possibly_const_expr<'w, I, E>(
&'w mut self,
expr: Handle<Expression>,
expressions: &Arena<Expression>,
info: I,
write_expression: E,
) -> Result<(), Error>where
I: Fn(Handle<Expression>) -> &'w TypeResolution,
E: Fn(&mut Self, Handle<Expression>) -> 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
sourcefn write_expr(
&mut self,
expr: Handle<Expression>,
ctx: &FunctionCtx<'_>,
) -> Result<(), Error>
fn write_expr( &mut self, expr: Handle<Expression>, ctx: &FunctionCtx<'_>, ) -> Result<(), Error>
sourcefn write_clamped_lod(
&mut self,
ctx: &FunctionCtx<'_>,
expr: Handle<Expression>,
image: Handle<Expression>,
level_expr: Handle<Expression>,
) -> Result<(), Error>
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
fn get_coordinate_vector_size(&self, dim: ImageDimension, arrayed: bool) -> u8
sourcefn write_texture_coord(
&mut self,
ctx: &FunctionCtx<'_>,
vector_size: u8,
coordinate: Handle<Expression>,
array_index: Option<Handle<Expression>>,
tex_1d_hack: bool,
) -> Result<(), Error>
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
sourcefn write_image_store(
&mut self,
ctx: &FunctionCtx<'_>,
image: Handle<Expression>,
coordinate: Handle<Expression>,
array_index: Option<Handle<Expression>>,
value: Handle<Expression>,
) -> Result<(), Error>
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
sourcefn 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>
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.
fn write_named_expr( &mut self, handle: Handle<Expression>, name: String, named: Handle<Expression>, ctx: &FunctionCtx<'_>, ) -> Result<(), Error>
sourcefn write_zero_init_value(&mut self, ty: Handle<Type>) -> Result<(), Error>
fn write_zero_init_value(&mut self, ty: Handle<Type>) -> Result<(), Error>
Helper function that write string with default zero initialization for supported types
sourcefn write_zero_init_scalar(&mut self, kind: ScalarKind) -> Result<(), Error>
fn write_zero_init_scalar(&mut self, kind: ScalarKind) -> Result<(), Error>
Helper function that write string with zero initialization for scalar
sourcefn write_barrier(&mut self, flags: Barrier, level: Level) -> Result<(), Error>
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*()
sourcefn write_storage_access(
&mut self,
storage_access: StorageAccess,
) -> Result<(), Error>
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
sourcefn collect_reflection_info(&mut self) -> Result<ReflectionInfo, Error>
fn collect_reflection_info(&mut self) -> Result<ReflectionInfo, Error>
Helper method used to produce the reflection info that’s returned to the user