This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/render/intern/raytrace/rayobject_rtbuild.cpp

511 lines
13 KiB
C++
Raw Normal View History

/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
* Contributor(s): André Pinto.
*
* ***** END GPL LICENSE BLOCK *****
*/
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
2011-02-27 19:31:27 +00:00
/** \file blender/render/intern/raytrace/rayobject_rtbuild.cpp
* \ingroup render
*/
#include <assert.h>
#include <math.h>
#include <stdlib.h>
2009-08-02 12:11:14 +00:00
#include <algorithm>
#include "rayobject_rtbuild.h"
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
static bool selected_node(RTBuilder::Object *node)
{
return node->selected;
}
static void rtbuild_init(RTBuilder *b)
{
b->split_axis = -1;
b->primitives.begin = 0;
b->primitives.end = 0;
b->primitives.maxsize = 0;
2012-05-15 18:45:20 +00:00
for (int i = 0; i < RTBUILD_MAX_CHILDS; i++)
b->child_offset[i] = 0;
2012-05-15 18:45:20 +00:00
for (int i = 0; i < 3; i++)
b->sorted_begin[i] = b->sorted_end[i] = 0;
2012-05-15 18:45:20 +00:00
INIT_MINMAX(b->bb, b->bb + 3);
}
2012-05-15 18:45:20 +00:00
RTBuilder *rtbuild_create(int size)
{
2012-05-15 18:45:20 +00:00
RTBuilder *builder = (RTBuilder *) MEM_mallocN(sizeof(RTBuilder), "RTBuilder");
RTBuilder::Object *memblock = (RTBuilder::Object *)MEM_mallocN(sizeof(RTBuilder::Object) * size, "RTBuilder.objects");
rtbuild_init(builder);
builder->primitives.begin = builder->primitives.end = memblock;
builder->primitives.maxsize = size;
2012-05-15 18:45:20 +00:00
for (int i = 0; i < 3; i++) {
builder->sorted_begin[i] = (RTBuilder::Object **)MEM_mallocN(sizeof(RTBuilder::Object *) * size, "RTBuilder.sorted_objects");
builder->sorted_end[i] = builder->sorted_begin[i];
}
return builder;
}
void rtbuild_free(RTBuilder *b)
{
if (b->primitives.begin) MEM_freeN(b->primitives.begin);
2012-05-15 18:45:20 +00:00
for (int i = 0; i < 3; i++)
if (b->sorted_begin[i])
MEM_freeN(b->sorted_begin[i]);
MEM_freeN(b);
}
void rtbuild_add(RTBuilder *b, RayObject *o)
{
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
float bb[6];
2012-05-15 18:45:20 +00:00
assert(b->primitives.begin + b->primitives.maxsize != b->primitives.end);
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
2012-05-15 18:45:20 +00:00
INIT_MINMAX(bb, bb + 3);
RE_rayobject_merge_bb(o, bb, bb + 3);
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
/* skip objects with invalid bounding boxes, nan causes DO_MINMAX
2012-03-09 18:28:30 +00:00
* to do nothing, so we get these invalid values. this shouldn't
* happen usually, but bugs earlier in the pipeline can cause it. */
if (bb[0] > bb[3] || bb[1] > bb[4] || bb[2] > bb[5])
return;
/* skip objects with inf bounding boxes */
if (!finite(bb[0]) || !finite(bb[1]) || !finite(bb[2]))
return;
if (!finite(bb[3]) || !finite(bb[4]) || !finite(bb[5]))
return;
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
/* skip objects with zero bounding box, they are of no use, and
2012-05-16 23:37:23 +00:00
* will give problems in rtbuild_heuristic_object_split later */
if (bb[0] == bb[3] && bb[1] == bb[4] && bb[2] == bb[5])
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
return;
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
copy_v3_v3(b->primitives.end->bb, bb);
2012-05-15 18:45:20 +00:00
copy_v3_v3(b->primitives.end->bb + 3, bb + 3);
b->primitives.end->obj = o;
b->primitives.end->cost = RE_rayobject_cost(o);
2012-05-15 18:45:20 +00:00
for (int i = 0; i < 3; i++) {
*(b->sorted_end[i]) = b->primitives.end;
b->sorted_end[i]++;
}
b->primitives.end++;
}
int rtbuild_size(RTBuilder *b)
{
return b->sorted_end[0] - b->sorted_begin[0];
}
2012-04-29 15:47:02 +00:00
template<class Obj, int Axis>
static bool obj_bb_compare(const Obj &a, const Obj &b)
{
if (a->bb[Axis] != b->bb[Axis])
return a->bb[Axis] < b->bb[Axis];
return a->obj < b->obj;
}
template<class Item>
static void object_sort(Item *begin, Item *end, int axis)
{
2012-04-29 15:47:02 +00:00
if (axis == 0) return std::sort(begin, end, obj_bb_compare<Item, 0> );
if (axis == 1) return std::sort(begin, end, obj_bb_compare<Item, 1> );
if (axis == 2) return std::sort(begin, end, obj_bb_compare<Item, 2> );
assert(false);
}
2012-05-15 18:45:20 +00:00
void rtbuild_done(RTBuilder *b, RayObjectControl *ctrl)
{
2012-05-15 18:45:20 +00:00
for (int i = 0; i < 3; i++) {
if (b->sorted_begin[i]) {
if (RE_rayobjectcontrol_test_break(ctrl)) break;
object_sort(b->sorted_begin[i], b->sorted_end[i], i);
}
}
}
2012-05-15 18:45:20 +00:00
RayObject *rtbuild_get_primitive(RTBuilder *b, int index)
{
return b->sorted_begin[0][index]->obj;
}
2012-05-15 18:45:20 +00:00
RTBuilder *rtbuild_get_child(RTBuilder *b, int child, RTBuilder *tmp)
{
2012-05-15 18:45:20 +00:00
rtbuild_init(tmp);
2012-05-15 18:45:20 +00:00
for (int i = 0; i < 3; i++)
if (b->sorted_begin[i]) {
2012-05-15 18:45:20 +00:00
tmp->sorted_begin[i] = b->sorted_begin[i] + b->child_offset[child];
tmp->sorted_end[i] = b->sorted_begin[i] + b->child_offset[child + 1];
}
else {
tmp->sorted_begin[i] = 0;
2012-05-15 18:45:20 +00:00
tmp->sorted_end[i] = 0;
}
return tmp;
}
void rtbuild_calc_bb(RTBuilder *b)
{
if (b->bb[0] == 1.0e30f) {
for (RTBuilder::Object **index = b->sorted_begin[0]; index != b->sorted_end[0]; index++)
2012-05-15 18:45:20 +00:00
RE_rayobject_merge_bb( (*index)->obj, b->bb, b->bb + 3);
}
}
void rtbuild_merge_bb(RTBuilder *b, float *min, float *max)
{
rtbuild_calc_bb(b);
DO_MIN(b->bb, min);
2012-05-15 18:45:20 +00:00
DO_MAX(b->bb + 3, max);
}
2012-03-09 18:28:30 +00:00
#if 0
int rtbuild_get_largest_axis(RTBuilder *b)
{
rtbuild_calc_bb(b);
2012-05-15 18:45:20 +00:00
return bb_largest_axis(b->bb, b->bb + 3);
}
//Left balanced tree
int rtbuild_mean_split(RTBuilder *b, int nchilds, int axis)
{
int i;
int mleafs_per_child, Mleafs_per_child;
int tot_leafs = rtbuild_size(b);
int missing_leafs;
long long s;
assert(nchilds <= RTBUILD_MAX_CHILDS);
//TODO optimize calc of leafs_per_child
2012-05-15 18:45:20 +00:00
for (s = nchilds; s < tot_leafs; s *= nchilds) ;
Mleafs_per_child = s / nchilds;
mleafs_per_child = Mleafs_per_child / nchilds;
//split min leafs per child
b->child_offset[0] = 0;
2012-05-15 18:45:20 +00:00
for (i = 1; i <= nchilds; i++)
b->child_offset[i] = mleafs_per_child;
//split remaining leafs
2012-05-15 18:45:20 +00:00
missing_leafs = tot_leafs - mleafs_per_child * nchilds;
for (i = 1; i <= nchilds; i++)
{
if (missing_leafs > Mleafs_per_child - mleafs_per_child)
{
b->child_offset[i] += Mleafs_per_child - mleafs_per_child;
missing_leafs -= Mleafs_per_child - mleafs_per_child;
}
2012-05-15 18:45:20 +00:00
else {
b->child_offset[i] += missing_leafs;
missing_leafs = 0;
break;
}
}
//adjust for accumulative offsets
2012-05-15 18:45:20 +00:00
for (i = 1; i <= nchilds; i++)
b->child_offset[i] += b->child_offset[i - 1];
//Count created childs
2012-05-15 18:45:20 +00:00
for (i = nchilds; b->child_offset[i] == b->child_offset[i - 1]; i--) ;
split_leafs(b, b->child_offset, i, axis);
2012-05-15 18:45:20 +00:00
assert(b->child_offset[0] == 0 && b->child_offset[i] == tot_leafs);
return i;
}
int rtbuild_mean_split_largest_axis(RTBuilder *b, int nchilds)
{
int axis = rtbuild_get_largest_axis(b);
return rtbuild_mean_split(b, nchilds, axis);
}
2012-03-09 18:28:30 +00:00
#endif
/*
* "separators" is an array of dim NCHILDS-1
* and indicates where to cut the childs
*/
2012-03-09 18:28:30 +00:00
#if 0
int rtbuild_median_split(RTBuilder *b, float *separators, int nchilds, int axis)
{
int size = rtbuild_size(b);
assert(nchilds <= RTBUILD_MAX_CHILDS);
if (size <= nchilds)
{
return rtbuild_mean_split(b, nchilds, axis);
}
2012-05-15 18:45:20 +00:00
else {
int i;
b->split_axis = axis;
//Calculate child offsets
b->child_offset[0] = 0;
2012-05-15 18:45:20 +00:00
for (i = 0; i < nchilds - 1; i++)
b->child_offset[i + 1] = split_leafs_by_plane(b, b->child_offset[i], size, separators[i]);
b->child_offset[nchilds] = size;
2012-05-15 18:45:20 +00:00
for (i = 0; i < nchilds; i++)
if (b->child_offset[i + 1] - b->child_offset[i] == size)
return rtbuild_mean_split(b, nchilds, axis);
return nchilds;
}
}
int rtbuild_median_split_largest_axis(RTBuilder *b, int nchilds)
{
int la, i;
float separators[RTBUILD_MAX_CHILDS];
rtbuild_calc_bb(b);
2012-05-15 18:45:20 +00:00
la = bb_largest_axis(b->bb, b->bb + 3);
for (i = 1; i < nchilds; i++)
separators[i - 1] = (b->bb[la + 3] - b->bb[la]) * i / nchilds;
return rtbuild_median_split(b, separators, nchilds, la);
}
2012-03-09 18:28:30 +00:00
#endif
//Heuristics Object Splitter
2012-05-15 18:45:20 +00:00
struct SweepCost {
float bb[6];
float cost;
};
/* Object Surface Area Heuristic splitter */
int rtbuild_heuristic_object_split(RTBuilder *b, int nchilds)
{
int size = rtbuild_size(b);
assert(nchilds == 2);
assert(size > 1);
int baxis = -1, boffset = 0;
if (size > nchilds) {
float bcost = FLT_MAX;
2012-05-15 18:45:20 +00:00
baxis = -1, boffset = size / 2;
2012-05-15 18:45:20 +00:00
SweepCost *sweep = (SweepCost *)MEM_mallocN(sizeof(SweepCost) * size, "RTBuilder.HeuristicSweep");
2012-05-15 18:45:20 +00:00
for (int axis = 0; axis < 3; axis++) {
SweepCost sweep_left;
RTBuilder::Object **obj = b->sorted_begin[axis];
// float right_cost = 0;
2012-05-15 18:45:20 +00:00
for (int i = size - 1; i >= 0; i--) {
if (i == size - 1) {
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
copy_v3_v3(sweep[i].bb, obj[i]->bb);
2012-05-15 18:45:20 +00:00
copy_v3_v3(sweep[i].bb + 3, obj[i]->bb + 3);
sweep[i].cost = obj[i]->cost;
}
else {
2012-05-15 18:45:20 +00:00
sweep[i].bb[0] = MIN2(obj[i]->bb[0], sweep[i + 1].bb[0]);
sweep[i].bb[1] = MIN2(obj[i]->bb[1], sweep[i + 1].bb[1]);
sweep[i].bb[2] = MIN2(obj[i]->bb[2], sweep[i + 1].bb[2]);
sweep[i].bb[3] = MAX2(obj[i]->bb[3], sweep[i + 1].bb[3]);
sweep[i].bb[4] = MAX2(obj[i]->bb[4], sweep[i + 1].bb[4]);
sweep[i].bb[5] = MAX2(obj[i]->bb[5], sweep[i + 1].bb[5]);
sweep[i].cost = obj[i]->cost + sweep[i + 1].cost;
}
// right_cost += obj[i]->cost;
}
sweep_left.bb[0] = obj[0]->bb[0];
sweep_left.bb[1] = obj[0]->bb[1];
sweep_left.bb[2] = obj[0]->bb[2];
sweep_left.bb[3] = obj[0]->bb[3];
sweep_left.bb[4] = obj[0]->bb[4];
sweep_left.bb[5] = obj[0]->bb[5];
sweep_left.cost = obj[0]->cost;
// right_cost -= obj[0]->cost; if (right_cost < 0) right_cost = 0;
2012-05-15 18:45:20 +00:00
for (int i = 1; i < size; i++) {
//Worst case heuristic (cost of each child is linear)
float hcost, left_side, right_side;
Raytrace modifications from the Render Branch. These should not have any effect on render results, except in some cases with you have overlapping faces, where the noise seems to be slightly reduced. There are some performance improvements, for simple scenes I wouldn't expect more than 5-10% to be cut off the render time, for sintel scenes we got about 50% on average, that's with millions of polygons on intel quad cores. This because memory access / cache misses were the main bottleneck for those scenes, and the optimizations improve that. Interal changes: * Remove RE_raytrace.h, raytracer is now only used by render engine again. * Split non-public parts rayobject.h into rayobject_internal.h, hopefully makes it clearer how the API is used. * Added rayintersection.h to contain some of the stuff from RE_raytrace.h * Change Isect.vec/labda to Isect.dir/dist, previously vec was sometimes normalized and sometimes not, confusing... now dir is always normalized and dist contains the distance. * Change VECCOPY and similar to BLI_math functions. * Force inlining of auxiliary functions for ray-triangle/quad intersection, helps a few percentages. * Reorganize svbvh code so all the traversal functions are in one file * Don't do test for root so that push_childs can be inlined * Make shadow a template parameter so it doesn't need to be runtime checked * Optimization in raytree building, was computing bounding boxes more often than necessary. * Leave out logf() factor in SAH, makes tree build quicker with no noticeable influence on raytracing on performance? * Set max childs to 4, simplifies traversal code a bit, but also seems to help slightly in general. * Store child pointers and child bb just as fixed arrays of size 4 in nodes, nearly all nodes have this many children, so overall it actually reduces memory usage a bit and avoids a pointer indirection.
2011-02-05 13:41:29 +00:00
// not using log seems to have no impact on raytracing perf, but
// makes tree construction quicker, left out for now to test (brecht)
// left_side = bb_area(sweep_left.bb, sweep_left.bb+3)*(sweep_left.cost+logf((float)i));
// right_side= bb_area(sweep[i].bb, sweep[i].bb+3)*(sweep[i].cost+logf((float)size-i));
2012-05-15 18:45:20 +00:00
left_side = bb_area(sweep_left.bb, sweep_left.bb + 3) * (sweep_left.cost);
right_side = bb_area(sweep[i].bb, sweep[i].bb + 3) * (sweep[i].cost);
hcost = left_side + right_side;
assert(left_side >= 0);
assert(right_side >= 0);
2012-05-15 18:45:20 +00:00
if (left_side > bcost) break; //No way we can find a better heuristic in this axis
assert(hcost >= 0);
// this makes sure the tree built is the same whatever is the order of the sorting axis
2012-05-15 18:45:20 +00:00
if (hcost < bcost || (hcost == bcost && axis < baxis)) {
bcost = hcost;
baxis = axis;
boffset = i;
}
2012-05-15 18:45:20 +00:00
DO_MIN(obj[i]->bb, sweep_left.bb);
DO_MAX(obj[i]->bb + 3, sweep_left.bb + 3);
sweep_left.cost += obj[i]->cost;
// right_cost -= obj[i]->cost; if (right_cost < 0) right_cost = 0;
}
//assert(baxis >= 0 && baxis < 3);
if (!(baxis >= 0 && baxis < 3))
baxis = 0;
}
MEM_freeN(sweep);
}
else if (size == 2) {
baxis = 0;
boffset = 1;
}
else if (size == 1) {
b->child_offset[0] = 0;
b->child_offset[1] = 1;
return 1;
}
b->child_offset[0] = 0;
b->child_offset[1] = boffset;
b->child_offset[2] = size;
/* Adjust sorted arrays for childs */
2012-05-15 18:45:20 +00:00
for (int i = 0; i < boffset; i++) b->sorted_begin[baxis][i]->selected = true;
for (int i = boffset; i < size; i++) b->sorted_begin[baxis][i]->selected = false;
for (int i = 0; i < 3; i++)
std::stable_partition(b->sorted_begin[i], b->sorted_end[i], selected_node);
return nchilds;
}
/*
* Helper code
* PARTITION code / used on mean-split
* basicly this a std::nth_element (like on C++ STL algorithm)
*/
2012-03-09 18:28:30 +00:00
#if 0
static void split_leafs(RTBuilder *b, int *nth, int partitions, int split_axis)
{
int i;
b->split_axis = split_axis;
2012-05-15 18:45:20 +00:00
for (i = 0; i < partitions - 1; i++)
{
2012-05-15 18:45:20 +00:00
assert(nth[i] < nth[i + 1] && nth[i + 1] < nth[partitions]);
2012-05-15 18:45:20 +00:00
if (split_axis == 0) std::nth_element(b, nth[i], nth[i + 1], nth[partitions], obj_bb_compare<RTBuilder::Object, 0>);
if (split_axis == 1) std::nth_element(b, nth[i], nth[i + 1], nth[partitions], obj_bb_compare<RTBuilder::Object, 1>);
if (split_axis == 2) std::nth_element(b, nth[i], nth[i + 1], nth[partitions], obj_bb_compare<RTBuilder::Object, 2>);
}
}
2012-03-09 18:28:30 +00:00
#endif
/*
* Bounding Box utils
*/
float bb_volume(float *min, float *max)
{
2012-05-15 18:45:20 +00:00
return (max[0] - min[0]) * (max[1] - min[1]) * (max[2] - min[2]);
}
float bb_area(float *min, float *max)
{
float sub[3], a;
2012-05-15 18:45:20 +00:00
sub[0] = max[0] - min[0];
sub[1] = max[1] - min[1];
sub[2] = max[2] - min[2];
a = (sub[0] * sub[1] + sub[0] * sub[2] + sub[1] * sub[2]) * 2;
/* used to have an assert() here on negative results
* however, in this case its likely some overflow or ffast math error.
* so just return 0.0f instead. */
return a < 0.0f ? 0.0f : a;
}
int bb_largest_axis(float *min, float *max)
{
float sub[3];
2012-05-15 18:45:20 +00:00
sub[0] = max[0] - min[0];
sub[1] = max[1] - min[1];
sub[2] = max[2] - min[2];
if (sub[0] > sub[1]) {
if (sub[0] > sub[2])
return 0;
else
return 2;
}
else {
if (sub[1] > sub[2])
return 1;
else
return 2;
}
}
int bb_fits_inside(float *outer_min, float *outer_max, float *inner_min, float *inner_max)
{
int i;
2012-05-15 18:45:20 +00:00
for (i = 0; i < 3; i++)
if (outer_min[i] > inner_min[i]) return 0;
2012-05-15 18:45:20 +00:00
for (i = 0; i < 3; i++)
if (outer_max[i] < inner_max[i]) return 0;
return 1;
}