Move the transform and lighting code to two new directories
[mesa.git] / src / mesa / math / m_vector.c
1 /* $Id: m_vector.c,v 1.1 2000/11/16 21:05:41 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 * New (3.1) transformation code written by Keith Whitwell.
29 */
30
31
32 #include "glheader.h"
33 #include "macros.h"
34 #include "mem.h"
35
36 #include "m_vector.h"
37
38
39
40 /*
41 * Given a vector [count][4] of floats, set all the [][elt] values
42 * to 0 (if elt = 0, 1, 2) or 1.0 (if elt = 3).
43 */
44 void gl_vector4f_clean_elem( GLvector4f *vec, GLuint count, GLuint elt )
45 {
46 static const GLubyte elem_bits[4] = {
47 VEC_DIRTY_0,
48 VEC_DIRTY_1,
49 VEC_DIRTY_2,
50 VEC_DIRTY_3
51 };
52 static const GLfloat clean[4] = { 0, 0, 0, 1 };
53 const GLfloat v = clean[elt];
54 GLfloat (*data)[4] = (GLfloat (*)[4])vec->start;
55 GLuint i;
56
57 for (i = 0 ; i < count ; i++)
58 data[i][elt] = v;
59
60 vec->flags &= ~elem_bits[elt];
61 }
62
63 static const GLubyte size_bits[5] = {
64 0,
65 VEC_SIZE_1,
66 VEC_SIZE_2,
67 VEC_SIZE_3,
68 VEC_SIZE_4,
69 };
70
71
72
73 /*
74 * Initialize GLvector objects.
75 * Input: v - the vector object to initialize.
76 * flags - bitwise-OR of VEC_* flags
77 * storage - pointer to storage for the vector's data
78 */
79
80
81 void gl_vector4f_init( GLvector4f *v, GLuint flags, GLfloat (*storage)[4] )
82 {
83 v->stride = 4 * sizeof(GLfloat);
84 v->size = 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
85 v->data = storage;
86 v->start = (GLfloat *) storage;
87 v->count = 0;
88 v->flags = size_bits[4] | flags | VEC_GOOD_STRIDE;
89 }
90
91 void gl_vector3f_init( GLvector3f *v, GLuint flags, GLfloat (*storage)[3] )
92 {
93 v->stride = 3 * sizeof(GLfloat);
94 v->data = storage;
95 v->start = (GLfloat *) storage;
96 v->count = 0;
97 v->flags = flags | VEC_GOOD_STRIDE;
98 }
99
100 void gl_vector1f_init( GLvector1f *v, GLuint flags, GLfloat *storage )
101 {
102 v->stride = 1*sizeof(GLfloat);
103 v->data = storage;
104 v->start = (GLfloat *)storage;
105 v->count = 0;
106 v->flags = flags | VEC_GOOD_STRIDE;
107 }
108
109 void gl_vector4ub_init( GLvector4ub *v, GLuint flags, GLubyte (*storage)[4] )
110 {
111 v->stride = 4 * sizeof(GLubyte);
112 v->data = storage;
113 v->start = (GLubyte *) storage;
114 v->count = 0;
115 v->flags = flags | VEC_GOOD_STRIDE;
116 }
117
118 void gl_vector1ub_init( GLvector1ub *v, GLuint flags, GLubyte *storage )
119 {
120 v->stride = 1 * sizeof(GLubyte);
121 v->data = storage;
122 v->start = (GLubyte *) storage;
123 v->count = 0;
124 v->flags = flags | VEC_GOOD_STRIDE;
125 }
126
127 void gl_vector1ui_init( GLvector1ui *v, GLuint flags, GLuint *storage )
128 {
129 v->stride = 1 * sizeof(GLuint);
130 v->data = storage;
131 v->start = (GLuint *) storage;
132 v->count = 0;
133 v->flags = flags | VEC_GOOD_STRIDE;
134 }
135
136
137 /*
138 * Initialize GLvector objects and allocate storage.
139 * Input: v - the vector object
140 * sz - unused????
141 * flags - bitwise-OR of VEC_* flags
142 * count - number of elements to allocate in vector
143 * alignment - desired memory alignment for the data (in bytes)
144 */
145
146
147 void gl_vector4f_alloc( GLvector4f *v, GLuint flags, GLuint count,
148 GLuint alignment )
149 {
150 v->stride = 4 * sizeof(GLfloat);
151 v->size = 2;
152 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLfloat), alignment );
153 v->start = (GLfloat *) v->storage;
154 v->data = (GLfloat (*)[4]) v->storage;
155 v->count = 0;
156 v->flags = size_bits[4] | flags | VEC_MALLOC | VEC_GOOD_STRIDE;
157 }
158
159 void gl_vector3f_alloc( GLvector3f *v, GLuint flags, GLuint count,
160 GLuint alignment )
161 {
162 v->stride = 3 * sizeof(GLfloat);
163 v->storage = ALIGN_MALLOC( count * 3 * sizeof(GLfloat), alignment );
164 v->start = (GLfloat *) v->storage;
165 v->data = (GLfloat (*)[3]) v->storage;
166 v->count = 0;
167 v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
168 }
169
170 void gl_vector1f_alloc( GLvector1f *v, GLuint flags, GLuint count,
171 GLuint alignment )
172 {
173 v->stride = sizeof(GLfloat);
174 v->storage = v->start = (GLfloat *)
175 ALIGN_MALLOC( count * sizeof(GLfloat), alignment );
176 v->data = v->start;
177 v->count = 0;
178 v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
179 }
180
181 void gl_vector4ub_alloc( GLvector4ub *v, GLuint flags, GLuint count,
182 GLuint alignment )
183 {
184 v->stride = 4 * sizeof(GLubyte);
185 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLubyte), alignment );
186 v->start = (GLubyte *) v->storage;
187 v->data = (GLubyte (*)[4]) v->storage;
188 v->count = 0;
189 v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
190 }
191
192 void gl_vector1ub_alloc( GLvector1ub *v, GLuint flags, GLuint count,
193 GLuint alignment )
194 {
195 v->stride = 1 * sizeof(GLubyte);
196 v->storage = ALIGN_MALLOC( count * sizeof(GLubyte), alignment );
197 v->start = (GLubyte *) v->storage;
198 v->data = (GLubyte *) v->storage;
199 v->count = 0;
200 v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
201 }
202
203 void gl_vector1ui_alloc( GLvector1ui *v, GLuint flags, GLuint count,
204 GLuint alignment )
205 {
206 v->stride = 1 * sizeof(GLuint);
207 v->storage = ALIGN_MALLOC( count * sizeof(GLuint), alignment );
208 v->start = (GLuint *) v->storage;
209 v->data = (GLuint *) v->storage;
210 v->count = 0;
211 v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
212 }
213
214
215
216 /*
217 * Vector deallocation. Free whatever memory is pointed to by the
218 * vector's storage field if the VEC_MALLOC flag is set.
219 * DO NOT free the GLvector object itself, though.
220 */
221
222
223 void gl_vector4f_free( GLvector4f *v )
224 {
225 if (v->flags & VEC_MALLOC) {
226 ALIGN_FREE( v->storage );
227 v->data = NULL;
228 v->start = NULL;
229 v->storage = NULL;
230 v->flags &= ~VEC_MALLOC;
231 }
232 }
233
234 void gl_vector3f_free( GLvector3f *v )
235 {
236 if (v->flags & VEC_MALLOC) {
237 ALIGN_FREE( v->storage );
238 v->data = 0;
239 v->start = 0;
240 v->storage = 0;
241 v->flags &= ~VEC_MALLOC;
242 }
243 }
244
245 void gl_vector1f_free( GLvector1f *v )
246 {
247 if (v->flags & VEC_MALLOC) {
248 ALIGN_FREE( v->storage );
249 v->data = NULL;
250 v->start = NULL;
251 v->storage = NULL;
252 v->flags &= ~VEC_MALLOC;
253 }
254 }
255
256 void gl_vector4ub_free( GLvector4ub *v )
257 {
258 if (v->flags & VEC_MALLOC) {
259 ALIGN_FREE( v->storage );
260 v->data = NULL;
261 v->start = NULL;
262 v->storage = NULL;
263 v->flags &= ~VEC_MALLOC;
264 }
265 }
266
267 void gl_vector1ub_free( GLvector1ub *v )
268 {
269 if (v->flags & VEC_MALLOC) {
270 ALIGN_FREE( v->storage );
271 v->data = NULL;
272 v->start = NULL;
273 v->storage = NULL;
274 v->flags &= ~VEC_MALLOC;
275 }
276 }
277
278 void gl_vector1ui_free( GLvector1ui *v )
279 {
280 if (v->flags & VEC_MALLOC) {
281 ALIGN_FREE( v->storage );
282 v->data = NULL;
283 v->start = NULL;
284 v->storage = NULL;
285 v->flags &= ~VEC_MALLOC;
286 }
287 }
288
289
290 /*
291 * For debugging
292 */
293 void gl_vector4f_print( GLvector4f *v, GLubyte *cullmask, GLboolean culling )
294 {
295 GLfloat c[4] = { 0, 0, 0, 1 };
296 const char *templates[5] = {
297 "%d:\t0, 0, 0, 1\n",
298 "%d:\t%f, 0, 0, 1\n",
299 "%d:\t%f, %f, 0, 1\n",
300 "%d:\t%f, %f, %f, 1\n",
301 "%d:\t%f, %f, %f, %f\n"
302 };
303
304 const char *t = templates[v->size];
305 GLfloat *d = (GLfloat *)v->data;
306 GLuint j, i = 0, count;
307
308 printf("data-start\n");
309 for ( ; d != v->start ; STRIDE_F(d, v->stride), i++)
310 printf( t, i, d[0], d[1], d[2], d[3]);
311
312 printf("start-count(%u)\n", v->count);
313 count = i + v->count;
314
315 if (culling) {
316 for ( ; i < count ; STRIDE_F(d, v->stride), i++)
317 if (cullmask[i])
318 printf( t, i, d[0], d[1], d[2], d[3]);
319 }
320 else {
321 for ( ; i < count ; STRIDE_F(d, v->stride), i++)
322 printf( t, i, d[0], d[1], d[2], d[3]);
323 }
324
325 for (j = v->size ; j < 4; j++) {
326 if ((v->flags & (1<<j)) == 0) {
327
328 printf("checking col %u is clean as advertised ", j);
329
330 for (i = 0, d = (GLfloat *) v->data ;
331 i < count && d[j] == c[j] ;
332 i++, STRIDE_F(d, v->stride)) {};
333
334 if (i == count)
335 printf(" --> ok\n");
336 else
337 printf(" --> Failed at %u ******\n", i);
338 }
339 }
340 }
341
342
343 /*
344 * For debugging
345 */
346 void gl_vector3f_print( GLvector3f *v, GLubyte *cullmask, GLboolean culling )
347 {
348 GLfloat *d = (GLfloat *)v->data;
349 GLuint i = 0, count;
350
351 printf("data-start\n");
352 for ( ; d != v->start ; STRIDE_F(d,v->stride), i++)
353 printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
354
355 printf("start-count(%u)\n", v->count);
356 count = i + v->count;
357
358 if (culling) {
359 for ( ; i < count ; STRIDE_F(d,v->stride), i++)
360 if (cullmask[i])
361 printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
362 }
363 else {
364 for ( ; i < count ; STRIDE_F(d,v->stride), i++)
365 printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
366 }
367 }