[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42076] trunk/blender: ocean sim

Campbell Barton ideasman42 at gmail.com
Tue Nov 22 19:03:34 CET 2011


Revision: 42076
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42076
Author:   campbellbarton
Date:     2011-11-22 18:03:33 +0000 (Tue, 22 Nov 2011)
Log Message:
-----------
ocean sim
- UV's were not being calculated if there were too many VColor layers.
- precalc (omd->size * omd->spatial_size) was being called in a loop.
- use vector functions to avoid pointer indrections on each access which the compiler wont optimize - eg: och->ibufs_disp[f]->rect_float[4*(res_x*j + i) + 1]
- dont call abs() on ints (converts to double and back to int in this case).

also unrelated render buttons change. move saving options directly under the file path since these were easy to confuse with image format options like zbuf, ycc, preview.. etc.

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_ui/properties_render.py
    trunk/blender/source/blender/blenkernel/intern/ocean.c
    trunk/blender/source/blender/modifiers/intern/MOD_ocean.c

Modified: trunk/blender/release/scripts/startup/bl_ui/properties_render.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/properties_render.py	2011-11-22 17:49:06 UTC (rev 42075)
+++ trunk/blender/release/scripts/startup/bl_ui/properties_render.py	2011-11-22 18:03:33 UTC (rev 42076)
@@ -456,13 +456,14 @@
         file_format = rd.image_settings.file_format
 
         layout.prop(rd, "filepath", text="")
-        layout.template_image_settings(rd.image_settings)
         
         flow = layout.column_flow()
         flow.prop(rd, "use_overwrite")
         flow.prop(rd, "use_placeholder")
         flow.prop(rd, "use_file_extension")
 
+        layout.template_image_settings(rd.image_settings)
+
         if file_format == 'QUICKTIME_CARBON':
             layout.operator("scene.render_data_set_quicktime_codec")
 

Modified: trunk/blender/source/blender/blenkernel/intern/ocean.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/ocean.c	2011-11-22 17:49:06 UTC (rev 42075)
+++ trunk/blender/source/blender/blenkernel/intern/ocean.c	2011-11-22 18:03:33 UTC (rev 42076)
@@ -313,8 +313,8 @@
 	float uu,vv;
 
 	// first wrap the texture so 0 <= (u,v) < 1
-	u = fmod(u,1.0f);
-	v = fmod(v,1.0f);
+	u = fmodf(u,1.0f);
+	v = fmodf(v,1.0f);
 
 	if (u < 0) u += 1.0f;
 	if (v < 0) v += 1.0f;
@@ -1021,6 +1021,22 @@
 	BKE_makepicstring(string, cachepath, relbase, frame, R_IMF_IMTYPE_OPENEXR, 1, TRUE);
 }
 
