[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29031] branches/soc-2010-jwilkins/source/ blender: Optimizations to sculpt: simplified calc_flatten_center, simplified point/plane projection, removed use of memset/ calloc from performance critical areas, removed creation of undo points from places they weren't needed.

Jason Wilkins Jason.A.Wilkins at gmail.com
Thu May 27 20:37:06 CEST 2010


Revision: 29031
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29031
Author:   jwilkins
Date:     2010-05-27 20:37:06 +0200 (Thu, 27 May 2010)

Log Message:
-----------
Optimizations to sculpt: simplified calc_flatten_center, simplified point/plane projection, removed use of memset/calloc from performance critical areas, removed creation of undo points from places they weren't needed.  Based on profiling the bottleneck is not in sculpt code anymore

Modified Paths:
--------------
    branches/soc-2010-jwilkins/source/blender/blenkernel/intern/brush.c
    branches/soc-2010-jwilkins/source/blender/blenlib/BLI_pbvh.h
    branches/soc-2010-jwilkins/source/blender/editors/sculpt_paint/sculpt.c

Modified: branches/soc-2010-jwilkins/source/blender/blenkernel/intern/brush.c
===================================================================
--- branches/soc-2010-jwilkins/source/blender/blenkernel/intern/brush.c	2010-05-27 18:22:50 UTC (rev 29030)
+++ branches/soc-2010-jwilkins/source/blender/blenkernel/intern/brush.c	2010-05-27 18:37:06 UTC (rev 29031)
@@ -899,9 +899,12 @@
  * used for sculpt only */
 float brush_curve_strength(Brush *br, float p, const float len)
 {
-	if(p >= len)	p= 1.0f;
-	else			p= p/len;
-	return curvemapping_evaluateF(br->curve, 0, p);
+    if(p >= len)
+        p= 1.0f;
+    else
+        p= p/len;
+
+    return curvemapping_evaluateF(br->curve, 0, p);
 }
 
 /* TODO: should probably be unified with BrushPainter stuff? */

Modified: branches/soc-2010-jwilkins/source/blender/blenlib/BLI_pbvh.h
===================================================================
--- branches/soc-2010-jwilkins/source/blender/blenlib/BLI_pbvh.h	2010-05-27 18:22:50 UTC (rev 29030)
+++ branches/soc-2010-jwilkins/source/blender/blenlib/BLI_pbvh.h	2010-05-27 18:37:06 UTC (rev 29031)
@@ -157,7 +157,7 @@
 		struct MVert *verts; \
 		int *grid_indices, totgrid, gridsize, *vert_indices, uniq_verts, totvert; \
 		\
-		memset(&vi, 0, sizeof(PBVHVertexIter)); \
+		vi.grid = 0; /*memset(&vi, 0, sizeof(PBVHVertexIter));*/ \
 		\
 		BLI_pbvh_node_get_grids(bvh, node, &grid_indices, &totgrid, NULL, &gridsize, &grids, NULL); \
 		BLI_pbvh_node_num_verts(bvh, node, &uniq_verts, &totvert); \

Modified: branches/soc-2010-jwilkins/source/blender/editors/sculpt_paint/sculpt.c
===================================================================
--- branches/soc-2010-jwilkins/source/blender/editors/sculpt_paint/sculpt.c	2010-05-27 18:22:50 UTC (rev 29030)
+++ branches/soc-2010-jwilkins/source/blender/editors/sculpt_paint/sculpt.c	2010-05-27 18:37:06 UTC (rev 29031)
@@ -406,66 +406,75 @@
 
 static SculptUndoNode *sculpt_undo_push_node(SculptSession *ss, PBVHNode *node)
 {
-	ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
-	Object *ob= ss->ob;
-	SculptUndoNode *unode;
-	int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
+    ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
+    Object *ob= ss->ob;
+    SculptUndoNode *unode;
+    int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
 
-	/* list is manipulated by multiple threads, so we lock */
-	BLI_lock_thread(LOCK_CUSTOM1);
+    /* list is manipulated by multiple threads, so we lock */
+    BLI_lock_thread(LOCK_CUSTOM1);
 
-	if((unode= sculpt_undo_get_node(ss, node))) {
-		BLI_unlock_thread(LOCK_CUSTOM1);
-		return unode;
-	}
+    if((unode= sculpt_undo_get_node(ss, node))) {
+        BLI_unlock_thread(LOCK_CUSTOM1);
+        return unode;
+    }
 
-	unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
-	strcpy(unode->idname, ob->id.name);
-	unode->node= node;
+    unode= MEM_mallocN(sizeof(SculptUndoNode), "SculptUndoNode");
+    strcpy(unode->idname, ob->id.name);
+    unode->node= node;
 
-	BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
-	BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
-		&maxgrid, &gridsize, NULL, NULL);
+    BLI_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
+    BLI_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
+        &maxgrid, &gridsize, NULL, NULL);
 
