Cycles: Don't calculate primitive time if BVH motion steps are not used
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 15 Feb 2017 11:59:31 +0000 (12:59 +0100)
committerSergey Sharybin <sergey.vfx@gmail.com>
Wed, 15 Feb 2017 11:59:31 +0000 (12:59 +0100)
Solves memory regression by the default configuration.

intern/cycles/bvh/bvh.cpp
intern/cycles/bvh/bvh_build.cpp
intern/cycles/bvh/bvh_build.h
intern/cycles/kernel/geom/geom_curve.h

index 7e91140709cb3c23d5084c46cd99ed2d13497107..1fb2f371a0f57b0e6127634d9f47915dbee935f7 100644 (file)
@@ -253,11 +253,14 @@ void BVH::pack_instances(size_t nodes_size, size_t leaf_nodes_size)
        pack.prim_visibility.resize(prim_index_size);
        pack.prim_tri_verts.resize(prim_tri_verts_size);
        pack.prim_tri_index.resize(prim_index_size);
-       pack.prim_time.resize(prim_index_size);
        pack.nodes.resize(nodes_size);
        pack.leaf_nodes.resize(leaf_nodes_size);
        pack.object_node.resize(objects.size());
 
+       if(params.num_motion_curve_steps > 0 || params.num_motion_triangle_steps > 0) {
+               pack.prim_time.resize(prim_index_size);
+       }
+
        int *pack_prim_index = (pack.prim_index.size())? &pack.prim_index[0]: NULL;
        int *pack_prim_type = (pack.prim_type.size())? &pack.prim_type[0]: NULL;
        int *pack_prim_object = (pack.prim_object.size())? &pack.prim_object[0]: NULL;
@@ -312,7 +315,7 @@ void BVH::pack_instances(size_t nodes_size, size_t leaf_nodes_size)
                        int *bvh_prim_type = &bvh->pack.prim_type[0];
                        uint *bvh_prim_visibility = &bvh->pack.prim_visibility[0];
                        uint *bvh_prim_tri_index = &bvh->pack.prim_tri_index[0];
-                       float2 *bvh_prim_time = &bvh->pack.prim_time[0];
+                       float2 *bvh_prim_time = bvh->pack.prim_time.size()? &bvh->pack.prim_time[0]: NULL;
 
                        for(size_t i = 0; i < bvh_prim_index_size; i++) {
                                if(bvh->pack.prim_type[i] & PRIMITIVE_ALL_CURVE) {
@@ -328,7 +331,9 @@ void BVH::pack_instances(size_t nodes_size, size_t leaf_nodes_size)
                                pack_prim_type[pack_prim_index_offset] = bvh_prim_type[i];
                                pack_prim_visibility[pack_prim_index_offset] = bvh_prim_visibility[i];
                                pack_prim_object[pack_prim_index_offset] = 0;  // unused for instances
-                               pack_prim_time[pack_prim_index_offset] = bvh_prim_time[i];
+                               if(bvh_prim_time != NULL) {
+                                       pack_prim_time[pack_prim_index_offset] = bvh_prim_time[i];
+                               }
                                pack_prim_index_offset++;
                        }
                }
index 06dfe5e439b985618a656235eac2eeae604f395b..fcbc50f4f6fe3074733709005a3fa457af8f7f3e 100644 (file)
@@ -467,6 +467,9 @@ BVHNode* BVHBuild::run()
        }
        spatial_free_index = 0;
 
+       need_prim_time = params.num_motion_curve_steps > 0 ||
+                        params.num_motion_triangle_steps > 0;
+
        /* init progress updates */
        double build_start_time;
        build_start_time = progress_start_time = time_dt();
@@ -477,7 +480,12 @@ BVHNode* BVHBuild::run()
        prim_type.resize(references.size());
        prim_index.resize(references.size());
        prim_object.resize(references.size());
-       prim_time.resize(references.size());
+       if(need_prim_time) {
+               prim_time.resize(references.size());
+       }
+       else {
+               prim_time.resize(0);
+       }
 
        /* build recursively */
        BVHNode *rootnode;
