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