Skip to content
Write modern GPU shaders in Python!
Python JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
codegen
examples
exp
python_shader
tests
.gitignore
LICENSE
README.md
azure-pipelines.yml
setup.cfg
setup.py

README.md

Build Status

python_shader

Write modern GPU shaders in Python! Provides a Python to SpirV compiler, to start with.

Introduction

SpirV is a binary platform independent represenation for GPU shaders. This module makes it easier to write SpirV shaders in Python.

Status

Very much a WIP. The plan is to build out the python2shader() compiler to allow writing any shader in Python. Currently, it does not even implement basic arithmetic yet.

There's also WSL which we want to "take into account" when it becomes a thing.

Installation

pip install python-shader

Usage

Decorate a function to turn it into a ShaderModule object:

import python_shader

@python_shader.python2shader
def vertex_shader(input, output):
    input.define("index", "VertexId", i32)
    output.define("pos", "Position", vec4)
    output.define("color", 0, vec3)

    positions = [vec2(+0.0, -0.5), vec2(+0.5, +0.5), vec2(-0.5, +0.7)]

    p = positions[input.index]
    output.pos = vec4(p, 0.0, 1.0)
    output.color = vec3(p, 0.5)

You can then get the bytes representing the SpirV via vertex_shader.to_spirv(). The module object can be used as-is in wgpu-py.

Developers

If you want to use python_shader.dev.validate(), python_shader.dev.disassemble(), or python_shader.dev.glsl2spirv(), you need to seperately install the Vulkan SDK.

License

This code is distributed under the 2-clause BSD license.

You can’t perform that action at this time.