[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42664] trunk/blender/source: static functions for getting power of 2 values were being copied about too much , add to the BLI_math api.

Campbell Barton ideasman42 at gmail.com
Fri Dec 16 10:25:09 CET 2011


Revision: 42664
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42664
Author:   campbellbarton
Date:     2011-12-16 09:25:07 +0000 (Fri, 16 Dec 2011)
Log Message:
-----------
static functions for getting power of 2 values were being copied about too much, add to the BLI_math api.
- is_power_of_2_i
- power_of_2_min_i
- power_of_2_max_i

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/image_gen.c
    trunk/blender/source/blender/blenlib/BLI_math_base.h
    trunk/blender/source/blender/blenlib/intern/math_base_inline.c
    trunk/blender/source/blender/editors/interface/interface.c
    trunk/blender/source/blender/editors/space_view3d/drawvolume.c
    trunk/blender/source/blender/gpu/intern/gpu_draw.c
    trunk/blender/source/blender/gpu/intern/gpu_extensions.c
    trunk/blender/source/blender/windowmanager/intern/wm_draw.c
    trunk/blender/source/gameengine/Ketsji/BL_Texture.cpp

Modified: trunk/blender/source/blender/blenkernel/intern/image_gen.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/image_gen.c	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/blenkernel/intern/image_gen.c	2011-12-16 09:25:07 UTC (rev 42664)
@@ -30,6 +30,7 @@
 
 #include "BKE_image.h"
 #include "BLI_math_color.h"
+#include "BLI_math_base.h"
 #include "BLF_api.h"
 
 void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, float color[4])
@@ -161,21 +162,6 @@
 #define BLEND_FLOAT(real, add)  (real+add <= 1.0f) ? (real+add) : 1.0f
 #define BLEND_CHAR(real, add) ((real + (char)(add * 255.0f)) <= 255) ? (real + (char)(add * 255.0f)) : 255
 
-static int is_pow2(int n)
-{
-	return ((n)&(n-1))==0;
-}
-static int larger_pow2(int n)
-{
-	if (is_pow2(n))
-		return n;
-
-	while(!is_pow2(n))
-		n= n&(n-1);
-
-	return n*2;
-}
-
 static void checker_board_color_fill(unsigned char *rect, float *rect_float, int width, int height)
 {
 	int hue_step, y, x;
@@ -183,7 +169,7 @@
 
 	sat= 1.0;
 
-	hue_step= larger_pow2(width / 8);
+	hue_step= power_of_2_max_i(width / 8);
 	if(hue_step < 8) hue_step= 8;
 
 	for(y= 0; y < height; y++)

Modified: trunk/blender/source/blender/blenlib/BLI_math_base.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_base.h	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/blenlib/BLI_math_base.h	2011-12-16 09:25:07 UTC (rev 42664)
@@ -167,6 +167,11 @@
 
 MINLINE float power_of_2(float f);
 
+/* these dont really fit anywhere but were being copied about a lot */
+MINLINE int is_power_of_2_i(int n);
+MINLINE int power_of_2_max_i(int n);
+MINLINE int power_of_2_min_i(int n);
+
 MINLINE float shell_angle_to_dist(float angle);
 
 #if (defined(WIN32) || defined(WIN64)) && !defined(FREE_WINDOWS)

Modified: trunk/blender/source/blender/blenlib/intern/math_base_inline.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_base_inline.c	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/blenlib/intern/math_base_inline.c	2011-12-16 09:25:07 UTC (rev 42664)
@@ -115,6 +115,31 @@
 	return (float)pow(2.0, ceil(log((double)val) / M_LN2));
 }
 
+MINLINE int is_power_of_2_i(int n)
+{
+	return (n & (n - 1)) == 0;
+}
+
+MINLINE int power_of_2_max_i(int n)
+{
+	if (is_power_of_2_i(n))
+		return n;
+
+	while(!is_power_of_2_i(n))
+		n = n & (n - 1);
+
+	return n * 2;
+}
+
+MINLINE int power_of_2_min_i(int n)
+{
+	while (!is_power_of_2_i(n))
+		n = n & (n - 1);
+
+	return n;
+}
+
+
 MINLINE float minf(float a, float b)
 {
 	return (a < b)? a: b;
@@ -130,5 +155,6 @@
 	return (f < 0.f)? -1.f: 1.f;
 }
 
+
 #endif /* BLI_MATH_BASE_INLINE_H */
 

