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
qssgrenderextensions.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5#include "private/qssgassert_p.h"
6#include "private/qssglayerrenderdata_p.h"
8
10
11/*!
12 \class QSSGFrameData
13 \inmodule QtQuick3D
14 \since 6.7
15
16 \brief Storage class containing data collected for a frame.
17*/
18
19/*!
20 \return The renderable texture result from \a id. \nullptr if no matching \a id was found.
21
22 \note Even if the function returns a non-null result, the returned QSSGRhiRenderableTexture
23 might not be ready unless the pass rendering to the texture has been executed.
24
25 \note The returned value is only valid within the current frame. On each new frame
26 the renderable will be reset and should therefore be queried again.
27*/
28QSSGFrameData::Result QSSGFrameData::getRenderResult(RenderResult id) const
29{
30 using RenderResultT = std::underlying_type_t<RenderResult>;
31 const QSSGRhiRenderableTexture *res = nullptr;
32 auto *data = QSSGLayerRenderData::getCurrent(*m_ctx->renderer());
33 if (QSSG_GUARD(data && (std::size(data->renderResults) > RenderResultT(id))))
34 res = data->getRenderResult(id);
35
36 return res ? Result{ res->texture, res->depthStencil } : Result{};
37}
38
39/*!
40 Schedule the given \a results to be made available for this frame.
41
42 This function should only be called during the prepare phase in \l QSSGRenderExtension::prepareData().
43
44 \note The requested results might not be available if the underlying layer does not support
45 them or if the layer does not contain any data that would make it necessary to produce the
46 requested results, in which case \l getRenderResult() will return a empty result.
47
48 \sa QSSGRenderExtension::getRenderResult()
49*/
50
51void QSSGFrameData::scheduleRenderResults(RenderResults results) const
52{
53 auto *data = QSSGLayerRenderData::getCurrent(*m_ctx->renderer());
54 QSSG_ASSERT(data, return);
55
56 auto &prepResult = data->layerPrepResult;
57
58 if (prepResult.getState() != QSSGLayerRenderPreparationResult::State::DataPrep) {
59 qWarning("QSSGFrameData::requestRenderResults: "
60 "Requesting render results should only be done during the prepare phase in prepareData().");
61 return;
62 }
63
64 if (results.testFlag(QSSGFrameData::RenderResult::DepthTexture))
65 prepResult.flags.setRequiresDepthTexture(true);
66 if (results.testFlag(QSSGFrameData::RenderResult::ScreenTexture))
67 prepResult.flags.setRequiresScreenTexture(true);
68 if (results.testFlag(RenderResult::AoTexture)) {
69 // NOTE: AO depends on the depth texture
70 prepResult.flags.setRequiresSsaoPass(true);
71 prepResult.flags.setRequiresDepthTexture(true);
72 }
73 if (results.testFlag(RenderResult::NormalTexture))
74 prepResult.flags.setRequiresNormalTexture(true);
75}
76
77/*!
78 \return Base pipeline state for this frame
79 */
80QSSGRhiGraphicsPipelineState QSSGFrameData::getPipelineState() const
81{
82 auto *data = QSSGLayerRenderData::getCurrent(*m_ctx->renderer());
83 QSSG_ASSERT(data, return {});
84 return data->getPipelineState();
85}
86
87/*!
88 \return The active camera for the scene, or null if non could be found.
89*/
90QSSGCameraId QSSGFrameData::activeCamera() const
91{
92 QSSGCameraId ret { QSSGCameraId::Invalid };
93 auto *data = QSSGLayerRenderData::getCurrent(*m_ctx->renderer());
94 QSSG_ASSERT(data, return ret);
95 if (auto *ac = data->activeCamera())
96 ret = QSSGRenderGraphObjectUtils::getCameraId(*ac);
97
98 return ret;
99}
100
101QSSGRenderContextInterface *QSSGFrameData::contextInterface() const
102{
103 return m_ctx;
104}
105
106/*!
107 \return A list of layer nodes that match the \a layerMask and \a typeMask.
108 */
109QSSGNodeIdList QSSGFrameData::getLayerNodes(quint32 layerMask, TypeMask typeMask) const
110{
111 QSSG_ASSERT(m_ctx, return {});
112
113 auto *layer = getCurrent();
114 QSSG_ASSERT_X(layer, "No active layer for renderer!", return {});
115 const auto &layerNodes = layer->layerNodes;
116
117 return QSSGLayerRenderData::filter(layerNodes, layerMask, typeMask);;
118}
119
120/*!
121 \return A list of layer nodes for the given \a cameraId that match the \a typeMask.
122 If the camera does not have a layer mask, an empty list is returned.
123*/
124QSSGNodeIdList QSSGFrameData::getLayerNodes(QSSGCameraId cameraId, TypeMask typeMask) const
125{
126 auto *camera = QSSGRenderGraphObjectUtils::getNode<QSSGRenderCamera>(QSSGNodeId(cameraId));
127 const quint32 layerMask = camera ? camera->tag.value() : 0 /* LayerNone */;
128
129 return (layerMask != 0) ? getLayerNodes(layerMask, typeMask) : QSSGNodeIdList{};
130}
131
132void QSSGFrameData::clear()
133{
134
135}
136
137QSSGLayerRenderData *QSSGFrameData::getCurrent() const
138{
139 return QSSGLayerRenderData::getCurrent(*m_ctx->renderer());
140}
141
142QSSGFrameData::QSSGFrameData(QSSGRenderContextInterface *ctx)
143 : m_ctx(ctx)
144{
145
146}
147
148/*!
149 \class QSSGRenderExtension
150 \inmodule QtQuick3D
151 \since 6.7
152
153 \brief Base class for extension backend node implementations.
154
155 \sa QQuick3DRenderExtension
156*/
157
158/*!
159 Constructor that allows users to specifying a user-type and flags for an extension.
160
161 \note For user-defined extensions the type must be a combination of \l QSSGRenderGraphObject::BaseType::User
162 and a value between 0 and 4095.
163
164 \note The \l QSSGRenderGraphObject::BaseType::Extension type is automatically added to the given \a inType.
165
166 \note The \a inFlags must include \l Flags::HasGraphicsResources if the extension
167 allocates graphics resources.
168
169 */
170QSSGRenderExtension::QSSGRenderExtension(Type inType, FlagT inFlags)
171 : QSSGRenderGraphObject(static_cast<Type>(TypeT(inType) | TypeT(QSSGRenderGraphObject::BaseType::Extension)), inFlags)
172{
173 Q_ASSERT_X((QSSGRenderGraphObjectUtils::getBaseType(type) == QSSGRenderGraphObject::BaseType::Extension) ||
174 (QSSGRenderGraphObjectUtils::getBaseType(type) == (QSSGRenderGraphObject::BaseType::Extension | QSSGRenderGraphObject::BaseType::User)),
175 "QSSGRenderExtension()",
176 "The type must be a combination of QSSGRenderGraphObject::BaseType::Extension "
177 "and optionally QSSGRenderGraphObject::BaseType::User.");
178}
179
180QSSGRenderExtension::QSSGRenderExtension()
181 : QSSGRenderGraphObject(QSSGRenderGraphObject::Type::RenderExtension, FlagT(Flags::HasGraphicsResources))
182{
183
184}
185
186QSSGRenderExtension::~QSSGRenderExtension()
187{
188
189}
190
191/*!
192 \enum QSSGRenderExtension::RenderMode
193
194 Specifies the render extension mode.
195
196 \value Standalone The rendering code is recorded in full during the render prepare phase.
197 This will usually imply that there are some output crated for a preceding render extension(s).
198 When this mode is used the \l prepareRender() and \l render() functions are both called during
199 the frame's prepare phase.
200
201 \value Main The rendering code is recorded within the main render pass. In this mode the
202 \l prepareRender() is called in the frame's prepare phase while \l render() is called the frame's render phase.
203
204*/
205
206/*!
207 \enum QSSGRenderExtension::RenderStage
208
209 Specifies the order the extension will be called.
210
211 \value PreColor The rendering code is recorded and executed before the main (color) pass.
212 \value PostColor The rendering code is recorded and executed after the main (color) pass.
213*/
214
215
216/*!
217 Called after scene \a data is collected, but before any render data or rendering in the current
218 frame has been done.
219
220 \return Dirty state. Return \c true if the there are dirty data
221 that needs to be rendered.
222
223 \note Much of the data created/collected from the engine during the prepare and render phases
224 is per-frame and should be released or assumed released at the start of the next frame
225
226 \sa QSSGFrameData
227*/
228bool QSSGRenderExtension::prepareData(QSSGFrameData &data)
229{
230 Q_UNUSED(data);
231 return false;
232}
233
234/*!
235 Prepare data for rendering. Build and collect \a data needed for rendering. Any render extension
236 scheduled before this one has been processed. In addition; any render extension of
237 mode \l RenderMode::Standalone will, if successful, have been completed in full.
238
239 \note Much of the data created/collected from the engine during the prepare and render phases
240 is per-frame and should be released or assumed released at the start of the next frame
241
242 \sa QSSGFrameData
243*/
244void QSSGRenderExtension::prepareRender(QSSGFrameData &data)
245{
246 Q_UNUSED(data);
247}
248
249/*!
250 Record the render pass. Depending on the extensions \l {RenderMode}{mode} this function will be called
251 during the frame's prepare or render phase.
252
253 Use \a data to gain access to the render context from which the active QRhi object can be queried.
254
255 \sa QSSGRenderExtension::RenderMode
256*/
257void QSSGRenderExtension::render(QSSGFrameData &data)
258{
259 Q_UNUSED(data);
260}
261
262/*!
263 Called each time a new frame starts. Any data from the previous frame should be cleared at
264 this point.
265*/
266void QSSGRenderExtension::resetForFrame()
267{
268
269}
270
271/*!
272 \return The render mode used for this extension.
273 */
274QSSGRenderExtension::RenderMode QSSGRenderExtension::mode() const
275{
276 return RenderMode::Main;
277}
278
279/*!
280 \return The stage in which this render extension will be used.
281*/
282QSSGRenderExtension::RenderStage QSSGRenderExtension::stage() const
283{
284 return RenderStage::PostColor;
285}
286
287/*!
288 \class QSSGRenderTextureProviderExtension
289 \inmodule QtQuick3D
290 \since 6.11
291
292 \brief Base class for texture providers backend node implementations.
293
294 \note This class is meant to be used together with \l QQuick3DTextureProviderExtension.
295 and the \l mode and \l stage is always \c Standalone and \c PostColor respectively.
296
297 \sa QQuick3DTextureProviderExtension
298*/
299
300
301QSSGRenderTextureProviderExtension::QSSGRenderTextureProviderExtension()
302 : QSSGRenderExtension(QSSGRenderGraphObject::Type::TextureProvider, FlagT(Flags::HasGraphicsResources))
303{
304
305}
306
307QSSGRenderTextureProviderExtension::~QSSGRenderTextureProviderExtension()
308{
309
310}
311
312QSSGRenderExtension::RenderStage QSSGRenderTextureProviderExtension::stage() const { return QSSGRenderExtension::RenderStage::PreColor; }
313
314QSSGRenderExtension::RenderMode QSSGRenderTextureProviderExtension::mode() const { return QSSGRenderExtension::RenderMode::Standalone; }
315
316QT_END_NAMESPACE