Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qssgrhiresourcehandle.cpp
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
"qssgrhiresourcehandle_p.h"
6
7
#
include
<
QtQuick3DUtils
/
private
/
qssgassert_p
.
h
>
8
9
#
include
<
QtCore
/
qthread
.
h
>
10
#
include
<
rhi
/
qrhi
.
h
>
11
12
QT_BEGIN_NAMESPACE
13
14
QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QSSGRhiResourceHandlePrivate)
15
16
/*!
17
\class QSSGRhiResourceHandle
18
\inmodule QtQuick3D
19
\since 6.13
20
21
\brief A non-owning handle to an RHI resource created by the renderer.
22
23
A QSSGRhiResourceHandle is returned by \l QSSGFrameData::getRhiResource().
24
It gives access to an RHI resource the renderer created, such as the
25
QRhiTexture of a \l {QtQuick3D::ShaderImage}{ShaderImage}, the QRhiBuffer
26
of a \l {QtQuick3D::ShaderStorageBuffer}{ShaderStorageBuffer}, a render
27
result such as the depth texture of a View3D, or an attachment of a
28
\l {QtQuick3D::RenderPass}{RenderPass}. Depending on the resource,
29
either texture(), buffer() or renderBuffer() returns it.
30
31
\section1 Scene resources and RHI resources
32
33
The extension API deals with two different kinds of resources, and each
34
has its own kind of id:
35
36
\list
37
\li A \e{scene resource} is an object in the scene, such as a ShaderImage,
38
a ShaderStorageBuffer or a RenderPass. It is identified by a
39
\l QSSGResourceId, obtained with
40
\l QQuick3DExtensionHelpers::getResourceId(). The id stays the same
41
for as long as the object exists.
42
\li An \e{RHI resource} is the QRhiTexture, QRhiBuffer or
43
QRhiRenderBuffer the renderer creates for a scene resource. It is accessed through a
44
QSSGRhiResourceHandle and identified by globalResourceId(), which is
45
the QRhiResource::globalResourceId() of the RHI resource.
46
\endlist
47
48
A scene resource keeps its QSSGResourceId while the renderer replaces the
49
RHI resource behind it, for example when the size or format of a
50
ShaderImage changes. Pass a QSSGResourceId to
51
\l QSSGFrameData::getRhiResource() to get the current RHI resource, and
52
compare the handle with one obtained earlier to find out whether the
53
resource has been replaced. A replacement is usually a new RHI resource
54
with a new globalResourceId(), but the renderer can also rebuild a
55
resource in place, for example with a new size, which keeps the
56
QRhiResource object and its globalResourceId(). The handles compare as
57
different in both cases. The two ids are unrelated and cannot be
58
converted into each other.
59
60
\section1 Comparing handles
61
62
Handles can be compared with \c{==} and \c{!=}. Two handles compare equal
63
if they were obtained for the same RHI resource, without the renderer
64
replacing or rebuilding it in between. Copies of a handle compare equal,
65
also after the resource was retired, and so do two default constructed
66
handles. A handle obtained before the resource was rebuilt in place
67
compares unequal to one obtained after, even though both have the same
68
globalResourceId().
69
70
\section1 Lifetime
71
72
The handle does not own the RHI resource, and holding it does not keep the
73
resource alive. The renderer retires the resource when it replaces it,
74
when the scene resource is destroyed, and when the window's graphics
75
resources are released. From then on isValid() returns \c false and
76
texture(), buffer() and renderBuffer() return \nullptr. A retired handle
77
still compares equal to its copies, so a stored handle can be compared
78
with a new one:
79
80
\code
81
void MyExtension::prepareRender(QSSGFrameData &data)
82
{
83
const QSSGRhiResourceHandle image = data.getRhiResource(m_imageId);
84
if (image != m_boundImage) {
85
// The texture was replaced, or this is the first frame.
86
m_boundImage = image;
87
rebuildShaderResourceBindings(image.texture());
88
}
89
}
90
\endcode
91
92
The pointers returned by texture(), buffer() and renderBuffer() are not
93
tracked. When the renderer replaces or releases a resource during a
94
frame, a pointer obtained earlier stays usable until the end of that
95
frame, but must not be used after that. For a buffer registered by a
96
buffer provider, this depends on the provider destroying the buffer as
97
described for QSSGRenderExtensionHelpers::ResultOwnership.
98
99
QSSGRhiResourceHandle may only be used on the render thread.
100
101
\sa QSSGFrameData::getRhiResource(), QSSGResourceId
102
*/
103
104
/*!
105
Constructs an invalid handle.
106
*/
107
QSSGRhiResourceHandle::QSSGRhiResourceHandle()
noexcept
=
default
;
108
109
/*!
110
\internal
111
*/
112
QSSGRhiResourceHandle::QSSGRhiResourceHandle(QSSGRhiResourceHandlePrivate *dd)
noexcept
113
: d(dd)
114
{
115
}
116
117
/*!
118
Destroys the handle. This does not affect the resource.
119
*/
120
QSSGRhiResourceHandle::~QSSGRhiResourceHandle() =
default
;
121
122
/*!
123
Constructs a copy of \a other.
124
*/
125
QSSGRhiResourceHandle::QSSGRhiResourceHandle(
const
QSSGRhiResourceHandle &other)
noexcept
=
default
;
126
127
/*!
128
Assigns \a other to this handle.
129
*/
130
QSSGRhiResourceHandle &QSSGRhiResourceHandle::operator=(
const
QSSGRhiResourceHandle &other)
noexcept
=
default
;
131
132
/*!
133
\return \c true if the handle refers to a resource that the renderer has
134
not retired.
135
*/
136
bool
QSSGRhiResourceHandle::isValid()
const
noexcept
137
{
138
return
d && (d->texture || d->buffer || d->renderBuffer);
139
}
140
141
/*!
142
\return The texture, or \nullptr if the handle is invalid or does not
143
refer to a texture.
144
*/
145
QRhiTexture *QSSGRhiResourceHandle::texture()
const
noexcept
146
{
147
return
d ? d->texture :
nullptr
;
148
}
149
150
/*!
151
\return The buffer, or \nullptr if the handle is invalid or does not
152
refer to a buffer.
153
*/
154
QRhiBuffer *QSSGRhiResourceHandle::buffer()
const
noexcept
155
{
156
return
d ? d->buffer :
nullptr
;
157
}
158
159
/*!
160
\return The render buffer, or \nullptr if the handle is invalid or does
161
not refer to a render buffer.
162
163
A render buffer is returned for the depth-stencil attachment of a
164
\l {QtQuick3D::RenderPass}{RenderPass} that does not sample its depth.
165
166
\sa QSSGFrameData::AttachmentSelector
167
*/
168
QRhiRenderBuffer *QSSGRhiResourceHandle::renderBuffer()
const
noexcept
169
{
170
return
d ? d->renderBuffer :
nullptr
;
171
}
172
173
/*!
174
\return The QRhiResource::globalResourceId() of the RHI resource, or 0 for
175
a default constructed handle.
176
177
This is not the \l QSSGResourceId of the scene resource. The scene
178
resource keeps its QSSGResourceId when the renderer replaces the RHI
179
resource behind it, while the replacement gets a new globalResourceId().
180
Unlike the address of the QRhiResource, a globalResourceId() is unique
181
across all QRhi instances and is never given to another RHI resource.
182
183
The globalResourceId() is kept after the handle becomes invalid. To find
184
out whether the resource was replaced, compare handles rather than ids,
185
since a resource the renderer rebuilds in place keeps its
186
globalResourceId().
187
188
\sa {Scene resources and RHI resources}
189
*/
190
quint64 QSSGRhiResourceHandle::globalResourceId()
const
noexcept
191
{
192
return
d ? d->id : 0;
193
}
194
195
QSSGRhiResourceHandlePrivate::QSSGRhiResourceHandlePrivate(QRhiTexture *texture)
196
: texture(texture)
197
, id(texture->globalResourceId())
198
{
199
}
200
201
QSSGRhiResourceHandlePrivate::QSSGRhiResourceHandlePrivate(QRhiBuffer *buffer)
202
: buffer(buffer)
203
, id(buffer->globalResourceId())
204
{
205
}
206
207
QSSGRhiResourceHandlePrivate::QSSGRhiResourceHandlePrivate(QRhiRenderBuffer *renderBuffer)
208
: renderBuffer(renderBuffer)
209
, id(renderBuffer->globalResourceId())
210
{
211
}
212
213
// The records are not synchronized, so the registry must only be used on the
214
// thread of the QRhi that owns the resources, as for QSSGManagedRhiTexture.
215
// The resource itself is not used, since retire() can be given a resource the
216
// owner has already deleted.
217
static
void
checkRenderThread
(
const
QRhi *rhi)
218
{
219
QSSG_CHECK_X(!rhi || QThread::currentThread() == rhi->thread(),
220
"Render resource handle requested or retired off the render thread"
);
221
}
222
223
QSSGRhiResourceRegistry::~QSSGRhiResourceRegistry()
224
{
225
retireAll();
226
}
227
228
template
<
typename
T>
229
QSSGRhiResourceHandle QSSGRhiResourceRegistry::handleImpl(T *resource)
230
{
231
if
(!resource)
232
return
{};
233
234
retireIfReplaced(resource);
235
236
auto
&record = m_records[resource];
237
if
(!record)
238
record =
new
QSSGRhiResourceHandlePrivate(resource);
239
240
return
record->handle();
241
}
242
243
QSSGRhiResourceHandle QSSGRhiResourceRegistry::handle(QRhiTexture *texture)
244
{
245
return
handleImpl(texture);
246
}
247
248
QSSGRhiResourceHandle QSSGRhiResourceRegistry::handle(QRhiBuffer *buffer)
249
{
250
return
handleImpl(buffer);
251
}
252
253
QSSGRhiResourceHandle QSSGRhiResourceRegistry::handle(QRhiRenderBuffer *renderBuffer)
254
{
255
return
handleImpl(renderBuffer);
256
}
257
258
void
QSSGRhiResourceRegistry::retire(
const
QRhiResource *resource)
259
{
260
if
(!resource)
261
return
;
262
checkRenderThread(m_rhi);
263
if
(
auto
record = m_records.take(resource))
264
record->invalidate();
265
}
266
267
void
QSSGRhiResourceRegistry::retireIfReplaced(
const
QRhiResource *resource)
268
{
269
if
(!resource)
270
return
;
271
checkRenderThread(m_rhi);
272
// A resource that was destroyed without being retired, for example an
273
// extension's buffer, can leave a record behind whose address is later
274
// reused. The id tells the two apart.
275
const
auto
it = m_records.find(resource);
276
if
(it != m_records.end() && it.value()->id != resource->globalResourceId()) {
277
it.value()->invalidate();
278
m_records.erase(it);
279
}
280
}
281
282
void
QSSGRhiResourceRegistry::retireAll()
283
{
284
for
(
const
auto
&record : std::as_const(m_records))
285
record->invalidate();
286
m_records.clear();
287
}
288
289
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
checkRenderThread
static void checkRenderThread(const QRhi *rhi)
Definition
qssgrhiresourcehandle.cpp:217
qtquick3d
src
runtimerender
extensionapi
qssgrhiresourcehandle.cpp
Generated on
for Qt by
1.18.0