From 8801d9268ddad18e5acaae1d469374836fe5fefe Mon Sep 17 00:00:00 2001 From: buckn Date: Fri, 29 Dec 2023 18:31:56 -0500 Subject: [PATCH] non working update, about to cargo fix --- build.rs | 28 +++++----- src/entities.rs | 26 +++++---- src/main.rs | 6 +- src/render.rs | 19 ++----- src/utility/debug.rs | 1 - src/utility/share.rs | 128 +++++++++++++++++++++++++++++++------------ 6 files changed, 133 insertions(+), 75 deletions(-) diff --git a/build.rs b/build.rs index e3c16ec..dc24b92 100644 --- a/build.rs +++ b/build.rs @@ -20,7 +20,10 @@ fn main() -> std::io::Result<()> { let shader_path_string: String = "./target/shaders/".to_string() + shader_path.file_name().unwrap().to_str().unwrap() + ".spv"; - println!("cargo:warning=compiling destination: {:?}", shader_path_string); + println!( + "cargo:warning=compiling destination: {:?}", + shader_path_string + ); let shader_file: &OsStr = OsStr::new::(shader_path_string.as_str()); Command::new("glslc") .arg("-c") @@ -40,18 +43,17 @@ fn main() -> std::io::Result<()> { if let Ok(entry) = entry { let bin_path = entry.path(); let bin_path_string = bin_path.file_name().unwrap().to_str().unwrap().to_string(); - - let shader_type = ( - match bin_path_string.split(".").nth(1).unwrap() { - "vert" => "VERTEX", - "frag" => "FRAGMENT", - "comp" => "COMPUTE", - "tesc" => "TESSELLATION_CONTROL", - "tese" => "TESSELLATION_EVALUATION", - "geom" => "GEOMETRY", - _ => panic!("unknown shader type"), - } - ).to_string(); + + let shader_type = (match bin_path_string.split(".").nth(1).unwrap() { + "vert" => "VERTEX", + "frag" => "FRAGMENT", + "comp" => "COMPUTE", + "tesc" => "TESSELLATION_CONTROL", + "tese" => "TESSELLATION_EVALUATION", + "geom" => "GEOMETRY", + _ => panic!("unknown shader type"), + }) + .to_string(); txt += &("(include_bytes!(\"../target/shaders/".to_owned() + &bin_path_string + "\").to_vec(), ShaderStageFlags::" diff --git a/src/entities.rs b/src/entities.rs index 2016fc8..2e7f3d0 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -1,7 +1,10 @@ -use memoffset::offset_of; use ash::vk; -use rand::{thread_rng, distributions::{Distribution, Uniform}}; use cgmath::{InnerSpace, Vector2, Vector4}; +use memoffset::offset_of; +use rand::{ + distributions::{Distribution, Uniform}, + thread_rng, +}; use crate::utility::constants::*; use std::f32::consts::PI; @@ -19,7 +22,7 @@ pub struct Particle { impl Particle { pub fn gen() -> Vec { let mut res = vec![]; - + let between = Uniform::from(0.0..1.0); let mut rng = thread_rng(); @@ -28,13 +31,16 @@ impl Particle { let theta = between.sample(&mut rng) * 2.0 * PI; let x = r * theta.cos() * WINDOW_HEIGHT as f32 * WINDOW_WIDTH as f32; let y = r * theta.sin(); - res.push( - Particle { - pos: Vector2::new(x, y), - vel: Vector2::new(x, y).normalize() * 0.00025, - color: Vector4::new(between.sample(&mut rng), between.sample(&mut rng), between.sample(&mut rng), 1.0), - } - ) + res.push(Particle { + pos: Vector2::new(x, y), + vel: Vector2::new(x, y).normalize() * 0.00025, + color: Vector4::new( + between.sample(&mut rng), + between.sample(&mut rng), + between.sample(&mut rng), + 1.0, + ), + }) } res } diff --git a/src/main.rs b/src/main.rs index bb804f7..e9975fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ +pub mod entities; +pub mod render; pub mod shaders; pub mod utility; -pub mod render; -pub mod entities; -use winit::event_loop::EventLoop; use render::VulkanApp; +use winit::event_loop::EventLoop; fn main() { let event_loop = EventLoop::new(); diff --git a/src/render.rs b/src/render.rs index 539a9b7..c8f2dc6 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,10 +1,10 @@ use crate::{ - shaders::*, entities::*, utility, utility::constants::*, utility::debug::*, utility::share, utility::structures::*, + entities::*, shaders::*, utility, utility::constants::*, utility::debug::*, utility::share, + utility::structures::*, }; use ash::{vk, Entry}; - use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; @@ -362,8 +362,7 @@ impl VulkanApp { let mut shader_modules: Vec = vec![]; let main_function = CString::new("main").unwrap(); for (shader, stage_i) in shaders() { - shader_modules.push( - vk::PipelineShaderStageCreateInfo { + shader_modules.push(vk::PipelineShaderStageCreateInfo { s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO, p_next: ptr::null(), flags: vk::PipelineShaderStageCreateFlags::empty(), @@ -371,9 +370,7 @@ impl VulkanApp { p_name: main_function.as_ptr(), stage: stage_i, p_specialization_info: ptr::null(), - - } - ) + }) } let binding_description = Vertex::get_binding_description(); @@ -674,11 +671,8 @@ impl VulkanApp { self.swapchain_format = swapchain_stuff.swapchain_format; self.swapchain_extent = swapchain_stuff.swapchain_extent; - self.swapchain_imageviews = share::create_image_views( - &self.device, - self.swapchain_format, - &self.swapchain_images, - ); + self.swapchain_imageviews = + share::create_image_views(&self.device, self.swapchain_format, &self.swapchain_images); self.render_pass = share::create_render_pass(&self.device, self.swapchain_format); let (graphics_pipeline, pipeline_layout) = VulkanApp::create_graphics_pipeline( &self.device, @@ -798,4 +792,3 @@ impl VulkanApp { }) } } - diff --git a/src/utility/debug.rs b/src/utility/debug.rs index eb621f5..2720a29 100644 --- a/src/utility/debug.rs +++ b/src/utility/debug.rs @@ -1,6 +1,5 @@ use ash::vk; - use std::ffi::CStr; use std::os::raw::c_void; use std::ptr; diff --git a/src/utility/share.rs b/src/utility/share.rs index 55c55ad..4492798 100644 --- a/src/utility/share.rs +++ b/src/utility/share.rs @@ -2,16 +2,14 @@ use ash::vk; use image; use image::GenericImageView; +use crate::utility::constants::*; +use crate::utility::structures::*; +use crate::utility::{debug, platforms}; use std::cmp::max; use std::ffi::CString; -use std::path::Path; -use std::ptr; use std::os::raw::{c_char, c_void}; -use crate::utility::constants::*; -use crate::utility::{debug, platforms}; -use crate::utility::structures::*; - - +use std::path::Path; +use std::{ptr, mem::size_of}; pub fn create_render_pass(device: &ash::Device, surface_format: vk::Format) -> vk::RenderPass { let color_attachment = vk::AttachmentDescription { @@ -337,7 +335,8 @@ pub fn create_descriptor_sets( device: &ash::Device, descriptor_pool: vk::DescriptorPool, descriptor_set_layout: vk::DescriptorSetLayout, - uniforms_buffers: &Vec, + uniform_buffers: Option<&Vec>, + compute_buffers: Option<&Vec>, swapchain_images_size: usize, ) -> Vec { let mut layouts: Vec = vec![]; @@ -360,24 +359,68 @@ pub fn create_descriptor_sets( }; for (i, &descritptor_set) in descriptor_sets.iter().enumerate() { - let descriptor_buffer_info = [vk::DescriptorBufferInfo { - buffer: uniforms_buffers[i], - offset: 0, - range: ::std::mem::size_of::() as u64, - }]; - - let descriptor_write_sets = [vk::WriteDescriptorSet { - s_type: vk::StructureType::WRITE_DESCRIPTOR_SET, - p_next: ptr::null(), - dst_set: descritptor_set, - dst_binding: 0, - dst_array_element: 0, - descriptor_count: 1, - descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, - p_image_info: ptr::null(), - p_buffer_info: descriptor_buffer_info.as_ptr(), - p_texel_buffer_view: ptr::null(), - }]; + let uniform_descriptor_info = vec![]; + let compute_descriptor_info = vec![]; + + let uniform_buffers_inner = uniform_buffers.unwrap(); + let compute_buffers_inner = compute_buffers.unwrap(); + + for (i, buffer) in uniform_buffers_inner.iter().enumerate() { + uniform_descriptor_info.push( + vk::DescriptorBufferInfo { + buffer: uniform_buffers.unwrap()[i as usize], + offset: i as u64, + range: size_of:: as u64, + } + ); + } + for (i, buffer) in compute_buffers_inner.iter().enumerate() { + compute_descriptor_info.push( + vk::DescriptorBufferInfo { + buffer: compute_buffers.unwrap()[i as usize], + offset: i as u64, + range: size_of:: as u64, + } + ); + } + let mut descriptor_write_sets = vec![]; + + let mut descriptor_count = 0; + + for info in &uniform_descriptor_info { + descriptor_write_sets.push( + vk::WriteDescriptorSet { + s_type: vk::StructureType::WRITE_DESCRIPTOR_SET, + p_next: ptr::null(), + dst_set: descritptor_set, + dst_binding: descriptor_count, + dst_array_element: 0, + descriptor_count: uniform_descriptor_info.len() as u32, + descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, + p_image_info: ptr::null(), + p_buffer_info: uniform_descriptor_info.as_ptr(), + p_texel_buffer_view: ptr::null(), + } + ); + descriptor_count += 1; + } + for info in &compute_descriptor_info { + descriptor_write_sets.push( + vk::WriteDescriptorSet { + s_type: vk::StructureType::WRITE_DESCRIPTOR_SET, + p_next: ptr::null(), + dst_set: descritptor_set, + dst_binding: descriptor_count, + dst_array_element: 0, + descriptor_count: compute_descriptor_info.len() as u32, + descriptor_type: vk::DescriptorType::STORAGE_BUFFER, + p_image_info: ptr::null(), + p_buffer_info: compute_descriptor_info.as_ptr(), + p_texel_buffer_view: ptr::null(), + } + ); + descriptor_count += 1; + } unsafe { device.update_descriptor_sets(&descriptor_write_sets, &[]); @@ -388,13 +431,29 @@ pub fn create_descriptor_sets( } pub fn create_descriptor_set_layout(device: &ash::Device) -> vk::DescriptorSetLayout { - let ubo_layout_bindings = [vk::DescriptorSetLayoutBinding { - binding: 0, - descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, - descriptor_count: 1, - stage_flags: vk::ShaderStageFlags::VERTEX, - p_immutable_samplers: ptr::null(), - }]; + let ubo_layout_bindings = [ + vk::DescriptorSetLayoutBinding { + binding: 0, + descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, + descriptor_count: 1, + stage_flags: vk::ShaderStageFlags::VERTEX, + p_immutable_samplers: ptr::null(), + }, + vk::DescriptorSetLayoutBinding { + binding: 1, + descriptor_type: vk::DescriptorType::STORAGE_BUFFER, + descriptor_count: 1, + stage_flags: vk::ShaderStageFlags::COMPUTE, + p_immutable_samplers: ptr::null(), + }, + vk::DescriptorSetLayoutBinding { + binding: 2, + descriptor_type: vk::DescriptorType::STORAGE_BUFFER, + descriptor_count: 1, + stage_flags: vk::ShaderStageFlags::COMPUTE, + p_immutable_samplers: ptr::null(), + }, + ]; let ubo_layout_create_info = vk::DescriptorSetLayoutCreateInfo { s_type: vk::StructureType::DESCRIPTOR_SET_LAYOUT_CREATE_INFO, @@ -476,8 +535,7 @@ pub fn create_image( .expect("Failed to create Texture Image!") }; - let image_memory_requirement = - unsafe { device.get_image_memory_requirements(texture_image) }; + let image_memory_requirement = unsafe { device.get_image_memory_requirements(texture_image) }; let memory_allocate_info = vk::MemoryAllocateInfo { s_type: vk::StructureType::MEMORY_ALLOCATE_INFO, p_next: ptr::null(),