patching together a scuffed shader lang
This commit is contained in:
commit
251e20b041
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
224
Cargo.lock
generated
Normal file
224
Cargo.lock
generated
Normal file
|
@ -0,0 +1,224 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.112"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"rand_hc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_hc"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
|
||||
dependencies = [
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rhai"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c7433068977c56619bf2b7831da26eb986d0645fe56f2ad9357eda7ae4c435e"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"instant",
|
||||
"num-traits",
|
||||
"rhai_codegen",
|
||||
"smallvec",
|
||||
"smartstring",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rhai-rand"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8f631680dbade170949a4330ae8937c87e86a0c1f7830121ec82d3780ebe5a1"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"rhai",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rhai_codegen"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e02d33d76a7aa8ec72ac8298d5b52134fd2dff77445ada0c65f6f8c40d8f2931"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shadeable"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rhai",
|
||||
"rhai-rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
|
||||
|
||||
[[package]]
|
||||
name = "smartstring"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31aa6a31c0c2b21327ce875f7e8952322acfcfd0c27569a6e18a647281352c9b"
|
||||
dependencies = [
|
||||
"static_assertions",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.85"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "shadeable"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rhai = "*"
|
||||
rhai-rand = "0.1"
|
15
example_shaders/simple.shade
Normal file
15
example_shaders/simple.shade
Normal file
|
@ -0,0 +1,15 @@
|
|||
// This gets executed for each pixel on the buffer
|
||||
|
||||
|
||||
|
||||
const VERSION = 0.5;
|
||||
|
||||
|
||||
fn main(){
|
||||
let abcd = RGBA(0, 33, 0, 0);
|
||||
|
||||
// print(rand_float());
|
||||
|
||||
|
||||
PIXEL_RGBA;
|
||||
}
|
19
src/engine_internals.rs
Normal file
19
src/engine_internals.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use crate::pixel_format::{rgba_add, rgba_div, rgba_mult, rgba_sub, RGBA};
|
||||
use rhai_rand::RandomPackage;
|
||||
|
||||
use rhai::{packages::Package, Engine};
|
||||
pub fn engine_startup() -> Engine {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
engine
|
||||
.register_type::<RGBA>()
|
||||
.register_fn("RGBA", RGBA::new)
|
||||
.register_fn("*", rgba_mult)
|
||||
.register_fn("/", rgba_div)
|
||||
.register_fn("+", rgba_add)
|
||||
.register_fn("-", rgba_sub);
|
||||
|
||||
engine.register_global_module(RandomPackage::new().as_shared_module());
|
||||
|
||||
engine
|
||||
}
|
70
src/main.rs
Normal file
70
src/main.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use pixel_format::Rgba64;
|
||||
use rhai::{EvalAltResult, Scope};
|
||||
|
||||
use crate::{
|
||||
engine_internals::engine_startup,
|
||||
pixel_format::{rgba_div, RGBA},
|
||||
};
|
||||
|
||||
pub const SHADER: &str = include_str!("../example_shaders/simple.shade");
|
||||
|
||||
mod engine_internals;
|
||||
pub mod pixel_format;
|
||||
|
||||
fn main() -> Result<(), Box<EvalAltResult>> {
|
||||
let rgba: Rgba64 = 0xffffffff;
|
||||
let rgba_2: Rgba64 = 0x02010101;
|
||||
|
||||
println!("{}", rgba_div(rgba, rgba_2));
|
||||
|
||||
/*
|
||||
pixel_format::get_g(rgba);
|
||||
pixel_format::get_b(rgba);
|
||||
pixel_format::get_a(rgba);
|
||||
*/
|
||||
|
||||
let engine = engine_startup();
|
||||
let ast = engine.compile(SHADER)?;
|
||||
let mut scope = Scope::new();
|
||||
|
||||
let a = vec![
|
||||
RGBA {
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 0,
|
||||
},
|
||||
RGBA {
|
||||
r: 0,
|
||||
g: 255,
|
||||
b: 0,
|
||||
a: 0,
|
||||
},
|
||||
RGBA {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 255,
|
||||
a: 0,
|
||||
},
|
||||
RGBA {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 255,
|
||||
},
|
||||
];
|
||||
|
||||
let mut index = 0;
|
||||
/*
|
||||
for pixel in a {
|
||||
scope.push("PIXEL_X", 0); // add constant variable
|
||||
scope.push("PIXEL_Y", index); // add constant variable
|
||||
scope.push("PIXEL_RGBA", pixel);
|
||||
|
||||
let result: RGBA = engine.call_fn(&mut scope, &ast, "main", ())?;
|
||||
println!("Pixel {}: {:?}", index, result); // prints 42
|
||||
index += 1;
|
||||
}
|
||||
*/
|
||||
Ok(())
|
||||
}
|
112
src/pixel_format.rs
Normal file
112
src/pixel_format.rs
Normal file
|
@ -0,0 +1,112 @@
|
|||
use rhai::INT;
|
||||
|
||||
pub type Rgba64 = u64;
|
||||
|
||||
pub fn get_r(rgba: Rgba64) -> u8 {
|
||||
let x: u64 = rgba;
|
||||
|
||||
let y = x >> 24;
|
||||
|
||||
return (y & 0xff).try_into().unwrap();
|
||||
}
|
||||
pub fn get_g(rgba: Rgba64) -> u8 {
|
||||
let x: u64 = rgba;
|
||||
|
||||
let y = x >> 16;
|
||||
|
||||
return (y & 0xff).try_into().unwrap();
|
||||
}
|
||||
pub fn get_b(rgba: Rgba64) -> u8 {
|
||||
let x: u64 = rgba;
|
||||
|
||||
let y = x >> (24 - 16);
|
||||
|
||||
return (y & 0xff).try_into().unwrap();
|
||||
}
|
||||
pub fn get_a(rgba: Rgba64) -> u8 {
|
||||
let x = rgba;
|
||||
|
||||
let y = x >> (24 - 16 - 8);
|
||||
|
||||
return (y & 0xff).try_into().unwrap();
|
||||
}
|
||||
|
||||
pub fn set_r(rgba: Rgba64, r: u8) -> Rgba64 {
|
||||
let z = (r as Rgba64) << 24;
|
||||
let y = rgba & 0xffffff;
|
||||
return z | y;
|
||||
}
|
||||
|
||||
pub fn set_g(rgba: Rgba64, r: u8) -> Rgba64 {
|
||||
let z = (r as Rgba64) << 16;
|
||||
let y = rgba & 0xffffff;
|
||||
return z | y;
|
||||
}
|
||||
|
||||
pub fn set_b(rgba: Rgba64, r: u8) -> Rgba64 {
|
||||
let z = (r as Rgba64) << 26 - 16;
|
||||
let y = rgba & 0xffffff;
|
||||
return z | y;
|
||||
}
|
||||
|
||||
pub fn set_a(rgba: Rgba64, r: u8) -> Rgba64 {
|
||||
let z = (r as Rgba64) << 8;
|
||||
let y = rgba & 0xffffff;
|
||||
return z | y;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RGBA {
|
||||
pub r: INT,
|
||||
pub g: INT,
|
||||
pub b: INT,
|
||||
pub a: INT,
|
||||
}
|
||||
impl RGBA {
|
||||
pub fn new(r: INT, g: INT, b: INT, a: INT) -> Self {
|
||||
Self { r, g, b, a }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rgba_div(a: Rgba64, b: Rgba64) -> Rgba64 {
|
||||
let mut fin: Rgba64 = 0;
|
||||
|
||||
println!("{}", fin);
|
||||
fin = set_r(fin, get_r(a) / get_r(b));
|
||||
println!("{}", fin);
|
||||
|
||||
fin = set_g(fin, get_g(a) / get_g(b));
|
||||
/*
|
||||
|
||||
get_b(a) / get_b(b);
|
||||
|
||||
get_a(a) / get_a(b);
|
||||
*/
|
||||
|
||||
return fin;
|
||||
}
|
||||
|
||||
pub fn rgba_mult(a: RGBA, b: RGBA) -> RGBA {
|
||||
RGBA {
|
||||
r: a.r * b.r,
|
||||
g: a.g * b.g,
|
||||
b: a.b * b.b,
|
||||
a: a.a * b.a,
|
||||
}
|
||||
}
|
||||
pub fn rgba_add(a: RGBA, b: RGBA) -> RGBA {
|
||||
RGBA {
|
||||
r: a.r + b.r,
|
||||
g: a.g + b.g,
|
||||
b: a.b + b.b,
|
||||
a: a.a + b.a,
|
||||
}
|
||||
}
|
||||
pub fn rgba_sub(a: RGBA, b: RGBA) -> RGBA {
|
||||
RGBA {
|
||||
r: a.r - b.r,
|
||||
g: a.g - b.g,
|
||||
b: a.b - b.b,
|
||||
a: a.a - b.a,
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue