2023-06-14 08:52:36 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2009-2010 NVIDIA Corporation
|
|
|
|
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Adapted code from NVIDIA Corporation. */
|
2016-04-18 16:15:05 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "bvh/sort.h"
|
2011-04-27 13:58:34 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "bvh/build.h"
|
2017-04-13 11:20:33 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/algorithm.h"
|
|
|
|
#include "util/task.h"
|
2011-04-27 13:58:34 +02:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2016-04-18 16:15:05 +02:00
|
|
|
static const int BVH_SORT_THRESHOLD = 4096;
|
|
|
|
|
2011-04-27 13:58:34 +02:00
|
|
|
struct BVHReferenceCompare {
|
|
|
|
public:
|
|
|
|
int dim;
|
2016-07-07 12:18:57 +02:00
|
|
|
const BVHUnaligned *unaligned_heuristic;
|
|
|
|
const Transform *aligned_space;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-07 12:18:57 +02:00
|
|
|
BVHReferenceCompare(int dim,
|
|
|
|
const BVHUnaligned *unaligned_heuristic,
|
|
|
|
const Transform *aligned_space)
|
|
|
|
: dim(dim), unaligned_heuristic(unaligned_heuristic), aligned_space(aligned_space)
|
|
|
|
{
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-07 12:18:57 +02:00
|
|
|
__forceinline BoundBox get_prim_bounds(const BVHReference &prim) const
|
2011-04-27 13:58:34 +02:00
|
|
|
{
|
2016-07-07 12:18:57 +02:00
|
|
|
return (aligned_space != NULL) ?
|
|
|
|
unaligned_heuristic->compute_aligned_prim_boundbox(prim, *aligned_space) :
|
|
|
|
prim.bounds();
|
2011-04-27 13:58:34 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-04-18 16:15:05 +02:00
|
|
|
/* Compare two references.
|
|
|
|
*
|
2023-03-07 04:58:15 +01:00
|
|
|
* Returns value is similar to return value of `strcmp()`.
|
2016-04-18 16:15:05 +02:00
|
|
|
*/
|
|
|
|
__forceinline int compare(const BVHReference &ra, const BVHReference &rb) const
|
2011-04-27 13:58:34 +02:00
|
|
|
{
|
2016-07-07 12:18:57 +02:00
|
|
|
BoundBox ra_bounds = get_prim_bounds(ra), rb_bounds = get_prim_bounds(rb);
|
|
|
|
float ca = ra_bounds.min[dim] + ra_bounds.max[dim];
|
|
|
|
float cb = rb_bounds.min[dim] + rb_bounds.max[dim];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-09-17 01:01:48 +02:00
|
|
|
if (ca < cb) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return -1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
|
|
|
else if (ca > cb) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return 1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
|
|
|
else if (ra.prim_object() < rb.prim_object()) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return -1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
|
|
|
else if (ra.prim_object() > rb.prim_object()) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return 1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
|
|
|
else if (ra.prim_index() < rb.prim_index()) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return -1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
|
|
|
else if (ra.prim_index() > rb.prim_index()) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return 1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
|
|
|
else if (ra.prim_type() < rb.prim_type()) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return -1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
|
|
|
else if (ra.prim_type() > rb.prim_type()) {
|
2016-04-18 16:15:05 +02:00
|
|
|
return 1;
|
2023-09-17 01:01:48 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-04-18 16:15:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-04-18 16:15:05 +02:00
|
|
|
bool operator()(const BVHReference &ra, const BVHReference &rb)
|
|
|
|
{
|
|
|
|
return (compare(ra, rb) < 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void bvh_reference_sort_threaded(TaskPool *task_pool,
|
|
|
|
BVHReference *data,
|
|
|
|
const int job_start,
|
|
|
|
const int job_end,
|
|
|
|
const BVHReferenceCompare &compare);
|
2011-04-27 13:58:34 +02:00
|
|
|
|
2016-04-18 16:15:05 +02:00
|
|
|
/* Multi-threaded reference sort. */
|
|
|
|
static void bvh_reference_sort_threaded(TaskPool *task_pool,
|
|
|
|
BVHReference *data,
|
|
|
|
const int job_start,
|
|
|
|
const int job_end,
|
|
|
|
const BVHReferenceCompare &compare)
|
|
|
|
{
|
|
|
|
int start = job_start, end = job_end;
|
|
|
|
bool have_work = (start < end);
|
|
|
|
while (have_work) {
|
|
|
|
const int count = job_end - job_start;
|
|
|
|
if (count < BVH_SORT_THRESHOLD) {
|
|
|
|
/* Number of reference low enough, faster to finish the job
|
|
|
|
* in one thread rather than to spawn more threads.
|
|
|
|
*/
|
|
|
|
sort(data + job_start, data + job_end + 1, compare);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Single QSort step.
|
|
|
|
* Use median-of-three method for the pivot point.
|
|
|
|
*/
|
|
|
|
int left = start, right = end;
|
|
|
|
int center = (left + right) >> 1;
|
|
|
|
if (compare.compare(data[left], data[center]) > 0) {
|
|
|
|
swap(data[left], data[center]);
|
|
|
|
}
|
|
|
|
if (compare.compare(data[left], data[right]) > 0) {
|
|
|
|
swap(data[left], data[right]);
|
|
|
|
}
|
2016-05-17 22:08:34 +02:00
|
|
|
if (compare.compare(data[center], data[right]) > 0) {
|
2016-04-18 16:15:05 +02:00
|
|
|
swap(data[center], data[right]);
|
|
|
|
}
|
|
|
|
swap(data[center], data[right - 1]);
|
|
|
|
BVHReference median = data[right - 1];
|
|
|
|
do {
|
|
|
|
while (compare.compare(data[left], median) < 0) {
|
|
|
|
++left;
|
|
|
|
}
|
|
|
|
while (compare.compare(data[right], median) > 0) {
|
|
|
|
--right;
|
|
|
|
}
|
|
|
|
if (left <= right) {
|
|
|
|
swap(data[left], data[right]);
|
|
|
|
++left;
|
|
|
|
--right;
|
|
|
|
}
|
|
|
|
} while (left <= right);
|
|
|
|
/* We only create one new task here to reduce downside effects of
|
|
|
|
* latency in TaskScheduler.
|
|
|
|
* So generally current thread keeps working on the left part of the
|
|
|
|
* array, and we create new task for the right side.
|
|
|
|
* However, if there's nothing to be done in the left side of the array
|
|
|
|
* we don't create any tasks and make it so current thread works on the
|
|
|
|
* right side.
|
|
|
|
*/
|
|
|
|
have_work = false;
|
|
|
|
if (left < end) {
|
|
|
|
if (start < right) {
|
2020-06-05 14:18:02 +02:00
|
|
|
task_pool->push(
|
2020-06-05 16:39:57 +02:00
|
|
|
function_bind(bvh_reference_sort_threaded, task_pool, data, left, end, compare));
|
2016-04-18 16:15:05 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
start = left;
|
|
|
|
have_work = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start < right) {
|
|
|
|
end = right;
|
|
|
|
have_work = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 12:18:57 +02:00
|
|
|
void bvh_reference_sort(int start,
|
|
|
|
int end,
|
|
|
|
BVHReference *data,
|
|
|
|
int dim,
|
|
|
|
const BVHUnaligned *unaligned_heuristic,
|
|
|
|
const Transform *aligned_space)
|
2011-04-27 13:58:34 +02:00
|
|
|
{
|
2016-04-18 16:15:05 +02:00
|
|
|
const int count = end - start;
|
2016-07-07 12:18:57 +02:00
|
|
|
BVHReferenceCompare compare(dim, unaligned_heuristic, aligned_space);
|
2016-04-18 16:15:05 +02:00
|
|
|
if (count < BVH_SORT_THRESHOLD) {
|
|
|
|
/* It is important to not use any mutex if array is small enough,
|
|
|
|
* otherwise we end up in situation when we're going to sleep far
|
|
|
|
* too often.
|
|
|
|
*/
|
|
|
|
sort(data + start, data + end, compare);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TaskPool task_pool;
|
2016-05-11 16:50:10 +02:00
|
|
|
bvh_reference_sort_threaded(&task_pool, data, start, end - 1, compare);
|
2016-04-18 16:15:05 +02:00
|
|
|
task_pool.wait_work();
|
|
|
|
}
|
2011-04-27 13:58:34 +02:00
|
|
|
}
|
|
|
|
|
2016-02-22 16:25:33 +01:00
|
|
|
CCL_NAMESPACE_END
|