forked from bartvdbraak/blender
Cycles: Avoid having duplication of BVH arrays during build
Previous idea behind having vector during building and array for actual storage was needed in order to minimize amount of re-allocations happening during the build, but it lead to double memory overhead used by those arrays at the vector to array conversion stage. Issue with such approach was that for BVH without spatial split size of arrays is known in advance and it never changes, which made vector to array conversion totally redundant. Also after testing with several rather complex from spatial split scenes (such as trees) it seems even conservative approach of reallocation (when we perform re-allocation when leaf does not fit into the memory) doesn't give measurable difference in time. This makes it so we can switch to array, which will avoid unneeded memory re-allocations when spatial split is disabled without harming other cases. it's a bit difficult to measure exact benefit of this change on our production files here, but depending on the scene it might give quite reasonable memory save.
This commit is contained in:
parent
b506f3d328
commit
68478aea01
@ -190,11 +190,12 @@ void BVH::build(Progress& progress)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* build nodes */
|
/* build nodes */
|
||||||
vector<int> prim_type;
|
BVHBuild bvh_build(objects,
|
||||||
vector<int> prim_index;
|
pack.prim_type,
|
||||||
vector<int> prim_object;
|
pack.prim_index,
|
||||||
|
pack.prim_object,
|
||||||
BVHBuild bvh_build(objects, prim_type, prim_index, prim_object, params, progress);
|
params,
|
||||||
|
progress);
|
||||||
BVHNode *root = bvh_build.run();
|
BVHNode *root = bvh_build.run();
|
||||||
|
|
||||||
if(progress.get_cancel()) {
|
if(progress.get_cancel()) {
|
||||||
@ -202,14 +203,6 @@ void BVH::build(Progress& progress)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* todo: get rid of this copy */
|
|
||||||
pack.prim_type = prim_type;
|
|
||||||
pack.prim_index = prim_index;
|
|
||||||
pack.prim_object = prim_object;
|
|
||||||
prim_type.free_memory();
|
|
||||||
prim_index.free_memory();
|
|
||||||
prim_object.free_memory();
|
|
||||||
|
|
||||||
/* compute SAH */
|
/* compute SAH */
|
||||||
if(!params.top_level)
|
if(!params.top_level)
|
||||||
pack.SAH = root->computeSubtreeSAHCost(params);
|
pack.SAH = root->computeSubtreeSAHCost(params);
|
||||||
|
@ -65,9 +65,12 @@ public:
|
|||||||
/* Constructor / Destructor */
|
/* Constructor / Destructor */
|
||||||
|
|
||||||
BVHBuild::BVHBuild(const vector<Object*>& objects_,
|
BVHBuild::BVHBuild(const vector<Object*>& objects_,
|
||||||
vector<int>& prim_type_, vector<int>& prim_index_, vector<int>& prim_object_,
|
array<int>& prim_type_,
|
||||||
const BVHParams& params_, Progress& progress_)
|
array<int>& prim_index_,
|
||||||
: objects(objects_),
|
array<int>& prim_object_,
|
||||||
|
const BVHParams& params_,
|
||||||
|
Progress& progress_)
|
||||||
|
: objects(objects_),
|
||||||
prim_type(prim_type_),
|
prim_type(prim_type_),
|
||||||
prim_index(prim_index_),
|
prim_index(prim_index_),
|
||||||
prim_object(prim_object_),
|
prim_object(prim_object_),
|
||||||
@ -446,18 +449,10 @@ BVHNode *BVHBuild::create_object_leaf_nodes(const BVHReference *ref, int start,
|
|||||||
return new LeafNode(bounds, 0, 0, 0);
|
return new LeafNode(bounds, 0, 0, 0);
|
||||||
}
|
}
|
||||||
else if(num == 1) {
|
else if(num == 1) {
|
||||||
if(start == prim_index.size()) {
|
assert(start < prim_type.size());
|
||||||
assert(params.use_spatial_split);
|
|
||||||
|
|
||||||
prim_type.push_back(ref->prim_type());
|
|
||||||
prim_index.push_back(ref->prim_index());
|
|
||||||
prim_object.push_back(ref->prim_object());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
prim_type[start] = ref->prim_type();
|
prim_type[start] = ref->prim_type();
|
||||||
prim_index[start] = ref->prim_index();
|
prim_index[start] = ref->prim_index();
|
||||||
prim_object[start] = ref->prim_object();
|
prim_object[start] = ref->prim_object();
|
||||||
}
|
|
||||||
|
|
||||||
uint visibility = objects[ref->prim_object()]->visibility;
|
uint visibility = objects[ref->prim_object()]->visibility;
|
||||||
return new LeafNode(ref->bounds(), visibility, start, start+1);
|
return new LeafNode(ref->bounds(), visibility, start, start+1);
|
||||||
@ -484,18 +479,10 @@ BVHNode *BVHBuild::create_primitive_leaf_node(const int *p_type,
|
|||||||
int num)
|
int num)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < num; ++i) {
|
for(int i = 0; i < num; ++i) {
|
||||||
if(start + i == prim_index.size()) {
|
|
||||||
assert(params.use_spatial_split);
|
|
||||||
prim_type.push_back(p_type[i]);
|
|
||||||
prim_index.push_back(p_index[i]);
|
|
||||||
prim_object.push_back(p_object[i]);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
prim_type[start + i] = p_type[i];
|
prim_type[start + i] = p_type[i];
|
||||||
prim_index[start + i] = p_index[i];
|
prim_index[start + i] = p_index[i];
|
||||||
prim_object[start + i] = p_object[i];
|
prim_object[start + i] = p_object[i];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return new LeafNode(bounds, visibility, start, start + num);
|
return new LeafNode(bounds, visibility, start, start + num);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,6 +522,19 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Extend an array when needed. */
|
||||||
|
if(prim_type.size() < range.end()) {
|
||||||
|
assert(params.use_spatial_split);
|
||||||
|
/* TODO(sergey): We might want to look into different policies of
|
||||||
|
* re-allocation here, so on the one hand we would not do as much
|
||||||
|
* re-allocations and on the other hand will have small memory
|
||||||
|
* overhead.
|
||||||
|
*/
|
||||||
|
prim_type.resize(range.end());
|
||||||
|
prim_index.resize(range.end());
|
||||||
|
prim_object.resize(range.end());
|
||||||
|
}
|
||||||
|
|
||||||
/* Create leaf nodes for every existing primitive. */
|
/* Create leaf nodes for every existing primitive. */
|
||||||
BVHNode *leaves[PRIMITIVE_NUM_TOTAL + 1] = {NULL};
|
BVHNode *leaves[PRIMITIVE_NUM_TOTAL + 1] = {NULL};
|
||||||
int num_leaves = 0;
|
int num_leaves = 0;
|
||||||
|
@ -42,11 +42,10 @@ class BVHBuild
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/* Constructor/Destructor */
|
/* Constructor/Destructor */
|
||||||
BVHBuild(
|
BVHBuild(const vector<Object*>& objects,
|
||||||
const vector<Object*>& objects,
|
array<int>& prim_type,
|
||||||
vector<int>& prim_type,
|
array<int>& prim_index,
|
||||||
vector<int>& prim_index,
|
array<int>& prim_object,
|
||||||
vector<int>& prim_object,
|
|
||||||
const BVHParams& params,
|
const BVHParams& params,
|
||||||
Progress& progress);
|
Progress& progress);
|
||||||
~BVHBuild();
|
~BVHBuild();
|
||||||
@ -99,9 +98,9 @@ protected:
|
|||||||
int num_original_references;
|
int num_original_references;
|
||||||
|
|
||||||
/* output primitive indexes and objects */
|
/* output primitive indexes and objects */
|
||||||
vector<int>& prim_type;
|
array<int>& prim_type;
|
||||||
vector<int>& prim_index;
|
array<int>& prim_index;
|
||||||
vector<int>& prim_object;
|
array<int>& prim_object;
|
||||||
|
|
||||||
/* build parameters */
|
/* build parameters */
|
||||||
BVHParams params;
|
BVHParams params;
|
||||||
|
Loading…
Reference in New Issue
Block a user