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