[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17722] branches/projection-paint/source/ blender/src/imagepaint.c: Use MEM_*alloc for some of the main arrays rather then MemArena ( was waisting too much memory)

Campbell Barton ideasman42 at gmail.com
Fri Dec 5 14:00:14 CET 2008


Revision: 17722
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17722
Author:   campbellbarton
Date:     2008-12-05 14:00:14 +0100 (Fri, 05 Dec 2008)

Log Message:
-----------
Use MEM_*alloc for some of the main arrays rather then MemArena (was waisting too much memory)

Modified Paths:
--------------
    branches/projection-paint/source/blender/src/imagepaint.c

Modified: branches/projection-paint/source/blender/src/imagepaint.c
===================================================================
--- branches/projection-paint/source/blender/src/imagepaint.c	2008-12-05 11:03:16 UTC (rev 17721)
+++ branches/projection-paint/source/blender/src/imagepaint.c	2008-12-05 13:00:14 UTC (rev 17722)
@@ -217,10 +217,9 @@
 	MTFace 		   *dm_mtface_clone;	/* other UV layer, use for cloning between layers */
 	
 	/* projection painting only */
-	MemArena *arena;			/* use for alocating many pixel structs and link-lists */
-	MemArena *arena_mt[BLENDER_MAX_THREADS];		/* Same as above but use for multithreading */
+	MemArena *arena_mt[BLENDER_MAX_THREADS];/* for multithreading, the first item is sometimes used for non threaded cases too */
 	LinkNode **bucketRect;				/* screen sized 2D array, each pixel has a linked list of ProjPixel's */
-	LinkNode **bucketFaces;				/* bucketRect alligned array linkList of faces overlapping each bucket */
+	LinkNode **bucketFaces;				/* bucketRect aligned array linkList of faces overlapping each bucket */
 	unsigned char *bucketFlags;					/* store if the bucks have been initialized  */
 #ifndef PROJ_DEBUG_NOSEAMBLEED
 	char *faceSeamFlags;				/* store info about faces, if they are initialized etc*/
