1 /* $Id: t_imm_exec.c,v 1.34 2002/01/06 03:54:12 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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 /* XXX should be able t replace these conditions with a loop over
132 * the 16 vertex attributes.
134 if (flag
& VERT_NORMAL_BIT
)
135 COPY_4FV( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
],
136 IM
->Attrib
[VERT_ATTRIB_NORMAL
][count
]);
138 if (flag
& VERT_INDEX_BIT
)
139 ctx
->Current
.Index
= IM
->Index
[count
];
141 if (flag
& VERT_EDGEFLAG_BIT
)
142 ctx
->Current
.EdgeFlag
= IM
->EdgeFlag
[count
];
144 if (flag
& VERT_COLOR0_BIT
) {
145 COPY_4FV(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
],
146 IM
->Attrib
[VERT_ATTRIB_COLOR0
][count
]);
147 if (ctx
->Light
.ColorMaterialEnabled
) {
148 _mesa_update_color_material( ctx
,
149 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
] );
150 _mesa_validate_all_lighting_tables( ctx
);
154 if (flag
& VERT_COLOR1_BIT
)
155 COPY_4FV(ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
],
156 IM
->Attrib
[VERT_ATTRIB_COLOR1
][count
]);
158 if (flag
& VERT_FOG_BIT
)
159 ctx
->Current
.Attrib
[VERT_ATTRIB_FOG
][0] = IM
->Attrib
[VERT_ATTRIB_FOG
][count
][0];
161 if (flag
& VERT_TEX_ANY
) {
163 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
164 if (flag
& VERT_TEX(i
)) {
165 COPY_4FV( ctx
->Current
.Attrib
[VERT_ATTRIB_TEX0
+ i
],
166 IM
->Attrib
[VERT_ATTRIB_TEX0
+ i
][count
]);
171 if (flag
& VERT_MATERIAL
) {
172 _mesa_update_material( ctx
,
173 IM
->Material
[IM
->LastMaterial
],
174 IM
->MaterialOrMask
);
176 _mesa_validate_all_lighting_tables( ctx
);
182 void _tnl_compute_orflag( struct immediate
*IM
, GLuint start
)
184 GLuint count
= IM
->Count
;
186 GLuint andflag
= ~0U;
189 IM
->LastData
= count
-1;
192 /* Compute the flags for the whole buffer.
194 for (i
= start
; i
< count
; i
++) {
195 andflag
&= IM
->Flag
[i
];
196 orflag
|= IM
->Flag
[i
];
199 /* It is possible there will be data in the buffer arising from
200 * calls like 'glNormal', 'glMaterial' that occur after the final
201 * glVertex, glEval, etc. Additionally, a buffer can consist of
202 * eg. a single glMaterial call, in which case IM->Start ==
203 * IM->Count, but the buffer is definitely not empty.
205 if (IM
->Flag
[i
] & VERT_DATA
) {
207 orflag
|= IM
->Flag
[i
];
210 IM
->Flag
[IM
->LastData
+1] |= VERT_END_VB
;
211 IM
->CopyAndFlag
= IM
->AndFlag
= andflag
;
213 IM
->CopyOrFlag
= orflag
;
219 * This is where the vertex data is transfered from the 'struct immediate
220 * into the 'struct vertex_buffer'.
222 * Note: The 'start' member of the GLvector structs is now redundant
223 * because we always re-transform copied vertices, and the vectors
224 * below are set up so that the first copied vertex (if any) appears
227 static void _tnl_vb_bind_immediate( GLcontext
*ctx
, struct immediate
*IM
)
229 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
230 struct vertex_buffer
*VB
= &tnl
->vb
;
231 struct vertex_arrays
*tmp
= &tnl
->imm_inputs
;
232 GLuint inputs
= tnl
->pipeline
.inputs
; /* for copy-to-current */
233 const GLuint start
= IM
->CopyStart
;
234 const GLuint count
= IM
->Count
- start
;
236 /* TODO: optimize the case where nothing has changed. (Just bind
240 /* Setup constant data in the VB.
243 VB
->FirstClipped
= IMM_MAXDATA
- IM
->CopyStart
;
245 VB
->importable_data
= 0;
247 /* Need an IM->FirstPrimitive?
249 VB
->Primitive
= IM
->Primitive
+ IM
->CopyStart
;
250 VB
->PrimitiveLength
= IM
->PrimitiveLength
+ IM
->CopyStart
;
251 VB
->FirstPrimitive
= 0;
253 VB
->Flag
= IM
->Flag
+ start
;
255 /* TexCoordPtr's are zeroed in loop below.
258 VB
->NormalLengthPtr
= 0;
264 VB
->SecondaryColorPtr
[0] = 0;
265 VB
->SecondaryColorPtr
[1] = 0;
267 VB
->MaterialMask
= 0;
270 /* _tnl_print_vert_flags("copy-orflag", IM->CopyOrFlag); */
271 /* _tnl_print_vert_flags("orflag", IM->OrFlag); */
272 /* _tnl_print_vert_flags("inputs", inputs); */
274 /* Setup the initial values of array pointers in the vb.
276 if (inputs
& VERT_OBJ_BIT
) {
277 tmp
->Obj
.data
= IM
->Attrib
[VERT_ATTRIB_POS
] + start
;
278 tmp
->Obj
.start
= (GLfloat
*)(IM
->Attrib
[VERT_ATTRIB_POS
] + start
);
279 tmp
->Obj
.count
= count
;
280 VB
->ObjPtr
= &tmp
->Obj
;
281 if ((IM
->CopyOrFlag
& VERT_OBJ_234
) == VERT_OBJ_234
)
283 else if ((IM
->CopyOrFlag
& VERT_OBJ_234
) == VERT_OBJ_23
)
289 if (inputs
& VERT_NORMAL_BIT
) {
290 tmp
->Normal
.data
= IM
->Attrib
[VERT_ATTRIB_NORMAL
] + start
;
291 tmp
->Normal
.start
= (GLfloat
*) (IM
->Attrib
[VERT_ATTRIB_NORMAL
] + start
);
292 tmp
->Normal
.count
= count
;
293 VB
->NormalPtr
= &tmp
->Normal
;
294 if (IM
->NormalLengthPtr
)
295 VB
->NormalLengthPtr
= IM
->NormalLengthPtr
+ start
;
298 if (inputs
& VERT_INDEX_BIT
) {
299 tmp
->Index
.count
= count
;
300 tmp
->Index
.data
= IM
->Index
+ start
;
301 tmp
->Index
.start
= IM
->Index
+ start
;
302 VB
->IndexPtr
[0] = &tmp
->Index
;
305 if (inputs
& VERT_FOG_BIT
) {
306 tmp
->FogCoord
.data
= IM
->Attrib
[VERT_ATTRIB_FOG
] + start
;
307 tmp
->FogCoord
.start
= (GLfloat
*) (IM
->Attrib
[VERT_ATTRIB_FOG
] + start
);
308 tmp
->FogCoord
.count
= count
;
309 VB
->FogCoordPtr
= &tmp
->FogCoord
;
312 if (inputs
& VERT_COLOR1_BIT
) {
313 tmp
->SecondaryColor
.Ptr
= IM
->Attrib
[VERT_ATTRIB_COLOR1
] + start
;
314 VB
->SecondaryColorPtr
[0] = &tmp
->SecondaryColor
;
317 if (inputs
& VERT_EDGEFLAG_BIT
) {
318 VB
->EdgeFlag
= IM
->EdgeFlag
+ start
;
321 if (inputs
& VERT_COLOR0_BIT
) {
322 if (IM
->CopyOrFlag
& VERT_COLOR0_BIT
) {
323 tmp
->Color
.Ptr
= IM
->Attrib
[VERT_ATTRIB_COLOR0
] + start
;
324 tmp
->Color
.StrideB
= 4 * sizeof(GLfloat
);
325 tmp
->Color
.Flags
= 0;
328 tmp
->Color
.Ptr
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
329 tmp
->Color
.StrideB
= 0;
330 tmp
->Color
.Flags
= CA_CLIENT_DATA
; /* hack */
331 VB
->import_source
= IM
;
332 VB
->importable_data
|= VERT_COLOR0_BIT
;
333 VB
->import_data
= _tnl_upgrade_current_data
;
335 VB
->ColorPtr
[0] = &tmp
->Color
;
338 if (inputs
& VERT_TEX_ANY
) {
340 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
341 VB
->TexCoordPtr
[i
] = 0;
342 if (inputs
& VERT_TEX(i
)) {
343 tmp
->TexCoord
[i
].count
= count
;
344 tmp
->TexCoord
[i
].data
= IM
->Attrib
[VERT_ATTRIB_TEX0
+ i
] + start
;
345 tmp
->TexCoord
[i
].start
= (GLfloat
*)(IM
->Attrib
[VERT_ATTRIB_TEX0
+ i
] + start
);
346 tmp
->TexCoord
[i
].size
= 2;
347 if (IM
->TexSize
& TEX_SIZE_3(i
)) {
348 tmp
->TexCoord
[i
].size
= 3;
349 if (IM
->TexSize
& TEX_SIZE_4(i
))
350 tmp
->TexCoord
[i
].size
= 4;
352 VB
->TexCoordPtr
[i
] = &tmp
->TexCoord
[i
];
357 if ((inputs
& VERT_MATERIAL
) && IM
->Material
) {
358 VB
->MaterialMask
= IM
->MaterialMask
+ start
;
359 VB
->Material
= IM
->Material
+ start
;
362 /* GL_NV_vertex_program */
363 if (ctx
->VertexProgram
.Enabled
) {
365 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
366 tmp
->Attribs
[attr
].count
= count
;
367 tmp
->Attribs
[attr
].data
= IM
->Attrib
[attr
] + start
;
368 tmp
->Attribs
[attr
].start
= (GLfloat
*) (IM
->Attrib
[attr
] + start
);
369 tmp
->Attribs
[attr
].size
= 4;
370 VB
->AttribPtr
[attr
] = &(tmp
->Attribs
[attr
]);
378 /* Called by exec_vert_cassette, execute_compiled_cassette, but not
381 void _tnl_run_cassette( GLcontext
*ctx
, struct immediate
*IM
)
383 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
385 _tnl_vb_bind_immediate( ctx
, IM
);
387 if (IM
->OrFlag
& VERT_EVAL_ANY
)
388 _tnl_eval_immediate( ctx
, IM
);
390 /* Invalidate all stored data before and after run:
392 tnl
->pipeline
.run_input_changes
|= tnl
->pipeline
.inputs
;
393 tnl
->Driver
.RunPipeline( ctx
);
394 tnl
->pipeline
.run_input_changes
|= tnl
->pipeline
.inputs
;
396 _tnl_copy_to_current( ctx
, IM
, IM
->OrFlag
, IM
->LastData
);
400 /* Called for regular vertex cassettes.
402 static void exec_vert_cassette( GLcontext
*ctx
, struct immediate
*IM
)
404 /* fprintf(stderr, "%s\n", __FUNCTION__); */
407 /* Orflag is computed twice, but only reach this code if app is
408 * using a mixture of glArrayElement() and glVertex() while
409 * arrays are locked (else would be in exec_elt_cassette now).
411 ASSERT(ctx
->Array
.LockCount
);
412 ASSERT(IM
->FlushElt
== FLUSH_ELT_LAZY
);
413 _tnl_translate_array_elts( ctx
, IM
, IM
->CopyStart
, IM
->Count
);
414 _tnl_compute_orflag( IM
, IM
->CopyStart
);
417 _tnl_fixup_input( ctx
, IM
);
418 /* _tnl_print_cassette( IM ); */
419 _tnl_run_cassette( ctx
, IM
);
423 /* Called for pure, locked VERT_ELT cassettes instead of
426 static void exec_elt_cassette( GLcontext
*ctx
, struct immediate
*IM
)
428 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
429 struct vertex_buffer
*VB
= &tnl
->vb
;
431 /* fprintf(stderr, "%s\n", __FUNCTION__); */
433 _tnl_vb_bind_arrays( ctx
, ctx
->Array
.LockFirst
, ctx
->Array
.LockCount
);
435 /* Take only elements and primitive information from the immediate:
437 VB
->Elts
= IM
->Elt
+ IM
->CopyStart
;
438 VB
->Primitive
= IM
->Primitive
+ IM
->CopyStart
;
439 VB
->PrimitiveLength
= IM
->PrimitiveLength
+ IM
->CopyStart
;
440 VB
->FirstPrimitive
= 0;
442 /* Run the pipeline. No input changes as a result of this action.
444 tnl
->Driver
.RunPipeline( ctx
);
446 /* Still need to update current values:
448 if (ctx
->Driver
.CurrentExecPrimitive
== GL_POLYGON
+1) {
449 _tnl_translate_array_elts( ctx
, IM
, IM
->LastData
, IM
->LastData
);
450 _tnl_copy_to_current( ctx
, IM
, ctx
->Array
._Enabled
, IM
->LastData
);
456 exec_empty_cassette( GLcontext
*ctx
, struct immediate
*IM
)
459 _tnl_translate_array_elts( ctx
, IM
, IM
->CopyStart
, IM
->CopyStart
);
461 _tnl_copy_to_current( ctx
, IM
, IM
->OrFlag
, IM
->LastData
);
466 /* Called for all cassettes when not compiling or playing a display
469 void _tnl_execute_cassette( GLcontext
*ctx
, struct immediate
*IM
)
471 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
473 _tnl_compute_orflag( IM
, IM
->Start
);
474 _tnl_copy_immediate_vertices( ctx
, IM
);
475 _tnl_get_exec_copy_verts( ctx
, IM
);
477 if (tnl
->pipeline
.build_state_changes
)
478 _tnl_validate_pipeline( ctx
);
480 if (IM
->CopyStart
== IM
->Count
) {
481 exec_empty_cassette( ctx
, IM
);
483 else if ((IM
->CopyOrFlag
& VERT_DATA
) == VERT_ELT
&&
484 ctx
->Array
.LockCount
&&
485 ctx
->Array
.Vertex
.Enabled
) {
486 exec_elt_cassette( ctx
, IM
);
489 exec_vert_cassette( ctx
, IM
);
492 /* Only reuse the immediate if there are no copied vertices living
496 GLuint begin_state
= IM
->BeginState
& (VERT_BEGIN_0
|VERT_BEGIN_1
);
497 GLuint saved_begin_state
= IM
->SavedBeginState
;
499 if (--IM
->ref_count
!= 0) {
500 IM
= _tnl_alloc_immediate( ctx
);
501 SET_IMMEDIATE( ctx
, IM
);
506 _tnl_reset_exec_input( ctx
, IMM_MAX_COPIED_VERTS
,
507 begin_state
, saved_begin_state
);
510 if (ctx
->Driver
.CurrentExecPrimitive
== GL_POLYGON
+1)
511 ctx
->Driver
.NeedFlush
&= ~FLUSH_STORED_VERTICES
;
517 /* Setup vector pointers that will be used to bind immediates to VB's.
519 void _tnl_imm_init( GLcontext
*ctx
)
521 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
522 struct vertex_arrays
*tmp
= &tnl
->imm_inputs
;
524 static int firsttime
= 1;
531 ctx
->swtnl_im
= _tnl_alloc_immediate( ctx
);
532 TNL_CURRENT_IM(ctx
)->ref_count
++;
534 tnl
->ExecCopyTexSize
= 0;
535 tnl
->ExecCopyCount
= 0;
536 tnl
->ExecCopySource
= 0;
538 TNL_CURRENT_IM(ctx
)->CopyStart
= IMM_MAX_COPIED_VERTS
;
540 _mesa_vector4f_init( &tmp
->Obj
, 0, 0 );
541 _mesa_vector4f_init( &tmp
->Normal
, 0, 0 );
544 tmp
->Color
.Type
= GL_FLOAT
;
546 tmp
->Color
.Stride
= 0;
547 tmp
->Color
.StrideB
= 4 * sizeof(GLfloat
);
548 tmp
->Color
.Flags
= 0;
550 tmp
->SecondaryColor
.Ptr
= 0;
551 tmp
->SecondaryColor
.Type
= GL_FLOAT
;
552 tmp
->SecondaryColor
.Size
= 4;
553 tmp
->SecondaryColor
.Stride
= 0;
554 tmp
->SecondaryColor
.StrideB
= 4 * sizeof(GLfloat
);
555 tmp
->SecondaryColor
.Flags
= 0;
557 _mesa_vector4f_init( &tmp
->FogCoord
, 0, 0 );
558 _mesa_vector1ui_init( &tmp
->Index
, 0, 0 );
559 _mesa_vector1ub_init( &tmp
->EdgeFlag
, 0, 0 );
561 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++)
562 _mesa_vector4f_init( &tmp
->TexCoord
[i
], 0, 0);
564 /* Install the first immediate. Intially outside begin/end.
566 _tnl_reset_exec_input( ctx
, IMM_MAX_COPIED_VERTS
, 0, 0 );
567 tnl
->ReplayHardBeginEnd
= 0;
569 _tnl_imm_vtxfmt_init( ctx
);
573 void _tnl_imm_destroy( GLcontext
*ctx
)
575 if (TNL_CURRENT_IM(ctx
)) {
576 TNL_CURRENT_IM(ctx
)->ref_count
--;
577 if (TNL_CURRENT_IM(ctx
)->ref_count
== 0)
578 _tnl_free_immediate( TNL_CURRENT_IM(ctx
) );
580 * Don't use SET_IMMEDIATE here, or else we'll whack the
581 * _tnl_CurrentInput pointer - not good when another
582 * context has already been made current.
583 * So we just set the context's own tnl immediate pointer