disable debug printf's, fix a crash
[mesa.git] / src / mesa / tnl / t_imm_exec.c
1 /* $Id: t_imm_exec.c,v 1.32 2001/12/15 02:13:32 brianp 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
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "dlist.h"
36 #include "macros.h"
37 #include "mem.h"
38 #include "mmath.h"
39 #include "light.h"
40 #include "state.h"
41 #include "mtypes.h"
42
43 #include "math/m_matrix.h"
44 #include "math/m_xform.h"
45
46 #include "t_context.h"
47 #include "t_array_import.h"
48 #include "t_imm_alloc.h"
49 #include "t_imm_api.h"
50 #include "t_imm_debug.h"
51 #include "t_imm_dlist.h"
52 #include "t_imm_eval.h"
53 #include "t_imm_elt.h"
54 #include "t_imm_exec.h"
55 #include "t_imm_fixup.h"
56 #include "t_pipeline.h"
57
58
59
60 static void reset_input( GLcontext *ctx,
61 GLuint start,
62 GLuint beginstate,
63 GLuint savedbeginstate )
64 {
65 struct immediate *IM = TNL_CURRENT_IM(ctx);
66
67 /* Clear the dirty part of the flag array.
68 */
69 if (start < IM->Count+2)
70 MEMSET(IM->Flag + start, 0, sizeof(GLuint) * (IM->Count+2-start));
71
72 IM->Start = start;
73 IM->Count = start;
74 IM->LastMaterial = start;
75 IM->BeginState = beginstate;
76 IM->SavedBeginState = savedbeginstate;
77 IM->TexSize = 0;
78 IM->MaterialOrMask = 0;
79
80 if (IM->MaterialMask)
81 IM->MaterialMask[IM->Start] = 0;
82
83 IM->ArrayEltFlags = ~ctx->Array._Enabled;
84 IM->ArrayEltIncr = ctx->Array.Vertex.Enabled ? 1 : 0;
85 IM->ArrayEltFlush = ctx->Array.LockCount ? FLUSH_ELT_LAZY : FLUSH_ELT_EAGER;
86 }
87
88 void _tnl_reset_exec_input( GLcontext *ctx,
89 GLuint start,
90 GLuint beginstate,
91 GLuint savedbeginstate )
92 {
93 TNLcontext *tnl = TNL_CONTEXT(ctx);
94 struct immediate *IM = TNL_CURRENT_IM(ctx);
95
96 reset_input( ctx, start, beginstate, savedbeginstate );
97
98 IM->CopyStart = start - tnl->ExecCopyCount;
99
100 IM->Primitive[IM->CopyStart] = ctx->Driver.CurrentExecPrimitive;
101 if (tnl->ExecParity)
102 IM->Primitive[IM->CopyStart] |= PRIM_PARITY;
103
104 IM->LastPrimitive = IM->CopyStart;
105 }
106
107
108 void _tnl_reset_compile_input( GLcontext *ctx,
109 GLuint start,
110 GLuint beginstate,
111 GLuint savedbeginstate )
112 {
113 struct immediate *IM = TNL_CURRENT_IM(ctx);
114
115 reset_input( ctx, start, beginstate, savedbeginstate );
116 IM->CopyStart = start;
117 IM->LastPrimitive = IM->Start;
118 }
119
120
121 /*
122 * Copy the last specified normal, color, texcoord, edge flag, etc
123 * from the immediate struct into the ctx->Current attribute group.
124 */
125 void _tnl_copy_to_current( GLcontext *ctx, struct immediate *IM,
126 GLuint flag, GLuint count )
127 {
128 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
129 _tnl_print_vert_flags("copy to current", flag);
130
131 if (flag & VERT_NORMAL_BIT)
132 COPY_3FV( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], IM->Normal[count]);
133
134 if (flag & VERT_INDEX_BIT)
135 ctx->Current.Index = IM->Index[count];
136
137 if (flag & VERT_EDGEFLAG_BIT)
138 ctx->Current.EdgeFlag = IM->EdgeFlag[count];
139
140 if (flag & VERT_COLOR0_BIT) {
141 COPY_4FV(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], IM->Color[count]);
142 if (ctx->Light.ColorMaterialEnabled) {
143 _mesa_update_color_material( ctx, ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
144 _mesa_validate_all_lighting_tables( ctx );
145 }
146 }
147
148 if (flag & VERT_COLOR1_BIT)
149 COPY_4FV(ctx->Current.Attrib[VERT_ATTRIB_COLOR1], IM->SecondaryColor[count]);
150
151 if (flag & VERT_FOG_BIT)
152 ctx->Current.Attrib[VERT_ATTRIB_FOG][0] = IM->FogCoord[count];
153
154 if (flag & VERT_TEX_ANY) {
155 GLuint i;
156 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
157 if (flag & VERT_TEX(i)) {
158 COPY_4FV( ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i], IM->TexCoord[i][count]);
159 }
160 }
161 }
162
163 if (flag & VERT_MATERIAL) {
164 _mesa_update_material( ctx,
165 IM->Material[IM->LastMaterial],
166 IM->MaterialOrMask );
167
168 _mesa_validate_all_lighting_tables( ctx );
169 }
170 }
171
172
173
174 void _tnl_compute_orflag( struct immediate *IM, GLuint start )
175 {
176 GLuint count = IM->Count;
177 GLuint orflag = 0;
178 GLuint andflag = ~0U;
179 GLuint i;
180
181 IM->LastData = count-1;
182
183
184 /* Compute the flags for the whole buffer.
185 */
186 for (i = start ; i < count ; i++) {
187 andflag &= IM->Flag[i];
188 orflag |= IM->Flag[i];
189 }
190
191 /* It is possible there will be data in the buffer arising from
192 * calls like 'glNormal', 'glMaterial' that occur after the final
193 * glVertex, glEval, etc. Additionally, a buffer can consist of
194 * eg. a single glMaterial call, in which case IM->Start ==
195 * IM->Count, but the buffer is definitely not empty.
196 */
197 if (IM->Flag[i] & VERT_DATA) {
198 IM->LastData++;
199 orflag |= IM->Flag[i];
200 }
201
202 IM->Flag[IM->LastData+1] |= VERT_END_VB;
203 IM->CopyAndFlag = IM->AndFlag = andflag;
204 IM->OrFlag = orflag;
205 IM->CopyOrFlag = orflag;
206 IM->Evaluated = 0;
207 }
208
209
210 /*
211 * This is where the vertex data is transfered from the 'struct immediate
212 * into the 'struct vertex_buffer'.
213 *
214 * Note: The 'start' member of the GLvector structs is now redundant
215 * because we always re-transform copied vertices, and the vectors
216 * below are set up so that the first copied vertex (if any) appears
217 * at position zero.
218 */
219 static void _tnl_vb_bind_immediate( GLcontext *ctx, struct immediate *IM )
220 {
221 TNLcontext *tnl = TNL_CONTEXT(ctx);
222 struct vertex_buffer *VB = &tnl->vb;
223 struct vertex_arrays *tmp = &tnl->imm_inputs;
224 GLuint inputs = tnl->pipeline.inputs; /* for copy-to-current */
225 GLuint start = IM->CopyStart;
226 GLuint count = IM->Count - start;
227
228 /* TODO: optimize the case where nothing has changed. (Just bind
229 * tmp to vb).
230 */
231
232 /* Setup constant data in the VB.
233 */
234 VB->Count = count;
235 VB->FirstClipped = IMM_MAXDATA - IM->CopyStart;
236 VB->import_data = 0;
237 VB->importable_data = 0;
238
239 /* Need an IM->FirstPrimitive?
240 */
241 VB->Primitive = IM->Primitive + IM->CopyStart;
242 VB->PrimitiveLength = IM->PrimitiveLength + IM->CopyStart;
243 VB->FirstPrimitive = 0;
244
245 VB->Flag = IM->Flag + start;
246
247 /* TexCoordPtr's are zeroed in loop below.
248 */
249 VB->NormalPtr = 0;
250 VB->NormalLengthPtr = 0;
251 VB->FogCoordPtr = 0;
252 VB->EdgeFlag = 0;
253 VB->IndexPtr[0] = 0;
254 VB->IndexPtr[1] = 0;
255 VB->ColorPtr[0] = 0;
256 VB->ColorPtr[1] = 0;
257 VB->SecondaryColorPtr[0] = 0;
258 VB->SecondaryColorPtr[1] = 0;
259 VB->Elts = 0;
260 VB->MaterialMask = 0;
261 VB->Material = 0;
262
263 /* _tnl_print_vert_flags("copy-orflag", IM->CopyOrFlag); */
264 /* _tnl_print_vert_flags("orflag", IM->OrFlag); */
265 /* _tnl_print_vert_flags("inputs", inputs); */
266
267 /* Setup the initial values of array pointers in the vb.
268 */
269 if (inputs & VERT_OBJ_BIT) {
270 tmp->Obj.data = IM->Obj + start;
271 tmp->Obj.start = (GLfloat *)(IM->Obj + start);
272 tmp->Obj.count = count;
273 VB->ObjPtr = &tmp->Obj;
274 if ((IM->CopyOrFlag & VERT_OBJ_234) == VERT_OBJ_234)
275 tmp->Obj.size = 4;
276 else if ((IM->CopyOrFlag & VERT_OBJ_234) == VERT_OBJ_23)
277 tmp->Obj.size = 3;
278 else
279 tmp->Obj.size = 2;
280 }
281
282 if (inputs & VERT_NORMAL_BIT) {
283 tmp->Normal.data = IM->Normal + start;
284 tmp->Normal.start = (GLfloat *)(IM->Normal + start);
285 tmp->Normal.count = count;
286 VB->NormalPtr = &tmp->Normal;
287 if (IM->NormalLengthPtr)
288 VB->NormalLengthPtr = IM->NormalLengthPtr + start;
289 }
290
291 if (inputs & VERT_INDEX_BIT) {
292 tmp->Index.count = count;
293 tmp->Index.data = IM->Index + start;
294 tmp->Index.start = IM->Index + start;
295 VB->IndexPtr[0] = &tmp->Index;
296 }
297
298 if (inputs & VERT_FOG_BIT) {
299 tmp->FogCoord.data = IM->FogCoord + start;
300 tmp->FogCoord.start = IM->FogCoord + start;
301 tmp->FogCoord.count = count;
302 VB->FogCoordPtr = &tmp->FogCoord;
303 }
304
305 if (inputs & VERT_COLOR1_BIT) {
306 tmp->SecondaryColor.Ptr = IM->SecondaryColor + start;
307 VB->SecondaryColorPtr[0] = &tmp->SecondaryColor;
308 }
309
310 if (inputs & VERT_EDGEFLAG_BIT) {
311 VB->EdgeFlag = IM->EdgeFlag + start;
312 }
313
314 if (inputs & VERT_COLOR0_BIT) {
315 if (IM->CopyOrFlag & VERT_COLOR0_BIT) {
316 tmp->Color.Ptr = IM->Color + start;
317 tmp->Color.StrideB = 4 * sizeof(GLfloat);
318 tmp->Color.Flags = 0;
319 }
320 else {
321 tmp->Color.Ptr = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
322 tmp->Color.StrideB = 0;
323 tmp->Color.Flags = CA_CLIENT_DATA; /* hack */
324 VB->import_source = IM;
325 VB->importable_data |= VERT_COLOR0_BIT;
326 VB->import_data = _tnl_upgrade_current_data;
327 }
328 VB->ColorPtr[0] = &tmp->Color;
329 }
330
331 if (inputs & VERT_TEX_ANY) {
332 GLuint i;
333 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
334 VB->TexCoordPtr[i] = 0;
335 if (inputs & VERT_TEX(i)) {
336 tmp->TexCoord[i].count = count;
337 tmp->TexCoord[i].data = IM->TexCoord[i] + start;
338 tmp->TexCoord[i].start = (GLfloat *)(IM->TexCoord[i] + start);
339 tmp->TexCoord[i].size = 2;
340 if (IM->TexSize & TEX_SIZE_3(i)) {
341 tmp->TexCoord[i].size = 3;
342 if (IM->TexSize & TEX_SIZE_4(i))
343 tmp->TexCoord[i].size = 4;
344 }
345 VB->TexCoordPtr[i] = &tmp->TexCoord[i];
346 }
347 }
348 }
349
350 if ((inputs & VERT_MATERIAL) && IM->Material) {
351 VB->MaterialMask = IM->MaterialMask + start;
352 VB->Material = IM->Material + start;
353 }
354
355 /* GL_NV_vertex_program */
356 if (ctx->VertexProgram.Enabled) {
357 GLuint attr;
358 for (attr = 0; attr < 16; attr++) {
359 tmp->Attribs[attr].count = count;
360 tmp->Attribs[attr].data = IM->Attrib[attr] + start;
361 tmp->Attribs[attr].start = (GLfloat *) (IM->Attrib[attr] + start);
362 tmp->Attribs[attr].size = 4;
363 VB->AttribPtr[attr] = &(tmp->Attribs[attr]);
364 }
365 }
366 }
367
368
369
370
371 /* Called by exec_vert_cassette, execute_compiled_cassette, but not
372 * exec_elt_cassette.
373 */
374 void _tnl_run_cassette( GLcontext *ctx, struct immediate *IM )
375 {
376 TNLcontext *tnl = TNL_CONTEXT(ctx);
377
378 _tnl_vb_bind_immediate( ctx, IM );
379
380 if (IM->OrFlag & VERT_EVAL_ANY)
381 _tnl_eval_immediate( ctx, IM );
382
383 /* Invalidate all stored data before and after run:
384 */
385 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
386 tnl->Driver.RunPipeline( ctx );
387 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
388
389 _tnl_copy_to_current( ctx, IM, IM->OrFlag, IM->LastData );
390 }
391
392
393 /* Called for regular vertex cassettes.
394 */
395 static void exec_vert_cassette( GLcontext *ctx, struct immediate *IM )
396 {
397 /* fprintf(stderr, "%s\n", __FUNCTION__); */
398
399 if (IM->FlushElt) {
400 /* Orflag is computed twice, but only reach this code if app is
401 * using a mixture of glArrayElement() and glVertex() while
402 * arrays are locked (else would be in exec_elt_cassette now).
403 */
404 ASSERT(ctx->Array.LockCount);
405 ASSERT(IM->FlushElt == FLUSH_ELT_LAZY);
406 _tnl_translate_array_elts( ctx, IM, IM->CopyStart, IM->Count );
407 _tnl_compute_orflag( IM, IM->CopyStart );
408 }
409
410 _tnl_fixup_input( ctx, IM );
411 /* _tnl_print_cassette( IM ); */
412 _tnl_run_cassette( ctx, IM );
413 }
414
415
416 /* Called for pure, locked VERT_ELT cassettes instead of
417 * _tnl_run_cassette.
418 */
419 static void exec_elt_cassette( GLcontext *ctx, struct immediate *IM )
420 {
421 TNLcontext *tnl = TNL_CONTEXT(ctx);
422 struct vertex_buffer *VB = &tnl->vb;
423
424 /* fprintf(stderr, "%s\n", __FUNCTION__); */
425
426 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
427
428 /* Take only elements and primitive information from the immediate:
429 */
430 VB->Elts = IM->Elt + IM->CopyStart;
431 VB->Primitive = IM->Primitive + IM->CopyStart;
432 VB->PrimitiveLength = IM->PrimitiveLength + IM->CopyStart;
433 VB->FirstPrimitive = 0;
434
435 /* Run the pipeline. No input changes as a result of this action.
436 */
437 tnl->Driver.RunPipeline( ctx );
438
439 /* Still need to update current values:
440 */
441 if (ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1) {
442 _tnl_translate_array_elts( ctx, IM, IM->LastData, IM->LastData );
443 _tnl_copy_to_current( ctx, IM, ctx->Array._Enabled, IM->LastData );
444 }
445 }
446
447
448 static void
449 exec_empty_cassette( GLcontext *ctx, struct immediate *IM )
450 {
451 if (IM->FlushElt)
452 _tnl_translate_array_elts( ctx, IM, IM->CopyStart, IM->CopyStart );
453
454 _tnl_copy_to_current( ctx, IM, IM->OrFlag, IM->LastData );
455 }
456
457
458
459 /* Called for all cassettes when not compiling or playing a display
460 * list.
461 */
462 void _tnl_execute_cassette( GLcontext *ctx, struct immediate *IM )
463 {
464 TNLcontext *tnl = TNL_CONTEXT(ctx);
465
466 _tnl_compute_orflag( IM, IM->Start );
467 _tnl_copy_immediate_vertices( ctx, IM );
468 _tnl_get_exec_copy_verts( ctx, IM );
469
470 if (tnl->pipeline.build_state_changes)
471 _tnl_validate_pipeline( ctx );
472
473 if (IM->CopyStart == IM->Count) {
474 exec_empty_cassette( ctx, IM );
475 }
476 else if ((IM->CopyOrFlag & VERT_DATA) == VERT_ELT &&
477 ctx->Array.LockCount &&
478 ctx->Array.Vertex.Enabled) {
479 exec_elt_cassette( ctx, IM );
480 }
481 else {
482 exec_vert_cassette( ctx, IM );
483 }
484
485 /* Only reuse the immediate if there are no copied vertices living
486 * inside it:
487 */
488 {
489 GLuint begin_state = IM->BeginState & (VERT_BEGIN_0|VERT_BEGIN_1);
490 GLuint saved_begin_state = IM->SavedBeginState;
491
492 if (--IM->ref_count != 0) {
493 IM = _tnl_alloc_immediate( ctx );
494 SET_IMMEDIATE( ctx, IM );
495 }
496
497 IM->ref_count++;
498
499 _tnl_reset_exec_input( ctx, IMM_MAX_COPIED_VERTS,
500 begin_state, saved_begin_state );
501 }
502
503 if (ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1)
504 ctx->Driver.NeedFlush &= ~FLUSH_STORED_VERTICES;
505 }
506
507
508
509
510 /* Setup vector pointers that will be used to bind immediates to VB's.
511 */
512 void _tnl_imm_init( GLcontext *ctx )
513 {
514 TNLcontext *tnl = TNL_CONTEXT(ctx);
515 struct vertex_arrays *tmp = &tnl->imm_inputs;
516 GLuint i;
517 static int firsttime = 1;
518
519 if (firsttime) {
520 firsttime = 0;
521 _tnl_imm_elt_init();
522 }
523
524 ctx->swtnl_im = _tnl_alloc_immediate( ctx );
525 TNL_CURRENT_IM(ctx)->ref_count++;
526
527 tnl->ExecCopyTexSize = 0;
528 tnl->ExecCopyCount = 0;
529 tnl->ExecCopySource = 0;
530
531 TNL_CURRENT_IM(ctx)->CopyStart = IMM_MAX_COPIED_VERTS;
532
533 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
534 _mesa_vector3f_init( &tmp->Normal, 0, 0 );
535
536 tmp->Color.Ptr = 0;
537 tmp->Color.Type = GL_FLOAT;
538 tmp->Color.Size = 4;
539 tmp->Color.Stride = 0;
540 tmp->Color.StrideB = 4 * sizeof(GLfloat);
541 tmp->Color.Flags = 0;
542
543 tmp->SecondaryColor.Ptr = 0;
544 tmp->SecondaryColor.Type = GL_FLOAT;
545 tmp->SecondaryColor.Size = 4;
546 tmp->SecondaryColor.Stride = 0;
547 tmp->SecondaryColor.StrideB = 4 * sizeof(GLfloat);
548 tmp->SecondaryColor.Flags = 0;
549
550 _mesa_vector1f_init( &tmp->FogCoord, 0, 0 );
551 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
552 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
553
554 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
555 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
556
557 /* Install the first immediate. Intially outside begin/end.
558 */
559 _tnl_reset_exec_input( ctx, IMM_MAX_COPIED_VERTS, 0, 0 );
560 tnl->ReplayHardBeginEnd = 0;
561
562 _tnl_imm_vtxfmt_init( ctx );
563 }
564
565
566 void _tnl_imm_destroy( GLcontext *ctx )
567 {
568 if (TNL_CURRENT_IM(ctx)) {
569 TNL_CURRENT_IM(ctx)->ref_count--;
570 if (TNL_CURRENT_IM(ctx)->ref_count == 0)
571 _tnl_free_immediate( TNL_CURRENT_IM(ctx) );
572 /*
573 * Don't use SET_IMMEDIATE here, or else we'll whack the
574 * _tnl_CurrentInput pointer - not good when another
575 * context has already been made current.
576 * So we just set the context's own tnl immediate pointer
577 * to 0.
578 */
579 ctx->swtnl_im = 0;
580 }
581 }