Snippet¶
-
class
glumpy.gloo.
Snippet
(code=None, default=None, *args, **kwargs)¶ Bases:
object
A snippet is a piece of GLSL code that can be injected into an another GLSL code. It provides the necessary machinery to take care of name collisions, external variables and snippet composition (call, +, -, /, *).
Parameters: - code (string) – Shader code
- default (string) – Default function to be called if none is specified.
- args (list) – Arguments
- kwargs (dict) – Keyword arguments
A snippet can declare uniforms, const, attributes and varying using random names. However, these names will be later mangled such as to avoid name collisions with other snippets and/or main code. This means that any snippet variable must be accessed through the snippet (snippet[“variable”]) to be able to change its value within the main program.
Snippet can be composed together through call:
A = Snippet(code="...") B = Snippet(code="...") C = A(B("P"))
Warning
Calling a snippet does not create a new snippet but instead links it the called snippet to the calling one. In the example above, the
A._call
is nowB
.and arithmetic composition:
A = Snippet(code="...") B = Snippet(code="...") C = A("P") + B("P")
-
args
¶ Call arguments
-
attach
(program)¶ Attach this snippet to a program
Note
Attachment is recursive and will attach all the snippets composing self.
-
call
¶ Computes and returns the GLSL code that correspond to the call
-
code
¶ Mangled code
-
copy
(deep=False)¶ Shallow or deep copy of the snippet
-
dependencies
¶ Compute all snippet dependencies.
Example:
A,B,C,D = Snippet(...), Snippet(...), Snippet(...), Snippet(...) AB = A(B) CD = C(D) S = AB+CD S.dependencies # [A,B,C,D]
-
detach
(program)¶ Detach this snippet from a program
Parameters: program (Program) – Program to detach this snippet from Note
Detachment is recursive and will detach all the snippets composing self.
-
globals
¶ Global string symbols generated from all the codes composing this snippet, taking into account symbols from arguments (call) and next (operators).
-
is_attached
¶ Whether snippet is attached to one or several programs.
-
last
¶ Get last snippet in the arithmetic chain
Example:
A,B,C = Snippet(...), Snippet(...), Snippet(...) D = A(B) + C D.last # C
-
locals
¶ Local table of symbols defined from the code of this snippet only, not taking into account symbols from arguments (call) or next (operators).
-
lookup
(name, deepsearch=True)¶ Search for a specific symbol.
Parameters: - name (str) – Name to be search in symbols
- deepsearch (bool) – Whether to restrict search to self (False) or to search into all snippets (True)
-
mangled_call
(function=None, arguments=None, override=False)¶ Compute tGLSL code that corresponds to the actual call of the snippet
Parameters: - function (string) – Snippet’s function to be called
- arguments (list) – Arguments to give to the function call
- override (bool) – Whether to override python arguments with shader arguments
-
mangled_code
()¶ Generate mangled code
-
name
¶ Name of the snippet
-
next
¶ Next snippet in the arihmetic chain.
-
objects
¶ Objects composing this snippet only.
Object are uniforms, attributes, consts, varying and functions.
-
process_kwargs
(**kwargs)¶ Process kwargs as given in __init__() or __call__()
-
programs
¶ Currently attached programs
-
snippets
¶ Get all snippets composing this snippet (including self).
Example:
A,B,C = Snippet(...), Snippet(...), Snippet(...) D = A(B) + C D.snippets # [A,B,C]
-
symbols
¶ Symbol table defined as a list of (name, mangled_name).