1// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QT3DRENDER_RENDER_TEXTURE_H
5#define QT3DRENDER_RENDER_TEXTURE_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <Qt3DRender/private/backendnode_p.h>
19#include <Qt3DRender/private/handle_types_p.h>
20#include <Qt3DRender/private/qabstracttexture_p.h>
21#include <Qt3DRender/private/qtexturegenerator_p.h>
22#include <Qt3DRender/qtexture.h>
23#include <Qt3DRender/qtexturedata.h>
24#include <QOpenGLContext>
25#include <QMutex>
26
27QT_BEGIN_NAMESPACE
28
29class QOpenGLTexture;
30
31namespace Qt3DRender {
32
33class QAbstractTexture;
34
35namespace Render {
36
37class TextureManager;
38class TextureImageManager;
39
40/**
41 * General, constant properties of a texture
42 */
43struct TextureProperties
44{
45 int width = 1;
46 int height = 1;
47 int depth = 1;
48 int layers = 1;
49 int mipLevels = 1;
50 int samples = 1;
51 QAbstractTexture::Target target = QAbstractTexture::TargetAutomatic;
52 QAbstractTexture::TextureFormat format = QAbstractTexture::NoFormat;
53 bool generateMipMaps = false;
54 QAbstractTexture::Status status = QAbstractTexture::None;
55
56 bool operator==(const TextureProperties &o) const {
57 return (width == o.width) && (height == o.height) && (depth == o.depth)
58 && (layers == o.layers) && (mipLevels == o.mipLevels) && (target == o.target)
59 && (format == o.format) && (generateMipMaps == o.generateMipMaps)
60 && (samples == o.samples) && (status == o.status);
61 }
62 inline bool operator!=(const TextureProperties &o) const { return !(*this == o); }
63};
64
65
66/**
67 * Texture parameters that are independent of texture data and that may
68 * change without the re-uploading the texture
69 */
70struct TextureParameters
71{
72 QAbstractTexture::Filter magnificationFilter = QAbstractTexture::Nearest;
73 QAbstractTexture::Filter minificationFilter = QAbstractTexture::Nearest;
74 QTextureWrapMode::WrapMode wrapModeX = QTextureWrapMode::ClampToEdge;
75 QTextureWrapMode::WrapMode wrapModeY = QTextureWrapMode::ClampToEdge;
76 QTextureWrapMode::WrapMode wrapModeZ = QTextureWrapMode::ClampToEdge;
77 float maximumAnisotropy = 1.0f;
78 QAbstractTexture::ComparisonFunction comparisonFunction = QAbstractTexture::CompareLessEqual;
79 QAbstractTexture::ComparisonMode comparisonMode = QAbstractTexture::CompareNone;
80
81 bool operator==(const TextureParameters &o) const {
82 return (magnificationFilter == o.magnificationFilter) && (minificationFilter == o.minificationFilter)
83 && (wrapModeX == o.wrapModeX) && (wrapModeY == o.wrapModeY) && (wrapModeZ == o.wrapModeZ)
84 && (maximumAnisotropy == o.maximumAnisotropy)
85 && (comparisonFunction == o.comparisonFunction) && (comparisonMode == o.comparisonMode);
86 }
87 inline bool operator!=(const TextureParameters &o) const { return !(*this == o); }
88};
89
90/**
91 * Backend object for QAbstractTexture. Just holds texture properties and parameters.
92 * Will query the TextureImplManager for an instance of TextureImpl that matches it's
93 * properties.
94 */
95class Q_3DRENDERSHARED_PRIVATE_EXPORT Texture : public BackendNode
96{
97public:
98 Texture();
99 ~Texture();
100
101 enum DirtyFlag {
102 NotDirty = 0,
103 DirtyProperties = (1 << 0),
104 DirtyParameters = (1 << 1),
105 DirtyImageGenerators = (1 << 2),
106 DirtyDataGenerator = (1 << 3),
107 DirtySharedTextureId = (1 << 4),
108 DirtyPendingDataUpdates = (1 << 5),
109 };
110 Q_DECLARE_FLAGS(DirtyFlags, DirtyFlag)
111
112 struct TextureUpdateInfo
113 {
114 TextureProperties properties;
115 QVariant handle;
116 QAbstractTexture::HandleType handleType;
117 };
118
119 void addDirtyFlag(DirtyFlags flags);
120 DirtyFlags dirtyFlags();
121 void unsetDirty();
122
123 void cleanup();
124
125 void addTextureDataUpdate(const QTextureDataUpdate &update);
126 std::vector<QTextureDataUpdate> takePendingTextureDataUpdates() {
127 return std::exchange(obj&: m_pendingTextureDataUpdates, new_val: {}); }
128
129 void syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) override;
130
131 inline const TextureProperties& properties() const { return m_properties; }
132 inline const TextureParameters& parameters() const { return m_parameters; }
133 inline const Qt3DCore::QNodeIdVector textureImageIds() const { return m_textureImageIds; }
134 inline const QTextureGeneratorPtr& dataGenerator() const { return m_dataFunctor; }
135 inline int sharedTextureId() const { return m_sharedTextureId; }
136
137 void setDataGenerator(const QTextureGeneratorPtr &generator);
138 bool isValid(TextureImageManager *manager) const;
139private:
140 DirtyFlags m_dirty;
141 TextureProperties m_properties;
142 TextureParameters m_parameters;
143 int m_sharedTextureId;
144
145 QTextureGeneratorPtr m_dataFunctor;
146 Qt3DCore::QNodeIdVector m_textureImageIds;
147
148 QMutex m_flagsMutex;
149 std::vector<QTextureDataUpdate> m_pendingTextureDataUpdates;
150};
151
152class Q_AUTOTEST_EXPORT TextureFunctor : public Qt3DCore::QBackendNodeMapper
153{
154public:
155 explicit TextureFunctor(AbstractRenderer *renderer,
156 TextureManager *textureNodeManager);
157 Qt3DCore::QBackendNode *create(Qt3DCore::QNodeId id) const final;
158 Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const final;
159 void destroy(Qt3DCore::QNodeId id) const final;
160
161private:
162 AbstractRenderer *m_renderer;
163 TextureManager *m_textureNodeManager;
164};
165
166#ifndef QT_NO_DEBUG_STREAM
167inline QDebug operator<<(QDebug dbg, const Texture &texture)
168{
169 QDebugStateSaver saver(dbg);
170 dbg << "QNodeId =" << texture.peerId() << "imageCount =" << texture.textureImageIds().size() << Qt::endl;
171 return dbg;
172}
173#endif
174
175} // namespace Render
176} // namespace Qt3DRender
177
178QT_END_NAMESPACE
179
180Q_DECLARE_METATYPE(Qt3DRender::Render::Texture*) // LCOV_EXCL_LINE
181Q_DECLARE_METATYPE(Qt3DRender::Render::TextureProperties) // LCOV_EXCL_LINE
182
183#endif // QT3DRENDER_RENDER_TEXTURE_H
184

source code of qt3d/src/render/texture/texture_p.h