summaryrefslogtreecommitdiff
path: root/src/mesa/math/m_xform.c
blob: 7103f11f94b0d298ac4dd5e5642231e184a29a56 (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
256
257
/* $Id: m_xform.c,v 1.11 2001/03/12 00:48:41 gareth Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.5
 *
 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


/*
 * Matrix/vertex/vector transformation stuff
 *
 *
 * NOTES:
 * 1. 4x4 transformation matrices are stored in memory in column major order.
 * 2. Points/vertices are to be thought of as column vectors.
 * 3. Transformation of a point p by a matrix M is: p' = M * p
 */

#include <math.h>

#include "glheader.h"
#include "macros.h"
#include "mmath.h"

#include "m_eval.h"
#include "m_matrix.h"
#include "m_translate.h"
#include "m_xform.h"
#include "mathmod.h"


#ifdef DEBUG
#include "m_debug.h"
#endif

#ifdef USE_X86_ASM
#include "X86/common_x86_asm.h"
#endif

clip_func _mesa_clip_tab[5];
clip_func _mesa_clip_np_tab[5];
dotprod_func _mesa_dotprod_tab[2][5];
vec_copy_func _mesa_copy_tab[2][0x10];
normal_func _mesa_normal_tab[0xf][0x4];
transform_func **(_mesa_transform_tab[2]);
static transform_func *cull_transform_tab[5];
static transform_func *raw_transform_tab[5];


/* Raw data format used for:
 *    - Object-to-eye transform prior to culling, although this too
 *      could be culled under some circumstances.
 *    - Eye-to-clip transform (via the function above).
 *    - Cliptesting
 *    - And everything else too, if culling happens to be disabled.
 */
#define TAG(x) x##_raw
#define TAG2(x,y) x##y##_raw
#define IDX 0
#define STRIDE_LOOP for (i=0;i<count;i++, STRIDE_F(from, stride))
#define LOOP for (i=0;i<n;i++)
#define CULL_CHECK
#define CLIP_CHECK
#define ARGS
#include "m_xform_tmp.h"
#include "m_clip_tmp.h"
#include "m_norm_tmp.h"
#include "m_dotprod_tmp.h"
#include "m_copy_tmp.h"
#undef TAG
#undef TAG2
#undef LOOP
#undef CULL_CHECK
#undef CLIP_CHECK
#undef ARGS
#undef IDX

/* Culled data used for:
 *    - texture transformations
 *    - viewport map transformation
 *    - normal transformations prior to lighting
 *    - user cliptests
 */
#define TAG(x) x##_masked
#define TAG2(x,y) x##y##_masked
#define IDX 1
#define STRIDE_LOOP for (i=0;i<count;i++, STRIDE_F(from, stride))
#define LOOP for (i=0;i<n;i++)
#define CULL_CHECK if (mask[i])
#define CLIP_CHECK if ((mask[i] & flag) == 0)
#define ARGS , const GLubyte mask[]
#include "m_xform_tmp.h"
#include "m_norm_tmp.h"
#include "m_dotprod_tmp.h"
#include "m_copy_tmp.h"
#undef TAG
#undef TAG2
#undef LOOP
#undef CULL_CHECK
#undef CLIP_CHECK
#undef ARGS
#undef IDX






GLvector4f *_mesa_project_points( GLvector4f *proj_vec,
			       const GLvector4f *clip_vec )
{
   const GLuint stride = clip_vec->stride;
   const GLfloat *from = (GLfloat *)clip_vec->start;
   const GLuint count = clip_vec->count;
   GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start;
   GLuint i;

   for (i = 0 ; i < count ; i++, STRIDE_F(from, stride))
   {
	 GLfloat oow = 1.0F / from[3];
	 vProj[i][3] = oow;
	 vProj[i][0] = from[0] * oow;
	 vProj[i][1] = from[1] * oow;
	 vProj[i][2] = from[2] * oow;
   }

   proj_vec->flags |= VEC_SIZE_4;
   proj_vec->size = 3;
   proj_vec->count = clip_vec->count;
   return proj_vec;
}






/*
 * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix.  This
 * function is used for transforming clipping plane equations and spotlight
 * directions.
 * Mathematically,  u = v * m.
 * Input:  v - input vector
 *         m - transformation matrix
 * Output:  u - transformed vector
 */
void _mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
{
   GLfloat v0=v[0], v1=v[1], v2=v[2], v3=v[3];
#define M(row,col)  m[row + col*4]
   u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
   u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
   u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
   u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
#undef M
}


/* Useful for one-off point transformations, as in clipping.
 * Note that because the matrix isn't analysed we do too many
 * multiplies, and that the result is always 4-clean.
 */
void _mesa_transform_point_sz( GLfloat Q[4], const GLfloat M[16],
			    const GLfloat P[4], GLuint sz )
{
   if (Q == P)
      return;

   if (sz == 4)
   {
      Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] *  P[2] + M[12] * P[3];
      Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] *  P[2] + M[13] * P[3];
      Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14] * P[3];
      Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15] * P[3];
   }
   else if (sz == 3)
   {
      Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] *  P[2] + M[12];
      Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] *  P[2] + M[13];
      Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14];
      Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15];
   }
   else if (sz == 2)
   {
      Q[0] = M[0] * P[0] + M[4] * P[1] +                M[12];
      Q[1] = M[1] * P[0] + M[5] * P[1] +                M[13];
      Q[2] = M[2] * P[0] + M[6] * P[1] +                M[14];
      Q[3] = M[3] * P[0] + M[7] * P[1] +                M[15];
   }
   else if (sz == 1)
   {
      Q[0] = M[0] * P[0] +                              M[12];
      Q[1] = M[1] * P[0] +                              M[13];
      Q[2] = M[2] * P[0] +                              M[14];
      Q[3] = M[3] * P[0] +                              M[15];
   }
}


/*
 * This is called only once.  It initializes several tables with pointers
 * to optimized transformation functions.  This is where we can test for
 * AMD 3Dnow! capability, Intel Katmai, etc. and hook in the right code.
 */
void
_math_init_transformation( void )
{
   _mesa_transform_tab[0] = raw_transform_tab;
   _mesa_transform_tab[1] = cull_transform_tab;

   init_c_transformations_raw();
   init_c_transformations_masked();
   init_c_norm_transform_raw();
   init_c_norm_transform_masked();
   init_c_cliptest_raw();
   init_copy0_raw();
   init_copy0_masked();
   init_dotprod_raw();
   init_dotprod_masked();

#ifdef DEBUG
   _math_test_all_transform_functions( "default" );
   _math_test_all_normal_transform_functions( "default" );
#endif

#ifdef USE_X86_ASM
   _mesa_init_all_x86_transform_asm();
#endif
}

void
_math_init( void )
{
   _math_init_transformation();
   _math_init_translate();
   _math_init_vertices();
   _math_init_eval();
}