File: testextension.cpp

package info (click to toggle)
qt6-quick3d 6.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 143,588 kB
  • sloc: cpp: 395,989; ansic: 41,469; xml: 288; sh: 242; makefile: 32
file content (187 lines) | stat: -rw-r--r-- 4,798 bytes parent folder | download | duplicates (3)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "testextension.h"
#include <ssg/qssgrenderextensions.h>
#include <ssg/qssgrenderhelpers.h>
#include <QThread>
#include <QTest>

int extensionCtorCount = 0;
int extensionDtorCount = 0;
int extensionFunctional = 0;
int childExtensionCtorCount = 0;
int childExtensionDtorCount = 0;
int childExtensionFunctional = 0;

class ChildExtension : public QQuick3DRenderExtension
{
public:
    ChildExtension(QQuick3DObject *parent = nullptr)
        : QQuick3DRenderExtension(parent)
    {
    }

    QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) override;
};

class ChildRenderer : public QSSGRenderExtension
{
public:
    ChildRenderer();
    ~ChildRenderer();

    bool prepareData(QSSGFrameData &data) override;
    void prepareRender(QSSGFrameData &data) override;
    void render(QSSGFrameData &data) override;
    void resetForFrame() override;
    RenderMode mode() const override { return RenderMode::Main; }
    RenderStage stage() const override { return RenderStage::PostColor; }

    QThread *t;
};

QSSGRenderGraphObject *ChildExtension::updateSpatialNode(QSSGRenderGraphObject *node)
{
    if (!node)
        node = new ChildRenderer;

    return node;
}

ChildRenderer::ChildRenderer()
{
    t = QThread::currentThread();
    childExtensionCtorCount += 1;
}

ChildRenderer::~ChildRenderer()
{
    // There is no ordering guarantee when it comes to the destruction of nodes.
    // The Renderer (the other extension backend node) may or may not be alive at this point.

    // with the threaded render loop it is important that the dtor is called on the same thread as the ctor
    QCOMPARE(QThread::currentThread(), t);

    childExtensionDtorCount += 1;
}

bool ChildRenderer::prepareData(QSSGFrameData &data)
{
    auto camera = data.activeCamera();
    if (camera == QSSGCameraId::Invalid)
        qWarning("No camera in prepareData");

    QMatrix4x4 vp = QSSGCameraHelpers::getViewProjectionMatrix(camera);
    if (!vp.isIdentity())
        childExtensionFunctional += 1;
    else
        qWarning("Camera's view-projection matrix does not look valid.");

    return true;
}

void ChildRenderer::prepareRender(QSSGFrameData &)
{
    QVERIFY(hasGraphicsResources()); // should be set implicitly

    childExtensionFunctional += 1;
}

void ChildRenderer::render(QSSGFrameData &)
{
    childExtensionFunctional += 1;
}

void ChildRenderer::resetForFrame()
{
}

class Renderer : public QSSGRenderExtension
{
public:
    Renderer();
    ~Renderer();

    bool prepareData(QSSGFrameData &data) override;
    void prepareRender(QSSGFrameData &data) override;
    void render(QSSGFrameData &data) override;
    void resetForFrame() override;
    RenderMode mode() const override { return RenderMode::Standalone; }
    RenderStage stage() const override { return RenderStage::PostColor; }

    QThread *t;
};

MyExtension::MyExtension(QQuick3DObject *parent)
    : QQuick3DRenderExtension(parent)
{
    new ChildExtension(this);
}

QSSGRenderGraphObject *MyExtension::updateSpatialNode(QSSGRenderGraphObject *node)
{
    if (!node)
        node = new Renderer;

    return node;
}

void MyExtension::markDirty(Dirty v)
{
    m_dirtyFlag |= v;
    update();
}

Renderer::Renderer()
{
    t = QThread::currentThread();
    extensionCtorCount += 1;
}

Renderer::~Renderer()
{
    // There is no ordering guarantee when it comes to the destruction of nodes.
    // The ChildRenderer (the other extension backend node) may or may not be alive at this point.

    // with the threaded render loop it is important that the dtor is called on the same thread as the ctor
    // NOTE: We only want to check that the address is the same (the object might have been destroyed already)...
    QVERIFY(QThread::currentThread() == t);

    extensionDtorCount += 1;
}

bool Renderer::prepareData(QSSGFrameData &data)
{
    auto camera = data.activeCamera();
    if (camera == QSSGCameraId::Invalid)
        qWarning("No camera in prepareData");
    else if (childExtensionFunctional == 0) // the order is undefined for siblings, and bottom-up for parent-child relationships
        qWarning("Child did not run before parent's prepareData");
    else
        extensionFunctional += 1;

    return true;
}

void Renderer::prepareRender(QSSGFrameData &)
{
    QVERIFY(hasGraphicsResources());

    if (childExtensionFunctional == 0)
        qWarning("Child did not run before parent's prepareRender");
    else
        extensionFunctional += 1;
}

void Renderer::render(QSSGFrameData &)
{
    if (childExtensionFunctional == 0)
        qWarning("Child did not run before parent's render");
    else
        extensionFunctional += 1;
}

void Renderer::resetForFrame()
{
}