[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [48916] trunk/blender/source/blender/ blenkernel/intern/mask_rasterize.c: minor refactor for mask rasterizer

Campbell Barton ideasman42 at gmail.com
Sat Jul 14 17:46:32 CEST 2012


Revision: 48916
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48916
Author:   campbellbarton
Date:     2012-07-14 15:46:32 +0000 (Sat, 14 Jul 2012)
Log Message:
-----------
minor refactor for mask rasterizer

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/mask_rasterize.c

Modified: trunk/blender/source/blender/blenkernel/intern/mask_rasterize.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/mask_rasterize.c	2012-07-14 15:29:45 UTC (rev 48915)
+++ trunk/blender/source/blender/blenkernel/intern/mask_rasterize.c	2012-07-14 15:46:32 UTC (rev 48916)
@@ -46,6 +46,15 @@
 
 #ifndef USE_RASKTER
 
+#define SPLINE_RESOL 32
+
+#define SF_EDGE_IS_BOUNDARY 0xff
+#define SF_KEYINDEX_TEMP_ID ((unsigned int) -1)
+
+#define TRI_TERMINATOR_ID   ((unsigned int) -1)
+#define TRI_VERT            ((unsigned int) -1)
+
+
 /**
  * A single #MaskRasterHandle contains multile #MaskRasterLayer's,
  * each #MaskRasterLayer does its own lookup which contributes to
@@ -82,6 +91,11 @@
 
 static void layer_bucket_init(MaskRasterLayer *layer);
 
+
+/* --------------------------------------------------------------------- */
+/* alloc / free functions                                                */
+/* --------------------------------------------------------------------- */
+
 /**
  * opaque local struct for mask pixel lookup, each MaskLayer needs one of these
  */
@@ -137,16 +151,7 @@
 	MEM_freeN(mr_handle);
 }
 
-#define RESOL 32
 
-#define PRINT_MASK_DEBUG printf
-
-#define SF_EDGE_IS_BOUNDARY 0xff
-
-#define SF_KEYINDEX_TEMP_ID ((unsigned int) -1)
-#define TRI_TERMINATOR_ID   ((unsigned int) -1)
-
-
 void maskrasterize_spline_differentiate_point_inset(float (*diff_feather_points)[2], float (*diff_points)[2],
                                                     const unsigned int tot_diff_point, const float ofs,
                                                     const short do_test)
@@ -208,16 +213,126 @@
 	}
 }
 
-#define TRI_VERT ((unsigned int) -1)
 
