1 /* $Id: m_vector.c,v 1.9 2002/12/04 14:24:44 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.
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).
45 void _mesa_vector4f_clean_elem( GLvector4f
*vec
, GLuint count
, GLuint elt
)
47 static const GLubyte elem_bits
[4] = {
53 static const GLfloat clean
[4] = { 0, 0, 0, 1 };
54 const GLfloat v
= clean
[elt
];
55 GLfloat (*data
)[4] = (GLfloat (*)[4])vec
->start
;
58 for (i
= 0 ; i
< count
; i
++)
61 vec
->flags
&= ~elem_bits
[elt
];
64 static const GLubyte size_bits
[5] = {
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
82 void _mesa_vector4f_init( GLvector4f
*v
, GLuint flags
, GLfloat (*storage
)[4] )
84 v
->stride
= 4 * sizeof(GLfloat
);
85 v
->size
= 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
87 v
->start
= (GLfloat
*) storage
;
89 v
->flags
= size_bits
[4] | flags
;
92 void _mesa_vector3f_init( GLvector3f
*v
, GLuint flags
, GLfloat (*storage
)[3] )
94 v
->stride
= 3 * sizeof(GLfloat
);
96 v
->start
= (GLfloat
*) storage
;
101 void _mesa_vector1f_init( GLvector1f
*v
, GLuint flags
, GLfloat
*storage
)
103 v
->stride
= 1*sizeof(GLfloat
);
105 v
->start
= (GLfloat
*)storage
;
110 void _mesa_vector4ub_init( GLvector4ub
*v
, GLuint flags
, GLubyte (*storage
)[4] )
112 v
->stride
= 4 * sizeof(GLubyte
);
114 v
->start
= (GLubyte
*) storage
;
119 void _mesa_vector4chan_init( GLvector4chan
*v
, GLuint flags
, GLchan (*storage
)[4] )
121 v
->stride
= 4 * sizeof(GLchan
);
123 v
->start
= (GLchan
*) storage
;
128 void _mesa_vector4us_init( GLvector4us
*v
, GLuint flags
, GLushort (*storage
)[4] )
130 v
->stride
= 4 * sizeof(GLushort
);
132 v
->start
= (GLushort
*) storage
;
137 void _mesa_vector1ub_init( GLvector1ub
*v
, GLuint flags
, GLubyte
*storage
)
139 v
->stride
= 1 * sizeof(GLubyte
);
141 v
->start
= (GLubyte
*) storage
;
146 void _mesa_vector1ui_init( GLvector1ui
*v
, GLuint flags
, GLuint
*storage
)
148 v
->stride
= 1 * sizeof(GLuint
);
150 v
->start
= (GLuint
*) storage
;
157 * Initialize GLvector objects and allocate storage.
158 * Input: v - the vector object
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)
166 void _mesa_vector4f_alloc( GLvector4f
*v
, GLuint flags
, GLuint count
,
169 v
->stride
= 4 * sizeof(GLfloat
);
171 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLfloat
), alignment
);
172 v
->start
= (GLfloat
*) v
->storage
;
173 v
->data
= (GLfloat (*)[4]) v
->storage
;
175 v
->flags
= size_bits
[4] | flags
| VEC_MALLOC
;
178 void _mesa_vector3f_alloc( GLvector3f
*v
, GLuint flags
, GLuint count
,
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
;
186 v
->flags
= flags
| VEC_MALLOC
;
189 void _mesa_vector1f_alloc( GLvector1f
*v
, GLuint flags
, GLuint count
,
192 v
->stride
= sizeof(GLfloat
);
193 v
->storage
= v
->start
= (GLfloat
*)
194 ALIGN_MALLOC( count
* sizeof(GLfloat
), alignment
);
197 v
->flags
= flags
| VEC_MALLOC
;
200 void _mesa_vector4ub_alloc( GLvector4ub
*v
, GLuint flags
, GLuint count
,
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
;
208 v
->flags
= flags
| VEC_MALLOC
;
211 void _mesa_vector4chan_alloc( GLvector4chan
*v
, GLuint flags
, GLuint count
,
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
;
219 v
->flags
= flags
| VEC_MALLOC
;
222 void _mesa_vector4us_alloc( GLvector4us
*v
, GLuint flags
, GLuint count
,
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
;
230 v
->flags
= flags
| VEC_MALLOC
;
233 void _mesa_vector1ub_alloc( GLvector1ub
*v
, GLuint flags
, GLuint count
,
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
;
241 v
->flags
= flags
| VEC_MALLOC
;
244 void _mesa_vector1ui_alloc( GLvector1ui
*v
, GLuint flags
, GLuint count
,
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
;
252 v
->flags
= flags
| VEC_MALLOC
;
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.
264 void _mesa_vector4f_free( GLvector4f
*v
)
266 if (v
->flags
& VEC_MALLOC
) {
267 ALIGN_FREE( v
->storage
);
271 v
->flags
&= ~VEC_MALLOC
;
275 void _mesa_vector3f_free( GLvector3f
*v
)
277 if (v
->flags
& VEC_MALLOC
) {
278 ALIGN_FREE( v
->storage
);
282 v
->flags
&= ~VEC_MALLOC
;
286 void _mesa_vector1f_free( GLvector1f
*v
)
288 if (v
->flags
& VEC_MALLOC
) {
289 ALIGN_FREE( v
->storage
);
293 v
->flags
&= ~VEC_MALLOC
;
297 void _mesa_vector4ub_free( GLvector4ub
*v
)
299 if (v
->flags
& VEC_MALLOC
) {
300 ALIGN_FREE( v
->storage
);
304 v
->flags
&= ~VEC_MALLOC
;
308 void _mesa_vector4chan_free( GLvector4chan
*v
)
310 if (v
->flags
& VEC_MALLOC
) {
311 ALIGN_FREE( v
->storage
);
315 v
->flags
&= ~VEC_MALLOC
;
319 void _mesa_vector4us_free( GLvector4us
*v
)
321 if (v
->flags
& VEC_MALLOC
) {
322 ALIGN_FREE( v
->storage
);
326 v
->flags
&= ~VEC_MALLOC
;
330 void _mesa_vector1ub_free( GLvector1ub
*v
)
332 if (v
->flags
& VEC_MALLOC
) {
333 ALIGN_FREE( v
->storage
);
337 v
->flags
&= ~VEC_MALLOC
;
341 void _mesa_vector1ui_free( GLvector1ui
*v
)
343 if (v
->flags
& VEC_MALLOC
) {
344 ALIGN_FREE( v
->storage
);
348 v
->flags
&= ~VEC_MALLOC
;
356 void _mesa_vector4f_print( GLvector4f
*v
, GLubyte
*cullmask
, GLboolean culling
)
358 GLfloat c
[4] = { 0, 0, 0, 1 };
359 const char *templates
[5] = {
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"
367 const char *t
= templates
[v
->size
];
368 GLfloat
*d
= (GLfloat
*)v
->data
;
369 GLuint j
, i
= 0, count
;
371 _mesa_printf("data-start\n");
372 for ( ; d
!= v
->start
; STRIDE_F(d
, v
->stride
), i
++)
373 _mesa_printf(t
, i
, d
[0], d
[1], d
[2], d
[3]);
375 _mesa_printf("start-count(%u)\n", v
->count
);
376 count
= i
+ v
->count
;
379 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
381 _mesa_printf(t
, i
, d
[0], d
[1], d
[2], d
[3]);
384 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
385 _mesa_printf(t
, i
, d
[0], d
[1], d
[2], d
[3]);
388 for (j
= v
->size
; j
< 4; j
++) {
389 if ((v
->flags
& (1<<j
)) == 0) {
391 _mesa_printf("checking col %u is clean as advertised ", j
);
393 for (i
= 0, d
= (GLfloat
*) v
->data
;
394 i
< count
&& d
[j
] == c
[j
] ;
395 i
++, STRIDE_F(d
, v
->stride
)) {};
398 _mesa_printf(" --> ok\n");
400 _mesa_printf(" --> Failed at %u ******\n", i
);
409 void _mesa_vector3f_print( GLvector3f
*v
, GLubyte
*cullmask
, GLboolean culling
)
411 GLfloat
*d
= (GLfloat
*)v
->data
;
414 _mesa_printf("data-start\n");
415 for ( ; d
!= v
->start
; STRIDE_F(d
,v
->stride
), i
++)
416 _mesa_printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
418 _mesa_printf("start-count(%u)\n", v
->count
);
419 count
= i
+ v
->count
;
422 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
424 _mesa_printf("%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
427 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
428 _mesa_printf("%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);