diffuse raytracing

This commit is contained in:
zomseffen 2025-01-17 11:10:39 +01:00
parent 729fcb8559
commit 86135cb2ce
6 changed files with 57 additions and 24 deletions

Binary file not shown.

View file

@ -134,24 +134,19 @@ vec3 get_light_color(uint light_index) {
return vec3(float(scene_info.infos[light_index + 3]) / 255.0, float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0);
}
void main() {
uint max_length = scene_info.infos[0];
uint last = scene_info.infos[max_length];
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing);
vec4 orig_color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1);
vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sample) {
uint max_light_num = scene_info.infos[0];
uint light_num = 0;
uint volume_index = fragVolumeStart;
// setup volume info
uint volume_index = volume_start;
uint volume_pos_x = scene_info.infos[volume_index + 0];
uint volume_pos_y = scene_info.infos[volume_index + 1];
uint volume_pos_z = scene_info.infos[volume_index + 2];
// setup light info
uint light_index = scene_info.infos[fragVolumeStart + 6 + light_num];
vec3 light_direction = get_light_position(light_index) - origPosition;
uint light_index = scene_info.infos[volume_start + 6 + light_num];
vec3 light_direction = get_light_position(light_index) - starting_pos;
vec3 light_color = get_light_color(light_index);
bool x_pos = light_direction.x > 0.0;
@ -166,7 +161,7 @@ void main() {
// initialize color
vec3 color_sum = vec3(0.0, 0.0, 0.0) + (orig_color_sample.xyz * 0.01);
uint max_iterations = max_light_num * 20;
uint max_iterations = max_light_num * scene_info.infos[1];
for (int i = 0; i < max_iterations; i++) {
float x_border = float(volume_pos_x + (scene_info.infos[volume_index + 3]) * uint(x_pos)) - 0.5;
float y_border = float(volume_pos_y + (scene_info.infos[volume_index + 4]) * uint(y_pos)) - 0.5;
@ -179,18 +174,18 @@ void main() {
float y_factor = 2.0;
float z_factor = 2.0;
if (!x_null) {
x_factor = (x_border - origPosition.x) / light_direction.x;
x_factor = (x_border - starting_pos.x) / light_direction.x;
}
if (!y_null) {
y_factor = (y_border - origPosition.y) / light_direction.y;
y_factor = (y_border - starting_pos.y) / light_direction.y;
}
if (!z_null) {
z_factor = (z_border - origPosition.z) / light_direction.z;
z_factor = (z_border - starting_pos.z) / light_direction.z;
}
if ((x_factor >= 1.0) && (y_factor >= 1.0) && (z_factor >= 1.0)) {
// no hit, add light color result
color_sum += (orig_color_sample.xyz * light_color) / ((0.1 * light_direction.length() * light_direction.length()) + 1.0);
color_sum += (orig_color_sample.xyz * light_color) / ((0.01 * length(light_direction) * length(light_direction)) + 1.0);
needs_next_light = true;
} else {
// if there is a border hit before reaching the light
@ -205,7 +200,7 @@ void main() {
} else {
hit_facing = 2;
}
vec3 intersection_pos = origPosition + x_factor * light_direction;
vec3 intersection_pos = starting_pos + x_factor * light_direction;
u = uint(round(intersection_pos.y)) - volume_pos_y;
v = uint(round(intersection_pos.z)) - volume_pos_z;
}
@ -216,7 +211,7 @@ void main() {
} else {
hit_facing = 4;
}
vec3 intersection_pos = origPosition + y_factor * light_direction;
vec3 intersection_pos = starting_pos + y_factor * light_direction;
u = uint(round(intersection_pos.x)) - volume_pos_x;
v = uint(round(intersection_pos.z)) - volume_pos_z;
}
@ -227,7 +222,7 @@ void main() {
} else {
hit_facing = 1;
}
vec3 intersection_pos = origPosition + z_factor * light_direction;
vec3 intersection_pos = starting_pos + z_factor * light_direction;
u = uint(round(intersection_pos.x)) - volume_pos_x;
v = uint(round(intersection_pos.y)) - volume_pos_y;
}
@ -256,12 +251,12 @@ void main() {
break;
}
// set up the new light
light_index = scene_info.infos[fragVolumeStart + 6 + light_num];
light_index = scene_info.infos[volume_start + 6 + light_num];
if (light_index == 0) {
// abort if there is no new light
break;
}
light_direction = get_light_position(light_index) - origPosition;
light_direction = get_light_position(light_index) - starting_pos;
light_color = get_light_color(light_index);
x_pos = light_direction.x > 0.0;
@ -273,11 +268,43 @@ void main() {
z_pos = light_direction.z > 0.0;
z_null = (light_direction.z == 0.0);
// reset volume info
volume_index = fragVolumeStart;
volume_index = volume_start;
volume_pos_x = scene_info.infos[volume_index + 0];
volume_pos_y = scene_info.infos[volume_index + 1];
volume_pos_z = scene_info.infos[volume_index + 2];
}
}
return color_sum;
}
void main() {
uint max_length = scene_info.infos[0];
uint last = scene_info.infos[max_length];
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing);
vec4 orig_color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1);
// singular raytracing
//vec3 color_sum = get_lighting_color(fragVolumeStart, origPosition, orig_color_sample);
// diffuse raytracing using a quadratic raster of rays
int raster_half_steps = 0;
float raster_distance = 0.01;
int raster_points = (2 * raster_half_steps + 1) * (2 * raster_half_steps + 1);
vec3 color_sum = vec3(0.0, 0.0, 0.0);
for (int u_offset = -raster_half_steps; u_offset <= raster_half_steps; u_offset++) {
for (int v_offset = -raster_half_steps; v_offset <= raster_half_steps; v_offset++) {
float x_offset = raster_distance * float(u_offset) * float(facing == 0 || facing == 1 || facing == 4 || facing == 5);
float y_offset = raster_distance * float(u_offset) * float(facing == 2 || facing == 3);
y_offset += raster_distance * float(v_offset) * float(facing == 0 || facing == 1);
float z_offset = raster_distance * float(v_offset) * float(facing == 4 || facing == 5 || facing == 2 || facing == 3);
vec3 offset = vec3(x_offset, y_offset, z_offset);
color_sum += get_lighting_color(fragVolumeStart, origPosition + offset, orig_color_sample) / float(raster_points);
}
}
outColor = vec4(color_sum, 1.0);
}

