Home | History | Annotate | Download | only in tnl
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright (C) 1999-2008  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  * Authors:
     25  *    Keith Whitwell <keithw (at) vmware.com>
     26  */
     27 
     28 
     29 #include "main/glheader.h"
     30 #include "main/imports.h"
     31 #include "main/context.h"
     32 #include "main/macros.h"
     33 #include "main/mtypes.h"
     34 #include "main/light.h"
     35 #include "math/m_translate.h"
     36 #include "math/m_xform.h"
     37 #include "main/state.h"
     38 #include "main/viewport.h"
     39 #include "util/simple_list.h"
     40 
     41 #include "tnl.h"
     42 #include "t_context.h"
     43 #include "t_pipeline.h"
     44 
     45 #include "vbo/vbo.h"
     46 
     47 GLboolean
     48 _tnl_CreateContext( struct gl_context *ctx )
     49 {
     50    TNLcontext *tnl;
     51    GLuint i;
     52 
     53    /* Create the TNLcontext structure
     54     */
     55    ctx->swtnl_context = tnl = calloc(1, sizeof(TNLcontext));
     56 
     57    if (!tnl) {
     58       return GL_FALSE;
     59    }
     60 
     61    /* Initialize the VB.
     62     */
     63    tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
     64 
     65 
     66    /* Initialize tnl state.
     67     */
     68    if (ctx->VertexProgram._MaintainTnlProgram) {
     69       _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
     70    } else {
     71       _tnl_install_pipeline( ctx, _tnl_default_pipeline );
     72    }
     73 
     74    _math_matrix_ctr(&tnl->_WindowMap);
     75 
     76    tnl->NeedNdcCoords = GL_TRUE;
     77    tnl->AllowVertexFog = GL_TRUE;
     78    tnl->AllowPixelFog = GL_TRUE;
     79 
     80    /* Set a few default values in the driver struct.
     81     */
     82    tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
     83    tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
     84    tnl->Driver.NotifyMaterialChange = _tnl_validate_shine_tables;
     85 
     86    tnl->nr_blocks = 0;
     87 
     88    /* Lighting miscellaneous */
     89    tnl->_ShineTabList = MALLOC_STRUCT( tnl_shine_tab );
     90    make_empty_list( tnl->_ShineTabList );
     91    /* Allocate 10 (arbitrary) shininess lookup tables */
     92    for (i = 0 ; i < 10 ; i++) {
     93       struct tnl_shine_tab *s = MALLOC_STRUCT( tnl_shine_tab );
     94       s->shininess = -1;
     95       s->refcount = 0;
     96       insert_at_tail( tnl->_ShineTabList, s );
     97    }
     98 
     99    /* plug in the VBO drawing function */
    100    vbo_set_draw_func(ctx, _tnl_draw_prims);
    101 
    102    _math_init_transformation();
    103    _math_init_translate();
    104 
    105    return GL_TRUE;
    106 }
    107 
    108 
    109 void
    110 _tnl_DestroyContext( struct gl_context *ctx )
    111 {
    112    struct tnl_shine_tab *s, *tmps;
    113    TNLcontext *tnl = TNL_CONTEXT(ctx);
    114 
    115    _math_matrix_dtr(&tnl->_WindowMap);
    116 
    117    /* Free lighting shininess exponentiation table */
    118    foreach_s( s, tmps, tnl->_ShineTabList ) {
    119       free( s );
    120    }
    121    free( tnl->_ShineTabList );
    122 
    123    _tnl_destroy_pipeline( ctx );
    124 
    125    free(tnl);
    126    ctx->swtnl_context = NULL;
    127 }
    128 
    129 
    130 void
    131 _tnl_InvalidateState( struct gl_context *ctx, GLuint new_state )
    132 {
    133    TNLcontext *tnl = TNL_CONTEXT(ctx);
    134    const struct gl_program *vp = ctx->VertexProgram._Current;
    135    const struct gl_program *fp = ctx->FragmentProgram._Current;
    136    GLuint i;
    137 
    138    if (new_state & (_NEW_HINT | _NEW_PROGRAM)) {
    139       assert(tnl->AllowVertexFog || tnl->AllowPixelFog);
    140       tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
    141          || !tnl->AllowPixelFog) && !fp;
    142    }
    143 
    144    tnl->pipeline.new_state |= new_state;
    145 
    146    /* Calculate tnl->render_inputs.  This bitmask indicates which vertex
    147     * attributes need to be emitted to the rasterizer.
    148     */
    149    tnl->render_inputs_bitset = BITFIELD64_BIT(_TNL_ATTRIB_POS);
    150 
    151    if (!fp || (fp->info.inputs_read & VARYING_BIT_COL0)) {
    152      tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR0);
    153    }
    154 
    155    if (_mesa_need_secondary_color(ctx))
    156      tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR1);
    157 
    158    for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
    159      if (ctx->Texture._EnabledCoordUnits & (1 << i) ||
    160 	 (fp && fp->info.inputs_read & VARYING_BIT_TEX(i)) ||
    161          _mesa_ati_fragment_shader_enabled(ctx)) {
    162        tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX(i));
    163      }
    164    }
    165 
    166    if (ctx->Fog.Enabled
    167        || (fp != NULL && (fp->info.inputs_read & VARYING_BIT_FOGC) != 0)) {
    168       /* Either fixed-function fog or a fragment program needs fog coord.
    169        */
    170       tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_FOG);
    171    }
    172 
    173    if (ctx->Polygon.FrontMode != GL_FILL ||
    174        ctx->Polygon.BackMode != GL_FILL)
    175       tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_EDGEFLAG);
    176 
    177    if (ctx->RenderMode == GL_FEEDBACK)
    178       tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX0);
    179 
    180    if (ctx->Point._Attenuated || ctx->VertexProgram.PointSizeEnabled)
    181       tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_POINTSIZE);
    182 
    183    /* check for varying vars which are written by the vertex program */
    184    if (vp) {
    185       GLuint i;
    186       for (i = 0; i < MAX_VARYING; i++) {
    187 	 if (vp->info.outputs_written &
    188              BITFIELD64_BIT(VARYING_SLOT_VAR0 + i)) {
    189             tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_GENERIC(i));
    190          }
    191       }
    192    }
    193 
    194    if (new_state & (_NEW_VIEWPORT | _NEW_BUFFERS)) {
    195       float scale[3], translate[3];
    196       _mesa_get_viewport_xform(ctx, 0, scale, translate);
    197       _math_matrix_viewport(&tnl->_WindowMap, scale, translate,
    198                             ctx->DrawBuffer->_DepthMaxF);
    199    }
    200 }
    201 
    202 
    203 void
    204 _tnl_wakeup( struct gl_context *ctx )
    205 {
    206    /* Assume we haven't been getting state updates either:
    207     */
    208    _tnl_InvalidateState( ctx, ~0 );
    209 
    210 #if 0
    211    if (ctx->Light.ColorMaterialEnabled) {
    212       _mesa_update_color_material( ctx,
    213 				   ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
    214    }
    215 #endif
    216 }
    217 
    218 
    219 
    220 
    221 /**
    222  * Drivers call this function to tell the TCL module whether or not
    223  * it wants Normalized Device Coords (NDC) computed.  I.e. whether
    224  * we should "Divide-by-W".  Software renders will want that.
    225  */
    226 void
    227 _tnl_need_projected_coords( struct gl_context *ctx, GLboolean mode )
    228 {
    229    TNLcontext *tnl = TNL_CONTEXT(ctx);
    230    tnl->NeedNdcCoords = mode;
    231 }
    232 
    233 void
    234 _tnl_allow_vertex_fog( struct gl_context *ctx, GLboolean value )
    235 {
    236    TNLcontext *tnl = TNL_CONTEXT(ctx);
    237    tnl->AllowVertexFog = value;
    238    tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
    239       || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
    240 
    241 }
    242 
    243 void
    244 _tnl_allow_pixel_fog( struct gl_context *ctx, GLboolean value )
    245 {
    246    TNLcontext *tnl = TNL_CONTEXT(ctx);
    247    tnl->AllowPixelFog = value;
    248    tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
    249       || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
    250 }
    251 
    252