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
Alixinne alixinne@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.3.tar.gz
(26.4 kB
view hashes)
Built Distributions
Close
Hashes for glslt-0.7.3-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17ed346bf5e5f1ea9ebbb561eb49240c2c89bfbfca8c9031e8f80434b8d37cab |
|
MD5 | c964849cc14daa05ed450d90ed1282b2 |
|
BLAKE2b-256 | 84e3b67220fbb17b077e0a78b0020dbb87a96ee915c6bcfc5a5cb9b9a0d39ed3 |
Close
Hashes for glslt-0.7.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51920704c99bf22562dc09d96b23aaaf58407eb328d173102354881a58448f0b |
|
MD5 | 248abe5774c3ebf87d05a3e78b7a375f |
|
BLAKE2b-256 | 88e29aafe3ee1417988bafc752cb1e47b4c5e5fb1a79f095c93831005460f425 |
Close
Hashes for glslt-0.7.3-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7aaca32a473158d1a5a70f222876345d780d73d1458ff3b51a93a656b42848f9 |
|
MD5 | 62638fd17e11eb6ad1abb28af1db06ce |
|
BLAKE2b-256 | 0ac2334400c2eda50986c75c312facce9129134612d3a89d1176399ed4313912 |
Close
Hashes for glslt-0.7.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0726159e54332f451f5a6ea04f952a77f3d1ca21cc34a1d7ec0cccd5e620cba6 |
|
MD5 | 03bee66dfeb2a827d27d1074e5e79356 |
|
BLAKE2b-256 | 9e508707f7ef4cf639d3b2aed11eac9a2c8510191b992d4c37bc6d6d06723b8d |
Close
Hashes for glslt-0.7.3-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42fd71a45637ac53af49b8f0f7edde77463a5d3a891aa65aec56b184ca5f42b3 |
|
MD5 | b7c4b2005c6f4f3c47c38dee53ce7d04 |
|
BLAKE2b-256 | b9779f6060e52e68d85c1872945c08c0812330997b3d8f7575e598834eaf0a94 |
Close
Hashes for glslt-0.7.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95aec8127bf13809e60be7e8126a0afc17073353023cac71997473a8a9bb2f32 |
|
MD5 | d189107acc6d6ba9e0f9cbcdc4fd2bfd |
|
BLAKE2b-256 | 231ed245134f0ca19cffe8ad76016cb4c718130a765114750214e44afd5dc25f |
Close
Hashes for glslt-0.7.3-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74fa75ab5f72757bf8aa00d38f1c7ff2a7486c9071fc07d4a6b60e8d22ed94e5 |
|
MD5 | e286706b36d0902fa0551f1371aefb83 |
|
BLAKE2b-256 | 5d683bc6e2d3c1d383a2b248f93873871c91126bcd08f53728f4ab0f7a3a1761 |
Close
Hashes for glslt-0.7.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 473e06869a07a930a73b1af05899e56a5ac00a264eda037ee23f9925e86bfe9f |
|
MD5 | 1fa8264eaecbac2b0bd8427d09414a24 |
|
BLAKE2b-256 | 9e1eabcedbe0dec5dfbb29a2dbaf65b61bb9441399cb416fddcbaa9a6dbf2565 |