Can the Texture Data of a Qt 6.1 QSGTexture be Changed Dynamically?

Viewed 244

I am updating my Qt 5.15 graphics code from OpenGL dependent to the new RHI format in Qt 6.1. One requirement is to change the texture data on a 2D model frequently (20 times per sec). This was fairly easy to accomplish in OpenGL by just updating the texture data to an already allocated texture. I don't see how this is possible in the Qt 6 documentation. There is QSGDynamicTexture but I don't see anything in the API to allow to actually change the texture data. It provides updateTexture() to override but how can you update the actual texture data in this method?

I've also looked at using QSGTextureProvider or recreating the texture when needed but that seems very inefficient recreating the texture object every time.

Is it possible to update the raw texture data of a SQGTexture or similar in Qt 6.1?

Update

The following code demonstrates a working method. This draws an updated image correctly but I'd like to eliminate the QImage use and recreating the QSGTexture. Updating the QSGTexture data directly would achieve that, if possible.

    { // contruction of custom QSGNode
    QSGGeometryNode* node = new QSGGeometryNode;
    QSGGeometry* geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 0);
    // geometry vertices defined later
    QSGTextureMaterial* material = new QSGTextureMaterial;

    // blue image for testing
    QImage image(100, 100, QImage::Format_ARGB32);
    image.fill(Qt::blue);
    QSGTexture* texture = window->createTextureFromImage(image);
    m_material->setTexture(texture);

    node->setGeometry(geometry);
    node->setFlag(QSGNode::OwnsGeometry);
    node->setMaterial(material);
    node->setFlag(QSGNode::OwnsMaterial);
    }
    ...

    { // new image data - called in QQuickItem::::updatePaintNode
    QImage image = QImage((const uchar*)data, imageWidth, imageHeight, QImage::Format_ARGB32);
    QSGTexture* texture = window->createTextureFromImage(image);
    delete material->texture();
    material->setTexture(texture);
    node->markDirty(QSGNode::DirtyMaterial);
    }
0 Answers
Related