Home | History | Annotate | Download | only in main
      1 /*
      2  * Mesa 3-D graphics library
      3  * Version:  7.3
      4  *
      5  * Copyright (C) 1999-2008  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 /**
     27  * \file state.c
     28  * State management.
     29  *
     30  * This file manages recalculation of derived values in struct gl_context.
     31  */
     32 
     33 
     34 #include "glheader.h"
     35 #include "mtypes.h"
     36 #include "arrayobj.h"
     37 #include "context.h"
     38 #include "debug.h"
     39 #include "macros.h"
     40 #include "ffvertex_prog.h"
     41 #include "framebuffer.h"
     42 #include "light.h"
     43 #include "matrix.h"
     44 #include "pixel.h"
     45 #include "program/program.h"
     46 #include "program/prog_parameter.h"
     47 #include "shaderobj.h"
     48 #include "state.h"
     49 #include "stencil.h"
     50 #include "texenvprogram.h"
     51 #include "texobj.h"
     52 #include "texstate.h"
     53 #include "varray.h"
     54 
     55 
     56 static void
     57 update_separate_specular(struct gl_context *ctx)
     58 {
     59    if (_mesa_need_secondary_color(ctx))
     60       ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
     61    else
     62       ctx->_TriangleCaps &= ~DD_SEPARATE_SPECULAR;
     63 }
     64 
     65 
     66 /**
     67  * Update the following fields:
     68  *   ctx->VertexProgram._Enabled
     69  *   ctx->FragmentProgram._Enabled
     70  *   ctx->ATIFragmentShader._Enabled
     71  * This needs to be done before texture state validation.
     72  */
     73 static void
     74 update_program_enables(struct gl_context *ctx)
     75 {
     76    /* These _Enabled flags indicate if the user-defined ARB/NV vertex/fragment
     77     * program is enabled AND valid.  Similarly for ATI fragment shaders.
     78     * GLSL shaders not relevant here.
     79     */
     80    ctx->VertexProgram._Enabled = ctx->VertexProgram.Enabled
     81       && ctx->VertexProgram.Current->Base.Instructions;
     82    ctx->FragmentProgram._Enabled = ctx->FragmentProgram.Enabled
     83       && ctx->FragmentProgram.Current->Base.Instructions;
     84    ctx->ATIFragmentShader._Enabled = ctx->ATIFragmentShader.Enabled
     85       && ctx->ATIFragmentShader.Current->Instructions[0];
     86 }
     87 
     88 
     89 /**
     90  * Update the ctx->Vertex/Geometry/FragmentProgram._Current pointers to point
     91  * to the current/active programs.  Then call ctx->Driver.BindProgram() to
     92  * tell the driver which programs to use.
     93  *
     94  * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
     95  * programs or programs derived from fixed-function state.
     96  *
     97  * This function needs to be called after texture state validation in case
     98  * we're generating a fragment program from fixed-function texture state.
     99  *
    100  * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
    101  * or fragment program is being used.
    102  */
    103 static GLbitfield
    104 update_program(struct gl_context *ctx)
    105 {
    106    const struct gl_shader_program *vsProg = ctx->Shader.CurrentVertexProgram;
    107    const struct gl_shader_program *gsProg = ctx->Shader.CurrentGeometryProgram;
    108    struct gl_shader_program *fsProg = ctx->Shader.CurrentFragmentProgram;
    109    const struct gl_vertex_program *prevVP = ctx->VertexProgram._Current;
    110    const struct gl_fragment_program *prevFP = ctx->FragmentProgram._Current;
    111    const struct gl_geometry_program *prevGP = ctx->GeometryProgram._Current;
    112    GLbitfield new_state = 0x0;
    113 
    114    /*
    115     * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
    116     * pointers to the programs that should be used for rendering.  If either
    117     * is NULL, use fixed-function code paths.
    118     *
    119     * These programs may come from several sources.  The priority is as
    120     * follows:
    121     *   1. OpenGL 2.0/ARB vertex/fragment shaders
    122     *   2. ARB/NV vertex/fragment programs
    123     *   3. Programs derived from fixed-function state.
    124     *
    125     * Note: it's possible for a vertex shader to get used with a fragment
    126     * program (and vice versa) here, but in practice that shouldn't ever
    127     * come up, or matter.
    128     */
    129 
    130    if (fsProg && fsProg->LinkStatus
    131        && fsProg->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
    132       /* Use GLSL fragment shader */
    133       _mesa_reference_shader_program(ctx,
    134 				     &ctx->Shader._CurrentFragmentProgram,
    135 				     fsProg);
    136       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
    137                                gl_fragment_program(fsProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program));
    138       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
    139 			       NULL);
    140    }
    141    else if (ctx->FragmentProgram._Enabled) {
    142       /* Use user-defined fragment program */
    143       _mesa_reference_shader_program(ctx,
    144 				     &ctx->Shader._CurrentFragmentProgram,
    145 				     NULL);
    146       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
    147                                ctx->FragmentProgram.Current);
    148       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
    149 			       NULL);
    150    }
    151    else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
    152       /* Use fragment program generated from fixed-function state */
    153       struct gl_shader_program *f = _mesa_get_fixed_func_fragment_program(ctx);
    154 
    155       _mesa_reference_shader_program(ctx,
    156 				     &ctx->Shader._CurrentFragmentProgram,
    157 				     f);
    158       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
    159 			       gl_fragment_program(f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program));
    160       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
    161 			       gl_fragment_program(f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program));
    162    }
    163    else {
    164       /* No fragment program */
    165       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
    166       _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
    167 			       NULL);
    168    }
    169 
    170    if (gsProg && gsProg->LinkStatus
    171        && gsProg->_LinkedShaders[MESA_SHADER_GEOMETRY]) {
    172       /* Use GLSL geometry shader */
    173       _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current,
    174 			       gl_geometry_program(gsProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program));
    175    } else {
    176       /* No geometry program */
    177       _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current, NULL);
    178    }
    179 
    180    /* Examine vertex program after fragment program as
    181     * _mesa_get_fixed_func_vertex_program() needs to know active
    182     * fragprog inputs.
    183     */
    184    if (vsProg && vsProg->LinkStatus
    185        && vsProg->_LinkedShaders[MESA_SHADER_VERTEX]) {
    186       /* Use GLSL vertex shader */
    187       _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
    188 			       gl_vertex_program(vsProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program));
    189    }
    190    else if (ctx->VertexProgram._Enabled) {
    191       /* Use user-defined vertex program */
    192       _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
    193                                ctx->VertexProgram.Current);
    194    }
    195    else if (ctx->VertexProgram._MaintainTnlProgram) {
    196       /* Use vertex program generated from fixed-function state */
    197       _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
    198                                _mesa_get_fixed_func_vertex_program(ctx));
    199       _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram,
    200                                ctx->VertexProgram._Current);
    201    }
    202    else {
    203       /* no vertex program */
    204       _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
    205    }
    206 
    207    /* Let the driver know what's happening:
    208     */
    209    if (ctx->FragmentProgram._Current != prevFP) {
    210       new_state |= _NEW_PROGRAM;
    211       if (ctx->Driver.BindProgram) {
    212          ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
    213                           (struct gl_program *) ctx->FragmentProgram._Current);
    214       }
    215    }
    216 
    217    if (ctx->GeometryProgram._Current != prevGP) {
    218       new_state |= _NEW_PROGRAM;
    219       if (ctx->Driver.BindProgram) {
    220          ctx->Driver.BindProgram(ctx, MESA_GEOMETRY_PROGRAM,
    221                             (struct gl_program *) ctx->GeometryProgram._Current);
    222       }
    223    }
    224 
    225    if (ctx->VertexProgram._Current != prevVP) {
    226       new_state |= _NEW_PROGRAM;
    227       if (ctx->Driver.BindProgram) {
    228          ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
    229                             (struct gl_program *) ctx->VertexProgram._Current);
    230       }
    231    }
    232 
    233    return new_state;
    234 }
    235 
    236 
    237 /**
    238  * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
    239  */
    240 static GLbitfield
    241 update_program_constants(struct gl_context *ctx)
    242 {
    243    GLbitfield new_state = 0x0;
    244 
    245    if (ctx->FragmentProgram._Current) {
    246       const struct gl_program_parameter_list *params =
    247          ctx->FragmentProgram._Current->Base.Parameters;
    248       if (params && params->StateFlags & ctx->NewState) {
    249          new_state |= _NEW_PROGRAM_CONSTANTS;
    250       }
    251    }
    252 
    253    if (ctx->GeometryProgram._Current) {
    254       const struct gl_program_parameter_list *params =
    255          ctx->GeometryProgram._Current->Base.Parameters;
    256       /*FIXME: StateFlags is always 0 because we have unnamed constant
    257        *       not state changes */
    258       if (params /*&& params->StateFlags & ctx->NewState*/) {
    259          new_state |= _NEW_PROGRAM_CONSTANTS;
    260       }
    261    }
    262 
    263    if (ctx->VertexProgram._Current) {
    264       const struct gl_program_parameter_list *params =
    265          ctx->VertexProgram._Current->Base.Parameters;
    266       if (params && params->StateFlags & ctx->NewState) {
    267          new_state |= _NEW_PROGRAM_CONSTANTS;
    268       }
    269    }
    270 
    271    return new_state;
    272 }
    273 
    274 
    275 
    276 
    277 static void
    278 update_viewport_matrix(struct gl_context *ctx)
    279 {
    280    const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
    281 
    282    ASSERT(depthMax > 0);
    283 
    284    /* Compute scale and bias values. This is really driver-specific
    285     * and should be maintained elsewhere if at all.
    286     * NOTE: RasterPos uses this.
    287     */
    288    _math_matrix_viewport(&ctx->Viewport._WindowMap,
    289                          ctx->Viewport.X, ctx->Viewport.Y,
    290                          ctx->Viewport.Width, ctx->Viewport.Height,
    291                          ctx->Viewport.Near, ctx->Viewport.Far,
    292                          depthMax);
    293 }
    294 
    295 
    296 /**
    297  * Update derived multisample state.
    298  */
    299 static void
    300 update_multisample(struct gl_context *ctx)
    301 {
    302    ctx->Multisample._Enabled = GL_FALSE;
    303    if (ctx->Multisample.Enabled &&
    304        ctx->DrawBuffer &&
    305        ctx->DrawBuffer->Visual.sampleBuffers)
    306       ctx->Multisample._Enabled = GL_TRUE;
    307 }
    308 
    309 
    310 /**
    311  * Update the ctx->Color._ClampFragmentColor field
    312  */
    313 static void
    314 update_clamp_fragment_color(struct gl_context *ctx)
    315 {
    316    if (ctx->Color.ClampFragmentColor == GL_FIXED_ONLY_ARB)
    317       ctx->Color._ClampFragmentColor =
    318          !ctx->DrawBuffer || !ctx->DrawBuffer->Visual.floatMode;
    319    else
    320       ctx->Color._ClampFragmentColor = ctx->Color.ClampFragmentColor;
    321 }
    322 
    323 
    324 /**
    325  * Update the ctx->Color._ClampVertexColor field
    326  */
    327 static void
    328 update_clamp_vertex_color(struct gl_context *ctx)
    329 {
    330    if (ctx->Light.ClampVertexColor == GL_FIXED_ONLY_ARB)
    331       ctx->Light._ClampVertexColor =
    332          !ctx->DrawBuffer || !ctx->DrawBuffer->Visual.floatMode;
    333    else
    334       ctx->Light._ClampVertexColor = ctx->Light.ClampVertexColor;
    335 }
    336 
    337 
    338 /**
    339  * Update the ctx->Color._ClampReadColor field
    340  */
    341 static void
    342 update_clamp_read_color(struct gl_context *ctx)
    343 {
    344    if (ctx->Color.ClampReadColor == GL_FIXED_ONLY_ARB)
    345       ctx->Color._ClampReadColor =
    346          !ctx->ReadBuffer || !ctx->ReadBuffer->Visual.floatMode;
    347    else
    348       ctx->Color._ClampReadColor = ctx->Color.ClampReadColor;
    349 }
    350 
    351 /**
    352  * Update the ctx->VertexProgram._TwoSideEnabled flag.
    353  */
    354 static void
    355 update_twoside(struct gl_context *ctx)
    356 {
    357    if (ctx->Shader.CurrentVertexProgram ||
    358        ctx->VertexProgram._Enabled) {
    359       ctx->VertexProgram._TwoSideEnabled = ctx->VertexProgram.TwoSideEnabled;
    360    } else {
    361       ctx->VertexProgram._TwoSideEnabled = (ctx->Light.Enabled &&
    362 					    ctx->Light.Model.TwoSide);
    363    }
    364 }
    365 
    366 
    367 /*
    368  * Check polygon state and set DD_TRI_OFFSET
    369  * in ctx->_TriangleCaps if needed.
    370  */
    371 static void
    372 update_polygon(struct gl_context *ctx)
    373 {
    374    ctx->_TriangleCaps &= ~DD_TRI_OFFSET;
    375 
    376    if (   ctx->Polygon.OffsetPoint
    377        || ctx->Polygon.OffsetLine
    378        || ctx->Polygon.OffsetFill)
    379       ctx->_TriangleCaps |= DD_TRI_OFFSET;
    380 }
    381 
    382 
    383 /**
    384  * Update the ctx->_TriangleCaps bitfield.
    385  * XXX that bitfield should really go away someday!
    386  * This function must be called after other update_*() functions since
    387  * there are dependencies on some other derived values.
    388  */
    389 #if 0
    390 static void
    391 update_tricaps(struct gl_context *ctx, GLbitfield new_state)
    392 {
    393    ctx->_TriangleCaps = 0;
    394 
    395    /*
    396     * Points
    397     */
    398    if (1/*new_state & _NEW_POINT*/) {
    399       if (ctx->Point.SmoothFlag)
    400          ctx->_TriangleCaps |= DD_POINT_SMOOTH;
    401       if (ctx->Point._Attenuated)
    402          ctx->_TriangleCaps |= DD_POINT_ATTEN;
    403    }
    404 
    405    /*
    406     * Lines
    407     */
    408    if (1/*new_state & _NEW_LINE*/) {
    409       if (ctx->Line.SmoothFlag)
    410          ctx->_TriangleCaps |= DD_LINE_SMOOTH;
    411       if (ctx->Line.StippleFlag)
    412          ctx->_TriangleCaps |= DD_LINE_STIPPLE;
    413    }
    414 
    415    /*
    416     * Polygons
    417     */
    418    if (1/*new_state & _NEW_POLYGON*/) {
    419       if (ctx->Polygon.SmoothFlag)
    420          ctx->_TriangleCaps |= DD_TRI_SMOOTH;
    421       if (ctx->Polygon.StippleFlag)
    422          ctx->_TriangleCaps |= DD_TRI_STIPPLE;
    423       if (ctx->Polygon.FrontMode != GL_FILL
    424           || ctx->Polygon.BackMode != GL_FILL)
    425          ctx->_TriangleCaps |= DD_TRI_UNFILLED;
    426       if (ctx->Polygon.OffsetPoint ||
    427           ctx->Polygon.OffsetLine ||
    428           ctx->Polygon.OffsetFill)
    429          ctx->_TriangleCaps |= DD_TRI_OFFSET;
    430    }
    431 
    432    /*
    433     * Lighting and shading
    434     */
    435    if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
    436       ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
    437    if (_mesa_need_secondary_color(ctx))
    438       ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
    439 }
    440 #endif
    441 
    442 
    443 /**
    444  * Compute derived GL state.
    445  * If __struct gl_contextRec::NewState is non-zero then this function \b must
    446  * be called before rendering anything.
    447  *
    448  * Calls dd_function_table::UpdateState to perform any internal state
    449  * management necessary.
    450  *
    451  * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
    452  * _mesa_update_buffer_bounds(),
    453  * _mesa_update_lighting() and _mesa_update_tnl_spaces().
    454  */
    455 void
    456 _mesa_update_state_locked( struct gl_context *ctx )
    457 {
    458    GLbitfield new_state = ctx->NewState;
    459    GLbitfield prog_flags = _NEW_PROGRAM;
    460    GLbitfield new_prog_state = 0x0;
    461 
    462    if (new_state == _NEW_CURRENT_ATTRIB)
    463       goto out;
    464 
    465    if (MESA_VERBOSE & VERBOSE_STATE)
    466       _mesa_print_state("_mesa_update_state", new_state);
    467 
    468    /* Determine which state flags effect vertex/fragment program state */
    469    if (ctx->FragmentProgram._MaintainTexEnvProgram) {
    470       prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE | _NEW_FOG |
    471 		     _NEW_VARYING_VP_INPUTS | _NEW_LIGHT | _NEW_POINT |
    472 		     _NEW_RENDERMODE | _NEW_PROGRAM | _NEW_FRAG_CLAMP |
    473 		     _NEW_COLOR);
    474    }
    475    if (ctx->VertexProgram._MaintainTnlProgram) {
    476       prog_flags |= (_NEW_VARYING_VP_INPUTS | _NEW_TEXTURE |
    477                      _NEW_TEXTURE_MATRIX | _NEW_TRANSFORM | _NEW_POINT |
    478                      _NEW_FOG | _NEW_LIGHT |
    479                      _MESA_NEW_NEED_EYE_COORDS);
    480    }
    481 
    482    /*
    483     * Now update derived state info
    484     */
    485 
    486    if (new_state & prog_flags)
    487       update_program_enables( ctx );
    488 
    489    if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
    490       _mesa_update_modelview_project( ctx, new_state );
    491 
    492    if (new_state & (_NEW_PROGRAM|_NEW_TEXTURE|_NEW_TEXTURE_MATRIX))
    493       _mesa_update_texture( ctx, new_state );
    494 
    495    if (new_state & _NEW_BUFFERS)
    496       _mesa_update_framebuffer(ctx);
    497 
    498    if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
    499       _mesa_update_draw_buffer_bounds( ctx );
    500 
    501    if (new_state & _NEW_POLYGON)
    502       update_polygon( ctx );
    503 
    504    if (new_state & _NEW_LIGHT)
    505       _mesa_update_lighting( ctx );
    506 
    507    if (new_state & (_NEW_LIGHT | _NEW_PROGRAM))
    508       update_twoside( ctx );
    509 
    510    if (new_state & (_NEW_LIGHT | _NEW_BUFFERS))
    511       update_clamp_vertex_color(ctx);
    512 
    513    if (new_state & (_NEW_STENCIL | _NEW_BUFFERS))
    514       _mesa_update_stencil( ctx );
    515 
    516    if (new_state & _NEW_PIXEL)
    517       _mesa_update_pixel( ctx, new_state );
    518 
    519    if (new_state & _MESA_NEW_SEPARATE_SPECULAR)
    520       update_separate_specular( ctx );
    521 
    522    if (new_state & (_NEW_BUFFERS | _NEW_VIEWPORT))
    523       update_viewport_matrix(ctx);
    524 
    525    if (new_state & (_NEW_MULTISAMPLE | _NEW_BUFFERS))
    526       update_multisample( ctx );
    527 
    528    if (new_state & (_NEW_COLOR | _NEW_BUFFERS))
    529       update_clamp_read_color(ctx);
    530 
    531    if(new_state & (_NEW_FRAG_CLAMP | _NEW_BUFFERS))
    532       update_clamp_fragment_color(ctx);
    533 
    534 #if 0
    535    if (new_state & (_NEW_POINT | _NEW_LINE | _NEW_POLYGON | _NEW_LIGHT
    536                     | _NEW_STENCIL | _MESA_NEW_SEPARATE_SPECULAR))
    537       update_tricaps( ctx, new_state );
    538 #endif
    539 
    540    /* ctx->_NeedEyeCoords is now up to date.
    541     *
    542     * If the truth value of this variable has changed, update for the
    543     * new lighting space and recompute the positions of lights and the
    544     * normal transform.
    545     *
    546     * If the lighting space hasn't changed, may still need to recompute
    547     * light positions & normal transforms for other reasons.
    548     */
    549    if (new_state & _MESA_NEW_NEED_EYE_COORDS)
    550       _mesa_update_tnl_spaces( ctx, new_state );
    551 
    552    if (new_state & prog_flags) {
    553       /* When we generate programs from fixed-function vertex/fragment state
    554        * this call may generate/bind a new program.  If so, we need to
    555        * propogate the _NEW_PROGRAM flag to the driver.
    556        */
    557       new_prog_state |= update_program( ctx );
    558    }
    559 
    560    if (new_state & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT))
    561       _mesa_update_array_object_max_element(ctx, ctx->Array.ArrayObj);
    562 
    563  out:
    564    new_prog_state |= update_program_constants(ctx);
    565 
    566    /*
    567     * Give the driver a chance to act upon the new_state flags.
    568     * The driver might plug in different span functions, for example.
    569     * Also, this is where the driver can invalidate the state of any
    570     * active modules (such as swrast_setup, swrast, tnl, etc).
    571     *
    572     * Set ctx->NewState to zero to avoid recursion if
    573     * Driver.UpdateState() has to call FLUSH_VERTICES().  (fixed?)
    574     */
    575    new_state = ctx->NewState | new_prog_state;
    576    ctx->NewState = 0;
    577    ctx->Driver.UpdateState(ctx, new_state);
    578    ctx->Array.ArrayObj->NewArrays = 0x0;
    579 }
    580 
    581 
    582 /* This is the usual entrypoint for state updates:
    583  */
    584 void
    585 _mesa_update_state( struct gl_context *ctx )
    586 {
    587    _mesa_lock_context_textures(ctx);
    588    _mesa_update_state_locked(ctx);
    589    _mesa_unlock_context_textures(ctx);
    590 }
    591 
    592 
    593 
    594 
    595 /**
    596  * Want to figure out which fragment program inputs are actually
    597  * constant/current values from ctx->Current.  These should be
    598  * referenced as a tracked state variable rather than a fragment
    599  * program input, to save the overhead of putting a constant value in
    600  * every submitted vertex, transferring it to hardware, interpolating
    601  * it across the triangle, etc...
    602  *
    603  * When there is a VP bound, just use vp->outputs.  But when we're
    604  * generating vp from fixed function state, basically want to
    605  * calculate:
    606  *
    607  * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
    608  *                 potential_vp_outputs )
    609  *
    610  * Where potential_vp_outputs is calculated by looking at enabled
    611  * texgen, etc.
    612  *
    613  * The generated fragment program should then only declare inputs that
    614  * may vary or otherwise differ from the ctx->Current values.
    615  * Otherwise, the fp should track them as state values instead.
    616  */
    617 void
    618 _mesa_set_varying_vp_inputs( struct gl_context *ctx,
    619                              GLbitfield64 varying_inputs )
    620 {
    621    if (ctx->varying_vp_inputs != varying_inputs) {
    622       ctx->varying_vp_inputs = varying_inputs;
    623 
    624       /* Only the fixed-func generated programs need to use the flag
    625        * and the fixed-func fragment program uses it only if there is also
    626        * a fixed-func vertex program, so this only depends on the latter.
    627        *
    628        * It's okay to check the VP pointer here, because this is called after
    629        * _mesa_update_state in the vbo module. */
    630       if (ctx->VertexProgram._TnlProgram ||
    631           ctx->FragmentProgram._TexEnvProgram) {
    632          ctx->NewState |= _NEW_VARYING_VP_INPUTS;
    633       }
    634       /*printf("%s %x\n", __FUNCTION__, varying_inputs);*/
    635    }
    636 }
    637 
    638 
    639 /**
    640  * Used by drivers to tell core Mesa that the driver is going to
    641  * install/ use its own vertex program.  In particular, this will
    642  * prevent generated fragment programs from using state vars instead
    643  * of ordinary varyings/inputs.
    644  */
    645 void
    646 _mesa_set_vp_override(struct gl_context *ctx, GLboolean flag)
    647 {
    648    if (ctx->VertexProgram._Overriden != flag) {
    649       ctx->VertexProgram._Overriden = flag;
    650 
    651       /* Set one of the bits which will trigger fragment program
    652        * regeneration:
    653        */
    654       ctx->NewState |= _NEW_PROGRAM;
    655    }
    656 }
    657