added hello-world example + modified vkproccessor to live inside specs

a-star
mitchellhansen 4 years ago
parent b070a7dd32
commit c10115e7b9

@ -11,16 +11,23 @@ So, I need to figure out how to determine which objects will :
* Get batched
* And initialized
The best candidate that I have right now is some sort of "scene" setup. Maybe something like
pub struct Scene {
Looks like it could possibly be specs...
drawables_bucket
notifiable_bucket
}
Sprites currently are just a container for the meta-information needed in order to return a VertexTypeContainer.
impl Scene {
pub fn init() -> Scene {}
pub fn get_drawable() -> Maybe iterator of drawables?
pub fn get_notifiable() -
{{{code: lang="rust" linenumbers="True"
pub enum VertexTypeContainer {
TextureType(Vec<TextureVertex3D>, Arc<CanvasTextureHandle>),
ImageType(Vec<ImageVertex3D>, Arc<CanvasImageHandle>),
ColorType(Vec<ColorVertex3D>),
ThreeDType(Vec<Vertex3D>),
TextType(Vec<ColorVertex3D>),
}
}}}
So for a sprite, which is a generic texture and position/size combo
Images are similar, but instead of a "sprite" I made a computsprite because that's the only thing that uses them.
Now if I had to shove these into a component / a set of components... I could have a component of the vertex type even?

@ -6,12 +6,12 @@ use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef;
use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, Handle};
use vulkano::pipeline::vertex::Vertex;
use std::any::Any;
use crate::VertexType;
use crate::VertexTypeContainer;
use winit::event::Event;
/// Trait which may be inherited by objects that wish to be drawn to the screen
pub trait Drawable {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType>;
fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer>;
}
/// Trait which may be inherited by objects that wish to receive events
@ -27,7 +27,7 @@ pub trait Updatable {
/// Accumulator for Vectors of VertexTypes
#[derive(Default)]
pub struct CanvasFrame {
pub map: Vec<VertexType>,
pub map: Vec<VertexTypeContainer>,
window_size: (u32, u32),
}

@ -31,7 +31,7 @@ use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, Ca
use crate::canvas::managed::gpu_buffers::{CanvasImage, CanvasTexture, CanvasFont};
use crate::canvas::managed::shader::shader_common::CompiledShader;
use crate::canvas::managed::shader::generic_shader::GenericShader;
use crate::VertexType;
use crate::VertexTypeContainer;
use crate::util::vertex::{TextVertex3D, TextureVertex3D, ImageVertex3D, ColorVertex3D, CanvasFrameAllocation};
use shade_runner::Input;
use winit::window::Window;
@ -52,7 +52,7 @@ pub struct CanvasState {
font_buffers: Vec<Arc<CanvasFont>>,
/// 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 + Send + Sync>>>,
/// Looks like we gotta hold onto the queue for managing textures
queue: Arc<Queue>,
@ -254,15 +254,16 @@ impl CanvasState {
/// 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<T: 'static, V>(&mut self,
pub fn load_shader<T, V>(&mut self,
filename: String,
capabilities: Capabilities) -> Option<Arc<CompiledShaderHandle>>
where T: CompiledShader, V: Vertex {
where T: CompiledShader + Send + Sync + 'static, V: Vertex {
let handle = Arc::new(CompiledShaderHandle {
handle: self.shader_buffers.len() as u32
});
let shader: Box<dyn CompiledShader> = Box::new(T::new::<V>(
let shader: Box<dyn CompiledShader + Send + Sync> = Box::new(T::new::<V>(
filename.clone(),
self.device.clone(),
handle.clone(),
@ -396,19 +397,19 @@ impl CanvasState {
// separate the mux of vertex containers back out
for value in &canvas_frame.map {
match value {
VertexType::TextureType(vertices, handle) => {
VertexTypeContainer::TextureType(vertices, handle) => {
textured_vertex_buffer.entry(handle.clone()).or_insert(vertices.clone()).extend(vertices);
}
VertexType::ImageType(vertices, handle) => {
VertexTypeContainer::ImageType(vertices, handle) => {
image_vertex_buffer.entry(handle.clone()).or_insert(vertices.clone()).extend(vertices);
}
VertexType::ColorType(vertices) => {
VertexTypeContainer::ColorType(vertices) => {
colored_vertex_buffer.extend(vertices);
}
VertexType::ThreeDType(vertices) => {
VertexTypeContainer::ThreeDType(vertices) => {
}
VertexType::TextType(vertices) => {
VertexTypeContainer::TextType(vertices) => {
text_vertex_buffer.extend(vertices);
}
};

@ -18,7 +18,7 @@ use crate::canvas::managed::shader::shader_common::{ShaderType, CompiledShaderRe
use crate::canvas::managed::handles::CompiledShaderHandle;
use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef;
use crate::canvas::managed::ShaderSpecializationConstants;
use crate::util::vertex::{VertexType, ColorVertex3D};
use crate::util::vertex::{VertexTypeContainer, ColorVertex3D};
/// CanvasShader holds the pipeline and render pass for the input shader source
#[derive(Clone)]

@ -98,19 +98,18 @@ pub trait CompiledShaderResources {
}
}
pub trait CompiledShader {
fn new<V>(filename: String,
fn new<V: Vertex + Send>(filename: String,
device: Arc<Device>,
handle: Arc<CompiledShaderHandle>,
render_pass: Arc<dyn RenderPassAbstract + Send + Sync>) -> Self where Self: Sized, V: Vertex,;
render_pass: Arc<dyn RenderPassAbstract + Send + Sync>)
-> Self where Self: Sized + Send + Sync;
fn get_name(&self) -> String;
fn get_handle(&self) -> Arc<CompiledShaderHandle>;
fn get_pipeline(&self) -> Arc<dyn GraphicsPipelineAbstract + Sync + Send>;
fn get_renderpass(&self) -> Arc<dyn RenderPassAbstract + Send + Sync>;
fn recompile<V: Vertex>(self, render_pass: Arc<dyn RenderPassAbstract + Send + Sync>)
-> Self where Self: Sized;
fn recompile<V: Vertex + Send>(self, render_pass: Arc<dyn RenderPassAbstract + Send + Sync>)
-> Self where Self: Sized + Send;
}
/// Legacy ShaderType enum for single type shaders.

@ -3,7 +3,7 @@ use crate::canvas::managed::handles::{CanvasImageHandle};
use crate::compute::managed::handles::{CompuKernelHandle, CompuBufferHandle};
use crate::drawables::compu_sprite::CompuSprite;
use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::VertexType;
use crate::util::vertex::VertexTypeContainer;
pub struct CompuFrame {
// Vec<(Buffer, Kernel)>
@ -60,7 +60,7 @@ impl CompuFrame {
let compu_sprites = sprite.get(self.window_size);
if compu_sprites.len() == 1 {
if let VertexType::ImageType(a, b) = compu_sprites.first().unwrap() {
if let VertexTypeContainer::ImageType(a, b) = compu_sprites.first().unwrap() {
self.swapped_to_image.push((buffer, b.clone(), kernel))
};
}

@ -1,11 +1,11 @@
use std::sync::Arc;
use crate::canvas::managed::handles::{CanvasImageHandle, CanvasTextureHandle};
use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::{VertexType, ImageVertex3D};
use crate::util::vertex::{VertexTypeContainer, ImageVertex3D};
pub struct CompuSprite {
pub verts: VertexType,
pub verts: VertexTypeContainer,
position: (f32, f32),
size: (f32, f32),
@ -44,7 +44,7 @@ impl CompuSprite {
];
CompuSprite {
verts: VertexType::ImageType(verts, image_handle.clone()),
verts: VertexTypeContainer::ImageType(verts, image_handle.clone()),
position: position,
size: size,
color: (0.0, 0.0, 0.0, 0.0),
@ -53,7 +53,7 @@ impl CompuSprite {
}
impl Drawable for CompuSprite {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer> {
vec![self.verts.clone()]
}
}

@ -2,14 +2,14 @@ use std::sync::Arc;
use crate::canvas::*;
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle};
use crate::canvas::canvas_frame::{Drawable};
use crate::util::vertex::{VertexType, TextureVertex3D, Vertex3D, ColorVertex3D};
use crate::util::vertex::{VertexTypeContainer, TextureVertex3D, Vertex3D, ColorVertex3D};
use crate::drawables::sprite::Sprite;
/// Convex multi verticy polygon
#[derive(Debug, Clone)]
pub struct Polygon {
pub verts: VertexType,
pub verts: VertexTypeContainer,
position: (f32, f32),
size: (f32, f32),
@ -54,14 +54,14 @@ impl Polygon {
Polygon {
verts: VertexType::ColorType(verts),
verts: VertexTypeContainer::ColorType(verts),
position: position,
size: size,
}
}
}
impl Drawable for Polygon {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer> {
vec![self.verts.clone()]
}

@ -1,5 +1,5 @@
use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::{VertexType, ColorVertex3D};
use crate::util::vertex::{VertexTypeContainer, ColorVertex3D};
///
#[derive(Debug, Clone)]
@ -73,9 +73,9 @@ impl Rect {
}
impl Drawable for Rect {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer> {
vec![
VertexType::ColorType(
VertexTypeContainer::ColorType(
Rect::generate_vertices(window_size, self.position, self.size, self.depth, self.color)
)
]

@ -5,7 +5,7 @@ use winit::event::Event;
use crate::canvas::canvas_frame::{Drawable, Eventable};
use crate::drawables::rect::Rect;
use crate::drawables::sprite::Sprite;
use crate::util::vertex::VertexType;
use crate::util::vertex::VertexTypeContainer;
pub struct Slider {
handle: Rect,
@ -45,14 +45,14 @@ impl Slider {
}
impl Drawable for Slider {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer> {
let mut vertices = self.handle.get(window_size).clone();
vertices.extend_from_slice(
self.guide.iter()
.map(|x| x.get(window_size))
.flatten()
.collect::<Vec<VertexType>>()
.collect::<Vec<VertexTypeContainer>>()
.as_slice()
);

@ -2,7 +2,7 @@ use std::sync::Arc;
use crate::canvas::*;
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle};
use crate::canvas::canvas_frame::{Drawable, Eventable, Updatable};
use crate::util::vertex::{VertexType, TextureVertex3D, Vertex3D};
use crate::util::vertex::{VertexTypeContainer, TextureVertex3D, Vertex3D};
use winit::event::{DeviceEvent, MouseButton, ElementState, Event, WindowEvent};
///
@ -73,9 +73,9 @@ impl Sprite {
}
impl Drawable for Sprite {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer> {
vec![
VertexType::TextureType(
VertexTypeContainer::TextureType(
Sprite::generate_verts(window_size, self.position, self.size, self.depth),
self.texture_handle.clone())
]

@ -1,10 +1,10 @@
use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::{VertexType, ColorVertex3D};
use crate::util::vertex::{VertexTypeContainer, ColorVertex3D};
///
#[derive(Debug, Clone)]
pub struct Text {
pub verts: VertexType,
pub verts: VertexTypeContainer,
position: (f32, f32),
size: (f32, f32),
@ -129,7 +129,7 @@ impl Text {
};
Text {
verts: VertexType::TextType(verts),
verts: VertexTypeContainer::TextType(verts),
position: position,
size: size,
}
@ -137,7 +137,7 @@ impl Text {
}
impl Drawable for Text {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer> {
vec![self.verts.clone()]
}
}

@ -37,7 +37,7 @@ use crate::drawables::text::Text;
use crate::util::load_raw;
use crate::util::timer::Timer;
use crate::util::tr_event::TrEvent;
use crate::util::vertex::{TextureVertex3D, VertexType};
use crate::util::vertex::{TextureVertex3D, VertexTypeContainer};
use crate::vkprocessor::VkProcessor;
use crate::drawables::slider::Slider;
@ -52,8 +52,14 @@ extern crate specs;
use specs::prelude::*;
// A component contains data which is
// associated with an entity.
struct Draws(VertexTypeContainer);
impl Component for Draws {
type Storage = VecStorage<Self>;
}
struct Renderer(u32);
struct Vel(f32);
@ -67,6 +73,32 @@ impl Component for Pos {
type Storage = VecStorage<Self>;
}
struct RenderSystem;
impl<'a> System<'a> for RenderSystem {
type SystemData = (
WriteStorage<'a, Pos>,
WriteStorage<'a, Vel>,
WriteStorage<'a, Draws>,
);
fn run(&mut self, (mut pos, vel, data): Self::SystemData) {
// The `.join()` combines multiple components,
// so we only access those entities which have
// both of them.
// This joins the component storages for Position
// and Velocity together; it's also possible to do this
// in parallel using rayon's `ParallelIterator`s.
// See `ParJoin` for more.
for (pos, vel) in (&mut pos, &vel).join() {
pos.0 += vel.0;
}
}
}
struct SysA;
impl<'a> System<'a> for SysA {
@ -93,34 +125,6 @@ impl<'a> System<'a> for SysA {
pub fn main() {
// The `World` is our
// container for components
// and other resources.
let mut world = World::new();
world.register::<Pos>();
world.register::<Vel>();
world.reg
// An entity may or may not contain some component.
world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build();
world.create_entity().with(Vel(4.0)).with(Pos(1.6)).build();
world.create_entity().with(Vel(1.5)).with(Pos(5.4)).build();
world.create_entity().with(Pos(2.0)).build();
let mut dispatcher = DispatcherBuilder::new()
.with(SysA, "sys_a", &[]).build();
// This dispatches all the systems in parallel (but blocking).
dispatcher.dispatch(&mut world);
hprof::start_frame();
let q1 = hprof::enter("setup");
@ -159,7 +163,7 @@ pub fn main() {
let step_size: f32 = 0.005;
let mut elapsed_time: f32 = timer.elap_time();
;
let mut delta_time: f32 = 0.0;
let mut accumulator_time: f32 = 0.0;
let mut current_time: f32 = timer.elap_time();
@ -191,6 +195,41 @@ pub fn main() {
//let font_handle : Arc<CanvasFontHandle> =
// processor.get_font_handle(String::from("sansation.ttf")).unwrap();
// The `World` is our
// container for components
// and other resources.
let mut world = World::new();
world.register::<Pos>();
world.register::<Vel>();
world.register::<Draws>();
world.insert::<Renderer>(Renderer(10));
world.insert::<VkProcessor>(processor);
// An entity may or may not contain some component.
world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build();
let mut dispatcher = DispatcherBuilder::new()
.with(SysA, "sys_a", &[])
.with(RenderSystem, "render_s", &[]).build();
// This dispatches all the systems in parallel (but blocking).
dispatcher.dispatch(&mut world);
let mut funky_sprite = Sprite::new(
(200.0, 200.0),
(100.0, 150.0), 10, funky_handle.clone());
@ -254,10 +293,10 @@ pub fn main() {
// maybe some sort of input system
//
let mut big_container = vec![
Box::new(Slider::new((0.1, 0.1), (0.9, 0.9), 5000)),
Box::new(Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone())),
];
// let mut big_container = vec![
// Box::new(Slider::new((0.1, 0.1), (0.9, 0.9), 5000)),
// Box::new(Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone())),
// ];
//container.push(Sprite::new((0.1)));
@ -265,13 +304,13 @@ pub fn main() {
events_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
for eventable in &mut big_container {
eventable.notify(&event);
}
for drawable in &mut big_container {
canvas_frame.draw(&drawable);
}
// for eventable in &mut big_container {
// eventable.notify(&event);
// }
//
// for drawable in &mut big_container {
// canvas_frame.draw(&drawable);
// }
match event {
Event::NewEvents(cause) => {
@ -294,7 +333,7 @@ pub fn main() {
*control_flow = ControlFlow::Exit
}
Event::WindowEvent { event: WindowEvent::Resized(new_size), .. } => {
processor.swapchain_recreate_needed = true;
world.write_resource::<VkProcessor>().swapchain_recreate_needed = true;
let size = (new_size.width, new_size.height);
}
Event::WindowEvent {
@ -317,7 +356,7 @@ pub fn main() {
}
VirtualKeyCode::P => {
if keyboard_input.state == ElementState::Pressed {
let data = processor.read_compute_buffer(compute_buffer.clone());
let data = world.write_resource::<VkProcessor>().read_compute_buffer(compute_buffer.clone());
image::save_buffer(&Path::new("image.png"), data.as_slice(), (image_data.1).0, (image_data.1).1, image::RGBA(8));
}
}
@ -343,7 +382,8 @@ pub fn main() {
{
let g = hprof::enter("Run");
processor.run(&surface.clone(),
world.write_resource::<VkProcessor>()
.run(&surface.clone(),
&canvas_frame,
&compu_frame);
}

@ -51,13 +51,12 @@ vulkano::impl_vertex!(GlyphInstance, screen_position, atlas_position, atlas_size
// ==============================================================================
#[derive(Debug, Clone)]
pub enum VertexType {
pub enum VertexTypeContainer {
TextureType(Vec<TextureVertex3D>, Arc<CanvasTextureHandle>),
ImageType(Vec<ImageVertex3D>, Arc<CanvasImageHandle>),
ColorType(Vec<ColorVertex3D>),
ThreeDType(Vec<Vertex3D>),
TextType(Vec<ColorVertex3D>),
}
#[derive(Clone)]

@ -19,13 +19,14 @@ use crate::canvas::managed::shader::generic_shader::GenericShader;
use crate::canvas::managed::shader::text_shader::TextShader;
use crate::canvas::managed::handles::{CanvasTextureHandle, CompiledShaderHandle, CanvasFontHandle, CanvasImageHandle};
use crate::compute::managed::handles::{CompuKernelHandle, CompuBufferHandle};
use crate::util::vertex::{VertexType, ColorVertex3D, TextVertex3D, TextureVertex3D, ImageVertex3D};
use crate::util::vertex::{VertexTypeContainer, ColorVertex3D, TextVertex3D, TextureVertex3D, ImageVertex3D};
use vulkano_text::DrawText;
use winit::window::{Window, WindowBuilder};
use vulkano::instance::debug::DebugCallback;
use winit::dpi::LogicalSize;
use vulkano_win::VkSurfaceBuild;
use winit::event_loop::EventLoop;
use specs::prelude::Resource;
/// VKProcessor holds the vulkan instance information, the swapchain,
@ -53,6 +54,7 @@ pub struct VkProcessor {
}
impl VkProcessor {
/// Creates a new VkProcessor from an instance and surface
/// This includes the physical device, queues, compute and canvas state

Loading…
Cancel
Save