summaryrefslogtreecommitdiffstats
path: root/src/render/materialsystem/shaderdata.cpp
blob: ad6dbfff703ad4eb593d89dc772984451d33988c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "shaderdata_p.h"
#include "qshaderdata.h"
#include "qshaderdata_p.h"
#include <QMetaProperty>
#include <QMetaObject>
#include <private/qbackendnode_p.h>
#include <private/managers_p.h>
#include <private/nodemanagers_p.h>
#include <Qt3DRender/private/stringtoint_p.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {
namespace Render {

using namespace Qt3DCore;

namespace {

const int qNodeIdTypeId = qMetaTypeId<Qt3DCore::QNodeId>();

}

ShaderData::ShaderData()
    : m_managers(nullptr)
{
}

ShaderData::~ShaderData()
{
}

void ShaderData::setManagers(NodeManagers *managers)
{
    m_managers = managers;
}

void ShaderData::syncFromFrontEnd(const QNode *frontEnd, bool firstTime)
{
    const QShaderData *node = qobject_cast<const QShaderData *>(frontEnd);
    if (!node)
        return;
    BackendNode::syncFromFrontEnd(frontEnd, firstTime);

    if (firstTime) {
        m_propertyReader = node->propertyReader();
        m_blockNameToPropertyValues.clear();

        const QMetaObject *metaObj = node->metaObject();
        const int propertyOffset = QShaderData::staticMetaObject.propertyOffset();
        const int propertyCount = metaObj->propertyCount();
        // Dynamic properties names
        const auto dynamicPropertyNames = node->dynamicPropertyNames();

        QList<QString> propertyNames;
        propertyNames.reserve(propertyCount - propertyOffset + dynamicPropertyNames.size());

        // Statiically defined properties
        for (int i = propertyOffset; i < propertyCount; ++i) {
            const QMetaProperty pro = metaObj->property(i);
            if (pro.isWritable())
                propertyNames.push_back(QString::fromLatin1(pro.name()));
        }
        // Dynamic properties
        for (const QByteArray &propertyName : dynamicPropertyNames)
            propertyNames.push_back(QString::fromLatin1(propertyName));

        for (const QString &propertyName : propertyNames) {
            if (propertyName == QStringLiteral("data") ||
                    propertyName == QStringLiteral("objectName") ||
                    propertyName == QStringLiteral("childNodes")) // We don't handle default Node properties
                continue;

            const QVariant &propertyValue = m_propertyReader->readProperty(node->property(propertyName.toLatin1()));
            bool isNode = false;
            bool isTransformed = false;
            bool isArray = false;

            // We check if the property is a QNodeId
            isNode = (propertyValue.userType() == qNodeIdTypeId);
            // We check if QList<QNodeId>
            if (propertyValue.userType() == QMetaType::QVariantList) {
                isArray = true;
                QVariantList list = propertyValue.value<QVariantList>();
                if (list.size() > 0 && list.at(0).userType() == qNodeIdTypeId)
                    isNode = true;
            }

            // We check if property is a Transformed property
            QString transformedPropertyName;
            if (propertyValue.userType() == QMetaType::QVector3D) {
                // if there is a matching QShaderData::TransformType propertyTransformed
                transformedPropertyName = propertyName + QLatin1String("Transformed");
                isTransformed = propertyNames.contains(transformedPropertyName);
                if (!isTransformed)
                    transformedPropertyName.clear();
            }
            m_originalProperties.insert(propertyName, { propertyValue, isNode, isArray, isTransformed, transformedPropertyName });
        }
        BackendNode::markDirty(AbstractRenderer::ParameterDirty);
    } else {
        // Updates
        if (!m_propertyReader.isNull()) {
            auto it = m_originalProperties.begin();
            const auto end = m_originalProperties.end();

            while (it != end) {
                const QVariant newValue = m_propertyReader->readProperty(node->property(it.key().toLatin1()));
                PropertyValue &propValue = it.value();
                if (propValue.value != newValue) {
                    // Note we aren't notified about nested QShaderData in this call
                    // only scalar / vec properties
                    propValue.value = newValue;
                    BackendNode::markDirty(AbstractRenderer::ParameterDirty);
                }
                ++it;
            }
        }
    }
}

const ShaderData::PropertyValuesForBlock &ShaderData::propertyValuesForBlock(int blockName) const
{
    std::shared_lock readLocker(m_lock);
    return m_blockNameToPropertyValues.at(blockName);
}

void ShaderData::generatePropertyValuesForBlockIfNeeded(const QString &fullBlockName)
{
    const int fullBlockNameId = StringToInt::lookupId(fullBlockName);

    std::unique_lock readWriteLocker(m_lock);
    const bool hasPropertyValuesForBlock = m_blockNameToPropertyValues.find(fullBlockNameId) != m_blockNameToPropertyValues.cend();
    if (hasPropertyValuesForBlock) {
        return;
    }

    const QHash<QString, ShaderData::PropertyValue> &props = properties();

    ShaderData::PropertyValuesForBlock valueBlock;
    valueBlock.reserve(props.size());

    auto it = props.cbegin();
    const auto end = props.cend();
    while (it != end) {
        QString propertyName = it.key();
        // If we are dealing with a nested value, check if it is an an array
        if (it->isArray && !it->isNode)
            propertyName += QLatin1String("[0]");

        QString fullPropertyName;
        fullPropertyName.reserve(fullBlockName.size() + 1 + it.key().size());
        fullPropertyName.append(fullBlockName);
        fullPropertyName.append(QLatin1String("."));
        fullPropertyName.append(propertyName);

        // We only do this for properties on root level
        valueBlock.push_back({ StringToInt::lookupId(fullPropertyName),
                               StringToInt::lookupId(propertyName),
                               it.operator ->() });
        ++it;
    }

    m_blockNameToPropertyValues[StringToInt::lookupId(fullBlockName)] = std::move(valueBlock);
}

ShaderData *ShaderData::lookupResource(NodeManagers *managers, QNodeId id)
{
    return managers->shaderDataManager()->lookupResource(id);
}

ShaderData *ShaderData::lookupResource(QNodeId id)
{
    return ShaderData::lookupResource(m_managers, id);
}

// RenderCommand updater jobs
QVariant ShaderData::getTransformedProperty(const PropertyValue *v, const Matrix4x4 &viewMatrix) const noexcept
{
    // Note protecting m_worldMatrix at this point as we assume all world updates
    // have been performed when reaching this point
    if (v->isTransformed) {
        const auto transformedIt = m_originalProperties.constFind(v->transformedPropertyName);
        if (transformedIt != m_originalProperties.constEnd()) {
            const PropertyValue &transformedValue = transformedIt.value();
            const TransformType transformType = static_cast<TransformType>(transformedValue.value.toInt());
            switch (transformType) {
            case ModelToEye:
                return QVariant::fromValue(viewMatrix.map(m_worldMatrix.map(Vector3D(v->value.value<QVector3D>()))));
            case ModelToWorld:
                return QVariant::fromValue(m_worldMatrix.map(Vector3D(v->value.value<QVector3D>())));
            case ModelToWorldDirection:
                return QVariant::fromValue(Vector3D(m_worldMatrix * Vector4D(v->value.value<QVector3D>(), 0.0f)));
            case NoTransform:
                break;
            }
        }
    }
    return v->value;
}

// Unit tests only
ShaderData::TransformType ShaderData::propertyTransformType(const QString &name) const
{
    const auto it = m_originalProperties.constFind(name);
    if (it != m_originalProperties.constEnd()) {
        const PropertyValue &propertyValue = it.value();
        if (propertyValue.isTransformed) {
            auto transformedIt = m_originalProperties.constFind(name + QLatin1String("Transformed"));
            if (transformedIt != m_originalProperties.end())
                return static_cast<TransformType>(transformedIt.value().value.toInt());
        }
    }
    return NoTransform;
}

// Called by FramePreparationJob or by RenderView when dealing with lights
void ShaderData::updateWorldTransform(const Matrix4x4 &worldMatrix)
{
    if (m_worldMatrix != worldMatrix) {
        m_worldMatrix = worldMatrix;
    }
}

RenderShaderDataFunctor::RenderShaderDataFunctor(AbstractRenderer *renderer, NodeManagers *managers)
    : m_managers(managers)
    , m_renderer(renderer)
{
}

Qt3DCore::QBackendNode *RenderShaderDataFunctor::create(Qt3DCore::QNodeId id) const
{
    ShaderData *backend = m_managers->shaderDataManager()->getOrCreateResource(id);
    backend->setManagers(m_managers);
    backend->setRenderer(m_renderer);
    return backend;
}

Qt3DCore::QBackendNode *RenderShaderDataFunctor::get(Qt3DCore::QNodeId id) const
{
    return m_managers->shaderDataManager()->lookupResource(id);
}

void RenderShaderDataFunctor::destroy(Qt3DCore::QNodeId id) const
{
    m_managers->shaderDataManager()->releaseResource(id);
}

} // namespace Render
} // namespace Qt3DRender

QT_END_NAMESPACE