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.4.tar.gz
(26.9 kB
view hashes)
Built Distributions
Close
Hashes for glslt-0.6.4-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7260f0f64a2fcf2a66b928b06c0477ae33ec1560ba4729b35a0f603c670630b9 |
|
MD5 | b4449c36988eef78258f42f6928ca196 |
|
BLAKE2b-256 | b05e36b2b4353edc27dc51ceb79ef2cdc571154025bb6f3a6e41f0953319c754 |
Close
Hashes for glslt-0.6.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a30c13404e30c08b9caeb4ea99a3977a8d42de7f192e909a98f1a16e83eb6c2 |
|
MD5 | f4c7a1db4287ce3be1e57c4d625e6a57 |
|
BLAKE2b-256 | 34478294b2823027f8e8ee8fa22bc36196acda304572e198864691c23faab12d |
Close
Hashes for glslt-0.6.4-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99e7f87b491a638488ad27bdfdccee3867482a0b76892b17fa92f1483f79fdd7 |
|
MD5 | 59a2901286f22d87f2b35fd894a98a5f |
|
BLAKE2b-256 | 82de933c01c822bca13d813dfd160e466f97745233afbc2c073b896b5d9f7ac8 |
Close
Hashes for glslt-0.6.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e3a4245c51c56cabde5636e5bd36d67dd55632b2effdb8c78c6bd8b6eb64310 |
|
MD5 | 49a12db0fc6b2aba262984b95934713e |
|
BLAKE2b-256 | fa13a5175ac74767a4ea0845e499923e702318323018a42d280d5f3cfa5ba005 |
Close
Hashes for glslt-0.6.4-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e6936095657a6c63abfca9e127b1900a91e3b6da9c72e94c798ddc2d6e868c6 |
|
MD5 | 4a8649726386c84ac87a50c68b7f86a3 |
|
BLAKE2b-256 | 4a2342d6f0fa661a99b7563c2f403889a1aa021ef782e76df9006d35b7fe35c3 |
Close
Hashes for glslt-0.6.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cb6d4a02e9657a8626b8ebf6ee6086eba4056927f95be9bb2d09601d1e941e1 |
|
MD5 | 60940f408f09dfc697232422315d8a55 |
|
BLAKE2b-256 | e28e9c42eafee37569928e2fa8c4b9636ba5e3f6d1609df806646ca49641dd1f |
Close
Hashes for glslt-0.6.4-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d30be8d45c89aab803837af686e67d80313bfa59fe62583326e2b32b68c41f3 |
|
MD5 | 37dce1cf9cd78c433bb056d215f2f221 |
|
BLAKE2b-256 | fc21c170c6963efe03055e759b1cbe9148b4765bbc53a0620ad366c3f1434b8e |
Close
Hashes for glslt-0.6.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7678c22d1151eb5a69999794a02045bd45b29a8b7cc407280d94b88089d9bd34 |
|
MD5 | 2c6bc46ae47f51f13f6b07feb7e6c06f |
|
BLAKE2b-256 | afee70d3cb8ea3f252e04128b3e52f47f0282bd762ce74ce74c183eb18af6947 |