[Bf-blender-cvs] [7d05a1035a1] temp-udim-images: Support 3D texture painting across tiles

Lukas Stockner noreply at git.blender.org
Fri Jun 15 23:33:39 CEST 2018


Commit: 7d05a1035a14131c2c0efeed623c586de8f70a88
Author: Lukas Stockner
Date:   Fri Jun 15 21:51:48 2018 +0200
Branches: temp-udim-images
https://developer.blender.org/rB7d05a1035a14131c2c0efeed623c586de8f70a88

Support 3D texture painting across tiles

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

M	source/blender/editors/sculpt_paint/paint_image_proj.c
M	source/blender/editors/sculpt_paint/paint_utils.c

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

diff --git a/source/blender/editors/sculpt_paint/paint_image_proj.c b/source/blender/editors/sculpt_paint/paint_image_proj.c
index 40789140924..d6770196f6e 100644
--- a/source/blender/editors/sculpt_paint/paint_image_proj.c
+++ b/source/blender/editors/sculpt_paint/paint_image_proj.c
@@ -193,15 +193,15 @@ BLI_INLINE unsigned char f_to_char(const float val)
  * because 'partRedrawRect' and 'touch' values would not be thread safe */
 typedef struct ProjPaintImage {
 	Image *ima;
-	ImBuf *ibuf;
+	int tile;
 	ImageUser iuser;
+	ImBuf *ibuf;
 	ImagePaintPartialRedraw *partRedrawRect;
 	volatile void **undoRect; /* only used to build undo tiles during painting */
 	unsigned short **maskRect; /* the mask accumulation must happen on canvas, not on space screen bucket.
 	                            * Here we store the mask rectangle */
 	bool **valid; /* store flag to enforce validation of undo rectangle */
 	bool touch;
-	float uv_ofs[2];
 } ProjPaintImage;
 
 /**
@@ -468,6 +468,16 @@ BLI_INLINE const MPoly *ps_tri_index_to_mpoly(const ProjPaintState *ps, int tri_
 
 /* Finish projection painting structs */
 
