1/****************************************************************************
2**
3** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
4** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the Qt3D module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "renderpass_p.h"
42#include <Qt3DRender/private/filterkey_p.h>
43#include <Qt3DRender/qrenderstate.h>
44#include <Qt3DRender/qrenderpass.h>
45#include <Qt3DRender/qparameter.h>
46
47#include <Qt3DRender/private/qrenderpass_p.h>
48#include <Qt3DRender/private/renderstates_p.h>
49#include <Qt3DRender/private/renderstateset_p.h>
50
51#include <Qt3DCore/qpropertyupdatedchange.h>
52#include <Qt3DCore/qpropertynodeaddedchange.h>
53#include <Qt3DCore/qpropertynoderemovedchange.h>
54
55QT_BEGIN_NAMESPACE
56
57using namespace Qt3DCore;
58
59namespace Qt3DRender {
60namespace Render {
61
62RenderPass::RenderPass()
63 : BackendNode()
64{
65}
66
67RenderPass::~RenderPass()
68{
69}
70
71void RenderPass::cleanup()
72{
73 QBackendNode::setEnabled(false);
74 m_renderStates.clear();
75 m_filterKeyList.clear();
76 m_parameterPack.clear();
77 m_shaderUuid = Qt3DCore::QNodeId();
78}
79
80void RenderPass::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change)
81{
82 const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QRenderPassData>>(change);
83 const auto &data = typedChange->data;
84 m_filterKeyList = data.filterKeyIds;
85 m_parameterPack.setParameters(data.parameterIds);
86 for (const auto &renderStateId : qAsConst(data.renderStateIds))
87 addRenderState(renderStateId);
88 m_shaderUuid = data.shaderId;
89}
90
91void RenderPass::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e)
92{
93 switch (e->type()) {
94 case PropertyValueAdded: {
95 const auto change = qSharedPointerCast<QPropertyNodeAddedChange>(e);
96 if (change->propertyName() == QByteArrayLiteral("filterKeys"))
97 appendFilterKey(change->addedNodeId());
98 else if (change->propertyName() == QByteArrayLiteral("shaderProgram"))
99 m_shaderUuid = change->addedNodeId();
100 else if (change->propertyName() == QByteArrayLiteral("renderState"))
101 addRenderState(change->addedNodeId());
102 else if (change->propertyName() == QByteArrayLiteral("parameter"))
103 m_parameterPack.appendParameter(change->addedNodeId());
104 break;
105 }
106
107 case PropertyUpdated: {
108 const auto change = qSharedPointerCast<QPropertyUpdatedChange>(e);
109 if (change->propertyName() == QByteArrayLiteral("shaderProgram"))
110 m_shaderUuid = change->value().value<Qt3DCore::QNodeId>();
111 break;
112 }
113
114 case PropertyValueRemoved: {
115 const auto change = qSharedPointerCast<QPropertyNodeRemovedChange>(e);
116 if (change->propertyName() == QByteArrayLiteral("filterKeys"))
117 removeFilterKey(change->removedNodeId());
118 else if (change->propertyName() == QByteArrayLiteral("shaderProgram"))
119 m_shaderUuid = QNodeId();
120 else if (change->propertyName() == QByteArrayLiteral("renderState"))
121 removeRenderState(change->removedNodeId());
122 else if (change->propertyName() == QByteArrayLiteral("parameter"))
123 m_parameterPack.removeParameter(change->removedNodeId());
124 break;
125 }
126
127 default:
128 break;
129 }
130
131 BackendNode::sceneChangeEvent(e);
132 markDirty(AbstractRenderer::AllDirty);
133}
134
135Qt3DCore::QNodeId RenderPass::shaderProgram() const
136{
137 return m_shaderUuid;
138}
139
140QVector<Qt3DCore::QNodeId> RenderPass::filterKeys() const
141{
142 return m_filterKeyList;
143}
144
145QVector<Qt3DCore::QNodeId> RenderPass::parameters() const
146{
147 return m_parameterPack.parameters();
148}
149
150QVector<QNodeId> RenderPass::renderStates() const
151{
152 return m_renderStates;
153}
154
155void RenderPass::appendFilterKey(Qt3DCore::QNodeId filterKeyId)
156{
157 if (!m_filterKeyList.contains(filterKeyId))
158 m_filterKeyList.append(filterKeyId);
159}
160
161void RenderPass::removeFilterKey(Qt3DCore::QNodeId filterKeyId)
162{
163 m_filterKeyList.removeOne(filterKeyId);
164}
165
166void RenderPass::addRenderState(QNodeId renderStateId)
167{
168 if (!m_renderStates.contains(renderStateId))
169 m_renderStates.push_back(renderStateId);
170}
171
172void RenderPass::removeRenderState(QNodeId renderStateId)
173{
174 m_renderStates.removeOne(renderStateId);
175}
176
177
178} // namespace Render
179} // namespace Qt3DRender
180
181QT_END_NAMESPACE
182