ugga ugga. Lifetime problems with the Canvas

master
mitchellhansen 5 years ago
parent a7e79668eb
commit f06adc9a55

@ -2,7 +2,7 @@ use crate::vertex_2d::{ColoredVertex2D, Vertex2D};
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use crate::Sprite; use crate::Sprite;
use std::collections::HashMap; use std::collections::HashMap;
use vulkano::buffer::{BufferAccess, CpuAccessibleBuffer, BufferUsage}; use vulkano::buffer::{BufferAccess, CpuAccessibleBuffer, BufferUsage, ImmutableBuffer};
use std::sync::Arc; use std::sync::Arc;
use vulkano::format::{ClearValue, Format}; use vulkano::format::{ClearValue, Format};
use vulkano::framebuffer::FramebufferAbstract; use vulkano::framebuffer::FramebufferAbstract;
@ -111,13 +111,14 @@ pub trait Drawable {
// Need three types of shaders. Solid, Textured, Compute // Need three types of shaders. Solid, Textured, Compute
#[derive(PartialEq, Eq, Hash)] #[derive(PartialEq, Eq, Hash, Clone)]
pub enum ShaderType { pub enum ShaderType {
SOLID = 0, SOLID = 0,
TEXTURED = 1, TEXTURED = 1,
COMPUTE = 2 COMPUTE = 2
} }
#[derive(Clone)]
pub struct Canvas { pub struct Canvas {
colored_drawables : Vec<ColoredVertex2D>, colored_drawables : Vec<ColoredVertex2D>,
@ -126,9 +127,6 @@ pub struct Canvas {
textured_drawables: HashMap<String, Vec<Vertex2D>>, textured_drawables: HashMap<String, Vec<Vertex2D>>,
textured_vertex_buffer: HashMap<String, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>, textured_vertex_buffer: HashMap<String, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>,
vertex_buffers: Vec<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>,
shader_kernels: HashMap<ShaderType, ShaderKernels>, shader_kernels: HashMap<ShaderType, ShaderKernels>,
texture_store: HashMap<String, Arc<ImmutableImage<Format>>>, texture_store: HashMap<String, Arc<ImmutableImage<Format>>>,
@ -161,8 +159,6 @@ impl Canvas {
textured_drawables: Default::default(), textured_drawables: Default::default(),
textured_vertex_buffer: Default::default(), textured_vertex_buffer: Default::default(),
vertex_buffers: vec![],
shader_kernels: shader_kernels, shader_kernels: shader_kernels,
texture_store: Default::default(), texture_store: Default::default(),
@ -235,8 +231,15 @@ impl Canvas {
match drawable.get_texture_id() { match drawable.get_texture_id() {
Some(id) => { Some(id) => {
// This dont work self.textured_drawables
self.textured_drawables.get(&id).unwrap(); .entry(id)
.or_insert(Vec::new())
.extend(drawable.get_vertices().iter().map(|n|
Vertex2D {
position: [n.0, n.1],
}
));
}, },
None => { None => {
let colors = drawable.get_color(); let colors = drawable.get_color();
@ -254,74 +257,58 @@ impl Canvas {
} }
fn get_texture(&mut self, texture_id: String) -> Arc<ImmutableImage<Format>> { fn get_texture(&self, texture_id: String) -> Arc<ImmutableImage<Format>> {
if let Some(i) = self.texture_store.get(&texture_id) { if let Some(i) = self.texture_store.get(&texture_id) {
return i.clone(); return i.clone();
} else { } else {
self.load_texture_from_filename(texture_id) panic!("{} : Texture not loaded", texture_id);
} }
} }
pub fn allocate_vertex_buffers(&mut self, device: Arc<Device>) {
pub fn allocate_colored_vertex_buffers(&mut self, device: Arc<Device>) {
self.vertex_buffers.clear();
self.colored_vertex_buffer.clear(); self.colored_vertex_buffer.clear();
self.textured_vertex_buffer.clear();
self.vertex_buffers.push(
CpuAccessibleBuffer::from_iter(
device.clone(),
BufferUsage::vertex_buffer(),
self.colored_drawables.iter().cloned()
).unwrap()
);
self.colored_vertex_buffer.push( self.colored_vertex_buffer.push(
CpuAccessibleBuffer::from_iter( ImmutableBuffer::from_iter(
device.clone(), self.colored_drawables.iter().cloned(),
BufferUsage::vertex_buffer(), BufferUsage::vertex_buffer(),
self.colored_drawables.iter().cloned() self.queue.clone(),
).unwrap() ).unwrap().0
); );
}
// I guess these go out as an array. So I can add multiple descriptor sets
// So probably needs to know which shaders are for textures for (k, v) in self.textured_drawables.iter() {
// needs to associate this descriptor set to a texture (key-pair, tuple) self.textured_vertex_buffer.insert(
// k.clone(),
ImmutableBuffer::from_iter(
self.textured_drawables.get(k).iter().cloned(),
BufferUsage::vertex_buffer(),
self.queue.clone(),
).unwrap().0
);
}
}
// So this is fine for now. But I want to be able to: fn get_solid_color_descriptor_set(&self) -> Box<dyn DescriptorSet + Send + Sync> {
// Choose which texture I want to add to this descriptor set. let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
// Add multiple textures PersistentDescriptorSet::start(
// Choose which shader and pipeline it should run on self.shader_kernels.get(&ShaderType::SOLID).unwrap().clone().get_pipeline().clone(), 0
).build().unwrap());
o
}
fn get_descriptor_set(&mut self, fn get_textured_descriptor_set(&self, texture_id: String)
shader_type: &ShaderType, -> Box<dyn DescriptorSet + Send + Sync> {
texture_id: String) -> Box<dyn DescriptorSet + Send + Sync> {
match shader_type { let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
ShaderType::SOLID => { PersistentDescriptorSet::start(
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new( self.shader_kernels.get(&ShaderType::TEXTURED).unwrap().clone().get_pipeline().clone(), 0
PersistentDescriptorSet::start( )
self.shader_kernels.get(&shader_type).unwrap().clone().get_pipeline().clone(), 0 .add_sampled_image(self.get_texture(texture_id), self.sampler.clone()).unwrap()
).build().unwrap()); .build().unwrap());
o o
},
ShaderType::TEXTURED => {
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
PersistentDescriptorSet::start(
self.shader_kernels.get(&shader_type).unwrap().clone().get_pipeline().clone(), 0
)
.add_sampled_image(self.get_texture(texture_id), self.sampler.clone()).unwrap()
.build().unwrap());
o
},
ShaderType::COMPUTE => {
unimplemented!("Compute dont work here");
},
}
} }
// This is the image which is written to by the write compute buffer // This is the image which is written to by the write compute buffer
@ -345,10 +332,7 @@ impl Canvas {
} }
/* /*
So I need the image set in order to get my texture or compute texture So I need the image set in order to get my texture or compute texture
Done Done
@ -369,14 +353,13 @@ impl Canvas {
I just need to add a third option on sprite and allow it to have a swap buffer I just need to add a third option on sprite and allow it to have a swap buffer
*/ */
pub fn draw_commands(&mut self, pub fn draw_commands(&self,
command_buffer: AutoCommandBufferBuilder, command_buffer: &mut AutoCommandBufferBuilder,
framebuffers: Vec<Arc<dyn FramebufferAbstract + Send + Sync>>, framebuffers: Vec<Arc<dyn FramebufferAbstract + Send + Sync>>,
image_num: usize) -> AutoCommandBufferBuilder { image_num: usize) {
// Specify the color to clear the framebuffer with i.e. blue // 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 clear_values = vec!(ClearValue::Float([0.0, 0.0, 1.0, 1.0]));
@ -388,34 +371,29 @@ impl Canvas {
).unwrap(); ).unwrap();
// So I need to do this without borrowing self from the shader_kernels.... for (shader_type, kernel) in self.shader_kernels.clone().iter() {
// Maybe self referential struct issue cropping up match shader_type {
// ShaderType::SOLID => {
// command_buffer = command_buffer.draw(
// kernel.clone().get_pipeline().clone(),
// &dynamic_state.clone(), self.colored_vertex_buffer.clone(),
// vec![self.get_solid_color_descriptor_set()], ()
// ).unwrap();
// },
// ShaderType::TEXTURED => {
command_buffer = command_buffer.draw(
for (shader_type, kernel) in self.shader_kernels.iter() { kernel.clone().get_pipeline().clone(),
command_buffer = command_buffer.draw( &dynamic_state.clone(), self.colored_vertex_buffer.clone(),
kernel.clone().get_pipeline().clone(), vec![self.get_textured_descriptor_set(String::from("funky-bird"))], ()
&dynamic_state.clone(), self.vertex_buffers.clone(), ).unwrap();
vec![self.get_descriptor_set(shader_type, String::from("texture"))], () },
).unwrap(); ShaderType::COMPUTE => {},
}
} }
// command_buffer = command_buffer
// .draw(self.shader_kernels.clone().unwrap().get_pipeline(),
// &dynamic_state.clone(), self.vertex_buffers,
// vec![self.get_gui_image_set()], ())
// .unwrap();
//
command_buffer
.end_render_pass() .end_render_pass()
.unwrap() .unwrap();
} }
} }

