1 /*
2  * mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
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.
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 
54 #include "main/mtypes.h"
55 
56 #include "math/m_vector.h"
57 
58 #include "vbo/vbo.h"
59 
60 #include "tnl.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,
82 	_TNL_ATTRIB_NORMAL,
83 	_TNL_ATTRIB_COLOR0,
84 	_TNL_ATTRIB_COLOR1,
85 	_TNL_ATTRIB_FOG,
86 	_TNL_ATTRIB_COLOR_INDEX,
87 	_TNL_ATTRIB_EDGEFLAG,
88 	_TNL_ATTRIB_TEX0,
89 	_TNL_ATTRIB_TEX1,
90 	_TNL_ATTRIB_TEX2,
91 	_TNL_ATTRIB_TEX3,
92 	_TNL_ATTRIB_TEX4,
93 	_TNL_ATTRIB_TEX5,
94 	_TNL_ATTRIB_TEX6,
95 	_TNL_ATTRIB_TEX7,
96 
97 	/* This is really a VARYING_SLOT, not an attrib.  Need to fix
98 	 * tnl to understand the difference.
99 	 */
100 	_TNL_ATTRIB_POINTSIZE,
101 
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,
118 
119 	_TNL_ATTRIB_MAX,
120 
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.
125 	 *
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
129 	 * data.
130 	 */
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,
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_NORMAL
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 /* GENERIC4 */
164 #define _TNL_LAST_MAT        _TNL_ATTRIB_MAT_BACK_INDEXES  /* GENERIC15 */
165 
166 /* Number of available texture attributes */
167 #define _TNL_NUM_TEX 8
168 
169 /* Number of available generic attributes */
170 #define _TNL_NUM_GENERIC 16
171 
172 /* Number of attributes used for evaluators */
173 #define _TNL_NUM_EVAL 16
174 
175 
176 #define PRIM_BEGIN     0x10
177 #define PRIM_END       0x20
178 #define PRIM_MODE_MASK 0x0f
179 
_tnl_translate_prim(const struct _mesa_prim * prim)180 static inline GLuint _tnl_translate_prim( const struct _mesa_prim *prim )
181 {
182    GLuint flag;
183    flag = prim->mode;
184    if (prim->begin) flag |= PRIM_BEGIN;
185    if (prim->end) flag |= PRIM_END;
186    return flag;
187 }
188 
189 
190 
191 
192 /**
193  * Contains the current state of a running pipeline.
194  */
195 struct vertex_buffer
196 {
197    GLuint Size;  /**< Max vertices per vertex buffer, constant */
198 
199    /* Constant over the pipeline.
200     */
201    GLuint Count;  /**< Number of vertices currently in buffer */
202 
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
206     * here.
207     */
208    GLuint      *Elts;
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;
220 
221    const struct _mesa_prim  *Primitive;
222    GLuint      PrimitiveCount;
223 
224    /* Inputs to the vertex program stage */
225    GLvector4f *AttribPtr[_TNL_ATTRIB_MAX];
226 };
227 
228 
229 /**
230  * Describes an individual operation on the pipeline.
231  */
232 struct tnl_pipeline_stage
233 {
234    const char *name;
235 
236    /* Private data for the pipeline stage:
237     */
238    void *privatePtr;
239 
240    /* Allocate private data
241     */
242    GLboolean (*create)( struct gl_context *ctx, struct tnl_pipeline_stage * );
243 
244    /* Free private data.
245     */
246    void (*destroy)( struct tnl_pipeline_stage * );
247 
248    /* Called on any statechange or input array size change or
249     * input array change to/from zero stride.
250     */
251    void (*validate)( struct gl_context *ctx, struct tnl_pipeline_stage * );
252 
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.
257     *
258     * Return value: GL_TRUE - keep going
259     *               GL_FALSE - finished pipeline
260     */
261    GLboolean (*run)( struct gl_context *ctx, struct tnl_pipeline_stage * );
262 };
263 
264 
265 
266 /** Contains the array of all pipeline stages.
267  * The default values are defined at the end of t_pipeline.c
268  */
269 struct tnl_pipeline {
270 
271    GLuint last_attrib_stride[_TNL_ATTRIB_MAX];
272    GLuint last_attrib_size[_TNL_ATTRIB_MAX];
273    GLuint input_changes;
274    GLuint new_state;
275 
276    struct tnl_pipeline_stage stages[MAX_PIPELINE_STAGES+1];
277    GLuint nr_stages;
278 };
279 
280 struct tnl_clipspace;
281 struct tnl_clipspace_attr;
282 
283 typedef void (*tnl_extract_func)( const struct tnl_clipspace_attr *a,
284 				  GLfloat *out,
285 				  const GLubyte *v );
286 
287 typedef void (*tnl_insert_func)( const struct tnl_clipspace_attr *a,
288 				 GLubyte *v,
289 				 const GLfloat *in );
290 
291 typedef void (*tnl_emit_func)( struct gl_context *ctx,
292 			       GLuint count,
293 			       GLubyte *dest );
294 
295 
296 /**
297  * Describes how to convert/move a vertex attribute from a vertex array
298  * to a vertex structure.
299  */
300 struct tnl_clipspace_attr
301 {
302    GLuint attrib;          /* which vertex attrib (0=position, etc) */
303    GLuint format;
304    GLuint vertoffset;      /* position of the attrib in the vertex struct */
305    GLuint vertattrsize;    /* size of the attribute in bytes */
306    GLubyte *inputptr;
307    GLuint inputstride;
308    GLuint inputsize;
309    const tnl_insert_func *insert;
310    tnl_insert_func emit;
311    tnl_extract_func extract;
312    const GLfloat *vp;   /* NDC->Viewport mapping matrix */
313 };
314 
315 
316 
317 
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,
325 				 GLuint flags );
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,
332 				GLuint new_inputs);
333 
334 
335 struct tnl_attr_type {
336    GLuint format;
337    GLuint size;
338    GLuint stride;
339    GLuint offset;
340 };
341 
342 struct tnl_clipspace_fastpath {
343    GLuint vertex_size;
344    GLuint attr_count;
345    GLboolean match_strides;
346 
347    struct tnl_attr_type *attr;
348 
349    tnl_emit_func func;
350    struct tnl_clipspace_fastpath *next;
351 };
352 
353 /**
354  * Used to describe conversion of vertex arrays to vertex structures.
355  * I.e. Structure of arrays to arrays of structs.
356  */
357 struct tnl_clipspace
358 {
359    GLboolean need_extras;
360 
361    GLuint new_inputs;
362 
363    GLubyte *vertex_buf;
364    GLuint vertex_size;
365    GLuint max_vertex_size;
366 
367    struct tnl_clipspace_attr attr[_TNL_ATTRIB_MAX];
368    GLuint attr_count;
369 
370    tnl_emit_func emit;
371    tnl_interp_func interp;
372    tnl_copy_pv_func copy_pv;
373 
374    /* Parameters and constants for codegen:
375     */
376    GLboolean need_viewport;
377    GLfloat vp_scale[4];
378    GLfloat vp_xlate[4];
379    GLfloat chan_scale[4];
380    GLfloat identity[4];
381 
382    struct tnl_clipspace_fastpath *fastpath;
383 
384    void (*codegen_emit)( struct gl_context *ctx );
385 };
386 
387 
388 #define SHINE_TABLE_SIZE 256	/**< Material shininess lookup table sizes */
389 
390 /**
391  * Material shininess lookup table.
392  */
393 struct tnl_shine_tab
394 {
395    struct tnl_shine_tab *next, *prev;
396    GLfloat tab[SHINE_TABLE_SIZE+1];
397    GLfloat shininess;
398    GLuint refcount;
399 };
400 
401 
402 struct tnl_device_driver
403 {
404    /***
405     *** TNL Pipeline
406     ***/
407 
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.
412     */
413 
414    void (*NotifyMaterialChange)(struct gl_context *ctx);
415    /* Alert tnl-aware drivers of changes to material.
416     */
417 
418    /***
419     *** Rendering -- These functions called only from t_vb_render.c
420     ***/
421    struct
422    {
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.
428        */
429 
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().
434        */
435 
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'
440        * vertices.
441        */
442 
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.
446        */
447 
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>
450        * array.
451        */
452 
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. */
455 
456       tnl_points_func           Points; /* must now respect vb->elts */
457       tnl_line_func             Line;
458       tnl_triangle_func         Triangle;
459       tnl_quad_func             Quad;
460       /* These functions are called in order to render points, lines,
461        * triangles and quads.  These are only called via the T&L module.
462        */
463 
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
470        * vertices.
471        */
472 
473       void (*ResetLineStipple)( struct gl_context *ctx );
474       /* Reset the hardware's line stipple counter.
475        */
476 
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.
483        *
484        * This function is called only from _tnl_render_stage in tnl/t_render.c.
485        */
486 
487 
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
493        * is made.
494        *
495        * This function will be first invoked with passno == 1.
496        */
497    } Render;
498 };
499 
500 
501 /**
502  * Utility that tracks and updates the current array entries.
503  */
504 struct tnl_inputs
505 {
506    /**
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.
511     */
512    struct tnl_vertex_array inputs[VERT_ATTRIB_MAX];
513    /** Those VERT_BIT_'s where the inputs array point to current values. */
514    GLbitfield current;
515    /** Store which aliasing current values - generics or materials - are set. */
516    gl_vertex_processing_mode vertex_processing_mode;
517 };
518 
519 
520 /**
521  * Initialize inputs.
522  */
523 void
524 _tnl_init_inputs(struct tnl_inputs *inputs);
525 
526 
527 /**
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.
531  */
532 void
533 _tnl_update_inputs(struct gl_context *ctx, struct tnl_inputs *inputs);
534 
535 
536 /**
537  * Context state for T&L context.
538  */
539 typedef struct
540 {
541    /* Driver interface.
542     */
543    struct tnl_device_driver Driver;
544 
545    /* Pipeline
546     */
547    struct tnl_pipeline pipeline;
548    struct vertex_buffer vb;
549 
550    /* Clipspace/ndc/window vertex managment:
551     */
552    struct tnl_clipspace clipspace;
553    GLmatrix _WindowMap;
554 
555    /* Probably need a better configuration mechanism:
556     */
557    GLboolean NeedNdcCoords;
558    GLboolean AllowVertexFog;
559    GLboolean AllowPixelFog;
560    GLboolean _DoVertexFog;  /* eval fog function at each vertex? */
561 
562    GLbitfield64 render_inputs_bitset;
563 
564    GLvector4f tmp_inputs[VERT_ATTRIB_MAX];
565 
566    /* Temp storage for t_draw.c:
567     */
568    GLubyte *block[VERT_ATTRIB_MAX];
569    GLuint nr_blocks;
570 
571    GLuint CurInstance;
572 
573    struct tnl_shine_tab *_ShineTable[2]; /**< Active shine tables */
574    struct tnl_shine_tab *_ShineTabList;  /**< MRU list of inactive shine tables */
575    /**@}*/
576 
577    /* The list of tnl_vertex_array inputs. */
578    struct tnl_inputs draw_arrays;
579 } TNLcontext;
580 
581 
582 
583 #define TNL_CONTEXT(ctx) ((TNLcontext *)((ctx)->swtnl_context))
584 
585 
586 #define TYPE_IDX(t) ((t) & 0xf)
587 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1      /* 0xa + 1 */
588 
589 
590 extern void
591 tnl_clip_prepare(struct gl_context *ctx);
592 
593 
594 #endif
595