[Bf-blender-cvs] [d101a25b2f9] temp-3d-texturing-brush-b: Tag triangles for painting gives additional performance.

Jeroen Bakker noreply at git.blender.org
Mon Mar 14 15:30:12 CET 2022


Commit: d101a25b2f9132859d15e69d84952c197ca47183
Author: Jeroen Bakker
Date:   Mon Mar 14 15:30:01 2022 +0100
Branches: temp-3d-texturing-brush-b
https://developer.blender.org/rBd101a25b2f9132859d15e69d84952c197ca47183

Tag triangles for painting gives additional performance.

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

M	source/blender/editors/sculpt_paint/sculpt_texture_paint_d.cc
M	source/blender/editors/sculpt_paint/sculpt_texture_paint_pixel_extraction_d.cc

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

diff --git a/source/blender/editors/sculpt_paint/sculpt_texture_paint_d.cc b/source/blender/editors/sculpt_paint/sculpt_texture_paint_d.cc
index b28332b3d16..17d1895baf7 100644
--- a/source/blender/editors/sculpt_paint/sculpt_texture_paint_d.cc
+++ b/source/blender/editors/sculpt_paint/sculpt_texture_paint_d.cc
@@ -41,12 +41,11 @@ namespace blender::ed::sculpt_paint::texture_paint {
 namespace painting {
 
 static Pixel get_start_pixel(const PixelsPackage &encoded_pixels,
-                             const Vector<Triangle> &triangles,
+                             const Triangle &triangle,
                              const MVert *mvert,
                              const MLoopUV *ldata_uv)
 {
   Pixel result;
-  const Triangle &triangle = triangles[encoded_pixels.triangle_index];
   const float3 weights = encoded_pixels.start_edge_coord;
   interp_v3_v3v3v3(result.pos,
                    mvert[triangle.vert_indices[0]].co,
@@ -63,7 +62,7 @@ static Pixel get_start_pixel(const PixelsPackage &encoded_pixels,
 }
 
 static Pixel get_delta_pixel(const PixelsPackage &encoded_pixels,
-                             const Vector<Triangle> &triangles,
+                             const Triangle &triangle,
                              const Pixel &start_pixel,
                              const MVert *mvert,
                              const MLoopUV *ldata_uv
@@ -71,7 +70,6 @@ static Pixel get_delta_pixel(const PixelsPackage &encoded_pixels,
 )
 {
   Pixel result;
-  const Triangle &triangle = triangles[encoded_pixels.triangle_index];
   const float3 weights = encoded_pixels.start_edge_coord + triangle.add_edge_coord_x;
   interp_v3_v3v3v3(result.pos,
                    mvert[triangle.vert_indices[0]].co,
@@ -121,16 +119,41 @@ static void do_task_cb_ex(void *__restrict userdata,
   Mesh *mesh = static_cast<Mesh *>(ob->data);
   MLoopUV *ldata_uv = static_cast<MLoopUV *>(CustomData_get_layer(&mesh->ldata, CD_MLOOPUV));
 
+  std::vector<bool> vert_brush_test_results(mesh->totvert);
+
+  PBVHVertexIter vd;
+  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
+    vert_brush_test_results[vd.index] = sculpt_brush_test_sq_fn(&test, vd.co);
+  }
+  BKE_pbvh_vertex_iter_end;
+
+  /* Propagate vertex brush test to triangle. This should be extended with brush overlapping edges
+   * and faces only. */
+  std::vector<bool> triangle_brush_test_results(node_data->triangles.size());
+  int triangle_index = 0;
+  for (Triangle &triangle : node_data->triangles) {
+    for (int i = 0; i < 3; i++) {
+      triangle_brush_test_results[triangle_index] =
+          triangle_brush_test_results[triangle_index] ||
+          vert_brush_test_results[triangle.vert_indices[i]];
+    }
+    triangle_index += 1;
+  }
+
   const float brush_strength = ss->cache->bstrength;
+  int packages_clipped = 0;
 
   for (PixelsPackage &encoded_pixels : node_data->encoded_pixels) {
+    if (!triangle_brush_test_results[encoded_pixels.triangle_index]) {
+      packages_clipped += 1;
+      continue;
+    }
     Triangle &triangle = node_data->triangles[encoded_pixels.triangle_index];
     int pixel_offset = encoded_pixels.start_image_coordinate.y * image_buffer->x +
                        encoded_pixels.start_image_coordinate.x;
     float3 edge_coord = encoded_pixels.start_edge_coord;
-    Pixel pixel = get_start_pixel(encoded_pixels, node_data->triangles, mvert, ldata_uv);
-    const Pixel add_pixel = get_delta_pixel(
-        encoded_pixels, node_data->triangles, pixel, mvert, ldata_uv);
+    Pixel pixel = get_start_pixel(encoded_pixels, triangle, mvert, ldata_uv);
+    const Pixel add_pixel = get_delta_pixel(encoded_pixels, triangle, pixel, mvert, ldata_uv);
     bool pixels_painted = false;
     for (int x = 0; x < encoded_pixels.num_pixels; x++) {
       if (!sculpt_brush_test_sq_fn(&test, pixel.pos)) {
@@ -150,8 +173,8 @@ static void do_task_cb_ex(void *__restrict userdata,
       pixels_painted = true;
 
       edge_coord += triangle.add_edge_coord_x;
-      pixel_offset++;
       add(pixel, add_pixel);
+      pixel_offset++;
     }
 
     if (pixels_painted) {
@@ -163,6 +186,7 @@ static void do_task_cb_ex(void *__restrict userdata,
       node_data->flags.dirty = true;
     }
   }
+  printf("%d of %ld pixel packages clipped\n", packages_clipped, node_data->encoded_pixels.size());
 }
 }  // namespace painting
 
diff --git a/source/blender/editors/sculpt_paint/sculpt_texture_paint_pixel_extraction_d.cc b/source/blender/editors/sculpt_paint/sculpt_texture_paint_pixel_extraction_d.cc
index a552de4bd33..3d7cf37241d 100644
--- a/source/blender/editors/sculpt_paint/sculpt_texture_paint_pixel_extraction_d.cc
+++ b/source/blender/editors/sculpt_paint/sculpt_texture_paint_pixel_extraction_d.cc
@@ -45,54 +45,14 @@ static float3 packed_edge_coordinate(const float2 v1,
                                      const float2 v3,
                                      const float2 co)
 {
-#if 0
-  float2 v13 = v3 - v1;
-  float2 v12 = v2 - v1;
-  float2 isect_point;
-  float2 tmp;
-
-  float2 point_1 = co - v13;
-  isect_line_line_v2_point(v1, v2, co, point_1, isect_point);
-  print_v2_id(v1);
-  print_v2_id(v2);
-  print_v2_id(co);
-  print_v2_id(point_1);
-  print_v2_id(isect_point);
-  float d1 = closest_to_line_v2(tmp, isect_point, v1, v2);
-  printf("d: %f\n")
-  float2 point_2 = co - v12;
-  isect_line_line_v2_point(v1, v3, co, point_2, isect_point);
-  print_v2_id(v1);
-  print_v2_id(v3);
-  print_v2_id(co);
-  print_v2_id(point_2);
-  print_v2_id(isect_point);
-  float d2 = closest_to_line_v2(tmp, isect_point, v1, v3);
-  return float2(d1, d2);
-#else
   float3 weights;
   barycentric_weights_v2(v1, v2, v3, co, weights);
   return weights;
-#endif
 }
 
 static bool is_inside_triangle(const float3 co)
 {
-#if 0
-  if (co.x < 0.0 || co.x > 1.0) {
-    return false;
-  }
-  if (co.y < 0.0 || co.y > 1.0) {
-    return false;
-  }
-  const float v = co.x + co.y;
-  if (v > 1.0) {
-    return false;
-  }
-  return true;
-#else
   return barycentric_inside_triangle_v2(co);
-#endif
 }
 
 static void init(Object *ob, int totnode, PBVHNode **nodes)
@@ -153,10 +113,6 @@ static void init(Object *ob, int totnode, PBVHNode **nodes)
               ldata_uv[triangle.loop_indices[2]].uv,
           };
 
-          // print_v2_id(uvs[0]);
-          // print_v2_id(uvs[1]);
-          // print_v2_id(uvs[2]);
-
           const float minv = min_fff(uvs[0].y, uvs[1].y, uvs[2].y);
           const int miny = floor(minv * image_buffer->y);
           const float maxv = max_fff(uvs[0].y, uvs[1].y, uvs[2].y);
@@ -168,8 +124,6 @@ static void init(Object *ob, int totnode, PBVHNode **nodes)
           const float add_u = 1.0 / image_buffer->x;
           const float add_v = 1.0 / image_buffer->y;
 
-          // printf("(%d, %d) - (%d, %d)\n", minx, miny, maxx, maxy);
-
           float2 min_uv(minu, minv);
           float3 start_edge_coord = packed_edge_coordinate(uvs[0], uvs[1], uvs[2], min_uv);
           float3 add_edge_coord_x = packed_edge_coordinate(
@@ -178,9 +132,6 @@ static void init(Object *ob, int totnode, PBVHNode **nodes)
           float3 add_edge_coord_y = packed_edge_coordinate(
                                         uvs[0], uvs[1], uvs[2], min_uv + float2(0.0, add_v)) -
                                     start_edge_coord;
-          // print_v3_id(start_edge_coord);
-          // print_v3_id(add_edge_coord_x);
-          // print_v3_id(add_edge_coord_y);
 
           triangle.add_edge_coord_x = add_edge_coord_x;
           int triangle_index = node_data->triangles.size();
@@ -196,8 +147,6 @@ static void init(Object *ob, int totnode, PBVHNode **nodes)
             int end_x = -1;
             int x;
             for (x = minx; x < maxx; x++) {
-              // printf("(%d, %d)", x, y);
-              // print_v2_id(edge_coord);
               if (is_inside_triangle(edge_coord)) {
                 start_x = x;
                 break;
@@ -207,8 +156,6 @@ static void init(Object *ob, int totnode, PBVHNode **nodes)
             edge_coord += add_edge_coord_x;
             x += 1;
             for (; x < maxx; x++) {
-              // printf("(%d, %d)", x, y);
-              // print_v2_id(edge_coord);
               if (!is_inside_triangle(edge_coord)) {
                 break;
               }
@@ -228,24 +175,12 @@ static void init(Object *ob, int totnode, PBVHNode **nodes)
             package.num_pixels = num_pixels;
             node_data->encoded_pixels.append(package);
             num_encoded_pixels += num_pixels;
-            // printf("x: %d y: %d, cx: %f, cy: %f, len: %d\n",
-            //  package.start_image_coordinate.x,
-            //  package.start_image_coordinate.y,
-            //  package.start_edge_coord.x,
-            //  package.start_edge_coord.y,
-            //  package.num_pixels);
             num_packages += 1;
           }
-          // printf("new pixel packages created: %d\n", num_packages);
         }
       }
     }
     BKE_pbvh_vertex_iter_end;
-
-    // printf(" - encoded %d pixels into %lu bytes\n",
-    //  num_encoded_pixels,
-    //  node_data->encoded_pixels.size() * sizeof(PixelsPackage) +
-    //  node_data->triangles.size() * sizeof(Triangle));
   }
 
   {



More information about the Bf-blender-cvs mailing list