@ -46,6 +46,7 @@ use winit::{EventsLoop, WindowBuilder, WindowEvent, Event, DeviceEvent, VirtualK
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use vulkano_win::VkSurfaceBuild; use vulkano_win::VkSurfaceBuild;
use sprite::Sprite; use sprite::Sprite;
use crate::canvas::Canvas;
mod util; mod util;
@ -138,7 +139,7 @@ fn main() {
} }
frame_future = processor.run(&surface, frame_future); (frame_future) = processor.run(&surface, frame_future);
} }
} }

@ -45,7 +45,8 @@ use crate::util::compute_image::ComputeImage;
use vulkano::descriptor::descriptor::DescriptorDesc; use vulkano::descriptor::descriptor::DescriptorDesc;
use crate::vertex_2d::ColoredVertex2D; 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 /// This method is called once during initialization, then again whenever the window is resized
@ -99,7 +100,8 @@ pub struct VkProcessor<'a> {
swapchain_recreate_needed: bool, swapchain_recreate_needed: bool,
capabilities: Capabilities capabilities: Capabilities,
canvas: Canvas,
} }
@ -107,8 +109,6 @@ impl<'a> VkProcessor<'a> {
pub fn new(instance: &'a Arc<Instance>, surface: &'a Arc<Surface<Window>>) -> VkProcessor<'a> { pub fn new(instance: &'a Arc<Instance>, surface: &'a Arc<Surface<Window>>) -> VkProcessor<'a> {
let physical = PhysicalDevice::enumerate(instance).next().unwrap(); let physical = PhysicalDevice::enumerate(instance).next().unwrap();
let queue_family = physical.queue_families().find(|&q| { let queue_family = physical.queue_families().find(|&q| {
@ -126,11 +126,13 @@ impl<'a> VkProcessor<'a> {
[(queue_family, 0.5)].iter().cloned()).unwrap(); [(queue_family, 0.5)].iter().cloned()).unwrap();
let queue = queues.next().unwrap(); let queue = queues.next().unwrap();
let capabilities = surface.capabilities(physical).unwrap();
VkProcessor { VkProcessor {
instance: instance.clone(), instance: instance.clone(),
physical: physical.clone(), physical: physical.clone(),
device: device.clone(), device: device.clone(),
queue: queue, queue: queue.clone(),
queues: queues, queues: queues,
dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None }, dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None },
shader_kernels: None, shader_kernels: None,
@ -142,7 +144,8 @@ impl<'a> VkProcessor<'a> {
swapchain: None, swapchain: None,
swapchain_images: None, swapchain_images: None,
swapchain_recreate_needed: false, swapchain_recreate_needed: false,
capabilities: surface.capabilities(physical).unwrap() capabilities: capabilities.clone(),
canvas: Canvas::new(queue, device, physical, capabilities)
} }
} }
@ -346,7 +349,10 @@ impl<'a> VkProcessor<'a> {
pub fn run(&mut self, surface: &'a Arc<Surface<Window>>, mut frame_future: Box<dyn GpuFuture>) -> Box<dyn GpuFuture> { pub fn run(&mut self,
surface: &'a Arc<Surface<Window>>,
mut frame_future: Box<dyn GpuFuture>,
) -> Box<dyn GpuFuture> {
let mut framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(), let mut framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(),
self.shader_kernels.clone().unwrap().render_pass.clone(), self.shader_kernels.clone().unwrap().render_pass.clone(),
@ -397,26 +403,20 @@ impl<'a> VkProcessor<'a> {
.get_descriptor_set(self.compute_kernel.clone().unwrap().clone().get_pipeline()).clone(), ()).unwrap() .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(), .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();
.begin_render_pass(framebuffers[image_num].clone(), false, clear_values.clone())
.unwrap()
.draw(self.shader_kernels.clone().unwrap().get_pipeline(), /*
&self.dynamic_state.clone(), v, Fuck. So this is a problem...
vec![self.get_image_set()], ())
.unwrap();
command_buffer = command_buffer.draw(self.shader_kernels.clone().unwrap().get_pipeline(), I can't replace canvas.
&self.dynamic_state.clone(), v2, */
vec![self.get_gui_image_set()], ())
.unwrap();
let command_buffer = command_buffer self.canvas.draw_commands(&mut command_buffer, framebuffers, image_num);
.end_render_pass()
.unwrap() //self.canvas = mem::replace(&mut self.canvas,
.build().unwrap(); let command_buffer = command_buffer.build().unwrap();
// Wait on the previous frame, then execute the command buffer and present the image // Wait on the previous frame, then execute the command buffer and present the image
let future = frame_future.join(acquire_future) let future = frame_future.join(acquire_future)

Loading…
Cancel
Save