From 2207c41956275aab4f21c5025155f66a03347e67 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 7 Aug 2019 23:36:52 -0700 Subject: [PATCH] Brainstorming the design. --- src/canvas.rs | 71 ++++++++++++++++++++++++++++++++++++++++----------- src/sprite.rs | 12 ++++----- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/canvas.rs b/src/canvas.rs index fdbb0b90..b43149eb 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -16,6 +16,7 @@ use vulkano::descriptor::DescriptorSet; use vulkano::descriptor::descriptor_set::PersistentDescriptorSet; use std::path::PathBuf; use image::GenericImageView; +use crate::util::compute_image::ComputeImage; // Canvas is the accumulator of Sprites for drawing // Needs to know: @@ -101,11 +102,38 @@ impl Vertex for ColoredVertex2D { pub trait Drawable { fn get_vertices(&self) -> Vec<(f32, f32)>; fn get_color(&self) -> (f32, f32, f32, f32); - fn get_texture_id(&self) -> Option; + fn get_texture_id(&self) -> Option; } +/* + + +OASIJDOQIWEJFOWIEJFOWIEJFOWEIJFOIWEJFOIW + + +Right now I'm in the middle of adding texture ownership to the canvas. +I'm aiming at being able to load them on the fly from what texture name (String) the +sprite is requesting. This might be too slow though as I can't really avoid a lookup table +somewhere in the code. + + + + + +*/ + +// Need three types of shaders. Solid, Textured, Compute +#[derive(PartialEq)] +#[derive(Eq)] +#[derive(Hash)] +pub enum ShaderType { + SOLID = 0, + TEXTURED = 1, + COMPUTE = 2 +} + pub struct Canvas { colored_drawables : Vec, @@ -114,7 +142,7 @@ pub struct Canvas { vertex_buffers: Vec>, - shader_kernels: Vec, + shader_kernels: HashMap, textures: Vec>>, } @@ -128,8 +156,7 @@ impl Canvas { textured_drawables: Default::default(), vertex_buffers: vec![], - - shader_kernels: Vec::new(), + shader_kernels: HashMap::new(), textures: vec![] } } @@ -219,16 +246,25 @@ impl Canvas { ); } - // The image set is the containing object for all texture and image hooks. - fn get_texture_set(&mut self, device: Arc) -> Box { + // 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 + // needs to associate this descriptor set to a texture (key-pair, tuple) + // + + // So this is fine for now. But I want to be able to: + // Choose which texture I want to add to this descriptor set. + // Add multiple textures + // Choose which shader and pipeline it should run on + fn get_texture_descriptor_set(&mut self, device: Arc) -> Box { let sampler = 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(); - let o : Box = Box::new( + let o : Box = Box::new( PersistentDescriptorSet::start( - self.shader_kernels.get(0).unwrap().clone().unwrap().get_pipeline(), 0 + self.shader_kernels.get(&ShaderType::TEXTURED).unwrap().clone().get_pipeline(), 0 ) .add_sampled_image(self.textures.get(0).unwrap().clone(), sampler.clone()).unwrap() .build().unwrap()); @@ -236,17 +272,19 @@ impl Canvas { } // The image set is the containing object for all texture and image hooks. - fn get_compute_swap_set(&mut self, device: Arc) -> Box { + fn get_compute_swap_descriptor_set(&mut self, + device: Arc, + compute_image: &ComputeImage) -> Box { let sampler = 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(); - let o : Box = Box::new( + let o : Box = Box::new( PersistentDescriptorSet::start( - self.shader_kernels.get(0).unwrap().clone().unwrap().get_pipeline(), 0 + self.shader_kernels.get(&ShaderType::COMPUTE).clone().unwrap().clone().get_pipeline(), 0 ) - .add_image(self.compute_image.clone().unwrap().clone().get_swap_buffer().clone()).unwrap() + .add_image(compute_image.clone().get_swap_buffer().clone()).unwrap() .build().unwrap()); o } @@ -256,14 +294,17 @@ impl Canvas { 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 - adds vk processor owned texture - adds compute image taken from the ComputeImage + 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? diff --git a/src/sprite.rs b/src/sprite.rs index 49f376ff..03c728b4 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -10,7 +10,7 @@ pub struct Sprite { color: (f32, f32, f32, f32), textured: bool, - texture_id: i32, + texture_id: Option, } @@ -39,11 +39,11 @@ impl Sprite { size: size, color: color, textured: false, - texture_id: 0 + texture_id: None } } - pub fn new_with_texture(position: (f32, f32), size: (u32, u32), texture_id: i32) -> Sprite { + pub fn new_with_texture(position: (f32, f32), size: (u32, u32), texture_id: String) -> Sprite { let fsize = (size.0 as f32, size.1 as f32); @@ -60,7 +60,7 @@ impl Sprite { size, color: (0.0, 0.0, 0.0, 0.0), textured: false, - texture_id + texture_id: Some(texture_id) } } @@ -77,10 +77,10 @@ impl Drawable for Sprite { self.color.clone() } - fn get_texture_id(&self) -> Option { + fn get_texture_id(&self) -> Option { match self.textured { true => { - Some(self.texture_id.clone()) + self.texture_id.clone() }, false => None, }