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