1// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qeffect.h"
5#include "qeffect_p.h"
6#include "qtechnique.h"
7#include "qparameter.h"
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt3DCore;
12
13namespace Qt3DRender {
14
15QEffectPrivate::QEffectPrivate()
16 : QNodePrivate()
17{
18}
19
20/*!
21 \class Qt3DRender::QEffect
22 \inmodule Qt3DRender
23 \inherits Qt3DCore::QNode
24 \since 5.7
25 \brief The base class for effects in a Qt 3D scene.
26
27 The QEffect class combines a set of techniques and parameters used by those techniques to
28 produce a rendering effect for a material.
29
30 An QEffect instance should be shared among several QMaterial instances when possible.
31
32 \note QEffect node can not be disabled.
33
34 \code
35 QEffect *effect = new QEffect();
36
37 // Create technique, render pass and shader
38 QTechnique *gl3Technique = new QTechnique();
39 QRenderPass *gl3Pass = new QRenderPass();
40 QShaderProgram *glShader = new QShaderProgram();
41
42 // Set the shader on the render pass
43 gl3Pass->setShaderProgram(glShader);
44
45 // Add the pass to the technique
46 gl3Technique->addRenderPass(gl3Pass);
47
48 // Set the targeted GL version for the technique
49 gl3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL);
50 gl3Technique->graphicsApiFilter()->setMajorVersion(3);
51 gl3Technique->graphicsApiFilter()->setMinorVersion(1);
52 gl3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile);
53
54 // Add the technique to the effect
55 effect->addTechnique(gl3Technique);
56 \endcode
57
58 A QParameter defined on a QEffect overrides parameter (of the same
59 name) defined in QTechnique and QRenderPass, but are overridden by parameter in
60 QRenderPassFilter, QTechniqueFilter and QMaterial.
61
62 \sa QMaterial, QTechnique, QParameter
63*/
64
65/*!
66 \qmltype Effect
67 \instantiates Qt3DRender::QEffect
68 \inherits Node
69 \inqmlmodule Qt3D.Render
70 \since 5.7
71 \brief The base class for effects in a Qt 3D scene.
72
73 The Effect type combines a set of techniques and parameters used by those techniques to
74 produce a rendering effect for a material.
75
76 An Effect instance should be shared among several Material instances when possible.
77
78 A Parameter defined on a Effect overrides parameter (of the same
79 name) defined in Technique and RenderPass, but are overridden by parameter in
80 RenderPassFilter, TechniqueFilter and Material.
81
82 \note Effect node can not be disabled.
83
84 \code
85 Effect {
86 id: effect
87
88 techniques: [
89 Technique {
90 id: gl3Technique
91 graphicsApiFilter {
92 api: GraphicsApiFilter.OpenGL
93 profile: GraphicsApiFilter.CoreProfile
94 majorVersion: 3
95 minorVersion: 1
96 }
97 renderPasses: [
98 RenderPass {
99 id: gl3Pass
100 shaderProgram: ShaderProgram {
101 ...
102 }
103 }
104 ]
105 }
106 ]
107 }
108 \endcode
109
110 \sa Material, Technique, Parameter
111*/
112
113QEffect::QEffect(QNode *parent)
114 : QNode(*new QEffectPrivate, parent)
115{
116}
117
118QEffect::~QEffect()
119{
120}
121
122/*! \internal */
123QEffect::QEffect(QEffectPrivate &dd, QNode *parent)
124 : QNode(dd, parent)
125{
126}
127
128/*!
129 \qmlproperty list<Technique> Effect::techniques
130
131 Holds the list of techniques used by this effect.
132*/
133/*!
134 \qmlproperty list<Parameter> Effect::parameters
135
136 Holds the list of parameters used by this effect.
137 A parameter is used to set a corresponding uniform value in the shader used by this effect.
138*/
139
140/*!
141 * Adds \a parameter to the effect. It sends an update to the backend.
142 * The \a parameter will be used to set a corresponding uniform value in the shader used
143 * by this effect.
144 */
145void QEffect::addParameter(QParameter *parameter)
146{
147 Q_D(QEffect);
148 if (parameter && !d->m_parameters.contains(t: parameter)) {
149 d->m_parameters.append(t: parameter);
150
151 // Ensures proper bookkeeping
152 d->registerDestructionHelper(node: parameter, func: &QEffect::removeParameter, d->m_parameters);
153
154 // We need to add it as a child of the current node if it has been declared inline
155 // Or not previously added as a child of the current node so that
156 // 1) The backend gets notified about it's creation
157 // 2) When the current node is destroyed, it gets destroyed as well
158 if (!parameter->parent())
159 parameter->setParent(this);
160
161 d->update();
162 }
163}
164
165/*!
166 * Removes a parameter \a parameter from the effect.
167 */
168void QEffect::removeParameter(QParameter *parameter)
169{
170 Q_D(QEffect);
171
172 if (!d->m_parameters.removeOne(t: parameter))
173 return;
174 // Remove bookkeeping connection
175 d->unregisterDestructionHelper(node: parameter);
176 d->update();
177}
178
179/*!
180 * Returns the list of parameters used by the effect.
181 */
182QList<QParameter *> QEffect::parameters() const
183{
184 Q_D(const QEffect);
185 return d->m_parameters;
186}
187
188/*!
189 * Adds a new technique \a t to the effect. It sends an update to the backend.
190 */
191void QEffect::addTechnique(QTechnique *t)
192{
193 Q_ASSERT(t);
194 Q_D(QEffect);
195 if (t && !d->m_techniques.contains(t)) {
196 d->m_techniques.append(t);
197
198 // Ensures proper bookkeeping
199 d->registerDestructionHelper(node: t, func: &QEffect::removeTechnique, d->m_techniques);
200
201 // We need to add it as a child of the current node if it has been declared inline
202 // Or not previously added as a child of the current node so that
203 // 1) The backend gets notified about it's creation
204 // 2) When the current node is destroyed, tit gets destroyed as well
205 if (!t->parent())
206 t->setParent(this);
207
208 d->update();
209 }
210}
211
212/*!
213 * Removes a technique \a t from the effect.
214 */
215void QEffect::removeTechnique(QTechnique *t)
216{
217 Q_D(QEffect);
218 if (!d->m_techniques.removeOne(t))
219 return;
220 d->update();
221 // Remove bookkeeping connection
222 d->unregisterDestructionHelper(node: t);
223}
224
225/*!
226 * Returns the list of techniques used by the effect.
227 */
228QList<QTechnique *> QEffect::techniques() const
229{
230 Q_D(const QEffect);
231 return d->m_techniques;
232}
233
234} // namespace Qt3DRender
235
236QT_END_NAMESPACE
237
238#include "moc_qeffect.cpp"
239

source code of qt3d/src/render/materialsystem/qeffect.cpp