summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/Qt3DSRuntimeRender/Include/Qt3DSOffscreenRenderManager.h
blob: 57dcf4302b4b5a7c97df9b06d93af03a39966d22 (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
253
254
255
256
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://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$
**
****************************************************************************/
#pragma once
#ifndef QT3DS_OFFSCREEN_RENDER_MANAGER_H
#define QT3DS_OFFSCREEN_RENDER_MANAGER_H
#include "Qt3DSRender.h"
#include "foundation/Qt3DSSimpleTypes.h"
#include "foundation/Qt3DSRefCounted.h"
#include "foundation/StringTable.h"
#include "render/Qt3DSRenderBaseTypes.h"
#include "foundation/Qt3DSOption.h"
#include "Qt3DSRenderScene.h"
#include "Qt3DSRenderLayer.h"

namespace qt3ds {
namespace render {
    class IResourceManager;
    struct Qt3DSRenderPickResult;
    class IGraphObjectPickQuery;
    struct OffscreenRendererDepthValues
    {
        enum Enum {
            NoDepthBuffer = 0,
            Depth16, // 16 bit depth buffer
            Depth24, // 24 bit depth buffer
            Depth32, // 32 bit depth buffer
            Depth24Stencil8 // 24 bit depth buffer 8 bit stencil buffer
        };
    };

    struct SOffscreenRendererEnvironment
    {
        QT3DSU32 m_Width;
        QT3DSU32 m_Height;
        NVRenderTextureFormats::Enum m_Format;
        OffscreenRendererDepthValues::Enum m_Depth;
        bool m_Stencil;
        AAModeValues::Enum m_MSAAMode;

        SOffscreenRendererEnvironment()
            : m_Width(0)
            , m_Height(0)
            , m_Format(NVRenderTextureFormats::Unknown)
            , m_Depth(OffscreenRendererDepthValues::NoDepthBuffer)
            , m_Stencil(false)
            , m_MSAAMode(AAModeValues::NoAA)
        {
        }

        SOffscreenRendererEnvironment(QT3DSU32 inWidth, QT3DSU32 inHeight,
                                      NVRenderTextureFormats::Enum inFormat)
            : m_Width(inWidth)
            , m_Height(inHeight)
            , m_Format(inFormat)
            , m_Depth(OffscreenRendererDepthValues::Depth16)
            , m_Stencil(false)
            , m_MSAAMode(AAModeValues::NoAA)
        {
        }

        SOffscreenRendererEnvironment(QT3DSU32 inWidth, QT3DSU32 inHeight,
                                      NVRenderTextureFormats::Enum inFormat,
                                      OffscreenRendererDepthValues::Enum inDepth, bool inStencil,
                                      AAModeValues::Enum inAAMode)
            : m_Width(inWidth)
            , m_Height(inHeight)
            , m_Format(inFormat)
            , m_Depth(inDepth)
            , m_Stencil(inStencil)
            , m_MSAAMode(inAAMode)
        {
        }

        SOffscreenRendererEnvironment(const SOffscreenRendererEnvironment &inOther)
            : m_Width(inOther.m_Width)
            , m_Height(inOther.m_Height)
            , m_Format(inOther.m_Format)
            , m_Depth(inOther.m_Depth)
            , m_Stencil(inOther.m_Stencil)
            , m_MSAAMode(inOther.m_MSAAMode)
        {
        }
    };

    struct SOffscreenRenderFlags
    {
        bool m_HasTransparency;
        bool m_HasChangedSinceLastFrame;
        SOffscreenRenderFlags()
            : m_HasTransparency(false)
            , m_HasChangedSinceLastFrame(false)
        {
        }

        SOffscreenRenderFlags(bool transparency, bool hasChanged)
            : m_HasTransparency(transparency)
            , m_HasChangedSinceLastFrame(hasChanged)
        {
        }
    };

    typedef void *SRenderInstanceId;

    class IOffscreenRenderer : public NVRefCounted
    {
    public:
        class IOffscreenRendererCallback
        {
        public:
            virtual void onOffscreenRendererInitialized(const QString &id) = 0;
            virtual void onOffscreenRendererFrame(const QString &id) = 0;
        protected:
            virtual ~IOffscreenRendererCallback() {}
        };

    protected:
        virtual ~IOffscreenRenderer() {}
    public:
        virtual void addCallback(IOffscreenRendererCallback *cb) = 0;
        // Arbitrary const char* returned to indicate the type of this renderer
        // Can be overloaded to form the basis of an RTTI type system.
        // Not currently used by the rendering system.
        virtual CRegisteredString GetOffscreenRendererType() = 0;
        virtual SOffscreenRendererEnvironment
        GetDesiredEnvironment(QT3DSVec2 inPresentationScaleFactor) = 0;
        // Returns true of this object needs to be rendered, false if this object is not dirty
        virtual SOffscreenRenderFlags
        NeedsRender(const SOffscreenRendererEnvironment &inEnvironment,
                    QT3DSVec2 inPresentationScaleFactor,
                    const SRenderInstanceId instanceId) = 0;
        // Returns true if the rendered result image has transparency, or false
        // if it should be treated as a completely opaque image.
        // It is the IOffscreenRenderer's job to clear any buffers (color, depth, stencil) that it
        // needs to.  It should not assume that it's buffers are clear;
        // Sometimes we scale the width and height of the main presentation in order to fit a
        // window.
        // If we do so, the scale factor tells the subpresentation renderer how much the system has
        // scaled.
        virtual void Render(const SOffscreenRendererEnvironment &inEnvironment,
                            NVRenderContext &inRenderContext, QT3DSVec2 inPresentationScaleFactor,
                            SScene::RenderClearCommand inColorBufferNeedsClear,
                            const SRenderInstanceId instanceId) = 0;
        virtual void RenderWithClear(const SOffscreenRendererEnvironment &inEnvironment,
                            NVRenderContext &inRenderContext, QT3DSVec2 inPresentationScaleFactor,
                            SScene::RenderClearCommand inColorBufferNeedsClear,
                            QT3DSVec3 inclearColor,
                            const SRenderInstanceId instanceId) = 0;

        // Implementors should implement one of the two interfaces below.

        // If this renderer supports picking that can return graph objects
        // then return an interface here.
        virtual IGraphObjectPickQuery *GetGraphObjectPickQuery(const SRenderInstanceId instanceId) = 0;

        // If you *don't* support the GraphObjectPickIterator interface, then you should implement
        // this interface
        // The system will just ask you to pick.
        // If you return true, then we will assume that you swallowed the pick and will continue no
        // further.
        // else we will assume you did not and will continue the picking algorithm.
        virtual bool Pick(const QT3DSVec2 &inMouseCoords, const QT3DSVec2 &inViewportDimensions,
                          const SRenderInstanceId instanceId) = 0;
    };

    struct SOffscreenRenderResult
    {
        NVScopedRefCounted<IOffscreenRenderer> m_Renderer;
        NVRenderTexture2D *m_Texture;
        bool m_HasTransparency;
        bool m_HasChangedSinceLastFrame;

        SOffscreenRenderResult(IOffscreenRenderer &inRenderer, NVRenderTexture2D &inTexture,
                               bool inTrans, bool inDirty)
            : m_Renderer(&inRenderer)
            , m_Texture(&inTexture)
            , m_HasTransparency(inTrans)
            , m_HasChangedSinceLastFrame(inDirty)
        {
        }
        SOffscreenRenderResult()
            : m_Renderer(NULL)
            , m_Texture(NULL)
            , m_HasTransparency(false)
            , m_HasChangedSinceLastFrame(false)
        {
        }
    };

    struct SOffscreenRendererKey;

    /**
     *	The offscreen render manager attempts to satisfy requests for a given image under a given
     *key.
     *	Renderers are throttled such that they render at most once per frame and potentially less
     *than
     *	that if they don't require a new render.
     */
    class IOffscreenRenderManager : public NVRefCounted
    {
    protected:
        virtual ~IOffscreenRenderManager() {}
    public:
        // returns true if the renderer has not been registered.
        // No return value means there was an error registering this id.
        virtual Option<bool> MaybeRegisterOffscreenRenderer(const SOffscreenRendererKey &inKey,
                                                            IOffscreenRenderer &inRenderer) = 0;
        virtual void RegisterOffscreenRenderer(const SOffscreenRendererKey &inKey,
                                               IOffscreenRenderer &inRenderer) = 0;
        virtual bool HasOffscreenRenderer(const SOffscreenRendererKey &inKey) = 0;
        virtual IOffscreenRenderer *GetOffscreenRenderer(const SOffscreenRendererKey &inKey) = 0;
        virtual void ReleaseOffscreenRenderer(const SOffscreenRendererKey &inKey) = 0;

        // This doesn't trigger rendering right away.  A node is added to the render graph that
        // points to this item.
        // Thus rendering is deffered until the graph is run but we promise to render to this
        // resource.
        virtual SOffscreenRenderResult GetRenderedItem(const SOffscreenRendererKey &inKey) = 0;
        // Called by the UICRenderContext, clients don't need to call this.
        virtual void BeginFrame() = 0;
        virtual void EndFrame() = 0;

        static IOffscreenRenderManager &
        CreateOffscreenRenderManager(NVAllocatorCallback &inCallback, IStringTable &inStringTable,
                                     IResourceManager &inManager, IQt3DSRenderContext &inContext);
    };
}
}

#endif