@@ -871,21 +870,30 @@
 #define ISECT_TRUE_P2 3
 static int line_isect_y(const float p1[2], const float p2[2], const float y_level, float *x_isect)
 {
-	if (y_level==p1[1]) {
+	float y_diff;
+	
+	if (y_level==p1[1]) { /* are we touching the first point? - no interpolation needed */
 		*x_isect = p1[0];
 		return ISECT_TRUE_P1;
 	}
-	if (y_level==p2[1]) {
+	if (y_level==p2[1]) { /* are we touching the second point? - no interpolation needed */
 		*x_isect = p2[0];
 		return ISECT_TRUE_P2;
 	}
 	
+	y_diff= fabs(p1[1]-p2[1]); /* yuck, horizontal line, we cant do much here */
+	
+	if (y_diff < 0.000001) {
+		*x_isect = (p1[0]+p2[0]) * 0.5f;
+		return ISECT_TRUE;		
+	}
+	
 	if (p1[1] > y_level && p2[1] < y_level) {
-		*x_isect = (p2[0]*(p1[1]-y_level) + p1[0]*(y_level-p2[1])) / (p1[1]-p2[1]);
+		*x_isect = (p2[0]*(p1[1]-y_level) + p1[0]*(y_level-p2[1])) / y_diff;  /*(p1[1]-p2[1]);*/
 		return ISECT_TRUE;
 	}
 	else if (p1[1] < y_level && p2[1] > y_level) {
-		*x_isect = (p2[0]*(y_level-p1[1]) + p1[0]*(p2[1]-y_level)) / (p2[1]-p1[1]);
+		*x_isect = (p2[0]*(y_level-p1[1]) + p1[0]*(p2[1]-y_level)) / y_diff;  /*(p2[1]-p1[1]);*/
 		return ISECT_TRUE;
 	}
 	else {
@@ -895,21 +903,30 @@
 
 static int line_isect_x(const float p1[2], const float p2[2], const float x_level, float *y_isect)
 {
-	if (x_level==p1[0]) {
+	float x_diff;
+	
+	if (x_level==p1[0]) { /* are we touching the first point? - no interpolation needed */
 		*y_isect = p1[1];
 		return ISECT_TRUE_P1;
 	}
-	if (x_level==p2[0]) {
+	if (x_level==p2[0]) { /* are we touching the second point? - no interpolation needed */
 		*y_isect = p2[1];
 		return ISECT_TRUE_P2;
 	}
 	
+	x_diff= fabs(p1[0]-p2[0]); /* yuck, horizontal line, we cant do much here */
+	
+	if (x_diff < 0.000001) { /* yuck, vertical line, we cant do much here */
+		*y_isect = (p1[0]+p2[0]) * 0.5f;
+		return ISECT_TRUE;		
+	}
+	
 	if (p1[0] > x_level && p2[0] < x_level) {
-		*y_isect = (p2[1]*(p1[0]-x_level) + p1[1]*(x_level-p2[0])) / (p1[0]-p2[0]);
+		*y_isect = (p2[1]*(p1[0]-x_level) + p1[1]*(x_level-p2[0])) / x_diff; /*(p1[0]-p2[0]);*/
 		return ISECT_TRUE;
 	}
 	else if (p1[0] < x_level && p2[0] > x_level) {
-		*y_isect = (p2[1]*(x_level-p1[0]) + p1[1]*(p2[0]-x_level)) / (p2[0]-p1[0]);
+		*y_isect = (p2[1]*(x_level-p1[0]) + p1[1]*(p2[0]-x_level)) / x_diff; /*(p2[0]-p1[0]);*/
 		return ISECT_TRUE;
 	}
 	else {
@@ -1811,7 +1828,36 @@
 	float uv[2];
 	float bucket_bounds_ss[4][2];
 	float w[3];
+	
+//#if 0
+	rctf bucket_bounds_expand;
+	int use_expanded = 0;
+	bucket_bounds_expand.xmin = bucket_bounds->xmin;
+	bucket_bounds_expand.ymin = bucket_bounds->ymin;
+	bucket_bounds_expand.xmax = bucket_bounds->xmax;
+	bucket_bounds_expand.ymax = bucket_bounds->ymax;
+	
+	if (fabs(v1coSS[0]-bucket_bounds_expand.xmin) < 0.1f) {bucket_bounds_expand.xmin -= 0.1f; use_expanded = 1;}
+	if (fabs(v2coSS[0]-bucket_bounds_expand.xmin) < 0.1f) {bucket_bounds_expand.xmin -= 0.1f; use_expanded = 1;}
+	if (fabs(v3coSS[0]-bucket_bounds_expand.xmin) < 0.1f) {bucket_bounds_expand.xmin -= 0.1f; use_expanded = 1;}
+	
+	if (fabs(v1coSS[1]-bucket_bounds_expand.ymin) < 0.1f) {bucket_bounds_expand.ymin -= 0.1f; use_expanded = 1;}
+	if (fabs(v2coSS[1]-bucket_bounds_expand.ymin) < 0.1f) {bucket_bounds_expand.ymin -= 0.1f; use_expanded = 1;}
+	if (fabs(v3coSS[1]-bucket_bounds_expand.ymin) < 0.1f) {bucket_bounds_expand.ymin -= 0.1f; use_expanded = 1;}
+	
+	if (fabs(v1coSS[0]-bucket_bounds_expand.xmax) < 0.1f) {bucket_bounds_expand.xmax += 0.1f; use_expanded = 1;}
+	if (fabs(v2coSS[0]-bucket_bounds_expand.xmax) < 0.1f) {bucket_bounds_expand.xmax += 0.1f; use_expanded = 1;}
+	if (fabs(v3coSS[0]-bucket_bounds_expand.xmax) < 0.1f) {bucket_bounds_expand.xmax += 0.1f; use_expanded = 1;}
+	
+	if (fabs(v1coSS[1]-bucket_bounds_expand.ymax) < 0.1f) {bucket_bounds_expand.ymax += 0.1f; use_expanded = 1;}
+	if (fabs(v2coSS[1]-bucket_bounds_expand.ymax) < 0.1f) {bucket_bounds_expand.ymax += 0.1f; use_expanded = 1;}
+	if (fabs(v3coSS[1]-bucket_bounds_expand.ymax) < 0.1f) {bucket_bounds_expand.ymax += 0.1f; use_expanded = 1;}	
 
+	if ( use_expanded ) {
+		bucket_bounds = &bucket_bounds_expand;
+	}
+//#endif
+
 	/* get the UV space bounding box */
 	inside_bucket_flag |= BLI_in_rctf(bucket_bounds, v1coSS[0], v1coSS[1]);
 	inside_bucket_flag |= BLI_in_rctf(bucket_bounds, v2coSS[0], v2coSS[1])		<< 1;
@@ -1950,7 +1996,7 @@
 			isectVAngles[i] = -atan2(vClipSS_A[0]*vClipSS_B[1] - vClipSS_A[1]*vClipSS_B[0], vClipSS_A[0]*vClipSS_B[0]+vClipSS_A[1]*vClipSS_B[1]);
 			if (flip)
 				isectVAngles[i] = -isectVAngles[i];
-		} 
+		}
 #endif	/* end abuse */
 		
 #if 0	/* uses a few more cycles then the above loop */
@@ -2002,8 +2048,9 @@
 	{
 		/* If there are ever any problems, */
 		float test_uv[4][2];
-		if (is_ortho)	rect_to_uvspace_ortho(bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, test_uv);
-		else				rect_to_uvspace_persp(bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, test_uv);
+		int i;
+		if (is_ortho)	rect_to_uvspace_ortho(bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, test_uv, flip);
+		else				rect_to_uvspace_persp(bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, test_uv, flip);
 		printf("(  [(%f,%f), (%f,%f), (%f,%f), (%f,%f)], ", test_uv[0][0], test_uv[0][1],   test_uv[1][0], test_uv[1][1],    test_uv[2][0], test_uv[2][1],    test_uv[3][0], test_uv[3][1]);
 		
 		printf("  [(%f,%f), (%f,%f), (%f,%f)], ", uv1co[0], uv1co[1],   uv2co[0], uv2co[1],    uv3co[0], uv3co[1]);
@@ -2014,7 +2061,7 @@
 		}
 		printf("]),\\\n");
 	}
-	
+}
 	/*
 # This script creates faces in a blender scene from printed data above.
 
@@ -2060,6 +2107,7 @@
 if __name__ == '__main__':
 	main()
  */	
+	
 #endif
 
 #undef ISECT_1
@@ -2185,8 +2233,8 @@
 				uv1co, uv2co, uv3co,
 				uv_clip, &uv_clip_tot
 		);
-
 		
+		
 		/* sometimes this happens, better just allow for 8 intersectiosn even though there should be max 6 */
 		/*
 		if (uv_clip_tot>6) {
@@ -2241,10 +2289,12 @@
 						}
 						
 					}
+#if 0
 					else if (has_x_isect) {
 						/* assuming the face is not a bow-tie - we know we cant intersect again on the X */
 						break;
 					}
+#endif
 				}
 				
 				
@@ -2618,8 +2668,8 @@
 	float min[2], max[2], *vCoSS;
 	int bucketMin[2], bucketMax[2]; /* for  ps->bucketRect indexing */
 	int fidx, bucket_x, bucket_y, bucket_index;
-	
 	int has_x_isect = -1, has_isect = 0; /* for early loop exit */
+	MemArena *arena = ps->arena_mt[0]; /* just use the first thread arena since threading has not started yet */
 	
 	INIT_MINMAX2(min, max);
 	
@@ -2641,7 +2691,7 @@
 				BLI_linklist_prepend_arena(
 					&ps->bucketFaces[ bucket_index ],
 					SET_INT_IN_POINTER(face_index), /* cast to a pointer to shut up the compiler */
-					ps->arena
+					arena
 				);
 				
 				has_x_isect = has_isect = 1;
@@ -2692,15 +2742,8 @@
 	int a, i; /* generic looping vars */
 	int image_index = -1, face_index;
 	
-	/* memory sized to add to arena size */
-	int tot_bucketRectMem=0;
-	int tot_faceSeamFlagsMem=0;
-	int tot_faceSeamUVMem=0;
-	int tot_bucketFacesMem=0;
-	int tot_bucketFlagsMem=0;
-	int tot_vertFacesMem=0;
-	int tot_vertFlagsMem=0;
-
+	MemArena *arena; /* at the moment this is just ps->arena_mt[0], but use this to show were not multithreading */
+	
 	/* ---- end defines ---- */
 	
 	/* paint onto the derived mesh
@@ -2770,10 +2813,6 @@
 	
 	/* calculate vert screen coords
 	 * run this early so we can calculate the x/y resolution of our bucket rect */
-	
-	/* since we now run this before the memarena is allocated, this will need its own memory */
-	/*ps->screenCoords = BLI_memarena_alloc(ps->arena, sizeof(float) * ps->dm_totvert * 4);*/
-	
 	INIT_MINMAX2(ps->screenMin, ps->screenMax);
 	
 	ps->screenCoords = MEM_mallocN(sizeof(float) * ps->dm_totvert * 4, "ProjectPaint ScreenVerts");
@@ -2847,58 +2886,18 @@
 	CLAMP(ps->buckets_x, PROJ_BUCKET_RECT_MIN, PROJ_BUCKET_RECT_MAX);
 	CLAMP(ps->buckets_y, PROJ_BUCKET_RECT_MIN, PROJ_BUCKET_RECT_MAX);
 	
-	tot_bucketRectMem =				sizeof(LinkNode *) * ps->buckets_x * ps->buckets_y;
-	tot_bucketFacesMem =			sizeof(LinkNode *) * ps->buckets_x * ps->buckets_y;
+	ps->bucketRect = (LinkNode **)MEM_callocN(sizeof(LinkNode *) * ps->buckets_x * ps->buckets_y, "paint-bucketRect");
+	ps->bucketFaces= (LinkNode **)MEM_callocN(sizeof(LinkNode *) * ps->buckets_x * ps->buckets_y, "paint-bucketFaces");
 	
-	tot_bucketFlagsMem =			sizeof(char) * ps->buckets_x * ps->buckets_y;
+	ps->bucketFlags= (unsigned char *)MEM_callocN(sizeof(char) * ps->buckets_x * ps->buckets_y, "paint-bucketFaces");
 #ifndef PROJ_DEBUG_NOSEAMBLEED
-	if (ps->seam_bleed_px > 0.0f) { /* UV Seams for bleeding */
-		tot_vertFacesMem =	sizeof(LinkNode *) * ps->dm_totvert;
-		tot_faceSeamFlagsMem =		sizeof(char) * ps->dm_totface;
-		tot_faceSeamUVMem =			sizeof(float) * ps->dm_totface * 8;
-	}
-#endif
-	
-	if (ps->do_backfacecull && ps->do_mask_normal) {
-		tot_vertFlagsMem = sizeof(char) * ps->dm_totvert;
-	}
-	
-	/* BLI_memarena_new uses calloc */
-	ps->arena =
-		BLI_memarena_new(	tot_bucketRectMem +
-							tot_bucketFacesMem +
-							tot_faceSeamFlagsMem +
-							tot_faceSeamUVMem +
-							tot_vertFacesMem +
-							tot_vertFlagsMem + (1<<16));
-	

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list