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