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 v.tavernier@pm.me
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.7.1.tar.gz
(26.9 kB
view hashes)
Built Distributions
Close
Hashes for glslt-0.7.1-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25552aaa4772901ebac85635819428adaee4d03d4862bb255583d5bb0b450e56 |
|
MD5 | 466c324d9bf90e79c4568acf79db4ffd |
|
BLAKE2b-256 | 692db103665c1bcef53657f0adae169cb5affdefc3574f5221f5bd664fa83443 |
Close
Hashes for glslt-0.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c5d8d585efe3ceeee2452e3b534d974b622bdb5ba889da6951e96d56f1e3453 |
|
MD5 | 691a898f461a97a20b6b1411b0d32d2a |
|
BLAKE2b-256 | 2e1ba60216e62bafc8f806f6ad9f7c92ea75a159f0cab8770e773328349b330d |
Close
Hashes for glslt-0.7.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6bff1ed548f9c8ef9cbf7ca9ddaa4a05f1b4b220bcb1ca30b355411e9427616 |
|
MD5 | 3d0343b054f98aaa4087b2fc8a691b13 |
|
BLAKE2b-256 | 2eb0541ff5d2bbd4ceea5362140fcc0116a3fa6061053b3a995b37aadc456860 |
Close
Hashes for glslt-0.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 552662bb736722c318d2a08d5c24925780c411a56f3468ee6b0c542fc206e34c |
|
MD5 | df85724869f22ea6a7eb41875b0274c9 |
|
BLAKE2b-256 | 8b10713610e4dfa3943ac9d54df45c04ad7cd50d3bd964c67f5a9cf8fb3bb27d |
Close
Hashes for glslt-0.7.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 452bd2cf395453b2691b4de075ca0b4f7cb357e00ad95065375c57f5428991fb |
|
MD5 | e4709bcc86e0702a7b8976830e392a72 |
|
BLAKE2b-256 | eec094273f6a3ca40764ad5cfd7d55c1c951d16b3aed875df0825bf930025533 |
Close
Hashes for glslt-0.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5ddfd2960d2eb88cadd925778ac8e4ec5f1c003033c74267e708678fee0fd14 |
|
MD5 | b8045e252d5a42f1d33ebf1b656a4de6 |
|
BLAKE2b-256 | 3f65d0828cf7bd4d4fcefd0e987efa4d1920cb71aff93d7b8f0af06641b14454 |
Close
Hashes for glslt-0.7.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58e293915610b5cca54a088350338cb3e1cac4a748df4a9ec65f2b37bea6a0a1 |
|
MD5 | e19f8ec48fffaee41195a7363d28640b |
|
BLAKE2b-256 | 0b2bf4528fa79e9ccbdb9ff8cb4995f26fbc042ef76941325cc2693085d2381b |
Close
Hashes for glslt-0.7.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35849822fc0acd59a05b3ed82014f8d2473ec8a32ff6e269d89a196e5f42309b |
|
MD5 | 063962a6f5927378af536030e908d852 |
|
BLAKE2b-256 | b2005d35bb2942fa75eb15a2ba419d242d4a5e57f63ea358a28feb517dab2596 |