View file

@ -61,4 +61,5 @@ pub struct AppData {
pub scene_rt_memory_size: u64,
pub num_lights_per_volume: u32,
pub max_iterations_per_light: u32,
}

View file

@ -58,7 +58,7 @@ const DEVICE_EXTENSIONS: &[vk::ExtensionName] = &[
vk::KHR_SWAPCHAIN_EXTENSION.name
];
const MAX_FRAMES_IN_FLIGHT: usize = 2;
const MAX_FRAMES_IN_FLIGHT: usize = 3;
fn main() -> Result<()> {
pretty_env_logger::init();
@ -179,6 +179,7 @@ impl App {
let mut data = app_data::AppData::default();
data.use_geometry_shader = false;
data.num_lights_per_volume = 2;
data.max_iterations_per_light = 20;
let mut scene_handler = scene::Scene::default();
//load_model::load_model(&mut data)?;

View file

@ -138,7 +138,9 @@ impl Scene {
let index = self.sized_vertices.len();
cube.draw(&data.topology, index, self);
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)
let mut memory_index = 2;
// 0 - location for the maximum number of lights referenced per chunk (also will be the invalid memory allocation for pointing to a nonexistant neighbor)
// 1 - location for the max iterations per light
for light in &mut self.point_lights {
light.memory_start = memory_index;
memory_index += light.get_buffer_mem_size() as usize;
@ -157,6 +159,8 @@ impl Scene {
}
println!("Memory size is {} kB, max indes is {}", memory_index * 32 / 8 /1024 + 1, memory_index);
let mut volume_vec = vec![data.num_lights_per_volume; memory_index];
volume_vec[1] = data.max_iterations_per_light;
for volume in &empty_volumes {
volume_vec = volume.borrow().insert_into_memory(volume_vec, data.num_lights_per_volume, &self.point_lights);
}

View file

@ -9,7 +9,7 @@ extern crate rand;
pub const CHUNK_SIZE_EXPONENT: u32 = 4;
pub const CHUNK_SIZE: usize = (2 as usize).pow(CHUNK_SIZE_EXPONENT);
pub const MAX_TREE_DEPTH: usize = 2;
pub const MAX_TREE_DEPTH: usize = CHUNK_SIZE_EXPONENT as usize - 2;
pub const MIN_CHUNK_SIZE: usize = CHUNK_SIZE / (2 as usize).pow(MAX_TREE_DEPTH as u32);
#[derive(Clone, Debug)]