diff --git a/src/button.rs b/src/button.rs deleted file mode 100644 index 1c4d4e7a..00000000 --- a/src/button.rs +++ /dev/null @@ -1,63 +0,0 @@ -use sfml::graphics::{Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Text, Font}; - -use sfml::system::Vector2f; - -trait Clickable { - fn name(&self) -> &'static str; - fn is_clicked(&self, mouse_position: Vector2f) -> &'static str; -} - -pub struct Button<'s> { - body: RectangleShape<'s>, - text: Text<'s>, - font: &'s Font, - callback: Option<&'s FnMut(i32)>, -} - -impl<'s> Button<'s> { - pub fn new(size: Vector2f, pos: Vector2f, font: &'s Font) -> Self { - - let mut body = RectangleShape::with_size(size); - body.set_position(pos); - - let mut text = Text::new("", font, 13); - text.set_fill_color(&Color::BLUE); - text.set_position(pos); - - Self { body , text, font, callback: None } - } - - pub fn set_text(&mut self, text: &str) { - self.text.set_string(text); - } - - pub fn set_position(&mut self, position: Vector2f) { - self.body.set_position(position); - self.text.set_position(position); - } - - pub fn is_within(point: Vector2f, button: &Button) -> bool { - button.body.local_bounds().contains(point) - } -} - -impl<'s> Drawable for Button<'s> { - fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( - &'a self, - render_target: &mut RenderTarget, - _: RenderStates<'texture, 'shader, 'shader_texture>, - ) { - render_target.draw(&self.body); - render_target.draw(&self.text); - } -} - -impl<'s> Clickable for Button<'s> { - fn name(&self) -> &'static str { - unimplemented!() - } - - fn is_clicked(&self, mouse_position: Vector2f) -> &'static str { - unimplemented!() - } -} \ No newline at end of file diff --git a/src/canvas.rs b/src/canvas.rs index 59caea58..1f3df7d3 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -8,7 +8,6 @@ use vulkano::framebuffer::{FramebufferAbstract, Framebuffer}; use vulkano::device::{Device, Queue}; use vulkano::instance::PhysicalDevice; use vulkano::image::immutable::ImmutableImage; -use crate::util::shader_kernels::ShaderKernels; use vulkano::image::{Dimensions, ImageAccess, ImageDimensions, SwapchainImage, ImageUsage, AttachmentImage}; use vulkano::sampler::{Sampler, SamplerAddressMode, MipmapMode, Filter}; use vulkano::descriptor::DescriptorSet; @@ -22,6 +21,7 @@ use vulkano::pipeline::viewport::Viewport; 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 @@ -113,7 +113,7 @@ pub struct CanvasTexture { impl CanvasTexture { fn get_descriptor_set(&mut self, - shader: Arc, + shader: Arc, sampler: Arc) -> Box { let o: Box = Box::new( PersistentDescriptorSet::start( @@ -133,7 +133,7 @@ pub struct CanvasImage { } impl CanvasImage { - fn get_descriptor_set(&mut self, shader: Arc) + fn get_descriptor_set(&mut self, shader: Arc) -> Box { let o: Box = Box::new( PersistentDescriptorSet::start( @@ -153,7 +153,7 @@ pub struct CanvasState { // hold the image, texture, and shader buffers the same was as we do CompuState image_buffers: Vec>, texture_buffers: Vec>, - shader_buffers: HashMap, + shader_buffers: HashMap, // Hold onto the vertices we get from the Compu and Canvas Frames // When the run comes around, push the vertices to the GPU @@ -211,13 +211,13 @@ impl CanvasState { image_buffers: vec![], texture_buffers: vec![], shader_buffers: HashMap::from_iter(vec![ - (solid_color_kernel.clone(), ShaderKernels::new(solid_color_kernel.clone(), + (solid_color_kernel.clone(), CanvasShader::new(solid_color_kernel.clone(), capabilities.clone(), queue.clone(), physical.clone(), device.clone()) ), - (texture_kernel.clone(), ShaderKernels::new(texture_kernel.clone(), + (texture_kernel.clone(), CanvasShader::new(texture_kernel.clone(), capabilities.clone(), queue.clone(), physical.clone(), @@ -378,7 +378,7 @@ impl CanvasState { } } - fn get_solid_color_descriptor_set(&self, kernel: Arc) -> Box { + fn get_solid_color_descriptor_set(&self, kernel: Arc) -> Box { let o: Box = Box::new( PersistentDescriptorSet::start( diff --git a/src/canvas_shader.rs b/src/canvas_shader.rs index 5d745ed5..c2baee17 100644 --- a/src/canvas_shader.rs +++ b/src/canvas_shader.rs @@ -47,7 +47,7 @@ impl CanvasShader { (vertex_shader_path, fragment_shader_path) } - pub fn get_pipeline(&mut self) -> Arc { + pub fn get_pipeline(&self) -> Arc { self.graphics_pipeline.clone().unwrap() } diff --git a/src/main.rs b/src/main.rs index 6cbc06c5..2d0d7a2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,11 +28,9 @@ use crate::util::load_raw; use crate::canvas_frame::CanvasFrame; mod util; -mod slider; mod timer; mod input; mod vkprocessor; -mod button; mod vertex_2d; mod vertex_3d; mod sprite; diff --git a/src/slider.rs b/src/slider.rs deleted file mode 100644 index d7e2a930..00000000 --- a/src/slider.rs +++ /dev/null @@ -1,70 +0,0 @@ - -use sfml::graphics::{Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Font}; -use crate::button::Button; -use sfml::system::Vector2f; - -pub struct Slider<'a> { - value: f32, - left_button: Button<'a>, - right_button: Button<'a>, - slider_button: Button<'a>, - rail: RectangleShape<'a>, - root_position: Vector2f, -} - -impl<'a> Slider<'a> { - - pub fn new(position: Vector2f, value: Option, font: &'a Font) -> Self { - - let val = match value { - Some(v) => v, - None => 0.0, - }; - - let left_button_position = position + Vector2f::new(0.0,0.0); - let right_button_position = position + Vector2f::new(150.0,0.0); - let slider_position = position + Vector2f::new(val,0.0); - - let mut slider = Slider { - value: val, - left_button: Button::new(Vector2f::new(30.0, 50.0), left_button_position, font), - right_button: Button::new(Vector2f::new(30.0, 50.0), right_button_position, font), - slider_button: Button::new(Vector2f::new(30.0, 50.0), slider_position, font), - rail: RectangleShape::with_size(Vector2f::new(150.0, 2.0)), - root_position: position, - }; - - slider.left_button.set_text("<"); - slider.right_button.set_text(">"); - - slider.rail.set_position(position); - - slider - } - - pub fn click(&mut self, click: Vector2f) { - if Button::is_within(click, &self.left_button) { - self.value -= 1.0; - } - if Button::is_within(click, &self.right_button) { - self.value += 1.0; - } - } - - pub fn update(&mut self) { - self.slider_button.set_position(self.root_position + Vector2f::new(self.value,0.0)); - } -} - -impl<'s> Drawable for Slider<'s> { - fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( - &'a self, - render_target: &mut RenderTarget, - _: RenderStates<'texture, 'shader, 'shader_texture>, - ) { - render_target.draw(&self.left_button); - render_target.draw(&self.right_button); - render_target.draw(&self.slider_button); - render_target.draw(&self.rail); - } -} \ No newline at end of file diff --git a/src/util/mod.rs b/src/util/mod.rs index 04c31575..38d6575f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -3,10 +3,6 @@ use std::sync::Arc; use std::ffi::CStr; use std::path::PathBuf; -pub mod shader_kernels; - - - pub fn load_raw(filename: String) -> (Vec, (u32,u32)) { let project_root = diff --git a/src/util/shader_kernels.rs b/src/util/shader_kernels.rs deleted file mode 100644 index 6ef56163..00000000 --- a/src/util/shader_kernels.rs +++ /dev/null @@ -1,204 +0,0 @@ -use vulkano::device::{Device, Queue}; -use vulkano::instance::{PhysicalDevice, QueueFamily}; -use vulkano::pipeline::{GraphicsPipeline, GraphicsPipelineAbstract, GraphicsPipelineBuilder}; -use std::sync::Arc; -use std::ffi::CStr; -use std::path::PathBuf; -use shade_runner as sr; -use vulkano::framebuffer::{Subpass, RenderPassAbstract, Framebuffer, FramebufferAbstract}; -use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, SpecializationConstants, SpecializationMapEntry}; -use vulkano::swapchain::{Capabilities}; -use crate::vertex_2d::ColoredVertex2D; - -/* - -Shaderkernel holds the pipeline and render pass for the inputted shader source - -*/ - -#[derive(Clone)] -pub struct ShaderKernels { - - pub render_pass: Arc, - graphics_pipeline: Option>, - - device: Arc, -} - -impl ShaderKernels { - - fn get_path(filename: String) -> (PathBuf, PathBuf) { - - let project_root = - std::env::current_dir() - .expect("failed to get root directory"); - - let mut shader_path = project_root.clone(); - shader_path.push(PathBuf::from("resources/shaders/")); - - let mut vertex_shader_path = project_root.clone(); - vertex_shader_path.push(PathBuf::from("resources/shaders/")); - vertex_shader_path.push(PathBuf::from(filename.clone() + ".vertex")); - - let mut fragment_shader_path = project_root.clone(); - fragment_shader_path.push(PathBuf::from("resources/shaders/")); - fragment_shader_path.push(PathBuf::from(filename.clone() + ".fragment")); - - (vertex_shader_path, fragment_shader_path) - } - - pub fn get_pipeline(&self) -> Arc { - self.graphics_pipeline.clone().unwrap() - } - - pub fn new(filename: String, - capabilities: Capabilities, - queue: Arc, - physical: PhysicalDevice, - device: Arc) -> ShaderKernels { - - let format = capabilities.supported_formats[0].0; - - let filenames = ShaderKernels::get_path(filename.clone()); - - // TODO: better compile message, run til successful compile - let shader = sr::load(filenames.0, filenames.1) - .expect("Shader didn't compile"); - - let vulkano_entry = - sr::parse(&shader) - .expect("failed to parse"); - - let fragment_shader_module: Arc = unsafe { - let filenames1 = ShaderKernels::get_path(filename.clone()); - let shader1 = sr::load(filenames1.0, filenames1.1) - .expect("Shader didn't compile"); - vulkano::pipeline::shader::ShaderModule::from_words(device.clone(), &shader1.fragment.clone()) - }.unwrap(); - - let vertex_shader_module: Arc = unsafe { - let filenames1 = ShaderKernels::get_path(filename.clone()); - let shader1 = sr::load(filenames1.0, filenames1.1) - .expect("Shader didn't compile"); - vulkano::pipeline::shader::ShaderModule::from_words(device.clone(), &shader1.vertex.clone()) - }.unwrap(); - - let filenames = ShaderKernels::get_path(filename.clone()); - - - let frag_entry_point = unsafe { - Some(fragment_shader_module.graphics_entry_point(CStr::from_bytes_with_nul_unchecked(b"main\0"), - vulkano_entry.frag_input, - vulkano_entry.frag_output, - vulkano_entry.frag_layout, - GraphicsShaderType::Fragment)) - }; - - let vertex_entry_point = unsafe { - Some(vertex_shader_module.graphics_entry_point(CStr::from_bytes_with_nul_unchecked(b"main\0"), - vulkano_entry.vert_input, - vulkano_entry.vert_output, - vulkano_entry.vert_layout, - GraphicsShaderType::Vertex)) - }; - - - let render_pass = Arc::new(vulkano::single_pass_renderpass!( - device.clone(), - - // Attachments are outgoing like f_color - attachments: { - // `color` is a custom name we give to the first and only attachment. - color: { - // `load: Clear` means that we ask the GPU to clear the content of this - // attachment at the start of the drawing. - load: Clear, - // `store: Store` means that we ask the GPU to store the output of the draw - // in the actual image. We could also ask it to discard the result. - store: Store, - // `format: ` indicates the type of the format of the image. This has to - // be one of the types of the `vulkano::format` module (or alternatively one - // of your structs that implements the `FormatDesc` trait). Here we use the - // same format as the swapchain. - format: format, - // TODO: - samples: 1, - } - }, - pass: { - // We use the attachment named `color` as the one and only color attachment. - color: [color], - //color: [], - // No depth-stencil attachment is indicated with empty brackets. - depth_stencil: {} - } - ).unwrap()); - - - ShaderKernels { - - graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() - - .vertex_input_single_buffer::() - - .vertex_shader(vertex_entry_point.clone().unwrap(), ShaderSpecializationConstants { - first_constant: 0, - second_constant: 0, - third_constant: 0.0, - }) - - .triangle_list() - // Use a resizable viewport set to draw over the entire window - .viewports_dynamic_scissors_irrelevant(1) - - .fragment_shader(frag_entry_point.clone().unwrap(), ShaderSpecializationConstants { - first_constant: 0, - second_constant: 0, - third_constant: 0.0, - }) - // We have to indicate which subpass of which render pass this pipeline is going to be used - // in. The pipeline will only be usable from this particular subpass. - .render_pass(Subpass::from(render_pass.clone(), 0).unwrap()) - - .build(device.clone()) - .unwrap())), - - device: device, - render_pass: render_pass, - } - } - -} - -#[repr(C)] -#[derive(Default, Debug, Clone)] -// TODO: This needs to be duplicated and moved into their respective containers shaderkenrels copute -struct ShaderSpecializationConstants { - first_constant: i32, - second_constant: u32, - third_constant: f32, -} - -unsafe impl SpecializationConstants for ShaderSpecializationConstants { - fn descriptors() -> &'static [SpecializationMapEntry] { - static DESCRIPTORS: [SpecializationMapEntry; 3] = [ - SpecializationMapEntry { - constant_id: 0, - offset: 0, - size: 4, - }, - SpecializationMapEntry { - constant_id: 1, - offset: 4, - size: 4, - }, - SpecializationMapEntry { - constant_id: 2, - offset: 8, - size: 4, - }, - ]; - - &DESCRIPTORS - } -} \ No newline at end of file