naga::front::glsl

Struct Frontend

source
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

source

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

source

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>

source

fn constructor_single( &mut self, ctx: &mut Context<'_>, ty: Handle<Type>, (value, expr_meta): (Handle<Expression>, Span), meta: Span, ) -> Result<Handle<Expression>, Error>

source

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>

source

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>

source

fn constructor_many( &mut self, ctx: &mut Context<'_>, ty: Handle<Type>, args: Vec<(Handle<Expression>, Span)>, meta: Span, ) -> Result<Handle<Expression>, Error>

source

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>

source

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.

source

pub(crate) fn add_function( &mut self, ctx: Context<'_>, name: String, result: Option<FunctionResult>, meta: Span, )

source

pub(crate) fn add_prototype( &mut self, ctx: Context<'_>, name: String, result: Option<FunctionResult>, meta: Span, )

source

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 GlobalVariables 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 GLSL main function, and then

  • build its return value from whatever values the GLSL main left in the Naga globals representing GLSL output 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

source

fn handle_directive(&mut self, directive: Directive, meta: Span)

source§

impl Frontend

source

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

source

pub(crate) fn lookup_variable( &mut self, ctx: &mut Context<'_>, name: &str, meta: Span, ) -> Result<Option<VariableReference>, Error>

source

pub(crate) fn make_variable_invariant( &mut self, ctx: &mut Context<'_>, name: &str, meta: Span, ) -> Result<(), Error>

source

pub(crate) fn field_selection( &mut self, ctx: &mut Context<'_>, pos: ExprPos, expression: Handle<Expression>, name: &str, meta: Span, ) -> Result<Handle<Expression>, Error>

source

pub(crate) fn add_global_var( &mut self, ctx: &mut Context<'_>, _: VarDeclaration<'_, '_>, ) -> Result<GlobalOrConstant, Error>

source

pub(crate) fn add_local_var( &mut self, ctx: &mut Context<'_>, decl: VarDeclaration<'_, '_>, ) -> Result<Handle<Expression>, Error>

source§

impl Frontend

source

fn reset(&mut self, stage: ShaderStage)

source

pub fn parse( &mut self, options: &Options, source: &str, ) -> Result<Module, ParseErrors>

Parses a shader either outputting a shader Module or a list of Errors.

Multiple calls using the same Frontend and different shaders are supported.

source

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.

Trait Implementations§

source§

impl Debug for Frontend

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Frontend

source§

fn default() -> Frontend

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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>,

source§

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>,

source§

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.