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

@ -58,4 +58,6 @@ pub struct AppData {
pub use_geometry_shader: bool,
pub topology: vk::PrimitiveTopology,
pub scene_rt_memory_size: u64,
}

View file

@ -253,7 +253,7 @@ pub unsafe fn create_storage_buffers(
instance,
device,
data,
size_of::<UniformBufferObject>() as u64,
data.scene_rt_memory_size,
vk::BufferUsageFlags::STORAGE_BUFFER,
vk::MemoryPropertyFlags::HOST_COHERENT | vk::MemoryPropertyFlags::HOST_VISIBLE,
)?;
@ -324,7 +324,7 @@ pub unsafe fn create_descriptor_sets(device: &Device, data: &mut app_data::AppDa
let info = vk::DescriptorBufferInfo::builder()
.buffer(data.storage_buffers[i])
.offset(0)
.range(size_of::<UniformBufferObject>() as u64);
.range(data.scene_rt_memory_size);
let storage_info = &[info];
let storage_write = vk::WriteDescriptorSet::builder()

View file

@ -8,7 +8,7 @@
use anyhow::{anyhow, Result};
use log::*;
use winit::dpi::{LogicalSize, LogicalPosition};
use winit::event::{Event, WindowEvent};
use winit::event::{ElementState, Event, WindowEvent};
use winit::event_loop::EventLoop;
use winit::keyboard::NamedKey;
use winit::window::{Window, WindowBuilder};
@ -132,6 +132,9 @@ fn main() -> Result<()> {
if event.logical_key == "d" {
app.cur_pos += app.view_direction.cross(vertex::Vec3::new(0.0, 0.0, 1.0)) * 0.1;
}
if event.logical_key == "f" && event.state == ElementState::Pressed && event.repeat == false {
app.show_frame_rate = !app.show_frame_rate;
}
if event.logical_key == NamedKey::Escape {
elwt.exit();
unsafe { app.device.device_wait_idle().unwrap(); }
@ -164,6 +167,8 @@ struct App {
view_direction: vertex::Vec3,
cur_pos: cgmath::Point3<f32>,
scene_handler: scene::Scene,
show_frame_rate: bool,
synchronized: usize,
}
impl App {
@ -199,7 +204,7 @@ impl App {
image::create_texture_image_view(&device, &mut data)?;
image::create_texture_sampler(&device, &mut data)?;
scene_handler.prepare_data(&instance, &device, &data)?;
scene_handler.prepare_data(&instance, &device, &mut data)?;
buffer::create_uniform_buffers(&instance, &device, &mut data)?;
buffer::create_storage_buffers(&instance, &device, &mut data)?;
@ -215,7 +220,9 @@ impl App {
last_pos: LogicalPosition::new(-1 as f32, -1 as f32),
view_direction: vertex::Vec3::new(0.0, 0.0, 0.0),
cur_pos: cgmath::point3(0.0, 0.0, 0.0),
scene_handler
scene_handler,
show_frame_rate: false,
synchronized: 0
})
}
@ -247,6 +254,10 @@ impl App {
self.data.images_in_flight[image_index] = in_flight_fence;
self.update_uniform_buffer(image_index)?;
if self.synchronized < MAX_FRAMES_IN_FLIGHT {
self.update_storage_buffer(image_index)?;
self.synchronized += 1
}
let wait_semaphores = &[self.data.image_available_semaphores[self.frame]];
let wait_stages = &[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT];
@ -281,7 +292,9 @@ impl App {
self.frame = (self.frame + 1) % MAX_FRAMES_IN_FLIGHT;
println!("{}", 1000000.0 / start_time.elapsed().as_micros() as f32);
if self.show_frame_rate {
println!("{}", 1000000.0 / start_time.elapsed().as_micros() as f32);
}
Ok(())
}
@ -338,6 +351,7 @@ impl App {
self.data
.images_in_flight
.resize(self.data.swapchain_images.len(), vk::Fence::null());
self.synchronized = 0;
Ok(())
}
@ -431,14 +445,18 @@ impl App {
self.device.unmap_memory(self.data.uniform_buffers_memory[image_index]);
Ok(())
}
unsafe fn update_storage_buffer(&mut self, image_index: usize) -> Result<()> {
let memory = self.device.map_memory(
self.data.storage_buffers_memory[image_index],
0,
size_of::<buffer::UniformBufferObject>() as u64,
self.data.scene_rt_memory_size,
vk::MemoryMapFlags::empty(),
)?;
memcpy(&ubo, memory.cast(), 1);
memcpy(self.scene_handler.rt_memory.as_ptr(), memory.cast(), 2103);
self.device.unmap_memory(self.data.storage_buffers_memory[image_index]);
@ -677,9 +695,9 @@ unsafe fn create_logical_device(
unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Result<()> {
// set up shaders for cubes
// load the byte data
let vert_cube = include_bytes!("../shaders/vert_cube.spv");
let geo_cube = include_bytes!("../shaders/geo_cube.spv");
let frag_cube = include_bytes!("../shaders/frag_cube.spv");
let vert_cube = include_bytes!("../shaders/compiled/vert_cube.spv");
let geo_cube = include_bytes!("../shaders/compiled/geo_cube.spv");
let frag_cube = include_bytes!("../shaders/compiled/frag_cube.spv");
// create the shaders
let vert_shader_module_cube = create_shader_module(device, &vert_cube[..])?;
let geo_shader_module_cube = create_shader_module(device, &geo_cube[..])?;
@ -708,9 +726,9 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
// set up shaders for cuboids
// load the byte data
let vert_cuboid = include_bytes!("../shaders/vert_cuboid.spv");
let geo_cuboid = include_bytes!("../shaders/geo_cuboid.spv");
let frag_cuboid = include_bytes!("../shaders/frag_cuboid.spv");
let vert_cuboid = include_bytes!("../shaders/compiled/vert_cuboid.spv");
let geo_cuboid = include_bytes!("../shaders/compiled/geo_cuboid.spv");
let frag_cuboid = include_bytes!("../shaders/compiled/frag_cuboid.spv");
// create the shaders
let vert_shader_module_cuboid = create_shader_module(device, &vert_cuboid[..])?;
let geo_shader_module_cuboid = create_shader_module(device, &geo_cuboid[..])?;
@ -739,8 +757,8 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
// set up shaders for quads/raytracing
// load the byte data
let vert_quad = include_bytes!("../shaders/vert_rt_quad.spv");
let frag_quad = include_bytes!("../shaders/frag_rt_quad.spv");
let vert_quad = include_bytes!("../shaders/compiled/vert_rt_quad.spv");
let frag_quad = include_bytes!("../shaders/compiled/frag_rt_quad.spv");
// create the shaders
let vert_shader_module_quad = create_shader_module(device, &vert_quad[..])?;
let frag_shader_module_quad = create_shader_module(device, &frag_quad[..])?;

View file

@ -1,6 +1,6 @@
use vulkanalia::prelude::v1_0::*;
use cgmath::vec3;
use crate::vertex;
use crate::vertex::{self, Facing};
use crate::scene::Scene;
use crate::primitives::drawable::Drawable;
@ -12,6 +12,7 @@ pub struct Quad{
pub pos4: vertex::Vec3,
pub raster_pos: cgmath::Vector2<u32>,
pub volume_index: u32,
pub facing: Facing,
}
impl Drawable for Quad {
@ -21,25 +22,29 @@ impl Drawable for Quad {
scene.rt_vertices.push(vertex::RTVertex::new(
vec3(self.pos1.x as f32, self.pos1.y as f32, self.pos1.z as f32),
self.raster_pos,
self.volume_index
self.volume_index,
self.facing
));
// 1 top right far
scene.rt_vertices.push(vertex::RTVertex::new(
vec3(self.pos2.x as f32, self.pos2.y as f32, self.pos2.z as f32),
self.raster_pos,
self.volume_index
self.volume_index,
self.facing
));
// 2 top left near
scene.rt_vertices.push(vertex::RTVertex::new(
vec3(self.pos3.x as f32, self.pos3.y as f32, self.pos3.z as f32),
self.raster_pos,
self.volume_index
self.volume_index,
self.facing
));
// 3 top right near
scene.rt_vertices.push(vertex::RTVertex::new(
vec3(self.pos4.x as f32, self.pos4.y as f32, self.pos4.z as f32),
self.raster_pos,
self.volume_index
self.volume_index,
self.facing
));
// top

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)]

View file

@ -163,20 +163,32 @@ impl Hash for SizedVertex {
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Facing {
Top,
Bottom,
Left,
Right,
Front,
Back
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct RTVertex {
pub pos: Vec3,
pub raster_pos: cgmath::Vector2<u32>,
pub volume_start: u32,
facing: Facing,
}
impl RTVertex {
pub const fn new(pos: Vec3, raster_pos: cgmath::Vector2<u32>, volume_start: u32) -> Self {
Self { pos, raster_pos, volume_start }
pub const fn new(pos: Vec3, raster_pos: cgmath::Vector2<u32>, volume_start: u32, facing: Facing) -> Self {
Self { pos, raster_pos, volume_start, facing }
}
}
impl VertexContainer<3> for RTVertex {
impl VertexContainer<4> for RTVertex {
fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::builder()
.binding(0)
@ -185,7 +197,7 @@ impl VertexContainer<3> for RTVertex {
.build()
}
fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 4] {
let pos = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(0)
@ -206,7 +218,15 @@ impl VertexContainer<3> for RTVertex {
.format(vk::Format::R32_UINT)
.offset((size_of::<Vec3>() + size_of::<cgmath::Vector2<u32>>()) as u32)
.build();
[pos, raster_pos, volume_start]
let facing = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(3)
.format(vk::Format::R32_UINT)
.offset((size_of::<Vec3>() + size_of::<cgmath::Vector2<u32>>() + size_of::<u32>()) as u32)
.build();
[pos, raster_pos, volume_start, facing]
}
}