1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Quick 3D.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #ifndef QSSGSCENERENDERER_H
31 #define QSSGSCENERENDERER_H
32 
33 //
34 //  W A R N I N G
35 //  -------------
36 //
37 // This file is not part of the Qt API.  It exists purely as an
38 // implementation detail.  This header file may change from version to
39 // version without notice, or even be removed.
40 //
41 // We mean it.
42 //
43 
44 #include <QtQuick3DRender/private/qssgrendercontext_p.h>
45 #include <QtQuick3DRuntimeRender/private/qssgrendercontextcore_p.h>
46 
47 #include <qsgtextureprovider.h>
48 #include <qsgrendernode.h>
49 #include <QSGSimpleTextureNode>
50 
51 #include <QtQuick3D/private/qquick3dviewport_p.h>
52 
53 QT_BEGIN_NAMESPACE
54 
55 
56 class QQuick3DSceneManager;
57 class QQuick3DViewport;
58 struct QSSGRenderLayer;
59 
60 class QQuick3DSceneRenderer
61 {
62 public:
63     struct FramebufferObject {
64         FramebufferObject(const QSize &s, const QSSGRef<QSSGRenderContext> &context,
65                           int msaaSamples = 1);
66         ~FramebufferObject();
67         QSize size;
68         QSSGRef<QSSGRenderContext> renderContext;
69         QSSGRef<QSSGRenderFrameBuffer> fbo;
70         QSSGRef<QSSGRenderTexture2D> color0;
71         QSSGRef<QSSGRenderTexture2D> depthStencil;
72         int samples;
73     };
74 
75     QQuick3DSceneRenderer(QWindow *window);
76     ~QQuick3DSceneRenderer();
77 protected:
78     GLuint render();
79     void render(const QRect &viewport, bool clearFirst = false);
80     void synchronize(QQuick3DViewport *item, const QSize &size, bool useFBO = true);
81     void update();
82     void invalidateFramebufferObject();
83     void onRenderModeChanged();
surfaceSize()84     QSize surfaceSize() const { return m_surfaceSize; }
85     QSSGRenderPickResult pick(const QPointF &pos);
86     QSSGRenderPickResult syncPick(const QPointF &pos);
87     QQuick3DRenderStats *renderStats();
88 
89 private:
90     void updateLayerNode(QQuick3DViewport *view3D);
91     void addNodeToLayer(QSSGRenderNode *node);
92     void removeNodeFromLayer(QSSGRenderNode *node);
93     void deleteResources();
94     QSSGRef<QSSGRenderContextInterface> m_sgContext;
95     QSharedPointer<QQuick3DSceneManager> m_sceneManager;
96     QSSGRenderLayer *m_layer = nullptr;
97     QSize m_surfaceSize;
98     void *data = nullptr;
99     bool m_layerSizeIsDirty = true;
100     bool m_aaIsDirty = true;
101     QWindow *m_window = nullptr;
102     FramebufferObject *m_antialiasingFbo = nullptr;
103     FramebufferObject *m_fbo = nullptr;
104     QQuick3DRenderStats *m_renderStats = nullptr;
105 
106     QSSGRenderNode *m_sceneRootNode = nullptr;
107     QSSGRenderNode *m_importRootNode = nullptr;
108 
109     float m_ssaaMultiplier = 1.5f;
110 
111     friend class SGFramebufferObjectNode;
112     friend class QQuick3DSGRenderNode;
113     friend class QQuick3DSGDirectRenderer;
114     friend class QQuick3DViewport;
115 };
116 
117 class QOpenGLVertexArrayObjectHelper;
118 
119 class SGFramebufferObjectNode final : public QSGTextureProvider, public QSGSimpleTextureNode
120 {
121     Q_OBJECT
122 
123 public:
124     SGFramebufferObjectNode();
125     ~SGFramebufferObjectNode() override;
126 
127     void scheduleRender();
128 
129     QSGTexture *texture() const override;
130 
131     void preprocess() override;
132 
133 public Q_SLOTS:
134     void render();
135 
136     void handleScreenChange();
137 
138 public:
139     QQuickWindow *window;
140     QQuick3DSceneRenderer *renderer;
141     QQuick3DViewport *quickFbo;
142 
143     bool renderPending;
144     bool invalidatePending;
145 
146     qreal devicePixelRatio;
147     int requestedFramesCount;
148 };
149 
150 class QQuick3DSGRenderNode final : public QSGRenderNode
151 {
152 public:
153 
154     StateFlags changedStates() const override;
155     void render(const RenderState *state) override;
156     void releaseResources() override;
157     RenderingFlags flags() const override;
158 public:
159     QQuickWindow *window = nullptr;
160     QQuick3DSceneRenderer *renderer = nullptr;
161 };
162 
163 class QQuick3DSGDirectRenderer : public QObject
164 {
165     Q_OBJECT
166 public:
167     enum QQuick3DSGDirectRendererMode {
168         Underlay,
169         Overlay
170     };
171     QQuick3DSGDirectRenderer(QQuick3DSceneRenderer *renderer, QQuickWindow *window, QQuick3DSGDirectRendererMode mode = Underlay);
172     ~QQuick3DSGDirectRenderer();
173 
renderer()174     QQuick3DSceneRenderer *renderer() { return m_renderer; }
onRenderModeChanged()175     void onRenderModeChanged() { m_renderer = nullptr; }
176     void setViewport(const QRectF &viewport);
177 
178     void requestRender();
179     void setVisibility(bool visible);
180 
181 private Q_SLOTS:
182     void render();
183 
184 private:
185     QQuick3DSceneRenderer *m_renderer = nullptr;
186     QQuickWindow *m_window = nullptr;
187     QQuick3DSGDirectRendererMode m_mode;
188     QRectF m_viewport;
189     bool m_isVisible = true;
190 };
191 
192 QT_END_NAMESPACE
193 
194 #endif // QSSGSCENERENDERER_H
195