Transform Python to GLSL
Project description
pyglsl
[!CAUTION] Work in progress, see TODO section.
Transform Python to GLSL. Fork from long abandoned nicholasbishop/shaderdef. Most of the work was done by the original author, I've just fixed and updated some stuff.
[!NOTE]
pip install pyglsl==0.0.5
Example
[!IMPORTANT] Shaders are only translated, not compiled. This is to avoid dependency issues and makes it more portable. There is also no static analysis beyond what is valid Python. Errors will have to be deciphered after compiling or runtime.
[!WARNING] Not every aspect of Python is supported when writing a shader. For example, loops are limited to
for i in range()type loops.
[!TIP] It is a good idea to keep your pyglsl shader code in a seperate file and importing with
from pyglsl.glsl import *. This will avoid naming conflicts aspyglsl.glslcontains the builtin types + functions from GLSL. This will pollute your namespace.
Python input
# shader.py
from pyglsl.glsl import *
class VertAttrs(AttributeBlock):
vert_loc = vec3()
vert_nor = vec3()
vert_col = vec4()
class View(UniformBlock):
projection = mat4()
camera = mat4()
model = mat4()
class VsOut(ShaderInterface):
gl_Position = vec4()
normal = vec3()
color = vec4()
class FsOut(FragmentShaderOutputBlock):
fs_color = vec4()
def perspective_projection(projection: mat4, camera: mat4, model: mat4, point: vec3) -> vec4:
return projection * camera * model * vec4(point, 1.0)
def vert_shader(view: View, attr: VertAttrs) -> VsOut:
return VsOut(gl_position=perspective_projection(view.projection, view.camera, view.model, attr.vert_loc),
normal=attr.vert_nor,
color=attr.vert_col)
def frag_shader(vs_out: VsOut) -> FsOut:
color = vec4((vs_out.normal.x + 1.0) * 0.5,
(vs_out.normal.y + 1.0) * 0.5,
(vs_out.normal.z + 1.0) * 0.5,
1.0)
return FsOut(fs_color=mix(vs_out.color, color, 1.0))
# ShaderDef class is optional. Makes shaders easier to export+import
export = ShaderDef(vertex_shader=vert_shader,
fragment_shader=frag_shader,
vertex_functions=[perspective_projection])
Then import the shader module and compile.
# example.py
# import shader module (shader.py)
from shader import export as test_shader
# NOTE: You can compile each stage individually by using the *Stage classes. This is if you
# don't want to use the ShaderDef class inside of your pyglsl shader.
# from pyglsl import VertexStage, FragmentStage
# from shader import vert_shader, frag_shader, perspective_projection
# vs_stage = VertexStage(vert_shader, library=[perspective_projection])
# fs_stage = FragmentStage(frag_shader)
# vs = vs_stage.compile()
# fs = fs_stage.compile()
# Compile Python AST to GLSL + print
vs, fs = test_shader.compile()
print(vs)
print(fs)
Vertex shader output
#version 330 core
layout(location=0) in vec3 vert_loc;
layout(location=1) in vec3 vert_nor;
layout(location=2) in vec4 vert_col;
uniform mat4 projection;
uniform mat4 camera;
uniform mat4 model;
vec4 perspective_projection(mat4 projection, mat4 camera, mat4 model, vec3 point) {
return (((projection * camera) * model) * vec4(point, 1.0));
}
out VsOut {
vec3 normal;
vec4 color;
} vs_out;
void main() {
gl_Position = perspective_projection(projection, camera, model, vert_loc);
vs_out.normal = vert_nor;
vs_out.color = vert_col;
}
Fragment shader output
#version 330 core
in VsOut {
vec3 normal;
vec4 color;
} vs_out;
layout(location=0) out vec4 fs_color;
void main() {
vec4 color = vec4(((vs_out.normal.x + 1.0) * 0.5), ((vs_out.normal.y + 1.0) * 0.5), ((vs_out.normal.z + 1.0) * 0.5), 1.0);
fs_color = mix(vs_out.color, color, 1.0);
}
Voilà.
TODO
- Documentation
-
Finish adding GLSL types + builtins -
Add to pypi - Geometry Shaders
LICENSE
pyglsl
Copyright (C) 2016 Nicholas Bishop
Copyright (C) 2025 George Watson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyglsl-0.0.5-py3-none-any.whl.
File metadata
- Download URL: pyglsl-0.0.5-py3-none-any.whl
- Upload date:
- Size: 27.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
029e212bf6a5f8ab1fe41ab579a3aae8575ef6ef1f00077ce3156672c2d856e4
|
|
| MD5 |
f95095acad46e4d61473a19668fb8e8d
|
|
| BLAKE2b-256 |
2e7bb9bf79e8fe9c8163e5522f768e2fe690cd622e11d8fba3260172d12214b6
|