blender/intern/cycles/bvh/bvh_sort.cpp

194 lines
5.6 KiB
C++
Raw Normal View History

/*
* Adapted from code copyright 2009-2010 NVIDIA Corporation
* Modifications Copyright 2011, Blender Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bvh_build.h"
#include "bvh_sort.h"
#include "util_algorithm.h"
#include "util_debug.h"
#include "util_task.h"
CCL_NAMESPACE_BEGIN
static const int BVH_SORT_THRESHOLD = 4096;
/* Silly workaround for float extended precision that happens when compiling
* on x86, due to one float staying in 80 bit precision register and the other
* not, which causes the strictly weak ordering to break.
*/
#if !defined(__i386__)
# define NO_EXTENDED_PRECISION
#else
# define NO_EXTENDED_PRECISION volatile
#endif
struct BVHReferenceCompare {
public:
int dim;
BVHReferenceCompare(int dim_)
{
dim = dim_;
}
/* Compare two references.
*
* Returns value is similar to return value of strcmp().
*/
__forceinline int compare(const BVHReference& ra,
const BVHReference& rb) const
{
NO_EXTENDED_PRECISION float ca = ra.bounds().min[dim] + ra.bounds().max[dim];
NO_EXTENDED_PRECISION float cb = rb.bounds().min[dim] + rb.bounds().max[dim];
if(ca < cb) return -1;
else if(ca > cb) return 1;
else if(ra.prim_object() < rb.prim_object()) return -1;
else if(ra.prim_object() > rb.prim_object()) return 1;
else if(ra.prim_index() < rb.prim_index()) return -1;
else if(ra.prim_index() > rb.prim_index()) return 1;
else if(ra.prim_type() < rb.prim_type()) return -1;
else if(ra.prim_type() > rb.prim_type()) return 1;
return 0;
}
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);
class BVHSortTask : public Task {
public:
BVHSortTask(TaskPool *task_pool,
BVHReference *data,
const int job_start,
const int job_end,
const BVHReferenceCompare& compare)
{
run = function_bind(bvh_reference_sort_threaded,
task_pool,
data,
job_start,
job_end,
compare);
}
};
/* 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]);
}
if (compare.compare(data[center], data[right]) > 0) {
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) {
task_pool->push(new BVHSortTask(task_pool,
data,
left, end,
compare), true);
}
else {
start = left;
have_work = true;
}
}
if(start < right) {
end = right;
have_work = true;
}
}
}
Cycles: merging features from tomato branch. === BVH build time optimizations === * BVH building was multithreaded. Not all building is multithreaded, packing and the initial bounding/splitting is still single threaded, but recursive splitting is, which was the main bottleneck. * Object splitting now uses binning rather than sorting of all elements, using code from the Embree raytracer from Intel. http://software.intel.com/en-us/articles/embree-photo-realistic-ray-tracing-kernels/ * Other small changes to avoid allocations, pack memory more tightly, avoid some unnecessary operations, ... These optimizations do not work yet when Spatial Splits are enabled, for that more work is needed. There's also other optimizations still needed, in particular for the case of many low poly objects, the packing step and node memory allocation. BVH raytracing time should remain about the same, but BVH build time should be significantly reduced, test here show speedup of about 5x to 10x on a dual core and 5x to 25x on an 8-core machine, depending on the scene. === Threads === Centralized task scheduler for multithreading, which is basically the CPU device threading code wrapped into something reusable. Basic idea is that there is a single TaskScheduler that keeps a pool of threads, one for each core. Other places in the code can then create a TaskPool that they can drop Tasks in to be executed by the scheduler, and wait for them to complete or cancel them early. === Normal ==== Added a Normal output to the texture coordinate node. This currently gives the object space normal, which is the same under object animation. In the future this might become a "generated" normal so it's also stable for deforming objects, but for now it's already useful for non-deforming objects. === Render Layers === Per render layer Samples control, leaving it to 0 will use the common scene setting. Environment pass will now render environment even if film is set to transparent. Exclude Layers" added. Scene layers (all object that influence the render, directly or indirectly) are shared between all render layers. However sometimes it's useful to leave out some object influence for a particular render layer. That's what this option allows you to do. === Filter Glossy === When using a value higher than 0.0, this will blur glossy reflections after blurry bounces, to reduce noise at the cost of accuracy. 1.0 is a good starting value to tweak. Some light paths have a low probability of being found while contributing much light to the pixel. As a result these light paths will be found in some pixels and not in others, causing fireflies. An example of such a difficult path might be a small light that is causing a small specular highlight on a sharp glossy material, which we are seeing through a rough glossy material. With path tracing it is difficult to find the specular highlight, but if we increase the roughness on the material the highlight gets bigger and softer, and so easier to find. Often this blurring will be hardly noticeable, because we are seeing it through a blurry material anyway, but there are also cases where this will lead to a loss of detail in lighting.
2012-04-28 08:53:59 +00:00
void bvh_reference_sort(int start, int end, BVHReference *data, int dim)
{
const int count = end - start;
BVHReferenceCompare compare(dim);
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;
bvh_reference_sort_threaded(&task_pool, data, start, end - 1, dim);
task_pool.wait_work();
}
}
CCL_NAMESPACE_END