Package: glsl-shaderinfo
glsl-shaderinfoglsl-shaderinfoaims to parse shader files written in the GLSL language and extract useful info about declared vars, inputs outputs, uniforms etc.Implementation infoglsl-shaderinfois a Python library implemented primarily in Rust, using the PyO3 + Maturin toolchain.https://github.com/PyO3/pyo3https://github.com/PyO3/maturinWe are using the GLSL parser crate here:https://crates.io/crates/glsl/https://github.com/phaazon/glslIt seems fairly robust and actively maintained (2021-04). Supports up to GLSL450/GLSL460.Installationpip install glsl-shaderinfoWheels 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.cargoon your PATH) and then it will be built from source automatically.You can install it via:https://rustup.rs/UsageNow we can:$ ipythonPython 3.9.2 (default, Feb 23 2021, 12:16:37)Type 'copyright', 'credits' or 'license' for more informationIPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.In [1]: from glsl_shaderinfo import get_infoIn [2]: with open("../modernglproj/resources/shaders/myproj_geometry.glsl") as f:...:     src = f.read()...:In [3]: info = get_info(src)In [4]: info.inputsOut[4]: [<VarInfo in VS_OUT "gs_in">]In [5]: info.blocksOut[5]:{'GS_OUT': <BlockInfo "GS_OUT" (3 fields)>,'VS_OUT': <BlockInfo "VS_OUT" (3 fields)>}In [7]: info.blocks[info.inputs[0].type_specifier].fieldsOut[7]:[<FieldInfo vec2 "gridCoord">,<FieldInfo vec3 "pos">,<FieldInfo int "textureId">]In [8]: info.uniformsOut[8]: [<VarInfo uniform float "size">, <VarInfo uniform mat4 "projection">]In [9]: info.uniforms[0].storageOut[9]: <StorageQualifier.UNIFORM: 'Uniform'>In [10]: info.uniforms[0].type_specifierOut[10]: <TypeSpecifier.FLOAT: 'Float'>In [11]: infoOut[11]: <ShaderInfo for GLSL: 330 (1 in, 1 out)>Development setupInstall the Rust toolchain:https://rustup.rs/Clone this repo.python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
maturin developmaturinis installed as a python library and is used as the rust-python-lib build tool.maturin developbuilds the python module and installs it in the current virtualenv.Build & ReleaseThe Linux wheels are built via Docker:docker run --rm -v $(pwd):/io konstin2/maturin publish --username anentropic --password <pass>maturinwill push them up to PyPI.PyPI package metadata is derived primarily fromCargo.tomlrather thanpyproject.toml.TODOcomprehensive tests, any tests...here are some to copyhttps://github.com/graphitemaster/glsl-parser/tree/main/testsalso e.g. all the example programs frommodernglare the returned data structures the most useful representation?what to do with uniform blocks?what to do with layouts? i.e.https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)#Shader_stage_optionsother obscure corners of syntax (I believe the parser supports everything)docsdocstrings:on the Rust side:https://doc.rust-lang.org/stable/book/ch14-02-publishing-to-crates-io.html#making-useful-documentation-commentsPyO3 should bring them across:https://pyo3.rs/v0.13.2/module.html#documentationrelease automation via GitHub ActionsFuture directionsI 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 theglslcrate 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 usehttps://serde.rs/remote-derive.htmlto shadow the types fromglsland dump them to Python primitives. See alsohttps://docs.rs/pythonize/0.13.0/pythonize/and possiblyhttps://github.com/gperinazzo/dict-deriveBut 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 singleget_infomethod 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-astTo run it:target/debug/glsl-ast ../modernglproj/resources/shaders/myproj_geometry.glsl