iv/src/renderer.rs
Steins7 8992c06d16 Started major rework of the code
+ added defined code structure
+ started implementing generic outputs
+ added support for multiples windows
+ switched to frame-based architecture
2020-09-01 14:19:44 +02:00

104 lines
2.5 KiB
Rust

#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use std::{
mem::ManuallyDrop,
};
mod gpu;
use gpu::Gpu;
mod swap_system;
use swap_system::SwapSystem;
pub mod output;
use output::Output;
//mod pipeline;
//use pipeline::Pipeline;
//--Renderer implementation-------------------------------------------------------------------------
#[derive(Debug)]
pub struct Renderer<B: gfx_hal::Backend> {
instance: ManuallyDrop<B::Instance>,
gpu: ManuallyDrop<Gpu<B>>,
//pipelines: Vec<Pipeline<B>>,
swap_systems: Vec<SwapSystem<B>>,
}
impl<B> Drop for Renderer<B>
where
B: gfx_hal::Backend,
{
fn drop(&mut self) {
use gfx_hal::device::Device;
debug!("Waiting for device to idle...");
let _ = self.gpu
.device()
.wait_idle();
info!("Dropping Renderer...");
for mut swap_system in self.swap_systems.drain(..) {
swap_system.drop(&mut self.gpu, &self.instance);
}
// for mut pipeline in self.pipelines.drain(..) {
//
// }
unsafe {
ManuallyDrop::drop(&mut self.gpu);
ManuallyDrop::drop(&mut self.instance);
}
trace!("Renderer dropped !");
}
}
impl<B> Renderer<B>
where
B: gfx_hal::Backend,
{
pub fn new<'a, H, T: 'a, I>(outputs: I) -> Result<Renderer<B>, &'static str>
where
H: raw_window_handle::HasRawWindowHandle,
T: Output<H>,
I: IntoIterator<Item = &'a mut T>,
{
use gfx_hal::Instance;
info!("Creating renderer...");
let instance = B::Instance::create("IV", 1)
.map_err(|_| "Could not create instance")?;
let (mut gpu, mut surfaces) = Gpu::new(&instance, outputs)
.map_err(|err| err)?;
let swap_systems = {
surfaces
.drain(..)
.map(|surface| SwapSystem::new(&mut gpu, surface))
.collect::<Result<Vec<_>, &str>>()?
};
debug!("Renderer created !");
Ok( Renderer {
instance: ManuallyDrop::new(instance),
gpu: ManuallyDrop::new(gpu),
swap_systems,
})
}
pub fn draw_clear_frame<T, O>(&mut self, output: &mut O, _color: [f32; 4])
-> Result<(), &'static str>
where
T: raw_window_handle::HasRawWindowHandle,
O: Output<T>
{
let swap_index = output.get_id();
let frame = self.swap_systems[swap_index].acquire_frame();
frame.sync_gpu(&self.gpu);
Ok(())
}
}