[Bf-blender-cvs] [c707b91] master: Cycles: Optimize leaf splitting code by avoid vector allocation

Sergey Sharybin noreply at git.blender.org
Mon Jan 12 11:07:07 CET 2015


Commit: c707b91ce6b14182a3a579b645cfd832fc1ae658
Author: Sergey Sharybin
Date:   Sat Jan 10 22:32:44 2015 +0500
Branches: master
https://developer.blender.org/rBc707b91ce6b14182a3a579b645cfd832fc1ae658

Cycles: Optimize leaf splitting code by avoid vector allocation

Use variables allocated in the stack and avoid heap allocation which should make
leaf splitting code a bit faster.

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

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

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

diff --git a/intern/cycles/bvh/bvh_build.cpp b/intern/cycles/bvh/bvh_build.cpp
index f4368a8..d0542f0 100644
--- a/intern/cycles/bvh/bvh_build.cpp
+++ b/intern/cycles/bvh/bvh_build.cpp
@@ -449,16 +449,15 @@ BVHNode *BVHBuild::create_object_leaf_nodes(const BVHReference *ref, int start,
 	}
 }
 
-BVHNode *BVHBuild::create_primitive_leaf_node(const vector<int>& p_type,
-                                              const vector<int>& p_index,
-                                              const vector<int>& p_object,
+BVHNode *BVHBuild::create_primitive_leaf_node(const int *p_type,
+                                              const int *p_index,
+                                              const int *p_object,
                                               const BoundBox& bounds,
                                               uint visibility,
-                                              int start)
+                                              int start,
+                                              int num)
 {
-	assert(p_type.size() == p_index.size());
-	assert(p_index.size() == p_object.size());
-	for(int i = 0; i < p_type.size(); ++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]);
@@ -471,30 +470,34 @@ BVHNode *BVHBuild::create_primitive_leaf_node(const vector<int>& p_type,
 			prim_object[start + i] = p_object[i];
 		}
 	}
-	return new LeafNode(bounds, visibility, start, start + p_type.size());
+	return new LeafNode(bounds, visibility, start, start + num);
 }
 
 BVHNode* BVHBuild::create_leaf_node_split(const BVHRange& range)
 {
-	/* TODO(sergey): Reserve some elementsin arrays by default. */
-	vector<int> p_type[PRIMITIVE_NUM_TOTAL];
-	vector<int> p_index[PRIMITIVE_NUM_TOTAL];
-	vector<int> p_object[PRIMITIVE_NUM_TOTAL];
+#define MAX_LEAF_SIZE 8
+	int p_num[PRIMITIVE_NUM_TOTAL] = {0};
+	int p_type[PRIMITIVE_NUM_TOTAL][MAX_LEAF_SIZE];
+	int p_index[PRIMITIVE_NUM_TOTAL][MAX_LEAF_SIZE];
+	int p_object[PRIMITIVE_NUM_TOTAL][MAX_LEAF_SIZE];
+	uint visibility[PRIMITIVE_NUM_TOTAL] = {0};
+	/* NOTE: Keep initializtion in sync with actual number of primitives. */
 	BoundBox bounds[PRIMITIVE_NUM_TOTAL] = {BoundBox::empty,
 	                                        BoundBox::empty,
 	                                        BoundBox::empty,
 	                                        BoundBox::empty};
 	int ob_num = 0;
-	uint visibility[PRIMITIVE_NUM_TOTAL] = {0};
 
 	/* Fill in per-type type/index array. */
 	for(int i = 0; i < range.size(); i++) {
 		BVHReference& ref = references[range.start() + i];
 		if(ref.prim_index() != -1) {
 			int type_index = bitscan(ref.prim_type() & PRIMITIVE_ALL);
-			p_type[type_index].push_back(ref.prim_type());
-			p_index[type_index].push_back(ref.prim_index());
-			p_object[type_index].push_back(ref.prim_object());
+			int idx = p_num[type_index];
+			p_type[type_index][idx] = ref.prim_type();
+			p_index[type_index][idx] = ref.prim_index();
+			p_object[type_index][idx] = ref.prim_object();
+			++p_num[type_index];
 
 			bounds[type_index].grow(ref.bounds());
 			visibility[type_index] |= objects[ref.prim_object()]->visibility;
@@ -512,15 +515,16 @@ BVHNode* BVHBuild::create_leaf_node_split(const BVHRange& range)
 	int num_leaves = 0;
 	int start = range.start();
 	for(int i = 0; i < PRIMITIVE_NUM_TOTAL; ++i) {
-		if(p_type[i].size()) {
+		if(p_num[i] != 0) {
 			leaves[num_leaves] = create_primitive_leaf_node(p_type[i],
 			                                                p_index[i],
 			                                                p_object[i],
 			                                                bounds[i],
 			                                                visibility[i],
-			                                                start);
+			                                                start,
+			                                                p_num[i]);
 			++num_leaves;
-			start += p_type[i].size();
+			start += p_num[i];
 		}
 	}
 
@@ -557,6 +561,7 @@ BVHNode* BVHBuild::create_leaf_node_split(const BVHRange& range)
 		BVHNode *inner_b = new InnerNode(inner_bounds_b, leaves[2], leaves[3]);
 		return new InnerNode(range.bounds(), inner_a, inner_b);
 	}
+#undef AMX_LEAF_SIZE
 }
 
 BVHNode* BVHBuild::create_leaf_node(const BVHRange& range)
diff --git a/intern/cycles/bvh/bvh_build.h b/intern/cycles/bvh/bvh_build.h
index 75c64be..1e629fb 100644
--- a/intern/cycles/bvh/bvh_build.h
+++ b/intern/cycles/bvh/bvh_build.h
@@ -72,12 +72,13 @@ protected:
 
 	/* Leaf node type splitting. */
 	BVHNode *create_leaf_node_split(const BVHRange& range);
-	BVHNode *create_primitive_leaf_node(const vector<int>& p_type,
-	                                    const vector<int>& p_index,
-	                                    const vector<int>& p_object,
+	BVHNode *create_primitive_leaf_node(const int *p_type,
+	                                    const int *p_index,
+	                                    const int *p_object,
 	                                    const BoundBox& bounds,
 	                                    uint visibility,
-	                                    int start);
+	                                    int start,
+	                                    int nun);
 
 	bool range_within_max_leaf_size(const BVHRange& range);




More information about the Bf-blender-cvs mailing list