1 /* $Id: t_context.h,v 1.22 2001/04/30 21:08:52 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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>
36 #include "math/m_matrix.h"
37 #include "math/m_vector.h"
38 #include "math/m_xform.h"
41 #define MAX_PIPELINE_STAGES 30
44 /* Numbers for sizing immediate structs.
46 #define IMM_MAX_COPIED_VERTS 3
47 #define IMM_MAXDATA (216 + IMM_MAX_COPIED_VERTS)
48 #define IMM_SIZE (IMM_MAXDATA + MAX_CLIPPED_VERTICES)
51 /* Values for IM->BeginState
53 #define VERT_BEGIN_0 0x1 /* glBegin (if initially inside beg/end) */
54 #define VERT_BEGIN_1 0x2 /* glBegin (if initially outside beg/end) */
55 #define VERT_ERROR_0 0x4 /* invalid_operation in initial state 0 */
56 #define VERT_ERROR_1 0x8 /* invalid_operation in initial state 1 */
59 /* Flags to be added to the primitive enum in VB->Primitive.
61 #define PRIM_MODE_MASK 0xff /* Extract the actual primitive */
62 #define PRIM_BEGIN 0x100 /* The prim starts here (not wrapped) */
63 #define PRIM_END 0x200 /* The prim ends in this VB (does not wrap) */
64 #define PRIM_PARITY 0x400 /* The prim wrapped on an odd number of verts */
65 #define PRIM_LAST 0x800 /* No more prims in the VB */
68 /* Flags that describe the inputs and outputs of pipeline stages, and
69 * the contents of a vertex-cassette.
71 * 5 spare flags, rearrangement of eval flags can secure at least 3
74 #define VERT_OBJ _NEW_ARRAY_VERTEX
75 #define VERT_RGBA _NEW_ARRAY_COLOR
76 #define VERT_NORM _NEW_ARRAY_NORMAL
77 #define VERT_INDEX _NEW_ARRAY_INDEX
78 #define VERT_EDGE _NEW_ARRAY_EDGEFLAG
79 #define VERT_SPEC_RGB _NEW_ARRAY_SECONDARYCOLOR
80 #define VERT_FOG_COORD _NEW_ARRAY_FOGCOORD
81 #define VERT_TEX0 _NEW_ARRAY_TEXCOORD_0
82 #define VERT_TEX1 _NEW_ARRAY_TEXCOORD_1
83 #define VERT_TEX2 _NEW_ARRAY_TEXCOORD_2
84 #define VERT_TEX3 _NEW_ARRAY_TEXCOORD_3
85 #define VERT_TEX4 _NEW_ARRAY_TEXCOORD_4
86 #define VERT_TEX5 _NEW_ARRAY_TEXCOORD_5
87 #define VERT_TEX6 _NEW_ARRAY_TEXCOORD_6
88 #define VERT_TEX7 _NEW_ARRAY_TEXCOORD_7
89 #define VERT_EVAL_C1 0x8000 /* imm only */
90 #define VERT_EVAL_C2 0x10000 /* imm only */
91 #define VERT_EVAL_P1 0x20000 /* imm only */
92 #define VERT_EVAL_P2 0x40000 /* imm only */
93 #define VERT_OBJ_3 0x80000 /* imm only */
94 #define VERT_OBJ_4 0x100000 /* imm only */
95 #define VERT_MATERIAL 0x200000 /* imm only, but tested in vb code */
96 #define VERT_ELT 0x400000 /* imm only */
97 #define VERT_BEGIN 0x800000 /* imm only, but tested in vb code */
98 #define VERT_END 0x1000000 /* imm only, but tested in vb code */
99 #define VERT_END_VB 0x2000000 /* imm only, but tested in vb code */
100 #define VERT_POINT_SIZE 0x4000000 /* vb only, could reuse a bit */
101 #define VERT_EYE VERT_BEGIN /* vb only, reuse imm bit */
102 #define VERT_CLIP VERT_END /* vb only, reuse imm bit*/
105 /* Flags for IM->TexCoordSize. Enough flags for 16 units.
107 #define TEX_0_SIZE_3 0x1
108 #define TEX_0_SIZE_4 0x1001
109 #define TEX_SIZE_3(unit) (TEX_0_SIZE_3<<unit)
110 #define TEX_SIZE_4(unit) (TEX_0_SIZE_4<<unit)
115 #define VERT_EVAL_ANY (VERT_EVAL_C1|VERT_EVAL_P1| \
116 VERT_EVAL_C2|VERT_EVAL_P2)
118 #define VERT_OBJ_23 (VERT_OBJ_3|VERT_OBJ)
119 #define VERT_OBJ_234 (VERT_OBJ_4|VERT_OBJ_23)
121 #define VERT_TEX0_SHIFT 11
123 #define VERT_TEX(i) (VERT_TEX0 << i)
125 #define VERT_TEX_ANY (VERT_TEX0 | \
134 #define VERT_FIXUP (VERT_TEX_ANY | \
142 #define VERT_CURRENT_DATA (VERT_FIXUP | \
145 #define VERT_DATA (VERT_TEX_ANY | \
158 /* KW: Represents everything that can take place between a begin and
159 * end, and can represent multiple begin/end pairs. Can be used to
160 * losslessly encode this information in display lists.
164 struct __GLcontextRec
*backref
;
165 GLuint id
, ref_count
;
167 /* This must be saved when immediates are shared in display lists.
169 GLuint CopyStart
, Start
, Count
;
170 GLuint LastData
; /* count or count+1 */
171 GLuint AndFlag
, OrFlag
;
172 GLuint TexSize
; /* keep track of texcoord sizes */
173 GLuint BeginState
, SavedBeginState
;
174 GLuint LastPrimitive
;
176 GLuint ArrayEltFlags
; /* precalc'ed for glArrayElt */
178 GLuint ArrayEltFlush
;
181 GLuint MaxTextureUnits
; /* precalc'ed for glMultiTexCoordARB */
183 /* Temporary values created when vertices are copied into the
184 * first 3 slots of the struct:
191 /* allocate storage for these on demand:
193 struct gl_material (*Material
)[2];
194 GLuint
*MaterialMask
;
196 GLuint MaterialOrMask
;
197 GLuint MaterialAndMask
;
199 GLfloat (*TexCoord
[MAX_TEXTURE_UNITS
])[4];
201 GLuint Primitive
[IMM_SIZE
]; /* BEGIN/END */
202 GLuint PrimitiveLength
[IMM_SIZE
]; /* BEGIN/END */
203 GLuint Flag
[IMM_SIZE
]; /* VERT_* flags */
204 GLfloat Color
[IMM_SIZE
][4];
205 GLfloat Obj
[IMM_SIZE
][4];
206 GLfloat Normal
[IMM_SIZE
][3];
207 GLfloat TexCoord0
[IMM_SIZE
][4]; /* just VERT_TEX0 */
208 GLuint Elt
[IMM_SIZE
];
209 GLubyte EdgeFlag
[IMM_SIZE
];
210 GLuint Index
[IMM_SIZE
];
211 GLfloat SecondaryColor
[IMM_SIZE
][4];
212 GLfloat FogCoord
[IMM_SIZE
];
220 struct gl_client_array Color
;
221 struct gl_client_array SecondaryColor
;
223 GLvector1ub EdgeFlag
;
224 GLvector4f TexCoord
[MAX_TEXTURE_UNITS
];
230 typedef struct gl_material GLmaterial
;
232 /* Contains the current state of a running pipeline.
234 typedef struct vertex_buffer
236 /* Constant over life of the vertex_buffer.
240 /* Constant over the pipeline.
242 GLuint Count
; /* for everything except Elts */
243 GLuint FirstClipped
; /* temp verts for clipping */
244 GLuint FirstPrimitive
; /* usually zero */
246 /* Pointers to current data.
248 GLuint
*Elts
; /* VERT_ELT */
249 GLvector4f
*ObjPtr
; /* VERT_OBJ */
250 GLvector4f
*EyePtr
; /* VERT_EYE */
251 GLvector4f
*ClipPtr
; /* VERT_CLIP */
252 GLvector4f
*ProjectedClipPtr
; /* VERT_CLIP (2) */
253 GLubyte ClipOrMask
; /* VERT_CLIP (3) */
254 GLubyte
*ClipMask
; /* VERT_CLIP (4) */
255 GLvector3f
*NormalPtr
; /* VERT_NORM */
256 GLboolean
*EdgeFlag
; /* VERT_EDGE */
257 GLvector4f
*TexCoordPtr
[MAX_TEXTURE_UNITS
]; /* VERT_TEX_0..n */
258 GLvector1ui
*IndexPtr
[2]; /* VERT_INDEX */
259 struct gl_client_array
*ColorPtr
[2]; /* VERT_RGBA */
260 struct gl_client_array
*SecondaryColorPtr
[2]; /* VERT_SPEC_RGB */
261 GLvector1f
*FogCoordPtr
; /* VERT_FOG_COORD */
262 GLvector1f
*PointSizePtr
; /* VERT_POINT_SIZE */
263 GLmaterial (*Material
)[2]; /* VERT_MATERIAL, optional */
264 GLuint
*MaterialMask
; /* VERT_MATERIAL, optional */
265 GLuint
*Flag
; /* VERT_* flags, optional */
266 GLuint
*Primitive
; /* GL_(mode)|PRIM_* flags */
267 GLuint
*PrimitiveLength
; /* integers */
270 GLuint importable_data
;
271 void (*import_data
)( GLcontext
*ctx
, GLuint flags
, GLuint vecflags
);
272 /* Callback to the provider of the untransformed input for the
273 * render stage (or other stages) to call if they need to write into
274 * write-protected arrays, or fixup the stride on input arrays.
276 * This is currently only necessary for client arrays that make it
277 * as far down the pipeline as the render stage.
281 /* Private data from _tnl_render_stage that has no business being
289 /* Describes an individual operation on the pipeline.
291 struct gl_pipeline_stage
{
293 GLuint check_state
; /* All state referenced in check() --
294 * When is the pipeline_stage struct
295 * itself invalidated? Must be
299 /* Usually constant or set by the 'check' callback:
301 GLuint run_state
; /* All state referenced in run() --
302 * When is the cached output of the
306 GLboolean active
; /* True if runnable in current state */
307 GLuint inputs
; /* VERT_* inputs to the stage */
308 GLuint outputs
; /* VERT_* outputs of the stage */
310 /* Set in _tnl_run_pipeline():
312 GLuint changed_inputs
; /* Generated value -- inputs to the
313 * stage that have changed since last
317 /* Private data for the pipeline stage:
321 /* Free private data. May not be null.
323 void (*destroy
)( struct gl_pipeline_stage
* );
325 /* Called from _tnl_validate_pipeline(). Must update all fields in
326 * the pipeline_stage struct for the current state.
328 void (*check
)( GLcontext
*ctx
, struct gl_pipeline_stage
* );
330 /* Called from _tnl_run_pipeline(). The stage.changed_inputs value
331 * encodes all inputs to thee struct which have changed. If
332 * non-zero, recompute all affected outputs of the stage, otherwise
333 * execute any 'sideeffects' of the stage.
335 * Return value: GL_TRUE - keep going
336 * GL_FALSE - finished pipeline
338 GLboolean (*run
)( GLcontext
*ctx
, struct gl_pipeline_stage
* );
343 GLuint build_state_trigger
; /* state changes which require build */
344 GLuint build_state_changes
; /* state changes since last build */
345 GLuint run_state_changes
; /* state changes since last run */
346 GLuint run_input_changes
; /* VERT_* changes since last run */
347 GLuint inputs
; /* VERT_* inputs to pipeline */
348 struct gl_pipeline_stage stages
[MAX_PIPELINE_STAGES
];
353 struct tnl_eval_store
{
354 GLuint EvalMap1Flags
;
355 GLuint EvalMap2Flags
;
357 struct immediate
*im
; /* used for temporary data */
361 typedef void (*points_func
)( GLcontext
*ctx
, GLuint first
, GLuint last
);
362 typedef void (*line_func
)( GLcontext
*ctx
, GLuint v1
, GLuint v2
);
363 typedef void (*triangle_func
)( GLcontext
*ctx
,
364 GLuint v1
, GLuint v2
, GLuint v3
);
365 typedef void (*quad_func
)( GLcontext
*ctx
, GLuint v1
, GLuint v2
,
366 GLuint v3
, GLuint v4
);
367 typedef void (*render_func
)( GLcontext
*ctx
, GLuint start
, GLuint count
,
369 typedef void (*interp_func
)( GLcontext
*ctx
,
370 GLfloat t
, GLuint dst
, GLuint in
, GLuint out
,
371 GLboolean force_boundary
);
372 typedef void (*copy_pv_func
)( GLcontext
*ctx
, GLuint dst
, GLuint src
);
375 struct tnl_device_driver
{
380 void (*PipelineStart
)(GLcontext
*ctx
);
381 void (*PipelineFinish
)(GLcontext
*ctx
);
382 /* Called before and after all pipeline stages.
383 * These are a suitable place for grabbing/releasing hardware locks.
390 void (*RenderStart
)(GLcontext
*ctx
);
391 void (*RenderFinish
)(GLcontext
*ctx
);
392 /* Called before and after all rendering operations, including DrawPixels,
393 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
394 * These are a suitable place for grabbing/releasing hardware locks.
397 void (*RenderPrimitive
)(GLcontext
*ctx
, GLenum mode
);
398 /* Called between RednerStart() and RenderFinish() to indicate the
399 * type of primitive we're about to draw. Mode will be one of the
400 * modes accepted by glBegin().
403 interp_func RenderInterp
;
404 copy_pv_func RenderCopyPV
;
405 void (*RenderClippedPolygon
)( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
);
406 void (*RenderClippedLine
)( GLcontext
*ctx
, GLuint v0
, GLuint v1
);
407 /* Functions to interpolate between prebuilt vertices, copy flat-shade
408 * provoking color, and to render clipped primitives.
411 points_func PointsFunc
; /* must now respect vb->elts */
413 triangle_func TriangleFunc
;
415 /* These functions are called in order to render points, lines,
416 * triangles and quads. These are only called via the T&L module.
419 render_func
*RenderTabVerts
;
420 render_func
*RenderTabElts
;
421 /* Render whole unclipped primitives (points, lines, linestrips,
422 * lineloops, etc). The tables are indexed by the GL enum of the
423 * primitive to be rendered.
426 void (*ResetLineStipple
)( GLcontext
*ctx
);
427 /* Reset the hardware's line stipple counter.
430 void (*BuildProjectedVertices
)( GLcontext
*ctx
,
431 GLuint start
, GLuint end
,
433 /* This function is called whenever new vertices are required for
434 * rendering. The vertices in question are those n such that start
435 * <= n < end. The new_inputs parameter indicates those fields of
436 * the vertex which need to be updated, if only a partial repair of
437 * the vertex is required.
439 * This function is called only from _tnl_render_stage in tnl/t_render.c.
443 GLboolean (*MultipassFunc
)( GLcontext
*ctx
, GLuint passno
);
444 /* Driver may request additional render passes by returning GL_TRUE
445 * when this function is called. This function will be called
446 * after the first pass, and passes will be made until the function
447 * returns GL_FALSE. If no function is registered, only one pass
450 * This function will be first invoked with passno == 1.
459 struct tnl_device_driver Driver
;
461 /* Track whether the module is active.
463 GLboolean bound_exec
;
465 /* Display list extensions
467 GLuint opcode_vertex_cassette
;
471 struct gl_pipeline pipeline
;
472 struct vertex_buffer vb
;
474 /* GLvectors for binding to vb:
476 struct vertex_arrays imm_inputs
;
477 struct vertex_arrays array_inputs
;
478 GLuint
*tmp_primitive
;
479 GLuint
*tmp_primitive_length
;
481 /* Set when executing an internally generated begin/end object. If
482 * such an object is encountered in a display list, it will be
483 * replayed only if the list is outside any existing begin/end
486 GLboolean ReplayHardBeginEnd
;
488 /* Note which vertices need copying over succesive immediates.
489 * Will add save versions to precompute vertex copying where
492 struct immediate
*ExecCopySource
;
493 GLuint ExecCopyCount
;
494 GLuint ExecCopyElts
[IMM_MAX_COPIED_VERTS
];
495 GLuint ExecCopyTexSize
;
498 GLuint DlistPrimitive
;
499 GLuint DlistPrimitiveLength
;
500 GLuint DlistLastPrimitive
;
502 /* Cache a single free immediate (refcount == 0)
504 struct immediate
*freed_immediate
;
506 /* Probably need a better configuration mechanism:
508 GLboolean NeedProjCoords
;
510 /* Derived state and storage for _tnl_eval_vb:
512 struct tnl_eval_store eval
;
514 /* Functions to be plugged into dispatch when tnl is active.
516 GLvertexformat vtxfmt
;
522 #define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context))
523 #define TNL_CURRENT_IM(ctx) ((struct immediate *)(ctx->swtnl_im))
526 #define TYPE_IDX(t) ((t) & 0xf)
527 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
529 extern void _tnl_MakeCurrent( GLcontext
*ctx
,
530 GLframebuffer
*drawBuffer
,
531 GLframebuffer
*readBuffer
);
535 * Macros for fetching current input buffer.
538 #define GET_IMMEDIATE struct immediate *IM = TNL_CURRENT_IM(((GLcontext *) (_glapi_Context ? _glapi_Context : _glapi_get_context())))
539 #define SET_IMMEDIATE(ctx, im) ctx->swtnl_im = (void *)im
541 extern struct immediate
*_tnl_CurrentInput
;
542 #define GET_IMMEDIATE struct immediate *IM = _tnl_CurrentInput
543 #define SET_IMMEDIATE(ctx, im) \
545 ctx->swtnl_im = (void *)im; \
546 _tnl_CurrentInput = im; \