|
|
@ -22,6 +22,7 @@ use vulkano::descriptor::descriptor::DescriptorDescTy::TexelBuffer;
|
|
|
|
use crate::canvas_frame::CanvasFrame;
|
|
|
|
use crate::canvas_frame::CanvasFrame;
|
|
|
|
use std::hash::Hash;
|
|
|
|
use std::hash::Hash;
|
|
|
|
use crate::canvas_shader::CanvasShader;
|
|
|
|
use crate::canvas_shader::CanvasShader;
|
|
|
|
|
|
|
|
use crate::canvas_buffer::{CanvasImage, CanvasTexture};
|
|
|
|
|
|
|
|
|
|
|
|
// Canvas is the accumulator of Sprites for drawing
|
|
|
|
// Canvas is the accumulator of Sprites for drawing
|
|
|
|
|
|
|
|
|
|
|
@ -103,48 +104,6 @@ pub struct CanvasShaderHandle {
|
|
|
|
pub handle: u32
|
|
|
|
pub handle: u32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[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<CanvasShader>,
|
|
|
|
|
|
|
|
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 CanvasImage {
|
|
|
|
|
|
|
|
handle: Arc<CanvasImageHandle>,
|
|
|
|
|
|
|
|
buffer: Arc<AttachmentImage>,
|
|
|
|
|
|
|
|
size: (u32, u32),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl CanvasImage {
|
|
|
|
|
|
|
|
fn get_descriptor_set(&mut self, shader: Arc<CanvasShader>)
|
|
|
|
|
|
|
|
-> Box<dyn DescriptorSet + Send + Sync> {
|
|
|
|
|
|
|
|
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
|
|
|
|
|
|
|
|
PersistentDescriptorSet::start(
|
|
|
|
|
|
|
|
shader.clone().get_pipeline().clone(), 0,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.add_image(self.buffer.clone()).unwrap()
|
|
|
|
|
|
|
|
.build().unwrap());
|
|
|
|
|
|
|
|
o
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct CanvasState {
|
|
|
|
pub struct CanvasState {
|
|
|
|
dynamic_state: DynamicState,
|
|
|
|
dynamic_state: DynamicState,
|
|
|
@ -199,7 +158,6 @@ impl CanvasState {
|
|
|
|
device: Arc<Device>,
|
|
|
|
device: Arc<Device>,
|
|
|
|
physical: PhysicalDevice,
|
|
|
|
physical: PhysicalDevice,
|
|
|
|
capabilities: Capabilities) -> CanvasState {
|
|
|
|
capabilities: Capabilities) -> CanvasState {
|
|
|
|
|
|
|
|
|
|
|
|
let solid_color_kernel = String::from("color-passthrough");
|
|
|
|
let solid_color_kernel = String::from("color-passthrough");
|
|
|
|
let texture_kernel = String::from("simple_texture");
|
|
|
|
let texture_kernel = String::from("simple_texture");
|
|
|
|
|
|
|
|
|
|
|
@ -212,16 +170,16 @@ impl CanvasState {
|
|
|
|
texture_buffers: vec![],
|
|
|
|
texture_buffers: vec![],
|
|
|
|
shader_buffers: HashMap::from_iter(vec![
|
|
|
|
shader_buffers: HashMap::from_iter(vec![
|
|
|
|
(solid_color_kernel.clone(), Arc::new(CanvasShader::new_colored(solid_color_kernel.clone(),
|
|
|
|
(solid_color_kernel.clone(), Arc::new(CanvasShader::new_colored(solid_color_kernel.clone(),
|
|
|
|
capabilities.clone(),
|
|
|
|
capabilities.clone(),
|
|
|
|
queue.clone(),
|
|
|
|
queue.clone(),
|
|
|
|
physical.clone(),
|
|
|
|
physical.clone(),
|
|
|
|
device.clone()))
|
|
|
|
device.clone()))
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(texture_kernel.clone(), Arc::new(CanvasShader::new_textured(texture_kernel.clone(),
|
|
|
|
(texture_kernel.clone(), Arc::new(CanvasShader::new_textured(texture_kernel.clone(),
|
|
|
|
capabilities.clone(),
|
|
|
|
capabilities.clone(),
|
|
|
|
queue.clone(),
|
|
|
|
queue.clone(),
|
|
|
|
physical.clone(),
|
|
|
|
physical.clone(),
|
|
|
|
device.clone()))
|
|
|
|
device.clone()))
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
|
|
|
|
|
|
|
@ -238,8 +196,7 @@ impl CanvasState {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn create_image(&mut self, dimensions: (u32, u32), usage: ImageUsage) -> Arc<CanvasImageHandle> {
|
|
|
|
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 handle = Arc::new(CanvasImageHandle { handle: self.image_buffers.len() as u32});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let image = CanvasImage {
|
|
|
|
let image = CanvasImage {
|
|
|
|
handle: handle.clone(),
|
|
|
|
handle: handle.clone(),
|
|
|
@ -323,8 +280,7 @@ impl CanvasState {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_texture_handle(&self, texture_name: String)
|
|
|
|
pub fn get_texture_handle(&self, texture_name: String)
|
|
|
|
-> Option<Arc<CanvasTextureHandle>> {
|
|
|
|
-> Option<Arc<CanvasTextureHandle>> {
|
|
|
|
|
|
|
|
|
|
|
|
for i in self.texture_buffers.clone() {
|
|
|
|
for i in self.texture_buffers.clone() {
|
|
|
|
if i.name == texture_name {
|
|
|
|
if i.name == texture_name {
|
|
|
|
return Some(i.handle.clone());
|
|
|
|
return Some(i.handle.clone());
|
|
|
@ -334,8 +290,7 @@ impl CanvasState {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_texture(&self, texture_handle: Arc<CanvasTextureHandle>)
|
|
|
|
pub fn get_texture(&self, texture_handle: Arc<CanvasTextureHandle>)
|
|
|
|
-> Arc<ImmutableImage<Format>> {
|
|
|
|
-> Arc<ImmutableImage<Format>> {
|
|
|
|
|
|
|
|
|
|
|
|
let handle = texture_handle.handle as usize;
|
|
|
|
let handle = texture_handle.handle as usize;
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(i) = self.texture_buffers.get(handle) {
|
|
|
|
if let Some(i) = self.texture_buffers.get(handle) {
|
|
|
@ -355,10 +310,25 @@ impl CanvasState {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn allocate_vertex_buffers(&mut self, device: Arc<Device>) {
|
|
|
|
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.image_vertex_buffer.clear();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
So a bit of brainstorming with the shaders:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I compile shaders into their respective buffers and add them to a descriptor set
|
|
|
|
|
|
|
|
along with the textures or whatever other resource buffer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
So I'm gonna fix that texturing issue by adding vertex texture coordinate attributes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Still don't really know how I'm gonna do this...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Going to definitely need to use the CpuAccessbileBuffer
|
|
|
|
|
|
|
|
* Maybe calculate deltas between frames???
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
//TODO should probably use cpu accessible buffer instead of recreating immutes each frame
|
|
|
|
//TODO should probably use cpu accessible buffer instead of recreating immutes each frame
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
CpuAccessibleBuffer::from_iter(
|
|
|
|
CpuAccessibleBuffer::from_iter(
|
|
|
@ -369,29 +339,67 @@ impl CanvasState {
|
|
|
|
).unwrap().0;
|
|
|
|
).unwrap().0;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
self.colored_vertex_buffer.push(
|
|
|
|
//if self.colored_vertex_buffer.len() == 0 {
|
|
|
|
ImmutableBuffer::from_iter(
|
|
|
|
|
|
|
|
self.colored_drawables.iter().cloned(),
|
|
|
|
|
|
|
|
BufferUsage::vertex_buffer(),
|
|
|
|
|
|
|
|
self.queue.clone(),
|
|
|
|
|
|
|
|
).unwrap().0
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (k, v) in self.textured_drawables.drain() {
|
|
|
|
|
|
|
|
println!("{:?}", v.len());
|
|
|
|
|
|
|
|
self.textured_vertex_buffer.insert(
|
|
|
|
self.colored_vertex_buffer.clear();
|
|
|
|
k.clone(),
|
|
|
|
{
|
|
|
|
|
|
|
|
let g = hprof::enter("Colored Vertex Buffer : From Data");
|
|
|
|
|
|
|
|
self.colored_vertex_buffer.push(
|
|
|
|
|
|
|
|
ImmutableBuffer::from_data(self.colored_drawables.clone(),
|
|
|
|
|
|
|
|
BufferUsage::vertex_buffer(),
|
|
|
|
|
|
|
|
self.queue.clone()).unwrap().0
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let g = hprof::enter("Colored Vertex Buffer : From Iter");
|
|
|
|
|
|
|
|
self.colored_vertex_buffer.push(
|
|
|
|
ImmutableBuffer::from_iter(
|
|
|
|
ImmutableBuffer::from_iter(
|
|
|
|
v.first().unwrap().iter().cloned(),
|
|
|
|
self.colored_drawables.iter().cloned(),
|
|
|
|
BufferUsage::vertex_buffer(),
|
|
|
|
BufferUsage::vertex_buffer(),
|
|
|
|
self.queue.clone(),
|
|
|
|
self.queue.clone(),
|
|
|
|
).unwrap().0,
|
|
|
|
).unwrap().0
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.colored_vertex_buffer.clear();
|
|
|
|
|
|
|
|
//println!("{:?}", self.colored_vertex_buffer.last().unwrap().size());
|
|
|
|
|
|
|
|
//self.colored_vertex_buffer.pop();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// println!("{} {}", self.textured_vertex_buffer.len() , self.textured_drawables.len());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if self.textured_vertex_buffer.len() != self.textured_drawables.len() {
|
|
|
|
|
|
|
|
self.textured_vertex_buffer.clear();
|
|
|
|
|
|
|
|
for (k, v) in self.textured_drawables.drain() {
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let g = hprof::enter("Textured Vertex Buffer : From Iter");
|
|
|
|
|
|
|
|
self.textured_vertex_buffer.insert(
|
|
|
|
|
|
|
|
k.clone(),
|
|
|
|
|
|
|
|
ImmutableBuffer::from_iter(
|
|
|
|
|
|
|
|
v.first().unwrap().iter().cloned(),
|
|
|
|
|
|
|
|
BufferUsage::vertex_buffer(),
|
|
|
|
|
|
|
|
self.queue.clone(),
|
|
|
|
|
|
|
|
).unwrap().0,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// let g = hprof::enter("Textured Vertex Buffer : From Data");
|
|
|
|
|
|
|
|
// self.textured_vertex_buffer.insert(
|
|
|
|
|
|
|
|
// k.clone(),
|
|
|
|
|
|
|
|
// ImmutableBuffer::from_data(v.first().unwrap().clone(),
|
|
|
|
|
|
|
|
// BufferUsage::vertex_buffer(),
|
|
|
|
|
|
|
|
// self.queue.clone()
|
|
|
|
|
|
|
|
// ).unwrap().0
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//self.textured_vertex_buffer.pop();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn get_solid_color_descriptor_set(&self, kernel: Arc<CanvasShader>) -> Box<dyn DescriptorSet + Send + Sync> {
|
|
|
|
fn get_solid_color_descriptor_set(&self, kernel: Arc<CanvasShader>) -> Box<dyn DescriptorSet + Send + Sync> {
|
|
|
|
|
|
|
|
|
|
|
|
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
|
|
|
|
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
|
|
|
|
PersistentDescriptorSet::start(
|
|
|
|
PersistentDescriptorSet::start(
|
|
|
|
kernel.clone().get_pipeline().clone(), 0,
|
|
|
|
kernel.clone().get_pipeline().clone(), 0,
|
|
|
@ -414,32 +422,36 @@ impl CanvasState {
|
|
|
|
// Solid colors
|
|
|
|
// Solid colors
|
|
|
|
let mut shader = self.shader_buffers.get("color-passthrough").unwrap().clone();
|
|
|
|
let mut shader = self.shader_buffers.get("color-passthrough").unwrap().clone();
|
|
|
|
|
|
|
|
|
|
|
|
command_buffer = command_buffer.draw(
|
|
|
|
// This looks a little weird as colored_vertex_buffer is a vec of GPU allocated vecs.
|
|
|
|
shader.get_pipeline().clone(),
|
|
|
|
// But we can pass in multiple vertex buffers
|
|
|
|
&self.dynamic_state.clone(),
|
|
|
|
if !self.colored_vertex_buffer.is_empty() {
|
|
|
|
self.colored_vertex_buffer.clone(),
|
|
|
|
command_buffer = command_buffer.draw(
|
|
|
|
(), (),
|
|
|
|
shader.get_pipeline().clone(),
|
|
|
|
).unwrap();
|
|
|
|
&self.dynamic_state.clone(),
|
|
|
|
|
|
|
|
self.colored_vertex_buffer.clone(),
|
|
|
|
|
|
|
|
(), (),
|
|
|
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Images
|
|
|
|
// Images
|
|
|
|
let mut shader = self.shader_buffers.get("simple_texture").unwrap().clone();
|
|
|
|
let mut shader = self.shader_buffers.get("simple_texture").unwrap().clone();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let handle = self.get_texture_handle(String::from("funky-bird.jpg")).unwrap().clone();
|
|
|
|
if !self.textured_vertex_buffer.is_empty() {
|
|
|
|
|
|
|
|
let handle = self.get_texture_handle(String::from("funky-bird.jpg")).unwrap().clone();
|
|
|
|
|
|
|
|
|
|
|
|
let descriptor_set = self.texture_buffers.first().clone().unwrap().clone()
|
|
|
|
// TODO : BAD BAD BAD. SELECTS FIRST TEXTURE ONLY!!!!!!!!!!!!
|
|
|
|
.get_descriptor_set(shader.clone(), self.sampler.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();
|
|
|
|
let vertex_buffer = self.textured_vertex_buffer.get(&handle).unwrap().clone();
|
|
|
|
|
|
|
|
|
|
|
|
println!("{:?}", self.texture_buffers.len());
|
|
|
|
|
|
|
|
command_buffer = command_buffer.draw(
|
|
|
|
|
|
|
|
shader.get_pipeline().clone(),
|
|
|
|
|
|
|
|
&self.dynamic_state.clone(), vec![vertex_buffer],
|
|
|
|
|
|
|
|
vec![descriptor_set], ()
|
|
|
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
command_buffer = command_buffer.draw(
|
|
|
|
|
|
|
|
shader.get_pipeline().clone(),
|
|
|
|
|
|
|
|
&self.dynamic_state.clone(), vec![vertex_buffer],
|
|
|
|
|
|
|
|
vec![descriptor_set], (),
|
|
|
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
/*for (shader_type, kernel) in self.shader_kernels.clone().iter() {
|
|
|
|
/*for (shader_type, kernel) in self.shader_kernels.clone().iter() {
|
|
|
|
match shader_type {
|
|
|
|
match shader_type {
|
|
|
|
ShaderType::SOLID => {
|
|
|
|
ShaderType::SOLID => {
|
|
|
|