-	unode->totvert= totvert;
-	/* we will use this while sculpting, is mapalloc slow to access then? */
-	unode->co= MEM_mapallocN(sizeof(float)*3*allvert, "SculptUndoNode.co");
-	unode->no= MEM_mapallocN(sizeof(short)*3*allvert, "SculptUndoNode.no");
-	undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(short)*3 + sizeof(int))*allvert);
-	BLI_addtail(lb, unode);
+    unode->totvert= totvert;
+    /* we will use this while sculpting, is mapalloc slow to access then? */
+    unode->co= MEM_mapallocN(sizeof(float)*3*allvert, "SculptUndoNode.co");
+    unode->no= MEM_mapallocN(sizeof(short)*3*allvert, "SculptUndoNode.no");
+    undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(short)*3 + sizeof(int))*allvert);
+    BLI_addtail(lb, unode);
 
-	if(maxgrid) {
-		/* multires */
-		unode->maxgrid= maxgrid;
-		unode->totgrid= totgrid;
-		unode->gridsize= gridsize;
-		unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids");
-	}
-	else {
-		/* regular mesh */
-		unode->maxvert= ss->totvert;
-		unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index");
-	}
+    if(maxgrid) {
+        /* multires */
+        unode->maxgrid= maxgrid;
+        unode->totgrid= totgrid;
+        unode->gridsize= gridsize;
+        unode->grids= MEM_mapallocN(sizeof(int)*totgrid, "SculptUndoNode.grids");
 
-	BLI_unlock_thread(LOCK_CUSTOM1);
+        unode->maxvert = 0;
+        unode->index   = 0;
+    }
+    else {
+        /* regular mesh */
+        unode->maxvert= ss->totvert;
+        unode->index= MEM_mapallocN(sizeof(int)*allvert, "SculptUndoNode.index");
 
-	/* copy threaded, hopefully this is the performance critical part */
-	{
-		PBVHVertexIter vd;
+        unode->maxgrid=  0;
+        unode->totgrid=  0;
+        unode->gridsize= 0;
+        unode->grids=    0;
+    }
 
-		BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
-			copy_v3_v3(unode->co[vd.i], vd.co);
-			if(vd.no) VECCOPY(unode->no[vd.i], vd.no)
-			else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
-			if(vd.vert_indices) unode->index[vd.i]= vd.vert_indices[vd.i];
-		}
-		BLI_pbvh_vertex_iter_end;
-	}
+    BLI_unlock_thread(LOCK_CUSTOM1);
 
-	if(unode->grids)
-		memcpy(unode->grids, grids, sizeof(int)*totgrid);
-	
-	return unode;
+    /* copy threaded, hopefully this is the performance critical part */
+    {
+        PBVHVertexIter vd;
+
+        BLI_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
+            copy_v3_v3(unode->co[vd.i], vd.co);
+            if(vd.no) VECCOPY(unode->no[vd.i], vd.no)
+            else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
+            if(vd.vert_indices) unode->index[vd.i]= vd.vert_indices[vd.i];
+        }
+        BLI_pbvh_vertex_iter_end;
+    }
+
+    if(unode->grids) memcpy(unode->grids, grids, sizeof(int)*totgrid);
+
+    unode->layer_disp= 0;
+
+    return unode;
 }
 
 static void sculpt_undo_push_begin(SculptSession *ss, char *name)
@@ -533,6 +542,32 @@
 	return 0;
 }
 
+static int sculpt_brush_test_sq(SculptBrushTest *test, float co[3])
+{
+	float distsq, delta[3];
+
+	sub_v3_v3v3(delta, co, test->location);
+	distsq = INPR(delta, delta);
+
+	if(distsq < test->radius_squared) {
+		test->dist = distsq;
+		return 1;
+	}
+        else {
+        	return 0;
+        }
+}
+
+static int sculpt_brush_test_fast(SculptBrushTest *test, float co[3])
+{
+	float distsq, delta[3];
+
+	sub_v3_v3v3(delta, co, test->location);
+	distsq = INPR(delta, delta);
+
+	return distsq < test->radius_squared;
+}
+
 /* ===== Sculpting =====
  *
  */
