ableos/shadeable/src/lib.rs

82 lines
1.7 KiB
Rust
Raw Normal View History

2022-01-16 19:42:11 -06:00
#![no_std]
extern crate alloc;
use alloc::{boxed::Box, vec, vec::Vec};
use engine_internals::engine_startup;
2022-01-18 06:15:51 -06:00
use pixel_format::{Rgba64, RGBA};
2022-01-16 19:42:11 -06:00
// use core::fmt::Result;
use core::result::Result;
use rhai::{EvalAltResult, Scope};
mod engine_internals;
2022-01-18 06:15:51 -06:00
pub mod pixel_format;
2022-01-16 19:42:11 -06:00
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)
}
2022-01-18 06:15:51 -06:00
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)
}