working on docs

This commit is contained in:
2019-09-09 20:57:15 -07:00
parent 314fa3e4af
commit 26b73c48a8
5 changed files with 128 additions and 115 deletions

View File

@@ -23,46 +23,25 @@ use crate::canvas_frame::CanvasFrame;
use std::hash::Hash;
use crate::canvas_shader::CanvasShader;
use crate::canvas_buffer::{CanvasImage, CanvasTexture};
// Canvas is the accumulator of Sprites for drawing
// Needs to know:
// textured?
// colored?
// vertices
/*
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)
@@ -73,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);
@@ -80,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,
@@ -88,22 +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 CanvasState {
dynamic_state: DynamicState,
@@ -158,8 +135,6 @@ impl CanvasState {
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 },
@@ -168,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![],
@@ -279,6 +241,22 @@ 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() {
@@ -289,6 +267,7 @@ 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;
@@ -300,35 +279,18 @@ 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.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???
*
*/
/// 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();
{
@@ -356,8 +318,24 @@ impl CanvasState {
);
}
}
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(
@@ -366,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>>,
@@ -392,10 +371,9 @@ impl CanvasState {
).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();
@@ -411,21 +389,26 @@ impl CanvasState {
vec![descriptor_set], (),
).unwrap();
}
/*for (shader_type, kernel) in self.shader_kernels.clone().iter() {
match shader_type {
ShaderType::SOLID => {
}
ShaderType::TEXTURED => {
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"))], ()
).unwrap();
}
ShaderType::IMAGE => {}
}
}*/
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(
shader.get_pipeline().clone(),
&self.dynamic_state.clone(), vec![vertex_buffer],
vec![descriptor_set], (),
).unwrap();
}
command_buffer
.end_render_pass()