Merge branch 'mesa_7_6_branch' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / tnl / t_context.h
1 /*
2 * mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file t_context.h
27 * \brief TnL module datatypes and definitions.
28 * \author Keith Whitwell
29 */
30
31
32 /**
33 * \mainpage The TNL-module
34 *
35 * TNL stands for "transform and lighting", i.e. this module implements
36 * a pipeline that receives as input a buffer of vertices and does all
37 * necessary transformations (rotations, clipping, vertex shader etc.)
38 * and passes then the output to the rasterizer.
39 *
40 * The tnl_pipeline contains the array of all stages, which should be
41 * applied. Each stage is a black-box, which is described by an
42 * tnl_pipeline_stage. The function ::_tnl_run_pipeline applies all the
43 * stages to the vertex_buffer TNLcontext::vb, where the vertex data
44 * is stored. The last stage in the pipeline is the rasterizer.
45 *
46 */
47
48
49 #ifndef _T_CONTEXT_H
50 #define _T_CONTEXT_H
51
52 #include "main/glheader.h"
53 #include "main/bitset.h"
54 #include "main/mtypes.h"
55
56 #include "math/m_matrix.h"
57 #include "math/m_vector.h"
58 #include "math/m_xform.h"
59
60 #include "vbo/vbo.h"
61
62 #define MAX_PIPELINE_STAGES 30
63
64 /*
65 * Note: The first attributes match the VERT_ATTRIB_* definitions
66 * in mtypes.h. However, the tnl module has additional attributes
67 * for materials, color indexes, edge flags, etc.
68 */
69 /* Although it's nice to use these as bit indexes in a DWORD flag, we
70 * could manage without if necessary. Another limit currently is the
71 * number of bits allocated for these numbers in places like vertex
72 * program instruction formats and register layouts.
73 */
74 /* The bit space exhaustion is a fact now, done by _TNL_ATTRIB_ATTRIBUTE* for
75 * GLSL vertex shader which cannot be aliased with conventional vertex attribs.
76 * Compacting _TNL_ATTRIB_MAT_* attribs would not work, they would not give
77 * as many free bits (11 plus already 1 free bit) as _TNL_ATTRIB_ATTRIBUTE*
78 * attribs want (16).
79 */
80 enum {
81 _TNL_ATTRIB_POS = 0,
82 _TNL_ATTRIB_WEIGHT = 1,
83 _TNL_ATTRIB_NORMAL = 2,
84 _TNL_ATTRIB_COLOR0 = 3,
85 _TNL_ATTRIB_COLOR1 = 4,
86 _TNL_ATTRIB_FOG = 5,
87 _TNL_ATTRIB_COLOR_INDEX = 6,
88 _TNL_ATTRIB_EDGEFLAG = 7,
89 _TNL_ATTRIB_TEX0 = 8,
90 _TNL_ATTRIB_TEX1 = 9,
91 _TNL_ATTRIB_TEX2 = 10,
92 _TNL_ATTRIB_TEX3 = 11,
93 _TNL_ATTRIB_TEX4 = 12,
94 _TNL_ATTRIB_TEX5 = 13,
95 _TNL_ATTRIB_TEX6 = 14,
96 _TNL_ATTRIB_TEX7 = 15,
97
98 _TNL_ATTRIB_GENERIC0 = 16, /* doesn't really exist! */
99 _TNL_ATTRIB_GENERIC1 = 17,
100 _TNL_ATTRIB_GENERIC2 = 18,
101 _TNL_ATTRIB_GENERIC3 = 19,
102 _TNL_ATTRIB_GENERIC4 = 20,
103 _TNL_ATTRIB_GENERIC5 = 21,
104 _TNL_ATTRIB_GENERIC6 = 22,
105 _TNL_ATTRIB_GENERIC7 = 23,
106 _TNL_ATTRIB_GENERIC8 = 24,
107 _TNL_ATTRIB_GENERIC9 = 25,
108 _TNL_ATTRIB_GENERIC10 = 26,
109 _TNL_ATTRIB_GENERIC11 = 27,
110 _TNL_ATTRIB_GENERIC12 = 28,
111 _TNL_ATTRIB_GENERIC13 = 29,
112 _TNL_ATTRIB_GENERIC14 = 30,
113 _TNL_ATTRIB_GENERIC15 = 31,
114
115 /* These alias with the generics, but they are not active
116 * concurrently, so it's not a problem. The TNL module
117 * doesn't have to do anything about this as this is how they
118 * are passed into the _draw_prims callback.
119 *
120 * When we generate fixed-function replacement programs (in
121 * t_vp_build.c currently), they refer to the appropriate
122 * generic attribute in order to pick up per-vertex material
123 * data.
124 */
125 _TNL_ATTRIB_MAT_FRONT_AMBIENT = 16,
126 _TNL_ATTRIB_MAT_BACK_AMBIENT = 17,
127 _TNL_ATTRIB_MAT_FRONT_DIFFUSE = 18,
128 _TNL_ATTRIB_MAT_BACK_DIFFUSE = 19,
129 _TNL_ATTRIB_MAT_FRONT_SPECULAR = 20,
130 _TNL_ATTRIB_MAT_BACK_SPECULAR = 21,
131 _TNL_ATTRIB_MAT_FRONT_EMISSION = 22,
132 _TNL_ATTRIB_MAT_BACK_EMISSION = 23,
133 _TNL_ATTRIB_MAT_FRONT_SHININESS = 24,
134 _TNL_ATTRIB_MAT_BACK_SHININESS = 25,
135 _TNL_ATTRIB_MAT_FRONT_INDEXES = 26,
136 _TNL_ATTRIB_MAT_BACK_INDEXES = 27,
137
138 /* This is really a VERT_RESULT, not an attrib. Need to fix
139 * tnl to understand the difference.
140 */
141 _TNL_ATTRIB_POINTSIZE = 16,
142
143 _TNL_ATTRIB_MAX = 32
144 } ;
145
146 #define _TNL_ATTRIB_TEX(u) (_TNL_ATTRIB_TEX0 + (u))
147 #define _TNL_ATTRIB_GENERIC(n) (_TNL_ATTRIB_GENERIC0 + (n))
148
149 /* special index used for handing invalid glVertexAttribute() indices */
150 #define _TNL_ATTRIB_ERROR (_TNL_ATTRIB_GENERIC15 + 1)
151
152 /**
153 * Handy attribute ranges:
154 */
155 #define _TNL_FIRST_PROG _TNL_ATTRIB_WEIGHT
156 #define _TNL_LAST_PROG _TNL_ATTRIB_TEX7
157
158 #define _TNL_FIRST_TEX _TNL_ATTRIB_TEX0
159 #define _TNL_LAST_TEX _TNL_ATTRIB_TEX7
160
161 #define _TNL_FIRST_GENERIC _TNL_ATTRIB_GENERIC0
162 #define _TNL_LAST_GENERIC _TNL_ATTRIB_GENERIC15
163
164 #define _TNL_FIRST_MAT _TNL_ATTRIB_MAT_FRONT_AMBIENT /* GENERIC0 */
165 #define _TNL_LAST_MAT _TNL_ATTRIB_MAT_BACK_INDEXES /* GENERIC11 */
166
167 /* Number of available generic attributes */
168 #define _TNL_NUM_GENERIC 16
169
170 /* Number of attributes used for evaluators */
171 #define _TNL_NUM_EVAL 16
172
173
174 #define PRIM_BEGIN 0x10
175 #define PRIM_END 0x20
176 #define PRIM_MODE_MASK 0x0f
177
178 static INLINE GLuint _tnl_translate_prim( const struct _mesa_prim *prim )
179 {
180 GLuint flag;
181 flag = prim->mode;
182 if (prim->begin) flag |= PRIM_BEGIN;
183 if (prim->end) flag |= PRIM_END;
184 return flag;
185 }
186
187
188
189
190 /**
191 * Contains the current state of a running pipeline.
192 */
193 struct vertex_buffer
194 {
195 GLuint Size; /**< Max vertices per vertex buffer, constant */
196
197 /* Constant over the pipeline.
198 */
199 GLuint Count; /**< Number of vertices currently in buffer */
200
201 /* Pointers to current data.
202 * XXX some of these fields alias AttribPtr below and should be removed
203 * such as NormalPtr, TexCoordPtr, FogCoordPtr, etc.
204 */
205 GLuint *Elts;
206 GLvector4f *ObjPtr; /* _TNL_BIT_POS */
207 GLvector4f *EyePtr; /* _TNL_BIT_POS */
208 GLvector4f *ClipPtr; /* _TNL_BIT_POS */
209 GLvector4f *NdcPtr; /* _TNL_BIT_POS */
210 GLfloat *ClipDistancePtr[MAX_CLIP_PLANES]; /* _TNL_BIT_POS */
211 GLubyte ClipOrMask; /* _TNL_BIT_POS */
212 GLubyte ClipAndMask; /* _TNL_BIT_POS */
213 GLubyte *ClipMask; /* _TNL_BIT_POS */
214 GLvector4f *NormalPtr; /* _TNL_BIT_NORMAL */
215 GLfloat *NormalLengthPtr; /* _TNL_BIT_NORMAL */
216 GLboolean *EdgeFlag; /* _TNL_BIT_EDGEFLAG */
217 GLvector4f *TexCoordPtr[MAX_TEXTURE_COORD_UNITS]; /* VERT_TEX_0..n */
218 GLvector4f *IndexPtr[2]; /* _TNL_BIT_INDEX */
219 GLvector4f *ColorPtr[2]; /* _TNL_BIT_COLOR0 */
220 GLvector4f *SecondaryColorPtr[2]; /* _TNL_BIT_COLOR1 */
221 GLvector4f *FogCoordPtr; /* _TNL_BIT_FOG */
222
223 const struct _mesa_prim *Primitive;
224 GLuint PrimitiveCount;
225
226 /* Inputs to the vertex program stage */
227 GLvector4f *AttribPtr[_TNL_ATTRIB_MAX]; /* GL_NV_vertex_program */
228 };
229
230
231 /**
232 * Describes an individual operation on the pipeline.
233 */
234 struct tnl_pipeline_stage
235 {
236 const char *name;
237
238 /* Private data for the pipeline stage:
239 */
240 void *privatePtr;
241
242 /* Allocate private data
243 */
244 GLboolean (*create)( GLcontext *ctx, struct tnl_pipeline_stage * );
245
246 /* Free private data.
247 */
248 void (*destroy)( struct tnl_pipeline_stage * );
249
250 /* Called on any statechange or input array size change or
251 * input array change to/from zero stride.
252 */
253 void (*validate)( GLcontext *ctx, struct tnl_pipeline_stage * );
254
255 /* Called from _tnl_run_pipeline(). The stage.changed_inputs value
256 * encodes all inputs to thee struct which have changed. If
257 * non-zero, recompute all affected outputs of the stage, otherwise
258 * execute any 'sideeffects' of the stage.
259 *
260 * Return value: GL_TRUE - keep going
261 * GL_FALSE - finished pipeline
262 */
263 GLboolean (*run)( GLcontext *ctx, struct tnl_pipeline_stage * );
264 };
265
266
267
268 /** Contains the array of all pipeline stages.
269 * The default values are defined at the end of t_pipeline.c
270 */
271 struct tnl_pipeline {
272
273 GLuint last_attrib_stride[_TNL_ATTRIB_MAX];
274 GLuint last_attrib_size[_TNL_ATTRIB_MAX];
275 GLuint input_changes;
276 GLuint new_state;
277
278 struct tnl_pipeline_stage stages[MAX_PIPELINE_STAGES+1];
279 GLuint nr_stages;
280 };
281
282 struct tnl_clipspace;
283 struct tnl_clipspace_attr;
284
285 typedef void (*tnl_extract_func)( const struct tnl_clipspace_attr *a,
286 GLfloat *out,
287 const GLubyte *v );
288
289 typedef void (*tnl_insert_func)( const struct tnl_clipspace_attr *a,
290 GLubyte *v,
291 const GLfloat *in );
292
293 typedef void (*tnl_emit_func)( GLcontext *ctx,
294 GLuint count,
295 GLubyte *dest );
296
297
298 /**
299 * Describes how to convert/move a vertex attribute from a vertex array
300 * to a vertex structure.
301 */
302 struct tnl_clipspace_attr
303 {
304 GLuint attrib; /* which vertex attrib (0=position, etc) */
305 GLuint format;
306 GLuint vertoffset; /* position of the attrib in the vertex struct */
307 GLuint vertattrsize; /* size of the attribute in bytes */
308 GLubyte *inputptr;
309 GLuint inputstride;
310 GLuint inputsize;
311 const tnl_insert_func *insert;
312 tnl_insert_func emit;
313 tnl_extract_func extract;
314 const GLfloat *vp; /* NDC->Viewport mapping matrix */
315 };
316
317
318
319
320 typedef void (*tnl_points_func)( GLcontext *ctx, GLuint first, GLuint last );
321 typedef void (*tnl_line_func)( GLcontext *ctx, GLuint v1, GLuint v2 );
322 typedef void (*tnl_triangle_func)( GLcontext *ctx,
323 GLuint v1, GLuint v2, GLuint v3 );
324 typedef void (*tnl_quad_func)( GLcontext *ctx, GLuint v1, GLuint v2,
325 GLuint v3, GLuint v4 );
326 typedef void (*tnl_render_func)( GLcontext *ctx, GLuint start, GLuint count,
327 GLuint flags );
328 typedef void (*tnl_interp_func)( GLcontext *ctx,
329 GLfloat t, GLuint dst, GLuint out, GLuint in,
330 GLboolean force_boundary );
331 typedef void (*tnl_copy_pv_func)( GLcontext *ctx, GLuint dst, GLuint src );
332 typedef void (*tnl_setup_func)( GLcontext *ctx,
333 GLuint start, GLuint end,
334 GLuint new_inputs);
335
336
337 struct tnl_attr_type {
338 GLuint format;
339 GLuint size;
340 GLuint stride;
341 GLuint offset;
342 };
343
344 struct tnl_clipspace_fastpath {
345 GLuint vertex_size;
346 GLuint attr_count;
347 GLboolean match_strides;
348
349 struct tnl_attr_type *attr;
350
351 tnl_emit_func func;
352 struct tnl_clipspace_fastpath *next;
353 };
354
355 /**
356 * Used to describe conversion of vertex arrays to vertex structures.
357 * I.e. Structure of arrays to arrays of structs.
358 */
359 struct tnl_clipspace
360 {
361 GLboolean need_extras;
362
363 GLuint new_inputs;
364
365 GLubyte *vertex_buf;
366 GLuint vertex_size;
367 GLuint max_vertex_size;
368
369 struct tnl_clipspace_attr attr[_TNL_ATTRIB_MAX];
370 GLuint attr_count;
371
372 tnl_emit_func emit;
373 tnl_interp_func interp;
374 tnl_copy_pv_func copy_pv;
375
376 /* Parameters and constants for codegen:
377 */
378 GLboolean need_viewport;
379 GLfloat vp_scale[4];
380 GLfloat vp_xlate[4];
381 GLfloat chan_scale[4];
382 GLfloat identity[4];
383
384 struct tnl_clipspace_fastpath *fastpath;
385
386 void (*codegen_emit)( GLcontext *ctx );
387 };
388
389
390 struct tnl_device_driver
391 {
392 /***
393 *** TNL Pipeline
394 ***/
395
396 void (*RunPipeline)(GLcontext *ctx);
397 /* Replaces PipelineStart/PipelineFinish -- intended to allow
398 * drivers to wrap _tnl_run_pipeline() with code to validate state
399 * and grab/release hardware locks.
400 */
401
402 void (*NotifyMaterialChange)(GLcontext *ctx);
403 /* Alert tnl-aware drivers of changes to material.
404 */
405
406 void (*NotifyInputChanges)(GLcontext *ctx, GLuint bitmask);
407 /* Alert tnl-aware drivers of changes to size and stride of input
408 * arrays.
409 */
410
411 /***
412 *** Rendering -- These functions called only from t_vb_render.c
413 ***/
414 struct
415 {
416 void (*Start)(GLcontext *ctx);
417 void (*Finish)(GLcontext *ctx);
418 /* Called before and after all rendering operations, including DrawPixels,
419 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
420 * These are a suitable place for grabbing/releasing hardware locks.
421 */
422
423 void (*PrimitiveNotify)(GLcontext *ctx, GLenum mode);
424 /* Called between RenderStart() and RenderFinish() to indicate the
425 * type of primitive we're about to draw. Mode will be one of the
426 * modes accepted by glBegin().
427 */
428
429 tnl_interp_func Interp;
430 /* The interp function is called by the clipping routines when we need
431 * to generate an interpolated vertex. All pertinant vertex ancilliary
432 * data should be computed by interpolating between the 'in' and 'out'
433 * vertices.
434 */
435
436 tnl_copy_pv_func CopyPV;
437 /* The copy function is used to make a copy of a vertex. All pertinant
438 * vertex attributes should be copied.
439 */
440
441 void (*ClippedPolygon)( GLcontext *ctx, const GLuint *elts, GLuint n );
442 /* Render a polygon with <n> vertices whose indexes are in the <elts>
443 * array.
444 */
445
446 void (*ClippedLine)( GLcontext *ctx, GLuint v0, GLuint v1 );
447 /* Render a line between the two vertices given by indexes v0 and v1. */
448
449 tnl_points_func Points; /* must now respect vb->elts */
450 tnl_line_func Line;
451 tnl_triangle_func Triangle;
452 tnl_quad_func Quad;
453 /* These functions are called in order to render points, lines,
454 * triangles and quads. These are only called via the T&L module.
455 */
456
457 tnl_render_func *PrimTabVerts;
458 tnl_render_func *PrimTabElts;
459 /* Render whole unclipped primitives (points, lines, linestrips,
460 * lineloops, etc). The tables are indexed by the GL enum of the
461 * primitive to be rendered. RenderTabVerts is used for non-indexed
462 * arrays of vertices. RenderTabElts is used for indexed arrays of
463 * vertices.
464 */
465
466 void (*ResetLineStipple)( GLcontext *ctx );
467 /* Reset the hardware's line stipple counter.
468 */
469
470 tnl_setup_func BuildVertices;
471 /* This function is called whenever new vertices are required for
472 * rendering. The vertices in question are those n such that start
473 * <= n < end. The new_inputs parameter indicates those fields of
474 * the vertex which need to be updated, if only a partial repair of
475 * the vertex is required.
476 *
477 * This function is called only from _tnl_render_stage in tnl/t_render.c.
478 */
479
480
481 GLboolean (*Multipass)( GLcontext *ctx, GLuint passno );
482 /* Driver may request additional render passes by returning GL_TRUE
483 * when this function is called. This function will be called
484 * after the first pass, and passes will be made until the function
485 * returns GL_FALSE. If no function is registered, only one pass
486 * is made.
487 *
488 * This function will be first invoked with passno == 1.
489 */
490 } Render;
491 };
492
493
494 #define DECLARE_RENDERINPUTS(name) BITSET64_DECLARE(name, _TNL_ATTRIB_MAX)
495 #define RENDERINPUTS_COPY BITSET64_COPY
496 #define RENDERINPUTS_EQUAL BITSET64_EQUAL
497 #define RENDERINPUTS_ZERO BITSET64_ZERO
498 #define RENDERINPUTS_ONES BITSET64_ONES
499 #define RENDERINPUTS_TEST BITSET64_TEST
500 #define RENDERINPUTS_SET BITSET64_SET
501 #define RENDERINPUTS_CLEAR BITSET64_CLEAR
502 #define RENDERINPUTS_TEST_RANGE BITSET64_TEST_RANGE
503 #define RENDERINPUTS_SET_RANGE BITSET64_SET_RANGE
504 #define RENDERINPUTS_CLEAR_RANGE BITSET64_CLEAR_RANGE
505
506
507 /**
508 * Context state for T&L context.
509 */
510 typedef struct
511 {
512 /* Driver interface.
513 */
514 struct tnl_device_driver Driver;
515
516 /* Pipeline
517 */
518 struct tnl_pipeline pipeline;
519 struct vertex_buffer vb;
520
521 /* Clipspace/ndc/window vertex managment:
522 */
523 struct tnl_clipspace clipspace;
524
525 /* Probably need a better configuration mechanism:
526 */
527 GLboolean NeedNdcCoords;
528 GLboolean AllowVertexFog;
529 GLboolean AllowPixelFog;
530 GLboolean _DoVertexFog; /* eval fog function at each vertex? */
531
532 DECLARE_RENDERINPUTS(render_inputs_bitset);
533
534 GLvector4f tmp_inputs[VERT_ATTRIB_MAX];
535
536 /* Temp storage for t_draw.c:
537 */
538 GLubyte *block[VERT_ATTRIB_MAX];
539 GLuint nr_blocks;
540
541 } TNLcontext;
542
543
544
545 #define TNL_CONTEXT(ctx) ((TNLcontext *)((ctx)->swtnl_context))
546
547
548 #define TYPE_IDX(t) ((t) & 0xf)
549 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
550
551
552 extern void
553 tnl_clip_prepare(GLcontext *ctx);
554
555
556 #endif