From d1051a0ca33d1530cc97ddba8da639f3326b7826 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 13 Feb 2020 23:37:41 -0800 Subject: [PATCH] Switched vertex type to be chosen at the shader load --- notes/VKProcessor/CanvasState.txt | 3 ++- src/canvas/canvas_frame.rs | 1 - src/canvas/canvas_state.rs | 9 +++++---- src/canvas/managed/shader/generic_shader.rs | 18 +++++++---------- src/canvas/managed/shader/shader_common.rs | 7 ++++--- src/canvas/managed/shader/text_shader.rs | 22 ++++++++------------- src/sprite.rs | 1 - src/util/vertex.rs | 8 ++++++++ src/vkprocessor.rs | 10 +++++----- 9 files changed, 39 insertions(+), 40 deletions(-) diff --git a/notes/VKProcessor/CanvasState.txt b/notes/VKProcessor/CanvasState.txt index 97bef200..f7ff3529 100644 --- a/notes/VKProcessor/CanvasState.txt +++ b/notes/VKProcessor/CanvasState.txt @@ -17,11 +17,12 @@ Creation-Date: 2020-02-03T23:30:41-08:00 ===== Interface ===== - Generally there is a get for handle and pointer and a load (or create) for each of the stored items. + Generally there is a getter for the resource handle & pointer and a load (or create) for each of the stored items. The class then interacts with these stored items by taking and executing a list of operations to perform on them. [[VKProcessor::CanvasFrame|CanvasFrame]] + An object which accumulate frame draws. The State will then parse this list of calls and display them on the screen -------------------- diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index 9e4f5454..41bd52a9 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -4,7 +4,6 @@ use std::hash::Hash; use crate::canvas::*; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, Handle}; -use crate::canvas::managed::shader::text_shader::GlyphInstance; use vulkano::pipeline::vertex::Vertex; use std::any::Any; use crate::VertexTypes; diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index 8705781d..41b6096a 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -26,7 +26,7 @@ use std::borrow::Borrow; use std::fs::File; use std::io::Read; use rusttype::{Font, PositionedGlyph, Scale, Rect, point, GlyphId, Line, Curve, Segment}; -use vulkano::pipeline::vertex::VertexDefinition; +use vulkano::pipeline::vertex::{VertexDefinition, Vertex}; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::handles::{CanvasTextureHandle, CanvasImageHandle, CanvasFontHandle, CompiledShaderHandle, Handle, DrawableHandle}; use crate::canvas::managed::gpu_buffers::{CanvasImage, CanvasTexture, CanvasFont}; @@ -34,6 +34,7 @@ use crate::canvas::managed::shader::shader_common::CompiledGraphicsPipeline; use crate::canvas::managed::shader::generic_shader::GenericShader; use crate::VertexTypes; use crate::util::vertex::{TextVertex3D, TextureVertex2D, ImageVertex2D, ColorVertex2D, CanvasFrameAllocation}; +use shade_runner::Input; /// Canvas state is used for storage of texture and image buffers in addition to vertex buffers @@ -255,17 +256,17 @@ 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(&mut self, + pub fn load_shader(&mut self, filename: String, physical: PhysicalDevice, capabilities: Capabilities) -> Option> - where T: CompiledGraphicsPipeline { + where T: CompiledGraphicsPipeline, V: Vertex { let handle = Arc::new(CompiledShaderHandle { handle: self.shader_buffers.len() as u32 }); - let shader: Box = Box::new(T::new( + let shader: Box = Box::new(T::new::( filename.clone(), self.device.clone(), handle.clone(), diff --git a/src/canvas/managed/shader/generic_shader.rs b/src/canvas/managed/shader/generic_shader.rs index e3d2537a..713e230f 100644 --- a/src/canvas/managed/shader/generic_shader.rs +++ b/src/canvas/managed/shader/generic_shader.rs @@ -10,7 +10,7 @@ use shade_runner::{Input, Output, Layout, Entry}; use std::ffi::CStr; use std::marker::PhantomData; use vulkano::pipeline::depth_stencil::{DepthStencil, Compare, DepthBounds, Stencil, StencilOp}; -use vulkano::pipeline::vertex::{SingleBufferDefinition, VertexDefinition}; +use vulkano::pipeline::vertex::{SingleBufferDefinition, VertexDefinition, Vertex}; use shade_runner as sr; use vulkano::memory::pool::PotentialDedicatedAllocation::Generic; use vulkano::SafeDeref; @@ -18,7 +18,7 @@ use crate::canvas::managed::shader::shader_common::{ShaderType, CompiledGraphics use crate::canvas::managed::handles::CompiledShaderHandle; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::ShaderSpecializationConstants; -use crate::util::vertex::VertexTypes; +use crate::util::vertex::{VertexTypes, ColorVertex2D}; /// CanvasShader holds the pipeline and render pass for the input shader source #[derive(Clone)] @@ -34,10 +34,6 @@ pub struct GenericShader { impl GenericShader { - - fn get(&self) -> VertexTypes { - VertexTypes::ImageType - } } /// Gives CanvasShader the resource functions @@ -47,7 +43,7 @@ impl CompiledGraphicsPipelineResources for GenericShader {} impl CompiledGraphicsPipeline for GenericShader { /// This will explode when the shader does not want to compile - fn new(filename: String, + fn new(filename: String, device: Arc, handle: Arc, render_pass: Arc) -> GenericShader { @@ -88,8 +84,8 @@ impl CompiledGraphicsPipeline for GenericShader { graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() - //SingleBufferDefinition:: - .vertex_input(vertex_definition) + .vertex_input(SingleBufferDefinition::::new()) + //.vertex_input(vertex_definition) .vertex_shader(vertex_entry_point.clone(), ShaderSpecializationConstants { first_constant: 0, @@ -139,8 +135,8 @@ impl CompiledGraphicsPipeline for GenericShader { self.renderpass.clone() } - fn recompile(self, render_pass: Arc) -> GenericShader { - GenericShader::new(self.name, + fn recompile(self, render_pass: Arc) -> GenericShader { + GenericShader::new::(self.name, self.device, self.handle, render_pass.clone()) diff --git a/src/canvas/managed/shader/shader_common.rs b/src/canvas/managed/shader/shader_common.rs index fa87ecc0..366b8368 100644 --- a/src/canvas/managed/shader/shader_common.rs +++ b/src/canvas/managed/shader/shader_common.rs @@ -9,6 +9,7 @@ use vulkano::device::Device; use shade_runner::Entry; use shaderc::ShaderKind; use crate::canvas::managed::handles::CompiledShaderHandle; +use vulkano::pipeline::vertex::Vertex; /* @@ -100,15 +101,15 @@ pub trait CompiledGraphicsPipelineResources { pub trait CompiledGraphicsPipeline { - fn new(filename: String, + fn new(filename: String, device: Arc, handle: Arc, - render_pass: Arc) -> Self where Self: Sized; + render_pass: Arc) -> Self where Self: Sized, V: Vertex,; fn get_name(&self) -> String; fn get_handle(&self) -> Arc; fn get_pipeline(&self) -> Arc; fn get_renderpass(&self) -> Arc; - fn recompile(self, render_pass: Arc) + fn recompile(self, render_pass: Arc) -> Self where Self: Sized; } diff --git a/src/canvas/managed/shader/text_shader.rs b/src/canvas/managed/shader/text_shader.rs index f1ac5608..bb9e506a 100644 --- a/src/canvas/managed/shader/text_shader.rs +++ b/src/canvas/managed/shader/text_shader.rs @@ -9,22 +9,15 @@ use shade_runner::{Input, Output, Layout, Entry}; use std::ffi::CStr; use std::marker::PhantomData; use vulkano::pipeline::depth_stencil::{DepthStencil, Compare, DepthBounds, Stencil, StencilOp}; -use vulkano::pipeline::vertex::{SingleBufferDefinition, OneVertexOneInstanceDefinition}; +use vulkano::pipeline::vertex::{SingleBufferDefinition, OneVertexOneInstanceDefinition, Vertex}; use shade_runner as sr; use crate::canvas::managed::shader::shader_common::{ShaderType, CompiledGraphicsPipelineResources, CompiledGraphicsPipeline}; use crate::canvas::managed::handles::CompiledShaderHandle; use crate::canvas::managed::shader::generic_shader::GenericShader; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::ShaderSpecializationConstants; +use crate::util::vertex::ColorVertex2D; -#[derive(Default, Debug, Clone, Copy)] -pub struct GlyphInstance { - pub screen_position: (f32, f32), - pub atlas_position: (f32, f32), - pub atlas_size: (f32, f32), - pub scale: f32, -} -vulkano::impl_vertex!(GlyphInstance, screen_position, atlas_position, atlas_size, scale); /// CanvasShader holds the pipeline and render pass for the input shader source #[derive(Clone)] @@ -47,7 +40,7 @@ impl CompiledGraphicsPipelineResources for TextShader {} impl CompiledGraphicsPipeline for TextShader { /// This will explode when the shader does not want to compile - fn new(filename: String, + fn new(filename: String, device: Arc, handle: Arc, render_pass: Arc) -> TextShader { @@ -111,8 +104,9 @@ impl CompiledGraphicsPipeline for TextShader { TextShader { graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() -//OneVertexOneInstanceDefinition:: - .vertex_input(vertex_definition) + + .vertex_input(SingleBufferDefinition::::new()) + //.vertex_input(vertex_definition) .vertex_shader(vertex_entry_point.clone(), ShaderSpecializationConstants { first_constant: 0, @@ -160,8 +154,8 @@ impl CompiledGraphicsPipeline for TextShader { fn get_renderpass(&self) -> Arc { self.renderpass.clone() } - fn recompile(self, render_pass: Arc) -> TextShader { - TextShader::new(self.name, + fn recompile(self, render_pass: Arc) -> TextShader { + TextShader::new::(self.name, self.device, self.handle, self.renderpass.clone()) diff --git a/src/sprite.rs b/src/sprite.rs index 80395791..75cb01a3 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,7 +1,6 @@ use std::sync::Arc; use crate::canvas::*; use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle}; -use crate::canvas::managed::shader::text_shader::GlyphInstance; use crate::canvas::canvas_frame::{Drawable}; use crate::util::vertex::{VertexTypes, TextureVertex2D, Vertex3D}; diff --git a/src/util/vertex.rs b/src/util/vertex.rs index ed4eca52..9dc6f5c6 100644 --- a/src/util/vertex.rs +++ b/src/util/vertex.rs @@ -40,6 +40,14 @@ pub struct TextVertex3D { vulkano::impl_vertex!(TextVertex3D, position); +#[derive(Default, Debug, Clone, Copy)] +pub struct GlyphInstance { + pub screen_position: (f32, f32), + pub atlas_position: (f32, f32), + pub atlas_size: (f32, f32), + pub scale: f32, +} +vulkano::impl_vertex!(GlyphInstance, screen_position, atlas_position, atlas_size, scale); // ============================================================================== #[derive(Debug, Clone)] diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 08fb38f9..1ac7cc23 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -20,7 +20,7 @@ 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::VertexTypes; +use crate::util::vertex::{VertexTypes, ColorVertex2D, TextVertex3D, TextureVertex2D, ImageVertex2D}; /// VKProcessor holds the vulkan instance information, the swapchain, @@ -168,10 +168,10 @@ impl<'a> VkProcessor<'a> { /// A hardcoded list of shaders which can be preloaded from this function pub fn preload_shaders(&mut self) { - self.canvas_state.load_shader::(String::from("color-passthrough"), self.physical.clone(), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_texture"), self.physical.clone(), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_image"), self.physical.clone(), self.capabilities.clone()); - self.canvas_state.load_shader::(String::from("simple_text"), self.physical.clone(), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("color-passthrough"), self.physical.clone(), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("simple_texture"), self.physical.clone(), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("simple_image"), self.physical.clone(), self.capabilities.clone()); + self.canvas_state.load_shader::(String::from("simple_text"), self.physical.clone(), self.capabilities.clone()); } /// A hardcoded list of shaders which can be proloaded from this function