[PROPSYS]
[reactos.git] / reactos / dll / opengl / mesa / math / m_vector.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 3.5
4 *
5 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * New (3.1) transformation code written by Keith Whitwell.
27 */
28
29 #include <precomp.h>
30
31 #include "m_vector.h"
32
33 /**
34 * Given a vector [count][4] of floats, set all the [][elt] values
35 * to 0 (if elt = 0, 1, 2) or 1.0 (if elt = 3).
36 */
37 void
38 _mesa_vector4f_clean_elem( GLvector4f *vec, GLuint count, GLuint elt )
39 {
40 static const GLubyte elem_bits[4] = {
41 VEC_DIRTY_0,
42 VEC_DIRTY_1,
43 VEC_DIRTY_2,
44 VEC_DIRTY_3
45 };
46 static const GLfloat clean[4] = { 0, 0, 0, 1 };
47 const GLfloat v = clean[elt];
48 GLfloat (*data)[4] = (GLfloat (*)[4])vec->start;
49 GLuint i;
50
51 for (i = 0; i < count; i++)
52 data[i][elt] = v;
53
54 vec->flags &= ~elem_bits[elt];
55 }
56
57
58 static const GLubyte size_bits[5] = {
59 0,
60 VEC_SIZE_1,
61 VEC_SIZE_2,
62 VEC_SIZE_3,
63 VEC_SIZE_4,
64 };
65
66
67 /**
68 * Initialize GLvector objects.
69 * \param v the vector object to initialize.
70 * \param flags bitwise-OR of VEC_* flags
71 * \param storage pointer to storage for the vector's data
72 */
73 void
74 _mesa_vector4f_init( GLvector4f *v, GLbitfield flags, GLfloat (*storage)[4] )
75 {
76 v->stride = 4 * sizeof(GLfloat);
77 v->size = 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
78 v->data = storage;
79 v->start = (GLfloat *) storage;
80 v->count = 0;
81 v->flags = size_bits[4] | flags;
82 }
83
84
85 /**
86 * Initialize GLvector objects and allocate storage.
87 * \param v the vector object
88 * \param flags bitwise-OR of VEC_* flags
89 * \param count number of elements to allocate in vector
90 * \param alignment desired memory alignment for the data (in bytes)
91 */
92 void
93 _mesa_vector4f_alloc( GLvector4f *v, GLbitfield flags, GLuint count,
94 GLuint alignment )
95 {
96 v->stride = 4 * sizeof(GLfloat);
97 v->size = 2;
98 v->storage = _mesa_align_malloc( count * 4 * sizeof(GLfloat), alignment );
99 v->storage_count = count;
100 v->start = (GLfloat *) v->storage;
101 v->data = (GLfloat (*)[4]) v->storage;
102 v->count = 0;
103 v->flags = size_bits[4] | flags | VEC_MALLOC;
104 }
105
106
107 /**
108 * Vector deallocation. Free whatever memory is pointed to by the
109 * vector's storage field if the VEC_MALLOC flag is set.
110 * DO NOT free the GLvector object itself, though.
111 */
112 void
113 _mesa_vector4f_free( GLvector4f *v )
114 {
115 if (v->flags & VEC_MALLOC) {
116 _mesa_align_free( v->storage );
117 v->data = NULL;
118 v->start = NULL;
119 v->storage = NULL;
120 v->flags &= ~VEC_MALLOC;
121 }
122 }
123
124
125 /**
126 * For debugging
127 */
128 void
129 _mesa_vector4f_print( const GLvector4f *v, const GLubyte *cullmask,
130 GLboolean culling )
131 {
132 static const GLfloat c[4] = { 0, 0, 0, 1 };
133 static const char *templates[5] = {
134 "%d:\t0, 0, 0, 1\n",
135 "%d:\t%f, 0, 0, 1\n",
136 "%d:\t%f, %f, 0, 1\n",
137 "%d:\t%f, %f, %f, 1\n",
138 "%d:\t%f, %f, %f, %f\n"
139 };
140
141 const char *t = templates[v->size];
142 GLfloat *d = (GLfloat *)v->data;
143 GLuint j, i = 0, count;
144
145 printf("data-start\n");
146 for (; d != v->start; STRIDE_F(d, v->stride), i++)
147 printf(t, i, d[0], d[1], d[2], d[3]);
148
149 printf("start-count(%u)\n", v->count);
150 count = i + v->count;
151
152 if (culling) {
153 for (; i < count; STRIDE_F(d, v->stride), i++)
154 if (cullmask[i])
155 printf(t, i, d[0], d[1], d[2], d[3]);
156 }
157 else {
158 for (; i < count; STRIDE_F(d, v->stride), i++)
159 printf(t, i, d[0], d[1], d[2], d[3]);
160 }
161
162 for (j = v->size; j < 4; j++) {
163 if ((v->flags & (1<<j)) == 0) {
164
165 printf("checking col %u is clean as advertised ", j);
166
167 for (i = 0, d = (GLfloat *) v->data;
168 i < count && d[j] == c[j];
169 i++, STRIDE_F(d, v->stride)) {
170 /* no-op */
171 }
172
173 if (i == count)
174 printf(" --> ok\n");
175 else
176 printf(" --> Failed at %u ******\n", i);
177 }
178 }
179 }