DRW: Make shaded batch validation more correct
authorClément Foucault <foucault.clem@gmail.com>
Thu, 18 Apr 2019 15:25:04 +0000 (17:25 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Fri, 19 Apr 2019 13:19:43 +0000 (15:19 +0200)
This is in order to support garbage collection of unused Custom data layer uploaded to the GPU.

Actual Garbage Collection is not added by this commit.

source/blender/draw/intern/draw_cache_impl_mesh.c
source/blender/makesrna/intern/rna_space.c

index 630cbd2..6b43fbe 100644 (file)
@@ -4198,16 +4198,16 @@ GPUBatch **DRW_mesh_batch_cache_get_surface_shaded(Mesh *me,
 
   BLI_assert(gpumat_array_len == cache->mat_len);
 
-  bool cd_overlap = mesh_cd_layers_type_overlap(cache->cd_used, cd_needed);
-  if (!cd_overlap) {
-    mesh_cd_layers_type_merge(&cache->cd_needed, cd_needed);
+  mesh_cd_layers_type_merge(&cache->cd_needed, cd_needed);
 
+  if (!mesh_cd_layers_type_overlap(cache->cd_used, cd_needed)) {
     mesh_cd_extract_auto_layers_names_and_srgb(me,
                                                cache->cd_needed,
                                                &cache->auto_layer_names,
                                                &cache->auto_layer_is_srgb,
                                                &cache->auto_layer_len);
   }
+
   if (auto_layer_names) {
     *auto_layer_names = cache->auto_layer_names;
     *auto_layer_is_srgb = cache->auto_layer_is_srgb;
@@ -4733,6 +4733,18 @@ void DRW_mesh_batch_cache_create_requested(
     drw_mesh_weight_state_clear(&wstate);
   }
 
+  /* Optimization : Only create orco layer if mesh is deformed. */
+  if (cache->cd_needed.orco != 0) {
+    CustomData *cd_vdata = (me->edit_mesh) ? &me->edit_mesh->bm->vdata : &me->vdata;
+    if (CustomData_get_layer(cd_vdata, CD_ORCO) != NULL && ob->modifiers.first != NULL) {
+      /* Orco layer is needed. */
+    }
+    else if (cache->cd_needed.tan_orco == 0) {
+      /* Skip orco calculation if not needed by tangent generation. */
+      cache->cd_needed.orco = 0;
+    }
+  }
+
   /* Verify that all surface batches have needed attribute layers. */
   /* TODO(fclem): We could be a bit smarter here and only do it per material. */
   bool cd_overlap = mesh_cd_layers_type_overlap(cache->cd_used, cache->cd_needed);
@@ -4929,15 +4941,7 @@ void DRW_mesh_batch_cache_create_requested(
         DRW_vbo_request(cache->surf_per_mat[i], &cache->ordered.loop_vcol);
       }
       if (cache->cd_used.orco != 0) {
-        /* OPTI : Only do that if there is modifiers that modify orcos. */
-        CustomData *cd_vdata = (me->edit_mesh) ? &me->edit_mesh->bm->vdata : &me->vdata;
-        if (CustomData_get_layer(cd_vdata, CD_ORCO) != NULL && ob->modifiers.first != NULL) {
-          DRW_vbo_request(cache->surf_per_mat[i], &cache->ordered.loop_orco);
-        }
-        else if (cache->cd_used.tan_orco == 0) {
-          /* Skip orco calculation if not needed by tangent generation. */
-          cache->cd_used.orco = 0;
-        }
+        DRW_vbo_request(cache->surf_per_mat[i], &cache->ordered.loop_orco);
       }
     }
   }
index 984536b..ce6a2af 100644 (file)
@@ -914,17 +914,6 @@ static void rna_3DViewShading_type_update(Main *bmain, Scene *scene, PointerRNA
     return;
   }
 
-  for (Material *ma = bmain->materials.first; ma; ma = ma->id.next) {
-    /* XXX Dependency graph does not support CD mask tracking,
-     * so we trigger  materials shading for until it's properly supported.
-     * This is to ensure material batches are all recreated when switching
-     * shading type. In the future DEG should replace this and just tag
-     * the meshes itself.
-     * This hack just tag BKE_MESH_BATCH_DIRTY_SHADING for every mesh that
-     * have a material. (see T55059) */
-    DEG_id_tag_update(&ma->id, ID_RECALC_SHADING);
-  }
-
   View3DShading *shading = ptr->data;
   if (shading->type == OB_MATERIAL ||
       (shading->type == OB_RENDER &&