+/* silly functions but useful to inline when the args do a lot of indirections */
+MINLINE void rgb_to_rgba_unit_alpha(float r_rgba[4], const float rgb[3])
+{
+	r_rgba[0]= rgb[0];
+	r_rgba[1]= rgb[1];
+	r_rgba[2]= rgb[2];
+	r_rgba[3]= 1.0f;
+}
+MINLINE void value_to_rgba_unit_alpha(float r_rgba[4], const float value)
+{
+	r_rgba[0]= value;
+	r_rgba[1]= value;
+	r_rgba[2]= value;
+	r_rgba[3]= 1.0f;
+}
+
 void BKE_free_ocean_cache(struct OceanCache *och)
 {
 	int i, f=0;
@@ -1076,9 +1092,7 @@
 
 	if (och->ibufs_disp[f]) {
 		ibuf_sample(och->ibufs_disp[f], u, v, (1.0f/(float)res_x), (1.0f/(float)res_y), result);
-		ocr->disp[0] = result[0];
-		ocr->disp[1] = result[1];
-		ocr->disp[2] = result[2];
+		copy_v3_v3(ocr->disp, result);
 	}
 
 	if (och->ibufs_foam[f]) {
@@ -1088,34 +1102,31 @@
 
 	if (och->ibufs_norm[f]) {
 		ibuf_sample(och->ibufs_norm[f], u, v, (1.0f/(float)res_x), (1.0f/(float)res_y), result);
-		ocr->normal[0] = result[0];
-		ocr->normal[1] = result[1];
-		ocr->normal[2] = result[2];
+		copy_v3_v3(ocr->normal, result);
 	}
 }
 
 void BKE_ocean_cache_eval_ij(struct OceanCache *och, struct OceanResult *ocr, int f, int i, int j)
 {
-	int res_x = och->resolution_x;
-	int res_y = och->resolution_y;
+	const int res_x = och->resolution_x;
+	const int res_y = och->resolution_y;
 
-	i = abs(i) % res_x;
-	j = abs(j) % res_y;
+	if (i < 0) i= -i;
+	if (j < 0) j= -j;
 
+	i = i % res_x;
+	j = j % res_y;
+
 	if (och->ibufs_disp[f]) {
-		ocr->disp[0] = och->ibufs_disp[f]->rect_float[4*(res_x*j + i) + 0];
-		ocr->disp[1] = och->ibufs_disp[f]->rect_float[4*(res_x*j + i) + 1];
-		ocr->disp[2] = och->ibufs_disp[f]->rect_float[4*(res_x*j + i) + 2];
+		copy_v3_v3(ocr->disp, &och->ibufs_disp[f]->rect_float[4*(res_x*j + i)]);
 	}
 
 	if (och->ibufs_foam[f]) {
-		ocr->foam = och->ibufs_foam[f]->rect_float[4*(res_x*j + i) + 0];
+		ocr->foam = och->ibufs_foam[f]->rect_float[4*(res_x*j + i)];
 	}
 
 	if (och->ibufs_norm[f]) {
-		ocr->normal[0] = och->ibufs_norm[f]->rect_float[4*(res_x*j + i) + 0];
-		ocr->normal[1] = och->ibufs_norm[f]->rect_float[4*(res_x*j + i) + 1];
-		ocr->normal[2] = och->ibufs_norm[f]->rect_float[4*(res_x*j + i) + 2];
+		copy_v3_v3(ocr->normal, &och->ibufs_norm[f]->rect_float[4*(res_x*j + i)]);
 	}
 }
 
@@ -1206,7 +1217,7 @@
 	/* setup image format */
 	imf.imtype= R_IMF_IMTYPE_OPENEXR;
 	imf.depth=  R_IMF_CHAN_DEPTH_16;
-	imf.exr_codec= R_IMF_EXR_CODEC_ZIP; /* ZIP */
+	imf.exr_codec= R_IMF_EXR_CODEC_ZIP;
 
 	for (f=och->start, i=0; f<=och->end; f++, i++) {
 
@@ -1226,10 +1237,7 @@
 				BKE_ocean_eval_ij(o, &ocr, x, y);
 
 				/* 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;
+				rgb_to_rgba_unit_alpha(&ibuf_disp->rect_float[4*(res_x*y + x)], ocr.disp);
 
 				if (o->_do_jacobian) {
 					/* TODO, cleanup unused code - campbell */
@@ -1282,17 +1290,11 @@
 
 					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;
-					ibuf_foam->rect_float[4*(res_x*y + x) + 3] = 1.0;
+					value_to_rgba_unit_alpha(&ibuf_foam->rect_float[4*(res_x*y + x)], foam_result);
 				}
 
 				if (o->_do_normals) {
-					ibuf_normal->rect_float[4*(res_x*y + x) + 0] = ocr.normal[0];
-					ibuf_normal->rect_float[4*(res_x*y + x) + 1] = ocr.normal[1];
-					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;
+					rgb_to_rgba_unit_alpha(&ibuf_normal->rect_float[4*(res_x*y + x)], ocr.normal);
 				}
 			}
 		}

Modified: trunk/blender/source/blender/modifiers/intern/MOD_ocean.c
===================================================================
--- trunk/blender/source/blender/modifiers/intern/MOD_ocean.c	2011-11-22 17:49:06 UTC (rev 42075)
+++ trunk/blender/source/blender/modifiers/intern/MOD_ocean.c	2011-11-22 18:03:33 UTC (rev 42076)
@@ -78,10 +78,10 @@
 
 	BKE_free_ocean_data(omd->ocean);
 	BKE_init_ocean(omd->ocean, omd->resolution*omd->resolution, omd->resolution*omd->resolution, omd->spatial_size, omd->spatial_size,
-				   omd->wind_velocity, omd->smallest_wave, 1.0, omd->wave_direction, omd->damp, omd->wave_alignment,
-				   omd->depth, omd->time,
-				   do_heightfield, do_chop, do_normals, do_jacobian,
-				   omd->seed);
+	               omd->wind_velocity, omd->smallest_wave, 1.0, omd->wave_direction, omd->damp, omd->wave_alignment,
+	               omd->depth, omd->time,
+	               do_heightfield, do_chop, do_normals, do_jacobian,
+	               omd->seed);
 }
 
 static void simulate_ocean_modifier(struct OceanModifierData *omd)
@@ -90,7 +90,7 @@
 
 	BKE_simulate_ocean(omd->ocean, omd->time, omd->wave_scale, omd->chop_amount);
 }
-#endif // WITH_OCEANSIM
+#endif /* WITH_OCEANSIM */
 
 
 
@@ -261,13 +261,7 @@
 #endif
 
 #ifdef WITH_OCEANSIM
-MINLINE float ocean_co(OceanModifierData *omd, float v)
-{
-	//float scale = 1.0 / (omd->size * omd->spatial_size);
-	//*v = (*v * scale) + 0.5;
 
-	return (v / (omd->size * omd->spatial_size)) + 0.5f;
-}
 
 #define OMP_MIN_RES	18
 static DerivedMesh *generate_ocean_geometry(OceanModifierData *omd)
@@ -378,11 +372,8 @@
 	DerivedMesh *dm=NULL;
 	OceanResult ocr;
 
-	MVert *mv;
-	MFace *mf;
+	MVert *mverts, *mv;
 
-	int cdlayer;
-
 	int i, j;
 
 	int num_verts;
@@ -390,6 +381,14 @@
 
 	int cfra;
 
