home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / MesaDLL / context.cpp < prev    next >
C/C++ Source or Header  |  2002-12-08  |  78KB  |  2,386 lines

  1. /* $Id: context.c,v 1.189 2002/11/19 15:25:00 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  5.1
  6.  *
  7.  * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28. #include "glheader.h"
  29. #include "imports.h"
  30. #include "buffers.h"
  31. #include "clip.h"
  32. #include "colortab.h"
  33. #include "context.h"
  34. #include "dlist.h"
  35. #include "eval.h"
  36. #include "enums.h"
  37. #include "extensions.h"
  38. #include "fog.h"
  39. #include "get.h"
  40. #include "glthread.h"
  41. #include "hash.h"
  42. #include "light.h"
  43. #include "macros.h"
  44. #include "mmath.h"
  45. #include "simple_list.h"
  46. #include "state.h"
  47. #include "teximage.h"
  48. #include "texobj.h"
  49. #include "texstate.h"
  50. #include "mtypes.h"
  51. #include "varray.h"
  52. #if FEATURE_NV_vertex_program
  53. #include "vpstate.h"
  54. #endif
  55. #include "vtxfmt.h"
  56. #include "math/m_translate.h"
  57. #include "math/m_matrix.h"
  58. #include "math/m_xform.h"
  59. #include "math/mathmod.h"
  60.  
  61.  
  62. #if defined(MESA_TRACE)
  63. #include "Trace/tr_context.h"
  64. #include "Trace/tr_wrapper.h"
  65. #endif
  66.  
  67. #ifdef USE_SPARC_ASM
  68. //EK#include "SPARC/sparc.h"
  69. #endif
  70.  
  71. #ifndef MESA_VERBOSE
  72. int MESA_VERBOSE = 0;
  73. //int  MESA_VERBOSE=0xffff;
  74. #endif
  75.  
  76. #ifndef MESA_DEBUG_FLAGS
  77. int MESA_DEBUG_FLAGS = 0;
  78. #endif
  79.  
  80.  
  81. static void
  82. free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
  83.  
  84.  
  85. /**********************************************************************/
  86. /*****       OpenGL SI-style interface (new in Mesa 3.5)          *****/
  87. /**********************************************************************/
  88.  
  89. /* Called by window system/device driver (via gc->exports.destroyCurrent())
  90.  * when the rendering context is to be destroyed.
  91.  */
  92. GLboolean
  93. _mesa_destroyContext(__GLcontext *gc)
  94. {
  95.    if (gc) {
  96.       _mesa_free_context_data(gc);
  97.       _mesa_free(gc);
  98.    }
  99.    return GL_TRUE;
  100. }
  101.  
  102. /* Called by window system/device driver (via gc->exports.loseCurrent())
  103.  * when the rendering context is made non-current.
  104.  */
  105. GLboolean
  106. _mesa_loseCurrent(__GLcontext *gc)
  107. {
  108.    /* XXX unbind context from thread */
  109.    return GL_TRUE;
  110. }
  111.  
  112. /* Called by window system/device driver (via gc->exports.makeCurrent())
  113.  * when the rendering context is made current.
  114.  */
  115. GLboolean
  116. _mesa_makeCurrent(__GLcontext *gc)
  117. {
  118.    /* XXX bind context to thread */
  119.    return GL_TRUE;
  120. }
  121.  
  122. /* Called by window system/device driver - yadda, yadda, yadda.
  123.  * See above comments.
  124.  */
  125. GLboolean
  126. _mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
  127. {
  128.    if (gc && gcShare && gc->Shared && gcShare->Shared) {
  129.       gc->Shared->RefCount--;
  130.       if (gc->Shared->RefCount == 0) {
  131.          free_shared_state(gc, gc->Shared);
  132.       }
  133.       gc->Shared = gcShare->Shared;
  134.       gc->Shared->RefCount++;
  135.       return GL_TRUE;
  136.    }
  137.    else {
  138.       return GL_FALSE;
  139.    }
  140. }
  141.  
  142. GLboolean
  143. _mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
  144. {
  145.    if (dst && src) {
  146.       _mesa_copy_context( src, dst, mask );
  147.       return GL_TRUE;
  148.    }
  149.    else {
  150.       return GL_FALSE;
  151.    }
  152. }
  153.  
  154. GLboolean
  155. _mesa_forceCurrent(__GLcontext *gc)
  156. {
  157.    return GL_TRUE;
  158. }
  159.  
  160. GLboolean
  161. _mesa_notifyResize(__GLcontext *gc)
  162. {
  163.    GLint x, y;
  164.    GLuint width, height;
  165.    __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
  166.    if (!d || !d->getDrawableSize)
  167.       return GL_FALSE;
  168.    d->getDrawableSize( d, &x, &y, &width, &height );
  169.    /* update viewport, resize software buffers, etc. */
  170.    return GL_TRUE;
  171. }
  172.  
  173. void
  174. _mesa_notifyDestroy(__GLcontext *gc)
  175. {
  176.    /* Called when the context's window/buffer is going to be destroyed. */
  177.    /* Unbind from it. */
  178. }
  179.  
  180. /* Called by window system just before swapping buffers.
  181.  * We have to finish any pending rendering.
  182.  */
  183. void
  184. _mesa_notifySwapBuffers(__GLcontext *gc)
  185. {
  186.    FLUSH_VERTICES( gc, 0 );
  187. }
  188.  
  189. struct __GLdispatchStateRec *
  190. _mesa_dispatchExec(__GLcontext *gc)
  191. {
  192.    return NULL;
  193. }
  194.  
  195. void
  196. _mesa_beginDispatchOverride(__GLcontext *gc)
  197. {
  198. }
  199.  
  200. void
  201. _mesa_endDispatchOverride(__GLcontext *gc)
  202. {
  203. }
  204.  
  205. /* Setup the exports.  The window system will call these functions
  206.  * when it needs Mesa to do something.
  207.  * NOTE: Device drivers should override these functions!  For example,
  208.  * the Xlib driver should plug in the XMesa*-style functions into this
  209.  * structure.  The XMesa-style functions should then call the _mesa_*
  210.  * version of these functions.  This is an approximation to OO design
  211.  * (inheritance and virtual functions).
  212.  */
  213. static void
  214. _mesa_init_default_exports(__GLexports *exports)
  215. {
  216.     exports->destroyContext = _mesa_destroyContext;
  217.     exports->loseCurrent = _mesa_loseCurrent;
  218.     exports->makeCurrent = _mesa_makeCurrent;
  219.     exports->shareContext = _mesa_shareContext;
  220.     exports->copyContext = _mesa_copyContext;
  221.     exports->forceCurrent = _mesa_forceCurrent;
  222.     exports->notifyResize = _mesa_notifyResize;
  223.     exports->notifyDestroy = _mesa_notifyDestroy;
  224.     exports->notifySwapBuffers = _mesa_notifySwapBuffers;
  225.     exports->dispatchExec = _mesa_dispatchExec;
  226.     exports->beginDispatchOverride = _mesa_beginDispatchOverride;
  227.     exports->endDispatchOverride = _mesa_endDispatchOverride;
  228. }
  229.  
  230.  
  231.  
  232. /* exported OpenGL SI interface */
  233. __GLcontext *
  234. __glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
  235. {
  236.     GLcontext *ctx;
  237.  
  238.     ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
  239.     if (ctx == NULL) {
  240.     return NULL;
  241.     }
  242.  
  243.     _mesa_initialize_context(ctx, modes, NULL, imports, GL_FALSE);
  244.     ctx->imports = *imports;
  245.  
  246.     return ctx;
  247. }
  248.  
  249.  
  250. /* exported OpenGL SI interface */
  251. void
  252. __glCoreNopDispatch(void)
  253. {
  254. #if 0
  255.    /* SI */
  256.    __gl_dispatch = __glNopDispatchState;
  257. #else
  258.    /* Mesa */
  259.    _glapi_set_dispatch(NULL);
  260. #endif
  261. }
  262.  
  263.  
  264. /**********************************************************************/
  265. /***** GL Visual allocation/destruction                           *****/
  266. /**********************************************************************/
  267.  
  268.  
  269. /*
  270.  * Allocate a new GLvisual object.
  271.  * Input:  rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
  272.  *         dbFlag - double buffering?
  273.  *         stereoFlag - stereo buffer?
  274.  *         depthBits - requested bits per depth buffer value
  275.  *                     Any value in [0, 32] is acceptable but the actual
  276.  *                     depth type will be GLushort or GLuint as needed.
  277.  *         stencilBits - requested minimum bits per stencil buffer value
  278.  *         accumBits - requested minimum bits per accum buffer component
  279.  *         indexBits - number of bits per pixel if rgbFlag==GL_FALSE
  280.  *         red/green/blue/alphaBits - number of bits per color component
  281.  *                                    in frame buffer for RGB(A) mode.
  282.  *                                    We always use 8 in core Mesa though.
  283.  * Return:  pointer to new GLvisual or NULL if requested parameters can't
  284.  *          be met.
  285.  */
  286. GLvisual *
  287. _mesa_create_visual( GLboolean rgbFlag,
  288.                      GLboolean dbFlag,
  289.                      GLboolean stereoFlag,
  290.                      GLint redBits,
  291.                      GLint greenBits,
  292.                      GLint blueBits,
  293.                      GLint alphaBits,
  294.                      GLint indexBits,
  295.                      GLint depthBits,
  296.                      GLint stencilBits,
  297.                      GLint accumRedBits,
  298.                      GLint accumGreenBits,
  299.                      GLint accumBlueBits,
  300.                      GLint accumAlphaBits,
  301.                      GLint numSamples )
  302. {
  303.    GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
  304.    if (vis) {
  305.       if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
  306.                                    redBits, greenBits, blueBits, alphaBits,
  307.                                    indexBits, depthBits, stencilBits,
  308.                                    accumRedBits, accumGreenBits,
  309.                                    accumBlueBits, accumAlphaBits,
  310.                                    numSamples)) {
  311.          FREE(vis);
  312.          return NULL;
  313.       }
  314.    }
  315.    return vis;
  316. }
  317.  
  318.  
  319. /*
  320.  * Initialize the fields of the given GLvisual.
  321.  * Input:  see _mesa_create_visual() above.
  322.  * Return: GL_TRUE = success
  323.  *         GL_FALSE = failure.
  324.  */
  325. GLboolean
  326. _mesa_initialize_visual( GLvisual *vis,
  327.                          GLboolean rgbFlag,
  328.                          GLboolean dbFlag,
  329.                          GLboolean stereoFlag,
  330.                          GLint redBits,
  331.                          GLint greenBits,
  332.                          GLint blueBits,
  333.                          GLint alphaBits,
  334.                          GLint indexBits,
  335.                          GLint depthBits,
  336.                          GLint stencilBits,
  337.                          GLint accumRedBits,
  338.                          GLint accumGreenBits,
  339.                          GLint accumBlueBits,
  340.                          GLint accumAlphaBits,
  341.                          GLint numSamples )
  342. {
  343.    (void) numSamples;
  344.  
  345.    assert(vis);
  346.  
  347.    /* This is to catch bad values from device drivers not updated for
  348.     * Mesa 3.3.  Some device drivers just passed 1.  That's a REALLY
  349.     * bad value now (a 1-bit depth buffer!?!).
  350.     */
  351.    assert(depthBits == 0 || depthBits > 1);
  352.  
  353.    if (depthBits < 0 || depthBits > 32) {
  354.       return GL_FALSE;
  355.    }
  356.    if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
  357.       return GL_FALSE;
  358.    }
  359.    if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
  360.       return GL_FALSE;
  361.    }
  362.    if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
  363.       return GL_FALSE;
  364.    }
  365.    if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
  366.       return GL_FALSE;
  367.    }
  368.    if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
  369.       return GL_FALSE;
  370.    }
  371.  
  372.    vis->rgbMode          = rgbFlag;
  373.    vis->doubleBufferMode = dbFlag;
  374.    vis->stereoMode       = stereoFlag;
  375.  
  376.    vis->redBits          = redBits;
  377.    vis->greenBits        = greenBits;
  378.    vis->blueBits         = blueBits;
  379.    vis->alphaBits        = alphaBits;
  380.  
  381.    vis->indexBits      = indexBits;
  382.    vis->depthBits      = depthBits;
  383.    vis->accumRedBits   = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
  384.    vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
  385.    vis->accumBlueBits  = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
  386.    vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
  387.    vis->stencilBits    = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
  388.  
  389.    vis->haveAccumBuffer   = accumRedBits > 0;
  390.    vis->haveDepthBuffer   = depthBits > 0;
  391.    vis->haveStencilBuffer = stencilBits > 0;
  392.  
  393.    vis->numAuxBuffers = 0;
  394.    vis->level = 0;
  395.    vis->pixmapMode = 0;
  396.  
  397.    return GL_TRUE;
  398. }
  399.  
  400.  
  401. void
  402. _mesa_destroy_visual( GLvisual *vis )
  403. {
  404.    FREE(vis);
  405. }
  406.  
  407.  
  408. /**********************************************************************/
  409. /***** GL Framebuffer allocation/destruction                      *****/
  410. /**********************************************************************/
  411.  
  412.  
  413. /*
  414.  * Create a new framebuffer.  A GLframebuffer is a struct which
  415.  * encapsulates the depth, stencil and accum buffers and related
  416.  * parameters.
  417.  * Input:  visual - a GLvisual pointer (we copy the struct contents)
  418.  *         softwareDepth - create/use a software depth buffer?
  419.  *         softwareStencil - create/use a software stencil buffer?
  420.  *         softwareAccum - create/use a software accum buffer?
  421.  *         softwareAlpha - create/use a software alpha buffer?
  422.  * Return:  pointer to new GLframebuffer struct or NULL if error.
  423.  */
  424. GLframebuffer *
  425. _mesa_create_framebuffer( const GLvisual *visual,
  426.                           GLboolean softwareDepth,
  427.                           GLboolean softwareStencil,
  428.                           GLboolean softwareAccum,
  429.                           GLboolean softwareAlpha )
  430. {
  431.    GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
  432.    assert(visual);
  433.    if (buffer) {
  434.       _mesa_initialize_framebuffer(buffer, visual,
  435.                                    softwareDepth, softwareStencil,
  436.                                    softwareAccum, softwareAlpha );
  437.    }
  438.    return buffer;
  439. }
  440.  
  441.  
  442. /*
  443.  * Initialize a GLframebuffer object.
  444.  * Input:  See _mesa_create_framebuffer() above.
  445.  */
  446. void
  447. _mesa_initialize_framebuffer( GLframebuffer *buffer,
  448.                               const GLvisual *visual,
  449.                               GLboolean softwareDepth,
  450.                               GLboolean softwareStencil,
  451.                               GLboolean softwareAccum,
  452.                               GLboolean softwareAlpha )
  453. {
  454.    assert(buffer);
  455.    assert(visual);
  456.  
  457.    _mesa_bzero(buffer, sizeof(GLframebuffer));
  458.  
  459.    /* sanity checks */
  460.    if (softwareDepth ) {
  461.       assert(visual->depthBits > 0);
  462.    }
  463.    if (softwareStencil) {
  464.       assert(visual->stencilBits > 0);
  465.    }
  466.    if (softwareAccum) {
  467.       assert(visual->rgbMode);
  468.       assert(visual->accumRedBits > 0);
  469.       assert(visual->accumGreenBits > 0);
  470.       assert(visual->accumBlueBits > 0);
  471.    }
  472.    if (softwareAlpha) {
  473.       assert(visual->rgbMode);
  474.       assert(visual->alphaBits > 0);
  475.    }
  476.  
  477.    buffer->Visual = *visual;
  478.    buffer->UseSoftwareDepthBuffer = softwareDepth;
  479.    buffer->UseSoftwareStencilBuffer = softwareStencil;
  480.    buffer->UseSoftwareAccumBuffer = softwareAccum;
  481.    buffer->UseSoftwareAlphaBuffers = softwareAlpha;
  482. }
  483.  
  484.  
  485. /*
  486.  * Free a framebuffer struct and its buffers.
  487.  */
  488. void
  489. _mesa_destroy_framebuffer( GLframebuffer *buffer )
  490. {
  491.    if (buffer) {
  492.       _mesa_free_framebuffer_data(buffer);
  493.       FREE(buffer);
  494.    }
  495. }
  496.  
  497.  
  498. /*
  499.  * Free the data hanging off of <buffer>, but not <buffer> itself.
  500.  */
  501. void
  502. _mesa_free_framebuffer_data( GLframebuffer *buffer )
  503. {
  504.    if (!buffer)
  505.       return;
  506.  
  507.    if (buffer->DepthBuffer) {
  508.       MESA_PBUFFER_FREE( buffer->DepthBuffer );
  509.       buffer->DepthBuffer = NULL;
  510.    }
  511.    if (buffer->Accum) {
  512.       MESA_PBUFFER_FREE( buffer->Accum );
  513.       buffer->Accum = NULL;
  514.    }
  515.    if (buffer->Stencil) {
  516.       MESA_PBUFFER_FREE( buffer->Stencil );
  517.       buffer->Stencil = NULL;
  518.    }
  519.    if (buffer->FrontLeftAlpha) {
  520.       MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
  521.       buffer->FrontLeftAlpha = NULL;
  522.    }
  523.    if (buffer->BackLeftAlpha) {
  524.       MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
  525.       buffer->BackLeftAlpha = NULL;
  526.    }
  527.    if (buffer->FrontRightAlpha) {
  528.       MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
  529.       buffer->FrontRightAlpha = NULL;
  530.    }
  531.    if (buffer->BackRightAlpha) {
  532.       MESA_PBUFFER_FREE( buffer->BackRightAlpha );
  533.       buffer->BackRightAlpha = NULL;
  534.    }
  535. }
  536.  
  537.  
  538.  
  539. /**********************************************************************/
  540. /*****       Context allocation, initialization, destroying       *****/
  541. /**********************************************************************/
  542.  
  543.  
  544. _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
  545.  
  546.  
  547. /*
  548.  * This function just calls all the various one-time-init functions in Mesa.
  549.  */
  550. static void
  551. one_time_init( GLcontext *ctx )
  552. {
  553.    static GLboolean alreadyCalled = GL_FALSE;
  554.    _glthread_LOCK_MUTEX(OneTimeLock);
  555.    if (!alreadyCalled) {
  556.       /* do some implementation tests */
  557.       assert( sizeof(GLbyte) == 1 );
  558.       assert( sizeof(GLshort) >= 2 );
  559.       assert( sizeof(GLint) >= 4 );
  560.       assert( sizeof(GLubyte) == 1 );
  561.       assert( sizeof(GLushort) >= 2 );
  562.       assert( sizeof(GLuint) >= 4 );
  563.  
  564.       _mesa_init_lists();
  565.  
  566.       _math_init();
  567.       _mesa_init_math();
  568.  
  569. #ifdef USE_SPARC_ASM
  570.       _mesa_init_sparc_glapi_relocs();
  571. #endif
  572.       if (_mesa_getenv("MESA_DEBUG")) {
  573.          _glapi_noop_enable_warnings(GL_TRUE);
  574. #ifndef GLX_DIRECT_RENDERING
  575.          /* libGL from before 2002/06/28 don't have this function.  Someday,
  576.           * when newer libGL libs are common, remove the #ifdef test.  This
  577.           * only serves to print warnings when calling undefined GL functions.
  578.           */
  579.          _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
  580. #endif
  581.       }
  582.       else {
  583.          _glapi_noop_enable_warnings(GL_FALSE);
  584.       }
  585.  
  586. #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
  587.       _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
  588. #endif
  589.  
  590.       alreadyCalled = GL_TRUE;
  591.    }
  592.    _glthread_UNLOCK_MUTEX(OneTimeLock);
  593. }
  594.  
  595.  
  596. static void
  597. init_matrix_stack( struct matrix_stack *stack,
  598.                    GLuint maxDepth, GLuint dirtyFlag )
  599. {
  600.    GLuint i;
  601.  
  602.    stack->Depth = 0;
  603.    stack->MaxDepth = maxDepth;
  604.    stack->DirtyFlag = dirtyFlag;
  605.    /* The stack */
  606.    stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
  607.    for (i = 0; i < maxDepth; i++) {
  608.       _math_matrix_ctr(&stack->Stack[i]);
  609.       _math_matrix_alloc_inv(&stack->Stack[i]);
  610.    }
  611.    stack->Top = stack->Stack;
  612. }
  613.  
  614.  
  615. static void
  616. free_matrix_stack( struct matrix_stack *stack )
  617. {
  618.    GLuint i;
  619.    for (i = 0; i < stack->MaxDepth; i++) {
  620.       _math_matrix_dtr(&stack->Stack[i]);
  621.    }
  622.    FREE(stack->Stack);
  623.    stack->Stack = stack->Top = NULL;
  624. }
  625.  
  626.  
  627. /*
  628.  * Allocate and initialize a shared context state structure.
  629.  */
  630. static struct gl_shared_state *
  631. alloc_shared_state( void )
  632. {
  633.    struct gl_shared_state *ss;
  634.    GLboolean outOfMemory;
  635.  
  636.    ss = CALLOC_STRUCT(gl_shared_state);
  637.    if (!ss)
  638.       return NULL;
  639.  
  640.    _glthread_INIT_MUTEX(ss->Mutex);
  641.  
  642.    ss->DisplayList = _mesa_NewHashTable();
  643.    ss->TexObjects = _mesa_NewHashTable();
  644. #if FEATURE_NV_vertex_program
  645.    ss->VertexPrograms = _mesa_NewHashTable();
  646. #endif
  647.  
  648.    /* Default Texture objects */
  649.    outOfMemory = GL_FALSE;
  650.  
  651.    ss->Default1D = _mesa_alloc_texture_object(ss, 0, GL_TEXTURE_1D);
  652.    if (!ss->Default1D) {
  653.       outOfMemory = GL_TRUE;
  654.    }
  655.  
  656.    ss->Default2D = _mesa_alloc_texture_object(ss, 0, GL_TEXTURE_2D);
  657.    if (!ss->Default2D) {
  658.       outOfMemory = GL_TRUE;
  659.    }
  660.  
  661.    ss->Default3D = _mesa_alloc_texture_object(ss, 0, GL_TEXTURE_3D);
  662.    if (!ss->Default3D) {
  663.       outOfMemory = GL_TRUE;
  664.    }
  665.  
  666.    ss->DefaultCubeMap = _mesa_alloc_texture_object(ss, 0,
  667.                                                    GL_TEXTURE_CUBE_MAP_ARB);
  668.    if (!ss->DefaultCubeMap) {
  669.       outOfMemory = GL_TRUE;
  670.    }
  671.  
  672.    ss->DefaultRect = _mesa_alloc_texture_object(ss, 0,
  673.                                                 GL_TEXTURE_RECTANGLE_NV);
  674.    if (!ss->DefaultRect) {
  675.       outOfMemory = GL_TRUE;
  676.    }
  677.  
  678.    if (!ss->DisplayList || !ss->TexObjects
  679. #if FEATURE_NV_vertex_program
  680.        || !ss->VertexPrograms
  681. #endif
  682.        || outOfMemory) {
  683.       /* Ran out of memory at some point.  Free everything and return NULL */
  684.       if (ss->DisplayList)
  685.          _mesa_DeleteHashTable(ss->DisplayList);
  686.       if (ss->TexObjects)
  687.          _mesa_DeleteHashTable(ss->TexObjects);
  688.       if (ss->VertexPrograms)
  689.          _mesa_DeleteHashTable(ss->VertexPrograms);
  690.       if (ss->Default1D)
  691.          _mesa_free_texture_object(ss, ss->Default1D);
  692.       if (ss->Default2D)
  693.          _mesa_free_texture_object(ss, ss->Default2D);
  694.       if (ss->Default3D)
  695.          _mesa_free_texture_object(ss, ss->Default3D);
  696.       if (ss->DefaultCubeMap)
  697.          _mesa_free_texture_object(ss, ss->DefaultCubeMap);
  698.       if (ss->DefaultRect)
  699.          _mesa_free_texture_object(ss, ss->DefaultRect);
  700.       FREE(ss);
  701.       return NULL;
  702.    }
  703.    else {
  704.       return ss;
  705.    }
  706. }
  707.  
  708.  
  709. /*
  710.  * Deallocate a shared state context and all children structures.
  711.  */
  712. static void
  713. free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
  714. {
  715.    /* Free display lists */
  716.    while (1) {
  717.       GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
  718.       if (list) {
  719.          _mesa_destroy_list(ctx, list);
  720.       }
  721.       else {
  722.          break;
  723.       }
  724.    }
  725.    _mesa_DeleteHashTable(ss->DisplayList);
  726.  
  727.    /* Free texture objects */
  728.    while (ss->TexObjectList) {
  729.       if (ctx->Driver.DeleteTexture)
  730.          (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
  731.       /* this function removes from linked list too! */
  732.       _mesa_free_texture_object(ss, ss->TexObjectList);
  733.    }
  734.    _mesa_DeleteHashTable(ss->TexObjects);
  735.  
  736. #if FEATURE_NV_vertex_program
  737.    /* Free vertex programs */
  738.    while (1) {
  739.       GLuint prog = _mesa_HashFirstEntry(ss->VertexPrograms);
  740.       if (prog) {
  741.          _mesa_delete_program(ctx, prog);
  742.       }
  743.       else {
  744.          break;
  745.       }
  746.    }
  747.    _mesa_DeleteHashTable(ss->VertexPrograms);
  748. #endif
  749.  
  750.    FREE(ss);
  751. }
  752.  
  753.  
  754.  
  755. /*
  756.  * Initialize the nth light.  Note that the defaults for light 0 are
  757.  * different than the other lights.
  758.  */
  759. static void
  760. init_light( struct gl_light *l, GLuint n )
  761. {
  762.    make_empty_list( l );
  763.  
  764.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  765.    if (n==0) {
  766.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  767.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  768.    }
  769.    else {
  770.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  771.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  772.    }
  773.    ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
  774.    ASSIGN_3V( l->EyeDirection, 0.0, 0.0, -1.0 );
  775.    l->SpotExponent = 0.0;
  776.    _mesa_invalidate_spot_exp_table( l );
  777.    l->SpotCutoff = 180.0;
  778.    l->_CosCutoff = 0.0;        /* KW: -ve values not admitted */
  779.    l->ConstantAttenuation = 1.0;
  780.    l->LinearAttenuation = 0.0;
  781.    l->QuadraticAttenuation = 0.0;
  782.    l->Enabled = GL_FALSE;
  783. }
  784.  
  785.  
  786.  
  787. static void
  788. init_lightmodel( struct gl_lightmodel *lm )
  789. {
  790.    ASSIGN_4V( lm->Ambient, 0.2F, 0.2F, 0.2F, 1.0F );
  791.    lm->LocalViewer = GL_FALSE;
  792.    lm->TwoSide = GL_FALSE;
  793.    lm->ColorControl = GL_SINGLE_COLOR;
  794. }
  795.  
  796.  
  797. static void
  798. init_material( struct gl_material *m )
  799. {
  800.    ASSIGN_4V( m->Ambient,  0.2F, 0.2F, 0.2F, 1.0F );
  801.    ASSIGN_4V( m->Diffuse,  0.8F, 0.8F, 0.8F, 1.0F );
  802.    ASSIGN_4V( m->Specular, 0.0F, 0.0F, 0.0F, 1.0F );
  803.    ASSIGN_4V( m->Emission, 0.0F, 0.0F, 0.0F, 1.0F );
  804.    m->Shininess = 0.0;
  805.    m->AmbientIndex = 0;
  806.    m->DiffuseIndex = 1;
  807.    m->SpecularIndex = 1;
  808. }
  809.  
  810.  
  811.  
  812. static void
  813. init_texture_unit( GLcontext *ctx, GLuint unit )
  814. {
  815.    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
  816.  
  817.    texUnit->EnvMode = GL_MODULATE;
  818.    texUnit->CombineModeRGB = GL_MODULATE;
  819.    texUnit->CombineModeA = GL_MODULATE;
  820.    texUnit->CombineSourceRGB[0] = GL_TEXTURE;
  821.    texUnit->CombineSourceRGB[1] = GL_PREVIOUS_EXT;
  822.    texUnit->CombineSourceRGB[2] = GL_CONSTANT_EXT;
  823.    texUnit->CombineSourceA[0] = GL_TEXTURE;
  824.    texUnit->CombineSourceA[1] = GL_PREVIOUS_EXT;
  825.    texUnit->CombineSourceA[2] = GL_CONSTANT_EXT;
  826.    texUnit->CombineOperandRGB[0] = GL_SRC_COLOR;
  827.    texUnit->CombineOperandRGB[1] = GL_SRC_COLOR;
  828.    texUnit->CombineOperandRGB[2] = GL_SRC_ALPHA;
  829.    texUnit->CombineOperandA[0] = GL_SRC_ALPHA;
  830.    texUnit->CombineOperandA[1] = GL_SRC_ALPHA;
  831.    texUnit->CombineOperandA[2] = GL_SRC_ALPHA;
  832.    texUnit->CombineScaleShiftRGB = 0;
  833.    texUnit->CombineScaleShiftA = 0;
  834.  
  835.    ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
  836.    texUnit->TexGenEnabled = 0;
  837.    texUnit->GenModeS = GL_EYE_LINEAR;
  838.    texUnit->GenModeT = GL_EYE_LINEAR;
  839.    texUnit->GenModeR = GL_EYE_LINEAR;
  840.    texUnit->GenModeQ = GL_EYE_LINEAR;
  841.    texUnit->_GenBitS = TEXGEN_EYE_LINEAR;
  842.    texUnit->_GenBitT = TEXGEN_EYE_LINEAR;
  843.    texUnit->_GenBitR = TEXGEN_EYE_LINEAR;
  844.    texUnit->_GenBitQ = TEXGEN_EYE_LINEAR;
  845.  
  846.    /* Yes, these plane coefficients are correct! */
  847.    ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  848.    ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  849.    ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  850.    ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  851.    ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  852.    ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  853.    ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  854.    ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  855.  
  856.    texUnit->Current1D = ctx->Shared->Default1D;
  857.    texUnit->Current2D = ctx->Shared->Default2D;
  858.    texUnit->Current3D = ctx->Shared->Default3D;
  859.    texUnit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
  860.    texUnit->CurrentRect = ctx->Shared->DefaultRect;
  861. }
  862.  
  863.  
  864.  
  865.  
  866. /* Initialize a 1-D evaluator map */
  867. static void
  868. init_1d_map( struct gl_1d_map *map, int n, const float *initial )
  869. {
  870.    map->Order = 1;
  871.    map->u1 = 0.0;
  872.    map->u2 = 1.0;
  873.    map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
  874.    if (map->Points) {
  875.       GLint i;
  876.       for (i=0;i<n;i++)
  877.          map->Points[i] = initial[i];
  878.    }
  879. }
  880.  
  881.  
  882. /* Initialize a 2-D evaluator map */
  883. static void
  884. init_2d_map( struct gl_2d_map *map, int n, const float *initial )
  885. {
  886.    map->Uorder = 1;
  887.    map->Vorder = 1;
  888.    map->u1 = 0.0;
  889.    map->u2 = 1.0;
  890.    map->v1 = 0.0;
  891.    map->v2 = 1.0;
  892.    map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
  893.    if (map->Points) {
  894.       GLint i;
  895.       for (i=0;i<n;i++)
  896.          map->Points[i] = initial[i];
  897.    }
  898. }
  899.  
  900.  
  901. /*
  902.  * Initialize the attribute groups in a GLcontext.
  903.  */
  904. static void
  905. init_attrib_groups( GLcontext *ctx )
  906. {
  907.    GLuint i;
  908.  
  909.    assert(ctx);
  910.  
  911.    assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
  912.    assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
  913.  
  914.    /* Constants, may be overriden by device drivers */
  915.    ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
  916.    ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
  917.    ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
  918.    ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
  919.    ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
  920.    ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
  921.    ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
  922.    ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
  923.    ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
  924.    ctx->Const.MinPointSize = MIN_POINT_SIZE;
  925.    ctx->Const.MaxPointSize = MAX_POINT_SIZE;
  926.    ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
  927.    ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
  928.    ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
  929.    ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
  930.    ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
  931.    ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
  932.    ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
  933.    ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
  934.    ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
  935.    ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
  936.    ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
  937.    ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
  938.    ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
  939.    ctx->Const.MaxLights = MAX_LIGHTS;
  940.  
  941.    /* Initialize matrix stacks */
  942.    init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
  943.                      _NEW_MODELVIEW);
  944.    init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
  945.                      _NEW_PROJECTION);
  946.    init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
  947.                      _NEW_COLOR_MATRIX);
  948.    for (i = 0; i < MAX_TEXTURE_UNITS; i++)
  949.       init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
  950.                         _NEW_TEXTURE_MATRIX);
  951.    for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
  952.       init_matrix_stack(&ctx->ProgramMatrixStack[i], MAX_PROGRAM_STACK_DEPTH,
  953.                         _NEW_TRACK_MATRIX);
  954.    ctx->CurrentStack = &ctx->ModelviewMatrixStack;
  955.  
  956.    /* Init combined Modelview*Projection matrix */
  957.    _math_matrix_ctr( &ctx->_ModelProjectMatrix );
  958.  
  959.    /* Accumulate buffer group */
  960.    ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  961.  
  962.    /* Color buffer group */
  963.    ctx->Color.IndexMask = 0xffffffff;
  964.    ctx->Color.ColorMask[0] = 0xff;
  965.    ctx->Color.ColorMask[1] = 0xff;
  966.    ctx->Color.ColorMask[2] = 0xff;
  967.    ctx->Color.ColorMask[3] = 0xff;
  968.    ctx->Color.ClearIndex = 0;
  969.    ASSIGN_4V( ctx->Color.ClearColor, 0, 0, 0, 0 );
  970.    ctx->Color.DrawBuffer = GL_FRONT;
  971.    ctx->Color.AlphaEnabled = GL_FALSE;
  972.    ctx->Color.AlphaFunc = GL_ALWAYS;
  973.    ctx->Color.AlphaRef = 0;
  974.    ctx->Color.BlendEnabled = GL_FALSE;
  975.    ctx->Color.BlendSrcRGB = GL_ONE;
  976.    ctx->Color.BlendDstRGB = GL_ZERO;
  977.    ctx->Color.BlendSrcA = GL_ONE;
  978.    ctx->Color.BlendDstA = GL_ZERO;
  979.    ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
  980.    ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  981.    ctx->Color.IndexLogicOpEnabled = GL_FALSE;
  982.    ctx->Color.ColorLogicOpEnabled = GL_FALSE;
  983.    ctx->Color.LogicOp = GL_COPY;
  984.    ctx->Color.DitherFlag = GL_TRUE;
  985.  
  986.    /* Current group */
  987.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 0.0, 0.0, 0.0, 0.0 );
  988.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 0.0 );
  989.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
  990.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 0.0 );
  991.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
  992.    for (i = 0; i < MAX_TEXTURE_UNITS; i++)
  993.       ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i], 0.0, 0.0, 0.0, 1.0 );
  994.    ctx->Current.Index = 1;
  995.    ctx->Current.EdgeFlag = GL_TRUE;
  996.    
  997.    ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  998.    ctx->Current.RasterDistance = 0.0;
  999.    ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  1000.    ctx->Current.RasterIndex = 1;
  1001.    for (i=0; i<MAX_TEXTURE_UNITS; i++)
  1002.       ASSIGN_4V( ctx->Current.RasterTexCoords[i], 0.0, 0.0, 0.0, 1.0 );
  1003.    ctx->Current.RasterPosValid = GL_TRUE;
  1004.  
  1005.  
  1006.    /* Depth buffer group */
  1007.    ctx->Depth.Test = GL_FALSE;
  1008.    ctx->Depth.Clear = 1.0;
  1009.    ctx->Depth.Func = GL_LESS;
  1010.    ctx->Depth.Mask = GL_TRUE;
  1011.    ctx->Depth.OcclusionTest = GL_FALSE;
  1012.  
  1013.    /* Evaluators group */
  1014.    ctx->Eval.Map1Color4 = GL_FALSE;
  1015.    ctx->Eval.Map1Index = GL_FALSE;
  1016.    ctx->Eval.Map1Normal = GL_FALSE;
  1017.    ctx->Eval.Map1TextureCoord1 = GL_FALSE;
  1018.    ctx->Eval.Map1TextureCoord2 = GL_FALSE;
  1019.    ctx->Eval.Map1TextureCoord3 = GL_FALSE;
  1020.    ctx->Eval.Map1TextureCoord4 = GL_FALSE;
  1021.    ctx->Eval.Map1Vertex3 = GL_FALSE;
  1022.    ctx->Eval.Map1Vertex4 = GL_FALSE;
  1023.    MEMSET(ctx->Eval.Map1Attrib, 0, sizeof(ctx->Eval.Map1Attrib));
  1024.    ctx->Eval.Map2Color4 = GL_FALSE;
  1025.    ctx->Eval.Map2Index = GL_FALSE;
  1026.    ctx->Eval.Map2Normal = GL_FALSE;
  1027.    ctx->Eval.Map2TextureCoord1 = GL_FALSE;
  1028.    ctx->Eval.Map2TextureCoord2 = GL_FALSE;
  1029.    ctx->Eval.Map2TextureCoord3 = GL_FALSE;
  1030.    ctx->Eval.Map2TextureCoord4 = GL_FALSE;
  1031.    ctx->Eval.Map2Vertex3 = GL_FALSE;
  1032.    ctx->Eval.Map2Vertex4 = GL_FALSE;
  1033.    MEMSET(ctx->Eval.Map2Attrib, 0, sizeof(ctx->Eval.Map2Attrib));
  1034.    ctx->Eval.AutoNormal = GL_FALSE;
  1035.    ctx->Eval.MapGrid1un = 1;
  1036.    ctx->Eval.MapGrid1u1 = 0.0;
  1037.    ctx->Eval.MapGrid1u2 = 1.0;
  1038.    ctx->Eval.MapGrid2un = 1;
  1039.    ctx->Eval.MapGrid2vn = 1;
  1040.    ctx->Eval.MapGrid2u1 = 0.0;
  1041.    ctx->Eval.MapGrid2u2 = 1.0;
  1042.    ctx->Eval.MapGrid2v1 = 0.0;
  1043.    ctx->Eval.MapGrid2v2 = 1.0;
  1044.  
  1045.    /* Evaluator data */
  1046.    {
  1047.       static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
  1048.       static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
  1049.       static GLfloat index[1] = { 1.0 };
  1050.       static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
  1051.       static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
  1052.       static GLfloat attrib[4] = { 0.0, 0.0, 0.0, 1.0 };
  1053.  
  1054.       init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
  1055.       init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
  1056.       init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
  1057.       init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
  1058.       init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
  1059.       init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
  1060.       init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
  1061.       init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
  1062.       init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
  1063.       for (i = 0; i < 16; i++)
  1064.          init_1d_map( ctx->EvalMap.Map1Attrib + i, 4, attrib );
  1065.  
  1066.       init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
  1067.       init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
  1068.       init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
  1069.       init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
  1070.       init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
  1071.       init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
  1072.       init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
  1073.       init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
  1074.       init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
  1075.       for (i = 0; i < 16; i++)
  1076.          init_2d_map( ctx->EvalMap.Map2Attrib + i, 4, attrib );
  1077.    }
  1078.  
  1079.    /* Fog group */
  1080.    ctx->Fog.Enabled = GL_FALSE;
  1081.    ctx->Fog.Mode = GL_EXP;
  1082.    ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  1083.    ctx->Fog.Index = 0.0;
  1084.    ctx->Fog.Density = 1.0;
  1085.    ctx->Fog.Start = 0.0;
  1086.    ctx->Fog.End = 1.0;
  1087.    ctx->Fog.ColorSumEnabled = GL_FALSE;
  1088.    ctx->Fog.FogCoordinateSource = GL_FRAGMENT_DEPTH_EXT;
  1089.  
  1090.    /* Hint group */
  1091.    ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
  1092.    ctx->Hint.PointSmooth = GL_DONT_CARE;
  1093.    ctx->Hint.LineSmooth = GL_DONT_CARE;
  1094.    ctx->Hint.PolygonSmooth = GL_DONT_CARE;
  1095.    ctx->Hint.Fog = GL_DONT_CARE;
  1096.    ctx->Hint.ClipVolumeClipping = GL_DONT_CARE;
  1097.    ctx->Hint.TextureCompression = GL_DONT_CARE;
  1098.    ctx->Hint.GenerateMipmap = GL_DONT_CARE;
  1099.  
  1100.    /* Histogram group */
  1101.    ctx->Histogram.Width = 0;
  1102.    ctx->Histogram.Format = GL_RGBA;
  1103.    ctx->Histogram.Sink = GL_FALSE;
  1104.    ctx->Histogram.RedSize       = 0;
  1105.    ctx->Histogram.GreenSize     = 0;
  1106.    ctx->Histogram.BlueSize      = 0;
  1107.    ctx->Histogram.AlphaSize     = 0;
  1108.    ctx->Histogram.LuminanceSize = 0;
  1109.    for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
  1110.       ctx->Histogram.Count[i][0] = 0;
  1111.       ctx->Histogram.Count[i][1] = 0;
  1112.       ctx->Histogram.Count[i][2] = 0;
  1113.       ctx->Histogram.Count[i][3] = 0;
  1114.    }
  1115.  
  1116.    /* Min/Max group */
  1117.    ctx->MinMax.Format = GL_RGBA;
  1118.    ctx->MinMax.Sink = GL_FALSE;
  1119.    ctx->MinMax.Min[RCOMP] = 1000;    ctx->MinMax.Max[RCOMP] = -1000;
  1120.    ctx->MinMax.Min[GCOMP] = 1000;    ctx->MinMax.Max[GCOMP] = -1000;
  1121.    ctx->MinMax.Min[BCOMP] = 1000;    ctx->MinMax.Max[BCOMP] = -1000;
  1122.    ctx->MinMax.Min[ACOMP] = 1000;    ctx->MinMax.Max[ACOMP] = -1000;
  1123.  
  1124.    /* Extensions */
  1125.    _mesa_extensions_ctr( ctx );
  1126.  
  1127.    /* Lighting group */
  1128.    for (i=0;i<MAX_LIGHTS;i++) {
  1129.       init_light( &ctx->Light.Light[i], i );
  1130.    }
  1131.    make_empty_list( &ctx->Light.EnabledList );
  1132.  
  1133.    init_lightmodel( &ctx->Light.Model );
  1134.    init_material( &ctx->Light.Material[0] );
  1135.    init_material( &ctx->Light.Material[1] );
  1136.    ctx->Light.ShadeModel = GL_SMOOTH;
  1137.    ctx->Light.Enabled = GL_FALSE;
  1138.    ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  1139.    ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  1140.    ctx->Light.ColorMaterialBitmask = _mesa_material_bitmask( ctx,
  1141.                                                GL_FRONT_AND_BACK,
  1142.                                                GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
  1143.  
  1144.    ctx->Light.ColorMaterialEnabled = GL_FALSE;
  1145.  
  1146.    /* Lighting miscellaneous */
  1147.    ctx->_ShineTabList = MALLOC_STRUCT( gl_shine_tab );
  1148.    make_empty_list( ctx->_ShineTabList );
  1149.    for (i = 0 ; i < 10 ; i++) {
  1150.       struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
  1151.       s->shininess = -1;
  1152.       s->refcount = 0;
  1153.       insert_at_tail( ctx->_ShineTabList, s );
  1154.    }
  1155.  
  1156.  
  1157.    /* Line group */
  1158.    ctx->Line.SmoothFlag = GL_FALSE;
  1159.    ctx->Line.StippleFlag = GL_FALSE;
  1160.    ctx->Line.Width = 1.0;
  1161.    ctx->Line._Width = 1.0;
  1162.    ctx->Line.StipplePattern = 0xffff;
  1163.    ctx->Line.StippleFactor = 1;
  1164.  
  1165.    /* Display List group */
  1166.    ctx->List.ListBase = 0;
  1167.  
  1168.    /* Multisample */
  1169.    ctx->Multisample.Enabled = GL_FALSE;
  1170.    ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
  1171.    ctx->Multisample.SampleAlphaToOne = GL_FALSE;
  1172.    ctx->Multisample.SampleCoverage = GL_FALSE;
  1173.    ctx->Multisample.SampleCoverageValue = 1.0;
  1174.    ctx->Multisample.SampleCoverageInvert = GL_FALSE;
  1175.  
  1176.    /* Pixel group */
  1177.    ctx->Pixel.RedBias = 0.0;
  1178.    ctx->Pixel.RedScale = 1.0;
  1179.    ctx->Pixel.GreenBias = 0.0;
  1180.    ctx->Pixel.GreenScale = 1.0;
  1181.    ctx->Pixel.BlueBias = 0.0;
  1182.    ctx->Pixel.BlueScale = 1.0;
  1183.    ctx->Pixel.AlphaBias = 0.0;
  1184.    ctx->Pixel.AlphaScale = 1.0;
  1185.    ctx->Pixel.DepthBias = 0.0;
  1186.    ctx->Pixel.DepthScale = 1.0;
  1187.    ctx->Pixel.IndexOffset = 0;
  1188.    ctx->Pixel.IndexShift = 0;
  1189.    ctx->Pixel.ZoomX = 1.0;
  1190.    ctx->Pixel.ZoomY = 1.0;
  1191.    ctx->Pixel.MapColorFlag = GL_FALSE;
  1192.    ctx->Pixel.MapStencilFlag = GL_FALSE;
  1193.    ctx->Pixel.MapStoSsize = 1;
  1194.    ctx->Pixel.MapItoIsize = 1;
  1195.    ctx->Pixel.MapItoRsize = 1;
  1196.    ctx->Pixel.MapItoGsize = 1;
  1197.    ctx->Pixel.MapItoBsize = 1;
  1198.    ctx->Pixel.MapItoAsize = 1;
  1199.    ctx->Pixel.MapRtoRsize = 1;
  1200.    ctx->Pixel.MapGtoGsize = 1;
  1201.    ctx->Pixel.MapBtoBsize = 1;
  1202.    ctx->Pixel.MapAtoAsize = 1;
  1203.    ctx->Pixel.MapStoS[0] = 0;
  1204.    ctx->Pixel.MapItoI[0] = 0;
  1205.    ctx->Pixel.MapItoR[0] = 0.0;
  1206.    ctx->Pixel.MapItoG[0] = 0.0;
  1207.    ctx->Pixel.MapItoB[0] = 0.0;
  1208.    ctx->Pixel.MapItoA[0] = 0.0;
  1209.    ctx->Pixel.MapItoR8[0] = 0;
  1210.    ctx->Pixel.MapItoG8[0] = 0;
  1211.    ctx->Pixel.MapItoB8[0] = 0;
  1212.    ctx->Pixel.MapItoA8[0] = 0;
  1213.    ctx->Pixel.MapRtoR[0] = 0.0;
  1214.    ctx->Pixel.MapGtoG[0] = 0.0;
  1215.    ctx->Pixel.MapBtoB[0] = 0.0;
  1216.    ctx->Pixel.MapAtoA[0] = 0.0;
  1217.    ctx->Pixel.HistogramEnabled = GL_FALSE;
  1218.    ctx->Pixel.MinMaxEnabled = GL_FALSE;
  1219.    ctx->Pixel.PixelTextureEnabled = GL_FALSE;
  1220.    ctx->Pixel.FragmentRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
  1221.    ctx->Pixel.FragmentAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
  1222.    ASSIGN_4V(ctx->Pixel.PostColorMatrixScale, 1.0, 1.0, 1.0, 1.0);
  1223.    ASSIGN_4V(ctx->Pixel.PostColorMatrixBias, 0.0, 0.0, 0.0, 0.0);
  1224.    ASSIGN_4V(ctx->Pixel.ColorTableScale, 1.0, 1.0, 1.0, 1.0);
  1225.    ASSIGN_4V(ctx->Pixel.ColorTableBias, 0.0, 0.0, 0.0, 0.0);
  1226.    ASSIGN_4V(ctx->Pixel.PCCTscale, 1.0, 1.0, 1.0, 1.0);
  1227.    ASSIGN_4V(ctx->Pixel.PCCTbias, 0.0, 0.0, 0.0, 0.0);
  1228.    ASSIGN_4V(ctx->Pixel.PCMCTscale, 1.0, 1.0, 1.0, 1.0);
  1229.    ASSIGN_4V(ctx->Pixel.PCMCTbias, 0.0, 0.0, 0.0, 0.0);
  1230.    ctx->Pixel.ColorTableEnabled = GL_FALSE;
  1231.    ctx->Pixel.PostConvolutionColorTableEnabled = GL_FALSE;
  1232.    ctx->Pixel.PostColorMatrixColorTableEnabled = GL_FALSE;
  1233.    ctx->Pixel.Convolution1DEnabled = GL_FALSE;
  1234.    ctx->Pixel.Convolution2DEnabled = GL_FALSE;
  1235.    ctx->Pixel.Separable2DEnabled = GL_FALSE;
  1236.    for (i = 0; i < 3; i++) {
  1237.       ASSIGN_4V(ctx->Pixel.ConvolutionBorderColor[i], 0.0, 0.0, 0.0, 0.0);
  1238.       ctx->Pixel.ConvolutionBorderMode[i] = GL_REDUCE;
  1239.       ASSIGN_4V(ctx->Pixel.ConvolutionFilterScale[i], 1.0, 1.0, 1.0, 1.0);
  1240.       ASSIGN_4V(ctx->Pixel.ConvolutionFilterBias[i], 0.0, 0.0, 0.0, 0.0);
  1241.    }
  1242.    for (i = 0; i < MAX_CONVOLUTION_WIDTH * MAX_CONVOLUTION_WIDTH * 4; i++) {
  1243.       ctx->Convolution1D.Filter[i] = 0.0;
  1244.       ctx->Convolution2D.Filter[i] = 0.0;
  1245.       ctx->Separable2D.Filter[i] = 0.0;
  1246.    }
  1247.    ASSIGN_4V(ctx->Pixel.PostConvolutionScale, 1.0, 1.0, 1.0, 1.0);
  1248.    ASSIGN_4V(ctx->Pixel.PostConvolutionBias, 0.0, 0.0, 0.0, 0.0);
  1249.  
  1250.    /* Point group */
  1251.    ctx->Point.SmoothFlag = GL_FALSE;
  1252.    ctx->Point.Size = 1.0;
  1253.    ctx->Point._Size = 1.0;
  1254.    ctx->Point.Params[0] = 1.0;
  1255.    ctx->Point.Params[1] = 0.0;
  1256.    ctx->Point.Params[2] = 0.0;
  1257.    ctx->Point._Attenuated = GL_FALSE;
  1258.    ctx->Point.MinSize = 0.0;
  1259.    ctx->Point.MaxSize = ctx->Const.MaxPointSize;
  1260.    ctx->Point.Threshold = 1.0;
  1261.    ctx->Point.PointSprite = GL_FALSE; /* GL_NV_point_sprite */
  1262.    ctx->Point.SpriteRMode = GL_ZERO; /* GL_NV_point_sprite */
  1263.    for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
  1264.       ctx->Point.CoordReplace[i] = GL_FALSE; /* GL_NV_point_sprite */
  1265.    }
  1266.  
  1267.    /* Polygon group */
  1268.    ctx->Polygon.CullFlag = GL_FALSE;
  1269.    ctx->Polygon.CullFaceMode = GL_BACK;
  1270.    ctx->Polygon.FrontFace = GL_CCW;
  1271.    ctx->Polygon._FrontBit = 0;
  1272.    ctx->Polygon.FrontMode = GL_FILL;
  1273.    ctx->Polygon.BackMode = GL_FILL;
  1274.    ctx->Polygon.SmoothFlag = GL_FALSE;
  1275.    ctx->Polygon.StippleFlag = GL_FALSE;
  1276.    ctx->Polygon.OffsetFactor = 0.0F;
  1277.    ctx->Polygon.OffsetUnits = 0.0F;
  1278.    ctx->Polygon.OffsetPoint = GL_FALSE;
  1279.    ctx->Polygon.OffsetLine = GL_FALSE;
  1280.    ctx->Polygon.OffsetFill = GL_FALSE;
  1281.  
  1282.    /* Polygon Stipple group */
  1283.    MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  1284.  
  1285.    /* Scissor group */
  1286.    ctx->Scissor.Enabled = GL_FALSE;
  1287.    ctx->Scissor.X = 0;
  1288.    ctx->Scissor.Y = 0;
  1289.    ctx->Scissor.Width = 0;
  1290.    ctx->Scissor.Height = 0;
  1291.  
  1292.    /* Stencil group */
  1293.    ctx->Stencil.Enabled = GL_FALSE;
  1294.    ctx->Stencil.TestTwoSide = GL_FALSE;
  1295.    ctx->Stencil.ActiveFace = 0;  /* 0 = GL_FRONT, 1 = GL_BACK */
  1296.    ctx->Stencil.Function[0] = GL_ALWAYS;
  1297.    ctx->Stencil.Function[1] = GL_ALWAYS;
  1298.    ctx->Stencil.FailFunc[0] = GL_KEEP;
  1299.    ctx->Stencil.FailFunc[1] = GL_KEEP;
  1300.    ctx->Stencil.ZPassFunc[0] = GL_KEEP;
  1301.    ctx->Stencil.ZPassFunc[1] = GL_KEEP;
  1302.    ctx->Stencil.ZFailFunc[0] = GL_KEEP;
  1303.    ctx->Stencil.ZFailFunc[1] = GL_KEEP;
  1304.    ctx->Stencil.Ref[0] = 0;
  1305.    ctx->Stencil.Ref[1] = 0;
  1306.    ctx->Stencil.ValueMask[0] = STENCIL_MAX;
  1307.    ctx->Stencil.ValueMask[1] = STENCIL_MAX;
  1308.    ctx->Stencil.WriteMask[0] = STENCIL_MAX;
  1309.    ctx->Stencil.WriteMask[1] = STENCIL_MAX;
  1310.    ctx->Stencil.Clear = 0;
  1311.  
  1312.    /* Texture group */
  1313.    ctx->Texture.CurrentUnit = 0;      /* multitexture */
  1314.    ctx->Texture._EnabledUnits = 0;
  1315.    for (i=0; i<MAX_TEXTURE_UNITS; i++)
  1316.       init_texture_unit( ctx, i );
  1317.    ctx->Texture.SharedPalette = GL_FALSE;
  1318.    _mesa_init_colortable(&ctx->Texture.Palette);
  1319.  
  1320.    /* Transformation group */
  1321.    ctx->Transform.MatrixMode = GL_MODELVIEW;
  1322.    ctx->Transform.Normalize = GL_FALSE;
  1323.    ctx->Transform.RescaleNormals = GL_FALSE;
  1324.    ctx->Transform.RasterPositionUnclipped = GL_FALSE;
  1325.    for (i=0;i<MAX_CLIP_PLANES;i++) {
  1326.       ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
  1327.    }
  1328.    ctx->Transform.ClipPlanesEnabled = 0;
  1329.  
  1330.    /* Viewport group */
  1331.    ctx->Viewport.X = 0;
  1332.    ctx->Viewport.Y = 0;
  1333.    ctx->Viewport.Width = 0;
  1334.    ctx->Viewport.Height = 0;
  1335.    ctx->Viewport.Near = 0.0;
  1336.    ctx->Viewport.Far = 1.0;
  1337.    _math_matrix_ctr(&ctx->Viewport._WindowMap);
  1338.  
  1339. #define Sz 10
  1340. #define Tz 14
  1341.    ctx->Viewport._WindowMap.m[Sz] = 0.5F * ctx->DepthMaxF;
  1342.    ctx->Viewport._WindowMap.m[Tz] = 0.5F * ctx->DepthMaxF;
  1343. #undef Sz
  1344. #undef Tz
  1345.  
  1346.    ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
  1347.    ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
  1348.  
  1349.    /* Vertex arrays */
  1350.    ctx->Array.Vertex.Size = 4;
  1351.    ctx->Array.Vertex.Type = GL_FLOAT;
  1352.    ctx->Array.Vertex.Stride = 0;
  1353.    ctx->Array.Vertex.StrideB = 0;
  1354.    ctx->Array.Vertex.Ptr = NULL;
  1355.    ctx->Array.Vertex.Enabled = GL_FALSE;
  1356.    ctx->Array.Vertex.Flags = CA_CLIENT_DATA;
  1357.    ctx->Array.Normal.Type = GL_FLOAT;
  1358.    ctx->Array.Normal.Stride = 0;
  1359.    ctx->Array.Normal.StrideB = 0;
  1360.    ctx->Array.Normal.Ptr = NULL;
  1361.    ctx->Array.Normal.Enabled = GL_FALSE;
  1362.    ctx->Array.Normal.Flags = CA_CLIENT_DATA;
  1363.    ctx->Array.Color.Size = 4;
  1364.    ctx->Array.Color.Type = GL_FLOAT;
  1365.    ctx->Array.Color.Stride = 0;
  1366.    ctx->Array.Color.StrideB = 0;
  1367.    ctx->Array.Color.Ptr = NULL;
  1368.    ctx->Array.Color.Enabled = GL_FALSE;
  1369.    ctx->Array.Color.Flags = CA_CLIENT_DATA;
  1370.    ctx->Array.SecondaryColor.Size = 4;
  1371.    ctx->Array.SecondaryColor.Type = GL_FLOAT;
  1372.    ctx->Array.SecondaryColor.Stride = 0;
  1373.    ctx->Array.SecondaryColor.StrideB = 0;
  1374.    ctx->Array.SecondaryColor.Ptr = NULL;
  1375.    ctx->Array.SecondaryColor.Enabled = GL_FALSE;
  1376.    ctx->Array.SecondaryColor.Flags = CA_CLIENT_DATA;
  1377.    ctx->Array.FogCoord.Size = 1;
  1378.    ctx->Array.FogCoord.Type = GL_FLOAT;
  1379.    ctx->Array.FogCoord.Stride = 0;
  1380.    ctx->Array.FogCoord.StrideB = 0;
  1381.    ctx->Array.FogCoord.Ptr = NULL;
  1382.    ctx->Array.FogCoord.Enabled = GL_FALSE;
  1383.    ctx->Array.FogCoord.Flags = CA_CLIENT_DATA;
  1384.    ctx->Array.Index.Type = GL_FLOAT;
  1385.    ctx->Array.Index.Stride = 0;
  1386.    ctx->Array.Index.StrideB = 0;
  1387.    ctx->Array.Index.Ptr = NULL;
  1388.    ctx->Array.Index.Enabled = GL_FALSE;
  1389.    ctx->Array.Index.Flags = CA_CLIENT_DATA;
  1390.    for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
  1391.       ctx->Array.TexCoord[i].Size = 4;
  1392.       ctx->Array.TexCoord[i].Type = GL_FLOAT;
  1393.       ctx->Array.TexCoord[i].Stride = 0;
  1394.       ctx->Array.TexCoord[i].StrideB = 0;
  1395.       ctx->Array.TexCoord[i].Ptr = NULL;
  1396.       ctx->Array.TexCoord[i].Enabled = GL_FALSE;
  1397.       ctx->Array.TexCoord[i].Flags = CA_CLIENT_DATA;
  1398.    }
  1399.    ctx->Array.TexCoordInterleaveFactor = 1;
  1400.    ctx->Array.EdgeFlag.Stride = 0;
  1401.    ctx->Array.EdgeFlag.StrideB = 0;
  1402.    ctx->Array.EdgeFlag.Ptr = NULL;
  1403.    ctx->Array.EdgeFlag.Enabled = GL_FALSE;
  1404.    ctx->Array.EdgeFlag.Flags = CA_CLIENT_DATA;
  1405.    ctx->Array.ActiveTexture = 0;   /* GL_ARB_multitexture */
  1406.  
  1407.    /* Pixel transfer */
  1408.    ctx->Pack.Alignment = 4;
  1409.    ctx->Pack.RowLength = 0;
  1410.    ctx->Pack.ImageHeight = 0;
  1411.    ctx->Pack.SkipPixels = 0;
  1412.    ctx->Pack.SkipRows = 0;
  1413.    ctx->Pack.SkipImages = 0;
  1414.    ctx->Pack.SwapBytes = GL_FALSE;
  1415.    ctx->Pack.LsbFirst = GL_FALSE;
  1416.    ctx->Unpack.Alignment = 4;
  1417.    ctx->Unpack.RowLength = 0;
  1418.    ctx->Unpack.ImageHeight = 0;
  1419.    ctx->Unpack.SkipPixels = 0;
  1420.    ctx->Unpack.SkipRows = 0;
  1421.    ctx->Unpack.SkipImages = 0;
  1422.    ctx->Unpack.SwapBytes = GL_FALSE;
  1423.    ctx->Unpack.LsbFirst = GL_FALSE;
  1424.  
  1425.    /* Feedback */
  1426.    ctx->Feedback.Type = GL_2D;   /* TODO: verify */
  1427.    ctx->Feedback.Buffer = NULL;
  1428.    ctx->Feedback.BufferSize = 0;
  1429.    ctx->Feedback.Count = 0;
  1430.  
  1431.    /* Selection/picking */
  1432.    ctx->Select.Buffer = NULL;
  1433.    ctx->Select.BufferSize = 0;
  1434.    ctx->Select.BufferCount = 0;
  1435.    ctx->Select.Hits = 0;
  1436.    ctx->Select.NameStackDepth = 0;
  1437.  
  1438.    /* Renderer and client attribute stacks */
  1439.    ctx->AttribStackDepth = 0;
  1440.    ctx->ClientAttribStackDepth = 0;
  1441.  
  1442.    /* Display list */
  1443.    ctx->CallDepth = 0;
  1444.    ctx->ExecuteFlag = GL_TRUE;
  1445.    ctx->CompileFlag = GL_FALSE;
  1446.    ctx->CurrentListPtr = NULL;
  1447.    ctx->CurrentBlock = NULL;
  1448.    ctx->CurrentListNum = 0;
  1449.    ctx->CurrentPos = 0;
  1450.  
  1451.    /* Color tables */
  1452.    _mesa_init_colortable(&ctx->ColorTable);
  1453.    _mesa_init_colortable(&ctx->ProxyColorTable);
  1454.    _mesa_init_colortable(&ctx->PostConvolutionColorTable);
  1455.    _mesa_init_colortable(&ctx->ProxyPostConvolutionColorTable);
  1456.    _mesa_init_colortable(&ctx->PostColorMatrixColorTable);
  1457.    _mesa_init_colortable(&ctx->ProxyPostColorMatrixColorTable);
  1458.  
  1459.    /* GL_NV_vertex_program */
  1460.    ctx->VertexProgram.Enabled = GL_FALSE;
  1461.    ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
  1462.    ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
  1463.    ctx->VertexProgram.CurrentID = 0;
  1464.    ctx->VertexProgram.ErrorPos = -1;
  1465.    ctx->VertexProgram.Current = NULL;
  1466.    for (i = 0; i < VP_NUM_PROG_REGS / 4; i++) {
  1467.       ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
  1468.       ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
  1469.    }
  1470.  
  1471.    /* Miscellaneous */
  1472.    ctx->NewState = _NEW_ALL;
  1473.    ctx->RenderMode = GL_RENDER;
  1474.    ctx->_ImageTransferState = 0;
  1475.  
  1476.    ctx->_NeedNormals = 0;
  1477.    ctx->_NeedEyeCoords = 0;
  1478.    ctx->_ModelViewInvScale = 1.0;
  1479.  
  1480.    ctx->ErrorValue = (GLenum) GL_NO_ERROR;
  1481.  
  1482.    ctx->CatchSignals = GL_TRUE;
  1483.    ctx->OcclusionResult = GL_FALSE;
  1484.    ctx->OcclusionResultSaved = GL_FALSE;
  1485.    ctx->_Facing = 0;
  1486.  
  1487.    /* For debug/development only */
  1488.    ctx->NoRaster = _mesa_getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
  1489.    ctx->FirstTimeCurrent = GL_TRUE;
  1490.  
  1491.    /* Dither disable */
  1492.    ctx->NoDither = _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
  1493.    if (ctx->NoDither) {
  1494.       if (_mesa_getenv("MESA_DEBUG")) {
  1495.          _mesa_debug(ctx, "MESA_NO_DITHER set - dithering disabled\n");
  1496.       }
  1497.       ctx->Color.DitherFlag = GL_FALSE;
  1498.    }
  1499. }
  1500.  
  1501.  
  1502.  
  1503.  
  1504. /*
  1505.  * Allocate the proxy textures.  If we run out of memory part way through
  1506.  * the allocations clean up and return GL_FALSE.
  1507.  * Return:  GL_TRUE=success, GL_FALSE=failure
  1508.  */
  1509. static GLboolean
  1510. alloc_proxy_textures( GLcontext *ctx )
  1511. {
  1512.    GLboolean out_of_memory;
  1513.    GLint i;
  1514.  
  1515.    ctx->Texture.Proxy1D = _mesa_alloc_texture_object(NULL, 0, GL_TEXTURE_1D);
  1516.    if (!ctx->Texture.Proxy1D) {
  1517.       return GL_FALSE;
  1518.    }
  1519.  
  1520.    ctx->Texture.Proxy2D = _mesa_alloc_texture_object(NULL, 0, GL_TEXTURE_2D);
  1521.    if (!ctx->Texture.Proxy2D) {
  1522.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1523.       return GL_FALSE;
  1524.    }
  1525.  
  1526.    ctx->Texture.Proxy3D = _mesa_alloc_texture_object(NULL, 0, GL_TEXTURE_3D);
  1527.    if (!ctx->Texture.Proxy3D) {
  1528.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1529.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1530.       return GL_FALSE;
  1531.    }
  1532.  
  1533.    ctx->Texture.ProxyCubeMap = _mesa_alloc_texture_object(NULL, 0,
  1534.                                                      GL_TEXTURE_CUBE_MAP_ARB);
  1535.    if (!ctx->Texture.ProxyCubeMap) {
  1536.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1537.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1538.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy3D);
  1539.       return GL_FALSE;
  1540.    }
  1541.  
  1542.    ctx->Texture.ProxyRect = _mesa_alloc_texture_object(NULL, 0,
  1543.                                                       GL_TEXTURE_RECTANGLE_NV);
  1544.    if (!ctx->Texture.ProxyRect) {
  1545.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1546.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1547.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy3D);
  1548.       _mesa_free_texture_object(NULL, ctx->Texture.ProxyCubeMap);
  1549.       return GL_FALSE;
  1550.    }
  1551.  
  1552.    out_of_memory = GL_FALSE;
  1553.    for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1554.       ctx->Texture.Proxy1D->Image[i] = _mesa_alloc_texture_image();
  1555.       ctx->Texture.Proxy2D->Image[i] = _mesa_alloc_texture_image();
  1556.       ctx->Texture.Proxy3D->Image[i] = _mesa_alloc_texture_image();
  1557.       ctx->Texture.ProxyCubeMap->Image[i] = _mesa_alloc_texture_image();
  1558.       if (!ctx->Texture.Proxy1D->Image[i]
  1559.           || !ctx->Texture.Proxy2D->Image[i]
  1560.           || !ctx->Texture.Proxy3D->Image[i]
  1561.           || !ctx->Texture.ProxyCubeMap->Image[i]) {
  1562.          out_of_memory = GL_TRUE;
  1563.       }
  1564.    }
  1565.    ctx->Texture.ProxyRect->Image[0] = _mesa_alloc_texture_image();
  1566.    if (!ctx->Texture.ProxyRect->Image[0])
  1567.       out_of_memory = GL_TRUE;
  1568.  
  1569.    if (out_of_memory) {
  1570.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1571.          if (ctx->Texture.Proxy1D->Image[i]) {
  1572.             _mesa_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
  1573.          }
  1574.          if (ctx->Texture.Proxy2D->Image[i]) {
  1575.             _mesa_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
  1576.          }
  1577.          if (ctx->Texture.Proxy3D->Image[i]) {
  1578.             _mesa_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
  1579.          }
  1580.          if (ctx->Texture.ProxyCubeMap->Image[i]) {
  1581.             _mesa_free_texture_image(ctx->Texture.ProxyCubeMap->Image[i]);
  1582.          }
  1583.       }
  1584.       if (ctx->Texture.ProxyRect->Image[0]) {
  1585.          _mesa_free_texture_image(ctx->Texture.ProxyRect->Image[0]);
  1586.       }
  1587.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1588.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1589.       _mesa_free_texture_object(NULL, ctx->Texture.Proxy3D);
  1590.       _mesa_free_texture_object(NULL, ctx->Texture.ProxyCubeMap);
  1591.       _mesa_free_texture_object(NULL, ctx->Texture.ProxyRect);
  1592.       return GL_FALSE;
  1593.    }
  1594.    else {
  1595.       return GL_TRUE;
  1596.    }
  1597. }
  1598.  
  1599.  
  1600. static void add_debug_flags( const char *debug )
  1601. {
  1602. #ifdef MESA_DEBUG
  1603.    if (_mesa_strstr(debug, "varray")) 
  1604.       MESA_VERBOSE |= VERBOSE_VARRAY;
  1605.  
  1606.    if (_mesa_strstr(debug, "tex")) 
  1607.       MESA_VERBOSE |= VERBOSE_TEXTURE;
  1608.  
  1609.    if (_mesa_strstr(debug, "imm")) 
  1610.       MESA_VERBOSE |= VERBOSE_IMMEDIATE;
  1611.  
  1612.    if (_mesa_strstr(debug, "pipe")) 
  1613.       MESA_VERBOSE |= VERBOSE_PIPELINE;
  1614.  
  1615.    if (_mesa_strstr(debug, "driver")) 
  1616.       MESA_VERBOSE |= VERBOSE_DRIVER;
  1617.  
  1618.    if (_mesa_strstr(debug, "state")) 
  1619.       MESA_VERBOSE |= VERBOSE_STATE;
  1620.  
  1621.    if (_mesa_strstr(debug, "api")) 
  1622.       MESA_VERBOSE |= VERBOSE_API;
  1623.  
  1624.    if (_mesa_strstr(debug, "list")) 
  1625.       MESA_VERBOSE |= VERBOSE_DISPLAY_LIST;
  1626.  
  1627.    if (_mesa_strstr(debug, "lighting")) 
  1628.       MESA_VERBOSE |= VERBOSE_LIGHTING;
  1629.    
  1630.    /* Debug flag:
  1631.     */
  1632.    if (_mesa_strstr(debug, "flush")) 
  1633.       MESA_DEBUG_FLAGS |= DEBUG_ALWAYS_FLUSH;
  1634. #endif
  1635. }
  1636.  
  1637.  
  1638. /*
  1639.  * Initialize a GLcontext struct.  This includes allocating all the
  1640.  * other structs and arrays which hang off of the context by pointers.
  1641.  */
  1642. GLboolean
  1643. _mesa_initialize_context( GLcontext *ctx,
  1644.                           const GLvisual *visual,
  1645.                           GLcontext *share_list,
  1646.                           void *driver_ctx,
  1647.                           GLboolean direct )
  1648. {
  1649.    GLuint dispatchSize;
  1650.    const char *c;
  1651.  
  1652.    ASSERT(driver_ctx);
  1653.  
  1654.    /* If the driver wants core Mesa to use special imports, it'll have to
  1655.     * override these defaults.
  1656.     */
  1657.    _mesa_init_default_imports( &(ctx->imports), driver_ctx );
  1658.  
  1659.    /* initialize the exports (Mesa functions called by the window system) */
  1660.    _mesa_init_default_exports( &(ctx->exports) );
  1661.  
  1662.    /* misc one-time initializations */
  1663.    one_time_init(ctx);
  1664.  
  1665.    ctx->DriverCtx = driver_ctx;
  1666.    ctx->Visual = *visual;
  1667.    ctx->DrawBuffer = NULL;
  1668.    ctx->ReadBuffer = NULL;
  1669.  
  1670.    if (share_list) {
  1671.       /* share state with another context */
  1672.       ctx->Shared = share_list->Shared;
  1673.    }
  1674.    else {
  1675.       /* allocate new, unshared state */
  1676.       ctx->Shared = alloc_shared_state();
  1677.       if (!ctx->Shared) {
  1678.          return GL_FALSE;
  1679.       }
  1680.    }
  1681.    _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
  1682.    ctx->Shared->RefCount++;
  1683.    _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
  1684.  
  1685.    /* Effectively bind the default textures to all texture units */
  1686.    ctx->Shared->Default1D->RefCount += MAX_TEXTURE_UNITS;
  1687.    ctx->Shared->Default2D->RefCount += MAX_TEXTURE_UNITS;
  1688.    ctx->Shared->Default3D->RefCount += MAX_TEXTURE_UNITS;
  1689.    ctx->Shared->DefaultCubeMap->RefCount += MAX_TEXTURE_UNITS;
  1690.    ctx->Shared->DefaultRect->RefCount += MAX_TEXTURE_UNITS;
  1691.  
  1692.    init_attrib_groups( ctx );
  1693.  
  1694.    if (visual->doubleBufferMode) {
  1695.       ctx->Color.DrawBuffer = GL_BACK;
  1696.       ctx->Color._DrawDestMask = BACK_LEFT_BIT;
  1697.       ctx->Pixel.ReadBuffer = GL_BACK;
  1698.       ctx->Pixel._ReadSrcMask = BACK_LEFT_BIT;
  1699.    }
  1700.    else {
  1701.       ctx->Color.DrawBuffer = GL_FRONT;
  1702.       ctx->Color._DrawDestMask = FRONT_LEFT_BIT;
  1703.       ctx->Pixel.ReadBuffer = GL_FRONT;
  1704.       ctx->Pixel._ReadSrcMask = FRONT_LEFT_BIT;
  1705.    }
  1706.  
  1707.    if (!alloc_proxy_textures(ctx)) {
  1708.       free_shared_state(ctx, ctx->Shared);
  1709.       return GL_FALSE;
  1710.    }
  1711.  
  1712.    /*
  1713.     * For XFree86/DRI: tell libGL to add these functions to the dispatcher.
  1714.     * Basically, we should add all extension functions above offset 577.
  1715.     * This enables older libGL libraries to work with newer drivers that
  1716.     * have newer extensions.
  1717.     */
  1718.    /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
  1719.    _glapi_add_entrypoint("glWindowPos2dARB", 513);
  1720.    _glapi_add_entrypoint("glWindowPos2dvARB", 514);
  1721.    _glapi_add_entrypoint("glWindowPos2fARB", 515);
  1722.    _glapi_add_entrypoint("glWindowPos2fvARB", 516);
  1723.    _glapi_add_entrypoint("glWindowPos2iARB", 517);
  1724.    _glapi_add_entrypoint("glWindowPos2ivARB", 518);
  1725.    _glapi_add_entrypoint("glWindowPos2sARB", 519);
  1726.    _glapi_add_entrypoint("glWindowPos2svARB", 520);
  1727.    _glapi_add_entrypoint("glWindowPos3dARB", 521);
  1728.    _glapi_add_entrypoint("glWindowPos3dvARB", 522);
  1729.    _glapi_add_entrypoint("glWindowPos3fARB", 523);
  1730.    _glapi_add_entrypoint("glWindowPos3fvARB", 524);
  1731.    _glapi_add_entrypoint("glWindowPos3iARB", 525);
  1732.    _glapi_add_entrypoint("glWindowPos3ivARB", 526);
  1733.    _glapi_add_entrypoint("glWindowPos3sARB", 527);
  1734.    _glapi_add_entrypoint("glWindowPos3svARB", 528);
  1735.    /* new extension functions */
  1736.    _glapi_add_entrypoint("glAreProgramsResidentNV", 578);
  1737.    _glapi_add_entrypoint("glBindProgramNV", 579);
  1738.    _glapi_add_entrypoint("glDeleteProgramsNV", 580);
  1739.    _glapi_add_entrypoint("glExecuteProgramNV", 581);
  1740.    _glapi_add_entrypoint("glGenProgramsNV", 582);
  1741.    _glapi_add_entrypoint("glGetProgramParameterdvNV", 583);
  1742.    _glapi_add_entrypoint("glGetProgramParameterfvNV", 584);
  1743.    _glapi_add_entrypoint("glGetProgramivNV", 585);
  1744.    _glapi_add_entrypoint("glGetProgramStringNV", 586);
  1745.    _glapi_add_entrypoint("glGetTrackMatrixivNV", 587);
  1746.    _glapi_add_entrypoint("glGetVertexAttribdvNV", 588);
  1747.    _glapi_add_entrypoint("glGetVertexAttribfvNV", 589);
  1748.    _glapi_add_entrypoint("glGetVertexAttribivNV", 590);
  1749.    _glapi_add_entrypoint("glGetVertexAttribPointervNV", 591);
  1750.    _glapi_add_entrypoint("glIsProgramNV", 592);
  1751.    _glapi_add_entrypoint("glLoadProgramNV", 593);
  1752.    _glapi_add_entrypoint("glProgramParameter4dNV", 594);
  1753.    _glapi_add_entrypoint("glProgramParameter4dvNV", 595);
  1754.    _glapi_add_entrypoint("glProgramParameter4fNV", 596);
  1755.    _glapi_add_entrypoint("glProgramParameter4fvNV", 597);
  1756.    _glapi_add_entrypoint("glProgramParameters4dvNV", 598);
  1757.    _glapi_add_entrypoint("glProgramParameters4fvNV", 599);
  1758.    _glapi_add_entrypoint("glRequestResidentProgramsNV", 600);
  1759.    _glapi_add_entrypoint("glTrackMatrixNV", 601);
  1760.    _glapi_add_entrypoint("glVertexAttribPointerNV", 602);
  1761.    _glapi_add_entrypoint("glVertexAttrib1dNV", 603);
  1762.    _glapi_add_entrypoint("glVertexAttrib1dvNV", 604);
  1763.    _glapi_add_entrypoint("glVertexAttrib1fNV", 605);
  1764.    _glapi_add_entrypoint("glVertexAttrib1fvNV", 606);
  1765.    _glapi_add_entrypoint("glVertexAttrib1sNV", 607);
  1766.    _glapi_add_entrypoint("glVertexAttrib1svNV", 608);
  1767.    _glapi_add_entrypoint("glVertexAttrib2dNV", 609);
  1768.    _glapi_add_entrypoint("glVertexAttrib2dvNV", 610);
  1769.    _glapi_add_entrypoint("glVertexAttrib2fNV", 611);
  1770.    _glapi_add_entrypoint("glVertexAttrib2fvNV", 612);
  1771.    _glapi_add_entrypoint("glVertexAttrib2sNV", 613);
  1772.    _glapi_add_entrypoint("glVertexAttrib2svNV", 614);
  1773.    _glapi_add_entrypoint("glVertexAttrib3dNV", 615);
  1774.    _glapi_add_entrypoint("glVertexAttrib3dvNV", 616);
  1775.    _glapi_add_entrypoint("glVertexAttrib3fNV", 617);
  1776.    _glapi_add_entrypoint("glVertexAttrib3fvNV", 618);
  1777.    _glapi_add_entrypoint("glVertexAttrib3sNV", 619);
  1778.    _glapi_add_entrypoint("glVertexAttrib3svNV", 620);
  1779.    _glapi_add_entrypoint("glVertexAttrib4dNV", 621);
  1780.    _glapi_add_entrypoint("glVertexAttrib4dvNV", 622);
  1781.    _glapi_add_entrypoint("glVertexAttrib4fNV", 623);
  1782.    _glapi_add_entrypoint("glVertexAttrib4fvNV", 624);
  1783.    _glapi_add_entrypoint("glVertexAttrib4sNV", 625);
  1784.    _glapi_add_entrypoint("glVertexAttrib4svNV", 626);
  1785.    _glapi_add_entrypoint("glVertexAttrib4ubNV", 627);
  1786.    _glapi_add_entrypoint("glVertexAttrib4ubvNV", 628);
  1787.    _glapi_add_entrypoint("glVertexAttribs1dvNV", 629);
  1788.    _glapi_add_entrypoint("glVertexAttribs1fvNV", 630);
  1789.    _glapi_add_entrypoint("glVertexAttribs1svNV", 631);
  1790.    _glapi_add_entrypoint("glVertexAttribs2dvNV", 632);
  1791.    _glapi_add_entrypoint("glVertexAttribs2fvNV", 633);
  1792.    _glapi_add_entrypoint("glVertexAttribs2svNV", 634);
  1793.    _glapi_add_entrypoint("glVertexAttribs3dvNV", 635);
  1794.    _glapi_add_entrypoint("glVertexAttribs3fvNV", 636);
  1795.    _glapi_add_entrypoint("glVertexAttribs3svNV", 637);
  1796.    _glapi_add_entrypoint("glVertexAttribs4dvNV", 638);
  1797.    _glapi_add_entrypoint("glVertexAttribs4fvNV", 639);
  1798.    _glapi_add_entrypoint("glVertexAttribs4svNV", 640);
  1799.    _glapi_add_entrypoint("glVertexAttribs4ubvNV", 641);
  1800.    _glapi_add_entrypoint("glPointParameteriNV", 642);
  1801.    _glapi_add_entrypoint("glPointParameterivNV", 643);
  1802.    _glapi_add_entrypoint("glMultiDrawArraysEXT", 644);
  1803.    _glapi_add_entrypoint("glMultiDrawElementsEXT", 645);
  1804.    _glapi_add_entrypoint("glActiveStencilFaceEXT", 646);
  1805.    _glapi_add_entrypoint("glDeleteFencesNV", 647);
  1806.    _glapi_add_entrypoint("glGenFencesNV", 648);
  1807.    _glapi_add_entrypoint("glIsFenceNV", 649);
  1808.    _glapi_add_entrypoint("glTestFenceNV", 650);
  1809.    _glapi_add_entrypoint("glGetFenceivNV", 651);
  1810.    _glapi_add_entrypoint("glFinishFenceNV", 652);
  1811.    _glapi_add_entrypoint("glSetFenceNV", 653);
  1812.  
  1813.    /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
  1814.     * In practice, this'll be the same for stand-alone Mesa.  But for DRI
  1815.     * Mesa we do this to accomodate different versions of libGL and various
  1816.     * DRI drivers.
  1817.     */
  1818.    dispatchSize = MAX2(_glapi_get_dispatch_table_size(),
  1819.                        sizeof(struct _glapi_table) / sizeof(void *));
  1820.  
  1821.    /* setup API dispatch tables */
  1822.    ctx->Exec = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
  1823.    ctx->Save = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
  1824.    if (!ctx->Exec || !ctx->Save) {
  1825.       free_shared_state(ctx, ctx->Shared);
  1826.       if (ctx->Exec)
  1827.          FREE( ctx->Exec );
  1828.    }
  1829.    _mesa_init_exec_table(ctx->Exec, dispatchSize);
  1830.    _mesa_init_dlist_table(ctx->Save, dispatchSize);
  1831.    ctx->CurrentDispatch = ctx->Exec;
  1832.  
  1833.    ctx->ExecPrefersFloat = GL_FALSE;
  1834.    ctx->SavePrefersFloat = GL_FALSE;
  1835.  
  1836.    /* Neutral tnl module stuff */
  1837.    _mesa_init_exec_vtxfmt( ctx );
  1838.    ctx->TnlModule.Current = NULL;
  1839.    ctx->TnlModule.SwapCount = 0;
  1840.  
  1841.    /* Z buffer stuff */
  1842.    if (ctx->Visual.depthBits == 0) {
  1843.       /* Special case.  Even if we don't have a depth buffer we need
  1844.        * good values for DepthMax for Z vertex transformation purposes
  1845.        * and for per-fragment fog computation.
  1846.        */
  1847.       ctx->DepthMax = 1 << 16;
  1848.       ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
  1849.    }
  1850.    else if (ctx->Visual.depthBits < 32) {
  1851.       ctx->DepthMax = (1 << ctx->Visual.depthBits) - 1;
  1852.       ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
  1853.    }
  1854.    else {
  1855.       /* Special case since shift values greater than or equal to the
  1856.        * number of bits in the left hand expression's type are undefined.
  1857.        */
  1858.       ctx->DepthMax = 0xffffffff;
  1859.       ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
  1860.    }
  1861.    ctx->MRD = 1.0;  /* Minimum resolvable depth value, for polygon offset */
  1862.  
  1863.    c = _mesa_getenv("MESA_DEBUG");
  1864.    if (c)
  1865.       add_debug_flags(c);
  1866.  
  1867.    c = _mesa_getenv("MESA_VERBOSE");
  1868.    if (c)
  1869.       add_debug_flags(c);
  1870.  
  1871.    return GL_TRUE;
  1872. }
  1873.  
  1874.  
  1875.  
  1876. /*
  1877.  * Allocate and initialize a GLcontext structure.
  1878.  * Input:  visual - a GLvisual pointer (we copy the struct contents)
  1879.  *         sharelist - another context to share display lists with or NULL
  1880.  *         driver_ctx - pointer to device driver's context state struct
  1881.  *         direct - direct rendering?
  1882.  * Return:  pointer to a new __GLcontextRec or NULL if error.
  1883.  */
  1884. GLcontext *
  1885. _mesa_create_context( const GLvisual *visual,
  1886.                       GLcontext *share_list,
  1887.                       void *driver_ctx,
  1888.                       GLboolean direct )
  1889.  
  1890. {
  1891.    GLcontext *ctx;
  1892.  
  1893.    ASSERT(visual);
  1894.    ASSERT(driver_ctx);
  1895.  
  1896.    ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
  1897.    if (!ctx)
  1898.       return NULL;
  1899.  
  1900.    if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
  1901.       return ctx;
  1902.    }
  1903.    else {
  1904.       _mesa_free(ctx);
  1905.       return NULL;
  1906.    }
  1907. }
  1908.  
  1909.  
  1910.  
  1911. /*
  1912.  * Free the data associated with the given context.
  1913.  * But don't free() the GLcontext struct itself!
  1914.  */
  1915. void
  1916. _mesa_free_context_data( GLcontext *ctx )
  1917. {
  1918.    struct gl_shine_tab *s, *tmps;
  1919.    GLuint i;
  1920.  
  1921.    /* if we're destroying the current context, unbind it first */
  1922.    if (ctx == _mesa_get_current_context()) {
  1923.       _mesa_make_current(NULL, NULL);
  1924.    }
  1925.  
  1926.    /*
  1927.     * Free transformation matrix stacks
  1928.     */
  1929.    free_matrix_stack(&ctx->ModelviewMatrixStack);
  1930.    free_matrix_stack(&ctx->ProjectionMatrixStack);
  1931.    free_matrix_stack(&ctx->ColorMatrixStack);
  1932.    for (i = 0; i < MAX_TEXTURE_UNITS; i++)
  1933.       free_matrix_stack(&ctx->TextureMatrixStack[i]);
  1934.    for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
  1935.       free_matrix_stack(&ctx->ProgramMatrixStack[i]);
  1936.    /* combined Modelview*Projection matrix */
  1937.    _math_matrix_dtr( &ctx->_ModelProjectMatrix );
  1938.  
  1939.  
  1940. #if FEATURE_NV_vertex_program
  1941.    if (ctx->VertexProgram.Current) {
  1942.       ctx->VertexProgram.Current->RefCount--;
  1943.       if (ctx->VertexProgram.Current->RefCount <= 0)
  1944.          _mesa_delete_program(ctx, ctx->VertexProgram.CurrentID);
  1945.    }
  1946. #endif
  1947.  
  1948.    /* Shared context state (display lists, textures, etc) */
  1949.    _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
  1950.    ctx->Shared->RefCount--;
  1951.    assert(ctx->Shared->RefCount >= 0);
  1952.    _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
  1953.    if (ctx->Shared->RefCount == 0) {
  1954.       /* free shared state */
  1955.       free_shared_state( ctx, ctx->Shared );
  1956.    }
  1957.  
  1958.    /* Free lighting shininess exponentiation table */
  1959.    foreach_s( s, tmps, ctx->_ShineTabList ) {
  1960.       FREE( s );
  1961.    }
  1962.    FREE( ctx->_ShineTabList );
  1963.  
  1964.    /* Free proxy texture objects */
  1965.    _mesa_free_texture_object( NULL, ctx->Texture.Proxy1D );
  1966.    _mesa_free_texture_object( NULL, ctx->Texture.Proxy2D );
  1967.    _mesa_free_texture_object( NULL, ctx->Texture.Proxy3D );
  1968.    _mesa_free_texture_object( NULL, ctx->Texture.ProxyCubeMap );
  1969.    _mesa_free_texture_object( NULL, ctx->Texture.ProxyRect );
  1970.  
  1971.    /* Free evaluator data */
  1972.    if (ctx->EvalMap.Map1Vertex3.Points)
  1973.       FREE( ctx->EvalMap.Map1Vertex3.Points );
  1974.    if (ctx->EvalMap.Map1Vertex4.Points)
  1975.       FREE( ctx->EvalMap.Map1Vertex4.Points );
  1976.    if (ctx->EvalMap.Map1Index.Points)
  1977.       FREE( ctx->EvalMap.Map1Index.Points );
  1978.    if (ctx->EvalMap.Map1Color4.Points)
  1979.       FREE( ctx->EvalMap.Map1Color4.Points );
  1980.    if (ctx->EvalMap.Map1Normal.Points)
  1981.       FREE( ctx->EvalMap.Map1Normal.Points );
  1982.    if (ctx->EvalMap.Map1Texture1.Points)
  1983.       FREE( ctx->EvalMap.Map1Texture1.Points );
  1984.    if (ctx->EvalMap.Map1Texture2.Points)
  1985.       FREE( ctx->EvalMap.Map1Texture2.Points );
  1986.    if (ctx->EvalMap.Map1Texture3.Points)
  1987.       FREE( ctx->EvalMap.Map1Texture3.Points );
  1988.    if (ctx->EvalMap.Map1Texture4.Points)
  1989.       FREE( ctx->EvalMap.Map1Texture4.Points );
  1990.    for (i = 0; i < 16; i++)
  1991.       FREE((ctx->EvalMap.Map1Attrib[i].Points));
  1992.  
  1993.    if (ctx->EvalMap.Map2Vertex3.Points)
  1994.       FREE( ctx->EvalMap.Map2Vertex3.Points );
  1995.    if (ctx->EvalMap.Map2Vertex4.Points)
  1996.       FREE( ctx->EvalMap.Map2Vertex4.Points );
  1997.    if (ctx->EvalMap.Map2Index.Points)
  1998.       FREE( ctx->EvalMap.Map2Index.Points );
  1999.    if (ctx->EvalMap.Map2Color4.Points)
  2000.       FREE( ctx->EvalMap.Map2Color4.Points );
  2001.    if (ctx->EvalMap.Map2Normal.Points)
  2002.       FREE( ctx->EvalMap.Map2Normal.Points );
  2003.    if (ctx->EvalMap.Map2Texture1.Points)
  2004.       FREE( ctx->EvalMap.Map2Texture1.Points );
  2005.    if (ctx->EvalMap.Map2Texture2.Points)
  2006.       FREE( ctx->EvalMap.Map2Texture2.Points );
  2007.    if (ctx->EvalMap.Map2Texture3.Points)
  2008.       FREE( ctx->EvalMap.Map2Texture3.Points );
  2009.    if (ctx->EvalMap.Map2Texture4.Points)
  2010.       FREE( ctx->EvalMap.Map2Texture4.Points );
  2011.    for (i = 0; i < 16; i++)
  2012.       FREE((ctx->EvalMap.Map2Attrib[i].Points));
  2013.  
  2014.    _mesa_free_colortable_data( &ctx->ColorTable );
  2015.    _mesa_free_colortable_data( &ctx->PostConvolutionColorTable );
  2016.    _mesa_free_colortable_data( &ctx->PostColorMatrixColorTable );
  2017.    _mesa_free_colortable_data( &ctx->Texture.Palette );
  2018.  
  2019.    _math_matrix_dtr(&ctx->Viewport._WindowMap);
  2020.  
  2021.    _mesa_extensions_dtr(ctx);
  2022.  
  2023.    FREE(ctx->Exec);
  2024.    FREE(ctx->Save);
  2025. }
  2026.  
  2027.  
  2028.  
  2029. /*
  2030.  * Destroy a GLcontext structure.
  2031.  */
  2032. void
  2033. _mesa_destroy_context( GLcontext *ctx )
  2034. {
  2035.    if (ctx) {
  2036.       _mesa_free_context_data(ctx);
  2037.       FREE( (void *) ctx );
  2038.    }
  2039. }
  2040.  
  2041.  
  2042.  
  2043. /*
  2044.  * Copy attribute groups from one context to another.
  2045.  * Input:  src - source context
  2046.  *         dst - destination context
  2047.  *         mask - bitwise OR of GL_*_BIT flags
  2048.  */
  2049. void
  2050. _mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
  2051. {
  2052.    if (mask & GL_ACCUM_BUFFER_BIT) {
  2053.       /* OK to memcpy */
  2054.       dst->Accum = src->Accum;
  2055.    }
  2056.    if (mask & GL_COLOR_BUFFER_BIT) {
  2057.       /* OK to memcpy */
  2058.       dst->Color = src->Color;
  2059.    }
  2060.    if (mask & GL_CURRENT_BIT) {
  2061.       /* OK to memcpy */
  2062.       dst->Current = src->Current;
  2063.    }
  2064.    if (mask & GL_DEPTH_BUFFER_BIT) {
  2065.       /* OK to memcpy */
  2066.       dst->Depth = src->Depth;
  2067.    }
  2068.    if (mask & GL_ENABLE_BIT) {
  2069.       /* no op */
  2070.    }
  2071.    if (mask & GL_EVAL_BIT) {
  2072.       /* OK to memcpy */
  2073.       dst->Eval = src->Eval;
  2074.    }
  2075.    if (mask & GL_FOG_BIT) {
  2076.       /* OK to memcpy */
  2077.       dst->Fog = src->Fog;
  2078.    }
  2079.    if (mask & GL_HINT_BIT) {
  2080.       /* OK to memcpy */
  2081.       dst->Hint = src->Hint;
  2082.    }
  2083.    if (mask & GL_LIGHTING_BIT) {
  2084.       GLuint i;
  2085.       /* begin with memcpy */
  2086.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
  2087.       /* fixup linked lists to prevent pointer insanity */
  2088.       make_empty_list( &(dst->Light.EnabledList) );
  2089.       for (i = 0; i < MAX_LIGHTS; i++) {
  2090.          if (dst->Light.Light[i].Enabled) {
  2091.             insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
  2092.          }
  2093.       }
  2094.    }
  2095.    if (mask & GL_LINE_BIT) {
  2096.       /* OK to memcpy */
  2097.       dst->Line = src->Line;
  2098.    }
  2099.    if (mask & GL_LIST_BIT) {
  2100.       /* OK to memcpy */
  2101.       dst->List = src->List;
  2102.    }
  2103.    if (mask & GL_PIXEL_MODE_BIT) {
  2104.       /* OK to memcpy */
  2105.       dst->Pixel = src->Pixel;
  2106.    }
  2107.    if (mask & GL_POINT_BIT) {
  2108.       /* OK to memcpy */
  2109.       dst->Point = src->Point;
  2110.    }
  2111.    if (mask & GL_POLYGON_BIT) {
  2112.       /* OK to memcpy */
  2113.       dst->Polygon = src->Polygon;
  2114.    }
  2115.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  2116.       /* Use loop instead of MEMCPY due to problem with Portland Group's
  2117.        * C compiler.  Reported by John Stone.
  2118.        */
  2119.       GLuint i;
  2120.       for (i = 0; i < 32; i++) {
  2121.          dst->PolygonStipple[i] = src->PolygonStipple[i];
  2122.       }
  2123.    }
  2124.    if (mask & GL_SCISSOR_BIT) {
  2125.       /* OK to memcpy */
  2126.       dst->Scissor = src->Scissor;
  2127.    }
  2128.    if (mask & GL_STENCIL_BUFFER_BIT) {
  2129.       /* OK to memcpy */
  2130.       dst->Stencil = src->Stencil;
  2131.    }
  2132.    if (mask & GL_TEXTURE_BIT) {
  2133.       /* Cannot memcpy because of pointers */
  2134.       _mesa_copy_texture_state(src, dst);
  2135.    }
  2136.    if (mask & GL_TRANSFORM_BIT) {
  2137.       /* OK to memcpy */
  2138.       dst->Transform = src->Transform;
  2139.    }
  2140.    if (mask & GL_VIEWPORT_BIT) {
  2141.       /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
  2142.       dst->Viewport.X = src->Viewport.X;
  2143.       dst->Viewport.Y = src->Viewport.Y;
  2144.       dst->Viewport.Width = src->Viewport.Width;
  2145.       dst->Viewport.Height = src->Viewport.Height;
  2146.       dst->Viewport.Near = src->Viewport.Near;
  2147.       dst->Viewport.Far = src->Viewport.Far;
  2148.       _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
  2149.    }
  2150.  
  2151.    /* XXX FIXME:  Call callbacks?
  2152.     */
  2153.    dst->NewState = _NEW_ALL;
  2154. }
  2155.  
  2156.  
  2157.  
  2158. static void print_info( void )
  2159. {
  2160.    _mesa_debug(NULL, "Mesa GL_VERSION = %s\n",
  2161.        (char *) _mesa_GetString(GL_VERSION));
  2162.    _mesa_debug(NULL, "Mesa GL_RENDERER = %s\n",
  2163.        (char *) _mesa_GetString(GL_RENDERER));
  2164.    _mesa_debug(NULL, "Mesa GL_VENDOR = %s\n",
  2165.        (char *) _mesa_GetString(GL_VENDOR));
  2166.    _mesa_debug(NULL, "Mesa GL_EXTENSIONS = %s\n",
  2167.        (char *) _mesa_GetString(GL_EXTENSIONS));
  2168. #if defined(THREADS)
  2169.    _mesa_debug(NULL, "Mesa thread-safe: YES\n");
  2170. #else
  2171.    _mesa_debug(NULL, "Mesa thread-safe: NO\n");
  2172. #endif
  2173. #if defined(USE_X86_ASM)
  2174.    _mesa_debug(NULL, "Mesa x86-optimized: YES\n");
  2175. #else
  2176.    _mesa_debug(NULL, "Mesa x86-optimized: NO\n");
  2177. #endif
  2178. #if defined(USE_SPARC_ASM)
  2179.    _mesa_debug(NULL, "Mesa sparc-optimized: YES\n");
  2180. #else
  2181.    _mesa_debug(NULL, "Mesa sparc-optimized: NO\n");
  2182. #endif
  2183. }
  2184.  
  2185.  
  2186. /*
  2187.  * Set the current context, binding the given frame buffer to the context.
  2188.  */
  2189. void
  2190. _mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
  2191. {
  2192.    _mesa_make_current2( newCtx, buffer, buffer );
  2193. }
  2194.  
  2195.  
  2196. /*
  2197.  * Bind the given context to the given draw-buffer and read-buffer
  2198.  * and make it the current context for this thread.
  2199.  */
  2200. void
  2201. _mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
  2202.                      GLframebuffer *readBuffer )
  2203. {
  2204.    if (MESA_VERBOSE)
  2205.       _mesa_debug(newCtx, "_mesa_make_current2()\n");
  2206.  
  2207.    /* Check that the context's and framebuffer's visuals are compatible.
  2208.     * We could do a lot more checking here but this'll catch obvious
  2209.     * problems.
  2210.     */
  2211.    if (newCtx && drawBuffer && readBuffer) {
  2212.       if (newCtx->Visual.rgbMode != drawBuffer->Visual.rgbMode ||
  2213.           newCtx->Visual.redBits != drawBuffer->Visual.redBits ||
  2214.           newCtx->Visual.depthBits != drawBuffer->Visual.depthBits ||
  2215.           newCtx->Visual.stencilBits != drawBuffer->Visual.stencilBits ||
  2216.           newCtx->Visual.accumRedBits != drawBuffer->Visual.accumRedBits) {
  2217.          return; /* incompatible */
  2218.       }
  2219.    }
  2220.  
  2221.    /* We call this function periodically (just here for now) in
  2222.     * order to detect when multithreading has begun.
  2223.     */
  2224.    _glapi_check_multithread();
  2225.  
  2226.    _glapi_set_context((void *) newCtx);
  2227.    ASSERT(_mesa_get_current_context() == newCtx);
  2228.  
  2229.  
  2230.    if (!newCtx) {
  2231.       _glapi_set_dispatch(NULL);  /* none current */
  2232.    }
  2233.    else {
  2234.       _glapi_set_dispatch(newCtx->CurrentDispatch);
  2235.  
  2236.       if (drawBuffer && readBuffer) {
  2237.      /* TODO: check if newCtx and buffer's visual match??? */
  2238.      newCtx->DrawBuffer = drawBuffer;
  2239.      newCtx->ReadBuffer = readBuffer;
  2240.      newCtx->NewState |= _NEW_BUFFERS;
  2241.  
  2242.          if (drawBuffer->Width == 0 && drawBuffer->Height == 0) {
  2243.             /* get initial window size */
  2244.             GLuint bufWidth, bufHeight;
  2245.  
  2246.             /* ask device driver for size of output buffer */
  2247.             (*newCtx->Driver.GetBufferSize)( drawBuffer, &bufWidth, &bufHeight );
  2248.  
  2249.             if (drawBuffer->Width == bufWidth && drawBuffer->Height == bufHeight)
  2250.                return; /* size is as expected */
  2251.  
  2252.             drawBuffer->Width = bufWidth;
  2253.             drawBuffer->Height = bufHeight;
  2254.  
  2255.             newCtx->Driver.ResizeBuffers( drawBuffer );
  2256.          }
  2257.  
  2258.          if (readBuffer != drawBuffer &&
  2259.              readBuffer->Width == 0 && readBuffer->Height == 0) {
  2260.             /* get initial window size */
  2261.             GLuint bufWidth, bufHeight;
  2262.  
  2263.             /* ask device driver for size of output buffer */
  2264.             (*newCtx->Driver.GetBufferSize)( readBuffer, &bufWidth, &bufHeight );
  2265.  
  2266.             if (readBuffer->Width == bufWidth && readBuffer->Height == bufHeight)
  2267.                return; /* size is as expected */
  2268.  
  2269.             readBuffer->Width = bufWidth;
  2270.             readBuffer->Height = bufHeight;
  2271.  
  2272.             newCtx->Driver.ResizeBuffers( readBuffer );
  2273.          }
  2274.       }
  2275.  
  2276.       /* This is only for T&L - a bit out of place, or misnamed (BP) */
  2277.       if (newCtx->Driver.MakeCurrent)
  2278.      newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
  2279.  
  2280.       /* We can use this to help debug user's problems.  Tell them to set
  2281.        * the MESA_INFO env variable before running their app.  Then the
  2282.        * first time each context is made current we'll print some useful
  2283.        * information.
  2284.        */
  2285.       if (newCtx->FirstTimeCurrent) {
  2286.      if (_mesa_getenv("MESA_INFO")) {
  2287.         print_info();
  2288.      }
  2289.      newCtx->FirstTimeCurrent = GL_FALSE;
  2290.       }
  2291.    }
  2292. }
  2293.  
  2294.  
  2295.  
  2296. /*
  2297.  * Return current context handle for the calling thread.
  2298.  * This isn't the fastest way to get the current context.
  2299.  * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
  2300.  */
  2301. GLcontext *
  2302. _mesa_get_current_context( void )
  2303. {
  2304.    return (GLcontext *) _glapi_get_context();
  2305. }
  2306.  
  2307.  
  2308. /*
  2309.  * Return pointer to this context's current API dispatch table.
  2310.  * It'll either be the immediate-mode execute dispatcher or the
  2311.  * display list compile dispatcher.
  2312.  */
  2313. struct _glapi_table *
  2314. _mesa_get_dispatch(GLcontext *ctx)
  2315. {
  2316.    return ctx->CurrentDispatch;
  2317. }
  2318.  
  2319.  
  2320.  
  2321. /**********************************************************************/
  2322. /*****                Miscellaneous functions                     *****/
  2323. /**********************************************************************/
  2324.  
  2325.  
  2326. /*
  2327.  * Record the given error code and call the driver's Error function if defined.
  2328.  * This is called via _mesa_error().
  2329.  */
  2330. void
  2331. _mesa_record_error( GLcontext *ctx, GLenum error )
  2332. {
  2333.    if (!ctx)
  2334.       return;
  2335.  
  2336.    if (ctx->ErrorValue == GL_NO_ERROR) {
  2337.       ctx->ErrorValue = error;
  2338.    }
  2339.  
  2340.    /* Call device driver's error handler, if any.  This is used on the Mac. */
  2341.    if (ctx->Driver.Error) {
  2342.       (*ctx->Driver.Error)( ctx );
  2343.    }
  2344. }
  2345.  
  2346.  
  2347. void
  2348. _mesa_Finish( void )
  2349. {
  2350.    GET_CURRENT_CONTEXT(ctx);
  2351.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
  2352.    if (ctx->Driver.Finish) {
  2353.       (*ctx->Driver.Finish)( ctx );
  2354.    }
  2355. }
  2356.  
  2357.  
  2358.  
  2359. void
  2360. _mesa_Flush( void )
  2361. {
  2362.    GET_CURRENT_CONTEXT(ctx);
  2363.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
  2364.    if (ctx->Driver.Flush) {
  2365.       (*ctx->Driver.Flush)( ctx );
  2366.    }
  2367. }
  2368.  
  2369.  
  2370.  
  2371. const char *_mesa_prim_name[GL_POLYGON+4] = {
  2372.    "GL_POINTS",
  2373.    "GL_LINES",
  2374.    "GL_LINE_LOOP",
  2375.    "GL_LINE_STRIP",
  2376.    "GL_TRIANGLES",
  2377.    "GL_TRIANGLE_STRIP",
  2378.    "GL_TRIANGLE_FAN",
  2379.    "GL_QUADS",
  2380.    "GL_QUAD_STRIP",
  2381.    "GL_POLYGON",
  2382.    "outside begin/end",
  2383.    "inside unkown primitive",
  2384.    "unknown state"
  2385. };
  2386.