summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Render/StudioSubPresentationRenderer.cpp
blob: 893ee654fed903927f7469076097a528e069bb2e (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "StudioSubPresentationRenderer.h"
#include "render/Qt3DSRenderContext.h"
#include "q3dspresentation.h"
#include "q3dsviewersettings.h"

#include <QtCore/qfileinfo.h>
#include <qxmlstream.h>

using namespace qt3ds::render;

class RendererThread : public QThread
{
public:
    RendererThread(QThread *mainThread)
        : m_running(false)
        , m_updated(false)
        , m_initialized(false)
        , m_mainThread(mainThread)
        , m_semaphore(0)
    {

    }

    QSize readPresentationSize(const QString &url)
    {
        QFile file(url);
        file.open(QFile::Text | QFile::ReadOnly);
        if (!file.isOpen()) {
            qWarning () << file.errorString();
            return QSize();
        }
        QXmlStreamReader reader(&file);
        reader.setNamespaceProcessing(false);

        if (reader.readNextStartElement() && reader.qualifiedName() == QLatin1String("UIP")) {
            if (reader.readNextStartElement()
                    && reader.qualifiedName() == QLatin1String("Project")) {
                if (reader.readNextStartElement()
                        && reader.qualifiedName() == QLatin1String("ProjectSettings")) {
                    const auto attrib = reader.attributes();
                    return QSize(attrib.value(QLatin1String("presentationWidth")).toInt(),
                                 attrib.value(QLatin1String("presentationHeight")).toInt());
                }
            }
        }
        return QSize();
    }

    void initialize(const QString &presentation, const QString &path)
    {
        m_path = path;
        m_presentation = presentation;

        m_surfaceViewer.reset(new Q3DSSurfaceViewer);
        m_context.reset(new QT_PREPEND_NAMESPACE(QOpenGLContext));
        m_surface.reset(new QOffscreenSurface);

        QSurfaceFormat format = QSurfaceFormat::defaultFormat();
        if (QT_PREPEND_NAMESPACE(QOpenGLContext)::currentContext())
            format = QT_PREPEND_NAMESPACE(QOpenGLContext)::currentContext()->format();
        m_context->setShareContext(QT_PREPEND_NAMESPACE(QOpenGLContext)::currentContext());
        m_context->setFormat(format);
        m_surface->setFormat(format);
        m_context->create();
        m_surface->create();
        m_context->moveToThread(this);
        m_surface->moveToThread(this);
        m_surfaceViewer->moveToThread(this);
#ifdef Q3DS_PREVIEW_SUBPRESENTATION_RT2
        QObject::connect(m_surfaceViewer.data(), &Q3DSSurfaceViewer::frameUpdate,
                         this, &RendererThread::frameRendered);
#else
        QObject::connect(m_surfaceViewer.data(), &Q3DSSurfaceViewer::frameUpdated,
                         this, &RendererThread::frameRendered);
#endif
        m_initialized = true;
    }

    void run() override
    {
        QFileInfo info(m_path + "/" + m_presentation);
        m_size = readPresentationSize(m_path + "/" + m_presentation);

        m_context->makeCurrent(m_surface.data());

        m_fbo.reset(new QOpenGLFramebufferObject(m_size,
                                                 QOpenGLFramebufferObject::CombinedDepthStencil));

        m_surfaceViewer->setSize(m_size);
        m_surfaceViewer->setAutoSize(false);
        m_surfaceViewer->setUpdateInterval(-1);
        m_surfaceViewer->presentation()->setSource(QUrl::fromLocalFile(info.absoluteFilePath()));
        m_surfaceViewer->settings()->setMatteColor(Qt::transparent);
#ifdef Q3DS_PREVIEW_SUBPRESENTATION_RT2
        m_surfaceViewer->create(m_surface.data(), m_context.data(), m_fbo->handle());
#else
        m_surfaceViewer->initialize(m_surface.data(), m_context.data(), m_fbo->handle());
#endif
        m_running = true;
        m_semaphore.release();

        m_context->doneCurrent();

        while (true) {
            QMutexLocker lock(&m_mutex);
            if (!m_running)
                break;
            m_context->makeCurrent(m_surface.data());
            if (!m_surfaceViewer->isRunning()) {
                qWarning () << "Subpresentation stopped running. Stopping updating.";
                break;
            }

            m_surfaceViewer->update();

            m_updated = true;
            m_context->doneCurrent();
            lock.unlock();

            // Update approx 30 fps
            QThread::usleep(33000);
        }
        m_fbo.reset();
#ifdef Q3DS_PREVIEW_SUBPRESENTATION_RT2
        m_surfaceViewer->destroy();
#else
        m_surfaceViewer->shutdown();
#endif
        m_surfaceViewer.reset();
        m_context->doneCurrent();
        m_context.reset();
        m_surface->moveToThread(m_mainThread);
        m_semaphore.release();
    }

    void frameRendered()
    {
        m_updated = true;
    }

    QScopedPointer<Q3DSSurfaceViewer> m_surfaceViewer;
    QScopedPointer<QT_PREPEND_NAMESPACE(QOpenGLContext)> m_context;
    QScopedPointer<QOffscreenSurface> m_surface;
    QScopedPointer<QOpenGLFramebufferObject> m_fbo;
    QString m_path;
    QString m_presentation;
    QMutex m_mutex;
    bool m_running, m_updated, m_initialized;
    QThread *m_mainThread;
    QSemaphore m_semaphore;
    QSize m_size;
};

StudioSubpresentationRenderer::StudioSubpresentationRenderer(
        IQt3DSRenderContext &inRenderContext, const QString &id,
        const QString &presentation, const QString &path)
    : m_renderContext(inRenderContext), m_id(id), m_presentation(presentation), m_path(path)
    , m_program(nullptr)
    , m_vao(nullptr)
    , m_vertices(nullptr)
    , m_callback(nullptr)
    , m_rendererType(inRenderContext.GetStringTable().RegisterStr("StudioPresentationRenderer"))
    , mRefCount(0)
{
    m_thread.reset(new RendererThread(QThread::currentThread()));
}

StudioSubpresentationRenderer::~StudioSubpresentationRenderer()
{
    QMutexLocker lock(&m_thread->m_mutex);
    if (m_thread->m_running) {
        m_thread->m_running = false;
        lock.unlock();
        m_thread->m_semaphore.acquire();
    }
}

CRegisteredString StudioSubpresentationRenderer::GetOffscreenRendererType()
{
    return m_rendererType;
}

SOffscreenRendererEnvironment
StudioSubpresentationRenderer::GetDesiredEnvironment(QT3DSVec2 inPresentationScaleFactor)
{
    // If we aren't using a clear color, then we are expected to blend with the background
    if (!m_thread->m_initialized) {
        initialize();
    }
    bool hasTransparency = true;
    NVRenderTextureFormats::Enum format =
        hasTransparency ? NVRenderTextureFormats::RGBA8 : NVRenderTextureFormats::RGB8;
    return SOffscreenRendererEnvironment(
                 QT3DSU32(m_thread->m_size.width() * inPresentationScaleFactor.x),
                 QT3DSU32(m_thread->m_size.height() * inPresentationScaleFactor.y),
                 format, OffscreenRendererDepthValues::Depth24, false,
                 AAModeValues::NoAA);
}

SOffscreenRenderFlags StudioSubpresentationRenderer::NeedsRender(
        const SOffscreenRendererEnvironment &inEnvironment,
        QT3DSVec2 inPresentationScaleFactor,
        const SRenderInstanceId instanceId)
{
    Q_UNUSED(inEnvironment)
    Q_UNUSED(inPresentationScaleFactor)
    Q_UNUSED(instanceId)
    return SOffscreenRenderFlags(true, true);
}

void StudioSubpresentationRenderer::initialize()
{
    m_thread->initialize(m_presentation, m_path);
    m_thread->start();
    m_thread->m_semaphore.acquire();
    if (m_callback)
        m_callback->onOffscreenRendererInitialized(m_id);
}

void StudioSubpresentationRenderer::Render(const SOffscreenRendererEnvironment &inEnvironment,
                    NVRenderContext &inRenderContext, QT3DSVec2 inPresentationScaleFactor,
                    SScene::RenderClearCommand inColorBufferNeedsClear,
                    const SRenderInstanceId instanceId)
{
    Q_UNUSED(inEnvironment)
    Q_UNUSED(inPresentationScaleFactor)
    Q_UNUSED(instanceId)
    inRenderContext.PushPropertySet();
    if (!m_thread->m_initialized) {
        initialize();
    }
    QMutexLocker lock(&m_thread->m_mutex);
    if (m_thread->m_initialized && m_thread->m_updated) {
        QT_PREPEND_NAMESPACE(QOpenGLContext) *context
                = QT_PREPEND_NAMESPACE(QOpenGLContext)::currentContext();
        QOpenGLFunctions *func = context->functions();
        GLuint texture = m_thread->m_fbo->texture();
        func->glDisable(GL_DEPTH_TEST);
        if (inColorBufferNeedsClear == SScene::RenderClearCommand::AlwaysClear)
            func->glDisable(GL_BLEND);
        else
            func->glEnable(GL_BLEND);
        func->glBlendEquation(GL_FUNC_ADD);
        func->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        if (!m_program)
            initializeFboCopy();

        m_program->bind();

        if (!m_vao) {
            m_vao = new QOpenGLVertexArrayObject;
            m_vao->create();
            m_vao->bind();
            m_vertices->bind();

            m_program->enableAttributeArray(0);
            m_program->enableAttributeArray(1);
            func->glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr);
            func->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
                                        reinterpret_cast<const void *>(4 * sizeof(GLfloat)));
            m_vertices->release();
        } else {
            m_vao->bind();
        }

        func->glActiveTexture(GL_TEXTURE0);
        func->glBindTexture(GL_TEXTURE_2D, texture);
        func->glDrawArrays(GL_TRIANGLES, 0, 6);
        func->glEnable(GL_DEPTH_TEST);
        func->glBindTexture(GL_TEXTURE_2D, 0);

        m_program->release();
        m_vao->release();

        if (m_callback)
            m_callback->onOffscreenRendererFrame(m_id);
    }
    inRenderContext.PopPropertySet(true);
}

