aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scenegraph/d3d12/qsgd3d12layer.cpp
blob: b9d3a180cf3d6277c333d50e903fc0b4a6488820 (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
/****************************************************************************
**
** Copyright (C) 2016 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:LGPL$
** 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 Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** 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-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qsgd3d12layer_p.h"
#include "qsgd3d12rendercontext_p.h"
#include "qsgd3d12engine_p.h"
#include "qsgd3d12renderer_p.h"

QT_BEGIN_NAMESPACE

// NOTE: Avoid categorized logging. It is slow.

#define DECLARE_DEBUG_VAR(variable) \
    static bool debug_ ## variable() \
    { static bool value = qgetenv("QSG_RENDERER_DEBUG").contains(QT_STRINGIFY(variable)); return value; }

DECLARE_DEBUG_VAR(render)

QSGD3D12Layer::QSGD3D12Layer(QSGD3D12RenderContext *rc)
    : QSGLayer(*(new QSGD3D12LayerPrivate)),
      m_rc(rc)
{
    if (Q_UNLIKELY(debug_render()))
        qDebug("new layer %p", this);
}

QSGD3D12Layer::~QSGD3D12Layer()
{
    if (Q_UNLIKELY(debug_render()))
        qDebug("destroying layer %p", this);

    cleanup();
}

// QSGTexture

int QSGD3D12Layer::textureId() const
{
    return m_rt; // not a texture id per se but will do
}

int QSGD3D12LayerPrivate::comparisonKey() const
{
    Q_Q(const QSGD3D12Layer);
    return q->m_rt;
}

QSize QSGD3D12Layer::textureSize() const
{
    return m_size;
}

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

bool QSGD3D12Layer::hasMipmaps() const
{
    // mipmapped layers are not supported for now
    return false;
}

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

void QSGD3D12Layer::bind()
{
    if (Q_UNLIKELY(debug_render()))
        qDebug("layer %p bind rt=%u", this, m_rt);

    QSGD3D12Engine *engine = m_rc->engine();
    Q_ASSERT(m_rt);

#ifndef QT_NO_DEBUG
    // Should not use the color buffer as a texture while it is the current render target.
    if (!m_recursive && engine->activeRenderTarget() == m_rt && engine->windowSamples() == 1)
        qWarning("ShaderEffectSource: \'recursive\' must be set to true when rendering recursively.");
#endif

    engine->useRenderTargetAsTexture(m_rt);
}

// QSGDynamicTexture

bool QSGD3D12Layer::updateTexture()
{
    if (Q_UNLIKELY(debug_render()))
        qDebug("layer %p updateTexture", this);

    const bool doUpdate = (m_live || m_updateContentPending) && m_dirtyTexture;

    if (doUpdate)
        updateContent();

    if (m_updateContentPending) {
        m_updateContentPending = false;
        emit scheduledUpdateCompleted();
    }

    return doUpdate;
}

// QSGLayer

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

    if (m_live && !item)
        resetRenderTarget();

    m_item = item;
    markDirtyTexture();
}

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

    m_rect = rect;
    markDirtyTexture();
}

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

    if (m_live && size.isNull())
        resetRenderTarget();

    m_size = size;
    markDirtyTexture();
}

void QSGD3D12Layer::scheduleUpdate()
{
    if (m_updateContentPending)
        return;

    if (Q_UNLIKELY(debug_render()))
        qDebug("layer %p scheduleUpdate", this);

    m_updateContentPending = true;

    if (m_dirtyTexture)
        emit updateRequested();
}

QImage QSGD3D12Layer::toImage() const
{
    return m_rc->engine()->executeAndWaitReadbackRenderTarget(m_rt);
}

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

    if (live && (!m_item || m_size.isNull()))
        resetRenderTarget();

    m_live = live;
    markDirtyTexture();
}

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

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

void QSGD3D12Layer::setHasMipmaps(bool mipmap)
{
    // mipmapped layers are not supported for now
    Q_UNUSED(mipmap);
}

void QSGD3D12Layer::setDevicePixelRatio(qreal ratio)
{
    m_dpr = ratio;
}

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

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

void QSGD3D12Layer::markDirtyTexture()
{
    if (Q_UNLIKELY(debug_render()))
        qDebug("layer %p markDirtyTexture", this);

    m_dirtyTexture = true;

    if (m_live || m_updateContentPending)
        emit updateRequested();
}

void QSGD3D12Layer::invalidated()
{
    cleanup();
}

void QSGD3D12Layer::cleanup()
{
    if (!m_renderer && !m_rt)
        return;

    if (Q_UNLIKELY(debug_render()))
        qDebug("layer %p cleanup renderer=%p rt=%u", this, m_renderer, m_rt);

    delete m_renderer;
    m_renderer = nullptr;

    resetRenderTarget();
}

void QSGD3D12Layer::resetRenderTarget()
{
    if (!m_rt)
        return;

    if (Q_UNLIKELY(debug_render()))
        qDebug("layer %p resetRenderTarget rt=%u", this, m_rt);

    m_rc->engine()->releaseRenderTarget(m_rt);
    m_rt = 0;

    if (m_secondaryRT) {
        m_rc->engine()->releaseRenderTarget(m_secondaryRT);
        m_secondaryRT = 0;
    }
}

void QSGD3D12Layer::updateContent()
{
    if (Q_UNLIKELY(debug_render()))
        qDebug("layer %p updateContent", this);

    if (!m_item || m_size.isNull()) {
        resetRenderTarget();
        m_dirtyTexture = false;
        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_rc->createRenderer();
        static_cast<QSGD3D12Renderer *>(m_renderer)->turnToLayerRenderer();
        connect(m_renderer, &QSGRenderer::sceneGraphChanged, this, &QSGD3D12Layer::markDirtyTexture);
    }

    m_renderer->setDevicePixelRatio(m_dpr);
    m_renderer->setRootNode(static_cast<QSGRootNode *>(root));

    QSGD3D12Engine *engine = m_rc->engine();
    const uint sampleCount = engine->windowSamples();
    const QVector4D clearColor;

    if (!m_rt || m_rtSize != m_size) {
        if (m_rt)
            resetRenderTarget();

        m_rt = engine->genRenderTarget();
        m_rtSize = m_size;

        if (Q_UNLIKELY(debug_render()))
            qDebug("new render target for layer %p, size=%dx%d, samples=%d",
                   this, m_size.width(), m_size.height(), sampleCount);

        engine->createRenderTarget(m_rt, m_rtSize, clearColor, sampleCount);

        // For multisampling the resolving via an extra non-ms color buffer is
        // handled internally in the engine, no need to worry about it here.
    }

    if (m_recursive && !m_secondaryRT && sampleCount == 1) {
        m_secondaryRT = engine->genRenderTarget();
        engine->createRenderTarget(m_secondaryRT, m_rtSize, clearColor, sampleCount);
    }

    m_dirtyTexture = false;

    m_renderer->setDeviceRect(m_size);
    m_renderer->setViewportRect(m_size);

    // Note that the handling of vertical mirroring differs from OpenGL here
    // due to y running top-bottom with D3D as opposed to bottom-top with GL.
    // The common parts of Quick follow OpenGL so vertical mirroring is
    // typically enabled.
    QRectF mirrored(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());

    m_renderer->setProjectionMatrixToRect(mirrored);
    m_renderer->setClearColor(Qt::transparent);

    if (!m_recursive || sampleCount > 1) {
        m_renderer->renderScene(m_rt);
    } else {
        m_renderer->renderScene(m_secondaryRT);
        qSwap(m_rt, m_secondaryRT);
    }

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

QT_END_NAMESPACE