Skip to main content

glsl-shaderinfo

glsl-shaderinfo aims to parse shader files written in the GLSL language and extract useful info about declared vars, inputs outputs, uniforms etc.

Implementation info

glsl-shaderinfo is a Python library implemented primarily in Rust, using the PyO3 + Maturin toolchain.

We are using the GLSL parser crate here:

It seems fairly robust and actively maintained (2021-04). Supports up to GLSL450/GLSL460.

Installation

pip install glsl-shaderinfo

Wheels for Linux (Python 3.6, 3.7, 3.8, 3.9) are provided.

For macOS or Windows you can still install via pip but you need to have the Rust toolchain installed (i.e. cargo on your PATH) and then it will be built from source automatically.

You can install it via: https://rustup.rs/

Usage

Now we can:

$ ipython
Python 3.9.2 (default, Feb 23 2021, 12:16:37)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from glsl_shaderinfo import get_info

In [2]: with open("../modernglproj/resources/shaders/myproj_geometry.glsl") as f:
   ...:     src = f.read()
   ...:

In [3]: info = get_info(src)

In [4]: info.inputs
Out[4]: [<VarInfo in VS_OUT "gs_in">]

In [5]: info.blocks
Out[5]:
{'GS_OUT': <BlockInfo "GS_OUT" (3 fields)>,
 'VS_OUT': <BlockInfo "VS_OUT" (3 fields)>}

In [7]: info.blocks[info.inputs[0].type_specifier].fields
Out[7]:
[<FieldInfo vec2 "gridCoord">,
 <FieldInfo vec3 "pos">,
 <FieldInfo int "textureId">]

In [8]: info.uniforms
Out[8]: [<VarInfo uniform float "size">, <VarInfo uniform mat4 "projection">]

In [9]: info.uniforms[0].storage
Out[9]: <StorageQualifier.UNIFORM: 'Uniform'>

In [10]: info.uniforms[0].type_specifier
Out[10]: <TypeSpecifier.FLOAT: 'Float'>

In [11]: info
Out[11]: <ShaderInfo for GLSL: 330 (1 in, 1 out)>

Development setup

Install the Rust toolchain: https://rustup.rs/

Clone this repo.

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
maturin develop

maturin is installed as a python library and is used as the rust-python-lib build tool.

maturin develop builds the python module and installs it in the current virtualenv.

Build & Release

The Linux wheels are built via Docker:

docker run --rm -v $(pwd):/io konstin2/maturin publish --username anentropic --password <pass>

maturin will push them up to PyPI.

PyPI package metadata is derived primarily from Cargo.toml rather than pyproject.toml.

TODO

Future directions

I have basically no Rust experience so the code here is probably awful, but it does work at least. I will try and improve it as I learn more.

The most flexible option for future use cases would be to re-export the whole of the glsl crate interface into Python types and modules, i.e. expose the full parser and AST. Then we could build AST visitors for abitrary use cases in Python. Possibly we can use https://serde.rs/remote-derive.html to shadow the types from glsl and dump them to Python primitives. See also https://docs.rs/pythonize/0.13.0/pythonize/ and possibly https://github.com/gperinazzo/dict-derive

But currently the AST visitors are built in the Rust side and provide a general meta info about declared variables, and we export this much smaller interface (basically just a single get_info method and some types) into Python.

This project has a cli util for printing the AST from the parser, to help explore what is available.

To build it:

cargo build --bin glsl-ast

To run it:

target/debug/glsl-ast ../modernglproj/resources/shaders/myproj_geometry.glsl

Metadata

Release files for glsl-shaderinfo 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for glsl-shaderinfo 0.1.2
File Size Uploaded
glsl_shaderinfo-0.1.2.tar.gz 17.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for glsl-shaderinfo 0.1.2
File
glsl_shaderinfo-0.1.2-cp39-cp39-manylinux1_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.5+ x86-64 Details
glsl_shaderinfo-0.1.2-cp38-cp38-manylinux1_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.5+ x86-64 Details
glsl_shaderinfo-0.1.2-cp37-cp37m-manylinux1_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64 Details
glsl_shaderinfo-0.1.2-cp36-cp36m-manylinux1_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 Details

Total release size: 1.8 MB

Release files / glsl_shaderinfo-0.1.2.tar.gz

Download URL glsl_shaderinfo-0.1.2.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e8a1d71c2e6d8e079642e4e3b3646c985a5c5130da2c550e5b26e2e54381d04c
BLAKE2b-256 checksum
How to use checksums
b1938da397de2911fae029dcb85ee322dfb6a6ce276e1418efdeda1c52ebf021
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.0

Release files / glsl_shaderinfo-0.1.2-cp39-cp39-manylinux1_x86_64.whl

Download URL glsl_shaderinfo-0.1.2-cp39-cp39-manylinux1_x86_64.whl
Size 445.6 kB
Tags CPython 3.9 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
023bc3d771e73a8bf4bf183842a78fdd7497719a6bf92cc26de7a7b77e0cd209
BLAKE2b-256 checksum
How to use checksums
176271a035f543a5e108ed3bc271f0fc1c9ccbced11fa6b0a6127c3e62185fa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.0

Release files / glsl_shaderinfo-0.1.2-cp38-cp38-manylinux1_x86_64.whl

Download URL glsl_shaderinfo-0.1.2-cp38-cp38-manylinux1_x86_64.whl
Size 445.7 kB
Tags CPython 3.8 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
5052aa8afcf46dd24200db2449a73c8c67d8763fd01e2cbcde94310e900d2ac8
BLAKE2b-256 checksum
How to use checksums
1e0dc3423c4ef21165037be85ab5d210967d287388d178cff0f28d913dff4815
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.0

Release files / glsl_shaderinfo-0.1.2-cp37-cp37m-manylinux1_x86_64.whl

Download URL glsl_shaderinfo-0.1.2-cp37-cp37m-manylinux1_x86_64.whl
Size 445.6 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
d32147da975ae4a6bf608fd3703bbeff341bc8f26f223cf66790c1d6b249f166
BLAKE2b-256 checksum
How to use checksums
1ab978441295ca3c595ba7fad7e0131236d401767b260dfc2ce0f2b49dcd7387
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.0

Release files / glsl_shaderinfo-0.1.2-cp36-cp36m-manylinux1_x86_64.whl

Download URL glsl_shaderinfo-0.1.2-cp36-cp36m-manylinux1_x86_64.whl
Size 445.5 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
b4aeeb2e40f089e5dd53685f3902bea52eda40430b9b248a20a0ca2d19873b2a
BLAKE2b-256 checksum
How to use checksums
e3ef6a405679c5faef9b52a60278277cbd8664d45e8a00483c14fdba8873647a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.10.0

Release history Release notifications | RSS feed

This release

0.1.2 This release

5 release files

0.1.1

5 release files

0.1.0

5 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page