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
This commit is contained in:
Campbell Barton 2011-12-16 09:25:07 +00:00
parent 91f14ddf3d
commit 2253b63c97
9 changed files with 61 additions and 113 deletions

@ -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++)

@ -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)

@ -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 */

@ -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;

@ -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

@ -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);

@ -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;

@ -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

@ -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;
}