QSSGRhiResourceHandle Class
A non-owning handle to an RHI resource created by the renderer. More...
| Header: | #include <qssgrenderextensions.h> |
| Since: | Qt 6.13 |
Public Functions
| QSSGRhiResourceHandle() | |
| QSSGRhiResourceHandle(const QSSGRhiResourceHandle &other) | |
| QSSGRhiResourceHandle(QSSGRhiResourceHandle &&other) | |
| ~QSSGRhiResourceHandle() | |
| QRhiBuffer * | buffer() const |
| quint64 | globalResourceId() const |
| bool | isValid() const |
| QRhiRenderBuffer * | renderBuffer() const |
| QRhiTexture * | texture() const |
| QSSGRhiResourceHandle & | operator=(const QSSGRhiResourceHandle &other) |
Detailed Description
A QSSGRhiResourceHandle is returned by QSSGFrameData::getRhiResource(). It gives access to an RHI resource the renderer created, such as the QRhiTexture of a ShaderImage, the QRhiBuffer of a ShaderStorageBuffer, a render result such as the depth texture of a View3D, or an attachment of a RenderPass. Depending on the resource, either texture(), buffer() or renderBuffer() returns it.
Scene resources and RHI resources
The extension API deals with two different kinds of resources, and each has its own kind of id:
- A scene resource is an object in the scene, such as a ShaderImage, a ShaderStorageBuffer or a RenderPass. It is identified by a QSSGResourceId, obtained with QQuick3DExtensionHelpers::getResourceId(). The id stays the same for as long as the object exists.
- An RHI resource is the QRhiTexture, QRhiBuffer or QRhiRenderBuffer the renderer creates for a scene resource. It is accessed through a QSSGRhiResourceHandle and identified by globalResourceId(), which is the QRhiResource::globalResourceId() of the RHI resource.
A scene resource keeps its QSSGResourceId while the renderer replaces the RHI resource behind it, for example when the size or format of a ShaderImage changes. Pass a QSSGResourceId to QSSGFrameData::getRhiResource() to get the current RHI resource, and compare the handle with one obtained earlier to find out whether the resource has been replaced. A replacement is usually a new RHI resource with a new globalResourceId(), but the renderer can also rebuild a resource in place, for example with a new size, which keeps the QRhiResource object and its globalResourceId(). The handles compare as different in both cases. The two ids are unrelated and cannot be converted into each other.
Comparing handles
Handles can be compared with == and !=. Two handles compare equal if they were obtained for the same RHI resource, without the renderer replacing or rebuilding it in between. Copies of a handle compare equal, also after the resource was retired, and so do two default constructed handles. A handle obtained before the resource was rebuilt in place compares unequal to one obtained after, even though both have the same globalResourceId().
Lifetime
The handle does not own the RHI resource, and holding it does not keep the resource alive. The renderer retires the resource when it replaces it, when the scene resource is destroyed, and when the window's graphics resources are released. From then on isValid() returns false and texture(), buffer() and renderBuffer() return nullptr. A retired handle still compares equal to its copies, so a stored handle can be compared with a new one:
void MyExtension::prepareRender(QSSGFrameData &data)
{
const QSSGRhiResourceHandle image = data.getRhiResource(m_imageId);
if (image != m_boundImage) {
// The texture was replaced, or this is the first frame.
m_boundImage = image;
rebuildShaderResourceBindings(image.texture());
}
}The pointers returned by texture(), buffer() and renderBuffer() are not tracked. When the renderer replaces or releases a resource during a frame, a pointer obtained earlier stays usable until the end of that frame, but must not be used after that. For a buffer registered by a buffer provider, this depends on the provider destroying the buffer as described for QSSGRenderExtensionHelpers::ResultOwnership.
QSSGRhiResourceHandle may only be used on the render thread.
See also QSSGFrameData::getRhiResource() and QSSGResourceId.
Member Function Documentation
[noexcept] QSSGRhiResourceHandle::QSSGRhiResourceHandle()
Constructs an invalid handle.
[noexcept] QSSGRhiResourceHandle::QSSGRhiResourceHandle(const QSSGRhiResourceHandle &other)
Constructs a copy of other.
[noexcept default] QSSGRhiResourceHandle::QSSGRhiResourceHandle(QSSGRhiResourceHandle &&other)
Move-constructs an instance of QSSGRhiResourceHandle.
[noexcept] QSSGRhiResourceHandle::~QSSGRhiResourceHandle()
Destroys the handle. This does not affect the resource.
[noexcept] QRhiBuffer *QSSGRhiResourceHandle::buffer() const
Returns The buffer, or nullptr if the handle is invalid or does not refer to a buffer.
[noexcept] quint64 QSSGRhiResourceHandle::globalResourceId() const
Returns The QRhiResource::globalResourceId() of the RHI resource, or 0 for a default constructed handle.
This is not the QSSGResourceId of the scene resource. The scene resource keeps its QSSGResourceId when the renderer replaces the RHI resource behind it, while the replacement gets a new globalResourceId(). Unlike the address of the QRhiResource, a globalResourceId() is unique across all QRhi instances and is never given to another RHI resource.
The globalResourceId() is kept after the handle becomes invalid. To find out whether the resource was replaced, compare handles rather than ids, since a resource the renderer rebuilds in place keeps its globalResourceId().
See also Scene resources and RHI resources.
[noexcept] bool QSSGRhiResourceHandle::isValid() const
Returns true if the handle refers to a resource that the renderer has not retired.
[noexcept] QRhiRenderBuffer *QSSGRhiResourceHandle::renderBuffer() const
Returns The render buffer, or nullptr if the handle is invalid or does not refer to a render buffer.
A render buffer is returned for the depth-stencil attachment of a RenderPass that does not sample its depth.
See also QSSGFrameData::AttachmentSelector.
[noexcept] QRhiTexture *QSSGRhiResourceHandle::texture() const
Returns The texture, or nullptr if the handle is invalid or does not refer to a texture.
[noexcept] QSSGRhiResourceHandle &QSSGRhiResourceHandle::operator=(const QSSGRhiResourceHandle &other)
Assigns other to this handle.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.