summaryrefslogtreecommitdiffstats
path: root/src/render/backend/renderer.h
blob: 48520eac75ea1813b64eb99faf4a9893d97835a8 (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
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QT3D_RENDER_RENDERER_H
#define QT3D_RENDER_RENDERER_H

#include <Qt3DRenderer/qtechnique.h>
#include <Qt3DRenderer/quniformvalue.h>
#include <Qt3DCore/qjob.h>
#include <Qt3DCore/qhandle.h>

#include <QHash>
#include <QMatrix4x4>
#include <QObject>
#include <QTimer>

#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QMutex>
#include <QWaitCondition>
#include <QAtomicInt>

QT_BEGIN_NAMESPACE

class QSurface;

namespace Qt3D {

class Camera;
class QEntity;
class QMaterial;
class ShaderProgram;
class QMesh;
class QRenderPass;
class QAbstractShapeMesh;
class RendererAspect;
class QFrameAllocator;

typedef QVector<QFrameAllocator *> QFrameAllocatorQueue;

namespace Render {

class RenderTextureProvider;
class RenderCamera;
class QGraphicsContext;
class FrameGraphNode;
class RenderMaterial;
class RenderTechnique;
class RenderShader;
class RenderEntity;
class MeshDataManager;
class MeshManager;
class RenderCommand;
class CameraManager;
class EntityManager;
class RenderQueues;
class RenderView;
class MaterialManager;
class MatrixManager;
class VAOManager;
class ShaderManager;
class TechniqueManager;
class EffectManager;
class RenderPassManager;
class RenderEffect;
class RenderRenderPass;
class TextureManager;
class TextureDataManager;
class LayerManager;
class LightManager;

typedef QHandle<RenderMaterial, 16> HMaterial;
typedef QHandle<RenderEffect, 16> HEffect;
typedef QHandle<RenderRenderPass, 16> HRenderPass;
typedef QHandle<RenderTechnique, 16> HTechnique;

class Renderer
{
public:
    explicit Renderer(int cachedFrames = 5);
    ~Renderer();

    void setRendererAspect(RendererAspect *rendererAspect) { m_rendererAspect = rendererAspect; }
    RendererAspect *rendererAspect() const { return m_rendererAspect; }

    void setFrameGraphRoot(Render::FrameGraphNode *fgRoot);
    Render::FrameGraphNode *frameGraphRoot() const;

    void setSceneGraphRoot(QEntity *sgRoot);
    QEntity *sceneGraphRoot() const;
    RenderEntity *renderSceneRoot() const { return m_renderSceneRoot; }

    void render();
    void doRender();

    QVector<QJobPtr> createRenderBinJobs();
    QJobPtr createRenderViewJob(FrameGraphNode *node, int submitOrderIndex);
    void executeCommands(const QVector<RenderCommand *> &commands);

    inline RenderQueues* renderQueues() const { return m_renderQueues; }
    inline MeshDataManager *meshDataManager() const { return m_meshDataManager; }
    inline MeshManager *meshManager() const { return m_meshManager; }
    inline CameraManager *cameraManager() const { return m_cameraManager; }
    inline EntityManager *renderNodesManager() const { return m_renderNodesManager; }
    inline MaterialManager *materialManager() const { return m_materialManager; }
    inline MatrixManager *worldMatrixManager() const { return m_worldMatrixManager; }
    inline MatrixManager *localMatrixManager() const { return m_localMatrixManager; }
    inline VAOManager *vaoManager() const { return m_vaoManager; }
    inline ShaderManager *shaderManager() const { return m_shaderManager; }
    inline TechniqueManager *techniqueManager() const { return m_techniqueManager; }
    inline EffectManager *effectManager() const { return m_effectManager; }
    inline RenderPassManager *renderPassManager() const { return m_renderPassManager; }
    inline TextureManager *textureManager() const { return m_textureManager; }
    inline TextureDataManager *textureDataManager() const { return m_textureDataManager; }
    inline LayerManager *layerManager() const { return m_layerManager; }
    inline LightManager *lightManager() const { return m_lightManager; }

    inline HMaterial defaultMaterialHandle() const { return m_defaultMaterialHandle; }
    inline HEffect defaultEffectHandle() const { return m_defaultEffectHandle; }
    inline HTechnique defaultTechniqueHandle() const { return m_defaultTechniqueHandle; }
    inline HRenderPass defaultRenderPassHandle() const { return m_defaultRenderPassHandle; }

    inline int cachedFramesCount() const { return m_cachedFramesCount; }

    void buildMeshes(QMesh *mesh, QMaterial *mat, const QMatrix4x4& mm);
    void setSurface(QSurface *s);

    void enqueueRenderView(RenderView *renderView, int submitOrder);
    void submitRenderViews();

    void initialize();

    QFrameAllocator *currentFrameAllocator(int frameIndex);

    QMutex* mutex() { return &m_mutex; }

private:
    RendererAspect *m_rendererAspect;

    // Frame graph root
    Render::FrameGraphNode *m_frameGraphRoot;

    QEntity *m_sceneGraphRoot;
    RenderEntity *m_renderSceneRoot;

    QHash<QMaterial*, RenderMaterial*> m_materialHash;
    QHash<QTechnique *, RenderTechnique*> m_techniqueHash;
    QHash<ShaderProgram*, RenderShader*> m_shaderHash;

    RenderTechnique *createTechnique(QTechnique *tech);
    RenderShader *getOrCreateShader(ShaderProgram *sp);
    RenderMaterial *getOrCreateMaterial(QMaterial *mat);

    QMaterial* m_defaultMaterial;
    QTechnique* m_defaultTechnique;

    HMaterial m_defaultMaterialHandle;
    HEffect m_defaultEffectHandle;
    HTechnique m_defaultTechniqueHandle;
    HRenderPass m_defaultRenderPassHandle;

    // Fail safe values that we can use if a RenderCommand
    // is missing a shader
    RenderShader *m_defaultRenderShader;
    DrawStateSet *m_defaultDrawStateSet;
    QHash<QString, QString> m_defaultParameterToGLSLAttributeNames;
    QUniformPack m_defaultUniformPack;

    QGraphicsContext* m_graphicsContext;
    QSurface *m_surface;
    RenderTextureProvider* m_textureProvider;
    MeshDataManager *m_meshDataManager;
    MeshManager *m_meshManager;
    CameraManager *m_cameraManager;
    EntityManager *m_renderNodesManager;
    MaterialManager *m_materialManager;
    MatrixManager *m_worldMatrixManager;
    MatrixManager *m_localMatrixManager;
    VAOManager *m_vaoManager;
    ShaderManager *m_shaderManager;
    TechniqueManager *m_techniqueManager;
    EffectManager *m_effectManager;
    RenderPassManager *m_renderPassManager;
    TextureManager *m_textureManager;
    TextureDataManager *m_textureDataManager;
    LayerManager *m_layerManager;
    LightManager *m_lightManager;

    QTimer* m_frameTimer;

    RenderQueues* m_renderQueues;

    void buildDefaultMaterial();
    void buildDefaultTechnique();

    QMutex m_mutex;
    QWaitCondition m_submitRenderViewsCondition;
    QWaitCondition m_waitForWindowToBeSetCondition;
    QWaitCondition m_waitForInitializationToBeCompleted;
    uint m_frameCount;
    int m_currentPreprocessingFrameIndex;

    QThreadStorage< QPair<int, QFrameAllocatorQueue *> > m_tlsAllocators;

    const int m_cachedFramesCount;
};

} // namespace Render
} // namespace Qt3D

QT_END_NAMESPACE

#endif // QT3D_RENDER_RENDERER_H