Gui: Add class Multisample to check supported MSAA modes

This commit is contained in:
wmayer
2024-07-11 16:40:55 +02:00
parent 9eeca1f6ad
commit 692d84a085
3 changed files with 177 additions and 0 deletions

View File

@@ -850,6 +850,7 @@ SET(View3D_CPP_SRCS
Flag.cpp
GLBuffer.cpp
GLPainter.cpp
Multisample.cpp
MouseSelection.cpp
NavigationStyle.cpp
InventorNavigationStyle.cpp
@@ -882,6 +883,7 @@ SET(View3D_SRCS
Flag.h
GLBuffer.h
GLPainter.h
Multisample.h
MouseSelection.h
NavigationStyle.h
GestureNavigationStyle.h

105
src/Gui/Multisample.cpp Normal file
View File

@@ -0,0 +1,105 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <QCoreApplication>
#include <QOpenGLFramebufferObjectFormat>
#endif
#include "Multisample.h"
using namespace Gui;
// clang-format off
static constexpr auto numMSAA {6};
static constexpr auto idStr {0};
static constexpr auto idEnum {1};
static constexpr auto idMSAA {2};
static constexpr std::array<std::tuple<std::string_view, AntiAliasing, int>, numMSAA> textMSAA {{
{QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "None"), AntiAliasing::None, 0},
{QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "Line Smoothing"), AntiAliasing::MSAA1x, 1},
{QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 2x"), AntiAliasing::MSAA2x, 2},
{QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 4x"), AntiAliasing::MSAA4x, 4},
{QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 6x"), AntiAliasing::MSAA6x, 6},
{QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 8x"), AntiAliasing::MSAA8x, 8},
}};
// clang-format on
Multisample::Multisample()
{
context.setFormat(format);
context.create();
offscreen.setFormat(format);
offscreen.create();
context.makeCurrent(&offscreen);
}
bool Multisample::testSamples(int num) const
{
// This is always true
if (num == 0 || num == 1) {
return true;
}
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QOpenGLFramebufferObject::Depth);
fboFormat.setSamples(num);
QOpenGLFramebufferObject fbo(100, 100, fboFormat); // NOLINT
return fbo.format().samples() == num;
}
std::vector<std::pair<QString, AntiAliasing>> Multisample::supported() const
{
std::vector<std::pair<QString, AntiAliasing>> modes;
std::for_each(textMSAA.begin(), textMSAA.end(), [&modes, this](const auto& mode) {
if (testSamples(std::get<idMSAA>(mode))) {
const char* context = "Gui::Dialog::DlgSettings3DView";
QString str = QCoreApplication::translate(context, std::get<idStr>(mode).data());
modes.emplace_back(str, std::get<idEnum>(mode));
}
});
return modes;
}
int Multisample::toSamples(AntiAliasing msaa)
{
auto it = std::find_if(textMSAA.begin(), textMSAA.end(), [msaa](const auto& mode) {
return std::get<idEnum>(mode) == msaa;
});
if (it != textMSAA.end()) {
return std::get<idMSAA>(*it);
}
return 0;
}
AntiAliasing Multisample::toAntiAliasing(int samples)
{
auto it = std::find_if(textMSAA.begin(), textMSAA.end(), [samples](const auto& mode) {
return std::get<idMSAA>(mode) == samples;
});
if (it != textMSAA.end()) {
return std::get<idEnum>(*it);
}
return AntiAliasing::None;
}

70
src/Gui/Multisample.h Normal file
View File

@@ -0,0 +1,70 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#ifndef GUI_MULTISAMPLE_H
#define GUI_MULTISAMPLE_H
#include <map>
#include <vector>
#include <FCGlobal.h>
#include <QOffscreenSurface>
#include <QOpenGLContext>
#include <QSurfaceFormat>
namespace Gui
{
/** @name Anti-Aliasing modes of the rendered 3D scene
* Specifies Anti-Aliasing (AA) method
* - Smoothing enables OpenGL line and vertex smoothing (basically deprecated)
* - MSAA is hardware multi sampling (with 2, 4, 6 or 8 passes), a quite common and efficient AA technique
*/
//@{
enum class AntiAliasing {
None = 0,
MSAA1x = 1,
MSAA2x = 2,
MSAA4x = 3,
MSAA6x = 5,
MSAA8x = 4
};
//@}
class GuiExport Multisample
{
public:
Multisample();
bool testSamples(int num) const;
std::vector<std::pair<QString, AntiAliasing>> supported() const;
static int toSamples(AntiAliasing msaa);
static AntiAliasing toAntiAliasing(int samples);
private:
QSurfaceFormat format;
QOpenGLContext context;
QOffscreenSurface offscreen;
};
} // namespace Gui
#endif // GUI_MULTISAMPLE_H