[Bf-blender-cvs] [68478ae] master: Cycles: Avoid having duplication of BVH arrays during build

Sergey Sharybin noreply at git.blender.org
Sun Jun 28 18:15:41 CEST 2015


Commit: 68478aea016e87e071d550797b36acd32d33bd12
Author: Sergey Sharybin
Date:   Sun Jun 28 18:07:48 2015 +0200
Branches: master
https://developer.blender.org/rB68478aea016e87e071d550797b36acd32d33bd12

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.

===================================================================

M	intern/cycles/bvh/bvh.cpp
M	intern/cycles/bvh/bvh_build.cpp
M	intern/cycles/bvh/bvh_build.h

===================================================================

diff --git a/intern/cycles/bvh/bvh.cpp b/intern/cycles/bvh/bvh.cpp
index 0d9412a..350ca16 100644
--- a/intern/cycles/bvh/bvh.cpp
+++ b/intern/cycles/bvh/bvh.cpp
@@ -190,11 +190,12 @@ void BVH::build(Progress& progress)
 	}
 
 	/* build nodes */
-	vector<int> prim_type;
-	vector<int> prim_index;
-	vector<int> prim_object;
-
-	BVHBuild bvh_build(objects, prim_type, prim_index, prim_object, params, progress);
+	BVHBuild bvh_build(objects,
+	                   pack.prim_type,
+	                   pack.prim_index,
+	                   pack.prim_object,
+	                   params,
+	                   progress);
 	BVHNode *root = bvh_build.run();
 
 	if(progress.get_cancel()) {
@@ -202,14 +203,6 @@ void BVH::build(Progress& progress)
 		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 */
 	if(!params.top_level)
 		pack.SAH = root->computeSubtreeSAHCost(params);
diff --git a/intern/cycles/bvh/bvh_build.cpp b/intern/cycles/bvh/bvh_build.cpp
index 5baf949..a44ad65 100644
--- a/intern/cycles/bvh/bvh_build.cpp
+++ b/intern/cycles/bvh/bvh_build.cpp
@@ -65,15 +65,18 @@ public:
 /* Constructor / Destructor */
 
 BVHBuild::BVHBuild(const vector<Object*>& objects_,
-	vector<int>& prim_type_, vector<int>& prim_index_, vector<int>& prim_object_,
-	const BVHParams& params_, Progress& progress_)
-: objects(objects_),
-  prim_type(prim_type_),
-  prim_index(prim_index_),
-  prim_object(prim_object_),
-  params(params_),
-  progress(progress_),
-  progress_start_time(0.0)
+                   array<int>& prim_type_,
+                   array<int>& prim_index_,
+                   array<int>& prim_object_,
+                   const BVHParams& params_,
+                   Progress& progress_)
+ : objects(objects_),
+   prim_type(prim_type_),
+   prim_index(prim_index_),
+   prim_object(prim_object_),
+   params(params_),
+   progress(progress_),
+   progress_start_time(0.0)
 {
 	spatial_min_overlap = 0.0f;
 }
@@ -446,18 +449,10 @@ BVHNode *BVHBuild::create_object_leaf_nodes(const BVHReference *ref, int start,
 		return new LeafNode(bounds, 0, 0, 0);
 	}
 	else if(num == 1) {
-		if(start == prim_index.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_index[start] = ref->prim_index();
-			prim_object[start] = ref->prim_object();
-		}
+		assert(start < prim_type.size());
+		prim_type[start] = ref->prim_type();
+		prim_index[start] = ref->prim_index();
+		prim_object[start] = ref->prim_object();
 
 		uint visibility = objects[ref->prim_object()]->visibility;
 		return new LeafNode(ref->bounds(), visibility, start, start+1);
@@ -484,17 +479,9 @@ BVHNode *BVHBuild::create_primitive_leaf_node(const int *p_type,
                                               int num)
 {
 	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_index[start + i] = p_index[i];
-			prim_object[start + i] = p_object[i];
-		}
+		prim_type[start + i] = p_type[i];
+		prim_index[start + i] = p_index[i];
+		prim_object[start + i] = p_object[i];
 	}
 	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. */
 	BVHNode *leaves[PRIMITIVE_NUM_TOTAL + 1] = {NULL};
 	int num_leaves = 0;
diff --git a/intern/cycles/bvh/bvh_build.h b/intern/cycles/bvh/bvh_build.h
index 294c5a1..eefb7b6 100644
--- a/intern/cycles/bvh/bvh_build.h
+++ b/intern/cycles/bvh/bvh_build.h
@@ -42,13 +42,12 @@ class BVHBuild
 {
 public:
 	/* Constructor/Destructor */
-	BVHBuild(
-		const vector<Object*>& objects,
-		vector<int>& prim_type,
-		vector<int>& prim_index,
-		vector<int>& prim_object,
-		const BVHParams& params,
-		Progress& progress);
+	BVHBuild(const vector<Object*>& objects,
+	         array<int>& prim_type,
+	         array<int>& prim_index,
+	         array<int>& prim_object,
+	         const BVHParams& params,
+	         Progress& progress);
 	~BVHBuild();
 
 	BVHNode *run();
@@ -99,9 +98,9 @@ protected:
 	int num_original_references;
 
 	/* output primitive indexes and objects */
-	vector<int>& prim_type;
-	vector<int>& prim_index;
-	vector<int>& prim_object;
+	array<int>& prim_type;
+	array<int>& prim_index;
+	array<int>& prim_object;
 
 	/* build parameters */
 	BVHParams params;




More information about the Bf-blender-cvs mailing list