2 * mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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.
52 #include "main/glheader.h"
54 #include "main/mtypes.h"
56 #include "math/m_vector.h"
62 #define MAX_PIPELINE_STAGES 30
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.
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.
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*
86 _TNL_ATTRIB_COLOR_INDEX
,
97 /* This is really a VARYING_SLOT, not an attrib. Need to fix
98 * tnl to understand the difference.
100 _TNL_ATTRIB_POINTSIZE
,
102 _TNL_ATTRIB_GENERIC0
, /* doesn't really exist! */
103 _TNL_ATTRIB_GENERIC1
,
104 _TNL_ATTRIB_GENERIC2
,
105 _TNL_ATTRIB_GENERIC3
,
106 _TNL_ATTRIB_GENERIC4
,
107 _TNL_ATTRIB_GENERIC5
,
108 _TNL_ATTRIB_GENERIC6
,
109 _TNL_ATTRIB_GENERIC7
,
110 _TNL_ATTRIB_GENERIC8
,
111 _TNL_ATTRIB_GENERIC9
,
112 _TNL_ATTRIB_GENERIC10
,
113 _TNL_ATTRIB_GENERIC11
,
114 _TNL_ATTRIB_GENERIC12
,
115 _TNL_ATTRIB_GENERIC13
,
116 _TNL_ATTRIB_GENERIC14
,
117 _TNL_ATTRIB_GENERIC15
,
121 /* These alias with the generics, but they are not active
122 * concurrently, so it's not a problem. The TNL module
123 * doesn't have to do anything about this as this is how they
124 * are passed into the _draw_prims callback.
126 * When we generate fixed-function replacement programs (in
127 * t_vp_build.c currently), they refer to the appropriate
128 * generic attribute in order to pick up per-vertex material
131 _TNL_ATTRIB_MAT_FRONT_AMBIENT
=VERT_ATTRIB_MAT(MAT_ATTRIB_FRONT_AMBIENT
),
132 _TNL_ATTRIB_MAT_BACK_AMBIENT
,
133 _TNL_ATTRIB_MAT_FRONT_DIFFUSE
,
134 _TNL_ATTRIB_MAT_BACK_DIFFUSE
,
135 _TNL_ATTRIB_MAT_FRONT_SPECULAR
,
136 _TNL_ATTRIB_MAT_BACK_SPECULAR
,
137 _TNL_ATTRIB_MAT_FRONT_EMISSION
,
138 _TNL_ATTRIB_MAT_BACK_EMISSION
,
139 _TNL_ATTRIB_MAT_FRONT_SHININESS
,
140 _TNL_ATTRIB_MAT_BACK_SHININESS
,
141 _TNL_ATTRIB_MAT_FRONT_INDEXES
,
142 _TNL_ATTRIB_MAT_BACK_INDEXES
,
145 #define _TNL_ATTRIB_TEX(u) (_TNL_ATTRIB_TEX0 + (u))
146 #define _TNL_ATTRIB_GENERIC(n) (_TNL_ATTRIB_GENERIC0 + (n))
148 /* special index used for handing invalid glVertexAttribute() indices */
149 #define _TNL_ATTRIB_ERROR (_TNL_ATTRIB_GENERIC15 + 1)
152 * Handy attribute ranges:
154 #define _TNL_FIRST_PROG _TNL_ATTRIB_NORMAL
155 #define _TNL_LAST_PROG _TNL_ATTRIB_TEX7
157 #define _TNL_FIRST_TEX _TNL_ATTRIB_TEX0
158 #define _TNL_LAST_TEX _TNL_ATTRIB_TEX7
160 #define _TNL_FIRST_GENERIC _TNL_ATTRIB_GENERIC0
161 #define _TNL_LAST_GENERIC _TNL_ATTRIB_GENERIC15
163 #define _TNL_FIRST_MAT _TNL_ATTRIB_MAT_FRONT_AMBIENT /* GENERIC4 */
164 #define _TNL_LAST_MAT _TNL_ATTRIB_MAT_BACK_INDEXES /* GENERIC15 */
166 /* Number of available texture attributes */
167 #define _TNL_NUM_TEX 8
169 /* Number of available generic attributes */
170 #define _TNL_NUM_GENERIC 16
172 /* Number of attributes used for evaluators */
173 #define _TNL_NUM_EVAL 16
176 #define PRIM_BEGIN 0x10
177 #define PRIM_END 0x20
178 #define PRIM_MODE_MASK 0x0f
180 static inline GLuint
_tnl_translate_prim( const struct _mesa_prim
*prim
)
184 if (prim
->begin
) flag
|= PRIM_BEGIN
;
185 if (prim
->end
) flag
|= PRIM_END
;
193 * Contains the current state of a running pipeline.
197 GLuint Size
; /**< Max vertices per vertex buffer, constant */
199 /* Constant over the pipeline.
201 GLuint Count
; /**< Number of vertices currently in buffer */
203 /* Pointers to current data. Most of the data is in AttribPtr -- all of
204 * it that is one of VERT_ATTRIB_X. For things only produced by TNL,
205 * such as backface color or eye-space coordinates, they are stored
209 GLvector4f
*EyePtr
; /* _TNL_BIT_POS */
210 GLvector4f
*ClipPtr
; /* _TNL_BIT_POS */
211 GLvector4f
*NdcPtr
; /* _TNL_BIT_POS */
212 GLubyte ClipOrMask
; /* _TNL_BIT_POS */
213 GLubyte ClipAndMask
; /* _TNL_BIT_POS */
214 GLubyte
*ClipMask
; /* _TNL_BIT_POS */
215 GLfloat
*NormalLengthPtr
; /* _TNL_BIT_NORMAL */
216 GLboolean
*EdgeFlag
; /* _TNL_BIT_EDGEFLAG */
217 GLvector4f
*BackfaceIndexPtr
;
218 GLvector4f
*BackfaceColorPtr
;
219 GLvector4f
*BackfaceSecondaryColorPtr
;
221 const struct _mesa_prim
*Primitive
;
222 GLuint PrimitiveCount
;
224 /* Inputs to the vertex program stage */
225 GLvector4f
*AttribPtr
[_TNL_ATTRIB_MAX
];
230 * Describes an individual operation on the pipeline.
232 struct tnl_pipeline_stage
236 /* Private data for the pipeline stage:
240 /* Allocate private data
242 GLboolean (*create
)( struct gl_context
*ctx
, struct tnl_pipeline_stage
* );
244 /* Free private data.
246 void (*destroy
)( struct tnl_pipeline_stage
* );
248 /* Called on any statechange or input array size change or
249 * input array change to/from zero stride.
251 void (*validate
)( struct gl_context
*ctx
, struct tnl_pipeline_stage
* );
253 /* Called from _tnl_run_pipeline(). The stage.changed_inputs value
254 * encodes all inputs to thee struct which have changed. If
255 * non-zero, recompute all affected outputs of the stage, otherwise
256 * execute any 'sideeffects' of the stage.
258 * Return value: GL_TRUE - keep going
259 * GL_FALSE - finished pipeline
261 GLboolean (*run
)( struct gl_context
*ctx
, struct tnl_pipeline_stage
* );
266 /** Contains the array of all pipeline stages.
267 * The default values are defined at the end of t_pipeline.c
269 struct tnl_pipeline
{
271 GLuint last_attrib_stride
[_TNL_ATTRIB_MAX
];
272 GLuint last_attrib_size
[_TNL_ATTRIB_MAX
];
273 GLuint input_changes
;
276 struct tnl_pipeline_stage stages
[MAX_PIPELINE_STAGES
+1];
280 struct tnl_clipspace
;
281 struct tnl_clipspace_attr
;
283 typedef void (*tnl_extract_func
)( const struct tnl_clipspace_attr
*a
,
287 typedef void (*tnl_insert_func
)( const struct tnl_clipspace_attr
*a
,
291 typedef void (*tnl_emit_func
)( struct gl_context
*ctx
,
297 * Describes how to convert/move a vertex attribute from a vertex array
298 * to a vertex structure.
300 struct tnl_clipspace_attr
302 GLuint attrib
; /* which vertex attrib (0=position, etc) */
304 GLuint vertoffset
; /* position of the attrib in the vertex struct */
305 GLuint vertattrsize
; /* size of the attribute in bytes */
309 const tnl_insert_func
*insert
;
310 tnl_insert_func emit
;
311 tnl_extract_func extract
;
312 const GLfloat
*vp
; /* NDC->Viewport mapping matrix */
318 typedef void (*tnl_points_func
)( struct gl_context
*ctx
, GLuint first
, GLuint last
);
319 typedef void (*tnl_line_func
)( struct gl_context
*ctx
, GLuint v1
, GLuint v2
);
320 typedef void (*tnl_triangle_func
)( struct gl_context
*ctx
,
321 GLuint v1
, GLuint v2
, GLuint v3
);
322 typedef void (*tnl_quad_func
)( struct gl_context
*ctx
, GLuint v1
, GLuint v2
,
323 GLuint v3
, GLuint v4
);
324 typedef void (*tnl_render_func
)( struct gl_context
*ctx
, GLuint start
, GLuint count
,
326 typedef void (*tnl_interp_func
)( struct gl_context
*ctx
,
327 GLfloat t
, GLuint dst
, GLuint out
, GLuint in
,
328 GLboolean force_boundary
);
329 typedef void (*tnl_copy_pv_func
)( struct gl_context
*ctx
, GLuint dst
, GLuint src
);
330 typedef void (*tnl_setup_func
)( struct gl_context
*ctx
,
331 GLuint start
, GLuint end
,
335 struct tnl_attr_type
{
342 struct tnl_clipspace_fastpath
{
345 GLboolean match_strides
;
347 struct tnl_attr_type
*attr
;
350 struct tnl_clipspace_fastpath
*next
;
354 * Used to describe conversion of vertex arrays to vertex structures.
355 * I.e. Structure of arrays to arrays of structs.
359 GLboolean need_extras
;
365 GLuint max_vertex_size
;
367 struct tnl_clipspace_attr attr
[_TNL_ATTRIB_MAX
];
371 tnl_interp_func interp
;
372 tnl_copy_pv_func copy_pv
;
374 /* Parameters and constants for codegen:
376 GLboolean need_viewport
;
379 GLfloat chan_scale
[4];
382 struct tnl_clipspace_fastpath
*fastpath
;
384 void (*codegen_emit
)( struct gl_context
*ctx
);
388 #define SHINE_TABLE_SIZE 256 /**< Material shininess lookup table sizes */
391 * Material shininess lookup table.
395 struct tnl_shine_tab
*next
, *prev
;
396 GLfloat tab
[SHINE_TABLE_SIZE
+1];
402 struct tnl_device_driver
408 void (*RunPipeline
)(struct gl_context
*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.
414 void (*NotifyMaterialChange
)(struct gl_context
*ctx
);
415 /* Alert tnl-aware drivers of changes to material.
419 *** Rendering -- These functions called only from t_vb_render.c
423 void (*Start
)(struct gl_context
*ctx
);
424 void (*Finish
)(struct gl_context
*ctx
);
425 /* Called before and after all rendering operations, including DrawPixels,
426 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
427 * These are a suitable place for grabbing/releasing hardware locks.
430 void (*PrimitiveNotify
)(struct gl_context
*ctx
, GLenum mode
);
431 /* Called between RenderStart() and RenderFinish() to indicate the
432 * type of primitive we're about to draw. Mode will be one of the
433 * modes accepted by glBegin().
436 tnl_interp_func Interp
;
437 /* The interp function is called by the clipping routines when we need
438 * to generate an interpolated vertex. All pertinant vertex ancilliary
439 * data should be computed by interpolating between the 'in' and 'out'
443 tnl_copy_pv_func CopyPV
;
444 /* The copy function is used to make a copy of a vertex. All pertinant
445 * vertex attributes should be copied.
448 void (*ClippedPolygon
)( struct gl_context
*ctx
, const GLuint
*elts
, GLuint n
);
449 /* Render a polygon with <n> vertices whose indexes are in the <elts>
453 void (*ClippedLine
)( struct gl_context
*ctx
, GLuint v0
, GLuint v1
);
454 /* Render a line between the two vertices given by indexes v0 and v1. */
456 tnl_points_func Points
; /* must now respect vb->elts */
458 tnl_triangle_func Triangle
;
460 /* These functions are called in order to render points, lines,
461 * triangles and quads. These are only called via the T&L module.
464 tnl_render_func
*PrimTabVerts
;
465 tnl_render_func
*PrimTabElts
;
466 /* Render whole unclipped primitives (points, lines, linestrips,
467 * lineloops, etc). The tables are indexed by the GL enum of the
468 * primitive to be rendered. RenderTabVerts is used for non-indexed
469 * arrays of vertices. RenderTabElts is used for indexed arrays of
473 void (*ResetLineStipple
)( struct gl_context
*ctx
);
474 /* Reset the hardware's line stipple counter.
477 tnl_setup_func BuildVertices
;
478 /* This function is called whenever new vertices are required for
479 * rendering. The vertices in question are those n such that start
480 * <= n < end. The new_inputs parameter indicates those fields of
481 * the vertex which need to be updated, if only a partial repair of
482 * the vertex is required.
484 * This function is called only from _tnl_render_stage in tnl/t_render.c.
488 GLboolean (*Multipass
)( struct gl_context
*ctx
, GLuint passno
);
489 /* Driver may request additional render passes by returning GL_TRUE
490 * when this function is called. This function will be called
491 * after the first pass, and passes will be made until the function
492 * returns GL_FALSE. If no function is registered, only one pass
495 * This function will be first invoked with passno == 1.
502 * Utility that tracks and updates the current array entries.
507 * Array of inputs to be set to the _DrawArrays pointer.
508 * The array contains pointers into the _DrawVAO and to the vbo modules
509 * current values. The array of pointers is updated incrementally
510 * based on the current and vertex_processing_mode values below.
512 struct tnl_vertex_array inputs
[VERT_ATTRIB_MAX
];
513 /** Those VERT_BIT_'s where the inputs array point to current values. */
515 /** Store which aliasing current values - generics or materials - are set. */
516 gl_vertex_processing_mode vertex_processing_mode
;
524 _tnl_init_inputs(struct tnl_inputs
*inputs
);
528 * Update the tnl_vertex_array array inside the tnl_inputs structure
529 * provided the current _VPMode, the provided vao and
530 * the vao's enabled arrays filtered by the filter bitmask.
533 _tnl_update_inputs(struct gl_context
*ctx
, struct tnl_inputs
*inputs
);
537 * Context state for T&L context.
543 struct tnl_device_driver Driver
;
547 struct tnl_pipeline pipeline
;
548 struct vertex_buffer vb
;
550 /* Clipspace/ndc/window vertex managment:
552 struct tnl_clipspace clipspace
;
555 /* Probably need a better configuration mechanism:
557 GLboolean NeedNdcCoords
;
558 GLboolean AllowVertexFog
;
559 GLboolean AllowPixelFog
;
560 GLboolean _DoVertexFog
; /* eval fog function at each vertex? */
562 GLbitfield64 render_inputs_bitset
;
564 GLvector4f tmp_inputs
[VERT_ATTRIB_MAX
];
566 /* Temp storage for t_draw.c:
568 GLubyte
*block
[VERT_ATTRIB_MAX
];
573 struct tnl_shine_tab
*_ShineTable
[2]; /**< Active shine tables */
574 struct tnl_shine_tab
*_ShineTabList
; /**< MRU list of inactive shine tables */
577 /* The list of tnl_vertex_array inputs. */
578 struct tnl_inputs draw_arrays
;
583 #define TNL_CONTEXT(ctx) ((TNLcontext *)((ctx)->swtnl_context))
586 #define TYPE_IDX(t) ((t) & 0xf)
587 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
591 tnl_clip_prepare(struct gl_context
*ctx
);