Modified: trunk/blender/source/blender/editors/interface/interface.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface.c	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/editors/interface/interface.c	2011-12-16 09:25:07 UTC (rev 42664)
@@ -2833,7 +2833,7 @@
 	 */
 static int findBitIndex(unsigned int x)
 {
-	if (!x || (x&(x-1))!=0) {	/* x&(x-1) strips lowest bit */
+	if (!x || !is_power_of_2_i(x)) { /* is_power_of_2_i(x) strips lowest bit */
 		return -1;
 	} else {
 		int idx= 0;

Modified: trunk/blender/source/blender/editors/space_view3d/drawvolume.c
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/drawvolume.c	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/editors/space_view3d/drawvolume.c	2011-12-16 09:25:07 UTC (rev 42664)
@@ -158,23 +158,6 @@
 	return dot_v3v3(up, tmp) >= 0;
 }
 
-// copied from gpu_extension.c
-static int is_pow2(int n)
-{
-	return ((n)&(n-1))==0;
-}
-
-static int larger_pow2(int n)
-{
-	if (is_pow2(n))
-		return n;
-
-	while(!is_pow2(n))
-		n= n&(n-1);
-
-	return n*2;
-}
-
 void draw_volume(ARegion *ar, GPUTexture *tex, float *min, float *max, int res[3], float dx, GPUTexture *tex_shadow)
 {
 	RegionView3D *rv3d= ar->regiondata;
@@ -379,9 +362,9 @@
 		printf("No volume shadow\n");
 
 	if (!GPU_non_power_of_two_support()) {
-		cor[0] = (float)res[0]/(float)larger_pow2(res[0]);
-		cor[1] = (float)res[1]/(float)larger_pow2(res[1]);
-		cor[2] = (float)res[2]/(float)larger_pow2(res[2]);
+		cor[0] = (float)res[0]/(float)power_of_2_max_i(res[0]);
+		cor[1] = (float)res[1]/(float)power_of_2_max_i(res[1]);
+		cor[2] = (float)res[2]/(float)power_of_2_max_i(res[2]);
 	}
 
 	// our slices are defined by the plane equation a*x + b*y +c*z + d = 0

Modified: trunk/blender/source/blender/gpu/intern/gpu_draw.c
===================================================================
--- trunk/blender/source/blender/gpu/intern/gpu_draw.c	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/gpu/intern/gpu_draw.c	2011-12-16 09:25:07 UTC (rev 42664)
@@ -189,20 +189,6 @@
 
 /* Checking powers of two for images since opengl 1.x requires it */
 
-static int is_pow2(int num)
-{
-	/* (n&(n-1)) zeros the least significant bit of n */
-	return ((num)&(num-1))==0;
-}
-
-static int smaller_pow2(int num)
-{
-	while (!is_pow2(num))
-		num= num&(num-1);
-
-	return num;	
-}
-
 static int is_pow2_limit(int num)
 {
 	/* take texture clamping into account */
@@ -214,7 +200,7 @@
 	if (U.glreslimit != 0 && num > U.glreslimit)
 		return 0;
 
-	return ((num)&(num-1))==0;
+	return is_power_of_2_i(num);
 }
 
 static int smaller_pow2_limit(int num)
@@ -227,7 +213,7 @@
 	if (U.glreslimit != 0 && num > U.glreslimit)
 		return U.glreslimit;
 
-	return smaller_pow2(num);
+	return power_of_2_min_i(num);
 }
 
 /* Current OpenGL state caching for GPU_set_tpage */
@@ -692,7 +678,7 @@
 	ibuf = BKE_image_get_ibuf(ima, NULL);
 	
 	if (ima->repbind || (gpu_get_mipmap() && mipmap) || !ima->bindcode || !ibuf ||
-		(!is_pow2(ibuf->x) || !is_pow2(ibuf->y)) ||
+		(!is_power_of_2_i(ibuf->x) || !is_power_of_2_i(ibuf->y)) ||
 		(w == 0) || (h == 0)) {
 		/* these cases require full reload still */
 		GPU_free_image(ima);

Modified: trunk/blender/source/blender/gpu/intern/gpu_extensions.c
===================================================================
--- trunk/blender/source/blender/gpu/intern/gpu_extensions.c	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/gpu/intern/gpu_extensions.c	2011-12-16 09:25:07 UTC (rev 42664)
@@ -41,6 +41,7 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
+#include "BLI_math_base.h"
 
 #include "GPU_draw.h"
 #include "GPU_extensions.h"
@@ -292,22 +293,6 @@
 	return pixels;
 }
 
-static int is_pow2(int n)
-{
-	return ((n)&(n-1))==0;
-}
-
-static int larger_pow2(int n)
-{
-	if (is_pow2(n))
-		return n;
-
-	while(!is_pow2(n))
-		n= n&(n-1);
-
-	return n*2;
-}
-
 static void GPU_glTexSubImageEmpty(GLenum target, GLenum format, int x, int y, int w, int h)
 {
 	void *pixels = MEM_callocN(sizeof(char)*4*w*h, "GPUTextureEmptyPixels");
@@ -353,8 +338,8 @@
 	}
 
 	if (!GPU_non_power_of_two_support()) {
-		tex->w = larger_pow2(tex->w);
-		tex->h = larger_pow2(tex->h);
+		tex->w = power_of_2_max_i(tex->w);
+		tex->h = power_of_2_max_i(tex->h);
 	}
 
 	tex->number = 0;
@@ -462,9 +447,9 @@
 	}
 
 	if (!GPU_non_power_of_two_support()) {
-		tex->w = larger_pow2(tex->w);
-		tex->h = larger_pow2(tex->h);
-		tex->depth = larger_pow2(tex->depth);
+		tex->w = power_of_2_max_i(tex->w);
+		tex->h = power_of_2_max_i(tex->h);
+		tex->depth = power_of_2_max_i(tex->depth);
 	}
 
 	tex->number = 0;

Modified: trunk/blender/source/blender/windowmanager/intern/wm_draw.c
===================================================================
--- trunk/blender/source/blender/windowmanager/intern/wm_draw.c	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/blender/windowmanager/intern/wm_draw.c	2011-12-16 09:25:07 UTC (rev 42664)
@@ -43,6 +43,7 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
+#include "BLI_math_base.h"
 
 #include "BKE_context.h"
 #include "BKE_global.h"
@@ -355,36 +356,12 @@
 	GLenum target;
 } wmDrawTriple;
 
