Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3drenderoutputprovider.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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
7
8#include "private/qquick3dobject_p.h"
9
10#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
11
12#include <ssg/qssgrenderextensions.h>
13#include <ssg/qssgrenderhelpers.h>
14#include <ssg/qssgrendercontextcore.h>
15#include <ssg/qssgrhicontext.h>
16#include <ssg/qquick3dextensionhelpers.h>
17
19
45
47 : m_ext(ext)
48{
49
50}
51
56
58{
59 // Update the extension ID.
60 // NOTE: This needs to be done on each call, the extension object may have changed.
61 // If the extension is destroyed (m_ext == nullptr), we cannot proceed and should return false!
62 m_extensionId = m_ext ? QQuick3DExtensionHelpers::getExtensionId(*m_ext) : QSSGExtensionId::Invalid;
63 if (QQuick3DExtensionHelpers::isNull(m_extensionId))
64 return false;
65
66 const bool wasDirty = m_isDirty;
67
68 switch (m_sourceType) {
70 // Nothing to do
71 m_isDirty = false;
72 break;
74 {
75 // Make sure we schedule the pass to run this frame
76 data.scheduleRenderResults(QSSGFrameData::RenderResults(m_builtInPass));
77
78 // Check if a texture exists for the result already
79 if (QRhiTexture *texture = data.getRhiResource(m_builtInPass).texture()) {
80 QSSGRenderExtensionHelpers::registerRenderResult(data, m_extensionId, texture);
81 m_isDirty = false;
82
83 }
84 // No "else" in this case since its likely the texture for the pass doesn't exist yet, try again in prepareRender.
85 }
86 break;
88 {
89 if (userPassId != QSSGResourceId::Invalid) {
90 // Make sure we schedule the pass to run this frame
91 data.scheduleRenderResults(userPassId);
92
93 // Check if a texture exists for the result already
94 if (QRhiTexture *texture = data.getRhiResource(userPassId, attachmentSelector).texture()) {
95 // Associate the texture with this extension and expose it so it can be used.
96 QSSGRenderExtensionHelpers::registerRenderResult(data, m_extensionId, texture);
97 m_isDirty = false;
98 }
99 }
100 // No "else" in this case since its likely the texture for the pass doesn't exist yet, try again in prepareRender.
101 }
102 break;
103 }
104
105 return wasDirty;
106}
107
109{
110 // If no id is set prepareData() should return false and this should not be called!
111 Q_ASSERT(!QQuick3DExtensionHelpers::isNull(m_extensionId));
112
113 QSSGRhiResourceHandle result;
115 result = data.getRhiResource(m_builtInPass);
116 else if (m_sourceType == SourceType::UserPass && userPassId != QSSGResourceId::Invalid)
117 result = data.getRhiResource(userPassId, attachmentSelector);
118
119 if (userPassId != QSSGResourceId::Invalid) {
120 if (QRhiTexture *texture = result.texture()) {
121 QSSGRenderExtensionHelpers::registerRenderResult(data, m_extensionId, texture);
122 m_isDirty = false;
123 }
124 }
125}
126
128{
129 // Nothing to render
130}
131
133{
134 // Nothing to reset
135}
136
137
138/*!
139 \qmltype RenderOutputProvider
140 \nativetype QQuick3DRenderOutputProvider
141 \inqmlmodule QtQuick3D.Helpers
142 \inherits TextureProviderExtension
143 \since 6.11
144 \brief Used as a bridge to access textures provided in passes.
145
146 This type is used to provide textures that are generated by passes in the rendering pipeline.
147
148 Some example of when this is useful is when you want to access the depth texture generated by the ZPrePass,
149 or the ambient occlusion texture generated by the SSAO pass. This type can be used to access these generated
150 textures and use them in materials or effects.
151
152 \qml
153 Texture {
154 textureProvider: RenderOutputProvider {
155 // Specify the pass buffer texture you want to access
156 textureSource: RenderOutputProvider.DepthTexture
157 }
158 }
159 \endqml
160
161 \sa QQuick3DTextureProviderExtension, QSSGRenderExtension
162*/
163
164QQuick3DRenderOutputProvider::QQuick3DRenderOutputProvider(QQuick3DObject *parent)
165 : QQuick3DTextureProviderExtension(parent)
166{
167 update();
168}
169
170QSSGRenderGraphObject *QQuick3DRenderOutputProvider::updateSpatialNode(QSSGRenderGraphObject *node)
171{
172 // Create new node if needed
173 if (!node)
174 node = new QSSGRenderOutputProviderExtension(this);
175
176 QQuick3DTextureProviderExtension::updateSpatialNode(node);
177
178 QSSGRenderOutputProviderExtension *providerNode = static_cast<QSSGRenderOutputProviderExtension *>(node);
179
180 providerNode->m_isDirty = (m_dirtyAttributes != 0);
181
182 if (m_dirtyAttributes & TextureSourceDirty) {
183 switch (m_textureSource) {
184 case QQuick3DRenderOutputProvider::TextureSource::None:
185 providerNode->m_sourceType = QSSGRenderOutputProviderExtension::SourceType::None;
186 // m_builtInPass is not used in this case
187 break;
188 case QQuick3DRenderOutputProvider::TextureSource::UserPassTexture:
189 case QQuick3DRenderOutputProvider::TextureSource::UserPassDepthTexture:
190 providerNode->m_sourceType = QSSGRenderOutputProviderExtension::SourceType::UserPass;
191 break;
192 case QQuick3DRenderOutputProvider::TextureSource::AoTexture:
193 providerNode->m_sourceType = QSSGRenderOutputProviderExtension::SourceType::BuiltInPass;
194 providerNode->m_builtInPass = QSSGFrameData::RenderResult::AoTexture;
195 break;
196 case QQuick3DRenderOutputProvider::TextureSource::DepthTexture:
197 providerNode->m_sourceType = QSSGRenderOutputProviderExtension::SourceType::BuiltInPass;
198 providerNode->m_builtInPass = QSSGFrameData::RenderResult::DepthTexture;
199 break;
200 case QQuick3DRenderOutputProvider::TextureSource::ScreenTexture:
201 providerNode->m_sourceType = QSSGRenderOutputProviderExtension::SourceType::BuiltInPass;
202 providerNode->m_builtInPass = QSSGFrameData::RenderResult::ScreenTexture;
203 break;
204 case QQuick3DRenderOutputProvider::TextureSource::NormalTexture:
205 providerNode->m_sourceType = QSSGRenderOutputProviderExtension::SourceType::BuiltInPass;
206 providerNode->m_builtInPass = QSSGFrameData::RenderResult::NormalTexture;
207 break;
208 case QQuick3DRenderOutputProvider::TextureSource::MotionVectorTexture:
209 providerNode->m_sourceType = QSSGRenderOutputProviderExtension::SourceType::BuiltInPass;
210 providerNode->m_builtInPass = QSSGFrameData::RenderResult::MotionVectorTexture;
211 break;
212 }
213 }
214
215 if (m_dirtyAttributes & (UserPassTextureDirty | TextureSourceDirty)) {
216 // m_builtInPass is not used in this case
217 QSSGResourceId userPassId = m_renderPass ? QQuick3DExtensionHelpers::getResourceId(*m_renderPass) : QSSGResourceId::Invalid;
218 providerNode->userPassId = userPassId;
219 providerNode->attachmentSelector = (m_textureSource == QQuick3DRenderOutputProvider::TextureSource::UserPassDepthTexture)
220 ? QSSGFrameData::AttachmentSelector::DepthAttachment
221 : QSSGFrameData::AttachmentSelector(m_attachmentSelector);
222 }
223
224 m_dirtyAttributes = 0;
225
226 return node;
227}
228
229void QQuick3DRenderOutputProvider::markAllDirty()
230{
231 m_dirtyAttributes = AllDirty;
232 QQuick3DTextureProviderExtension::markAllDirty();
233}
234
235void QQuick3DRenderOutputProvider::markDirty(QQuick3DRenderOutputProvider::DirtyType type)
236{
237 if (!(m_dirtyAttributes & quint32(type))) {
238 m_dirtyAttributes |= quint32(type);
239 update();
240 }
241}
242
243/*!
244 \qmlproperty enumeration RenderOutputProvider::textureSource
245 This property holds which pass buffer texture to expose.
246
247 \value RenderOutputProvider.None No texture source is selected.
248 \value RenderOutputProvider.UserPassTexture Accesses \l {ColorAttachment}{a color attachment} from a user-defined
249 \l RenderPass. Use the \l renderPass and \l attachmentSelector properties to specify
250 which pass and attachment to use.
251 \value [since 6.13] RenderOutputProvider.UserPassDepthTexture Accesses \l {DepthTextureAttachment}{a depth attachment} from a user-defined
252 \l RenderPass. Use the \l renderPass property to specify which pass to use. With a \l DepthStencilAttachment the texture stays null.
253 \value RenderOutputProvider.AoTexture The ambient occlusion texture created by QtQuick3D's SSAO pass.
254 \value RenderOutputProvider.DepthTexture The depth texture created by QtQuick3D's depth pre-pass.
255 \value RenderOutputProvider.ScreenTexture The texture containing the rendered scene.
256 \value RenderOutputProvider.NormalTexture The world-space normal texture created by QtQuick3D's normal pre-pass.
257 \value RenderOutputProvider.MotionVectorTexture The motion vector texture created by QtQuick3D's motion vector pass.
258 */
259
260QQuick3DRenderOutputProvider::TextureSource QQuick3DRenderOutputProvider::textureSource() const
261{
262 return m_textureSource;
263}
264
265void QQuick3DRenderOutputProvider::setTextureSource(TextureSource newTextureSource)
266{
267 if (m_textureSource == newTextureSource)
268 return;
269 m_textureSource = newTextureSource;
270 emit textureSourceChanged();
271 markDirty(TextureSourceDirty);
272}
273
274/*!
275 \qmlproperty RenderPass RenderOutputProvider::renderPass
276 The \l RenderPass whose output attachments will be exposed as a texture. Setting this
277 property automatically sets \l textureSource to \c UserPassTexture. Use
278 \l attachmentSelector to choose which color attachment of the pass to expose.
279 Set the textureSource to \c UserPassDepthTexture to access the depth attachment.
280
281 \sa attachmentSelector, textureSource, RenderPass
282*/
283
284QQuick3DRenderPass *QQuick3DRenderOutputProvider::renderPass() const
285{
286 return m_renderPass;
287}
288
289void QQuick3DRenderOutputProvider::setRenderPass(QQuick3DRenderPass *newRenderPass)
290{
291 if (m_renderPass == newRenderPass)
292 return;
293
294 QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DRenderOutputProvider::setRenderPass, newRenderPass, m_renderPass);
295
296 m_renderPass = newRenderPass;
297 if (m_renderPass) {
298 if (m_textureSource != TextureSource::UserPassTexture && m_textureSource != TextureSource::UserPassDepthTexture)
299 setTextureSource(TextureSource::UserPassTexture);
300 }
301 // Keep the current m_textureSource if m_renderPass is null.
302
303 markDirty(UserPassTextureDirty);
304
305 emit renderPassChanged();
306}
307
308/*!
309 \qmlproperty enumeration RenderOutputProvider::attachmentSelector
310 Selects which color attachment of the user-defined \l RenderPass to expose as a texture.
311 Only relevant when \l textureSource is set to \c UserPassTexture.
312
313 \value RenderOutputProvider.Attachment0 Use color attachment 0 (default).
314 \value RenderOutputProvider.Attachment1 Use color attachment 1.
315 \value RenderOutputProvider.Attachment2 Use color attachment 2.
316 \value RenderOutputProvider.Attachment3 Use color attachment 3.
317
318 \sa renderPass, textureSource
319*/
320
321QQuick3DRenderOutputProvider::AttachmentSelector QQuick3DRenderOutputProvider::attachmentSelector() const
322{
323 return m_attachmentSelector;
324}
325
326void QQuick3DRenderOutputProvider::setAttachmentSelector(AttachmentSelector newAttachmentSelector)
327{
328 if (m_attachmentSelector == newAttachmentSelector)
329 return;
330 m_attachmentSelector = newAttachmentSelector;
331 markDirty(UserPassTextureDirty);
332 emit attachmentSelectorChanged();
333}
334
335QT_END_NAMESPACE
QSSGFrameData::AttachmentSelector attachmentSelector
QSSGRenderOutputProviderExtension(QQuick3DRenderOutputProvider *ext)
void prepareRender(QSSGFrameData &data) override
Prepare data for rendering.
bool prepareData(QSSGFrameData &data) override
Called after scene data is collected, but before any render data or rendering in the current frame ha...
QPointer< QQuick3DRenderOutputProvider > m_ext
void render(QSSGFrameData &data) override
Record the render pass.
void resetForFrame() override
Called each time a new frame starts.
Combined button and popup list for selecting options.