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_GLTEXTURE_H |
41 | #define QT3DRENDER_RENDER_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 | |
65 | QT_BEGIN_NAMESPACE |
66 | |
67 | class QOpenGLTexture; |
68 | |
69 | namespace Qt3DRender { |
70 | namespace Render { |
71 | |
72 | class TextureImageManager; |
73 | class TextureDataManager; |
74 | class TextureImageDataManager; |
75 | class RenderBuffer; |
76 | |
77 | /** |
78 | * @brief |
79 | * Actual implementation of the OpenGL texture object. Makes sure the |
80 | * QOpenGLTexture is up-to-date with the generators, properties and parameters |
81 | * that were set for this GLTexture. |
82 | * |
83 | * Can be shared among multiple QTexture backend nodes through the |
84 | * GLTextureManager, which will make sure that there are no two GLTextures |
85 | * sharing the same texture data. |
86 | * |
87 | * A GLTexture can be unique though. In that case, it will not be shared |
88 | * between QTextures, but private to one QTexture only. |
89 | * |
90 | * A GLTexture can also represent an OpenGL renderbuffer object. This is used |
91 | * only in certain special cases, mainly to provide a packed depth-stencil |
92 | * renderbuffer suitable as an FBO attachment with OpenGL ES 3.1 and earlier. |
93 | * Such a GLTexture will have no texture object under the hood, and therefore |
94 | * the only valid operation is getOrCreateRenderBuffer(). |
95 | */ |
96 | class Q_AUTOTEST_EXPORT GLTexture |
97 | { |
98 | public: |
99 | GLTexture(); |
100 | ~GLTexture(); |
101 | |
102 | enum DirtyFlag { |
103 | None = 0, |
104 | TextureData = (1 << 0), // texture data needs uploading to GPU |
105 | Properties = (1 << 1), // texture needs to be (re-)created |
106 | Parameters = (1 << 2), // texture parameters need to be (re-)set |
107 | SharedTextureId = (1 << 3), // texture id from shared context |
108 | TextureImageData = (1 << 4) // texture image data needs uploading |
109 | }; |
110 | |
111 | /** |
112 | * Helper class to hold the defining properties of TextureImages |
113 | */ |
114 | struct Image { |
115 | QTextureImageDataGeneratorPtr generator; |
116 | int layer; |
117 | int mipLevel; |
118 | QAbstractTexture::CubeMapFace face; |
119 | |
120 | inline bool operator==(const Image &o) const { |
121 | bool sameGenerators = (generator == o.generator) |
122 | || (!generator.isNull() && !o.generator.isNull() && *generator == *o.generator); |
123 | return sameGenerators && layer == o.layer && mipLevel == o.mipLevel && face == o.face; |
124 | } |
125 | inline bool operator!=(const Image &o) const { return !(*this == o); } |
126 | }; |
127 | |
128 | inline TextureProperties properties() const { return m_properties; } |
129 | inline TextureParameters parameters() const { return m_parameters; } |
130 | inline QTextureGeneratorPtr textureGenerator() const { return m_dataFunctor; } |
131 | inline int sharedTextureId() const { return m_sharedTextureId; } |
132 | inline QVector<Image> images() const { return m_images; } |
133 | |
134 | inline QSize size() const { return QSize(m_properties.width, m_properties.height); } |
135 | inline QOpenGLTexture *getGLTexture() const { return m_gl; } |
136 | |
137 | /** |
138 | * @brief |
139 | * Returns the QOpenGLTexture for this GLTexture. If necessary, |
140 | * the GL texture will be created from the TextureImageDatas associated |
141 | * with the texture and image functors. If no functors are provided, |
142 | * the texture will be created without images. |
143 | * |
144 | * If the texture properties or parameters have changed, these changes |
145 | * will be applied to the resulting OpenGL texture. |
146 | */ |
147 | struct TextureUpdateInfo |
148 | { |
149 | QOpenGLTexture *texture = nullptr; |
150 | bool wasUpdated = false; |
151 | TextureProperties properties; |
152 | }; |
153 | |
154 | TextureUpdateInfo createOrUpdateGLTexture(); |
155 | |
156 | /** |
157 | * @brief |
158 | * Returns the RenderBuffer for this GLTexture. If this is the first |
159 | * call, the OpenGL renderbuffer object will be created. |
160 | */ |
161 | RenderBuffer *getOrCreateRenderBuffer(); |
162 | |
163 | |
164 | void destroy(); |
165 | |
166 | void cleanup(); |
167 | |
168 | bool isDirty() const |
169 | { |
170 | return m_dirtyFlags != None; |
171 | } |
172 | |
173 | bool hasTextureData() const { return !m_textureData.isNull(); } |
174 | bool hasImagesData() const { return !m_imageData.isEmpty(); } |
175 | |
176 | QFlags<DirtyFlag> dirtyFlags() const { return m_dirtyFlags; } |
177 | |
178 | QMutex *externalRenderingLock() |
179 | { |
180 | return &m_externalRenderingMutex; |
181 | } |
182 | |
183 | void setExternalRenderingEnabled(bool enable) |
184 | { |
185 | m_externalRendering = enable; |
186 | } |
187 | |
188 | bool isExternalRenderingEnabled() const |
189 | { |
190 | return m_externalRendering; |
191 | } |
192 | |
193 | // Purely for unit testing purposes |
194 | bool wasTextureRecreated() const |
195 | { |
196 | return m_wasTextureRecreated; |
197 | } |
198 | |
199 | void setParameters(const TextureParameters ¶ms); |
200 | void setProperties(const TextureProperties &props); |
201 | void setImages(const QVector<Image> &images); |
202 | void setGenerator(const QTextureGeneratorPtr &generator); |
203 | void setSharedTextureId(int textureId); |
204 | void addTextureDataUpdates(const QVector<QTextureDataUpdate> &updates); |
205 | |
206 | QVector<QTextureDataUpdate> textureDataUpdates() const { return m_pendingTextureDataUpdates; } |
207 | QTextureGeneratorPtr dataGenerator() const { return m_dataFunctor; } |
208 | |
209 | private: |
210 | void requestImageUpload() |
211 | { |
212 | m_dirtyFlags |= TextureImageData; |
213 | } |
214 | |
215 | void requestUpload() |
216 | { |
217 | m_dirtyFlags |= TextureData; |
218 | } |
219 | |
220 | bool testDirtyFlag(DirtyFlag flag) |
221 | { |
222 | return m_dirtyFlags.testFlag(flag); |
223 | } |
224 | |
225 | void setDirtyFlag(DirtyFlag flag, bool value = true) |
226 | { |
227 | m_dirtyFlags.setFlag(flag, value); |
228 | } |
229 | |
230 | QOpenGLTexture *buildGLTexture(); |
231 | bool loadTextureDataFromGenerator(); |
232 | void loadTextureDataFromImages(); |
233 | void uploadGLTextureData(); |
234 | void updateGLTextureParameters(); |
235 | void introspectPropertiesFromSharedTextureId(); |
236 | void destroyResources(); |
237 | |
238 | QFlags<DirtyFlag> m_dirtyFlags; |
239 | QMutex m_externalRenderingMutex; |
240 | QOpenGLTexture *m_gl; |
241 | RenderBuffer *m_renderBuffer; |
242 | |
243 | // target which is actually used for GL texture |
244 | TextureProperties m_properties; |
245 | TextureParameters m_parameters; |
246 | |
247 | QTextureGeneratorPtr m_dataFunctor; |
248 | QTextureGenerator *m_pendingDataFunctor; |
249 | QVector<Image> m_images; |
250 | |
251 | // cache actual image data generated by the functors |
252 | QTextureDataPtr m_textureData; |
253 | QVector<QTextureImageDataPtr> m_imageData; |
254 | QVector<QTextureDataUpdate> m_pendingTextureDataUpdates; |
255 | |
256 | int m_sharedTextureId; |
257 | bool m_externalRendering; |
258 | bool m_wasTextureRecreated; |
259 | }; |
260 | |
261 | } // namespace Render |
262 | } // namespace Qt3DRender |
263 | |
264 | QT_END_NAMESPACE |
265 | |
266 | #endif // QT3DRENDER_RENDER_GLTEXTURE_H |
267 | |