From f1e9a75599f1c06ed946014ffde432fc21c18ec2 Mon Sep 17 00:00:00 2001 From: RedShip Date: Sat, 18 Jan 2025 19:21:29 +0100 Subject: [PATCH] ~ | BVH Opti working --- scenes/dragon.rt | 2 +- shaders/debug.glsl | 91 ++++++++++++++++++++----------------------- srcs/RT.cpp | 14 +++---- srcs/class/BVH.cpp | 2 +- srcs/class/Shader.cpp | 4 +- srcs/class/Window.cpp | 4 +- 6 files changed, 55 insertions(+), 62 deletions(-) diff --git a/scenes/dragon.rt b/scenes/dragon.rt index 245a171..7299d46 100644 --- a/scenes/dragon.rt +++ b/scenes/dragon.rt @@ -18,7 +18,7 @@ pl 0 0 -2 0 0 1 1 // back wall pl 0 0 2 0 0 -1 5 // front wall pl 2 0 0 -1 0 0 3 // right wall pl -2 0 0 1 0 0 2 // left wall -pl 0 2 0 0 -1 0 0 // ceiling +# pl 0 2 0 0 -1 0 0 // ceiling pl 0 -2 0 0 1 0 2 // floor qu -1 1.999 -1 2 0 0 0 0 2 6 diff --git a/shaders/debug.glsl b/shaders/debug.glsl index 41665c5..7072d66 100644 --- a/shaders/debug.glsl +++ b/shaders/debug.glsl @@ -150,56 +150,49 @@ hitInfo traceBVH(Ray ray, inout Stats stats) GPUBvh node = bvh[current_index]; - stats.box_count++; - if (intersectRayBVH(ray, node, hit_bvh) && hit_bvh.t < hit.t) - { - if (node.is_leaf != 0) - { - for (int i = 0; i < node.primitive_count; i++) - { - GPUTriangle obj = triangles[node.first_primitive + i]; - - hitInfo temp_hit; - if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001) - { - hit.t = temp_hit.t; - hit.normal = temp_hit.normal; - hit.obj_index = node.first_primitive + i; - } - stats.triangle_count++; - } - } + if (node.is_leaf != 0) + { + for (int i = 0; i < node.primitive_count; i++) + { + GPUTriangle obj = triangles[node.first_primitive + i]; + + hitInfo temp_hit; + if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001) + { + hit.t = temp_hit.t; + hit.normal = temp_hit.normal; + hit.obj_index = node.first_primitive + i; + } + stats.triangle_count++; + } + } + else + { + GPUBvh left_node = bvh[node.left_index]; + GPUBvh right_node = bvh[node.right_index]; + + hitInfo left_hit; + hitInfo right_hit; + + left_hit.t = 1e30; + right_hit.t = 1e30; + + stats.box_count += 2; + + bool left_bool = intersectRayBVH(ray, left_node, left_hit); + bool right_bool = intersectRayBVH(ray, right_node, right_hit); + + if (left_hit.t > right_hit.t) + { + if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = node.left_index; + if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.right_index; + } else - { - // GPUBvh left_node = bvh[node.left_index]; - // GPUBvh right_node = bvh[node.right_index]; - - // hitInfo left_hit; - // hitInfo right_hit; - - // left_hit.t = 1e30; - // right_hit.t = 1e30; - - // stats.box_count += 2; - - // intersectRayBVH(ray, left_node, left_hit); - // intersectRayBVH(ray, right_node, right_hit); - - // if (left_hit.t > right_hit.t) - // { - // if (left_hit.t < hit.t) stack[++stack_ptr] = node.left_index; - // if (right_hit.t < hit.t) stack[++stack_ptr] = node.right_index; - // } - // else - // { - // if (right_hit.t < hit.t) stack[++stack_ptr] = node.right_index; - // if (left_hit.t < hit.t) stack[++stack_ptr] = node.left_index; - // } - - stack[++stack_ptr] = node.left_index; - stack[++stack_ptr] = node.right_index; - } - } + { + if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.right_index; + if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = node.left_index; + } + } } return (hit); diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 386fe47..e6eee1c 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */ -/* Updated: 2025/01/17 19:25:14 by ycontre ### ########.fr */ +/* Updated: 2025/01/18 19:21:00 by ycontre ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ int main(int argc, char **argv) return (1); Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0); - Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl"); - // Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl"); + // Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl"); + Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl"); GLint max_gpu_size; glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size); @@ -128,8 +128,8 @@ int main(int argc, char **argv) recorded_fps.push_back((int)window.getFps()); - float y_offset = 30; - float dist_to_obj = 60; + float y_offset = 0.; + float dist_to_obj = 2; float speed = 0.5; camera->setPosition(glm::vec3( @@ -189,8 +189,8 @@ int main(int argc, char **argv) // performance profiling std::ofstream file("fps.txt"); - for (int i = 0; i < recorded_fps.size(); i++) - file << i << " " << recorded_fps[i] << std::endl; + for (int i = 0; i < (int) recorded_fps.size(); i++) + file << recorded_fps[i] << std::endl; file.close(); // diff --git a/srcs/class/BVH.cpp b/srcs/class/BVH.cpp index c0cbf90..a01aa9d 100644 --- a/srcs/class/BVH.cpp +++ b/srcs/class/BVH.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 21:48:48 by TheRed #+# #+# */ -/* Updated: 2025/01/17 19:25:08 by ycontre ### ########.fr */ +/* Updated: 2025/01/18 19:16:45 by ycontre ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/class/Shader.cpp b/srcs/class/Shader.cpp index abc338d..b5933f6 100644 --- a/srcs/class/Shader.cpp +++ b/srcs/class/Shader.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */ -/* Updated: 2025/01/17 19:05:41 by ycontre ### ########.fr */ +/* Updated: 2025/01/18 19:08:58 by ycontre ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,7 @@ Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string com const char *fragmentCode = loadFileWithIncludes(fragmentPath); const char *computeCode = loadFileWithIncludes(computePath); - printWithLineNumbers(computeCode); + // printWithLineNumbers(computeCode); _vertex = glCreateShader(GL_VERTEX_SHADER); diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 409269b..247a70f 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ -/* Updated: 2025/01/15 19:34:25 by ycontre ### ########.fr */ +/* Updated: 2025/01/18 18:40:41 by ycontre ### ########.fr */ /* */ /* ************************************************************************** */ @@ -175,7 +175,7 @@ void Window::imGuiRender() ImGui::Text("Fps: %d", int(_fps)); ImGui::Text("Frame: %d", _frameCount); - ImGui::Text("Objects: %d", _scene->getObjectData().size() + _scene->getTriangleData().size()); + ImGui::Text("Objects: %lu", _scene->getObjectData().size() + _scene->getTriangleData().size()); ImGui::Separator(); if (ImGui::Checkbox("Accumulate", &accumulate))