From fa46f5a2897d7cd3731271b3cf7e2d619643d73a Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Thu, 22 Jan 2015 14:56:00 +0500 Subject: [PATCH] Fix T43357: Cycles crash with spatial splits after recent changes When doing BVH leaf node split we can't rely on leaf size limit from BVH parameters in case there's spatial split enabled. This commit basically reverts previous optimization change here which used stack-allocated memory and uses heap-allocated vector now. It's possible to boost this code up again by using own allocator. --- intern/cycles/bvh/bvh_build.cpp | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/intern/cycles/bvh/bvh_build.cpp b/intern/cycles/bvh/bvh_build.cpp index e248040301a..4ce8f787169 100644 --- a/intern/cycles/bvh/bvh_build.cpp +++ b/intern/cycles/bvh/bvh_build.cpp @@ -501,11 +501,12 @@ BVHNode *BVHBuild::create_primitive_leaf_node(const int *p_type, BVHNode* BVHBuild::create_leaf_node(const BVHRange& range) { -#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]; + /* TODO(sergey): Consider writing own allocator which would + * not do heap allocation if number of elements is relatively small. + */ + vector p_type[PRIMITIVE_NUM_TOTAL]; + vector p_index[PRIMITIVE_NUM_TOTAL]; + vector p_object[PRIMITIVE_NUM_TOTAL]; uint visibility[PRIMITIVE_NUM_TOTAL] = {0}; /* NOTE: Keep initializtion in sync with actual number of primitives. */ BoundBox bounds[PRIMITIVE_NUM_TOTAL] = {BoundBox::empty, @@ -519,11 +520,9 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range) BVHReference& ref = references[range.start() + i]; if(ref.prim_index() != -1) { int type_index = bitscan(ref.prim_type() & PRIMITIVE_ALL); - 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]; + 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()); bounds[type_index].grow(ref.bounds()); visibility[type_index] |= objects[ref.prim_object()]->visibility; @@ -541,16 +540,19 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range) int num_leaves = 0; int start = range.start(); for(int i = 0; i < PRIMITIVE_NUM_TOTAL; ++i) { - if(p_num[i] != 0) { - leaves[num_leaves] = create_primitive_leaf_node(p_type[i], - p_index[i], - p_object[i], + int num = (int)p_type[i].size(); + if(num != 0) { + assert(p_type[i].size() == p_index[i].size()); + assert(p_type[i].size() == p_object[i].size()); + leaves[num_leaves] = create_primitive_leaf_node(&p_type[i][0], + &p_index[i][0], + &p_object[i][0], bounds[i], visibility[i], start, - p_num[i]); + num); ++num_leaves; - start += p_num[i]; + start += num; } } @@ -587,7 +589,6 @@ BVHNode* BVHBuild::create_leaf_node(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 } /* Tree Rotations */