Fixed MAXFIFO_S4. Removed WAIT_IDLE_EMPTY from savage_BCI_swap which resulted
[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(0, " 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 if (0) fprintf(stderr, "_tnl_vb_bind_vtx(): %d verts %d vertsize\n",
117 count, tnl->vtx.vertex_size);
118
119
120 /* Setup constant data in the VB.
121 */
122 VB->Count = count;
123 VB->Primitive = tnl->vtx.prim;
124 VB->PrimitiveCount = tnl->vtx.prim_count;
125 VB->Elts = NULL;
126 VB->NormalLengthPtr = NULL;
127
128 for (attr = 0; attr <= _TNL_ATTRIB_INDEX ; attr++) {
129 if (tnl->vtx.attrsz[attr]) {
130 tmp->Attribs[attr].count = count;
131 tmp->Attribs[attr].data = (GLfloat (*)[4]) data;
132 tmp->Attribs[attr].start = data;
133 tmp->Attribs[attr].size = tnl->vtx.attrsz[attr];
134 tmp->Attribs[attr].stride = tnl->vtx.vertex_size * sizeof(GLfloat);
135 VB->AttribPtr[attr] = &tmp->Attribs[attr];
136 data += tnl->vtx.attrsz[attr];
137 }
138 else {
139 /* VB->AttribPtr[attr] = &tnl->current.Attribs[attr]; */
140
141
142 tmp->Attribs[attr].count = count;
143 tmp->Attribs[attr].data = (GLfloat (*)[4]) tnl->vtx.current[attr];
144 tmp->Attribs[attr].start = tnl->vtx.current[attr];
145 tmp->Attribs[attr].size = get_size( tnl->vtx.current[attr] );
146 tmp->Attribs[attr].stride = 0;
147 VB->AttribPtr[attr] = &tmp->Attribs[attr];
148 }
149 }
150
151
152 /* Copy and translate EdgeFlag to a contiguous array of GLbooleans
153 */
154 if (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL) {
155 if (tnl->vtx.attrsz[_TNL_ATTRIB_EDGEFLAG]) {
156 VB->EdgeFlag = _tnl_translate_edgeflag( ctx, data, count,
157 tnl->vtx.vertex_size );
158 data++;
159 }
160 else
161 VB->EdgeFlag = _tnl_import_current_edgeflag( ctx, count );
162 }
163
164 /* Legacy pointers -- remove one day.
165 */
166 VB->ObjPtr = VB->AttribPtr[_TNL_ATTRIB_POS];
167 VB->NormalPtr = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
168 VB->ColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
169 VB->ColorPtr[1] = 0;
170 VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_INDEX];
171 VB->IndexPtr[1] = 0;
172 VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
173 VB->SecondaryColorPtr[1] = 0;
174 VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG];
175
176 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
177 VB->TexCoordPtr[i] = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i];
178 }
179 }
180
181
182 /*
183 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
184 * NOTE: Old 'parity' issue is gone.
185 */
186 static GLuint _tnl_copy_vertices( GLcontext *ctx )
187 {
188 TNLcontext *tnl = TNL_CONTEXT( ctx );
189 GLuint nr = tnl->vtx.prim[tnl->vtx.prim_count-1].count;
190 GLuint ovf, i;
191 GLuint sz = tnl->vtx.vertex_size;
192 GLfloat *dst = tnl->vtx.copied.buffer;
193 GLfloat *src = (tnl->vtx.buffer +
194 tnl->vtx.prim[tnl->vtx.prim_count-1].start *
195 tnl->vtx.vertex_size);
196
197
198 switch( ctx->Driver.CurrentExecPrimitive )
199 {
200 case GL_POINTS:
201 return 0;
202 case GL_LINES:
203 ovf = nr&1;
204 for (i = 0 ; i < ovf ; i++)
205 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
206 return i;
207 case GL_TRIANGLES:
208 ovf = nr%3;
209 for (i = 0 ; i < ovf ; i++)
210 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
211 return i;
212 case GL_QUADS:
213 ovf = nr&3;
214 for (i = 0 ; i < ovf ; i++)
215 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
216 return i;
217 case GL_LINE_STRIP:
218 if (nr == 0)
219 return 0;
220 else {
221 _mesa_memcpy( dst, src+(nr-1)*sz, sz * sizeof(GLfloat) );
222 return 1;
223 }
224 case GL_LINE_LOOP:
225 case GL_TRIANGLE_FAN:
226 case GL_POLYGON:
227 if (nr == 0)
228 return 0;
229 else if (nr == 1) {
230 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
231 return 1;
232 } else {
233 _mesa_memcpy( dst, src+0, sz * sizeof(GLfloat) );
234 _mesa_memcpy( dst+sz, src+(nr-1)*sz, sz * sizeof(GLfloat) );
235 return 2;
236 }
237 case GL_TRIANGLE_STRIP:
238 case GL_QUAD_STRIP:
239 switch (nr) {
240 case 0: ovf = 0; break;
241 case 1: ovf = 1; break;
242 default: ovf = 2 + (nr&1); break;
243 }
244 for (i = 0 ; i < ovf ; i++)
245 _mesa_memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz * sizeof(GLfloat) );
246 return i;
247 case GL_POLYGON+1:
248 return 0;
249 default:
250 assert(0);
251 return 0;
252 }
253 }
254
255
256 /**
257 * Execute the buffer and save copied verts.
258 */
259 void _tnl_flush_vtx( GLcontext *ctx )
260 {
261 TNLcontext *tnl = TNL_CONTEXT(ctx);
262
263 if (0)
264 _tnl_print_vtx( ctx );
265
266 if (tnl->vtx.prim_count &&
267 tnl->vtx.counter != tnl->vtx.initial_counter) {
268
269 tnl->vtx.copied.nr = _tnl_copy_vertices( ctx );
270
271 if (ctx->NewState)
272 _mesa_update_state( ctx );
273
274 if (tnl->pipeline.build_state_changes)
275 _tnl_validate_pipeline( ctx );
276
277 _tnl_vb_bind_vtx( ctx );
278
279 /* Invalidate all stored data before and after run:
280 */
281 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
282 tnl->Driver.RunPipeline( ctx );
283 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
284 }
285
286 tnl->vtx.prim_count = 0;
287 tnl->vtx.counter = tnl->vtx.initial_counter;
288 tnl->vtx.vbptr = tnl->vtx.buffer;
289 }