2 /* $Id: t_context.h,v 1.6 2000/12/08 00:18:39 brianp 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 0x1000
95 #define VERT_TEX1 0x2000
96 #define VERT_TEX2 0x3000
97 #define VERT_TEX3 0x4000
98 #define VERT_TEX4 0x10000
99 #define VERT_TEX5 0x20000
100 #define VERT_TEX6 0x30000
101 #define VERT_TEX7 0x40000
102 #define VERT_TEX8 0x100000
103 #define VERT_TEX9 0x200000
104 #define VERT_TEX10 0x300000
105 #define VERT_TEX11 0x400000
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
112 #define VERT_POINT_SIZE 0x40000000
114 #define VERT_EYE VERT_BEGIN /* reuse */
115 #define VERT_WIN VERT_END /* reuse */
116 #define VERT_SETUP_FULL VERT_EVAL_P1 /* Rastersetup has been done */
117 #define VERT_PRECALC_DATA VERT_END_VB /* reuse */
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_SHIFT 11
130 #define VERT_TEX(i) (VERT_TEX0 << i)
131 #define VERT_TEX_ANY (VERT_TEX0 | \
144 #define VERT_FIXUP (VERT_TEX_ANY | \
152 #define VERT_DATA (VERT_TEX_ANY | \
172 ** Vertex buffer/array structures
178 GLfloat (*Normal
)[3];
182 GLfloat (*TexCoord
[MAX_TEXTURE_UNITS
])[4];
185 GLubyte (*SecondaryColor
)[4];
194 GLvector1ub EdgeFlag
;
195 GLvector4f TexCoord
[MAX_TEXTURE_UNITS
];
197 GLvector4ub SecondaryColor
;
201 struct vertex_array_pointers
207 GLvector1ub
*EdgeFlag
;
208 GLvector4f
*TexCoord
[MAX_TEXTURE_UNITS
];
210 GLvector4ub
*SecondaryColor
;
211 GLvector1f
*FogCoord
;
214 /* Values for VB->Type */
221 /* Values for immediate->BeginState */
222 #define VERT_BEGIN_0 0x1 /* glBegin (if initially inside beg/end) */
223 #define VERT_BEGIN_1 0x2 /* glBegin (if initially outside beg/end) */
224 #define VERT_ERROR_0 0x4 /* invalid_operation in initial state 0 */
225 #define VERT_ERROR_1 0x8 /* invalid_operation in initial state 1 */
227 /* KW: Represents everything that can take place between a begin and
228 * end, and can represent multiple begin/end pairs. This plus *any*
229 * state variable (GLcontext) should be all you need to replay the
230 * represented begin/end pairs as if they took place in that state.
232 * Thus this is sufficient for both immediate and compiled modes, but
233 * we could/should throw some elements away for compiled mode if we
234 * know they were empty.
238 struct immediate
*next
; /* for cache of free IM's */
239 GLuint id
, ref_count
;
241 /* This must be saved when immediates are shared in display lists.
244 GLuint LastData
; /* count or count+1 */
245 GLuint AndFlag
, OrFlag
;
246 GLuint Tex3Flag
, Tex4Flag
; /* keep track of texcoord sizes */
247 GLuint BeginState
, SavedBeginState
;
248 GLuint LastPrimitive
;
250 GLuint ArrayAndFlags
; /* precalc'ed for glArrayElt */
252 GLuint ArrayEltFlush
;
255 GLuint Primitive
[VB_SIZE
]; /* GLubyte would do... */
256 GLuint NextPrimitive
[VB_SIZE
];
258 /* allocate storage for these on demand:
260 struct gl_material (*Material
)[2];
261 GLuint
*MaterialMask
;
263 GLfloat (*TexCoordPtr
[MAX_TEXTURE_UNITS
])[4];
265 struct vertex_arrays v
;
267 struct __GLcontextRec
*backref
;
269 /* Normal lengths, zero if not available.
271 GLfloat
*NormalLengths
;
272 GLuint LastCalcedLength
;
274 GLuint Flag
[VB_SIZE
]; /* bitwise-OR of VERT_ flags */
275 GLchan Color
[VB_SIZE
][4];
276 GLfloat Obj
[VB_SIZE
][4];
277 GLfloat Normal
[VB_SIZE
][3];
278 GLfloat TexCoord
[MAX_TEXTURE_UNITS
][VB_SIZE
][4];
280 GLubyte EdgeFlag
[VB_SIZE
];
281 GLuint Index
[VB_SIZE
];
282 GLubyte SecondaryColor
[VB_SIZE
][4];
283 GLfloat FogCoord
[VB_SIZE
];
287 /* Not so big on storage these days, although still has pointers to
288 * arrays used for temporary results.
290 typedef struct vertex_buffer
294 struct __GLcontextRec
*ctx
;
295 struct tnl_context
*tnlctx
;
297 /* Driver_data is allocated in Driver.RegisterVB(), if required.
301 /* List of operations to process vertices in current state.
303 struct gl_pipeline
*pipeline
;
305 /* Temporary storage used by immediate mode functions and various
306 * operations in the pipeline.
308 struct immediate
*IM
;
309 struct vertex_array_pointers store
;
311 /* Where to find outstanding untransformed vertices.
313 struct immediate
*prev_buffer
;
315 GLuint Type
; /* Either VB_IMMEDIATE or VB_CVA_PRECALC */
317 GLuint Size
, Start
, Count
;
318 GLuint Free
, FirstFree
;
322 GLuint IndirectCount
; /* defaults to count */
323 GLuint OrFlag
, SavedOrFlag
;
324 GLuint Tex3Flag
, Tex4Flag
;
325 GLuint SavedTex3Flag
, SavedTex4Flag
;
327 GLuint Culled
, CullDone
;
329 /* Pointers to input data - default to buffers in 'im' above.
332 GLvector3f
*NormalPtr
;
333 GLvector4ub
*ColorPtr
;
334 GLvector1ui
*IndexPtr
;
335 GLvector1ub
*EdgeFlagPtr
;
336 GLvector4f
*TexCoordPtr
[MAX_TEXTURE_UNITS
];
338 GLvector4ub
*SecondaryColorPtr
;
339 GLvector1f
*FogCoordPtr
;
340 GLuint
*Flag
, FlagMax
;
341 struct gl_material (*Material
)[2];
342 GLuint
*MaterialMask
;
344 GLuint
*NextPrimitive
;
346 GLuint LastPrimitive
;
348 GLfloat (*BoundsPtr
)[3]; /* Bounds for cull check */
349 GLfloat
*NormalLengthPtr
; /* Array of precomputed inv. normal lengths */
351 /* Holds malloced storage for pipeline data not supplied by
352 * the immediate struct.
357 GLvector1f PointSize
;
358 GLvector4ub BColor
; /* not used in cva vb's */
359 GLvector1ui BIndex
; /* not used in cva vb's */
360 GLvector4ub BSecondary
; /* not used in cva vb's */
362 /* Temporary storage - may point into IM, or be dynamically
363 * allocated (for cva).
366 GLubyte
*UserClipMask
;
368 /* Internal values. Where these point depends on whether
369 * there were any identity matrices defined as transformations
374 GLvector4f
*Unprojected
;
375 GLvector4f
*Projected
;
376 GLvector4f
*CurrentTexCoord
;
377 GLuint
*Indirect
; /* For eval rescue and cva render */
379 /* Currently active colors
381 GLvector4ub
*Color
[2];
382 GLvector1ui
*Index
[2];
383 GLvector4ub
*SecondaryColor
[2];
385 /* Storage for colors which have been lit but not yet fogged.
386 * Required for CVA, just point into store for normal VB's.
388 GLvector4ub
*LitColor
[2];
389 GLvector1ui
*LitIndex
[2];
390 GLvector4ub
*LitSecondary
[2];
392 /* Temporary values used in texgen.
397 /* Temporary values used in eval.
399 GLuint
*EvaluatedFlags
;
403 GLubyte
*NormCullStart
;
404 GLubyte
*CullMask
; /* Results of vertex culling */
405 GLubyte
*NormCullMask
; /* Compressed onto shared normals */
407 GLubyte ClipOrMask
; /* bitwise-OR of all ClipMask[] values */
408 GLubyte ClipAndMask
; /* bitwise-AND of all ClipMask[] values */
410 GLubyte CullMode
; /* see flags below */
412 GLuint CopyCount
; /* max 3 vertices to copy after transform */
414 GLfloat CopyProj
[3][4]; /* temporary store for projected clip coords */
416 /* Hooks for module private data
423 typedef void (*shade_func
)( struct vertex_buffer
*VB
);
425 typedef void (*clip_interp_func
)( struct vertex_buffer
*VB
, GLuint dst
,
426 GLfloat t
, GLuint in
, GLuint out
);
428 typedef GLuint (*clip_line_func
)( struct vertex_buffer
*VB
,
429 GLuint
*i
, GLuint
*j
,
432 typedef GLuint (*clip_poly_func
)( struct vertex_buffer
*VB
,
433 GLuint n
, GLuint vlist
[],
437 #define MAX_PIPELINE_STAGES 30
439 #define PIPE_IMMEDIATE 0x1
440 #define PIPE_PRECALC 0x2
442 #define PIPE_OP_VERT_XFORM 0x1
443 #define PIPE_OP_NORM_XFORM 0x2
444 #define PIPE_OP_LIGHT 0x4
445 #define PIPE_OP_FOG 0x8
446 #define PIPE_OP_TEX 0x10
447 #define PIPE_OP_RAST_SETUP_0 0x100
448 #define PIPE_OP_RAST_SETUP_1 0x200
449 #define PIPE_OP_RENDER 0x400
450 #define PIPE_OP_CVA_PREPARE 0x800
451 #define PIPE_OP_POINT_SIZE 0x1000
454 struct gl_pipeline_stage
{
456 GLuint ops
; /* PIPE_OP flags */
457 GLuint type
; /* VERT flags */
458 GLuint special
; /* VERT flags - force update_inputs() */
459 GLuint state_change
; /* state flags - trigger update_inputs() */
460 GLuint cva_state_change
; /* state flags - recalc cva buffer */
461 GLuint elt_forbidden_inputs
; /* VERT flags - force a pipeline recalc */
462 GLuint pre_forbidden_inputs
; /* VERT flags - force a pipeline recalc */
463 GLuint active
; /* VERT flags */
464 GLuint inputs
; /* VERT flags */
465 GLuint outputs
; /* VERT flags */
466 void (*check
)( GLcontext
*ctx
, struct gl_pipeline_stage
* );
467 void (*run
)( struct vertex_buffer
*VB
);
472 GLuint state_change
; /* state changes which require recalc */
473 GLuint cva_state_change
; /* ... which require re-run */
474 GLuint forbidden_inputs
; /* inputs which require recalc */
475 GLuint ops
; /* what gets done in this pipe */
483 GLuint pipeline_valid
:1;
485 GLuint copy_transformed_data
:1;
486 GLuint replay_copied_vertices
:1;
487 GLuint new_state
; /* state changes since last recalc */
488 struct gl_pipeline_stage
*stages
[MAX_PIPELINE_STAGES
];
493 /* All fields are derived.
496 struct gl_pipeline pre
;
497 struct gl_pipeline elt
;
499 struct gl_client_array Elt
;
500 trans_1ui_func EltFunc
;
502 struct vertex_buffer
*VB
;
503 struct vertex_arrays v
;
504 struct vertex_data store
;
510 GLuint forbidden_inputs
;
517 GLuint last_array_flags
;
518 GLuint last_array_new_state
;
521 /* These are used to make the ctx->Current values look like
522 * arrays (with zero StrideB).
524 struct gl_fallback_arrays
{
525 struct gl_client_array Normal
;
526 struct gl_client_array Color
;
527 struct gl_client_array SecondaryColor
;
528 struct gl_client_array FogCoord
;
529 struct gl_client_array Index
;
530 struct gl_client_array TexCoord
[MAX_TEXTURE_UNITS
];
531 struct gl_client_array EdgeFlag
;
536 typedef void (*texgen_func
)( struct vertex_buffer
*VB
,
541 typedef struct tnl_context
{
543 GLuint _ArrayFlag
[VB_SIZE
]; /* crock */
545 GLuint _ArraySummary
; /* Like flags, but no size information */
546 GLuint _ArrayNewState
; /* Tracks which arrays have been changed. */
547 GLuint _ArrayTex3Flag
;
548 GLuint _ArrayTex4Flag
;
551 /* Pipeline stages - shared between the two pipelines,
554 struct gl_pipeline_stage PipelineStage
[MAX_PIPELINE_STAGES
];
555 GLuint NrPipelineStages
;
557 /* Per-texunit derived state.
559 GLuint _TexgenSize
[MAX_TEXTURE_UNITS
];
560 GLuint _TexgenHoles
[MAX_TEXTURE_UNITS
];
561 texgen_func
*_TexgenFunc
[MAX_TEXTURE_UNITS
];
564 /* Display list extensions
566 GLuint opcode_vertex_cassette
;
571 GLboolean CompileCVAFlag
;
573 clip_poly_func
*_poly_clip_tab
;
574 clip_line_func
*_line_clip_tab
;
575 clip_interp_func _ClipInterpFunc
; /* Clip interpolation function */
576 normal_func
*_NormalTransform
;
577 shade_func
*_shade_func_tab
; /* Current shading function table */
579 GLenum _CurrentPrimitive
; /* Prim or GL_POLYGON+1 */
580 GLuint _CurrentTex3Flag
;
581 GLuint _CurrentTex4Flag
;
583 GLboolean _ReplayHardBeginEnd
; /* Display list execution of rect, etc */
585 GLuint _RenderFlags
; /* Active inputs to render stage */
587 /* Cache of unused immediate structs */
588 struct immediate
*freed_im_queue
;
591 struct gl_fallback_arrays Fallback
;
593 GLvertexformat vtxfmt
;
599 #define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context))
600 #define TNL_CURRENT_IM(ctx) ((struct immediate *)(ctx->swtnl_im))
601 #define TNL_VB(ctx) ((struct vertex_buffer *)(ctx->swtnl_vb))
603 extern GLboolean
_tnl_flush_vertices( GLcontext
*ctx
, GLuint flush_flags
);
607 _tnl_MakeCurrent( GLcontext
*ctx
,
608 GLframebuffer
*drawBuffer
,
609 GLframebuffer
*readBuffer
);
613 _tnl_LightingSpaceChange( GLcontext
*ctx
);
616 * Macros for fetching current input buffer.
619 #define GET_IMMEDIATE struct immediate *IM = TNL_CURRENT_IM(((GLcontext *) (_glapi_Context ? _glapi_Context : _glapi_get_context())))
620 #define SET_IMMEDIATE(ctx, im) ctx->swtnl_im = (void *)im
622 extern struct immediate
*_tnl_CurrentInput
;
623 #define GET_IMMEDIATE struct immediate *IM = _tnl_CurrentInput
624 #define SET_IMMEDIATE(ctx, im) \
626 ctx->swtnl_im = (void *)im; \
627 _tnl_CurrentInput = im; \