Files
renderbug/src/buffers.rs

214 lines
5.6 KiB
Rust

use crate::geometry::*;
use crate::lib8::interpolate::Fract8Ops;
use crate::power::AsMilliwatts;
use crate::render::{PixelView, Sample, Shader, Surface, Surfaces, HardwarePixel};
use std::fmt::Debug;
use std::rc::Rc;
use std::cell::RefCell;
use std::io;
use std::ops::IndexMut;
#[cfg(feature="threads")]
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct ShaderBinding {
shader: Option<Box<dyn Shader>>,
rect: Rectangle<Virtual>,
opacity: u8
}
#[derive(Clone)]
pub struct BoundSurface<T> {
pub binding: T
}
impl Debug for BoundSurface<Rc<RefCell<ShaderBinding>>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BoundSurface")
.field("shader", &self.binding.borrow().shader)
.field("opacity", &self.binding.borrow().opacity)
.finish()
}
}
pub type SimpleSurface = BoundSurface<Rc<RefCell<ShaderBinding>>>;
impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
fn default() -> Self {
Self {
binding: Rc::new(RefCell::new(ShaderBinding {
shader: None,
rect: Rectangle::everything(),
opacity: 255
})),
}
}
}
impl Surface for BoundSurface<Rc<RefCell<ShaderBinding>>> {
fn rect(&self) -> Rectangle<Virtual> {
self.binding.borrow().rect
}
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
if let Some(ref shader) = self.binding.borrow().shader {
f(shader.as_ref());
}
}
fn set_shader(&mut self, shader: Box<dyn Shader>) {
self.binding.borrow_mut().shader = Some(shader);
}
fn clear_shader(&mut self) {
self.binding.borrow_mut().shader = None;
}
fn set_rect(&mut self, rect: &Rectangle<Virtual>) {
self.binding.borrow_mut().rect = *rect;
}
fn opacity(&self) -> u8 {
self.binding.borrow().opacity
}
fn set_opacity(&mut self, opacity: u8) {
self.binding.borrow_mut().opacity = opacity
}
}
#[cfg(feature="threads")]
pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
#[cfg(feature="threads")]
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
fn default() -> Self {
Self {
binding: Arc::new(Mutex::new(ShaderBinding {
shader: None,
rect: Rectangle::everything(),
opacity: 255
})),
}
}
}
#[cfg(feature="threads")]
impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
fn rect(&self) -> Rectangle<Virtual> {
let r = self.binding.lock().unwrap();
r.rect.clone()
}
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
if let Some(ref shader) = self.binding.lock().unwrap().shader {
f(shader.as_ref());
}
}
fn set_shader(&mut self, shader: Box<dyn Shader>) {
self.binding.lock().unwrap().shader = Some(shader);
}
fn clear_shader(&mut self) {
self.binding.lock().unwrap().shader = None;
}
fn set_rect(&mut self, rect: &Rectangle<Virtual>) {
self.binding.lock().unwrap().rect = rect.clone();
}
fn opacity(&self) -> u8 {
self.binding.lock().unwrap().opacity
}
fn set_opacity(&mut self, opacity: u8) {
self.binding.lock().unwrap().opacity = opacity
}
}
#[cfg(feature="threads")]
impl Debug for BoundSurface<Arc<Mutex<ShaderBinding>>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BoundSurface")
.field("shader", &self.binding.lock().unwrap().shader)
.finish()
}
}
#[derive(Clone)]
pub struct SurfacePool<S: Surface + Default> {
surfaces: Vec<S>
}
impl<S: Surface + Debug> Debug for SurfacePool<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.surfaces.fmt(f)
}
}
impl<S: Surface + Default> SurfacePool<S> {
pub const fn new() -> Self {
Self {
surfaces: Vec::new()
}
}
pub fn iter(&self) -> std::slice::Iter<S> {
self.surfaces.iter()
}
}
impl<S: Surface + Default> Surfaces for SurfacePool<S> {
type Surface = S;
fn new_surface(&mut self, area: &Rectangle<Virtual>) -> Result<S, io::Error> {
let mut surface = S::default();
surface.set_rect(area);
self.surfaces.push(surface.clone());
return Ok(surface);
}
fn render_to<Sampler: Sample>(&self, output: &mut Sampler, frame: usize) {
for surface in self.iter() {
let opacity = surface.opacity();
if opacity > 0 {
let rect = surface.rect();
let mut sample = output.sample(&rect);
surface.with_shader(|shader| {
while let Some((virt_coords, pixel)) = sample.next() {
*pixel = pixel.blend8(shader.draw(&virt_coords, frame).into(), opacity);
}
})
}
}
}
}
pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
type Pixel: HardwarePixel;
fn new() -> Self;
fn blank(&mut self);
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = Self::Pixel> + Send;
fn pixel_count(&self) -> usize;
}
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
type Pixel = T;
fn new() -> Self {
[T::default(); PIXEL_NUM]
}
fn pixel_count(&self) -> usize {
self.len()
}
fn blank(&mut self) {
self.fill(T::default())
}
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item=T> + Send {
self.iter().map(move |x| { x.scale8(brightness)})
}
}