diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2018-08-09 10:09:32 +0200 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2018-10-08 13:36:33 +0000 |
commit | 1f274113e413d9cc0b8d8e692315bbcbc0d3dec1 (patch) | |
tree | 6abe87c75418add6fd238932861a2b20b33f7995 /src/render/texture | |
parent | 321817950ebedd34d2b4f609526affa221c28877 (diff) |
Add QSharedGLTexture
QAbstractTexture subclass that will allow to make Qt3D
work with a texture id created by another GL engine on
a shared context.
[ChangeLog][Qt3DRender] Add QSharedGLTexture to allow
creating a Qt3D from an OpenGL texture id.
Change-Id: Ic5b19eae6ebc7aca7e6372d62217348f99db19df
Task-number: QTBUG-69918
Reviewed-by: Kevin Ottens <kevin.ottens@kdab.com>
Diffstat (limited to 'src/render/texture')
-rw-r--r-- | src/render/texture/qabstracttexture.cpp | 2 | ||||
-rw-r--r-- | src/render/texture/qabstracttexture_p.h | 3 | ||||
-rw-r--r-- | src/render/texture/qtexture.cpp | 63 | ||||
-rw-r--r-- | src/render/texture/qtexture.h | 17 | ||||
-rw-r--r-- | src/render/texture/texture.cpp | 5 | ||||
-rw-r--r-- | src/render/texture/texture_p.h | 5 |
6 files changed, 94 insertions, 1 deletions
diff --git a/src/render/texture/qabstracttexture.cpp b/src/render/texture/qabstracttexture.cpp index 03746620e..18ec3ccec 100644 --- a/src/render/texture/qabstracttexture.cpp +++ b/src/render/texture/qabstracttexture.cpp @@ -66,6 +66,7 @@ QAbstractTexturePrivate::QAbstractTexturePrivate() , m_comparisonMode(QAbstractTexture::CompareNone) , m_layers(1) , m_samples(1) + , m_sharedTextureId(-1) { } @@ -909,6 +910,7 @@ Qt3DCore::QNodeCreatedChangeBasePtr QAbstractTexture::createNodeCreationChange() data.layers = d->m_layers; data.samples = d->m_samples; data.dataFunctor = d->m_dataFunctor; + data.sharedTextureId = d->m_sharedTextureId; return creationChange; } diff --git a/src/render/texture/qabstracttexture_p.h b/src/render/texture/qabstracttexture_p.h index a27ae3729..21b0fd9d3 100644 --- a/src/render/texture/qabstracttexture_p.h +++ b/src/render/texture/qabstracttexture_p.h @@ -87,6 +87,8 @@ public : int m_layers; int m_samples; + int m_sharedTextureId; + QTextureGeneratorPtr dataFunctor() const; void setDataFunctor(const QTextureGeneratorPtr &generator); @@ -113,6 +115,7 @@ struct QAbstractTextureData Qt3DCore::QNodeIdVector textureImageIds; int layers; int samples; + int sharedTextureId; QTextureGeneratorPtr dataFunctor; }; diff --git a/src/render/texture/qtexture.cpp b/src/render/texture/qtexture.cpp index f7ee3f6df..110df30e3 100644 --- a/src/render/texture/qtexture.cpp +++ b/src/render/texture/qtexture.cpp @@ -1469,6 +1469,69 @@ bool QTextureFromSourceGenerator::isMirrored() const return m_mirrored; } +/*! + * \class QSharedGLTexture + * \brief Allows to use a textureId from a separate OpenGL context in a Qt 3D scene. + * + * Depending on the rendering mode used by Qt 3D, the shared context will either be: + * \list + * \li qt_gl_global_share_context when letting Qt 3D drive the rendering. When + * setting the attribute Qt::AA_ShareOpenGLContexts on the QApplication class, + * this will automatically make QOpenGLWidget instances have their context shared + * with qt_gl_global_share_context. + * \li the shared context from the QtQuick scene. You might have to subclass + * QWindow or use QtQuickRenderControl to have control over what that shared + * context is though as of 5.13 it is qt_gl_global_share_context. + * \endlist + * + * \since 5.13 + * + * Any 3rd party engine that shares its context with the Qt 3D renderer can now + * provide texture ids that will be referenced by the Qt 3D texture. + * + * You can omit specifying the texture properties, Qt 3D will try at runtime to + * determine what they are. If you know them, you can of course provide them, + * avoid additional work for Qt 3D. + * + * Keep in mind that if you are using custom materials and shaders, you need to + * specify the correct sampler type to be used. + */ + +QSharedGLTexture::QSharedGLTexture(Qt3DCore::QNode *parent) + : QAbstractTexture(parent) +{ + QAbstractTexturePrivate *d = static_cast<QAbstractTexturePrivate *>(Qt3DCore::QNodePrivate::get(this)); + d->m_target = TargetAutomatic; +} + +QSharedGLTexture::~QSharedGLTexture() +{ +} + +/*! + * \qmlproperty textureId + * + * The OpenGL texture id value that you want Qt3D to gain access to. + */ +/*! + *\property Qt3DRender::QSharedGLTexture::textureId + * + * The OpenGL texture id value that you want Qt3D to gain access to. + */ +int QSharedGLTexture::textureId() const +{ + return static_cast<QAbstractTexturePrivate *>(d_ptr.get())->m_sharedTextureId; +} + +void QSharedGLTexture::setTextureId(int id) +{ + QAbstractTexturePrivate *d = static_cast<QAbstractTexturePrivate *>(Qt3DCore::QNodePrivate::get(this)); + if (d->m_sharedTextureId != id) { + d->m_sharedTextureId = id; + emit textureIdChanged(id); + } +} + } // namespace Qt3DRender QT_END_NAMESPACE diff --git a/src/render/texture/qtexture.h b/src/render/texture/qtexture.h index 24d19fbcf..991725de2 100644 --- a/src/render/texture/qtexture.h +++ b/src/render/texture/qtexture.h @@ -169,6 +169,23 @@ private: Q_DECLARE_PRIVATE(QTextureLoader) }; +class QT3DRENDERSHARED_EXPORT QSharedGLTexture : public QAbstractTexture +{ + Q_OBJECT + Q_PROPERTY(int textureId READ textureId WRITE setTextureId NOTIFY textureIdChanged) +public: + explicit QSharedGLTexture(Qt3DCore::QNode *parent = nullptr); + ~QSharedGLTexture(); + + int textureId() const; + +public Q_SLOTS: + void setTextureId(int id); + +Q_SIGNALS: + void textureIdChanged(int textureId); +}; + } // namespace Qt3DRender QT_END_NAMESPACE diff --git a/src/render/texture/texture.cpp b/src/render/texture/texture.cpp index 749b85802..b87ea9c6d 100644 --- a/src/render/texture/texture.cpp +++ b/src/render/texture/texture.cpp @@ -115,6 +115,7 @@ void Texture::cleanup() // texture is being referenced by a shared API specific texture (GLTexture) m_dataFunctor.reset(); m_textureImageIds.clear(); + m_sharedTextureId = -1; // set default values m_properties = {}; @@ -181,6 +182,9 @@ void Texture::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) dirty = DirtyProperties; } else if (propertyChange->propertyName() == QByteArrayLiteral("generator")) { setDataGenerator(propertyChange->value().value<QTextureGeneratorPtr>()); + } else if (propertyChange->propertyName() == QByteArrayLiteral("textureId")) { + m_sharedTextureId = propertyChange->value().toInt(); + dirty = DirtySharedTextureId; } } break; @@ -315,6 +319,7 @@ void Texture::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &chan m_parameters.comparisonFunction = data.comparisonFunction; m_parameters.comparisonMode = data.comparisonMode; m_dataFunctor = data.dataFunctor; + m_sharedTextureId = data.sharedTextureId; for (const QNodeId imgId : data.textureImageIds) addTextureImage(imgId); diff --git a/src/render/texture/texture_p.h b/src/render/texture/texture_p.h index 9e385cefe..d48e35b0c 100644 --- a/src/render/texture/texture_p.h +++ b/src/render/texture/texture_p.h @@ -137,7 +137,8 @@ public: DirtyProperties = 0x1, DirtyParameters = 0x2, DirtyImageGenerators = 0x4, - DirtyDataGenerator = 0x8 + DirtyDataGenerator = 0x8, + DirtySharedTextureId = 0x16 }; Q_DECLARE_FLAGS(DirtyFlags, DirtyFlag) @@ -155,6 +156,7 @@ public: inline const TextureParameters& parameters() const { return m_parameters; } inline const Qt3DCore::QNodeIdVector textureImageIds() const { return m_textureImageIds; } inline const QTextureGeneratorPtr& dataGenerator() const { return m_dataFunctor; } + inline int sharedTextureId() const { return m_sharedTextureId; } void setDataGenerator(const QTextureGeneratorPtr &generator); void updatePropertiesAndNotify(const TextureProperties &propreties); @@ -165,6 +167,7 @@ private: DirtyFlags m_dirty; TextureProperties m_properties; TextureParameters m_parameters; + int m_sharedTextureId; QTextureGeneratorPtr m_dataFunctor; Qt3DCore::QNodeIdVector m_textureImageIds; |