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 glsltcc
.
Usage
Rust crate
The glslt crate manipulates syntax trees generated by the glsl crate (note that the fork currently used is https://github.com/vtavernier/glsl/tree/dev/).
use *;
use ;
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 = parse_source_default.expect;
// Create the transform unit
let mut unit = new;
// Parse declarations
for decl in .0.into_iter
// Generate the result
let tu = unit.into_translation_unit.expect;
// Transpile the syntax tree to GLSL source
let mut output_src = String new;
show_translation_unit.expect;
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.
# Parse the `sdf.glsl` file with `my-glsl-lib/include` being a system include
# directory for #include resolution
=
# Create a new minimizing transform unit
=
# Add the parsed declarations to the transform unit
# Get the output of the transform
=
# Print the GLSL code
Author
Vincent Tavernier vince.tavernier@gmail.com