From 83f0c41d81a6566e9b803798bb6530a354f85c2c Mon Sep 17 00:00:00 2001 From: relief7 Date: Mon, 26 Feb 2024 17:45:01 +0100 Subject: [PATCH 1/3] -removed const qualifier for "objects" vector in bvh constructor arguments -so now we are essentially changing the objects vector in place via sorting -this should be ok though because we don't need the scene objects array any longer after the bvh has been built -removed 2nd unnecessary branch within condition "object_span == 2" -did not change bbox calculation in the beginning of constructor as using "for const & auto : objects ..." made everything a lot slower --- books/RayTracingTheNextWeek.html | 29 +++++++++++------------------ src/TheNextWeek/bvh.h | 17 +++++------------ src/TheRestOfYourLife/bvh.h | 19 ++++++------------- 3 files changed, 22 insertions(+), 43 deletions(-) diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html index 84842ee7..7daef7ec 100644 --- a/books/RayTracingTheNextWeek.html +++ b/books/RayTracingTheNextWeek.html @@ -899,9 +899,9 @@ class bvh_node : public hittable { public: - bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} + bvh_node(hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} - bvh_node(const std::vector>& src_objects, size_t start, size_t end) { + bvh_node(std::vector>& objects, size_t start, size_t end) { // To be implemented later. } @@ -956,27 +956,20 @@ class bvh_node : public hittable { public: ... - bvh_node(const std::vector>& src_objects, size_t start, size_t end) { + bvh_node(std::vector>& objects, size_t start, size_t end) { int axis = random_int(0,2); auto comparator = (axis == 0) ? box_x_compare : (axis == 1) ? box_y_compare : box_z_compare; - auto objects = src_objects; // A modifiable array of the source scene objects - size_t object_span = end - start; if (object_span == 1) { left = right = objects[start]; } else if (object_span == 2) { - if (comparator(objects[start], objects[start+1])) { - left = objects[start]; - right = objects[start+1]; - } else { - left = objects[start+1]; - right = objects[start]; - } + left = objects[start]; + right = objects[start+1]; } else { std::sort(objects.begin() + start, objects.begin() + end, comparator); @@ -1094,12 +1087,12 @@ class bvh_node : public hittable { public: ... - bvh_node(const std::vector>& src_objects, size_t start, size_t end) { + bvh_node(std::vector>& objects, size_t start, size_t end) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight // Build the bounding box of the span of source objects. bbox = aabb::empty; - for (int object_index=start; object_index < end; object_index++) - bbox = aabb(bbox, src_objects[object_index]->bounding_box()); + for (size_t object_index=start; object_index < end; object_index++) + bbox = aabb(bbox, objects[object_index]->bounding_box()); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ ... @@ -1115,11 +1108,11 @@ class bvh_node : public hittable { public: ... - bvh_node(const std::vector>& src_objects, size_t start, size_t end) { + bvh_node(std::vector>& objects, size_t start, size_t end) { // Build the bounding box of the span of source objects. bbox = aabb::empty; - for (int object_index=start; object_index < end; object_index++) - bbox = aabb(bbox, src_objects[object_index]->bounding_box()); + for (size_t object_index=start; object_index < end; object_index++) + bbox = aabb(bbox, objects[object_index]->bounding_box()); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight diff --git a/src/TheNextWeek/bvh.h b/src/TheNextWeek/bvh.h index e87d8d07..c7813c23 100644 --- a/src/TheNextWeek/bvh.h +++ b/src/TheNextWeek/bvh.h @@ -22,13 +22,13 @@ class bvh_node : public hittable { public: - bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} + bvh_node(hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} - bvh_node(const std::vector>& src_objects, size_t start, size_t end) { + bvh_node(std::vector>& objects, size_t start, size_t end) { // Build the bounding box of the span of source objects. bbox = aabb::empty; for (size_t object_index=start; object_index < end; object_index++) - bbox = aabb(bbox, src_objects[object_index]->bounding_box()); + bbox = aabb(bbox, objects[object_index]->bounding_box()); int axis = bbox.longest_axis(); @@ -36,20 +36,13 @@ class bvh_node : public hittable { : (axis == 1) ? box_y_compare : box_z_compare; - auto objects = src_objects; // A modifiable array of the source scene objects - size_t object_span = end - start; if (object_span == 1) { left = right = objects[start]; } else if (object_span == 2) { - if (comparator(objects[start], objects[start+1])) { - left = objects[start]; - right = objects[start+1]; - } else { - left = objects[start+1]; - right = objects[start]; - } + left = objects[start]; + right = objects[start+1]; } else { std::sort(objects.begin() + start, objects.begin() + end, comparator); diff --git a/src/TheRestOfYourLife/bvh.h b/src/TheRestOfYourLife/bvh.h index 5f9a4990..c7813c23 100644 --- a/src/TheRestOfYourLife/bvh.h +++ b/src/TheRestOfYourLife/bvh.h @@ -22,13 +22,13 @@ class bvh_node : public hittable { public: - bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} + bvh_node(hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} - bvh_node(const std::vector>& src_objects, size_t start, size_t end) { + bvh_node(std::vector>& objects, size_t start, size_t end) { // Build the bounding box of the span of source objects. bbox = aabb::empty; - for (int object_index=start; object_index < end; object_index++) - bbox = aabb(bbox, src_objects[object_index]->bounding_box()); + for (size_t object_index=start; object_index < end; object_index++) + bbox = aabb(bbox, objects[object_index]->bounding_box()); int axis = bbox.longest_axis(); @@ -36,20 +36,13 @@ class bvh_node : public hittable { : (axis == 1) ? box_y_compare : box_z_compare; - auto objects = src_objects; // A modifiable array of the source scene objects - size_t object_span = end - start; if (object_span == 1) { left = right = objects[start]; } else if (object_span == 2) { - if (comparator(objects[start], objects[start+1])) { - left = objects[start]; - right = objects[start+1]; - } else { - left = objects[start+1]; - right = objects[start]; - } + left = objects[start]; + right = objects[start+1]; } else { std::sort(objects.begin() + start, objects.begin() + end, comparator); From 41c1795abd69c668a3b60044dece523e78d73450 Mon Sep 17 00:00:00 2001 From: relief7 Date: Mon, 26 Feb 2024 18:12:29 +0100 Subject: [PATCH 2/3] -added credits --- CHANGELOG.md | 2 ++ books/acknowledgments.md.html | 1 + 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c955cab1..b9c33c53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ then. - Change - Reworked the AABB chapter (#1236) - New - add section on alternative 2D primitives such as triangle, ellipse and annulus (#1204, #1205) + - Change - changed bvh construction (removed const qualifer for objects vector) so sorting is done + in place and copying of vector is avoided, better bvh build performance (#1388, #1391) ### The Rest of Your Life diff --git a/books/acknowledgments.md.html b/books/acknowledgments.md.html index 0cfb777a..902cf4f6 100644 --- a/books/acknowledgments.md.html +++ b/books/acknowledgments.md.html @@ -53,6 +53,7 @@ - [Manas Kale](https://github.com/manas96) - Marcus Ottosson - [Mark Craig](https://github.com/mrmcsoftware) + - Markus Boos - Matthew Heimlich - Nakata Daisuke - [Nate Rupsis](https://github.com/rupsis) From 78d24c1fd87b259a9a1606ebb982bda477c7594c Mon Sep 17 00:00:00 2001 From: relief7 Date: Fri, 1 Mar 2024 16:46:21 +0100 Subject: [PATCH 3/3] -removed passing by ref in bvh constructor so we make a copy of the input hittable_list -updated listings in book 2 -fixed indentation error in RayTracingTheNextWeek.html --- books/RayTracingTheNextWeek.html | 6 +++--- src/TheNextWeek/bvh.h | 2 +- src/TheRestOfYourLife/bvh.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html index 7daef7ec..c7954459 100644 --- a/books/RayTracingTheNextWeek.html +++ b/books/RayTracingTheNextWeek.html @@ -899,7 +899,7 @@ class bvh_node : public hittable { public: - bvh_node(hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} + bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {} bvh_node(std::vector>& objects, size_t start, size_t end) { // To be implemented later. @@ -968,8 +968,8 @@ if (object_span == 1) { left = right = objects[start]; } else if (object_span == 2) { - left = objects[start]; - right = objects[start+1]; + left = objects[start]; + right = objects[start+1]; } else { std::sort(objects.begin() + start, objects.begin() + end, comparator); diff --git a/src/TheNextWeek/bvh.h b/src/TheNextWeek/bvh.h index c7813c23..c546e86b 100644 --- a/src/TheNextWeek/bvh.h +++ b/src/TheNextWeek/bvh.h @@ -22,7 +22,7 @@ class bvh_node : public hittable { public: - bvh_node(hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} + bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {} bvh_node(std::vector>& objects, size_t start, size_t end) { // Build the bounding box of the span of source objects. diff --git a/src/TheRestOfYourLife/bvh.h b/src/TheRestOfYourLife/bvh.h index c7813c23..c546e86b 100644 --- a/src/TheRestOfYourLife/bvh.h +++ b/src/TheRestOfYourLife/bvh.h @@ -22,7 +22,7 @@ class bvh_node : public hittable { public: - bvh_node(hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {} + bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {} bvh_node(std::vector>& objects, size_t start, size_t end) { // Build the bounding box of the span of source objects.