diff --git a/notes/MakingAnActualThing.txt b/notes/MakingAnActualThing.txt index cc205e45..79a24c7f 100644 --- a/notes/MakingAnActualThing.txt +++ b/notes/MakingAnActualThing.txt @@ -11,16 +11,23 @@ So, I need to figure out how to determine which objects will : * Get batched * And initialized -The best candidate that I have right now is some sort of "scene" setup. Maybe something like -pub struct Scene { - - drawables_bucket - notifiable_bucket -} +Looks like it could possibly be specs... + +Sprites currently are just a container for the meta-information needed in order to return a VertexTypeContainer. -impl Scene { - pub fn init() -> Scene {} - pub fn get_drawable() -> Maybe iterator of drawables? - pub fn get_notifiable() - +{{{code: lang="rust" linenumbers="True" +pub enum VertexTypeContainer { + TextureType(Vec, Arc), + ImageType(Vec, Arc), + ColorType(Vec), + ThreeDType(Vec), + TextType(Vec), } +}}} + + +So for a sprite, which is a generic texture and position/size combo +Images are similar, but instead of a "sprite" I made a computsprite because that's the only thing that uses them. + +Now if I had to shove these into a component / a set of components... I could have a component of the vertex type even? diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index 05545fac..bb851b8d 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -6,12 +6,12 @@ use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, Handle}; use vulkano::pipeline::vertex::Vertex; use std::any::Any; -use crate::VertexType; +use crate::VertexTypeContainer; use winit::event::Event; /// Trait which may be inherited by objects that wish to be drawn to the screen pub trait Drawable { - fn get(&self, window_size: (u32, u32)) -> Vec; + fn get(&self, window_size: (u32, u32)) -> Vec; } /// Trait which may be inherited by objects that wish to receive events @@ -27,7 +27,7 @@ pub trait Updatable { /// Accumulator for Vectors of VertexTypes #[derive(Default)] pub struct CanvasFrame { - pub map: Vec, + pub map: Vec, window_size: (u32, u32), } diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index 3c28c5a9..d8e72467 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -31,7 +31,7 @@ use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, Ca use crate::canvas::managed::gpu_buffers::{CanvasImage, CanvasTexture, CanvasFont}; use crate::canvas::managed::shader::shader_common::CompiledShader; use crate::canvas::managed::shader::generic_shader::GenericShader; -use crate::VertexType; +use crate::VertexTypeContainer; use crate::util::vertex::{TextVertex3D, TextureVertex3D, ImageVertex3D, ColorVertex3D, CanvasFrameAllocation}; use shade_runner::Input; use winit::window::Window; @@ -52,7 +52,7 @@ pub struct CanvasState { font_buffers: Vec>, /// Compiled Graphics pipelines have a handle which self describe their position in this vector - shader_buffers: Vec>>, + shader_buffers: Vec>>, /// Looks like we gotta hold onto the queue for managing textures queue: Arc, @@ -254,15 +254,16 @@ impl CanvasState { /// Load and Compile a shader with the filename at resources/shaders /// Takes physical and capabilities as we don't store that in Canvas - pub fn load_shader(&mut self, + pub fn load_shader(&mut self, filename: String, capabilities: Capabilities) -> Option> - where T: CompiledShader, V: Vertex { + where T: CompiledShader + Send + Sync + 'static, V: Vertex { + let handle = Arc::new(CompiledShaderHandle { handle: self.shader_buffers.len() as u32 }); - let shader: Box = Box::new(T::new::( + let shader: Box = Box::new(T::new::( filename.clone(), self.device.clone(), handle.clone(), @@ -396,19 +397,19 @@ impl CanvasState { // separate the mux of vertex containers back out for value in &canvas_frame.map { match value { - VertexType::TextureType(vertices, handle) => { + VertexTypeContainer::TextureType(vertices, handle) => { textured_vertex_buffer.entry(handle.clone()).or_insert(vertices.clone()).extend(vertices); } - VertexType::ImageType(vertices, handle) => { + VertexTypeContainer::ImageType(vertices, handle) => { image_vertex_buffer.entry(handle.clone()).or_insert(vertices.clone()).extend(vertices); } - VertexType::ColorType(vertices) => { + VertexTypeContainer::ColorType(vertices) => { colored_vertex_buffer.extend(vertices); } - VertexType::ThreeDType(vertices) => { + VertexTypeContainer::ThreeDType(vertices) => { } - VertexType::TextType(vertices) => { + VertexTypeContainer::TextType(vertices) => { text_vertex_buffer.extend(vertices); } }; diff --git a/src/canvas/managed/shader/generic_shader.rs b/src/canvas/managed/shader/generic_shader.rs index 9e156daf..62eb5138 100644 --- a/src/canvas/managed/shader/generic_shader.rs +++ b/src/canvas/managed/shader/generic_shader.rs @@ -18,7 +18,7 @@ use crate::canvas::managed::shader::shader_common::{ShaderType, CompiledShaderRe use crate::canvas::managed::handles::CompiledShaderHandle; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::ShaderSpecializationConstants; -use crate::util::vertex::{VertexType, ColorVertex3D}; +use crate::util::vertex::{VertexTypeContainer, ColorVertex3D}; /// CanvasShader holds the pipeline and render pass for the input shader source #[derive(Clone)] diff --git a/src/canvas/managed/shader/shader_common.rs b/src/canvas/managed/shader/shader_common.rs index 14c5a8f1..1909c8b4 100644 --- a/src/canvas/managed/shader/shader_common.rs +++ b/src/canvas/managed/shader/shader_common.rs @@ -98,19 +98,18 @@ pub trait CompiledShaderResources { } } - - pub trait CompiledShader { - fn new(filename: String, + fn new(filename: String, device: Arc, handle: Arc, - render_pass: Arc) -> Self where Self: Sized, V: Vertex,; + render_pass: Arc) + -> Self where Self: Sized + Send + Sync; fn get_name(&self) -> String; fn get_handle(&self) -> Arc; fn get_pipeline(&self) -> Arc; fn get_renderpass(&self) -> Arc; - fn recompile(self, render_pass: Arc) - -> Self where Self: Sized; + fn recompile(self, render_pass: Arc) + -> Self where Self: Sized + Send; } /// Legacy ShaderType enum for single type shaders. diff --git a/src/compute/compu_frame.rs b/src/compute/compu_frame.rs index 0c679278..9c1815f2 100644 --- a/src/compute/compu_frame.rs +++ b/src/compute/compu_frame.rs @@ -3,7 +3,7 @@ use crate::canvas::managed::handles::{CanvasImageHandle}; use crate::compute::managed::handles::{CompuKernelHandle, CompuBufferHandle}; use crate::drawables::compu_sprite::CompuSprite; use crate::canvas::canvas_frame::Drawable; -use crate::util::vertex::VertexType; +use crate::util::vertex::VertexTypeContainer; pub struct CompuFrame { // Vec<(Buffer, Kernel)> @@ -60,7 +60,7 @@ impl CompuFrame { let compu_sprites = sprite.get(self.window_size); if compu_sprites.len() == 1 { - if let VertexType::ImageType(a, b) = compu_sprites.first().unwrap() { + if let VertexTypeContainer::ImageType(a, b) = compu_sprites.first().unwrap() { self.swapped_to_image.push((buffer, b.clone(), kernel)) }; } diff --git a/src/drawables/compu_sprite.rs b/src/drawables/compu_sprite.rs index 5baeb103..56f490bb 100644 --- a/src/drawables/compu_sprite.rs +++ b/src/drawables/compu_sprite.rs @@ -1,11 +1,11 @@ use std::sync::Arc; use crate::canvas::managed::handles::{CanvasImageHandle, CanvasTextureHandle}; use crate::canvas::canvas_frame::Drawable; -use crate::util::vertex::{VertexType, ImageVertex3D}; +use crate::util::vertex::{VertexTypeContainer, ImageVertex3D}; pub struct CompuSprite { - pub verts: VertexType, + pub verts: VertexTypeContainer, position: (f32, f32), size: (f32, f32), @@ -44,7 +44,7 @@ impl CompuSprite { ]; CompuSprite { - verts: VertexType::ImageType(verts, image_handle.clone()), + verts: VertexTypeContainer::ImageType(verts, image_handle.clone()), position: position, size: size, color: (0.0, 0.0, 0.0, 0.0), @@ -53,7 +53,7 @@ impl CompuSprite { } impl Drawable for CompuSprite { - fn get(&self, window_size: (u32, u32)) -> Vec { + fn get(&self, window_size: (u32, u32)) -> Vec { vec![self.verts.clone()] } } \ No newline at end of file diff --git a/src/drawables/polygon.rs b/src/drawables/polygon.rs index ba4d80ce..c62d9178 100644 --- a/src/drawables/polygon.rs +++ b/src/drawables/polygon.rs @@ -2,14 +2,14 @@ use std::sync::Arc; use crate::canvas::*; use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle}; use crate::canvas::canvas_frame::{Drawable}; -use crate::util::vertex::{VertexType, TextureVertex3D, Vertex3D, ColorVertex3D}; +use crate::util::vertex::{VertexTypeContainer, TextureVertex3D, Vertex3D, ColorVertex3D}; use crate::drawables::sprite::Sprite; /// Convex multi verticy polygon #[derive(Debug, Clone)] pub struct Polygon { - pub verts: VertexType, + pub verts: VertexTypeContainer, position: (f32, f32), size: (f32, f32), @@ -54,14 +54,14 @@ impl Polygon { Polygon { - verts: VertexType::ColorType(verts), + verts: VertexTypeContainer::ColorType(verts), position: position, size: size, } } } impl Drawable for Polygon { - fn get(&self, window_size: (u32, u32)) -> Vec { + fn get(&self, window_size: (u32, u32)) -> Vec { vec![self.verts.clone()] } diff --git a/src/drawables/rect.rs b/src/drawables/rect.rs index 9503ba3c..fb8b4c78 100644 --- a/src/drawables/rect.rs +++ b/src/drawables/rect.rs @@ -1,5 +1,5 @@ use crate::canvas::canvas_frame::Drawable; -use crate::util::vertex::{VertexType, ColorVertex3D}; +use crate::util::vertex::{VertexTypeContainer, ColorVertex3D}; /// #[derive(Debug, Clone)] @@ -73,9 +73,9 @@ impl Rect { } impl Drawable for Rect { - fn get(&self, window_size: (u32, u32)) -> Vec { + fn get(&self, window_size: (u32, u32)) -> Vec { vec![ - VertexType::ColorType( + VertexTypeContainer::ColorType( Rect::generate_vertices(window_size, self.position, self.size, self.depth, self.color) ) ] diff --git a/src/drawables/slider.rs b/src/drawables/slider.rs index d4416c03..cd59f060 100644 --- a/src/drawables/slider.rs +++ b/src/drawables/slider.rs @@ -5,7 +5,7 @@ use winit::event::Event; use crate::canvas::canvas_frame::{Drawable, Eventable}; use crate::drawables::rect::Rect; use crate::drawables::sprite::Sprite; -use crate::util::vertex::VertexType; +use crate::util::vertex::VertexTypeContainer; pub struct Slider { handle: Rect, @@ -45,14 +45,14 @@ impl Slider { } impl Drawable for Slider { - fn get(&self, window_size: (u32, u32)) -> Vec { + fn get(&self, window_size: (u32, u32)) -> Vec { let mut vertices = self.handle.get(window_size).clone(); vertices.extend_from_slice( self.guide.iter() .map(|x| x.get(window_size)) .flatten() - .collect::>() + .collect::>() .as_slice() ); diff --git a/src/drawables/sprite.rs b/src/drawables/sprite.rs index 644e8297..223ba00e 100644 --- a/src/drawables/sprite.rs +++ b/src/drawables/sprite.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use crate::canvas::*; use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle}; use crate::canvas::canvas_frame::{Drawable, Eventable, Updatable}; -use crate::util::vertex::{VertexType, TextureVertex3D, Vertex3D}; +use crate::util::vertex::{VertexTypeContainer, TextureVertex3D, Vertex3D}; use winit::event::{DeviceEvent, MouseButton, ElementState, Event, WindowEvent}; /// @@ -73,9 +73,9 @@ impl Sprite { } impl Drawable for Sprite { - fn get(&self, window_size: (u32, u32)) -> Vec { + fn get(&self, window_size: (u32, u32)) -> Vec { vec![ - VertexType::TextureType( + VertexTypeContainer::TextureType( Sprite::generate_verts(window_size, self.position, self.size, self.depth), self.texture_handle.clone()) ] diff --git a/src/drawables/text.rs b/src/drawables/text.rs index c3215380..b1a0928b 100644 --- a/src/drawables/text.rs +++ b/src/drawables/text.rs @@ -1,10 +1,10 @@ use crate::canvas::canvas_frame::Drawable; -use crate::util::vertex::{VertexType, ColorVertex3D}; +use crate::util::vertex::{VertexTypeContainer, ColorVertex3D}; /// #[derive(Debug, Clone)] pub struct Text { - pub verts: VertexType, + pub verts: VertexTypeContainer, position: (f32, f32), size: (f32, f32), @@ -129,7 +129,7 @@ impl Text { }; Text { - verts: VertexType::TextType(verts), + verts: VertexTypeContainer::TextType(verts), position: position, size: size, } @@ -137,7 +137,7 @@ impl Text { } impl Drawable for Text { - fn get(&self, window_size: (u32, u32)) -> Vec { + fn get(&self, window_size: (u32, u32)) -> Vec { vec![self.verts.clone()] } } diff --git a/src/main.rs b/src/main.rs index 365f1ca2..a5aa9ee8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ use crate::drawables::text::Text; use crate::util::load_raw; use crate::util::timer::Timer; use crate::util::tr_event::TrEvent; -use crate::util::vertex::{TextureVertex3D, VertexType}; +use crate::util::vertex::{TextureVertex3D, VertexTypeContainer}; use crate::vkprocessor::VkProcessor; use crate::drawables::slider::Slider; @@ -52,8 +52,14 @@ extern crate specs; use specs::prelude::*; -// A component contains data which is -// associated with an entity. + +struct Draws(VertexTypeContainer); + +impl Component for Draws { + type Storage = VecStorage; +} + +struct Renderer(u32); struct Vel(f32); @@ -67,6 +73,32 @@ impl Component for Pos { type Storage = VecStorage; } +struct RenderSystem; + +impl<'a> System<'a> for RenderSystem { + + type SystemData = ( + WriteStorage<'a, Pos>, + WriteStorage<'a, Vel>, + WriteStorage<'a, Draws>, + ); + + fn run(&mut self, (mut pos, vel, data): Self::SystemData) { + + // The `.join()` combines multiple components, + // so we only access those entities which have + // both of them. + + // This joins the component storages for Position + // and Velocity together; it's also possible to do this + // in parallel using rayon's `ParallelIterator`s. + // See `ParJoin` for more. + for (pos, vel) in (&mut pos, &vel).join() { + pos.0 += vel.0; + } + } +} + struct SysA; impl<'a> System<'a> for SysA { @@ -93,34 +125,6 @@ impl<'a> System<'a> for SysA { pub fn main() { - - - // The `World` is our - // container for components - // and other resources. - - let mut world = World::new(); - world.register::(); - world.register::(); - - world.reg - - // An entity may or may not contain some component. - world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build(); - world.create_entity().with(Vel(4.0)).with(Pos(1.6)).build(); - world.create_entity().with(Vel(1.5)).with(Pos(5.4)).build(); - - world.create_entity().with(Pos(2.0)).build(); - - let mut dispatcher = DispatcherBuilder::new() - .with(SysA, "sys_a", &[]).build(); - - // This dispatches all the systems in parallel (but blocking). - dispatcher.dispatch(&mut world); - - - - hprof::start_frame(); let q1 = hprof::enter("setup"); @@ -159,7 +163,7 @@ pub fn main() { let step_size: f32 = 0.005; let mut elapsed_time: f32 = timer.elap_time(); - ; + let mut delta_time: f32 = 0.0; let mut accumulator_time: f32 = 0.0; let mut current_time: f32 = timer.elap_time(); @@ -191,6 +195,41 @@ pub fn main() { //let font_handle : Arc = // processor.get_font_handle(String::from("sansation.ttf")).unwrap(); + + + + + + + // The `World` is our + // container for components + // and other resources. + + let mut world = World::new(); + world.register::(); + world.register::(); + world.register::(); + world.insert::(Renderer(10)); + world.insert::(processor); + + + // An entity may or may not contain some component. + world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build(); + + let mut dispatcher = DispatcherBuilder::new() + .with(SysA, "sys_a", &[]) + .with(RenderSystem, "render_s", &[]).build(); + + // This dispatches all the systems in parallel (but blocking). + dispatcher.dispatch(&mut world); + + + + + + + + let mut funky_sprite = Sprite::new( (200.0, 200.0), (100.0, 150.0), 10, funky_handle.clone()); @@ -254,10 +293,10 @@ pub fn main() { // maybe some sort of input system // - let mut big_container = vec![ - Box::new(Slider::new((0.1, 0.1), (0.9, 0.9), 5000)), - Box::new(Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone())), - ]; + // let mut big_container = vec![ + // Box::new(Slider::new((0.1, 0.1), (0.9, 0.9), 5000)), + // Box::new(Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone())), + // ]; //container.push(Sprite::new((0.1))); @@ -265,13 +304,13 @@ pub fn main() { events_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Poll; - for eventable in &mut big_container { - eventable.notify(&event); - } - - for drawable in &mut big_container { - canvas_frame.draw(&drawable); - } + // for eventable in &mut big_container { + // eventable.notify(&event); + // } + // + // for drawable in &mut big_container { + // canvas_frame.draw(&drawable); + // } match event { Event::NewEvents(cause) => { @@ -294,7 +333,7 @@ pub fn main() { *control_flow = ControlFlow::Exit } Event::WindowEvent { event: WindowEvent::Resized(new_size), .. } => { - processor.swapchain_recreate_needed = true; + world.write_resource::().swapchain_recreate_needed = true; let size = (new_size.width, new_size.height); } Event::WindowEvent { @@ -317,7 +356,7 @@ pub fn main() { } VirtualKeyCode::P => { if keyboard_input.state == ElementState::Pressed { - let data = processor.read_compute_buffer(compute_buffer.clone()); + let data = world.write_resource::().read_compute_buffer(compute_buffer.clone()); image::save_buffer(&Path::new("image.png"), data.as_slice(), (image_data.1).0, (image_data.1).1, image::RGBA(8)); } } @@ -343,7 +382,8 @@ pub fn main() { { let g = hprof::enter("Run"); - processor.run(&surface.clone(), + world.write_resource::() + .run(&surface.clone(), &canvas_frame, &compu_frame); } diff --git a/src/util/vertex.rs b/src/util/vertex.rs index 68c10149..66fb3233 100644 --- a/src/util/vertex.rs +++ b/src/util/vertex.rs @@ -51,13 +51,12 @@ vulkano::impl_vertex!(GlyphInstance, screen_position, atlas_position, atlas_size // ============================================================================== #[derive(Debug, Clone)] -pub enum VertexType { +pub enum VertexTypeContainer { TextureType(Vec, Arc), ImageType(Vec, Arc), ColorType(Vec), ThreeDType(Vec), TextType(Vec), - } #[derive(Clone)] diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 9e7aac68..926f2b6c 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -19,13 +19,14 @@ use crate::canvas::managed::shader::generic_shader::GenericShader; use crate::canvas::managed::shader::text_shader::TextShader; use crate::canvas::managed::handles::{CanvasTextureHandle, CompiledShaderHandle, CanvasFontHandle, CanvasImageHandle}; use crate::compute::managed::handles::{CompuKernelHandle, CompuBufferHandle}; -use crate::util::vertex::{VertexType, ColorVertex3D, TextVertex3D, TextureVertex3D, ImageVertex3D}; +use crate::util::vertex::{VertexTypeContainer, ColorVertex3D, TextVertex3D, TextureVertex3D, ImageVertex3D}; use vulkano_text::DrawText; use winit::window::{Window, WindowBuilder}; use vulkano::instance::debug::DebugCallback; use winit::dpi::LogicalSize; use vulkano_win::VkSurfaceBuild; use winit::event_loop::EventLoop; +use specs::prelude::Resource; /// VKProcessor holds the vulkan instance information, the swapchain, @@ -53,6 +54,7 @@ pub struct VkProcessor { } + impl VkProcessor { /// Creates a new VkProcessor from an instance and surface /// This includes the physical device, queues, compute and canvas state