+static int project_paint_face_paint_tile(Image *ima, const float *uv)
+{
+	if (!ima || ima->source != IMA_SRC_TILED)
+		return 0;
+	/* Currently, faces are assumed to belong to one tile, so checking the first loop is enough. */
+	int tx = (int) uv[0];
+	int ty = (int) uv[1];
+	return 10*ty + tx;
+}
+
 static TexPaintSlot *project_paint_face_paint_slot(const ProjPaintState *ps, int tri_index)
 {
 	const MPoly *mp = ps_tri_index_to_mpoly(ps, tri_index);
@@ -664,8 +674,18 @@ static bool project_paint_PickColor(
 	interp_v2_v2v2v2(uv, UNPACK3(lt_tri_uv), w);
 
 	ima = project_paint_face_paint_image(ps, tri_index);
-	ibuf = BKE_image_get_first_ibuf(ima); /* we must have got the imbuf before getting here */ /* TODO */
-	if (!ibuf) return 0;
+	int tile_number = project_paint_face_paint_tile(ima, lt_tri_uv[0]);
+	ImageUser iuser = {NULL};
+	iuser.ok = 1;
+	iuser.tile = tile_number;
+	ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
+	if (!ibuf) {
+		iuser.tile = 0;
+		ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
+		if (!ibuf) {
+			return 0;
+		}
+	}
 
 	if (interp) {
 		float x, y;
@@ -1052,6 +1072,8 @@ static bool check_seam(
 				const float *lt_tri_uv[3] = { PS_LOOPTRI_AS_UV_3(ps->dm_mloopuv, lt) };
 				Image *tpage = project_paint_face_paint_image(ps, tri_index);
 				Image *orig_tpage = project_paint_face_paint_image(ps, orig_face);
+				int tile = project_paint_face_paint_tile(tpage, lt_tri_uv[0]);
+				int orig_tile = project_paint_face_paint_tile(orig_tpage, orig_lt_tri_uv[0]);
 
 				BLI_assert(i1_fidx != -1);
 
@@ -1069,6 +1091,7 @@ static bool check_seam(
 
 				/* first test if they have the same image */
 				if ((orig_tpage == tpage) &&
+				    (orig_tile == tile) &&
 				    cmp_uv(orig_lt_tri_uv[orig_i1_fidx], lt_tri_uv[i1_fidx]) &&
 				    cmp_uv(orig_lt_tri_uv[orig_i2_fidx], lt_tri_uv[i2_fidx]))
 				{
@@ -1317,40 +1340,25 @@ static float screen_px_line_point_factor_v2_persp(
 	return line_point_factor_v2(zero, v1_proj, v2_proj);
 }
 
-static bool project_face_pixel(
-        const float *lt_tri_uv[3], Image *ima_other, const float w[3],
-        unsigned char rgba_ub[4], float rgba_f[4], bool *is_float)
-{
-	if (!ima_other)
-		return false;
 
+static void project_face_pixel(
+        const float *lt_tri_uv[3], ImBuf *ibuf_other, const float w[3],
+        unsigned char rgba_ub[4], float rgba_f[4])
+{
 	float uv_other[2], x, y;
 
 	interp_v2_v2v2v2(uv_other, UNPACK3(lt_tri_uv), w);
 
-	ImageUser iuser = {NULL};
-	iuser.ok = true;
-	iuser.tile = BKE_image_get_tile_from_pos(ima_other, uv_other, uv_other, NULL);
-	ImBuf *ibuf_other = BKE_image_acquire_ibuf(ima_other, &iuser, NULL);
-
-	if (!ibuf_other)
-		return false;
-
 	/* use */
 	uvco_to_wrapped_pxco(uv_other, ibuf_other->x, ibuf_other->y, &x, &y);
 
 	if (ibuf_other->rect_float) { /* from float to float */
-		*is_float = true;
 		bilinear_interpolation_color_wrap(ibuf_other, NULL, rgba_f, x, y);
 	}
 	else { /* from char to float */
-		*is_float = false;
 		bilinear_interpolation_color_wrap(ibuf_other, rgba_ub, NULL, x, y);
 	}
 
-	BKE_image_release_ibuf(ima_other, ibuf_other, NULL);
-
-	return true;
 }
 
 /* run this outside project_paint_uvpixel_init since pixels with mask 0 don't need init */
@@ -1364,22 +1372,28 @@ static float project_paint_uvpixel_mask(
 	/* Image Mask */
 	if (ps->do_layer_stencil) {
 		/* another UV maps image is masking this one's */
+		ImBuf *ibuf_other;
 		Image *other_tpage = ps->stencil_ima;
-		const MLoopTri *lt_other = &ps->dm_mlooptri[tri_index];
-		const float *lt_other_tri_uv[3] = { PS_LOOPTRI_AS_UV_3(ps->dm_mloopuv, lt_other) };
-
-		unsigned char rgba_ub[4];
-		float rgba_f[4];
-		bool is_float;
-		/* BKE_image_acquire_ibuf - TODO - this may be slow */
-		if (project_face_pixel(lt_other_tri_uv, other_tpage, w, rgba_ub, rgba_f, &is_float)) {
-			if (is_float) { /* from float to float */
+
+		if (other_tpage && (ibuf_other = BKE_image_acquire_ibuf(other_tpage, NULL, NULL))) {
+			const MLoopTri *lt_other = &ps->dm_mlooptri[tri_index];
+			const float *lt_other_tri_uv[3] = { PS_LOOPTRI_AS_UV_3(ps->dm_mloopuv, lt_other) };
+
+			/* BKE_image_acquire_ibuf - TODO - this may be slow */
+			unsigned char rgba_ub[4];
+			float rgba_f[4];
+
+			project_face_pixel(lt_other_tri_uv, ibuf_other, w, rgba_ub, rgba_f);
+
+			if (ibuf_other->rect_float) { /* from float to float */
 				mask = ((rgba_f[0] + rgba_f[1] + rgba_f[2]) * (1.0f / 3.0f)) * rgba_f[3];
 			}
 			else { /* from char to float */
 				mask = ((rgba_ub[0] + rgba_ub[1] + rgba_ub[2]) * (1.0f / (255.0f * 3.0f))) * (rgba_ub[3] * (1.0f / 255.0f));
 			}
 
+			BKE_image_release_ibuf(other_tpage, ibuf_other, NULL);
+
 			if (!ps->do_layer_stencil_inv) /* matching the gimps layer mask black/white rules, white==full opacity */
 				mask = (1.0f - mask);
 
@@ -1564,15 +1578,8 @@ static ProjPixel *project_paint_uvpixel_init(
 	ImBuf *ibuf = projima->ibuf;
 	/* wrap pixel location */
 
-	if (projima->ima->source == IMA_SRC_TILED) {
-		if (x_px < 0 || y_px < 0 || x_px >= ibuf->x || y_px >= ibuf->y) {
-			return NULL;
-		}
-	}
-	else {
-		x_px = mod_i(x_px, ibuf->x);
-		y_px = mod_i(y_px, ibuf->y);
-	}
+	x_px = mod_i(x_px, ibuf->x);
+	y_px = mod_i(y_px, ibuf->y);
 
 	BLI_assert(ps->pixel_sizeof == project_paint_pixel_sizeof(ps->tool));
 	projPixel = BLI_memarena_alloc(arena, ps->pixel_sizeof);
@@ -1632,20 +1639,23 @@ static ProjPixel *project_paint_uvpixel_init(
 	/* done with view3d_project_float inline */
 	if (ps->tool == PAINT_TOOL_CLONE) {
 		if (ps->dm_mloopuv_clone) {
+			ImBuf *ibuf_other;
 			Image *other_tpage = project_paint_face_clone_image(ps, tri_index);
-			const MLoopTri *lt_other = &ps->dm_mlooptri[tri_index];
-			const float *lt_other_tri_uv[3] = { PS_LOOPTRI_AS_UV_3(ps->dm_mloopuv_clone, lt_other) };
 
-			unsigned char rgba_ub[4];
-			float rgba[4];
-			bool is_float;
-			/* BKE_image_acquire_ibuf - TODO - this may be slow */
-			if (project_face_pixel(lt_other_tri_uv, other_tpage, w, rgba_ub, rgba, &is_float)) {
+			if (other_tpage && (ibuf_other = BKE_image_acquire_ibuf(other_tpage, NULL, NULL))) {
+				const MLoopTri *lt_other = &ps->dm_mlooptri[tri_index];
+				const float *lt_other_tri_uv[3] = { PS_LOOPTRI_AS_UV_3(ps->dm_mloopuv_clone, lt_other) };
+
+				/* BKE_image_acquire_ibuf - TODO - this may be slow */
+
 				if (ibuf->rect_float) {
-					if (is_float) { /* from float to float */
-						copy_v4_v4(((ProjPixelClone *)projPixel)->clonepx.f, rgba);
+					if (ibuf_other->rect_float) { /* from float to float */
+						project_face_pixel(lt_other_tri_uv, ibuf_other, w, NULL, ((ProjPixelClone *)projPixel)->clonepx.f);
 					}
 					else { /* from char to float */
+						unsigned char rgba_ub[4];
+						float rgba[4];
+						project_face_pixel(lt_other_tri_uv, ibuf_other, w, rgba_ub, NULL);
 						if (ps->use_colormanagement) {
 							srgb_to_linearrgb_uchar4(rgba, rgba_ub);
 						}
@@ -1656,7 +1666,9 @@ static ProjPixel *project_paint_uvpixel_init(
 					}
 				}
 				else {
-					if (is_float) { /* float to char */
+					if (ibuf_other->rect_float) { /* float to char */
+						float rgba[4];
+						project_face_pixel(lt_other_tri_uv, ibuf_other, w, NULL, rgba);
 						premul_to_straight_v4(rgba);
 						if (ps->use_colormanagement) {
 							linearrgb_to_srgb_uchar3(((ProjPixelClone *)projPixel)->clonepx.ch, rgba);
@@ -1667,9 +1679,11 @@ static ProjPixel *project_paint_uvpixel_init(
 						((ProjPixelClone *)projPixel)->clonepx.ch[3] =  rgba[3] * 255;
 					}
 					else { /* char to char */
-						copy_v4_v4_uchar(((ProjPixelClone *)projPixel)->clonepx.ch, rgba_ub);
+						project_face_pixel(lt_other_tri_uv, ibuf_other, w, ((ProjPixelClone *)projPixel)->clonepx.ch, NULL);
 					}
 				}
+
+				BKE_image_release_ibuf(other_tpage, ibuf_other, NULL);
 			}
 			else {
 				if (ibuf->rect_float) {
@@ -2505,7 +2519,7 @@ static bool IsectPoly2Df_twoside(const float pt[2], float uv[][2], const int tot
 static void project_paint_face_init(
         const ProjPaintState *ps,
         const int thread_index, const int bucket_index, const int tri_index, const int image_index,
-        const rctf *clip_rect, const rctf *bucket_bounds, ImBuf *ibuf, ImBuf **tmpibuf, float uv_ofs[2])
+        const rctf *clip_rect, const rctf *bucket_bounds, ImBuf *ibuf, ImBuf **tmpibuf)
 {
 	/* Projection vars, to get the 3D locations into screen space  */
 	MemArena *arena = ps->arena_mt[thread_index];
@@ -2537,7 +2551,7 @@ static void project_paint_face_init(
 
 	float w[3], wco[3];
 
-	float uv1co[2], uv2co[2], uv3co[3];
+	float *uv1co, *uv2co, *uv3co; /* for convenience only, these will be assigned to lt_tri_uv[0],1,2 or lt_tri_uv[0],2,3 */
 	float pixelScreenCo[4];
 	bool do_3d_mapping = ps->brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D;
 
@@ -2587,9 +2601,9 @@ static void project_paint_face_init(
 	lt_uv_pxoffset[2][1] = lt_tri_uv[2][1] - yhalfpx;
 
 	{
-		sub_v2_v2v2(uv1co, lt_uv_pxoffset[0], uv_ofs);
-		sub_v2_v2v2(uv2co, lt_uv_pxoffset[1], uv_ofs);
-		sub_v2_v2v2(uv3co, lt_uv_pxoffset[2], uv_ofs);
+		uv1co = lt_uv_pxoffset[0]; // was lt_tri_uv[i1];
+		uv2co = lt_uv_pxoffset[1]; // was lt_tri_uv

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list