[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [41815] trunk/blender/source/blender: Ocean baking was using uninitialized memory, but further investigation it was calculating foam values when they were not used .

Campbell Barton ideasman42 at gmail.com
Mon Nov 14 06:55:50 CET 2011


Revision: 41815
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=41815
Author:   campbellbarton
Date:     2011-11-14 05:55:50 +0000 (Mon, 14 Nov 2011)
Log Message:
-----------
Ocean baking was using uninitialized memory, but further investigation it was calculating foam values when they were not used.

avoid calculating foam and allocating memory when its not needed.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/implicit.c
    trunk/blender/source/blender/blenkernel/intern/ocean.c
    trunk/blender/source/blender/render/intern/source/convertblender.c

Modified: trunk/blender/source/blender/blenkernel/intern/implicit.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/implicit.c	2011-11-14 05:52:06 UTC (rev 41814)
+++ trunk/blender/source/blender/blenkernel/intern/implicit.c	2011-11-14 05:55:50 UTC (rev 41815)
@@ -1814,7 +1814,7 @@
 		
 		/*compute forces*/
 		sub_v3_v3v3(vec, cos[i], cv->tx);
-		mul_v3_fl(vec, cv->mass*dt*20.0);
+		mul_v3_fl(vec, cv->mass*dt*20.0f);
 		add_v3_v3(cv->tv, vec);
 		//copy_v3_v3(cv->tx, cos[i]);
 	}

Modified: trunk/blender/source/blender/blenkernel/intern/ocean.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/ocean.c	2011-11-14 05:52:06 UTC (rev 41814)
+++ trunk/blender/source/blender/blenkernel/intern/ocean.c	2011-11-14 05:55:50 UTC (rev 41815)
@@ -490,6 +490,8 @@
 		ocr->normal[0] = oc->_N_x[i*oc->_N+j];
 		ocr->normal[1] = oc->_N_y/*oc->_N_y[i*oc->_N+j] (MEM01)*/;
 		ocr->normal[2] = oc->_N_z[i*oc->_N+j];
+
+		normalize_v3(ocr->normal);
 	}
 
 	if (oc->_do_jacobian)
