More g++ warning fixes. Fixes for CHAN_BITS==16, it seems to work.
[mesa.git] / src / mesa / tnl / t_imm_exec.c
1 /* $Id: t_imm_exec.c,v 1.15 2001/03/08 15:23:47 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 /* Called to initialize new buffers, and to recycle old ones.
61 */
62 void _tnl_reset_input( GLcontext *ctx,
63 GLuint start,
64 GLuint beginstate,
65 GLuint savedbeginstate )
66 {
67 struct immediate *IM = TNL_CURRENT_IM(ctx);
68
69 /* Clear the dirty part of the flag array.
70 */
71 if (start < IM->Count+2)
72 MEMSET(IM->Flag + start, 0, sizeof(GLuint) * (IM->Count+2-start));
73
74 IM->CopyStart = IM->Start = IM->Count = start;
75 IM->Primitive[IM->Start] = (ctx->Driver.CurrentExecPrimitive | PRIM_LAST);
76 IM->LastPrimitive = IM->Start;
77 IM->BeginState = beginstate;
78 IM->SavedBeginState = savedbeginstate;
79 IM->TexSize = 0;
80 IM->LastMaterial = 0;
81 IM->MaterialOrMask = 0;
82
83 IM->ArrayEltFlags = ~ctx->Array._Enabled;
84 IM->ArrayEltIncr = ctx->Array.Vertex.Enabled ? 1 : 0;
85 IM->ArrayEltFlush = !ctx->Array.LockCount;
86 }
87
88
89
90 void _tnl_copy_to_current( GLcontext *ctx, struct immediate *IM,
91 GLuint flag )
92 {
93 GLuint count = IM->LastData;
94
95 if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
96 _tnl_print_vert_flags("copy to current", flag);
97
98 if (flag & VERT_NORM)
99 COPY_3FV( ctx->Current.Normal, IM->Normal[count]);
100
101 if (flag & VERT_INDEX)
102 ctx->Current.Index = IM->Index[count];
103
104 if (flag & VERT_EDGE)
105 ctx->Current.EdgeFlag = IM->EdgeFlag[count];
106
107 if (flag & VERT_RGBA) {
108 COPY_CHAN4(ctx->Current.Color, IM->Color[count]);
109 if (ctx->Light.ColorMaterialEnabled) {
110 _mesa_update_color_material( ctx, ctx->Current.Color );
111 _mesa_validate_all_lighting_tables( ctx );
112 }
113 }
114
115 if (flag & VERT_SPEC_RGB)
116 COPY_CHAN4(ctx->Current.SecondaryColor, IM->SecondaryColor[count]);
117
118 if (flag & VERT_FOG_COORD)
119 ctx->Current.FogCoord = IM->FogCoord[count];
120
121 if (flag & VERT_TEX_ANY) {
122 GLuint i;
123 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
124 if (flag & VERT_TEX(i)) {
125 COPY_4FV( ctx->Current.Texcoord[0], IM->TexCoord[0][count]);
126 }
127 }
128 }
129
130 if (flag & VERT_MATERIAL) {
131 _mesa_update_material( ctx,
132 IM->Material[IM->LastMaterial],
133 IM->MaterialOrMask );
134
135 _mesa_validate_all_lighting_tables( ctx );
136 }
137 }
138
139
140
141 void _tnl_compute_orflag( struct immediate *IM )
142 {
143 GLuint count = IM->Count;
144 GLuint orflag = 0;
145 GLuint andflag = ~0U;
146 GLuint i;
147
148 IM->LastData = count-1;
149
150
151 /* Compute the flags for the whole buffer.
152 */
153 for (i = IM->CopyStart ; i < count ; i++) {
154 andflag &= IM->Flag[i];
155 orflag |= IM->Flag[i];
156 }
157
158 /* It is possible there will be data in the buffer arising from
159 * calls like 'glNormal', 'glMaterial' that occur after the final
160 * glVertex, glEval, etc. Additionally, a buffer can consist of
161 * eg. a single glMaterial call, in which case IM->Start ==
162 * IM->Count, but the buffer is definitely not empty.
163 */
164 if (IM->Flag[i] & VERT_DATA) {
165 IM->LastData++;
166 orflag |= IM->Flag[i];
167 }
168
169 IM->Flag[IM->LastData+1] |= VERT_END_VB;
170 IM->CopyAndFlag = IM->AndFlag = andflag;
171 IM->CopyOrFlag = IM->OrFlag = orflag;
172 }
173
174
175
176
177
178 /* Note: The 'start' member of the GLvector structs is now redundant
179 * because we always re-transform copied vertices, and the vectors
180 * below are set up so that the first copied vertex (if any) appears
181 * at position zero.
182 */
183 static void _tnl_vb_bind_immediate( GLcontext *ctx, struct immediate *IM )
184 {
185 TNLcontext *tnl = TNL_CONTEXT(ctx);
186 struct vertex_buffer *VB = &tnl->vb;
187 struct vertex_arrays *tmp = &tnl->imm_inputs;
188 GLuint inputs = tnl->pipeline.inputs; /* for copy-to-current */
189 GLuint start = IM->CopyStart;
190 GLuint count = IM->Count - start;
191
192 /* TODO: optimize the case where nothing has changed. (Just bind
193 * tmp to vb).
194 */
195
196 /* Setup constant data in the VB.
197 */
198 VB->Count = count;
199 VB->FirstClipped = IMM_MAXDATA - IM->CopyStart;
200 VB->import_data = 0;
201 VB->importable_data = 0;
202
203 /* Need an IM->FirstPrimitive?
204 */
205 VB->Primitive = IM->Primitive + IM->CopyStart;
206 VB->PrimitiveLength = IM->PrimitiveLength + IM->CopyStart;
207 VB->FirstPrimitive = 0;
208
209 VB->Flag = IM->Flag + start;
210
211 /* TexCoordPtr's are zeroed in loop below.
212 */
213 VB->NormalPtr = 0;
214 VB->FogCoordPtr = 0;
215 VB->EdgeFlag = 0;
216 VB->IndexPtr[0] = 0;
217 VB->IndexPtr[1] = 0;
218 VB->ColorPtr[0] = 0;
219 VB->ColorPtr[1] = 0;
220 VB->SecondaryColorPtr[0] = 0;
221 VB->SecondaryColorPtr[1] = 0;
222 VB->Elts = 0;
223 VB->MaterialMask = 0;
224 VB->Material = 0;
225
226 /* _tnl_print_vert_flags("copy-orflag", IM->CopyOrFlag); */
227 /* _tnl_print_vert_flags("orflag", IM->OrFlag); */
228 /* _tnl_print_vert_flags("inputs", inputs); */
229
230 /* Setup the initial values of array pointers in the vb.
231 */
232 if (inputs & VERT_OBJ) {
233 tmp->Obj.data = IM->Obj + start;
234 tmp->Obj.start = (GLfloat *)(IM->Obj + start);
235 tmp->Obj.count = count;
236 VB->ObjPtr = &tmp->Obj;
237 if ((IM->CopyOrFlag & VERT_OBJ_234) == VERT_OBJ_234)
238 tmp->Obj.size = 4;
239 else if ((IM->CopyOrFlag & VERT_OBJ_234) == VERT_OBJ_23)
240 tmp->Obj.size = 3;
241 else
242 tmp->Obj.size = 2;
243 }
244
245 if (inputs & VERT_NORM) {
246 tmp->Normal.data = IM->Normal + start;
247 tmp->Normal.start = (GLfloat *)(IM->Normal + start);
248 tmp->Normal.count = count;
249 VB->NormalPtr = &tmp->Normal;
250 }
251
252 if (inputs & VERT_INDEX) {
253 tmp->Index.count = count;
254 tmp->Index.data = IM->Index + start;
255 tmp->Index.start = IM->Index + start;
256 VB->IndexPtr[0] = &tmp->Index;
257 }
258
259 if (inputs & VERT_FOG_COORD) {
260 tmp->FogCoord.data = IM->FogCoord + start;
261 tmp->FogCoord.start = IM->FogCoord + start;
262 tmp->FogCoord.count = count;
263 VB->FogCoordPtr = &tmp->FogCoord;
264 }
265
266 if (inputs & VERT_SPEC_RGB) {
267 tmp->SecondaryColor.data = IM->SecondaryColor + start;
268 tmp->SecondaryColor.start = (GLchan *)(IM->SecondaryColor + start);
269 tmp->SecondaryColor.count = count;
270 VB->SecondaryColorPtr[0] = &tmp->SecondaryColor;
271 }
272
273 if (inputs & VERT_EDGE) {
274 VB->EdgeFlag = IM->EdgeFlag + start;
275 }
276
277 if (inputs & VERT_RGBA) {
278 tmp->Color.data = IM->Color + start;
279 tmp->Color.start = (GLchan *)(IM->Color + start);
280 tmp->Color.count = count;
281 VB->ColorPtr[0] = &tmp->Color;
282 }
283
284 if (inputs & VERT_TEX_ANY) {
285 GLuint i;
286 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
287 VB->TexCoordPtr[i] = 0;
288 if (inputs & VERT_TEX(i)) {
289 tmp->TexCoord[i].count = count;
290 tmp->TexCoord[i].data = IM->TexCoord[i] + start;
291 tmp->TexCoord[i].start = (GLfloat *)(IM->TexCoord[i] + start);
292 tmp->TexCoord[i].size = 2;
293 if (IM->TexSize & TEX_SIZE_3(i)) {
294 tmp->TexCoord[i].size = 3;
295 if (IM->TexSize & TEX_SIZE_4(i))
296 tmp->TexCoord[i].size = 4;
297 }
298 VB->TexCoordPtr[i] = &tmp->TexCoord[i];
299 }
300 }
301 }
302
303 if ((inputs & VERT_MATERIAL) && IM->Material) {
304 VB->MaterialMask = IM->MaterialMask + start;
305 VB->Material = IM->Material + start;
306 }
307 }
308
309
310
311
312 /* Called by exec_cassette and execute_compiled_cassette.
313 */
314 void _tnl_run_cassette( GLcontext *ctx, struct immediate *IM )
315 {
316 TNLcontext *tnl = TNL_CONTEXT(ctx);
317
318 _tnl_vb_bind_immediate( ctx, IM );
319
320 if (IM->CopyOrFlag & VERT_EVAL_ANY)
321 _tnl_eval_vb( ctx,
322 IM->Obj + IM->CopyStart,
323 IM->CopyOrFlag,
324 IM->CopyAndFlag );
325
326
327 /* Invalidate all stored data before and after run:
328 */
329 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
330 _tnl_run_pipeline( ctx );
331 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
332
333 _tnl_copy_to_current( ctx, IM, IM->OrFlag );
334 }
335
336
337
338
339 /* Called for pure, locked VERT_ELT cassettes instead of
340 * _tnl_run_cassette.
341 */
342 static void exec_elt_cassette( GLcontext *ctx, struct immediate *IM )
343 {
344 TNLcontext *tnl = TNL_CONTEXT(ctx);
345 struct vertex_buffer *VB = &tnl->vb;
346
347 _tnl_vb_bind_arrays( ctx, ctx->Array.LockFirst, ctx->Array.LockCount );
348
349 VB->Elts = IM->Elt + IM->CopyStart;
350 VB->Primitive = IM->Primitive + IM->CopyStart;
351 VB->PrimitiveLength = IM->PrimitiveLength + IM->CopyStart;
352 VB->FirstPrimitive = 0;
353
354 /* Run the pipeline. No input changes as a result of this action.
355 */
356 _tnl_run_pipeline( ctx );
357
358 /* Still need to update current values: (TODO - copy from VB)
359 * TODO: delay this until FlushVertices
360 */
361 if (ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1) {
362 _tnl_translate_array_elts( ctx, IM, IM->LastData, IM->LastData );
363 _tnl_copy_to_current( ctx, IM, ctx->Array._Enabled );
364 }
365 }
366
367
368
369 /* Called for regular vertex cassettes.
370 */
371 static void exec_vert_cassette( GLcontext *ctx, struct immediate *IM )
372 {
373 if (IM->OrFlag & VERT_ELT) {
374 GLuint andflag = ~0;
375 GLuint i;
376 GLuint start = IM->FlushElt ? IM->LastPrimitive : IM->CopyStart;
377 _tnl_translate_array_elts( ctx, IM, start, IM->Count );
378
379 /* Need to recompute andflag.
380 */
381 if (IM->CopyAndFlag & VERT_ELT)
382 IM->CopyAndFlag |= ctx->Array._Enabled;
383 else {
384 for (i = IM->CopyStart ; i < IM->Count ; i++)
385 andflag &= IM->Flag[i];
386 IM->CopyAndFlag = andflag;
387 }
388 }
389
390 _tnl_fixup_input( ctx, IM );
391 /* _tnl_print_cassette( IM ); */
392 _tnl_run_cassette( ctx, IM );
393 }
394
395
396
397 /* Called for all cassettes when not compiling or playing a display
398 * list.
399 */
400 void _tnl_execute_cassette( GLcontext *ctx, struct immediate *IM )
401 {
402 TNLcontext *tnl = TNL_CONTEXT(ctx);
403
404 ASSERT(tnl->ExecCopySource == IM);
405
406 _tnl_compute_orflag( IM );
407
408 /* Mark the last primitive:
409 */
410 IM->PrimitiveLength[IM->LastPrimitive] = IM->Count - IM->LastPrimitive;
411 ASSERT(IM->Primitive[IM->LastPrimitive] & PRIM_LAST);
412
413 if (tnl->pipeline.build_state_changes)
414 _tnl_validate_pipeline( ctx );
415
416 _tnl_get_exec_copy_verts( ctx, IM );
417
418 if (IM->CopyStart == IM->Count) {
419 if (IM->OrFlag & VERT_ELT)
420 _tnl_translate_array_elts( ctx, IM, IM->CopyStart, IM->CopyStart );
421
422 _tnl_copy_to_current( ctx, IM, IM->OrFlag );
423 }
424 else if ((IM->OrFlag & VERT_DATA) == VERT_ELT &&
425 ctx->Array.LockCount &&
426 ctx->Array.Vertex.Enabled) {
427 exec_elt_cassette( ctx, IM );
428 }
429 else {
430 exec_vert_cassette( ctx, IM );
431 }
432
433 _tnl_reset_input( ctx,
434 IMM_MAX_COPIED_VERTS,
435 IM->BeginState & (VERT_BEGIN_0|VERT_BEGIN_1),
436 IM->SavedBeginState );
437
438 /* Copy vertices and primitive information to immediate before it
439 * can be overwritten.
440 */
441 _tnl_copy_immediate_vertices( ctx, IM );
442
443 if (ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1)
444 ctx->Driver.NeedFlush &= ~FLUSH_STORED_VERTICES;
445 }
446
447
448
449
450 /* Setup vector pointers that will be used to bind immediates to VB's.
451 */
452 void _tnl_imm_init( GLcontext *ctx )
453 {
454 TNLcontext *tnl = TNL_CONTEXT(ctx);
455 struct vertex_arrays *tmp = &tnl->imm_inputs;
456 GLuint i;
457 static int firsttime = 1;
458
459 if (firsttime) {
460 firsttime = 0;
461 _tnl_imm_elt_init();
462 }
463
464 ctx->swtnl_im = _tnl_alloc_immediate( ctx );
465 TNL_CURRENT_IM(ctx)->ref_count++;
466
467 tnl->ExecCopyTexSize = 0;
468 tnl->ExecCopyCount = 0;
469 tnl->ExecCopySource = TNL_CURRENT_IM(ctx);
470 TNL_CURRENT_IM(ctx)->ref_count++;
471
472 TNL_CURRENT_IM(ctx)->CopyStart = IMM_MAX_COPIED_VERTS;
473
474 _mesa_vector4f_init( &tmp->Obj, 0, 0 );
475 _mesa_vector3f_init( &tmp->Normal, 0, 0 );
476 _mesa_vector4chan_init( &tmp->Color, 0, 0 );
477 _mesa_vector4chan_init( &tmp->SecondaryColor, 0, 0 );
478 _mesa_vector1f_init( &tmp->FogCoord, 0, 0 );
479 _mesa_vector1ui_init( &tmp->Index, 0, 0 );
480 _mesa_vector1ub_init( &tmp->EdgeFlag, 0, 0 );
481
482 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
483 _mesa_vector4f_init( &tmp->TexCoord[i], 0, 0);
484
485 /* Install the first immediate. Intially outside begin/end.
486 */
487 _tnl_reset_input( ctx, IMM_MAX_COPIED_VERTS, 0, 0 );
488 tnl->ReplayHardBeginEnd = 0;
489
490 _tnl_imm_vtxfmt_init( ctx );
491 }
492
493
494 void _tnl_imm_destroy( GLcontext *ctx )
495 {
496 if (TNL_CURRENT_IM(ctx))
497 _tnl_free_immediate( TNL_CURRENT_IM(ctx) );
498
499 }