f25b7b20642df81f8c105c6e6b7650eeccbd0fcf
[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 #include <stddef.h>
31
32 #include "main/glheader.h"
33 #include "main/imports.h"
34 #include "main/macros.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 */
44 void
45 _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
65 static const GLubyte size_bits[5] = {
66 0,
67 VEC_SIZE_1,
68 VEC_SIZE_2,
69 VEC_SIZE_3,
70 VEC_SIZE_4,
71 };
72
73
74 /**
75 * Initialize GLvector objects.
76 * \param v the vector object to initialize.
77 * \param flags bitwise-OR of VEC_* flags
78 * \param storage pointer to storage for the vector's data
79 */
80 void
81 _mesa_vector4f_init( GLvector4f *v, GLbitfield flags, GLfloat (*storage)[4] )
82 {
83 STATIC_ASSERT(V4F_DATA == offsetof(GLvector4f, data));
84 STATIC_ASSERT(V4F_START == offsetof(GLvector4f, start));
85 STATIC_ASSERT(V4F_COUNT == offsetof(GLvector4f, count));
86 STATIC_ASSERT(V4F_STRIDE == offsetof(GLvector4f, stride));
87 STATIC_ASSERT(V4F_SIZE == offsetof(GLvector4f, size));
88 STATIC_ASSERT(V4F_FLAGS == offsetof(GLvector4f, flags));
89
90 v->stride = 4 * sizeof(GLfloat);
91 v->size = 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
92 v->data = storage;
93 v->start = (GLfloat *) storage;
94 v->count = 0;
95 v->flags = size_bits[4] | flags;
96 }
97
98
99 /**
100 * Initialize GLvector objects and allocate storage.
101 * \param v the vector object
102 * \param flags bitwise-OR of VEC_* flags
103 * \param count number of elements to allocate in vector
104 * \param alignment desired memory alignment for the data (in bytes)
105 */
106 void
107 _mesa_vector4f_alloc( GLvector4f *v, GLbitfield flags, GLuint count,
108 GLuint alignment )
109 {
110 v->stride = 4 * sizeof(GLfloat);
111 v->size = 2;
112 v->storage = _mesa_align_malloc( count * 4 * sizeof(GLfloat), alignment );
113 v->storage_count = count;
114 v->start = (GLfloat *) v->storage;
115 v->data = (GLfloat (*)[4]) v->storage;
116 v->count = 0;
117 v->flags = size_bits[4] | flags | VEC_MALLOC;
118 }
119
120
121 /**
122 * Vector deallocation. Free whatever memory is pointed to by the
123 * vector's storage field if the VEC_MALLOC flag is set.
124 * DO NOT free the GLvector object itself, though.
125 */
126 void
127 _mesa_vector4f_free( GLvector4f *v )
128 {
129 if (v->flags & VEC_MALLOC) {
130 _mesa_align_free( v->storage );
131 v->data = NULL;
132 v->start = NULL;
133 v->storage = NULL;
134 v->flags &= ~VEC_MALLOC;
135 }
136 }
137
138
139 /**
140 * For debugging
141 */
142 void
143 _mesa_vector4f_print( const GLvector4f *v, const GLubyte *cullmask,
144 GLboolean culling )
145 {
146 static const GLfloat c[4] = { 0, 0, 0, 1 };
147 static const char *templates[5] = {
148 "%d:\t0, 0, 0, 1\n",
149 "%d:\t%f, 0, 0, 1\n",
150 "%d:\t%f, %f, 0, 1\n",
151 "%d:\t%f, %f, %f, 1\n",
152 "%d:\t%f, %f, %f, %f\n"
153 };
154
155 const char *t = templates[v->size];
156 GLfloat *d = (GLfloat *)v->data;
157 GLuint j, i = 0, count;
158
159 printf("data-start\n");
160 for (; d != v->start; STRIDE_F(d, v->stride), i++)
161 printf(t, i, d[0], d[1], d[2], d[3]);
162
163 printf("start-count(%u)\n", v->count);
164 count = i + v->count;
165
166 if (culling) {
167 for (; i < count; STRIDE_F(d, v->stride), i++)
168 if (cullmask[i])
169 printf(t, i, d[0], d[1], d[2], d[3]);
170 }
171 else {
172 for (; i < count; STRIDE_F(d, v->stride), i++)
173 printf(t, i, d[0], d[1], d[2], d[3]);
174 }
175
176 for (j = v->size; j < 4; j++) {
177 if ((v->flags & (1<<j)) == 0) {
178
179 printf("checking col %u is clean as advertised ", j);
180
181 for (i = 0, d = (GLfloat *) v->data;
182 i < count && d[j] == c[j];
183 i++, STRIDE_F(d, v->stride)) {
184 /* no-op */
185 }
186
187 if (i == count)
188 printf(" --> ok\n");
189 else
190 printf(" --> Failed at %u ******\n", i);
191 }
192 }
193 }