1 /* $Id: m_vector.c,v 1.5 2001/03/03 20:33:30 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
28 * New (3.1) transformation code written by Keith Whitwell.
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).
44 void _mesa_vector4f_clean_elem( GLvector4f
*vec
, GLuint count
, GLuint elt
)
46 static const GLubyte elem_bits
[4] = {
52 static const GLfloat clean
[4] = { 0, 0, 0, 1 };
53 const GLfloat v
= clean
[elt
];
54 GLfloat (*data
)[4] = (GLfloat (*)[4])vec
->start
;
57 for (i
= 0 ; i
< count
; i
++)
60 vec
->flags
&= ~elem_bits
[elt
];
63 static const GLubyte size_bits
[5] = {
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
81 void _mesa_vector4f_init( GLvector4f
*v
, GLuint flags
, GLfloat (*storage
)[4] )
83 v
->stride
= 4 * sizeof(GLfloat
);
84 v
->size
= 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
86 v
->start
= (GLfloat
*) storage
;
88 v
->flags
= size_bits
[4] | flags
;
91 void _mesa_vector3f_init( GLvector3f
*v
, GLuint flags
, GLfloat (*storage
)[3] )
93 v
->stride
= 3 * sizeof(GLfloat
);
95 v
->start
= (GLfloat
*) storage
;
100 void _mesa_vector1f_init( GLvector1f
*v
, GLuint flags
, GLfloat
*storage
)
102 v
->stride
= 1*sizeof(GLfloat
);
104 v
->start
= (GLfloat
*)storage
;
109 void _mesa_vector4ub_init( GLvector4ub
*v
, GLuint flags
, GLubyte (*storage
)[4] )
111 v
->stride
= 4 * sizeof(GLubyte
);
113 v
->start
= (GLubyte
*) storage
;
118 void _mesa_vector4chan_init( GLvector4chan
*v
, GLuint flags
, GLchan (*storage
)[4] )
120 v
->stride
= 4 * sizeof(GLchan
);
122 v
->start
= (GLchan
*) storage
;
127 void _mesa_vector4us_init( GLvector4us
*v
, GLuint flags
, GLushort (*storage
)[4] )
129 v
->stride
= 4 * sizeof(GLushort
);
131 v
->start
= (GLushort
*) storage
;
136 void _mesa_vector1ub_init( GLvector1ub
*v
, GLuint flags
, GLubyte
*storage
)
138 v
->stride
= 1 * sizeof(GLubyte
);
140 v
->start
= (GLubyte
*) storage
;
145 void _mesa_vector1ui_init( GLvector1ui
*v
, GLuint flags
, GLuint
*storage
)
147 v
->stride
= 1 * sizeof(GLuint
);
149 v
->start
= (GLuint
*) storage
;
156 * Initialize GLvector objects and allocate storage.
157 * Input: v - the vector object
159 * flags - bitwise-OR of VEC_* flags
160 * count - number of elements to allocate in vector
161 * alignment - desired memory alignment for the data (in bytes)
165 void _mesa_vector4f_alloc( GLvector4f
*v
, GLuint flags
, GLuint count
,
168 v
->stride
= 4 * sizeof(GLfloat
);
170 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLfloat
), alignment
);
171 v
->start
= (GLfloat
*) v
->storage
;
172 v
->data
= (GLfloat (*)[4]) v
->storage
;
174 v
->flags
= size_bits
[4] | flags
| VEC_MALLOC
;
177 void _mesa_vector3f_alloc( GLvector3f
*v
, GLuint flags
, GLuint count
,
180 v
->stride
= 3 * sizeof(GLfloat
);
181 v
->storage
= ALIGN_MALLOC( count
* 3 * sizeof(GLfloat
), alignment
);
182 v
->start
= (GLfloat
*) v
->storage
;
183 v
->data
= (GLfloat (*)[3]) v
->storage
;
185 v
->flags
= flags
| VEC_MALLOC
;
188 void _mesa_vector1f_alloc( GLvector1f
*v
, GLuint flags
, GLuint count
,
191 v
->stride
= sizeof(GLfloat
);
192 v
->storage
= v
->start
= (GLfloat
*)
193 ALIGN_MALLOC( count
* sizeof(GLfloat
), alignment
);
196 v
->flags
= flags
| VEC_MALLOC
;
199 void _mesa_vector4ub_alloc( GLvector4ub
*v
, GLuint flags
, GLuint count
,
202 v
->stride
= 4 * sizeof(GLubyte
);
203 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLubyte
), alignment
);
204 v
->start
= (GLubyte
*) v
->storage
;
205 v
->data
= (GLubyte (*)[4]) v
->storage
;
207 v
->flags
= flags
| VEC_MALLOC
;
210 void _mesa_vector4chan_alloc( GLvector4chan
*v
, GLuint flags
, GLuint count
,
213 v
->stride
= 4 * sizeof(GLchan
);
214 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLchan
), alignment
);
215 v
->start
= (GLchan
*) v
->storage
;
216 v
->data
= (GLchan (*)[4]) v
->storage
;
218 v
->flags
= flags
| VEC_MALLOC
;
221 void _mesa_vector4us_alloc( GLvector4us
*v
, GLuint flags
, GLuint count
,
224 v
->stride
= 4 * sizeof(GLushort
);
225 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLushort
), alignment
);
226 v
->start
= (GLushort
*) v
->storage
;
227 v
->data
= (GLushort (*)[4]) v
->storage
;
229 v
->flags
= flags
| VEC_MALLOC
;
232 void _mesa_vector1ub_alloc( GLvector1ub
*v
, GLuint flags
, GLuint count
,
235 v
->stride
= 1 * sizeof(GLubyte
);
236 v
->storage
= ALIGN_MALLOC( count
* sizeof(GLubyte
), alignment
);
237 v
->start
= (GLubyte
*) v
->storage
;
238 v
->data
= (GLubyte
*) v
->storage
;
240 v
->flags
= flags
| VEC_MALLOC
;
243 void _mesa_vector1ui_alloc( GLvector1ui
*v
, GLuint flags
, GLuint count
,
246 v
->stride
= 1 * sizeof(GLuint
);
247 v
->storage
= ALIGN_MALLOC( count
* sizeof(GLuint
), alignment
);
248 v
->start
= (GLuint
*) v
->storage
;
249 v
->data
= (GLuint
*) v
->storage
;
251 v
->flags
= flags
| VEC_MALLOC
;
257 * Vector deallocation. Free whatever memory is pointed to by the
258 * vector's storage field if the VEC_MALLOC flag is set.
259 * DO NOT free the GLvector object itself, though.
263 void _mesa_vector4f_free( GLvector4f
*v
)
265 if (v
->flags
& VEC_MALLOC
) {
266 ALIGN_FREE( v
->storage
);
270 v
->flags
&= ~VEC_MALLOC
;
274 void _mesa_vector3f_free( GLvector3f
*v
)
276 if (v
->flags
& VEC_MALLOC
) {
277 ALIGN_FREE( v
->storage
);
281 v
->flags
&= ~VEC_MALLOC
;
285 void _mesa_vector1f_free( GLvector1f
*v
)
287 if (v
->flags
& VEC_MALLOC
) {
288 ALIGN_FREE( v
->storage
);
292 v
->flags
&= ~VEC_MALLOC
;
296 void _mesa_vector4ub_free( GLvector4ub
*v
)
298 if (v
->flags
& VEC_MALLOC
) {
299 ALIGN_FREE( v
->storage
);
303 v
->flags
&= ~VEC_MALLOC
;
307 void _mesa_vector4chan_free( GLvector4chan
*v
)
309 if (v
->flags
& VEC_MALLOC
) {
310 ALIGN_FREE( v
->storage
);
314 v
->flags
&= ~VEC_MALLOC
;
318 void _mesa_vector4us_free( GLvector4us
*v
)
320 if (v
->flags
& VEC_MALLOC
) {
321 ALIGN_FREE( v
->storage
);
325 v
->flags
&= ~VEC_MALLOC
;
329 void _mesa_vector1ub_free( GLvector1ub
*v
)
331 if (v
->flags
& VEC_MALLOC
) {
332 ALIGN_FREE( v
->storage
);
336 v
->flags
&= ~VEC_MALLOC
;
340 void _mesa_vector1ui_free( GLvector1ui
*v
)
342 if (v
->flags
& VEC_MALLOC
) {
343 ALIGN_FREE( v
->storage
);
347 v
->flags
&= ~VEC_MALLOC
;
355 void _mesa_vector4f_print( GLvector4f
*v
, GLubyte
*cullmask
, GLboolean culling
)
357 GLfloat c
[4] = { 0, 0, 0, 1 };
358 const char *templates
[5] = {
360 "%d:\t%f, 0, 0, 1\n",
361 "%d:\t%f, %f, 0, 1\n",
362 "%d:\t%f, %f, %f, 1\n",
363 "%d:\t%f, %f, %f, %f\n"
366 const char *t
= templates
[v
->size
];
367 GLfloat
*d
= (GLfloat
*)v
->data
;
368 GLuint j
, i
= 0, count
;
370 printf("data-start\n");
371 for ( ; d
!= v
->start
; STRIDE_F(d
, v
->stride
), i
++)
372 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
374 printf("start-count(%u)\n", v
->count
);
375 count
= i
+ v
->count
;
378 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
380 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
383 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
384 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
387 for (j
= v
->size
; j
< 4; j
++) {
388 if ((v
->flags
& (1<<j
)) == 0) {
390 printf("checking col %u is clean as advertised ", j
);
392 for (i
= 0, d
= (GLfloat
*) v
->data
;
393 i
< count
&& d
[j
] == c
[j
] ;
394 i
++, STRIDE_F(d
, v
->stride
)) {};
399 printf(" --> Failed at %u ******\n", i
);
408 void _mesa_vector3f_print( GLvector3f
*v
, GLubyte
*cullmask
, GLboolean culling
)
410 GLfloat
*d
= (GLfloat
*)v
->data
;
413 printf("data-start\n");
414 for ( ; d
!= v
->start
; STRIDE_F(d
,v
->stride
), i
++)
415 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
417 printf("start-count(%u)\n", v
->count
);
418 count
= i
+ v
->count
;
421 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
423 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
426 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
427 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);