From ccbd21d90be8d36de9e76f9608ce14a83ac7d61a Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 9 Aug 2020 23:39:03 -0700 Subject: [PATCH] compiles, slowly moving everything over to spec stuff --- src/canvas/canvas_frame.rs | 8 ++++ src/canvas/canvas_state.rs | 47 +++++++++--------- src/canvas/compu_frame.rs | 1 + src/main.rs | 97 ++++++++++++++++++++------------------ src/vkprocessor.rs | 46 +++++++++--------- 5 files changed, 104 insertions(+), 95 deletions(-) diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index bb851b8d..1afce6f4 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -39,6 +39,14 @@ impl CanvasFrame { window_size: window_size, } } + + /// Push this drawable onto the back of the accumulator + pub fn add(&mut self, drawable: Vec) { + for i in drawable{ + self.map.push(i); + } + } + /// Push this drawable onto the back of the accumulator pub fn draw(&mut self, drawable: &dyn Drawable) { for i in drawable.get(self.window_size) { diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index aa92b0a7..83c68616 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -42,12 +42,12 @@ use crate::canvas::compu_frame::CompuFrame; /// Canvas state is used for storage of texture and image buffers in addition to vertex buffers /// Canvas state also contains logic for writing the stored buffers to the command_buffer -#[derive(Clone)] +#[derive(Clone, Default)] pub struct CanvasState { /// Generated during new() dynamic_state: DynamicState, /// Generated during new() - sampler: Arc, + sampler: Option>, /// hold the image, texture, and Fonts the same was as we do CompuState image_buffers: Vec>, @@ -58,9 +58,9 @@ pub struct CanvasState { shader_buffers: Vec>>, /// Looks like we gotta hold onto the queue for managing textures - queue: Arc, - device: Arc, - render_pass: Arc, + queue: Option>, + device: Option>, + render_pass: Option>, compute_buffers: Vec, kernels: Vec, @@ -81,11 +81,11 @@ impl CanvasState { }]); let dimensions = [dimensions.width(), dimensions.height()]; - let depth_buffer = AttachmentImage::transient(self.device.clone(), dimensions, Format::D32Sfloat_S8Uint).unwrap(); + let depth_buffer = AttachmentImage::transient(self.device.clone().unwrap(), dimensions, Format::D32Sfloat_S8Uint).unwrap(); images.iter().map(|image| { Arc::new( - Framebuffer::start(self.render_pass.clone()) + Framebuffer::start(self.render_pass.clone().unwrap()) .add(image.clone()).unwrap() .add(depth_buffer.clone()).unwrap() .build().unwrap() @@ -160,19 +160,19 @@ impl CanvasState { value: 0xFF, }), }, - sampler: Sampler::new(device.clone(), + sampler: Some(Sampler::new(device.clone(), Filter::Linear, Filter::Linear, MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, - SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(), + SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap()), image_buffers: vec![], texture_buffers: vec![], shader_buffers: vec![], font_buffers: vec![], - queue: queue.clone(), - device: device.clone(), - render_pass: render_pass.clone(), + queue: Some(queue.clone()), + device: Some(device.clone()), + render_pass: Some(render_pass.clone()), } } @@ -310,7 +310,7 @@ impl CanvasState { let image = CanvasImage { handle: handle.clone(), buffer: AttachmentImage::with_usage( - self.device.clone(), + self.device.clone().unwrap(), [dimensions.0, dimensions.1], Format::R8G8B8A8Uint, usage).unwrap(), @@ -364,7 +364,7 @@ impl CanvasState { image_buffer.iter().cloned(), Dimensions::Dim2d { width: xy.0, height: xy.1 }, Format::R8G8B8A8Srgb, - self.queue.clone(), + self.queue.clone().unwrap(), ).unwrap(); texture @@ -391,8 +391,7 @@ 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, - filename: String, - capabilities: Capabilities) -> Option> + filename: String) -> Option> where T: CompiledShader + Send + Sync + 'static, V: Vertex { let handle = Arc::new(CompiledShaderHandle { @@ -401,9 +400,9 @@ impl CanvasState { let shader: Box = Box::new(T::new::( filename.clone(), - self.device.clone(), + self.device.clone().unwrap(), handle.clone(), - self.render_pass.clone(), + self.render_pass.clone().unwrap(), )); self.shader_buffers.push(Arc::new(shader)); @@ -461,7 +460,7 @@ impl CanvasState { name: name, buffer: ImmutableBuffer::from_iter( accumulator.iter().cloned(), - BufferUsage::vertex_buffer(), self.queue.clone()).unwrap().0, + BufferUsage::vertex_buffer(), self.queue.clone().unwrap()).unwrap().0, } })); @@ -556,7 +555,7 @@ impl CanvasState { allocated_colored_buffer.push(ImmutableBuffer::from_iter( colored_vertex_buffer.iter().cloned(), BufferUsage::vertex_buffer(), - self.queue.clone(), + self.queue.clone().unwrap(), ).unwrap().0); } @@ -565,7 +564,7 @@ impl CanvasState { allocated_text_buffer.push(ImmutableBuffer::from_iter( text_vertex_buffer.iter().cloned(), BufferUsage::vertex_buffer(), - self.queue.clone(), + self.queue.clone().unwrap(), ).unwrap().0); } @@ -576,7 +575,7 @@ impl CanvasState { ImmutableBuffer::from_iter( v.iter().cloned(), BufferUsage::vertex_buffer(), - self.queue.clone(), + self.queue.clone().unwrap(), ).unwrap().0 as Arc<(dyn BufferAccess + Send + Sync)>) }).collect(), image_vertex_buffer: image_vertex_buffer.into_iter().map(|(k, v)| { @@ -584,7 +583,7 @@ impl CanvasState { ImmutableBuffer::from_iter( v.iter().cloned(), BufferUsage::vertex_buffer(), - self.queue.clone(), + self.queue.clone().unwrap(), ).unwrap().0 as Arc<(dyn BufferAccess + Send + Sync)>) }).collect(), text_instances: Default::default(), @@ -669,7 +668,7 @@ impl CanvasState { for (texture_handle, vertex_buffer) in allocated_buffers.textured_vertex_buffer.clone() { let handle = texture_handle.clone().get_handle() as usize; let descriptor_set = self.texture_buffers.get(handle).clone().unwrap().clone() - .get_descriptor_set(shader.get_pipeline(), self.sampler.clone()); + .get_descriptor_set(shader.get_pipeline(), self.sampler.clone().unwrap()); command_buffer = command_buffer.draw( shader.get_pipeline().clone(), diff --git a/src/canvas/compu_frame.rs b/src/canvas/compu_frame.rs index 69a8c03b..0b8743dc 100644 --- a/src/canvas/compu_frame.rs +++ b/src/canvas/compu_frame.rs @@ -5,6 +5,7 @@ use crate::drawables::compu_sprite::CompuSprite; use crate::canvas::canvas_frame::Drawable; use crate::util::vertex::VertexTypeContainer; +#[derive(Default)] pub struct CompuFrame { // Vec<(Buffer, Kernel)> pub pure_compute: Vec<( diff --git a/src/main.rs b/src/main.rs index d3df48e5..9b423210 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ use winit::dpi::LogicalSize; use winit::event::{DeviceEvent, ElementState, Event, MouseButton, StartCause, VirtualKeyCode, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy}; use winit::platform::unix::WindowBuilderExtUnix; -use winit::window::WindowBuilder; +use winit::window::{WindowBuilder, Window}; use crate::canvas::canvas_frame::{CanvasFrame, Drawable, Eventable, Updatable}; use crate::canvas::canvas_state::CanvasState; @@ -50,16 +50,15 @@ pub mod canvas; extern crate specs; use specs::prelude::*; +use vulkano::swapchain::Surface; -struct Draws(VertexTypeContainer); +struct Draws(Sprite); impl Component for Draws { type Storage = VecStorage; } -struct Renderer(u32); - struct Vel(f32); impl Component for Vel { @@ -72,17 +71,33 @@ impl Component for Pos { type Storage = VecStorage; } -struct RenderSystem; +#[derive(Default)] +struct PersistentState { + surface: Option>>, + window_size: (u32, u32), + canvas_frame: CanvasFrame, + compu_frame: CompuFrame, +} + +struct RenderSystem; impl<'a> System<'a> for RenderSystem { type SystemData = ( WriteStorage<'a, Pos>, WriteStorage<'a, Vel>, WriteStorage<'a, Draws>, + Write<'a, PersistentState>, + Write<'a, VkProcessor>, ); - fn run(&mut self, (mut pos, vel, data): Self::SystemData) { + fn run(&mut self, (mut pos, vel, 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()); // The `.join()` combines multiple components, // so we only access those entities which have @@ -92,9 +107,19 @@ impl<'a> System<'a> for RenderSystem { // 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; + + for draw_data in (&draw).join() { + let size = state.window_size.clone(); + state.canvas_frame.add(draw_data.0.get(size)) + } + + vk_processor.run(&state.surface.clone().unwrap(), + &state.canvas_frame, + &state.compu_frame); + + + } } @@ -159,7 +184,7 @@ pub fn main() { let mut timer = Timer::new(); let mut frame_future: Box = - Box::new(sync::now(processor.device.clone())) as Box; + Box::new(sync::now(processor.device.clone().unwrap())) as Box; let step_size: f32 = 0.005; let mut elapsed_time: f32 = timer.elap_time(); @@ -214,9 +239,13 @@ pub fn main() { world.register::(); world.register::(); world.register::(); - world.insert::(Renderer(10)); world.insert::(processor); - + world.insert::(PersistentState { + surface: Some(surface.clone()), + window_size: (0, 0), + canvas_frame: CanvasFrame::new((0,0)), + compu_frame: CompuFrame::new((0,0)), + }); // An entity may or may not contain some component. world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build(); @@ -232,9 +261,6 @@ pub fn main() { - - - let mut funky_sprite = Sprite::new( (200.0, 200.0), (100.0, 150.0), 10, funky_handle.clone()); @@ -242,15 +268,18 @@ pub fn main() { let slider = Slider::new((300.0, 50.0), (550.0, 100.0), 30000); - //let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone()); - //let text_sprite = Text::new((-0.1, -0.1), (10.0, 10.0), 1); - //let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0)); + + drop(q2); drop(q1); let l = hprof::enter("Loop"); + + + + let event_loop_proxy = events_loop.create_proxy(); std::thread::spawn(move || { @@ -285,11 +314,6 @@ pub fn main() { } }); - let mut window_size: (u32, u32) = (0, 0); - - let mut canvas_frame = CanvasFrame::new(window_size); - let mut compu_frame = CompuFrame::new(window_size); - // What would the component for a sprite be... @@ -320,11 +344,8 @@ pub fn main() { match event { Event::NewEvents(cause) => { if cause == StartCause::Init { - canvas_frame.draw(&funky_sprite); - canvas_frame.draw(&compu_sprite1); - canvas_frame.draw(&slider); - - window_size = surface.window().inner_size().into(); + world.write_resource::() + .window_size = surface.window().inner_size().into(); } elapsed_time = timer.elap_time(); delta_time = elapsed_time - current_time; @@ -338,7 +359,8 @@ pub fn main() { *control_flow = ControlFlow::Exit } Event::WindowEvent { event: WindowEvent::Resized(new_size), .. } => { - world.write_resource::().swapchain_recreate_needed = true; + world.write_resource::() + .swapchain_recreate_needed = true; let size = (new_size.width, new_size.height); } Event::WindowEvent { @@ -373,25 +395,6 @@ pub fn main() { } Event::MainEventsCleared => { - funky_sprite.update(delta_time); - - canvas_frame = CanvasFrame::new(window_size); - canvas_frame.draw(&funky_sprite); - //canvas_frame.draw(&container); - // canvas_frame.draw(&compu_sprite1); - canvas_frame.draw(&slider); - - compu_frame = CompuFrame::new(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()); - - { - let g = hprof::enter("Run"); - world.write_resource::() - .run(&surface.clone(), - &canvas_frame, - &compu_frame); - } // while (accumulator_time - step_size) >= step_size { // accumulator_time -= step_size; diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 270aaa6c..8412e5e5 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -30,12 +30,13 @@ use specs::prelude::Resource; /// VKProcessor holds the vulkan instance information, the swapchain, /// and the compute and canvas states +#[derive(Default)] pub struct VkProcessor { + // Vulkan state fields - //pub physical: PhysicalDevice<'a>, - pub device: Arc, - pub queues: QueuesIter, - pub queue: Arc, + pub device: Option>, + pub queues: Option, + pub queue: Option>, pub swapchain: Option>>, pub swapchain_images: Option>>>, @@ -45,8 +46,6 @@ pub struct VkProcessor { /// State holding textures, images, and their related vertex buffers canvas_state: CanvasState, - capabilities: Capabilities, - } @@ -76,13 +75,12 @@ impl VkProcessor { let capabilities = surface.capabilities(physical).unwrap(); VkProcessor { - device: device.clone(), - queue: queue.clone(), - queues: queues, + device: Some(device.clone()), + queue: Some(queue.clone()), + queues: Some(queues), swapchain: None, swapchain_images: None, swapchain_recreate_needed: false, - capabilities: capabilities.clone(), canvas_state: CanvasState::new(queue, device, physical, capabilities), } } @@ -112,14 +110,14 @@ impl VkProcessor { panic!("window closed"); }; - Swapchain::new(self.device.clone(), + Swapchain::new(self.device.clone().unwrap(), surface.clone(), capabilities.min_image_count, // number of attachment images format, initial_dimensions, 1, // Layers ImageUsage::color_attachment(), - &self.queue, + (&self.queue).as_ref().unwrap(), SurfaceTransform::Identity, alpha, PresentMode::Immediate, @@ -165,16 +163,16 @@ impl VkProcessor { /// A hardcoded list of kernels which can be preloaded from this function pub fn preload_kernels(&mut self) { - self.canvas_state.new_kernel(String::from("simple-homogenize.compute"), self.device.clone()); - self.canvas_state.new_kernel(String::from("simple-edge.compute"), self.device.clone()); + self.canvas_state.new_kernel(String::from("simple-homogenize.compute"), self.device.clone().unwrap()); + self.canvas_state.new_kernel(String::from("simple-edge.compute"), self.device.clone().unwrap()); } /// A hardcoded list of shaders which can be preloaded from this function pub fn preload_shaders(&mut self) { - self.canvas_state.load_shader::(String::from("color-passthrough"), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_texture"), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_image"), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_text"), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("color-passthrough")); + self.canvas_state.load_shader::(String::from("simple_texture")); + self.canvas_state.load_shader::(String::from("simple_image")); + self.canvas_state.load_shader::(String::from("simple_text")); } /// A hardcoded list of shaders which can be proloaded from this function @@ -212,7 +210,7 @@ impl VkProcessor { /// Builds a compute buffer and returns it's handle pub fn new_compute_buffer(&mut self, data: Vec, dimensions: (u32, u32), stride: u32) -> Arc { - self.canvas_state.new_compute_buffer(data, dimensions, stride, self.device.clone()) + self.canvas_state.new_compute_buffer(data, dimensions, stride, self.device.clone().unwrap()) } /// Takes a compute buffer handle and returns the read data @@ -233,7 +231,7 @@ impl VkProcessor { ) { { let g = hprof::enter("Waiting at queue"); - self.queue.wait(); + self.queue.as_ref().unwrap().wait(); } let g = hprof::enter("Frame buffer, future, swapchain recreate"); @@ -280,7 +278,7 @@ impl VkProcessor { // let mut draw_text = DrawText::new(self.device.clone(), self.queue.clone(), self.swapchain.unwrap().clone(), &self.swapchain_images.images); let mut command_buffer = - AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(), self.queue.family()).unwrap(); + AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone().unwrap(), self.queue.as_ref().unwrap().family()).unwrap(); let g = hprof::enter("Push compute commands to command buffer"); // Add the compute commands @@ -300,15 +298,15 @@ impl VkProcessor { // Wait on the previous frame, then execute the command buffer and present the image { let g = hprof::enter("Joining on the framebuffer"); - let mut future = sync::now(self.device.clone()) + let mut future = sync::now(self.device.clone().unwrap()) .join(acquire_future); drop(g); let g = hprof::enter("Running the kernel and waiting on the future"); let future = future - .then_execute(self.queue.clone(), command_buffer).unwrap() - .then_swapchain_present(self.queue.clone(), self.swapchain.clone().unwrap().clone(), image_num) + .then_execute(self.queue.clone().unwrap(), command_buffer).unwrap() + .then_swapchain_present(self.queue.clone().unwrap(), self.swapchain.clone().unwrap().clone(), image_num) .then_signal_fence_and_flush(); match future {