|
|
|
@ -22,46 +22,26 @@ use vulkano::descriptor::descriptor::DescriptorDescTy::TexelBuffer;
|
|
|
|
|
use crate::canvas_frame::CanvasFrame;
|
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
use crate::canvas_shader::CanvasShader;
|
|
|
|
|
|
|
|
|
|
// Canvas is the accumulator of Sprites for drawing
|
|
|
|
|
|
|
|
|
|
// Needs to know:
|
|
|
|
|
// textured?
|
|
|
|
|
// colored?
|
|
|
|
|
// vertices
|
|
|
|
|
|
|
|
|
|
use crate::canvas_buffer::{CanvasImage, CanvasTexture};
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
So framebuffer is tied to the swapchains images as well as the renderpass
|
|
|
|
|
|
|
|
|
|
it appears that renderpass is tied to the individual shader
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// I want to be able to draw 2d sprites.
|
|
|
|
|
|
|
|
|
|
// These sprites might be textured or a single color
|
|
|
|
|
|
|
|
|
|
// All of the single colors will be grouped into one batch using colored vertices.
|
|
|
|
|
// The rest will be grouped by their texture and run individually
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Vertex trait for Drawable Vertices.
|
|
|
|
|
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.))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Vertex for ColoredVertex2D {
|
|
|
|
|
fn position(&self) -> (f32, f32) {
|
|
|
|
|
(0.0, 0.0)
|
|
|
|
@ -72,6 +52,8 @@ impl Vertex for ColoredVertex2D {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A drawable object can be passed into a CanvasFrame to be rendered
|
|
|
|
|
/// Allows Texture or Image drawing via their handles
|
|
|
|
|
pub trait Drawable {
|
|
|
|
|
fn get_vertices(&self) -> Vec<(f32, f32)>;
|
|
|
|
|
fn get_color(&self) -> (f32, f32, f32, f32);
|
|
|
|
@ -79,7 +61,7 @@ pub trait Drawable {
|
|
|
|
|
fn get_image_handle(&self) -> Option<Arc<CanvasImageHandle>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need three types of shaders. Solid, Textured, Image
|
|
|
|
|
/// Legacy ShaderType enum for single type shaders.
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Clone)]
|
|
|
|
|
pub enum ShaderType {
|
|
|
|
|
SOLID = 0,
|
|
|
|
@ -87,64 +69,18 @@ pub enum ShaderType {
|
|
|
|
|
IMAGE = 2,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Typed wrapper for a u32 texture handle (index id)
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct CanvasTextureHandle {
|
|
|
|
|
pub handle: u32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Typed wrapper for a u32 image handle (index id)
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct CanvasImageHandle {
|
|
|
|
|
pub handle: u32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct CanvasShaderHandle {
|
|
|
|
|
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)]
|
|
|
|
|
pub struct CanvasState {
|
|
|
|
|
dynamic_state: DynamicState,
|
|
|
|
@ -200,9 +136,6 @@ impl CanvasState {
|
|
|
|
|
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 },
|
|
|
|
|
sampler: Sampler::new(device.clone(), Filter::Linear, Filter::Linear,
|
|
|
|
@ -210,20 +143,7 @@ impl CanvasState {
|
|
|
|
|
SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(),
|
|
|
|
|
image_buffers: vec![],
|
|
|
|
|
texture_buffers: vec![],
|
|
|
|
|
shader_buffers: HashMap::from_iter(vec![
|
|
|
|
|
(solid_color_kernel.clone(), Arc::new(CanvasShader::new_colored(solid_color_kernel.clone(),
|
|
|
|
|
capabilities.clone(),
|
|
|
|
|
queue.clone(),
|
|
|
|
|
physical.clone(),
|
|
|
|
|
device.clone()))
|
|
|
|
|
),
|
|
|
|
|
(texture_kernel.clone(), Arc::new(CanvasShader::new_textured(texture_kernel.clone(),
|
|
|
|
|
capabilities.clone(),
|
|
|
|
|
queue.clone(),
|
|
|
|
|
physical.clone(),
|
|
|
|
|
device.clone()))
|
|
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
shader_buffers: HashMap::from_iter(vec![]),
|
|
|
|
|
|
|
|
|
|
colored_drawables: vec![],
|
|
|
|
|
colored_vertex_buffer: vec![],
|
|
|
|
@ -238,8 +158,7 @@ impl CanvasState {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
handle: handle.clone(),
|
|
|
|
@ -322,9 +241,24 @@ impl CanvasState {
|
|
|
|
|
Some(handle)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Load and Compile a shader with the filename at resources/shaders
|
|
|
|
|
/// Takes physical and capabilities as we don't store that in Canvas
|
|
|
|
|
pub fn load_shader(&mut self,
|
|
|
|
|
filename: String,
|
|
|
|
|
physical: PhysicalDevice,
|
|
|
|
|
capabilities: Capabilities) {
|
|
|
|
|
|
|
|
|
|
self.shader_buffers.insert(filename.clone(),
|
|
|
|
|
Arc::new(CanvasShader::new_colored(filename.clone(),
|
|
|
|
|
capabilities.clone(),
|
|
|
|
|
self.queue.clone(),
|
|
|
|
|
physical.clone(),
|
|
|
|
|
self.device.clone())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Using the texture name, iterates through the stored textures and matches by the name
|
|
|
|
|
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());
|
|
|
|
@ -333,9 +267,9 @@ impl CanvasState {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Using the texture handle, grab the stored texture and return the buffer
|
|
|
|
|
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) {
|
|
|
|
@ -345,30 +279,22 @@ impl CanvasState {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// After done using this, need to call allocated vertex buffers
|
|
|
|
|
/// Scrape all the values from the CanvasFrame and then allocate the vertex buffers
|
|
|
|
|
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());
|
|
|
|
|
self.allocate_vertex_buffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn allocate_vertex_buffers(&mut self, device: Arc<Device>) {
|
|
|
|
|
self.colored_vertex_buffer.clear();
|
|
|
|
|
self.textured_vertex_buffer.clear();
|
|
|
|
|
self.image_vertex_buffer.clear();
|
|
|
|
|
|
|
|
|
|
//TODO should probably use cpu accessible buffer instead of recreating immutes each frame
|
|
|
|
|
/*
|
|
|
|
|
CpuAccessibleBuffer::from_iter(
|
|
|
|
|
|
|
|
|
|
device.clone(),
|
|
|
|
|
BufferUsage::vertex_buffer(),
|
|
|
|
|
self.colored_drawables.iter().cloned(),
|
|
|
|
|
).unwrap().0;
|
|
|
|
|
*/
|
|
|
|
|
/// draw(canvas_fame) stored all the intermediate information, this function
|
|
|
|
|
/// allocates the vertex buffers using that information
|
|
|
|
|
fn allocate_vertex_buffers(&mut self) {
|
|
|
|
|
|
|
|
|
|
self.colored_vertex_buffer.clear();
|
|
|
|
|
{
|
|
|
|
|
let g = hprof::enter("Colored Vertex Buffer");
|
|
|
|
|
self.colored_vertex_buffer.push(
|
|
|
|
|
ImmutableBuffer::from_iter(
|
|
|
|
|
self.colored_drawables.iter().cloned(),
|
|
|
|
@ -376,9 +302,12 @@ impl CanvasState {
|
|
|
|
|
self.queue.clone(),
|
|
|
|
|
).unwrap().0
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.textured_vertex_buffer.clear();
|
|
|
|
|
{
|
|
|
|
|
let g = hprof::enter("Textured Vertex Buffer");
|
|
|
|
|
for (k, v) in self.textured_drawables.drain() {
|
|
|
|
|
println!("{:?}", v.len());
|
|
|
|
|
self.textured_vertex_buffer.insert(
|
|
|
|
|
k.clone(),
|
|
|
|
|
ImmutableBuffer::from_iter(
|
|
|
|
@ -390,8 +319,24 @@ impl CanvasState {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_solid_color_descriptor_set(&self, kernel: Arc<CanvasShader>) -> Box<dyn DescriptorSet + Send + Sync> {
|
|
|
|
|
self.image_vertex_buffer.clear();
|
|
|
|
|
{
|
|
|
|
|
let g = hprof::enter("Image Vertex Buffer");
|
|
|
|
|
for (k, v) in self.image_drawables.drain() {
|
|
|
|
|
self.image_vertex_buffer.insert(
|
|
|
|
|
k.clone(),
|
|
|
|
|
ImmutableBuffer::from_iter(
|
|
|
|
|
v.first().unwrap().iter().cloned(),
|
|
|
|
|
BufferUsage::vertex_buffer(),
|
|
|
|
|
self.queue.clone(),
|
|
|
|
|
).unwrap().0,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builds the descriptor set for solid colors using the input kernel (needs to support solid colors)
|
|
|
|
|
fn get_solid_color_descriptor_set(&self, kernel: Arc<CanvasShader>) -> Box<dyn DescriptorSet + Send + Sync> {
|
|
|
|
|
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
|
|
|
|
|
PersistentDescriptorSet::start(
|
|
|
|
|
kernel.clone().get_pipeline().clone(), 0,
|
|
|
|
@ -399,6 +344,7 @@ impl CanvasState {
|
|
|
|
|
o
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pushes the draw commands s
|
|
|
|
|
pub fn draw_commands(&self,
|
|
|
|
|
mut command_buffer: AutoCommandBufferBuilder,
|
|
|
|
|
framebuffers: Vec<Arc<dyn FramebufferAbstract + Send + Sync>>,
|
|
|
|
@ -414,47 +360,55 @@ impl CanvasState {
|
|
|
|
|
// Solid colors
|
|
|
|
|
let mut shader = self.shader_buffers.get("color-passthrough").unwrap().clone();
|
|
|
|
|
|
|
|
|
|
// This looks a little weird as colored_vertex_buffer is a vec of GPU allocated vecs.
|
|
|
|
|
// But we can pass in multiple vertex buffers
|
|
|
|
|
if !self.colored_vertex_buffer.is_empty() {
|
|
|
|
|
command_buffer = command_buffer.draw(
|
|
|
|
|
shader.get_pipeline().clone(),
|
|
|
|
|
&self.dynamic_state.clone(),
|
|
|
|
|
self.colored_vertex_buffer.clone(),
|
|
|
|
|
(), (),
|
|
|
|
|
).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Images
|
|
|
|
|
// Textures
|
|
|
|
|
let mut shader = self.shader_buffers.get("simple_texture").unwrap().clone();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !self.textured_vertex_buffer.is_empty() {
|
|
|
|
|
let handle = self.get_texture_handle(String::from("funky-bird.jpg")).unwrap().clone();
|
|
|
|
|
|
|
|
|
|
// TODO : BAD BAD BAD. SELECTS FIRST TEXTURE ONLY!!!!!!!!!!!!
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
println!("{:?}", self.texture_buffers.len());
|
|
|
|
|
command_buffer = command_buffer.draw(
|
|
|
|
|
shader.get_pipeline().clone(),
|
|
|
|
|
&self.dynamic_state.clone(), vec![vertex_buffer],
|
|
|
|
|
vec![descriptor_set], ()
|
|
|
|
|
vec![descriptor_set], (),
|
|
|
|
|
).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*for (shader_type, kernel) in self.shader_kernels.clone().iter() {
|
|
|
|
|
match shader_type {
|
|
|
|
|
ShaderType::SOLID => {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
ShaderType::TEXTURED => {
|
|
|
|
|
let mut shader = self.shader_buffers.get("simple-image").unwrap().clone();
|
|
|
|
|
|
|
|
|
|
if !self.image_vertex_buffer.is_empty() {
|
|
|
|
|
|
|
|
|
|
let handle = self.get_texture_handle(String::from("funky-bird.jpg")).unwrap().clone();
|
|
|
|
|
|
|
|
|
|
// TODO : BAD BAD BAD. SELECTS FIRST TEXTURE ONLY!!!!!!!!!!!!
|
|
|
|
|
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(
|
|
|
|
|
kernel.clone().get_pipeline().clone(),
|
|
|
|
|
&dynamic_state.clone(), self.textured_vertex_buffer.clone(),
|
|
|
|
|
vec![self.get_textured_descriptor_set(String::from("funky-bird.jpg"))], ()
|
|
|
|
|
shader.get_pipeline().clone(),
|
|
|
|
|
&self.dynamic_state.clone(), vec![vertex_buffer],
|
|
|
|
|
vec![descriptor_set], (),
|
|
|
|
|
).unwrap();
|
|
|
|
|
}
|
|
|
|
|
ShaderType::IMAGE => {}
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
command_buffer
|
|
|
|
|
.end_render_pass()
|
|
|
|
|