OpenGL / GLSL Source Backend

GLSL source backend exports for CrossGL Translator.

OpenGL/GLSL AST Node definitions

class crosstl.backend.GLSL.OpenglAst.BlockNode(statements)[source]

Bases: ASTNode

Node representing a block of statements

class crosstl.backend.GLSL.OpenglAst.ConstantNode(vtype, name, value=None)[source]

Bases: ASTNode

Node representing a constant declaration

class crosstl.backend.GLSL.OpenglAst.LayoutNode(qualifiers=None, declaration=None)[source]

Bases: ASTNode

Node representing layout qualifiers

class crosstl.backend.GLSL.OpenglAst.NumberNode(value)[source]

Bases: ASTNode

Node representing a numeric literal

class crosstl.backend.GLSL.OpenglAst.UniformNode(vtype, name, value=None)[source]

Bases: ASTNode

Node representing a uniform variable

Lexer for importing GLSL source into CrossGL Translator.

class crosstl.backend.GLSL.OpenglLexer.GLSLLexer(code, preprocess=True, include_paths=None, defines=None, strict_preprocessor=True, max_expansion_depth=64, file_path=None)[source]

Bases: object

Tokenize GLSL source for the OpenGL parser.

classmethod from_file(filepath, preprocess=True, include_paths=None, defines=None, strict_preprocessor=True, max_expansion_depth=64)[source]

Create a lexer instance from a GLSL source file.

Return type:

GLSLLexer

token_generator()[source]

Yield GLSL tokens while skipping whitespace and comments.

Return type:

Iterator[Tuple[str, str]]

tokenize()[source]

Return the full token stream as (token_type, text) tuples.

Return type:

List[Tuple[str, str]]

Parser for GLSL source AST construction.

class crosstl.backend.GLSL.OpenglParser.GLSLParser(tokens, shader_type='vertex')[source]

Bases: object

Parse GLSL tokens into the OpenGL backend shader AST.

advance()[source]

Advance to the next token, falling back to EOF at the end.

eat(token_type)[source]

Consume the current token when it matches token_type.

parse()[source]

Parse the complete token stream into a shader node.

parse_shader()[source]

Parse top-level GLSL declarations, functions, and layout blocks.

peek(offset=1)[source]

Return a lookahead token without advancing the parser.

skip_newlines()[source]

Advance past newline tokens that separate GLSL declarations.

Reverse code generator that emits CrossGL from GLSL AST nodes.

class crosstl.backend.GLSL.openglCrossglCodegen.GLSLToCrossGLConverter(shader_type='vertex')[source]

Bases: object

Serialize OpenGL backend AST nodes back into CrossGL source.

convert_type(type_name)[source]

Convert a GLSL type to its CrossGL equivalent

Parameters:

type_name – The GLSL type name

Returns:

The equivalent CrossGL type name

Return type:

str

generate(ast)[source]

Generate a complete CrossGL shader from a parsed GLSL AST.

generate_array_access(node)[source]

Generate CrossGL code for an array access expression

Parameters:

node – ArrayAccessNode representing a GLSL array access

Returns:

The CrossGL array access expression

Return type:

str

generate_expression(node)[source]

Render an OpenGL backend expression node as CrossGL syntax.

generate_function(node)[source]

Render one GLSL function node as a CrossGL function block.

generate_statement(node)[source]

Render a GLSL statement node as CrossGL source.

generate_switch_statement(node)[source]

Generate CrossGL code for a switch statement

Parameters:

node – SwitchNode representing a GLSL switch statement

Returns:

The CrossGL switch statement

Return type:

str

generate_variable_declaration(node)[source]

Generate CrossGL code for a variable declaration

Parameters:

node – VariableNode representing a GLSL variable declaration

Returns:

The CrossGL variable declaration

Return type:

str

Preprocessor support for GLSL source imports.

class crosstl.backend.GLSL.preprocessor.GLSLPreprocessor(include_paths=None, defines=None, strict=True, max_expansion_depth=64)[source]

Bases: object

GLSL preprocessor wrapper that preserves #version placement rules.