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