Replace PipelineStart, PipelineFinish with RunPipeline. Clean up
[mesa.git] / src / mesa / tnl / t_array_api.c
1 /* $Id: t_array_api.c,v 1.13 2001/05/10 12:18:38 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30 #include "glheader.h"
31 #include "api_validate.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "mmath.h"
35 #include "mem.h"
36 #include "state.h"
37 #include "mtypes.h"
38
39 #include "array_cache/acache.h"
40
41 #include "t_array_api.h"
42 #include "t_array_import.h"
43 #include "t_imm_api.h"
44 #include "t_imm_exec.h"
45 #include "t_context.h"
46 #include "t_pipeline.h"
47
48 static void fallback_drawarrays( GLcontext *ctx, GLenum mode, GLint start,
49 GLsizei count )
50 {
51 /* Need to produce immediate structs, either for compiling or
52 * because the array range is too large to process in a single
53 * VB. In GL_EXECUTE mode, this introduces two redundant
54 * operations: producing the flag array and computing the orflag
55 * of the flag array.
56 */
57 #if 1
58 if (_tnl_hard_begin( ctx, mode )) {
59 GLint j;
60 for (j = 0 ; j < count ; ) {
61 struct immediate *IM = TNL_CURRENT_IM(ctx);
62 GLuint nr = MIN2( IMM_MAXDATA - IM->Start, (GLuint) (count - j) );
63 GLuint sf = IM->Flag[IM->Start];
64
65 _tnl_fill_immediate_drawarrays( ctx, IM, j, j+nr );
66
67 if (j == 0) IM->Flag[IM->Start] |= sf;
68
69 IM->Count = IM->Start + nr;
70 j += nr;
71
72 if (j == count)
73 _tnl_end( ctx );
74
75 _tnl_flush_immediate( IM );
76 }
77 }
78 #else
79 /* Simple alternative to above code.
80 */
81 if (_tnl_hard_begin( ctx, mode ))
82 {
83 GLuint i;
84 for (i=start;i<count;i++) {
85 _tnl_array_element( ctx, i );
86 }
87 _tnl_end( ctx );
88 }
89 #endif
90 }
91
92
93 static void fallback_drawelements( GLcontext *ctx, GLenum mode, GLsizei count,
94 const GLuint *indices)
95 {
96 #if 1
97 /* Optimized code that fakes the effect of calling
98 * _tnl_array_element for each index in the list.
99 */
100 if (_tnl_hard_begin( ctx, mode )) {
101 GLint i, j;
102 for (j = 0 ; j < count ; ) {
103 struct immediate *IM = TNL_CURRENT_IM(ctx);
104 GLuint start = IM->Start;
105 GLint nr = MIN2( (GLint) (IMM_MAXDATA - start), count - j ) + start;
106 GLuint sf = IM->Flag[start];
107 IM->FlushElt |= 1;
108
109 for (i = start ; i < nr ; i++) {
110 IM->Elt[i] = (GLuint) *indices++;
111 IM->Flag[i] = VERT_ELT;
112 }
113
114 if (j == 0) IM->Flag[start] |= sf;
115
116 IM->Count = nr;
117 j += nr - start;
118
119 if (j == count)
120 _tnl_end( ctx );
121
122 _tnl_flush_immediate( IM );
123 }
124 }
125 #else
126 /* Simple version of the above code.
127 */
128 if (_tnl_hard_begin(ctx, mode)) {
129 GLuint i;
130 for (i = 0 ; i < count ; i++)
131 _tnl_array_element( ctx, indices[i] );
132 _tnl_end( ctx );
133 }
134 #endif
135 }
136
137
138 static void _tnl_draw_range_elements( GLcontext *ctx, GLenum mode,
139 GLuint start, GLuint end,
140 GLsizei count, const GLuint *indices )
141
142 {
143 TNLcontext *tnl = TNL_CONTEXT(ctx);
144 FLUSH_CURRENT( ctx, 0 );
145
146 _tnl_vb_bind_arrays( ctx, start, end );
147
148 tnl->vb.FirstPrimitive = 0;
149 tnl->vb.Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
150 tnl->vb.PrimitiveLength[0] = count;
151 tnl->vb.Elts = (GLuint *)indices;
152
153 if (ctx->Array.LockCount)
154 tnl->Driver.RunPipeline( ctx );
155 else {
156 /* Note that arrays may have changed before/after execution.
157 */
158 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
159 tnl->Driver.RunPipeline( ctx );
160 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
161 }
162 }
163
164
165
166
167 void
168 _tnl_DrawArrays(GLenum mode, GLint start, GLsizei count)
169 {
170 GET_CURRENT_CONTEXT(ctx);
171 TNLcontext *tnl = TNL_CONTEXT(ctx);
172 struct vertex_buffer *VB = &tnl->vb;
173
174 /* Check arguments, etc.
175 */
176 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
177 return;
178
179 if (tnl->pipeline.build_state_changes)
180 _tnl_validate_pipeline( ctx );
181
182 if (ctx->CompileFlag) {
183 fallback_drawarrays( ctx, mode, start, count );
184 }
185 else if (count - start < (GLint) ctx->Const.MaxArrayLockSize) {
186
187 /* Small primitives which can fit in a single vertex buffer:
188 */
189 FLUSH_CURRENT( ctx, 0 );
190
191 if (ctx->Array.LockCount)
192 {
193 if (start < (GLint) ctx->Array.LockFirst)
194 start = ctx->Array.LockFirst;
195 if (count > (GLint) ctx->Array.LockCount)
196 count = ctx->Array.LockCount;
197 if (start >= count)
198 return;
199
200 /* Locked drawarrays. Reuse any previously transformed data.
201 */
202 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
203 VB->FirstPrimitive = start;
204 VB->Primitive[start] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
205 VB->PrimitiveLength[start] = count - start;
206 tnl->Driver.RunPipeline( ctx );
207 } else {
208 /* The arrays are small enough to fit in a single VB; just bind
209 * them and go. Any untransformed data will be copied on
210 * clipping.
211 *
212 * Invalidate any cached data dependent on these arrays.
213 */
214 _tnl_vb_bind_arrays( ctx, start, count );
215 VB->FirstPrimitive = 0;
216 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
217 VB->PrimitiveLength[0] = count - start;
218 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
219 tnl->Driver.RunPipeline( ctx );
220 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
221 }
222 }
223 else {
224 int bufsz = (ctx->Const.MaxArrayLockSize - 4) & ~3;
225 int j, nr;
226 int minimum, modulo, skip;
227
228 /* Large primitives requiring decomposition to multiple vertex
229 * buffers:
230 */
231 switch (mode) {
232 case GL_POINTS:
233 minimum = 0;
234 modulo = 1;
235 skip = 0;
236 case GL_LINES:
237 minimum = 1;
238 modulo = 2;
239 skip = 1;
240 case GL_LINE_STRIP:
241 minimum = 1;
242 modulo = 1;
243 skip = 0;
244 break;
245 case GL_TRIANGLES:
246 minimum = 2;
247 modulo = 3;
248 skip = 2;
249 break;
250 case GL_TRIANGLE_STRIP:
251 minimum = 2;
252 modulo = 1;
253 skip = 0;
254 break;
255 case GL_QUADS:
256 minimum = 3;
257 modulo = 4;
258 skip = 3;
259 break;
260 case GL_QUAD_STRIP:
261 minimum = 3;
262 modulo = 2;
263 skip = 0;
264 break;
265 case GL_LINE_LOOP:
266 case GL_TRIANGLE_FAN:
267 case GL_POLYGON:
268 default:
269 /* Primitives requiring a copied vertex (fan-like primitives)
270 * must use the slow path:
271 */
272 fallback_drawarrays( ctx, mode, start, count );
273 return;
274 }
275
276 FLUSH_CURRENT( ctx, 0 );
277
278 /* fprintf(stderr, "start %d count %d min %d modulo %d skip %d\n", */
279 /* start, count, minimum, modulo, skip); */
280
281 for (j = start + minimum ; j < count ; j += nr + skip ) {
282
283 nr = MIN2( bufsz, count - j );
284 nr -= nr % modulo;
285
286 /* fprintf(stderr, "%d..%d\n", j - minimum, j+nr); */
287
288 _tnl_vb_bind_arrays( ctx, j - minimum, j + nr );
289
290 VB->FirstPrimitive = 0;
291 VB->Primitive[0] = mode | PRIM_BEGIN | PRIM_END | PRIM_LAST;
292 VB->PrimitiveLength[0] = nr + minimum;
293 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
294 tnl->Driver.RunPipeline( ctx );
295 tnl->pipeline.run_input_changes |= ctx->Array._Enabled;
296 }
297 }
298 }
299
300
301 void
302 _tnl_DrawRangeElements(GLenum mode,
303 GLuint start, GLuint end,
304 GLsizei count, GLenum type, const GLvoid *indices)
305 {
306 GET_CURRENT_CONTEXT(ctx);
307 TNLcontext *tnl = TNL_CONTEXT(ctx);
308 GLuint *ui_indices;
309
310 /* Check arguments, etc.
311 */
312 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
313 type, indices ))
314 return;
315
316 if (tnl->pipeline.build_state_changes)
317 _tnl_validate_pipeline( ctx );
318
319 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
320 count, type, indices );
321
322
323 if (ctx->Array.LockCount) {
324 /* Are the arrays already locked? If so we currently have to look
325 * at the whole locked range.
326 */
327 if (start >= ctx->Array.LockFirst && end <= ctx->Array.LockCount)
328 _tnl_draw_range_elements( ctx, mode,
329 ctx->Array.LockFirst,
330 ctx->Array.LockCount,
331 count, ui_indices );
332 else {
333 /* The spec says referencing elements outside the locked
334 * range is undefined. I'm going to make it a noop this time
335 * round, maybe come up with something beter before 3.6.
336 *
337 * May be able to get away with just setting LockCount==0,
338 * though this raises the problems of dependent state. May
339 * have to call glUnlockArrays() directly?
340 *
341 * Or scan the list and replace bad indices?
342 */
343 _mesa_problem( ctx,
344 "DrawRangeElements references "
345 "elements outside locked range.");
346 }
347 }
348 else if (end + 1 - start < ctx->Const.MaxArrayLockSize) {
349 /* The arrays aren't locked but we can still fit them inside a
350 * single vertexbuffer.
351 */
352 _tnl_draw_range_elements( ctx, mode, start, end + 1, count, ui_indices );
353 } else {
354 /* Range is too big to optimize:
355 */
356 fallback_drawelements( ctx, mode, count, ui_indices );
357 }
358 }
359
360
361
362 void
363 _tnl_DrawElements(GLenum mode, GLsizei count, GLenum type,
364 const GLvoid *indices)
365 {
366 GET_CURRENT_CONTEXT(ctx);
367 TNLcontext *tnl = TNL_CONTEXT(ctx);
368 GLuint *ui_indices;
369
370 /* Check arguments, etc.
371 */
372 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
373 return;
374
375 if (tnl->pipeline.build_state_changes)
376 _tnl_validate_pipeline( ctx );
377
378 ui_indices = (GLuint *)_ac_import_elements( ctx, GL_UNSIGNED_INT,
379 count, type, indices );
380
381 if (ctx->Array.LockCount) {
382 _tnl_draw_range_elements( ctx, mode,
383 ctx->Array.LockFirst,
384 ctx->Array.LockCount,
385 count, ui_indices );
386 }
387 else {
388 /* Scan the index list and see if we can use the locked path anyway.
389 */
390 GLuint max_elt = 0;
391 GLint i;
392
393 for (i = 0 ; i < count ; i++)
394 if (ui_indices[i] > max_elt)
395 max_elt = ui_indices[i];
396
397 if (max_elt < ctx->Const.MaxArrayLockSize && /* can we use it? */
398 max_elt < (GLuint) count) /* do we want to use it? */
399 _tnl_draw_range_elements( ctx, mode, 0, max_elt+1, count, ui_indices );
400 else
401 fallback_drawelements( ctx, mode, count, ui_indices );
402 }
403 }
404
405
406 void _tnl_array_init( GLcontext *ctx )
407 {
408 TNLcontext *tnl = TNL_CONTEXT(ctx);
409 struct vertex_arrays *tmp = &tnl->array_inputs;
410 GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->vtxfmt);
411 GLuint i;
412
413 vfmt->DrawArrays = _tnl_DrawArrays;
414 vfmt->DrawElements = _tnl_DrawElements;
415 vfmt->DrawRangeElements = _tnl_DrawRangeElements;
416
417 /* Setup vector pointers that will be used to bind arrays to VB's.
418 */
419 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
420 _mesa_vector3f_init( &tmp->Normal, 0, 0 );
421 _mesa_vector1f_init( &tmp->FogCoord, 0, 0 );
422 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
423 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
424
425 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
426 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
427
428 tnl->tmp_primitive = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
429 tnl->tmp_primitive_length = (GLuint *)MALLOC(sizeof(GLuint)*tnl->vb.Size);
430 }
431
432
433 void _tnl_array_destroy( GLcontext *ctx )
434 {
435 TNLcontext *tnl = TNL_CONTEXT(ctx);
436 if (tnl->tmp_primitive_length) FREE(tnl->tmp_primitive_length);
437 if (tnl->tmp_primitive) FREE(tnl->tmp_primitive);
438 }