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_blibvh.cpp

170 lines
4.4 KiB
C++
Raw Normal View History

/*
2009-05-29 22:55: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-05-29 22:55:06 +00:00
*
* 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.
2009-05-29 22:55:06 +00:00
*
* ***** END GPL LICENSE BLOCK *****
*/
2011-02-27 19:31:27 +00:00
/** \file blender/render/intern/raytrace/rayobject_blibvh.cpp
* \ingroup render
*/
2009-05-29 22:55:06 +00:00
#include <assert.h>
#include "MEM_guardedalloc.h"
2009-05-29 22:55:06 +00:00
#include "BLI_kdopbvh.h"
#include "BLI_math.h"
#include "BLI_utildefines.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 "rayintersection.h"
2009-05-29 22:55:06 +00:00
#include "rayobject.h"
static int RE_rayobject_blibvh_intersect(RayObject *o, Isect *isec);
static void RE_rayobject_blibvh_add(RayObject *o, RayObject *ob);
static void RE_rayobject_blibvh_done(RayObject *o);
static void RE_rayobject_blibvh_free(RayObject *o);
2012-05-15 18:45:20 +00:00
static void RE_rayobject_blibvh_bb(RayObject * o, float min[3], float max[3]);
2009-05-29 22:55:06 +00:00
static float RE_rayobject_blibvh_cost(RayObject *UNUSED(o))
{
//TODO calculate the expected cost to raycast on this structure
return 1.0;
}
static void RE_rayobject_blibvh_hint_bb(RayObject *UNUSED(o), RayHint *UNUSED(hint),
float *UNUSED(min), float *UNUSED(max))
{
return;
}
2009-05-29 22:55:06 +00:00
static RayObjectAPI bvh_api =
{
RE_rayobject_blibvh_intersect,
RE_rayobject_blibvh_add,
RE_rayobject_blibvh_done,
RE_rayobject_blibvh_free,
RE_rayobject_blibvh_bb,
RE_rayobject_blibvh_cost,
RE_rayobject_blibvh_hint_bb
2009-05-29 22:55:06 +00:00
};
2012-05-15 18:45:20 +00:00
typedef struct BVHObject {
2009-05-29 22:55:06 +00:00
RayObject rayobj;
2009-10-05 23:30:00 +00:00
RayObject **leafs, **next_leaf;
2009-05-29 22:55:06 +00:00
BVHTree *bvh;
float bb[2][3];
2009-05-29 22:55:06 +00:00
} BVHObject;
RayObject *RE_rayobject_blibvh_create(int size)
2009-05-29 22:55:06 +00:00
{
2012-05-15 18:45:20 +00:00
BVHObject *obj = (BVHObject *)MEM_callocN(sizeof(BVHObject), "BVHObject");
assert(RE_rayobject_isAligned(obj)); /* RayObject API assumes real data to be 4-byte aligned */
2009-05-29 22:55:06 +00:00
obj->rayobj.api = &bvh_api;
obj->bvh = BLI_bvhtree_new(size, 0.0, 4, 6);
2012-05-15 18:45:20 +00:00
obj->next_leaf = obj->leafs = (RayObject **)MEM_callocN(size * sizeof(RayObject *), "BVHObject leafs");
2009-05-29 22:55:06 +00:00
INIT_MINMAX(obj->bb[0], obj->bb[1]);
2012-05-15 18:45:20 +00:00
return RE_rayobject_unalignRayAPI((RayObject *) obj);
2009-05-29 22:55:06 +00:00
}
2012-05-15 18:45:20 +00:00
struct BVHCallbackUserData {
2009-10-05 23:30:00 +00:00
Isect *isec;
RayObject **leafs;
};
static void bvh_callback(void *userdata, int index, const BVHTreeRay *UNUSED(ray), BVHTreeRayHit *hit)
2009-05-29 22:55:06 +00:00
{
2012-05-15 18:45:20 +00:00
struct BVHCallbackUserData *data = (struct BVHCallbackUserData *)userdata;
2009-10-05 23:30:00 +00:00
Isect *isec = data->isec;
RayObject *face = data->leafs[index];
2009-05-29 22:55:06 +00:00
if (RE_rayobject_intersect(face, isec)) {
2009-05-29 22:55:06 +00:00
hit->index = index;
if (isec->mode == RE_RAY_SHADOW)
hit->dist = 0;
else
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
hit->dist = isec->dist;
2009-05-29 22:55:06 +00:00
}
}
static int RE_rayobject_blibvh_intersect(RayObject *o, Isect *isec)
2009-05-29 22:55:06 +00:00
{
2012-05-15 18:45:20 +00:00
BVHObject *obj = (BVHObject *)o;
BVHTreeRayHit hit;
float dir[3];
2009-10-05 23:30:00 +00:00
struct BVHCallbackUserData data;
data.isec = isec;
data.leafs = obj->leafs;
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(dir, isec->dir);
hit.index = 0;
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
hit.dist = isec->dist;
2009-05-29 22:55:06 +00:00
2012-05-15 18:45:20 +00:00
return BLI_bvhtree_ray_cast(obj->bvh, isec->start, dir, 0.0, &hit, bvh_callback, (void *)&data);
2009-05-29 22:55:06 +00:00
}
static void RE_rayobject_blibvh_add(RayObject *o, RayObject *ob)
2009-05-29 22:55:06 +00:00
{
2012-05-15 18:45:20 +00:00
BVHObject *obj = (BVHObject *)o;
2009-05-29 22:55:06 +00:00
float min_max[6];
2012-05-15 18:45:20 +00:00
INIT_MINMAX(min_max, min_max + 3);
RE_rayobject_merge_bb(ob, min_max, min_max + 3);
2012-04-29 15:47:02 +00:00
DO_MIN(min_max, obj->bb[0]);
DO_MAX(min_max + 3, obj->bb[1]);
BLI_bvhtree_insert(obj->bvh, obj->next_leaf - obj->leafs, min_max, 2);
2009-10-05 23:30:00 +00:00
*(obj->next_leaf++) = ob;
2009-05-29 22:55:06 +00:00
}
static void RE_rayobject_blibvh_done(RayObject *o)
2009-05-29 22:55:06 +00:00
{
2012-05-15 18:45:20 +00:00
BVHObject *obj = (BVHObject *)o;
2009-05-29 22:55:06 +00:00
BLI_bvhtree_balance(obj->bvh);
}
static void RE_rayobject_blibvh_free(RayObject *o)
2009-05-29 22:55:06 +00:00
{
2012-05-15 18:45:20 +00:00
BVHObject *obj = (BVHObject *)o;
2009-05-29 22:55:06 +00:00
if (obj->bvh)
2009-05-29 22:55:06 +00:00
BLI_bvhtree_free(obj->bvh);
if (obj->leafs)
2009-10-05 23:30:00 +00:00
MEM_freeN(obj->leafs);
2009-05-29 22:55:06 +00:00
MEM_freeN(obj);
}
static void RE_rayobject_blibvh_bb(RayObject *o, float min[3], float max[3])
2009-05-29 22:55:06 +00:00
{
2012-05-15 18:45:20 +00:00
BVHObject *obj = (BVHObject *)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
DO_MIN(obj->bb[0], min);
DO_MAX(obj->bb[1], max);
2009-05-29 22:55:06 +00:00
}
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