Skip to main content

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


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.2.tar.gz (26.9 kB view hashes)

Uploaded Source

Built Distributions

glslt-0.7.2-cp310-none-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

glslt-0.7.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

glslt-0.7.2-cp39-none-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

glslt-0.7.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

glslt-0.7.2-cp38-none-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

glslt-0.7.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

glslt-0.7.2-cp37-none-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.7 Windows x86-64

glslt-0.7.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page