[Bf-blender-cvs] [231b0f7] soc-2013-paint: Refactoring:

Antony Riakiotakis noreply at git.blender.org
Fri Mar 7 23:39:31 CET 2014


Commit: 231b0f7950c062141eac7a46d5d359107c37a724
Author: Antony Riakiotakis
Date:   Fri Mar 7 22:43:19 2014 +0200
https://developer.blender.org/rB231b0f7950c062141eac7a46d5d359107c37a724

Refactoring:

Separate mask texture and color texture sampling. Old code was
exceedingly convoluted and hard to follow. Now the two texture slots
handle updating independently. There may be some updating errors
but preliminary testing seems OK.

TODO, possible now at last: Manage partial update
for mask textures just like color textures.

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

M	source/blender/editors/sculpt_paint/paint_image.c
M	source/blender/editors/sculpt_paint/paint_image_2d.c
M	source/blender/editors/sculpt_paint/paint_intern.h
M	source/blender/editors/sculpt_paint/paint_utils.c
M	source/blender/imbuf/IMB_imbuf.h
M	source/blender/imbuf/intern/rectop.c

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

diff --git a/source/blender/editors/sculpt_paint/paint_image.c b/source/blender/editors/sculpt_paint/paint_image.c
index a5b1a60..fd0d6d7 100644
--- a/source/blender/editors/sculpt_paint/paint_image.c
+++ b/source/blender/editors/sculpt_paint/paint_image.c
@@ -73,6 +73,7 @@
 #include "BKE_paint.h"
 #include "BKE_report.h"
 #include "BKE_scene.h"
+#include "BKE_texture.h"
 #include "BKE_colortools.h"
 
 #include "BKE_editmesh.h"
@@ -638,6 +639,35 @@ bool paint_use_opacity_masking(Brush *brush)
 				false : true;
 }
 
