xorblender.cpp Example File
scenegraph/twotextureproviders/xorblender.cpp
#include "xorblender.h"
#include <QtCore/QPointer>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFunctions>
#include <QtQuick/QSGSimpleMaterial>
#include <QtQuick/QSGTexture>
#include <QtQuick/QSGGeometryNode>
#include <QtQuick/QSGTextureProvider>
struct XorBlendState {
QSGTexture *texture1;
QSGTexture *texture2;
};
class XorBlendShader : public QSGSimpleMaterialShader<XorBlendState>
{
QSG_DECLARE_SIMPLE_SHADER(XorBlendShader, XorBlendState)
public:
const char *vertexShader() const override {
return
"attribute highp vec4 aVertex; \n"
"attribute highp vec2 aTexCoord; \n"
"uniform highp mat4 qt_Matrix; \n"
"varying highp vec2 vTexCoord; \n"
"void main() { \n"
" gl_Position = qt_Matrix * aVertex; \n"
" vTexCoord = aTexCoord; \n"
"}";
}
const char *fragmentShader() const override {
return
"uniform lowp float qt_Opacity; \n"
"uniform lowp sampler2D uSource1; \n"
"uniform lowp sampler2D uSource2; \n"
"varying highp vec2 vTexCoord; \n"
"void main() { \n"
" lowp vec4 p1 = texture2D(uSource1, vTexCoord); \n"
" lowp vec4 p2 = texture2D(uSource2, vTexCoord); \n"
" gl_FragColor = (p1 * (1.0 - p2.a) + p2 * (1.0 - p1.a)) * qt_Opacity; \n"
"}";
}
QList<QByteArray> attributes() const override {
return QList<QByteArray>() << "aVertex" << "aTexCoord";
}
void updateState(const XorBlendState *state, const XorBlendState *) override {
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glActiveTexture(GL_TEXTURE1);
state->texture2->bind();
f->glActiveTexture(GL_TEXTURE0);
state->texture1->bind();
}
void resolveUniforms() override {
program()->setUniformValue("uSource1", 0);
program()->setUniformValue("uSource2", 1);
}
};
class XorNode : public QObject, public QSGNode
{
Q_OBJECT
public:
XorNode(QSGTextureProvider *p1, QSGTextureProvider *p2)
: m_provider1(p1)
, m_provider2(p2)
{
setFlag(QSGNode::UsePreprocess, true);
m_material = XorBlendShader::createMaterial();
m_material->state()->texture1 = nullptr;
m_material->state()->texture2 = nullptr;
m_material->setFlag(QSGMaterial::Blending);
m_node.setMaterial(m_material);
m_node.setFlag(QSGNode::OwnsMaterial);
m_node.setGeometry(new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4));
m_node.setFlag(QSGNode::OwnsGeometry);
connect(m_provider1.data(), &QSGTextureProvider::textureChanged, this, &XorNode::textureChange, Qt::DirectConnection);
connect(m_provider2.data(), &QSGTextureProvider::textureChanged, this, &XorNode::textureChange, Qt::DirectConnection);
}
void preprocess() override {
XorBlendState *state = m_material->state();
if (m_provider1) {
state->texture1 = m_provider1->texture();
if (QSGDynamicTexture *dt1 = qobject_cast<QSGDynamicTexture *>(state->texture1))
dt1->updateTexture();
}
if (m_provider2) {
state->texture2 = m_provider2->texture();
if (QSGDynamicTexture *dt2 = qobject_cast<QSGDynamicTexture *>(state->texture2))
dt2->updateTexture();
}
if (m_node.parent() && (!state->texture1 || !state->texture2))
removeChildNode(&m_node);
else if (!m_node.parent() && state->texture1 && state->texture2)
appendChildNode(&m_node);
}
void setRect(const QRectF &rect) {
if (m_rect != rect) {
m_rect = rect;
QSGGeometry::updateTexturedRectGeometry(m_node.geometry(), m_rect, QRectF(0, 0, 1, 1));
m_node.markDirty(QSGNode::DirtyGeometry);
}
}
public slots:
void textureChange() {
markDirty(QSGNode::DirtyMaterial);
}
private:
QRectF m_rect;
QSGSimpleMaterial<XorBlendState> *m_material;
QSGGeometryNode m_node;
QPointer<QSGTextureProvider> m_provider1;
QPointer<QSGTextureProvider> m_provider2;
};
XorBlender::XorBlender(QQuickItem *parent)
: QQuickItem(parent)
, m_source1(nullptr)
, m_source2(nullptr)
, m_source1Changed(false)
, m_source2Changed(false)
{
setFlag(ItemHasContents, true);
}
void XorBlender::setSource1(QQuickItem *i)
{
if (i == m_source1)
return;
m_source1 = i;
emit source1Changed(m_source1);
m_source1Changed = true;
update();
}
void XorBlender::setSource2(QQuickItem *i)
{
if (i == m_source2)
return;
m_source2 = i;
emit source2Changed(m_source2);
m_source2Changed = true;
update();
}
QSGNode *XorBlender::updatePaintNode(QSGNode *old, UpdatePaintNodeData *)
{
bool abort = false;
if (!m_source1 || !m_source1->isTextureProvider()) {
qDebug() << "source1 is missing or not a texture provider";
abort = true;
}
if (!m_source2 || !m_source2->isTextureProvider()) {
qDebug() << "source2 is missing or not a texture provider";
abort = true;
}
if (abort) {
delete old;
return nullptr;
}
XorNode *node = static_cast<XorNode *>(old);
if (m_source1Changed || m_source2Changed) {
delete node;
node = nullptr;
m_source1Changed = false;
m_source2Changed = false;
}
if (!node)
node = new XorNode(m_source1->textureProvider(), m_source2->textureProvider());
node->setRect(boundingRect());
return node;
}
#include "xorblender.moc"