Cycles: Add option to split curve motion primitives by time steps
[blender.git] / intern / cycles / bvh / bvh_sort.cpp
1 /*
2  * Adapted from code copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011, Blender Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "bvh_build.h"
19 #include "bvh_sort.h"
20
21 #include "util_algorithm.h"
22 #include "util_debug.h"
23 #include "util_task.h"
24
25 CCL_NAMESPACE_BEGIN
26
27 static const int BVH_SORT_THRESHOLD = 4096;
28
29 struct BVHReferenceCompare {
30 public:
31         int dim;
32         const BVHUnaligned *unaligned_heuristic;
33         const Transform *aligned_space;
34
35         BVHReferenceCompare(int dim,
36                             const BVHUnaligned *unaligned_heuristic,
37                             const Transform *aligned_space)
38                 : dim(dim),
39                   unaligned_heuristic(unaligned_heuristic),
40                   aligned_space(aligned_space)
41         {
42         }
43
44         __forceinline BoundBox get_prim_bounds(const BVHReference& prim) const
45         {
46                 return (aligned_space != NULL)
47                         ? unaligned_heuristic->compute_aligned_prim_boundbox(
48                                   prim, *aligned_space)
49                         : prim.bounds();
50         }
51
52         /* Compare two references.
53          *
54          * Returns value is similar to return value of strcmp().
55          */
56         __forceinline int compare(const BVHReference& ra,
57                                   const BVHReference& rb) const
58         {
59                 BoundBox ra_bounds = get_prim_bounds(ra),
60                          rb_bounds = get_prim_bounds(rb);
61                 float ca = ra_bounds.min[dim] + ra_bounds.max[dim];
62                 float cb = rb_bounds.min[dim] + rb_bounds.max[dim];
63
64                 if(ca < cb) return -1;
65                 else if(ca > cb) return 1;
66                 else if(ra.prim_object() < rb.prim_object()) return -1;
67                 else if(ra.prim_object() > rb.prim_object()) return 1;
68                 else if(ra.prim_index() < rb.prim_index()) return -1;
69                 else if(ra.prim_index() > rb.prim_index()) return 1;
70                 else if(ra.prim_type() < rb.prim_type()) return -1;
71                 else if(ra.prim_type() > rb.prim_type()) return 1;
72
73                 return 0;
74         }
75
76         bool operator()(const BVHReference& ra, const BVHReference& rb)
77         {
78                 return (compare(ra, rb) < 0);
79         }
80 };
81
82 static void bvh_reference_sort_threaded(TaskPool *task_pool,
83                                         BVHReference *data,
84                                         const int job_start,
85                                         const int job_end,
86                                         const BVHReferenceCompare& compare);
87
88 class BVHSortTask : public Task {
89 public:
90         BVHSortTask(TaskPool *task_pool,
91                     BVHReference *data,
92                     const int job_start,
93                     const int job_end,
94                     const BVHReferenceCompare& compare)
95         {
96                 run = function_bind(bvh_reference_sort_threaded,
97                                     task_pool,
98                                     data,
99                                     job_start,
100                                     job_end,
101                                     compare);
102         }
103 };
104
105 /* Multi-threaded reference sort. */
106 static void bvh_reference_sort_threaded(TaskPool *task_pool,
107                                         BVHReference *data,
108                                         const int job_start,
109                                         const int job_end,
110                                         const BVHReferenceCompare& compare)
111 {
112         int start = job_start, end = job_end;
113         bool have_work = (start < end);
114         while(have_work) {
115                 const int count = job_end - job_start;
116                 if(count < BVH_SORT_THRESHOLD) {
117                         /* Number of reference low enough, faster to finish the job
118                          * in one thread rather than to spawn more threads.
119                          */
120                         sort(data+job_start, data+job_end+1, compare);
121                         break;
122                 }
123                 /* Single QSort step.
124                  * Use median-of-three method for the pivot point.
125                  */
126                 int left = start, right = end;
127                 int center = (left + right) >> 1;
128                 if(compare.compare(data[left], data[center]) > 0) {
129                         swap(data[left], data[center]);
130                 }
131                 if(compare.compare(data[left], data[right]) > 0) {
132                         swap(data[left], data[right]);
133                 }
134                 if(compare.compare(data[center], data[right]) > 0) {
135                         swap(data[center], data[right]);
136                 }
137                 swap(data[center], data[right - 1]);
138                 BVHReference median = data[right - 1];
139                 do {
140                         while(compare.compare(data[left], median) < 0) {
141                                 ++left;
142                         }
143                         while(compare.compare(data[right], median) > 0) {
144                                 --right;
145                         }
146                         if(left <= right) {
147                                 swap(data[left], data[right]);
148                                 ++left;
149                                 --right;
150                         }
151                 } while(left <= right);
152                 /* We only create one new task here to reduce downside effects of
153                  * latency in TaskScheduler.
154                  * So generally current thread keeps working on the left part of the
155                  * array, and we create new task for the right side.
156                  * However, if there's nothing to be done in the left side of the array
157                  * we don't create any tasks and make it so current thread works on the
158                  * right side.
159                  */
160                 have_work = false;
161                 if(left < end) {
162                         if(start < right) {
163                                 task_pool->push(new BVHSortTask(task_pool,
164                                                                 data,
165                                                                 left, end,
166                                                                 compare), true);
167                         }
168                         else {
169                                 start = left;
170                                 have_work = true;
171                         }
172                 }
173                 if(start < right) {
174                         end = right;
175                         have_work = true;
176                 }
177         }
178 }
179
180 void bvh_reference_sort(int start,
181                         int end,
182                         BVHReference *data,
183                         int dim,
184                         const BVHUnaligned *unaligned_heuristic,
185                         const Transform *aligned_space)
186 {
187         const int count = end - start;
188         BVHReferenceCompare compare(dim, unaligned_heuristic, aligned_space);
189         if(count < BVH_SORT_THRESHOLD) {
190                 /* It is important to not use any mutex if array is small enough,
191                  * otherwise we end up in situation when we're going to sleep far
192                  * too often.
193                  */
194                 sort(data+start, data+end, compare);
195         }
196         else {
197                 TaskPool task_pool;
198                 bvh_reference_sort_threaded(&task_pool, data, start, end - 1, compare);
199                 task_pool.wait_work();
200         }
201 }
202
203 CCL_NAMESPACE_END
204