Skip to content

Commit 181f04e

Browse files
committed
Add support for anisotropic filtering of StelTexture
1 parent aac75af commit 181f04e

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

guide/app_config_ini.tex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,9 @@ \subsection{\big[video\big]}
846846
viewport\_effect & string & This is used when the spheric mirror display mode is activated. Values include \emph{none} and \emph{sphericMirrorDistorter}.\\%\midrule
847847
minimum\_fps & int & Sets the minimum number of frames per second to display at (hardware performance permitting)\\%\midrule
848848
maximum\_fps & int & Sets the maximum number of frames per second to display at. This is useful to reduce power consumption in laptops.\\%\midrule
849-
multisampling & int & Sets the number of samples to use for multisampling antialiasing. \emph{0} disables antialiasing, \emph{1} is no-op (single sample per pixel), higher values increase smoothness and degrade performance. Too high a value may result in excessive blurriness of the GUI. A good starting value to try is \emph{4}.\\\bottomrule
849+
multisampling & int & Sets the number of samples to use for multisampling antialiasing. \emph{0} disables antialiasing, \emph{1} is no-op (single sample per pixel), higher values increase smoothness and degrade performance. Too high a value may result in excessive blurriness of the GUI. A good starting value to try is \emph{4}.\\
850+
anisotropic\_filtering & int & Improves quality of rendering of inclined textured surfaces like e.g. planetary surface near the limb. Modern (and even quite old, like from 2010) GPUs can handle high anisotropy with no noticeable performance drop, so it should be fine to set this value as high as 16. The default is 0, which disables anisotropic filtering.\\
851+
\bottomrule
850852
\end{tabularx}
851853

852854
\subsection{\big[viewing\big]}

src/StelMainView.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ Q_LOGGING_CATEGORY(mainview, "stel.MainView")
7777

7878
#include <clocale>
7979

80+
#ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY
81+
# define GL_MAX_TEXTURE_MAX_ANISOTROPY 0x84FF
82+
#endif
83+
8084
// Initialize static variables
8185
StelMainView* StelMainView::singleton = Q_NULLPTR;
8286

@@ -864,6 +868,32 @@ void StelMainView::init()
864868
glInfo.isGLES = format.renderableType()==QSurfaceFormat::OpenGLES;
865869
qDebug().nospace() << "Luminance textures are " << (glInfo.supportsLuminanceTextures ? "" : "not ") << "supported";
866870
glInfo.isCoreProfile = format.profile() == QSurfaceFormat::CoreProfile;
871+
if(format.majorVersion() * 1000 + format.minorVersion() >= 4006 ||
872+
glInfo.mainContext->hasExtension("GL_EXT_texture_filter_anisotropic") ||
873+
glInfo.mainContext->hasExtension("GL_ARB_texture_filter_anisotropic"))
874+
{
875+
StelOpenGL::clearGLErrors();
876+
auto& gl = *QOpenGLContext::currentContext()->functions();
877+
gl.glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &glInfo.maxAnisotropy);
878+
const auto error = gl.glGetError();
879+
if(error != GL_NO_ERROR)
880+
{
881+
qDebug() << "Failed to get maximum texture anisotropy:" << StelOpenGL::getGLErrorText(error);
882+
}
883+
else if(glInfo.maxAnisotropy > 0)
884+
{
885+
qDebug() << "Maximum texture anisotropy:" << glInfo.maxAnisotropy;
886+
}
887+
else
888+
{
889+
qDebug() << "Maximum texture anisotropy is not positive:" << glInfo.maxAnisotropy;
890+
glInfo.maxAnisotropy = 0;
891+
}
892+
}
893+
else
894+
{
895+
qDebug() << "Anisotropic filtering is not supported!";
896+
}
867897

868898
gui = new StelGui();
869899

src/StelMainView.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class StelMainView : public QGraphicsView
7272
QSurface* surface;
7373
QOpenGLContext* mainContext;
7474
QOpenGLFunctions* functions;
75+
GLint maxAnisotropy = 0;
7576
bool supportsLuminanceTextures = false;
7677
bool isCoreProfile = false;
7778
bool isGLES = false;

src/core/StelTexture.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include <QFuture>
3636
#include <QtConcurrent>
3737

38+
#ifndef GL_TEXTURE_MAX_ANISOTROPY
39+
# define GL_TEXTURE_MAX_ANISOTROPY 0x84FE
40+
#endif
41+
3842
StelTexture::StelTexture(StelTextureMgr *mgr) : textureMgr(mgr), gl(Q_NULLPTR), networkReply(Q_NULLPTR), loader(Q_NULLPTR), errorOccured(false), alphaChannel(false), id(0),
3943
width(-1), height(-1), glSize(0)
4044
{
@@ -411,8 +415,26 @@ bool StelTexture::glLoad(const GLData& data)
411415
if (loadParams.generateMipmaps)
412416
{
413417
gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, loadParams.filterMipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST);
418+
419+
const auto conf = StelApp::getInstance().getSettings();
420+
const GLint desiredAnisotropyLevel = conf->value("video/anisotropic_filtering", 0).toUInt();
421+
const auto maxAnisotropy = StelMainView::getInstance().getGLInformation().maxAnisotropy;
422+
if(maxAnisotropy > 0 && desiredAnisotropyLevel > 0)
423+
{
424+
const int anisotropy = std::min(maxAnisotropy, desiredAnisotropyLevel);
425+
gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, anisotropy);
426+
427+
// Assuming maximum possible anisotropy, all mipmaps will require
428+
// 4× the size of base mip level. Generally anisotropy may be
429+
// lower, but we prefer to overestimate VRAM consumption than to
430+
// underestimate it.
431+
glSize *= 4;
432+
}
433+
else
434+
{
435+
glSize = glSize + glSize/3; //mipmaps require 1/3 more mem
436+
}
414437
gl->glGenerateMipmap(GL_TEXTURE_2D);
415-
glSize = glSize + glSize/3; //mipmaps require 1/3 more mem
416438
}
417439

418440
//register ID with textureMgr and increment size

0 commit comments

Comments
 (0)