use crate::util::vertex_2d::{Vertex2D};
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use std::collections::HashMap;
use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer};
use std::sync::Arc;
use vulkano::format::{ClearValue, Format};
use vulkano::framebuffer::{FramebufferAbstract, Framebuffer};
use vulkano::device::{Device, Queue};
use vulkano::instance::PhysicalDevice;
use vulkano::image::immutable::ImmutableImage;
use vulkano::image::{Dimensions, ImageAccess, ImageDimensions, SwapchainImage, ImageUsage, AttachmentImage};
use vulkano::sampler::{Sampler, SamplerAddressMode, MipmapMode, Filter};
use vulkano::descriptor::DescriptorSet;
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
use std::path::PathBuf;
use image::GenericImageView;
use std::iter::FromIterator;
use vulkano::swapchain::Capabilities;
use winit::Window;
use vulkano::pipeline::viewport::Viewport;
use vulkano::descriptor::descriptor::DescriptorDescTy::TexelBuffer;
use crate::canvas::canvas_frame::CanvasFrame;
use std::hash::Hash;
use crate::canvas::shader::generic_shader::GenericShader;
use crate::canvas::shader::common::CompiledGraphicsPipeline;
use shade_runner::CompiledShader;
use crate::canvas::{CanvasTextureHandle, CanvasImageHandle};
use crate::canvas::shader::dynamic_vertex::RuntimeVertexDef;
use crate::canvas::canvas_buffer::CanvasImage;
pub trait Vertex {
fn position(&self) -> (f32, f32) {
(0.0, 0.0)
}
fn color(&self) -> Option<(f32, f32, f32, f32)> {
Some((0., 0., 0., 0.))
}
}
pub trait Drawable {
fn get_vertices(&self) -> Vec<(f32, f32)>;
fn get_color(&self) -> (f32, f32, f32, f32);
fn get_texture_handle(&self) -> Option<Arc<CanvasTextureHandle>>;
fn get_image_handle(&self) -> Option<Arc<CanvasImageHandle>>;
}
#[derive(PartialEq, Eq, Hash, Clone)]
pub enum ShaderType {
SOLID = 0,
TEXTURED = 1,
IMAGE = 2,
}
#[derive(Clone)]
pub struct CanvasTexture {
handle: Arc<CanvasTextureHandle>,
buffer: Arc<ImmutableImage<Format>>,
name: String,
size: (u32, u32),
}
impl CanvasTexture {
fn get_descriptor_set(&self,
shader: Arc<CompiledGraphicsPipeline>,
sampler: Arc<Sampler>) -> Box<dyn DescriptorSet + Send + Sync> {
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
PersistentDescriptorSet::start(
shader.clone().get_pipeline().clone(), 0,
)
.add_sampled_image(self.buffer.clone(), sampler.clone()).unwrap()
.build().unwrap());
o
}
}
#[derive(Clone)]
pub struct CanvasState {
dynamic_state: DynamicState,
sampler: Arc<Sampler>,
image_buffers: Vec<Arc<CanvasImage>>,
texture_buffers: Vec<Arc<CanvasTexture>>,
shader_buffers: HashMap<String, Arc<CompiledGraphicsPipeline>>,
colored_drawables: Vec<RuntimeVertexDef>,
colored_vertex_buffer: Vec<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,
textured_drawables: HashMap<Arc<CanvasTextureHandle>, Vec<Vec<RuntimeVertexDef>>>,
textured_vertex_buffer: HashMap<Arc<CanvasTextureHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,
image_drawables: HashMap<Arc<CanvasImageHandle>, Vec<Vec<RuntimeVertexDef>>>,
image_vertex_buffer: HashMap<Arc<CanvasImageHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,
queue: Arc<Queue>,
device: Arc<Device>,
}
impl CanvasState {
pub fn window_size_dependent_setup(&mut self, images: &[Arc<SwapchainImage<Window>>])
-> Vec<Arc<dyn FramebufferAbstract + Send + Sync>> {
let dimensions = images[0].dimensions();
self.dynamic_state.viewports =
Some(vec![Viewport {
origin: [0.0, 0.0],
dimensions: [dimensions.width() as f32, dimensions.height() as f32],
depth_range: 0.0..1.0,
}]);
images.iter().map(|image| {
Arc::new(
Framebuffer::start(self.shader_buffers.get("color-passthrough").unwrap().get_renderpass().clone())
.add(image.clone()).unwrap()
.build().unwrap()
) as Arc<dyn FramebufferAbstract + Send + Sync>
}).collect::<Vec<_>>()
}
pub fn new(queue: Arc<Queue>,
device: Arc<Device>,
physical: PhysicalDevice,
capabilities: Capabilities) -> CanvasState {
let solid_color_kernel = String::from("color-passthrough");
let texture_kernel = String::from("simple_texture");
CanvasState {
dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None, compare_mask: None, write_mask: None, reference: None },
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(),
image_buffers: vec![],
texture_buffers: vec![],
shader_buffers: HashMap::from_iter(vec![
]),
colored_drawables: vec![],
colored_vertex_buffer: vec![],
textured_drawables: HashMap::default(),
textured_vertex_buffer: Default::default(),
image_drawables: Default::default(),
image_vertex_buffer: Default::default(),
queue: queue.clone(),
device: device.clone(),
}
}
pub fn create_image(&mut self, dimensions: (u32, u32), usage: ImageUsage) -> Arc<CanvasImageHandle> {
let handle = Arc::new(CanvasImageHandle { handle: self.image_buffers.len() as u32});
let image = CanvasImage {
handle: handle.clone(),
buffer: AttachmentImage::with_usage(
self.device.clone(),
[dimensions.0, dimensions.1],
Format::R8G8B8A8Uint,
usage).unwrap(),
size: dimensions,
};
self.image_buffers.push(Arc::new(image));
handle
}
pub fn get_image(&self, image_handle: Arc<CanvasImageHandle>) -> Arc<AttachmentImage> {
self.image_buffers.get((*image_handle).clone().handle as usize).unwrap()
.clone().buffer.clone()
}
fn get_texture_from_file(&self, image_filename: String) -> Arc<ImmutableImage<Format>> {
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,
self.queue.clone(),
).unwrap();
texture
}
pub fn load_texture(&mut self, filename: String) -> Option<Arc<CanvasTextureHandle>> {
let texture_buffer = self.get_texture_from_file(filename.clone());
let handle = Arc::new(CanvasTextureHandle {
handle: self.texture_buffers.len() as u32
});
let texture = Arc::new(CanvasTexture {
handle: handle.clone(),
buffer: self.get_texture_from_file(filename.clone()),
name: filename.clone(),
size: (0, 0),
});
self.texture_buffers.push(texture);
Some(handle)
}
pub fn get_texture_handle(&self, texture_name: String)
-> Option<Arc<CanvasTextureHandle>> {
for i in self.texture_buffers.clone() {
if i.name == texture_name {
return Some(i.handle.clone());
}
}
None
}
pub fn get_texture(&self, texture_handle: Arc<CanvasTextureHandle>)
-> Arc<ImmutableImage<Format>> {
let handle = texture_handle.handle as usize;
if let Some(i) = self.texture_buffers.get(handle) {
return i.clone().buffer.clone();
} else {
panic!("{} : Texture not loaded", handle);
}
}
pub fn draw(&mut self, canvas_frame: CanvasFrame) {
self.textured_drawables = canvas_frame.textured_drawables;
self.colored_drawables = canvas_frame.colored_drawables;
self.image_drawables = canvas_frame.image_drawables;
self.allocate_vertex_buffers(self.device.clone());
}
fn allocate_vertex_buffers(&mut self, device: Arc<Device>) {
self.colored_vertex_buffer.clear();
self.textured_vertex_buffer.clear();
self.image_vertex_buffer.clear();
self.colored_vertex_buffer.push(
ImmutableBuffer::from_iter(
self.colored_drawables.iter().cloned(),
BufferUsage::vertex_buffer(),
self.queue.clone(),
).unwrap().0
);
for (k, v) in self.textured_drawables.drain() {
self.textured_vertex_buffer.insert(
k.clone(),
ImmutableBuffer::from_iter(
v.first().unwrap().iter().cloned(),
BufferUsage::vertex_buffer(),
self.queue.clone(),
).unwrap().0,
);
}
}
fn get_solid_color_descriptor_set(&self, kernel: Arc<CompiledGraphicsPipeline>) -> Box<dyn DescriptorSet + Send + Sync> {
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
PersistentDescriptorSet::start(
kernel.clone().get_pipeline().clone(), 0,
).build().unwrap());
o
}
pub fn draw_commands(&self,
mut command_buffer: AutoCommandBufferBuilder,
framebuffers: Vec<Arc<dyn FramebufferAbstract + Send + Sync>>,
image_num: usize) -> AutoCommandBufferBuilder {
let clear_values = vec!(ClearValue::Float([0.0, 0.0, 1.0, 1.0]));
let mut command_buffer = command_buffer.begin_render_pass(
framebuffers[image_num].clone(), false, clear_values.clone(),
).unwrap();
let mut shader = self.shader_buffers.get("color-passthrough").unwrap().clone();
command_buffer = command_buffer.draw(
shader.get_pipeline().clone(),
&self.dynamic_state.clone(),
self.colored_vertex_buffer.clone(),
(), (),
).unwrap();
let mut shader = self.shader_buffers.get("simple_texture").unwrap().clone();
let handle = self.get_texture_handle(String::from("funky-bird.jpg")).unwrap().clone();
let descriptor_set = self.texture_buffers.first().clone().unwrap().clone()
.get_descriptor_set(shader.clone(), self.sampler.clone());
let vertex_buffer = self.textured_vertex_buffer.get(&handle).unwrap().clone();
command_buffer = command_buffer.draw(
shader.get_pipeline().clone(),
&self.dynamic_state.clone(), vec![vertex_buffer],
vec![descriptor_set], ()
).unwrap();
command_buffer
.end_render_pass()
.unwrap()
}
}