@@ -781,24 +816,24 @@
 	copy_v3_v3(out_dir, cache->view_normal_symmetry);
 
 	/* threaded loop over nodes */
-	#pragma omp parallel for private(n) schedule(static)
+	//#pragma omp parallel for private(n) schedule(static)
 	for(n=0; n<totnode; n++) {
 		PBVHVertexIter vd;
 		SculptBrushTest test;
-		SculptUndoNode *unode;
+		//SculptUndoNode *unode;
 		float fno[3];
 		float nout[3] = {0.0f, 0.0f, 0.0f};
 		float nout_flip[3] = {0.0f, 0.0f, 0.0f};
 		
 		// XXX push instead of get for thread safety in draw
 		// brush .. lame, but also not harmful really
-		unode= sculpt_undo_push_node(ss, nodes[n]);
+		//unode= sculpt_undo_push_node(ss, nodes[n]);
 		sculpt_brush_test_init(ss, &test);
 
 		if(ss->cache->original) {
 			BLI_pbvh_vertex_iter_begin(bvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
-				if(sculpt_brush_test(&test, unode->co[vd.i])) {
-					normal_short_to_float_v3(fno, unode->no[vd.i]);
+				if(sculpt_brush_test_fast(&test, vd.co)) {
+					normal_short_to_float_v3(fno, vd.no);
 					add_norm_if(out_dir, nout, nout_flip, fno);
 				}
 			}
@@ -806,7 +841,7 @@
 		}
 		else {
 			BLI_pbvh_vertex_iter_begin(bvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
-				if(sculpt_brush_test(&test, vd.co)) {
+				if(sculpt_brush_test_fast(&test, vd.co)) {
 					if(vd.no) {
 						normal_short_to_float_v3(fno, vd.no);
 						add_norm_if(out_dir, nout, nout_flip, fno);
@@ -1227,73 +1262,107 @@
 	}
 }
 
+//static void calc_flatten_center(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, float co[3])
+//{
+//    float outer_dist[FLATTEN_SAMPLE_SIZE];
+//    float outer_co[FLATTEN_SAMPLE_SIZE][3];
+//
+//    int n;
+//
+//    float count;
+//
+//    for (n = 0; n < FLATTEN_SAMPLE_SIZE; n++) {
+//        zero_v3(outer_co[n]);
+//        outer_dist[n]= -1.0f;
+//    }
+//
+//    for (n = 0; n < totnode; n++) {
+//        PBVHVertexIter vd;
+//        SculptBrushTest test;
+//
+//        int j;
+//
+//        //sculpt_undo_push_node(ss, nodes[n]);
+//
+//        sculpt_brush_test_init(ss, &test);
+//
+//        BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
+//            if (sculpt_brush_test(&test, vd.co)) {
+//                for (j = 0; j < FLATTEN_SAMPLE_SIZE; ++j) {
+//                    if (test.dist > outer_dist[j]) {
+//                        copy_v3_v3(outer_co[j], vd.co);
+//                        outer_dist[j] = test.dist;
+//                        break;
+//                    }
+//                }
+//            }
+//        }
+//        BLI_pbvh_vertex_iter_end;
+//
+//        //BLI_pbvh_node_mark_update(nodes[n]);
+//    }
+//
+//    count = co[0] = co[1] = co[2] = 0.0f;
+//
+//    for (n = 0; n < FLATTEN_SAMPLE_SIZE; n++) {
+//        if (outer_dist[n] >= 0.0f) {
+//            add_v3_v3(co, outer_co[n]);
+//            count++;
+//        }
+//    }
+//
+//    mul_v3_fl(co, 1.0f / count);
+//}
+
 static void calc_flatten_center(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, float co[3])
 {
-    float outer_dist[FLATTEN_SAMPLE_SIZE];
-    float outer_co[FLATTEN_SAMPLE_SIZE][3];
-
     int n;
 
-    float count;
+    float count = 0;
 
-    for (n = 0; n < FLATTEN_SAMPLE_SIZE; n++) {
-        zero_v3(outer_co[n]);
-        outer_dist[n]= -1.0f;
-    }
+    co[0] = co[1] = co[2] = 0;
 
     for (n = 0; n < totnode; n++) {
         PBVHVertexIter vd;
         SculptBrushTest test;
 
-        int j;
-
-        sculpt_undo_push_node(ss, nodes[n]);
-
         sculpt_brush_test_init(ss, &test);
 
         BLI_pbvh_vertex_iter_begin(ss->pbvh, nodes[n], vd, PBVH_ITER_UNIQUE) {
-            if (sculpt_brush_test(&test, vd.co)) {

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list