+static void layer_bucket_init(MaskRasterLayer *layer)
+{
+	MemArena *arena = BLI_memarena_new(1 << 16, __func__);
+
+	/* TODO - calculate best bucket size */
+	layer->buckets_x = 256;
+	layer->buckets_y = 256;
+
+	layer->buckets_xy_scalar[0] = (1.0f / ((layer->bounds.xmax - layer->bounds.xmin) + FLT_EPSILON)) * layer->buckets_x;
+	layer->buckets_xy_scalar[1] = (1.0f / ((layer->bounds.ymax - layer->bounds.ymin) + FLT_EPSILON)) * layer->buckets_y;
+
+	{
+		unsigned int *tri = &layer->tri_array[0][0];
+		float (*cos)[3] = layer->tri_coords;
+
+		const unsigned int   bucket_tot = layer->buckets_x * layer->buckets_y;
+		LinkNode     **bucketstore     = MEM_callocN(bucket_tot * sizeof(LinkNode *),  __func__);
+		unsigned int  *bucketstore_tot = MEM_callocN(bucket_tot * sizeof(unsigned int), __func__);
+
+		unsigned int tri_index;
+
+		for (tri_index = 0; tri_index < layer->tri_tot; tri_index++, tri += 4) {
+			float xmin;
+			float xmax;
+			float ymin;
+			float ymax;
+
+			if (tri[3] == TRI_VERT) {
+				const float *v1 = cos[tri[0]];
+				const float *v2 = cos[tri[1]];
+				const float *v3 = cos[tri[2]];
+
+				xmin = fminf(v1[0], fminf(v2[0], v3[0]));
+				xmax = fmaxf(v1[0], fmaxf(v2[0], v3[0]));
+				ymin = fminf(v1[1], fminf(v2[1], v3[1]));
+				ymax = fmaxf(v1[1], fmaxf(v2[1], v3[1]));
+			}
+			else {
+				const float *v1 = cos[tri[0]];
+				const float *v2 = cos[tri[1]];
+				const float *v3 = cos[tri[2]];
+				const float *v4 = cos[tri[3]];
+
+				xmin = fminf(v1[0], fminf(v2[0], fminf(v3[0], v4[0])));
+				xmax = fmaxf(v1[0], fmaxf(v2[0], fmaxf(v3[0], v4[0])));
+				ymin = fminf(v1[1], fminf(v2[1], fminf(v3[1], v4[1])));
+				ymax = fmaxf(v1[1], fmaxf(v2[1], fmaxf(v3[1], v4[1])));
+			}
+
+
+			/* not essential but may as will skip any faces outside the view */
+			if (!((xmax < 0.0f) || (ymax < 0.0f) || (xmin > 1.0f) || (ymin > 1.0f))) {
+				const unsigned int xi_min = (unsigned int) ((xmin - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
+				const unsigned int xi_max = (unsigned int) ((xmax - layer->bounds.xmin) * layer->buckets_xy_scalar[0]);
+				const unsigned int yi_min = (unsigned int) ((ymin - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
+				const unsigned int yi_max = (unsigned int) ((ymax - layer->bounds.ymin) * layer->buckets_xy_scalar[1]);
+
+				unsigned int xi, yi;
+
+				for (xi = xi_min; xi <= xi_max; xi++) {
+					for (yi = yi_min; yi <= yi_max; yi++) {
+						unsigned int bucket_index = (layer->buckets_x * yi) + xi;
+
+						BLI_assert(xi < layer->buckets_x);
+						BLI_assert(yi < layer->buckets_y);
+						BLI_assert(bucket_index < bucket_tot);
+
+						BLI_linklist_prepend_arena(&bucketstore[bucket_index],
+						                           SET_UINT_IN_POINTER(tri_index),
+						                           arena);
+
+						bucketstore_tot[bucket_index]++;
+					}
+				}
+			}
+		}
+
+		if (1) {
+			/* now convert linknodes into arrays for faster per pixel access */
+			unsigned int  **buckets_tri = MEM_mallocN(bucket_tot * sizeof(unsigned int **), __func__);
+			unsigned int bucket_index;
+
+			for (bucket_index = 0; bucket_index < bucket_tot; bucket_index++) {
+				if (bucketstore_tot[bucket_index]) {
+					unsigned int  *bucket = MEM_mallocN((bucketstore_tot[bucket_index] + 1) * sizeof(unsigned int),
+					                                    __func__);
+					LinkNode *bucket_node;
+
+					buckets_tri[bucket_index] = bucket;
+
+					for (bucket_node = bucketstore[bucket_index]; bucket_node; bucket_node = bucket_node->next) {
+						*bucket = GET_UINT_FROM_POINTER(bucket_node->link);
+						bucket++;
+					}
+					*bucket = TRI_TERMINATOR_ID;
+				}
+				else {
+					buckets_tri[bucket_index] = NULL;
+				}
+			}
+
+			layer->buckets_tri = buckets_tri;
+		}
+
+		MEM_freeN(bucketstore);
+		MEM_freeN(bucketstore_tot);
+	}
+
+	BLI_memarena_free(arena);
+}
+
 void BLI_maskrasterize_handle_init(MaskRasterHandle *mr_handle, struct Mask *mask,
                                    const int width, const int height,
                                    const short do_aspect_correct, const short do_mask_aa,
                                    const short do_feather)
 {
 	/* TODO: real size */
-	const int resol = RESOL;
-	const float aa_filter_size = 1.0f / MIN2(width, height);
+	const int resol = SPLINE_RESOL;
+	const float pixel_size = 1.0f / MIN2(width, height);
 
 	const float zvec[3] = {0.0f, 0.0f, 1.0f};
 	MaskLayer *masklay;
@@ -254,11 +369,13 @@
 			float (*diff_feather_points)[2];
 			int tot_diff_feather_points;
 
-			diff_points = BKE_mask_spline_differentiate_with_resolution_ex(spline, resol, &tot_diff_point);
+			diff_points = BKE_mask_spline_differentiate_with_resolution_ex(
+			                  spline, resol, &tot_diff_point);
 
 			/* dont ch*/
 			if (do_feather) {
-				diff_feather_points = BKE_mask_spline_feather_differentiated_points_with_resolution_ex(spline, resol, &tot_diff_feather_points);
+				diff_feather_points = BKE_mask_spline_feather_differentiated_points_with_resolution_ex(
+				                          spline, resol, &tot_diff_feather_points);
 			}
 			else {
 				tot_diff_feather_points = 0;
@@ -306,15 +423,16 @@
 				if (do_mask_aa == TRUE) {
 					if (do_feather == FALSE) {
 						tot_diff_feather_points = tot_diff_point;
-						diff_feather_points = MEM_mallocN(sizeof(*diff_feather_points) * tot_diff_feather_points, __func__);
+						diff_feather_points = MEM_mallocN(sizeof(*diff_feather_points) * tot_diff_feather_points,
+						                                  __func__);
 						/* add single pixel feather */
 						maskrasterize_spline_differentiate_point_inset(diff_feather_points, diff_points,
-						                                               tot_diff_point, aa_filter_size, FALSE);
+						                                               tot_diff_point, pixel_size, FALSE);
 					}
 					else {
 						/* ensure single pixel feather, on any zero feather areas */
 						maskrasterize_spline_differentiate_point_inset(diff_feather_points, diff_points,
-						                                               tot_diff_point, aa_filter_size, TRUE);
+						                                               tot_diff_point, pixel_size, TRUE);
 					}
 				}
 
@@ -471,7 +589,7 @@
 				BLI_union_rctf(&mr_handle->bounds, &bounds);
 			}
 
-			PRINT_MASK_DEBUG("tris %d, feather tris %d\n", sf_tri_tot, tot_feather_quads);
+			/* printf("tris %d, feather tris %d\n", sf_tri_tot, tot_feather_quads); */
 		}
 
 		/* add trianges */
@@ -479,6 +597,11 @@
 	}
 }
 
