2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2009-09-06 19:14:06 +00:00
|
|
|
* ***** 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.
|
2009-09-06 19:14:06 +00:00
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2009 Blender Foundation.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): André Pinto.
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*/
|
2009-08-29 17:24:45 +00:00
|
|
|
|
2011-02-27 19:31:27 +00:00
|
|
|
/** \file blender/render/intern/raytrace/vbvh.h
|
|
|
|
|
* \ingroup render
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
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 <assert.h>
|
2009-08-29 17:24:45 +00:00
|
|
|
#include <algorithm>
|
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
|
|
|
|
2009-08-29 17:24:45 +00:00
|
|
|
#include "BLI_memarena.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 "rayobject_rtbuild.h"
|
2009-08-29 17:24:45 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* VBVHNode represents a BVHNode with support for a variable number of childrens
|
|
|
|
|
*/
|
2012-05-15 18:45:20 +00:00
|
|
|
struct VBVHNode {
|
|
|
|
|
float bb[6];
|
2009-08-29 17:24:45 +00:00
|
|
|
|
|
|
|
|
VBVHNode *child;
|
|
|
|
|
VBVHNode *sibling;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Push nodes (used on dfs)
|
|
|
|
|
*/
|
|
|
|
|
template<class Node>
|
2012-01-14 10:08:47 +00:00
|
|
|
inline static void bvh_node_push_childs(Node *node, Isect *UNUSED(isec), Node **stack, int &stack_pos)
|
2009-08-29 17:24:45 +00:00
|
|
|
{
|
|
|
|
|
Node *child = node->child;
|
|
|
|
|
|
2012-04-28 06:31:57 +00:00
|
|
|
if (is_leaf(child)) {
|
2009-08-29 17:24:45 +00:00
|
|
|
stack[stack_pos++] = child;
|
|
|
|
|
}
|
2012-04-28 06:31:57 +00:00
|
|
|
else {
|
|
|
|
|
while (child) {
|
2012-03-09 18:28:30 +00:00
|
|
|
/* Skips BB tests on primitives */
|
|
|
|
|
#if 0
|
2012-04-28 06:31:57 +00:00
|
|
|
if (is_leaf(child->child)) {
|
2009-08-29 17:24:45 +00:00
|
|
|
stack[stack_pos++] = child->child;
|
2012-03-09 18:28:30 +00:00
|
|
|
}
|
2009-08-29 17:24:45 +00:00
|
|
|
else
|
2012-03-09 18:28:30 +00:00
|
|
|
#endif
|
|
|
|
|
{
|
2009-08-29 17:24:45 +00:00
|
|
|
stack[stack_pos++] = child;
|
2012-03-09 18:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-29 17:24:45 +00:00
|
|
|
child = child->sibling;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Node>
|
2013-05-08 12:55:05 +00:00
|
|
|
static int count_childs(Node *parent)
|
2009-08-29 17:24:45 +00:00
|
|
|
{
|
|
|
|
|
int n = 0;
|
2012-04-28 06:31:57 +00:00
|
|
|
for (Node *i = parent->child; i; i = i->sibling) {
|
2009-08-29 17:24:45 +00:00
|
|
|
n++;
|
2012-04-28 06:31:57 +00:00
|
|
|
if (is_leaf(i))
|
2009-08-29 17:24:45 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Node>
|
2013-05-08 12:55:05 +00:00
|
|
|
static void append_sibling(Node *node, Node *sibling)
|
2009-08-29 17:24:45 +00:00
|
|
|
{
|
2012-04-28 06:31:57 +00:00
|
|
|
while (node->sibling)
|
2009-08-29 17:24:45 +00:00
|
|
|
node = node->sibling;
|
|
|
|
|
|
|
|
|
|
node->sibling = sibling;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Builds a binary VBVH from a rtbuild
|
|
|
|
|
*/
|
2009-09-06 19:14:06 +00:00
|
|
|
template<class Node>
|
2012-05-15 18:45:20 +00:00
|
|
|
struct BuildBinaryVBVH {
|
2009-08-29 17:24:45 +00:00
|
|
|
MemArena *arena;
|
2009-10-04 16:56:00 +00:00
|
|
|
RayObjectControl *control;
|
2009-08-29 17:24:45 +00:00
|
|
|
|
2009-10-04 16:56:00 +00:00
|
|
|
void test_break()
|
|
|
|
|
{
|
2012-04-28 06:31:57 +00:00
|
|
|
if (RE_rayobjectcontrol_test_break(control))
|
2009-10-04 16:56:00 +00:00
|
|
|
throw "Stop";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BuildBinaryVBVH(MemArena *a, RayObjectControl *c)
|
2009-08-29 17:24:45 +00:00
|
|
|
{
|
|
|
|
|
arena = a;
|
2009-10-04 16:56:00 +00:00
|
|
|
control = c;
|
2009-08-29 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
2009-09-06 19:14:06 +00:00
|
|
|
Node *create_node()
|
2009-08-29 17:24:45 +00:00
|
|
|
{
|
2012-05-15 18:45:20 +00:00
|
|
|
Node *node = (Node *)BLI_memarena_alloc(arena, sizeof(Node) );
|
2012-04-29 17:11:40 +00:00
|
|
|
assert(RE_rayobject_isAligned(node));
|
2009-08-29 17:24:45 +00:00
|
|
|
|
|
|
|
|
node->sibling = NULL;
|
|
|
|
|
node->child = NULL;
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int rtbuild_split(RTBuilder *builder)
|
|
|
|
|
{
|
|
|
|
|
return ::rtbuild_heuristic_object_split(builder, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-06 19:14:06 +00:00
|
|
|
Node *transform(RTBuilder *builder)
|
2009-10-04 16:56:00 +00:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _transform(builder);
|
|
|
|
|
|
2012-05-15 18:45:20 +00:00
|
|
|
} catch (...)
|
2009-10-04 16:56:00 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Node *_transform(RTBuilder *builder)
|
2009-08-29 17:24:45 +00:00
|
|
|
{
|
|
|
|
|
int size = rtbuild_size(builder);
|
2011-03-11 22:27:06 +00:00
|
|
|
|
2012-04-28 06:31:57 +00:00
|
|
|
if (size == 0) {
|
2011-03-11 22:27:06 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2012-04-28 06:31:57 +00:00
|
|
|
else if (size == 1) {
|
2009-09-06 19:14:06 +00:00
|
|
|
Node *node = create_node();
|
2012-05-15 18:45:20 +00:00
|
|
|
INIT_MINMAX(node->bb, node->bb + 3);
|
|
|
|
|
rtbuild_merge_bb(builder, node->bb, node->bb + 3);
|
2012-03-11 19:09:01 +00:00
|
|
|
node->child = (Node *) rtbuild_get_primitive(builder, 0);
|
2009-08-29 17:24:45 +00:00
|
|
|
return node;
|
|
|
|
|
}
|
2012-04-28 06:31:57 +00:00
|
|
|
else {
|
2009-10-04 16:56:00 +00:00
|
|
|
test_break();
|
|
|
|
|
|
2009-09-06 19:14:06 +00:00
|
|
|
Node *node = create_node();
|
2009-08-29 17:24:45 +00:00
|
|
|
|
2009-09-06 19:14:06 +00:00
|
|
|
Node **child = &node->child;
|
2009-08-29 17:24:45 +00:00
|
|
|
|
|
|
|
|
int nc = rtbuild_split(builder);
|
2012-05-15 18:45:20 +00:00
|
|
|
INIT_MINMAX(node->bb, node->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
|
|
|
|
2009-08-29 17:24:45 +00:00
|
|
|
assert(nc == 2);
|
2012-04-28 06:31:57 +00:00
|
|
|
for (int i = 0; i < nc; i++) {
|
2009-08-29 17:24:45 +00:00
|
|
|
RTBuilder tmp;
|
|
|
|
|
rtbuild_get_child(builder, i, &tmp);
|
|
|
|
|
|
2009-10-04 16:56:00 +00:00
|
|
|
*child = _transform(&tmp);
|
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
|
|
|
DO_MIN((*child)->bb, node->bb);
|
2012-05-15 18:45:20 +00:00
|
|
|
DO_MAX((*child)->bb + 3, node->bb + 3);
|
2009-08-29 17:24:45 +00:00
|
|
|
child = &((*child)->sibling);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-26 08:58:17 +00:00
|
|
|
*child = NULL;
|
2009-08-29 17:24:45 +00:00
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-09 18:28:30 +00:00
|
|
|
#if 0
|
2012-05-15 18:45:20 +00:00
|
|
|
template<class Tree, class OldNode>
|
|
|
|
|
struct Reorganize_VBVH {
|
2009-08-29 17:24:45 +00:00
|
|
|
Tree *tree;
|
|
|
|
|
|
|
|
|
|
Reorganize_VBVH(Tree *t)
|
|
|
|
|
{
|
|
|
|
|
tree = t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VBVHNode *create_node()
|
|
|
|
|
{
|
2012-05-15 18:45:20 +00:00
|
|
|
VBVHNode *node = (VBVHNode *)BLI_memarena_alloc(tree->node_arena, sizeof(VBVHNode));
|
2009-08-29 17:24:45 +00:00
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void copy_bb(VBVHNode *node, OldNode *old)
|
|
|
|
|
{
|
2012-05-15 18:45:20 +00:00
|
|
|
std::copy(old->bb, old->bb + 6, node->bb);
|
2009-08-29 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VBVHNode *transform(OldNode *old)
|
|
|
|
|
{
|
2012-04-28 06:31:57 +00:00
|
|
|
if (is_leaf(old))
|
2012-05-15 18:45:20 +00:00
|
|
|
return (VBVHNode *)old;
|
2009-08-29 17:24:45 +00:00
|
|
|
|
|
|
|
|
VBVHNode *node = create_node();
|
|
|
|
|
VBVHNode **child_ptr = &node->child;
|
|
|
|
|
node->sibling = 0;
|
|
|
|
|
|
2012-05-15 18:45:20 +00:00
|
|
|
copy_bb(node, old);
|
2009-08-29 17:24:45 +00:00
|
|
|
|
2012-05-15 18:45:20 +00:00
|
|
|
for (OldNode *o_child = old->child; o_child; o_child = o_child->sibling)
|
2009-08-29 17:24:45 +00:00
|
|
|
{
|
|
|
|
|
VBVHNode *n_child = transform(o_child);
|
|
|
|
|
*child_ptr = n_child;
|
2012-04-28 06:31:57 +00:00
|
|
|
if (is_leaf(n_child)) return node;
|
2009-08-29 17:24:45 +00:00
|
|
|
child_ptr = &n_child->sibling;
|
|
|
|
|
}
|
|
|
|
|
*child_ptr = 0;
|
|
|
|
|
|
|
|
|
|
return node;
|
2012-10-21 05:46:41 +00:00
|
|
|
}
|
2009-08-29 17:24:45 +00:00
|
|
|
};
|
2012-03-09 18:28:30 +00:00
|
|
|
#endif
|