void StudioSubpresentationRenderer::RenderWithClear(
                    const SOffscreenRendererEnvironment &inEnvironment,
                    NVRenderContext &inRenderContext, QT3DSVec2 inPresentationScaleFactor,
                    SScene::RenderClearCommand inColorBufferNeedsClear,
                    QT3DSVec3 inclearColor,
                    const SRenderInstanceId instanceId)
{
    inRenderContext.SetClearColor(QT3DSVec4(inclearColor, 1.0));
    Render(inEnvironment, inRenderContext, inPresentationScaleFactor,
           inColorBufferNeedsClear, instanceId);
}

void StudioSubpresentationRenderer::initializeFboCopy()
{
    m_program = new QOpenGLShaderProgram;
    if (m_thread->m_context->format().renderableType() == QSurfaceFormat::OpenGLES) {
        static const char *vsSource =
            "attribute highp vec4 pos;\n"
            "attribute highp vec2 tc;\n"
            "varying lowp vec2 texcoord;\n"
            "void main() {\n"
            "   texcoord = tc;\n"
            "   gl_Position = pos;\n"
            "}\n";
        static const char *fsSource =
            "varying highp vec2 texcoord;\n"
            "uniform sampler2D sampler;\n"
            "void main() {\n"
            "   gl_FragColor = texture2D(sampler, texcoord);\n"
            "}\n";
        m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vsSource);
        m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fsSource);
    } else {
        static const char *vsSource =
            "#version 150 core\n"
            "in vec4 pos;\n"
            "in vec2 tc;\n"
            "out vec2 texcoord;\n"
            "void main() {\n"
            "   texcoord = tc;\n"
            "   gl_Position = pos;\n"
            "}\n";
        static const char *fsSource =
            "#version 150 core\n"
            "in vec2 texcoord;\n"
            "out vec4 fragColor;\n"
            "uniform sampler2D sampler;\n"
            "void main() {\n"
            "   fragColor = texture(sampler, texcoord);\n"
            "}\n";
        m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vsSource);
        m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fsSource);
    }

    m_program->bindAttributeLocation("pos", 0);
    m_program->bindAttributeLocation("tc", 1);

    if (!m_program->link()) {
        QByteArray logData(m_program->log().toLocal8Bit());
        const char *log = logData.data();
        qFatal("Failed to create shader program: %s", log);
    }

    m_vertices = new QOpenGLBuffer;
    m_vertices->create();
    m_vertices->bind();

    static const float vertices[] =
    {
        -1, -1, 0, 1, 0, 0,
        1, -1, 0, 1, 1, 0,
        1, 1, 0, 1, 1, 1,
        -1, -1, 0, 1, 0, 0,
        1, 1, 0, 1, 1, 1,
        -1, 1, 0, 1, 0, 1,
    };

    m_vertices->allocate(vertices, sizeof(vertices));
    m_vertices->release();
}