From 2253b63c979fbfbbb6f06c93ede85d9b9b6caddf Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 16 Dec 2011 09:25:07 +0000 Subject: 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 --- source/blender/blenkernel/intern/image_gen.c | 18 ++---------- source/blender/blenlib/BLI_math_base.h | 5 ++++ source/blender/blenlib/intern/math_base_inline.c | 26 ++++++++++++++++++ source/blender/editors/interface/interface.c | 2 +- source/blender/editors/space_view3d/drawvolume.c | 23 ++-------------- source/blender/gpu/intern/gpu_draw.c | 20 ++------------ source/blender/gpu/intern/gpu_extensions.c | 27 ++++-------------- source/blender/windowmanager/intern/wm_draw.c | 35 ++++-------------------- source/gameengine/Ketsji/BL_Texture.cpp | 18 ++++++------ 9 files changed, 61 insertions(+), 113 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/intern/image_gen.c b/source/blender/blenkernel/intern/image_gen.c index 751980f61e8..a93d0221cf0 100644 --- a/source/blender/blenkernel/intern/image_gen.c +++ b/source/blender/blenkernel/intern/image_gen.c @@ -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 @@ void BKE_image_buf_fill_checker(unsigned char *rect, float *rect_float, int widt #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 @@ static void checker_board_color_fill(unsigned char *rect, float *rect_float, int 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++) diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 204ec9b5159..53db77dc203 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -167,6 +167,11 @@ MINLINE float signf(float f); 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) diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 0b2411af009..7e04e0ae566 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -115,6 +115,31 @@ MINLINE float power_of_2(float val) 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 @@ MINLINE float signf(float f) return (f < 0.f)? -1.f: 1.f; } + #endif /* BLI_MATH_BASE_INLINE_H */ diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index a10de9a8a42..1bad61be324 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -2833,7 +2833,7 @@ uiBut *uiDefBut(uiBlock *block, int type, int retval, const char *str, int x1, i */ 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; diff --git a/source/blender/editors/space_view3d/drawvolume.c b/source/blender/editors/space_view3d/drawvolume.c index 6605d488d36..8308a4a9ccf 100644 --- a/source/blender/editors/space_view3d/drawvolume.c +++ b/source/blender/editors/space_view3d/drawvolume.c @@ -158,23 +158,6 @@ static int convex(float *p0, float *up, float *a, float *b) 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 @@ void draw_volume(ARegion *ar, GPUTexture *tex, float *min, float *max, int res[3 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 diff --git a/source/blender/gpu/intern/gpu_draw.c b/source/blender/gpu/intern/gpu_draw.c index 4af0cceb4e0..2dcce996065 100644 --- a/source/blender/gpu/intern/gpu_draw.c +++ b/source/blender/gpu/intern/gpu_draw.c @@ -189,20 +189,6 @@ void GPU_render_text(MTFace *tface, int mode, /* 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 @@ static int is_pow2_limit(int num) 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 @@ static int smaller_pow2_limit(int num) 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 @@ void GPU_paint_update_image(Image *ima, int x, int y, int w, int h, int mipmap) 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); diff --git a/source/blender/gpu/intern/gpu_extensions.c b/source/blender/gpu/intern/gpu_extensions.c index 79db125c21f..4c0a3d6c5e4 100644 --- a/source/blender/gpu/intern/gpu_extensions.c +++ b/source/blender/gpu/intern/gpu_extensions.c @@ -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 @@ static unsigned char *GPU_texture_convert_pixels(int length, float *fpixels) 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 @@ static GPUTexture *GPU_texture_create_nD(int w, int h, int n, float *fpixels, in } 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 @@ GPUTexture *GPU_texture_create_3D(int w, int h, int depth, float *fpixels) } 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; diff --git a/source/blender/windowmanager/intern/wm_draw.c b/source/blender/windowmanager/intern/wm_draw.c index f03680852ea..4fe11044d30 100644 --- a/source/blender/windowmanager/intern/wm_draw.c +++ b/source/blender/windowmanager/intern/wm_draw.c @@ -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 @@ typedef struct wmDrawTriple { 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 @@ static void split_width(int x, int n, int *splitx, int *nx) 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 @@ static void split_width(int x, int n, int *splitx, int *nx) /* 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 diff --git a/source/gameengine/Ketsji/BL_Texture.cpp b/source/gameengine/Ketsji/BL_Texture.cpp index a306e059442..6c57776cb60 100644 --- a/source/gameengine/Ketsji/BL_Texture.cpp +++ b/source/gameengine/Ketsji/BL_Texture.cpp @@ -38,11 +38,11 @@ extern "C" { } // (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 @@ bool BL_Texture::InitFromImage(int unit, Image *img, bool mipmap) 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::InitGLTex(unsigned int *pix,int x,int y,bool mipmap) 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 @@ bool BL_Texture::InitCubeMap(int unit, EnvMap *cubemap) my_envmap_split_ima(cubemap, ibuf); - if (!is_pow2(cubemap->cube[0]->x) || !is_pow2(cubemap->cube[0]->y)) + if (!is_power_of_2_i(cubemap->cube[0]->x) || !is_power_of_2_i(cubemap->cube[0]->y)) { spit("invalid envmap size please render with CubeRes @ power of two"); @@ -610,8 +610,8 @@ void BL_Texture::setTexEnv(BL_Material *mat, bool modulate) int BL_Texture::GetPow2(int n) { - if(!is_pow2(n)) - n = smaller_pow2(n); + if(!is_power_of_2_i(n)) + n = power_of_2_min_i(n); return n; } -- cgit v1.2.3