Rust-like syntax for OpenGL Shading Language

Overview

Rust-like syntax for GLSL

Build Status

glassful translates a small subset of Rust to OpenGL Shading Language.

Besides one's personal preferences regarding Rust-like vs. C-like syntax, this has a few specific advantages for the Rust programmer:

  • Syntax is checked at Rust compile time, with friendly rustc-style errors
  • There's less cognitive overhead switching between CPU and GPU code
  • Shaders can use Rust macros!
  • Shaders embedded in a Rust program have syntax highlighting

The library is still in a very early stage! Many improvements are possible. See the issue tracker and don't hesitate to send pull requests :)

Usage

There are three ways to invoke the translator. The language syntax is exactly the same in all three cases.

As a macro

#![feature(plugin)]

#![plugin(glassful_macros)]

const VERTEX: &'static str = glassful! {
    #![version="110"]

    #[attribute] static position: vec2 = UNINIT;
    #[varying]   static color:    vec3 = UNINIT;

    fn main() {
        gl_Position = vec4(position, 0.0, 1.0);
        color = vec3(0.5*(position + vec2(1.0, 1.0)), 0.0);
    }
};

const FRAGMENT: &'static str = glassful! {
    #![version="110"]

    #[varying] static color: vec3 = UNINIT;

    fn main() {
        gl_FragColor = vec4(color, 1.0);
    }
};

let program = glium::Program::from_source(&display, VERTEX, FRAGMENT, None);

See examples/gradient/ for a full glium/glutin example.

As an external program

$ ./target/glassful < shader.glassful > shader.glsl

As an ordinary library

extern crate glassful;

pub fn main() {
    let prog = io::stdin().read_to_end().unwrap();
    let prog = String::from_utf8(prog).unwrap();
    print!("{}", glassful::translate(prog));
}
You might also like...
Deduplicating Training Data Makes Language Models Better

Deduplicating Training Data Makes Language Models Better This repository contains code to deduplicate language model datasets as descrbed in the paper

Framework and Language for Neuro-Symbolic Programming
Framework and Language for Neuro-Symbolic Programming

Scallop Scallop is a declarative language designed to support rich symbolic reasoning in AI applications. It is based on Datalog, a logic rule-based q

A lesson in Humility - or how a boy teaches himself a new programming language.

Learning Rust - A Lesson in Humility J. M. Barrie - Author of Peter Pan Life is a long lesson in humility... and so is learning Rust. I tried to teach

A programming language written in an "easy to understand" way

Trees Trees は、ブロックプログラミング言語で、以下の特徴を備えています! 分かりやすい (easy to understand) 読みやすい (readable) 曖昧性がない (clear) ┌─────┐ │print│ └───┬─┘

Msgpack serialization/deserialization library for Python, written in Rust using PyO3, and rust-msgpack. Reboot of orjson. msgpack.org[Python]

ormsgpack ormsgpack is a fast msgpack library for Python. It is a fork/reboot of orjson It serializes faster than msgpack-python and deserializes a bi

Practice repo for learning Rust. Currently going through "Rust for JavaScript Developers" course.

rust-practice 🦀 Practice repo for learning Rust. Directories /rust-for-js-dev Files directed towards "Rust for JavaScript Developers" course. Thank y

A Rust library with homemade machine learning models to classify the MNIST dataset. Built in an attempt to get familiar with advanced Rust concepts.

mnist-classifier Ideas UPDATED: Finish CLI Flags Parallelize conputationally intensive functions Class-based naive bayes README Image parsing Confusio

🦀Rust Turkiye - Rust Dersleri

Rust Turkiye - Rust Dersleri CURIOSITY - Featuring Richard Feynman Bu repo Rust Turkiye tarafindan duzenlenen Rust Dersleri egitiminin alistirma ve ko

A Rust machine learning framework.

Linfa linfa (Italian) / sap (English): The vital circulating fluid of a plant. linfa aims to provide a comprehensive toolkit to build Machine Learning