+
+/* --------------------------------------------------------------------- */
+/* functions that run inside the sampling thread (keep fast!)            */
+/* --------------------------------------------------------------------- */
+
 /* 2D ray test */
 static float maskrasterize_layer_z_depth_tri(const float pt[2],
                                              const float v1[3], const float v2[3], const float v3[3])
@@ -516,7 +639,7 @@
 			}
 		}
 #else
-        /* we know all tris are close for now */
+		/* we know all tris are close for now */
 		if (1) {
 			if (isect_point_tri_v2(xy, cos[tri[0]], cos[tri[1]], cos[tri[2]])) {
 				return 0.0f;
@@ -556,116 +679,6 @@
 	return 1.0f;
 }
 
-static void layer_bucket_init(MaskRasterLayer *layer)
-{
-	MemArena *arena = BLI_memarena_new(1 << 16, __func__);
-
-	/* TODO - calculate best bucket size */
-	layer->buckets_x = 256;
-	layer->buckets_y = 256;
-
-	layer->buckets_xy_scalar[0] = (1.0f / ((layer->bounds.xmax - layer->bounds.xmin) + FLT_EPSILON)) * layer->buckets_x;
-	layer->buckets_xy_scalar[1] = (1.0f / ((layer->bounds.ymax - layer->bounds.ymin) + FLT_EPSILON)) * layer->buckets_y;
-
-	{
-		unsigned int *tri = &layer->tri_array[0][0];
-		float (*cos)[3] = layer->tri_coords;
-
-		const unsigned int   bucket_tot = layer->buckets_x * layer->buckets_y;
-		LinkNode     **bucketstore     = MEM_callocN(bucket_tot * sizeof(LinkNode *),  __func__);
-		unsigned int  *bucketstore_tot = MEM_callocN(bucket_tot * sizeof(unsigned int), __func__);
-
-		unsigned int tri_index;
-
-		for (tri_index = 0; tri_index < layer->tri_tot; tri_index++, tri += 4) {
-			float xmin;
-			float xmax;
-			float ymin;
-			float ymax;
-
-			if (tri[3] == TRI_VERT) {
-				const float *v1 = cos[tri[0]];
-				const float *v2 = cos[tri[1]];
-				const float *v3 = cos[tri[2]];
-
-				xmin = fminf(v1[0], fminf(v2[0], v3[0]));
-				xmax = fmaxf(v1[0], fmaxf(v2[0], v3[0]));
-				ymin = fminf(v1[1], fminf(v2[1], v3[1]));
-				ymax = fmaxf(v1[1], fmaxf(v2[1], v3[1]));
-			}
-			else {
-				const float *v1 = cos[tri[0]];
-				const float *v2 = cos[tri[1]];
-				const float *v3 = cos[tri[2]];
-				const float *v4 = cos[tri[3]];
-
-				xmin = fminf(v1[0], fminf(v2[0], fminf(v3[0], v4[0])));

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list