1 /* $Id: t_imm_exec.c,v 1.32 2001/12/15 02:13:32 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keithw@valinux.com>
43 #include "math/m_matrix.h"
44 #include "math/m_xform.h"
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"
60 static void reset_input( GLcontext
*ctx
,
63 GLuint savedbeginstate
)
65 struct immediate
*IM
= TNL_CURRENT_IM(ctx
);
67 /* Clear the dirty part of the flag array.
69 if (start
< IM
->Count
+2)
70 MEMSET(IM
->Flag
+ start
, 0, sizeof(GLuint
) * (IM
->Count
+2-start
));
74 IM
->LastMaterial
= start
;
75 IM
->BeginState
= beginstate
;
76 IM
->SavedBeginState
= savedbeginstate
;
78 IM
->MaterialOrMask
= 0;
81 IM
->MaterialMask
[IM
->Start
] = 0;
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
;
88 void _tnl_reset_exec_input( GLcontext
*ctx
,
91 GLuint savedbeginstate
)
93 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
94 struct immediate
*IM
= TNL_CURRENT_IM(ctx
);
96 reset_input( ctx
, start
, beginstate
, savedbeginstate
);
98 IM
->CopyStart
= start
- tnl
->ExecCopyCount
;
100 IM
->Primitive
[IM
->CopyStart
] = ctx
->Driver
.CurrentExecPrimitive
;
102 IM
->Primitive
[IM
->CopyStart
] |= PRIM_PARITY
;
104 IM
->LastPrimitive
= IM
->CopyStart
;
108 void _tnl_reset_compile_input( GLcontext
*ctx
,
111 GLuint savedbeginstate
)
113 struct immediate
*IM
= TNL_CURRENT_IM(ctx
);
115 reset_input( ctx
, start
, beginstate
, savedbeginstate
);
116 IM
->CopyStart
= start
;
117 IM
->LastPrimitive
= IM
->Start
;
122 * Copy the last specified normal, color, texcoord, edge flag, etc
123 * from the immediate struct into the ctx->Current attribute group.
125 void _tnl_copy_to_current( GLcontext
*ctx
, struct immediate
*IM
,
126 GLuint flag
, GLuint count
)
128 if (MESA_VERBOSE
&VERBOSE_IMMEDIATE
)
129 _tnl_print_vert_flags("copy to current", flag
);
131 if (flag
& VERT_NORMAL_BIT
)
132 COPY_3FV( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
], IM
->Normal
[count
]);
134 if (flag
& VERT_INDEX_BIT
)
135 ctx
->Current
.Index
= IM
->Index
[count
];
137 if (flag
& VERT_EDGEFLAG_BIT
)
138 ctx
->Current
.EdgeFlag
= IM
->EdgeFlag
[count
];
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
);
148 if (flag
& VERT_COLOR1_BIT
)
149 COPY_4FV(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
], IM
->SecondaryColor
[count
]);
151 if (flag
& VERT_FOG_BIT
)
152 ctx
->Current
.Attrib
[VERT_ATTRIB_FOG
][0] = IM
->FogCoord
[count
];
154 if (flag
& VERT_TEX_ANY
) {
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
]);
163 if (flag
& VERT_MATERIAL
) {
164 _mesa_update_material( ctx
,
165 IM
->Material
[IM
->LastMaterial
],
166 IM
->MaterialOrMask
);
168 _mesa_validate_all_lighting_tables( ctx
);
174 void _tnl_compute_orflag( struct immediate
*IM
, GLuint start
)
176 GLuint count
= IM
->Count
;
178 GLuint andflag
= ~0U;
181 IM
->LastData
= count
-1;
184 /* Compute the flags for the whole buffer.
186 for (i
= start
; i
< count
; i
++) {
187 andflag
&= IM
->Flag
[i
];
188 orflag
|= IM
->Flag
[i
];
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.
197 if (IM
->Flag
[i
] & VERT_DATA
) {
199 orflag
|= IM
->Flag
[i
];
202 IM
->Flag
[IM
->LastData
+1] |= VERT_END_VB
;
203 IM
->CopyAndFlag
= IM
->AndFlag
= andflag
;
205 IM
->CopyOrFlag
= orflag
;
211 * This is where the vertex data is transfered from the 'struct immediate
212 * into the 'struct vertex_buffer'.
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
219 static void _tnl_vb_bind_immediate( GLcontext
*ctx
, struct immediate
*IM
)
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
;
228 /* TODO: optimize the case where nothing has changed. (Just bind
232 /* Setup constant data in the VB.
235 VB
->FirstClipped
= IMM_MAXDATA
- IM
->CopyStart
;
237 VB
->importable_data
= 0;
239 /* Need an IM->FirstPrimitive?
241 VB
->Primitive
= IM
->Primitive
+ IM
->CopyStart
;
242 VB
->PrimitiveLength
= IM
->PrimitiveLength
+ IM
->CopyStart
;
243 VB
->FirstPrimitive
= 0;
245 VB
->Flag
= IM
->Flag
+ start
;
247 /* TexCoordPtr's are zeroed in loop below.
250 VB
->NormalLengthPtr
= 0;
257 VB
->SecondaryColorPtr
[0] = 0;
258 VB
->SecondaryColorPtr
[1] = 0;
260 VB
->MaterialMask
= 0;
263 /* _tnl_print_vert_flags("copy-orflag", IM->CopyOrFlag); */
264 /* _tnl_print_vert_flags("orflag", IM->OrFlag); */
265 /* _tnl_print_vert_flags("inputs", inputs); */
267 /* Setup the initial values of array pointers in the vb.
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
)
276 else if ((IM
->CopyOrFlag
& VERT_OBJ_234
) == VERT_OBJ_23
)
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
;
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
;
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
;
305 if (inputs
& VERT_COLOR1_BIT
) {
306 tmp
->SecondaryColor
.Ptr
= IM
->SecondaryColor
+ start
;
307 VB
->SecondaryColorPtr
[0] = &tmp
->SecondaryColor
;
310 if (inputs
& VERT_EDGEFLAG_BIT
) {
311 VB
->EdgeFlag
= IM
->EdgeFlag
+ start
;
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;
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
;
328 VB
->ColorPtr
[0] = &tmp
->Color
;
331 if (inputs
& VERT_TEX_ANY
) {
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;
345 VB
->TexCoordPtr
[i
] = &tmp
->TexCoord
[i
];
350 if ((inputs
& VERT_MATERIAL
) && IM
->Material
) {
351 VB
->MaterialMask
= IM
->MaterialMask
+ start
;
352 VB
->Material
= IM
->Material
+ start
;
355 /* GL_NV_vertex_program */
356 if (ctx
->VertexProgram
.Enabled
) {
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
]);
371 /* Called by exec_vert_cassette, execute_compiled_cassette, but not
374 void _tnl_run_cassette( GLcontext
*ctx
, struct immediate
*IM
)
376 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
378 _tnl_vb_bind_immediate( ctx
, IM
);
380 if (IM
->OrFlag
& VERT_EVAL_ANY
)
381 _tnl_eval_immediate( ctx
, IM
);
383 /* Invalidate all stored data before and after run:
385 tnl
->pipeline
.run_input_changes
|= tnl
->pipeline
.inputs
;
386 tnl
->Driver
.RunPipeline( ctx
);
387 tnl
->pipeline
.run_input_changes
|= tnl
->pipeline
.inputs
;
389 _tnl_copy_to_current( ctx
, IM
, IM
->OrFlag
, IM
->LastData
);
393 /* Called for regular vertex cassettes.
395 static void exec_vert_cassette( GLcontext
*ctx
, struct immediate
*IM
)
397 /* fprintf(stderr, "%s\n", __FUNCTION__); */
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).
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
);
410 _tnl_fixup_input( ctx
, IM
);
411 /* _tnl_print_cassette( IM ); */
412 _tnl_run_cassette( ctx
, IM
);
416 /* Called for pure, locked VERT_ELT cassettes instead of
419 static void exec_elt_cassette( GLcontext
*ctx
, struct immediate
*IM
)
421 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
422 struct vertex_buffer
*VB
= &tnl
->vb
;
424 /* fprintf(stderr, "%s\n", __FUNCTION__); */
426 _tnl_vb_bind_arrays( ctx
, ctx
->Array
.LockFirst
, ctx
->Array
.LockCount
);
428 /* Take only elements and primitive information from the immediate:
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;
435 /* Run the pipeline. No input changes as a result of this action.
437 tnl
->Driver
.RunPipeline( ctx
);
439 /* Still need to update current values:
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
);
449 exec_empty_cassette( GLcontext
*ctx
, struct immediate
*IM
)
452 _tnl_translate_array_elts( ctx
, IM
, IM
->CopyStart
, IM
->CopyStart
);
454 _tnl_copy_to_current( ctx
, IM
, IM
->OrFlag
, IM
->LastData
);
459 /* Called for all cassettes when not compiling or playing a display
462 void _tnl_execute_cassette( GLcontext
*ctx
, struct immediate
*IM
)
464 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
466 _tnl_compute_orflag( IM
, IM
->Start
);
467 _tnl_copy_immediate_vertices( ctx
, IM
);
468 _tnl_get_exec_copy_verts( ctx
, IM
);
470 if (tnl
->pipeline
.build_state_changes
)
471 _tnl_validate_pipeline( ctx
);
473 if (IM
->CopyStart
== IM
->Count
) {
474 exec_empty_cassette( ctx
, IM
);
476 else if ((IM
->CopyOrFlag
& VERT_DATA
) == VERT_ELT
&&
477 ctx
->Array
.LockCount
&&
478 ctx
->Array
.Vertex
.Enabled
) {
479 exec_elt_cassette( ctx
, IM
);
482 exec_vert_cassette( ctx
, IM
);
485 /* Only reuse the immediate if there are no copied vertices living
489 GLuint begin_state
= IM
->BeginState
& (VERT_BEGIN_0
|VERT_BEGIN_1
);
490 GLuint saved_begin_state
= IM
->SavedBeginState
;
492 if (--IM
->ref_count
!= 0) {
493 IM
= _tnl_alloc_immediate( ctx
);
494 SET_IMMEDIATE( ctx
, IM
);
499 _tnl_reset_exec_input( ctx
, IMM_MAX_COPIED_VERTS
,
500 begin_state
, saved_begin_state
);
503 if (ctx
->Driver
.CurrentExecPrimitive
== GL_POLYGON
+1)
504 ctx
->Driver
.NeedFlush
&= ~FLUSH_STORED_VERTICES
;
510 /* Setup vector pointers that will be used to bind immediates to VB's.
512 void _tnl_imm_init( GLcontext
*ctx
)
514 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
515 struct vertex_arrays
*tmp
= &tnl
->imm_inputs
;
517 static int firsttime
= 1;
524 ctx
->swtnl_im
= _tnl_alloc_immediate( ctx
);
525 TNL_CURRENT_IM(ctx
)->ref_count
++;
527 tnl
->ExecCopyTexSize
= 0;
528 tnl
->ExecCopyCount
= 0;
529 tnl
->ExecCopySource
= 0;
531 TNL_CURRENT_IM(ctx
)->CopyStart
= IMM_MAX_COPIED_VERTS
;
533 _mesa_vector4f_init( &tmp
->Obj
, 0, 0 );
534 _mesa_vector3f_init( &tmp
->Normal
, 0, 0 );
537 tmp
->Color
.Type
= GL_FLOAT
;
539 tmp
->Color
.Stride
= 0;
540 tmp
->Color
.StrideB
= 4 * sizeof(GLfloat
);
541 tmp
->Color
.Flags
= 0;
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;
550 _mesa_vector1f_init( &tmp
->FogCoord
, 0, 0 );
551 _mesa_vector1ui_init( &tmp
->Index
, 0, 0 );
552 _mesa_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
554 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
555 _mesa_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
557 /* Install the first immediate. Intially outside begin/end.
559 _tnl_reset_exec_input( ctx
, IMM_MAX_COPIED_VERTS
, 0, 0 );
560 tnl
->ReplayHardBeginEnd
= 0;
562 _tnl_imm_vtxfmt_init( ctx
);
566 void _tnl_imm_destroy( GLcontext
*ctx
)
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
) );
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