aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgrhilayer.cpp
blob: 701b539f4f82507e6fc6a20189f2437ecac76212 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qsgrhilayer_p.h"

#include <private/qqmlglobal_p.h>
#include <private/qsgrenderer_p.h>
#include <private/qsgdefaultrendercontext_p.h>

QSGRhiLayer::QSGRhiLayer(QSGRenderContext *context)
    : QSGLayer(*(new QSGRhiLayerPrivate))
    , m_mipmap(false)
    , m_live(true)
    , m_recursive(false)
    , m_dirtyTexture(true)
    , m_multisampling(false)
    , m_grab(false)
    , m_mirrorHorizontal(false)
    , m_mirrorVertical(true)
{
    m_context = static_cast<QSGDefaultRenderContext *>(context);
    m_rhi = m_context->rhi();
    Q_ASSERT(m_rhi);
}

QSGRhiLayer::~QSGRhiLayer()
{
    invalidated();
}

void QSGRhiLayer::invalidated()
{
    releaseResources();

    delete m_renderer;
    m_renderer = nullptr;
}

int QSGRhiLayerPrivate::comparisonKey() const
{
    Q_Q(const QSGRhiLayer);
    return int(qintptr(q->m_texture));
}

bool QSGRhiLayer::hasAlphaChannel() const
{
    return true;
}

bool QSGRhiLayer::hasMipmaps() const
{
    return m_mipmap;
}

int QSGRhiLayer::textureId() const
{
    Q_ASSERT_X(false, "QSGRhiLayer::textureId()", "Not implemented for RHI");
    return 0;
}

void QSGRhiLayer::bind()
{
    Q_ASSERT_X(false, "QSGRhiLayer::bind()", "Not implemented for RHI");
}

QRhiTexture *QSGRhiLayerPrivate::rhiTexture() const
{
    Q_Q(const QSGRhiLayer);
    return q->m_texture;
}

