t_context.h revision 0cb28418d06c30e431bdff515c1d36a812d5950d
1/* $Id: t_context.h,v 1.38 2002/02/13 00:53:20 keithw Exp $ */ 2 3/* 4 * Mesa 3-D graphics library 5 * Version: 4.1 6 * 7 * Copyright (C) 1999-2002 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 27/** 28 * \file t_context.h 29 * \brief TnL module datatypes and definitions. 30 * \author Keith Whitwell 31 */ 32 33#ifndef _T_CONTEXT_H 34#define _T_CONTEXT_H 35 36#include "glheader.h" 37#include "mtypes.h" 38 39#include "math/m_matrix.h" 40#include "math/m_vector.h" 41#include "math/m_xform.h" 42 43 44#define MAX_PIPELINE_STAGES 30 45 46 47/* Numbers for sizing immediate structs. 48 */ 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) 52 53 54/* Values for IM->BeginState 55 */ 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 */ 60 61 62/* Flags to be added to the primitive enum in VB->Primitive. 63 */ 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 */ 69 70 71/** 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. 75 */ 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*/ 94 95 96 97/* Flags for IM->TexCoordSize. Enough flags for 16 units. 98 */ 99#define TEX_0_SIZE_3 0x1 100#define TEX_0_SIZE_4 0x10001 101#define TEX_SIZE_3(unit) (TEX_0_SIZE_3 << (unit)) 102#define TEX_SIZE_4(unit) (TEX_0_SIZE_4 << (unit)) 103 104 105/* Shorthands. 106 */ 107#define VERT_BITS_OBJ_23 (VERT_BIT_POS | VERT_BIT_OBJ_3) 108#define VERT_BITS_OBJ_234 (VERT_BIT_POS | VERT_BIT_OBJ_3 | VERT_BIT_OBJ_4) 109 110#define VERT_BITS_TEX_ANY (VERT_BIT_TEX0 | \ 111 VERT_BIT_TEX1 | \ 112 VERT_BIT_TEX2 | \ 113 VERT_BIT_TEX3 | \ 114 VERT_BIT_TEX4 | \ 115 VERT_BIT_TEX5 | \ 116 VERT_BIT_TEX6 | \ 117 VERT_BIT_TEX7) 118 119#define VERT_BITS_EVAL_ANY (VERT_BIT_EVAL_C1 | VERT_BIT_EVAL_P1 | \ 120 VERT_BIT_EVAL_C2 | VERT_BIT_EVAL_P2) 121 122#define VERT_BITS_FIXUP (VERT_BITS_TEX_ANY | \ 123 VERT_BIT_COLOR0 | \ 124 VERT_BIT_COLOR1 | \ 125 VERT_BIT_FOG | \ 126 VERT_BIT_INDEX | \ 127 VERT_BIT_EDGEFLAG | \ 128 VERT_BIT_NORMAL) 129 130#define VERT_BITS_CURRENT_DATA (VERT_BITS_FIXUP | \ 131 VERT_BIT_MATERIAL) 132 133#define VERT_BITS_DATA (VERT_BITS_TEX_ANY | \ 134 VERT_BIT_COLOR0 | \ 135 VERT_BIT_COLOR1 | \ 136 VERT_BIT_FOG | \ 137 VERT_BIT_INDEX | \ 138 VERT_BIT_EDGEFLAG | \ 139 VERT_BIT_NORMAL | \ 140 VERT_BIT_POS | \ 141 VERT_BIT_MATERIAL | \ 142 VERT_BIT_ELT | \ 143 VERT_BITS_EVAL_ANY) 144 145 146/** 147 * KW: Represents everything that can take place between a begin and 148 * end, and can represent multiple begin/end pairs. Can be used to 149 * losslessly encode this information in display lists. 150 */ 151struct immediate 152{ 153 struct __GLcontextRec *backref; 154 GLuint id, ref_count; 155 156 /* This must be saved when immediates are shared in display lists. 157 */ 158 GLuint CopyStart, Start, Count; 159 GLuint LastData; /* count or count+1 */ 160 GLuint AndFlag, OrFlag; 161 GLuint TexSize; /* keep track of texcoord sizes */ 162 GLuint BeginState, SavedBeginState; 163 GLuint LastPrimitive; 164 165 GLuint ArrayEltFlags; /* precalc'ed for glArrayElt */ 166 GLuint ArrayEltIncr; 167 GLuint ArrayEltFlush; 168 169#define FLUSH_ELT_EAGER 0x1 170#define FLUSH_ELT_LAZY 0x2 171 GLuint FlushElt; 172 173 GLuint MaxTextureUnits; /* precalc'ed for glMultiTexCoordARB */ 174 175 /* Temporary values created when vertices are copied into the 176 * first 3 slots of the struct: 177 */ 178 GLuint CopyOrFlag; 179 GLuint CopyAndFlag; 180 GLuint CopyTexSize; 181 GLuint Evaluated; 182 183 184 /* allocate storage for these on demand: 185 */ 186 struct gl_material (*Material)[2]; 187 GLuint *MaterialMask; 188 GLuint LastMaterial; 189 GLuint MaterialOrMask; 190 GLuint MaterialAndMask; 191 192 GLuint Primitive[IMM_SIZE]; /* BEGIN/END */ 193 GLuint PrimitiveLength[IMM_SIZE]; /* BEGIN/END */ 194 GLuint Flag[IMM_SIZE]; /* VERT_BIT_* flags */ 195 196 /* All vertex attributes (position, normal, color, secondary color, 197 * texcoords, fog coord) are stored in the Attrib[] arrays instead 198 * of individual arrays as we did prior to Mesa 4.1. 199 * 200 * XXX may need to use 32-byte aligned allocation for this!!! 201 */ 202 GLfloat Attrib[VERT_ATTRIB_MAX][IMM_SIZE][4]; /* GL_NV_vertex_program */ 203 204 GLfloat *NormalLengthPtr; /* length of normal vectors (display list only) */ 205 206 GLuint Elt[IMM_SIZE]; 207 GLubyte EdgeFlag[IMM_SIZE]; 208 GLuint Index[IMM_SIZE]; 209}; 210 211 212struct vertex_arrays 213{ 214 GLvector4f Obj; 215 GLvector4f Normal; 216 struct gl_client_array Color; 217 struct gl_client_array SecondaryColor; 218 GLvector1ui Index; 219 GLvector1ub EdgeFlag; 220 GLvector4f TexCoord[MAX_TEXTURE_UNITS]; 221 GLvector1ui Elt; 222 GLvector4f FogCoord; 223 GLvector4f Attribs[VERT_ATTRIB_MAX]; 224}; 225 226 227/** 228 * Contains the current state of a running pipeline. 229 */ 230typedef struct vertex_buffer 231{ 232 /* Constant over life of the vertex_buffer. 233 */ 234 GLuint Size; 235 236 /* Constant over the pipeline. 237 */ 238 GLuint Count; /* for everything except Elts */ 239 GLuint FirstClipped; /* temp verts for clipping */ 240 GLuint FirstPrimitive; /* usually zero */ 241 242 /* Pointers to current data. 243 */ 244 GLuint *Elts; /* VERT_BIT_ELT */ 245 GLvector4f *ObjPtr; /* VERT_BIT_POS */ 246 GLvector4f *EyePtr; /* VERT_BIT_EYE */ 247 GLvector4f *ClipPtr; /* VERT_BIT_CLIP */ 248 GLvector4f *NdcPtr; /* VERT_BIT_CLIP (2) */ 249 GLubyte ClipOrMask; /* VERT_BIT_CLIP (3) */ 250 GLubyte *ClipMask; /* VERT_BIT_CLIP (4) */ 251 GLvector4f *NormalPtr; /* VERT_BIT_NORMAL */ 252 GLfloat *NormalLengthPtr; /* VERT_BIT_NORMAL */ 253 GLboolean *EdgeFlag; /* VERT_BIT_EDGEFLAG */ 254 GLvector4f *TexCoordPtr[MAX_TEXTURE_UNITS]; /* VERT_TEX_0..n */ 255 GLvector1ui *IndexPtr[2]; /* VERT_BIT_INDEX */ 256 struct gl_client_array *ColorPtr[2]; /* VERT_BIT_COLOR0 */ 257 struct gl_client_array *SecondaryColorPtr[2];/* VERT_BIT_COLOR1 */ 258 GLvector4f *PointSizePtr; /* VERT_BIT_POINT_SIZE */ 259 GLvector4f *FogCoordPtr; /* VERT_BIT_FOG */ 260 struct gl_material (*Material)[2]; /* VERT_BIT_MATERIAL, optional */ 261 GLuint *MaterialMask; /* VERT_BIT_MATERIAL, optional */ 262 GLuint *Flag; /* VERT_BIT_* flags, optional */ 263 GLuint *Primitive; /* GL_(mode)|PRIM_* flags */ 264 GLuint *PrimitiveLength; /* integers */ 265 266 /* Inputs to the vertex program stage */ 267 GLvector4f *AttribPtr[VERT_ATTRIB_MAX]; /* GL_NV_vertex_program */ 268 269 GLuint importable_data; 270 void *import_source; 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. 275 * 276 * This is currently only necessary for client arrays that make it 277 * as far down the pipeline as the render stage. 278 */ 279 280 GLuint LastClipped; 281 /* Private data from _tnl_render_stage that has no business being 282 * in this struct. 283 */ 284 285} TNLvertexbuffer; 286 287 288 289/* Describes an individual operation on the pipeline. 290 */ 291struct gl_pipeline_stage { 292 const char *name; 293 GLuint check_state; /* All state referenced in check() -- 294 * When is the pipeline_stage struct 295 * itself invalidated? Must be 296 * constant. 297 */ 298 299 /* Usually constant or set by the 'check' callback: 300 */ 301 GLuint run_state; /* All state referenced in run() -- 302 * When is the cached output of the 303 * stage invalidated? 304 */ 305 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 */ 309 310 /* Set in _tnl_run_pipeline(): 311 */ 312 GLuint changed_inputs; /* Generated value -- inputs to the 313 * stage that have changed since last 314 * call to 'run'. 315 */ 316 317 /* Private data for the pipeline stage: 318 */ 319 void *privatePtr; 320 321 /* Free private data. May not be null. 322 */ 323 void (*destroy)( struct gl_pipeline_stage * ); 324 325 /* Called from _tnl_validate_pipeline(). Must update all fields in 326 * the pipeline_stage struct for the current state. 327 */ 328 void (*check)( GLcontext *ctx, struct gl_pipeline_stage * ); 329 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. 334 * 335 * Return value: GL_TRUE - keep going 336 * GL_FALSE - finished pipeline 337 */ 338 GLboolean (*run)( GLcontext *ctx, struct gl_pipeline_stage * ); 339}; 340 341 342struct gl_pipeline { 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+1]; 349 GLuint nr_stages; 350}; 351 352 353struct tnl_eval_store { 354 GLuint EvalMap1Flags; 355 GLuint EvalMap2Flags; 356 GLuint EvalNewState; 357 struct immediate *im; /* used for temporary data */ 358}; 359 360 361typedef void (*points_func)( GLcontext *ctx, GLuint first, GLuint last ); 362typedef void (*line_func)( GLcontext *ctx, GLuint v1, GLuint v2 ); 363typedef void (*triangle_func)( GLcontext *ctx, 364 GLuint v1, GLuint v2, GLuint v3 ); 365typedef void (*quad_func)( GLcontext *ctx, GLuint v1, GLuint v2, 366 GLuint v3, GLuint v4 ); 367typedef void (*render_func)( GLcontext *ctx, GLuint start, GLuint count, 368 GLuint flags ); 369typedef void (*interp_func)( GLcontext *ctx, 370 GLfloat t, GLuint dst, GLuint out, GLuint in, 371 GLboolean force_boundary ); 372typedef void (*copy_pv_func)( GLcontext *ctx, GLuint dst, GLuint src ); 373typedef void (*setup_func)( GLcontext *ctx, 374 GLuint start, GLuint end, 375 GLuint new_inputs); 376 377 378struct tnl_device_driver { 379 /*** 380 *** TNL Pipeline 381 ***/ 382 383 void (*RunPipeline)(GLcontext *ctx); 384 /* Replaces PipelineStart/PipelineFinish -- intended to allow 385 * drivers to wrap _tnl_run_pipeline() with code to validate state 386 * and grab/release hardware locks. 387 */ 388 389 void (*NotifyMaterialChange)(GLcontext *ctx); 390 /* Alert tnl-aware drivers of changes to material, especially in 391 * exec_empty_cassette, which doesn't otherwise reach the driver. 392 * --> Need to be able to disable exec_empty_cassette??? 393 */ 394 395 /*** 396 *** Rendering -- These functions called only from t_vb_render.c 397 ***/ 398 struct { 399 void (*Start)(GLcontext *ctx); 400 void (*Finish)(GLcontext *ctx); 401 /* Called before and after all rendering operations, including DrawPixels, 402 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands. 403 * These are a suitable place for grabbing/releasing hardware locks. 404 */ 405 406 void (*PrimitiveNotify)(GLcontext *ctx, GLenum mode); 407 /* Called between RenderStart() and RenderFinish() to indicate the 408 * type of primitive we're about to draw. Mode will be one of the 409 * modes accepted by glBegin(). 410 */ 411 412 interp_func Interp; 413 /* The interp function is called by the clipping routines when we need 414 * to generate an interpolated vertex. All pertinant vertex ancilliary 415 * data should be computed by interpolating between the 'in' and 'out' 416 * vertices. 417 */ 418 419 copy_pv_func CopyPV; 420 /* The copy function is used to make a copy of a vertex. All pertinant 421 * vertex attributes should be copied. 422 */ 423 424 void (*ClippedPolygon)( GLcontext *ctx, const GLuint *elts, GLuint n ); 425 /* Render a polygon with <n> vertices whose indexes are in the <elts> 426 * array. 427 */ 428 429 void (*ClippedLine)( GLcontext *ctx, GLuint v0, GLuint v1 ); 430 /* Render a line between the two vertices given by indexes v0 and v1. */ 431 432 points_func Points; /* must now respect vb->elts */ 433 line_func Line; 434 triangle_func Triangle; 435 quad_func Quad; 436 /* These functions are called in order to render points, lines, 437 * triangles and quads. These are only called via the T&L module. 438 */ 439 440 render_func *PrimTabVerts; 441 render_func *PrimTabElts; 442 /* Render whole unclipped primitives (points, lines, linestrips, 443 * lineloops, etc). The tables are indexed by the GL enum of the 444 * primitive to be rendered. RenderTabVerts is used for non-indexed 445 * arrays of vertices. RenderTabElts is used for indexed arrays of 446 * vertices. 447 */ 448 449 void (*ResetLineStipple)( GLcontext *ctx ); 450 /* Reset the hardware's line stipple counter. 451 */ 452 453 setup_func BuildVertices; 454 /* This function is called whenever new vertices are required for 455 * rendering. The vertices in question are those n such that start 456 * <= n < end. The new_inputs parameter indicates those fields of 457 * the vertex which need to be updated, if only a partial repair of 458 * the vertex is required. 459 * 460 * This function is called only from _tnl_render_stage in tnl/t_render.c. 461 */ 462 463 464 GLboolean (*Multipass)( GLcontext *ctx, GLuint passno ); 465 /* Driver may request additional render passes by returning GL_TRUE 466 * when this function is called. This function will be called 467 * after the first pass, and passes will be made until the function 468 * returns GL_FALSE. If no function is registered, only one pass 469 * is made. 470 * 471 * This function will be first invoked with passno == 1. 472 */ 473 } Render; 474}; 475 476 477typedef struct { 478 479 /* Driver interface. 480 */ 481 struct tnl_device_driver Driver; 482 483 /* Track whether the module is active. 484 */ 485 GLboolean bound_exec; 486 487 /* Display list extensions 488 */ 489 GLuint opcode_vertex_cassette; 490 491 /* Pipeline 492 */ 493 struct gl_pipeline pipeline; 494 struct vertex_buffer vb; 495 496 /* GLvectors for binding to vb: 497 */ 498 struct vertex_arrays imm_inputs; 499 struct vertex_arrays array_inputs; 500 GLuint *tmp_primitive; 501 GLuint *tmp_primitive_length; 502 503 /* Set when executing an internally generated begin/end object. If 504 * such an object is encountered in a display list, it will be 505 * replayed only if the list is outside any existing begin/end 506 * objects. 507 */ 508 GLboolean ReplayHardBeginEnd; 509 510 /* Note which vertices need copying over succesive immediates. 511 * Will add save versions to precompute vertex copying where 512 * possible. 513 */ 514 struct immediate *ExecCopySource; 515 GLuint ExecCopyCount; 516 GLuint ExecCopyElts[IMM_MAX_COPIED_VERTS]; 517 GLuint ExecCopyTexSize; 518 GLuint ExecParity; 519 520 GLuint DlistPrimitive; 521 GLuint DlistPrimitiveLength; 522 GLuint DlistLastPrimitive; 523 524 /* Cache a single free immediate (refcount == 0) 525 */ 526 struct immediate *freed_immediate; 527 528 /* Probably need a better configuration mechanism: 529 */ 530 GLboolean NeedNdcCoords; 531 GLboolean LoopbackDListCassettes; 532 GLboolean CalcDListNormalLengths; 533 GLboolean IsolateMaterials; 534 535 /* Derived state and storage for _tnl_eval_vb: 536 */ 537 struct tnl_eval_store eval; 538 539 /* Functions to be plugged into dispatch when tnl is active. 540 */ 541 GLvertexformat vtxfmt; 542 543} TNLcontext; 544 545 546 547#define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context)) 548#define TNL_CURRENT_IM(ctx) ((struct immediate *)(ctx->swtnl_im)) 549 550 551#define TYPE_IDX(t) ((t) & 0xf) 552#define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */ 553 554extern void _tnl_MakeCurrent( GLcontext *ctx, 555 GLframebuffer *drawBuffer, 556 GLframebuffer *readBuffer ); 557 558 559/* 560 * Macros for fetching current input buffer. 561 */ 562#ifdef THREADS 563#define GET_IMMEDIATE struct immediate *IM = TNL_CURRENT_IM(((GLcontext *) (_glapi_Context ? _glapi_Context : _glapi_get_context()))) 564#define SET_IMMEDIATE(ctx, im) ctx->swtnl_im = (void *)im 565#else 566extern struct immediate *_tnl_CurrentInput; 567#define GET_IMMEDIATE struct immediate *IM = _tnl_CurrentInput 568#define SET_IMMEDIATE(ctx, im) \ 569do { \ 570 ctx->swtnl_im = (void *)im; \ 571 _tnl_CurrentInput = im; \ 572} while (0) 573#endif 574 575 576#endif 577