ableos/shadeable/src/lib.rs
2022-01-18 06:15:51 -06:00

82 lines
1.7 KiB
Rust

#![no_std]
extern crate alloc;
use alloc::{boxed::Box, vec, vec::Vec};
use engine_internals::engine_startup;
use pixel_format::{Rgba64, RGBA};
// use core::fmt::Result;
use core::result::Result;
use rhai::{EvalAltResult, Scope};
mod engine_internals;
pub mod pixel_format;
pub const SHADER: &str = include_str!("../shaders/simple.shade");
pub fn rhai_test() -> Result<Vec<RGBA>, Box<EvalAltResult>>
// ^^^^^^^^^^^^^^^^^^
// Rhai API error type
{
// Create an 'Engine'
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 result_rgba = vec![];
for pixel in a {
/*
scope.push("PIXEL_X", 0);
scope.push("PIXEL_Y", index);
*/
scope.push("PIXEL_RGBA", pixel);
let result: RGBA = engine.call_fn(&mut scope, &ast, "main", ())?;
result_rgba.push(result);
}
// Done!
Ok(result_rgba)
}
pub fn evaluate_shader(pixel: u8) -> Result<Rgba64, Box<EvalAltResult>> {
let engine = engine_startup();
let ast = engine.compile(SHADER)?;
let mut scope = Scope::new();
scope.push("PIXEL_RGBA", pixel);
let result: RGBA = engine.call_fn(&mut scope, &ast, "main", ())?;
Ok(8)
}