From 93fa0b2e214fbb70e6c03f5ca7536c7c32544aa7 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 1 Aug 2021 18:45:50 +0200 Subject: Pack bvh nodes, optimize vertices size --- include/aabb.hpp | 11 ++++++++--- include/bvh-node.hpp | 38 ++++++++++++++++++++++++-------------- include/scene.hpp | 2 +- include/triangle.hpp | 7 +------ 4 files changed, 34 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/aabb.hpp b/include/aabb.hpp index 3a4e680..9bc69d1 100644 --- a/include/aabb.hpp +++ b/include/aabb.hpp @@ -2,15 +2,20 @@ #define __AABB_HPP_ #include "vec3.hpp" +#include "triangle.hpp" #include #include class aabb { public: aabb() = default; - aabb(vec3 min_point, vec3 max_point): minimum(min_point), maximum(max_point) {} - aabb(vec3 position, float radius): minimum(position - vec3(radius)), maximum(position + vec3(radius)) {} - aabb(point3 &v1, point3 &v2, point3 &v3) { + aabb(vec3 &min_point, vec3 &max_point): minimum(min_point), maximum(max_point) {} + aabb(vec3 &position, float radius): minimum(position - vec3(radius)), maximum(position + vec3(radius)) {} + aabb(triangle &tri) { + auto &v1 = tri.v1.position; + auto &v2 = tri.v2.position; + auto &v3 = tri.v3.position; + minimum.x = fmin(fmin(v1.x, v2.x), v3.x); minimum.y = fmin(fmin(v1.y, v2.y), v3.y); minimum.z = fmin(fmin(v1.z, v2.z), v3.z); diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp index 72c4bdf..09c9420 100644 --- a/include/bvh-node.hpp +++ b/include/bvh-node.hpp @@ -20,6 +20,13 @@ class bvh_node { uint32_t padding[2]; }; +struct packed_bvh_node { + float min[3]; + int32_t next_id = -1; + float max[3]; + int32_t primitive_id = -1; +}; + struct temp_node : public bvh_node { int32_t left_id; int32_t df_id; @@ -60,14 +67,14 @@ public: // // exporter(); // } - bvh(std::vector& triangles, std::vector& nodes) - :triangles(triangles), nodes(nodes) { - nodes.resize(2 * triangles.size() - 1); + bvh(std::vector& triangles, std::vector& packed_nodes) + :triangles(triangles) { + packed_nodes.resize(2 * triangles.size() - 1); temp_nodes = std::vector(2 * triangles.size() - 1); leafs = std::vector(triangles.size()); for (uint32_t id = 0; id < triangles.size(); id++) { - leafs[id].bounding_box = triangles[id].bounding_box; + leafs[id].bounding_box = aabb(triangles[id]); leafs[id].primitive_id = id; } @@ -80,18 +87,23 @@ public: for (uint32_t id = 0; id < temp_nodes.size(); id++) { auto old_node = temp_nodes[id]; - auto &node = nodes[old_node.df_id]; + auto &node = packed_nodes[old_node.df_id]; node.primitive_id = old_node.primitive_id; - node.bounding_box = old_node.bounding_box; + + node.min[0] = old_node.bounding_box.minimum.x; + node.min[1] = old_node.bounding_box.minimum.y; + node.min[2] = old_node.bounding_box.minimum.z; + + node.max[0] = old_node.bounding_box.maximum.x; + node.max[1] = old_node.bounding_box.maximum.y; + node.max[2] = old_node.bounding_box.maximum.z; if (old_node.next_id != -1) { node.next_id = temp_nodes[old_node.next_id].df_id; } } - // TODO: Pack nodes - // exporter(); } @@ -148,7 +160,6 @@ public: return -1; } - std::vector& nodes; std::vector temp_nodes; std::vector leafs; @@ -164,10 +175,9 @@ private: aabb compute_bounds(uint32_t begin, uint32_t end) { aabb global_box = aabb(); - for (uint32_t box_id = begin; box_id < end; box_id++) { - auto leaf = leafs[box_id]; - // global_box = aabb::surrounding_box(global_box, spheres[leaf.primitive_id].bounding_box); - global_box.union_with(triangles[leaf.primitive_id].bounding_box); + for (uint32_t id = begin; id < end; id++) { + // global_box.union_with(spheres[leaf.primitive_id].bounding_box); + global_box.union_with(leafs[id].bounding_box); } return global_box; @@ -235,7 +245,7 @@ private: } buckets[bucket_index].count++; - buckets[bucket_index].bb.union_with(primitive.bounding_box); + buckets[bucket_index].bb.union_with(leafs[i].bounding_box); } // Compute each potential split axis cost diff --git a/include/scene.hpp b/include/scene.hpp index 2a9ebf2..0002b1d 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -44,7 +44,7 @@ public: // Geometry & BVH // std::vector spheres; std::vector triangles; - std::vector nodes; + std::vector packed_nodes; }; #endif // !__SCENE_HPP_ diff --git a/include/triangle.hpp b/include/triangle.hpp index f1af241..9993efe 100644 --- a/include/triangle.hpp +++ b/include/triangle.hpp @@ -1,9 +1,6 @@ #ifndef __TRIANGLE_HPP_ #define __TRIANGLE_HPP_ -#include "aabb.hpp" -#include "material.hpp" - struct vertex { vec3 position; }; @@ -11,7 +8,7 @@ struct vertex { struct triangle { triangle() = default; triangle(vertex v1, vertex v2, vertex v3) - : v1(v1), v2(v2), v3(v3), bounding_box(v1.position, v2.position, v3.position) + : v1(v1), v2(v2), v3(v3) {} vec3 center() { @@ -21,8 +18,6 @@ struct triangle { vertex v1; vertex v2; vertex v3; - - aabb bounding_box; }; #endif // !__TRIANGLE_HPP_ -- cgit v1.3