From dfd4cbb6a2063598c80f1c8ec18be785338000ef Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 11 Aug 2019 16:37:14 -0700 Subject: [PATCH] Moved the framebuffer gen to the canvas. Cleaned up a lot of unused imports --- src/button.rs | 4 +- src/canvas.rs | 88 ++++++-------- src/main.rs | 55 ++++----- src/slider.rs | 4 +- src/sprite.rs | 2 +- src/util/compute_image.rs | 41 ++----- src/util/compute_kernel.rs | 64 +++++----- src/util/shader_kernels.rs | 74 ++++++------ src/vkprocessor.rs | 242 ++----------------------------------- 9 files changed, 156 insertions(+), 418 deletions(-) diff --git a/src/button.rs b/src/button.rs index 1812670c..1c4d4e7a 100644 --- a/src/button.rs +++ b/src/button.rs @@ -1,5 +1,5 @@ -use sfml::graphics::{CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Text, Font}; -use sfml::window::{Event, Key, Style}; +use sfml::graphics::{Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Text, Font}; + use sfml::system::Vector2f; trait Clickable { diff --git a/src/canvas.rs b/src/canvas.rs index dcec91bf..92ff7611 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,16 +1,16 @@ use crate::vertex_2d::{ColoredVertex2D, Vertex2D}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; -use crate::Sprite; + use std::collections::HashMap; -use vulkano::buffer::{BufferAccess, CpuAccessibleBuffer, BufferUsage, ImmutableBuffer}; +use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer}; use std::sync::Arc; use vulkano::format::{ClearValue, Format}; -use vulkano::framebuffer::FramebufferAbstract; +use vulkano::framebuffer::{FramebufferAbstract, Framebuffer}; use vulkano::device::{Device, Queue}; use vulkano::instance::PhysicalDevice; use vulkano::image::immutable::ImmutableImage; use crate::util::shader_kernels::ShaderKernels; -use vulkano::image::{Dimensions, ImageUsage, ImageAccess, ImageDimensions}; +use vulkano::image::{Dimensions, ImageAccess, ImageDimensions, SwapchainImage}; use vulkano::sampler::{Sampler, SamplerAddressMode, MipmapMode, Filter}; use vulkano::descriptor::DescriptorSet; use vulkano::descriptor::descriptor_set::PersistentDescriptorSet; @@ -19,7 +19,10 @@ use image::GenericImageView; use crate::util::compute_image::ComputeImage; use std::iter::FromIterator; use vulkano::swapchain::Capabilities; -use shaderc::TargetEnv; + +use winit::Window; +use vulkano::pipeline::viewport::Viewport; + // Canvas is the accumulator of Sprites for drawing // Needs to know: @@ -32,8 +35,6 @@ use shaderc::TargetEnv; If it is textured. It needs to be rendered with the texture shader which requires a separate graphics pipeline. Might as well have a new render pass as well. -I need a second version of shaderkernels - So framebuffer is tied to the swapchains images as well as the renderpass @@ -50,28 +51,6 @@ it appears that renderpass is tied to the individual shader // The rest will be grouped by their texture and run individually -/* - -vertex count differing is a big nono - -ColoredVertex2D - Non-Textured - -Vertex2D - Textured - -Colored, vs non-colored: - Calling the color() field or not - - - - -I just wanna - - - -*/ - pub trait Vertex { fn position(&self) -> (f32, f32) { (0.0,0.0) @@ -215,7 +194,7 @@ impl Canvas { pub fn load_texture_from_filename(&mut self, filename: String) -> Arc> { - if (self.texture_store.contains_key(&filename.clone())) { + if self.texture_store.contains_key(&filename.clone()) { println!("{} Already exists, not going to replace it.", filename.clone()); self.texture_store.get(&filename.clone()).unwrap().clone() } else { @@ -331,31 +310,9 @@ impl Canvas { o } - /* - So I need the image set in order to get my texture or compute texture - Done - - compute image currently holds the set for compute and its swap buffer - but during a descriptor set build, we corrow the compute_image and take - it's swap buffer - - vkprocessor creates the image sets for draw calls - takes the pipeline from the ShaderKernel - Which we own - adds vk processor owned texture - Not any more - adds compute image taken from the ComputeImage - Still true - - we have shaderkernel in here so thats fine - Who should own the texture? - I would need to borrow it each time I created an image set... - These are tied very closely to the input output of a shader, which we would own - - - I just need to add a third option on sprite and allow it to have a swap buffer */ - - pub fn draw_commands(&self, mut command_buffer: AutoCommandBufferBuilder, framebuffers: Vec>, @@ -395,6 +352,33 @@ impl Canvas { .end_render_pass() .unwrap() } + + /// This method is called once during initialization, then again whenever the window is resized + pub fn window_size_dependent_setup(&self, + images: &[Arc>], + ) -> Vec> { + + let dimensions = images[0].dimensions(); + + let mut dynamic_state = DynamicState { + line_width: None, + viewports: Some(vec![Viewport { + origin: [0.0, 0.0], + dimensions: [dimensions.width() as f32, dimensions.height() as f32], + depth_range: 0.0..1.0, + }]), + scissors: None + }; + + images.iter().map(|image| { + Arc::new( + Framebuffer::start(self.shader_kernels.get(&ShaderType::SOLID).unwrap().render_pass.clone()) + .add(image.clone()).unwrap() + .build().unwrap() + ) as Arc + }).collect::>() + } + } diff --git a/src/main.rs b/src/main.rs index fe37b217..d4120f75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,43 +10,41 @@ extern crate rand; extern crate sfml; extern crate time; -use sfml::graphics::*; -use sfml::graphics::{ - Color, RenderTarget, RenderWindow, -}; + + use sfml::system::*; -use sfml::window::{Key, Style}; -use sfml::window::mouse::*; -use sfml::window::mouse; + + + use vulkano::sync; -use std::sync::Arc; -use std::{fs, mem, iter, ptr}; -use std::path::PathBuf; -use std::result; -use crate::input::Input; -use crate::slider::Slider; + + + + + + use crate::timer::Timer; -use na::DimAdd; -use std::time::{SystemTime, Duration}; -use std::ffi::CStr; -use std::ptr::write; -use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess}; -use vulkano::command_buffer::AutoCommandBufferBuilder; -use vulkano::descriptor::descriptor_set::PersistentDescriptorSet; -use vulkano::device::{Device, DeviceExtensions}; -use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice}; -use vulkano::pipeline::ComputePipeline; -use vulkano::descriptor::pipeline_layout::PipelineLayoutAbstract; + + + + + + + + +use vulkano::instance::{Instance}; + + use vulkano::sync::GpuFuture; -use shaderc::CompileOptions; -use shade_runner::CompileError; + + use winit::{EventsLoop, WindowBuilder, WindowEvent, Event, DeviceEvent, VirtualKeyCode, ElementState}; use winit::dpi::LogicalSize; use vulkano_win::VkSurfaceBuild; use sprite::Sprite; -use crate::canvas::Canvas; + mod util; @@ -75,8 +73,7 @@ fn main() { let mut processor = vkprocessor::VkProcessor::new(&instance, &surface); processor.compile_kernel(String::from("simple-edge.compute")); - processor.compile_shaders(String::from("simple_texture"), &surface); - processor.load_buffers(String::from("background.jpg")); + processor.load_textures(String::from("background.jpg")); processor.create_swapchain(&surface); let mut timer = Timer::new(); diff --git a/src/slider.rs b/src/slider.rs index 4879a012..d7e2a930 100644 --- a/src/slider.rs +++ b/src/slider.rs @@ -1,5 +1,5 @@ -use sfml::window::{Key, Event}; -use sfml::graphics::{CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Font}; + +use sfml::graphics::{Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Font}; use crate::button::Button; use sfml::system::Vector2f; diff --git a/src/sprite.rs b/src/sprite.rs index 03c728b4..1f6be35e 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,4 +1,4 @@ -use crate::vertex_2d::ColoredVertex2D; + use crate::canvas::Drawable; #[derive(Debug, Clone)] diff --git a/src/util/compute_image.rs b/src/util/compute_image.rs index be22dde0..2e26268a 100644 --- a/src/util/compute_image.rs +++ b/src/util/compute_image.rs @@ -1,43 +1,19 @@ -use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess}; -use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet, StdDescriptorPoolAlloc}; -use vulkano::device::{Device, DeviceExtensions, QueuesIter, Queue}; -use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice, QueueFamily}; -use vulkano::pipeline::{ComputePipeline, GraphicsPipeline, GraphicsPipelineAbstract, GraphicsPipelineBuilder}; -use vulkano::sync::{GpuFuture, FlushError}; -use vulkano::sync; +use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer}; + +use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet}; +use vulkano::device::{Device}; +use vulkano::pipeline::{ComputePipeline}; use std::time::SystemTime; use std::sync::Arc; -use std::ffi::CStr; use std::path::PathBuf; -use shade_runner as sr; -use image::{DynamicImage, ImageBuffer}; +use image::{ImageBuffer}; use image::GenericImageView; use vulkano::descriptor::pipeline_layout::PipelineLayout; -use image::GenericImage; -use shade_runner::{ComputeLayout, CompileError, FragLayout, FragInput, FragOutput, VertInput, VertOutput, VertLayout, CompiledShaders, Entry}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSetImg, PersistentDescriptorSetSampler}; -use shaderc::CompileOptions; -use vulkano::framebuffer::{Subpass, RenderPass, RenderPassAbstract, Framebuffer, FramebufferAbstract}; -use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, GraphicsEntryPoint, SpecializationConstants, SpecializationMapEntry}; -use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, Surface, SwapchainCreationError, AcquireError}; -use vulkano::swapchain::acquire_next_image; -use vulkano::image::swapchain::SwapchainImage; -use winit::{EventsLoop, WindowBuilder, Window, Event, WindowEvent}; -use vulkano_win::VkSurfaceBuild; -use vulkano::pipeline::vertex::{SingleBufferDefinition, Vertex}; -use vulkano::descriptor::PipelineLayoutAbstract; -use std::alloc::Layout; -use vulkano::pipeline::viewport::Viewport; -use image::ImageFormat; -use vulkano::image::immutable::ImmutableImage; +use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf}; use vulkano::image::attachment::AttachmentImage; -use vulkano::image::{Dimensions, ImageUsage}; +use vulkano::image::{ImageUsage}; use vulkano::format::Format; -use vulkano::sampler::{Sampler, Filter, MipmapMode, SamplerAddressMode}; -use image::flat::NormalForm::ColumnMajorPacked; use image::Rgba; -use crate::vertex_2d::ColoredVertex2D; /* @@ -47,7 +23,6 @@ This is a pretty specific use case. One in for settings. One in for data, two fo multiple data inputs might be nice? - */ #[derive(Clone)] diff --git a/src/util/compute_kernel.rs b/src/util/compute_kernel.rs index ed3b0bd3..b9f750bc 100644 --- a/src/util/compute_kernel.rs +++ b/src/util/compute_kernel.rs @@ -1,41 +1,41 @@ -use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess}; -use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet, StdDescriptorPoolAlloc}; -use vulkano::device::{Device, DeviceExtensions, QueuesIter, Queue}; -use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice, QueueFamily}; -use vulkano::pipeline::{ComputePipeline, GraphicsPipeline, GraphicsPipelineAbstract}; -use vulkano::sync::{GpuFuture, FlushError}; -use vulkano::sync; -use std::time::SystemTime; + + + +use vulkano::device::{Device}; + +use vulkano::pipeline::{ComputePipeline}; + + + use std::sync::Arc; use std::ffi::CStr; use std::path::PathBuf; use shade_runner as sr; -use image::{DynamicImage, ImageBuffer}; -use image::GenericImageView; + + use vulkano::descriptor::pipeline_layout::PipelineLayout; -use image::GenericImage; -use shade_runner::{ComputeLayout, CompileError, FragLayout, FragInput, FragOutput, VertInput, VertOutput, VertLayout, CompiledShaders, Entry}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSetImg, PersistentDescriptorSetSampler}; + +use shade_runner::{CompileError, FragLayout, FragInput, FragOutput, VertInput, VertOutput, VertLayout, CompiledShaders, Entry}; + use shaderc::CompileOptions; -use vulkano::framebuffer::{Subpass, RenderPass, RenderPassAbstract, Framebuffer, FramebufferAbstract}; -use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, GraphicsEntryPoint, SpecializationConstants, SpecializationMapEntry}; -use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, Surface, SwapchainCreationError, AcquireError}; -use vulkano::swapchain::acquire_next_image; -use vulkano::image::swapchain::SwapchainImage; -use winit::{EventsLoop, WindowBuilder, Window, Event, WindowEvent}; -use vulkano_win::VkSurfaceBuild; -use vulkano::pipeline::vertex::{SingleBufferDefinition, Vertex}; -use vulkano::descriptor::PipelineLayoutAbstract; -use std::alloc::Layout; -use vulkano::pipeline::viewport::Viewport; -use image::ImageFormat; -use vulkano::image::immutable::ImmutableImage; -use vulkano::image::attachment::AttachmentImage; -use vulkano::image::{Dimensions, ImageUsage}; -use vulkano::format::Format; -use vulkano::sampler::{Sampler, Filter, MipmapMode, SamplerAddressMode}; -use image::flat::NormalForm::ColumnMajorPacked; + +use vulkano::pipeline::shader::{ShaderModule, GraphicsEntryPoint, SpecializationConstants, SpecializationMapEntry}; + + + + + + + + + + + + + + + + #[derive(Clone)] diff --git a/src/util/shader_kernels.rs b/src/util/shader_kernels.rs index f51bff2c..5765103c 100644 --- a/src/util/shader_kernels.rs +++ b/src/util/shader_kernels.rs @@ -1,41 +1,41 @@ -use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess}; -use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet, StdDescriptorPoolAlloc, DescriptorSetDesc}; -use vulkano::device::{Device, DeviceExtensions, QueuesIter, Queue}; -use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice, QueueFamily}; -use vulkano::pipeline::{ComputePipeline, GraphicsPipeline, GraphicsPipelineAbstract, GraphicsPipelineBuilder}; -use vulkano::sync::{GpuFuture, FlushError}; -use vulkano::sync; -use std::time::SystemTime; + + + +use vulkano::device::{Device, Queue}; +use vulkano::instance::{PhysicalDevice, QueueFamily}; +use vulkano::pipeline::{GraphicsPipeline, GraphicsPipelineAbstract, GraphicsPipelineBuilder}; + + + use std::sync::Arc; use std::ffi::CStr; use std::path::PathBuf; use shade_runner as sr; -use image::{DynamicImage, ImageBuffer}; -use image::GenericImageView; -use vulkano::descriptor::pipeline_layout::PipelineLayout; -use image::GenericImage; -use shade_runner::{ComputeLayout, CompileError, FragLayout, FragInput, FragOutput, VertInput, VertOutput, VertLayout, CompiledShaders, Entry}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSetImg, PersistentDescriptorSetSampler}; -use shaderc::CompileOptions; -use vulkano::framebuffer::{Subpass, RenderPass, RenderPassAbstract, Framebuffer, FramebufferAbstract, RenderPassDesc}; -use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, GraphicsEntryPoint, SpecializationConstants, SpecializationMapEntry}; -use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, Surface, SwapchainCreationError, AcquireError, Capabilities}; -use vulkano::swapchain::acquire_next_image; -use vulkano::image::swapchain::SwapchainImage; -use winit::{EventsLoop, WindowBuilder, Window, Event, WindowEvent}; -use vulkano_win::VkSurfaceBuild; -use vulkano::pipeline::vertex::{SingleBufferDefinition, Vertex}; -use vulkano::descriptor::{PipelineLayoutAbstract, DescriptorSet}; -use std::alloc::Layout; -use vulkano::pipeline::viewport::Viewport; -use image::ImageFormat; -use vulkano::image::immutable::ImmutableImage; -use vulkano::image::attachment::AttachmentImage; -use vulkano::image::{Dimensions, ImageUsage}; -use vulkano::format::Format; -use vulkano::sampler::{Sampler, Filter, MipmapMode, SamplerAddressMode}; -use image::flat::NormalForm::ColumnMajorPacked; + + + + + + + +use vulkano::framebuffer::{Subpass, RenderPassAbstract, Framebuffer, FramebufferAbstract}; +use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, SpecializationConstants, SpecializationMapEntry}; +use vulkano::swapchain::{Capabilities}; + + + + + + + + + + + + + + + use crate::vertex_2d::ColoredVertex2D; /* @@ -47,8 +47,8 @@ Shaderkernel holds the pipeline and render pass for the inputted shader source #[derive(Clone)] pub struct ShaderKernels { - pub render_pass: Arc, - graphics_pipeline: Option>, + pub render_pass: Arc, + graphics_pipeline: Option>, device: Arc, } @@ -75,7 +75,7 @@ impl ShaderKernels { (vertex_shader_path, fragment_shader_path) } - pub fn get_pipeline(&mut self) -> Arc { + pub fn get_pipeline(&mut self) -> Arc { self.graphics_pipeline.clone().unwrap() } diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index d1855a73..b776ca9e 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -1,80 +1,17 @@ -use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet, StdDescriptorPoolAlloc, PersistentDescriptorSetBuilder, FixedSizeDescriptorSetsPool, StdDescriptorPool}; -use vulkano::descriptor::descriptor_set::collection::DescriptorSetsCollection; use vulkano::device::{Device, DeviceExtensions, QueuesIter, Queue}; -use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice, QueueFamily}; -use vulkano::pipeline::{ComputePipeline, GraphicsPipeline, GraphicsPipelineAbstract}; +use vulkano::instance::{Instance, PhysicalDevice}; use vulkano::sync::{GpuFuture, FlushError}; use vulkano::sync; -use std::time::SystemTime; use std::sync::Arc; -use std::ffi::CStr; -use std::path::PathBuf; -use shade_runner as sr; -use image::{DynamicImage, ImageBuffer}; -use image::GenericImageView; -use vulkano::descriptor::pipeline_layout::PipelineLayout; -use image::GenericImage; -use shade_runner::{ComputeLayout, CompileError, FragLayout, FragInput, FragOutput, VertInput, VertOutput, VertLayout, CompiledShaders, Entry}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSetImg, PersistentDescriptorSetSampler}; -use shaderc::CompileOptions; -use vulkano::framebuffer::{Subpass, RenderPass, RenderPassAbstract, Framebuffer, FramebufferAbstract}; -use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, GraphicsEntryPoint, SpecializationConstants, SpecializationMapEntry}; use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, Surface, SwapchainCreationError, AcquireError, Capabilities}; -use vulkano::swapchain::acquire_next_image; use vulkano::image::swapchain::SwapchainImage; -use winit::{EventsLoop, WindowBuilder, Window, Event, WindowEvent}; -use vulkano_win::VkSurfaceBuild; -use vulkano::pipeline::vertex::{SingleBufferDefinition, Vertex}; -use vulkano::descriptor::{PipelineLayoutAbstract, DescriptorSet}; -use std::alloc::Layout; -use vulkano::pipeline::viewport::Viewport; -use image::ImageFormat; -use vulkano::image::immutable::ImmutableImage; -use vulkano::image::attachment::AttachmentImage; -use vulkano::image::{Dimensions, ImageUsage, ImageAccess, ImageDimensions}; -use vulkano::format::Format; -use vulkano::format::ClearValue; -use vulkano::sampler::{Sampler, Filter, MipmapMode, SamplerAddressMode}; -use image::flat::NormalForm::ColumnMajorPacked; - +use winit::{Window}; use crate::util::compute_kernel::ComputeKernel; -use crate::util::shader_kernels::ShaderKernels; use crate::util::compute_image::ComputeImage; - -use vulkano::descriptor::descriptor::DescriptorDesc; -use crate::vertex_2d::ColoredVertex2D; use crate::canvas::Canvas; -use std::mem; - - -/// This method is called once during initialization, then again whenever the window is resized -fn window_size_dependent_setup( - images: &[Arc>], - render_pass: Arc, - dynamic_state: &mut DynamicState, -) -> Vec> { - let dimensions = images[0].dimensions(); - - let viewport = Viewport { - origin: [0.0, 0.0], - dimensions: [dimensions.width() as f32, dimensions.height() as f32], - depth_range: 0.0..1.0, - }; - dynamic_state.viewports = Some(vec!(viewport)); - - images.iter().map(|image| { - Arc::new( - Framebuffer::start(render_pass.clone()) - .add(image.clone()).unwrap() - .build().unwrap() - ) as Arc - }).collect::>() -} pub struct VkProcessor<'a> { - // Vulkan state fields pub instance: Arc, pub physical: PhysicalDevice<'a>, @@ -84,15 +21,7 @@ pub struct VkProcessor<'a> { pub dynamic_state: DynamicState, // TODO: This will need to handle multiple of each type - pub shader_kernels: Option, pub compute_kernel: Option, - - // TODO: Move this into canvas - pub vertex_buffer: Option>, - pub vertex_buffer2: Option>, - - pub textures: Vec>>, - pub compute_image: Option, pub swapchain: Option>>, @@ -106,9 +35,7 @@ pub struct VkProcessor<'a> { impl<'a> VkProcessor<'a> { - pub fn new(instance: &'a Arc, surface: &'a Arc>) -> VkProcessor<'a> { - let physical = PhysicalDevice::enumerate(instance).next().unwrap(); let queue_family = physical.queue_families().find(|&q| { @@ -135,36 +62,23 @@ impl<'a> VkProcessor<'a> { queue: queue.clone(), queues: queues, dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None }, - shader_kernels: None, compute_kernel: None, - vertex_buffer: None, - vertex_buffer2: None, - textures: vec![], compute_image: None, swapchain: None, swapchain_images: None, swapchain_recreate_needed: false, capabilities: capabilities.clone(), - canvas: Canvas::new(queue, device, physical, capabilities) + canvas: Canvas::new(queue, device, physical, capabilities), } } pub fn compile_kernel(&mut self, filename: String) { - self.compute_kernel = Some(ComputeKernel::new(filename, self.device.clone())); + self.compute_kernel = Some(ComputeKernel::new(filename, self.device.clone())); } - pub fn compile_shaders(&mut self, filename: String, surface: &'a Arc>) { - self.shader_kernels = Some( - ShaderKernels::new(filename.clone(), - self.capabilities.clone(), self.queue.clone(), - self.physical, - self.device.clone()) - ); - } pub fn create_swapchain(&mut self, surface: &'a Arc>) { - let (mut swapchain, images) = { let capabilities = surface.capabilities(self.physical).unwrap(); let usage = capabilities.supported_usage_flags; @@ -197,12 +111,10 @@ impl<'a> VkProcessor<'a> { self.swapchain = Some(swapchain); self.swapchain_images = Some(images); - } // On resizes we have to recreate the swapchain pub fn recreate_swapchain(&mut self, surface: &'a Arc>) { - let dimensions = if let Some(dimensions) = surface.window().get_inner_size() { let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); [dimensions.0, dimensions.1] @@ -220,143 +132,30 @@ impl<'a> VkProcessor<'a> { self.swapchain = Some(new_swapchain); self.swapchain_images = Some(new_images); - - } - - fn get_texture_from_file(image_filename: String, queue: Arc) -> Arc> { - - let project_root = - std::env::current_dir() - .expect("failed to get root directory"); - - let mut compute_path = project_root.clone(); - compute_path.push(PathBuf::from("resources/images/")); - compute_path.push(PathBuf::from(image_filename)); - - let img = image::open(compute_path).expect("Couldn't find image"); - - let xy = img.dimensions(); - - let data_length = xy.0 * xy.1 * 4; - let pixel_count = img.raw_pixels().len(); - - let mut image_buffer = Vec::new(); - - if pixel_count != data_length as usize { - println!("Creating apha channel..."); - for i in img.raw_pixels().iter() { - if (image_buffer.len() + 1) % 4 == 0 { - image_buffer.push(255); - } - image_buffer.push(*i); - } - image_buffer.push(255); - } else { - image_buffer = img.raw_pixels(); - } - - let (texture, tex_future) = ImmutableImage::from_iter( - image_buffer.iter().cloned(), - Dimensions::Dim2d { width: xy.0, height: xy.1 }, - Format::R8G8B8A8Srgb, - queue.clone() - ).unwrap(); - - texture } pub fn load_compute_image(&mut self, image_filename: String) { self.compute_image = Some(ComputeImage::new(self.device.clone(), image_filename.clone())); } - pub fn load_buffers(&mut self, image_filename: String) + pub fn load_textures(&mut self, image_filename: String) { self.load_compute_image(image_filename.clone()); - let color = [1.,0.,0.,0.]; - - let vertex_buffer = { - - CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), [ - ColoredVertex2D { position: [ 1.0, 1.0 ], color }, - ColoredVertex2D { position: [ 1.0, 0.5 ], color }, - ColoredVertex2D { position: [ 0.5, 0.5 ], color }, - ColoredVertex2D { position: [ 0.5, 1.0 ], color }, - ].iter().cloned()).unwrap() - }; - - let vertex_buffer2 = { - - CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), [ - ColoredVertex2D { position: [-1.0, -1.0 ], color }, - ColoredVertex2D { position: [-1.0, -0.5 ], color }, - ColoredVertex2D { position: [-0.5, -0.5 ], color }, - ColoredVertex2D { position: [-0.5, -1.0 ], color }, - ].iter().cloned()).unwrap() - }; - - - - - self.vertex_buffer = Some(vertex_buffer); - self.vertex_buffer2 = Some(vertex_buffer2); - - let texture = VkProcessor::get_texture_from_file(image_filename.clone(), self.queue.clone()); - self.textures.push(texture); - - let texture1 = VkProcessor::get_texture_from_file(String::from("button.png"), self.queue.clone()); - self.textures.push(texture1); + self.canvas.load_texture_from_filename(image_filename.clone()); } - - // The image set is the containing object for all texture and image hooks. - fn get_image_set(&mut self) -> Box { - - let sampler = Sampler::new(self.device.clone(), Filter::Linear, Filter::Linear, - MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, - SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(); - - let o : Box = Box::new( - PersistentDescriptorSet::start( - self.shader_kernels.clone().unwrap().get_pipeline(), 0 - ) - .add_sampled_image(self.textures.get(0).unwrap().clone(), sampler.clone()).unwrap() - .add_image(self.compute_image.clone().unwrap().clone().get_swap_buffer().clone()).unwrap() - .build().unwrap()); - o - } - - // The image set is the containing object for all texture and image hooks. - fn get_gui_image_set(&mut self) -> Box { - - let sampler = Sampler::new(self.device.clone(), Filter::Linear, Filter::Linear, - MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, - SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(); - - let o : Box = Box::new( - PersistentDescriptorSet::start( - self.shader_kernels.clone().unwrap().get_pipeline(), 0 - ) - .add_sampled_image(self.textures.get(1).unwrap().clone(), sampler.clone()).unwrap() - .add_image(self.compute_image.clone().unwrap().clone().get_swap_buffer().clone()).unwrap() - .build().unwrap()); - o - } - - pub fn save_edges_image(&mut self){ + pub fn save_edges_image(&mut self) { self.compute_image.clone().unwrap().clone().save_image(); } - pub fn run(&mut self, surface: &'a Arc>, mut frame_future: Box, ) -> Box { - - let mut framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(), - self.shader_kernels.clone().unwrap().render_pass.clone(), - &mut self.dynamic_state); + let mut framebuffers = + self.canvas.window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone()); // The docs said to call this on each loop. frame_future.cleanup_finished(); @@ -365,9 +164,8 @@ impl<'a> VkProcessor<'a> { // In this example that includes the swapchain, the framebuffers and the dynamic state viewport. if self.swapchain_recreate_needed { self.recreate_swapchain(surface); - framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(), - self.shader_kernels.clone().unwrap().render_pass.clone(), - &mut self.dynamic_state); + framebuffers = + self.canvas.window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone()); self.swapchain_recreate_needed = false; } @@ -382,14 +180,6 @@ impl<'a> VkProcessor<'a> { Err(err) => panic!("{:?}", err) }; - // Specify the color to clear the framebuffer with i.e. blue - let clear_values = vec!(ClearValue::Float([0.0, 0.0, 1.0, 1.0])); - - let mut v = Vec::new(); - v.push(self.vertex_buffer.clone().unwrap().clone()); - - let mut v2 = Vec::new(); - v2.push(self.vertex_buffer2.clone().unwrap().clone()); let xy = self.compute_image.clone().unwrap().get_size(); @@ -403,19 +193,11 @@ impl<'a> VkProcessor<'a> { .get_descriptor_set(self.compute_kernel.clone().unwrap().clone().get_pipeline()).clone(), ()).unwrap() .copy_buffer_to_image(self.compute_image.clone().unwrap().clone().rw_buffers.get(0).unwrap().clone(), - self.compute_image.clone().unwrap().clone().get_swap_buffer().clone()).unwrap(); + self.compute_image.clone().unwrap().clone().get_swap_buffer().clone()).unwrap(); - /* - Fuck. So this is a problem... - - I can't replace canvas. - */ - let mut command_buffer = self.canvas.draw_commands(command_buffer, framebuffers, image_num); - //self.canvas = mem::replace(&mut self.canvas, - let command_buffer = command_buffer.build().unwrap(); // Wait on the previous frame, then execute the command buffer and present the image