9864c8ec07055c1c99591131c49e1c376f831325
[mesa.git] / src / mesa / tnl / t_context.h
1 /* $Id: t_context.h,v 1.24 2001/05/11 08:11:31 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 * Author:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30 #ifndef _T_CONTEXT_H
31 #define _T_CONTEXT_H
32
33 #include "glheader.h"
34 #include "mtypes.h"
35
36 #include "math/m_matrix.h"
37 #include "math/m_vector.h"
38 #include "math/m_xform.h"
39
40
41 #define MAX_PIPELINE_STAGES 30
42
43
44 /* Numbers for sizing immediate structs.
45 */
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)
49
50
51 /* Values for IM->BeginState
52 */
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 */
57
58
59 /* Flags to be added to the primitive enum in VB->Primitive.
60 */
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 */
66
67
68 /* Flags that describe the inputs and outputs of pipeline stages, and
69 * the contents of a vertex-cassette.
70 *
71 * 5 spare flags, rearrangement of eval flags can secure at least 3
72 * more.
73 */
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*/
103
104
105 /* Flags for IM->TexCoordSize. Enough flags for 16 units.
106 */
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)
111
112
113 /* Shorthands.
114 */
115 #define VERT_EVAL_ANY (VERT_EVAL_C1|VERT_EVAL_P1| \
116 VERT_EVAL_C2|VERT_EVAL_P2)
117
118 #define VERT_OBJ_23 (VERT_OBJ_3|VERT_OBJ)
119 #define VERT_OBJ_234 (VERT_OBJ_4|VERT_OBJ_23)
120
121 #define VERT_TEX0_SHIFT 11
122
123 #define VERT_TEX(i) (VERT_TEX0 << i)
124
125 #define VERT_TEX_ANY (VERT_TEX0 | \
126 VERT_TEX1 | \
127 VERT_TEX2 | \
128 VERT_TEX3 | \
129 VERT_TEX4 | \
130 VERT_TEX5 | \
131 VERT_TEX6 | \
132 VERT_TEX7)
133
134 #define VERT_FIXUP (VERT_TEX_ANY | \
135 VERT_RGBA | \
136 VERT_SPEC_RGB | \
137 VERT_FOG_COORD | \
138 VERT_INDEX | \
139 VERT_EDGE | \
140 VERT_NORM)
141
142 #define VERT_CURRENT_DATA (VERT_FIXUP | \
143 VERT_MATERIAL)
144
145 #define VERT_DATA (VERT_TEX_ANY | \
146 VERT_RGBA | \
147 VERT_SPEC_RGB | \
148 VERT_FOG_COORD | \
149 VERT_INDEX | \
150 VERT_EDGE | \
151 VERT_NORM | \
152 VERT_OBJ | \
153 VERT_MATERIAL | \
154 VERT_ELT | \
155 VERT_EVAL_ANY)
156
157
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.
161 */
162 struct immediate
163 {
164 struct __GLcontextRec *backref;
165 GLuint id, ref_count;
166
167 /* This must be saved when immediates are shared in display lists.
168 */
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;
175
176 GLuint ArrayEltFlags; /* precalc'ed for glArrayElt */
177 GLuint ArrayEltIncr;
178 GLuint ArrayEltFlush;
179
180 #define FLUSH_ELT_EAGER 0x1
181 #define FLUSH_ELT_LAZY 0x2
182 GLuint FlushElt;
183
184 GLuint MaxTextureUnits; /* precalc'ed for glMultiTexCoordARB */
185
186 /* Temporary values created when vertices are copied into the
187 * first 3 slots of the struct:
188 */
189 GLuint CopyOrFlag;
190 GLuint CopyAndFlag;
191 GLuint CopyTexSize;
192
193
194 /* allocate storage for these on demand:
195 */
196 struct gl_material (*Material)[2];
197 GLuint *MaterialMask;
198 GLuint LastMaterial;
199 GLuint MaterialOrMask;
200 GLuint MaterialAndMask;
201
202 GLfloat (*TexCoord[MAX_TEXTURE_UNITS])[4];
203
204 GLuint Primitive[IMM_SIZE]; /* BEGIN/END */
205 GLuint PrimitiveLength[IMM_SIZE]; /* BEGIN/END */
206 GLuint Flag[IMM_SIZE]; /* VERT_* flags */
207 GLfloat Color[IMM_SIZE][4];
208 GLfloat Obj[IMM_SIZE][4];
209 GLfloat Normal[IMM_SIZE][3];
210 GLfloat TexCoord0[IMM_SIZE][4]; /* just VERT_TEX0 */
211 GLuint Elt[IMM_SIZE];
212 GLubyte EdgeFlag[IMM_SIZE];
213 GLuint Index[IMM_SIZE];
214 GLfloat SecondaryColor[IMM_SIZE][4];
215 GLfloat FogCoord[IMM_SIZE];
216 };
217
218
219 struct vertex_arrays
220 {
221 GLvector4f Obj;
222 GLvector3f Normal;
223 struct gl_client_array Color;
224 struct gl_client_array SecondaryColor;
225 GLvector1ui Index;
226 GLvector1ub EdgeFlag;
227 GLvector4f TexCoord[MAX_TEXTURE_UNITS];
228 GLvector1ui Elt;
229 GLvector1f FogCoord;
230 };
231
232
233 typedef struct gl_material GLmaterial;
234
235 /* Contains the current state of a running pipeline.
236 */
237 typedef struct vertex_buffer
238 {
239 /* Constant over life of the vertex_buffer.
240 */
241 GLuint Size;
242
243 /* Constant over the pipeline.
244 */
245 GLuint Count; /* for everything except Elts */
246 GLuint FirstClipped; /* temp verts for clipping */
247 GLuint FirstPrimitive; /* usually zero */
248
249 /* Pointers to current data.
250 */
251 GLuint *Elts; /* VERT_ELT */
252 GLvector4f *ObjPtr; /* VERT_OBJ */
253 GLvector4f *EyePtr; /* VERT_EYE */
254 GLvector4f *ClipPtr; /* VERT_CLIP */
255 GLvector4f *ProjectedClipPtr; /* VERT_CLIP (2) */
256 GLubyte ClipOrMask; /* VERT_CLIP (3) */
257 GLubyte *ClipMask; /* VERT_CLIP (4) */
258 GLvector3f *NormalPtr; /* VERT_NORM */
259 GLboolean *EdgeFlag; /* VERT_EDGE */
260 GLvector4f *TexCoordPtr[MAX_TEXTURE_UNITS]; /* VERT_TEX_0..n */
261 GLvector1ui *IndexPtr[2]; /* VERT_INDEX */
262 struct gl_client_array *ColorPtr[2]; /* VERT_RGBA */
263 struct gl_client_array *SecondaryColorPtr[2]; /* VERT_SPEC_RGB */
264 GLvector1f *FogCoordPtr; /* VERT_FOG_COORD */
265 GLvector1f *PointSizePtr; /* VERT_POINT_SIZE */
266 GLmaterial (*Material)[2]; /* VERT_MATERIAL, optional */
267 GLuint *MaterialMask; /* VERT_MATERIAL, optional */
268 GLuint *Flag; /* VERT_* flags, optional */
269 GLuint *Primitive; /* GL_(mode)|PRIM_* flags */
270 GLuint *PrimitiveLength; /* integers */
271
272
273 GLuint importable_data;
274 void (*import_data)( GLcontext *ctx, GLuint flags, GLuint vecflags );
275 /* Callback to the provider of the untransformed input for the
276 * render stage (or other stages) to call if they need to write into
277 * write-protected arrays, or fixup the stride on input arrays.
278 *
279 * This is currently only necessary for client arrays that make it
280 * as far down the pipeline as the render stage.
281 */
282
283 GLuint LastClipped;
284 /* Private data from _tnl_render_stage that has no business being
285 * in this struct.
286 */
287
288 } TNLvertexbuffer;
289
290
291
292 /* Describes an individual operation on the pipeline.
293 */
294 struct gl_pipeline_stage {
295 const char *name;
296 GLuint check_state; /* All state referenced in check() --
297 * When is the pipeline_stage struct
298 * itself invalidated? Must be
299 * constant.
300 */
301
302 /* Usually constant or set by the 'check' callback:
303 */
304 GLuint run_state; /* All state referenced in run() --
305 * When is the cached output of the
306 * stage invalidated?
307 */
308
309 GLboolean active; /* True if runnable in current state */
310 GLuint inputs; /* VERT_* inputs to the stage */
311 GLuint outputs; /* VERT_* outputs of the stage */
312
313 /* Set in _tnl_run_pipeline():
314 */
315 GLuint changed_inputs; /* Generated value -- inputs to the
316 * stage that have changed since last
317 * call to 'run'.
318 */
319
320 /* Private data for the pipeline stage:
321 */
322 void *privatePtr;
323
324 /* Free private data. May not be null.
325 */
326 void (*destroy)( struct gl_pipeline_stage * );
327
328 /* Called from _tnl_validate_pipeline(). Must update all fields in
329 * the pipeline_stage struct for the current state.
330 */
331 void (*check)( GLcontext *ctx, struct gl_pipeline_stage * );
332
333 /* Called from _tnl_run_pipeline(). The stage.changed_inputs value
334 * encodes all inputs to thee struct which have changed. If
335 * non-zero, recompute all affected outputs of the stage, otherwise
336 * execute any 'sideeffects' of the stage.
337 *
338 * Return value: GL_TRUE - keep going
339 * GL_FALSE - finished pipeline
340 */
341 GLboolean (*run)( GLcontext *ctx, struct gl_pipeline_stage * );
342 };
343
344
345 struct gl_pipeline {
346 GLuint build_state_trigger; /* state changes which require build */
347 GLuint build_state_changes; /* state changes since last build */
348 GLuint run_state_changes; /* state changes since last run */
349 GLuint run_input_changes; /* VERT_* changes since last run */
350 GLuint inputs; /* VERT_* inputs to pipeline */
351 struct gl_pipeline_stage stages[MAX_PIPELINE_STAGES+1];
352 GLuint nr_stages;
353 };
354
355
356 struct tnl_eval_store {
357 GLuint EvalMap1Flags;
358 GLuint EvalMap2Flags;
359 GLuint EvalNewState;
360 struct immediate *im; /* used for temporary data */
361 };
362
363
364 typedef void (*points_func)( GLcontext *ctx, GLuint first, GLuint last );
365 typedef void (*line_func)( GLcontext *ctx, GLuint v1, GLuint v2 );
366 typedef void (*triangle_func)( GLcontext *ctx,
367 GLuint v1, GLuint v2, GLuint v3 );
368 typedef void (*quad_func)( GLcontext *ctx, GLuint v1, GLuint v2,
369 GLuint v3, GLuint v4 );
370 typedef void (*render_func)( GLcontext *ctx, GLuint start, GLuint count,
371 GLuint flags );
372 typedef void (*interp_func)( GLcontext *ctx,
373 GLfloat t, GLuint dst, GLuint in, GLuint out,
374 GLboolean force_boundary );
375 typedef void (*copy_pv_func)( GLcontext *ctx, GLuint dst, GLuint src );
376
377
378 struct 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 /***
390 *** Rendering
391 ***/
392
393 void (*RenderStart)(GLcontext *ctx);
394 void (*RenderFinish)(GLcontext *ctx);
395 /* Called before and after all rendering operations, including DrawPixels,
396 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
397 * These are a suitable place for grabbing/releasing hardware locks.
398 */
399
400 void (*RenderPrimitive)(GLcontext *ctx, GLenum mode);
401 /* Called between RednerStart() and RenderFinish() to indicate the
402 * type of primitive we're about to draw. Mode will be one of the
403 * modes accepted by glBegin().
404 */
405
406 interp_func RenderInterp;
407 copy_pv_func RenderCopyPV;
408 void (*RenderClippedPolygon)( GLcontext *ctx, const GLuint *elts, GLuint n );
409 void (*RenderClippedLine)( GLcontext *ctx, GLuint v0, GLuint v1 );
410 /* Functions to interpolate between prebuilt vertices, copy flat-shade
411 * provoking color, and to render clipped primitives.
412 */
413
414 points_func PointsFunc; /* must now respect vb->elts */
415 line_func LineFunc;
416 triangle_func TriangleFunc;
417 quad_func QuadFunc;
418 /* These functions are called in order to render points, lines,
419 * triangles and quads. These are only called via the T&L module.
420 */
421
422 render_func *RenderTabVerts;
423 render_func *RenderTabElts;
424 /* Render whole unclipped primitives (points, lines, linestrips,
425 * lineloops, etc). The tables are indexed by the GL enum of the
426 * primitive to be rendered.
427 */
428
429 void (*ResetLineStipple)( GLcontext *ctx );
430 /* Reset the hardware's line stipple counter.
431 */
432
433 void (*BuildProjectedVertices)( GLcontext *ctx,
434 GLuint start, GLuint end,
435 GLuint new_inputs);
436 /* This function is called whenever new vertices are required for
437 * rendering. The vertices in question are those n such that start
438 * <= n < end. The new_inputs parameter indicates those fields of
439 * the vertex which need to be updated, if only a partial repair of
440 * the vertex is required.
441 *
442 * This function is called only from _tnl_render_stage in tnl/t_render.c.
443 */
444
445
446 GLboolean (*MultipassFunc)( GLcontext *ctx, GLuint passno );
447 /* Driver may request additional render passes by returning GL_TRUE
448 * when this function is called. This function will be called
449 * after the first pass, and passes will be made until the function
450 * returns GL_FALSE. If no function is registered, only one pass
451 * is made.
452 *
453 * This function will be first invoked with passno == 1.
454 */
455 };
456
457
458 typedef struct {
459
460 /* Driver interface.
461 */
462 struct tnl_device_driver Driver;
463
464 /* Track whether the module is active.
465 */
466 GLboolean bound_exec;
467
468 /* Display list extensions
469 */
470 GLuint opcode_vertex_cassette;
471
472 /* Pipeline
473 */
474 struct gl_pipeline pipeline;
475 struct vertex_buffer vb;
476
477 /* GLvectors for binding to vb:
478 */
479 struct vertex_arrays imm_inputs;
480 struct vertex_arrays array_inputs;
481 GLuint *tmp_primitive;
482 GLuint *tmp_primitive_length;
483
484 /* Set when executing an internally generated begin/end object. If
485 * such an object is encountered in a display list, it will be
486 * replayed only if the list is outside any existing begin/end
487 * objects.
488 */
489 GLboolean ReplayHardBeginEnd;
490
491 /* Note which vertices need copying over succesive immediates.
492 * Will add save versions to precompute vertex copying where
493 * possible.
494 */
495 struct immediate *ExecCopySource;
496 GLuint ExecCopyCount;
497 GLuint ExecCopyElts[IMM_MAX_COPIED_VERTS];
498 GLuint ExecCopyTexSize;
499 GLuint ExecParity;
500
501 GLuint DlistPrimitive;
502 GLuint DlistPrimitiveLength;
503 GLuint DlistLastPrimitive;
504
505 /* Cache a single free immediate (refcount == 0)
506 */
507 struct immediate *freed_immediate;
508
509 /* Probably need a better configuration mechanism:
510 */
511 GLboolean NeedProjCoords;
512
513 /* Derived state and storage for _tnl_eval_vb:
514 */
515 struct tnl_eval_store eval;
516
517 /* Functions to be plugged into dispatch when tnl is active.
518 */
519 GLvertexformat vtxfmt;
520
521 } TNLcontext;
522
523
524
525 #define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context))
526 #define TNL_CURRENT_IM(ctx) ((struct immediate *)(ctx->swtnl_im))
527
528
529 #define TYPE_IDX(t) ((t) & 0xf)
530 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
531
532 extern void _tnl_MakeCurrent( GLcontext *ctx,
533 GLframebuffer *drawBuffer,
534 GLframebuffer *readBuffer );
535
536
537 /*
538 * Macros for fetching current input buffer.
539 */
540 #ifdef THREADS
541 #define GET_IMMEDIATE struct immediate *IM = TNL_CURRENT_IM(((GLcontext *) (_glapi_Context ? _glapi_Context : _glapi_get_context())))
542 #define SET_IMMEDIATE(ctx, im) ctx->swtnl_im = (void *)im
543 #else
544 extern struct immediate *_tnl_CurrentInput;
545 #define GET_IMMEDIATE struct immediate *IM = _tnl_CurrentInput
546 #define SET_IMMEDIATE(ctx, im) \
547 do { \
548 ctx->swtnl_im = (void *)im; \
549 _tnl_CurrentInput = im; \
550 } while (0)
551 #endif
552
553
554 #endif