Header file clean-up:
[mesa.git] / src / mesa / math / m_vector.c
1 /* $Id: m_vector.c,v 1.8 2002/10/24 23:57:24 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 "imports.h"
34 #include "macros.h"
35 #include "imports.h"
36
37 #include "m_vector.h"
38
39
40
41 /*
42 * Given a vector [count][4] of floats, set all the [][elt] values
43 * to 0 (if elt = 0, 1, 2) or 1.0 (if elt = 3).
44 */
45 void _mesa_vector4f_clean_elem( GLvector4f *vec, GLuint count, GLuint elt )
46 {
47 static const GLubyte elem_bits[4] = {
48 VEC_DIRTY_0,
49 VEC_DIRTY_1,
50 VEC_DIRTY_2,
51 VEC_DIRTY_3
52 };
53 static const GLfloat clean[4] = { 0, 0, 0, 1 };
54 const GLfloat v = clean[elt];
55 GLfloat (*data)[4] = (GLfloat (*)[4])vec->start;
56 GLuint i;
57
58 for (i = 0 ; i < count ; i++)
59 data[i][elt] = v;
60
61 vec->flags &= ~elem_bits[elt];
62 }
63
64 static const GLubyte size_bits[5] = {
65 0,
66 VEC_SIZE_1,
67 VEC_SIZE_2,
68 VEC_SIZE_3,
69 VEC_SIZE_4,
70 };
71
72
73
74 /*
75 * Initialize GLvector objects.
76 * Input: v - the vector object to initialize.
77 * flags - bitwise-OR of VEC_* flags
78 * storage - pointer to storage for the vector's data
79 */
80
81
82 void _mesa_vector4f_init( GLvector4f *v, GLuint flags, GLfloat (*storage)[4] )
83 {
84 v->stride = 4 * sizeof(GLfloat);
85 v->size = 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
86 v->data = storage;
87 v->start = (GLfloat *) storage;
88 v->count = 0;
89 v->flags = size_bits[4] | flags ;
90 }
91
92 void _mesa_vector3f_init( GLvector3f *v, GLuint flags, GLfloat (*storage)[3] )
93 {
94 v->stride = 3 * sizeof(GLfloat);
95 v->data = storage;
96 v->start = (GLfloat *) storage;
97 v->count = 0;
98 v->flags = flags ;
99 }
100
101 void _mesa_vector1f_init( GLvector1f *v, GLuint flags, GLfloat *storage )
102 {
103 v->stride = 1*sizeof(GLfloat);
104 v->data = storage;
105 v->start = (GLfloat *)storage;
106 v->count = 0;
107 v->flags = flags ;
108 }
109
110 void _mesa_vector4ub_init( GLvector4ub *v, GLuint flags, GLubyte (*storage)[4] )
111 {
112 v->stride = 4 * sizeof(GLubyte);
113 v->data = storage;
114 v->start = (GLubyte *) storage;
115 v->count = 0;
116 v->flags = flags ;
117 }
118
119 void _mesa_vector4chan_init( GLvector4chan *v, GLuint flags, GLchan (*storage)[4] )
120 {
121 v->stride = 4 * sizeof(GLchan);
122 v->data = storage;
123 v->start = (GLchan *) storage;
124 v->count = 0;
125 v->flags = flags ;
126 }
127
128 void _mesa_vector4us_init( GLvector4us *v, GLuint flags, GLushort (*storage)[4] )
129 {
130 v->stride = 4 * sizeof(GLushort);
131 v->data = storage;
132 v->start = (GLushort *) storage;
133 v->count = 0;
134 v->flags = flags ;
135 }
136
137 void _mesa_vector1ub_init( GLvector1ub *v, GLuint flags, GLubyte *storage )
138 {
139 v->stride = 1 * sizeof(GLubyte);
140 v->data = storage;
141 v->start = (GLubyte *) storage;
142 v->count = 0;
143 v->flags = flags ;
144 }
145
146 void _mesa_vector1ui_init( GLvector1ui *v, GLuint flags, GLuint *storage )
147 {
148 v->stride = 1 * sizeof(GLuint);
149 v->data = storage;
150 v->start = (GLuint *) storage;
151 v->count = 0;
152 v->flags = flags ;
153 }
154
155
156 /*
157 * Initialize GLvector objects and allocate storage.
158 * Input: v - the vector object
159 * sz - unused????
160 * flags - bitwise-OR of VEC_* flags
161 * count - number of elements to allocate in vector
162 * alignment - desired memory alignment for the data (in bytes)
163 */
164
165
166 void _mesa_vector4f_alloc( GLvector4f *v, GLuint flags, GLuint count,
167 GLuint alignment )
168 {
169 v->stride = 4 * sizeof(GLfloat);
170 v->size = 2;
171 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLfloat), alignment );
172 v->start = (GLfloat *) v->storage;
173 v->data = (GLfloat (*)[4]) v->storage;
174 v->count = 0;
175 v->flags = size_bits[4] | flags | VEC_MALLOC ;
176 }
177
178 void _mesa_vector3f_alloc( GLvector3f *v, GLuint flags, GLuint count,
179 GLuint alignment )
180 {
181 v->stride = 3 * sizeof(GLfloat);
182 v->storage = ALIGN_MALLOC( count * 3 * sizeof(GLfloat), alignment );
183 v->start = (GLfloat *) v->storage;
184 v->data = (GLfloat (*)[3]) v->storage;
185 v->count = 0;
186 v->flags = flags | VEC_MALLOC ;
187 }
188
189 void _mesa_vector1f_alloc( GLvector1f *v, GLuint flags, GLuint count,
190 GLuint alignment )
191 {
192 v->stride = sizeof(GLfloat);
193 v->storage = v->start = (GLfloat *)
194 ALIGN_MALLOC( count * sizeof(GLfloat), alignment );
195 v->data = v->start;
196 v->count = 0;
197 v->flags = flags | VEC_MALLOC ;
198 }
199
200 void _mesa_vector4ub_alloc( GLvector4ub *v, GLuint flags, GLuint count,
201 GLuint alignment )
202 {
203 v->stride = 4 * sizeof(GLubyte);
204 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLubyte), alignment );
205 v->start = (GLubyte *) v->storage;
206 v->data = (GLubyte (*)[4]) v->storage;
207 v->count = 0;
208 v->flags = flags | VEC_MALLOC ;
209 }
210
211 void _mesa_vector4chan_alloc( GLvector4chan *v, GLuint flags, GLuint count,
212 GLuint alignment )
213 {
214 v->stride = 4 * sizeof(GLchan);
215 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLchan), alignment );
216 v->start = (GLchan *) v->storage;
217 v->data = (GLchan (*)[4]) v->storage;
218 v->count = 0;
219 v->flags = flags | VEC_MALLOC ;
220 }
221
222 void _mesa_vector4us_alloc( GLvector4us *v, GLuint flags, GLuint count,
223 GLuint alignment )
224 {
225 v->stride = 4 * sizeof(GLushort);
226 v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLushort), alignment );
227 v->start = (GLushort *) v->storage;
228 v->data = (GLushort (*)[4]) v->storage;
229 v->count = 0;
230 v->flags = flags | VEC_MALLOC ;
231 }
232
233 void _mesa_vector1ub_alloc( GLvector1ub *v, GLuint flags, GLuint count,
234 GLuint alignment )
235 {
236 v->stride = 1 * sizeof(GLubyte);
237 v->storage = ALIGN_MALLOC( count * sizeof(GLubyte), alignment );
238 v->start = (GLubyte *) v->storage;
239 v->data = (GLubyte *) v->storage;
240 v->count = 0;
241 v->flags = flags | VEC_MALLOC ;
242 }
243
244 void _mesa_vector1ui_alloc( GLvector1ui *v, GLuint flags, GLuint count,
245 GLuint alignment )
246 {
247 v->stride = 1 * sizeof(GLuint);
248 v->storage = ALIGN_MALLOC( count * sizeof(GLuint), alignment );
249 v->start = (GLuint *) v->storage;
250 v->data = (GLuint *) v->storage;
251 v->count = 0;
252 v->flags = flags | VEC_MALLOC ;
253 }
254
255
256
257 /*
258 * Vector deallocation. Free whatever memory is pointed to by the
259 * vector's storage field if the VEC_MALLOC flag is set.
260 * DO NOT free the GLvector object itself, though.
261 */
262
263
264 void _mesa_vector4f_free( GLvector4f *v )
265 {
266 if (v->flags & VEC_MALLOC) {
267 ALIGN_FREE( v->storage );
268 v->data = NULL;
269 v->start = NULL;
270 v->storage = NULL;
271 v->flags &= ~VEC_MALLOC;
272 }
273 }
274
275 void _mesa_vector3f_free( GLvector3f *v )
276 {
277 if (v->flags & VEC_MALLOC) {
278 ALIGN_FREE( v->storage );
279 v->data = 0;
280 v->start = 0;
281 v->storage = 0;
282 v->flags &= ~VEC_MALLOC;
283 }
284 }
285
286 void _mesa_vector1f_free( GLvector1f *v )
287 {
288 if (v->flags & VEC_MALLOC) {
289 ALIGN_FREE( v->storage );
290 v->data = NULL;
291 v->start = NULL;
292 v->storage = NULL;
293 v->flags &= ~VEC_MALLOC;
294 }
295 }
296
297 void _mesa_vector4ub_free( GLvector4ub *v )
298 {
299 if (v->flags & VEC_MALLOC) {
300 ALIGN_FREE( v->storage );
301 v->data = NULL;
302 v->start = NULL;
303 v->storage = NULL;
304 v->flags &= ~VEC_MALLOC;
305 }
306 }
307
308 void _mesa_vector4chan_free( GLvector4chan *v )
309 {
310 if (v->flags & VEC_MALLOC) {
311 ALIGN_FREE( v->storage );
312 v->data = NULL;
313 v->start = NULL;
314 v->storage = NULL;
315 v->flags &= ~VEC_MALLOC;
316 }
317 }
318
319 void _mesa_vector4us_free( GLvector4us *v )
320 {
321 if (v->flags & VEC_MALLOC) {
322 ALIGN_FREE( v->storage );
323 v->data = NULL;
324 v->start = NULL;
325 v->storage = NULL;
326 v->flags &= ~VEC_MALLOC;
327 }
328 }
329
330 void _mesa_vector1ub_free( GLvector1ub *v )
331 {
332 if (v->flags & VEC_MALLOC) {
333 ALIGN_FREE( v->storage );
334 v->data = NULL;
335 v->start = NULL;
336 v->storage = NULL;
337 v->flags &= ~VEC_MALLOC;
338 }
339 }
340
341 void _mesa_vector1ui_free( GLvector1ui *v )
342 {
343 if (v->flags & VEC_MALLOC) {
344 ALIGN_FREE( v->storage );
345 v->data = NULL;
346 v->start = NULL;
347 v->storage = NULL;
348 v->flags &= ~VEC_MALLOC;
349 }
350 }
351
352
353 /*
354 * For debugging
355 */
356 void _mesa_vector4f_print( GLvector4f *v, GLubyte *cullmask, GLboolean culling )
357 {
358 GLfloat c[4] = { 0, 0, 0, 1 };
359 const char *templates[5] = {
360 "%d:\t0, 0, 0, 1\n",
361 "%d:\t%f, 0, 0, 1\n",
362 "%d:\t%f, %f, 0, 1\n",
363 "%d:\t%f, %f, %f, 1\n",
364 "%d:\t%f, %f, %f, %f\n"
365 };
366
367 const char *t = templates[v->size];
368 GLfloat *d = (GLfloat *)v->data;
369 GLuint j, i = 0, count;
370
371 _mesa_printf(NULL, "data-start\n");
372 for ( ; d != v->start ; STRIDE_F(d, v->stride), i++)
373 _mesa_printf(NULL, t, i, d[0], d[1], d[2], d[3]);
374
375 _mesa_printf(NULL, "start-count(%u)\n", v->count);
376 count = i + v->count;
377
378 if (culling) {
379 for ( ; i < count ; STRIDE_F(d, v->stride), i++)
380 if (cullmask[i])
381 _mesa_printf(NULL, t, i, d[0], d[1], d[2], d[3]);
382 }
383 else {
384 for ( ; i < count ; STRIDE_F(d, v->stride), i++)
385 _mesa_printf(NULL, t, i, d[0], d[1], d[2], d[3]);
386 }
387
388 for (j = v->size ; j < 4; j++) {
389 if ((v->flags & (1<<j)) == 0) {
390
391 _mesa_printf(NULL, "checking col %u is clean as advertised ", j);
392
393 for (i = 0, d = (GLfloat *) v->data ;
394 i < count && d[j] == c[j] ;
395 i++, STRIDE_F(d, v->stride)) {};
396
397 if (i == count)
398 _mesa_printf(NULL, " --> ok\n");
399 else
400 _mesa_printf(NULL, " --> Failed at %u ******\n", i);
401 }
402 }
403 }
404
405
406 /*
407 * For debugging
408 */
409 void _mesa_vector3f_print( GLvector3f *v, GLubyte *cullmask, GLboolean culling )
410 {
411 GLfloat *d = (GLfloat *)v->data;
412 GLuint i = 0, count;
413
414 _mesa_printf(NULL, "data-start\n");
415 for ( ; d != v->start ; STRIDE_F(d,v->stride), i++)
416 _mesa_printf(NULL, "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
417
418 _mesa_printf(NULL, "start-count(%u)\n", v->count);
419 count = i + v->count;
420
421 if (culling) {
422 for ( ; i < count ; STRIDE_F(d,v->stride), i++)
423 if (cullmask[i])
424 _mesa_printf(NULL, "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
425 }
426 else {
427 for ( ; i < count ; STRIDE_F(d,v->stride), i++)
428 _mesa_printf(NULL, "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
429 }
430 }