Home | History | Annotate | Download | only in tnl
      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/imports.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 = 17, /* doesn't really exist! */
     97 	_TNL_ATTRIB_GENERIC1 = 18,
     98 	_TNL_ATTRIB_GENERIC2 = 19,
     99 	_TNL_ATTRIB_GENERIC3 = 20,
    100 	_TNL_ATTRIB_GENERIC4 = 21,
    101 	_TNL_ATTRIB_GENERIC5 = 22,
    102 	_TNL_ATTRIB_GENERIC6 = 23,
    103 	_TNL_ATTRIB_GENERIC7 = 24,
    104 	_TNL_ATTRIB_GENERIC8 = 25,
    105 	_TNL_ATTRIB_GENERIC9 = 26,
    106 	_TNL_ATTRIB_GENERIC10 = 27,
    107 	_TNL_ATTRIB_GENERIC11 = 28,
    108 	_TNL_ATTRIB_GENERIC12 = 29,
    109 	_TNL_ATTRIB_GENERIC13 = 30,
    110 	_TNL_ATTRIB_GENERIC14 = 31,
    111 	_TNL_ATTRIB_GENERIC15 = 32,
    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 = 17,
    124 	_TNL_ATTRIB_MAT_BACK_AMBIENT = 18,
    125 	_TNL_ATTRIB_MAT_FRONT_DIFFUSE = 19,
    126 	_TNL_ATTRIB_MAT_BACK_DIFFUSE = 20,
    127 	_TNL_ATTRIB_MAT_FRONT_SPECULAR = 21,
    128 	_TNL_ATTRIB_MAT_BACK_SPECULAR = 22,
    129 	_TNL_ATTRIB_MAT_FRONT_EMISSION = 23,
    130 	_TNL_ATTRIB_MAT_BACK_EMISSION = 24,
    131 	_TNL_ATTRIB_MAT_FRONT_SHININESS = 25,
    132 	_TNL_ATTRIB_MAT_BACK_SHININESS = 26,
    133 	_TNL_ATTRIB_MAT_FRONT_INDEXES = 27,
    134 	_TNL_ATTRIB_MAT_BACK_INDEXES = 28,
    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 = 33
    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 texture attributes */
    166 #define _TNL_NUM_TEX 8
    167 
    168 /* Number of available generic attributes */
    169 #define _TNL_NUM_GENERIC 16
    170 
    171 /* Number of attributes used for evaluators */
    172 #define _TNL_NUM_EVAL 16
    173 
    174 
    175 #define PRIM_BEGIN     0x10
    176 #define PRIM_END       0x20
    177 #define PRIM_MODE_MASK 0x0f
    178 
    179 static inline GLuint _tnl_translate_prim( const struct _mesa_prim *prim )
    180 {
    181    GLuint flag;
    182    flag = prim->mode;
    183    if (prim->begin) flag |= PRIM_BEGIN;
    184    if (prim->end) flag |= PRIM_END;
    185    return flag;
    186 }
    187 
    188 
    189 
    190 
    191 /**
    192  * Contains the current state of a running pipeline.
    193  */
    194 struct vertex_buffer
    195 {
    196    GLuint Size;  /**< Max vertices per vertex buffer, constant */
    197 
    198    /* Constant over the pipeline.
    199     */
    200    GLuint Count;  /**< Number of vertices currently in buffer */
    201 
    202    /* Pointers to current data.  Most of the data is in AttribPtr -- all of
    203     * it that is one of VERT_ATTRIB_X.  For things only produced by TNL,
    204     * such as backface color or eye-space coordinates, they are stored
    205     * here.
    206     */
    207    GLuint      *Elts;
    208    GLvector4f  *EyePtr;		                /* _TNL_BIT_POS */
    209    GLvector4f  *ClipPtr;	                /* _TNL_BIT_POS */
    210    GLvector4f  *NdcPtr;                         /* _TNL_BIT_POS */
    211    GLubyte     ClipOrMask;	                /* _TNL_BIT_POS */
    212    GLubyte     ClipAndMask;	                /* _TNL_BIT_POS */
    213    GLubyte     *ClipMask;		        /* _TNL_BIT_POS */
    214    GLfloat     *NormalLengthPtr;	        /* _TNL_BIT_NORMAL */
    215    GLboolean   *EdgeFlag;	                /* _TNL_BIT_EDGEFLAG */
    216    GLvector4f  *BackfaceIndexPtr;
    217    GLvector4f  *BackfaceColorPtr;
    218    GLvector4f  *BackfaceSecondaryColorPtr;
    219 
    220    const struct _mesa_prim  *Primitive;
    221    GLuint      PrimitiveCount;
    222 
    223    /* Inputs to the vertex program stage */
    224    GLvector4f *AttribPtr[_TNL_ATTRIB_MAX];      /* GL_NV_vertex_program */
    225 };
    226 
    227 
    228 /**
    229  * Describes an individual operation on the pipeline.
    230  */
    231 struct tnl_pipeline_stage
    232 {
    233    const char *name;
    234 
    235    /* Private data for the pipeline stage:
    236     */
    237    void *privatePtr;
    238 
    239    /* Allocate private data
    240     */
    241    GLboolean (*create)( struct gl_context *ctx, struct tnl_pipeline_stage * );
    242 
    243    /* Free private data.
    244     */
    245    void (*destroy)( struct tnl_pipeline_stage * );
    246 
    247    /* Called on any statechange or input array size change or
    248     * input array change to/from zero stride.
    249     */
    250    void (*validate)( struct gl_context *ctx, struct tnl_pipeline_stage * );
    251 
    252    /* Called from _tnl_run_pipeline().  The stage.changed_inputs value
    253     * encodes all inputs to thee struct which have changed.  If
    254     * non-zero, recompute all affected outputs of the stage, otherwise
    255     * execute any 'sideeffects' of the stage.
    256     *
    257     * Return value: GL_TRUE - keep going
    258     *               GL_FALSE - finished pipeline
    259     */
    260    GLboolean (*run)( struct gl_context *ctx, struct tnl_pipeline_stage * );
    261 };
    262 
    263 
    264 
    265 /** Contains the array of all pipeline stages.
    266  * The default values are defined at the end of t_pipeline.c
    267  */
    268 struct tnl_pipeline {
    269 
    270    GLuint last_attrib_stride[_TNL_ATTRIB_MAX];
    271    GLuint last_attrib_size[_TNL_ATTRIB_MAX];
    272    GLuint input_changes;
    273    GLuint new_state;
    274 
    275    struct tnl_pipeline_stage stages[MAX_PIPELINE_STAGES+1];
    276    GLuint nr_stages;
    277 };
    278 
    279 struct tnl_clipspace;
    280 struct tnl_clipspace_attr;
    281 
    282 typedef void (*tnl_extract_func)( const struct tnl_clipspace_attr *a,
    283 				  GLfloat *out,
    284 				  const GLubyte *v );
    285 
    286 typedef void (*tnl_insert_func)( const struct tnl_clipspace_attr *a,
    287 				 GLubyte *v,
    288 				 const GLfloat *in );
    289 
    290 typedef void (*tnl_emit_func)( struct gl_context *ctx,
    291 			       GLuint count,
    292 			       GLubyte *dest );
    293 
    294 
    295 /**
    296  * Describes how to convert/move a vertex attribute from a vertex array
    297  * to a vertex structure.
    298  */
    299 struct tnl_clipspace_attr
    300 {
    301    GLuint attrib;          /* which vertex attrib (0=position, etc) */
    302    GLuint format;
    303    GLuint vertoffset;      /* position of the attrib in the vertex struct */
    304    GLuint vertattrsize;    /* size of the attribute in bytes */
    305    GLubyte *inputptr;
    306    GLuint inputstride;
    307    GLuint inputsize;
    308    const tnl_insert_func *insert;
    309    tnl_insert_func emit;
    310    tnl_extract_func extract;
    311    const GLfloat *vp;   /* NDC->Viewport mapping matrix */
    312 };
    313 
    314 
    315 
    316 
    317 typedef void (*tnl_points_func)( struct gl_context *ctx, GLuint first, GLuint last );
    318 typedef void (*tnl_line_func)( struct gl_context *ctx, GLuint v1, GLuint v2 );
    319 typedef void (*tnl_triangle_func)( struct gl_context *ctx,
    320 				   GLuint v1, GLuint v2, GLuint v3 );
    321 typedef void (*tnl_quad_func)( struct gl_context *ctx, GLuint v1, GLuint v2,
    322 			       GLuint v3, GLuint v4 );
    323 typedef void (*tnl_render_func)( struct gl_context *ctx, GLuint start, GLuint count,
    324 				 GLuint flags );
    325 typedef void (*tnl_interp_func)( struct gl_context *ctx,
    326 				 GLfloat t, GLuint dst, GLuint out, GLuint in,
    327 				 GLboolean force_boundary );
    328 typedef void (*tnl_copy_pv_func)( struct gl_context *ctx, GLuint dst, GLuint src );
    329 typedef void (*tnl_setup_func)( struct gl_context *ctx,
    330 				GLuint start, GLuint end,
    331 				GLuint new_inputs);
    332 
    333 
    334 struct tnl_attr_type {
    335    GLuint format;
    336    GLuint size;
    337    GLuint stride;
    338    GLuint offset;
    339 };
    340 
    341 struct tnl_clipspace_fastpath {
    342    GLuint vertex_size;
    343    GLuint attr_count;
    344    GLboolean match_strides;
    345 
    346    struct tnl_attr_type *attr;
    347 
    348    tnl_emit_func func;
    349    struct tnl_clipspace_fastpath *next;
    350 };
    351 
    352 /**
    353  * Used to describe conversion of vertex arrays to vertex structures.
    354  * I.e. Structure of arrays to arrays of structs.
    355  */
    356 struct tnl_clipspace
    357 {
    358    GLboolean need_extras;
    359 
    360    GLuint new_inputs;
    361 
    362    GLubyte *vertex_buf;
    363    GLuint vertex_size;
    364    GLuint max_vertex_size;
    365 
    366    struct tnl_clipspace_attr attr[_TNL_ATTRIB_MAX];
    367    GLuint attr_count;
    368 
    369    tnl_emit_func emit;
    370    tnl_interp_func interp;
    371    tnl_copy_pv_func copy_pv;
    372 
    373    /* Parameters and constants for codegen:
    374     */
    375    GLboolean need_viewport;
    376    GLfloat vp_scale[4];
    377    GLfloat vp_xlate[4];
    378    GLfloat chan_scale[4];
    379    GLfloat identity[4];
    380 
    381    struct tnl_clipspace_fastpath *fastpath;
    382 
    383    void (*codegen_emit)( struct gl_context *ctx );
    384 };
    385 
    386 
    387 #define SHINE_TABLE_SIZE 256	/**< Material shininess lookup table sizes */
    388 
    389 /**
    390  * Material shininess lookup table.
    391  */
    392 struct tnl_shine_tab
    393 {
    394    struct tnl_shine_tab *next, *prev;
    395    GLfloat tab[SHINE_TABLE_SIZE+1];
    396    GLfloat shininess;
    397    GLuint refcount;
    398 };
    399 
    400 
    401 struct tnl_device_driver
    402 {
    403    /***
    404     *** TNL Pipeline
    405     ***/
    406 
    407    void (*RunPipeline)(struct gl_context *ctx);
    408    /* Replaces PipelineStart/PipelineFinish -- intended to allow
    409     * drivers to wrap _tnl_run_pipeline() with code to validate state
    410     * and grab/release hardware locks.
    411     */
    412 
    413    void (*NotifyMaterialChange)(struct gl_context *ctx);
    414    /* Alert tnl-aware drivers of changes to material.
    415     */
    416 
    417    /***
    418     *** Rendering -- These functions called only from t_vb_render.c
    419     ***/
    420    struct
    421    {
    422       void (*Start)(struct gl_context *ctx);
    423       void (*Finish)(struct gl_context *ctx);
    424       /* Called before and after all rendering operations, including DrawPixels,
    425        * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
    426        * These are a suitable place for grabbing/releasing hardware locks.
    427        */
    428 
    429       void (*PrimitiveNotify)(struct gl_context *ctx, GLenum mode);
    430       /* Called between RenderStart() and RenderFinish() to indicate the
    431        * type of primitive we're about to draw.  Mode will be one of the
    432        * modes accepted by glBegin().
    433        */
    434 
    435       tnl_interp_func Interp;
    436       /* The interp function is called by the clipping routines when we need
    437        * to generate an interpolated vertex.  All pertinant vertex ancilliary
    438        * data should be computed by interpolating between the 'in' and 'out'
    439        * vertices.
    440        */
    441 
    442       tnl_copy_pv_func CopyPV;
    443       /* The copy function is used to make a copy of a vertex.  All pertinant
    444        * vertex attributes should be copied.
    445        */
    446 
    447       void (*ClippedPolygon)( struct gl_context *ctx, const GLuint *elts, GLuint n );
    448       /* Render a polygon with <n> vertices whose indexes are in the <elts>
    449        * array.
    450        */
    451 
    452       void (*ClippedLine)( struct gl_context *ctx, GLuint v0, GLuint v1 );
    453       /* Render a line between the two vertices given by indexes v0 and v1. */
    454 
    455       tnl_points_func           Points; /* must now respect vb->elts */
    456       tnl_line_func             Line;
    457       tnl_triangle_func         Triangle;
    458       tnl_quad_func             Quad;
    459       /* These functions are called in order to render points, lines,
    460        * triangles and quads.  These are only called via the T&L module.
    461        */
    462 
    463       tnl_render_func          *PrimTabVerts;
    464       tnl_render_func          *PrimTabElts;
    465       /* Render whole unclipped primitives (points, lines, linestrips,
    466        * lineloops, etc).  The tables are indexed by the GL enum of the
    467        * primitive to be rendered.  RenderTabVerts is used for non-indexed
    468        * arrays of vertices.  RenderTabElts is used for indexed arrays of
    469        * vertices.
    470        */
    471 
    472       void (*ResetLineStipple)( struct gl_context *ctx );
    473       /* Reset the hardware's line stipple counter.
    474        */
    475 
    476       tnl_setup_func BuildVertices;
    477       /* This function is called whenever new vertices are required for
    478        * rendering.  The vertices in question are those n such that start
    479        * <= n < end.  The new_inputs parameter indicates those fields of
    480        * the vertex which need to be updated, if only a partial repair of
    481        * the vertex is required.
    482        *
    483        * This function is called only from _tnl_render_stage in tnl/t_render.c.
    484        */
    485 
    486 
    487       GLboolean (*Multipass)( struct gl_context *ctx, GLuint passno );
    488       /* Driver may request additional render passes by returning GL_TRUE
    489        * when this function is called.  This function will be called
    490        * after the first pass, and passes will be made until the function
    491        * returns GL_FALSE.  If no function is registered, only one pass
    492        * is made.
    493        *
    494        * This function will be first invoked with passno == 1.
    495        */
    496    } Render;
    497 };
    498 
    499 
    500 /**
    501  * Context state for T&L context.
    502  */
    503 typedef struct
    504 {
    505    /* Driver interface.
    506     */
    507    struct tnl_device_driver Driver;
    508 
    509    /* Pipeline
    510     */
    511    struct tnl_pipeline pipeline;
    512    struct vertex_buffer vb;
    513 
    514    /* Clipspace/ndc/window vertex managment:
    515     */
    516    struct tnl_clipspace clipspace;
    517 
    518    /* Probably need a better configuration mechanism:
    519     */
    520    GLboolean NeedNdcCoords;
    521    GLboolean AllowVertexFog;
    522    GLboolean AllowPixelFog;
    523    GLboolean _DoVertexFog;  /* eval fog function at each vertex? */
    524 
    525    GLbitfield64 render_inputs_bitset;
    526 
    527    GLvector4f tmp_inputs[VERT_ATTRIB_MAX];
    528 
    529    /* Temp storage for t_draw.c:
    530     */
    531    GLubyte *block[VERT_ATTRIB_MAX];
    532    GLuint nr_blocks;
    533 
    534    GLuint CurInstance;
    535 
    536    struct tnl_shine_tab *_ShineTable[2]; /**< Active shine tables */
    537    struct tnl_shine_tab *_ShineTabList;  /**< MRU list of inactive shine tables */
    538    /**@}*/
    539 } TNLcontext;
    540 
    541 
    542 
    543 #define TNL_CONTEXT(ctx) ((TNLcontext *)((ctx)->swtnl_context))
    544 
    545 
    546 #define TYPE_IDX(t) ((t) & 0xf)
    547 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1      /* 0xa + 1 */
    548 
    549 
    550 extern void
    551 tnl_clip_prepare(struct gl_context *ctx);
    552 
    553 
    554 #endif
    555