aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/qml2puppet/qml2puppet/instances/qt5bakelightsnodeinstanceserver.cpp
blob: d7a8b313acc21b5e1ac22fdfeea7801f3d1c54cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "qt5bakelightsnodeinstanceserver.h"

#if BAKE_LIGHTS_SUPPORTED
#include "createscenecommand.h"
#include "view3dactioncommand.h"

#include "nodeinstanceclientinterface.h"
#include "puppettocreatorcommand.h"

#include <QDir>
#include <QFileInfo>
#include <QLibraryInfo>
#include <QProcess>
#include <QQuickView>
#include <QQuickItem>

#include <private/qquickdesignersupport_p.h>
#include <private/qquick3dviewport_p.h>
#endif

namespace QmlDesigner {

Qt5BakeLightsNodeInstanceServer::Qt5BakeLightsNodeInstanceServer(NodeInstanceClientInterface *nodeInstanceClient) :
    Qt5NodeInstanceServer(nodeInstanceClient)
{
    setSlowRenderTimerInterval(100000000);
    setRenderTimerInterval(100);
}

#if BAKE_LIGHTS_SUPPORTED

Qt5BakeLightsNodeInstanceServer::~Qt5BakeLightsNodeInstanceServer()
{
    cleanup();
}

void Qt5BakeLightsNodeInstanceServer::createScene(const CreateSceneCommand &command)
{
    initializeView();
    registerFonts(command.resourceUrl);
    setTranslationLanguage(command.language);
    setupScene(command);
    startRenderTimer();

    // Set working directory to a temporary directory, as baking process creates a file there
    if (m_workingDir.isValid())
        QDir::setCurrent(m_workingDir.path());
}

void Qt5BakeLightsNodeInstanceServer::view3DAction(const View3DActionCommand &command)
{
    switch (command.type()) {
    case View3DActionType::SetBakeLightsView3D: {
        QString id = command.value().toString();
        const QList<ServerNodeInstance> allViews = allView3DInstances();
        for (const auto &view : allViews) {
            if (view.id() == id) {
                m_view3D = qobject_cast<QQuick3DViewport *>(view.internalObject());
                break;
            }
        }

        if (!m_view3D)
            abort(tr("View3D not found: '%1'").arg(id));
        else
            startRenderTimer();
        break;
    }
    default:
        break;
    }
}

void Qt5BakeLightsNodeInstanceServer::startRenderTimer()
{
    if (timerId() != 0)
        killTimer(timerId());

    int timerId = startTimer(renderTimerInterval());

    setTimerId(timerId);
}

void Qt5BakeLightsNodeInstanceServer::bakeLights()
{
    if (!m_view3D) {
        abort(tr("Invalid View3D object set."));
        return;
    }

    QQuick3DLightmapBaker::Callback callback = [this](QQuick3DLightmapBaker::BakingStatus status,
            std::optional<QString> msg, QQuick3DLightmapBaker::BakingControl *) {
        m_callbackReceived = true;
        switch (status) {
        case QQuick3DLightmapBaker::BakingStatus::Progress:
        case QQuick3DLightmapBaker::BakingStatus::Warning:
        case QQuick3DLightmapBaker::BakingStatus::Error:
            nodeInstanceClient()->handlePuppetToCreatorCommand(
                        {PuppetToCreatorCommand::BakeLightsProgress, msg.value_or("")});
            break;
        case QQuick3DLightmapBaker::BakingStatus::Cancelled:
            abort(tr("Baking cancelled."));
            break;
        case QQuick3DLightmapBaker::BakingStatus::Complete:
            runDenoiser();
            break;
        default:
            qWarning() << __FUNCTION__ << "Unexpected light baking status received:"
                       << int(status) << msg.value_or("");
            break;
        }
    };

    QQuick3DLightmapBaker *baker = m_view3D->lightmapBaker();
    baker->bake(callback);

    m_bakingStarted = true;
}

void Qt5BakeLightsNodeInstanceServer::cleanup()
{
    m_workingDir.remove();
    if (m_denoiser) {
        if (m_denoiser->state() == QProcess::Running)
            m_denoiser->terminate();
        m_denoiser->deleteLater();
    }
}

void Qt5BakeLightsNodeInstanceServer::abort(const QString &msg)
{
    cleanup();
    nodeInstanceClient()->handlePuppetToCreatorCommand(
                {PuppetToCreatorCommand::BakeLightsAborted, msg});
}

void Qt5BakeLightsNodeInstanceServer::finish()
{
    cleanup();
    nodeInstanceClient()->handlePuppetToCreatorCommand(
                {PuppetToCreatorCommand::BakeLightsFinished, {}});
}

void Qt5BakeLightsNodeInstanceServer::runDenoiser()
{
    // Check if denoiser exists (as it is third party app)
    QString binPath = QLibraryInfo::path(QLibraryInfo::BinariesPath);
#if defined(Q_OS_WIN)
    binPath += "/qlmdenoiser.exe";
#elif defined(Q_OS_MACOS)
    // TODO: What is the path in mac?
#else
    binPath += "/qlmdenoiser";
#endif

    QFileInfo fi(binPath);
    if (!fi.exists()) {
        nodeInstanceClient()->handlePuppetToCreatorCommand(
                    {PuppetToCreatorCommand::BakeLightsProgress,
                     tr("Warning: Denoiser executable not found, cannot denoise baked lightmaps (%1).")
                     .arg(binPath)});
        finish();
        return;
    }

    m_denoiser = new QProcess();

    QObject::connect(m_denoiser, &QProcess::errorOccurred, this, [this](QProcess::ProcessError) {
        m_workingDir.remove();
        nodeInstanceClient()->handlePuppetToCreatorCommand(
                    {PuppetToCreatorCommand::BakeLightsProgress,
                     tr("Warning: An error occurred while running denoiser process!")});
        finish();
    });

    QObject::connect(m_denoiser, &QProcess::finished, this, [this](int exitCode,
                                                                   QProcess::ExitStatus exitStatus) {
        if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
            nodeInstanceClient()->handlePuppetToCreatorCommand(
                        {PuppetToCreatorCommand::BakeLightsProgress,
                         tr("Denoising finished.")});
        } else {
            nodeInstanceClient()->handlePuppetToCreatorCommand(
                        {PuppetToCreatorCommand::BakeLightsProgress,
                         tr("Warning: Denoiser process failed with exit code '%1'!").arg(exitCode)});
        }
        finish();
    });

    nodeInstanceClient()->handlePuppetToCreatorCommand(
                {PuppetToCreatorCommand::BakeLightsProgress,
                 tr("Denoising baked lightmaps...")});

    m_denoiser->setWorkingDirectory(m_workingDir.path());
    m_denoiser->start(binPath, {"qlm_list.txt"});

}

void Qt5BakeLightsNodeInstanceServer::collectItemChangesAndSendChangeCommands()
{
    static bool inFunction = false;

    if (!rootNodeInstance().holdsGraphical())
        return;

    if (!inFunction) {
        inFunction = true;

        QQuickDesignerSupport::polishItems(quickWindow());

        render();

        inFunction = false;
    }
}

void Qt5BakeLightsNodeInstanceServer::render()
{
    // Render multiple times to make sure everything gets rendered correctly before baking
    if (++m_renderCount == 4) {
        bakeLights();
    } else {
        rootNodeInstance().updateDirtyNodeRecursive();
        renderWindow();
        if (m_bakingStarted) {
            slowDownRenderTimer(); // No more renders needed
            if (!m_callbackReceived)
                abort(tr("No bakeable models detected."));
        }
    }
}
#endif

} // namespace QmlDesigner