-static int is_pow2(int n)
-{
-	return ((n)&(n-1))==0;
-}
-
-static int smaller_pow2(int n)
-{
-	while (!is_pow2(n))
-		n= n&(n-1);
-
-	return n;
-}
-
-static int larger_pow2(int n)
-{
-	if (is_pow2(n))
-		return n;
-
-	while(!is_pow2(n))
-		n= n&(n-1);
-
-	return n*2;
-}
-
 static void split_width(int x, int n, int *splitx, int *nx)
 {
 	int a, newnx, waste;
 
 	/* if already power of two just use it */
-	if(is_pow2(x)) {
+	if(is_power_of_2_i(x)) {
 		splitx[0]= x;
 		(*nx)++;
 		return;
@@ -392,12 +369,12 @@
 
 	if(n == 1) {
 		/* last part, we have to go larger */
-		splitx[0]= larger_pow2(x);
+		splitx[0]= power_of_2_max_i(x);
 		(*nx)++;
 	}
 	else {
 		/* two or more parts to go, use smaller part */
-		splitx[0]= smaller_pow2(x);
+		splitx[0]= power_of_2_min_i(x);
 		newnx= ++(*nx);
 		split_width(x-splitx[0], n-1, splitx+1, &newnx);
 
@@ -406,8 +383,8 @@
 
 		/* if we waste more space or use the same amount,
 		 * revert deeper splits and just use larger */
-		if(waste >= larger_pow2(x)) {
-			splitx[0]= larger_pow2(x);
+		if(waste >= power_of_2_max_i(x)) {
+			splitx[0]= power_of_2_max_i(x);
 			memset(splitx+1, 0, sizeof(int)*(n-1));
 		}
 		else

Modified: trunk/blender/source/gameengine/Ketsji/BL_Texture.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/BL_Texture.cpp	2011-12-16 09:22:28 UTC (rev 42663)
+++ trunk/blender/source/gameengine/Ketsji/BL_Texture.cpp	2011-12-16 09:25:07 UTC (rev 42664)
@@ -38,11 +38,11 @@
 }
 
 // (n&(n-1)) zeros the least significant bit of n 
-static int is_pow2(int num) {
+static int is_power_of_2_i(int num) {
 	return ((num)&(num-1))==0;
 }
-static int smaller_pow2(int num) {
-	while (!is_pow2(num))
+static int power_of_2_min_i(int num) {
+	while (!is_power_of_2_i(num))
 		num= num&(num-1);
 	return num;	
 }
@@ -159,7 +159,7 @@
 
 void BL_Texture::InitGLTex(unsigned int *pix,int x,int y,bool mipmap)
 {
-	if (!is_pow2(x) || !is_pow2(y) ) {
+	if (!is_power_of_2_i(x) || !is_power_of_2_i(y) ) {
 		InitNonPow2Tex(pix, x,y,mipmap);
 		return;
 	}
@@ -184,8 +184,8 @@
 
 void BL_Texture::InitNonPow2Tex(unsigned int *pix,int x,int y,bool mipmap)
 {
-	int nx= smaller_pow2(x);
-	int ny= smaller_pow2(y);
+	int nx= power_of_2_min_i(x);
+	int ny= power_of_2_min_i(y);
 
 	unsigned int *newPixels = (unsigned int *)malloc(nx*ny*sizeof(unsigned int));
 	
@@ -274,7 +274,7 @@

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list