[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53134] trunk/blender/source/blender: Move multires baker into own file in render/

Sergey Sharybin sergey.vfx at gmail.com
Tue Dec 18 18:46:27 CET 2012


Revision: 53134
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53134
Author:   nazgul
Date:     2012-12-18 17:46:26 +0000 (Tue, 18 Dec 2012)
Log Message:
-----------
Move multires baker into own file in render/

Currently will only keep object editor a bit clearer, but in
the future will be needed because of using some internal renderer
data structures.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/object/object_bake.c
    trunk/blender/source/blender/render/CMakeLists.txt

Added Paths:
-----------
    trunk/blender/source/blender/render/extern/include/RE_multires_bake.h
    trunk/blender/source/blender/render/intern/source/multires_bake.c

Modified: trunk/blender/source/blender/editors/object/object_bake.c
===================================================================
--- trunk/blender/source/blender/editors/object/object_bake.c	2012-12-18 17:06:31 UTC (rev 53133)
+++ trunk/blender/source/blender/editors/object/object_bake.c	2012-12-18 17:46:26 UTC (rev 53134)
@@ -64,6 +64,7 @@
 
 #include "RE_pipeline.h"
 #include "RE_shader_ext.h"
+#include "RE_multires_bake.h"
 
 #include "PIL_time.h"
 
@@ -97,827 +98,6 @@
 	short mode, use_lores_mesh;
 } MultiresBakeJob;
 