void QSGRhiLayerPrivate::updateRhiTexture(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
{
    Q_UNUSED(rhi);
    Q_UNUSED(resourceUpdates);
}

bool QSGRhiLayer::updateTexture()
{
    // called during the preprocess phase, outside of frame rendering -> good

    bool doGrab = (m_live || m_grab) && m_dirtyTexture;
    if (doGrab)
        grab();

    if (m_grab)
        emit scheduledUpdateCompleted();

    m_grab = false;
    return doGrab;
}

void QSGRhiLayer::setHasMipmaps(bool mipmap)
{
    if (mipmap == m_mipmap)
        return;

    m_mipmap = mipmap;
    if (m_mipmap && m_texture)
        markDirtyTexture();
}


void QSGRhiLayer::setItem(QSGNode *item)
{
    if (item == m_item)
        return;

    m_item = item;

    if (m_live && !m_item)
        releaseResources();

    markDirtyTexture();
}

void QSGRhiLayer::setRect(const QRectF &rect)
{
    if (rect == m_rect)
        return;

    m_rect = rect;
    markDirtyTexture();
}

void QSGRhiLayer::setSize(const QSize &size)
{
    if (size == m_size)
        return;

    m_size = size;

    if (m_live && m_size.isNull())
        releaseResources();

    markDirtyTexture();
}

void QSGRhiLayer::setFormat(uint format)
{
    Q_UNUSED(format);
}

void QSGRhiLayer::setLive(bool live)
{
    if (live == m_live)
        return;

    m_live = live;

    if (m_live && (!m_item || m_size.isNull()))
        releaseResources();

    markDirtyTexture();
}

void QSGRhiLayer::scheduleUpdate()
{
    if (m_grab)
        return;

    m_grab = true;
    if (m_dirtyTexture)
        emit updateRequested();
}

void QSGRhiLayer::setRecursive(bool recursive)
{
    m_recursive = recursive;
}

void QSGRhiLayer::setMirrorHorizontal(bool mirror)
{
    m_mirrorHorizontal = mirror;
}

void QSGRhiLayer::setMirrorVertical(bool mirror)
{
    m_mirrorVertical = mirror;
}

void QSGRhiLayer::markDirtyTexture()
{
    m_dirtyTexture = true;
    if (m_live || m_grab)
        emit updateRequested();
}

void QSGRhiLayer::releaseResources()
{
    delete m_rt;
    m_rt = nullptr;

    delete m_rtRp;
    m_rtRp = nullptr;

    delete m_ds;
    m_ds = nullptr;

    delete m_msaaColorBuffer;
    m_msaaColorBuffer = nullptr;

    delete m_texture;
    m_texture = nullptr;

    delete m_secondaryTexture;
    m_secondaryTexture = nullptr;
}

void QSGRhiLayer::grab()
{
    if (!m_item || m_size.isNull()) {
        releaseResources();
        m_dirtyTexture = false;
        return;
    }

    int effectiveSamples = m_samples;
    // if no layer.samples was provided use the window's msaa setting
    if (effectiveSamples <= 1)
        effectiveSamples = m_context->msaaSampleCount();

    const bool needsNewRt = !m_rt || m_rt->pixelSize() != m_size || (m_recursive && !m_secondaryTexture);
    const bool mipmapSettingChanged = m_texture && m_texture->flags().testFlag(QRhiTexture::MipMapped) != m_mipmap;
    const bool msaaSettingChanged = (effectiveSamples > 1 && !m_msaaColorBuffer) || (effectiveSamples <= 1 && m_msaaColorBuffer);

    if (needsNewRt ||mipmapSettingChanged || msaaSettingChanged) {
        if (effectiveSamples <= 1) {
            m_multisampling = false;
        } else {
            m_multisampling = m_rhi->isFeatureSupported(QRhi::MultisampleRenderBuffer);
            if (!m_multisampling)
                qWarning("Layer requested %d samples but multisample renderbuffers are not supported", effectiveSamples);
        }

        QRhiTexture::Flags textureFlags = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource;
        if (m_mipmap)
            textureFlags |= QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips;

        if (m_multisampling) {
            releaseResources();
            m_msaaColorBuffer = m_rhi->newRenderBuffer(QRhiRenderBuffer::Color, m_size, effectiveSamples);
            if (!m_msaaColorBuffer->build()) {
                qWarning("Failed to build multisample color buffer for layer of size %dx%d, sample count %d",
                         m_size.width(), m_size.height(), effectiveSamples);
                releaseResources();
                return;
            }
            m_texture = m_rhi->newTexture(m_format, m_size, 1, textureFlags);
            if (!m_texture->build()) {
                qWarning("Failed to build texture for layer of size %dx%d", m_size.width(), m_size.height());
                releaseResources();
                return;
            }
            m_ds = m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, m_size, effectiveSamples);
            if (!m_ds->build()) {
                qWarning("Failed to build depth-stencil buffer for layer");
                releaseResources();
                return;
            }
            QRhiTextureRenderTargetDescription desc;
            QRhiColorAttachment color0(m_msaaColorBuffer);
            color0.setResolveTexture(m_texture);
            desc.setColorAttachments({ color0 });
            desc.setDepthStencilBuffer(m_ds);
            m_rt = m_rhi->newTextureRenderTarget(desc);
            m_rtRp = m_rt->newCompatibleRenderPassDescriptor();
            if (!m_rtRp) {
                qWarning("Failed to build render pass descriptor for layer");
                releaseResources();
                return;
            }
            m_rt->setRenderPassDescriptor(m_rtRp);
            if (!m_rt->build()) {
                qWarning("Failed to build texture render target for layer");
                releaseResources();
                return;
            }
        } else {
            releaseResources();
            m_texture = m_rhi->newTexture(m_format, m_size, 1, textureFlags);
            if (!m_texture->build()) {
                qWarning("Failed to build texture for layer of size %dx%d", m_size.width(), m_size.height());
                releaseResources();
                return;
            }
            m_ds = m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, m_size);
            if (!m_ds->build()) {
                qWarning("Failed to build depth-stencil buffer for layer");
                releaseResources();
                return;
            }
            QRhiColorAttachment color0(m_texture);
            if (m_recursive) {
                // Here rt is associated with m_secondaryTexture instead of m_texture.
                // We will issue a copy to m_texture afterwards.
                m_secondaryTexture = m_rhi->newTexture(m_format, m_size, 1, textureFlags);
                if (!m_secondaryTexture->build()) {
                    qWarning("Failed to build texture for layer of size %dx%d", m_size.width(), m_size.height());
                    releaseResources();
                    return;
                }
                color0.setTexture(m_secondaryTexture);
            }
            m_rt = m_rhi->newTextureRenderTarget({ color0, m_ds });
            m_rtRp = m_rt->newCompatibleRenderPassDescriptor();
            if (!m_rtRp) {
                qWarning("Failed to build render pass descriptor for layer");
                releaseResources();
                return;
            }
            m_rt->setRenderPassDescriptor(m_rtRp);
            if (!m_rt->build()) {
                qWarning("Failed to build texture render target for layer");
                releaseResources();
                return;
            }
        }
    }

    QSGNode *root = m_item;
    while (root->firstChild() && root->type() != QSGNode::RootNodeType)
        root = root->firstChild();
    if (root->type() != QSGNode::RootNodeType)
        return;

    if (!m_renderer) {
        m_renderer = m_context->createRenderer();
        connect(m_renderer, SIGNAL(sceneGraphChanged()), this, SLOT(markDirtyTexture()));
    }
    m_renderer->setRootNode(static_cast<QSGRootNode *>(root));
    root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update.
    m_renderer->nodeChanged(root, QSGNode::DirtyForceUpdate); // Force render list update.

    // This must not be moved. The flag must be reset only after the
    // nodeChanged otherwise we end up with constantly updating even when the
    // layer contents do not change.
    m_dirtyTexture = false;

    m_renderer->setDevicePixelRatio(m_dpr);
    m_renderer->setDeviceRect(m_size);
    m_renderer->setViewportRect(m_size);
    QRectF mirrored;
    if (m_rhi->isYUpInFramebuffer()) {
        mirrored = QRectF(m_mirrorHorizontal ? m_rect.right() : m_rect.left(),
                          m_mirrorVertical ? m_rect.bottom() : m_rect.top(),
                          m_mirrorHorizontal ? -m_rect.width() : m_rect.width(),
                          m_mirrorVertical ? -m_rect.height() : m_rect.height());
    } else {
        mirrored = QRectF(m_mirrorHorizontal ? m_rect.right() : m_rect.left(),
                          m_mirrorVertical ? m_rect.top() : m_rect.bottom(),
                          m_mirrorHorizontal ? -m_rect.width() : m_rect.width(),
                          m_mirrorVertical ? m_rect.height() : -m_rect.height());
    }
    QSGAbstractRenderer::MatrixTransformFlags matrixFlags;
    if (!m_rhi->isYUpInNDC())
        matrixFlags |= QSGAbstractRenderer::MatrixTransformFlipY;
    m_renderer->setProjectionMatrixToRect(mirrored, matrixFlags);
    m_renderer->setClearColor(Qt::transparent);
    m_renderer->setRenderTarget(m_rt);
    m_renderer->setCommandBuffer(m_context->currentFrameCommandBuffer());
    m_renderer->setRenderPassDescriptor(m_rtRp);

    QRhiResourceUpdateBatch *resourceUpdates = nullptr;

    // render with our own "sub-renderer" (this will just add a render pass to the command buffer)
    if (m_multisampling) {
        m_context->renderNextRhiFrame(m_renderer);
    } else {
        if (m_recursive) {
            m_context->renderNextRhiFrame(m_renderer);
            if (!resourceUpdates)
                resourceUpdates = m_rhi->nextResourceUpdateBatch();
            resourceUpdates->copyTexture(m_texture, m_secondaryTexture);
        } else {
            m_context->renderNextRhiFrame(m_renderer);
        }
    }

    if (m_mipmap) {
        if (!resourceUpdates)
            resourceUpdates = m_rhi->nextResourceUpdateBatch();
        // going to be expensive - if done every frame - but the user asked for it...
        resourceUpdates->generateMips(m_texture);
    }

    // Do not defer committing the resource updates to the main pass - with
    // multiple layers there can be dependencies, so the texture should be
    // usable once we return.
    m_context->currentFrameCommandBuffer()->resourceUpdate(resourceUpdates);

    root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip, opacity and render list update.

    if (m_recursive)
        markDirtyTexture(); // Continuously update if 'live' and 'recursive'.
}

