mesa: Replace gen_matypes with a simple header for V4F/mat layout.
[mesa.git] / src / mesa / math / m_vector.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * New (3.1) transformation code written by Keith Whitwell.
27 */
28
29 #include <stdio.h>
30
31 #include "main/glheader.h"
32 #include "main/imports.h"
33 #include "main/macros.h"
34
35 #include "m_vector.h"
36
37
38
39 /**
40 * Given a vector [count][4] of floats, set all the [][elt] values
41 * to 0 (if elt = 0, 1, 2) or 1.0 (if elt = 3).
42 */
43 void
44 _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
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 * Initialize GLvector objects.
75 * \param v the vector object to initialize.
76 * \param flags bitwise-OR of VEC_* flags
77 * \param storage pointer to storage for the vector's data
78 */
79 void
80 _mesa_vector4f_init( GLvector4f *v, GLbitfield flags, GLfloat (*storage)[4] )
81 {
82 STATIC_ASSERT(V4F_DATA == offsetof(GLvector4f, data));
83 STATIC_ASSERT(V4F_START == offsetof(GLvector4f, start));
84 STATIC_ASSERT(V4F_COUNT == offsetof(GLvector4f, count));
85 STATIC_ASSERT(V4F_STRIDE == offsetof(GLvector4f, stride));
86 STATIC_ASSERT(V4F_SIZE == offsetof(GLvector4f, size));
87 STATIC_ASSERT(V4F_FLAGS == offsetof(GLvector4f, flags));
88
89 v->stride = 4 * sizeof(GLfloat);
90 v->size = 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
91 v->data = storage;
92 v->start = (GLfloat *) storage;
93 v->count = 0;
94 v->flags = size_bits[4] | flags;
95 }
96
97
98 /**
99 * Initialize GLvector objects and allocate storage.
100 * \param v the vector object
101 * \param flags bitwise-OR of VEC_* flags
102 * \param count number of elements to allocate in vector
103 * \param alignment desired memory alignment for the data (in bytes)
104 */
105 void
106 _mesa_vector4f_alloc( GLvector4f *v, GLbitfield flags, GLuint count,
107 GLuint alignment )
108 {
109 v->stride = 4 * sizeof(GLfloat);
110 v->size = 2;
111 v->storage = _mesa_align_malloc( count * 4 * sizeof(GLfloat), alignment );
112 v->storage_count = count;
113 v->start = (GLfloat *) v->storage;
114 v->data = (GLfloat (*)[4]) v->storage;
115 v->count = 0;
116 v->flags = size_bits[4] | flags | VEC_MALLOC;
117 }
118
119
120 /**
121 * Vector deallocation. Free whatever memory is pointed to by the
122 * vector's storage field if the VEC_MALLOC flag is set.
123 * DO NOT free the GLvector object itself, though.
124 */
125 void
126 _mesa_vector4f_free( GLvector4f *v )
127 {
128 if (v->flags & VEC_MALLOC) {
129 _mesa_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 */
141 void
142 _mesa_vector4f_print( const GLvector4f *v, const GLubyte *cullmask,
143 GLboolean culling )
144 {
145 static const GLfloat c[4] = { 0, 0, 0, 1 };
146 static const char *templates[5] = {
147 "%d:\t0, 0, 0, 1\n",
148 "%d:\t%f, 0, 0, 1\n",
149 "%d:\t%f, %f, 0, 1\n",
150 "%d:\t%f, %f, %f, 1\n",
151 "%d:\t%f, %f, %f, %f\n"
152 };
153
154 const char *t = templates[v->size];
155 GLfloat *d = (GLfloat *)v->data;
156 GLuint j, i = 0, count;
157
158 printf("data-start\n");
159 for (; d != v->start; STRIDE_F(d, v->stride), i++)
160 printf(t, i, d[0], d[1], d[2], d[3]);
161
162 printf("start-count(%u)\n", v->count);
163 count = i + v->count;
164
165 if (culling) {
166 for (; i < count; STRIDE_F(d, v->stride), i++)
167 if (cullmask[i])
168 printf(t, i, d[0], d[1], d[2], d[3]);
169 }
170 else {
171 for (; i < count; STRIDE_F(d, v->stride), i++)
172 printf(t, i, d[0], d[1], d[2], d[3]);
173 }
174
175 for (j = v->size; j < 4; j++) {
176 if ((v->flags & (1<<j)) == 0) {
177
178 printf("checking col %u is clean as advertised ", j);
179
180 for (i = 0, d = (GLfloat *) v->data;
181 i < count && d[j] == c[j];
182 i++, STRIDE_F(d, v->stride)) {
183 /* no-op */
184 }
185
186 if (i == count)
187 printf(" --> ok\n");
188 else
189 printf(" --> Failed at %u ******\n", i);
190 }
191 }
192 }