1 /* $Id: t_context.h,v 1.41 2002/04/19 12:32:14 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.
29 * \brief TnL module datatypes and definitions.
30 * \author Keith Whitwell
39 #include "math/m_matrix.h"
40 #include "math/m_vector.h"
41 #include "math/m_xform.h"
44 #define MAX_PIPELINE_STAGES 30
47 /* Numbers for sizing immediate structs.
49 #define IMM_MAX_COPIED_VERTS 3
50 #define IMM_MAXDATA (216 + IMM_MAX_COPIED_VERTS)
51 #define IMM_SIZE (IMM_MAXDATA + MAX_CLIPPED_VERTICES)
54 /* Values for IM->BeginState
56 #define VERT_BEGIN_0 0x1 /* glBegin (if initially inside beg/end) */
57 #define VERT_BEGIN_1 0x2 /* glBegin (if initially outside beg/end) */
58 #define VERT_ERROR_0 0x4 /* invalid_operation in initial state 0 */
59 #define VERT_ERROR_1 0x8 /* invalid_operation in initial state 1 */
62 /* Flags to be added to the primitive enum in VB->Primitive.
64 #define PRIM_MODE_MASK 0xff /* Extract the actual primitive */
65 #define PRIM_BEGIN 0x100 /* The prim starts here (not wrapped) */
66 #define PRIM_END 0x200 /* The prim ends in this VB (does not wrap) */
67 #define PRIM_PARITY 0x400 /* The prim wrapped on an odd number of verts */
68 #define PRIM_LAST 0x800 /* No more prims in the VB */
72 * Flags that describe the inputs and outputs of pipeline stages, and
73 * the contents of a vertex-cassette. We reuse the VERT_BIT_* flags
74 * defined in mtypes.h and add a bunch of new ones.
76 /* bits 0..5 defined in mtypes.h */
77 #define VERT_BIT_INDEX VERT_BIT_SIX /* a free vertex attrib bit */
78 #define VERT_BIT_EDGEFLAG VERT_BIT_SEVEN /* a free vertex attrib bit */
79 /* bits 8..15 defined in mtypes.h */
80 #define VERT_BIT_EVAL_C1 (1 << 16) /* imm only */
81 #define VERT_BIT_EVAL_C2 (1 << 17) /* imm only */
82 #define VERT_BIT_EVAL_P1 (1 << 18) /* imm only */
83 #define VERT_BIT_EVAL_P2 (1 << 19) /* imm only */
84 #define VERT_BIT_OBJ_3 (1 << 20) /* imm only */
85 #define VERT_BIT_OBJ_4 (1 << 21) /* imm only */
86 #define VERT_BIT_MATERIAL (1 << 22) /* imm only, but tested in vb code */
87 #define VERT_BIT_ELT (1 << 23) /* imm only */
88 #define VERT_BIT_BEGIN (1 << 24) /* imm only, but tested in vb code */
89 #define VERT_BIT_END (1 << 25) /* imm only, but tested in vb code */
90 #define VERT_BIT_END_VB (1 << 26) /* imm only, but tested in vb code */
91 #define VERT_BIT_POINT_SIZE (1 << 27) /* vb only, could reuse a bit */
92 #define VERT_BIT_EYE VERT_BIT_BEGIN /* vb only, reuse imm bit */
93 #define VERT_BIT_CLIP VERT_BIT_END /* vb only, reuse imm bit*/
97 * XXXX these are temporary - for backward compatibility w/ DRI drivers
99 #define VERT_OBJ VERT_BIT_POS
100 #define VERT_RGBA VERT_BIT_COLOR0
101 #define VERT_NORM VERT_BIT_NORMAL
102 #define VERT_INDEX VERT_BIT_INDEX
103 #define VERT_EDGE VERT_BIT_SEVEN
104 #define VERT_SPEC_RGB VERT_BIT_COLOR1
105 #define VERT_FOG_COORD VERT_BIT_FOG
106 #define VERT_TEX0 VERT_BIT_TEX0
107 #define VERT_TEX1 VERT_BIT_TEX1
108 #define VERT_TEX2 VERT_BIT_TEX2
109 #define VERT_TEX3 VERT_BIT_TEX3
110 #define VERT_TEX4 VERT_BIT_TEX4
111 #define VERT_TEX5 VERT_BIT_TEX5
112 #define VERT_TEX6 VERT_BIT_TEX6
113 #define VERT_TEX7 VERT_BIT_TEX7
114 #define VERT_EVAL_C1 VERT_BIT_EVAL_C1
115 #define VERT_EVAL_C2 VERT_BIT_EVAL_C2
116 #define VERT_EVAL_P1 VERT_BIT_EVAL_P1
117 #define VERT_EVAL_P2 VERT_BIT_EVAL_P2
118 #define VERT_OBJ_3 VERT_BIT_OBJ_3
119 #define VERT_OBJ_4 VERT_BIT_OBJ_4
120 #define VERT_MATERIAL VERT_BIT_MATERIAL
121 #define VERT_ELT VERT_BIT_ELT
122 #define VERT_BEGIN VERT_BIT_BEGIN
123 #define VERT_END VERT_BIT_END
124 #define VERT_END_VB VERT_BIT_END_VB
125 #define VERT_POINT_SIZE VERT_BIT_POINT_SIZE
126 #define VERT_EYE VERT_BIT_EYE
127 #define VERT_CLIP VERT_BIT_CLIP
130 /* Flags for IM->TexCoordSize. Enough flags for 16 units.
132 #define TEX_0_SIZE_3 (unsigned)0x1
133 #define TEX_0_SIZE_4 (unsigned)0x10001
134 #define TEX_SIZE_3(unit) (TEX_0_SIZE_3 << (unit))
135 #define TEX_SIZE_4(unit) (TEX_0_SIZE_4 << (unit))
140 #define VERT_BITS_OBJ_23 (VERT_BIT_POS | VERT_BIT_OBJ_3)
141 #define VERT_BITS_OBJ_234 (VERT_BIT_POS | VERT_BIT_OBJ_3 | VERT_BIT_OBJ_4)
143 #define VERT_BITS_TEX_ANY (VERT_BIT_TEX0 | \
152 #define VERT_BITS_EVAL_ANY (VERT_BIT_EVAL_C1 | VERT_BIT_EVAL_P1 | \
153 VERT_BIT_EVAL_C2 | VERT_BIT_EVAL_P2)
155 #define VERT_BITS_FIXUP (VERT_BITS_TEX_ANY | \
160 VERT_BIT_EDGEFLAG | \
163 #define VERT_BITS_CURRENT_DATA (VERT_BITS_FIXUP | \
166 #define VERT_BITS_DATA (VERT_BITS_TEX_ANY | \
171 VERT_BIT_EDGEFLAG | \
174 VERT_BIT_MATERIAL | \
180 * KW: Represents everything that can take place between a begin and
181 * end, and can represent multiple begin/end pairs. Can be used to
182 * losslessly encode this information in display lists.
186 GLuint id
, ref_count
;
188 /* This must be saved when immediates are shared in display lists.
190 GLuint CopyStart
, Start
, Count
;
191 GLuint LastData
; /* count or count+1 */
192 GLuint AndFlag
, OrFlag
;
193 GLuint TexSize
; /* keep track of texcoord sizes */
194 GLuint BeginState
, SavedBeginState
;
195 GLuint LastPrimitive
;
197 GLuint ArrayEltFlags
; /* precalc'ed for glArrayElt */
199 GLuint ArrayEltFlush
;
201 #define FLUSH_ELT_EAGER 0x1
202 #define FLUSH_ELT_LAZY 0x2
205 GLuint MaxTextureUnits
; /* precalc'ed for glMultiTexCoordARB */
207 /* Temporary values created when vertices are copied into the
208 * first 3 slots of the struct:
216 /* allocate storage for these on demand:
218 struct gl_material (*Material
)[2];
219 GLuint
*MaterialMask
;
221 GLuint MaterialOrMask
;
222 GLuint MaterialAndMask
;
224 GLuint Primitive
[IMM_SIZE
]; /* BEGIN/END */
225 GLuint PrimitiveLength
[IMM_SIZE
]; /* BEGIN/END */
226 GLuint Flag
[IMM_SIZE
]; /* VERT_BIT_* flags */
228 /* All vertex attributes (position, normal, color, secondary color,
229 * texcoords, fog coord) are stored in the Attrib[] arrays instead
230 * of individual arrays as we did prior to Mesa 4.1.
232 * XXX may need to use 32-byte aligned allocation for this!!!
234 GLfloat Attrib
[VERT_ATTRIB_MAX
][IMM_SIZE
][4]; /* GL_NV_vertex_program */
236 GLfloat
*NormalLengthPtr
; /* length of normal vectors (display list only) */
238 GLuint Elt
[IMM_SIZE
];
239 GLubyte EdgeFlag
[IMM_SIZE
];
240 GLuint Index
[IMM_SIZE
];
248 struct gl_client_array Color
;
249 struct gl_client_array SecondaryColor
;
251 GLvector1ub EdgeFlag
;
252 GLvector4f TexCoord
[MAX_TEXTURE_UNITS
];
255 GLvector4f Attribs
[VERT_ATTRIB_MAX
];
260 * Contains the current state of a running pipeline.
262 typedef struct vertex_buffer
264 /* Constant over life of the vertex_buffer.
268 /* Constant over the pipeline.
270 GLuint Count
; /* for everything except Elts */
271 GLuint FirstClipped
; /* temp verts for clipping */
272 GLuint FirstPrimitive
; /* usually zero */
274 /* Pointers to current data.
276 GLuint
*Elts
; /* VERT_BIT_ELT */
277 GLvector4f
*ObjPtr
; /* VERT_BIT_POS */
278 GLvector4f
*EyePtr
; /* VERT_BIT_EYE */
279 GLvector4f
*ClipPtr
; /* VERT_BIT_CLIP */
280 GLvector4f
*NdcPtr
; /* VERT_BIT_CLIP (2) */
281 GLubyte ClipOrMask
; /* VERT_BIT_CLIP (3) */
282 GLubyte
*ClipMask
; /* VERT_BIT_CLIP (4) */
283 GLvector4f
*NormalPtr
; /* VERT_BIT_NORMAL */
284 GLfloat
*NormalLengthPtr
; /* VERT_BIT_NORMAL */
285 GLboolean
*EdgeFlag
; /* VERT_BIT_EDGEFLAG */
286 GLvector4f
*TexCoordPtr
[MAX_TEXTURE_UNITS
]; /* VERT_TEX_0..n */
287 GLvector1ui
*IndexPtr
[2]; /* VERT_BIT_INDEX */
288 struct gl_client_array
*ColorPtr
[2]; /* VERT_BIT_COLOR0 */
289 struct gl_client_array
*SecondaryColorPtr
[2];/* VERT_BIT_COLOR1 */
290 GLvector4f
*PointSizePtr
; /* VERT_BIT_POINT_SIZE */
291 GLvector4f
*FogCoordPtr
; /* VERT_BIT_FOG */
292 struct gl_material (*Material
)[2]; /* VERT_BIT_MATERIAL, optional */
293 GLuint
*MaterialMask
; /* VERT_BIT_MATERIAL, optional */
294 GLuint
*Flag
; /* VERT_BIT_* flags, optional */
295 GLuint
*Primitive
; /* GL_(mode)|PRIM_* flags */
296 GLuint
*PrimitiveLength
; /* integers */
298 /* Inputs to the vertex program stage */
299 GLvector4f
*AttribPtr
[VERT_ATTRIB_MAX
]; /* GL_NV_vertex_program */
301 GLuint importable_data
;
303 void (*import_data
)( GLcontext
*ctx
, GLuint flags
, GLuint vecflags
);
304 /* Callback to the provider of the untransformed input for the
305 * render stage (or other stages) to call if they need to write into
306 * write-protected arrays, or fixup the stride on input arrays.
308 * This is currently only necessary for client arrays that make it
309 * as far down the pipeline as the render stage.
313 /* Private data from _tnl_render_stage that has no business being
321 /* Describes an individual operation on the pipeline.
323 struct gl_pipeline_stage
{
325 GLuint check_state
; /* All state referenced in check() --
326 * When is the pipeline_stage struct
327 * itself invalidated? Must be
331 /* Usually constant or set by the 'check' callback:
333 GLuint run_state
; /* All state referenced in run() --
334 * When is the cached output of the
338 GLboolean active
; /* True if runnable in current state */
339 GLuint inputs
; /* VERT_* inputs to the stage */
340 GLuint outputs
; /* VERT_* outputs of the stage */
342 /* Set in _tnl_run_pipeline():
344 GLuint changed_inputs
; /* Generated value -- inputs to the
345 * stage that have changed since last
349 /* Private data for the pipeline stage:
353 /* Free private data. May not be null.
355 void (*destroy
)( struct gl_pipeline_stage
* );
357 /* Called from _tnl_validate_pipeline(). Must update all fields in
358 * the pipeline_stage struct for the current state.
360 void (*check
)( GLcontext
*ctx
, struct gl_pipeline_stage
* );
362 /* Called from _tnl_run_pipeline(). The stage.changed_inputs value
363 * encodes all inputs to thee struct which have changed. If
364 * non-zero, recompute all affected outputs of the stage, otherwise
365 * execute any 'sideeffects' of the stage.
367 * Return value: GL_TRUE - keep going
368 * GL_FALSE - finished pipeline
370 GLboolean (*run
)( GLcontext
*ctx
, struct gl_pipeline_stage
* );
375 GLuint build_state_trigger
; /* state changes which require build */
376 GLuint build_state_changes
; /* state changes since last build */
377 GLuint run_state_changes
; /* state changes since last run */
378 GLuint run_input_changes
; /* VERT_* changes since last run */
379 GLuint inputs
; /* VERT_* inputs to pipeline */
380 struct gl_pipeline_stage stages
[MAX_PIPELINE_STAGES
+1];
385 struct tnl_eval_store
{
386 GLuint EvalMap1Flags
;
387 GLuint EvalMap2Flags
;
389 struct immediate
*im
; /* used for temporary data */
393 typedef void (*points_func
)( GLcontext
*ctx
, GLuint first
, GLuint last
);
394 typedef void (*line_func
)( GLcontext
*ctx
, GLuint v1
, GLuint v2
);
395 typedef void (*triangle_func
)( GLcontext
*ctx
,
396 GLuint v1
, GLuint v2
, GLuint v3
);
397 typedef void (*quad_func
)( GLcontext
*ctx
, GLuint v1
, GLuint v2
,
398 GLuint v3
, GLuint v4
);
399 typedef void (*render_func
)( GLcontext
*ctx
, GLuint start
, GLuint count
,
401 typedef void (*interp_func
)( GLcontext
*ctx
,
402 GLfloat t
, GLuint dst
, GLuint out
, GLuint in
,
403 GLboolean force_boundary
);
404 typedef void (*copy_pv_func
)( GLcontext
*ctx
, GLuint dst
, GLuint src
);
405 typedef void (*setup_func
)( GLcontext
*ctx
,
406 GLuint start
, GLuint end
,
410 struct tnl_device_driver
{
415 void (*RunPipeline
)(GLcontext
*ctx
);
416 /* Replaces PipelineStart/PipelineFinish -- intended to allow
417 * drivers to wrap _tnl_run_pipeline() with code to validate state
418 * and grab/release hardware locks.
421 void (*NotifyMaterialChange
)(GLcontext
*ctx
);
422 /* Alert tnl-aware drivers of changes to material.
425 GLboolean (*NotifyBegin
)(GLcontext
*ctx
, GLenum p
);
426 /* Allow drivers to hook in optimized begin/end engines.
427 * Return value: GL_TRUE - driver handled the begin
428 * GL_FALSE - driver didn't handle the begin
432 *** Rendering -- These functions called only from t_vb_render.c
435 void (*Start
)(GLcontext
*ctx
);
436 void (*Finish
)(GLcontext
*ctx
);
437 /* Called before and after all rendering operations, including DrawPixels,
438 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
439 * These are a suitable place for grabbing/releasing hardware locks.
442 void (*PrimitiveNotify
)(GLcontext
*ctx
, GLenum mode
);
443 /* Called between RenderStart() and RenderFinish() to indicate the
444 * type of primitive we're about to draw. Mode will be one of the
445 * modes accepted by glBegin().
449 /* The interp function is called by the clipping routines when we need
450 * to generate an interpolated vertex. All pertinant vertex ancilliary
451 * data should be computed by interpolating between the 'in' and 'out'
456 /* The copy function is used to make a copy of a vertex. All pertinant
457 * vertex attributes should be copied.
460 void (*ClippedPolygon
)( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
);
461 /* Render a polygon with <n> vertices whose indexes are in the <elts>
465 void (*ClippedLine
)( GLcontext
*ctx
, GLuint v0
, GLuint v1
);
466 /* Render a line between the two vertices given by indexes v0 and v1. */
468 points_func Points
; /* must now respect vb->elts */
470 triangle_func Triangle
;
472 /* These functions are called in order to render points, lines,
473 * triangles and quads. These are only called via the T&L module.
476 render_func
*PrimTabVerts
;
477 render_func
*PrimTabElts
;
478 /* Render whole unclipped primitives (points, lines, linestrips,
479 * lineloops, etc). The tables are indexed by the GL enum of the
480 * primitive to be rendered. RenderTabVerts is used for non-indexed
481 * arrays of vertices. RenderTabElts is used for indexed arrays of
485 void (*ResetLineStipple
)( GLcontext
*ctx
);
486 /* Reset the hardware's line stipple counter.
489 setup_func BuildVertices
;
490 /* This function is called whenever new vertices are required for
491 * rendering. The vertices in question are those n such that start
492 * <= n < end. The new_inputs parameter indicates those fields of
493 * the vertex which need to be updated, if only a partial repair of
494 * the vertex is required.
496 * This function is called only from _tnl_render_stage in tnl/t_render.c.
500 GLboolean (*Multipass
)( GLcontext
*ctx
, GLuint passno
);
501 /* Driver may request additional render passes by returning GL_TRUE
502 * when this function is called. This function will be called
503 * after the first pass, and passes will be made until the function
504 * returns GL_FALSE. If no function is registered, only one pass
507 * This function will be first invoked with passno == 1.
517 struct tnl_device_driver Driver
;
519 /* Track whether the module is active.
521 GLboolean bound_exec
;
523 /* Display list extensions
525 GLuint opcode_vertex_cassette
;
529 struct gl_pipeline pipeline
;
530 struct vertex_buffer vb
;
532 /* GLvectors for binding to vb:
534 struct vertex_arrays imm_inputs
;
535 struct vertex_arrays array_inputs
;
536 GLuint
*tmp_primitive
;
537 GLuint
*tmp_primitive_length
;
539 /* Set when executing an internally generated begin/end object. If
540 * such an object is encountered in a display list, it will be
541 * replayed only if the list is outside any existing begin/end
544 GLboolean ReplayHardBeginEnd
;
546 /* Note which vertices need copying over succesive immediates.
547 * Will add save versions to precompute vertex copying where
550 struct immediate
*ExecCopySource
;
551 GLuint ExecCopyCount
;
552 GLuint ExecCopyElts
[IMM_MAX_COPIED_VERTS
];
553 GLuint ExecCopyTexSize
;
556 GLuint DlistPrimitive
;
557 GLuint DlistPrimitiveLength
;
558 GLuint DlistLastPrimitive
;
560 /* Cache a single free immediate (refcount == 0)
562 struct immediate
*freed_immediate
;
564 /* Probably need a better configuration mechanism:
566 GLboolean NeedNdcCoords
;
567 GLboolean LoopbackDListCassettes
;
568 GLboolean CalcDListNormalLengths
;
569 GLboolean IsolateMaterials
;
571 /* Derived state and storage for _tnl_eval_vb:
573 struct tnl_eval_store eval
;
575 /* Functions to be plugged into dispatch when tnl is active.
577 GLvertexformat vtxfmt
;
578 GLvertexformat save_vtxfmt
;
584 #define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context))
585 #define TNL_CURRENT_IM(ctx) ((struct immediate *)(ctx->swtnl_im))
588 #define TYPE_IDX(t) ((t) & 0xf)
589 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
591 extern void _tnl_MakeCurrent( GLcontext
*ctx
,
592 GLframebuffer
*drawBuffer
,
593 GLframebuffer
*readBuffer
);
597 * Macros for fetching current input buffer.
600 #define GET_IMMEDIATE struct immediate *IM = TNL_CURRENT_IM(((GLcontext *) (_glapi_Context ? _glapi_Context : _glapi_get_context())))
601 #define SET_IMMEDIATE(ctx, im) ctx->swtnl_im = (void *)im
603 extern struct immediate
*_tnl_CurrentInput
;
604 #define GET_IMMEDIATE struct immediate *IM = _tnl_CurrentInput
605 #define SET_IMMEDIATE(ctx, im) \
607 ctx->swtnl_im = (void *)im; \
608 _tnl_CurrentInput = im; \