Ignore:
Timestamp:
May 23, 2000, 10:41:28 PM (25 years ago)
Author:
jeroen
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/opengl/mesa/context.c

    r2970 r3598  
    1 /* $Id: context.c,v 1.3 2000-03-02 13:27:30 sandervl Exp $ */
     1/* $Id: context.c,v 1.4 2000-05-23 20:40:25 jeroen Exp $ */
    22
    33/*
    44 * Mesa 3-D graphics library
    5  * Version:  3.1
     5 * Version:  3.3
    66 *
    77 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
     
    2626
    2727
    28 /* $XFree86: xc/lib/GL/mesa/src/context.c,v 1.4 1999/04/04 00:20:21 dawes Exp $ */
    29 
    30 /*
    31  * If multi-threading is enabled (-DTHREADS) then each thread has it's
    32  * own rendering context.  A thread obtains the pointer to its GLcontext
    33  * with the gl_get_thread_context() function.  Otherwise, the global
    34  * pointer, CC, points to the current context used by all threads in
    35  * the address space.
    36  */
    37 
    38 #ifdef __WIN32OS2__
    39 #include <os2win.h>
    40 #ifdef DIVE
    41 #include "types.h"
    42 #include "wmesadef.h"
    43 #include "mesadive.h"
    44 #endif
    45 #endif
    46 
    4728#ifdef PC_HEADER
    4829#include "all.h"
    4930#else
    50 #ifndef XFree86Server
    51 #include <assert.h>
    52 #include <math.h>
    53 #include <stdio.h>
    54 #include <stdlib.h>
    55 #include <string.h>
    56 #else
    57 #include "GL/xf86glx.h"
    58 #endif
     31//#include "glheader.h"
    5932#include "accum.h"
     33#include "mem.h"
    6034#include "alphabuf.h"
    61 #include "api.h"
     35#include "glapi.h"
     36#include "glapinoop.h"
     37#include "glthread.h"
    6238#include "clip.h"
    6339#include "context.h"
     
    8056#include "pipeline.h"
    8157#include "points.h"
    82 #include "pointers.h"
    8358#include "quads.h"
    8459#include "shade.h"
     60#include "state.h"
    8561#include "simple_list.h"
    8662#include "stencil.h"
     
    10480#endif
    10581
    106 
    107 /*
    108  * Memory allocation functions.  Called via the MALLOC, CALLOC and
    109  * FREE macros when DEBUG symbol is defined.
    110  * You might want to set breakpoints on these functions or plug in
    111  * other memory allocation functions.  The Mesa sources should only
    112  * use the MALLOC and FREE macros (which could also be overriden).
    113  *
    114  * XXX these functions should probably go into a new glmemory.c file.
    115  */
    116 
    117 /*
    118  * Allocate memory (uninitialized)
    119  */
    120 void *gl_malloc(size_t bytes)
    121 {
    122    return malloc(bytes);
    123 }
    124 
    125 /*
    126  * Allocate memory and initialize to zero.
    127  */
    128 void *gl_calloc(size_t bytes)
    129 {
    130    return calloc(1, bytes);
    131 }
    132 
    133 /*
    134  * Free memory
    135  */
    136 void gl_free(void *ptr)
    137 {
    138    free(ptr);
    139 }
     82#ifdef __WIN32OS2__
     83                                       /* #include <os2win.h>              */
     84#ifdef DIVE
     85#include "types.h"
     86#include "wmesadef.h"
     87#include "mesadive.h"
     88#endif
     89#endif
    14090
    14191
     
    14595
    14696
    147 #ifdef THREADS
    148 
    149 #include "mthreads.h"        /* Mesa platform independent threads interface*/
    150 
    151 #ifdef __WIN32OS2__
    152 //SvL: Initialize this struct
    153 static MesaTSD mesa_ctx_tsd = {-1, -1};
    154 #else
    155 static MesaTSD mesa_ctx_tsd;
     97#if !defined(THREADS)
     98
     99struct immediate *_mesa_CurrentInput = NULL;
     100
    156101#endif
    157 
    158 static void mesa_ctx_thread_init() {
    159   MesaInitTSD(&mesa_ctx_tsd);
    160 }
    161 
    162 GLcontext *gl_get_thread_context( void ) {
    163 #ifdef __WIN32OS2__
    164   return (GLcontext *) MesaGetTSD(&mesa_ctx_tsd, mesa_ctx_thread_init);
    165 #else
    166   return (GLcontext *) MesaGetTSD(&mesa_ctx_tsd);
    167 #endif
    168 }
    169 
    170 static void set_thread_context( GLcontext *ctx ) {
    171   MesaSetTSD(&mesa_ctx_tsd, ctx, mesa_ctx_thread_init);
    172 }
    173 
    174 
    175 #else
    176 
    177 /* One Current Context pointer for all threads in the address space */
    178 GLcontext *CC = NULL;
    179 struct immediate *CURRENT_INPUT = NULL;
    180 
    181 #endif /*THREADS*/
    182102
    183103
     
    341261
    342262/**********************************************************************/
     263/***** GL Visual allocation/destruction                           *****/
     264/**********************************************************************/
     265
     266
     267/*
     268 * Allocate a new GLvisual object.
     269 * Input:  rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
     270 *         alphaFlag - alloc software alpha buffers?
     271 *         dbFlag - double buffering?
     272 *         stereoFlag - stereo buffer?
     273 *         depthBits - requested bits per depth buffer value
     274 *                     Any value in [0, 32] is acceptable but the actual
     275 *                     depth type will be GLushort or GLuint as needed.
     276 *         stencilBits - requested minimum bits per stencil buffer value
     277 *         accumBits - requested minimum bits per accum buffer component
     278 *         indexBits - number of bits per pixel if rgbFlag==GL_FALSE
     279 *         red/green/blue/alphaBits - number of bits per color component
     280 *                                    in frame buffer for RGB(A) mode.
     281 *                                    We always use 8 in core Mesa though.
     282 * Return:  pointer to new GLvisual or NULL if requested parameters can't
     283 *          be met.
     284 */
     285GLvisual *gl_create_visual( GLboolean rgbFlag,
     286                            GLboolean alphaFlag,
     287                            GLboolean dbFlag,
     288                            GLboolean stereoFlag,
     289                            GLint depthBits,
     290                            GLint stencilBits,
     291                            GLint accumBits,
     292                            GLint indexBits,
     293                            GLint redBits,
     294                            GLint greenBits,
     295                            GLint blueBits,
     296                            GLint alphaBits )
     297{
     298   GLvisual *vis;
     299
     300   /* This is to catch bad values from device drivers not updated for
     301    * Mesa 3.3.  Some device drivers just passed 1.  That's a REALLY
     302    * bad value now (a 1-bit depth buffer!?!).
     303    */
     304   ASSERT(depthBits == 0 || depthBits > 1);
     305
     306   if (depthBits < 0 || depthBits > 32) {
     307      return NULL;
     308   }
     309   if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
     310      return NULL;
     311   }
     312   if (accumBits < 0 || accumBits > (GLint) (8 * sizeof(GLaccum))) {
     313      return NULL;
     314   }
     315
     316   vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
     317   if (!vis) {
     318      return NULL;
     319   }
     320
     321   vis->RGBAflag   = rgbFlag;
     322   vis->DBflag     = dbFlag;
     323   vis->StereoFlag = stereoFlag;
     324   vis->RedBits    = redBits;
     325   vis->GreenBits  = greenBits;
     326   vis->BlueBits   = blueBits;
     327   vis->AlphaBits  = alphaFlag ? (8 * sizeof(GLubyte)) : alphaBits;
     328
     329   vis->IndexBits   = indexBits;
     330   vis->DepthBits   = depthBits;
     331   vis->AccumBits   = (accumBits > 0) ? (8 * sizeof(GLaccum)) : 0;
     332   vis->StencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
     333
     334   vis->SoftwareAlpha = alphaFlag;
     335
     336   if (depthBits == 0) {
     337      /* Special case.  Even if we don't have a depth buffer we need
     338       * good values for DepthMax for Z vertex transformation purposes.
     339       */
     340      vis->DepthMax = 1;
     341      vis->DepthMaxF = 1.0F;
     342   }
     343   else {
     344      vis->DepthMax = (1 << depthBits) - 1;
     345      vis->DepthMaxF = (GLfloat) vis->DepthMax;
     346   }
     347
     348   return vis;
     349}
     350
     351
     352
     353void gl_destroy_visual( GLvisual *vis )
     354{
     355   FREE( vis );
     356}
     357
     358
     359
     360/**********************************************************************/
     361/***** GL Framebuffer allocation/destruction                      *****/
     362/**********************************************************************/
     363
     364
     365/*
     366 * Create a new framebuffer.  A GLframebuffer is a struct which
     367 * encapsulates the depth, stencil and accum buffers and related
     368 * parameters.
     369 * Input:  visual - a GLvisual pointer
     370 *         softwareDepth - create/use a software depth buffer?
     371 *         softwareStencil - create/use a software stencil buffer?
     372 *         softwareAccum - create/use a software accum buffer?
     373 *         softwareAlpha - create/use a software alpha buffer?
     374 *
     375 * Return:  pointer to new GLframebuffer struct or NULL if error.
     376 */
     377GLframebuffer *gl_create_framebuffer( GLvisual *visual,
     378                                      GLboolean softwareDepth,
     379                                      GLboolean softwareStencil,
     380                                      GLboolean softwareAccum,
     381                                      GLboolean softwareAlpha )
     382{
     383   GLframebuffer *buffer;
     384
     385   buffer = CALLOC_STRUCT(gl_frame_buffer);
     386   if (!buffer) {
     387      return NULL;
     388   }
     389
     390   /* sanity checks */
     391   if (softwareDepth ) {
     392      ASSERT(visual->DepthBits > 0);
     393   }
     394   if (softwareStencil) {
     395      ASSERT(visual->StencilBits > 0);
     396   }
     397   if (softwareAccum) {
     398      ASSERT(visual->RGBAflag);
     399      ASSERT(visual->AccumBits > 0);
     400   }
     401   if (softwareAlpha) {
     402      ASSERT(visual->RGBAflag);
     403      ASSERT(visual->AlphaBits > 0);
     404   }
     405
     406   buffer->Visual = visual;
     407   buffer->UseSoftwareDepthBuffer = softwareDepth;
     408   buffer->UseSoftwareStencilBuffer = softwareStencil;
     409   buffer->UseSoftwareAccumBuffer = softwareAccum;
     410   buffer->UseSoftwareAlphaBuffers = softwareAlpha;
     411
     412   return buffer;
     413}
     414
     415
     416
     417/*
     418 * Free a framebuffer struct and its buffers.
     419 */
     420void gl_destroy_framebuffer( GLframebuffer *buffer )
     421{
     422   if (buffer) {
     423      if (buffer->DepthBuffer) {
     424         FREE( buffer->DepthBuffer );
     425      }
     426      if (buffer->Accum) {
     427         FREE( buffer->Accum );
     428      }
     429      if (buffer->Stencil) {
     430         FREE( buffer->Stencil );
     431      }
     432      if (buffer->FrontLeftAlpha) {
     433         FREE( buffer->FrontLeftAlpha );
     434      }
     435      if (buffer->BackLeftAlpha) {
     436         FREE( buffer->BackLeftAlpha );
     437      }
     438      if (buffer->FrontRightAlpha) {
     439         FREE( buffer->FrontRightAlpha );
     440      }
     441      if (buffer->BackRightAlpha) {
     442         FREE( buffer->BackRightAlpha );
     443      }
     444      FREE(buffer);
     445   }
     446}
     447
     448
     449
     450/**********************************************************************/
    343451/*****       Context allocation, initialization, destroying       *****/
    344452/**********************************************************************/
    345453
    346454
     455_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
     456
     457
    347458/*
    348459 * This function just calls all the various one-time-init functions in Mesa.
     
    351462{
    352463   static GLboolean alreadyCalled = GL_FALSE;
    353 
     464   _glthread_LOCK_MUTEX(OneTimeLock);
    354465   if (!alreadyCalled) {
     466      /* do some implementation tests */
     467      ASSERT( sizeof(GLbyte) == 1 );
     468      ASSERT( sizeof(GLshort) >= 2 );
     469      ASSERT( sizeof(GLint) >= 4 );
     470      ASSERT( sizeof(GLubyte) == 1 );
     471      ASSERT( sizeof(GLushort) >= 2 );
     472      ASSERT( sizeof(GLuint) >= 4 );
     473
    355474      gl_init_clip();
    356475      gl_init_eval();
    357       gl_init_fog();
    358       gl_init_math();
     476      _mesa_init_fog();
     477      _mesa_init_math();
    359478      gl_init_lists();
    360479      gl_init_shade();
     
    365484      gl_init_vbxform();
    366485      gl_init_vertices();
    367 #ifdef THREADS
    368       mesa_ctx_thread_init();
    369 #endif
    370       alreadyCalled = GL_TRUE;
    371    }
     486
     487      if (getenv("MESA_DEBUG")) {
     488         _glapi_noop_enable_warnings(GL_TRUE);
     489      }
     490      else {
     491         _glapi_noop_enable_warnings(GL_FALSE);
     492      }
     493
    372494#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
    373495   fprintf(stderr, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
    374496#endif
    375 }
     497
     498      alreadyCalled = GL_TRUE;
     499   }
     500   _glthread_UNLOCK_MUTEX(OneTimeLock);
     501}
     502
    376503
    377504
     
    389516      return NULL;
    390517
    391    ss->DisplayList = NewHashTable();
    392 
    393    ss->TexObjects = NewHashTable();
     518   ss->DisplayList = _mesa_NewHashTable();
     519
     520   ss->TexObjects = _mesa_NewHashTable();
    394521
    395522   /* Default Texture objects */
     
    407534      /* Ran out of memory at some point.  Free everything and return NULL */
    408535      if (ss->DisplayList)
    409          DeleteHashTable(ss->DisplayList);
     536         _mesa_DeleteHashTable(ss->DisplayList);
    410537      if (ss->TexObjects)
    411          DeleteHashTable(ss->TexObjects);
     538         _mesa_DeleteHashTable(ss->TexObjects);
    412539      if (ss->DefaultD[1])
    413540         gl_free_texture_object(ss, ss->DefaultD[1]);
     
    432559   /* Free display lists */
    433560   while (1) {
    434       GLuint list = HashFirstEntry(ss->DisplayList);
     561      GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
    435562      if (list) {
    436563         gl_destroy_list(ctx, list);
     
    440567      }
    441568   }
    442    DeleteHashTable(ss->DisplayList);
     569   _mesa_DeleteHashTable(ss->DisplayList);
    443570
    444571   /* Free texture objects */
     
    450577      gl_free_texture_object(ss, ss->TexObjectList);
    451578   }
    452    DeleteHashTable(ss->TexObjects);
     579   _mesa_DeleteHashTable(ss->TexObjects);
    453580
    454581   FREE(ss);
    455582}
    456 
    457 
    458 
    459583
    460584
     
    589713}
    590714
     715
    591716/* Initialize a 1-D evaluator map */
    592717static void init_1d_map( struct gl_1d_map *map, int n, const float *initial )
     
    601726         map->Points[i] = initial[i];
    602727   }
    603    map->Retain = GL_FALSE;
    604728}
    605729
     
    620744         map->Points[i] = initial[i];
    621745   }
    622    map->Retain = GL_FALSE;
    623 }
    624 
    625 
    626 
    627 /*
    628  * Initialize a gl_context structure to default values.
    629  */
    630 static void initialize_context( GLcontext *ctx )
     746}
     747
     748
     749static void init_color_table( struct gl_color_table *p )
     750{
     751   p->Table[0] = 255;
     752   p->Table[1] = 255;
     753   p->Table[2] = 255;
     754   p->Table[3] = 255;
     755   p->Size = 1;
     756   p->IntFormat = GL_RGBA;
     757   p->Format = GL_RGBA;
     758}
     759
     760
     761/*
     762 * Initialize the attribute groups in a GLcontext.
     763 */
     764static void init_attrib_groups( GLcontext *ctx )
    631765{
    632766   GLuint i, j;
    633767
    634    if (ctx) {
    635       /* Constants, may be overriden by device driver */
    636       ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
    637       ctx->Const.MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
    638       ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
    639       ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
    640 
    641       /* Modelview matrix */
    642       gl_matrix_ctr( &ctx->ModelView );
    643       gl_matrix_alloc_inv( &ctx->ModelView );
    644 
    645       ctx->ModelViewStackDepth = 0;
    646       for (i = 0 ; i < MAX_MODELVIEW_STACK_DEPTH ; i++) {
    647          gl_matrix_ctr( &ctx->ModelViewStack[i] );
    648          gl_matrix_alloc_inv( &ctx->ModelViewStack[i] );
    649       }
    650 
    651       /* Projection matrix - need inv for user clipping in clip space*/
    652       gl_matrix_ctr( &ctx->ProjectionMatrix );
    653       gl_matrix_alloc_inv( &ctx->ProjectionMatrix );
    654 
    655       gl_matrix_ctr( &ctx->ModelProjectMatrix );
    656       gl_matrix_ctr( &ctx->ModelProjectWinMatrix );
    657       ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
    658 
    659       ctx->ProjectionStackDepth = 0;
    660       ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
    661       ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
    662 
    663       for (i = 0 ; i < MAX_PROJECTION_STACK_DEPTH ; i++) {
    664          gl_matrix_ctr( &ctx->ProjectionStack[i] );
    665          gl_matrix_alloc_inv( &ctx->ProjectionStack[i] );
    666       }
    667 
    668       /* Texture matrix */
    669       for (i=0; i<MAX_TEXTURE_UNITS; i++) {
    670          gl_matrix_ctr( &ctx->TextureMatrix[i] );
    671          ctx->TextureStackDepth[i] = 0;
    672          for (j = 0 ; j < MAX_TEXTURE_STACK_DEPTH ; j++) {
    673             ctx->TextureStack[i][j].inv = 0;
    674          }
    675       }
    676 
    677       /* Accumulate buffer group */
    678       ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
    679 
    680       /* Color buffer group */
    681       ctx->Color.IndexMask = 0xffffffff;
    682       ctx->Color.ColorMask[0] = 0xff;
    683       ctx->Color.ColorMask[1] = 0xff;
    684       ctx->Color.ColorMask[2] = 0xff;
    685       ctx->Color.ColorMask[3] = 0xff;
    686       ctx->Color.SWmasking = GL_FALSE;
    687       ctx->Color.ClearIndex = 0;
    688       ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
    689       ctx->Color.DrawBuffer = GL_FRONT;
    690       ctx->Color.AlphaEnabled = GL_FALSE;
    691       ctx->Color.AlphaFunc = GL_ALWAYS;
    692       ctx->Color.AlphaRef = 0;
    693       ctx->Color.BlendEnabled = GL_FALSE;
    694       ctx->Color.BlendSrcRGB = GL_ONE;
    695       ctx->Color.BlendDstRGB = GL_ZERO;
    696       ctx->Color.BlendSrcA = GL_ONE;
    697       ctx->Color.BlendDstA = GL_ZERO;
    698       ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
    699       ctx->Color.BlendFunc = NULL;  /* this pointer set only when needed */
    700       ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
    701       ctx->Color.IndexLogicOpEnabled = GL_FALSE;
    702       ctx->Color.ColorLogicOpEnabled = GL_FALSE;
    703       ctx->Color.SWLogicOpEnabled = GL_FALSE;
    704       ctx->Color.LogicOp = GL_COPY;
    705       ctx->Color.DitherFlag = GL_TRUE;
    706       ctx->Color.MultiDrawBuffer = GL_FALSE;
    707 
    708       /* Current group */
    709       ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255);
    710       ctx->Current.Index = 1;
    711       for (i=0; i<MAX_TEXTURE_UNITS; i++)
    712          ASSIGN_4V( ctx->Current.Texcoord[i], 0.0, 0.0, 0.0, 1.0 );
    713       ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
    714       ctx->Current.RasterDistance = 0.0;
    715       ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
    716       ctx->Current.RasterIndex = 1;
    717       for (i=0; i<MAX_TEXTURE_UNITS; i++)
    718          ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
    719       ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
    720       ctx->Current.RasterPosValid = GL_TRUE;
    721       ctx->Current.EdgeFlag = GL_TRUE;
    722       ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
    723       ctx->Current.Primitive = (GLenum) (GL_POLYGON + 1);
    724 
    725       ctx->Current.Flag = (VERT_NORM|VERT_INDEX|VERT_RGBA|VERT_EDGE|
    726                            VERT_TEX0_1|VERT_TEX1_1|VERT_MATERIAL);
    727 
    728       init_fallback_arrays( ctx );
    729 
    730       /* Depth buffer group */
    731       ctx->Depth.Test = GL_FALSE;
    732       ctx->Depth.Clear = 1.0;
    733       ctx->Depth.Func = GL_LESS;
    734       ctx->Depth.Mask = GL_TRUE;
    735 
    736       /* Evaluators group */
    737       ctx->Eval.Map1Color4 = GL_FALSE;
    738       ctx->Eval.Map1Index = GL_FALSE;
    739       ctx->Eval.Map1Normal = GL_FALSE;
    740       ctx->Eval.Map1TextureCoord1 = GL_FALSE;
    741       ctx->Eval.Map1TextureCoord2 = GL_FALSE;
    742       ctx->Eval.Map1TextureCoord3 = GL_FALSE;
    743       ctx->Eval.Map1TextureCoord4 = GL_FALSE;
    744       ctx->Eval.Map1Vertex3 = GL_FALSE;
    745       ctx->Eval.Map1Vertex4 = GL_FALSE;
    746       ctx->Eval.Map2Color4 = GL_FALSE;
    747       ctx->Eval.Map2Index = GL_FALSE;
    748       ctx->Eval.Map2Normal = GL_FALSE;
    749       ctx->Eval.Map2TextureCoord1 = GL_FALSE;
    750       ctx->Eval.Map2TextureCoord2 = GL_FALSE;
    751       ctx->Eval.Map2TextureCoord3 = GL_FALSE;
    752       ctx->Eval.Map2TextureCoord4 = GL_FALSE;
    753       ctx->Eval.Map2Vertex3 = GL_FALSE;
    754       ctx->Eval.Map2Vertex4 = GL_FALSE;
    755       ctx->Eval.AutoNormal = GL_FALSE;
    756       ctx->Eval.MapGrid1un = 1;
    757       ctx->Eval.MapGrid1u1 = 0.0;
    758       ctx->Eval.MapGrid1u2 = 1.0;
    759       ctx->Eval.MapGrid2un = 1;
    760       ctx->Eval.MapGrid2vn = 1;
    761       ctx->Eval.MapGrid2u1 = 0.0;
    762       ctx->Eval.MapGrid2u2 = 1.0;
    763       ctx->Eval.MapGrid2v1 = 0.0;
    764       ctx->Eval.MapGrid2v2 = 1.0;
    765 
    766       /* Evaluator data */
    767       {
    768          static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
    769          static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
    770          static GLfloat index[1] = { 1.0 };
    771          static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
    772          static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
    773 
    774          init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
    775          init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
    776          init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
    777          init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
    778          init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
    779          init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
    780          init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
    781          init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
    782          init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
    783 
    784          init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
    785          init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
    786          init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
    787          init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
    788          init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
    789          init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
    790          init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
    791          init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
    792          init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
    793       }
    794 
    795       /* Fog group */
    796       ctx->Fog.Enabled = GL_FALSE;
    797       ctx->Fog.Mode = GL_EXP;
    798       ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
    799       ctx->Fog.Index = 0.0;
    800       ctx->Fog.Density = 1.0;
    801       ctx->Fog.Start = 0.0;
    802       ctx->Fog.End = 1.0;
    803 
    804       /* Hint group */
    805       ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
    806       ctx->Hint.PointSmooth = GL_DONT_CARE;
    807       ctx->Hint.LineSmooth = GL_DONT_CARE;
    808       ctx->Hint.PolygonSmooth = GL_DONT_CARE;
    809       ctx->Hint.Fog = GL_DONT_CARE;
    810 
    811       ctx->Hint.AllowDrawWin = GL_TRUE;
    812       ctx->Hint.AllowDrawSpn = GL_TRUE;
    813       ctx->Hint.AllowDrawMem = GL_TRUE;
    814       ctx->Hint.StrictLighting = GL_TRUE;
    815 
    816       /* Pipeline */
    817       gl_pipeline_init( ctx );
    818       gl_cva_init( ctx );
    819 
    820       /* Extensions */
    821       gl_extensions_ctr( ctx );
    822 
    823       ctx->AllowVertexCull = CLIP_CULLED_BIT;
    824 
    825       /* Lighting group */
    826       for (i=0;i<MAX_LIGHTS;i++) {
    827          init_light( &ctx->Light.Light[i], i );
    828       }
    829       make_empty_list( &ctx->Light.EnabledList );
    830 
    831       init_lightmodel( &ctx->Light.Model );
    832       init_material( &ctx->Light.Material[0] );
    833       init_material( &ctx->Light.Material[1] );
    834       ctx->Light.ShadeModel = GL_SMOOTH;
    835       ctx->Light.Enabled = GL_FALSE;
    836       ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
    837       ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
    838       ctx->Light.ColorMaterialBitmask
    839          = gl_material_bitmask( ctx,
    840                                 GL_FRONT_AND_BACK,
    841                                 GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
    842 
    843       ctx->Light.ColorMaterialEnabled = GL_FALSE;
    844 
    845       /* Line group */
    846       ctx->Line.SmoothFlag = GL_FALSE;
    847       ctx->Line.StippleFlag = GL_FALSE;
    848       ctx->Line.Width = 1.0;
    849       ctx->Line.StipplePattern = 0xffff;
    850       ctx->Line.StippleFactor = 1;
    851 
    852       /* Display List group */
    853       ctx->List.ListBase = 0;
    854 
    855       /* Pixel group */
    856       ctx->Pixel.RedBias = 0.0;
    857       ctx->Pixel.RedScale = 1.0;
    858       ctx->Pixel.GreenBias = 0.0;
    859       ctx->Pixel.GreenScale = 1.0;
    860       ctx->Pixel.BlueBias = 0.0;
    861       ctx->Pixel.BlueScale = 1.0;
    862       ctx->Pixel.AlphaBias = 0.0;
    863       ctx->Pixel.AlphaScale = 1.0;
    864       ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
    865       ctx->Pixel.DepthBias = 0.0;
    866       ctx->Pixel.DepthScale = 1.0;
    867       ctx->Pixel.IndexOffset = 0;
    868       ctx->Pixel.IndexShift = 0;
    869       ctx->Pixel.ZoomX = 1.0;
    870       ctx->Pixel.ZoomY = 1.0;
    871       ctx->Pixel.MapColorFlag = GL_FALSE;
    872       ctx->Pixel.MapStencilFlag = GL_FALSE;
    873       ctx->Pixel.MapStoSsize = 1;
    874       ctx->Pixel.MapItoIsize = 1;
    875       ctx->Pixel.MapItoRsize = 1;
    876       ctx->Pixel.MapItoGsize = 1;
    877       ctx->Pixel.MapItoBsize = 1;
    878       ctx->Pixel.MapItoAsize = 1;
    879       ctx->Pixel.MapRtoRsize = 1;
    880       ctx->Pixel.MapGtoGsize = 1;
    881       ctx->Pixel.MapBtoBsize = 1;
    882       ctx->Pixel.MapAtoAsize = 1;
    883       ctx->Pixel.MapStoS[0] = 0;
    884       ctx->Pixel.MapItoI[0] = 0;
    885       ctx->Pixel.MapItoR[0] = 0.0;
    886       ctx->Pixel.MapItoG[0] = 0.0;
    887       ctx->Pixel.MapItoB[0] = 0.0;
    888       ctx->Pixel.MapItoA[0] = 0.0;
    889       ctx->Pixel.MapItoR8[0] = 0;
    890       ctx->Pixel.MapItoG8[0] = 0;
    891       ctx->Pixel.MapItoB8[0] = 0;
    892       ctx->Pixel.MapItoA8[0] = 0;
    893       ctx->Pixel.MapRtoR[0] = 0.0;
    894       ctx->Pixel.MapGtoG[0] = 0.0;
    895       ctx->Pixel.MapBtoB[0] = 0.0;
    896       ctx->Pixel.MapAtoA[0] = 0.0;
    897 
    898       /* Point group */
    899       ctx->Point.SmoothFlag = GL_FALSE;
    900       ctx->Point.Size = 1.0;
    901       ctx->Point.Params[0] = 1.0;
    902       ctx->Point.Params[1] = 0.0;
    903       ctx->Point.Params[2] = 0.0;
    904       ctx->Point.Attenuated = GL_FALSE;
    905       ctx->Point.MinSize = 0.0;
    906       ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
    907       ctx->Point.Threshold = 1.0;
    908 
    909       /* Polygon group */
    910       ctx->Polygon.CullFlag = GL_FALSE;
    911       ctx->Polygon.CullFaceMode = GL_BACK;
    912       ctx->Polygon.FrontFace = GL_CCW;
    913       ctx->Polygon.FrontBit = 0;
    914       ctx->Polygon.FrontMode = GL_FILL;
    915       ctx->Polygon.BackMode = GL_FILL;
    916       ctx->Polygon.Unfilled = GL_FALSE;
    917       ctx->Polygon.SmoothFlag = GL_FALSE;
    918       ctx->Polygon.StippleFlag = GL_FALSE;
    919       ctx->Polygon.OffsetFactor = 0.0F;
    920       ctx->Polygon.OffsetUnits = 0.0F;
    921       ctx->Polygon.OffsetPoint = GL_FALSE;
    922       ctx->Polygon.OffsetLine = GL_FALSE;
    923       ctx->Polygon.OffsetFill = GL_FALSE;
    924 
    925       /* Polygon Stipple group */
    926       MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
    927 
    928       /* Scissor group */
    929       ctx->Scissor.Enabled = GL_FALSE;
    930       ctx->Scissor.X = 0;
    931       ctx->Scissor.Y = 0;
    932       ctx->Scissor.Width = 0;
    933       ctx->Scissor.Height = 0;
    934 
    935       /* Stencil group */
    936       ctx->Stencil.Enabled = GL_FALSE;
    937       ctx->Stencil.Function = GL_ALWAYS;
    938       ctx->Stencil.FailFunc = GL_KEEP;
    939       ctx->Stencil.ZPassFunc = GL_KEEP;
    940       ctx->Stencil.ZFailFunc = GL_KEEP;
    941       ctx->Stencil.Ref = 0;
    942       ctx->Stencil.ValueMask = STENCIL_MAX;
    943       ctx->Stencil.Clear = 0;
    944       ctx->Stencil.WriteMask = STENCIL_MAX;
    945 
    946       /* Texture group */
    947       ctx->Texture.CurrentUnit = 0;      /* multitexture */
    948       ctx->Texture.CurrentTransformUnit = 0; /* multitexture */
    949       ctx->Texture.Enabled = 0;
    950 
    951       for (i=0; i<MAX_TEXTURE_UNITS; i++)
    952          init_texture_unit( ctx, i );
    953 
    954       ctx->Texture.SharedPalette = GL_FALSE;
    955       ctx->Texture.Palette[0] = 255;
    956       ctx->Texture.Palette[1] = 255;
    957       ctx->Texture.Palette[2] = 255;
    958       ctx->Texture.Palette[3] = 255;
    959       ctx->Texture.PaletteSize = 1;
    960       ctx->Texture.PaletteIntFormat = GL_RGBA;
    961       ctx->Texture.PaletteFormat = GL_RGBA;
    962 
    963       /* Transformation group */
    964       ctx->Transform.MatrixMode = GL_MODELVIEW;
    965       ctx->Transform.Normalize = GL_FALSE;
    966       ctx->Transform.RescaleNormals = GL_FALSE;
    967       for (i=0;i<MAX_CLIP_PLANES;i++) {
    968          ctx->Transform.ClipEnabled[i] = GL_FALSE;
    969          ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
    970       }
    971       ctx->Transform.AnyClip = GL_FALSE;
    972 
    973       /* Viewport group */
    974       ctx->Viewport.X = 0;
    975       ctx->Viewport.Y = 0;
    976       ctx->Viewport.Width = 0;
    977       ctx->Viewport.Height = 0;
    978       ctx->Viewport.Near = 0.0;
    979       ctx->Viewport.Far = 1.0;
    980       gl_matrix_ctr(&ctx->Viewport.WindowMap);
     768   ASSERT(ctx);
     769
     770   /* Constants, may be overriden by device drivers */
     771   ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
     772   ctx->Const.MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
     773   ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
     774   ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
     775   ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
     776   ctx->Const.MinPointSize = MIN_POINT_SIZE;
     777   ctx->Const.MaxPointSize = MAX_POINT_SIZE;
     778   ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
     779   ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
     780   ctx->Const.PointSizeGranularity = POINT_SIZE_GRANULARITY;
     781   ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
     782   ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
     783   ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
     784   ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
     785   ctx->Const.LineWidthGranularity = LINE_WIDTH_GRANULARITY;
     786   ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
     787
     788   /* Modelview matrix */
     789   gl_matrix_ctr( &ctx->ModelView );
     790   gl_matrix_alloc_inv( &ctx->ModelView );
     791
     792   ctx->ModelViewStackDepth = 0;
     793   for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
     794      gl_matrix_ctr( &ctx->ModelViewStack[i] );
     795      gl_matrix_alloc_inv( &ctx->ModelViewStack[i] );
     796   }
     797
     798   /* Projection matrix - need inv for user clipping in clip space*/
     799   gl_matrix_ctr( &ctx->ProjectionMatrix );
     800   gl_matrix_alloc_inv( &ctx->ProjectionMatrix );
     801
     802   gl_matrix_ctr( &ctx->ModelProjectMatrix );
     803   gl_matrix_ctr( &ctx->ModelProjectWinMatrix );
     804   ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
     805
     806   ctx->ProjectionStackDepth = 0;
     807   ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
     808   ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
     809
     810   for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
     811      gl_matrix_ctr( &ctx->ProjectionStack[i] );
     812      gl_matrix_alloc_inv( &ctx->ProjectionStack[i] );
     813   }
     814
     815   /* Texture matrix */
     816   for (i=0; i<MAX_TEXTURE_UNITS; i++) {
     817      gl_matrix_ctr( &ctx->TextureMatrix[i] );
     818      ctx->TextureStackDepth[i] = 0;
     819      for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
     820         ctx->TextureStack[i][j].inv = 0;
     821      }
     822   }
     823
     824   /* Accumulate buffer group */
     825   ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
     826
     827   /* Color buffer group */
     828   ctx->Color.IndexMask = 0xffffffff;
     829   ctx->Color.ColorMask[0] = 0xff;
     830   ctx->Color.ColorMask[1] = 0xff;
     831   ctx->Color.ColorMask[2] = 0xff;
     832   ctx->Color.ColorMask[3] = 0xff;
     833   ctx->Color.SWmasking = GL_FALSE;
     834   ctx->Color.ClearIndex = 0;
     835   ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
     836   ctx->Color.DrawBuffer = GL_FRONT;
     837   ctx->Color.AlphaEnabled = GL_FALSE;
     838   ctx->Color.AlphaFunc = GL_ALWAYS;
     839   ctx->Color.AlphaRef = 0;
     840   ctx->Color.BlendEnabled = GL_FALSE;
     841   ctx->Color.BlendSrcRGB = GL_ONE;
     842   ctx->Color.BlendDstRGB = GL_ZERO;
     843   ctx->Color.BlendSrcA = GL_ONE;
     844   ctx->Color.BlendDstA = GL_ZERO;
     845   ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
     846   ctx->Color.BlendFunc = NULL;  /* this pointer set only when needed */
     847   ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
     848   ctx->Color.IndexLogicOpEnabled = GL_FALSE;
     849   ctx->Color.ColorLogicOpEnabled = GL_FALSE;
     850   ctx->Color.SWLogicOpEnabled = GL_FALSE;
     851   ctx->Color.LogicOp = GL_COPY;
     852   ctx->Color.DitherFlag = GL_TRUE;
     853   ctx->Color.MultiDrawBuffer = GL_FALSE;
     854
     855   /* Current group */
     856   ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255);
     857   ctx->Current.Index = 1;
     858   for (i=0; i<MAX_TEXTURE_UNITS; i++)
     859      ASSIGN_4V( ctx->Current.Texcoord[i], 0.0, 0.0, 0.0, 1.0 );
     860   ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
     861   ctx->Current.RasterDistance = 0.0;
     862   ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
     863   ctx->Current.RasterIndex = 1;
     864   for (i=0; i<MAX_TEXTURE_UNITS; i++)
     865      ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
     866   ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
     867   ctx->Current.RasterPosValid = GL_TRUE;
     868   ctx->Current.EdgeFlag = GL_TRUE;
     869   ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
     870   ctx->Current.Primitive = (GLenum) (GL_POLYGON + 1);
     871
     872   ctx->Current.Flag = (VERT_NORM|VERT_INDEX|VERT_RGBA|VERT_EDGE|
     873                        VERT_TEX0_1|VERT_TEX1_1|VERT_MATERIAL);
     874
     875   init_fallback_arrays( ctx );
     876
     877   /* Depth buffer group */
     878   ctx->Depth.Test = GL_FALSE;
     879   ctx->Depth.Clear = 1.0;
     880   ctx->Depth.Func = GL_LESS;
     881   ctx->Depth.Mask = GL_TRUE;
     882   ctx->Depth.OcclusionTest = GL_FALSE;
     883
     884   /* Evaluators group */
     885   ctx->Eval.Map1Color4 = GL_FALSE;
     886   ctx->Eval.Map1Index = GL_FALSE;
     887   ctx->Eval.Map1Normal = GL_FALSE;
     888   ctx->Eval.Map1TextureCoord1 = GL_FALSE;
     889   ctx->Eval.Map1TextureCoord2 = GL_FALSE;
     890   ctx->Eval.Map1TextureCoord3 = GL_FALSE;
     891   ctx->Eval.Map1TextureCoord4 = GL_FALSE;
     892   ctx->Eval.Map1Vertex3 = GL_FALSE;
     893   ctx->Eval.Map1Vertex4 = GL_FALSE;
     894   ctx->Eval.Map2Color4 = GL_FALSE;
     895   ctx->Eval.Map2Index = GL_FALSE;
     896   ctx->Eval.Map2Normal = GL_FALSE;
     897   ctx->Eval.Map2TextureCoord1 = GL_FALSE;
     898   ctx->Eval.Map2TextureCoord2 = GL_FALSE;
     899   ctx->Eval.Map2TextureCoord3 = GL_FALSE;
     900   ctx->Eval.Map2TextureCoord4 = GL_FALSE;
     901   ctx->Eval.Map2Vertex3 = GL_FALSE;
     902   ctx->Eval.Map2Vertex4 = GL_FALSE;
     903   ctx->Eval.AutoNormal = GL_FALSE;
     904   ctx->Eval.MapGrid1un = 1;
     905   ctx->Eval.MapGrid1u1 = 0.0;
     906   ctx->Eval.MapGrid1u2 = 1.0;
     907   ctx->Eval.MapGrid2un = 1;
     908   ctx->Eval.MapGrid2vn = 1;
     909   ctx->Eval.MapGrid2u1 = 0.0;
     910   ctx->Eval.MapGrid2u2 = 1.0;
     911   ctx->Eval.MapGrid2v1 = 0.0;
     912   ctx->Eval.MapGrid2v2 = 1.0;
     913
     914   /* Evaluator data */
     915   {
     916      static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
     917      static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
     918      static GLfloat index[1] = { 1.0 };
     919      static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
     920      static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
     921
     922      init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
     923      init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
     924      init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
     925      init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
     926      init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
     927      init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
     928      init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
     929      init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
     930      init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
     931
     932      init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
     933      init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
     934      init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
     935      init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
     936      init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
     937      init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
     938      init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
     939      init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
     940      init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
     941   }
     942
     943   /* Fog group */
     944   ctx->Fog.Enabled = GL_FALSE;
     945   ctx->Fog.Mode = GL_EXP;
     946   ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
     947   ctx->Fog.Index = 0.0;
     948   ctx->Fog.Density = 1.0;
     949   ctx->Fog.Start = 0.0;
     950   ctx->Fog.End = 1.0;
     951
     952   /* Hint group */
     953   ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
     954   ctx->Hint.PointSmooth = GL_DONT_CARE;
     955   ctx->Hint.LineSmooth = GL_DONT_CARE;
     956   ctx->Hint.PolygonSmooth = GL_DONT_CARE;
     957   ctx->Hint.Fog = GL_DONT_CARE;
     958
     959   ctx->Hint.AllowDrawWin = GL_TRUE;
     960   ctx->Hint.AllowDrawSpn = GL_TRUE;
     961   ctx->Hint.AllowDrawMem = GL_TRUE;
     962   ctx->Hint.StrictLighting = GL_TRUE;
     963
     964   /* Pipeline */
     965   gl_pipeline_init( ctx );
     966   gl_cva_init( ctx );
     967
     968   /* Extensions */
     969   gl_extensions_ctr( ctx );
     970
     971   ctx->AllowVertexCull = CLIP_CULLED_BIT;
     972
     973   /* Lighting group */
     974   for (i=0;i<MAX_LIGHTS;i++) {
     975      init_light( &ctx->Light.Light[i], i );
     976   }
     977   make_empty_list( &ctx->Light.EnabledList );
     978
     979   init_lightmodel( &ctx->Light.Model );
     980   init_material( &ctx->Light.Material[0] );
     981   init_material( &ctx->Light.Material[1] );
     982   ctx->Light.ShadeModel = GL_SMOOTH;
     983   ctx->Light.Enabled = GL_FALSE;
     984   ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
     985   ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
     986   ctx->Light.ColorMaterialBitmask
     987      = gl_material_bitmask( ctx,
     988                             GL_FRONT_AND_BACK,
     989                             GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
     990
     991   ctx->Light.ColorMaterialEnabled = GL_FALSE;
     992
     993   /* Lighting miscellaneous */
     994   ctx->ShineTabList = MALLOC_STRUCT( gl_shine_tab );
     995   make_empty_list( ctx->ShineTabList );
     996   for (i = 0 ; i < 10 ; i++) {
     997      struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
     998      s->shininess = -1;
     999      s->refcount = 0;
     1000      insert_at_tail( ctx->ShineTabList, s );
     1001   }
     1002   for (i = 0 ; i < 4 ; i++) {
     1003      ctx->ShineTable[i] = ctx->ShineTabList->prev;
     1004      ctx->ShineTable[i]->refcount++;
     1005   }
     1006
     1007
     1008   /* Line group */
     1009   ctx->Line.SmoothFlag = GL_FALSE;
     1010   ctx->Line.StippleFlag = GL_FALSE;
     1011   ctx->Line.Width = 1.0;
     1012   ctx->Line.StipplePattern = 0xffff;
     1013   ctx->Line.StippleFactor = 1;
     1014
     1015   /* Display List group */
     1016   ctx->List.ListBase = 0;
     1017
     1018   /* Pixel group */
     1019   ctx->Pixel.RedBias = 0.0;
     1020   ctx->Pixel.RedScale = 1.0;
     1021   ctx->Pixel.GreenBias = 0.0;
     1022   ctx->Pixel.GreenScale = 1.0;
     1023   ctx->Pixel.BlueBias = 0.0;
     1024   ctx->Pixel.BlueScale = 1.0;
     1025   ctx->Pixel.AlphaBias = 0.0;
     1026   ctx->Pixel.AlphaScale = 1.0;
     1027   ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
     1028   ctx->Pixel.DepthBias = 0.0;
     1029   ctx->Pixel.DepthScale = 1.0;
     1030   ctx->Pixel.IndexOffset = 0;
     1031   ctx->Pixel.IndexShift = 0;
     1032   ctx->Pixel.ZoomX = 1.0;
     1033   ctx->Pixel.ZoomY = 1.0;
     1034   ctx->Pixel.MapColorFlag = GL_FALSE;
     1035   ctx->Pixel.MapStencilFlag = GL_FALSE;
     1036   ctx->Pixel.MapStoSsize = 1;
     1037   ctx->Pixel.MapItoIsize = 1;
     1038   ctx->Pixel.MapItoRsize = 1;
     1039   ctx->Pixel.MapItoGsize = 1;
     1040   ctx->Pixel.MapItoBsize = 1;
     1041   ctx->Pixel.MapItoAsize = 1;
     1042   ctx->Pixel.MapRtoRsize = 1;
     1043   ctx->Pixel.MapGtoGsize = 1;
     1044   ctx->Pixel.MapBtoBsize = 1;
     1045   ctx->Pixel.MapAtoAsize = 1;
     1046   ctx->Pixel.MapStoS[0] = 0;
     1047   ctx->Pixel.MapItoI[0] = 0;
     1048   ctx->Pixel.MapItoR[0] = 0.0;
     1049   ctx->Pixel.MapItoG[0] = 0.0;
     1050   ctx->Pixel.MapItoB[0] = 0.0;
     1051   ctx->Pixel.MapItoA[0] = 0.0;
     1052   ctx->Pixel.MapItoR8[0] = 0;
     1053   ctx->Pixel.MapItoG8[0] = 0;
     1054   ctx->Pixel.MapItoB8[0] = 0;
     1055   ctx->Pixel.MapItoA8[0] = 0;
     1056   ctx->Pixel.MapRtoR[0] = 0.0;
     1057   ctx->Pixel.MapGtoG[0] = 0.0;
     1058   ctx->Pixel.MapBtoB[0] = 0.0;
     1059   ctx->Pixel.MapAtoA[0] = 0.0;
     1060
     1061   /* Point group */
     1062   ctx->Point.SmoothFlag = GL_FALSE;
     1063   ctx->Point.Size = 1.0;
     1064   ctx->Point.Params[0] = 1.0;
     1065   ctx->Point.Params[1] = 0.0;
     1066   ctx->Point.Params[2] = 0.0;
     1067   ctx->Point.Attenuated = GL_FALSE;
     1068   ctx->Point.MinSize = 0.0;
     1069   ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
     1070   ctx->Point.Threshold = 1.0;
     1071
     1072   /* Polygon group */
     1073   ctx->Polygon.CullFlag = GL_FALSE;
     1074   ctx->Polygon.CullFaceMode = GL_BACK;
     1075   ctx->Polygon.FrontFace = GL_CCW;
     1076   ctx->Polygon.FrontBit = 0;
     1077   ctx->Polygon.FrontMode = GL_FILL;
     1078   ctx->Polygon.BackMode = GL_FILL;
     1079   ctx->Polygon.Unfilled = GL_FALSE;
     1080   ctx->Polygon.SmoothFlag = GL_FALSE;
     1081   ctx->Polygon.StippleFlag = GL_FALSE;
     1082   ctx->Polygon.OffsetFactor = 0.0F;
     1083   ctx->Polygon.OffsetUnits = 0.0F;
     1084   ctx->Polygon.OffsetPoint = GL_FALSE;
     1085   ctx->Polygon.OffsetLine = GL_FALSE;
     1086   ctx->Polygon.OffsetFill = GL_FALSE;
     1087
     1088   /* Polygon Stipple group */
     1089   MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
     1090
     1091   /* Scissor group */
     1092   ctx->Scissor.Enabled = GL_FALSE;
     1093   ctx->Scissor.X = 0;
     1094   ctx->Scissor.Y = 0;
     1095   ctx->Scissor.Width = 0;
     1096   ctx->Scissor.Height = 0;
     1097
     1098   /* Stencil group */
     1099   ctx->Stencil.Enabled = GL_FALSE;
     1100   ctx->Stencil.Function = GL_ALWAYS;
     1101   ctx->Stencil.FailFunc = GL_KEEP;
     1102   ctx->Stencil.ZPassFunc = GL_KEEP;
     1103   ctx->Stencil.ZFailFunc = GL_KEEP;
     1104   ctx->Stencil.Ref = 0;
     1105   ctx->Stencil.ValueMask = STENCIL_MAX;
     1106   ctx->Stencil.Clear = 0;
     1107   ctx->Stencil.WriteMask = STENCIL_MAX;
     1108
     1109   /* Texture group */
     1110   ctx->Texture.CurrentUnit = 0;      /* multitexture */
     1111   ctx->Texture.CurrentTransformUnit = 0; /* multitexture */
     1112   ctx->Texture.Enabled = 0;
     1113   for (i=0; i<MAX_TEXTURE_UNITS; i++)
     1114      init_texture_unit( ctx, i );
     1115   init_color_table(&ctx->Texture.Palette);
     1116
     1117   /* Transformation group */
     1118   ctx->Transform.MatrixMode = GL_MODELVIEW;
     1119   ctx->Transform.Normalize = GL_FALSE;
     1120   ctx->Transform.RescaleNormals = GL_FALSE;
     1121   for (i=0;i<MAX_CLIP_PLANES;i++) {
     1122      ctx->Transform.ClipEnabled[i] = GL_FALSE;
     1123      ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
     1124   }
     1125   ctx->Transform.AnyClip = GL_FALSE;
     1126
     1127   /* Viewport group */
     1128   ctx->Viewport.X = 0;
     1129   ctx->Viewport.Y = 0;
     1130   ctx->Viewport.Width = 0;
     1131   ctx->Viewport.Height = 0;
     1132   ctx->Viewport.Near = 0.0;
     1133   ctx->Viewport.Far = 1.0;
     1134   gl_matrix_ctr(&ctx->Viewport.WindowMap);
    9811135
    9821136#define Sz 10
    9831137#define Tz 14
    984       ctx->Viewport.WindowMap.m[Sz] = 0.5 * DEPTH_SCALE;
    985       ctx->Viewport.WindowMap.m[Tz] = 0.5 * DEPTH_SCALE;
     1138   ctx->Viewport.WindowMap.m[Sz] = 0.5 * ctx->Visual->DepthMaxF;
     1139   ctx->Viewport.WindowMap.m[Tz] = 0.5 * ctx->Visual->DepthMaxF;
    9861140#undef Sz
    9871141#undef Tz
    9881142
    989       ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
    990       ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
    991 
    992       /* Vertex arrays */
    993       ctx->Array.Vertex.Size = 4;
    994       ctx->Array.Vertex.Type = GL_FLOAT;
    995       ctx->Array.Vertex.Stride = 0;
    996       ctx->Array.Vertex.StrideB = 0;
    997       ctx->Array.Vertex.Ptr = NULL;
    998       ctx->Array.Vertex.Enabled = GL_FALSE;
    999       ctx->Array.Normal.Type = GL_FLOAT;
    1000       ctx->Array.Normal.Stride = 0;
    1001       ctx->Array.Normal.StrideB = 0;
    1002       ctx->Array.Normal.Ptr = NULL;
    1003       ctx->Array.Normal.Enabled = GL_FALSE;
    1004       ctx->Array.Color.Size = 4;
    1005       ctx->Array.Color.Type = GL_FLOAT;
    1006       ctx->Array.Color.Stride = 0;
    1007       ctx->Array.Color.StrideB = 0;
    1008       ctx->Array.Color.Ptr = NULL;
    1009       ctx->Array.Color.Enabled = GL_FALSE;
    1010       ctx->Array.Index.Type = GL_FLOAT;
    1011       ctx->Array.Index.Stride = 0;
    1012       ctx->Array.Index.StrideB = 0;
    1013       ctx->Array.Index.Ptr = NULL;
    1014       ctx->Array.Index.Enabled = GL_FALSE;
    1015       for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
    1016          ctx->Array.TexCoord[i].Size = 4;
    1017          ctx->Array.TexCoord[i].Type = GL_FLOAT;
    1018          ctx->Array.TexCoord[i].Stride = 0;
    1019          ctx->Array.TexCoord[i].StrideB = 0;
    1020          ctx->Array.TexCoord[i].Ptr = NULL;
    1021          ctx->Array.TexCoord[i].Enabled = GL_FALSE;
    1022       }
    1023       ctx->Array.TexCoordInterleaveFactor = 1;
    1024       ctx->Array.EdgeFlag.Stride = 0;
    1025       ctx->Array.EdgeFlag.StrideB = 0;
    1026       ctx->Array.EdgeFlag.Ptr = NULL;
    1027       ctx->Array.EdgeFlag.Enabled = GL_FALSE;
    1028       ctx->Array.ActiveTexture = 0;   /* GL_ARB_multitexture */
    1029 
    1030       /* Pixel transfer */
    1031       ctx->Pack.Alignment = 4;
    1032       ctx->Pack.RowLength = 0;
    1033       ctx->Pack.ImageHeight = 0;
    1034       ctx->Pack.SkipPixels = 0;
    1035       ctx->Pack.SkipRows = 0;
    1036       ctx->Pack.SkipImages = 0;
    1037       ctx->Pack.SwapBytes = GL_FALSE;
    1038       ctx->Pack.LsbFirst = GL_FALSE;
    1039       ctx->Unpack.Alignment = 4;
    1040       ctx->Unpack.RowLength = 0;
    1041       ctx->Unpack.ImageHeight = 0;
    1042       ctx->Unpack.SkipPixels = 0;
    1043       ctx->Unpack.SkipRows = 0;
    1044       ctx->Unpack.SkipImages = 0;
    1045       ctx->Unpack.SwapBytes = GL_FALSE;
    1046       ctx->Unpack.LsbFirst = GL_FALSE;
    1047 
    1048       /* Feedback */
    1049       ctx->Feedback.Type = GL_2D;   /* TODO: verify */
    1050       ctx->Feedback.Buffer = NULL;
    1051       ctx->Feedback.BufferSize = 0;
    1052       ctx->Feedback.Count = 0;
    1053 
    1054       /* Selection/picking */
    1055       ctx->Select.Buffer = NULL;
    1056       ctx->Select.BufferSize = 0;
    1057       ctx->Select.BufferCount = 0;
    1058       ctx->Select.Hits = 0;
    1059       ctx->Select.NameStackDepth = 0;
    1060 
    1061       /* Optimized Accum buffer */
    1062       ctx->IntegerAccumMode = GL_TRUE;
    1063       ctx->IntegerAccumScaler = 0.0;
    1064 
    1065       /* Renderer and client attribute stacks */
    1066       ctx->AttribStackDepth = 0;
    1067       ctx->ClientAttribStackDepth = 0;
    1068 
    1069       /*** Miscellaneous ***/
    1070       ctx->NewState = NEW_ALL;
    1071       ctx->RenderMode = GL_RENDER;
    1072       ctx->StippleCounter = 0;
    1073       ctx->NeedNormals = GL_FALSE;
    1074       ctx->DoViewportMapping = GL_TRUE;
    1075 
    1076       ctx->NeedEyeCoords = GL_FALSE;
    1077       ctx->NeedEyeNormals = GL_FALSE;
    1078       ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
    1079 
    1080       /* Display list */
    1081       ctx->CallDepth = 0;
    1082       ctx->ExecuteFlag = GL_TRUE;
    1083       ctx->CompileFlag = GL_FALSE;
    1084       ctx->CurrentListPtr = NULL;
    1085       ctx->CurrentBlock = NULL;
    1086       ctx->CurrentListNum = 0;
    1087       ctx->CurrentPos = 0;
    1088 
    1089       ctx->ErrorValue = (GLenum) GL_NO_ERROR;
    1090 
    1091       ctx->CatchSignals = GL_TRUE;
    1092 
    1093       /* For debug/development only */
    1094       ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
    1095       ctx->FirstTimeCurrent = GL_TRUE;
    1096 
    1097       /* Dither disable */
    1098       ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
    1099       if (ctx->NoDither) {
    1100          if (getenv("MESA_DEBUG")) {
    1101             fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
    1102          }
    1103          ctx->Color.DitherFlag = GL_FALSE;
    1104       }
    1105    }
    1106 }
    1107 
    1108 
    1109 
    1110 /*
    1111  * Allocate a new GLvisual object.
    1112  * Input:  rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
    1113  *         alphaFlag - alloc software alpha buffers?
    1114  *         dbFlag - double buffering?
    1115  *         stereoFlag - stereo buffer?
    1116  *         depthFits - requested minimum bits per depth buffer value
    1117  *         stencilFits - requested minimum bits per stencil buffer value
    1118  *         accumFits - requested minimum bits per accum buffer component
    1119  *         indexFits - number of bits per pixel if rgbFlag==GL_FALSE
    1120  *         red/green/blue/alphaFits - number of bits per color component
    1121  *                                     in frame buffer for RGB(A) mode.
    1122  * Return:  pointer to new GLvisual or NULL if requested parameters can't
    1123  *          be met.
    1124  */
    1125 GLvisual *gl_create_visual( GLboolean rgbFlag,
    1126                             GLboolean alphaFlag,
    1127                             GLboolean dbFlag,
    1128                             GLboolean stereoFlag,
    1129                             GLint depthBits,
    1130                             GLint stencilBits,
    1131                             GLint accumBits,
    1132                             GLint indexBits,
    1133                             GLint redBits,
    1134                             GLint greenBits,
    1135                             GLint blueBits,
    1136                             GLint alphaBits )
    1137 {
    1138    GLvisual *vis;
    1139 
    1140    if (depthBits > (GLint) (8*sizeof(GLdepth))) {
    1141       /* can't meet depth buffer requirements */
    1142       return NULL;
    1143    }
    1144    if (stencilBits > (GLint) (8*sizeof(GLstencil))) {
    1145       /* can't meet stencil buffer requirements */
    1146       return NULL;
    1147    }
    1148    if (accumBits > (GLint) (8*sizeof(GLaccum))) {
    1149       /* can't meet accum buffer requirements */
    1150       return NULL;
    1151    }
    1152 
    1153    vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
    1154    if (!vis) {
    1155       return NULL;
    1156    }
    1157 
    1158    vis->RGBAflag   = rgbFlag;
    1159    vis->DBflag     = dbFlag;
    1160    vis->StereoFlag = stereoFlag;
    1161    vis->RedBits    = redBits;
    1162    vis->GreenBits  = greenBits;
    1163    vis->BlueBits   = blueBits;
    1164    vis->AlphaBits  = alphaFlag ? 8*sizeof(GLubyte) : alphaBits;
    1165 
    1166    vis->IndexBits   = indexBits;
    1167    vis->DepthBits   = (depthBits>0) ? 8*sizeof(GLdepth) : 0;
    1168    vis->AccumBits   = (accumBits>0) ? 8*sizeof(GLaccum) : 0;
    1169    vis->StencilBits = (stencilBits>0) ? 8*sizeof(GLstencil) : 0;
    1170 
    1171    vis->SoftwareAlpha = alphaFlag;
    1172 
    1173    return vis;
    1174 }
    1175 
    1176 
    1177 
    1178 void gl_destroy_visual( GLvisual *vis )
    1179 {
    1180    FREE( vis );
    1181 }
     1143   ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
     1144   ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
     1145
     1146   /* Vertex arrays */
     1147   ctx->Array.Vertex.Size = 4;
     1148   ctx->Array.Vertex.Type = GL_FLOAT;
     1149   ctx->Array.Vertex.Stride = 0;
     1150   ctx->Array.Vertex.StrideB = 0;
     1151   ctx->Array.Vertex.Ptr = NULL;
     1152   ctx->Array.Vertex.Enabled = GL_FALSE;
     1153   ctx->Array.Normal.Type = GL_FLOAT;
     1154   ctx->Array.Normal.Stride = 0;
     1155   ctx->Array.Normal.StrideB = 0;
     1156   ctx->Array.Normal.Ptr = NULL;
     1157   ctx->Array.Normal.Enabled = GL_FALSE;
     1158   ctx->Array.Color.Size = 4;
     1159   ctx->Array.Color.Type = GL_FLOAT;
     1160   ctx->Array.Color.Stride = 0;
     1161   ctx->Array.Color.StrideB = 0;
     1162   ctx->Array.Color.Ptr = NULL;
     1163   ctx->Array.Color.Enabled = GL_FALSE;
     1164   ctx->Array.Index.Type = GL_FLOAT;
     1165   ctx->Array.Index.Stride = 0;
     1166   ctx->Array.Index.StrideB = 0;
     1167   ctx->Array.Index.Ptr = NULL;
     1168   ctx->Array.Index.Enabled = GL_FALSE;
     1169   for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
     1170      ctx->Array.TexCoord[i].Size = 4;
     1171      ctx->Array.TexCoord[i].Type = GL_FLOAT;
     1172      ctx->Array.TexCoord[i].Stride = 0;
     1173      ctx->Array.TexCoord[i].StrideB = 0;
     1174      ctx->Array.TexCoord[i].Ptr = NULL;
     1175      ctx->Array.TexCoord[i].Enabled = GL_FALSE;
     1176   }
     1177   ctx->Array.TexCoordInterleaveFactor = 1;
     1178   ctx->Array.EdgeFlag.Stride = 0;
     1179   ctx->Array.EdgeFlag.StrideB = 0;
     1180   ctx->Array.EdgeFlag.Ptr = NULL;
     1181   ctx->Array.EdgeFlag.Enabled = GL_FALSE;
     1182   ctx->Array.ActiveTexture = 0;   /* GL_ARB_multitexture */
     1183
     1184   /* Pixel transfer */
     1185   ctx->Pack.Alignment = 4;
     1186   ctx->Pack.RowLength = 0;
     1187   ctx->Pack.ImageHeight = 0;
     1188   ctx->Pack.SkipPixels = 0;
     1189   ctx->Pack.SkipRows = 0;
     1190   ctx->Pack.SkipImages = 0;
     1191   ctx->Pack.SwapBytes = GL_FALSE;
     1192   ctx->Pack.LsbFirst = GL_FALSE;
     1193   ctx->Unpack.Alignment = 4;
     1194   ctx->Unpack.RowLength = 0;
     1195   ctx->Unpack.ImageHeight = 0;
     1196   ctx->Unpack.SkipPixels = 0;
     1197   ctx->Unpack.SkipRows = 0;
     1198   ctx->Unpack.SkipImages = 0;
     1199   ctx->Unpack.SwapBytes = GL_FALSE;
     1200   ctx->Unpack.LsbFirst = GL_FALSE;
     1201
     1202   /* Feedback */
     1203   ctx->Feedback.Type = GL_2D;   /* TODO: verify */
     1204   ctx->Feedback.Buffer = NULL;
     1205   ctx->Feedback.BufferSize = 0;
     1206   ctx->Feedback.Count = 0;
     1207
     1208   /* Selection/picking */
     1209   ctx->Select.Buffer = NULL;
     1210   ctx->Select.BufferSize = 0;
     1211   ctx->Select.BufferCount = 0;
     1212   ctx->Select.Hits = 0;
     1213   ctx->Select.NameStackDepth = 0;
     1214
     1215   /* Optimized Accum buffer */
     1216   ctx->IntegerAccumMode = GL_TRUE;
     1217   ctx->IntegerAccumScaler = 0.0;
     1218
     1219   /* Renderer and client attribute stacks */
     1220   ctx->AttribStackDepth = 0;
     1221   ctx->ClientAttribStackDepth = 0;
     1222
     1223   /* Miscellaneous */
     1224   ctx->NewState = NEW_ALL;
     1225   ctx->RenderMode = GL_RENDER;
     1226   ctx->StippleCounter = 0;
     1227   ctx->NeedNormals = GL_FALSE;
     1228   ctx->DoViewportMapping = GL_TRUE;
     1229
     1230   ctx->NeedEyeCoords = GL_FALSE;
     1231   ctx->NeedEyeNormals = GL_FALSE;
     1232   ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
     1233
     1234   /* Display list */
     1235   ctx->CallDepth = 0;
     1236   ctx->ExecuteFlag = GL_TRUE;
     1237   ctx->CompileFlag = GL_FALSE;
     1238   ctx->CurrentListPtr = NULL;
     1239   ctx->CurrentBlock = NULL;
     1240   ctx->CurrentListNum = 0;
     1241   ctx->CurrentPos = 0;
     1242
     1243   ctx->ErrorValue = (GLenum) GL_NO_ERROR;
     1244
     1245   ctx->CatchSignals = GL_TRUE;
     1246   ctx->OcclusionResult = GL_FALSE;
     1247
     1248   /* For debug/development only */
     1249   ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
     1250   ctx->FirstTimeCurrent = GL_TRUE;
     1251
     1252   /* Dither disable */
     1253   ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
     1254   if (ctx->NoDither) {
     1255      if (getenv("MESA_DEBUG")) {
     1256         fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
     1257      }
     1258      ctx->Color.DitherFlag = GL_FALSE;
     1259   }
     1260}
     1261
    11821262
    11831263
     
    12471327
    12481328/*
    1249  * Allocate and initialize a GLcontext structure.
    1250  * Input:  visual - a GLvisual pointer
    1251  *         sharelist - another context to share display lists with or NULL
    1252  *         driver_ctx - pointer to device driver's context state struct
    1253  * Return:  pointer to a new gl_context struct or NULL if error.
    1254  */
    1255 GLcontext *gl_create_context( GLvisual *visual,
    1256                               GLcontext *share_list,
    1257                               void *driver_ctx,
    1258                               GLboolean direct )
    1259 {
    1260    GLcontext *ctx;
    1261    GLuint i;
    1262 
     1329 * Initialize a GLcontext struct.
     1330 */
     1331GLboolean gl_initialize_context_data( GLcontext *ctx,
     1332                                      GLvisual *visual,
     1333                                      GLcontext *share_list,
     1334                                      void *driver_ctx,
     1335                                      GLboolean direct )
     1336{
    12631337   (void) direct;  /* not used */
    1264 
    1265    /* do some implementation tests */
    1266    assert( sizeof(GLbyte) == 1 );
    1267    assert( sizeof(GLshort) >= 2 );
    1268    assert( sizeof(GLint) >= 4 );
    1269    assert( sizeof(GLubyte) == 1 );
    1270    assert( sizeof(GLushort) >= 2 );
    1271    assert( sizeof(GLuint) >= 4 );
    12721338
    12731339   /* misc one-time initializations */
    12741340   one_time_init();
    12751341
    1276    ctx = (GLcontext *) CALLOC( sizeof(GLcontext) );
    1277    if (!ctx) {
    1278       return NULL;
    1279    }
    1280 
    12811342   ctx->DriverCtx = driver_ctx;
    12821343   ctx->Visual = visual;
    1283    ctx->Buffer = NULL;
     1344   ctx->DrawBuffer = NULL;
     1345   ctx->ReadBuffer = NULL;
    12841346
    12851347   ctx->VB = gl_vb_create_for_immediate( ctx );
    1286 
    12871348   if (!ctx->VB) {
    12881349      FREE( ctx );
    1289       return NULL;
     1350      return GL_FALSE;
    12901351   }
    12911352   ctx->input = ctx->VB->IM;
    12921353
    12931354   ctx->PB = gl_alloc_pb();
    1294 
    12951355   if (!ctx->PB) {
    12961356      FREE( ctx->VB );
    12971357      FREE( ctx );
    1298       return NULL;
     1358      return GL_FALSE;
    12991359   }
    13001360
     
    13101370         FREE(ctx->PB);
    13111371         FREE(ctx);
    1312          return NULL;
    1313       }
    1314    }
     1372         return GL_FALSE;
     1373      }
     1374   }
     1375   _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
    13151376   ctx->Shared->RefCount++;
    1316 
    1317    initialize_context( ctx );
     1377   _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
     1378
     1379   init_attrib_groups( ctx );
     1380
    13181381   gl_reset_vb( ctx->VB );
    13191382   gl_reset_input( ctx );
    1320 
    1321 
    1322    ctx->ShineTabList = MALLOC_STRUCT( gl_shine_tab );
    1323    make_empty_list( ctx->ShineTabList );
    1324 
    1325    for (i = 0 ; i < 10 ; i++) {
    1326       struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
    1327       s->shininess = -1;
    1328       s->refcount = 0;
    1329       insert_at_tail( ctx->ShineTabList, s );
    1330    }
    1331 
    1332    for (i = 0 ; i < 4 ; i++) {
    1333       ctx->ShineTable[i] = ctx->ShineTabList->prev;
    1334       ctx->ShineTable[i]->refcount++;
    1335    }
    13361383
    13371384   if (visual->DBflag) {
     
    13501397   }
    13511398
    1352 
    1353    /* Fill in some driver defaults now.
    1354     */
    1355    ctx->Driver.AllocDepthBuffer = gl_alloc_depth_buffer;
    1356    ctx->Driver.ReadDepthSpanFloat = gl_read_depth_span_float;
    1357    ctx->Driver.ReadDepthSpanInt = gl_read_depth_span_int;
    1358 
    1359 
    13601399#ifdef PROFILE
    13611400   init_timings( ctx );
    13621401#endif
    13631402
    1364 #ifdef GL_VERSION_1_1
    13651403   if (!alloc_proxy_textures(ctx)) {
    13661404      free_shared_state(ctx, ctx->Shared);
     
    13681406      FREE(ctx->PB);
    13691407      FREE(ctx);
     1408      return GL_FALSE;
     1409   }
     1410
     1411   /* setup API dispatch tables */
     1412   ctx->Exec = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
     1413   ctx->Save = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
     1414   if (!ctx->Exec || !ctx->Save) {
     1415      free_shared_state(ctx, ctx->Shared);
     1416      FREE(ctx->VB);
     1417      FREE(ctx->PB);
     1418      if (ctx->Exec)
     1419         FREE(ctx->Exec);
     1420      FREE(ctx);
     1421   }
     1422   _mesa_init_exec_table( ctx->Exec );
     1423   _mesa_init_dlist_table( ctx->Save );
     1424   ctx->CurrentDispatch = ctx->Exec;
     1425
     1426   return GL_TRUE;
     1427}
     1428
     1429
     1430
     1431/*
     1432 * Allocate and initialize a GLcontext structure.
     1433 * Input:  visual - a GLvisual pointer
     1434 *         sharelist - another context to share display lists with or NULL
     1435 *         driver_ctx - pointer to device driver's context state struct
     1436 * Return:  pointer to a new gl_context struct or NULL if error.
     1437 */
     1438GLcontext *gl_create_context( GLvisual *visual,
     1439                              GLcontext *share_list,
     1440                              void *driver_ctx,
     1441                              GLboolean direct )
     1442{
     1443   GLcontext *ctx = (GLcontext *) CALLOC( sizeof(GLcontext) );
     1444   if (!ctx) {
    13701445      return NULL;
    13711446   }
     1447
     1448   if (gl_initialize_context_data(ctx, visual, share_list,
     1449                                  driver_ctx, direct)) {
     1450      return ctx;
     1451   }
     1452   else {
     1453      FREE(ctx);
     1454      return NULL;
     1455   }
     1456}
     1457
     1458
     1459
     1460/*
     1461 * Free the data associated with the given context.
     1462 * But don't free() the GLcontext struct itself!
     1463 */
     1464void gl_free_context_data( GLcontext *ctx )
     1465{
     1466   struct gl_shine_tab *s, *tmps;
     1467   GLuint i, j;
     1468
     1469   /* if we're destroying the current context, unbind it first */
     1470   if (ctx == gl_get_current_context()) {
     1471      gl_make_current(NULL, NULL);
     1472   }
     1473
     1474#ifdef PROFILE
     1475   if (getenv("MESA_PROFILE")) {
     1476      print_timings( ctx );
     1477   }
    13721478#endif
    13731479
    1374    gl_init_api_function_pointers( ctx );
    1375    ctx->API = ctx->Exec;   /* GL_EXECUTE is default */
    1376 
    1377    return ctx;
    1378 }
    1379 
    1380 /* Just reads the config files...
     1480   gl_matrix_dtr( &ctx->ModelView );
     1481   for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
     1482      gl_matrix_dtr( &ctx->ModelViewStack[i] );
     1483   }
     1484   gl_matrix_dtr( &ctx->ProjectionMatrix );
     1485   for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
     1486      gl_matrix_dtr( &ctx->ProjectionStack[i] );
     1487   }
     1488   for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
     1489      gl_matrix_dtr( &ctx->TextureMatrix[i] );
     1490      for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
     1491         gl_matrix_dtr( &ctx->TextureStack[i][j] );
     1492      }
     1493   }
     1494
     1495   FREE( ctx->PB );
     1496
     1497   if(ctx->input != ctx->VB->IM)
     1498      gl_immediate_free( ctx->input );
     1499
     1500   gl_vb_free( ctx->VB );
     1501
     1502   _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
     1503   ctx->Shared->RefCount--;
     1504   ASSERT(ctx->Shared->RefCount >= 0);
     1505   _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
     1506   if (ctx->Shared->RefCount == 0) {
     1507      /* free shared state */
     1508      free_shared_state( ctx, ctx->Shared );
     1509   }
     1510
     1511   foreach_s( s, tmps, ctx->ShineTabList ) {
     1512      FREE( s );
     1513   }
     1514   FREE( ctx->ShineTabList );
     1515
     1516   /* Free proxy texture objects */
     1517   gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
     1518   gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
     1519   gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
     1520
     1521   /* Free evaluator data */
     1522   if (ctx->EvalMap.Map1Vertex3.Points)
     1523      FREE( ctx->EvalMap.Map1Vertex3.Points );
     1524   if (ctx->EvalMap.Map1Vertex4.Points)
     1525      FREE( ctx->EvalMap.Map1Vertex4.Points );
     1526   if (ctx->EvalMap.Map1Index.Points)
     1527      FREE( ctx->EvalMap.Map1Index.Points );
     1528   if (ctx->EvalMap.Map1Color4.Points)
     1529      FREE( ctx->EvalMap.Map1Color4.Points );
     1530   if (ctx->EvalMap.Map1Normal.Points)
     1531      FREE( ctx->EvalMap.Map1Normal.Points );
     1532   if (ctx->EvalMap.Map1Texture1.Points)
     1533      FREE( ctx->EvalMap.Map1Texture1.Points );
     1534   if (ctx->EvalMap.Map1Texture2.Points)
     1535      FREE( ctx->EvalMap.Map1Texture2.Points );
     1536   if (ctx->EvalMap.Map1Texture3.Points)
     1537      FREE( ctx->EvalMap.Map1Texture3.Points );
     1538   if (ctx->EvalMap.Map1Texture4.Points)
     1539      FREE( ctx->EvalMap.Map1Texture4.Points );
     1540
     1541   if (ctx->EvalMap.Map2Vertex3.Points)
     1542      FREE( ctx->EvalMap.Map2Vertex3.Points );
     1543   if (ctx->EvalMap.Map2Vertex4.Points)
     1544      FREE( ctx->EvalMap.Map2Vertex4.Points );
     1545   if (ctx->EvalMap.Map2Index.Points)
     1546      FREE( ctx->EvalMap.Map2Index.Points );
     1547   if (ctx->EvalMap.Map2Color4.Points)
     1548      FREE( ctx->EvalMap.Map2Color4.Points );
     1549   if (ctx->EvalMap.Map2Normal.Points)
     1550      FREE( ctx->EvalMap.Map2Normal.Points );
     1551   if (ctx->EvalMap.Map2Texture1.Points)
     1552      FREE( ctx->EvalMap.Map2Texture1.Points );
     1553   if (ctx->EvalMap.Map2Texture2.Points)
     1554      FREE( ctx->EvalMap.Map2Texture2.Points );
     1555   if (ctx->EvalMap.Map2Texture3.Points)
     1556      FREE( ctx->EvalMap.Map2Texture3.Points );
     1557   if (ctx->EvalMap.Map2Texture4.Points)
     1558      FREE( ctx->EvalMap.Map2Texture4.Points );
     1559
     1560   /* Free cache of immediate buffers. */
     1561   while (ctx->nr_im_queued-- > 0) {
     1562      struct immediate * next = ctx->freed_im_queue->next;
     1563      FREE( ctx->freed_im_queue );
     1564      ctx->freed_im_queue = next;
     1565   }
     1566   gl_extensions_dtr(ctx);
     1567
     1568   FREE(ctx->Exec);
     1569   FREE(ctx->Save);
     1570}
     1571
     1572
     1573
     1574/*
     1575 * Destroy a GLcontext structure.
     1576 */
     1577void gl_destroy_context( GLcontext *ctx )
     1578{
     1579   if (ctx) {
     1580      gl_free_context_data(ctx);
     1581      FREE( (void *) ctx );
     1582   }
     1583}
     1584
     1585
     1586
     1587/*
     1588 * Called by the driver after both the context and driver are fully
     1589 * initialized.  Currently just reads the config file.
    13811590 */
    13821591void gl_context_initialize( GLcontext *ctx )
    13831592{
    13841593   gl_read_config_file( ctx );
    1385 }
    1386 
    1387 
    1388 
    1389 
    1390 /*
    1391  * Destroy a gl_context structure.
    1392  */
    1393 void gl_destroy_context( GLcontext *ctx )
    1394 {
    1395    if (ctx) {
    1396 
    1397       GLuint i;
    1398       struct gl_shine_tab *s, *tmps;
    1399 
    1400 #ifdef PROFILE
    1401       if (getenv("MESA_PROFILE")) {
    1402          print_timings( ctx );
    1403       }
    1404 #endif
    1405 
    1406       gl_matrix_dtr( &ctx->ModelView );
    1407       for (i = 0 ; i < MAX_MODELVIEW_STACK_DEPTH ; i++) {
    1408          gl_matrix_dtr( &ctx->ModelViewStack[i] );
    1409       }
    1410       gl_matrix_dtr( &ctx->ProjectionMatrix );
    1411       for (i = 0 ; i < MAX_PROJECTION_STACK_DEPTH ; i++) {
    1412          gl_matrix_dtr( &ctx->ProjectionStack[i] );
    1413       }
    1414 
    1415       FREE( ctx->PB );
    1416 
    1417       if(ctx->input != ctx->VB->IM)
    1418          gl_immediate_free( ctx->input );
    1419 
    1420       gl_vb_free( ctx->VB );
    1421 
    1422       ctx->Shared->RefCount--;
    1423       assert(ctx->Shared->RefCount>=0);
    1424       if (ctx->Shared->RefCount==0) {
    1425          /* free shared state */
    1426          free_shared_state( ctx, ctx->Shared );
    1427       }
    1428 
    1429       foreach_s( s, tmps, ctx->ShineTabList ) {
    1430          FREE( s );
    1431       }
    1432       FREE( ctx->ShineTabList );
    1433 
    1434       /* Free proxy texture objects */
    1435       gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
    1436       gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
    1437       gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
    1438 
    1439       /* Free evaluator data */
    1440       if (ctx->EvalMap.Map1Vertex3.Points)
    1441          FREE( ctx->EvalMap.Map1Vertex3.Points );
    1442       if (ctx->EvalMap.Map1Vertex4.Points)
    1443          FREE( ctx->EvalMap.Map1Vertex4.Points );
    1444       if (ctx->EvalMap.Map1Index.Points)
    1445          FREE( ctx->EvalMap.Map1Index.Points );
    1446       if (ctx->EvalMap.Map1Color4.Points)
    1447          FREE( ctx->EvalMap.Map1Color4.Points );
    1448       if (ctx->EvalMap.Map1Normal.Points)
    1449          FREE( ctx->EvalMap.Map1Normal.Points );
    1450       if (ctx->EvalMap.Map1Texture1.Points)
    1451          FREE( ctx->EvalMap.Map1Texture1.Points );
    1452       if (ctx->EvalMap.Map1Texture2.Points)
    1453          FREE( ctx->EvalMap.Map1Texture2.Points );
    1454       if (ctx->EvalMap.Map1Texture3.Points)
    1455          FREE( ctx->EvalMap.Map1Texture3.Points );
    1456       if (ctx->EvalMap.Map1Texture4.Points)
    1457          FREE( ctx->EvalMap.Map1Texture4.Points );
    1458 
    1459       if (ctx->EvalMap.Map2Vertex3.Points)
    1460          FREE( ctx->EvalMap.Map2Vertex3.Points );
    1461       if (ctx->EvalMap.Map2Vertex4.Points)
    1462          FREE( ctx->EvalMap.Map2Vertex4.Points );
    1463       if (ctx->EvalMap.Map2Index.Points)
    1464          FREE( ctx->EvalMap.Map2Index.Points );
    1465       if (ctx->EvalMap.Map2Color4.Points)
    1466          FREE( ctx->EvalMap.Map2Color4.Points );
    1467       if (ctx->EvalMap.Map2Normal.Points)
    1468          FREE( ctx->EvalMap.Map2Normal.Points );
    1469       if (ctx->EvalMap.Map2Texture1.Points)
    1470          FREE( ctx->EvalMap.Map2Texture1.Points );
    1471       if (ctx->EvalMap.Map2Texture2.Points)
    1472          FREE( ctx->EvalMap.Map2Texture2.Points );
    1473       if (ctx->EvalMap.Map2Texture3.Points)
    1474          FREE( ctx->EvalMap.Map2Texture3.Points );
    1475       if (ctx->EvalMap.Map2Texture4.Points)
    1476          FREE( ctx->EvalMap.Map2Texture4.Points );
    1477 
    1478       /* Free cache of immediate buffers. */
    1479       while (ctx->nr_im_queued-- > 0) {
    1480          struct immediate * next = ctx->freed_im_queue->next;
    1481          FREE( ctx->freed_im_queue );
    1482          ctx->freed_im_queue = next;
    1483       }
    1484       gl_extensions_dtr(ctx);
    1485 
    1486       FREE( (void *) ctx );
    1487 
    1488 #ifndef THREADS
    1489       if (ctx==CC) {
    1490          CC = NULL;
    1491          CURRENT_INPUT = NULL;
    1492       }
    1493 #endif
    1494 
    1495    }
    1496 }
    1497 
    1498 
    1499 
    1500 /*
    1501  * Create a new framebuffer.  A GLframebuffer is a struct which
    1502  * encapsulates the depth, stencil and accum buffers and related
    1503  * parameters.
    1504  * Input:  visual - a GLvisual pointer
    1505  * Return:  pointer to new GLframebuffer struct or NULL if error.
    1506  */
    1507 GLframebuffer *gl_create_framebuffer( GLvisual *visual )
    1508 {
    1509    GLframebuffer *buffer;
    1510 
    1511    buffer = (GLframebuffer *) CALLOC( sizeof(GLframebuffer) );
    1512    if (!buffer) {
    1513       return NULL;
    1514    }
    1515 
    1516    buffer->Visual = visual;
    1517 
    1518    return buffer;
    1519 }
    1520 
    1521 
    1522 
    1523 /*
    1524  * Free a framebuffer struct and its buffers.
    1525  */
    1526 void gl_destroy_framebuffer( GLframebuffer *buffer )
    1527 {
    1528    if (buffer) {
    1529       if (buffer->Depth) {
    1530          FREE( buffer->Depth );
    1531       }
    1532       if (buffer->Accum) {
    1533          FREE( buffer->Accum );
    1534       }
    1535       if (buffer->Stencil) {
    1536          FREE( buffer->Stencil );
    1537       }
    1538       if (buffer->FrontLeftAlpha) {
    1539          FREE( buffer->FrontLeftAlpha );
    1540       }
    1541       if (buffer->BackLeftAlpha) {
    1542          FREE( buffer->BackLeftAlpha );
    1543       }
    1544       if (buffer->FrontRightAlpha) {
    1545          FREE( buffer->FrontRightAlpha );
    1546       }
    1547       if (buffer->BackRightAlpha) {
    1548          FREE( buffer->BackRightAlpha );
    1549       }
    1550       FREE(buffer);
    1551    }
    1552 }
    1553 
    1554 
    1555 
    1556 /*
    1557  * Set the current context, binding the given frame buffer to the context.
    1558  */
    1559 void gl_make_current( GLcontext *ctx, GLframebuffer *buffer )
    1560 {
    1561    GET_CONTEXT;
    1562 
    1563    /* Flush the old context
    1564     */
    1565    if (CC) {
    1566      ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(CC, "gl_make_current");
    1567    }
    1568 
    1569 #ifdef THREADS
    1570    /* TODO: unbind old buffer from context? */
    1571    set_thread_context( ctx );
    1572 #else
    1573    if (CC && CC->Buffer) {
    1574       /* unbind frame buffer from context */
    1575       CC->Buffer = NULL;
    1576    }
    1577 
    1578    CC = ctx;
    1579    if (ctx) {
    1580    SET_IMMEDIATE(ctx, ctx->input);
    1581    }
    1582 #endif
    1583 
    1584    if (ctx && buffer) {
    1585       /* TODO: check if ctx and buffer's visual match??? */
    1586       ctx->Buffer = buffer;         /* Bind the frame buffer to the context*/
    1587       ctx->NewState = NEW_ALL;                           /* just to be safe*/
    1588       gl_update_state( ctx );
    1589    }
    1590 
    1591    /* We can use this to help debug user's problems.  Tell the to set
    1592     * the MESA_INFO env variable before running their app.  Then the
    1593     * first time each context is made current we'll print some useful
    1594     * information.
    1595     */
    1596    if (ctx && ctx->FirstTimeCurrent)
    1597      {
    1598         dprintf(("Mesa GL_VERSION = %s\n", (char *) gl_GetString(ctx, GL_VERSION)));
    1599         dprintf(("Mesa GL_RENDERER = %s\n", (char *) gl_GetString(ctx, GL_RENDERER)));
    1600         dprintf(("Mesa GL_VENDOR = %s\n", (char *) gl_GetString(ctx, GL_VENDOR)));
    1601         dprintf(("Mesa GL_EXTENSIONS = %s\n", (char *) gl_GetString(ctx, GL_EXTENSIONS)));
    1602 
    1603         ctx->FirstTimeCurrent = GL_FALSE;
    1604      }
    1605 }
    1606 
    1607 
    1608 /*
    1609  * Return current context handle.
    1610  */
    1611 GLcontext *gl_get_current_context( void )
    1612 {
    1613 #ifdef THREADS
    1614    return gl_get_thread_context();
    1615 #else
    1616    return CC;
    1617 #endif
    16181594}
    16191595
     
    17021678
    17031679
    1704 
    1705 /*
    1706  * Someday a GLS library or OpenGL-like debugger may call this function
    1707  * to register it's own set of API entry points.
    1708  * Input: ctx - the context to set API pointers for
    1709  *        api - if NULL, restore original API pointers
    1710  *              else, set API function table to this table.
    1711  */
    1712 void gl_set_api_table( GLcontext *ctx, const struct gl_api_table *api )
    1713 {
    1714    if (api) {
    1715       MEMCPY( &ctx->API, api, sizeof(struct gl_api_table) );
     1680/*
     1681 * Set the current context, binding the given frame buffer to the context.
     1682 */
     1683void gl_make_current( GLcontext *newCtx, GLframebuffer *buffer )
     1684{
     1685   gl_make_current2( newCtx, buffer, buffer );
     1686}
     1687
     1688
     1689/*
     1690 * Bind the given context to the given draw-buffer and read-buffer
     1691 * and make it the current context for this thread.
     1692 */
     1693void gl_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
     1694                       GLframebuffer *readBuffer )
     1695{
     1696#if 0
     1697   GLcontext *oldCtx = gl_get_context();
     1698
     1699   /* Flush the old context
     1700    */
     1701   if (oldCtx) {
     1702      ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx, "gl_make_current");
     1703
     1704      /* unbind frame buffers from context */
     1705      if (oldCtx->DrawBuffer) {
     1706         oldCtx->DrawBuffer = NULL;
     1707      }
     1708      if (oldCtx->ReadBuffer) {
     1709         oldCtx->ReadBuffer = NULL;
     1710      }
     1711   }
     1712#endif
     1713
     1714   /* We call this function periodically (just here for now) in
     1715    * order to detect when multithreading has begun.
     1716    */
     1717   _glapi_check_multithread();
     1718
     1719   _glapi_set_context((void *) newCtx);
     1720   ASSERT(gl_get_current_context() == newCtx);
     1721   if (newCtx) {
     1722      SET_IMMEDIATE(newCtx, newCtx->input);
     1723      _glapi_set_dispatch(newCtx->CurrentDispatch);
    17161724   }
    17171725   else {
    1718       MEMCPY( &ctx->API, &ctx->Exec, sizeof(struct gl_api_table) );
    1719    }
    1720 }
    1721 
     1726      _glapi_set_dispatch(NULL);  /* none current */
     1727   }
     1728
     1729   if (MESA_VERBOSE) fprintf(stderr, "gl_make_current()\n");
     1730
     1731   if (newCtx && drawBuffer && readBuffer) {
     1732      /* TODO: check if newCtx and buffer's visual match??? */
     1733      newCtx->DrawBuffer = drawBuffer;
     1734      newCtx->ReadBuffer = readBuffer;
     1735      newCtx->NewState = NEW_ALL;   /* just to be safe */
     1736      gl_update_state( newCtx );
     1737   }
     1738
     1739   /* We can use this to help debug user's problems.  Tell the to set
     1740    * the MESA_INFO env variable before running their app.  Then the
     1741    * first time each context is made current we'll print some useful
     1742    * information.
     1743    */
     1744   if (newCtx && newCtx->FirstTimeCurrent) {
     1745      if (getenv("MESA_INFO")) {
     1746         fprintf(stderr, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION));
     1747         fprintf(stderr, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER));
     1748         fprintf(stderr, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR));
     1749         fprintf(stderr, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS));
     1750#if defined(THREADS)
     1751         fprintf(stderr, "Mesa thread-safe: YES\n");
     1752#else
     1753         fprintf(stderr, "Mesa thread-safe: NO\n");
     1754#endif
     1755#if defined(USE_X86_ASM)
     1756         fprintf(stderr, "Mesa x86-optimized: YES\n");
     1757#else
     1758         fprintf(stderr, "Mesa x86-optimized: NO\n");
     1759#endif
     1760      }
     1761      newCtx->FirstTimeCurrent = GL_FALSE;
     1762   }
     1763}
     1764
     1765
     1766
     1767/*
     1768 * Return current context handle for the calling thread.
     1769 * This isn't the fastest way to get the current context.
     1770 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
     1771 */
     1772GLcontext *gl_get_current_context( void )
     1773{
     1774   return (GLcontext *) _glapi_get_context();
     1775}
     1776
     1777
     1778
     1779/*
     1780 * This should be called by device drivers just before they do a
     1781 * swapbuffers.  Any pending rendering commands will be executed.
     1782 */
     1783void
     1784_mesa_swapbuffers(GLcontext *ctx)
     1785{
     1786   FLUSH_VB( ctx, "swap buffers" );
     1787}
     1788
     1789
     1790
     1791/*
     1792 * Return pointer to this context's current API dispatch table.
     1793 * It'll either be the immediate-mode execute dispatcher or the
     1794 * display list compile dispatcher.
     1795 */
     1796struct _glapi_table *
     1797_mesa_get_dispatch(GLcontext *ctx)
     1798{
     1799   return ctx->CurrentDispatch;
     1800}
    17221801
    17231802
     
    17351814{
    17361815#ifdef __WIN32OS2__
    1737    dprintf(("OPENGL32: Mesa implementation error: %s\n", s) );
    1738    dprintf(("OPENGL32: Report to J.vandenHorn@fibre.a2000.nl\n" ));
     1816   fprintf( stderr,"OPENGL32: Mesa implementation error: %s\n", s );
     1817   fprintf( stderr,"OPENGL32: Report to J.vandenHorn@fibre.a2000.nl\n" );
    17391818#else
    17401819   fprintf( stderr, "Mesa implementation error: %s\n", s );
     
    17651844#endif
    17661845#ifdef __WIN32OS2__
    1767    dprintf(("OPENGL32: Mesa warning: %s\n", s ));
     1846   fprintf( stderr,"OPENGL32: Mesa warning: %s\n", s );
    17681847#else
    17691848   if (debug) {
     
    17761855
    17771856
     1857/*
     1858 * Compile an error into current display list.
     1859 */
    17781860void gl_compile_error( GLcontext *ctx, GLenum error, const char *s )
    17791861{
     
    17841866      gl_error( ctx, error, s );
    17851867}
     1868
    17861869
    17871870
     
    18421925      }
    18431926#ifdef __WIN32OS2__
    1844       dprintf( ("OPENGL32: Mesa user error: %s in %s\n", errstr, s ));
     1927      fprintf( stderr, "OPENGL32: Mesa user error: %s in %s\n", errstr, s );
    18451928#else
    18461929      fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
     
    18601943
    18611944
    1862 /*
    1863  * Execute a glGetError command
    1864  */
    1865 GLenum gl_GetError( GLcontext *ctx )
    1866 {
    1867    GLenum e = ctx->ErrorValue;
    1868 
    1869    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL( ctx, "glGetError", (GLenum) 0);
    1870 
    1871    if (MESA_VERBOSE & VERBOSE_API)
    1872       fprintf(stderr, "glGetError <-- %s\n", gl_lookup_enum_by_nr(e));
    1873 
    1874    ctx->ErrorValue = (GLenum) GL_NO_ERROR;
    1875    return e;
    1876 }
    1877 
    1878 
    1879 
    1880 void gl_ResizeBuffersMESA( GLcontext *ctx )
    1881 {
    1882    GLuint buf_width, buf_height;
    1883 
    1884    if (MESA_VERBOSE & VERBOSE_API)
    1885       fprintf(stderr, "glResizeBuffersMESA\n");
    1886 
    1887    /* ask device driver for size of output buffer */
    1888    (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
    1889 
    1890    dprintf(("OPENGL32: gl_ResizeBuffersMESA - new w/h %d/%d\n",buf_width,buf_height));
    1891 
    1892    /* see if size of device driver's color buffer (window) has changed */
    1893    if (ctx->Buffer->Width == (GLint) buf_width &&
    1894        ctx->Buffer->Height == (GLint) buf_height)
    1895       return;
    1896 
    1897    ctx->NewState |= NEW_RASTER_OPS;    /* to update scissor / window bounds*/
    1898 
    1899    /* save buffer size */
    1900    ctx->Buffer->Width = buf_width;
    1901    ctx->Buffer->Height = buf_height;
    1902 
    1903    /* Reallocate other buffers if needed. */
    1904    if (ctx->Visual->DepthBits>0) {
    1905       /* reallocate depth buffer */
    1906       (*ctx->Driver.AllocDepthBuffer)( ctx );
    1907    }
    1908    if (ctx->Visual->StencilBits>0) {
    1909       /* reallocate stencil buffer */
    1910       gl_alloc_stencil_buffer( ctx );
    1911    }
    1912    if (ctx->Visual->AccumBits>0) {
    1913       /* reallocate accum buffer */
    1914       gl_alloc_accum_buffer( ctx );
    1915    }
    1916    if (ctx->Visual->SoftwareAlpha) {
    1917       gl_alloc_alpha_buffers( ctx );
    1918    }
    1919 
    1920 #ifdef DIVE
    1921    DiveResizeBuffers(ctx->Buffer->Width,ctx->Buffer->Height);
    1922 #endif
    1923 }
    1924 
    1925 
    1926 
    1927 
    1928 /**********************************************************************/
    1929 /*****                   State update logic                       *****/
    1930 /**********************************************************************/
    1931 
    1932 
    1933 /*
    1934  * Since the device driver may or may not support pixel logic ops we
    1935  * have to make some extensive tests to determine whether or not
    1936  * software-implemented logic operations have to be used.
    1937  */
    1938 static void update_pixel_logic( GLcontext *ctx )
    1939 {
    1940    if (ctx->Visual->RGBAflag) {
    1941       /* RGBA mode blending w/ Logic Op */
    1942       if (ctx->Color.ColorLogicOpEnabled) {
    1943          if (ctx->Driver.LogicOp
    1944              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
    1945             /* Device driver can do logic, don't have to do it in software */
    1946             ctx->Color.SWLogicOpEnabled = GL_FALSE;
    1947          }
    1948          else {
    1949             /* Device driver can't do logic op so we do it in software */
    1950             ctx->Color.SWLogicOpEnabled = GL_TRUE;
    1951          }
    1952       }
    1953       else {
    1954          /* no logic op */
    1955          if (ctx->Driver.LogicOp) {
    1956             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
    1957          }
    1958          ctx->Color.SWLogicOpEnabled = GL_FALSE;
    1959       }
    1960    }
    1961    else {
    1962       /* CI mode Logic Op */
    1963       if (ctx->Color.IndexLogicOpEnabled) {
    1964          if (ctx->Driver.LogicOp
    1965              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
    1966             /* Device driver can do logic, don't have to do it in software */
    1967             ctx->Color.SWLogicOpEnabled = GL_FALSE;
    1968          }
    1969          else {
    1970             /* Device driver can't do logic op so we do it in software */
    1971             ctx->Color.SWLogicOpEnabled = GL_TRUE;
    1972          }
    1973       }
    1974       else {
    1975          /* no logic op */
    1976          if (ctx->Driver.LogicOp) {
    1977             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
    1978          }
    1979          ctx->Color.SWLogicOpEnabled = GL_FALSE;
    1980       }
    1981    }
    1982 }
    1983 
    1984 
    1985 
    1986 /*
    1987  * Check if software implemented RGBA or Color Index masking is needed.
    1988  */
    1989 static void update_pixel_masking( GLcontext *ctx )
    1990 {
    1991    if (ctx->Visual->RGBAflag) {
    1992       GLuint *colorMask = (GLuint *) ctx->Color.ColorMask;
    1993       if (*colorMask == 0xffffffff) {
    1994          /* disable masking */
    1995          if (ctx->Driver.ColorMask) {
    1996             (void) (*ctx->Driver.ColorMask)( ctx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
    1997          }
    1998          ctx->Color.SWmasking = GL_FALSE;
    1999       }
    2000       else {
    2001          /* Ask driver to do color masking, if it can't then
    2002           * do it in software
    2003           */
    2004          GLboolean red   = ctx->Color.ColorMask[RCOMP] ? GL_TRUE : GL_FALSE;
    2005          GLboolean green = ctx->Color.ColorMask[GCOMP] ? GL_TRUE : GL_FALSE;
    2006          GLboolean blue  = ctx->Color.ColorMask[BCOMP] ? GL_TRUE : GL_FALSE;
    2007          GLboolean alpha = ctx->Color.ColorMask[ACOMP] ? GL_TRUE : GL_FALSE;
    2008          if (ctx->Driver.ColorMask
    2009              && (*ctx->Driver.ColorMask)( ctx, red, green, blue, alpha )) {
    2010             ctx->Color.SWmasking = GL_FALSE;
    2011          }
    2012          else {
    2013             ctx->Color.SWmasking = GL_TRUE;
    2014          }
    2015       }
    2016    }
    2017    else {
    2018       if (ctx->Color.IndexMask==0xffffffff) {
    2019          /* disable masking */
    2020          if (ctx->Driver.IndexMask) {
    2021             (void) (*ctx->Driver.IndexMask)( ctx, 0xffffffff );
    2022          }
    2023          ctx->Color.SWmasking = GL_FALSE;
    2024       }
    2025       else {
    2026          /* Ask driver to do index masking, if it can't then
    2027           * do it in software
    2028           */
    2029          if (ctx->Driver.IndexMask
    2030              && (*ctx->Driver.IndexMask)( ctx, ctx->Color.IndexMask )) {
    2031             ctx->Color.SWmasking = GL_FALSE;
    2032          }
    2033          else {
    2034             ctx->Color.SWmasking = GL_TRUE;
    2035          }
    2036       }
    2037    }
    2038 }
    2039 
    2040 
    2041 static void update_fog_mode( GLcontext *ctx )
    2042 {
    2043    int old_mode = ctx->FogMode;
    2044    ctx->FogMode = FOG_NONE;
    2045 
    2046    if (ctx->Fog.Enabled) {
    2047       ctx->FogMode = FOG_VERTEX;
    2048 
    2049       if (ctx->Texture.Enabled || ctx->Hint.Fog == GL_NICEST)
    2050          ctx->FogMode = FOG_FRAGMENT;
    2051 
    2052       if ( ctx->Driver.GetParameteri &&
    2053            ctx->Driver.GetParameteri( ctx, DD_HAVE_HARDWARE_FOG ) )
    2054          ctx->FogMode = FOG_FRAGMENT;
    2055    }
    2056 
    2057    if (old_mode != ctx->FogMode)
    2058       ctx->NewState |= NEW_FOG;
    2059 }
    2060 
    2061 
    2062 /*
    2063  * Recompute the value of ctx->RasterMask, etc. according to
    2064  * the current context.
    2065  */
    2066 static void update_rasterflags( GLcontext *ctx )
    2067 {
    2068    ctx->RasterMask = 0;
    2069 
    2070    if (ctx->Color.AlphaEnabled)         ctx->RasterMask |= ALPHATEST_BIT;
    2071    if (ctx->Color.BlendEnabled)         ctx->RasterMask |= BLEND_BIT;
    2072    if (ctx->Depth.Test)                 ctx->RasterMask |= DEPTH_BIT;
    2073    if (ctx->FogMode==FOG_FRAGMENT)      ctx->RasterMask |= FOG_BIT;
    2074    if (ctx->Color.SWLogicOpEnabled)     ctx->RasterMask |= LOGIC_OP_BIT;
    2075    if (ctx->Scissor.Enabled)            ctx->RasterMask |= SCISSOR_BIT;
    2076    if (ctx->Stencil.Enabled)            ctx->RasterMask |= STENCIL_BIT;
    2077    if (ctx->Color.SWmasking)            ctx->RasterMask |= MASKING_BIT;
    2078 
    2079    if (ctx->Visual->SoftwareAlpha && ctx->Color.ColorMask[ACOMP]
    2080        && ctx->Color.DrawBuffer != GL_NONE)
    2081       ctx->RasterMask |= ALPHABUF_BIT;
    2082 
    2083    if (   ctx->Viewport.X<0
    2084        || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
    2085        || ctx->Viewport.Y<0
    2086        || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
    2087       ctx->RasterMask |= WINCLIP_BIT;
    2088    }
    2089 
    2090    /* If we're not drawing to exactly one color buffer set the
    2091     * MULTI_DRAW_BIT flag.  Also set it if we're drawing to no
    2092     * buffers or the RGBA or CI mask disables all writes.
    2093     */
    2094 
    2095    ctx->TriangleCaps &= ~DD_MULTIDRAW;
    2096 
    2097    if (ctx->Color.MultiDrawBuffer) {
    2098       ctx->RasterMask |= MULTI_DRAW_BIT;
    2099       ctx->TriangleCaps |= DD_MULTIDRAW;
    2100    }
    2101    else if (ctx->Color.DrawBuffer==GL_NONE) {
    2102       ctx->RasterMask |= MULTI_DRAW_BIT;
    2103       ctx->TriangleCaps |= DD_MULTIDRAW;
    2104    }
    2105    else if (ctx->Visual->RGBAflag && ctx->Color.ColorMask==0) {
    2106       /* all RGBA channels disabled */
    2107       ctx->RasterMask |= MULTI_DRAW_BIT;
    2108       ctx->TriangleCaps |= DD_MULTIDRAW;
    2109       ctx->Color.DrawDestMask = 0;
    2110    }
    2111    else if (!ctx->Visual->RGBAflag && ctx->Color.IndexMask==0) {
    2112       /* all color index bits disabled */
    2113       ctx->RasterMask |= MULTI_DRAW_BIT;
    2114       ctx->TriangleCaps |= DD_MULTIDRAW;
    2115       ctx->Color.DrawDestMask = 0;
    2116    }
    2117 }
    2118 
    2119 
    2120 void gl_print_state( const char *msg, GLuint state )
    2121 {
    2122    fprintf(stderr,
    2123            "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
    2124            msg,
    2125            state,
    2126            (state & NEW_LIGHTING)         ? "lighting, " : "",
    2127            (state & NEW_RASTER_OPS)       ? "raster-ops, " : "",
    2128            (state & NEW_TEXTURING)        ? "texturing, " : "",
    2129            (state & NEW_POLYGON)          ? "polygon, " : "",
    2130            (state & NEW_DRVSTATE0)        ? "driver-0, " : "",
    2131            (state & NEW_DRVSTATE1)        ? "driver-1, " : "",
    2132            (state & NEW_DRVSTATE2)        ? "driver-2, " : "",
    2133            (state & NEW_DRVSTATE3)        ? "driver-3, " : "",
    2134            (state & NEW_MODELVIEW)        ? "modelview, " : "",
    2135            (state & NEW_PROJECTION)       ? "projection, " : "",
    2136            (state & NEW_TEXTURE_MATRIX)   ? "texture-matrix, " : "",
    2137            (state & NEW_USER_CLIP)        ? "user-clip, " : "",
    2138            (state & NEW_TEXTURE_ENV)      ? "texture-env, " : "",
    2139            (state & NEW_CLIENT_STATE)     ? "client-state, " : "",
    2140            (state & NEW_FOG)              ? "fog, " : "",
    2141            (state & NEW_NORMAL_TRANSFORM) ? "normal-transform, " : "",
    2142            (state & NEW_VIEWPORT)         ? "viewport, " : "",
    2143            (state & NEW_TEXTURE_ENABLE)   ? "texture-enable, " : "");
    2144 }
    2145 
    2146 void gl_print_enable_flags( const char *msg, GLuint flags )
    2147 {
    2148    fprintf(stderr,
    2149            "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s\n",
    2150            msg,
    2151            flags,
    2152            (flags & ENABLE_TEX0)       ? "tex-0, " : "",
    2153            (flags & ENABLE_TEX1)       ? "tex-1, " : "",
    2154            (flags & ENABLE_LIGHT)      ? "light, " : "",
    2155            (flags & ENABLE_FOG)        ? "fog, " : "",
    2156            (flags & ENABLE_USERCLIP)   ? "userclip, " : "",
    2157            (flags & ENABLE_TEXGEN0)    ? "tex-gen-0, " : "",
    2158            (flags & ENABLE_TEXGEN1)    ? "tex-gen-1, " : "",
    2159            (flags & ENABLE_TEXMAT0)    ? "tex-mat-0, " : "",
    2160            (flags & ENABLE_TEXMAT1)    ? "tex-mat-1, " : "",
    2161            (flags & ENABLE_NORMALIZE)  ? "normalize, " : "",
    2162            (flags & ENABLE_RESCALE)    ? "rescale, " : "");
    2163 }
    2164 
    2165 
    2166 /*
    2167  * If ctx->NewState is non-zero then this function MUST be called before
    2168  * rendering any primitive.  Basically, function pointers and miscellaneous
    2169  * flags are updated to reflect the current state of the state machine.
    2170  */
    2171 void gl_update_state( GLcontext *ctx )
    2172 {
    2173    GLuint i;
    2174 
    2175    if (MESA_VERBOSE & VERBOSE_STATE)
    2176       gl_print_state("", ctx->NewState);
    2177 
    2178    if (ctx->NewState & NEW_CLIENT_STATE)
    2179       gl_update_client_state( ctx );
    2180 
    2181    if ((ctx->NewState & NEW_TEXTURE_ENABLE) &&
    2182        (ctx->Enabled & ENABLE_TEX_ANY) != ctx->Texture.Enabled)
    2183       ctx->NewState |= NEW_TEXTURING | NEW_RASTER_OPS;
    2184 
    2185    if (ctx->NewState & NEW_TEXTURE_ENV) {
    2186       if (ctx->Texture.Unit[0].EnvMode == ctx->Texture.Unit[0].LastEnvMode &&
    2187           ctx->Texture.Unit[1].EnvMode == ctx->Texture.Unit[1].LastEnvMode)
    2188          ctx->NewState &= ~NEW_TEXTURE_ENV;
    2189       ctx->Texture.Unit[0].LastEnvMode = ctx->Texture.Unit[0].EnvMode;
    2190       ctx->Texture.Unit[1].LastEnvMode = ctx->Texture.Unit[1].EnvMode;
    2191    }
    2192 
    2193    if (ctx->NewState & NEW_TEXTURE_MATRIX) {
    2194       ctx->Enabled &= ~(ENABLE_TEXMAT0|ENABLE_TEXMAT1);
    2195 
    2196       for (i=0; i < MAX_TEXTURE_UNITS; i++) {
    2197          if (ctx->TextureMatrix[i].flags & MAT_DIRTY_ALL_OVER)
    2198          {
    2199             gl_matrix_analyze( &ctx->TextureMatrix[i] );
    2200             ctx->TextureMatrix[i].flags &= ~MAT_DIRTY_DEPENDENTS;
    2201 
    2202             if (ctx->Texture.Unit[i].Enabled &&
    2203                 ctx->TextureMatrix[i].type != MATRIX_IDENTITY)
    2204                ctx->Enabled |= ENABLE_TEXMAT0 << i;
    2205          }
    2206       }
    2207    }
    2208 
    2209    if (ctx->NewState & (NEW_TEXTURING | NEW_TEXTURE_ENABLE)) {
    2210       ctx->Texture.NeedNormals = GL_FALSE;
    2211       gl_update_dirty_texobjs(ctx);
    2212       ctx->Enabled &= ~(ENABLE_TEXGEN0|ENABLE_TEXGEN1);
    2213       ctx->Texture.ReallyEnabled = 0;
    2214 
    2215       for (i=0; i < MAX_TEXTURE_UNITS; i++) {
    2216          if (ctx->Texture.Unit[i].Enabled) {
    2217             gl_update_texture_unit( ctx, &ctx->Texture.Unit[i] );
    2218 
    2219             ctx->Texture.ReallyEnabled |=
    2220                ctx->Texture.Unit[i].ReallyEnabled<<(i*4);
    2221 
    2222             if (ctx->Texture.Unit[i].GenFlags != 0) {
    2223                ctx->Enabled |= ENABLE_TEXGEN0 << i;
    2224 
    2225                if (ctx->Texture.Unit[i].GenFlags & TEXGEN_NEED_NORMALS)
    2226                {
    2227                   ctx->Texture.NeedNormals = GL_TRUE;
    2228                   ctx->Texture.NeedEyeCoords = GL_TRUE;
    2229                }
    2230 
    2231                if (ctx->Texture.Unit[i].GenFlags & TEXGEN_NEED_EYE_COORD)
    2232                {
    2233                   ctx->Texture.NeedEyeCoords = GL_TRUE;
    2234                }
    2235             }
    2236          }
    2237       }
    2238 
    2239       ctx->Texture.Enabled = ctx->Enabled & ENABLE_TEX_ANY;
    2240       ctx->NeedNormals = (ctx->Light.Enabled || ctx->Texture.NeedNormals);
    2241    }
    2242 
    2243    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING | NEW_FOG)) {
    2244 
    2245 
    2246       if (ctx->NewState & NEW_RASTER_OPS) {
    2247          update_pixel_logic(ctx);
    2248          update_pixel_masking(ctx);
    2249          update_fog_mode(ctx);
    2250          update_rasterflags(ctx);
    2251          if (ctx->Driver.Dither) {
    2252             (*ctx->Driver.Dither)( ctx, ctx->Color.DitherFlag );
    2253          }
    2254 
    2255          /* Check if incoming colors can be modified during rasterization */
    2256          if (ctx->Fog.Enabled ||
    2257              ctx->Texture.Enabled ||
    2258              ctx->Color.BlendEnabled ||
    2259              ctx->Color.SWmasking ||
    2260              ctx->Color.SWLogicOpEnabled) {
    2261             ctx->MutablePixels = GL_TRUE;
    2262          }
    2263          else {
    2264             ctx->MutablePixels = GL_FALSE;
    2265          }
    2266 
    2267          /* update scissor region */
    2268 
    2269          ctx->Buffer->Xmin = 0;
    2270          ctx->Buffer->Ymin = 0;
    2271          ctx->Buffer->Xmax = ctx->Buffer->Width-1;
    2272          ctx->Buffer->Ymax = ctx->Buffer->Height-1;
    2273          if (ctx->Scissor.Enabled) {
    2274             if (ctx->Scissor.X > ctx->Buffer->Xmin) {
    2275                ctx->Buffer->Xmin = ctx->Scissor.X;
    2276             }
    2277             if (ctx->Scissor.Y > ctx->Buffer->Ymin) {
    2278                ctx->Buffer->Ymin = ctx->Scissor.Y;
    2279             }
    2280             if (ctx->Scissor.X + ctx->Scissor.Width - 1 < ctx->Buffer->Xmax) {
    2281                ctx->Buffer->Xmax = ctx->Scissor.X + ctx->Scissor.Width - 1;
    2282             }
    2283             if (ctx->Scissor.Y + ctx->Scissor.Height - 1 < ctx->Buffer->Ymax) {
    2284                ctx->Buffer->Ymax = ctx->Scissor.Y + ctx->Scissor.Height - 1;
    2285             }
    2286          }
    2287 
    2288          /* The driver isn't managing the depth buffer.
    2289           */
    2290          if (ctx->Driver.AllocDepthBuffer == gl_alloc_depth_buffer)
    2291          {
    2292             if (ctx->Depth.Mask) {
    2293                switch (ctx->Depth.Func) {
    2294                case GL_LESS:
    2295                   ctx->Driver.DepthTestSpan = gl_depth_test_span_less;
    2296                   ctx->Driver.DepthTestPixels = gl_depth_test_pixels_less;
    2297                   break;
    2298                case GL_GREATER:
    2299                   ctx->Driver.DepthTestSpan = gl_depth_test_span_greater;
    2300                   ctx->Driver.DepthTestPixels = gl_depth_test_pixels_greater;
    2301                   break;
    2302                default:
    2303                   ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
    2304                   ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
    2305                }
    2306             }
    2307             else {
    2308                ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
    2309                ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
    2310             }
    2311          }
    2312       }
    2313 
    2314       if (ctx->NewState & NEW_LIGHTING) {
    2315          ctx->TriangleCaps &= ~(DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL);
    2316          if (ctx->Light.Enabled) {
    2317             if (ctx->Light.Model.TwoSide)
    2318                ctx->TriangleCaps |= (DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL);
    2319             gl_update_lighting(ctx);
    2320          }
    2321       }
    2322    }
    2323 
    2324    if (ctx->NewState & (NEW_POLYGON | NEW_LIGHTING)) {
    2325 
    2326       ctx->TriangleCaps &= ~DD_TRI_CULL_FRONT_BACK;
    2327 
    2328       if (ctx->NewState & NEW_POLYGON) {
    2329          /* Setup CullBits bitmask */
    2330          if (ctx->Polygon.CullFlag) {
    2331             ctx->backface_sign = 1;
    2332             switch(ctx->Polygon.CullFaceMode) {
    2333             case GL_BACK:
    2334                if(ctx->Polygon.FrontFace==GL_CCW)
    2335                   ctx->backface_sign = -1;
    2336                ctx->Polygon.CullBits = 1;
    2337                break;
    2338             case GL_FRONT:
    2339                if(ctx->Polygon.FrontFace!=GL_CCW)
    2340                   ctx->backface_sign = -1;
    2341                ctx->Polygon.CullBits = 2;
    2342                break;
    2343             default:
    2344             case GL_FRONT_AND_BACK:
    2345                ctx->backface_sign = 0;
    2346                ctx->Polygon.CullBits = 0;
    2347                ctx->TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
    2348                break;
    2349             }
    2350          }
    2351          else {
    2352             ctx->Polygon.CullBits = 3;
    2353             ctx->backface_sign = 0;
    2354          }
    2355 
    2356          /* Any Polygon offsets enabled? */
    2357          ctx->TriangleCaps &= ~DD_TRI_OFFSET;
    2358 
    2359          if (ctx->Polygon.OffsetPoint ||
    2360              ctx->Polygon.OffsetLine ||
    2361              ctx->Polygon.OffsetFill)
    2362             ctx->TriangleCaps |= DD_TRI_OFFSET;
    2363 
    2364          /* reset Z offsets now */
    2365          ctx->PointZoffset   = 0.0;
    2366          ctx->LineZoffset    = 0.0;
    2367          ctx->PolygonZoffset = 0.0;
    2368       }
    2369    }
    2370 
    2371    if (ctx->NewState & ~(NEW_CLIENT_STATE|
    2372                          NEW_DRIVER_STATE|NEW_USER_CLIP|
    2373                          NEW_POLYGON))
    2374       gl_update_clipmask(ctx);
    2375 
    2376    if (ctx->NewState & (NEW_LIGHTING|
    2377                         NEW_RASTER_OPS|
    2378                         NEW_TEXTURING|
    2379                         NEW_TEXTURE_ENABLE|
    2380                         NEW_TEXTURE_ENV|
    2381                         NEW_POLYGON|
    2382                         NEW_DRVSTATE0|
    2383                         NEW_DRVSTATE1|
    2384                         NEW_DRVSTATE2|
    2385                         NEW_DRVSTATE3|
    2386                         NEW_USER_CLIP))
    2387    {
    2388       ctx->IndirectTriangles = ctx->TriangleCaps & ~ctx->Driver.TriangleCaps;
    2389       ctx->IndirectTriangles |= DD_SW_RASTERIZE;
    2390 
    2391       if (MESA_VERBOSE&VERBOSE_CULL)
    2392          gl_print_tri_caps("initial indirect tris", ctx->IndirectTriangles);
    2393 
    2394       ctx->Driver.PointsFunc = NULL;
    2395       ctx->Driver.LineFunc = NULL;
    2396       ctx->Driver.TriangleFunc = NULL;
    2397       ctx->Driver.QuadFunc = NULL;
    2398       ctx->Driver.RectFunc = NULL;
    2399       ctx->Driver.RenderVBClippedTab = NULL;
    2400       ctx->Driver.RenderVBCulledTab = NULL;
    2401       ctx->Driver.RenderVBRawTab = NULL;
    2402 
    2403       /*
    2404        * Here the driver sets up all the ctx->Driver function pointers to
    2405        * it's specific, private functions.
    2406        */
    2407       ctx->Driver.UpdateState(ctx);
    2408 
    2409       if (MESA_VERBOSE&VERBOSE_CULL)
    2410          gl_print_tri_caps("indirect tris", ctx->IndirectTriangles);
    2411 
    2412       /*
    2413        * In case the driver didn't hook in an optimized point, line or
    2414        * triangle function we'll now select "core/fallback" point, line
    2415        * and triangle functions.
    2416        */
    2417       if (ctx->IndirectTriangles & DD_SW_RASTERIZE) {
    2418          gl_set_point_function(ctx);
    2419          gl_set_line_function(ctx);
    2420          gl_set_triangle_function(ctx);
    2421          gl_set_quad_function(ctx);
    2422 
    2423          if ((ctx->IndirectTriangles &
    2424               (DD_TRI_SW_RASTERIZE|DD_QUAD_SW_RASTERIZE|DD_TRI_CULL)) ==
    2425              (DD_TRI_SW_RASTERIZE|DD_QUAD_SW_RASTERIZE|DD_TRI_CULL))
    2426             ctx->IndirectTriangles &= ~DD_TRI_CULL;
    2427       }
    2428 
    2429       if (MESA_VERBOSE&VERBOSE_CULL)
    2430          gl_print_tri_caps("indirect tris 2", ctx->IndirectTriangles);
    2431 
    2432       gl_set_render_vb_function(ctx);
    2433    }
    2434 
    2435    /* Should only be calc'd when !need_eye_coords and not culling.
    2436     */
    2437    if (ctx->NewState & (NEW_MODELVIEW|NEW_PROJECTION)) {
    2438       if (ctx->NewState & NEW_MODELVIEW) {
    2439          gl_matrix_analyze( &ctx->ModelView );
    2440          ctx->ProjectionMatrix.flags &= ~MAT_DIRTY_DEPENDENTS;
    2441       }
    2442 
    2443       if (ctx->NewState & NEW_PROJECTION) {
    2444          gl_matrix_analyze( &ctx->ProjectionMatrix );
    2445          ctx->ProjectionMatrix.flags &= ~MAT_DIRTY_DEPENDENTS;
    2446 
    2447          if (ctx->Transform.AnyClip) {
    2448             gl_update_userclip( ctx );
    2449          }
    2450       }
    2451 
    2452       gl_calculate_model_project_matrix( ctx );
    2453       ctx->ModelProjectWinMatrixUptodate = 0;
    2454    }
    2455 
    2456    /* Figure out whether we can light in object space or not.  If we
    2457     * can, find the current positions of the lights in object space
    2458     */
    2459    if ((ctx->Enabled & (ENABLE_POINT_ATTEN | ENABLE_LIGHT | ENABLE_FOG |
    2460                         ENABLE_TEXGEN0 | ENABLE_TEXGEN1)) &&
    2461        (ctx->NewState & (NEW_LIGHTING |
    2462                          NEW_FOG |
    2463                          NEW_MODELVIEW |
    2464                          NEW_PROJECTION |
    2465                          NEW_TEXTURING |
    2466                          NEW_RASTER_OPS |
    2467                          NEW_USER_CLIP)))
    2468    {
    2469       GLboolean oldcoord, oldnorm;
    2470 
    2471       oldcoord = ctx->NeedEyeCoords;
    2472       oldnorm = ctx->NeedEyeNormals;
    2473 
    2474       ctx->NeedNormals = (ctx->Light.Enabled || ctx->Texture.NeedNormals);
    2475       ctx->NeedEyeCoords = (ctx->FogMode == FOG_VERTEX ||
    2476                             ctx->Point.Attenuated);
    2477       ctx->NeedEyeNormals = GL_FALSE;
    2478 
    2479       if (ctx->Light.Enabled) {
    2480          if (ctx->Light.Flags & LIGHT_POSITIONAL) {
    2481             /* Need length for attenuation */
    2482             if (!TEST_MAT_FLAGS( &ctx->ModelView, MAT_FLAGS_LENGTH_PRESERVING))
    2483                ctx->NeedEyeCoords = GL_TRUE;
    2484          } else if (ctx->Light.NeedVertices) {
    2485             /* Need angle for spot calculations */
    2486             if (!TEST_MAT_FLAGS( &ctx->ModelView, MAT_FLAGS_ANGLE_PRESERVING))
    2487                ctx->NeedEyeCoords = GL_TRUE;
    2488          }
    2489          ctx->NeedEyeNormals = ctx->NeedEyeCoords;
    2490       }
    2491       if (ctx->Texture.Enabled || ctx->RenderMode==GL_FEEDBACK) {
    2492          if (ctx->Texture.NeedEyeCoords) ctx->NeedEyeCoords = GL_TRUE;
    2493          if (ctx->Texture.NeedNormals)
    2494             ctx->NeedNormals = ctx->NeedEyeNormals = GL_TRUE;
    2495       }
    2496 
    2497       ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
    2498 
    2499       if (ctx->NeedEyeCoords)
    2500          ctx->vb_proj_matrix = &ctx->ProjectionMatrix;
    2501 
    2502       if (ctx->Light.Enabled) {
    2503          gl_update_lighting_function(ctx);
    2504 
    2505          if ( (ctx->NewState & NEW_LIGHTING) ||
    2506               ((ctx->NewState & (NEW_MODELVIEW| NEW_PROJECTION)) &&
    2507                !ctx->NeedEyeCoords) ||
    2508               oldcoord != ctx->NeedEyeCoords ||
    2509               oldnorm != ctx->NeedEyeNormals) {
    2510             gl_compute_light_positions(ctx);
    2511          }
    2512 
    2513          ctx->rescale_factor = 1.0F;
    2514 
    2515          if (ctx->ModelView.flags & (MAT_FLAG_UNIFORM_SCALE |
    2516                                      MAT_FLAG_GENERAL_SCALE |
    2517                                      MAT_FLAG_GENERAL_3D |
    2518                                      MAT_FLAG_GENERAL) )
    2519 
    2520          {
    2521             GLfloat *m = ctx->ModelView.inv;
    2522             GLfloat f = m[2]*m[2] + m[6]*m[6] + m[10]*m[10];
    2523             if (f > 1e-12 && (f-1)*(f-1) > 1e-12)
    2524                ctx->rescale_factor = 1.0/GL_SQRT(f);
    2525          }
    2526       }
    2527 
    2528       gl_update_normal_transform( ctx );
    2529    }
    2530 
    2531    gl_update_pipelines(ctx);
    2532    ctx->NewState = 0;
    2533 }
     1945void
     1946_mesa_Finish( void )
     1947{
     1948   GET_CURRENT_CONTEXT(ctx);
     1949   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFinish");
     1950   if (ctx->Driver.Finish) {
     1951      (*ctx->Driver.Finish)( ctx );
     1952   }
     1953}
     1954
     1955
     1956
     1957void
     1958_mesa_Flush( void )
     1959{
     1960   GET_CURRENT_CONTEXT(ctx);
     1961   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFlush");
     1962   if (ctx->Driver.Flush) {
     1963      (*ctx->Driver.Flush)( ctx );
     1964   }
     1965}
Note: See TracChangeset for help on using the changeset viewer.