summaryrefslogtreecommitdiffstats
path: root/src/runtime/dragon/dragonrenderer_p.h
blob: c0408196f1690f5178160747d46be1bebba4f0ec (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
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) 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.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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef DRAGONRENDERER_P_H
#define DRAGONRENDERER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of other Qt classes.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

// Mostly needed for the types like LoadedBuffers
#include <private/dragonbufferjobs_p.h>
#include <private/dragonrenderviewjobs_p.h>
#include <private/dragontexturejobs_p.h>

//
#include <private/dragonglbuffer_p.h>
#include <private/dragonglshader_p.h>
#include <private/dragonoffscreensurfacehelper_p.h>
#include <private/dragonopenglvertexarrayobject_p.h>
#include <private/dragontexture_p.h>

#include <QAtomicInt>
#include <QElapsedTimer>
#include <QMutex>
#include <QQueue>
#include <QScopedPointer>
#include <QSemaphore>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

namespace Render {
// TODO SubmissionContext needs to be replaced because it expects to access the Renderer...
// While we're here: how much of it is needed?
class SubmissionContext;

// Consider the need for a shader cache with the new jobs - is it persistent across application
// runs?
class ShaderCache;

// TODO does this belong in this namespace?
class LoadedTexture;

// TODO do we need the VSyncFrameAdvanceService?
class VSyncFrameAdvanceService;

// TODO do we need the offscreen surface?
} // namespace Render

namespace Dragon {

// renderer.h
struct GLTexture;
class Renderer;
class GraphicsHelperInterface;
class RenderThread : public QThread
{
    Q_OBJECT
public:
    explicit RenderThread(Renderer *renderer);
    void waitForStart(Priority priority = InheritPriority);

protected:
    void run() override;

private:
    Renderer *m_renderer = nullptr;
    QSemaphore m_semaphore;
};

template<typename T>
struct ExplicitChangeWrapper
{
    std::unique_ptr<T> operator->() {}
    std::unique_ptr<T> m_data;
};

struct FrameInput
{
    RenderViews renderViews;
    ValueContainer<CameraMatrices> renderViewCameraMatrices;
    RenderCommands renderCommands;
    LoadedTextures loadedTextures;
    LoadedBuffers loadedBuffers;
    ValueContainer<Attribute> attributes;
    ValueContainer<Parameter> parameters;
    ValueContainer<Matrix4x4> worldTransforms;
    ValueContainer<Shader> shaders;
    TreeInfo frameGraph;
};

// TODO move to separate file
struct GLRenderTarget
{
    GLuint frameBufferObjectId = 0;
    QSize size;
};

class Renderer
{
public:
    /*!
     * \brief The Frame struct holds data that is shared across frames,
     * such as already uploaded textures and active textures.
     */
    struct Frame
    {
        int maxTextureImageUnits;
        // TODO consider adding OpenGLState here
        MutableContainer<GLTexture> uploadedTextures;
        MutableContainer<GLShader> uploadedShaders;
        MutableContainer<GLBuffer> uploadedBuffers;
        QHash<VAOIdentifier, Immutable<GLVertexArrayObject>> uploadedVaos;
        // TODO could be ValueContainer?
        QHash<Qt3DCore::QNodeId, GLRenderTarget> createdRenderTargets;
        int number = 0;
    };

    // TODO a cleaner option is to move all thread-specific code into a separate class
    // instead of letting one class handle both cases
    enum RenderType {
        Synchronous,
        Threaded
    };

    Renderer(RenderType renderType);
    ~Renderer();

    void render();
    Frame doRender(Frame previousFrame);

    // Data from the aspect jobs are set and gotten with these
    void addLatestData(FrameInput data);
    GraphicsApiFilterData contextInfo(RenderViews views);

    void initialize(QOpenGLContext *context = nullptr);

    QSemaphore nextFrameSemaphore;

    QOpenGLContext *openGLContext();

private:
    // Render thread
    QScopedPointer<RenderThread> m_renderThread;
    QAtomicInt m_running;

    // Render views
    QMutex m_latestDataMutex;

    // Context
    //    QScopedPointer<Render::SubmissionContext> m_submissionContext;
    QMutex m_shareContextMutex;
    bool m_ownedContext = false;

    // TODO Protect behind a read-only barrier with a mutex, similar to RenderQueue
    QQueue<FrameInput> m_latestData;

    // TODO any way to reduce the number of members on this class?
    QHash<QSurface *, QSharedPointer<Dragon::GraphicsHelperInterface>> m_glHelpers;

    // Context information
    QOpenGLContext *m_glContext = nullptr;
    QOpenGLContext *m_shareContext = nullptr;

    bool m_contextInfoRequested = false;
    RenderViews m_requestedContextInfoViews;
    QSemaphore m_writeContextInfoSemaphore;
    QSemaphore m_readContextInfoSemaphore;
    GraphicsApiFilterData m_contextInfo;

    QSemaphore m_waitForInitializationToBeCompleted;

    Immutable<RenderStateSet> m_defaultRenderStateSet;

    QElapsedTimer m_elapsedTimer;

    OffscreenSurfaceHelper *m_offscreenHelper;

    RenderType m_renderType;

    //    // Shaders
    //    Render::ShaderCache *m_shaderCache = nullptr;

    //    // Other
    //    QScopedPointer<Render::VSyncFrameAdvanceService> m_vsyncFrameAdvanceService;
};

} // namespace Dragon
} // namespace Qt3DRender
QT_END_NAMESPACE

#endif // DRAGONRENDERER_P_H