summaryrefslogtreecommitdiffstats
path: root/src/render/backend/uniform_p.h
blob: 24bbcd76c8d944dd44319034ada3dc05d23d4837 (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
/****************************************************************************
**
** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QT3DRENDER_RENDER_UNIFORM_P_H
#define QT3DRENDER_RENDER_UNIFORM_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of other Qt classes.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <qt3drender_global.h>
#include <Qt3DCore/qnodeid.h>
#include <Qt3DCore/private/matrix4x4_p.h>
#include <Qt3DCore/private/vector3d_p.h>
#include <Qt3DCore/private/vector4d_p.h>
#include <Qt3DRender/private/qt3drender_global_p.h>
#include <QMatrix4x4>
#include <QVector2D>
#include <QVector3D>
#include <QColor>

#include <QDebug>
#include <string.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {
namespace Render {

enum UniformType {
    Float = 0,
    Vec2,
    Vec3,
    Vec4,
    Double,
    DVec2,
    DVec3,
    DVec4,
    Int,
    IVec2,
    IVec3,
    IVec4,
    UInt,
    UIVec2,
    UIVec3,
    UIVec4,
    Bool,
    BVec2,
    BVec3,
    BVec4,
    Mat2,
    Mat3,
    Mat4,
    Mat2x3,
    Mat3x2,
    Mat2x4,
    Mat4x2,
    Mat3x4,
    Mat4x3,
    Sampler,
    Image,
    Unknown
};

class Q_3DRENDERSHARED_PRIVATE_EXPORT UniformValue
{
public:
    enum ValueType {
        ScalarValue,
        NodeId,
        TextureValue,
        BufferValue,
        ShaderImageValue
    };

    // UniformValue implicitely converts doubles to floats to ensure
    // correct rendering behavior for the cases where Qt3D parameters created from
    // a double or QVariant(double) are used to fill uniform values that in reality
    // should be floats. This occur especially with QML where qreal might be double
    // and not float. Otherwise, at when filling the uniforms, calling constData<float>
    // on something that contains a double will result in wrong values

    UniformValue()
        : m_data(4)
    {
        memset(m_data.data(), 0, m_data.size() * sizeof(float));
    }

    UniformValue(int i) : UniformValue() { data<int>()[0] = i; }
    UniformValue(uint i) : UniformValue() { data<uint>()[0] = i; }
    UniformValue(float f) : UniformValue() { data<float>()[0] = f; }
    UniformValue(double d) : UniformValue() { data<float>()[0] = d; } // Double to float conversion
    UniformValue(bool b) : UniformValue() { data<bool>()[0] = b; }
    UniformValue(const QVector2D &vec2) : UniformValue() { memcpy(m_data.data(), &vec2, sizeof(QVector2D)); }
    UniformValue(const Vector3D &vec3) : UniformValue() { memcpy(m_data.data(), &vec3, sizeof(Vector3D)); }
    UniformValue(const Vector4D &vec4) : m_data(sizeof(Vector4D) / sizeof(float)) { memcpy(m_data.data(), &vec4, sizeof(Vector4D)); }

    UniformValue(const QMatrix3x3 &mat33)
        : m_data(9)
    {
        // Use constData because we want column-major layout
        memcpy(m_data.data(), mat33.constData(), 9 * sizeof(float));
    }

    // We don t want the QMatrix4x4 builder to use sizeof since QMatrix4x4 contains a type flag
#if defined(__SSE2__) || defined(__AVX2__)
    UniformValue(const Matrix4x4 &mat44)
        : m_data(sizeof(Matrix4x4) / sizeof(float))
    {
        // Use constData because we want column-major layout
        memcpy(m_data.data(), &mat44, sizeof(Matrix4x4));
    }
#else
    UniformValue(const QMatrix4x4 &mat44)
        : m_data(16)
    {
        // Use constData because we want column-major layout
        memcpy(m_data.data(), mat44.constData(), 16 * sizeof(float));
    }
#endif

    UniformValue(const QVector<QMatrix4x4> &v)
        : m_data(16 * v.size())
    {
        int offset = 0;
        const int byteSize = 16 * sizeof(float);
        float *data = m_data.data();
        for (const auto &m : v) {
            memcpy(data + offset, m.constData(), byteSize);
            offset += 16;
        }
    }

    // Reserve data to be filled in later
    UniformValue(int byteSize, ValueType valueType)
        : m_data(byteSize / sizeof(float))
        , m_valueType(valueType)
    {
    }

    // For nodes which will later be replaced by a Texture or Buffer
    UniformValue(Qt3DCore::QNodeId id)
        : m_data(sizeof(Qt3DCore::QNodeId) / sizeof(float))
    {
        m_valueType = NodeId;
        memcpy(m_data.data(), &id, sizeof(Qt3DCore::QNodeId));
    }

    ValueType valueType() const { return m_valueType; }
    UniformType storedType() const { return m_storedType; }

    template<typename T>
    void setData(const QVector<T> &v)
    {
        m_elementByteSize = sizeof(T);
        m_data.resize(v.size() * sizeof(T) / sizeof(float));
        m_valueType = ScalarValue;
        float *data = m_data.data();
        memcpy(data, v.constData(), v.size() * sizeof(T));
    }

    static UniformValue fromVariant(const QVariant &variant);

    int byteSize() const { return int(m_data.size()) * sizeof(float); }
    int elementCount() const { return byteSize() / elementByteSize(); }
    int elementByteSize() const { return m_elementByteSize; }

    template<typename T>
    const T *constData() const
    {
        return reinterpret_cast<const T *>(m_data.constData());
    }

    template<typename T>
    T *data()
    {
        return reinterpret_cast<T *>(m_data.data());
    }

    bool operator==(const UniformValue &other) const
    {
        return other.m_data == m_data;
    }

    bool operator!=(const UniformValue &other) const
    {
        return !(*this == other);
    }
private:
    // Allocate 16 floats on stack
    // For larger elements, heap allocation will be used
    QVarLengthArray<float, 16> m_data;

    ValueType m_valueType = ScalarValue;

    // TODO: Replace this hack see QTBUG-57510
    UniformType m_storedType = Unknown;
    int m_elementByteSize = sizeof(float);
};

template<>
Q_3DRENDERSHARED_PRIVATE_EXPORT void UniformValue::setData<QMatrix4x4>(const QVector<QMatrix4x4> &v);

} // namespace Render
} // namespace Qt3DRender

QT_END_NAMESPACE

Q_DECLARE_METATYPE(Qt3DRender::Render::UniformType) // LCOV_EXCL_LINE

#endif // QT3DRENDER_RENDER_UNIFORM_P_H