1// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QT3DRENDER_SCENE3DRENDERER_P_H
5#define QT3DRENDER_SCENE3DRENDERER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/QObject>
19#include <QtCore/qsize.h>
20#include <QtCore/QMutex>
21#include <QtCore/QSemaphore>
22#include <scene3ditem_p.h>
23
24QT_BEGIN_NAMESPACE
25
26class QRhi;
27class QRhiTextureRenderTarget;
28class QRhiRenderBuffer;
29class QRhiTexture;
30class QRhiRenderPassDescriptor;
31class QQuickWindow;
32class QSGTexture;
33class QOpenGLFramebufferObject;
34
35namespace Qt3DCore {
36class QAspectEngine;
37}
38
39namespace Qt3DRender {
40
41class QRenderAspect;
42class Scene3DCleaner;
43class Scene3DSGNode;
44
45namespace Render {
46class AbstractRenderer;
47}
48
49class Scene3DRenderer : public QObject
50{
51 Q_OBJECT
52public:
53 Scene3DRenderer();
54 ~Scene3DRenderer();
55
56 Scene3DSGNode *sgNode() const;
57 void setCleanerHelper(Scene3DCleaner *cleaner);
58 void allowRender();
59 void setCompositingMode(Scene3DItem::CompositingMode mode);
60 void setSkipFrame(bool skip);
61 void init(Scene3DItem *item, Qt3DCore::QAspectEngine *aspectEngine, QRenderAspect *renderAspect);
62
63 void setMultisample(bool multisample);
64 void setBoundingSize(const QSize &size);
65
66 bool multisample() const { return m_multisample; }
67 QSize boundingSize() const { return m_boundingRectSize; }
68 bool isYUp() const;
69
70 void init(Qt3DCore::QAspectEngine *aspectEngine, QRenderAspect *renderAspect);
71
72 void beforeSynchronize();
73 void setWindow(QQuickWindow *window);
74
75 bool hasShutdown() const { return !m_needsShutdown; }
76
77 QRenderAspect *renderAspect() const { return m_renderAspect; }
78public Q_SLOTS:
79 void shutdown();
80
81private:
82
83 class QuickRenderer
84 {
85 public:
86 // ctor/dtor cannot be inlined because of QScopedPointer with forward
87 // declared class
88 QuickRenderer();
89 virtual ~QuickRenderer();
90 virtual void initialize(Scene3DRenderer *scene3DRenderer,
91 Qt3DRender::Render::AbstractRenderer *renderer) = 0;
92 virtual void beforeSynchronize(Scene3DRenderer *scene3DRenderer) = 0;
93 virtual void beforeRendering(Scene3DRenderer *scene3DRenderer) = 0;
94 virtual void beforeRenderPassRecording(Scene3DRenderer *scene3DRenderer) = 0;
95 virtual void shutdown(Scene3DRenderer *sceneRenderer) = 0;
96 virtual bool isYUp() const = 0;
97
98 protected:
99 bool m_lastMultisample = false;
100 QSize m_lastSize;
101 QScopedPointer<QSGTexture> m_texture;
102 Qt3DRender::Render::AbstractRenderer *m_renderer = nullptr;
103 };
104
105 class GLRenderer : public QuickRenderer
106 {
107 public:
108 QOpenGLFramebufferObject *createMultisampledFramebufferObject(const QSize &size);
109 QOpenGLFramebufferObject *createFramebufferObject(const QSize &size);
110
111 void initialize(Scene3DRenderer *scene3DRenderer,
112 Qt3DRender::Render::AbstractRenderer *renderer) override;
113 void beforeSynchronize(Scene3DRenderer *scene3DRenderer) override;
114 void beforeRendering(Scene3DRenderer *scene3DRenderer) override;
115 void beforeRenderPassRecording(Scene3DRenderer *scene3DRenderer) override;
116 void shutdown(Scene3DRenderer *sceneRenderer) override;
117 bool isYUp() const override { return true; };
118
119 private:
120 QScopedPointer<QOpenGLFramebufferObject> m_multisampledFBO;
121 QScopedPointer<QOpenGLFramebufferObject> m_finalFBO;
122 bool m_multisample = false;
123 quint32 m_textureId = 0;
124 };
125
126 class RHIRenderer : public QuickRenderer
127 {
128 public:
129 void initialize(Scene3DRenderer *scene3DRenderer,
130 Qt3DRender::Render::AbstractRenderer *renderer) override;
131 void beforeSynchronize(Scene3DRenderer *scene3DRenderer) override;
132 void beforeRendering(Scene3DRenderer *scene3DRenderer) override;
133 void beforeRenderPassRecording(Scene3DRenderer *scene3DRenderer) override;
134 void shutdown(Scene3DRenderer *sceneRenderer) override;
135 bool isYUp() const override;
136
137 private:
138 void releaseRHIResources();
139
140 QRhiTexture *m_rhiTexture = nullptr;
141 QRhiRenderBuffer *m_rhiColorRenderBuffer = nullptr;
142 QRhiRenderBuffer *m_rhiMSAARenderBuffer = nullptr;
143 QRhiRenderBuffer *m_rhiDepthRenderBuffer = nullptr;
144 QRhiTextureRenderTarget *m_rhiRenderTarget = nullptr;
145 QRhiRenderPassDescriptor *m_rhiRenderTargetPassDescriptor = nullptr;
146 QRhi *m_rhi = nullptr;
147 };
148
149 Qt3DCore::QAspectEngine *m_aspectEngine; // Will be released by the Scene3DItem
150 QRenderAspect *m_renderAspect; // Will be released by the aspectEngine
151 Scene3DSGNode *m_node; // Will be released by the QtQuick SceneGraph
152 QQuickWindow *m_window;
153 QMutex m_windowMutex;
154
155 QSize m_lastSize;
156 QSize m_boundingRectSize;
157 bool m_multisample;
158 bool m_lastMultisample;
159
160 bool m_needsShutdown;
161 bool m_shouldRender;
162 bool m_dirtyViews;
163 bool m_skipFrame;
164 QSemaphore m_allowRendering;
165 Scene3DItem::CompositingMode m_compositingMode;
166 bool m_resetRequested = false;
167 QuickRenderer *m_quickRenderer = nullptr;
168
169 friend class Scene3DItem;
170};
171
172} // namespace Qt3DRender
173
174QT_END_NAMESPACE
175
176#endif // QT3DRENDER_SCENE3DRENDERER_P_H
177

source code of qt3d/src/quick3d/imports/scene3d/scene3drenderer_p.h