@@ -852,7 +860,9 @@ BVHNode *BVHBuild::create_object_leaf_nodes(const BVHReference *ref, int start,
                prim_type[start] = ref->prim_type();
                prim_index[start] = ref->prim_index();
                prim_object[start] = ref->prim_object();
-               prim_time[start] = make_float2(ref->time_from(), ref->time_to());
+               if(need_prim_time) {
+                       prim_time[start] = make_float2(ref->time_from(), ref->time_to());
+               }
 
                uint visibility = objects[ref->prim_object()]->visibility;
                BVHNode *leaf_node =  new LeafNode(ref->bounds(), visibility, start, start+1);
@@ -958,7 +968,9 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range,
        local_prim_type.resize(num_new_prims);
        local_prim_index.resize(num_new_prims);
        local_prim_object.resize(num_new_prims);
-       local_prim_time.resize(num_new_prims);
+       if(need_prim_time) {
+               local_prim_time.resize(num_new_prims);
+       }
        for(int i = 0; i < PRIMITIVE_NUM_TOTAL; ++i) {
                int num = (int)p_type[i].size();
                if(num != 0) {
@@ -971,7 +983,9 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range,
                                local_prim_type[index] = p_type[i][j];
                                local_prim_index[index] = p_index[i][j];
                                local_prim_object[index] = p_object[i][j];
-                               local_prim_time[index] = p_time[i][j];
+                               if(need_prim_time) {
+                                       local_prim_time[index] = p_time[i][j];
+                               }
                                if(params.use_unaligned_nodes && !alignment_found) {
                                        alignment_found =
                                                unaligned_heuristic.compute_aligned_space(p_ref[i][j],
@@ -1038,13 +1052,17 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range,
                                prim_type.reserve(reserve);
                                prim_index.reserve(reserve);
                                prim_object.reserve(reserve);
-                               prim_time.reserve(reserve);
+                               if(need_prim_time) {
+                                       prim_time.reserve(reserve);
+                               }
                        }
 
                        prim_type.resize(range_end);
                        prim_index.resize(range_end);
                        prim_object.resize(range_end);
-                       prim_time.resize(range_end);
+                       if(need_prim_time) {
+                               prim_time.resize(range_end);
+                       }
                }
                spatial_spin_lock.unlock();
 
@@ -1053,7 +1071,9 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range,
                        memcpy(&prim_type[start_index], &local_prim_type[0], new_leaf_data_size);
                        memcpy(&prim_index[start_index], &local_prim_index[0], new_leaf_data_size);
                        memcpy(&prim_object[start_index], &local_prim_object[0], new_leaf_data_size);
-                       memcpy(&prim_time[start_index], &local_prim_time[0], sizeof(float2)*num_new_leaf_data);
+                       if(need_prim_time) {
+                               memcpy(&prim_time[start_index], &local_prim_time[0], sizeof(float2)*num_new_leaf_data);
+                       }
                }
        }
        else {
@@ -1066,7 +1086,9 @@ BVHNode* BVHBuild::create_leaf_node(const BVHRange& range,
                        memcpy(&prim_type[start_index], &local_prim_type[0], new_leaf_data_size);
                        memcpy(&prim_index[start_index], &local_prim_index[0], new_leaf_data_size);
                        memcpy(&prim_object[start_index], &local_prim_object[0], new_leaf_data_size);
-                       memcpy(&prim_time[start_index], &local_prim_time[0], sizeof(float2)*num_new_leaf_data);
+                       if(need_prim_time) {
+                               memcpy(&prim_time[start_index], &local_prim_time[0], sizeof(float2)*num_new_leaf_data);
+                       }
                }
        }
 
index 19af9c62ecd16bb8d09d9c9c54eb07c3cc7143d7..430efc3e0f63c11991d243345a510a85ab71c7b2 100644 (file)
@@ -115,6 +115,8 @@ protected:
        array<int>& prim_object;
        array<float2>& prim_time;
 
+       bool need_prim_time;
+
        /* Build parameters. */
        BVHParams params;
 
index c8749545cdd2cd4bb3bf056e34020e500e6fc12d..712b67a1b5547b842e7bb889ebf9665fe17e5dc8 100644 (file)
@@ -231,7 +231,7 @@ ccl_device_forceinline bool bvh_cardinal_curve_intersect(KernelGlobals *kg, Inte
 {
        const bool is_curve_primitive = (type & PRIMITIVE_CURVE);
 
-       if(!is_curve_primitive) {
+       if(!is_curve_primitive && kernel_data.bvh.use_bvh_steps) {
                const float2 prim_time = kernel_tex_fetch(__prim_time, curveAddr);
                if(time < prim_time.x || time > prim_time.y) {
                        return false;
@@ -700,7 +700,7 @@ ccl_device_forceinline bool bvh_curve_intersect(KernelGlobals *kg, Intersection
 
        const bool is_curve_primitive = (type & PRIMITIVE_CURVE);
 
-       if(!is_curve_primitive) {
+       if(!is_curve_primitive && kernel_data.bvh.use_bvh_steps) {
                const float2 prim_time = kernel_tex_fetch(__prim_time, curveAddr);
                if(time < prim_time.x || time > prim_time.y) {
                        return false;