Reworked internal anisotropic filtering code to be more concise and intuitive

This commit is contained in:
Alex Szpakowski
2013-03-21 12:21:13 -03:00
parent 7e753a0acb
commit 2009124cf9
16 changed files with 43 additions and 150 deletions
+1 -11
View File
@@ -26,12 +26,12 @@ namespace graphics
{ {
Image::Filter Image::defaultFilter; Image::Filter Image::defaultFilter;
float Image::defaultAnisotropy = 1.0f;
Image::Filter::Filter() Image::Filter::Filter()
: min(FILTER_LINEAR) : min(FILTER_LINEAR)
, mag(FILTER_LINEAR) , mag(FILTER_LINEAR)
, mipmap(FILTER_NONE) , mipmap(FILTER_NONE)
, anisotropy(1.0f)
{ {
} }
@@ -55,16 +55,6 @@ const Image::Filter &Image::getDefaultFilter()
return defaultFilter; return defaultFilter;
} }
void Image::setDefaultAnisotropy(float anisotropy)
{
defaultAnisotropy = anisotropy;
}
float Image::getDefaultAnisotropy()
{
return defaultAnisotropy;
}
bool Image::getConstant(const char *in, FilterMode &out) bool Image::getConstant(const char *in, FilterMode &out)
{ {
return filterModes.find(in, out); return filterModes.find(in, out);
+2 -8
View File
@@ -56,6 +56,7 @@ public:
FilterMode min; FilterMode min;
FilterMode mag; FilterMode mag;
FilterMode mipmap; FilterMode mipmap;
float anisotropy;
}; };
struct Wrap struct Wrap
@@ -71,10 +72,6 @@ public:
static void setDefaultFilter(const Filter &f); static void setDefaultFilter(const Filter &f);
static const Filter &getDefaultFilter(); 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(const char *in, FilterMode &out);
static bool getConstant(FilterMode in, const char *&out); static bool getConstant(FilterMode in, const char *&out);
static bool getConstant(const char *in, WrapMode &out); static bool getConstant(const char *in, WrapMode &out);
@@ -82,12 +79,9 @@ public:
private: private:
// The default image filter. // The default texture filter.
static Filter defaultFilter; static Filter defaultFilter;
// The default amount of anisotropic filtering.
static float defaultAnisotropy;
static StringMap<FilterMode, FILTER_MAX_ENUM>::Entry filterModeEntries[]; static StringMap<FilterMode, FILTER_MAX_ENUM>::Entry filterModeEntries[];
static StringMap<FilterMode, FILTER_MAX_ENUM> filterModes; static StringMap<FilterMode, FILTER_MAX_ENUM> filterModes;
static StringMap<WrapMode, WRAP_MAX_ENUM>::Entry wrapModeEntries[]; static StringMap<WrapMode, WRAP_MAX_ENUM>::Entry wrapModeEntries[];
-20
View File
@@ -317,7 +317,6 @@ Canvas::Canvas(int width, int height, TextureType texture_type)
vertices[3].t = 0; vertices[3].t = 0;
settings.filter = Image::getDefaultFilter(); settings.filter = Image::getDefaultFilter();
settings.anisotropy = Image::getDefaultAnisotropy();
getStrategy(); getStrategy();
@@ -497,24 +496,6 @@ Image::Wrap Canvas::getWrap() const
return getTextureWrap(); 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() bool Canvas::loadVolatile()
{ {
status = strategy->createFBO(fbo, depth_stencil, img, width, height, texture_type); status = strategy->createFBO(fbo, depth_stencil, img, width, height, texture_type);
@@ -523,7 +504,6 @@ bool Canvas::loadVolatile()
setFilter(settings.filter); setFilter(settings.filter);
setWrap(settings.wrap); setWrap(settings.wrap);
setAnisotropy(settings.anisotropy);
Color c; Color c;
c.r = c.g = c.b = c.a = 0; c.r = c.g = c.b = c.a = 0;
clear(c); clear(c);
-4
View File
@@ -72,9 +72,6 @@ public:
void setWrap(const Image::Wrap &w); void setWrap(const Image::Wrap &w);
Image::Wrap getWrap() const; Image::Wrap getWrap() const;
void setAnisotropy(float anisotropy);
float getAnisotropy() const;
int getWidth(); int getWidth();
int getHeight(); int getHeight();
@@ -122,7 +119,6 @@ private:
{ {
Image::Filter filter; Image::Filter filter;
Image::Wrap wrap; Image::Wrap wrap;
float anisotropy;
} settings; } settings;
void drawv(const Matrix &t, const vertex *v) const; void drawv(const Matrix &t, const vertex *v) const;
+1 -25
View File
@@ -47,7 +47,6 @@ Font::Font(love::font::Rasterizer *r, const Image::Filter &filter)
, height(r->getHeight()) , height(r->getHeight())
, lineHeight(1) , lineHeight(1)
, mSpacing(1) , mSpacing(1)
, anisotropy(Image::getDefaultAnisotropy())
, filter(filter) , filter(filter)
{ {
// try to find the best texture size match for the font size // try to find the best texture size match for the font size
@@ -167,7 +166,6 @@ void Font::createTexture()
&emptyData[0]); &emptyData[0]);
setFilter(filter); setFilter(filter);
setAnisotropy(anisotropy);
} }
Font::Glyph *Font::addGlyph(unsigned int glyph) 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) for (it = textures.begin(); it != textures.end(); ++it)
{ {
bindTexture(*it); bindTexture(*it);
setTextureFilter(f); filter.anisotropy = setTextureFilter(f);
} }
} }
@@ -515,28 +513,6 @@ const Image::Filter &Font::getFilter()
return filter; return filter;
} }
void Font::setAnisotropy(float anisotropy)
{
if (Image::hasAnisotropicFilteringSupport())
{
this->anisotropy = std::min(std::max(anisotropy, 1.0f), Image::getMaxAnisotropy());
std::vector<GLuint>::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() bool Font::loadVolatile()
{ {
createTexture(); createTexture();
-5
View File
@@ -129,9 +129,6 @@ public:
void setFilter(const Image::Filter &f); void setFilter(const Image::Filter &f);
const Image::Filter &getFilter(); const Image::Filter &getFilter();
void setAnisotropy(float anisotropy);
float getAnisotropy() const;
// Implements Volatile. // Implements Volatile.
bool loadVolatile(); bool loadVolatile();
void unloadVolatile(); void unloadVolatile();
@@ -186,8 +183,6 @@ private:
float lineHeight; float lineHeight;
float mSpacing; // modifies the spacing by multiplying it with this value float mSpacing; // modifies the spacing by multiplying it with this value
float anisotropy;
int texture_size_index; int texture_size_index;
int texture_width; int texture_width;
int texture_height; int texture_height;
-10
View File
@@ -639,16 +639,6 @@ const Image::Filter &Graphics::getDefaultFilter() const
return Image::getDefaultFilter(); 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) void Graphics::setDefaultMipmapFilter(Image::FilterMode filter, float sharpness)
{ {
Image::setDefaultMipmapFilter(filter); Image::setDefaultMipmapFilter(filter);
-6
View File
@@ -319,12 +319,6 @@ public:
**/ **/
const Image::Filter &getDefaultFilter() const; 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. * Default Image mipmap filter mode and sharpness values.
**/ **/
+3 -33
View File
@@ -34,7 +34,6 @@ namespace opengl
{ {
float Image::maxMipmapSharpness = 0.0f; float Image::maxMipmapSharpness = 0.0f;
float Image::maxAnisotropy = 1.0f;
Image::FilterMode Image::defaultMipmapFilter = Image::FILTER_NONE; Image::FilterMode Image::defaultMipmapFilter = Image::FILTER_NONE;
float Image::defaultMipmapSharpness = 0.0f; float Image::defaultMipmapSharpness = 0.0f;
@@ -45,7 +44,6 @@ Image::Image(love::image::ImageData *data)
, texture(0) , texture(0)
, mipmapSharpness(defaultMipmapSharpness) , mipmapSharpness(defaultMipmapSharpness)
, mipmapsCreated(false) , mipmapsCreated(false)
, anisotropy(getDefaultAnisotropy())
{ {
data->retain(); data->retain();
this->data = data; this->data = data;
@@ -208,7 +206,7 @@ void Image::setFilter(const Image::Filter &f)
filter = f; filter = f;
bind(); bind();
setTextureFilter(f); filter.anisotropy = setTextureFilter(f);
checkMipmapsCreated(); checkMipmapsCreated();
} }
@@ -249,24 +247,6 @@ float Image::getMipmapSharpness() const
return mipmapSharpness; 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 void Image::bind() const
{ {
if (texture == 0) if (texture == 0)
@@ -301,7 +281,7 @@ bool Image::loadVolatilePOT()
glGenTextures(1,(GLuint *)&texture); glGenTextures(1,(GLuint *)&texture);
bindTexture(texture); bindTexture(texture);
setTextureFilter(filter); filter.anisotropy = setTextureFilter(filter);
setTextureWrap(wrap); setTextureWrap(wrap);
float p2width = next_p2(width); float p2width = next_p2(width);
@@ -343,7 +323,6 @@ bool Image::loadVolatilePOT()
checkMipmapsCreated(); checkMipmapsCreated();
setMipmapSharpness(mipmapSharpness); setMipmapSharpness(mipmapSharpness);
setAnisotropy(anisotropy);
return true; return true;
} }
@@ -353,7 +332,7 @@ bool Image::loadVolatileNPOT()
glGenTextures(1,(GLuint *)&texture); glGenTextures(1,(GLuint *)&texture);
bindTexture(texture); bindTexture(texture);
setTextureFilter(filter); filter.anisotropy = setTextureFilter(filter);
setTextureWrap(wrap); setTextureWrap(wrap);
while (glGetError() != GL_NO_ERROR); // clear errors while (glGetError() != GL_NO_ERROR); // clear errors
@@ -375,7 +354,6 @@ bool Image::loadVolatileNPOT()
checkMipmapsCreated(); checkMipmapsCreated();
setMipmapSharpness(mipmapSharpness); setMipmapSharpness(mipmapSharpness);
setAnisotropy(anisotropy);
return true; return true;
} }
@@ -429,14 +407,6 @@ Image::FilterMode Image::getDefaultMipmapFilter()
return defaultMipmapFilter; return defaultMipmapFilter;
} }
float Image::getMaxAnisotropy()
{
if (hasAnisotropicFilteringSupport() && maxAnisotropy == 1.0f)
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
return maxAnisotropy;
}
bool Image::hasNpot() bool Image::hasNpot()
{ {
return GLEE_VERSION_2_0 || GLEE_ARB_texture_non_power_of_two; return GLEE_VERSION_2_0 || GLEE_ARB_texture_non_power_of_two;
-9
View File
@@ -104,9 +104,6 @@ public:
const Image::Wrap &getWrap() const; const Image::Wrap &getWrap() const;
void setAnisotropy(float anisotropy);
float getAnisotropy() const;
void setMipmapSharpness(float sharpness); void setMipmapSharpness(float sharpness);
float getMipmapSharpness() const; float getMipmapSharpness() const;
@@ -124,8 +121,6 @@ public:
static void setDefaultMipmapFilter(FilterMode f); static void setDefaultMipmapFilter(FilterMode f);
static FilterMode getDefaultMipmapFilter(); static FilterMode getDefaultMipmapFilter();
static float getMaxAnisotropy();
static bool hasNpot(); static bool hasNpot();
static bool hasAnisotropicFilteringSupport(); static bool hasAnisotropicFilteringSupport();
static bool hasMipmapSupport(); static bool hasMipmapSupport();
@@ -158,9 +153,6 @@ private:
// True if mipmaps have been created for this Image. // True if mipmaps have been created for this Image.
bool mipmapsCreated; bool mipmapsCreated;
// Anisotropic filtering value.
float anisotropy;
// The image's filter mode // The image's filter mode
Image::Filter filter; Image::Filter filter;
@@ -173,7 +165,6 @@ private:
void checkMipmapsCreated(); void checkMipmapsCreated();
static float maxMipmapSharpness; static float maxMipmapSharpness;
static float maxAnisotropy;
static FilterMode defaultMipmapFilter; static FilterMode defaultMipmapFilter;
static float defaultMipmapSharpness; static float defaultMipmapSharpness;
+21 -1
View File
@@ -39,6 +39,8 @@ static bool contextInitialized = false;
static int curTextureUnit = 0; static int curTextureUnit = 0;
static std::vector<GLuint> textureUnits; static std::vector<GLuint> textureUnits;
static float maxAnisotropy = 1.0f;
void initializeContext() void initializeContext()
{ {
if (contextInitialized) if (contextInitialized)
@@ -95,6 +97,11 @@ void initializeContext()
glUnmapBufferARB = (GLEEPFNGLUNMAPBUFFERARBPROC) glUnmapBuffer; 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. // Set the 'default' texture (id 0) as a repeating white pixel.
// Otherwise, texture2D inside a shader would return black when drawing graphics primitives, // 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. // 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); glDeleteTextures(1, &texture);
} }
void setTextureFilter(const graphics::Image::Filter &f) float setTextureFilter(const graphics::Image::Filter &f)
{ {
GLint gmin, gmag; 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_MIN_FILTER, gmin);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag); 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() graphics::Image::Filter getTextureFilter()
@@ -262,6 +279,9 @@ graphics::Image::Filter getTextureFilter()
break; break;
} }
if (GLEE_EXT_texture_filter_anisotropic)
glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &f.anisotropy);
return f; return f;
} }
+2 -1
View File
@@ -71,8 +71,9 @@ void deleteTexture(GLuint texture);
/** /**
* Sets the image filter mode for the currently bound 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. * Returns the image filter mode for the currently bound texture.
+3 -5
View File
@@ -99,10 +99,9 @@ int w_Canvas_setFilter(lua_State *L)
if (!Image::getConstant(magstr, f.mag)) if (!Image::getConstant(magstr, f.mag))
return luaL_error(L, "Invalid filter mode: %s", magstr); 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->setFilter(f);
canvas->setAnisotropy(anisotropy);
return 0; return 0;
@@ -120,8 +119,7 @@ int w_Canvas_getFilter(lua_State *L)
lua_pushstring(L, minstr); lua_pushstring(L, minstr);
lua_pushstring(L, magstr); lua_pushstring(L, magstr);
lua_pushnumber(L, f.anisotropy);
lua_pushnumber(L, canvas->getAnisotropy());
return 3; return 3;
} }
+3 -4
View File
@@ -103,6 +103,8 @@ int w_Font_setFilter(lua_State *L)
if (!Image::getConstant(magstr, f.mag)) if (!Image::getConstant(magstr, f.mag))
return luaL_error(L, "Invalid filter mode: %s", magstr); return luaL_error(L, "Invalid filter mode: %s", magstr);
f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
try try
{ {
t->setFilter(f); t->setFilter(f);
@@ -112,9 +114,6 @@ int w_Font_setFilter(lua_State *L)
return luaL_error(L, "%s", e.what()); return luaL_error(L, "%s", e.what());
} }
float anisotropy = (float) luaL_optnumber(L, 4, 1.0);
t->setAnisotropy(anisotropy);
return 0; return 0;
} }
@@ -128,7 +127,7 @@ int w_Font_getFilter(lua_State *L)
Image::getConstant(f.mag, magstr); Image::getConstant(f.mag, magstr);
lua_pushstring(L, minstr); lua_pushstring(L, minstr);
lua_pushstring(L, magstr); lua_pushstring(L, magstr);
lua_pushnumber(L, t->getAnisotropy()); lua_pushnumber(L, f.anisotropy);
return 3; return 3;
} }
@@ -749,14 +749,14 @@ int w_setDefaultFilter(lua_State *L)
if (!Image::getConstant(magstr, mag)) if (!Image::getConstant(magstr, mag))
return luaL_error(L, "Invalid filter mode: %s", magstr); return luaL_error(L, "Invalid filter mode: %s", magstr);
float anisotropy = (float) luaL_optnumber(L, 3, 1.0);
Image::Filter f; Image::Filter f;
f.min = min; f.min = min;
f.mag = mag; f.mag = mag;
f.anisotropy = anisotropy;
instance->setDefaultFilter(f); instance->setDefaultFilter(f);
float anisotropy = (float) luaL_optnumber(L, 3, 1.0);
instance->setDefaultAnisotropy(anisotropy);
return 0; return 0;
} }
@@ -772,7 +772,7 @@ int w_getDefaultFilter(lua_State *L)
return luaL_error(L, "Unknown magnification filter mode"); return luaL_error(L, "Unknown magnification filter mode");
lua_pushstring(L, minstr); lua_pushstring(L, minstr);
lua_pushstring(L, magstr); lua_pushstring(L, magstr);
lua_pushnumber(L, instance->getDefaultAnisotropy()); lua_pushnumber(L, f.anisotropy);
return 3; return 3;
} }
+3 -4
View File
@@ -68,6 +68,8 @@ int w_Image_setFilter(lua_State *L)
if (!Image::getConstant(magstr, f.mag)) if (!Image::getConstant(magstr, f.mag))
return luaL_error(L, "Invalid filter mode: %s", magstr); return luaL_error(L, "Invalid filter mode: %s", magstr);
f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
try try
{ {
t->setFilter(f); t->setFilter(f);
@@ -77,9 +79,6 @@ int w_Image_setFilter(lua_State *L)
return luaL_error(L, "%s", e.what()); return luaL_error(L, "%s", e.what());
} }
float anisotropy = (float) luaL_optnumber(L, 4, 1.0);
t->setAnisotropy(anisotropy);
return 0; return 0;
} }
@@ -93,7 +92,7 @@ int w_Image_getFilter(lua_State *L)
Image::getConstant(f.mag, magstr); Image::getConstant(f.mag, magstr);
lua_pushstring(L, minstr); lua_pushstring(L, minstr);
lua_pushstring(L, magstr); lua_pushstring(L, magstr);
lua_pushnumber(L, t->getAnisotropy()); lua_pushnumber(L, f.anisotropy);
return 3; return 3;
} }