1/****************************************************************************
2**
3** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QT3DRENDER_RENDER_OPENGL_GLTEXTURE_H
41#define QT3DRENDER_RENDER_OPENGL_GLTEXTURE_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of other Qt classes. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <Qt3DRender/qtexture.h>
55#include <Qt3DRender/qtextureimagedata.h>
56#include <Qt3DRender/qtexturegenerator.h>
57#include <Qt3DRender/private/backendnode_p.h>
58#include <Qt3DRender/private/handle_types_p.h>
59#include <Qt3DRender/private/texture_p.h>
60#include <QOpenGLContext>
61#include <QFlags>
62#include <QMutex>
63#include <QSize>
64
65QT_BEGIN_NAMESPACE
66
67class QOpenGLTexture;
68
69namespace Qt3DRender {
70namespace Render {
71
72template<class APITexture, class APITextureImage>
73class APITextureManager;
74
75class TextureImageManager;
76class TextureDataManager;
77class TextureImageDataManager;
78
79namespace OpenGL {
80class RenderBuffer;
81
82/**
83 * @brief
84 * Actual implementation of the OpenGL texture object. Makes sure the
85 * QOpenGLTexture is up-to-date with the generators, properties and parameters
86 * that were set for this GLTexture.
87 *
88 * Can be shared among multiple QTexture backend nodes through the
89 * GLTextureManager, which will make sure that there are no two GLTextures
90 * sharing the same texture data.
91 *
92 * A GLTexture can be unique though. In that case, it will not be shared
93 * between QTextures, but private to one QTexture only.
94 *
95 * A GLTexture can also represent an OpenGL renderbuffer object. This is used
96 * only in certain special cases, mainly to provide a packed depth-stencil
97 * renderbuffer suitable as an FBO attachment with OpenGL ES 3.1 and earlier.
98 * Such a GLTexture will have no texture object under the hood, and therefore
99 * the only valid operation is getOrCreateRenderBuffer().
100 */
101class Q_AUTOTEST_EXPORT GLTexture
102{
103public:
104 GLTexture();
105 ~GLTexture();
106
107 enum DirtyFlag {
108 None = 0,
109 TextureData = (1 << 0), // texture data needs uploading to GPU
110 Properties = (1 << 1), // texture needs to be (re-)created
111 Parameters = (1 << 2), // texture parameters need to be (re-)set
112 SharedTextureId = (1 << 3), // texture id from shared context
113 TextureImageData = (1 << 4) // texture image data needs uploading
114 };
115
116 /**
117 * Helper class to hold the defining properties of TextureImages
118 */
119 struct Image {
120 QTextureImageDataGeneratorPtr generator;
121 int layer;
122 int mipLevel;
123 QAbstractTexture::CubeMapFace face;
124
125 inline bool operator==(const Image &o) const {
126 bool sameGenerators = (generator == o.generator)
127 || (!generator.isNull() && !o.generator.isNull() && *generator == *o.generator);
128 return sameGenerators && layer == o.layer && mipLevel == o.mipLevel && face == o.face;
129 }
130 inline bool operator!=(const Image &o) const { return !(*this == o); }
131 };
132
133 inline TextureProperties properties() const { return m_properties; }
134 inline TextureParameters parameters() const { return m_parameters; }
135 inline QTextureGeneratorPtr textureGenerator() const { return m_dataFunctor; }
136 inline int sharedTextureId() const { return m_sharedTextureId; }
137 inline QVector<Image> images() const { return m_images; }
138
139 inline QSize size() const { return QSize(m_properties.width, m_properties.height); }
140 inline QOpenGLTexture *getGLTexture() const { return m_gl; }
141
142 /**
143 * @brief
144 * Returns the QOpenGLTexture for this GLTexture. If necessary,
145 * the GL texture will be created from the TextureImageDatas associated
146 * with the texture and image functors. If no functors are provided,
147 * the texture will be created without images.
148 *
149 * If the texture properties or parameters have changed, these changes
150 * will be applied to the resulting OpenGL texture.
151 */
152 struct TextureUpdateInfo
153 {
154 QOpenGLTexture *texture = nullptr;
155 bool wasUpdated = false;
156 TextureProperties properties;
157 };
158
159 TextureUpdateInfo createOrUpdateGLTexture();
160
161 /**
162 * @brief
163 * Returns the RenderBuffer for this GLTexture. If this is the first
164 * call, the OpenGL renderbuffer object will be created.
165 */
166 RenderBuffer *getOrCreateRenderBuffer();
167
168
169 void destroy();
170
171 void cleanup();
172
173 bool isDirty() const
174 {
175 return m_dirtyFlags != None;
176 }
177
178 bool hasTextureData() const { return !m_textureData.isNull(); }
179 bool hasImagesData() const { return !m_imageData.isEmpty(); }
180
181 QFlags<DirtyFlag> dirtyFlags() const { return m_dirtyFlags; }
182
183 QMutex *externalRenderingLock()
184 {
185 return &m_externalRenderingMutex;
186 }
187
188 void setExternalRenderingEnabled(bool enable)
189 {
190 m_externalRendering = enable;
191 }
192
193 bool isExternalRenderingEnabled() const
194 {
195 return m_externalRendering;
196 }
197
198 // Purely for unit testing purposes
199 bool wasTextureRecreated() const
200 {
201 return m_wasTextureRecreated;
202 }
203
204 void setParameters(const TextureParameters &params);
205 void setProperties(const TextureProperties &props);
206 void setImages(const QVector<Image> &images);
207 void setGenerator(const QTextureGeneratorPtr &generator);
208 void setSharedTextureId(int textureId);
209 void addTextureDataUpdates(const QVector<QTextureDataUpdate> &updates);
210
211 QVector<QTextureDataUpdate> textureDataUpdates() const { return m_pendingTextureDataUpdates; }
212 QTextureGeneratorPtr dataGenerator() const { return m_dataFunctor; }
213
214private:
215 void requestImageUpload()
216 {
217 m_dirtyFlags |= TextureImageData;
218 }
219
220 void requestUpload()
221 {
222 m_dirtyFlags |= TextureData;
223 }
224
225 bool testDirtyFlag(DirtyFlag flag)
226 {
227 return m_dirtyFlags.testFlag(flag);
228 }
229
230 void setDirtyFlag(DirtyFlag flag, bool value = true)
231 {
232 m_dirtyFlags.setFlag(flag, on: value);
233 }
234
235 QOpenGLTexture *buildGLTexture();
236 bool loadTextureDataFromGenerator();
237 void loadTextureDataFromImages();
238 void uploadGLTextureData();
239 void updateGLTextureParameters();
240 void introspectPropertiesFromSharedTextureId();
241 void destroyResources();
242
243 QFlags<DirtyFlag> m_dirtyFlags;
244 QMutex m_externalRenderingMutex;
245 QOpenGLTexture *m_gl;
246 RenderBuffer *m_renderBuffer;
247
248 // target which is actually used for GL texture
249 TextureProperties m_properties;
250 TextureParameters m_parameters;
251
252 QTextureGeneratorPtr m_dataFunctor;
253 QTextureGenerator *m_pendingDataFunctor;
254 QVector<Image> m_images;
255
256 // cache actual image data generated by the functors
257 QTextureDataPtr m_textureData;
258 QVector<QTextureImageDataPtr> m_imageData;
259 QVector<QTextureDataUpdate> m_pendingTextureDataUpdates;
260
261 int m_sharedTextureId;
262 bool m_externalRendering;
263 bool m_wasTextureRecreated;
264};
265
266} // namespace OpenGL
267} // namespace Render
268} // namespace Qt3DRender
269
270QT_END_NAMESPACE
271
272#endif // QT3DRENDER_RENDER_OPENGL_GLTEXTURE_H
273

source code of qt3d/src/plugins/renderers/opengl/textures/gltexture_p.h