1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 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 <private/qsimd_p.h>
41 
42 #if defined(__AVX2__) && defined(QT_COMPILER_SUPPORTS_AVX2)
43 #include "matrix4x4_avx2_p.h"
44 #else
45 #include "matrix4x4_sse_p.h"
46 #endif
47 
48 #include "vector3d_sse_p.h"
49 #include "vector4d_sse_p.h"
50 #include <QDebug>
51 
52 #ifdef QT_COMPILER_SUPPORTS_SSE2
53 
54 QT_BEGIN_NAMESPACE
55 
56 namespace Qt3DCore {
57 
operator <<(QDebug dbg,const Vector3D_SSE & v)58 QDebug operator<<(QDebug dbg, const Vector3D_SSE &v)
59 {
60     dbg.nospace() << "Vector3D_SSE(" << v.x() << ", " << v.y() << ", " << v.z() << ") ";
61     return dbg;
62 }
63 
Vector3D_SSE(const Vector4D_SSE & v)64 Vector3D_SSE::Vector3D_SSE(const Vector4D_SSE &v)
65 {
66     m_xyzw = _mm_mul_ps(v.m_xyzw, _mm_set_ps(0.0f, 1.0f, 1.0f, 1.0f));
67 }
68 
69 #if defined(__AVX2__) && defined(QT_COMPILER_SUPPORTS_AVX2)
70 
unproject(const Matrix4x4_AVX2 & modelView,const Matrix4x4_AVX2 & projection,const QRect & viewport) const71 Vector3D_SSE Vector3D_SSE::unproject(const Matrix4x4_AVX2 &modelView, const Matrix4x4_AVX2 &projection, const QRect &viewport) const
72 {
73     const Matrix4x4_AVX2 inverse = (projection * modelView).inverted();
74 
75     Vector4D_SSE tmp(*this, 1.0f);
76     tmp.setX((tmp.x() - float(viewport.x())) / float(viewport.width()));
77     tmp.setY((tmp.y() - float(viewport.y())) / float(viewport.height()));
78     tmp = tmp * 2.0f - Vector4D_SSE(1.0f, 1.0f, 1.0f, 1.0f);
79 
80     Vector4D_SSE obj = inverse * tmp;
81     if (qFuzzyIsNull(obj.w()))
82         obj.setW(1.0f);
83     obj /= obj.w();
84     return Vector3D_SSE(obj);
85 }
86 
project(const Matrix4x4_AVX2 & modelView,const Matrix4x4_AVX2 & projection,const QRect & viewport) const87 Vector3D_SSE Vector3D_SSE::project(const Matrix4x4_AVX2 &modelView, const Matrix4x4_AVX2 &projection, const QRect &viewport) const
88 {
89     Vector4D_SSE tmp(*this, 1.0f);
90     tmp = projection * modelView * tmp;
91     if (qFuzzyIsNull(tmp.w()))
92         tmp.setW(1.0f);
93     tmp /= tmp.w();
94 
95     tmp = tmp * 0.5f + Vector4D_SSE(0.5f, 0.5f, 0.5f, 0.5f);
96     tmp.setX(tmp.x() * viewport.width() + viewport.x());
97     tmp.setY(tmp.y() * viewport.height() + viewport.y());
98 
99     return Vector3D_SSE(tmp);
100 }
101 
102 #else
103 
unproject(const Matrix4x4_SSE & modelView,const Matrix4x4_SSE & projection,const QRect & viewport) const104 Vector3D_SSE Vector3D_SSE::unproject(const Matrix4x4_SSE &modelView, const Matrix4x4_SSE &projection, const QRect &viewport) const
105 {
106     const Matrix4x4_SSE inverse = (projection * modelView).inverted();
107 
108     Vector4D_SSE tmp(*this, 1.0f);
109     tmp.setX((tmp.x() - float(viewport.x())) / float(viewport.width()));
110     tmp.setY((tmp.y() - float(viewport.y())) / float(viewport.height()));
111     tmp = tmp * 2.0f - Vector4D_SSE(1.0f, 1.0f, 1.0f, 1.0f);
112 
113     Vector4D_SSE obj = inverse * tmp;
114     if (qFuzzyIsNull(obj.w()))
115         obj.setW(1.0f);
116     obj /= obj.w();
117     return Vector3D_SSE(obj);
118 }
119 
project(const Matrix4x4_SSE & modelView,const Matrix4x4_SSE & projection,const QRect & viewport) const120 Vector3D_SSE Vector3D_SSE::project(const Matrix4x4_SSE &modelView, const Matrix4x4_SSE &projection, const QRect &viewport) const
121 {
122     Vector4D_SSE tmp(*this, 1.0f);
123     tmp = projection * modelView * tmp;
124     if (qFuzzyIsNull(tmp.w()))
125         tmp.setW(1.0f);
126     tmp /= tmp.w();
127 
128     tmp = tmp * 0.5f + Vector4D_SSE(0.5f, 0.5f, 0.5f, 0.5f);
129     tmp.setX(tmp.x() * viewport.width() + viewport.x());
130     tmp.setY(tmp.y() * viewport.height() + viewport.y());
131 
132     return Vector3D_SSE(tmp);
133 }
134 
135 #endif
136 
137 } // Qt3DCore
138 
139 QT_END_NAMESPACE
140 
141 #endif // QT_COMPILER_SUPPORTS_SSE2
142