rendering works in pure ECS. This is actually pretty sweet. broke compu rendering though

a-star
mitchellhansen 4 years ago
parent 76c75c349b
commit 34b5d7b3d0

@ -9,7 +9,7 @@ use std::any::Any;
use crate::{VertexTypeContainer}; use crate::{VertexTypeContainer};
use winit::event::Event; use winit::event::Event;
use crate::util::tr_event::{TrEvent, TrUIEvent}; use crate::util::tr_event::{TrEvent, TrUIEvent};
use crate::drawables::sprite::{Velocity, Geometry}; use crate::render_system::Position;
enum CustomEvent { enum CustomEvent {
@ -27,14 +27,6 @@ pub trait Drawable {
depth: f32, depth: f32,
) -> Vec<VertexTypeContainer>; ) -> Vec<VertexTypeContainer>;
// Update simply passes the delta time. The implementor doesn't necessarily need to use delta_time
// or even add anything other than a Vec::new() to the function.
fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>>;
// Notify is where custom events created in other parts of the system will be ingested. It
// might be a good idea in the future to have some of pre-function-call filtering so we
// don't have as many notifies that just immediately reject.
fn notify<Y: 'static, T: 'static>(&self, tr_event : Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>>;
} }
@ -60,18 +52,18 @@ impl CanvasFrame {
} }
} }
/// Push this drawable onto the back of the accumulator // /// Push this drawable onto the back of the accumulator
pub fn draw(&mut self, drawable: &dyn Drawable, mv: Velocity, geom: Geometry) { // pub fn draw(&mut self, drawable: &dyn Drawable, pos: Position, geom: Geometry) {
for i in drawable.render( // for i in drawable.render(
self.window_size, // self.window_size,
(mv.pos_x, mv.pos_y), // (mv.pos_x, mv.pos_y),
geom.rotation, // geom.rotation,
(geom.size_x, geom.size_y), // (geom.size_x, geom.size_y),
geom.depth // geom.depth
) { // ) {
self.map.push(i); // self.map.push(i);
} // }
} // }
} }

@ -4,7 +4,7 @@ use crate::canvas::managed::handles::{CompuKernelHandle};
use crate::drawables::compu_sprite::CompuSprite; use crate::drawables::compu_sprite::CompuSprite;
use crate::canvas::canvas_frame::Drawable; use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::VertexTypeContainer; use crate::util::vertex::VertexTypeContainer;
use crate::drawables::sprite::{Velocity, Geometry}; use crate::render_system::{Images, Geometry};
#[derive(Default)] #[derive(Default)]
pub struct CompuFrame { pub struct CompuFrame {
@ -57,22 +57,23 @@ impl CompuFrame {
pub fn add_with_image_swap(&mut self, pub fn add_with_image_swap(&mut self,
buffer: Arc<CompuBufferHandle>, buffer: Arc<CompuBufferHandle>,
kernel: Arc<CompuKernelHandle>, kernel: Arc<CompuKernelHandle>,
sprite: &CompuSprite, images: &Images,
mv: Velocity,
geom: Geometry, geom: Geometry,
) { ) {
let compu_sprites = sprite.render(
self.window_size,
(mv.pos_x, mv.pos_y),
geom.rotation,
(geom.size_x, geom.size_y),
geom.depth,
);
if compu_sprites.len() == 1 { // TODO fix this
if let VertexTypeContainer::ImageType(a, b) = compu_sprites.first().unwrap() { // let compu_sprites = sprite.render(
self.swapped_to_image.push((buffer, b.clone(), kernel)) // self.window_size,
}; // (mv.pos_x, mv.pos_y),
} // geom.rotation,
// (geom.size_x, geom.size_y),
// geom.depth,
// );
//
// if compu_sprites.len() == 1 {
// if let VertexTypeContainer::ImageType(a, b) = compu_sprites.first().unwrap() {
// self.swapped_to_image.push((buffer, b.clone(), kernel))
// };
// }
} }
} }

@ -0,0 +1,61 @@
use std::sync::Arc;
use specs::{Component, Join, System, VecStorage, Write, WriteStorage};
use crate::canvas::canvas_frame::CanvasFrame;
use crate::canvas::compu_frame::CompuFrame;
use crate::canvas::managed::handles::{CanvasImageHandle, CanvasTextureHandle, CompuBufferHandle, CompuKernelHandle};
use crate::PersistentState;
use crate::render_system::{Geometry, Images, Position, Textures};
use crate::util::vertex::{ImageVertex3D, TextureVertex3D, VertexTypeContainer};
use crate::vkprocessor::VkProcessor;
pub struct Compu {
pub kernels: Vec<Arc<CompuKernelHandle>>,
pub buffers: Vec<Arc<CompuBufferHandle>>,
}
impl Component for Compu {
type Storage = VecStorage<Self>;
}
pub struct CompuSystem;
impl<'a> System<'a> for CompuSystem {
type SystemData = (
WriteStorage<'a, Compu>,
WriteStorage<'a, Geometry>,
WriteStorage<'a, Images>,
Write<'a, PersistentState>, // delta_time, window size, etc.
Write<'a, VkProcessor>, // Renderer
);
fn run(&mut self, (
mut compu_list,
mut geom_list,
mut image_list,
mut state,
mut vk_processor
): Self::SystemData) {
state.compu_frame = CompuFrame::new(state.window_size);
// compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
for (compute_item, geom, image) in (&mut compu_list, &mut geom_list, &mut image_list).join() {
state.compu_frame.add_with_image_swap(
compute_item.buffers.get(0).unwrap().clone(),
compute_item.kernels.get(0).unwrap().clone(),
image,
geom.clone(),
);
}
for (compute_item) in (&mut compu_list).join() {
state.compu_frame.add(
compute_item.buffers.get(0).unwrap().clone(),
compute_item.kernels.get(0).unwrap().clone(),
);
}
}
}

@ -64,11 +64,11 @@ impl Drawable for CompuSprite {
vec![self.verts.clone()] vec![self.verts.clone()]
} }
fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> { // fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
//
fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> { // fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
} }