+	/* use cached & inverted value for speed
+	 * expanded this would read...
+	 *
+	 * (axis / (omd->size * omd->spatial_size)) + 0.5f) */
+#define OCEAN_CO(_size_co_inv, _v) ((_v * _size_co_inv) + 0.5f)
+
+	const float size_co_inv= 1.0f / (omd->size * omd->spatial_size);
+
 	/* update modifier */
 	if (omd->refresh & MOD_OCEAN_REFRESH_ADD)
 		omd->ocean = BKE_add_ocean();
@@ -422,72 +421,71 @@
 	num_verts = dm->getNumVerts(dm);
 	num_faces = dm->getNumFaces(dm);
 
+	mverts = dm->getVertArray(dm);
+
 	/* add vcols before displacement - allows lookup based on position */
 
 	if (omd->flag & MOD_OCEAN_GENERATE_FOAM) {
-		MCol *mc;
-		float foam;
-		char cf;
+		int cdlayer= CustomData_number_of_layers(&dm->faceData, CD_MCOL);
 
-		float u=0.0, v=0.0;
+		if(cdlayer < MAX_MCOL) {
+			MFace *mfaces= dm->getFaceArray(dm);
+			MFace *mf;
 
-		cdlayer= CustomData_number_of_layers(&dm->faceData, CD_MCOL);
-		if(cdlayer >= MAX_MCOL)
-			return dm;
+			MCol *mcols, *mc;
+			float foam;
 
-		CustomData_add_layer_named(&dm->faceData, CD_MCOL, CD_CALLOC, NULL, num_faces, omd->foamlayername);
+			CustomData_add_layer_named(&dm->faceData, CD_MCOL, CD_CALLOC, NULL, num_faces, omd->foamlayername);
 
-		mc = dm->getFaceDataArray(dm, CD_MCOL);
-		mv = dm->getVertArray(dm);
-		mf = dm->getFaceArray(dm);
+			mcols = dm->getFaceDataArray(dm, CD_MCOL);
 
-		for (i = 0; i < num_faces; i++, mf++) {
-			j= mf->v4 ? 3 : 2;
-			do {
-				const float *co= mv[*(&mf->v1 + j)].co;
-				u = ocean_co(omd, co[0]);
-				v = ocean_co(omd, co[1]);
+			for (i = 0, mf= mfaces; i < num_faces; i++, mf++) {
+				j= mf->v4 ? 3 : 2;
+				do {
+					const float *co= mverts[*(&mf->v1 + j)].co;
+					const float u = OCEAN_CO(size_co_inv, co[0]);
+					const float v = OCEAN_CO(size_co_inv, co[1]);
 
-				if (omd->oceancache && omd->cached==TRUE) {
-					BKE_ocean_cache_eval_uv(omd->oceancache, &ocr, cfra, u, v);
-					foam = ocr.foam;
-					CLAMP(foam, 0.0f, 1.0f);
-				}
-				else {
-					BKE_ocean_eval_uv(omd->ocean, &ocr, u, v);
-					foam = BKE_ocean_jminus_to_foam(ocr.Jminus, omd->foam_coverage);
-				}
+					if (omd->oceancache && omd->cached==TRUE) {
+						BKE_ocean_cache_eval_uv(omd->oceancache, &ocr, cfra, u, v);
+						foam = ocr.foam;
+						CLAMP(foam, 0.0f, 1.0f);
+					}
+					else {
+						BKE_ocean_eval_uv(omd->ocean, &ocr, u, v);
+						foam = BKE_ocean_jminus_to_foam(ocr.Jminus, omd->foam_coverage);
+					}
 
-				cf = (char)(foam * 255);
-				mc[i*4 + j].r = mc[i*4 + j].g = mc[i*4 + j].b = cf;
-				mc[i*4 + j].a = 255;
-			} while (j--);
+					mc= &mcols[i*4 + j];
+					mc->r = mc->g = mc->b = (char)(foam * 255);
+					/* mc->a = 255; */ /* no need to set */
+				} while (j--);
+			}
 		}
 	}
 
 
 	/* displace the geometry */
 
-	mv = dm->getVertArray(dm);
-
 	//#pragma omp parallel for private(i, ocr) if (omd->resolution > OMP_MIN_RES)
-	for (i=0; i< num_verts; i++) {
-		const float u = ocean_co(omd, mv[i].co[0]);
-		const float v = ocean_co(omd, mv[i].co[1]);
+	for (i=0, mv= mverts; i< num_verts; i++, mv++) {
+		const float u = OCEAN_CO(size_co_inv, mv->co[0]);
+		const float v = OCEAN_CO(size_co_inv, mv->co[1]);
 
 		if (omd->oceancache && omd->cached==TRUE)
 			BKE_ocean_cache_eval_uv(omd->oceancache, &ocr, cfra, u, v);
 		else
 			BKE_ocean_eval_uv(omd->ocean, &ocr, u, v);
 
-		mv[i].co[2] += ocr.disp[1];
+		mv->co[2] += ocr.disp[1];
 
 		if (omd->chop_amount > 0.0f) {

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list