1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "buffer_p.h"
41#include <Qt3DCore/qpropertyupdatedchange.h>
42#include <Qt3DRender/private/buffermanager_p.h>
43#include <Qt3DRender/private/qbuffer_p.h>
44
45QT_BEGIN_NAMESPACE
46
47using namespace Qt3DCore;
48
49namespace Qt3DRender {
50namespace Render {
51
52Buffer::Buffer()
53 : BackendNode(QBackendNode::ReadWrite)
54 , m_usage(QBuffer::StaticDraw)
55 , m_bufferDirty(false)
56 , m_syncData(false)
57 , m_access(QBuffer::Write)
58 , m_manager(nullptr)
59{
60 // Maybe it could become read write if we want to inform
61 // the frontend QBuffer node of any backend issue
62}
63
64Buffer::~Buffer()
65{
66}
67
68void Buffer::cleanup()
69{
70 m_usage = QBuffer::StaticDraw;
71 m_data.clear();
72 m_bufferUpdates.clear();
73 m_functor.reset();
74 m_bufferDirty = false;
75 m_syncData = false;
76 m_access = QBuffer::Write;
77}
78
79
80void Buffer::setManager(BufferManager *manager)
81{
82 m_manager = manager;
83}
84
85void Buffer::executeFunctor()
86{
87 Q_ASSERT(m_functor);
88 m_data = (*m_functor)();
89 // Request data to be loaded
90 forceDataUpload();
91}
92
93//Called from th sendBufferJob
94void Buffer::updateDataFromGPUToCPU(QByteArray data)
95{
96 // Note: when this is called, data is what's currently in GPU memory
97 // so m_data shouldn't be reuploaded
98 m_data = data;
99}
100
101void Buffer::forceDataUpload()
102{
103 // We push back an update with offset = -1
104 // As this is the way to force data to be loaded
105 QBufferUpdate updateNewData;
106 updateNewData.offset = -1;
107 m_bufferUpdates.clear(); //previous updates are pointless
108 m_bufferUpdates.push_back(t: updateNewData);
109}
110
111void Buffer::syncFromFrontEnd(const QNode *frontEnd, bool firstTime)
112{
113 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
114 const QBuffer *node = qobject_cast<const QBuffer *>(object: frontEnd);
115 if (!node)
116 return;
117
118 if (firstTime && m_manager != nullptr) {
119 m_manager->addBufferReference(bufferId: peerId());
120 m_bufferDirty = true;
121 }
122
123 m_syncData = node->isSyncData();
124 m_access = node->accessType();
125 if (m_usage != node->usage()) {
126 m_usage = node->usage();
127 m_bufferDirty = true;
128 }
129 {
130 QBufferDataGeneratorPtr newGenerator = node->dataGenerator();
131 bool dirty = (newGenerator && m_functor && !(*newGenerator == *m_functor)) ||
132 (newGenerator.isNull() && !m_functor.isNull()) ||
133 (!newGenerator.isNull() && m_functor.isNull());
134 m_bufferDirty |= dirty;
135 m_functor = newGenerator;
136 if (m_functor && m_manager != nullptr)
137 m_manager->addDirtyBuffer(bufferId: peerId());
138 }
139 {
140 const QVariant v = node->property(name: QBufferPrivate::UpdateDataPropertyName);
141
142 // Make sure we record data if it's the first time we are called
143 // or if we have no partial updates
144 if (firstTime || !v.isValid()){
145 const QByteArray newData = node->data();
146 const bool dirty = m_data != newData;
147 m_bufferDirty |= dirty;
148 m_data = newData;
149
150 // Since frontend applies partial updates to its m_data
151 // if we enter this code block, there's no problem in actually
152 // ignoring the partial updates
153 if (v.isValid())
154 const_cast<QBuffer *>(node)->setProperty(name: QBufferPrivate::UpdateDataPropertyName, value: {});
155
156 if (dirty && !m_data.isEmpty())
157 forceDataUpload();
158 } else if (v.isValid()) {
159 // Apply partial updates and record them to allow partial upload to the GPU
160 const QVariantList updateList = v.toList();
161 for (const QVariant &update : updateList) {
162 Qt3DRender::QBufferUpdate updateData = update.value<Qt3DRender::QBufferUpdate>();
163 m_data.replace(index: updateData.offset, len: updateData.data.size(), s: updateData.data);
164 m_bufferUpdates.push_back(t: updateData);
165 m_bufferDirty = true;
166 }
167
168 const_cast<QBuffer *>(node)->setProperty(name: QBufferPrivate::UpdateDataPropertyName, value: {});
169 }
170 }
171 markDirty(changes: AbstractRenderer::BuffersDirty);
172}
173
174// Called by Renderer once the buffer has been uploaded to OpenGL
175void Buffer::unsetDirty()
176{
177 m_bufferDirty = false;
178}
179
180BufferFunctor::BufferFunctor(AbstractRenderer *renderer, BufferManager *manager)
181 : m_manager(manager)
182 , m_renderer(renderer)
183{
184}
185
186Qt3DCore::QBackendNode *BufferFunctor::create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const
187{
188 Buffer *buffer = m_manager->getOrCreateResource(id: change->subjectId());
189 buffer->setManager(m_manager);
190 buffer->setRenderer(m_renderer);
191 return buffer;
192}
193
194Qt3DCore::QBackendNode *BufferFunctor::get(Qt3DCore::QNodeId id) const
195{
196 return m_manager->lookupResource(id);
197}
198
199void BufferFunctor::destroy(Qt3DCore::QNodeId id) const
200{
201 m_manager->removeBufferReference(bufferId: id);
202 return m_manager->releaseResource(id);
203}
204
205} // namespace Render
206} // namespace Qt3DRender
207
208QT_END_NAMESPACE
209

source code of qt3d/src/render/geometry/buffer.cpp