@@ -1175,9 +1177,14 @@
 
 void BKE_bake_ocean(struct Ocean *o, struct OceanCache *och, void (*update_cb)(void *, float progress, int *cancel), void *update_cb_data)
 {
+	/* note: some of these values remain uninitialized unless certain options
+	 * are enabled, take care that BKE_ocean_eval_ij() initializes a member
+	 * before use - campbell */
+	OceanResult ocr;
+
 	int f, i=0, x, y, cancel=0;
 	float progress;
-	OceanResult ocr;
+
 	ImBuf *ibuf_foam, *ibuf_disp, *ibuf_normal;
 	float *prev_foam;
 	int res_x = och->resolution_x;
@@ -1186,7 +1193,8 @@
 
 	if (!o) return;
 
-	prev_foam = MEM_callocN(res_x*res_y*sizeof(float), "previous frame foam bake data");
+	if (o->_do_jacobian) prev_foam = MEM_callocN(res_x*res_y*sizeof(float), "previous frame foam bake data");
+	else                 prev_foam = NULL;
 
 	BLI_srand(0);
 
@@ -1204,64 +1212,66 @@
 		/* add new foam */
 		for (y=0; y < res_y; y++) {
 			for (x=0; x < res_x; x++) {
-				float /*r,*/ /* UNUSED */ pr=0.0f, foam_result;
-				float neg_disp, neg_eplus;
 
 				BKE_ocean_eval_ij(o, &ocr, x, y);
 
-				normalize_v3(ocr.normal);
+				/* add to the image */
+				ibuf_disp->rect_float[4*(res_x*y + x) + 0] = ocr.disp[0];
+				ibuf_disp->rect_float[4*(res_x*y + x) + 1] = ocr.disp[1];
+				ibuf_disp->rect_float[4*(res_x*y + x) + 2] = ocr.disp[2];
+				ibuf_disp->rect_float[4*(res_x*y + x) + 3] = 1.0f;
 
-				/* foam */
-				ocr.foam = BKE_ocean_jminus_to_foam(ocr.Jminus, och->foam_coverage);
+				if (o->_do_jacobian) {
+					/* TODO, cleanup unused code - campbell */
 
-				/* accumulate previous value for this cell */
-				if (i>0)
-					pr = prev_foam[res_x*y + x];
+					float /*r,*/ /* UNUSED */ pr=0.0f, foam_result;
+					float neg_disp, neg_eplus;
 
-				/* r = BLI_frand(); */ /* UNUSED */ // randomly reduce foam
+					ocr.foam = BKE_ocean_jminus_to_foam(ocr.Jminus, och->foam_coverage);
 
-				//pr = pr * och->foam_fade;		// overall fade
+					/* accumulate previous value for this cell */
+					if (i > 0) {
+						pr = prev_foam[res_x*y + x];
+					}
 
-				// remember ocean coord sys is Y up!
-				// break up the foam where height (Y) is low (wave valley),
-				// and X and Z displacement is greatest
+					/* r = BLI_frand(); */ /* UNUSED */ // randomly reduce foam
 
-				/*
-				 vec[0] = ocr.disp[0];
-				vec[1] = ocr.disp[2];
-				hor_stretch = len_v2(vec);
-				CLAMP(hor_stretch, 0.0, 1.0);
-				*/
+					//pr = pr * och->foam_fade;		// overall fade
 
-				neg_disp = ocr.disp[1] < 0.0f ? 1.0f+ocr.disp[1] : 1.0f;
-				neg_disp = neg_disp < 0.0f ? 0.0f : neg_disp;
+					// remember ocean coord sys is Y up!
+					// break up the foam where height (Y) is low (wave valley),
+					// and X and Z displacement is greatest
 
-				neg_eplus = ocr.Eplus[2] < 0.0f ? 1.0f + ocr.Eplus[2]:1.0f;
-				neg_eplus = neg_eplus<0.0f ? 0.0f : neg_eplus;
+					/*
+					vec[0] = ocr.disp[0];
+					vec[1] = ocr.disp[2];
+					hor_stretch = len_v2(vec);
+					CLAMP(hor_stretch, 0.0, 1.0);
+					*/
 
-				//if (ocr.disp[1] < 0.0 || r > och->foam_fade)
-				//	pr *= och->foam_fade;
+					neg_disp = ocr.disp[1] < 0.0f ? 1.0f+ocr.disp[1] : 1.0f;
+					neg_disp = neg_disp < 0.0f ? 0.0f : neg_disp;
 
+					/* foam, 'ocr.Eplus' only initialized with do_jacobian */
+					neg_eplus = ocr.Eplus[2] < 0.0f ? 1.0f + ocr.Eplus[2]:1.0f;
+					neg_eplus = neg_eplus<0.0f ? 0.0f : neg_eplus;
 
-				//pr = pr * (1.0 - hor_stretch) * ocr.disp[1];
-				//pr = pr * neg_disp * neg_eplus;
+					//if (ocr.disp[1] < 0.0 || r > och->foam_fade)
+					//	pr *= och->foam_fade;
 
-				if (pr < 1.0f) pr *=pr;
 
-				pr *= och->foam_fade * (0.75f + neg_eplus * 0.25f);
+					//pr = pr * (1.0 - hor_stretch) * ocr.disp[1];
+					//pr = pr * neg_disp * neg_eplus;
 
+					if (pr < 1.0f) pr *=pr;
 
-				foam_result = pr + ocr.foam;
+					pr *= och->foam_fade * (0.75f + neg_eplus * 0.25f);
 
-				prev_foam[res_x*y + x] = foam_result;
 
-				/* add to the image */
-				ibuf_disp->rect_float[4*(res_x*y + x) + 0] = ocr.disp[0];
-				ibuf_disp->rect_float[4*(res_x*y + x) + 1] = ocr.disp[1];
-				ibuf_disp->rect_float[4*(res_x*y + x) + 2] = ocr.disp[2];
-				ibuf_disp->rect_float[4*(res_x*y + x) + 3] = 1.0f;
+					foam_result = pr + ocr.foam;
 
-				if (o->_do_jacobian) {
+					prev_foam[res_x*y + x] = foam_result;
+
 					ibuf_foam->rect_float[4*(res_x*y + x) + 0] = foam_result;
 					ibuf_foam->rect_float[4*(res_x*y + x) + 1] = foam_result;
 					ibuf_foam->rect_float[4*(res_x*y + x) + 2] = foam_result;
@@ -1274,7 +1284,6 @@
 					ibuf_normal->rect_float[4*(res_x*y + x) + 2] = ocr.normal[2];
 					ibuf_normal->rect_float[4*(res_x*y + x) + 3] = 1.0;
 				}
-
 			}
 		}
 
@@ -1304,12 +1313,12 @@
 		update_cb(update_cb_data, progress, &cancel);
 
 		if (cancel) {
-			MEM_freeN(prev_foam);
+			if (prev_foam) MEM_freeN(prev_foam);
 			return;
 		}
 	}
 
-	MEM_freeN(prev_foam);
+	if (prev_foam) MEM_freeN(prev_foam);
 	och->baked = 1;
 }
 

Modified: trunk/blender/source/blender/render/intern/source/convertblender.c
===================================================================
--- trunk/blender/source/blender/render/intern/source/convertblender.c	2011-11-14 05:52:06 UTC (rev 41814)
+++ trunk/blender/source/blender/render/intern/source/convertblender.c	2011-11-14 05:55:50 UTC (rev 41815)
@@ -5264,7 +5264,7 @@
 		if(vec[0]<0.0f) ang= -ang;
 		zco[0]= ang/pixelphix + zmulx;
 		
-		ang= 0.5f*M_PI - saacos(vec[1]/sqrtf(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]));
+		ang= 0.5f*(float)M_PI - saacos(vec[1]/sqrtf(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]));
 		zco[1]= ang/pixelphiy + zmuly;
 		
 	}




More information about the Bf-blender-cvs mailing list