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.

get_uniforms()[source]
translate()[source]
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).

declare()[source]
declare_attribute(location=None)[source]
declare_output(location=None)[source]
declare_uniform()[source]
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.glsl_var.location_str(location)[source]

shaderdef.interface module

class shaderdef.interface.AttributeBlock(**kwargs)[source]

Bases: shaderdef.interface.ShaderInterface

classmethod declare_input_block(instance_name=None, array=None)[source]
class shaderdef.interface.FragmentShaderOutputBlock(**kwargs)[source]

Bases: shaderdef.interface.ShaderInterface

classmethod declare_output_block(array=None)[source]
class shaderdef.interface.GlGsIn(**kwargs)[source]

Bases: shaderdef.interface.ShaderInterface

gl_position
class shaderdef.interface.ShaderInterface(**kwargs)[source]

Bases: object

classmethod block_name()[source]
classmethod declare_input_block(instance_name, array=None)[source]
classmethod declare_output_block(array=None)[source]
classmethod get_vars()[source]
classmethod instance_name()[source]
class shaderdef.interface.UniformBlock(**kwargs)[source]

Bases: shaderdef.interface.ShaderInterface

classmethod declare_input_block(instance_name, array=None)[source]
shaderdef.interface.snake_case(string)[source]

shaderdef.glsl_funcs module

shaderdef.glsl_funcs.end_primitive()[source]
shaderdef.glsl_funcs.exp2(var)[source]
shaderdef.glsl_funcs.geom_shader_meta(input_primitive, output_primitive, max_vertices)[source]
shaderdef.glsl_funcs.length(vec_type)[source]
shaderdef.glsl_funcs.mod(num1, num2)[source]

shaderdef.glsl_types module

shaderdef.glsl_types.Array1

alias of GlslArray

shaderdef.glsl_types.Array10

alias of GlslArray

shaderdef.glsl_types.Array11

alias of GlslArray

shaderdef.glsl_types.Array12

alias of GlslArray

shaderdef.glsl_types.Array13

alias of GlslArray

shaderdef.glsl_types.Array14

alias of GlslArray

shaderdef.glsl_types.Array15

alias of GlslArray

shaderdef.glsl_types.Array16

alias of GlslArray

shaderdef.glsl_types.Array2

alias of GlslArray

shaderdef.glsl_types.Array3

alias of GlslArray

shaderdef.glsl_types.Array4

alias of GlslArray

shaderdef.glsl_types.Array5

alias of GlslArray

shaderdef.glsl_types.Array6

alias of GlslArray

shaderdef.glsl_types.Array7

alias of GlslArray

shaderdef.glsl_types.Array8

alias of GlslArray

shaderdef.glsl_types.Array9

alias of GlslArray

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)
class shaderdef.glsl_types.GlslArray(gtype)[source]

Bases: typing.Generic

class shaderdef.glsl_types.GlslType(*args, **kwargs)[source]

Bases: typing.SupportsAbs, typing.SupportsInt, typing.SupportsFloat

shaderdef.glsl_types.mat2

alias of GlslType

shaderdef.glsl_types.mat3

alias of GlslType

shaderdef.glsl_types.mat4

alias of GlslType

class shaderdef.glsl_types.noperspective[source]

Bases: object

shaderdef.glsl_types.vec2

alias of GlslType

shaderdef.glsl_types.vec3

alias of GlslType

shaderdef.glsl_types.vec4

alias of GlslType

class shaderdef.glsl_types.void[source]

Bases: object

Indices and tables