2 Copyright (c) 2012 Advanced Micro Devices, Inc.
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
14 //Originally written by Takahiro Harada
22 ///////////////////////////////////////
24 ///////////////////////////////////////
27 _MEM_CLASSALIGN16 struct
29 _MEM_ALIGNED_ALLOCATOR16;
37 Matrix3x3 mtIdentity();
40 Matrix3x3 mtDiagonal(float a, float b, float c);
43 Matrix3x3 mtTranspose(const Matrix3x3& m);
46 Matrix3x3 mtMul(const Matrix3x3& a, const Matrix3x3& b);
49 float4 mtMul1(const Matrix3x3& a, const float4& b);
52 Matrix3x3 mtMul2(float a, const Matrix3x3& b);
55 float4 mtMul3(const float4& b, const Matrix3x3& a);
58 Matrix3x3 mtInvert(const Matrix3x3& m);
64 m.m_row[0] = make_float4(0.f);
65 m.m_row[1] = make_float4(0.f);
66 m.m_row[2] = make_float4(0.f);
71 Matrix3x3 mtIdentity()
74 m.m_row[0] = make_float4(1,0,0);
75 m.m_row[1] = make_float4(0,1,0);
76 m.m_row[2] = make_float4(0,0,1);
81 Matrix3x3 mtDiagonal(float a, float b, float c)
84 m.m_row[0] = make_float4(a,0,0);
85 m.m_row[1] = make_float4(0,b,0);
86 m.m_row[2] = make_float4(0,0,c);
91 Matrix3x3 mtTranspose(const Matrix3x3& m)
94 out.m_row[0] = make_float4(m.m_row[0].s[0], m.m_row[1].s[0], m.m_row[2].s[0], 0.f);
95 out.m_row[1] = make_float4(m.m_row[0].s[1], m.m_row[1].s[1], m.m_row[2].s[1], 0.f);
96 out.m_row[2] = make_float4(m.m_row[0].s[2], m.m_row[1].s[2], m.m_row[2].s[2], 0.f);
101 Matrix3x3 mtMul(const Matrix3x3& a, const Matrix3x3& b)
104 transB = mtTranspose( b );
106 for(int i=0; i<3; i++)
108 ans.m_row[i].s[0] = dot3F4(a.m_row[i],transB.m_row[0]);
109 ans.m_row[i].s[1] = dot3F4(a.m_row[i],transB.m_row[1]);
110 ans.m_row[i].s[2] = dot3F4(a.m_row[i],transB.m_row[2]);
116 float4 mtMul1(const Matrix3x3& a, const float4& b)
119 ans.s[0] = dot3F4( a.m_row[0], b );
120 ans.s[1] = dot3F4( a.m_row[1], b );
121 ans.s[2] = dot3F4( a.m_row[2], b );
126 Matrix3x3 mtMul2(float a, const Matrix3x3& b)
129 ans.m_row[0] = a*b.m_row[0];
130 ans.m_row[1] = a*b.m_row[1];
131 ans.m_row[2] = a*b.m_row[2];
136 float4 mtMul3(const float4& a, const Matrix3x3& b)
139 ans.x = a.x*b.m_row[0].x + a.y*b.m_row[1].x + a.z*b.m_row[2].x;
140 ans.y = a.x*b.m_row[0].y + a.y*b.m_row[1].y + a.z*b.m_row[2].y;
141 ans.z = a.x*b.m_row[0].z + a.y*b.m_row[1].z + a.z*b.m_row[2].z;
146 Matrix3x3 mtInvert(const Matrix3x3& m)
148 float det = m.m_row[0].s[0]*m.m_row[1].s[1]*m.m_row[2].s[2]+m.m_row[1].s[0]*m.m_row[2].s[1]*m.m_row[0].s[2]+m.m_row[2].s[0]*m.m_row[0].s[1]*m.m_row[1].s[2]
149 -m.m_row[0].s[0]*m.m_row[2].s[1]*m.m_row[1].s[2]-m.m_row[2].s[0]*m.m_row[1].s[1]*m.m_row[0].s[2]-m.m_row[1].s[0]*m.m_row[0].s[1]*m.m_row[2].s[2];
154 ans.m_row[0].s[0] = m.m_row[1].s[1]*m.m_row[2].s[2] - m.m_row[1].s[2]*m.m_row[2].s[1];
155 ans.m_row[0].s[1] = m.m_row[0].s[2]*m.m_row[2].s[1] - m.m_row[0].s[1]*m.m_row[2].s[2];
156 ans.m_row[0].s[2] = m.m_row[0].s[1]*m.m_row[1].s[2] - m.m_row[0].s[2]*m.m_row[1].s[1];
157 ans.m_row[0].w = 0.f;
159 ans.m_row[1].s[0] = m.m_row[1].s[2]*m.m_row[2].s[0] - m.m_row[1].s[0]*m.m_row[2].s[2];
160 ans.m_row[1].s[1] = m.m_row[0].s[0]*m.m_row[2].s[2] - m.m_row[0].s[2]*m.m_row[2].s[0];
161 ans.m_row[1].s[2] = m.m_row[0].s[2]*m.m_row[1].s[0] - m.m_row[0].s[0]*m.m_row[1].s[2];
162 ans.m_row[1].w = 0.f;
164 ans.m_row[2].s[0] = m.m_row[1].s[0]*m.m_row[2].s[1] - m.m_row[1].s[1]*m.m_row[2].s[0];
165 ans.m_row[2].s[1] = m.m_row[0].s[1]*m.m_row[2].s[0] - m.m_row[0].s[0]*m.m_row[2].s[1];
166 ans.m_row[2].s[2] = m.m_row[0].s[0]*m.m_row[1].s[1] - m.m_row[0].s[1]*m.m_row[1].s[0];
167 ans.m_row[2].w = 0.f;
169 ans = mtMul2((1.0f/det), ans);
174 Matrix3x3 mtSet( const float4& a, const float4& b, const float4& c )
184 Matrix3x3 operator+(const Matrix3x3& a, const Matrix3x3& b)
187 out.m_row[0] = a.m_row[0] + b.m_row[0];
188 out.m_row[1] = a.m_row[1] + b.m_row[1];
189 out.m_row[2] = a.m_row[2] + b.m_row[2];