2 /* $Id: t_context.h,v 1.2 2000/11/20 13:39:36 keithw Exp $ */
5 * Mesa 3-D graphics library
8 * Copyright (C) 1999 Brian Paul All Rights Reserved.
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
24 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include "math/m_matrix.h"
35 #include "math/m_vector.h"
36 #include "math/m_xform.h"
38 #include "t_trans_elt.h"
43 * Bits to indicate which faces a vertex participates in,
44 * what facing the primitive provoked by that vertex has,
45 * and some misc. flags.
47 #define VERT_FACE_FRONT 0x1 /* is in a front-color primitive */
48 #define VERT_FACE_REAR 0x2 /* is in a rear-color primitive */
49 #define PRIM_FACE_FRONT 0x4 /* use front color */
50 #define PRIM_FACE_REAR 0x8 /* use rear color */
51 #define PRIM_CLIPPED 0x10 /* needs clipping */
52 #define PRIM_USER_CLIPPED CLIP_USER_BIT /* 0x40 */
55 #define PRIM_FLAG_SHIFT 2
56 #define PRIM_FACE_FLAGS (PRIM_FACE_FRONT|PRIM_FACE_REAR)
57 #define VERT_FACE_FLAGS (VERT_FACE_FRONT|VERT_FACE_REAR)
59 #define PRIM_ANY_CLIP (PRIM_CLIPPED|PRIM_USER_CLIPPED)
60 #define PRIM_NOT_CULLED (PRIM_ANY_CLIP|PRIM_FACE_FLAGS)
62 /* Flags for VB->CullMode.
64 #define CULL_MASK_ACTIVE 0x1
65 #define COMPACTED_NORMALS 0x2
66 #define CLIP_MASK_ACTIVE 0x4
68 /* Flags for selecting a shading function. The first two bits are
69 * shared with the cull mode (ie. cull_mask_active and
72 #define SHADE_TWOSIDE 0x4
75 /* KW: Flags that describe the current vertex state, and the contents
76 * of a vertex in a vertex-cassette.
78 * For really major expansion, consider a 'VERT_ADDITIONAL_FLAGS' flag,
79 * which means there is data in another flags array (eg, extra_flags[]).
82 #define VERT_OBJ_2 0x1 /* glVertex2 */
83 #define VERT_OBJ_3 0x2 /* glVertex3 */
84 #define VERT_OBJ_4 0x4 /* glVertex4 */
85 #define VERT_BEGIN 0x8 /* glBegin */
86 #define VERT_END 0x10 /* glEnd */
87 #define VERT_ELT 0x20 /* glArrayElement */
88 #define VERT_RGBA 0x40 /* glColor */
89 #define VERT_NORM 0x80 /* glNormal */
90 #define VERT_INDEX 0x100 /* glIndex */
91 #define VERT_EDGE 0x200 /* glEdgeFlag */
92 #define VERT_MATERIAL 0x400 /* glMaterial */
93 #define VERT_END_VB 0x800 /* end vb marker */
94 #define VERT_TEX0_12 0x1000
95 #define VERT_TEX0_3 0x2000
96 #define VERT_TEX0_4 0x4000
97 #define VERT_TEX1_12 0x8000
98 #define VERT_TEX1_3 0x10000
99 #define VERT_TEX1_4 0x20000
100 #define VERT_TEX2_12 0x40000
101 #define VERT_TEX2_3 0x80000
102 #define VERT_TEX2_4 0x100000
103 #define VERT_TEX3_12 0x200000
104 #define VERT_TEX3_3 0x400000
105 #define VERT_TEX3_4 0x800000
106 #define VERT_EVAL_C1 0x1000000 /* could reuse OBJ bits for this? */
107 #define VERT_EVAL_C2 0x2000000 /* - or just use 3 bits */
108 #define VERT_EVAL_P1 0x4000000 /* */
109 #define VERT_EVAL_P2 0x8000000 /* */
110 #define VERT_SPEC_RGB 0x10000000
111 #define VERT_FOG_COORD 0x20000000 /* internal use only, currently */
113 #define VERT_EYE VERT_BEGIN /* reuse */
114 #define VERT_WIN VERT_END /* reuse */
115 #define VERT_SETUP_FULL VERT_EVAL_P1 /* Rastersetup has been done */
116 #define VERT_PRECALC_DATA VERT_END_VB /* reuse */
120 #define VERT_TEX0_SHIFT 11
122 #define VERT_EVAL_ANY (VERT_EVAL_C1|VERT_EVAL_P1| \
123 VERT_EVAL_C2|VERT_EVAL_P2)
125 #define VERT_OBJ_23 (VERT_OBJ_3|VERT_OBJ_2)
126 #define VERT_OBJ_234 (VERT_OBJ_4|VERT_OBJ_23)
127 #define VERT_OBJ_ANY VERT_OBJ_2
129 #define VERT_TEX0_123 (VERT_TEX0_3|VERT_TEX0_12)
130 #define VERT_TEX0_1234 (VERT_TEX0_4|VERT_TEX0_123)
131 #define VERT_TEX0_ANY VERT_TEX0_12
133 #define VERT_TEX1_123 (VERT_TEX1_3|VERT_TEX1_12)
134 #define VERT_TEX1_1234 (VERT_TEX1_4|VERT_TEX1_123)
135 #define VERT_TEX1_ANY VERT_TEX1_12
137 #define VERT_TEX2_123 (VERT_TEX2_3|VERT_TEX2_12)
138 #define VERT_TEX2_1234 (VERT_TEX2_4|VERT_TEX2_123)
139 #define VERT_TEX2_ANY VERT_TEX2_12
141 #define VERT_TEX3_123 (VERT_TEX3_3|VERT_TEX3_12)
142 #define VERT_TEX3_1234 (VERT_TEX3_4|VERT_TEX3_123)
143 #define VERT_TEX3_ANY VERT_TEX3_12
145 #define NR_TEXSIZE_BITS 3
146 #define VERT_TEX_ANY(i) (VERT_TEX0_ANY << ((i) * NR_TEXSIZE_BITS))
148 #define VERT_FIXUP (VERT_TEX0_ANY | \
159 #define VERT_DATA (VERT_TEX0_ANY | \
178 #define VERT_BEGIN_0 0x1 /* glBegin (if initially inside beg/end) */
179 #define VERT_BEGIN_1 0x2 /* glBegin (if initially outside beg/end) */
180 #define VERT_ERROR_0 0x4 /* invalid_operation in initial state 0 */
181 #define VERT_ERROR_1 0x8 /* invalid_operation in initial state 1 */
188 ** Vertex buffer/array structures
194 GLfloat (*Normal
)[3];
198 GLfloat (*TexCoord
[MAX_TEXTURE_UNITS
])[4];
201 GLubyte (*SecondaryColor
)[4];
210 GLvector1ub EdgeFlag
;
211 GLvector4f TexCoord
[MAX_TEXTURE_UNITS
];
213 GLvector4ub SecondaryColor
;
217 struct vertex_array_pointers
223 GLvector1ub
*EdgeFlag
;
224 GLvector4f
*TexCoord
[MAX_TEXTURE_UNITS
];
226 GLvector4ub
*SecondaryColor
;
227 GLvector1f
*FogCoord
;
230 /* Values for VB->Type */
237 /* Values for immediate->BeginState */
238 #define VERT_BEGIN_0 0x1 /* glBegin (if initially inside beg/end) */
239 #define VERT_BEGIN_1 0x2 /* glBegin (if initially outside beg/end) */
240 #define VERT_ERROR_0 0x4 /* invalid_operation in initial state 0 */
241 #define VERT_ERROR_1 0x8 /* invalid_operation in initial state 1 */
244 /* KW: Represents everything that can take place between a begin and
245 * end, and can represent multiple begin/end pairs. This plus *any*
246 * state variable (GLcontext) should be all you need to replay the
247 * represented begin/end pairs as if they took place in that state.
249 * Thus this is sufficient for both immediate and compiled modes, but
250 * we could/should throw some elements away for compiled mode if we
251 * know they were empty.
255 struct immediate
*next
; /* for cache of free IM's */
256 GLuint id
, ref_count
;
258 /* This must be saved when immediates are shared in display lists.
261 GLuint LastData
; /* count or count+1 */
262 GLuint AndFlag
, OrFlag
, BeginState
;
263 GLuint LastPrimitive
;
265 GLuint ArrayAndFlags
; /* precalc'ed for glArrayElt */
267 GLuint ArrayEltFlush
;
270 GLuint TF1
[MAX_TEXTURE_UNITS
]; /* precalc'ed for glTexCoord */
271 GLuint TF2
[MAX_TEXTURE_UNITS
];
272 GLuint TF3
[MAX_TEXTURE_UNITS
];
273 GLuint TF4
[MAX_TEXTURE_UNITS
];
275 GLuint Primitive
[VB_SIZE
]; /* GLubyte would do... */
276 GLuint NextPrimitive
[VB_SIZE
];
278 /* allocate storage for these on demand:
280 struct gl_material (*Material
)[2];
281 GLuint
*MaterialMask
;
283 GLfloat (*TexCoordPtr
[MAX_TEXTURE_UNITS
])[4];
285 struct vertex_arrays v
;
287 struct __GLcontextRec
*backref
;
289 /* Normal lengths, zero if not available.
291 GLfloat
*NormalLengths
;
292 GLuint LastCalcedLength
;
294 GLuint Flag
[VB_SIZE
]; /* bitwise-OR of VERT_ flags */
295 GLchan Color
[VB_SIZE
][4];
296 GLfloat Obj
[VB_SIZE
][4];
297 GLfloat Normal
[VB_SIZE
][3];
298 GLfloat TexCoord
[MAX_TEXTURE_UNITS
][VB_SIZE
][4];
300 GLubyte EdgeFlag
[VB_SIZE
];
301 GLuint Index
[VB_SIZE
];
302 GLubyte SecondaryColor
[VB_SIZE
][4];
303 GLfloat FogCoord
[VB_SIZE
];
307 /* Not so big on storage these days, although still has pointers to
308 * arrays used for temporary results.
310 typedef struct vertex_buffer
314 struct __GLcontextRec
*ctx
;
315 struct tnl_context
*tnlctx
;
317 /* Driver_data is allocated in Driver.RegisterVB(), if required.
321 /* List of operations to process vertices in current state.
323 struct gl_pipeline
*pipeline
;
325 /* Temporary storage used by immediate mode functions and various
326 * operations in the pipeline.
328 struct immediate
*IM
;
329 struct vertex_array_pointers store
;
331 /* Where to find outstanding untransformed vertices.
333 struct immediate
*prev_buffer
;
335 GLuint Type
; /* Either VB_IMMEDIATE or VB_CVA_PRECALC */
337 GLuint Size
, Start
, Count
;
338 GLuint Free
, FirstFree
;
342 GLuint IndirectCount
; /* defaults to count */
343 GLuint OrFlag
, SavedOrFlag
;
345 GLuint Culled
, CullDone
;
347 /* Pointers to input data - default to buffers in 'im' above.
350 GLvector3f
*NormalPtr
;
351 GLvector4ub
*ColorPtr
;
352 GLvector1ui
*IndexPtr
;
353 GLvector1ub
*EdgeFlagPtr
;
354 GLvector4f
*TexCoordPtr
[MAX_TEXTURE_UNITS
];
356 GLvector4ub
*SecondaryColorPtr
;
357 GLvector1f
*FogCoordPtr
;
358 GLuint
*Flag
, FlagMax
;
359 struct gl_material (*Material
)[2];
360 GLuint
*MaterialMask
;
362 GLuint
*NextPrimitive
;
364 GLuint LastPrimitive
;
366 GLfloat (*BoundsPtr
)[3]; /* Bounds for cull check */
367 GLfloat
*NormalLengthPtr
; /* Array of precomputed inv. normal lengths */
369 /* Holds malloced storage for pipeline data not supplied by
370 * the immediate struct.
375 GLvector4ub BColor
; /* not used in cva vb's */
376 GLvector1ui BIndex
; /* not used in cva vb's */
377 GLvector4ub BSecondary
; /* not used in cva vb's */
379 /* Temporary storage - may point into IM, or be dynamically
380 * allocated (for cva).
383 GLubyte
*UserClipMask
;
385 /* Internal values. Where these point depends on whether
386 * there were any identity matrices defined as transformations
391 GLvector4f
*Unprojected
;
392 GLvector4f
*Projected
;
393 GLvector4f
*CurrentTexCoord
;
394 GLuint
*Indirect
; /* For eval rescue and cva render */
396 /* Currently active colors
398 GLvector4ub
*Color
[2];
399 GLvector1ui
*Index
[2];
400 GLvector4ub
*SecondaryColor
[2];
402 /* Storage for colors which have been lit but not yet fogged.
403 * Required for CVA, just point into store for normal VB's.
405 GLvector4ub
*LitColor
[2];
406 GLvector1ui
*LitIndex
[2];
407 GLvector4ub
*LitSecondary
[2];
409 /* Temporary values used in texgen.
414 /* Temporary values used in eval.
416 GLuint
*EvaluatedFlags
;
420 GLubyte
*NormCullStart
;
421 GLubyte
*CullMask
; /* Results of vertex culling */
422 GLubyte
*NormCullMask
; /* Compressed onto shared normals */
424 GLubyte ClipOrMask
; /* bitwise-OR of all ClipMask[] values */
425 GLubyte ClipAndMask
; /* bitwise-AND of all ClipMask[] values */
427 GLubyte CullMode
; /* see flags below */
429 GLuint CopyCount
; /* max 3 vertices to copy after transform */
431 GLfloat CopyProj
[3][4]; /* temporary store for projected clip coords */
433 /* Hooks for module private data
440 typedef void (*gl_shade_func
)( struct vertex_buffer
*VB
);
442 typedef void (*clip_interp_func
)( struct vertex_buffer
*VB
, GLuint dst
,
443 GLfloat t
, GLuint in
, GLuint out
);
445 typedef GLuint (*clip_line_func
)( struct vertex_buffer
*VB
,
446 GLuint
*i
, GLuint
*j
,
449 typedef GLuint (*clip_poly_func
)( struct vertex_buffer
*VB
,
450 GLuint n
, GLuint vlist
[],
454 #define MAX_PIPELINE_STAGES 30
456 #define PIPE_IMMEDIATE 0x1
457 #define PIPE_PRECALC 0x2
459 #define PIPE_OP_VERT_XFORM 0x1
460 #define PIPE_OP_NORM_XFORM 0x2
461 #define PIPE_OP_LIGHT 0x4
462 #define PIPE_OP_FOG 0x8
463 #define PIPE_OP_TEX0 0x10
464 #define PIPE_OP_TEX1 0x20
465 #define PIPE_OP_TEX2 0x40
466 #define PIPE_OP_TEX3 0x80
467 #define PIPE_OP_RAST_SETUP_0 0x100
468 #define PIPE_OP_RAST_SETUP_1 0x200
469 #define PIPE_OP_RENDER 0x400
470 #define PIPE_OP_CVA_PREPARE 0x800
474 struct gl_pipeline_stage
{
476 GLuint ops
; /* PIPE_OP flags */
477 GLuint type
; /* VERT flags */
478 GLuint special
; /* VERT flags - force update_inputs() */
479 GLuint state_change
; /* state flags - trigger update_inputs() */
480 GLuint cva_state_change
; /* state flags - recalc cva buffer */
481 GLuint elt_forbidden_inputs
; /* VERT flags - force a pipeline recalc */
482 GLuint pre_forbidden_inputs
; /* VERT flags - force a pipeline recalc */
483 GLuint active
; /* VERT flags */
484 GLuint inputs
; /* VERT flags */
485 GLuint outputs
; /* VERT flags */
486 void (*check
)( GLcontext
*ctx
, struct gl_pipeline_stage
* );
487 void (*run
)( struct vertex_buffer
*VB
);
492 GLuint state_change
; /* state changes which require recalc */
493 GLuint cva_state_change
; /* ... which require re-run */
494 GLuint forbidden_inputs
; /* inputs which require recalc */
495 GLuint ops
; /* what gets done in this pipe */
503 GLuint pipeline_valid
:1;
505 GLuint copy_transformed_data
:1;
506 GLuint replay_copied_vertices
:1;
507 GLuint new_state
; /* state changes since last recalc */
508 struct gl_pipeline_stage
*stages
[MAX_PIPELINE_STAGES
];
513 /* All fields are derived.
516 struct gl_pipeline pre
;
517 struct gl_pipeline elt
;
519 struct gl_client_array Elt
;
520 trans_1ui_func EltFunc
;
522 struct vertex_buffer
*VB
;
523 struct vertex_arrays v
;
524 struct vertex_data store
;
530 GLuint forbidden_inputs
;
536 GLuint last_array_flags
;
537 GLuint last_array_new_state
;
542 typedef void (*texgen_func
)( struct vertex_buffer
*VB
,
547 typedef struct tnl_context
{
549 GLuint _ArrayFlag
[VB_SIZE
]; /* crock */
551 GLuint _ArraySummary
; /* Like flags, but no size information */
552 GLuint _ArrayNewState
; /* Tracks which arrays have been changed. */
555 /* Pipeline stages - shared between the two pipelines,
558 struct gl_pipeline_stage PipelineStage
[MAX_PIPELINE_STAGES
];
559 GLuint NrPipelineStages
;
561 /* Per-texunit derived state.
563 GLuint _TexgenSize
[MAX_TEXTURE_UNITS
];
564 GLuint _TexgenHoles
[MAX_TEXTURE_UNITS
];
565 texgen_func
*_TexgenFunc
[MAX_TEXTURE_UNITS
];
568 /* Display list extensions
570 GLuint opcode_vertex_cassette
;
575 GLboolean CompileCVAFlag
;
577 clip_poly_func
*_poly_clip_tab
;
578 clip_line_func
*_line_clip_tab
;
579 clip_interp_func _ClipInterpFunc
; /* Clip interpolation function */
580 normal_func
*_NormalTransform
;
581 gl_shade_func
*_shade_func_tab
; /* Current shading function table */
583 GLenum _CurrentPrimitive
; /* Prim or GL_POLYGON+1 */
586 GLuint _RenderFlags
; /* Active inputs to render stage */
588 /* Cache of unused immediate structs */
589 struct immediate
*freed_im_queue
;
596 #define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context))
597 #define TNL_CURRENT_IM(ctx) ((struct immediate *)(ctx->swtnl_im))
598 #define TNL_VB(ctx) ((struct vertex_buffer *)(ctx->swtnl_vb))
600 extern void _tnl_reset_immediate( GLcontext
*ctx
);
601 extern GLboolean
_tnl_flush_vertices( GLcontext
*ctx
, GLuint flush_flags
);
605 _tnl_MakeCurrent( GLcontext
*ctx
,
606 GLframebuffer
*drawBuffer
,
607 GLframebuffer
*readBuffer
);
611 _tnl_LightingSpaceChange( GLcontext
*ctx
);
614 * Macros for fetching current input buffer.
617 #define GET_IMMEDIATE struct immediate *IM = TNL_CURRENT_IM(((GLcontext *) (_glapi_Context ? _glapi_Context : _glapi_get_context())))
618 #define SET_IMMEDIATE(ctx, im) ctx->swtnl_im = (void *)im
620 extern struct immediate
*_mesa_CurrentInput
;
621 #define GET_IMMEDIATE struct immediate *IM = _mesa_CurrentInput
622 #define SET_IMMEDIATE(ctx, im) \
624 ctx->swtnl_im = (void *)im; \
625 _mesa_CurrentInput = im; \