From 7086aeeebc0adeff2b7d28e610066ca05fbe684c Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 1 Oct 2007 08:50:58 +0000 Subject: [PATCH] Removed log2 and replaced the places where is was used with a new Image::computeNumberOfMipmapLevels method. --- include/osg/Image | 1 + include/osg/Math | 5 ----- src/osg/Image.cpp | 7 +++++++ src/osg/Texture1D.cpp | 2 +- src/osg/Texture2D.cpp | 2 +- src/osg/Texture2DArray.cpp | 2 +- src/osg/Texture3D.cpp | 2 +- src/osg/TextureCubeMap.cpp | 2 +- src/osgWrappers/osg/Image.cpp | 4 ++++ 9 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/osg/Image b/include/osg/Image index 55a14495f..c33763312 100644 --- a/include/osg/Image +++ b/include/osg/Image @@ -234,6 +234,7 @@ class OSG_EXPORT Image : public Object static unsigned int computePixelSizeInBits(GLenum pixelFormat,GLenum type); static unsigned int computeRowWidthInBytes(int width,GLenum pixelFormat,GLenum type,int packing); static int computeNearestPowerOfTwo(int s,float bias=0.5f); + static int computeNumberOfMipmapLevels(int s,int t = 1, int r = 1); /** Precomputed mipmaps stuff. */ typedef std::vector< unsigned int > MipmapDataType; diff --git a/include/osg/Math b/include/osg/Math index c0821e258..ec8eb0942 100644 --- a/include/osg/Math +++ b/include/osg/Math @@ -194,11 +194,6 @@ inline double RadiansToDegrees(double angle) { return angle*180.0/PI; } inline float round(float v) { return v>=0.0f?floorf(v+0.5f):ceilf(v-0.5f); } inline double round(double v) { return v>=0.0?floor(v+0.5):ceil(v-0.5); } -inline float log2(float v) { return logf(v) * static_cast(INVLN_2); } -inline double log2(double v) { return log(v) * INVLN_2; } -inline float log2(int v) { return log2(static_cast(v)); } -inline float log2(unsigned int v) { return log2(static_cast(v)); } - #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__MWERKS__) inline bool isNaN(float v) { return _isnan(v)!=0; } inline bool isNaN(double v) { return _isnan(v)!=0; } diff --git a/src/osg/Image.cpp b/src/osg/Image.cpp index cedea746f..033fdb31e 100644 --- a/src/osg/Image.cpp +++ b/src/osg/Image.cpp @@ -429,6 +429,13 @@ int Image::computeNearestPowerOfTwo(int s,float bias) return s; } +int Image::computeNumberOfMipmapLevels(int s,int t, int r) +{ + int w = maximum(s, t); + w = maximum(w, r); + return 1 + static_cast(floor(logf(w)/logf(2.0f))); +} + unsigned int Image::getTotalSizeInBytesIncludingMipmaps() const { if (_mipmapData.empty()) diff --git a/src/osg/Texture1D.cpp b/src/osg/Texture1D.cpp index 7a496e5c0..d99ffa507 100644 --- a/src/osg/Texture1D.cpp +++ b/src/osg/Texture1D.cpp @@ -414,7 +414,7 @@ void Texture1D::allocateMipmap(State& state) const // compute number of mipmap levels int width = _textureWidth; - int numMipmapLevels = 1 + (int)floor(log2(width)); + int numMipmapLevels = Image::computeNumberOfMipmapLevels(width); // we do not reallocate the level 0, since it was already allocated width >>= 1; diff --git a/src/osg/Texture2D.cpp b/src/osg/Texture2D.cpp index 5f73d117c..52bac5492 100644 --- a/src/osg/Texture2D.cpp +++ b/src/osg/Texture2D.cpp @@ -408,7 +408,7 @@ void Texture2D::allocateMipmap(State& state) const // compute number of mipmap levels int width = _textureWidth; int height = _textureHeight; - int numMipmapLevels = 1 + (int)floor(log2(maximum(width, height))); + int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height); // we do not reallocate the level 0, since it was already allocated width >>= 1; diff --git a/src/osg/Texture2DArray.cpp b/src/osg/Texture2DArray.cpp index d6355042e..6d7f3b3f0 100644 --- a/src/osg/Texture2DArray.cpp +++ b/src/osg/Texture2DArray.cpp @@ -464,7 +464,7 @@ void Texture2DArray::allocateMipmap(State& state) const // compute number of mipmap levels int width = _textureWidth; int height = _textureHeight; - int numMipmapLevels = 1 + (int)floor(log2(maximum(width, height))); + int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height); // we do not reallocate the level 0, since it was already allocated width >>= 1; diff --git a/src/osg/Texture3D.cpp b/src/osg/Texture3D.cpp index 8af8dab29..fbefbcfc3 100644 --- a/src/osg/Texture3D.cpp +++ b/src/osg/Texture3D.cpp @@ -456,7 +456,7 @@ void Texture3D::allocateMipmap(State& state) const int width = _textureWidth; int height = _textureHeight; int depth = _textureDepth; - int numMipmapLevels = 1 + (int)floor(log2(maximum(width, maximum(depth, height)))); + int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height, depth); // we do not reallocate the level 0, since it was already allocated width >>= 1; diff --git a/src/osg/TextureCubeMap.cpp b/src/osg/TextureCubeMap.cpp index bf11b7cad..f84f87170 100644 --- a/src/osg/TextureCubeMap.cpp +++ b/src/osg/TextureCubeMap.cpp @@ -384,7 +384,7 @@ void TextureCubeMap::allocateMipmap(State& state) const // compute number of mipmap levels int width = _textureWidth; int height = _textureHeight; - int numMipmapLevels = 1 + (int)floor(log2(maximum(width, height))); + int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height); // we do not reallocate the level 0, since it was already allocated width >>= 1; diff --git a/src/osgWrappers/osg/Image.cpp b/src/osgWrappers/osg/Image.cpp index 4570dacd3..8ec463239 100644 --- a/src/osgWrappers/osg/Image.cpp +++ b/src/osgWrappers/osg/Image.cpp @@ -357,6 +357,10 @@ BEGIN_OBJECT_REFLECTOR(osg::Image) __int__computeNearestPowerOfTwo__int__float_S, "", ""); + I_StaticMethodWithDefaults3(int, computeNumberOfMipmapLevels, IN, int, s, , IN, int, t, 1, IN, int, r, 1, + __int__computeNumberOfMipmapLevels__int__int__int_S, + "", + ""); I_ProtectedMethod0(void, deallocateData, Properties::NON_VIRTUAL, Properties::NON_CONST,