tried modifying buffer from CPU and it worked

working
Nathan Buck 6 months ago
parent 5223bd4fd3
commit 348e33df5e

8251
out.txt

File diff suppressed because it is too large Load Diff

@ -8,24 +8,35 @@ use rand::{
use crate::utility::constants::*; use crate::utility::constants::*;
use std::f32::consts::PI; use std::f32::consts::PI;
use std::fmt; use std::ffi::c_void;
use std::{ fmt, slice };
pub const PARTICLE_COUNT: u64 = 5; pub const PARTICLE_COUNT: u64 = 5;
#[repr(C)] #[repr(C)]
#[derive(Clone, Debug, Copy)] #[derive(Clone, Debug, Copy)]
pub struct Particle { pub struct Particle {
pos: (f32, f32), pub pos: (f32, f32),
vel: (f32, f32), pub vel: (f32, f32),
color: (f32, f32, f32, f32), pub color: (f32, f32, f32, f32),
} }
pub struct ParticlesList(Vec<Particle>); pub struct ParticlesList(pub Vec<Particle>);
impl ParticlesList { impl ParticlesList {
pub fn from_vec(particles: Vec<Particle>) -> ParticlesList { pub fn from_vec(particles: Vec<Particle>) -> ParticlesList {
ParticlesList(particles) ParticlesList(particles)
} }
pub fn from_raw_ptr(ptr: *const Particle, len: usize) -> ParticlesList {
let mut vec = vec![];
vec.extend_from_slice(
unsafe { slice::from_raw_parts(ptr, len) as &[Particle] }
);
ParticlesList(
vec
)
}
} }
impl fmt::Display for ParticlesList { impl fmt::Display for ParticlesList {
@ -70,12 +81,12 @@ impl Particle {
let theta = between.sample(&mut rng) * 2.0 * PI; let theta = between.sample(&mut rng) * 2.0 * PI;
println!("THETA {}", theta); println!("THETA {}", theta);
let x = r * theta.cos() * WINDOW_HEIGHT as f32 / WINDOW_WIDTH as f32; let x = r * theta.cos() * WINDOW_HEIGHT as f32 / WINDOW_WIDTH as f32;
let y = r * theta.sin() * 1000.0; let y = r * theta.sin();
//let (x, y) = (100000.0, 0.0); //let (x, y) = (100000.0, 0.0);
res.push(Particle { res.push(Particle {
pos: (x, y), pos: (x, y),
//vel: vec2_scalar_mul(normalize((x, y)), 0.00025), //vel: vec2_scalar_mul(normalize((x, y)), 0.00025),
vel: (0.0, 0.0), vel: (10000.1, 10000.1),
color: ( color: (
between.sample(&mut rng), between.sample(&mut rng),
between.sample(&mut rng), between.sample(&mut rng),

@ -2,6 +2,7 @@ use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEve
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
use ash::vk; use ash::vk;
use ash::util::Align;
use image; use image;
use image::GenericImageView; use image::GenericImageView;
@ -18,7 +19,8 @@ use crate::shaders::shaders;
use std::ffi::CString; use std::ffi::CString;
use std::ptr; use std::ptr;
use std::mem::size_of; use std::mem;
use mem::size_of;
use std::default; use std::default;
use std::os::raw::{ c_void, c_char }; 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 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); let sync_objects = SyncObjects::new(&device, MAX_FRAMES_IN_FLIGHT);
@ -720,12 +722,13 @@ impl App {
) -> (Vec<vk::Buffer>, Vec<vk::DeviceMemory>) { ) -> (Vec<vk::Buffer>, Vec<vk::DeviceMemory>) {
let mut particles = Particle::gen(); 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 mut slice = Align::new();
//let buffer_size: u64 = 16;
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( let (staging_buffer, staging_buffer_memory) = Self::create_buffer(
@ -741,6 +744,7 @@ impl App {
let mut shader_storage_buffers_memory = vec![]; let mut shader_storage_buffers_memory = vec![];
unsafe { unsafe {
println!("isize max is {}", isize::MAX);
println!("mapping device memory"); println!("mapping device memory");
let mut data = device let mut data = device
.map_memory( .map_memory(
@ -750,16 +754,21 @@ impl App {
vk::MemoryMapFlags::empty(), vk::MemoryMapFlags::empty(),
) )
.expect("failed to map shader storage buffer memory"); .expect("failed to map shader storage buffer memory");
println!("copying to device memory"); let mem_requirements = unsafe { device.get_buffer_memory_requirements(staging_buffer) };
ptr::copy_nonoverlapping(
tst.as_mut_ptr() as *mut c_void, println!("mem req: {}", mem_requirements.size);
let mut slice = Align::new(
data, data,
16 as usize, mem::align_of::<Particle>() as u64,
mem_requirements.size,
); );
//print mapped memory to make sure that the buffers are copied
slice.copy_from_slice(&particles_slice_data);
println!("shader storage buffers: {}", ParticlesList::from_vec((*(data as *const Vec<Particle>)).clone()));
println!("DATA INSIDE PARTICLES BUFFER: {}", ParticlesList::from_raw_ptr(data as *const Particle, particles.len()));
//println!("copying to device memory");
device device
.unmap_memory(staging_buffer_memory); .unmap_memory(staging_buffer_memory);
@ -1149,6 +1158,7 @@ impl App {
&[self.shader_storage_buffers[self.current_frame]], &[self.shader_storage_buffers[self.current_frame]],
offsets, offsets,
); );
println!("binding to shader storage buffer index: {}", self.current_frame);
self.device self.device
.cmd_draw( .cmd_draw(
command_buffer, command_buffer,
@ -1576,6 +1586,8 @@ impl App {
range: size_of::<UniformBufferObject>() as u64, 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 { let storage_buffer_info_last_frame = vk::DescriptorBufferInfo {
buffer: shader_storage_buffers[(i as i32 - 1) as usize % MAX_FRAMES_IN_FLIGHT], buffer: shader_storage_buffers[(i as i32 - 1) as usize % MAX_FRAMES_IN_FLIGHT],
offset: 0, offset: 0,
@ -1703,6 +1715,27 @@ impl App {
command_buffers 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) { fn wait_device_idle(&self) {
unsafe { unsafe {
self.device self.device
@ -1731,7 +1764,7 @@ impl VulkanApp for App {
} }
fn cleanup_swapchain(&self) { fn cleanup_swapchain(&self) {
unimplemented!(); self.swapchain_stuff.cleanup_swapchain(&self.device)
} }
fn recreate_swapchain(&mut self) { fn recreate_swapchain(&mut self) {
@ -1741,25 +1774,26 @@ impl VulkanApp for App {
fn draw_frame(&mut self) { fn draw_frame(&mut self) {
//log shader storage buffers with Particle positions for debugging and recording purposes //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{ for (i, memory) in (&self.shader_storage_buffers_memory).iter().enumerate() {
let data = self.device let mut data = unsafe { self.device
.map_memory( .map_memory(
self.shader_storage_buffers_memory[i], *memory,
0, 0,
buffer_size, buffer_size,
vk::MemoryMapFlags::empty(), vk::MemoryMapFlags::empty(),
) )
.expect("failed to map shader storage buffer memory"); .expect("failed to map shader storage buffer memory") };
//println!("buffer {} data: {:?}", i, *(data as *const Vec<Particle>)); println!("DATA INSIDE PARTICLES BUFFER #{}: {}", i, ParticlesList::from_raw_ptr(data as *const Particle, PARTICLE_COUNT as usize));
unsafe {
self.device self.device
.unmap_memory(self.shader_storage_buffers_memory[i]); .unmap_memory(*memory);
} }
} }
//compute submission //compute submission
let mut submit_infos; let mut submit_infos;
@ -1784,6 +1818,44 @@ impl VulkanApp for App {
self.record_compute_command_buffer(&self.device, self.compute_command_buffers[self.current_frame]); 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 { submit_infos = [vk::SubmitInfo {
s_type: vk::StructureType::SUBMIT_INFO, s_type: vk::StructureType::SUBMIT_INFO,
p_next: ptr::null(), 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 //graphics submission
unsafe { unsafe {

@ -27,11 +27,17 @@ void main()
Particle particleIn = particlesIn[index]; Particle particleIn = particlesIn[index];
//particlesOut[index].position = particleIn.position + particleIn.velocity.xy * ubo.deltaTime; //particlesOut[index].position = particleIn.position + particleIn.velocity.xy * ubo.deltaTime;
//particlesOut[index].position = particleIn.position + vec2(1.0, 1.0) * ubo.deltaTime; particlesOut[index].position = particleIn.position + vec2(1.0, 1.0) * ubo.deltaTime;
particlesOut[index].position = vec2(100.0, 100.0); particlesOut[index].position = vec2(100.0, 100.0);
particlesOut[0].position = vec2(100.0, 100.0);
//particlesOut[index].position = vec2(100, 100); //particlesOut[index].position = vec2(100, 100);
particlesOut[index].velocity = particleIn.velocity; particlesOut[index].velocity = particleIn.velocity;
particlesOut[index].color = vec4(150.0, 150.0, 150.0, 150.0); particlesOut[index].color = vec4(150.0, 150.0, 150.0, 150.0);
particlesOut[0].color = vec4(1.0, 1.0, 1.0, 1.0);
particlesOut[1].color = vec4(1.0, 1.0, 1.0, 1.0);
particlesOut[2].color = vec4(1.0, 1.0, 1.0, 1.0);
particlesOut[3].color = vec4(1.0, 1.0, 1.0, 1.0);
particlesOut[4].color = vec4(1.0, 1.0, 1.0, 1.0);
// Flip movement at window border // Flip movement at window border
if ((particlesOut[index].position.x <= -1.0) || (particlesOut[index].position.x >= 1.0)) { if ((particlesOut[index].position.x <= -1.0) || (particlesOut[index].position.x >= 1.0)) {

@ -7,8 +7,8 @@ layout(location = 0) out vec3 fragColor;
void main() { void main() {
gl_PointSize = 140.0; gl_PointSize = 14.0;
gl_Position = vec4(inPosition, 1.0, 1.0); gl_Position = vec4(inPosition, 1.0, 1.0);
//fragColor = inColor.rgb; fragColor = inColor.rgb;
fragColor = vec3(1.0, 1.0, 1.0); //fragColor = vec3(1.0, 1.0, 1.0);
} }

@ -17,7 +17,7 @@ pub const VALIDATION: ValidationInfo = ValidationInfo {
pub const DEVICE_EXTENSIONS: DeviceExtension = DeviceExtension { pub const DEVICE_EXTENSIONS: DeviceExtension = DeviceExtension {
names: ["VK_KHR_swapchain"], names: ["VK_KHR_swapchain"],
}; };
pub const MAX_FRAMES_IN_FLIGHT: usize = 2; pub const MAX_FRAMES_IN_FLIGHT: usize = 4;
pub const IS_PAINT_FPS_COUNTER: bool = true; pub const IS_PAINT_FPS_COUNTER: bool = true;
impl DeviceExtension { impl DeviceExtension {

@ -8,6 +8,7 @@ use memoffset::offset_of;
use super::platforms; use super::platforms;
use crate::render::App; use crate::render::App;
use crate::render::*; use crate::render::*;
use crate::utility::window::VulkanApp;
pub struct DeviceExtension { pub struct DeviceExtension {
pub names: [&'static str; 1], pub names: [&'static str; 1],
@ -154,7 +155,7 @@ impl SwapChainStuff {
unsafe { unsafe {
self.swapchain_loader self.swapchain_loader
.destroy_swapchain(swapchain, None) .destroy_swapchain(self.swapchain, None)
} }
} }
@ -164,7 +165,7 @@ impl SwapChainStuff {
.device_wait_idle(); .device_wait_idle();
} }
app.cleanup_swapchain(&app.device); app.cleanup_swapchain();
app.swapchain_stuff = SwapChainStuff::new( app.swapchain_stuff = SwapChainStuff::new(
&app.instance, &app.instance,

Loading…
Cancel
Save