adding 3d sprites

master
mitchellhansen 5 years ago
parent 1419f85455
commit 8c84c08f7e

@ -11,7 +11,8 @@ The Canvas needs to package certain buffers along with their metadata. These tak
All buffers will have a coupled handle type stored in [[/src/canvas/managed/handles.rs|handles.rs]] and a reference to that handle All buffers will have a coupled handle type stored in [[/src/canvas/managed/handles.rs|handles.rs]] and a reference to that handle
==== Todo ====
Need to pull out the persistent descriptor set **build **command after adding it's self.

@ -1,6 +1,6 @@
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer}; use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer, CpuBufferPool};
use std::sync::Arc; use std::sync::Arc;
use vulkano::format::{ClearValue, Format, R8Unorm, ClearValuesTuple}; use vulkano::format::{ClearValue, Format, R8Unorm, ClearValuesTuple};
use vulkano::framebuffer::{FramebufferAbstract, Framebuffer, RenderPass, RenderPassAbstract}; use vulkano::framebuffer::{FramebufferAbstract, Framebuffer, RenderPass, RenderPassAbstract};
@ -28,8 +28,8 @@ use std::io::Read;
use rusttype::{Font, PositionedGlyph, Scale, Rect, point, GlyphId, Line, Curve, Segment}; use rusttype::{Font, PositionedGlyph, Scale, Rect, point, GlyphId, Line, Curve, Segment};
use vulkano::pipeline::vertex::{VertexDefinition, Vertex}; use vulkano::pipeline::vertex::{VertexDefinition, Vertex};
use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef;
use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, CompiledShaderHandle, Handle, DrawableHandle}; use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, CompiledShaderHandle, Handle, DrawableHandle, CanvasUniformHandle};
use crate::canvas::managed::gpu_buffers::{CanvasImage, CanvasTexture, CanvasFont}; use crate::canvas::managed::gpu_buffers::{CanvasImage, CanvasTexture, CanvasFont, CanvasUniform};
use crate::canvas::managed::shader::shader_common::CompiledShader; use crate::canvas::managed::shader::shader_common::CompiledShader;
use crate::canvas::managed::shader::generic_shader::GenericShader; use crate::canvas::managed::shader::generic_shader::GenericShader;
use crate::VertexTypes; use crate::VertexTypes;
@ -50,6 +50,7 @@ pub struct CanvasState {
image_buffers: Vec<Arc<CanvasImage>>, image_buffers: Vec<Arc<CanvasImage>>,
texture_buffers: Vec<Arc<CanvasTexture>>, texture_buffers: Vec<Arc<CanvasTexture>>,
font_buffers: Vec<Arc<CanvasFont>>, font_buffers: Vec<Arc<CanvasFont>>,
uniform_buffers: Vec<Arc<CanvasUniform>>,
/// Compiled Graphics pipelines have a handle which self describe their position in this vector /// Compiled Graphics pipelines have a handle which self describe their position in this vector
shader_buffers: Vec<Arc<Box<dyn CompiledShader>>>, shader_buffers: Vec<Arc<Box<dyn CompiledShader>>>,
@ -161,6 +162,8 @@ impl CanvasState {
shader_buffers: vec![], shader_buffers: vec![],
font_buffers: vec![], font_buffers: vec![],
uniform_buffer: CpuBufferPool::new(device.clone(), BufferUsage::all()),
queue: queue.clone(), queue: queue.clone(),
device: device.clone(), device: device.clone(),
render_pass: render_pass.clone(), render_pass: render_pass.clone(),
@ -252,6 +255,24 @@ impl CanvasState {
Some(handle) Some(handle)
} }
/// Load a texture using it's filename from a file. Returns the handle of the loaded texture
pub fn load_buffer_pool(&mut self) -> Option<Arc<CanvasTextureHandle>> {
let handle = Arc::new(CanvasUniformHandle {
handle: self.uniform_buffers.len() as u32
});
let texture = Arc::new(CanvasUniform {
handle: handle.clone(),
buffer: self.get_texture_from_file(filename.clone()),
name: filename.clone(),
});
self.texture_buffers.push(texture);
Some(handle)
}
/// Load and Compile a shader with the filename at resources/shaders /// Load and Compile a shader with the filename at resources/shaders
/// Takes physical and capabilities as we don't store that in Canvas /// Takes physical and capabilities as we don't store that in Canvas
pub fn load_shader<T: 'static, V>(&mut self, pub fn load_shader<T: 'static, V>(&mut self,
@ -542,58 +563,11 @@ impl CanvasState {
} }
} }
// Text // Text
let mut shader = self.shader_buffers.get( let mut shader = self.shader_buffers.get(
self.get_shader_handle(String::from("simple_text")) self.get_shader_handle(String::from("simple_text"))
.unwrap().clone().get_handle() as usize .unwrap().clone().get_handle() as usize
).unwrap(); ).unwrap();
//
// self.dynamic_state = DynamicState {
// line_width: None,
// viewports: self.dynamic_state.viewports.clone(),
// scissors: None,
// compare_mask: Some(DynamicStencilValue {
// face: StencilFaceFlags::StencilFrontAndBack,
// value: 0x00,
// }),
// write_mask: Some(DynamicStencilValue {
// face: StencilFaceFlags::StencilFrontAndBack,
// value: 0xFF,
// }),
// reference: Some(DynamicStencilValue {
// face: StencilFaceFlags::StencilFrontAndBack,
// value: 0x00,
// }),
// };
//
// if !allocated_buffers.text_vertex_buffer.is_empty() {
// command_buffer = command_buffer.draw(
// shader.get_pipeline().clone(),
// &self.dynamic_state.clone(),
// allocated_buffers.text_vertex_buffer.clone(),
// (), (),
// ).unwrap();
// }
//
// self.dynamic_state = DynamicState {
// line_width: None,
// viewports: self.dynamic_state.viewports.clone(),
// scissors: None,
// compare_mask: Some(DynamicStencilValue {
// face: StencilFaceFlags::StencilFrontAndBack,
// value: 0xFF,
// }),
// write_mask: Some(DynamicStencilValue {
// face: StencilFaceFlags::StencilFrontAndBack,
// value: 0x00,
// }),
// reference: Some(DynamicStencilValue {
// face: StencilFaceFlags::StencilFrontAndBack,
// value: 0x00,
// }),
// };
if !allocated_buffers.text_vertex_buffer.is_empty() { if !allocated_buffers.text_vertex_buffer.is_empty() {
command_buffer = command_buffer.draw( command_buffer = command_buffer.draw(
@ -604,6 +578,35 @@ impl CanvasState {
).unwrap(); ).unwrap();
} }
// So I need a uniform buffer which is the transformation matrix for the camera I think...
// I also need two buffers, one for normals, one for vertices. These can be piped through the
// current interfaces by just adding another type to VertexTypes
//
// 3D
let mut shader = self.shader_buffers.get(
self.get_shader_handle(String::from("simple_3d"))
.unwrap().clone().get_handle() as usize
).unwrap();
// The other modules get the descriptor set like this
// let descriptor_set = self.texture_buffers.get(handle).clone().unwrap().clone()
// .get_descriptor_set(shader.get_pipeline(), self.sampler.clone());
//
// let set = Arc::new(
// PersistentDescriptorSet::start(layout.clone()) //
// .add_buffer(uniform_buffer_subbuffer).unwrap()
// .build().unwrap()
// );
// if !allocated_buffers.text_vertex_buffer.is_empty() {
// command_buffer = command_buffer.draw_indexed(
// shader.get_pipeline().clone(),
// &self.dynamic_state.clone(),
// vec!(vertex_buffer.clone(), normals_buffer.clone()),
// index_buffer.clone(), set.clone(), ()
// ).unwrap();
// }
command_buffer command_buffer
.end_render_pass() .end_render_pass()

@ -4,12 +4,35 @@ use vulkano::format::{Format, R8Unorm};
use vulkano::sampler::Sampler; use vulkano::sampler::Sampler;
use vulkano::descriptor::DescriptorSet; use vulkano::descriptor::DescriptorSet;
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet; use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
use vulkano::buffer::{CpuAccessibleBuffer, BufferAccess}; use vulkano::buffer::{CpuAccessibleBuffer, BufferAccess, CpuBufferPool};
use vulkano::pipeline::GraphicsPipelineAbstract; use vulkano::pipeline::GraphicsPipelineAbstract;
use rusttype::Font; use rusttype::Font;
use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle}; use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, CanvasUniformHandle};
/// Canvas buffer which represents an allocated Texture with a key and dimensions
#[derive(Clone)]
pub struct CanvasUniform {
pub(crate) handle: Arc<CanvasUniformHandle>,
pub(crate) buffer: Arc<CpuBufferPool<>>,
pub(crate) name: String,
}
impl CanvasUniform {
pub fn get_descriptor_set(&self,
pipeline: Arc<dyn GraphicsPipelineAbstract + Sync + Send>,
data: Arc<Format>) -> Box<dyn DescriptorSet + Send + Sync> {
let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
PersistentDescriptorSet::start(
pipeline.clone(), 0,
)
.add_buffer(self.buffer.next(data).unwrap()).unwrap()
.build().unwrap());
o
}
}
/// Canvas buffer which represents an allocated Texture with a key and dimensions /// Canvas buffer which represents an allocated Texture with a key and dimensions
#[derive(Clone)] #[derive(Clone)]
pub struct CanvasTexture { pub struct CanvasTexture {

@ -1,3 +1,4 @@
use crate::canvas::managed::gpu_buffers::CanvasUniform;
pub trait Handle { pub trait Handle {
fn get_handle(&self) -> u32; fn get_handle(&self) -> u32;
@ -57,4 +58,15 @@ impl Handle for CompiledShaderHandle {
} }
} }
/// Typed wrapper for a u32 handle
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct CanvasUniformHandle {
pub(in crate::canvas) handle: u32
}
impl Handle for CanvasUniformHandle {
fn get_handle(&self) -> u32 {
self.handle
}
}

@ -3,6 +3,7 @@ pub mod shader;
pub mod handles; pub mod handles;
pub mod canvas_text; pub mod canvas_text;
pub mod gpu_buffers; pub mod gpu_buffers;
pub mod teapot_data;
use vulkano::pipeline::shader::{SpecializationConstants, SpecializationMapEntry}; use vulkano::pipeline::shader::{SpecializationConstants, SpecializationMapEntry};

File diff suppressed because it is too large Load Diff

@ -0,0 +1,57 @@
use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::{VertexTypes, ColorVertex3D};
///
#[derive(Debug, Clone)]
pub struct Teapot {
pub verts: VertexTypes,
position: (f32, f32),
size: (f32, f32),
}
/// Container class which implements drawable.
impl Teapot {
///
pub fn new(position: (f32, f32),
size: (f32, f32),
depth: u32) -> Teapot {
let normalized_depth = (depth as f32 / 255.0);
let verts = vec![
ColorVertex3D{
v_position: [position.0, position.1, normalized_depth], // top left
color: [0.0, 1.0, 1.0, 0.5] },
ColorVertex3D{
v_position: [position.0, position.1 + size.1, normalized_depth], // bottom left
color: [1.0, 1.0, 1.0, 1.0] },
ColorVertex3D{
v_position: [position.0 + size.0, position.1 + size.1, normalized_depth], // bottom right
color: [1.0, 1.0, 1.0, 1.0] },
ColorVertex3D{
v_position: [position.0, position.1, normalized_depth], // top left
color: [1.0, 1.0, 1.0, 1.0] },
ColorVertex3D{
v_position: [position.0 + size.0, position.1 + size.1, normalized_depth], // bottom right
color: [1.0, 1.0, 1.0, 1.0] },
ColorVertex3D{
v_position: [position.0 + size.0, position.1, normalized_depth], // top right
color: [1.0, 1.0, 1.0, 1.0] },
];
Teapot {
verts: VertexTypes::ColorType(verts),
position: position,
size: size,
}
}
}
impl Drawable for Teapot {
fn get(&self) -> VertexTypes {
self.verts.clone()
}
}

@ -112,7 +112,7 @@ pub fn main() {
//let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone()); //let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
let text_sprite = Text::new((-0.1,-0.1), (10.0, 10.0), 1); // let text_sprite = Text::new((-0.1,-0.1), (10.0, 10.0), 1);
//let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0)); //let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0));
drop(q2); drop(q2);
@ -174,7 +174,7 @@ pub fn main() {
let mut canvas_frame = CanvasFrame::default(); let mut canvas_frame = CanvasFrame::default();
canvas_frame.draw(&funky_sprite); canvas_frame.draw(&funky_sprite);
canvas_frame.draw(&text_sprite); // canvas_frame.draw(&text_sprite);
// canvas_frame.draw(&rect); // canvas_frame.draw(&rect);
let mut compu_frame = CompuFrame::new(); let mut compu_frame = CompuFrame::new();

Loading…
Cancel
Save