GLSLT Template compiler library
Project description
glslt
glslt is the main library that supports the GLSL Template compiler transforms. If you are
building a system that relies on transforming GLSLT code, you'll want to interact with this
library directly instead of the command-line interface provided by glsltc
.
Usage
Rust crate
The glslt crate manipulates syntax trees generated by the glsl-lang crate.
use glslt::glsl_lang::{ast::*, parse::IntoParseBuilderExt};
use glslt::transform::{Unit, TransformUnit};
let glsl_src = r#"
float sdf3d(in vec3 p);
float colort();
float sdSphere(vec3 p, float r) {
return length(p) - r;
}
float opElongate(in sdf3d primitive, in colort C, in vec3 p, in colort D, in vec3 h) {
vec3 q = p - clamp(p, -h, h);
return C() * primitive(q) * D();
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
float sz = 5.;
fragColor = vec4(vec3(opElongate(sdSphere(_p, sz), 1.0, vec3(fragCoord, 0.), 2.0, vec3(1., 2., 3.))), 1.0);
}
"#;
// Parse the GLSLT source code
let tu: TranslationUnit = glsl_src
.builder()
.context(&glslt::parse::make_parse_context(None))
.parse()
.expect("failed to parse GLSLT source")
.0;
// Create the transform unit
let mut unit = Unit::new();
// Parse declarations
for decl in tu.0.into_iter() {
unit.parse_external_declaration(decl).expect("failed to parse declaration");
}
// Generate the result
let tu = unit.into_translation_unit().expect("failed to generate output");
// Transpile the syntax tree to GLSL source
let mut output_src = String::new();
glsl_lang::transpiler::glsl::show_translation_unit(
&mut output_src,
&tu,
glsl_lang::transpiler::glsl::FormattingState::default(),
).expect("failed to generate GLSL");
Python library
If you installed the glslt library via pip install glslt
or maturin develop
, you may use the Python interface to the GLSLT compiler.
import glslt
# Parse the `sdf.glsl` file with `my-glsl-lib/include` being a system include
# directory for #include resolution
translation_unit = glslt.parse_files(["sdf.glsl"], ["my-glsl-lib/include"])
# Create a new minimizing transform unit
unit = glslt.MinUnit()
# Add the parsed declarations to the transform unit
unit.add_unit(translation_unit)
# Get the output of the transform
result = unit.to_translation_unit(["mainImage"])
# Print the GLSL code
print(result.to_glsl())
Author
Vincent Tavernier vince.tavernier@gmail.com
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 Distribution
glslt-0.6.1.tar.gz
(26.9 kB
view hashes)
Built Distributions
Close
Hashes for glslt-0.6.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf25005d9c34f82ab245429a4c16d9397a747924bfa75e3acbb1cce1920989f4 |
|
MD5 | ea3c02c2a6ef75c31755ba0cfe8c69c8 |
|
BLAKE2b-256 | a66f808f66b4ca6e5146050bf2f443656f02cb02a02f2d4aa519a0780e159b7b |
Close
Hashes for glslt-0.6.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21a707f8e61a7633a7c1c38b48b740218b3745b6c3b3deb051ba1d1c593fd896 |
|
MD5 | e85419b958911e7a526ac90e7797371d |
|
BLAKE2b-256 | c6205252077c389e7670d9763ab2906fe8bb3b7b55c2db8d1ebe5f15f2464a93 |
Close
Hashes for glslt-0.6.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab8cee147c05a02d9a06a0d1341e08bbfe3b81b1d6e0e9d169c4f091a6597a22 |
|
MD5 | 578021fe4a96b38babb50f4c79e355af |
|
BLAKE2b-256 | 257f359f91e01c76db1096236a3a216cab07c84fc2ab672fc9b0e1b999c0f8fd |
Close
Hashes for glslt-0.6.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ceee833211dc7c56467c45b0540685273fa2aa2ebc4c9a90ab3d9563b1ebed7e |
|
MD5 | 6d6edddd647a43247c4d44ae2ec4762e |
|
BLAKE2b-256 | b1ded2d816c0bee5c501afbff656f563e24d9f0eebe46e0699a9d0704aa846e7 |
Close
Hashes for glslt-0.6.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ce15cd22bb828f9b3d622b6d803bf9aea49656a008fb3d48c509ff444ce1768 |
|
MD5 | 3ac942fbaa93ffcf64fc48d745719266 |
|
BLAKE2b-256 | c1297aa1f00274e0458cb5669800fe3d219f61c11afe9befa52eee3cb954536f |
Close
Hashes for glslt-0.6.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f6b4fd03ad6e7aaf2b5469999ab90d10d474944fbc18b97735c0dc1b99efb6d |
|
MD5 | e1998980b3bf514a3e81bfd6bd1f5374 |
|
BLAKE2b-256 | e26bca7b406fb485c2d3c8ef9ddb3242266343e046b46c6425c8ab7c127c7c91 |
Close
Hashes for glslt-0.6.1-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05e9c2de9c674e5db0cc3951163a0845ec1195a83456a8670954195aa866566d |
|
MD5 | f6f48c7df55c64692f09316ca3df77bf |
|
BLAKE2b-256 | 9e35b48bf3eb1a26209336dc6eb3ba580fdba8b0e1eefb448b1f88a5c58cc2af |
Close
Hashes for glslt-0.6.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68f4ccc7f6a682129d9f1a54ca7c6129654c3c1ca5c6ead6d7d999a40c953463 |
|
MD5 | ee356803c8a7dfa826587c398676f516 |
|
BLAKE2b-256 | 63ac1294af30d4b2ff60a352b23c9d87dfa53d2bea48e0fadb73edb818fb30d8 |