From 2009124cf9b1e9517be514d9e60c4c6d72d57023 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 21 Mar 2013 12:21:13 -0300 Subject: [PATCH] Reworked internal anisotropic filtering code to be more concise and intuitive --- src/modules/graphics/Image.cpp | 12 +------ src/modules/graphics/Image.h | 10 ++---- src/modules/graphics/opengl/Canvas.cpp | 20 ----------- src/modules/graphics/opengl/Canvas.h | 4 --- src/modules/graphics/opengl/Font.cpp | 26 +------------- src/modules/graphics/opengl/Font.h | 5 --- src/modules/graphics/opengl/Graphics.cpp | 10 ------ src/modules/graphics/opengl/Graphics.h | 6 ---- src/modules/graphics/opengl/Image.cpp | 36 ++----------------- src/modules/graphics/opengl/Image.h | 9 ----- src/modules/graphics/opengl/OpenGL.cpp | 22 +++++++++++- src/modules/graphics/opengl/OpenGL.h | 3 +- src/modules/graphics/opengl/wrap_Canvas.cpp | 8 ++--- src/modules/graphics/opengl/wrap_Font.cpp | 7 ++-- src/modules/graphics/opengl/wrap_Graphics.cpp | 8 ++--- src/modules/graphics/opengl/wrap_Image.cpp | 7 ++-- 16 files changed, 43 insertions(+), 150 deletions(-) diff --git a/src/modules/graphics/Image.cpp b/src/modules/graphics/Image.cpp index 08578339c..3bdc06e0f 100644 --- a/src/modules/graphics/Image.cpp +++ b/src/modules/graphics/Image.cpp @@ -26,12 +26,12 @@ namespace graphics { Image::Filter Image::defaultFilter; -float Image::defaultAnisotropy = 1.0f; Image::Filter::Filter() : min(FILTER_LINEAR) , mag(FILTER_LINEAR) , mipmap(FILTER_NONE) + , anisotropy(1.0f) { } @@ -55,16 +55,6 @@ const Image::Filter &Image::getDefaultFilter() return defaultFilter; } -void Image::setDefaultAnisotropy(float anisotropy) -{ - defaultAnisotropy = anisotropy; -} - -float Image::getDefaultAnisotropy() -{ - return defaultAnisotropy; -} - bool Image::getConstant(const char *in, FilterMode &out) { return filterModes.find(in, out); diff --git a/src/modules/graphics/Image.h b/src/modules/graphics/Image.h index f788d51fa..e2c49116d 100644 --- a/src/modules/graphics/Image.h +++ b/src/modules/graphics/Image.h @@ -56,6 +56,7 @@ public: FilterMode min; FilterMode mag; FilterMode mipmap; + float anisotropy; }; struct Wrap @@ -71,10 +72,6 @@ public: static void setDefaultFilter(const Filter &f); static const Filter &getDefaultFilter(); - // The default amount of anisotropic filtering. - static void setDefaultAnisotropy(float anisotropy); - static float getDefaultAnisotropy(); - static bool getConstant(const char *in, FilterMode &out); static bool getConstant(FilterMode in, const char *&out); static bool getConstant(const char *in, WrapMode &out); @@ -82,12 +79,9 @@ public: private: - // The default image filter. + // The default texture filter. static Filter defaultFilter; - // The default amount of anisotropic filtering. - static float defaultAnisotropy; - static StringMap::Entry filterModeEntries[]; static StringMap filterModes; static StringMap::Entry wrapModeEntries[]; diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index 4f977213f..1e5c20670 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/src/modules/graphics/opengl/Canvas.cpp @@ -317,7 +317,6 @@ Canvas::Canvas(int width, int height, TextureType texture_type) vertices[3].t = 0; settings.filter = Image::getDefaultFilter(); - settings.anisotropy = Image::getDefaultAnisotropy(); getStrategy(); @@ -497,24 +496,6 @@ Image::Wrap Canvas::getWrap() const return getTextureWrap(); } -void Canvas::setAnisotropy(float anisotropy) -{ - if (Image::hasAnisotropicFilteringSupport()) - { - settings.anisotropy = std::min(std::max(anisotropy, 1.0f), Image::getMaxAnisotropy()); - - bindTexture(img); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, settings.anisotropy); - } - else - settings.anisotropy = 1.0f; -} - -float Canvas::getAnisotropy() const -{ - return settings.anisotropy; -} - bool Canvas::loadVolatile() { status = strategy->createFBO(fbo, depth_stencil, img, width, height, texture_type); @@ -523,7 +504,6 @@ bool Canvas::loadVolatile() setFilter(settings.filter); setWrap(settings.wrap); - setAnisotropy(settings.anisotropy); Color c; c.r = c.g = c.b = c.a = 0; clear(c); diff --git a/src/modules/graphics/opengl/Canvas.h b/src/modules/graphics/opengl/Canvas.h index 1e6ab8502..81162e68e 100644 --- a/src/modules/graphics/opengl/Canvas.h +++ b/src/modules/graphics/opengl/Canvas.h @@ -72,9 +72,6 @@ public: void setWrap(const Image::Wrap &w); Image::Wrap getWrap() const; - void setAnisotropy(float anisotropy); - float getAnisotropy() const; - int getWidth(); int getHeight(); @@ -122,7 +119,6 @@ private: { Image::Filter filter; Image::Wrap wrap; - float anisotropy; } settings; void drawv(const Matrix &t, const vertex *v) const; diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index f51179d6f..19a957f31 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -47,7 +47,6 @@ Font::Font(love::font::Rasterizer *r, const Image::Filter &filter) , height(r->getHeight()) , lineHeight(1) , mSpacing(1) - , anisotropy(Image::getDefaultAnisotropy()) , filter(filter) { // try to find the best texture size match for the font size @@ -167,7 +166,6 @@ void Font::createTexture() &emptyData[0]); setFilter(filter); - setAnisotropy(anisotropy); } Font::Glyph *Font::addGlyph(unsigned int glyph) @@ -506,7 +504,7 @@ void Font::setFilter(const Image::Filter &f) for (it = textures.begin(); it != textures.end(); ++it) { bindTexture(*it); - setTextureFilter(f); + filter.anisotropy = setTextureFilter(f); } } @@ -515,28 +513,6 @@ const Image::Filter &Font::getFilter() return filter; } -void Font::setAnisotropy(float anisotropy) -{ - if (Image::hasAnisotropicFilteringSupport()) - { - this->anisotropy = std::min(std::max(anisotropy, 1.0f), Image::getMaxAnisotropy()); - - std::vector::const_iterator it; - for (it = textures.begin(); it != textures.end(); ++it) - { - bindTexture(*it); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->anisotropy); - } - } - else - this->anisotropy = 1.0f; -} - -float Font::getAnisotropy() const -{ - return anisotropy; -} - bool Font::loadVolatile() { createTexture(); diff --git a/src/modules/graphics/opengl/Font.h b/src/modules/graphics/opengl/Font.h index d0ea5d472..9a157e551 100644 --- a/src/modules/graphics/opengl/Font.h +++ b/src/modules/graphics/opengl/Font.h @@ -129,9 +129,6 @@ public: void setFilter(const Image::Filter &f); const Image::Filter &getFilter(); - void setAnisotropy(float anisotropy); - float getAnisotropy() const; - // Implements Volatile. bool loadVolatile(); void unloadVolatile(); @@ -186,8 +183,6 @@ private: float lineHeight; float mSpacing; // modifies the spacing by multiplying it with this value - float anisotropy; - int texture_size_index; int texture_width; int texture_height; diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index f0b4e7b94..1a9802001 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -639,16 +639,6 @@ const Image::Filter &Graphics::getDefaultFilter() const return Image::getDefaultFilter(); } -void Graphics::setDefaultAnisotropy(float anisotropy) -{ - Image::setDefaultAnisotropy(anisotropy); -} - -float Graphics::getDefaultAnisotropy() const -{ - return Image::getDefaultAnisotropy(); -} - void Graphics::setDefaultMipmapFilter(Image::FilterMode filter, float sharpness) { Image::setDefaultMipmapFilter(filter); diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 4d34db1d1..696fc525c 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -319,12 +319,6 @@ public: **/ const Image::Filter &getDefaultFilter() const; - /** - * Default texture anisotropic filtering level. - **/ - void setDefaultAnisotropy(float anisotropy); - float getDefaultAnisotropy() const; - /** * Default Image mipmap filter mode and sharpness values. **/ diff --git a/src/modules/graphics/opengl/Image.cpp b/src/modules/graphics/opengl/Image.cpp index b8b57d2ab..4af318e8d 100644 --- a/src/modules/graphics/opengl/Image.cpp +++ b/src/modules/graphics/opengl/Image.cpp @@ -34,7 +34,6 @@ namespace opengl { float Image::maxMipmapSharpness = 0.0f; -float Image::maxAnisotropy = 1.0f; Image::FilterMode Image::defaultMipmapFilter = Image::FILTER_NONE; float Image::defaultMipmapSharpness = 0.0f; @@ -45,7 +44,6 @@ Image::Image(love::image::ImageData *data) , texture(0) , mipmapSharpness(defaultMipmapSharpness) , mipmapsCreated(false) - , anisotropy(getDefaultAnisotropy()) { data->retain(); this->data = data; @@ -208,7 +206,7 @@ void Image::setFilter(const Image::Filter &f) filter = f; bind(); - setTextureFilter(f); + filter.anisotropy = setTextureFilter(f); checkMipmapsCreated(); } @@ -249,24 +247,6 @@ float Image::getMipmapSharpness() const return mipmapSharpness; } -void Image::setAnisotropy(float anisotropy) -{ - if (hasAnisotropicFilteringSupport()) - { - this->anisotropy = std::min(std::max(anisotropy, 1.0f), getMaxAnisotropy()); - - bind(); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->anisotropy); - } - else - this->anisotropy = 1.0f; -} - -float Image::getAnisotropy() const -{ - return anisotropy; -} - void Image::bind() const { if (texture == 0) @@ -301,7 +281,7 @@ bool Image::loadVolatilePOT() glGenTextures(1,(GLuint *)&texture); bindTexture(texture); - setTextureFilter(filter); + filter.anisotropy = setTextureFilter(filter); setTextureWrap(wrap); float p2width = next_p2(width); @@ -343,7 +323,6 @@ bool Image::loadVolatilePOT() checkMipmapsCreated(); setMipmapSharpness(mipmapSharpness); - setAnisotropy(anisotropy); return true; } @@ -353,7 +332,7 @@ bool Image::loadVolatileNPOT() glGenTextures(1,(GLuint *)&texture); bindTexture(texture); - setTextureFilter(filter); + filter.anisotropy = setTextureFilter(filter); setTextureWrap(wrap); while (glGetError() != GL_NO_ERROR); // clear errors @@ -375,7 +354,6 @@ bool Image::loadVolatileNPOT() checkMipmapsCreated(); setMipmapSharpness(mipmapSharpness); - setAnisotropy(anisotropy); return true; } @@ -429,14 +407,6 @@ Image::FilterMode Image::getDefaultMipmapFilter() return defaultMipmapFilter; } -float Image::getMaxAnisotropy() -{ - if (hasAnisotropicFilteringSupport() && maxAnisotropy == 1.0f) - glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy); - - return maxAnisotropy; -} - bool Image::hasNpot() { return GLEE_VERSION_2_0 || GLEE_ARB_texture_non_power_of_two; diff --git a/src/modules/graphics/opengl/Image.h b/src/modules/graphics/opengl/Image.h index 6bd8f6fb9..8b3295fb9 100644 --- a/src/modules/graphics/opengl/Image.h +++ b/src/modules/graphics/opengl/Image.h @@ -104,9 +104,6 @@ public: const Image::Wrap &getWrap() const; - void setAnisotropy(float anisotropy); - float getAnisotropy() const; - void setMipmapSharpness(float sharpness); float getMipmapSharpness() const; @@ -124,8 +121,6 @@ public: static void setDefaultMipmapFilter(FilterMode f); static FilterMode getDefaultMipmapFilter(); - static float getMaxAnisotropy(); - static bool hasNpot(); static bool hasAnisotropicFilteringSupport(); static bool hasMipmapSupport(); @@ -158,9 +153,6 @@ private: // True if mipmaps have been created for this Image. bool mipmapsCreated; - // Anisotropic filtering value. - float anisotropy; - // The image's filter mode Image::Filter filter; @@ -173,7 +165,6 @@ private: void checkMipmapsCreated(); static float maxMipmapSharpness; - static float maxAnisotropy; static FilterMode defaultMipmapFilter; static float defaultMipmapSharpness; diff --git a/src/modules/graphics/opengl/OpenGL.cpp b/src/modules/graphics/opengl/OpenGL.cpp index 10afe3c9b..650fb8a72 100644 --- a/src/modules/graphics/opengl/OpenGL.cpp +++ b/src/modules/graphics/opengl/OpenGL.cpp @@ -39,6 +39,8 @@ static bool contextInitialized = false; static int curTextureUnit = 0; static std::vector textureUnits; +static float maxAnisotropy = 1.0f; + void initializeContext() { if (contextInitialized) @@ -95,6 +97,11 @@ void initializeContext() glUnmapBufferARB = (GLEEPFNGLUNMAPBUFFERARBPROC) glUnmapBuffer; } + if (GLEE_EXT_texture_filter_anisotropic) + glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy); + else + maxAnisotropy = 1.0f; + // Set the 'default' texture (id 0) as a repeating white pixel. // Otherwise, texture2D inside a shader would return black when drawing graphics primitives, // which would create the need to use different "passthrough" shaders for untextured primitives vs images. @@ -175,7 +182,7 @@ void deleteTexture(GLuint texture) glDeleteTextures(1, &texture); } -void setTextureFilter(const graphics::Image::Filter &f) +float setTextureFilter(const graphics::Image::Filter &f) { GLint gmin, gmag; @@ -214,6 +221,16 @@ void setTextureFilter(const graphics::Image::Filter &f) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gmin); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag); + + float anisotropy = 1.0f; + + if (GLEE_EXT_texture_filter_anisotropic) + { + anisotropy = std::min(std::max(f.anisotropy, 1.0f), maxAnisotropy); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy); + } + + return anisotropy; } graphics::Image::Filter getTextureFilter() @@ -262,6 +279,9 @@ graphics::Image::Filter getTextureFilter() break; } + if (GLEE_EXT_texture_filter_anisotropic) + glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &f.anisotropy); + return f; } diff --git a/src/modules/graphics/opengl/OpenGL.h b/src/modules/graphics/opengl/OpenGL.h index bc469da71..12cbef800 100644 --- a/src/modules/graphics/opengl/OpenGL.h +++ b/src/modules/graphics/opengl/OpenGL.h @@ -71,8 +71,9 @@ void deleteTexture(GLuint texture); /** * Sets the image filter mode for the currently bound texture. + * Returns the actual amount of anisotropic filtering set. */ -void setTextureFilter(const graphics::Image::Filter &f); +float setTextureFilter(const graphics::Image::Filter &f); /** * Returns the image filter mode for the currently bound texture. diff --git a/src/modules/graphics/opengl/wrap_Canvas.cpp b/src/modules/graphics/opengl/wrap_Canvas.cpp index 7a8dc0a6f..5fe5c5099 100644 --- a/src/modules/graphics/opengl/wrap_Canvas.cpp +++ b/src/modules/graphics/opengl/wrap_Canvas.cpp @@ -99,10 +99,9 @@ int w_Canvas_setFilter(lua_State *L) if (!Image::getConstant(magstr, f.mag)) return luaL_error(L, "Invalid filter mode: %s", magstr); - canvas->setFilter(f); + f.anisotropy = (float) luaL_optnumber(L, 4, 1.0); - float anisotropy = (float) luaL_optnumber(L, 4, 1.0); - canvas->setAnisotropy(anisotropy); + canvas->setFilter(f); return 0; @@ -120,8 +119,7 @@ int w_Canvas_getFilter(lua_State *L) lua_pushstring(L, minstr); lua_pushstring(L, magstr); - - lua_pushnumber(L, canvas->getAnisotropy()); + lua_pushnumber(L, f.anisotropy); return 3; } diff --git a/src/modules/graphics/opengl/wrap_Font.cpp b/src/modules/graphics/opengl/wrap_Font.cpp index 4a88ca6df..82b5c152a 100644 --- a/src/modules/graphics/opengl/wrap_Font.cpp +++ b/src/modules/graphics/opengl/wrap_Font.cpp @@ -103,6 +103,8 @@ int w_Font_setFilter(lua_State *L) if (!Image::getConstant(magstr, f.mag)) return luaL_error(L, "Invalid filter mode: %s", magstr); + f.anisotropy = (float) luaL_optnumber(L, 4, 1.0); + try { t->setFilter(f); @@ -112,9 +114,6 @@ int w_Font_setFilter(lua_State *L) return luaL_error(L, "%s", e.what()); } - float anisotropy = (float) luaL_optnumber(L, 4, 1.0); - t->setAnisotropy(anisotropy); - return 0; } @@ -128,7 +127,7 @@ int w_Font_getFilter(lua_State *L) Image::getConstant(f.mag, magstr); lua_pushstring(L, minstr); lua_pushstring(L, magstr); - lua_pushnumber(L, t->getAnisotropy()); + lua_pushnumber(L, f.anisotropy); return 3; } diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index e12141c99..f3bb54c54 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -749,14 +749,14 @@ int w_setDefaultFilter(lua_State *L) if (!Image::getConstant(magstr, mag)) return luaL_error(L, "Invalid filter mode: %s", magstr); + float anisotropy = (float) luaL_optnumber(L, 3, 1.0); + Image::Filter f; f.min = min; f.mag = mag; + f.anisotropy = anisotropy; instance->setDefaultFilter(f); - - float anisotropy = (float) luaL_optnumber(L, 3, 1.0); - instance->setDefaultAnisotropy(anisotropy); return 0; } @@ -772,7 +772,7 @@ int w_getDefaultFilter(lua_State *L) return luaL_error(L, "Unknown magnification filter mode"); lua_pushstring(L, minstr); lua_pushstring(L, magstr); - lua_pushnumber(L, instance->getDefaultAnisotropy()); + lua_pushnumber(L, f.anisotropy); return 3; } diff --git a/src/modules/graphics/opengl/wrap_Image.cpp b/src/modules/graphics/opengl/wrap_Image.cpp index c66b63965..70d947393 100644 --- a/src/modules/graphics/opengl/wrap_Image.cpp +++ b/src/modules/graphics/opengl/wrap_Image.cpp @@ -68,6 +68,8 @@ int w_Image_setFilter(lua_State *L) if (!Image::getConstant(magstr, f.mag)) return luaL_error(L, "Invalid filter mode: %s", magstr); + f.anisotropy = (float) luaL_optnumber(L, 4, 1.0); + try { t->setFilter(f); @@ -77,9 +79,6 @@ int w_Image_setFilter(lua_State *L) return luaL_error(L, "%s", e.what()); } - float anisotropy = (float) luaL_optnumber(L, 4, 1.0); - t->setAnisotropy(anisotropy); - return 0; } @@ -93,7 +92,7 @@ int w_Image_getFilter(lua_State *L) Image::getConstant(f.mag, magstr); lua_pushstring(L, minstr); lua_pushstring(L, magstr); - lua_pushnumber(L, t->getAnisotropy()); + lua_pushnumber(L, f.anisotropy); return 3; }