@ -72,13 +72,13 @@ impl Drawable for Polygon {
vec![self.verts.clone()] vec![self.verts.clone()]
} }
fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> { // fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
//
fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> { // fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
} }

@ -88,11 +88,11 @@ impl Drawable for Rect {
] ]
} }
fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> { // fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
//
fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> { // fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
} }

@ -104,28 +104,28 @@ impl Drawable for Slider {
vertices vertices
} }
fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> { // fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
//
fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> { // fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> {
match tr_event { // match tr_event {
TrEvent::WindowEvent { event: TrWindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { // TrEvent::WindowEvent { event: TrWindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => {
//
//
match button { // match button {
MouseButton::Left => { // MouseButton::Left => {
if *state == ElementState::Pressed { // if *state == ElementState::Pressed {
self.position.0 += 30.0; // self.position.0 += 30.0;
self.value += 10; // self.value += 10;
self.update() // self.update()
} // }
} // }
_ => {} // _ => {}
} // }
} // }
_ => {} // _ => {}
} // }
Vec::new() // Vec::new()
} // }
} }

@ -6,26 +6,12 @@ use crate::util::vertex::{VertexTypeContainer, TextureVertex3D, Vertex3D};
use winit::event::{DeviceEvent, MouseButton, ElementState, Event, WindowEvent}; use winit::event::{DeviceEvent, MouseButton, ElementState, Event, WindowEvent};
use crate::util::tr_event::{TrEvent, TrWindowEvent, TrUIEvent}; use crate::util::tr_event::{TrEvent, TrWindowEvent, TrUIEvent};
pub struct Geometry {
pos_x: f32,
pos_y: f32,
size_x: f32,
size_y: f32,
rotation: f32,
depth: f32,
}
pub struct Velocity {
vel_x: f32,
vel_y: f32,
vel_r: f32,
}
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Sprite { pub struct Sprite {
texture_handle: Arc<CanvasTextureHandle>, texture_handle: Arc<CanvasTextureHandle>,
geometry: Geometry,
velocity: Velocity,
} }
/// Container class which implements drawable. /// Container class which implements drawable.
@ -78,19 +64,7 @@ impl Sprite {
pub fn new(texture_handle: Arc<CanvasTextureHandle>) -> Sprite { pub fn new(texture_handle: Arc<CanvasTextureHandle>) -> Sprite {
Sprite { Sprite {
texture_handle: texture_handle.clone(), texture_handle: texture_handle.clone(),
geometry: Geometry {
pos_x: 0.0,
pos_y: 0.0,
size_x: 0.0,
size_y: 0.0,
rotation: 0.0,
depth: 0.0
},
velocity: Velocity {
vel_x: 0.0,
vel_y: 0.0,
vel_r: 0.0
}
} }
} }
} }
@ -112,25 +86,25 @@ impl Drawable for Sprite {
self.texture_handle.clone()) self.texture_handle.clone())
] ]
} }
//
fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> { // fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
//
fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> { // fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> {
match tr_event { // match tr_event {
TrEvent::WindowEvent { event: TrWindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { // TrEvent::WindowEvent { event: TrWindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => {
match button { // match button {
MouseButton::Left => { // MouseButton::Left => {
if *state == ElementState::Pressed { // if *state == ElementState::Pressed {
//
} // }
} // }
_ => {} // _ => {}
} // }
} // }
_ => {} // _ => {}
} // }
Vec::new() // Vec::new()
} // }
} }

