Debian Codegraph @

src/Gui/Multisample.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2024 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24
25 #include <QCoreApplication>
26 #include <QOpenGLFramebufferObjectFormat>
27
28
29 #include "Multisample.h"
30 #include <App/Application.h>
31 #include <Base/Parameter.h>
32
33 using namespace Gui;
34
35 // clang-format off
36 static constexpr auto numMSAA {6};
37 static constexpr auto idStr {0};
38 static constexpr auto idEnum {1};
39 static constexpr auto idMSAA {2};
40 static constexpr std::array<std::tuple<std::string_view, AntiAliasing, int>, numMSAA> textMSAA {{
41 {QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "None"), AntiAliasing::None, 0},
42 {QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "Line smoothing"), AntiAliasing::MSAA1x, 1},
43 {QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 2x"), AntiAliasing::MSAA2x, 2},
44 {QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 4x"), AntiAliasing::MSAA4x, 4},
45 {QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 6x"), AntiAliasing::MSAA6x, 6},
46 {QT_TRANSLATE_NOOP("Gui::Dialog::DlgSettings3DView", "MSAA 8x"), AntiAliasing::MSAA8x, 8},
47 }};
48 // clang-format on
49
50 Multisample::Multisample()
51 {
52 context.setFormat(format);
53 context.create();
54 offscreen.setFormat(format);
55 offscreen.create();
56 context.makeCurrent(&offscreen);
57 }
58
59 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
60 bool Multisample::testSamples(int num) const
61 {
62 // This is always true
63 if (num == 0 || num == 1) {
64 return true;
65 }
66
67 QOpenGLFramebufferObjectFormat fboFormat;
68 fboFormat.setAttachment(QOpenGLFramebufferObject::Depth);
69 fboFormat.setSamples(num);
70 QOpenGLFramebufferObject fbo(100, 100, fboFormat); // NOLINT
71 return fbo.format().samples() == num;
72 }
73
74 std::vector<std::pair<QString, AntiAliasing>> Multisample::supported() const
75 {
76 std::vector<std::pair<QString, AntiAliasing>> modes;
77 std::for_each(textMSAA.begin(), textMSAA.end(), [&modes, this](const auto& mode) {
78 if (testSamples(std::get<idMSAA>(mode))) {
79 const char* context = "Gui::Dialog::DlgSettings3DView";
80 QString str = QCoreApplication::translate(context, std::get<idStr>(mode).data());
81 modes.emplace_back(str, std::get<idEnum>(mode));
82 }
83 });
84 return modes;
85 }
86
87 int Multisample::toSamples(AntiAliasing msaa)
88 {
89 auto it = std::find_if(textMSAA.begin(), textMSAA.end(), [msaa](const auto& mode) {
90 return std::get<idEnum>(mode) == msaa;
91 });
92 if (it != textMSAA.end()) {
93 return std::get<idMSAA>(*it);
94 }
95 return 0;
96 }
97
98 AntiAliasing Multisample::toAntiAliasing(int samples)
99 {
100 auto it = std::find_if(textMSAA.begin(), textMSAA.end(), [samples](const auto& mode) {
101 return std::get<idMSAA>(mode) == samples;
102 });
103 if (it != textMSAA.end()) {
104 return std::get<idEnum>(*it);
105 }
106 return AntiAliasing::None;
107 }
108
109 AntiAliasing Multisample::readMSAAFromSettings()
110 {
111 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
112 "User parameter:BaseApp/Preferences/View"
113 );
114 return AntiAliasing(hGrp->GetInt("AntiAliasing", int(Gui::AntiAliasing::None)));
115 }
116
117 void Multisample::writeMSAAToSettings(AntiAliasing msaa)
118 {
119 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
120 "User parameter:BaseApp/Preferences/View"
121 );
122 hGrp->SetInt("AntiAliasing", long(msaa));
123 }