-/* data passing to multires baker */
-typedef struct {
-	DerivedMesh *lores_dm, *hires_dm;
-	int simple, lvl, tot_lvl, bake_filter;
-	short mode, use_lores_mesh;
-
-	int tot_obj, tot_image;
-	ListBase image;
-
-	int baked_objects, baked_faces;
-
-	short *stop;
-	short *do_update;
-	float *progress;
-} MultiresBakeRender;
-
-typedef void (*MPassKnownData)(DerivedMesh *lores_dm, DerivedMesh *hires_dm, const void *bake_data,
-                               ImBuf *ibuf, const int face_index, const int lvl, const float st[2],
-                               float tangmat[3][3], const int x, const int y);
-
-typedef void * (*MInitBakeData)(MultiresBakeRender *bkr, Image *ima);
-typedef void   (*MApplyBakeData)(void *bake_data);
-typedef void   (*MFreeBakeData)(void *bake_data);
-
-typedef struct {
-	MVert *mvert;
-	MFace *mface;
-	MTFace *mtface;
-	float *pvtangent;
-	float *precomputed_normals;
-	int w, h;
-	int face_index;
-	int i0, i1, i2;
-	DerivedMesh *lores_dm, *hires_dm;
-	int lvl;
-	void *bake_data;
-	ImBuf *ibuf;
-	MPassKnownData pass_data;
-} MResolvePixelData;
-
-typedef void (*MFlushPixel)(const MResolvePixelData *data, const int x, const int y);
-
-typedef struct {
-	int w, h;
-	char *texels;
-	const MResolvePixelData *data;
-	MFlushPixel flush_pixel;
-} MBakeRast;
-
-typedef struct {
-	float *heights;
-	float height_min, height_max;
-	Image *ima;
-	DerivedMesh *ssdm;
-	const int *orig_index_mf_to_mpoly;
-	const int *orig_index_mp_to_orig;
-} MHeightBakeData;
-
-typedef struct {
-	const int *orig_index_mf_to_mpoly;
-	const int *orig_index_mp_to_orig;
-} MNormalBakeData;
-
-static void multiresbake_get_normal(const MResolvePixelData *data, float norm[], const int face_num, const int vert_index)
-{
-	unsigned int indices[] = {data->mface[face_num].v1, data->mface[face_num].v2,
-	                          data->mface[face_num].v3, data->mface[face_num].v4};
-	const int smoothnormal = (data->mface[face_num].flag & ME_SMOOTH);
-
-	if (!smoothnormal) { /* flat */
-		if (data->precomputed_normals) {
-			copy_v3_v3(norm, &data->precomputed_normals[3 * face_num]);
-		}
-		else {
-			float nor[3];
-			float *p0, *p1, *p2;
-			const int iGetNrVerts = data->mface[face_num].v4 != 0 ? 4 : 3;
-
-			p0 = data->mvert[indices[0]].co;
-			p1 = data->mvert[indices[1]].co;
-			p2 = data->mvert[indices[2]].co;
-
-			if (iGetNrVerts == 4) {
-				float *p3 = data->mvert[indices[3]].co;
-				normal_quad_v3(nor, p0, p1, p2, p3);
-			}
-			else {
-				normal_tri_v3(nor, p0, p1, p2);
-			}
-
-			copy_v3_v3(norm, nor);
-		}
-	}
-	else {
-		short *no = data->mvert[indices[vert_index]].no;
-
-		normal_short_to_float_v3(norm, no);
-		normalize_v3(norm);
-	}
-}
-
-static void init_bake_rast(MBakeRast *bake_rast, const ImBuf *ibuf, const MResolvePixelData *data, MFlushPixel flush_pixel)
-{
-	memset(bake_rast, 0, sizeof(MBakeRast));
-
-	bake_rast->texels = ibuf->userdata;
-	bake_rast->w = ibuf->x;
-	bake_rast->h = ibuf->y;
-	bake_rast->data = data;
-	bake_rast->flush_pixel = flush_pixel;
-}
-
-static void flush_pixel(const MResolvePixelData *data, const int x, const int y)
-{
-	float st[2] = {(x + 0.5f) / data->w, (y + 0.5f) / data->h};
-	float *st0, *st1, *st2;
-	float *tang0, *tang1, *tang2;
-	float no0[3], no1[3], no2[3];
-	float fUV[2], from_tang[3][3], to_tang[3][3];
-	float u, v, w, sign;
-	int r;
-
-	const int i0 = data->i0;
-	const int i1 = data->i1;
-	const int i2 = data->i2;
-
-	st0 = data->mtface[data->face_index].uv[i0];
-	st1 = data->mtface[data->face_index].uv[i1];
-	st2 = data->mtface[data->face_index].uv[i2];
-
-	tang0 = data->pvtangent + data->face_index * 16 + i0 * 4;
-	tang1 = data->pvtangent + data->face_index * 16 + i1 * 4;
-	tang2 = data->pvtangent + data->face_index * 16 + i2 * 4;
-
-	multiresbake_get_normal(data, no0, data->face_index, i0);   /* can optimize these 3 into one call */
-	multiresbake_get_normal(data, no1, data->face_index, i1);
-	multiresbake_get_normal(data, no2, data->face_index, i2);
-
-	resolve_tri_uv(fUV, st, st0, st1, st2);
-
-	u = fUV[0];
-	v = fUV[1];
-	w = 1 - u - v;
-
-	/* the sign is the same at all face vertices for any non degenerate face.
-	 * Just in case we clamp the interpolated value though. */
-	sign = (tang0[3] * u + tang1[3] * v + tang2[3] * w) < 0 ? (-1.0f) : 1.0f;
-
-	/* this sequence of math is designed specifically as is with great care
-	 * to be compatible with our shader. Please don't change without good reason. */
-	for (r = 0; r < 3; r++) {
-		from_tang[0][r] = tang0[r] * u + tang1[r] * v + tang2[r] * w;
-		from_tang[2][r] = no0[r] * u + no1[r] * v + no2[r] * w;
-	}
-
-	cross_v3_v3v3(from_tang[1], from_tang[2], from_tang[0]);  /* B = sign * cross(N, T)  */
-	mul_v3_fl(from_tang[1], sign);
-	invert_m3_m3(to_tang, from_tang);
-	/* sequence end */
-
-	data->pass_data(data->lores_dm, data->hires_dm, data->bake_data,
-	                data->ibuf, data->face_index, data->lvl, st, to_tang, x, y);
-}
-
-static void set_rast_triangle(const MBakeRast *bake_rast, const int x, const int y)
-{
-	const int w = bake_rast->w;
-	const int h = bake_rast->h;
-
-	if (x >= 0 && x < w && y >= 0 && y < h) {
-		if ((bake_rast->texels[y * w + x]) == 0) {
-			flush_pixel(bake_rast->data, x, y);
-			bake_rast->texels[y * w + x] = FILTER_MASK_USED;
-		}
-	}
-}
-
-static void rasterize_half(const MBakeRast *bake_rast,
-                           const float s0_s, const float t0_s, const float s1_s, const float t1_s,
-                           const float s0_l, const float t0_l, const float s1_l, const float t1_l,
-                           const int y0_in, const int y1_in, const int is_mid_right)
-{
-	const int s_stable = fabsf(t1_s - t0_s) > FLT_EPSILON ? 1 : 0;
-	const int l_stable = fabsf(t1_l - t0_l) > FLT_EPSILON ? 1 : 0;
-	const int w = bake_rast->w;
-	const int h = bake_rast->h;
-	int y, y0, y1;
-
-	if (y1_in <= 0 || y0_in >= h)
-		return;
-
-	y0 = y0_in < 0 ? 0 : y0_in;
-	y1 = y1_in >= h ? h : y1_in;
-
-	for (y = y0; y < y1; y++) {
-		/*-b(x-x0) + a(y-y0) = 0 */
-		int iXl, iXr, x;
-		float x_l = s_stable != 0 ? (s0_s + (((s1_s - s0_s) * (y - t0_s)) / (t1_s - t0_s))) : s0_s;
-		float x_r = l_stable != 0 ? (s0_l + (((s1_l - s0_l) * (y - t0_l)) / (t1_l - t0_l))) : s0_l;
-
-		if (is_mid_right != 0)
-			SWAP(float, x_l, x_r);
-
-		iXl = (int)ceilf(x_l);
-		iXr = (int)ceilf(x_r);
-
-		if (iXr > 0 && iXl < w) {
-			iXl = iXl < 0 ? 0 : iXl;
-			iXr = iXr >= w ? w : iXr;
-
-			for (x = iXl; x < iXr; x++)
-				set_rast_triangle(bake_rast, x, y);
-		}
-	}
-}
-
-static void bake_rasterize(const MBakeRast *bake_rast, const float st0_in[2], const float st1_in[2], const float st2_in[2])
-{
-	const int w = bake_rast->w;
-	const int h = bake_rast->h;
-	float slo = st0_in[0] * w - 0.5f;
-	float tlo = st0_in[1] * h - 0.5f;
-	float smi = st1_in[0] * w - 0.5f;
-	float tmi = st1_in[1] * h - 0.5f;
-	float shi = st2_in[0] * w - 0.5f;
-	float thi = st2_in[1] * h - 0.5f;
-	int is_mid_right = 0, ylo, yhi, yhi_beg;
-
-	/* skip degenerates */
-	if ((slo == smi && tlo == tmi) || (slo == shi && tlo == thi) || (smi == shi && tmi == thi))
-		return;
-
-	/* sort by T */
-	if (tlo > tmi && tlo > thi) {
-		SWAP(float, shi, slo);
-		SWAP(float, thi, tlo);
-	}
-	else if (tmi > thi) {
-		SWAP(float, shi, smi);
-		SWAP(float, thi, tmi);
-	}
-
-	if (tlo > tmi) {
-		SWAP(float, slo, smi);
-		SWAP(float, tlo, tmi);
-	}
-
-	/* check if mid point is to the left or to the right of the lo-hi edge */
-	is_mid_right = (-(shi - slo) * (tmi - thi) + (thi - tlo) * (smi - shi)) > 0 ? 1 : 0;
-	ylo = (int) ceilf(tlo);
-	yhi_beg = (int) ceilf(tmi);
-	yhi = (int) ceilf(thi);
-
-	/*if (fTmi>ceilf(fTlo))*/
-	rasterize_half(bake_rast, slo, tlo, smi, tmi, slo, tlo, shi, thi, ylo, yhi_beg, is_mid_right);
-	rasterize_half(bake_rast, smi, tmi, shi, thi, slo, tlo, shi, thi, yhi_beg, yhi, is_mid_right);
-}
-
-static int multiresbake_test_break(MultiresBakeRender *bkr)
-{
-	if (!bkr->stop) {
-		/* this means baker is executed outside from job system */
-		return 0;
-	}
-
-	return G.is_break;
-}
-
-static void do_multires_bake(MultiresBakeRender *bkr, Image *ima, MPassKnownData passKnownData,
-                             MInitBakeData initBakeData, MApplyBakeData applyBakeData, MFreeBakeData freeBakeData)
-{
-	DerivedMesh *dm = bkr->lores_dm;
-	ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
-	const int lvl = bkr->lvl;
-	const int tot_face = dm->getNumTessFaces(dm);
-	MVert *mvert = dm->getVertArray(dm);
-	MFace *mface = dm->getTessFaceArray(dm);
-	MTFace *mtface = dm->getTessFaceDataArray(dm, CD_MTFACE);
-	float *pvtangent = NULL;
-
-	if (CustomData_get_layer_index(&dm->faceData, CD_TANGENT) == -1)
-		DM_add_tangent_layer(dm);
-
-	pvtangent = DM_get_tessface_data_layer(dm, CD_TANGENT);
-
-	if (tot_face > 0) {  /* sanity check */
-		int f = 0;
-		MBakeRast bake_rast;
-		MResolvePixelData data = {NULL};
-
-		data.mface = mface;
-		data.mvert = mvert;
-		data.mtface = mtface;
-		data.pvtangent = pvtangent;
-		data.precomputed_normals = dm->getTessFaceDataArray(dm, CD_NORMAL);  /* don't strictly need this */
-		data.w = ibuf->x;
-		data.h = ibuf->y;
-		data.lores_dm = dm;
-		data.hires_dm = bkr->hires_dm;
-		data.lvl = lvl;
-		data.pass_data = passKnownData;
-
-		if (initBakeData)
-			data.bake_data = initBakeData(bkr, ima);
-
-		init_bake_rast(&bake_rast, ibuf, &data, flush_pixel);
-
-		for (f = 0; f < tot_face; f++) {
-			MTFace *mtfate = &mtface[f];
-			int verts[3][2], nr_tris, t;
-
-			if (multiresbake_test_break(bkr))
-				break;
-
-			if (mtfate->tpage != ima)
-				continue;
-
-			data.face_index = f;
-			data.ibuf = ibuf;
-
-			/* might support other forms of diagonal splits later on such as
-			 * split by shortest diagonal.*/
-			verts[0][0] = 0;
-			verts[1][0] = 1;
-			verts[2][0] = 2;
-
-			verts[0][1] = 0;
-			verts[1][1] = 2;
-			verts[2][1] = 3;
-
-			nr_tris = mface[f].v4 != 0 ? 2 : 1;
-			for (t = 0; t < nr_tris; t++) {
-				data.i0 = verts[0][t];
-				data.i1 = verts[1][t];
-				data.i2 = verts[2][t];
-

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list