7 releases
0.3.4 | Apr 30, 2023 |
---|---|
0.3.3 | Apr 29, 2023 |
0.2.1 | Apr 16, 2023 |
0.1.0 | Apr 16, 2023 |
#1314 in Rust patterns
74 downloads per month
8KB
52 lines
rs2glsl
Automatically convert Rust code to GLSL for use in GPU programming.
Example
You can write Rust code like so
use rs2glsl::prelude::*;
#[glsl]
fn smooth_min(a: f32, b: f32, k: f32) -> f32 {
let mut h: f32 = a - b;
h = 0.5 + 0.5 * h;
if h < 0.0 {
h = 0.0;
}
if h > 1.0 {
h = 1.0;
}
return interpolation(a, b, h) - k * h * (1.0 - h);
}
#[glsl]
fn interpolation(a: f32, b: f32, t: f32) -> f32 {
return a * (1.0 - t) + b * t;
}
And it gets translated to the following GLSL code.
float smooth_min(float a, float b, float k) {
float h = a - b;
h = 0.5 + 0.5 * h;
if (h < 0.0) {
h = 0.0;
}
if (h > 1.0) {
h = 1.0;
}
return interpolation(a, b, h) - k * h * (1.0 - h);
}
You can access the examples GLSL definition like so:
GLSL_SMOOTH_MIN.definition()
Have fun!
lib.rs
:
Automatic code convertion from Rust to GLSL.
use rs2glsl::prelude::*;
#[glsl]
fn smooth_min(a: f32, b: f32, k: f32) -> f32 {
let mut h: f32 = a - b;
h = 0.5 + 0.5 * h;
if h < 0.0 {
h = 0.0;
}
if h > 1.0 {
h = 1.0;
}
return interpolation(a, b, h) - k * h * (1.0 - h);
}
#[glsl]
fn interpolation(a: f32, b: f32, t: f32) -> f32 {
return a * (1.0 - t) + b * t;
}
let expected = r#"
float smooth_min(float a, float b, float k) {
float h = a - b;
h = 0.5 + 0.5 * h;
if (h < 0.0) {
h = 0.0;
}
if (h > 1.0) {
h = 1.0;
}
return interpolation(a, b, h) - k * h * (1.0 - h);
}
"#;
assert_eq!(expected.trim(), GLSL_SMOOTH_MIN.definition());
Dependencies
~280–740KB
~17K SLoC