pub struct Frontend {
meta: ShaderMetadata,
lookup_function: FastHashMap<String, FunctionDeclaration>,
lookup_type: FastHashMap<String, Handle<Type>>,
global_variables: Vec<(String, GlobalLookup)>,
entry_args: Vec<EntryArg>,
layouter: Layouter,
errors: Vec<Error>,
}
Expand description
The Frontend
is the central structure of the GLSL frontend.
To instantiate a new Frontend
the Default
trait is used, so a
call to the associated function Frontend::default
will
return a new Frontend
instance.
To parse a shader simply call the parse
method with a
Options
struct and a &str
holding the glsl code.
The Frontend
also provides the metadata
to get some
further information about the previously parsed shader, like version and
extensions used (see the documentation for
ShaderMetadata
to see all the returned information)
§Example usage
use naga::ShaderStage;
use naga::front::glsl::{Frontend, Options};
let glsl = r#"
#version 450 core
void main() {}
"#;
let mut frontend = Frontend::default();
let options = Options::from(ShaderStage::Vertex);
frontend.parse(&options, glsl);
§Reusability
If there’s a need to parse more than one shader reusing the same Frontend
instance may be beneficial since internal allocations will be reused.
Calling the parse
method multiple times will reset the
Frontend
so no extra care is needed when reusing.
Fields§
§meta: ShaderMetadata
§lookup_function: FastHashMap<String, FunctionDeclaration>
§lookup_type: FastHashMap<String, Handle<Type>>
§global_variables: Vec<(String, GlobalLookup)>
§entry_args: Vec<EntryArg>
§layouter: Layouter
§errors: Vec<Error>
Implementations§
source§impl Frontend
impl Frontend
sourcefn coordinate_components(
&mut self,
ctx: &mut Context<'_>,
image: Handle<Expression>,
coord: Handle<Expression>,
extra: Option<Handle<Expression>>,
meta: Span,
) -> Result<CoordComponents, Error>
fn coordinate_components( &mut self, ctx: &mut Context<'_>, image: Handle<Expression>, coord: Handle<Expression>, extra: Option<Handle<Expression>>, meta: Span, ) -> Result<CoordComponents, Error>
Helper function for texture calls, splits the vector argument into it’s components
source§impl Frontend
impl Frontend
pub(crate) fn function_or_constructor_call( &mut self, ctx: &mut Context<'_>, stmt: &StmtContext, fc: FunctionCallKind, raw_args: &[Handle<HirExpr>], meta: Span, ) -> Result<Option<Handle<Expression>>, Error>
fn constructor_single( &mut self, ctx: &mut Context<'_>, ty: Handle<Type>, (value, expr_meta): (Handle<Expression>, Span), meta: Span, ) -> Result<Handle<Expression>, Error>
fn matrix_one_arg( &mut self, ctx: &mut Context<'_>, ty: Handle<Type>, columns: VectorSize, rows: VectorSize, element_scalar: Scalar, (value, expr_meta): (Handle<Expression>, Span), meta: Span, ) -> Result<Handle<Expression>, Error>
fn vector_constructor( &mut self, ctx: &mut Context<'_>, ty: Handle<Type>, size: VectorSize, scalar: Scalar, args: &[(Handle<Expression>, Span)], meta: Span, ) -> Result<Handle<Expression>, Error>
fn constructor_many( &mut self, ctx: &mut Context<'_>, ty: Handle<Type>, args: Vec<(Handle<Expression>, Span)>, meta: Span, ) -> Result<Handle<Expression>, Error>
fn function_call( &mut self, ctx: &mut Context<'_>, stmt: &StmtContext, name: String, args: Vec<(Handle<Expression>, Span)>, raw_args: &[Handle<HirExpr>], meta: Span, ) -> Result<Option<Handle<Expression>>, Error>
sourcefn process_lhs_argument(
&mut self,
ctx: &mut Context<'_>,
meta: Span,
parameter_ty: Handle<Type>,
parameter_info: &ParameterInfo,
original: Handle<Expression>,
call_argument: &(Handle<Expression>, Span),
proxy_writes: &mut Vec<ProxyWrite>,
arguments: &mut Vec<Handle<Expression>>,
) -> Result<(), Error>
fn process_lhs_argument( &mut self, ctx: &mut Context<'_>, meta: Span, parameter_ty: Handle<Type>, parameter_info: &ParameterInfo, original: Handle<Expression>, call_argument: &(Handle<Expression>, Span), proxy_writes: &mut Vec<ProxyWrite>, arguments: &mut Vec<Handle<Expression>>, ) -> Result<(), Error>
Processes a function call argument that appears in place of an output parameter.
pub(crate) fn add_function( &mut self, ctx: Context<'_>, name: String, result: Option<FunctionResult>, meta: Span, )
pub(crate) fn add_prototype( &mut self, ctx: Context<'_>, name: String, result: Option<FunctionResult>, meta: Span, )
sourcepub(crate) fn add_entry_point(
&mut self,
function: Handle<Function>,
ctx: Context<'_>,
) -> Result<(), Error>
pub(crate) fn add_entry_point( &mut self, function: Handle<Function>, ctx: Context<'_>, ) -> Result<(), Error>
Create a Naga EntryPoint
that calls the GLSL main
function.
We compile the GLSL main
function as an ordinary Naga Function
.
This function synthesizes a Naga EntryPoint
to call that.
Each GLSL input and output variable (including builtins) becomes a Naga
GlobalVariable
s in the Private
address space, which main
can
access in the usual way.
The EntryPoint
we synthesize here has an argument for each GLSL input
variable, and returns a struct with a member for each GLSL output
variable. The entry point contains code to:
-
copy its arguments into the Naga globals representing the GLSL input variables,
-
call the Naga
Function
representing the GLSLmain
function, and then -
build its return value from whatever values the GLSL
main
left in the Naga globals representing GLSLoutput
variables.
Upon entry, ctx.body
should contain code, accumulated by prior calls
to ParsingContext::parse_external_declaration
, to initialize
private global variables as needed. This code gets spliced into the
entry point before the call to main
.
source§impl Frontend
impl Frontend
sourcefn add_builtin(
&mut self,
ctx: &mut Context<'_>,
name: &str,
data: BuiltInData,
meta: Span,
) -> Result<Option<VariableReference>, Error>
fn add_builtin( &mut self, ctx: &mut Context<'_>, name: &str, data: BuiltInData, meta: Span, ) -> Result<Option<VariableReference>, Error>
Adds a builtin and returns a variable reference to it
pub(crate) fn lookup_variable( &mut self, ctx: &mut Context<'_>, name: &str, meta: Span, ) -> Result<Option<VariableReference>, Error>
pub(crate) fn make_variable_invariant( &mut self, ctx: &mut Context<'_>, name: &str, meta: Span, ) -> Result<(), Error>
pub(crate) fn field_selection( &mut self, ctx: &mut Context<'_>, pos: ExprPos, expression: Handle<Expression>, name: &str, meta: Span, ) -> Result<Handle<Expression>, Error>
pub(crate) fn add_global_var( &mut self, ctx: &mut Context<'_>, _: VarDeclaration<'_, '_>, ) -> Result<GlobalOrConstant, Error>
pub(crate) fn add_local_var( &mut self, ctx: &mut Context<'_>, decl: VarDeclaration<'_, '_>, ) -> Result<Handle<Expression>, Error>
source§impl Frontend
impl Frontend
fn reset(&mut self, stage: ShaderStage)
sourcepub const fn metadata(&self) -> &ShaderMetadata
pub const fn metadata(&self) -> &ShaderMetadata
Returns additional information about the parsed shader which might not
be stored in the Module
, see the documentation for
ShaderMetadata
for more information about the returned data.
§Notes
Following an unsuccessful parsing the state of the returned information is undefined, it might contain only partial information about the current shader, the previous shader or both.