summaryrefslogtreecommitdiffstats
path: root/src/render/Qt3DSRenderFrameBuffer.h
blob: 1882a27db90a55f1819076c71ad4642e7a0ca65c (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/****************************************************************************
**
** 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_RENDER_QT3DS_RENDER_FRAME_BUFFER_H
#define QT3DS_RENDER_QT3DS_RENDER_FRAME_BUFFER_H
#include "foundation/Qt3DSRefCounted.h"
#include "foundation/Qt3DSAssert.h"
#include "foundation/Qt3DSAtomic.h"
#include "render/Qt3DSRenderBaseTypes.h"
#include "render/backends/Qt3DSRenderBackend.h"

namespace qt3ds {
namespace render {

    class NVRenderContextImpl;
    class NVRenderTexture2D;
    class NVRenderRenderBuffer;
    class NVRenderTexture2DArray;
    class NVRenderTextureCube;

    class NVRenderTextureOrRenderBuffer
    {
        NVRenderTexture2D *m_Texture2D;
        NVRenderTexture2DArray *m_Texture2DArray;
        NVRenderTextureCube *m_TextureCube;
        NVRenderRenderBuffer *m_RenderBuffer;

    public:
        NVRenderTextureOrRenderBuffer(NVRenderTexture2D &texture)
            : m_Texture2D(&texture)
            , m_Texture2DArray(NULL)
            , m_TextureCube(NULL)
            , m_RenderBuffer(NULL)
        {
        }
        NVRenderTextureOrRenderBuffer(NVRenderRenderBuffer &render)
            : m_Texture2D(NULL)
            , m_Texture2DArray(NULL)
            , m_TextureCube(NULL)
            , m_RenderBuffer(&render)
        {
        }
        NVRenderTextureOrRenderBuffer(NVRenderTexture2DArray &textureArray)
            : m_Texture2D(NULL)
            , m_Texture2DArray(&textureArray)
            , m_TextureCube(NULL)
            , m_RenderBuffer(NULL)
        {
        }
        NVRenderTextureOrRenderBuffer(NVRenderTextureCube &textureCube)
            : m_Texture2D(NULL)
            , m_Texture2DArray(NULL)
            , m_TextureCube(&textureCube)
            , m_RenderBuffer(NULL)
        {
        }
        NVRenderTextureOrRenderBuffer()
            : m_Texture2D(NULL)
            , m_Texture2DArray(NULL)
            , m_TextureCube(NULL)
            , m_RenderBuffer(NULL)
        {
        }
        NVRenderTextureOrRenderBuffer(const NVRenderTextureOrRenderBuffer &other)
            : m_Texture2D(other.m_Texture2D)
            , m_Texture2DArray(other.m_Texture2DArray)
            , m_TextureCube(other.m_TextureCube)
            , m_RenderBuffer(other.m_RenderBuffer)
        {
        }
        NVRenderTextureOrRenderBuffer &operator=(const NVRenderTextureOrRenderBuffer &other)
        {
            if (this != &other) {
                m_Texture2D = const_cast<NVRenderTexture2D *>(other.m_Texture2D);
                m_Texture2DArray = const_cast<NVRenderTexture2DArray *>(other.m_Texture2DArray);
                m_RenderBuffer = const_cast<NVRenderRenderBuffer *>(other.m_RenderBuffer);
                m_TextureCube = const_cast<NVRenderTextureCube *>(other.m_TextureCube);
            }
            return *this;
        }

        bool HasTexture2D() const { return m_Texture2D != NULL; }
        bool HasTexture2DArray() const { return m_Texture2DArray != NULL; }
        bool HasTextureCube() const { return m_TextureCube != NULL; }
        bool HasRenderBuffer() const { return m_RenderBuffer != NULL; }

        NVRenderTexture2D *GetTexture2D() const
        {
            QT3DS_ASSERT(HasTexture2D());
            return m_Texture2D;
        }
        NVRenderTexture2DArray *GetTexture2DArray() const
        {
            QT3DS_ASSERT(HasTexture2DArray());
            return m_Texture2DArray;
        }
        NVRenderTextureCube *GetTextureCube() const
        {
            QT3DS_ASSERT(HasTextureCube());
            return m_TextureCube;
        }
        NVRenderRenderBuffer *GetRenderBuffer() const
        {
            QT3DS_ASSERT(HasRenderBuffer());
            return m_RenderBuffer;
        }
    };

    class NVRenderFrameBuffer : public NVRefCounted, public NVRenderImplemented
    {
    private:
        NVRenderContextImpl &m_Context; ///< pointer to context
        NVFoundationBase &m_Foundation; ///< pointer to foundation
        volatile QT3DSI32 mRefCount; ///< Using foundations' naming convention to ease implementation
        NVRenderBackend *m_Backend; ///< pointer to backend

        NVRenderTextureOrRenderBuffer
            m_Attachments[NVRenderFrameBufferAttachments::LastAttachment]; ///< attachments array
        NVRenderBackend::NVRenderBackendRenderTargetObject
            m_BufferHandle; ///< opaque backend handle

    public:
        /**
         * @brief constructor
         *
         * @param[in] context		Pointer to context
         * @param[in] fnd			Pointer to foundation
         *
         * @return No return.
         */
        NVRenderFrameBuffer(NVRenderContextImpl &context, NVFoundationBase &fnd);

        /// destructor
        virtual ~NVRenderFrameBuffer();

        // define refcount functions
        QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE_OVERRIDE(m_Foundation)

        /**
         * @brief query attachment
         *
         *
         * @return buffer format
         */
        virtual NVRenderTextureOrRenderBuffer
        GetAttachment(NVRenderFrameBufferAttachments::Enum attachment);

        /**
         * @brief Attach a render or texture buffer to a render target
         *		  For texture attachments we use always level 0
         *
         * @param[in] attachment		Attachment point (e.g. COLOR0, DEPTH...)
         * @param[in] buffer			Contains a pointer to the attachment
         * @param[in] target			Attachment texture target
         *
         * @return no return
         */
        virtual void
        Attach(NVRenderFrameBufferAttachments::Enum attachment,
               NVRenderTextureOrRenderBuffer buffer,
               NVRenderTextureTargetType::Enum target = NVRenderTextureTargetType::Texture2D);

        /**
         * @brief Attach a particular layer of the texture 2D array to a render target
         *
         * @param[in] attachment		Attachment point (e.g. COLOR0, DEPTH...)
         * @param[in] buffer			Pointer to the Texture Array which contains the
         * layers
         * @param[in] layer				The index to the layer that will be attached to the
         * target
         * @param[in] level				Mip level of the texture that will be attached
         * (default 0)
         *
         * @return no return
         */
        virtual void AttachLayer(NVRenderFrameBufferAttachments::Enum attachment,
                                 NVRenderTextureOrRenderBuffer buffer, QT3DSI32 layer,
                                 QT3DSI32 level = 0);

        /**
         * @brief Attach a particular face of the texture cubemap to a render target
         *
         * @param[in] attachment		Attachment point (e.g. COLOR0, DEPTH...)
         * @param[in] buffer			Pointer to the Texture Array which contains the
         * layers
         * @param[in] face				The face of the cubemap that will be attached to the
         * target
         * @param[in] level				Mip level of the texture that will be attached
         * (default 0)
         *
         * @return no return
         */
        virtual void AttachFace(NVRenderFrameBufferAttachments::Enum attachment,
                                NVRenderTextureOrRenderBuffer buffer,
                                NVRenderTextureCubeFaces::Enum face);

        /**
         * @brief Check that this framebuffer is complete and can be rendered to.
         *
         *
         * @return true if complete
         */
        virtual bool IsComplete();

        /**
         * @brief query if framebuffer has any attachment
         *
         * @return true if any attachment
         */
        virtual bool HasAnyAttachment() { return (m_AttachmentBits != 0); }

        /**
         * @brief get the backend object handle
         *
         * @return the backend object handle.
         */
        virtual NVRenderBackend::NVRenderBackendRenderTargetObject GetFrameBuffertHandle()
        {
            return m_BufferHandle;
        }

        // this will be obsolete
        const void *GetImplementationHandle() const override
        {
            return reinterpret_cast<const void *>(m_BufferHandle);
        }

        /**
         * @brief static creator function
         *
         * @param[in] context		Pointer to context
         *
         * @return a pointer to framebuffer object.
         */
        static NVRenderFrameBuffer *Create(NVRenderContextImpl &context);

    private:
        /**
         * @brief releaes an attached object
         *
         * @return which target we released
         */
        NVRenderTextureTargetType::Enum releaseAttachment(NVRenderFrameBufferAttachments::Enum idx);

        QT3DSU32 m_AttachmentBits; ///< holds flags for current attached buffers
    };
}
}

#endif