[Interest] Creating Custom QSGTexture
Benjamin B (BBenj)
balga.benjamin at gmail.com
Thu Sep 11 15:51:30 CEST 2025
Hi
I also had some troubles figuring that out. Might not be the best way but it works:
I derive QSGTexture and create/update a QRhiTexture in commitTextureOperations.
I update the texture data and call commitTextureOperations in my QSGMaterial instance (in QSGMaterial::updateTextures, called from QSGMaterialShader::updateSampledImage).
class RgbTexture : public QSGTexture ...
std::unique_ptr<QRhiTexture> m_rhiTexture;
void RgbTexture::commitTextureOperations(QRhi* rhi, QRhiResourceUpdateBatch* resourceUpdates) override
{
if ( ! m_data || m_size <= 0) // raw RGBA data
return;
if ( ! m_rhiTexture) {
m_rhiTexture.reset(rhi->newTexture(QRhiTexture::RGBA8, textureSize(), 1, {}) );
bool const created = m_rhiTexture->create();
Q_ASSERT("Failed to create texture" && created);
}
QRhiTextureSubresourceUploadDescription subresDesc(m_data, m_size);
subresDesc.setSourceSize(textureSize());
subresDesc.setDestinationTopLeft(QPoint(0,0));
QRhiTextureUploadEntry entry(0,0, subresDesc);
QRhiTextureUploadDescription desc(entry);
resourceUpdates->uploadTexture(m_rhiTexture.get(), desc);
}
void RgbMaterial::updateTextures(QRhi* rhi, QRhiResourceUpdateBatch* resourceUpdates)
{
//std::unique_ptr<RgbTexture> m_texture;
m_texture->setData(...);
m_texture->commitTextureOperations(rhi, resourceUpdates);
}
-Benjamin
> Le 11 sept. 2025 à 15:11, Mike Krus via Interest <interest at qt-project.org> a écrit :
>
> Thanks, think I may have explained it wrong though.
>
> I don’t have a native texture to use. I have some data I want to use in a shader via a texture.
> I need to create a QSGTexture type object but there doesn’t seem to be any existing API to
> do that beyond createTextureFromImage().
>
> I presume I’d need to derive and use RHI itself to create an RHI texture but was hoping to
> find an example which that in the context of the scene graph, especially when it comes to
> the creation of the RHI texture and the uploading/updating of the data.
>
> Mike
>
>
>> On 11 Sep 2025, at 13:44, Allan Sandfeld Jensen <kde at carewolf.com> wrote:
>>
>> On Thursday, 11 September 2025 12:28:52 Central European Summer Time Mike Krus
>> via Interest wrote:
>>> Hi
>>>
>>> I’m porting a project from Qt5/OpenGL SceneGraph code to new Qt6/RHI
>>>
>>> I have a custom item with a scene graph which creates a custom shader and a
>>> custom texture from some application data (not an image).
>>>
>>> In Qt5, I have Texture class derived from QSGTexture which create the
>>> underlying OpenGL buffers for the textures and handles to data, binding,
>>> etc.
>>>
>>> In Qt6, the same classes exists but the custom Texture class needs to create
>>> RHI based texture objects, with the appropriate format, copy the data into
>>> it, etc. But I have not been able to find some relevant code to do that.
>>> All the code I found in QtDeclarative itself either use
>>> QQuickWindow::createTextureFromImage() or a QSGTextureProvider.
>>>
>>> There some example which create custom RHI textures, but not in the context
>>> of the scene graph (using RHI directly in QQuickRHIItem for example).
>>>
>>> Any ideas how this can be done?
>>>
>> The way we do in Qt WebEngine now, is using QNativeInterface. For instance
>> from OpenGL texture
>> QNativeInterface::QSGOpenGLTexture::fromNative(texture, win, size, texOpts);
>> or from Metal texture
>> QNativeInterface::QSGMetalTexture::fromNative(texture, win, size, texOpts);
>> or from Vulkan texture
>> QNativeInterface::QSGVulkanTexture::fromNative(importedImage,
>> importedImageCreateInfo.initialLayout, win, size, texOpts);
>>
>> Best regards
>> Allan
>
>
> —
> Mike Krus | mike.krus at kdab.com <mailto:mike.krus at kdab.com> | Senior Software Engineer & Teamlead
> KDAB (UK) Ltd., a KDAB Group company
> Tel: UK Office +44 1625 809908 Mobile +44 7833 491941
> KDAB - Trusted Software Excellence
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org <mailto:Interest at qt-project.org>
> https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20250911/827fb8d0/attachment.htm>
More information about the Interest
mailing list