@ -148,11 +148,11 @@ impl Drawable for Text {
vec![self.verts.clone()] vec![self.verts.clone()]
} }
fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> { // fn update<T>(&self, delta_time: f32) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
//
fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> { // fn notify<Y, T>(&self, tr_event: Vec<TrEvent<Y>>, ui_events: Vec<TrUIEvent<T>>) -> Vec<TrUIEvent<T>> {
Vec::new() // Vec::new()
} // }
} }

@ -8,14 +8,17 @@ extern crate hprof;
extern crate image; extern crate image;
extern crate nalgebra as na; extern crate nalgebra as na;
extern crate rand; extern crate rand;
extern crate specs;
extern crate time; extern crate time;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use gilrs::{Button, Event as GilEvent, Gamepad, GamepadId, Gilrs}; use gilrs::{Button, Event as GilEvent, Gamepad, GamepadId, Gilrs};
use specs::prelude::*;
use vulkano::instance::debug::DebugCallback; use vulkano::instance::debug::DebugCallback;
use vulkano::instance::Instance; use vulkano::instance::Instance;
use vulkano::swapchain::Surface;
use vulkano::sync; use vulkano::sync;
use vulkano::sync::GpuFuture; use vulkano::sync::GpuFuture;
use vulkano_win::VkSurfaceBuild; use vulkano_win::VkSurfaceBuild;
@ -23,44 +26,37 @@ use winit::dpi::LogicalSize;
use winit::event::{DeviceEvent, ElementState, Event, MouseButton, StartCause, VirtualKeyCode, WindowEvent}; use winit::event::{DeviceEvent, ElementState, Event, MouseButton, StartCause, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy}; use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy};
use winit::platform::unix::WindowBuilderExtUnix; use winit::platform::unix::WindowBuilderExtUnix;
use winit::window::{WindowBuilder, Window}; use winit::window::{Window, WindowBuilder};
use canvas::compu_frame::CompuFrame;
use crate::canvas::canvas_frame::{CanvasFrame, Drawable}; use crate::canvas::canvas_frame::{CanvasFrame, Drawable};
use crate::canvas::canvas_state::CanvasState; use crate::canvas::canvas_state::CanvasState;
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasTextureHandle, Handle}; use crate::canvas::managed::handles::{CanvasFontHandle, CanvasTextureHandle, Handle};
use canvas::compu_frame::CompuFrame;
use crate::canvas::managed::handles::{CompuBufferHandle, CompuKernelHandle}; use crate::canvas::managed::handles::{CompuBufferHandle, CompuKernelHandle};
use crate::drawables::compu_sprite::CompuSprite; use crate::drawables::compu_sprite::CompuSprite;
use crate::drawables::rect::Rect; use crate::drawables::rect::Rect;
use crate::drawables::slider::Slider;
use crate::drawables::sprite::Sprite; use crate::drawables::sprite::Sprite;
use crate::drawables::text::Text; use crate::drawables::text::Text;
use crate::render_system::{Geometry, Images, Position, Render, RenderSystem, Textures};
use crate::util::load_raw; use crate::util::load_raw;
use crate::util::timer::Timer; use crate::util::timer::Timer;
use crate::util::tr_event::{TrEventExtension, TrEvent}; use crate::util::tr_event::{TrEvent, TrEventExtension};
use crate::util::vertex::{TextureVertex3D, VertexTypeContainer}; use crate::util::vertex::{TextureVertex3D, VertexTypeContainer};
use crate::vkprocessor::VkProcessor; use crate::vkprocessor::VkProcessor;
use crate::drawables::slider::Slider; use crate::compu_system::{CompuSystem, Compu};
pub mod util; pub mod util;
pub mod vkprocessor; pub mod vkprocessor;
pub mod drawables; pub mod drawables;
pub mod canvas; pub mod canvas;
pub mod render_system;
pub mod compu_system;
extern crate specs;
use specs::prelude::*;
use vulkano::swapchain::Surface;
pub struct Render {
render_actor: Box<dyn Drawable + Sync + Send>,
}
impl Component for Render {
type Storage = VecStorage<Self>;
}
#[derive(Default)] #[derive(Default)]
struct PersistentState { pub struct PersistentState {
surface: Option<Arc<Surface<Window>>>, surface: Option<Arc<Surface<Window>>>,
window_size: (u32, u32), window_size: (u32, u32),
delta_time: f32, delta_time: f32,
@ -68,47 +64,6 @@ struct PersistentState {
compu_frame: CompuFrame, compu_frame: CompuFrame,
} }
struct RenderSystem;
impl<'a> System<'a> for RenderSystem {
type SystemData = (
WriteStorage<'a, Render>, // generates the vertices
Write<'a, PersistentState>, // delta_time, window size, etc.
Write<'a, VkProcessor>, // Renderer
);
fn run(&mut self, (mut mv, mut draw, mut state, mut vk_processor): Self::SystemData) {
state.canvas_frame = CanvasFrame::new(state.window_size);
state.compu_frame = CompuFrame::new(state.window_size);
// compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
for (mv, geom, draw) in (&mut mv, &mut draw).join() {
geom.pos_x += mv.vel_x * state.delta_time;
geom.pos_y += mv.vel_y * state.delta_time;
let window_size = state.window_size.clone();
state.canvas_frame.add(draw.0.get(
window_size,
(geom.pos_x, geom.pos_y),
geom.rotation,
(geom.size_x, geom.size_y),
geom.depth,
));
}
for draw_data in (&draw).join() {
let size = state.window_size.clone();
}
vk_processor.run(&state.surface.clone().unwrap(),
&state.canvas_frame,
&state.compu_frame);
}
}
struct EventSystem; struct EventSystem;
impl<'a> System<'a> for EventSystem { impl<'a> System<'a> for EventSystem {
@ -128,13 +83,11 @@ impl<'a> System<'a> for EventSystem {
} }
} }
struct Eventt(u32);
pub fn main() { pub fn main() {
//hprof::start_frame(); //hprof::start_frame();
//let g = hprof::enter("vulkan preload"); //let g = hprof::enter("vulkan preload");
let instance = { let instance = {
let extensions = vulkano_win::required_extensions(); let extensions = vulkano_win::required_extensions();
Instance::new(None, &extensions, None).unwrap() Instance::new(None, &extensions, None).unwrap()
@ -174,11 +127,6 @@ pub fn main() {
let image_dimensions_u: (u32, u32) = image_data.1; let image_dimensions_u: (u32, u32) = image_data.1;
let compu_sprite1: CompuSprite =
CompuSprite::new((-1.0, -1.0), (1.0, 1.0), 0, image_dimensions_f,
// Swap image to render the result to. Must match dimensions
processor.new_swap_image(image_dimensions_u));
let compute_buffer: Arc<CompuBufferHandle> = let compute_buffer: Arc<CompuBufferHandle> =
processor.new_compute_buffer(image_data.0.clone(), image_data.1, 4); processor.new_compute_buffer(image_data.0.clone(), image_data.1, 4);
@ -189,6 +137,8 @@ pub fn main() {
processor.get_kernel_handle(String::from("simple-edge.compute")) processor.get_kernel_handle(String::from("simple-edge.compute"))
.expect("Can't find that kernel"); .expect("Can't find that kernel");
let compu_image = processor.new_swap_image(image_dimensions_u);
// Get the handles for the assets // Get the handles for the assets
let funky_handle: Arc<CanvasTextureHandle> = let funky_handle: Arc<CanvasTextureHandle> =
processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap(); processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap();
@ -197,6 +147,11 @@ pub fn main() {
let mut world = World::new(); let mut world = World::new();
world.register::<Render>(); world.register::<Render>();
world.register::<Compu>();
world.register::<Position>();
world.register::<Geometry>();
world.register::<Textures>();
world.register::<Images>();
world.insert::<VkProcessor>(processor); world.insert::<VkProcessor>(processor);
world.insert::<Vec<TrEvent<TrEventExtension>>>(Vec::new()); world.insert::<Vec<TrEvent<TrEventExtension>>>(Vec::new());
world.insert::<PersistentState>(PersistentState { world.insert::<PersistentState>(PersistentState {
@ -207,26 +162,27 @@ pub fn main() {
compu_frame: CompuFrame::new((0, 0)), compu_frame: CompuFrame::new((0, 0)),
}); });
world.create_entity()
let thing = Box::new(Sprite::new(funky_handle.clone())); .with(Render { vertices: vec![] })// just a drawable
.with(Position { x: 0.0, y: 0.0, z: 0 })
// An entity may or may not contain some component. .with(Geometry { size_x: 300.0, size_y: 300.0, rotation: 0.0 })
let t = world.create_entity() .with(Textures { textures: vec![funky_handle] })
.with(Render{ render_actor: thing })// just a drawable
.build(); .build();
let thing2 = Box::new(Slider::new((300.0, 50.0), (550.0, 100.0), 30000));
world.create_entity() world.create_entity()
.with(Render{ render_actor: thing2 })// just a drawable .with(Render { vertices: vec![] })// just a drawable
.with(Position { x: 600.0, y: 500.0, z: 0 })
.with(Geometry { size_x: 600.0, size_y: 600.0, rotation: 0.0 })
.with(Images { images: vec![compu_image], image_resolutions: vec![image_dimensions_u] })
.build(); .build();
// call the run method for the following systems & deps // call the run method for the following systems & deps
let mut dispatcher = DispatcherBuilder::new() let mut dispatcher = DispatcherBuilder::new()
// .with(SysA, "sys_a", &[]) // .with(SysA, "sys_a", &[])
.with(EventSystem, "event_s", &[]) .with(EventSystem, "event_s", &[])
.with(RenderSystem, "render_s", &["event_s"]).build(); .with(CompuSystem, "compu_s", &["event_s"])
.with(RenderSystem, "render_s", &["event_s", "compu_s"]).build();
let event_loop_proxy = events_loop.create_proxy(); let event_loop_proxy = events_loop.create_proxy();

@ -0,0 +1,232 @@
use specs::{Component, Join, System, VecStorage, Write, WriteStorage};
use crate::canvas::canvas_frame::CanvasFrame;
use crate::canvas::compu_frame::CompuFrame;
use crate::canvas::managed::handles::{CanvasImageHandle, CanvasTextureHandle};
use crate::PersistentState;
use crate::util::vertex::{TextureVertex3D, VertexTypeContainer, ImageVertex3D};
use crate::vkprocessor::VkProcessor;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct Position {
pub x: f32,
pub y: f32,
pub z: u8,
}
impl Component for Position {
type Storage = VecStorage<Self>;
}
#[derive(Debug, Clone)]
pub struct Geometry {
pub size_x: f32,
pub size_y: f32,
pub rotation: f32,
}
impl Component for Geometry {
type Storage = VecStorage<Self>;
}
#[derive(Debug, Clone)]
pub struct Render {
pub vertices: Vec<VertexTypeContainer>,
}
impl Component for Render {
type Storage = VecStorage<Self>;
}
#[derive(Debug, Clone)]
pub struct Textures {
pub textures: Vec<Arc<CanvasTextureHandle>>,
}
impl Component for Textures {
type Storage = VecStorage<Self>;
}
#[derive(Debug, Clone)]
pub struct Images {
pub images: Vec<Arc<CanvasImageHandle>>,
pub image_resolutions: Vec<(u32, u32)>,
}
impl Component for Images {
type Storage = VecStorage<Self>;
}
pub struct RenderSystem;
impl<'a> System<'a> for RenderSystem {
type SystemData = (
WriteStorage<'a, Render>,
WriteStorage<'a, Position>,
WriteStorage<'a, Geometry>,
WriteStorage<'a, Textures>,
WriteStorage<'a, Images>,
Write<'a, PersistentState>, // delta_time, window size, etc.
Write<'a, VkProcessor>, // Renderer
);
fn run(&mut self, (
mut vertices_list,
mut pos_list,
mut geom_list,
mut textures_list,
mut images_list,
mut state,
mut vk_processor
): Self::SystemData) {
state.canvas_frame = CanvasFrame::new(state.window_size);
state.compu_frame = CompuFrame::new(state.window_size);
// compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
for (vertices, position, geometry, textures) in (&mut vertices_list, &mut pos_list, &mut geom_list, &mut textures_list).join() {
// geom.pos_x += mv.vel_x * state.delta_time;
// geom.pos_y += mv.vel_y * state.delta_time;
let window_size = state.window_size.clone();
let pos = (position.x, position.y);
let size = (geometry.size_x, geometry.size_y);
let normalized_depth = position.z as f32 / 255.0;
let textured_vertices = vec![
VertexTypeContainer::TextureType(
generate_textured_verts(window_size, pos, size, normalized_depth),
textures.textures.get(0).unwrap().clone()
)
];
state.canvas_frame.add(textured_vertices);
}
for (vertices, position, geometry, images) in (&mut vertices_list, &mut pos_list, &mut geom_list, &mut images_list).join() {
// geom.pos_x += mv.vel_x * state.delta_time;
// geom.pos_y += mv.vel_y * state.delta_time;
let window_size = state.window_size.clone();
let pos = (position.x, position.y);
let size = (geometry.size_x, geometry.size_y);
let normalized_depth = position.z as f32 / 255.0;
let textured_vertices = vec![
VertexTypeContainer::ImageType(
generate_image_verts(window_size, pos, size, images.image_resolutions.get(0).unwrap().clone(), normalized_depth),
images.images.get(0).unwrap().clone()
)
];
state.canvas_frame.add(textured_vertices);
}
vk_processor.run(&state.surface.clone().unwrap(),
&state.canvas_frame,
&state.compu_frame);
}
}
fn generate_image_verts(
window_size: (u32, u32),
position: (f32, f32),
size: (f32, f32),
image_size: (u32, u32),
depth: f32,
) -> Vec<ImageVertex3D> {
let image_size = (image_size.0 as f32, image_size.1 as f32);
// screen space position
let ss_position = (
position.0 / window_size.0 as f32 - 1.0,
position.1 / window_size.1 as f32 - 1.0
);
// screen space size
let ss_size = (
size.0 / window_size.0 as f32,
size.1 / window_size.1 as f32
);
// pub fn new(position: (f32, f32),
// size: (f32, f32),
// depth: u32,
// image_size: (f32, f32),
// image_handle: Arc<CanvasImageHandle>) -> CompuSprite {
vec![
ImageVertex3D {
v_position: [ss_position.0, ss_position.1, depth], // top left
ti_position: [-0.0, -0.0]
},
ImageVertex3D {
v_position: [ss_position.0, ss_position.1 + ss_size.1, depth], // bottom left
ti_position: [-0.0, image_size.1]
},
ImageVertex3D {
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
ti_position: [image_size.0, image_size.1]
},
ImageVertex3D {
v_position: [ss_position.0, ss_position.1, depth], // top left
ti_position: [-0.0, -0.0]
},
ImageVertex3D {
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
ti_position: [image_size.0, image_size.1]
},
ImageVertex3D {
v_position: [ss_position.0 + ss_size.0, ss_position.1, depth], // top right
ti_position: [image_size.0, -0.0]
},
]
}
fn generate_textured_verts(
window_size: (u32, u32),
position: (f32, f32),
size: (f32, f32),
depth: f32,
) -> Vec<TextureVertex3D> {
let ss_position = (
position.0 / window_size.0 as f32 - 1.0,
position.1 / window_size.1 as f32 - 1.0
);
let ss_size = (
size.0 / window_size.0 as f32,
size.1 / window_size.1 as f32
);
vec![
TextureVertex3D {
v_position: [ss_position.0, ss_position.1, depth], // top left
ti_position: [-0.0, -0.0],
},
TextureVertex3D {
v_position: [ss_position.0, ss_position.1 + ss_size.1, depth], // bottom left
ti_position: [-0.0, 1.0],
},
TextureVertex3D {
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
ti_position: [1.0, 1.0],
},
TextureVertex3D {
v_position: [ss_position.0, ss_position.1, depth], // top left
ti_position: [-0.0, -0.0],
},
TextureVertex3D {
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
ti_position: [1.0, 1.0],
},
TextureVertex3D {
v_position: [ss_position.0 + ss_size.0, ss_position.1, depth], // top right
ti_position: [1.0, -0.0],
},
]
}
Loading…
Cancel
Save