2 * mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * \brief TnL module datatypes and definitions.
28 * \author Keith Whitwell
33 * \mainpage The TNL-module
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.
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.
46 * The initial vertex_buffer data may either come from an ::immediate
47 * structure or client vertex_arrays or display lists:
50 * - The ::immediate structure records all the GL commands issued between
51 * glBegin and glEnd. \n
52 * The structure accumulates data, until it is either full or it is
53 * flushed (usually by a state change). Before starting then the pipeline,
54 * the collected vertex data in ::immediate has to be pushed into
56 * This happens in ::_tnl_vb_bind_immediate. The pipeline is then run by
57 * calling tnl_device_driver::RunPipeline = ::_tnl_run_pipeline, which
58 * is stored in TNLcontext::Driver. \n
59 * An ::immediate does (for performance reasons) usually not finish with a
60 * glEnd, and hence it also does not need to start with a glBegin.
61 * This means that the last vertices of one ::immediate may need to be
62 * saved for the next one.
65 * - NOT SURE ABOUT THIS: The vertex_arrays structure is used to handle
66 * glDrawArrays etc. \n
67 * Here, the data of the vertex_arrays is copied by ::_tnl_vb_bind_arrays
68 * into TNLcontext::vb, so that the pipeline can be started.
78 #include "math/m_matrix.h"
79 #include "math/m_vector.h"
80 #include "math/m_xform.h"
83 #define MAX_PIPELINE_STAGES 30
87 * Note: The first attributes match the VERT_ATTRIB_* definitions
88 * in mtypes.h. However, the tnl module has additional attributes
89 * for materials, color indexes, edge flags, etc.
91 /* Note: These are currently being used to define both inputs and
92 * outputs from the tnl pipeline. A better solution (which would also
93 * releive the congestion to slightly prolong the life of the bitmask
94 * below) is to have the fixed function pipeline populate a set of
95 * arrays named after those produced by the vertex program stage, and
96 * have the rest the mesa backend work on those.
100 _TNL_ATTRIB_WEIGHT
= 1,
101 _TNL_ATTRIB_NORMAL
= 2,
102 _TNL_ATTRIB_COLOR0
= 3,
103 _TNL_ATTRIB_COLOR1
= 4,
106 _TNL_ATTRIB_SEVEN
= 7,
107 _TNL_ATTRIB_TEX0
= 8,
108 _TNL_ATTRIB_TEX1
= 9,
109 _TNL_ATTRIB_TEX2
= 10,
110 _TNL_ATTRIB_TEX3
= 11,
111 _TNL_ATTRIB_TEX4
= 12,
112 _TNL_ATTRIB_TEX5
= 13,
113 _TNL_ATTRIB_TEX6
= 14,
114 _TNL_ATTRIB_TEX7
= 15,
115 _TNL_ATTRIB_MAT_FRONT_AMBIENT
= 16,
116 _TNL_ATTRIB_MAT_BACK_AMBIENT
= 17,
117 _TNL_ATTRIB_MAT_FRONT_DIFFUSE
= 18,
118 _TNL_ATTRIB_MAT_BACK_DIFFUSE
= 19,
119 _TNL_ATTRIB_MAT_FRONT_SPECULAR
= 20,
120 _TNL_ATTRIB_MAT_BACK_SPECULAR
= 21,
121 _TNL_ATTRIB_MAT_FRONT_EMISSION
= 22,
122 _TNL_ATTRIB_MAT_BACK_EMISSION
= 23,
123 _TNL_ATTRIB_MAT_FRONT_SHININESS
= 24,
124 _TNL_ATTRIB_MAT_BACK_SHININESS
= 25,
125 _TNL_ATTRIB_MAT_FRONT_INDEXES
= 26,
126 _TNL_ATTRIB_MAT_BACK_INDEXES
= 27,
127 _TNL_ATTRIB_INDEX
= 28,
128 _TNL_ATTRIB_EDGEFLAG
= 29,
129 _TNL_ATTRIB_POINTSIZE
= 30,
133 /* Will probably have to revise this scheme fairly shortly, eg. by
134 * compacting all the MAT flags down to one bit, or by using two
135 * dwords to store the flags.
137 #define _TNL_BIT_POS (1<<0)
138 #define _TNL_BIT_WEIGHT (1<<1)
139 #define _TNL_BIT_NORMAL (1<<2)
140 #define _TNL_BIT_COLOR0 (1<<3)
141 #define _TNL_BIT_COLOR1 (1<<4)
142 #define _TNL_BIT_FOG (1<<5)
143 #define _TNL_BIT_SIX (1<<6)
144 #define _TNL_BIT_SEVEN (1<<7)
145 #define _TNL_BIT_TEX0 (1<<8)
146 #define _TNL_BIT_TEX1 (1<<9)
147 #define _TNL_BIT_TEX2 (1<<10)
148 #define _TNL_BIT_TEX3 (1<<11)
149 #define _TNL_BIT_TEX4 (1<<12)
150 #define _TNL_BIT_TEX5 (1<<13)
151 #define _TNL_BIT_TEX6 (1<<14)
152 #define _TNL_BIT_TEX7 (1<<15)
153 #define _TNL_BIT_MAT_FRONT_AMBIENT (1<<16)
154 #define _TNL_BIT_MAT_BACK_AMBIENT (1<<17)
155 #define _TNL_BIT_MAT_FRONT_DIFFUSE (1<<18)
156 #define _TNL_BIT_MAT_BACK_DIFFUSE (1<<19)
157 #define _TNL_BIT_MAT_FRONT_SPECULAR (1<<20)
158 #define _TNL_BIT_MAT_BACK_SPECULAR (1<<21)
159 #define _TNL_BIT_MAT_FRONT_EMISSION (1<<22)
160 #define _TNL_BIT_MAT_BACK_EMISSION (1<<23)
161 #define _TNL_BIT_MAT_FRONT_SHININESS (1<<24)
162 #define _TNL_BIT_MAT_BACK_SHININESS (1<<25)
163 #define _TNL_BIT_MAT_FRONT_INDEXES (1<<26)
164 #define _TNL_BIT_MAT_BACK_INDEXES (1<<27)
165 #define _TNL_BIT_INDEX (1<<28)
166 #define _TNL_BIT_EDGEFLAG (1<<29)
167 #define _TNL_BIT_POINTSIZE (1<<30)
169 #define _TNL_BIT_TEX(u) (1 << (_TNL_ATTRIB_TEX0 + (u)))
173 #define _TNL_BITS_MAT_ANY (_TNL_BIT_MAT_FRONT_AMBIENT | \
174 _TNL_BIT_MAT_BACK_AMBIENT | \
175 _TNL_BIT_MAT_FRONT_DIFFUSE | \
176 _TNL_BIT_MAT_BACK_DIFFUSE | \
177 _TNL_BIT_MAT_FRONT_SPECULAR | \
178 _TNL_BIT_MAT_BACK_SPECULAR | \
179 _TNL_BIT_MAT_FRONT_EMISSION | \
180 _TNL_BIT_MAT_BACK_EMISSION | \
181 _TNL_BIT_MAT_FRONT_SHININESS | \
182 _TNL_BIT_MAT_BACK_SHININESS | \
183 _TNL_BIT_MAT_FRONT_INDEXES | \
184 _TNL_BIT_MAT_BACK_INDEXES)
187 #define _TNL_BITS_TEX_ANY (_TNL_BIT_TEX0 | \
197 #define _TNL_BITS_PROG_ANY (_TNL_BIT_POS | \
209 #define PRIM_BEGIN 0x10
210 #define PRIM_END 0x20
211 #define PRIM_WEAK 0x40
212 #define PRIM_MODE_MASK 0x0f
224 struct tnl_eval1_map
{
225 struct gl_1d_map
*map
;
229 struct tnl_eval2_map
{
230 struct gl_2d_map
*map
;
236 struct tnl_eval1_map map1
[_TNL_ATTRIB_INDEX
+ 1];
237 struct tnl_eval2_map map2
[_TNL_ATTRIB_INDEX
+ 1];
241 #define TNL_MAX_PRIM 16
242 #define TNL_MAX_COPIED_VERTS 3
244 struct tnl_copied_vtx
{
245 GLfloat buffer
[_TNL_ATTRIB_MAX
* 4 * TNL_MAX_COPIED_VERTS
];
249 #define VERT_BUFFER_SIZE 2048 /* 8kbytes */
251 typedef void (*attrfv_func
)( const GLfloat
* );
253 /* The assembly of vertices in immediate mode is separated from
254 * display list compilation. This allows a simpler immediate mode
255 * treatment and a display list compiler better suited to
256 * hardware-acceleration.
259 GLfloat buffer
[VERT_BUFFER_SIZE
];
260 GLubyte attrsz
[_TNL_ATTRIB_MAX
];
262 struct tnl_prim prim
[TNL_MAX_PRIM
];
264 GLfloat
*vbptr
; /* cursor, points into buffer */
265 GLfloat vertex
[_TNL_ATTRIB_MAX
*4]; /* current vertex */
266 GLfloat
*attrptr
[_TNL_ATTRIB_MAX
]; /* points into vertex */
267 GLfloat
*current
[_TNL_ATTRIB_MAX
]; /* points into ctx->Current, etc */
268 GLuint counter
, initial_counter
;
269 struct tnl_copied_vtx copied
;
270 attrfv_func tabfv
[_TNL_ATTRIB_MAX
][4];
271 struct tnl_eval eval
;
272 GLboolean
*edgeflag_tmp
;
273 GLboolean have_materials
;
279 /* For display lists, this structure holds a run of vertices of the
280 * same format, and a strictly well-formed set of begin/end pairs,
281 * starting on the first vertex and ending at the last. Vertex
282 * copying on buffer breaks is precomputed according to these
283 * primitives, though there are situations where the copying will need
284 * correction at execute-time, perhaps by replaying the list as
285 * immediate mode commands.
287 * On executing this list, the 'current' values may be updated with
288 * the values of the final vertex, and often no fixup of the start of
289 * the vertex list is required.
291 * Eval and other commands that don't fit into these vertex lists are
292 * compiled using the fallback opcode mechanism provided by dlist.c.
294 struct tnl_vertex_list
{
295 GLubyte attrsz
[_TNL_ATTRIB_MAX
];
300 GLuint wrap_count
; /* number of copied vertices at start */
301 GLboolean have_materials
; /* bit of a hack - quick check for materials */
302 GLboolean dangling_attr_ref
; /* current attr implicitly referenced
305 GLfloat
*normal_lengths
;
306 struct tnl_prim
*prim
;
309 struct tnl_vertex_store
*vertex_store
;
310 struct tnl_primitive_store
*prim_store
;
313 /* These buffers should be a reasonable size to support upload to
314 * hardware? Maybe drivers should stitch them back together, or
315 * specify a desired size?
317 #define SAVE_BUFFER_SIZE (16*1024)
318 #define SAVE_PRIM_SIZE 128
320 /* Storage to be shared among several vertex_lists.
322 struct tnl_vertex_store
{
323 GLfloat buffer
[SAVE_BUFFER_SIZE
];
328 struct tnl_primitive_store
{
329 struct tnl_prim buffer
[SAVE_PRIM_SIZE
];
336 GLubyte attrsz
[_TNL_ATTRIB_MAX
];
343 struct tnl_prim
*prim
;
344 GLuint prim_count
, prim_max
;
346 struct tnl_vertex_store
*vertex_store
;
347 struct tnl_primitive_store
*prim_store
;
349 GLfloat
*vbptr
; /* cursor, points into buffer */
350 GLfloat vertex
[_TNL_ATTRIB_MAX
*4]; /* current values */
351 GLfloat
*attrptr
[_TNL_ATTRIB_MAX
];
352 GLuint counter
, initial_counter
;
353 GLboolean dangling_attr_ref
;
354 GLboolean have_materials
;
356 GLuint opcode_vertex_list
;
358 struct tnl_copied_vtx copied
;
360 GLfloat
*current
[_TNL_ATTRIB_MAX
]; /* points into ctx->ListState */
361 GLubyte
*currentsz
[_TNL_ATTRIB_MAX
];
363 void (*tabfv
[_TNL_ATTRIB_MAX
][4])( const GLfloat
* );
372 struct tnl_vertex_arrays
374 /* Conventional vertex attribute arrays */
378 GLvector4f SecondaryColor
;
380 GLvector4f TexCoord
[MAX_TEXTURE_COORD_UNITS
];
386 /* These attributes don't alias with the conventional attributes.
387 * The GL_NV_vertex_program extension defines 16 extra sets of vertex
388 * arrays which have precedent over the conventional arrays when enabled.
390 GLvector4f Attribs
[_TNL_ATTRIB_MAX
];
395 * Contains the current state of a running pipeline.
399 /* Constant over life of the vertex_buffer.
403 /* Constant over the pipeline.
405 GLuint Count
; /* for everything except Elts */
407 /* Pointers to current data.
410 GLvector4f
*ObjPtr
; /* _TNL_BIT_POS */
411 GLvector4f
*EyePtr
; /* _TNL_BIT_POS */
412 GLvector4f
*ClipPtr
; /* _TNL_BIT_POS */
413 GLvector4f
*NdcPtr
; /* _TNL_BIT_POS */
414 GLubyte ClipOrMask
; /* _TNL_BIT_POS */
415 GLubyte
*ClipMask
; /* _TNL_BIT_POS */
416 GLvector4f
*NormalPtr
; /* _TNL_BIT_NORMAL */
417 GLfloat
*NormalLengthPtr
; /* _TNL_BIT_NORMAL */
418 GLboolean
*EdgeFlag
; /* _TNL_BIT_EDGEFLAG */
419 GLvector4f
*TexCoordPtr
[MAX_TEXTURE_COORD_UNITS
]; /* VERT_TEX_0..n */
420 GLvector4f
*IndexPtr
[2]; /* _TNL_BIT_INDEX */
421 GLvector4f
*ColorPtr
[2]; /* _TNL_BIT_COLOR0 */
422 GLvector4f
*SecondaryColorPtr
[2]; /* _TNL_BIT_COLOR1 */
423 GLvector4f
*PointSizePtr
; /* _TNL_BIT_POS */
424 GLvector4f
*FogCoordPtr
; /* _TNL_BIT_FOG */
426 struct tnl_prim
*Primitive
;
427 GLuint PrimitiveCount
;
429 /* Inputs to the vertex program stage */
430 GLvector4f
*AttribPtr
[_TNL_ATTRIB_MAX
]; /* GL_NV_vertex_program */
433 /* Private data from _tnl_render_stage that has no business being
441 /** Describes an individual operation on the pipeline.
443 struct tnl_pipeline_stage
{
445 GLuint check_state
; /* All state referenced in check() --
446 * When is the pipeline_stage struct
447 * itself invalidated? Must be
451 /* Usually constant or set by the 'check' callback:
453 GLuint run_state
; /* All state referenced in run() --
454 * When is the cached output of the
458 GLboolean active
; /* True if runnable in current state */
459 GLuint inputs
; /* VERT_* inputs to the stage */
460 GLuint outputs
; /* VERT_* outputs of the stage */
462 /* Set in _tnl_run_pipeline():
464 GLuint changed_inputs
; /* Generated value -- inputs to the
465 * stage that have changed since last
470 /* Private data for the pipeline stage:
474 /* Free private data. May not be null.
476 void (*destroy
)( struct tnl_pipeline_stage
* );
478 /* Called from _tnl_validate_pipeline(). Must update all fields in
479 * the pipeline_stage struct for the current state.
481 void (*check
)( GLcontext
*ctx
, struct tnl_pipeline_stage
* );
483 /* Called from _tnl_run_pipeline(). The stage.changed_inputs value
484 * encodes all inputs to thee struct which have changed. If
485 * non-zero, recompute all affected outputs of the stage, otherwise
486 * execute any 'sideeffects' of the stage.
488 * Return value: GL_TRUE - keep going
489 * GL_FALSE - finished pipeline
491 GLboolean (*run
)( GLcontext
*ctx
, struct tnl_pipeline_stage
* );
494 /** Contains the array of all pipeline stages.
495 * The default values are defined at the end of t_pipeline.c */
496 struct tnl_pipeline
{
497 GLuint build_state_trigger
; /**< state changes which require build */
498 GLuint build_state_changes
; /**< state changes since last build */
499 GLuint run_state_changes
; /**< state changes since last run */
500 GLuint run_input_changes
; /**< VERT_* changes since last run */
501 GLuint inputs
; /**< VERT_* inputs to pipeline */
502 /** This array has to end with a NULL-pointer. */
503 struct tnl_pipeline_stage stages
[MAX_PIPELINE_STAGES
+1];
507 struct tnl_clipspace_attr
;
509 typedef void (*extract_func
)( const struct tnl_clipspace_attr
*a
, GLfloat
*out
,
512 typedef void (*insert_func
)( const struct tnl_clipspace_attr
*a
, GLubyte
*v
,
516 struct tnl_clipspace_attr
{
524 extract_func extract
;
530 typedef void (*points_func
)( GLcontext
*ctx
, GLuint first
, GLuint last
);
531 typedef void (*line_func
)( GLcontext
*ctx
, GLuint v1
, GLuint v2
);
532 typedef void (*triangle_func
)( GLcontext
*ctx
,
533 GLuint v1
, GLuint v2
, GLuint v3
);
534 typedef void (*quad_func
)( GLcontext
*ctx
, GLuint v1
, GLuint v2
,
535 GLuint v3
, GLuint v4
);
536 typedef void (*render_func
)( GLcontext
*ctx
, GLuint start
, GLuint count
,
538 typedef void (*interp_func
)( GLcontext
*ctx
,
539 GLfloat t
, GLuint dst
, GLuint out
, GLuint in
,
540 GLboolean force_boundary
);
541 typedef void (*copy_pv_func
)( GLcontext
*ctx
, GLuint dst
, GLuint src
);
542 typedef void (*setup_func
)( GLcontext
*ctx
,
543 GLuint start
, GLuint end
,
549 struct tnl_clipspace
{
550 GLboolean need_extras
;
556 GLuint max_vertex_size
;
558 struct tnl_clipspace_attr attr
[_TNL_ATTRIB_MAX
];
561 void (*emit
)( GLcontext
*ctx
, GLuint start
, GLuint end
, void *dest
);
563 copy_pv_func copy_pv
;
567 struct tnl_device_driver
{
572 void (*RunPipeline
)(GLcontext
*ctx
);
573 /* Replaces PipelineStart/PipelineFinish -- intended to allow
574 * drivers to wrap _tnl_run_pipeline() with code to validate state
575 * and grab/release hardware locks.
578 void (*NotifyMaterialChange
)(GLcontext
*ctx
);
579 /* Alert tnl-aware drivers of changes to material.
582 GLboolean (*NotifyBegin
)(GLcontext
*ctx
, GLenum p
);
583 /* Allow drivers to hook in optimized begin/end engines.
584 * Return value: GL_TRUE - driver handled the begin
585 * GL_FALSE - driver didn't handle the begin
589 *** Rendering -- These functions called only from t_vb_render.c
592 void (*Start
)(GLcontext
*ctx
);
593 void (*Finish
)(GLcontext
*ctx
);
594 /* Called before and after all rendering operations, including DrawPixels,
595 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
596 * These are a suitable place for grabbing/releasing hardware locks.
599 void (*PrimitiveNotify
)(GLcontext
*ctx
, GLenum mode
);
600 /* Called between RenderStart() and RenderFinish() to indicate the
601 * type of primitive we're about to draw. Mode will be one of the
602 * modes accepted by glBegin().
606 /* The interp function is called by the clipping routines when we need
607 * to generate an interpolated vertex. All pertinant vertex ancilliary
608 * data should be computed by interpolating between the 'in' and 'out'
613 /* The copy function is used to make a copy of a vertex. All pertinant
614 * vertex attributes should be copied.
617 void (*ClippedPolygon
)( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
);
618 /* Render a polygon with <n> vertices whose indexes are in the <elts>
622 void (*ClippedLine
)( GLcontext
*ctx
, GLuint v0
, GLuint v1
);
623 /* Render a line between the two vertices given by indexes v0 and v1. */
625 points_func Points
; /* must now respect vb->elts */
627 triangle_func Triangle
;
629 /* These functions are called in order to render points, lines,
630 * triangles and quads. These are only called via the T&L module.
633 render_func
*PrimTabVerts
;
634 render_func
*PrimTabElts
;
635 /* Render whole unclipped primitives (points, lines, linestrips,
636 * lineloops, etc). The tables are indexed by the GL enum of the
637 * primitive to be rendered. RenderTabVerts is used for non-indexed
638 * arrays of vertices. RenderTabElts is used for indexed arrays of
642 void (*ResetLineStipple
)( GLcontext
*ctx
);
643 /* Reset the hardware's line stipple counter.
646 setup_func BuildVertices
;
647 /* This function is called whenever new vertices are required for
648 * rendering. The vertices in question are those n such that start
649 * <= n < end. The new_inputs parameter indicates those fields of
650 * the vertex which need to be updated, if only a partial repair of
651 * the vertex is required.
653 * This function is called only from _tnl_render_stage in tnl/t_render.c.
657 GLboolean (*Multipass
)( GLcontext
*ctx
, GLuint passno
);
658 /* Driver may request additional render passes by returning GL_TRUE
659 * when this function is called. This function will be called
660 * after the first pass, and passes will be made until the function
661 * returns GL_FALSE. If no function is registered, only one pass
664 * This function will be first invoked with passno == 1.
674 struct tnl_device_driver Driver
;
682 struct tnl_save save
;
686 struct tnl_pipeline pipeline
;
687 struct vertex_buffer vb
;
689 /* GLvectors for binding to vb:
691 struct tnl_vertex_arrays vtx_inputs
;
692 struct tnl_vertex_arrays save_inputs
;
693 struct tnl_vertex_arrays current
;
694 struct tnl_vertex_arrays array_inputs
;
697 /* Clipspace/ndc/window vertex managment:
699 struct tnl_clipspace clipspace
;
702 /* Probably need a better configuration mechanism:
704 GLboolean NeedNdcCoords
;
705 GLboolean LoopbackDListCassettes
;
706 GLboolean CalcDListNormalLengths
;
707 GLboolean IsolateMaterials
;
711 GLuint render_inputs
;
714 GLvertexformat exec_vtxfmt
;
715 GLvertexformat save_vtxfmt
;
721 #define TNL_CONTEXT(ctx) ((TNLcontext *)(ctx->swtnl_context))
724 #define TYPE_IDX(t) ((t) & 0xf)
725 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
727 extern void _tnl_MakeCurrent( GLcontext
*ctx
,
728 GLframebuffer
*drawBuffer
,
729 GLframebuffer
*readBuffer
);