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