QImage QSGRhiLayer::toImage() const
{
    if (!m_texture)
        return QImage();

    QRhiCommandBuffer *cb = m_context->currentFrameCommandBuffer();
    QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch();
    QRhiReadbackResult result;
    QRhiReadbackDescription readbackDesc(m_texture);
    resourceUpdates->readBackTexture(readbackDesc, &result);

    cb->resourceUpdate(resourceUpdates);

    // Inefficient but what can you do. We need the results right away. This is
    // not something that occurs in a normal rendering process anyway. (used by
    // QQuickItem's grabToImage).
    m_rhi->finish();

    if (result.data.isEmpty()) {
        qWarning("Layer grab failed");
        return QImage();
    }

    // There is no room for negotiation here, the texture is RGBA8, and the
    // readback happens with GL_RGBA on GL, so RGBA8888 is the only option.
    // Also, Quick is always premultiplied alpha.
    const QImage::Format imageFormat = QImage::Format_RGBA8888_Premultiplied;

    const uchar *p = reinterpret_cast<const uchar *>(result.data.constData());
    return QImage(p, result.pixelSize.width(), result.pixelSize.height(), imageFormat).mirrored();
}

QRectF QSGRhiLayer::normalizedTextureSubRect() const
{
    return QRectF(m_mirrorHorizontal ? 1 : 0,
                  m_mirrorVertical ? 0 : 1,
                  m_mirrorHorizontal ? -1 : 1,
                  m_mirrorVertical ? 1 : -1);
}

#include "moc_qsgrhilayer_p.cpp"