moves compiled shaders, first version with scene info on gpu

This commit is contained in:
zomseffen 2025-01-07 23:21:09 +01:00
parent 57e862832a
commit 694d93c0f3
20 changed files with 623 additions and 113 deletions

View file

@ -5,6 +5,7 @@ use anyhow::Result;
use cgmath::{vec2, vec3, Vector3};
use std::cell::RefCell;
use std::mem;
use std::rc::Rc;
use std::time::Instant;
@ -20,11 +21,12 @@ use crate::primitives::drawable::Drawable;
extern crate rand;
use rand::Rng;
const CHUNK_SIZE_EXPONENT: u32 = 6;
const CHUNK_SIZE_EXPONENT: u32 = 4;
const CHUNK_SIZE: usize = (2 as usize).pow(CHUNK_SIZE_EXPONENT);
const MAX_TREE_DEPTH: usize = 4;
const MAX_TREE_DEPTH: usize = 2;
const MIN_CHUNK_SIZE: usize = CHUNK_SIZE / (2 as usize).pow(MAX_TREE_DEPTH as u32);
#[repr(C)]
#[derive(Clone, Debug, Default)]
pub struct Scene {
pub vertices: Vec<vertex::Vertex>,
@ -51,10 +53,12 @@ pub struct Scene {
pub index_buffer_quad: vk::Buffer,
pub index_buffer_memory_quad: vk::DeviceMemory,
pub rt_memory: Vec<u32>,
}
impl Scene {
pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &AppData) -> Result<()> {
pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &mut AppData) -> Result<()> {
let mut rng = rand::thread_rng();
let grid_size = CHUNK_SIZE as i32;
@ -111,13 +115,34 @@ impl Scene {
None => {}
}
}
let mut memory_index = 1; // zero should be the location for the overall length (also will be the invalid memory allocation for pointing to a nonexistant neighbor)
for volume in &empty_volumes {
volume.borrow_mut().memory_start = memory_index;
memory_index += volume.borrow().get_buffer_mem_size() as usize;
for volume in empty_volumes {
}
for volume in &empty_volumes {
let quads = volume.borrow().to_quads();
for quad in quads {
quad.draw(&data.topology, self.rt_vertices.len(), self);
}
}
println!("Memory size is {} kB, max indes is {}", memory_index * 32 / 8 /1024 + 1, memory_index);
let mut volume_vec = vec![memory_index as u32; memory_index];
for volume in &empty_volumes {
volume_vec = volume.borrow().insert_into_memory(volume_vec);
}
println!("volume_vec print {:?}", volume_vec);
let mut empty_count = 0;
for element in &volume_vec {
if *element == memory_index as u32 {
empty_count += 1;
}
}
println!("empty elements count is {}", empty_count);
self.rt_memory = volume_vec;
data.scene_rt_memory_size = (self.rt_memory.len() * 4) as u64; // size of the needed buffer size in bytes
if self.vertices.len() != 0 {
(self.vertex_buffer_cube, self.vertex_buffer_memory_cube) = buffer::create_vertex_buffer(instance, device, &data, &self.vertices)?;
@ -658,12 +683,12 @@ struct EmptyVolume {
pub color_back: Vec<Vector3<u8>>,
pub color_front: Vec<Vector3<u8>>,
pub roughness_left: Vec<Vector3<u8>>,
pub roughness_right: Vec<Vector3<u8>>,
pub roughness_top: Vec<Vector3<u8>>,
pub roughness_bottom: Vec<Vector3<u8>>,
pub roughness_back: Vec<Vector3<u8>>,
pub roughness_front: Vec<Vector3<u8>>,
pub roughness_left: Vec<u8>,
pub roughness_right: Vec<u8>,
pub roughness_top: Vec<u8>,
pub roughness_bottom: Vec<u8>,
pub roughness_back: Vec<u8>,
pub roughness_front: Vec<u8>,
pub neighbor_left: Vec<Option<Rc<RefCell<Self>>>>,
pub neighbor_right: Vec<Option<Rc<RefCell<Self>>>>,
@ -842,11 +867,11 @@ impl EmptyVolume {
bottom_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
bottom_colors.push(u8_color);
bottom_roughness.push(Vector3 {x: 128, y: 128, z: 128});
bottom_roughness.push(128);
}
else {
bottom_colors.push(Vector3 { x: 255, y: 255, z: 255 });
bottom_roughness.push(Vector3 {x: 255, y: 255, z: 255});
bottom_roughness.push(255);
}
}
}
@ -868,11 +893,11 @@ impl EmptyVolume {
top_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
top_colors.push(u8_color);
top_roughness.push(Vector3 {x: 128, y: 128, z: 128});
top_roughness.push(128);
}
else {
top_colors.push(Vector3 { x: 255, y: 255, z: 255 });
top_roughness.push(Vector3 {x: 255, y: 255, z: 255});
top_roughness.push(255);
}
}
}
@ -895,11 +920,11 @@ impl EmptyVolume {
back_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
back_colors.push(u8_color);
back_roughness.push(Vector3 {x: 128, y: 128, z: 128});
back_roughness.push(128);
}
else {
back_colors.push(Vector3 { x: 255, y: 255, z: 255 });
back_roughness.push(Vector3 {x: 255, y: 255, z: 255});
back_roughness.push(255);
}
}
}
@ -922,11 +947,11 @@ impl EmptyVolume {
front_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
front_colors.push(u8_color);
front_roughness.push(Vector3 {x: 128, y: 128, z: 128});
front_roughness.push(128);
}
else {
front_colors.push(Vector3 { x: 255, y: 255, z: 255 });
front_roughness.push(Vector3 {x: 255, y: 255, z: 255});
front_roughness.push(255);
}
}
}
@ -949,11 +974,11 @@ impl EmptyVolume {
left_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
left_colors.push(u8_color);
left_roughness.push(Vector3 {x: 128, y: 128, z: 128});
left_roughness.push(128);
}
else {
left_colors.push(Vector3 { x: 255, y: 255, z: 255 });
left_roughness.push(Vector3 {x: 255, y: 255, z: 255});
left_roughness.push(255);
}
}
}
@ -976,11 +1001,11 @@ impl EmptyVolume {
right_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
right_colors.push(u8_color);
right_roughness.push(Vector3 {x: 128, y: 128, z: 128});
right_roughness.push(128);
}
else {
right_colors.push(Vector3 { x: 255, y: 255, z: 255 });
right_roughness.push(Vector3 {x: 255, y: 255, z: 255});
right_roughness.push(255);
}
}
}
@ -1186,7 +1211,8 @@ impl EmptyVolume {
pos3: float_pos + Vector3 { x: 0.5 + x as f32, y: 0.5 + y as f32, z: -0.5 },
pos2: float_pos + Vector3 { x: -0.5 + x as f32, y: 0.5 + y as f32, z: -0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: y as u32 },
volume_index: 0
volume_index: self.memory_start as u32,
facing: vertex::Facing::Bottom
};
quads.push(quad);
}
@ -1209,7 +1235,8 @@ impl EmptyVolume {
pos2: float_pos + Vector3 { x: 0.5 + x as f32, y: 0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos3: float_pos + Vector3 { x: -0.5 + x as f32, y: 0.5 + y as f32, z: self.size_z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: y as u32 },
volume_index: 0
volume_index: self.memory_start as u32,
facing: vertex::Facing::Top
};
quads.push(quad);
}
@ -1233,7 +1260,8 @@ impl EmptyVolume {
pos3: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 + 0.5 },
pos2: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: z as u32 },
volume_index: 0
volume_index: self.memory_start as u32,
facing: vertex::Facing::Front
};
quads.push(quad);
}
@ -1257,7 +1285,8 @@ impl EmptyVolume {
pos2: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 + 0.5 },
pos3: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: x as u32, y: z as u32 },
volume_index: 0
volume_index: self.memory_start as u32,
facing: vertex::Facing::Back
};
quads.push(quad);
}
@ -1281,7 +1310,8 @@ impl EmptyVolume {
pos2: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 + 0.5, z: z as f32 + 0.5 },
pos3: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 + 0.5, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: y as u32, y: z as u32 },
volume_index: 0
volume_index: self.memory_start as u32,
facing: vertex::Facing::Left
};
quads.push(quad);
}
@ -1305,13 +1335,342 @@ impl EmptyVolume {
pos3: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 + 0.5, z: z as f32 + 0.5 },
pos2: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 + 0.5, z: z as f32 - 0.5 },
raster_pos: cgmath::Vector2 { x: y as u32, y: z as u32 },
volume_index: 0
volume_index: self.memory_start as u32,
facing: vertex::Facing::Right
};
quads.push(quad);
}
}
quads
}
pub fn get_buffer_mem_size(&self) -> u32 {
let mut mem_size: u32 = 0;
mem_size += 3; //max sizes
mem_size += 12; //color/roughness buffer sizes, 2 values each
mem_size += 12; //neighbor buffer sizes, 2 values each
// this covers full color and roughness
mem_size += (self.color_top.len() as u32).max(1);
mem_size += (self.color_bottom.len() as u32).max(1);
mem_size += (self.color_left.len() as u32).max(1);
mem_size += (self.color_right.len() as u32).max(1);
mem_size += (self.color_front.len() as u32).max(1);
mem_size += (self.color_back.len() as u32).max(1);
mem_size += (self.neighbor_top.len() as u32).max(1);
mem_size += (self.neighbor_bottom.len() as u32).max(1);
mem_size += (self.neighbor_left.len() as u32).max(1);
mem_size += (self.neighbor_right.len() as u32).max(1);
mem_size += (self.neighbor_front.len() as u32).max(1);
mem_size += (self.neighbor_back.len() as u32).max(1);
mem_size
}
pub fn insert_into_memory(&self, mut v: Vec<u32>) -> Vec<u32> {
let mut mem_index = self.memory_start;
//max sizes
v[mem_index] = self.size_x as u32;
mem_index += 1;
v[mem_index] = self.size_y as u32;
mem_index += 1;
v[mem_index] = self.size_z as u32;
mem_index += 1;
//color/roughness buffer sizes, 2 values each
if self.color_top.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.color_bottom.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.color_left.len() > 0 {
v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.color_right.len() > 0 {
v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.color_front.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.color_back.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
//neighbor buffer sizes, 2 values each
if self.neighbor_top.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.neighbor_bottom.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.neighbor_left.len() > 0 {
v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.neighbor_right.len() > 0 {
v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.neighbor_front.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
if self.neighbor_back.len() > 0 {
v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32;
} else {
v[mem_index] = 1;
v[mem_index + 1] = 1;
}
mem_index += 2;
//color and roughness
//check which endian should be used in conjun´ction of the graphicscard (might already be handled by vulkan)
if self.color_top.len() > 0 {
for index in 0..self.color_top.len() {
let value = &self.color_top[index];
let roughness = self.roughness_top[index];
let test : u32 = 5;
v[mem_index] = u32::from_ne_bytes([value.x, value.y, value.z, roughness]);
mem_index += 1;
}
}
else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]);
mem_index += 1;
}
if self.color_bottom.len() > 0 {
for index in 0..self.color_bottom.len() {
let value = &self.color_bottom[index];
let roughness = self.roughness_bottom[index];
let test : u32 = 5;
v[mem_index] = u32::from_ne_bytes([value.x, value.y, value.z, roughness]);
mem_index += 1;
}
}
else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]);
mem_index += 1;
}
if self.color_left.len() > 0 {
for index in 0..self.color_left.len() {
let value = &self.color_left[index];
let roughness = self.roughness_left[index];
let test : u32 = 5;
v[mem_index] = u32::from_ne_bytes([value.x, value.y, value.z, roughness]);
mem_index += 1;
}
}
else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]);
mem_index += 1;
}
if self.color_right.len() > 0 {
for index in 0..self.color_right.len() {
let value = &self.color_right[index];
let roughness = self.roughness_right[index];
let test : u32 = 5;
v[mem_index] = u32::from_ne_bytes([value.x, value.y, value.z, roughness]);
mem_index += 1;
}
}
else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]);
mem_index += 1;
}
if self.color_front.len() > 0 {
for index in 0..self.color_front.len() {
let value = &self.color_front[index];
let roughness = self.roughness_front[index];
let test : u32 = 5;
v[mem_index] = u32::from_ne_bytes([value.x, value.y, value.z, roughness]);
mem_index += 1;
}
}
else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]);
mem_index += 1;
}
if self.color_back.len() > 0 {
for index in 0..self.color_back.len() {
let value = &self.color_back[index];
let roughness = self.roughness_back[index];
let test : u32 = 5;
v[mem_index] = u32::from_ne_bytes([value.x, value.y, value.z, roughness]);
mem_index += 1;
}
}
else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]);
mem_index += 1;
}
// neighbors
if self.neighbor_top.len() > 0 {
for value in &self.neighbor_top {
if let Some(reference) = value {
v[mem_index] = reference.borrow().memory_start as u32;
}
else {
v[mem_index] = 0;
}
mem_index += 1;
}
}
else {
v[mem_index] = 0;
mem_index += 1;
}
if self.neighbor_bottom.len() > 0 {
for value in &self.neighbor_bottom {
if let Some(reference) = value {
v[mem_index] = reference.borrow().memory_start as u32;
}
else {
v[mem_index] = 0;
}
mem_index += 1;
}
}
else {
v[mem_index] = 0;
mem_index += 1;
}
if self.neighbor_left.len() > 0 {
for value in &self.neighbor_left {
if let Some(reference) = value {
v[mem_index] = reference.borrow().memory_start as u32;
}
else {
v[mem_index] = 0;
}
mem_index += 1;
}
}
else {
v[mem_index] = 0;
mem_index += 1;
}
if self.neighbor_right.len() > 0 {
for value in &self.neighbor_right {
if let Some(reference) = value {
v[mem_index] = reference.borrow().memory_start as u32;
}
else {
v[mem_index] = 0;
}
mem_index += 1;
}
}
else {
v[mem_index] = 0;
mem_index += 1;
}
if self.neighbor_front.len() > 0 {
for value in &self.neighbor_front {
if let Some(reference) = value {
v[mem_index] = reference.borrow().memory_start as u32;
}
else {
v[mem_index] = 0;
}
mem_index += 1;
}
}
else {
v[mem_index] = 0;
mem_index += 1;
}
if self.neighbor_back.len() > 0 {
for value in &self.neighbor_back {
if let Some(reference) = value {
v[mem_index] = reference.borrow().memory_start as u32;
}
else {
v[mem_index] = 0;
}
mem_index += 1;
}
}
else {
v[mem_index] = 0;
mem_index += 1;
}
println!("last memory index of volume was {}, equivalent to {}kB", mem_index, mem_index * 32 / 8 / 1024);
v
}
}
#[cfg(test)]