+void paint_brush_color_get(struct Brush *br, bool color_correction, bool invert, float distance, float pressure, float color[3])
+{
+	if (invert)
+		copy_v3_v3(color, br->secondary_rgb);
+	else {
+		if (br->flag & BRUSH_USE_GRADIENT) {
+			switch (br->gradient_stroke_mode) {
+				case BRUSH_GRADIENT_PRESSURE:
+					do_colorband(br->gradient, pressure, color);
+					break;
+				case BRUSH_GRADIENT_SPACING_REPEAT:
+				{
+					float coord = fmod(distance / br->gradient_spacing, 1.0);
+					do_colorband(br->gradient, coord, color);
+					break;
+				}
+				case BRUSH_GRADIENT_SPACING_CLAMP:
+				{
+					do_colorband(br->gradient, distance / br->gradient_spacing, color);
+					break;
+				}
+			}
+		}
+		else
+			copy_v3_v3(color, br->rgb);
+	}
+	if (color_correction)
+		srgb_to_linearrgb_v3_v3(color, color);
+}
 
 void paint_brush_init_tex(Brush *brush)
 {
diff --git a/source/blender/editors/sculpt_paint/paint_image_2d.c b/source/blender/editors/sculpt_paint/paint_image_2d.c
index 7904ec9..4ca9809 100644
--- a/source/blender/editors/sculpt_paint/paint_image_2d.c
+++ b/source/blender/editors/sculpt_paint/paint_image_2d.c
@@ -77,7 +77,6 @@
 typedef struct BrushPainterCache {
 	bool use_float;              /* need float imbuf? */
 	bool use_color_correction;   /* use color correction for float */
-	bool use_masking;            /* use masking? */
 	bool invert;
 
 	bool is_texbrush;
@@ -93,7 +92,7 @@ typedef struct BrushPainterCache {
 	ImBuf *ibuf;
 	ImBuf *texibuf;
 	unsigned short *curve_mask;
-	unsigned short *max_mask;
+	unsigned short *tex_mask;
 } BrushPainterCache;
 
 typedef struct BrushPainter {
@@ -162,17 +161,17 @@ static BrushPainter *brush_painter_2d_new(Scene *scene, Brush *brush, bool inver
 }
 
 
-static void brush_painter_2d_require_imbuf(BrushPainter *painter, bool use_float, bool use_color_correction, bool use_masking)
+static void brush_painter_2d_require_imbuf(BrushPainter *painter, bool use_float, bool use_color_correction)
 {
 	Brush *brush = painter->brush;
 
 	if ((painter->cache.use_float != use_float)) {
 		if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
 		if (painter->cache.curve_mask) MEM_freeN(painter->cache.curve_mask);
-		if (painter->cache.max_mask) MEM_freeN(painter->cache.max_mask);
+		if (painter->cache.tex_mask) MEM_freeN(painter->cache.tex_mask);
 		painter->cache.ibuf = NULL;
 		painter->cache.curve_mask = NULL;
-		painter->cache.max_mask = NULL;
+		painter->cache.tex_mask = NULL;
 		painter->cache.lastdiameter = -1; /* force ibuf create in refresh */
 	}
 
@@ -184,7 +183,6 @@ static void brush_painter_2d_require_imbuf(BrushPainter *painter, bool use_float
 
 	painter->cache.use_float = use_float;
 	painter->cache.use_color_correction = use_float && use_color_correction;
-	painter->cache.use_masking = use_masking;
 	painter->cache.is_texbrush = (brush->mtex.tex && brush->imagepaint_tool == PAINT_TOOL_DRAW) ? true : false;
 	painter->cache.is_maskbrush = (brush->mask_mtex.tex) ? true : false;
 }
@@ -194,7 +192,7 @@ static void brush_painter_2d_free(BrushPainter *painter)
 	if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
 	if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
 	if (painter->cache.curve_mask) MEM_freeN(painter->cache.curve_mask);
-	if (painter->cache.max_mask) MEM_freeN(painter->cache.max_mask);
+	if (painter->cache.tex_mask) MEM_freeN(painter->cache.tex_mask);
 	MEM_freeN(painter);
 }
 
@@ -205,17 +203,13 @@ static void brush_imbuf_tex_co(rctf *mapping, int x, int y, float texco[3])
 	texco[2] = 0.0f;
 }
 
-/* create a mask with the falloff strength and optionally brush alpha */
-static unsigned short *brush_painter_max_mask_new(BrushPainter *painter, int size)
+/* create a mask with the mask texture */
+static unsigned short *brush_painter_mask_ibuf_new(BrushPainter *painter, int size)
 {
 	Scene *scene = painter->scene;
 	Brush *brush = painter->brush;
-	bool use_masking = painter->cache.use_masking;
 	rctf mask_mapping = painter->mask_mapping;
 	struct ImagePool *pool = painter->pool;
-	bool is_maskbrush = painter->cache.is_maskbrush;
-
-	float max_alpha = (use_masking)? BKE_brush_alpha_get(scene, brush) : 1.0f;
 
 	float texco[3];
 	unsigned short *mask, *m;
@@ -226,14 +220,11 @@ static unsigned short *brush_painter_max_mask_new(BrushPainter *painter, int siz
 
 	for (y = 0; y < size; y++) {
 		for (x = 0; x < size; x++, m++) {
-			float strength = max_alpha;
-
-			if (is_maskbrush) {
-				brush_imbuf_tex_co(&mask_mapping, x, y, texco);
-				strength *= BKE_brush_sample_masktex(scene, brush, texco, thread, pool);
-			}
-
-			*m = (unsigned short)(65535.0f * strength);
+			float res;
+			brush_imbuf_tex_co(&mask_mapping, x, y, texco);
+			res = BKE_brush_sample_masktex(scene, brush, texco, thread, pool);
+			CLAMP(res, 0.0, 1.0);
+			*m = (unsigned short)(65535.0f * res);
 		}
 	}
 
@@ -277,19 +268,12 @@ static ImBuf *brush_painter_imbuf_new(BrushPainter *painter, int size, float pre
 	rctf tex_mapping = painter->tex_mapping;
 	struct ImagePool *pool = painter->pool;
 
-	bool use_masking = painter->cache.use_masking;
 	bool use_color_correction = painter->cache.use_color_correction;
 	bool use_float = painter->cache.use_float;
 	bool is_texbrush = painter->cache.is_texbrush;
-	bool is_maskbrush = painter->cache.is_maskbrush;
 
 	int x, y, thread = 0;
 	float brush_rgb[3];
-	float alpha = (is_maskbrush)? BKE_brush_alpha_get(scene, brush) :
-	                              BKE_brush_alpha_get(scene, brush) * 65535.0f;
-
-	unsigned short *mask = painter->cache.curve_mask;
-	unsigned short *max_mask = painter->cache.max_mask;
 
 	/* allocate image buffer */
 	ImBuf *ibuf = IMB_allocImBuf(size, size, 32, (use_float) ? IB_rectfloat : IB_rect);
@@ -324,13 +308,6 @@ static ImBuf *brush_painter_imbuf_new(BrushPainter *painter, int size, float pre
 				rgba[3] = 1.0f;
 			}
 
-			/* when not using masking, multiply in falloff and strength */
-			if (!use_masking) {
-				unsigned short *m = mask + (y * ibuf->x + x);
-				unsigned short max_m = (is_maskbrush)? *(max_mask + (y * ibuf->x + x)) * alpha : alpha;
-				rgba[3] *= (*m) * (1.0f / 65535.0f) * (max_m) * (1.0f / 65535.0f);
-			}
-
 			if (use_float) {
 				/* write to float pixel */
 				float *dstf = ibuf->rect_float + (y * size + x) * 4;
@@ -360,33 +337,20 @@ static void brush_painter_imbuf_update(BrushPainter *painter, ImBuf *oldtexibuf,
 	rctf tex_mapping = painter->tex_mapping;
 	struct ImagePool *pool = painter->pool;
 
-	bool use_masking = painter->cache.use_masking;
 	bool use_color_correction = painter->cache.use_color_correction;
 	bool use_float = painter->cache.use_float;
 	bool is_texbrush = painter->cache.is_texbrush;
-	bool is_maskbrush = painter->cache.is_maskbrush;
 	bool use_texture_old = (oldtexibuf != NULL);
 
-	float alpha = (is_maskbrush)? BKE_brush_alpha_get(scene, brush) :
-	                              BKE_brush_alpha_get(scene, brush) * 65535.0f;
-
 	int x, y, thread = 0;
 	float brush_rgb[3];
 
 	ImBuf *ibuf = painter->cache.ibuf;
 	ImBuf *texibuf = painter->cache.texibuf;
-	unsigned short *mask = painter->cache.curve_mask;
-	unsigned short *max_mask = painter->cache.max_mask;
 
 	/* get brush color */
 	if (brush->imagepaint_tool == PAINT_TOOL_DRAW) {
-		if (painter->cache.invert)
-			copy_v3_v3(brush_rgb, brush->secondary_rgb);
-		else
-			copy_v3_v3(brush_rgb, brush->rgb);
-
-		if (use_color_correction)
-			srgb_to_linearrgb_v3_v3(brush_rgb, brush_rgb);
+		paint_brush_color_get(brush, use_color_correction, painter->cache.invert, 0.0, 1.0, brush_rgb);
 	}
 	else {
 		brush_rgb[0] = 1.0f;
@@ -430,13 +394,6 @@ static void brush_painter_imbuf_update(BrushPainter *painter, ImBuf *oldtexibuf,
 				/* write to new texture buffer */
 				copy_v4_v4(tf, rgba);
 
-				/* if not using masking, multiply in the mask now */
-				if (!use_masking) {
-					unsigned short *m = mask + (y * ibuf->x + x);
-					unsigned short max_m = (is_maskbrush)? *(max_mask + (y * ibuf->x + x)) * alpha : alpha;
-					rgba[3] *= (*m) * (1.0f / 65535.0f) * (max_m) * (1.0f / 65535.0f);
-				}
-
 				/* output premultiplied float image, mf was already premultiplied */
 				mul_v3_v3fl(bf, rgba, rgba[3]);
 				bf[3] = rgba[3];
@@ -465,13 +422,6 @@ static void brush_painter_imbuf_update(BrushPainter *painter, ImBuf *oldtexibuf,
 				t[2] = crgba[2];
 				t[3] = crgba[3];
 
-				/* if not using masking, multiply in the mask now */
-				if (!use_masking) {
-					unsigned short *m = mask + (y * ibuf->x + x);
-					unsigned short max_m = (is_maskbrush)? *(max_mask + (y * ibuf->x + x)) * alpha : alpha;
-					crgba[3] = crgba[3] * (*m) * 1/(65535.0f) * (max_m) * 1/(65535.0f);
-				}
-
 				/* write to brush image buffer */
 				b[0] = crgba[0];
 				b[1] = crgba[1];
@@ -621,26 +571,15 @@ static void brush_painter_2d_refresh_cache(ImagePaintState *s, BrushPainter *pai
 		bool do_view_mask = false;
 		//bool do_partial_update_mask = false;
 		/* invalidate case for all mapping modes */
-		if (diameter != cache->lastdiameter ||
-		    alpha != cache->lastalpha)
-		{
-			renew_maxmask = true;
-		}
 		if (brush->mask_mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
 			do_view_mask = true;
 			mask_rotation += ups->brush_rotation;
 		}
 		else if (brush->mask_mtex.brush_map_mode == MTEX_MAP_MODE_RANDOM) {
-			/* we need to force random to take new mask values into account */
-			if (!painter->cache.use_masking)
-				do_random = true;
 			renew_maxmask = true;
 			//do_partial_update_mask = false;
 		}
 		else {
-			/* we need to do partial update to take new mask values into account */
-			if (!painter->cache.use_masking)
-				do_partial_update = true;
 			//do_partial_update_mask = true;
 			renew_maxmask = true;
 		}
@@ 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list