Comments
  • Overhaul syntax to use function parameters and returns

    Overhaul syntax to use function parameters and returns

    I've made changes that allow (not force) the merging of shader stages into one invokation of glassful. Each stage's main is marked by an attribute of the stage. Everything besides a stage's main is copied into every stage, not including other stages' mains. Function parameters not mut become in values in glsl, and parameters marked mut become out values, and if there's a return value, then that becomes the very last out value. A couple uppercase types are converted into glsl types as well, including Pnt*, Vec*, Mat* into their (lowercase) glsl counterparts. The lowercase versions could still be used for function syntax if that is clearer. My changes to the README.md say more.

    Be warned: shaders.rs has alot of things commented out, I've been playing with syntax there. But, what I've outlined should work.

    opened by loafofpiecrust 0
  • Syntax error, unexpected IF

    Syntax error, unexpected IF

    Am I doing something wrong here:

    let fragment_shader_src = glassful! {
        #![version="140"]
    
        #[varying] static v_coord: vec3 = UNINIT;
    
        fn main() {
            if (abs(v_coord.z) == 0.25) {
                gl_FragColor = vec4(0., 1., 0., 1.);
            } else if (abs(v_coord.x) == 0.25) {
                gl_FragColor = vec4(0., 0., 1., 1.);
            } else if (abs(v_coord.y) == 0.25) {
                gl_FragColor = vec4(1., 0., 0., 1.);
            }
        }
    };
    

    I get:

    thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: CompilationError("0:5(8): error: syntax error, unexpected IF\n")', ../src/libcore/result.rs:731```
    
    opened by gsingh93 2
  • Express data flow better

    Express data flow better

    e.g.

    const UNIFORMS: Uniforms;
    
    fn vertex(v: Attributes) -> (vec4, Varyings) { ... }
    fn fragment(v: Varyings) -> vec4 { ... }
    

    where those struct definitions are lifted up from glium (c.f. #2)

    opened by kmcallister 1
  • Write a new parser?

    Write a new parser?

    We only support a tiny subset of Rust. It could be nice to have a 100 line grammar for shader programs instead of the grammar being the 6,000 line Rust parser + a pile of ad-hoc restrictions.

    We could also make small changes to the syntax, e.g. removing the mandatory initializers on statics. As long as the tokens are compatible with Rust, the macro re-parses from source anyway.

    Using the actual libsyntax parser was mostly a hack to get the proof of concept done in one day :P

    opened by kmcallister 0
Owner
Keegan McAllister
Keegan McAllister
[WIP] An experimental Java-like language and it's virtual machine, for learning Java and JVM.

Sky VM An experimental Java-like language and it's virtual machine, for learning Java and JVM. Dependencies Rust (rust-lang/rust) 2021 Edition, dual-l

Kk Shinkai 2 Jan 3, 2022
Tensors and differentiable operations (like TensorFlow) in Rust

autograd Differentiable operations and tensors backed by ndarray. Motivation Machine learning is one of the field where Rust lagging behind other lang

Ryo ASAKURA 403 Dec 25, 2022
Rust language bindings for TensorFlow

TensorFlow Rust provides idiomatic Rust language bindings for TensorFlow. Notice: This project is still under active development and not guaranteed to

null 4.1k Jan 1, 2023
Rust language bindings for Faiss

Faiss-rs This project provides Rust bindings to Faiss, the state-of-the-art vector search and clustering library. Installing as a dependency Currently

Eduardo Pinho 86 Jan 7, 2023
A tour of rust's language features

Tour of Rust Welcome to the source repo of Tour of Rust. Goals This project is meant to give an experienced programmer a swift introduction to Rust as

RICHΛRD ΛNΛYΛ 693 Jan 6, 2023
Rust language bindings for the LIBLINEAR C/C++ library.

liblinear-rs Rust bindings for the liblinear C/C++ library. Provides a thin (but rustic) wrapper around the original C-interface exposed by the librar

Madeesh Kannan 8 Sep 22, 2022
🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧

?? rust-gpu Rust as a first-class language and ecosystem for GPU graphics & compute shaders Current Status ?? Note: This project is still heavily in d

Embark 5.5k Jan 9, 2023
A collection of CC-BY-SA course material to teach the Rust programming language, in different formats, levels, and focus points

A collection of CC-BY-SA course material to teach the Rust programming language, in different formats, levels, and focus points. Contact me for remote and on-site trainings!

Katharina Fey 13 Apr 13, 2023
🌾 High-performance Text processing library for the Thai language, built with Rust and exposed as a Python package.

Thongna ?? Thongna (ท้องนา) is a high-performance text processing library for the Thai language, built with Rust and exposed as a Python package. Insp

fr4nk 3 Aug 17, 2024
Masked Language Model on Wasm

Masked Language Model on Wasm This project is for OPTiM TECH BLOG. Please see below: WebAssemblyを用いてBERTモデルをフロントエンドで動かす Demo Usage Build image docker

OPTiM Corporation 20 Sep 23, 2022