shaderdef¶
Transform Python code into GLSL shaders.
Website: https://github.com/nicholasbishop/shaderdef
Example program¶
#! /usr/bin/env python3
"""This demo shows how to define a very simple shader program with a
vertex shader and a fragment shader.
The vertex shader is defined in the `vert_shader` function. Note that
the inputs and outputs are annotated; this is how shaderdef knows
what types to use in the generated GLSL code.
Inputs and outputs are grouped together using a Python class. For
example, this shader program's vertex attributes are defined in
`VsIn`. It inherits from `AttributeBlock` to mark its members as vertex
attributes. (There's a UniformBlock for declaring uniform inputs.)
Shader outputs are set using the `return` keyword. The return type
should be a class such as `VsOut`; pass the outputs as keyword
arguments.
"""
from shaderdef import (AttributeBlock, FragmentShaderOutputBlock,
ShaderDef, ShaderInterface)
from shaderdef.glsl_types import vec2, vec4
class VsIn(AttributeBlock):
position = vec2()
class VsOut(ShaderInterface):
gl_position = vec4()
class FsOut(FragmentShaderOutputBlock):
color = vec4()
def vert_shader(attr: VsIn) -> VsOut:
return VsOut(gl_position=vec4(-attr.position.x, attr.position.y, 1.0, 1.0))
def frag_shader() -> FsOut:
return FsOut(color=vec4(1.0, 0.0, 0.0, 1.0))
def print_shaders():
sdef = ShaderDef(vert_shader=vert_shader, frag_shader=frag_shader)
sdef.translate()
print('\nvertex shader:')
print('--------------')
print(sdef.vert_shader)
print('\nfragment shader:')
print('----------------')
print(sdef.frag_shader)
def main():
"""
>>> print_shaders() # doctest: +NORMALIZE_WHITESPACE
vertex shader:
--------------
#version 330 core
layout(location=0) in vec2 position;
void main() {
gl_Position = vec4(-attr.position.x, attr.position.y, 1.0, 1.0);
}
fragment shader:
----------------
#version 330 core
layout(location=0) out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}
"""
print_shaders()
if __name__ == '__main__':
main()
shaderdef.shader module¶
-
class
shaderdef.shader.
ShaderDef
(vert_shader, frag_shader, geom_shader=None)[source]¶ Bases:
object
-
add_function
(function)[source]¶ Add a utility function to the shader program.
Each utility function is currently emitted in all shader stages regardless of which stage or stages the function is actually used in.
-
frag_shader
¶ Get the GLSL code for the fragment shader.
-
geom_shader
¶ Get the GLSL code for the geometry shader.
-
vert_shader
¶ Get the GLSL code for the vertex shader.
-
shaderdef.glsl_var module¶
-
class
shaderdef.glsl_var.
GlslVar
(name, gtype, interpolation=None)[source]¶ Bases:
object
Represent a GLSL variable declaration (or struct member).
-
gtype
= Attribute(name='gtype', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)¶
-
interpolation
= Attribute(name='interpolation', default=None, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)¶
-
name
= Attribute(name='name', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)¶
-
shaderdef.interface module¶
-
class
shaderdef.interface.
GlGsIn
(**kwargs)[source]¶ Bases:
shaderdef.interface.ShaderInterface
-
gl_position
¶
-
shaderdef.glsl_funcs module¶
shaderdef.glsl_types module¶
-
class
shaderdef.glsl_types.
ArraySpec
(element_type, length)[source]¶ Bases:
object
Represents an array declaration.
This type isn’t currently intended to be used by client code directly, it’s just a convenient form for internal use.
-
element_type
= Attribute(name='element_type', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)¶
-
classmethod
from_ast_node
(node)[source]¶ Create a GlslArray from an AST node if possible.
If the node cannot be converted then None is returned.
-
length
= Attribute(name='length', default=NOTHING, validator=None, repr=True, cmp=True, hash=True, init=True, convert=None)¶
-