Added GLvector4chan type, removed lots of CHAN_TYPE ifdefs.
[mesa.git] / src / mesa / tnl / t_context.h
1 /* $Id: t_context.h,v 1.15 2001/02/20 18:28:52 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999 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 GLuint FlushElt;
180
181 GLuint MaxTextureUnits; /* precalc'ed for glMultiTexCoordARB */
182
183 /* Temporary values created when vertices are copied into the
184 * first 3 slots of the struct:
185 */
186 GLuint CopyOrFlag;
187 GLuint CopyAndFlag;
188 GLuint CopyTexSize;
189
190
191 /* allocate storage for these on demand:
192 */
193 struct gl_material (*Material)[2];
194 GLuint *MaterialMask;
195 GLuint LastMaterial;
196 GLuint MaterialOrMask;
197
198 GLfloat (*TexCoord[MAX_TEXTURE_UNITS])[4];
199
200 GLuint Primitive[IMM_SIZE]; /* BEGIN/END */
201 GLuint PrimitiveLength[IMM_SIZE]; /* BEGIN/END */
202 GLuint Flag[IMM_SIZE]; /* VERT_* flags */
203 GLchan Color[IMM_SIZE][4];
204 GLfloat Obj[IMM_SIZE][4];
205 GLfloat Normal[IMM_SIZE][3];
206 GLfloat TexCoord0[IMM_SIZE][4]; /* just VERT_TEX0 */
207 GLuint Elt[IMM_SIZE];
208 GLubyte EdgeFlag[IMM_SIZE];
209 GLuint Index[IMM_SIZE];
210 GLchan SecondaryColor[IMM_SIZE][4];
211 GLfloat FogCoord[IMM_SIZE];
212 };
213
214
215 struct vertex_arrays
216 {
217 GLvector4f Obj;
218 GLvector3f Normal;
219 GLvector4chan Color;
220 GLvector4chan SecondaryColor;
221 GLvector1ui Index;
222 GLvector1ub EdgeFlag;
223 GLvector4f TexCoord[MAX_TEXTURE_UNITS];
224 GLvector1ui Elt;
225 GLvector1f FogCoord;
226 };
227
228
229 typedef struct gl_material GLmaterial;
230
231 /* Contains the current state of a running pipeline.
232 */
233 typedef struct vertex_buffer
234 {
235 /* Constant over life of the vertex_buffer.
236 */
237 GLuint Size;
238
239 /* Constant over the pipeline.
240 */
241 GLuint Count; /* for everything except Elts */
242 GLuint FirstClipped; /* temp verts for clipping */
243 GLuint FirstPrimitive; /* usually zero */
244
245 /* Pointers to current data.
246 */
247 GLuint *Elts; /* VERT_ELT */
248 GLvector4f *ObjPtr; /* VERT_OBJ */
249 GLvector4f *EyePtr; /* VERT_EYE */
250 GLvector4f *ClipPtr; /* VERT_CLIP */
251 GLvector4f *ProjectedClipPtr; /* VERT_CLIP (2) */
252 GLubyte ClipOrMask; /* VERT_CLIP (3) */
253 GLubyte *ClipMask; /* VERT_CLIP (4) */
254 GLvector3f *NormalPtr; /* VERT_NORM */
255 GLboolean *EdgeFlag; /* VERT_EDGE */
256 GLvector4f *TexCoordPtr[MAX_TEXTURE_UNITS]; /* VERT_TEX_0..n */
257 GLvector1ui *IndexPtr[2]; /* VERT_INDEX */
258 GLvector4chan *ColorPtr[2]; /* VERT_RGBA */
259 GLvector4chan *SecondaryColorPtr[2]; /* VERT_SPEC_RGB */
260 GLvector1f *FogCoordPtr; /* VERT_FOG_COORD */
261 GLvector1f *PointSizePtr; /* VERT_POINT_SIZE */
262 GLmaterial (*Material)[2]; /* VERT_MATERIAL, optional */
263 GLuint *MaterialMask; /* VERT_MATERIAL, optional */
264 GLuint *Flag; /* VERT_* flags, optional */
265 GLuint *Primitive; /* GL_(mode)|PRIM_* flags */
266 GLuint *PrimitiveLength; /* integers */
267
268
269 GLuint importable_data;
270 void (*import_data)( GLcontext *ctx, GLuint flags, GLuint vecflags );
271 /* Callback to the provider of the untransformed input for the
272 * render stage (or other stages) to call if they need to write into
273 * write-protected arrays, or fixup the stride on input arrays.
274 *
275 * This is currently only necessary for client arrays that make it
276 * as far down the pipeline as the render stage.
277 */
278
279 GLuint LastClipped;
280 /* Private data from _tnl_render_stage that has no business being
281 * in this struct.
282 */
283
284 } TNLvertexbuffer;
285
286
287
288 /* Describes an individual operation on the pipeline.
289 */
290 struct gl_pipeline_stage {
291 const char *name;
292 GLuint check_state; /* All state referenced in check() --
293 * When is the pipeline_stage struct
294 * itself invalidated? Must be
295 * constant.
296 */
297
298 /* Usually constant or set by the 'check' callback:
299 */
300 GLuint run_state; /* All state referenced in run() --
301 * When is the cached output of the
302 * stage invalidated?
303 */
304
305 GLboolean active; /* True if runnable in current state */
306 GLuint inputs; /* VERT_* inputs to the stage */
307 GLuint outputs; /* VERT_* outputs of the stage */
308
309 /* Set in _tnl_run_pipeline():
310 */
311 GLuint changed_inputs; /* Generated value -- inputs to the
312 * stage that have changed since last
313 * call to 'run'.
314 */
315
316 /* Private data for the pipeline stage:
317 */
318 void *private;
319
320 /* Free private data. May not be null.
321 */
322 void (*destroy)( struct gl_pipeline_stage * );
323
324 /* Called from _tnl_validate_pipeline(). Must update all fields in
325 * the pipeline_stage struct for the current state.
326 */
327 void (*check)( GLcontext *ctx, struct gl_pipeline_stage * );
328
329 /* Called from _tnl_run_pipeline(). The stage.changed_inputs value
330 * encodes all inputs to thee struct which have changed. If
331 * non-zero, recompute all affected outputs of the stage, otherwise
332 * execute any 'sideeffects' of the stage.
333 *
334 * Return value: GL_TRUE - keep going
335 * GL_FALSE - finished pipeline
336 */
337 GLboolean (*run)( GLcontext *ctx, struct gl_pipeline_stage * );
338 };
339
340
341 struct gl_pipeline {
342 GLuint build_state_trigger; /* state changes which require build */
343 GLuint build_state_changes; /* state changes since last build */
344 GLuint run_state_changes; /* state changes since last run */
345 GLuint run_input_changes; /* VERT_* changes since last run */
346 GLuint inputs; /* VERT_* inputs to pipeline */
347 struct gl_pipeline_stage stages[MAX_PIPELINE_STAGES];
348 GLuint nr_stages;
349 };
350
351
352 struct tnl_eval_store {
353 GLuint EvalMap1Flags;
354 GLuint EvalMap2Flags;
355 GLuint EvalNewState;
356
357 GLfloat Coord[IMM_SIZE][4];
358 GLfloat Obj[IMM_SIZE][4];
359 GLfloat TexCoord[IMM_SIZE][4];
360 GLfloat Normal[IMM_SIZE][3];
361 GLchan Color[IMM_SIZE][4];
362 GLuint Index[IMM_SIZE];
363 GLuint Flag[IMM_SIZE];
364 GLuint Elts[IMM_SIZE];
365 };
366
367 typedef struct {
368
369 /* Track whether the module is active.
370 */
371 GLboolean bound_exec;
372
373 /* Display list extensions
374 */
375 GLuint opcode_vertex_cassette;
376
377 /* Pipeline
378 */
379 struct gl_pipeline pipeline;
380 struct vertex_buffer vb;
381
382 /* GLvectors for binding to vb:
383 */
384 struct vertex_arrays imm_inputs;
385 struct vertex_arrays array_inputs;
386 GLuint *tmp_primitive;
387 GLuint *tmp_primitive_length;
388
389 /* Set when executing an internally generated immediate.
390 */
391 GLboolean ReplayHardBeginEnd;
392 GLenum CurrentPrimitive;
393
394 /* Note which vertices need copying over succesive immediates.
395 * Will add save versions to precompute vertex copying where
396 * possible.
397 */
398 struct immediate *ExecCopySource;
399 GLuint ExecCopyCount;
400 GLuint ExecCopyElts[IMM_MAX_COPIED_VERTS];
401 GLuint ExecCopyTexSize;
402 GLuint ExecParity;
403
404 GLuint DlistPrimitive;
405 GLuint DlistPrimitiveLength;
406 GLuint DlistLastPrimitive;
407
408 /* Probably need a better configuration mechanism:
409 */
410 GLboolean NeedProjCoords;
411
412 /* Derived state and storage for _tnl_eval_vb:
413 */
414 struct tnl_eval_store eval;
415
416 /* Functions to be plugged into dispatch when tnl is active.
417 */
418 GLvertexformat vtxfmt;
419
420 } TNLcontext;
421
422
423
424 #define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context))
425 #define TNL_CURRENT_IM(ctx) ((struct immediate *)(ctx->swtnl_im))
426
427
428 #define TYPE_IDX(t) ((t) & 0xf)
429 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
430
431 extern void _tnl_MakeCurrent( GLcontext *ctx,
432 GLframebuffer *drawBuffer,
433 GLframebuffer *readBuffer );
434
435
436 /*
437 * Macros for fetching current input buffer.
438 */
439 #ifdef THREADS
440 #define GET_IMMEDIATE struct immediate *IM = TNL_CURRENT_IM(((GLcontext *) (_glapi_Context ? _glapi_Context : _glapi_get_context())))
441 #define SET_IMMEDIATE(ctx, im) ctx->swtnl_im = (void *)im
442 #else
443 extern struct immediate *_tnl_CurrentInput;
444 #define GET_IMMEDIATE struct immediate *IM = _tnl_CurrentInput
445 #define SET_IMMEDIATE(ctx, im) \
446 do { \
447 ctx->swtnl_im = (void *)im; \
448 _tnl_CurrentInput = im; \
449 } while (0)
450 #endif
451
452
453 #endif