|
|
|
@ -2,6 +2,7 @@ use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEve
|
|
|
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
|
|
|
|
|
|
|
|
|
use ash::vk;
|
|
|
|
|
use ash::util::Align;
|
|
|
|
|
use image;
|
|
|
|
|
use image::GenericImageView;
|
|
|
|
|
|
|
|
|
@ -18,7 +19,8 @@ use crate::shaders::shaders;
|
|
|
|
|
|
|
|
|
|
use std::ffi::CString;
|
|
|
|
|
use std::ptr;
|
|
|
|
|
use std::mem::size_of;
|
|
|
|
|
use std::mem;
|
|
|
|
|
use mem::size_of;
|
|
|
|
|
use std::default;
|
|
|
|
|
use std::os::raw::{ c_void, c_char };
|
|
|
|
|
|
|
|
|
@ -322,7 +324,7 @@ impl App {
|
|
|
|
|
|
|
|
|
|
let command_buffers = Self::create_command_buffers(&device, command_pool);
|
|
|
|
|
|
|
|
|
|
let compute_command_buffers = Self::create_command_buffers(&device, command_pool);
|
|
|
|
|
let compute_command_buffers = Self::create_compute_command_buffers(&device, command_pool);
|
|
|
|
|
|
|
|
|
|
let sync_objects = SyncObjects::new(&device, MAX_FRAMES_IN_FLIGHT);
|
|
|
|
|
|
|
|
|
@ -720,12 +722,13 @@ impl App {
|
|
|
|
|
) -> (Vec<vk::Buffer>, Vec<vk::DeviceMemory>) {
|
|
|
|
|
let mut particles = Particle::gen();
|
|
|
|
|
|
|
|
|
|
let mut tst: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
|
|
|
|
let mut particles_slice_data = particles.as_mut_slice();
|
|
|
|
|
|
|
|
|
|
let buffer_size: u64 = std::mem::size_of::<Particle>() as u64 * PARTICLE_COUNT as u64;
|
|
|
|
|
//let buffer_size: u64 = 16;
|
|
|
|
|
//let mut slice = Align::new();
|
|
|
|
|
|
|
|
|
|
println!("particles count: {}, particle size: {}, buffer size: {}", PARTICLE_COUNT, std::mem::size_of::<Particle>(), buffer_size as usize);
|
|
|
|
|
let mut buffer_size: u64 = std::mem::size_of::<Particle>() as u64 * particles_slice_data.len() as u64;
|
|
|
|
|
|
|
|
|
|
println!("particles count: {}, particle size: {}, buffer size: {}", PARTICLE_COUNT as u64, std::mem::size_of::<Particle>() as u64, buffer_size as usize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (staging_buffer, staging_buffer_memory) = Self::create_buffer(
|
|
|
|
@ -741,6 +744,7 @@ impl App {
|
|
|
|
|
let mut shader_storage_buffers_memory = vec![];
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
println!("isize max is {}", isize::MAX);
|
|
|
|
|
println!("mapping device memory");
|
|
|
|
|
let mut data = device
|
|
|
|
|
.map_memory(
|
|
|
|
@ -751,15 +755,20 @@ impl App {
|
|
|
|
|
)
|
|
|
|
|
.expect("failed to map shader storage buffer memory");
|
|
|
|
|
|
|
|
|
|
println!("copying to device memory");
|
|
|
|
|
ptr::copy_nonoverlapping(
|
|
|
|
|
tst.as_mut_ptr() as *mut c_void,
|
|
|
|
|
let mem_requirements = unsafe { device.get_buffer_memory_requirements(staging_buffer) };
|
|
|
|
|
|
|
|
|
|
println!("mem req: {}", mem_requirements.size);
|
|
|
|
|
let mut slice = Align::new(
|
|
|
|
|
data,
|
|
|
|
|
16 as usize,
|
|
|
|
|
mem::align_of::<Particle>() as u64,
|
|
|
|
|
mem_requirements.size,
|
|
|
|
|
);
|
|
|
|
|
//print mapped memory to make sure that the buffers are copied
|
|
|
|
|
|
|
|
|
|
println!("shader storage buffers: {}", ParticlesList::from_vec((*(data as *const Vec<Particle>)).clone()));
|
|
|
|
|
slice.copy_from_slice(&particles_slice_data);
|
|
|
|
|
|
|
|
|
|
println!("DATA INSIDE PARTICLES BUFFER: {}", ParticlesList::from_raw_ptr(data as *const Particle, particles.len()));
|
|
|
|
|
|
|
|
|
|
//println!("copying to device memory");
|
|
|
|
|
device
|
|
|
|
|
.unmap_memory(staging_buffer_memory);
|
|
|
|
|
|
|
|
|
@ -1149,6 +1158,7 @@ impl App {
|
|
|
|
|
&[self.shader_storage_buffers[self.current_frame]],
|
|
|
|
|
offsets,
|
|
|
|
|
);
|
|
|
|
|
println!("binding to shader storage buffer index: {}", self.current_frame);
|
|
|
|
|
self.device
|
|
|
|
|
.cmd_draw(
|
|
|
|
|
command_buffer,
|
|
|
|
@ -1576,6 +1586,8 @@ impl App {
|
|
|
|
|
range: size_of::<UniformBufferObject>() as u64,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
println!("STORAGE BUFFERS INDEXES: {}, {}", i, (i as i32 - 1) as usize % MAX_FRAMES_IN_FLIGHT);
|
|
|
|
|
|
|
|
|
|
let storage_buffer_info_last_frame = vk::DescriptorBufferInfo {
|
|
|
|
|
buffer: shader_storage_buffers[(i as i32 - 1) as usize % MAX_FRAMES_IN_FLIGHT],
|
|
|
|
|
offset: 0,
|
|
|
|
@ -1703,6 +1715,27 @@ impl App {
|
|
|
|
|
|
|
|
|
|
command_buffers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_compute_command_buffers(
|
|
|
|
|
device: &ash::Device,
|
|
|
|
|
command_pool: vk::CommandPool,
|
|
|
|
|
) -> Vec<vk::CommandBuffer> {
|
|
|
|
|
let command_buffer_allocate_info = vk::CommandBufferAllocateInfo {
|
|
|
|
|
s_type: vk::StructureType::COMMAND_BUFFER_ALLOCATE_INFO,
|
|
|
|
|
p_next: ptr::null(),
|
|
|
|
|
command_buffer_count: MAX_FRAMES_IN_FLIGHT as u32,
|
|
|
|
|
command_pool,
|
|
|
|
|
level: vk::CommandBufferLevel::PRIMARY,
|
|
|
|
|
};
|
|
|
|
|
let command_buffers = unsafe {
|
|
|
|
|
device
|
|
|
|
|
.allocate_command_buffers(&command_buffer_allocate_info)
|
|
|
|
|
.expect("Failed to allocate Command Buffers!")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
command_buffers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn wait_device_idle(&self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
self.device
|
|
|
|
@ -1731,7 +1764,7 @@ impl VulkanApp for App {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cleanup_swapchain(&self) {
|
|
|
|
|
unimplemented!();
|
|
|
|
|
self.swapchain_stuff.cleanup_swapchain(&self.device)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn recreate_swapchain(&mut self) {
|
|
|
|
@ -1741,23 +1774,24 @@ impl VulkanApp for App {
|
|
|
|
|
fn draw_frame(&mut self) {
|
|
|
|
|
//log shader storage buffers with Particle positions for debugging and recording purposes
|
|
|
|
|
|
|
|
|
|
let buffer_size: u64 = std::mem::size_of::<Particle>() as u64 * PARTICLE_COUNT as u64;
|
|
|
|
|
let mut buffer_size: u64 = std::mem::size_of::<Particle>() as u64 * PARTICLE_COUNT as u64;
|
|
|
|
|
|
|
|
|
|
for i in 0..MAX_FRAMES_IN_FLIGHT {
|
|
|
|
|
unsafe{
|
|
|
|
|
let data = self.device
|
|
|
|
|
.map_memory(
|
|
|
|
|
self.shader_storage_buffers_memory[i],
|
|
|
|
|
0,
|
|
|
|
|
buffer_size,
|
|
|
|
|
vk::MemoryMapFlags::empty(),
|
|
|
|
|
)
|
|
|
|
|
.expect("failed to map shader storage buffer memory");
|
|
|
|
|
//println!("buffer {} data: {:?}", i, *(data as *const Vec<Particle>));
|
|
|
|
|
|
|
|
|
|
for (i, memory) in (&self.shader_storage_buffers_memory).iter().enumerate() {
|
|
|
|
|
let mut data = unsafe { self.device
|
|
|
|
|
.map_memory(
|
|
|
|
|
*memory,
|
|
|
|
|
0,
|
|
|
|
|
buffer_size,
|
|
|
|
|
vk::MemoryMapFlags::empty(),
|
|
|
|
|
)
|
|
|
|
|
.expect("failed to map shader storage buffer memory") };
|
|
|
|
|
println!("DATA INSIDE PARTICLES BUFFER #{}: {}", i, ParticlesList::from_raw_ptr(data as *const Particle, PARTICLE_COUNT as usize));
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
self.device
|
|
|
|
|
.unmap_memory(self.shader_storage_buffers_memory[i]);
|
|
|
|
|
.unmap_memory(*memory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//compute submission
|
|
|
|
@ -1784,6 +1818,44 @@ impl VulkanApp for App {
|
|
|
|
|
|
|
|
|
|
self.record_compute_command_buffer(&self.device, self.compute_command_buffers[self.current_frame]);
|
|
|
|
|
|
|
|
|
|
/*for i in 0..MAX_FRAMES_IN_FLIGHT {
|
|
|
|
|
self.record_compute_command_buffer(&self.device, self.compute_command_buffers[i]);
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
/*for (i, memory) in (&self.shader_storage_buffers_memory).iter().enumerate() {
|
|
|
|
|
let mut data = unsafe { self.device
|
|
|
|
|
.map_memory(
|
|
|
|
|
*memory,
|
|
|
|
|
0,
|
|
|
|
|
buffer_size,
|
|
|
|
|
vk::MemoryMapFlags::empty(),
|
|
|
|
|
)
|
|
|
|
|
.expect("failed to map shader storage buffer memory") };
|
|
|
|
|
println!("DATA INSIDE PARTICLES BUFFER #{}: {}", i, ParticlesList::from_raw_ptr(data as *const Particle, PARTICLE_COUNT as usize));
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
self.device
|
|
|
|
|
.unmap_memory(*memory);
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.update_uniform_buffer(self.current_frame);
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
self.device
|
|
|
|
|
.reset_fences(&[self.sync_objects.compute_inflight_fences[self.current_frame]]);
|
|
|
|
|
|
|
|
|
|
self.device
|
|
|
|
|
.reset_command_buffer(self.compute_command_buffers[self.current_frame], vk::CommandBufferResetFlags::empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//self.record_compute_command_buffer(&self.device, self.compute_command_buffers[self.current_frame]);
|
|
|
|
|
|
|
|
|
|
for i in 0..MAX_FRAMES_IN_FLIGHT {
|
|
|
|
|
self.record_compute_command_buffer(&self.device, self.compute_command_buffers[self.current_frame]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
submit_infos = [vk::SubmitInfo {
|
|
|
|
|
s_type: vk::StructureType::SUBMIT_INFO,
|
|
|
|
|
p_next: ptr::null(),
|
|
|
|
@ -1804,6 +1876,59 @@ impl VulkanApp for App {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mem_requirements = unsafe { self.device.get_buffer_memory_requirements(self.shader_storage_buffers[0]) };
|
|
|
|
|
println!("current frame: {}", self.current_frame);
|
|
|
|
|
|
|
|
|
|
for (i, memory) in (&self.shader_storage_buffers_memory).iter().enumerate() {
|
|
|
|
|
//if i != self.current_frame { continue; }
|
|
|
|
|
let is_current_buffer = i != self.current_frame;
|
|
|
|
|
let mut data = unsafe { self.device
|
|
|
|
|
.map_memory(
|
|
|
|
|
*memory,
|
|
|
|
|
0,
|
|
|
|
|
buffer_size,
|
|
|
|
|
vk::MemoryMapFlags::empty(),
|
|
|
|
|
)
|
|
|
|
|
.expect("failed to map shader storage buffer memory") };
|
|
|
|
|
let particles_list = ParticlesList::from_raw_ptr(data as *const Particle, PARTICLE_COUNT as usize);
|
|
|
|
|
let mut particles_vec = particles_list.0;
|
|
|
|
|
if !is_current_buffer {
|
|
|
|
|
particles_vec[0].color = (0.0, 0.0, 0.0, 0.0);
|
|
|
|
|
particles_vec[1].color = (0.0, 0.0, 0.0, 0.0);
|
|
|
|
|
particles_vec[2].color = (0.0, 0.0, 0.0, 0.0);
|
|
|
|
|
particles_vec[3].color = (0.0, 0.0, 0.0, 0.0);
|
|
|
|
|
particles_vec[4].color = (0.0, 0.0, 0.0, 0.0);
|
|
|
|
|
} else {
|
|
|
|
|
particles_vec[0].color = (1.0, 0.0, 1.0, 0.0);
|
|
|
|
|
particles_vec[1].color = (1.0, 0.0, 1.0, 0.0);
|
|
|
|
|
particles_vec[2].color = (1.0, 0.0, 1.0, 0.0);
|
|
|
|
|
particles_vec[3].color = (1.0, 0.0, 1.0, 0.0);
|
|
|
|
|
particles_vec[4].color = (1.0, 0.0, 1.0, 0.0);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
let particles_slice_data = particles_vec.as_mut_slice();
|
|
|
|
|
let mut buffer_size: u64 = std::mem::size_of::<Particle>() as u64 * particles_slice_data.len() as u64;
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
let mut slice = Align::new(
|
|
|
|
|
data,
|
|
|
|
|
mem::align_of::<Particle>() as u64,
|
|
|
|
|
mem_requirements.size,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
slice.copy_from_slice(&particles_slice_data);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//println!("DATA INSIDE PARTICLES BUFFER #{}: {}", i, ParticlesList::from_raw_ptr(data as *const Particle, PARTICLE_COUNT as usize));
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
self.device
|
|
|
|
|
.unmap_memory(*memory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//graphics submission
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|