Code refactor: reduce number of loops over meshes.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 10 Mar 2018 04:45:49 +0000 (05:45 +0100)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 10 Mar 2018 05:27:19 +0000 (06:27 +0100)
intern/cycles/render/mesh.cpp

index 9166d3b..d6c37c2 100644 (file)
@@ -2057,7 +2057,9 @@ void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scen
 
        VLOG(1) << "Total " << scene->meshes.size() << " meshes.";
 
-       /* Update normals. */
+       bool true_displacement_used = false;
+       size_t total_tess_needed = 0;
+
        foreach(Mesh *mesh, scene->meshes) {
                foreach(Shader *shader, mesh->used_shaders) {
                        if(shader->need_update_mesh)
@@ -2065,6 +2067,7 @@ void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scen
                }
 
                if(mesh->need_update) {
+                       /* Update normals. */
                        mesh->add_face_normals();
                        mesh->add_vertex_normals();
 
@@ -2072,57 +2075,53 @@ void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scen
                                mesh->add_undisplaced();
                        }
 
+                       /* Test if we need tesselation. */
+                       if(mesh->subdivision_type != Mesh::SUBDIVISION_NONE &&
+                          mesh->num_subd_verts == 0 &&
+                          mesh->subd_params)
+                       {
+                               total_tess_needed++;
+                       }
+
+                       /* Test if we need displacement. */
+                       if(mesh->has_true_displacement()) {
+                               true_displacement_used = true;
+                       }
+
                        if(progress.get_cancel()) return;
                }
        }
 
        /* Tessellate meshes that are using subdivision */
-       size_t total_tess_needed = 0;
-       foreach(Mesh *mesh, scene->meshes) {
-               if(mesh->need_update &&
-                  mesh->subdivision_type != Mesh::SUBDIVISION_NONE &&
-                  mesh->num_subd_verts == 0 &&
-                  mesh->subd_params)
-               {
-                       total_tess_needed++;
-               }
-       }
+       if(total_tess_needed) {
+               size_t i = 0;
+               foreach(Mesh *mesh, scene->meshes) {
+                       if(mesh->need_update &&
+                          mesh->subdivision_type != Mesh::SUBDIVISION_NONE &&
+                          mesh->num_subd_verts == 0 &&
+                          mesh->subd_params)
+                       {
+                               string msg = "Tessellating ";
+                               if(mesh->name == "")
+                                       msg += string_printf("%u/%u", (uint)(i+1), (uint)total_tess_needed);
+                               else
+                                       msg += string_printf("%s %u/%u", mesh->name.c_str(), (uint)(i+1), (uint)total_tess_needed);
 
-       size_t i = 0;
-       foreach(Mesh *mesh, scene->meshes) {
-               if(mesh->need_update &&
-                  mesh->subdivision_type != Mesh::SUBDIVISION_NONE &&
-                  mesh->num_subd_verts == 0 &&
-                  mesh->subd_params)
-               {
-                       string msg = "Tessellating ";
-                       if(mesh->name == "")
-                               msg += string_printf("%u/%u", (uint)(i+1), (uint)total_tess_needed);
-                       else
-                               msg += string_printf("%s %u/%u", mesh->name.c_str(), (uint)(i+1), (uint)total_tess_needed);
+                               progress.set_status("Updating Mesh", msg);
 
-                       progress.set_status("Updating Mesh", msg);
+                               DiagSplit dsplit(*mesh->subd_params);
+                               mesh->tessellate(&dsplit);
 
-                       DiagSplit dsplit(*mesh->subd_params);
-                       mesh->tessellate(&dsplit);
+                               i++;
 
-                       i++;
+                               if(progress.get_cancel()) return;
+                       }
 
-                       if(progress.get_cancel()) return;
                }
        }
 
        /* Update images needed for true displacement. */
-       bool true_displacement_used = false;
        bool old_need_object_flags_update = false;
-       foreach(Mesh *mesh, scene->meshes) {
-               if(mesh->need_update &&
-                  mesh->has_true_displacement())
-               {
-                       true_displacement_used = true;
-                       break;
-               }
-       }
        if(true_displacement_used) {
                VLOG(1) << "Updating images used for true displacement.";
                device_update_displacement_images(device, scene, progress);
@@ -2148,11 +2147,17 @@ void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scen
 
        /* Update displacement. */
        bool displacement_done = false;
+       size_t num_bvh = 0;
+
        foreach(Mesh *mesh, scene->meshes) {
-               if(mesh->need_update &&
-                  displace(device, dscene, scene, mesh, progress))
-               {
-                       displacement_done = true;
+               if(mesh->need_update) {
+                       if(displace(device, dscene, scene, mesh, progress)) {
+                               displacement_done = true;
+                       }
+
+                       if(mesh->need_build_bvh()) {
+                               num_bvh++;
+                       }
                }
        }
 
@@ -2167,17 +2172,9 @@ void MeshManager::device_update(Device *device, DeviceScene *dscene, Scene *scen
                if(progress.get_cancel()) return;
        }
 
-       /* Update bvh. */
-       size_t num_bvh = 0;
-       foreach(Mesh *mesh, scene->meshes) {
-               if(mesh->need_update && mesh->need_build_bvh()) {
-                       num_bvh++;
-               }
-       }
-
        TaskPool pool;
 
-       i = 0;
+       size_t i = 0;
        foreach(Mesh *mesh, scene->meshes) {
                if(mesh->need_update) {
                        pool.push(function_bind(&Mesh::compute_bvh,