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.0.tar.gz
(26.9 kB
view hashes)
Built Distributions
Close
Hashes for glslt-0.7.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 012f6e071def739731a9ea714d5bdf5e603ee24a90fcfa69cdc3193f564675fc |
|
MD5 | 90f40be26977d6ef2b2804936759d7f4 |
|
BLAKE2b-256 | f406301122f294726e5b3ee0ffe3cbcc7e55b637d347ed735fa21b945a638ad3 |
Close
Hashes for glslt-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da7101c5d21461e93c673171eb037afc5f7df8e39da1ae45b16dd9db0bd63544 |
|
MD5 | c88005974c8fda9b467996acedb5231d |
|
BLAKE2b-256 | 331c6127b251d46c84e7fefc6baff419ce6bf6e4be39900cef082230284d68bb |
Close
Hashes for glslt-0.7.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a24f3172fe7abcb0ad57999dcb5a4703f0ff5e0780030f22bc9851aaf10b3cca |
|
MD5 | a1d7a0c9c3d3bd2e22ed3fa11215d92c |
|
BLAKE2b-256 | c8ee7c4abcccfa3748372093a5f67d9c87aa2a6a8cdfe161fda05d9261c921a1 |
Close
Hashes for glslt-0.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bf6c829bc57e20f973c183473d6f730734e12a8322836ac3f4bc9c478c1a2fc |
|
MD5 | dc8605a00e2e9fc5f35863d700b7c424 |
|
BLAKE2b-256 | 82d2fee843e8be0a3ab639f48e581bac0abccfece4c899cfef66ec564e5b1efe |
Close
Hashes for glslt-0.7.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a1902743fa020cc7489ec312b4964ad25112e8e8aa8897a163bf47b9c7f7260 |
|
MD5 | 73fafb7312d28f901c4b047a6422e042 |
|
BLAKE2b-256 | b3a9457a8d22326838adcba8a761557cfe5b4e9fb2ce53ffe92e77fe0763ad26 |
Close
Hashes for glslt-0.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2248cd68db0ab0f441a3b164a3432d9c58aeddc70908f630d20d7c663cf2f469 |
|
MD5 | 9eb294a09dabe58eeedec6d745a4f20d |
|
BLAKE2b-256 | 121085dea4cb5a96df88924ae735f41268a80697f64978cc481fd5ee2c7aded1 |
Close
Hashes for glslt-0.7.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34119c8cfadb87f57b30f97e5c9c3bf76baaa072b42f4fd06b16f45035fa6364 |
|
MD5 | 9b739742e3292741e82a1f9cc932fda1 |
|
BLAKE2b-256 | c683afb42b132516a87cc2d3b6aa54a2ff01528b81fbe2a94419625929c9b97a |
Close
Hashes for glslt-0.7.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f873ef48b08bfa99c3a05804ba9d46a371c6681907ff39f5d571a30c8dc20c7c |
|
MD5 | f772fe4743785230b0cd94abd31b186b |
|
BLAKE2b-256 | 7011d6dab2469931a8cb6d603a9a699ff2f9cd2b2fa805a46e8150df5984b476 |