m_vector.c revision ae0eaf93e092ac8e8b1c98f3e986de96940663fa
1
2/*
3 * Mesa 3-D graphics library
4 * Version:  3.5
5 *
6 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26/*
27 * New (3.1) transformation code written by Keith Whitwell.
28 */
29
30
31#include "glheader.h"
32#include "imports.h"
33#include "macros.h"
34#include "imports.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 */
44void _mesa_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
63static 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
81void _mesa_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 ;
89}
90
91
92
93
94/*
95 * Initialize GLvector objects and allocate storage.
96 * Input: v - the vector object
97 *        sz - unused????
98 *        flags - bitwise-OR of VEC_* flags
99 *        count - number of elements to allocate in vector
100 *        alignment - desired memory alignment for the data (in bytes)
101 */
102
103
104void _mesa_vector4f_alloc( GLvector4f *v, GLuint flags, GLuint count,
105			GLuint alignment )
106{
107   v->stride = 4 * sizeof(GLfloat);
108   v->size = 2;
109   v->storage = ALIGN_MALLOC( count * 4 * sizeof(GLfloat), alignment );
110   v->start = (GLfloat *) v->storage;
111   v->data = (GLfloat (*)[4]) v->storage;
112   v->count = 0;
113   v->flags = size_bits[4] | flags | VEC_MALLOC ;
114}
115
116
117
118
119/*
120 * Vector deallocation.  Free whatever memory is pointed to by the
121 * vector's storage field if the VEC_MALLOC flag is set.
122 * DO NOT free the GLvector object itself, though.
123 */
124
125
126void _mesa_vector4f_free( GLvector4f *v )
127{
128   if (v->flags & VEC_MALLOC) {
129      ALIGN_FREE( v->storage );
130      v->data = NULL;
131      v->start = NULL;
132      v->storage = NULL;
133      v->flags &= ~VEC_MALLOC;
134   }
135}
136
137
138/*
139 * For debugging
140 */
141void _mesa_vector4f_print( GLvector4f *v, GLubyte *cullmask, GLboolean culling )
142{
143   GLfloat c[4] = { 0, 0, 0, 1 };
144   const char *templates[5] = {
145      "%d:\t0, 0, 0, 1\n",
146      "%d:\t%f, 0, 0, 1\n",
147      "%d:\t%f, %f, 0, 1\n",
148      "%d:\t%f, %f, %f, 1\n",
149      "%d:\t%f, %f, %f, %f\n"
150   };
151
152   const char *t = templates[v->size];
153   GLfloat *d = (GLfloat *)v->data;
154   GLuint j, i = 0, count;
155
156   _mesa_printf("data-start\n");
157   for ( ; d != v->start ; STRIDE_F(d, v->stride), i++)
158      _mesa_printf(t, i, d[0], d[1], d[2], d[3]);
159
160   _mesa_printf("start-count(%u)\n", v->count);
161   count = i + v->count;
162
163   if (culling) {
164      for ( ; i < count ; STRIDE_F(d, v->stride), i++)
165	 if (cullmask[i])
166	    _mesa_printf(t, i, d[0], d[1], d[2], d[3]);
167   }
168   else {
169      for ( ; i < count ; STRIDE_F(d, v->stride), i++)
170	 _mesa_printf(t, i, d[0], d[1], d[2], d[3]);
171   }
172
173   for (j = v->size ; j < 4; j++) {
174      if ((v->flags & (1<<j)) == 0) {
175
176	 _mesa_printf("checking col %u is clean as advertised ", j);
177
178	 for (i = 0, d = (GLfloat *) v->data ;
179	      i < count && d[j] == c[j] ;
180	      i++, STRIDE_F(d, v->stride)) {};
181
182	 if (i == count)
183	    _mesa_printf(" --> ok\n");
184	 else
185	    _mesa_printf(" --> Failed at %u ******\n", i);
186      }
187   }
188}
189
190
191