fix unitialized values in radeonClear
[mesa.git] / src / mesa / tnl / t_vtx_exec.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 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 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "glheader.h"
29 #include "api_eval.h"
30 #include "context.h"
31 #include "enums.h"
32 #include "state.h"
33 #include "macros.h"
34 #include "math/m_eval.h"
35 #include "t_vtx_api.h"
36 #include "t_pipeline.h"
37
38
39 static void _tnl_print_vtx( GLcontext *ctx )
40 {
41 TNLcontext *tnl = TNL_CONTEXT(ctx);
42 GLuint count = tnl->vtx.initial_counter - tnl->vtx.counter;
43 GLuint i;
44
45 _mesa_debug(ctx, "_tnl_print_vtx: %u vertices %d primitives, %d vertsize\n",
46 count,
47 tnl->vtx.prim_count,
48 tnl->vtx.vertex_size);
49
50 for (i = 0 ; i < tnl->vtx.prim_count ; i++) {
51 struct tnl_prim *prim = &tnl->vtx.prim[i];
52 _mesa_debug(NULL, " prim %d: %s %d..%d %s %s\n",
53 i,
54 _mesa_lookup_enum_by_nr(prim->mode & PRIM_MODE_MASK),
55 prim->start,
56 prim->start + prim->count,
57 (prim->mode & PRIM_BEGIN) ? "BEGIN" : "(wrap)",
58 (prim->mode & PRIM_END) ? "END" : "(wrap)");
59 }
60 }
61
62 GLboolean *_tnl_translate_edgeflag( GLcontext *ctx, const GLfloat *data,
63 GLuint count, GLuint stride )
64 {
65 TNLcontext *tnl = TNL_CONTEXT(ctx);
66 GLboolean *ef = tnl->vtx.edgeflag_tmp;
67 GLuint i;
68
69 if (!ef)
70 ef = tnl->vtx.edgeflag_tmp = (GLboolean *) MALLOC( tnl->vb.Size );
71
72 for (i = 0 ; i < count ; i++, data += stride)
73 ef[i] = (data[0] == 1.0);
74
75 return ef;
76 }
77
78
79 GLboolean *_tnl_import_current_edgeflag( GLcontext *ctx,
80 GLuint count )
81 {
82 TNLcontext *tnl = TNL_CONTEXT(ctx);
83 GLboolean *ef = tnl->vtx.edgeflag_tmp;
84 GLboolean tmp = ctx->Current.EdgeFlag;
85 GLuint i;
86
87 if (!ef)
88 ef = tnl->vtx.edgeflag_tmp = (GLboolean *) MALLOC( tnl->vb.Size );
89
90 for (i = 0 ; i < count ; i++)
91 ef[i] = tmp;
92
93 return ef;
94 }
95
96 static INLINE GLint get_size( const GLfloat *f )
97 {
98 if (f[3] != 1.0) return 4;
99 if (f[2] != 0.0) return 3;
100 return 2;
101 }
102
103 /* Some nasty stuff still hanging on here.
104 *
105 * TODO - remove VB->NormalPtr, etc and just use the AttrPtr's.
106 */
107 static void _tnl_vb_bind_vtx( GLcontext *ctx )
108 {
109 TNLcontext *tnl = TNL_CONTEXT(ctx);
110 struct vertex_buffer *VB = &tnl->vb;
111 struct tnl_vertex_arrays *tmp = &tnl->vtx_inputs;
112 GLfloat *data = tnl->vtx.buffer;
113 GLuint count = tnl->vtx.initial_counter - tnl->vtx.counter;
114 GLuint attr, i;
115
116 #undef DEBUG_VTX
117
118 #ifdef DEBUG_VTX
119 fprintf(stderr, "_tnl_vb_bind_vtx(): %d verts %d vertsize\n",
120 count, tnl->vtx.vertex_size);
121 #endif
122
123
124 /* Setup constant data in the VB.
125 */
126 VB->Count = count;
127 VB->Primitive = tnl->vtx.prim;
128 VB->PrimitiveCount = tnl->vtx.prim_count;
129 VB->Elts = NULL;
130 VB->NormalLengthPtr = NULL;
131
132 for (attr = 0; attr <= _TNL_ATTRIB_EDGEFLAG ; attr++) {
133 if (tnl->vtx.attrsz[attr]) {
134 tmp->Attribs[attr].count = count;
135 tmp->Attribs[attr].data = (GLfloat (*)[4]) data;
136 tmp->Attribs[attr].start = data;
137 tmp->Attribs[attr].size = tnl->vtx.attrsz[attr];
138 tmp->Attribs[attr].stride = tnl->vtx.vertex_size * sizeof(GLfloat);
139 VB->AttribPtr[attr] = &tmp->Attribs[attr];
140 data += tnl->vtx.attrsz[attr];
141 }
142 else {
143 /* VB->AttribPtr[attr] = &tnl->current.Attribs[attr]; */
144
145
146 tmp->Attribs[attr].count = 1;
147 tmp->Attribs[attr].data = (GLfloat (*)[4]) tnl->vtx.current[attr];
148 tmp->Attribs[attr].start = tnl->vtx.current[attr];
149 tmp->Attribs[attr].size = get_size( tnl->vtx.current[attr] );
150 tmp->Attribs[attr].stride = 0;
151 VB->AttribPtr[attr] = &tmp->Attribs[attr];
152 }
153 }
154
155
156 /* Copy and translate EdgeFlag to a contiguous array of GLbooleans
157 */
158 if (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL) {
159 if (tnl->vtx.attrsz[_TNL_ATTRIB_EDGEFLAG]) {
160 VB->EdgeFlag = _tnl_translate_edgeflag( ctx, data, count,
161 tnl->vtx.vertex_size );
162 data++;
163 }
164 else
165 VB->EdgeFlag = _tnl_import_current_edgeflag( ctx, count );
166 }
167
168 /* Legacy pointers -- remove one day.
169 */
170 VB->ObjPtr = VB->AttribPtr[_TNL_ATTRIB_POS];
171 VB->NormalPtr = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
172 VB->ColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
173 VB->ColorPtr[1] = NULL;
174 VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
175 VB->SecondaryColorPtr[1] = NULL;
176 VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR_INDEX];
177 VB->IndexPtr[1] = NULL;
178 VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG];
179
180 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
181 VB->TexCoordPtr[i] = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i];
182 }
183 }
184
185
186 /*
187 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
188 * NOTE: Old 'parity' issue is gone.
189 */
190 static GLuint _tnl_copy_vertices( GLcontext *ctx )
191 {
192 TNLcontext *tnl = TNL_CONTEXT( ctx );
193 GLuint nr = tnl->vtx.prim[tnl->vtx.prim_count-1].count;
194 GLuint ovf, i;
195 GLuint sz = tnl->vtx.vertex_size;
196 GLfloat *dst = tnl->vtx.copied.buffer;
197 GLfloat *src = (tnl->vtx.buffer +
198 tnl->vtx.prim[tnl->vtx.prim_count-1].start *
199 tnl->vtx.vertex_size);
200
201
202 switch( ctx->Driver.CurrentExecPrimitive )
203 {
204 case GL_POINTS:
205 return 0;
206 case GL_LINES:
207 ovf = nr&1;
208 for (i = 0 ; i < ovf ; i++)
209 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
210 return i;
211 case GL_TRIANGLES:
212 ovf = nr%3;
213 for (i = 0 ; i < ovf ; i++)
214 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
215 return i;
216 case GL_QUADS:
217 ovf = nr&3;
218 for (i = 0 ; i < ovf ; i++)
219 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
220 return i;
221 case GL_LINE_STRIP:
222 if (nr == 0)
223 return 0;
224 else {
225 _mesa_memcpy( dst, src+(nr-1)*sz, sz * sizeof(GLfloat) );
226 return 1;
227 }
228 case GL_LINE_LOOP:
229 case GL_TRIANGLE_FAN:
230 case GL_POLYGON:
231 if (nr == 0)
232 return 0;
233 else if (nr == 1) {
234 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
235 return 1;
236 } else {
237 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
238 _mesa_memcpy( dst+sz, src+(nr-1)*sz, sz * sizeof(GLfloat) );
239 return 2;
240 }
241 case GL_TRIANGLE_STRIP:
242 /* no parity issue, but need to make sure the tri is not drawn twice */
243 if (nr & 1) {
244 tnl->vtx.prim[tnl->vtx.prim_count-1].count--;
245 }
246 /* fallthrough */
247 case GL_QUAD_STRIP:
248 switch (nr) {
249 case 0: ovf = 0; break;
250 case 1: ovf = 1; break;
251 default: ovf = 2 + (nr&1); break;
252 }
253 for (i = 0 ; i < ovf ; i++)
254 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
255 return i;
256 case PRIM_OUTSIDE_BEGIN_END:
257 return 0;
258 default:
259 assert(0);
260 return 0;
261 }
262 }
263
264
265 /**
266 * Execute the buffer and save copied verts.
267 */
268 void _tnl_flush_vtx( GLcontext *ctx )
269 {
270 TNLcontext *tnl = TNL_CONTEXT(ctx);
271 GLuint vertex_count = tnl->vtx.initial_counter - tnl->vtx.counter;
272
273 if (0)
274 _tnl_print_vtx( ctx );
275
276 if (tnl->vtx.prim_count && vertex_count) {
277
278 tnl->vtx.copied.nr = _tnl_copy_vertices( ctx );
279
280 if (tnl->vtx.copied.nr != vertex_count) {
281 if (ctx->NewState)
282 _mesa_update_state( ctx );
283
284 _tnl_vb_bind_vtx( ctx );
285
286 tnl->Driver.RunPipeline( ctx );
287 }
288 }
289
290 tnl->vtx.prim_count = 0;
291 tnl->vtx.counter = tnl->vtx.initial_counter;
292 tnl->vtx.vbptr = tnl->vtx.buffer;
293 }