a6e34e9eb2b3b33fa52643c8086f2e89246b2dad
[mesa.git] / src / mesa / math / m_xform.c
1 /* $Id: m_xform.c,v 1.4 2000/11/24 10:25:11 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * Matrix/vertex/vector transformation stuff
30 *
31 *
32 * NOTES:
33 * 1. 4x4 transformation matrices are stored in memory in column major order.
34 * 2. Points/vertices are to be thought of as column vectors.
35 * 3. Transformation of a point p by a matrix M is: p' = M * p
36 */
37
38 #include <math.h>
39
40 #include "glheader.h"
41 #include "macros.h"
42 #include "mmath.h"
43
44 #include "m_matrix.h"
45 #include "m_translate.h"
46 #include "m_xform.h"
47
48
49 #ifdef DEBUG
50 #include "m_debug_xform.h"
51 #endif
52
53 #ifdef USE_X86_ASM
54 #include "X86/common_x86_asm.h"
55 #endif
56
57 clip_func gl_clip_tab[5];
58 dotprod_func gl_dotprod_tab[2][5];
59 vec_copy_func gl_copy_tab[2][0x10];
60 normal_func gl_normal_tab[0xf][0x4];
61 transform_func **(gl_transform_tab[2]);
62 static transform_func *cull_transform_tab[5];
63 static transform_func *raw_transform_tab[5];
64
65
66 /* Raw data format used for:
67 * - Object-to-eye transform prior to culling, although this too
68 * could be culled under some circumstances.
69 * - Eye-to-clip transform (via the function above).
70 * - Cliptesting
71 * - And everything else too, if culling happens to be disabled.
72 */
73 #define TAG(x) x##_raw
74 #define TAG2(x,y) x##y##_raw
75 #define IDX 0
76 #define STRIDE_LOOP for (i=0;i<count;i++, STRIDE_F(from, stride))
77 #define LOOP for (i=0;i<n;i++)
78 #define CULL_CHECK
79 #define CLIP_CHECK
80 #define ARGS
81 #include "m_xform_tmp.h"
82 #include "m_clip_tmp.h"
83 #include "m_norm_tmp.h"
84 #include "m_dotprod_tmp.h"
85 #include "m_copy_tmp.h"
86 #undef TAG
87 #undef TAG2
88 #undef LOOP
89 #undef CULL_CHECK
90 #undef CLIP_CHECK
91 #undef ARGS
92 #undef IDX
93
94 /* Culled data used for:
95 * - texture transformations
96 * - viewport map transformation
97 * - normal transformations prior to lighting
98 * - user cliptests
99 */
100 #define TAG(x) x##_masked
101 #define TAG2(x,y) x##y##_masked
102 #define IDX 1
103 #define STRIDE_LOOP for (i=0;i<count;i++, STRIDE_F(from, stride))
104 #define LOOP for (i=0;i<n;i++)
105 #define CULL_CHECK if (mask[i])
106 #define CLIP_CHECK if ((mask[i] & flag) == 0)
107 #define ARGS , const GLubyte mask[]
108 #include "m_xform_tmp.h"
109 #include "m_norm_tmp.h"
110 #include "m_dotprod_tmp.h"
111 #include "m_copy_tmp.h"
112 #undef TAG
113 #undef TAG2
114 #undef LOOP
115 #undef CULL_CHECK
116 #undef CLIP_CHECK
117 #undef ARGS
118 #undef IDX
119
120
121
122
123
124
125 GLvector4f *gl_project_points( GLvector4f *proj_vec,
126 const GLvector4f *clip_vec )
127 {
128 const GLuint stride = clip_vec->stride;
129 const GLfloat *from = (GLfloat *)clip_vec->start;
130 const GLuint count = clip_vec->count;
131 GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start;
132 GLuint i;
133
134 for (i = 0 ; i < count ; i++, STRIDE_F(from, stride))
135 {
136 GLfloat oow = 1.0F / from[3];
137 vProj[i][3] = oow;
138 vProj[i][0] = from[0] * oow;
139 vProj[i][1] = from[1] * oow;
140 vProj[i][2] = from[2] * oow;
141 }
142
143 proj_vec->flags |= VEC_SIZE_4;
144 proj_vec->size = 3;
145 proj_vec->count = clip_vec->count;
146 return proj_vec;
147 }
148
149
150
151
152
153
154 /*
155 * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This
156 * function is used for transforming clipping plane equations and spotlight
157 * directions.
158 * Mathematically, u = v * m.
159 * Input: v - input vector
160 * m - transformation matrix
161 * Output: u - transformed vector
162 */
163 void gl_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
164 {
165 GLfloat v0=v[0], v1=v[1], v2=v[2], v3=v[3];
166 #define M(row,col) m[row + col*4]
167 u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
168 u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
169 u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
170 u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
171 #undef M
172 }
173
174
175 /* Useful for one-off point transformations, as in clipping.
176 * Note that because the matrix isn't analysed we do too many
177 * multiplies, and that the result is always 4-clean.
178 */
179 void gl_transform_point_sz( GLfloat Q[4], const GLfloat M[16],
180 const GLfloat P[4], GLuint sz )
181 {
182 if (Q == P)
183 return;
184
185 if (sz == 4)
186 {
187 Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12] * P[3];
188 Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13] * P[3];
189 Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14] * P[3];
190 Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15] * P[3];
191 }
192 else if (sz == 3)
193 {
194 Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12];
195 Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13];
196 Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14];
197 Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15];
198 }
199 else if (sz == 2)
200 {
201 Q[0] = M[0] * P[0] + M[4] * P[1] + M[12];
202 Q[1] = M[1] * P[0] + M[5] * P[1] + M[13];
203 Q[2] = M[2] * P[0] + M[6] * P[1] + M[14];
204 Q[3] = M[3] * P[0] + M[7] * P[1] + M[15];
205 }
206 else if (sz == 1)
207 {
208 Q[0] = M[0] * P[0] + M[12];
209 Q[1] = M[1] * P[0] + M[13];
210 Q[2] = M[2] * P[0] + M[14];
211 Q[3] = M[3] * P[0] + M[15];
212 }
213 }
214
215
216 /*
217 * This is called only once. It initializes several tables with pointers
218 * to optimized transformation functions. This is where we can test for
219 * AMD 3Dnow! capability, Intel Katmai, etc. and hook in the right code.
220 */
221 void
222 _math_init_transformation( void )
223 {
224 gl_transform_tab[0] = raw_transform_tab;
225 gl_transform_tab[1] = cull_transform_tab;
226
227 init_c_transformations_raw();
228 init_c_transformations_masked();
229 init_c_norm_transform_raw();
230 init_c_norm_transform_masked();
231 init_c_cliptest_raw();
232 init_copy0_raw();
233 init_copy0_masked();
234 init_dotprod_raw();
235 init_dotprod_masked();
236
237 #ifdef DEBUG
238 gl_test_all_transform_functions( "default" );
239 gl_test_all_normal_transform_functions( "default" );
240 #endif
241
242 #ifdef USE_X86_ASM
243 gl_init_all_x86_transform_asm();
244 #endif
245 }
246
247 void
248 _math_init( void )
249 {
250 _math_init_transformation();
251 _math_init_translate();
252 _math_init_vertices();
253 }