home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src / context.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  72KB  |  2,486 lines

  1. /* $Id: context.c,v 1.18.2.6 1999/12/04 21:13:44 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.1
  6.  *
  7.  * Copyright (C) 1999  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. /* $XFree86: xc/lib/GL/mesa/src/context.c,v 1.4 1999/04/04 00:20:21 dawes Exp $ */
  29.  
  30. /*
  31.  * If multi-threading is enabled (-DTHREADS) then each thread has it's
  32.  * own rendering context.  A thread obtains the pointer to its GLcontext
  33.  * with the gl_get_thread_context() function.  Otherwise, the global
  34.  * pointer, CC, points to the current context used by all threads in
  35.  * the address space.
  36.  */
  37.  
  38.  
  39.  
  40. #ifdef PC_HEADER
  41. #include "all.h"
  42. #else
  43. #ifndef XFree86Server
  44. #include <assert.h>
  45. #include <math.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #else
  50. #include "GL/xf86glx.h"
  51. #endif
  52. #include "accum.h"
  53. #include "alphabuf.h"
  54. #include "api.h"
  55. #include "clip.h"
  56. #include "context.h"
  57. #include "cva.h"
  58. #include "depth.h"
  59. #include "dlist.h"
  60. #include "eval.h"
  61. #include "enums.h"
  62. #include "extensions.h"
  63. #include "fog.h"
  64. #include "get.h"
  65. #include "hash.h"
  66. #include "light.h"
  67. #include "lines.h"
  68. #include "dlist.h"
  69. #include "macros.h"
  70. #include "matrix.h"
  71. #include "mmath.h"
  72. #include "pb.h"
  73. #include "pipeline.h"
  74. #include "points.h"
  75. #include "pointers.h"
  76. #include "quads.h"
  77. #include "shade.h"
  78. #include "simple_list.h"
  79. #include "stencil.h"
  80. #include "stages.h"
  81. #include "triangle.h"
  82. #include "translate.h"
  83. #include "teximage.h"
  84. #include "texobj.h"
  85. #include "texstate.h"
  86. #include "texture.h"
  87. #include "types.h"
  88. #include "varray.h"
  89. #include "vb.h"
  90. #include "vbcull.h"
  91. #include "vbfill.h"
  92. #include "vbrender.h"
  93. #include "vbxform.h"
  94. #include "vertices.h"
  95. #include "xform.h"
  96. #endif
  97.  
  98.  
  99. /*
  100.  * Memory allocation functions.  Called via the MALLOC, CALLOC and
  101.  * FREE macros when DEBUG symbol is defined.
  102.  * You might want to set breakpoints on these functions or plug in
  103.  * other memory allocation functions.  The Mesa sources should only
  104.  * use the MALLOC and FREE macros (which could also be overriden).
  105.  *
  106.  * XXX these functions should probably go into a new glmemory.c file.
  107.  */
  108.  
  109. /*
  110.  * Allocate memory (uninitialized)
  111.  */
  112. void *gl_malloc(size_t bytes)
  113. {
  114.    return malloc(bytes);
  115. }
  116.  
  117. /*
  118.  * Allocate memory and initialize to zero.
  119.  */
  120. void *gl_calloc(size_t bytes)
  121. {
  122.    return calloc(1, bytes);
  123. }
  124.  
  125. /*
  126.  * Free memory
  127.  */
  128. void gl_free(void *ptr)
  129. {
  130.    free(ptr);
  131. }
  132.  
  133.  
  134. /**********************************************************************/
  135. /*****                  Context and Thread management             *****/
  136. /**********************************************************************/
  137.  
  138.  
  139. #ifdef THREADS
  140.  
  141. #include "mthreads.h" /* Mesa platform independent threads interface */
  142.  
  143. static MesaTSD mesa_ctx_tsd;
  144.  
  145. static void mesa_ctx_thread_init() {
  146.   MesaInitTSD(&mesa_ctx_tsd);
  147. }
  148.  
  149. GLcontext *gl_get_thread_context( void ) {
  150.   return (GLcontext *) MesaGetTSD(&mesa_ctx_tsd);
  151. }
  152.  
  153. static void set_thread_context( GLcontext *ctx ) {
  154.   MesaSetTSD(&mesa_ctx_tsd, ctx, mesa_ctx_thread_init);
  155. }
  156.  
  157.  
  158. #else
  159.  
  160. /* One Current Context pointer for all threads in the address space */
  161. GLcontext *CC = NULL;
  162. struct immediate *CURRENT_INPUT = NULL;
  163.  
  164. #endif /*THREADS*/
  165.  
  166.  
  167.  
  168.  
  169. /**********************************************************************/
  170. /*****                   Profiling functions                      *****/
  171. /**********************************************************************/
  172.  
  173. #ifdef PROFILE
  174.  
  175. #include <sys/times.h>
  176. #include <sys/param.h>
  177.  
  178.  
  179. /*
  180.  * Return system time in seconds.
  181.  * NOTE:  this implementation may not be very portable!
  182.  */
  183. GLdouble gl_time( void )
  184. {
  185.    static GLdouble prev_time = 0.0;
  186.    static GLdouble time;
  187.    struct tms tm;
  188.    clock_t clk;
  189.  
  190.    clk = times(&tm);
  191.  
  192. #ifdef CLK_TCK
  193.    time = (double)clk / (double)CLK_TCK;
  194. #else
  195.    time = (double)clk / (double)HZ;
  196. #endif
  197.  
  198.    if (time>prev_time) {
  199.       prev_time = time;
  200.       return time;
  201.    }
  202.    else {
  203.       return prev_time;
  204.    }
  205. }
  206.  
  207. /*
  208.  * Reset the timing/profiling counters
  209.  */
  210. static void init_timings( GLcontext *ctx )
  211. {
  212.    ctx->BeginEndCount = 0;
  213.    ctx->BeginEndTime = 0.0;
  214.    ctx->VertexCount = 0;
  215.    ctx->VertexTime = 0.0;
  216.    ctx->PointCount = 0;
  217.    ctx->PointTime = 0.0;
  218.    ctx->LineCount = 0;
  219.    ctx->LineTime = 0.0;
  220.    ctx->PolygonCount = 0;
  221.    ctx->PolygonTime = 0.0;
  222.    ctx->ClearCount = 0;
  223.    ctx->ClearTime = 0.0;
  224.    ctx->SwapCount = 0;
  225.    ctx->SwapTime = 0.0;
  226. }
  227.  
  228.  
  229. /*
  230.  * Print the accumulated timing/profiling data.
  231.  */
  232. static void print_timings( GLcontext *ctx )
  233. {
  234.    GLdouble beginendrate;
  235.    GLdouble vertexrate;
  236.    GLdouble pointrate;
  237.    GLdouble linerate;
  238.    GLdouble polygonrate;
  239.    GLdouble overhead;
  240.    GLdouble clearrate;
  241.    GLdouble swaprate;
  242.    GLdouble avgvertices;
  243.  
  244.    if (ctx->BeginEndTime>0.0) {
  245.       beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
  246.    }
  247.    else {
  248.       beginendrate = 0.0;
  249.    }
  250.    if (ctx->VertexTime>0.0) {
  251.       vertexrate = ctx->VertexCount / ctx->VertexTime;
  252.    }
  253.    else {
  254.       vertexrate = 0.0;
  255.    }
  256.    if (ctx->PointTime>0.0) {
  257.       pointrate = ctx->PointCount / ctx->PointTime;
  258.    }
  259.    else {
  260.       pointrate = 0.0;
  261.    }
  262.    if (ctx->LineTime>0.0) {
  263.       linerate = ctx->LineCount / ctx->LineTime;
  264.    }
  265.    else {
  266.       linerate = 0.0;
  267.    }
  268.    if (ctx->PolygonTime>0.0) {
  269.       polygonrate = ctx->PolygonCount / ctx->PolygonTime;
  270.    }
  271.    else {
  272.       polygonrate = 0.0;
  273.    }
  274.    if (ctx->ClearTime>0.0) {
  275.       clearrate = ctx->ClearCount / ctx->ClearTime;
  276.    }
  277.    else {
  278.       clearrate = 0.0;
  279.    }
  280.    if (ctx->SwapTime>0.0) {
  281.       swaprate = ctx->SwapCount / ctx->SwapTime;
  282.    }
  283.    else {
  284.       swaprate = 0.0;
  285.    }
  286.  
  287.    if (ctx->BeginEndCount>0) {
  288.       avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
  289.    }
  290.    else {
  291.       avgvertices = 0.0;
  292.    }
  293.  
  294.    overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
  295.               - ctx->LineTime - ctx->PolygonTime;
  296.  
  297.  
  298.    printf("                          Count   Time (s)    Rate (/s) \n");
  299.    printf("--------------------------------------------------------\n");
  300.    printf("glBegin/glEnd           %7d  %8.3f   %10.3f\n",
  301.           ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
  302.    printf("  vertexes transformed  %7d  %8.3f   %10.3f\n",
  303.           ctx->VertexCount, ctx->VertexTime, vertexrate );
  304.    printf("  points rasterized     %7d  %8.3f   %10.3f\n",
  305.           ctx->PointCount, ctx->PointTime, pointrate );
  306.    printf("  lines rasterized      %7d  %8.3f   %10.3f\n",
  307.           ctx->LineCount, ctx->LineTime, linerate );
  308.    printf("  polygons rasterized   %7d  %8.3f   %10.3f\n",
  309.           ctx->PolygonCount, ctx->PolygonTime, polygonrate );
  310.    printf("  overhead                       %8.3f\n", overhead );
  311.    printf("glClear                 %7d  %8.3f   %10.3f\n",
  312.           ctx->ClearCount, ctx->ClearTime, clearrate );
  313.    printf("SwapBuffers             %7d  %8.3f   %10.3f\n",
  314.           ctx->SwapCount, ctx->SwapTime, swaprate );
  315.    printf("\n");
  316.  
  317.    printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
  318. }
  319. #endif
  320.  
  321.  
  322.  
  323.  
  324.  
  325. /**********************************************************************/
  326. /*****       Context allocation, initialization, destroying       *****/
  327. /**********************************************************************/
  328.  
  329.  
  330. /*
  331.  * This function just calls all the various one-time-init functions in Mesa.
  332.  */
  333. static void one_time_init( void )
  334. {
  335.    static GLboolean alreadyCalled = GL_FALSE;
  336.    if (!alreadyCalled) {
  337.       gl_init_clip();
  338.       gl_init_eval();
  339.       gl_init_fog();
  340.       gl_init_math();
  341.       gl_init_lists();
  342.       gl_init_shade();
  343.       gl_init_texture();
  344.       gl_init_transformation();
  345.       gl_init_translate();
  346.       gl_init_vbrender();
  347.       gl_init_vbxform();
  348.       gl_init_vertices();
  349.       alreadyCalled = GL_TRUE;
  350.    }
  351. #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
  352.    fprintf(stderr, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
  353. #endif
  354. }
  355.  
  356.  
  357. /*
  358.  * Allocate and initialize a shared context state structure.
  359.  */
  360. static struct gl_shared_state *alloc_shared_state( void )
  361. {
  362.    GLuint d;
  363.    struct gl_shared_state *ss;
  364.    GLboolean outOfMemory;
  365.  
  366.    ss = CALLOC_STRUCT(gl_shared_state);
  367.    if (!ss)
  368.       return NULL;
  369.  
  370.    ss->DisplayList = NewHashTable();
  371.  
  372.    ss->TexObjects = NewHashTable();
  373.  
  374.    /* Default Texture objects */
  375.    outOfMemory = GL_FALSE;
  376.    for (d = 1 ; d <= 3 ; d++) {
  377.       ss->DefaultD[d] = gl_alloc_texture_object(ss, 0, d);
  378.       if (!ss->DefaultD[d]) {
  379.          outOfMemory = GL_TRUE;
  380.          break;
  381.       }
  382.       ss->DefaultD[d]->RefCount++; /* don't free if not in use */
  383.    }
  384.  
  385.    if (!ss->DisplayList || !ss->TexObjects || outOfMemory) {
  386.       /* Ran out of memory at some point.  Free everything and return NULL */
  387.       if (ss->DisplayList)
  388.          DeleteHashTable(ss->DisplayList);
  389.       if (ss->TexObjects)
  390.          DeleteHashTable(ss->TexObjects);
  391.       if (ss->DefaultD[1])
  392.          gl_free_texture_object(ss, ss->DefaultD[1]);
  393.       if (ss->DefaultD[2])
  394.          gl_free_texture_object(ss, ss->DefaultD[2]);
  395.       if (ss->DefaultD[3])
  396.          gl_free_texture_object(ss, ss->DefaultD[3]);
  397.       FREE(ss);
  398.       return NULL;
  399.    }
  400.    else {
  401.       return ss;
  402.    }
  403. }
  404.  
  405.  
  406. /*
  407.  * Deallocate a shared state context and all children structures.
  408.  */
  409. static void free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
  410. {
  411.    /* Free display lists */
  412.    while (1) {
  413.       GLuint list = HashFirstEntry(ss->DisplayList);
  414.       if (list) {
  415.          gl_destroy_list(ctx, list);
  416.       }
  417.       else {
  418.          break;
  419.       }
  420.    }
  421.    DeleteHashTable(ss->DisplayList);
  422.  
  423.    /* Free texture objects */
  424.    while (ss->TexObjectList)
  425.    {
  426.       if (ctx->Driver.DeleteTexture)
  427.          (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
  428.       /* this function removes from linked list too! */
  429.       gl_free_texture_object(ss, ss->TexObjectList);
  430.    }
  431.    DeleteHashTable(ss->TexObjects);
  432.  
  433.    FREE(ss);
  434. }
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441. /*
  442.  * Initialize the nth light.  Note that the defaults for light 0 are
  443.  * different than the other lights.
  444.  */
  445. static void init_light( struct gl_light *l, GLuint n )
  446. {
  447.    make_empty_list( l );
  448.  
  449.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  450.    if (n==0) {
  451.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  452.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  453.    }
  454.    else {
  455.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  456.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  457.    }
  458.    ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
  459.    ASSIGN_3V( l->EyeDirection, 0.0, 0.0, -1.0 );
  460.    l->SpotExponent = 0.0;
  461.    gl_compute_spot_exp_table( l );
  462.    l->SpotCutoff = 180.0;
  463.    l->CosCutoff = 0.0;        /* KW: -ve values not admitted */
  464.    l->ConstantAttenuation = 1.0;
  465.    l->LinearAttenuation = 0.0;
  466.    l->QuadraticAttenuation = 0.0;
  467.    l->Enabled = GL_FALSE;
  468. }
  469.  
  470.  
  471.  
  472. static void init_lightmodel( struct gl_lightmodel *lm )
  473. {
  474.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  475.    lm->LocalViewer = GL_FALSE;
  476.    lm->TwoSide = GL_FALSE;
  477.    lm->ColorControl = GL_SINGLE_COLOR;
  478. }
  479.  
  480.  
  481. static void init_material( struct gl_material *m )
  482. {
  483.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  484.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  485.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  486.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  487.    m->Shininess = 0.0;
  488.    m->AmbientIndex = 0;
  489.    m->DiffuseIndex = 1;
  490.    m->SpecularIndex = 1;
  491. }
  492.  
  493.  
  494.  
  495. static void init_texture_unit( GLcontext *ctx, GLuint unit )
  496. {
  497.    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
  498.  
  499.    texUnit->EnvMode = GL_MODULATE;
  500.    ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
  501.    texUnit->TexGenEnabled = 0;
  502.    texUnit->GenModeS = GL_EYE_LINEAR;
  503.    texUnit->GenModeT = GL_EYE_LINEAR;
  504.    texUnit->GenModeR = GL_EYE_LINEAR;
  505.    texUnit->GenModeQ = GL_EYE_LINEAR;
  506.    /* Yes, these plane coefficients are correct! */
  507.    ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  508.    ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  509.    ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  510.    ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  511.    ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  512.    ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  513.    ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  514.    ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  515.  
  516.    texUnit->CurrentD[1] = ctx->Shared->DefaultD[1];
  517.    texUnit->CurrentD[2] = ctx->Shared->DefaultD[2];
  518.    texUnit->CurrentD[3] = ctx->Shared->DefaultD[3];
  519. }
  520.  
  521.  
  522. static void init_fallback_arrays( GLcontext *ctx )
  523. {
  524.    struct gl_client_array *cl;
  525.    GLuint i;
  526.  
  527.    cl = &ctx->Fallback.Normal;
  528.    cl->Size = 3;
  529.    cl->Type = GL_FLOAT;
  530.    cl->Stride = 0;
  531.    cl->StrideB = 0;
  532.    cl->Ptr = (void *) ctx->Current.Normal;
  533.    cl->Enabled = 1;
  534.  
  535.    cl = &ctx->Fallback.Color;
  536.    cl->Size = 4;
  537.    cl->Type = GL_UNSIGNED_BYTE;
  538.    cl->Stride = 0;
  539.    cl->StrideB = 0;
  540.    cl->Ptr = (void *) ctx->Current.ByteColor;
  541.    cl->Enabled = 1;
  542.  
  543.    cl = &ctx->Fallback.Index;
  544.    cl->Size = 1;
  545.    cl->Type = GL_UNSIGNED_INT;
  546.    cl->Stride = 0;
  547.    cl->StrideB = 0;
  548.    cl->Ptr = (void *) &ctx->Current.Index;
  549.    cl->Enabled = 1;
  550.  
  551.    for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
  552.       cl = &ctx->Fallback.TexCoord[i];
  553.       cl->Size = 4;
  554.       cl->Type = GL_FLOAT;
  555.       cl->Stride = 0;
  556.       cl->StrideB = 0;
  557.       cl->Ptr = (void *) ctx->Current.Texcoord[i];
  558.       cl->Enabled = 1;
  559.    }
  560.  
  561.    cl = &ctx->Fallback.EdgeFlag;
  562.    cl->Size = 1;
  563.    cl->Type = GL_UNSIGNED_BYTE;
  564.    cl->Stride = 0;
  565.    cl->StrideB = 0;
  566.    cl->Ptr = (void *) &ctx->Current.EdgeFlag;
  567.    cl->Enabled = 1;
  568. }
  569.  
  570. /* Initialize a 1-D evaluator map */
  571. static void init_1d_map( struct gl_1d_map *map, int n, const float *initial )
  572. {
  573.    map->Order = 1;
  574.    map->u1 = 0.0;
  575.    map->u2 = 1.0;
  576.    map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
  577.    if (map->Points) {
  578.       GLint i;
  579.       for (i=0;i<n;i++)
  580.          map->Points[i] = initial[i];
  581.    }
  582.    map->Retain = GL_FALSE;
  583. }
  584.  
  585.  
  586. /* Initialize a 2-D evaluator map */
  587. static void init_2d_map( struct gl_2d_map *map, int n, const float *initial )
  588. {
  589.    map->Uorder = 1;
  590.    map->Vorder = 1;
  591.    map->u1 = 0.0;
  592.    map->u2 = 1.0;
  593.    map->v1 = 0.0;
  594.    map->v2 = 1.0;
  595.    map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
  596.    if (map->Points) {
  597.       GLint i;
  598.       for (i=0;i<n;i++)
  599.          map->Points[i] = initial[i];
  600.    }
  601.    map->Retain = GL_FALSE;
  602. }
  603.  
  604.  
  605.  
  606. /*
  607.  * Initialize a gl_context structure to default values.
  608.  */
  609. static void initialize_context( GLcontext *ctx )
  610. {
  611.    GLuint i, j;
  612.  
  613.    if (ctx) {
  614.       /* Constants, may be overriden by device driver */
  615.       ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
  616.       ctx->Const.MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
  617.       ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
  618.       ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
  619.  
  620.       /* Modelview matrix */
  621.       gl_matrix_ctr( &ctx->ModelView );
  622.       gl_matrix_alloc_inv( &ctx->ModelView );
  623.  
  624.       ctx->ModelViewStackDepth = 0;
  625.       for (i = 0 ; i < MAX_MODELVIEW_STACK_DEPTH ; i++) {
  626.      gl_matrix_ctr( &ctx->ModelViewStack[i] );
  627.      gl_matrix_alloc_inv( &ctx->ModelViewStack[i] );
  628.       }
  629.  
  630.       /* Projection matrix - need inv for user clipping in clip space*/
  631.       gl_matrix_ctr( &ctx->ProjectionMatrix );
  632.       gl_matrix_alloc_inv( &ctx->ProjectionMatrix );
  633.  
  634.       gl_matrix_ctr( &ctx->ModelProjectMatrix );
  635.       gl_matrix_ctr( &ctx->ModelProjectWinMatrix );
  636.       ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
  637.  
  638.       ctx->ProjectionStackDepth = 0;
  639.       ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
  640.       ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
  641.  
  642.       for (i = 0 ; i < MAX_PROJECTION_STACK_DEPTH ; i++) {
  643.      gl_matrix_ctr( &ctx->ProjectionStack[i] );
  644.      gl_matrix_alloc_inv( &ctx->ProjectionStack[i] );
  645.       }
  646.  
  647.       /* Texture matrix */
  648.       for (i=0; i<MAX_TEXTURE_UNITS; i++) {
  649.      gl_matrix_ctr( &ctx->TextureMatrix[i] );
  650.      ctx->TextureStackDepth[i] = 0;
  651.      for (j = 0 ; j < MAX_TEXTURE_STACK_DEPTH ; j++) {
  652.         ctx->TextureStack[i][j].inv = 0;
  653.      }
  654.       }
  655.  
  656.       /* Accumulate buffer group */
  657.       ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  658.  
  659.       /* Color buffer group */
  660.       ctx->Color.IndexMask = 0xffffffff;
  661.       ctx->Color.ColorMask[0] = 0xff;
  662.       ctx->Color.ColorMask[1] = 0xff;
  663.       ctx->Color.ColorMask[2] = 0xff;
  664.       ctx->Color.ColorMask[3] = 0xff;
  665.       ctx->Color.SWmasking = GL_FALSE;
  666.       ctx->Color.ClearIndex = 0;
  667.       ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  668.       ctx->Color.DrawBuffer = GL_FRONT;
  669.       ctx->Color.AlphaEnabled = GL_FALSE;
  670.       ctx->Color.AlphaFunc = GL_ALWAYS;
  671.       ctx->Color.AlphaRef = 0;
  672.       ctx->Color.BlendEnabled = GL_FALSE;
  673.       ctx->Color.BlendSrcRGB = GL_ONE;
  674.       ctx->Color.BlendDstRGB = GL_ZERO;
  675.       ctx->Color.BlendSrcA = GL_ONE;
  676.       ctx->Color.BlendDstA = GL_ZERO;
  677.       ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
  678.       ctx->Color.BlendFunc = NULL;  /* this pointer set only when needed */
  679.       ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  680.       ctx->Color.IndexLogicOpEnabled = GL_FALSE;
  681.       ctx->Color.ColorLogicOpEnabled = GL_FALSE;
  682.       ctx->Color.SWLogicOpEnabled = GL_FALSE;
  683.       ctx->Color.LogicOp = GL_COPY;
  684.       ctx->Color.DitherFlag = GL_TRUE;
  685.       ctx->Color.MultiDrawBuffer = GL_FALSE;
  686.  
  687.       /* Current group */
  688.       ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255);
  689.       ctx->Current.Index = 1;
  690.       for (i=0; i<MAX_TEXTURE_UNITS; i++)
  691.          ASSIGN_4V( ctx->Current.Texcoord[i], 0.0, 0.0, 0.0, 1.0 );
  692.       ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  693.       ctx->Current.RasterDistance = 0.0;
  694.       ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  695.       ctx->Current.RasterIndex = 1;
  696.       for (i=0; i<MAX_TEXTURE_UNITS; i++)
  697.          ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
  698.       ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
  699.       ctx->Current.RasterPosValid = GL_TRUE;
  700.       ctx->Current.EdgeFlag = GL_TRUE;
  701.       ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
  702.       ctx->Current.Primitive = (GLenum) (GL_POLYGON + 1);
  703.  
  704.       ctx->Current.Flag = (VERT_NORM|VERT_INDEX|VERT_RGBA|VERT_EDGE|
  705.                VERT_TEX0_1|VERT_TEX1_1|VERT_MATERIAL);
  706.  
  707.       init_fallback_arrays( ctx );
  708.  
  709.       /* Depth buffer group */
  710.       ctx->Depth.Test = GL_FALSE;
  711.       ctx->Depth.Clear = 1.0;
  712.       ctx->Depth.Func = GL_LESS;
  713.       ctx->Depth.Mask = GL_TRUE;
  714.  
  715.       /* Evaluators group */
  716.       ctx->Eval.Map1Color4 = GL_FALSE;
  717.       ctx->Eval.Map1Index = GL_FALSE;
  718.       ctx->Eval.Map1Normal = GL_FALSE;
  719.       ctx->Eval.Map1TextureCoord1 = GL_FALSE;
  720.       ctx->Eval.Map1TextureCoord2 = GL_FALSE;
  721.       ctx->Eval.Map1TextureCoord3 = GL_FALSE;
  722.       ctx->Eval.Map1TextureCoord4 = GL_FALSE;
  723.       ctx->Eval.Map1Vertex3 = GL_FALSE;
  724.       ctx->Eval.Map1Vertex4 = GL_FALSE;
  725.       ctx->Eval.Map2Color4 = GL_FALSE;
  726.       ctx->Eval.Map2Index = GL_FALSE;
  727.       ctx->Eval.Map2Normal = GL_FALSE;
  728.       ctx->Eval.Map2TextureCoord1 = GL_FALSE;
  729.       ctx->Eval.Map2TextureCoord2 = GL_FALSE;
  730.       ctx->Eval.Map2TextureCoord3 = GL_FALSE;
  731.       ctx->Eval.Map2TextureCoord4 = GL_FALSE;
  732.       ctx->Eval.Map2Vertex3 = GL_FALSE;
  733.       ctx->Eval.Map2Vertex4 = GL_FALSE;
  734.       ctx->Eval.AutoNormal = GL_FALSE;
  735.       ctx->Eval.MapGrid1un = 1;
  736.       ctx->Eval.MapGrid1u1 = 0.0;
  737.       ctx->Eval.MapGrid1u2 = 1.0;
  738.       ctx->Eval.MapGrid2un = 1;
  739.       ctx->Eval.MapGrid2vn = 1;
  740.       ctx->Eval.MapGrid2u1 = 0.0;
  741.       ctx->Eval.MapGrid2u2 = 1.0;
  742.       ctx->Eval.MapGrid2v1 = 0.0;
  743.       ctx->Eval.MapGrid2v2 = 1.0;
  744.  
  745.       /* Evaluator data */
  746.       {
  747.          static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
  748.          static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
  749.          static GLfloat index[1] = { 1.0 };
  750.          static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
  751.          static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
  752.  
  753.          init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
  754.          init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
  755.          init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
  756.          init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
  757.          init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
  758.          init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
  759.          init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
  760.          init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
  761.          init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
  762.  
  763.          init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
  764.          init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
  765.          init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
  766.          init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
  767.          init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
  768.          init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
  769.          init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
  770.          init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
  771.          init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
  772.       }
  773.  
  774.       /* Fog group */
  775.       ctx->Fog.Enabled = GL_FALSE;
  776.       ctx->Fog.Mode = GL_EXP;
  777.       ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  778.       ctx->Fog.Index = 0.0;
  779.       ctx->Fog.Density = 1.0;
  780.       ctx->Fog.Start = 0.0;
  781.       ctx->Fog.End = 1.0;
  782.  
  783.       /* Hint group */
  784.       ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
  785.       ctx->Hint.PointSmooth = GL_DONT_CARE;
  786.       ctx->Hint.LineSmooth = GL_DONT_CARE;
  787.       ctx->Hint.PolygonSmooth = GL_DONT_CARE;
  788.       ctx->Hint.Fog = GL_DONT_CARE;
  789.  
  790.       ctx->Hint.AllowDrawWin = GL_TRUE;
  791.       ctx->Hint.AllowDrawSpn = GL_TRUE;
  792.       ctx->Hint.AllowDrawMem = GL_TRUE;
  793.       ctx->Hint.StrictLighting = GL_TRUE;
  794.  
  795.       /* Pipeline */
  796.       gl_pipeline_init( ctx );
  797.       gl_cva_init( ctx );
  798.  
  799.       /* Extensions */
  800.       gl_extensions_ctr( ctx );
  801.  
  802.       ctx->AllowVertexCull = CLIP_CULLED_BIT;
  803.  
  804.       /* Lighting group */
  805.       for (i=0;i<MAX_LIGHTS;i++) {
  806.      init_light( &ctx->Light.Light[i], i );
  807.       }
  808.       make_empty_list( &ctx->Light.EnabledList );
  809.  
  810.       init_lightmodel( &ctx->Light.Model );
  811.       init_material( &ctx->Light.Material[0] );
  812.       init_material( &ctx->Light.Material[1] );
  813.       ctx->Light.ShadeModel = GL_SMOOTH;
  814.       ctx->Light.Enabled = GL_FALSE;
  815.       ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  816.       ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  817.       ctx->Light.ColorMaterialBitmask
  818.          = gl_material_bitmask( ctx,
  819.                 GL_FRONT_AND_BACK,
  820.                 GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
  821.  
  822.       ctx->Light.ColorMaterialEnabled = GL_FALSE;
  823.  
  824.       /* Line group */
  825.       ctx->Line.SmoothFlag = GL_FALSE;
  826.       ctx->Line.StippleFlag = GL_FALSE;
  827.       ctx->Line.Width = 1.0;
  828.       ctx->Line.StipplePattern = 0xffff;
  829.       ctx->Line.StippleFactor = 1;
  830.  
  831.       /* Display List group */
  832.       ctx->List.ListBase = 0;
  833.  
  834.       /* Pixel group */
  835.       ctx->Pixel.RedBias = 0.0;
  836.       ctx->Pixel.RedScale = 1.0;
  837.       ctx->Pixel.GreenBias = 0.0;
  838.       ctx->Pixel.GreenScale = 1.0;
  839.       ctx->Pixel.BlueBias = 0.0;
  840.       ctx->Pixel.BlueScale = 1.0;
  841.       ctx->Pixel.AlphaBias = 0.0;
  842.       ctx->Pixel.AlphaScale = 1.0;
  843.       ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
  844.       ctx->Pixel.DepthBias = 0.0;
  845.       ctx->Pixel.DepthScale = 1.0;
  846.       ctx->Pixel.IndexOffset = 0;
  847.       ctx->Pixel.IndexShift = 0;
  848.       ctx->Pixel.ZoomX = 1.0;
  849.       ctx->Pixel.ZoomY = 1.0;
  850.       ctx->Pixel.MapColorFlag = GL_FALSE;
  851.       ctx->Pixel.MapStencilFlag = GL_FALSE;
  852.       ctx->Pixel.MapStoSsize = 1;
  853.       ctx->Pixel.MapItoIsize = 1;
  854.       ctx->Pixel.MapItoRsize = 1;
  855.       ctx->Pixel.MapItoGsize = 1;
  856.       ctx->Pixel.MapItoBsize = 1;
  857.       ctx->Pixel.MapItoAsize = 1;
  858.       ctx->Pixel.MapRtoRsize = 1;
  859.       ctx->Pixel.MapGtoGsize = 1;
  860.       ctx->Pixel.MapBtoBsize = 1;
  861.       ctx->Pixel.MapAtoAsize = 1;
  862.       ctx->Pixel.MapStoS[0] = 0;
  863.       ctx->Pixel.MapItoI[0] = 0;
  864.       ctx->Pixel.MapItoR[0] = 0.0;
  865.       ctx->Pixel.MapItoG[0] = 0.0;
  866.       ctx->Pixel.MapItoB[0] = 0.0;
  867.       ctx->Pixel.MapItoA[0] = 0.0;
  868.       ctx->Pixel.MapItoR8[0] = 0;
  869.       ctx->Pixel.MapItoG8[0] = 0;
  870.       ctx->Pixel.MapItoB8[0] = 0;
  871.       ctx->Pixel.MapItoA8[0] = 0;
  872.       ctx->Pixel.MapRtoR[0] = 0.0;
  873.       ctx->Pixel.MapGtoG[0] = 0.0;
  874.       ctx->Pixel.MapBtoB[0] = 0.0;
  875.       ctx->Pixel.MapAtoA[0] = 0.0;
  876.  
  877.       /* Point group */
  878.       ctx->Point.SmoothFlag = GL_FALSE;
  879.       ctx->Point.Size = 1.0;
  880.       ctx->Point.Params[0] = 1.0;
  881.       ctx->Point.Params[1] = 0.0;
  882.       ctx->Point.Params[2] = 0.0;
  883.       ctx->Point.Attenuated = GL_FALSE;
  884.       ctx->Point.MinSize = 0.0;
  885.       ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
  886.       ctx->Point.Threshold = 1.0;
  887.  
  888.       /* Polygon group */
  889.       ctx->Polygon.CullFlag = GL_FALSE;
  890.       ctx->Polygon.CullFaceMode = GL_BACK;
  891.       ctx->Polygon.FrontFace = GL_CCW;
  892.       ctx->Polygon.FrontBit = 0;
  893.       ctx->Polygon.FrontMode = GL_FILL;
  894.       ctx->Polygon.BackMode = GL_FILL;
  895.       ctx->Polygon.Unfilled = GL_FALSE;
  896.       ctx->Polygon.SmoothFlag = GL_FALSE;
  897.       ctx->Polygon.StippleFlag = GL_FALSE;
  898.       ctx->Polygon.OffsetFactor = 0.0F;
  899.       ctx->Polygon.OffsetUnits = 0.0F;
  900.       ctx->Polygon.OffsetPoint = GL_FALSE;
  901.       ctx->Polygon.OffsetLine = GL_FALSE;
  902.       ctx->Polygon.OffsetFill = GL_FALSE;
  903.  
  904.       /* Polygon Stipple group */
  905.       MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  906.  
  907.       /* Scissor group */
  908.       ctx->Scissor.Enabled = GL_FALSE;
  909.       ctx->Scissor.X = 0;
  910.       ctx->Scissor.Y = 0;
  911.       ctx->Scissor.Width = 0;
  912.       ctx->Scissor.Height = 0;
  913.  
  914.       /* Stencil group */
  915.       ctx->Stencil.Enabled = GL_FALSE;
  916.       ctx->Stencil.Function = GL_ALWAYS;
  917.       ctx->Stencil.FailFunc = GL_KEEP;
  918.       ctx->Stencil.ZPassFunc = GL_KEEP;
  919.       ctx->Stencil.ZFailFunc = GL_KEEP;
  920.       ctx->Stencil.Ref = 0;
  921.       ctx->Stencil.ValueMask = STENCIL_MAX;
  922.       ctx->Stencil.Clear = 0;
  923.       ctx->Stencil.WriteMask = STENCIL_MAX;
  924.  
  925.       /* Texture group */
  926.       ctx->Texture.CurrentUnit = 0;      /* multitexture */
  927.       ctx->Texture.CurrentTransformUnit = 0; /* multitexture */
  928.       ctx->Texture.Enabled = 0;
  929.  
  930.       for (i=0; i<MAX_TEXTURE_UNITS; i++)
  931.          init_texture_unit( ctx, i );
  932.  
  933.       ctx->Texture.SharedPalette = GL_FALSE;
  934.       ctx->Texture.Palette[0] = 255;
  935.       ctx->Texture.Palette[1] = 255;
  936.       ctx->Texture.Palette[2] = 255;
  937.       ctx->Texture.Palette[3] = 255;
  938.       ctx->Texture.PaletteSize = 1;
  939.       ctx->Texture.PaletteIntFormat = GL_RGBA;
  940.       ctx->Texture.PaletteFormat = GL_RGBA;
  941.  
  942.       /* Transformation group */
  943.       ctx->Transform.MatrixMode = GL_MODELVIEW;
  944.       ctx->Transform.Normalize = GL_FALSE;
  945.       ctx->Transform.RescaleNormals = GL_FALSE;
  946.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  947.      ctx->Transform.ClipEnabled[i] = GL_FALSE;
  948.          ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
  949.       }
  950.       ctx->Transform.AnyClip = GL_FALSE;
  951.  
  952.       /* Viewport group */
  953.       ctx->Viewport.X = 0;
  954.       ctx->Viewport.Y = 0;
  955.       ctx->Viewport.Width = 0;
  956.       ctx->Viewport.Height = 0;
  957.       ctx->Viewport.Near = 0.0;
  958.       ctx->Viewport.Far = 1.0;
  959.       gl_matrix_ctr(&ctx->Viewport.WindowMap);
  960.  
  961. #define Sz 10
  962. #define Tz 14
  963.       ctx->Viewport.WindowMap.m[Sz] = 0.5 * DEPTH_SCALE;
  964.       ctx->Viewport.WindowMap.m[Tz] = 0.5 * DEPTH_SCALE;
  965. #undef Sz
  966. #undef Tz
  967.  
  968.       ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
  969.       ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
  970.  
  971.       /* Vertex arrays */
  972.       ctx->Array.Vertex.Size = 4;
  973.       ctx->Array.Vertex.Type = GL_FLOAT;
  974.       ctx->Array.Vertex.Stride = 0;
  975.       ctx->Array.Vertex.StrideB = 0;
  976.       ctx->Array.Vertex.Ptr = NULL;
  977.       ctx->Array.Vertex.Enabled = GL_FALSE;
  978.       ctx->Array.Normal.Type = GL_FLOAT;
  979.       ctx->Array.Normal.Stride = 0;
  980.       ctx->Array.Normal.StrideB = 0;
  981.       ctx->Array.Normal.Ptr = NULL;
  982.       ctx->Array.Normal.Enabled = GL_FALSE;
  983.       ctx->Array.Color.Size = 4;
  984.       ctx->Array.Color.Type = GL_FLOAT;
  985.       ctx->Array.Color.Stride = 0;
  986.       ctx->Array.Color.StrideB = 0;
  987.       ctx->Array.Color.Ptr = NULL;
  988.       ctx->Array.Color.Enabled = GL_FALSE;
  989.       ctx->Array.Index.Type = GL_FLOAT;
  990.       ctx->Array.Index.Stride = 0;
  991.       ctx->Array.Index.StrideB = 0;
  992.       ctx->Array.Index.Ptr = NULL;
  993.       ctx->Array.Index.Enabled = GL_FALSE;
  994.       for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
  995.          ctx->Array.TexCoord[i].Size = 4;
  996.          ctx->Array.TexCoord[i].Type = GL_FLOAT;
  997.          ctx->Array.TexCoord[i].Stride = 0;
  998.          ctx->Array.TexCoord[i].StrideB = 0;
  999.          ctx->Array.TexCoord[i].Ptr = NULL;
  1000.          ctx->Array.TexCoord[i].Enabled = GL_FALSE;
  1001.       }
  1002.       ctx->Array.TexCoordInterleaveFactor = 1;
  1003.       ctx->Array.EdgeFlag.Stride = 0;
  1004.       ctx->Array.EdgeFlag.StrideB = 0;
  1005.       ctx->Array.EdgeFlag.Ptr = NULL;
  1006.       ctx->Array.EdgeFlag.Enabled = GL_FALSE;
  1007.       ctx->Array.ActiveTexture = 0;   /* GL_ARB_multitexture */
  1008.  
  1009.       /* Pixel transfer */
  1010.       ctx->Pack.Alignment = 4;
  1011.       ctx->Pack.RowLength = 0;
  1012.       ctx->Pack.ImageHeight = 0;
  1013.       ctx->Pack.SkipPixels = 0;
  1014.       ctx->Pack.SkipRows = 0;
  1015.       ctx->Pack.SkipImages = 0;
  1016.       ctx->Pack.SwapBytes = GL_FALSE;
  1017.       ctx->Pack.LsbFirst = GL_FALSE;
  1018.       ctx->Unpack.Alignment = 4;
  1019.       ctx->Unpack.RowLength = 0;
  1020.       ctx->Unpack.ImageHeight = 0;
  1021.       ctx->Unpack.SkipPixels = 0;
  1022.       ctx->Unpack.SkipRows = 0;
  1023.       ctx->Unpack.SkipImages = 0;
  1024.       ctx->Unpack.SwapBytes = GL_FALSE;
  1025.       ctx->Unpack.LsbFirst = GL_FALSE;
  1026.  
  1027.       /* Feedback */
  1028.       ctx->Feedback.Type = GL_2D;   /* TODO: verify */
  1029.       ctx->Feedback.Buffer = NULL;
  1030.       ctx->Feedback.BufferSize = 0;
  1031.       ctx->Feedback.Count = 0;
  1032.  
  1033.       /* Selection/picking */
  1034.       ctx->Select.Buffer = NULL;
  1035.       ctx->Select.BufferSize = 0;
  1036.       ctx->Select.BufferCount = 0;
  1037.       ctx->Select.Hits = 0;
  1038.       ctx->Select.NameStackDepth = 0;
  1039.  
  1040.       /* Optimized Accum buffer */
  1041.       ctx->IntegerAccumMode = GL_TRUE;
  1042.       ctx->IntegerAccumScaler = 0.0;
  1043.  
  1044.       /* Renderer and client attribute stacks */
  1045.       ctx->AttribStackDepth = 0;
  1046.       ctx->ClientAttribStackDepth = 0;
  1047.  
  1048.       /*** Miscellaneous ***/
  1049.       ctx->NewState = NEW_ALL;
  1050.       ctx->RenderMode = GL_RENDER;
  1051.       ctx->StippleCounter = 0;
  1052.       ctx->NeedNormals = GL_FALSE;
  1053.       ctx->DoViewportMapping = GL_TRUE;
  1054.  
  1055.       ctx->NeedEyeCoords = GL_FALSE;
  1056.       ctx->NeedEyeNormals = GL_FALSE;
  1057.       ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
  1058.  
  1059.       /* Display list */
  1060.       ctx->CallDepth = 0;
  1061.       ctx->ExecuteFlag = GL_TRUE;
  1062.       ctx->CompileFlag = GL_FALSE;
  1063.       ctx->CurrentListPtr = NULL;
  1064.       ctx->CurrentBlock = NULL;
  1065.       ctx->CurrentListNum = 0;
  1066.       ctx->CurrentPos = 0;
  1067.  
  1068.       ctx->ErrorValue = (GLenum) GL_NO_ERROR;
  1069.  
  1070.       ctx->CatchSignals = GL_TRUE;
  1071.  
  1072.       /* For debug/development only */
  1073.       ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
  1074.       ctx->FirstTimeCurrent = GL_TRUE;
  1075.  
  1076.       /* Dither disable */
  1077.       ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
  1078.       if (ctx->NoDither) {
  1079.          if (getenv("MESA_DEBUG")) {
  1080.             fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
  1081.          }
  1082.          ctx->Color.DitherFlag = GL_FALSE;
  1083.       }
  1084.    }
  1085. }
  1086.  
  1087.  
  1088.  
  1089. /*
  1090.  * Allocate a new GLvisual object.
  1091.  * Input:  rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
  1092.  *         alphaFlag - alloc software alpha buffers?
  1093.  *         dbFlag - double buffering?
  1094.  *         stereoFlag - stereo buffer?
  1095.  *         depthFits - requested minimum bits per depth buffer value
  1096.  *         stencilFits - requested minimum bits per stencil buffer value
  1097.  *         accumFits - requested minimum bits per accum buffer component
  1098.  *         indexFits - number of bits per pixel if rgbFlag==GL_FALSE
  1099.  *         red/green/blue/alphaFits - number of bits per color component
  1100.  *                                     in frame buffer for RGB(A) mode.
  1101.  * Return:  pointer to new GLvisual or NULL if requested parameters can't
  1102.  *          be met.
  1103.  */
  1104. GLvisual *gl_create_visual( GLboolean rgbFlag,
  1105.                             GLboolean alphaFlag,
  1106.                             GLboolean dbFlag,
  1107.                             GLboolean stereoFlag,
  1108.                             GLint depthBits,
  1109.                             GLint stencilBits,
  1110.                             GLint accumBits,
  1111.                             GLint indexBits,
  1112.                             GLint redBits,
  1113.                             GLint greenBits,
  1114.                             GLint blueBits,
  1115.                             GLint alphaBits )
  1116. {
  1117.    GLvisual *vis;
  1118.  
  1119.    if (depthBits > (GLint) (8*sizeof(GLdepth))) {
  1120.       /* can't meet depth buffer requirements */
  1121.       return NULL;
  1122.    }
  1123.    if (stencilBits > (GLint) (8*sizeof(GLstencil))) {
  1124.       /* can't meet stencil buffer requirements */
  1125.       return NULL;
  1126.    }
  1127.    if (accumBits > (GLint) (8*sizeof(GLaccum))) {
  1128.       /* can't meet accum buffer requirements */
  1129.       return NULL;
  1130.    }
  1131.  
  1132.    vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
  1133.    if (!vis) {
  1134.       return NULL;
  1135.    }
  1136.  
  1137.    vis->RGBAflag   = rgbFlag;
  1138.    vis->DBflag     = dbFlag;
  1139.    vis->StereoFlag = stereoFlag;
  1140.    vis->RedBits    = redBits;
  1141.    vis->GreenBits  = greenBits;
  1142.    vis->BlueBits   = blueBits;
  1143.    vis->AlphaBits  = alphaFlag ? 8*sizeof(GLubyte) : alphaBits;
  1144.  
  1145.    vis->IndexBits   = indexBits;
  1146.    vis->DepthBits   = (depthBits>0) ? 8*sizeof(GLdepth) : 0;
  1147.    vis->AccumBits   = (accumBits>0) ? 8*sizeof(GLaccum) : 0;
  1148.    vis->StencilBits = (stencilBits>0) ? 8*sizeof(GLstencil) : 0;
  1149.  
  1150.    vis->SoftwareAlpha = alphaFlag;
  1151.  
  1152.    return vis;
  1153. }
  1154.  
  1155.  
  1156.  
  1157. void gl_destroy_visual( GLvisual *vis )
  1158. {
  1159.    FREE( vis );
  1160. }
  1161.  
  1162.  
  1163.  
  1164. /*
  1165.  * Allocate the proxy textures.  If we run out of memory part way through
  1166.  * the allocations clean up and return GL_FALSE.
  1167.  * Return:  GL_TRUE=success, GL_FALSE=failure
  1168.  */
  1169. static GLboolean alloc_proxy_textures( GLcontext *ctx )
  1170. {
  1171.    GLboolean out_of_memory;
  1172.    GLint i;
  1173.  
  1174.    ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
  1175.    if (!ctx->Texture.Proxy1D) {
  1176.       return GL_FALSE;
  1177.    }
  1178.  
  1179.    ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
  1180.    if (!ctx->Texture.Proxy2D) {
  1181.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1182.       return GL_FALSE;
  1183.    }
  1184.  
  1185.    ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
  1186.    if (!ctx->Texture.Proxy3D) {
  1187.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1188.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1189.       return GL_FALSE;
  1190.    }
  1191.  
  1192.    out_of_memory = GL_FALSE;
  1193.    for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1194.       ctx->Texture.Proxy1D->Image[i] = gl_alloc_texture_image();
  1195.       ctx->Texture.Proxy2D->Image[i] = gl_alloc_texture_image();
  1196.       ctx->Texture.Proxy3D->Image[i] = gl_alloc_texture_image();
  1197.       if (!ctx->Texture.Proxy1D->Image[i]
  1198.           || !ctx->Texture.Proxy2D->Image[i]
  1199.           || !ctx->Texture.Proxy3D->Image[i]) {
  1200.          out_of_memory = GL_TRUE;
  1201.       }
  1202.    }
  1203.    if (out_of_memory) {
  1204.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1205.          if (ctx->Texture.Proxy1D->Image[i]) {
  1206.             gl_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
  1207.          }
  1208.          if (ctx->Texture.Proxy2D->Image[i]) {
  1209.             gl_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
  1210.          }
  1211.          if (ctx->Texture.Proxy3D->Image[i]) {
  1212.             gl_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
  1213.          }
  1214.       }
  1215.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1216.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1217.       gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
  1218.       return GL_FALSE;
  1219.    }
  1220.    else {
  1221.       return GL_TRUE;
  1222.    }
  1223. }
  1224.  
  1225.  
  1226.  
  1227. /*
  1228.  * Allocate and initialize a GLcontext structure.
  1229.  * Input:  visual - a GLvisual pointer
  1230.  *         sharelist - another context to share display lists with or NULL
  1231.  *         driver_ctx - pointer to device driver's context state struct
  1232.  * Return:  pointer to a new gl_context struct or NULL if error.
  1233.  */
  1234. GLcontext *gl_create_context( GLvisual *visual,
  1235.                               GLcontext *share_list,
  1236.                               void *driver_ctx,
  1237.                               GLboolean direct )
  1238. {
  1239.    GLcontext *ctx;
  1240.    GLuint i;
  1241.  
  1242.    (void) direct;  /* not used */
  1243.  
  1244.    /* do some implementation tests */
  1245.    assert( sizeof(GLbyte) == 1 );
  1246.    assert( sizeof(GLshort) >= 2 );
  1247.    assert( sizeof(GLint) >= 4 );
  1248.    assert( sizeof(GLubyte) == 1 );
  1249.    assert( sizeof(GLushort) >= 2 );
  1250.    assert( sizeof(GLuint) >= 4 );
  1251.  
  1252.    /* misc one-time initializations */
  1253.    one_time_init();
  1254.  
  1255.    ctx = (GLcontext *) CALLOC( sizeof(GLcontext) );
  1256.    if (!ctx) {
  1257.       return NULL;
  1258.    }
  1259.  
  1260.    ctx->DriverCtx = driver_ctx;
  1261.    ctx->Visual = visual;
  1262.    ctx->Buffer = NULL;
  1263.  
  1264.    ctx->VB = gl_vb_create_for_immediate( ctx );
  1265.    if (!ctx->VB) {
  1266.       FREE( ctx );
  1267.       return NULL;
  1268.    }
  1269.    ctx->input = ctx->VB->IM;
  1270.  
  1271.    ctx->PB = gl_alloc_pb();
  1272.    if (!ctx->PB) {
  1273.       FREE( ctx->VB );
  1274.       FREE( ctx );
  1275.       return NULL;
  1276.    }
  1277.  
  1278.    if (share_list) {
  1279.       /* share the group of display lists of another context */
  1280.       ctx->Shared = share_list->Shared;
  1281.    }
  1282.    else {
  1283.       /* allocate new group of display lists */
  1284.       ctx->Shared = alloc_shared_state();
  1285.       if (!ctx->Shared) {
  1286.          FREE(ctx->VB);
  1287.          FREE(ctx->PB);
  1288.          FREE(ctx);
  1289.          return NULL;
  1290.       }
  1291.    }
  1292.    ctx->Shared->RefCount++;
  1293.  
  1294.    initialize_context( ctx );
  1295.    gl_reset_vb( ctx->VB );
  1296.    gl_reset_input( ctx );
  1297.  
  1298.  
  1299.    ctx->ShineTabList = MALLOC_STRUCT( gl_shine_tab );
  1300.    make_empty_list( ctx->ShineTabList );
  1301.  
  1302.    for (i = 0 ; i < 10 ; i++) {
  1303.       struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
  1304.       s->shininess = -1;
  1305.       s->refcount = 0;
  1306.       insert_at_tail( ctx->ShineTabList, s );
  1307.    }
  1308.  
  1309.    for (i = 0 ; i < 4 ; i++) {
  1310.       ctx->ShineTable[i] = ctx->ShineTabList->prev;
  1311.       ctx->ShineTable[i]->refcount++;
  1312.    }
  1313.  
  1314.    if (visual->DBflag) {
  1315.       ctx->Color.DrawBuffer = GL_BACK;
  1316.       ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
  1317.       ctx->Color.DrawDestMask = BACK_LEFT_BIT;
  1318.       ctx->Pixel.ReadBuffer = GL_BACK;
  1319.       ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
  1320.    }
  1321.    else {
  1322.       ctx->Color.DrawBuffer = GL_FRONT;
  1323.       ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
  1324.       ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
  1325.       ctx->Pixel.ReadBuffer = GL_FRONT;
  1326.       ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
  1327.    }
  1328.  
  1329.    
  1330.    /* Fill in some driver defaults now.
  1331.     */
  1332.    ctx->Driver.AllocDepthBuffer = gl_alloc_depth_buffer;
  1333.    ctx->Driver.ReadDepthSpanFloat = gl_read_depth_span_float;
  1334.    ctx->Driver.ReadDepthSpanInt = gl_read_depth_span_int;
  1335.  
  1336.    
  1337. #ifdef PROFILE
  1338.    init_timings( ctx );
  1339. #endif
  1340.  
  1341. #ifdef GL_VERSION_1_1
  1342.    if (!alloc_proxy_textures(ctx)) {
  1343.       free_shared_state(ctx, ctx->Shared);
  1344.       FREE(ctx->VB);
  1345.       FREE(ctx->PB);
  1346.       FREE(ctx);
  1347.       return NULL;
  1348.    }
  1349. #endif
  1350.  
  1351.    gl_init_api_function_pointers( ctx );
  1352.    ctx->API = ctx->Exec;   /* GL_EXECUTE is default */
  1353.  
  1354.    return ctx;
  1355. }
  1356.  
  1357. /* Just reads the config files...
  1358.  */
  1359. void gl_context_initialize( GLcontext *ctx )
  1360. {
  1361.    gl_read_config_file( ctx );
  1362. }
  1363.  
  1364.  
  1365.  
  1366.  
  1367. /*
  1368.  * Destroy a gl_context structure.
  1369.  */
  1370. void gl_destroy_context( GLcontext *ctx )
  1371. {
  1372.    if (ctx) {
  1373.  
  1374.       GLuint i;
  1375.       struct gl_shine_tab *s, *tmps;
  1376.  
  1377. #ifdef PROFILE
  1378.       if (getenv("MESA_PROFILE")) {
  1379.          print_timings( ctx );
  1380.       }
  1381. #endif
  1382.  
  1383.       gl_matrix_dtr( &ctx->ModelView );
  1384.       for (i = 0 ; i < MAX_MODELVIEW_STACK_DEPTH ; i++) {
  1385.      gl_matrix_dtr( &ctx->ModelViewStack[i] );
  1386.       }
  1387.       gl_matrix_dtr( &ctx->ProjectionMatrix );
  1388.       for (i = 0 ; i < MAX_PROJECTION_STACK_DEPTH ; i++) {
  1389.      gl_matrix_dtr( &ctx->ProjectionStack[i] );
  1390.       }
  1391.  
  1392.       FREE( ctx->PB );
  1393.  
  1394.       if(ctx->input != ctx->VB->IM)
  1395.          gl_immediate_free( ctx->input );
  1396.  
  1397.       gl_vb_free( ctx->VB );
  1398.  
  1399.       ctx->Shared->RefCount--;
  1400.       assert(ctx->Shared->RefCount>=0);
  1401.       if (ctx->Shared->RefCount==0) {
  1402.      /* free shared state */
  1403.      free_shared_state( ctx, ctx->Shared );
  1404.       }
  1405.  
  1406.       foreach_s( s, tmps, ctx->ShineTabList ) {
  1407.      FREE( s );
  1408.       }
  1409.       FREE( ctx->ShineTabList );
  1410.  
  1411.       /* Free proxy texture objects */
  1412.       gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
  1413.       gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
  1414.       gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
  1415.  
  1416.       /* Free evaluator data */
  1417.       if (ctx->EvalMap.Map1Vertex3.Points)
  1418.          FREE( ctx->EvalMap.Map1Vertex3.Points );
  1419.       if (ctx->EvalMap.Map1Vertex4.Points)
  1420.          FREE( ctx->EvalMap.Map1Vertex4.Points );
  1421.       if (ctx->EvalMap.Map1Index.Points)
  1422.          FREE( ctx->EvalMap.Map1Index.Points );
  1423.       if (ctx->EvalMap.Map1Color4.Points)
  1424.          FREE( ctx->EvalMap.Map1Color4.Points );
  1425.       if (ctx->EvalMap.Map1Normal.Points)
  1426.          FREE( ctx->EvalMap.Map1Normal.Points );
  1427.       if (ctx->EvalMap.Map1Texture1.Points)
  1428.          FREE( ctx->EvalMap.Map1Texture1.Points );
  1429.       if (ctx->EvalMap.Map1Texture2.Points)
  1430.          FREE( ctx->EvalMap.Map1Texture2.Points );
  1431.       if (ctx->EvalMap.Map1Texture3.Points)
  1432.          FREE( ctx->EvalMap.Map1Texture3.Points );
  1433.       if (ctx->EvalMap.Map1Texture4.Points)
  1434.          FREE( ctx->EvalMap.Map1Texture4.Points );
  1435.  
  1436.       if (ctx->EvalMap.Map2Vertex3.Points)
  1437.          FREE( ctx->EvalMap.Map2Vertex3.Points );
  1438.       if (ctx->EvalMap.Map2Vertex4.Points)
  1439.          FREE( ctx->EvalMap.Map2Vertex4.Points );
  1440.       if (ctx->EvalMap.Map2Index.Points)
  1441.          FREE( ctx->EvalMap.Map2Index.Points );
  1442.       if (ctx->EvalMap.Map2Color4.Points)
  1443.          FREE( ctx->EvalMap.Map2Color4.Points );
  1444.       if (ctx->EvalMap.Map2Normal.Points)
  1445.          FREE( ctx->EvalMap.Map2Normal.Points );
  1446.       if (ctx->EvalMap.Map2Texture1.Points)
  1447.          FREE( ctx->EvalMap.Map2Texture1.Points );
  1448.       if (ctx->EvalMap.Map2Texture2.Points)
  1449.          FREE( ctx->EvalMap.Map2Texture2.Points );
  1450.       if (ctx->EvalMap.Map2Texture3.Points)
  1451.          FREE( ctx->EvalMap.Map2Texture3.Points );
  1452.       if (ctx->EvalMap.Map2Texture4.Points)
  1453.          FREE( ctx->EvalMap.Map2Texture4.Points );
  1454.  
  1455.       /* Free cache of immediate buffers. */
  1456.       while (ctx->nr_im_queued-- > 0) {
  1457.          struct immediate * next = ctx->freed_im_queue->next;
  1458.          FREE( ctx->freed_im_queue );
  1459.          ctx->freed_im_queue = next;
  1460.       }
  1461.       gl_extensions_dtr(ctx);
  1462.  
  1463.       FREE( (void *) ctx );
  1464.  
  1465. #ifndef THREADS
  1466.       if (ctx==CC) {
  1467.          CC = NULL;
  1468.      CURRENT_INPUT = NULL;
  1469.       }
  1470. #endif
  1471.  
  1472.    }
  1473. }
  1474.  
  1475.  
  1476.  
  1477. /*
  1478.  * Create a new framebuffer.  A GLframebuffer is a struct which
  1479.  * encapsulates the depth, stencil and accum buffers and related
  1480.  * parameters.
  1481.  * Input:  visual - a GLvisual pointer
  1482.  * Return:  pointer to new GLframebuffer struct or NULL if error.
  1483.  */
  1484. GLframebuffer *gl_create_framebuffer( GLvisual *visual )
  1485. {
  1486.    GLframebuffer *buffer;
  1487.  
  1488.    buffer = (GLframebuffer *) CALLOC( sizeof(GLframebuffer) );
  1489.    if (!buffer) {
  1490.       return NULL;
  1491.    }
  1492.  
  1493.    buffer->Visual = visual;
  1494.  
  1495.    return buffer;
  1496. }
  1497.  
  1498.  
  1499.  
  1500. /*
  1501.  * Free a framebuffer struct and its buffers.
  1502.  */
  1503. void gl_destroy_framebuffer( GLframebuffer *buffer )
  1504. {
  1505.    if (buffer) {
  1506.       if (buffer->Depth) {
  1507.          FREE( buffer->Depth );
  1508.       }
  1509.       if (buffer->Accum) {
  1510.          FREE( buffer->Accum );
  1511.       }
  1512.       if (buffer->Stencil) {
  1513.          FREE( buffer->Stencil );
  1514.       }
  1515.       if (buffer->FrontLeftAlpha) {
  1516.          FREE( buffer->FrontLeftAlpha );
  1517.       }
  1518.       if (buffer->BackLeftAlpha) {
  1519.          FREE( buffer->BackLeftAlpha );
  1520.       }
  1521.       if (buffer->FrontRightAlpha) {
  1522.          FREE( buffer->FrontRightAlpha );
  1523.       }
  1524.       if (buffer->BackRightAlpha) {
  1525.          FREE( buffer->BackRightAlpha );
  1526.       }
  1527.       FREE(buffer);
  1528.    }
  1529. }
  1530.  
  1531.  
  1532.  
  1533. /*
  1534.  * Set the current context, binding the given frame buffer to the context.
  1535.  */
  1536. void gl_make_current( GLcontext *ctx, GLframebuffer *buffer )
  1537. {
  1538.    GET_CONTEXT;
  1539.  
  1540.    /* Flush the old context
  1541.     */
  1542.    if (CC) {
  1543.       ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(CC, "gl_make_current");
  1544.    }
  1545.  
  1546. #ifdef THREADS
  1547.    /* TODO: unbind old buffer from context? */
  1548.    set_thread_context( ctx );
  1549. #else
  1550.    if (CC && CC->Buffer) {
  1551.       /* unbind frame buffer from context */
  1552.       CC->Buffer = NULL;
  1553.    }
  1554.    CC = ctx;
  1555.    if (ctx) {
  1556.       SET_IMMEDIATE(ctx, ctx->input);
  1557.    }
  1558. #endif
  1559.  
  1560.    if (MESA_VERBOSE) fprintf(stderr, "gl_make_current()\n");
  1561.  
  1562.    if (ctx && buffer) {
  1563.       /* TODO: check if ctx and buffer's visual match??? */
  1564.       ctx->Buffer = buffer;      /* Bind the frame buffer to the context */
  1565.       ctx->NewState = NEW_ALL;   /* just to be safe */
  1566.       gl_update_state( ctx );
  1567.    }
  1568.  
  1569.    /* We can use this to help debug user's problems.  Tell the to set
  1570.     * the MESA_INFO env variable before running their app.  Then the
  1571.     * first time each context is made current we'll print some useful
  1572.     * information.
  1573.     */
  1574.    if (ctx && ctx->FirstTimeCurrent) {
  1575.       if (getenv("MESA_INFO")) {
  1576.          fprintf(stderr, "Mesa GL_VERSION = %s\n", (char *) gl_GetString(ctx, GL_VERSION));
  1577.          fprintf(stderr, "Mesa GL_RENDERER = %s\n", (char *) gl_GetString(ctx, GL_RENDERER));
  1578.          fprintf(stderr, "Mesa GL_VENDOR = %s\n", (char *) gl_GetString(ctx, GL_VENDOR));
  1579.          fprintf(stderr, "Mesa GL_EXTENSIONS = %s\n", (char *) gl_GetString(ctx, GL_EXTENSIONS));
  1580.       }
  1581.       ctx->FirstTimeCurrent = GL_FALSE;
  1582.    }
  1583. }
  1584.  
  1585.  
  1586. /*
  1587.  * Return current context handle.
  1588.  */
  1589. GLcontext *gl_get_current_context( void )
  1590. {
  1591. #ifdef THREADS
  1592.    return gl_get_thread_context();
  1593. #else
  1594.    return CC;
  1595. #endif
  1596. }
  1597.  
  1598.  
  1599.  
  1600. /*
  1601.  * Copy attribute groups from one context to another.
  1602.  * Input:  src - source context
  1603.  *         dst - destination context
  1604.  *         mask - bitwise OR of GL_*_BIT flags
  1605.  */
  1606. void gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
  1607. {
  1608.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1609.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  1610.    }
  1611.    if (mask & GL_COLOR_BUFFER_BIT) {
  1612.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  1613.    }
  1614.    if (mask & GL_CURRENT_BIT) {
  1615.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  1616.    }
  1617.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1618.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  1619.    }
  1620.    if (mask & GL_ENABLE_BIT) {
  1621.       /* no op */
  1622.    }
  1623.    if (mask & GL_EVAL_BIT) {
  1624.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  1625.    }
  1626.    if (mask & GL_FOG_BIT) {
  1627.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  1628.    }
  1629.    if (mask & GL_HINT_BIT) {
  1630.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  1631.    }
  1632.    if (mask & GL_LIGHTING_BIT) {
  1633.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  1634. /*       gl_reinit_light_attrib( &dst->Light ); */
  1635.    }
  1636.    if (mask & GL_LINE_BIT) {
  1637.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  1638.    }
  1639.    if (mask & GL_LIST_BIT) {
  1640.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  1641.    }
  1642.    if (mask & GL_PIXEL_MODE_BIT) {
  1643.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  1644.    }
  1645.    if (mask & GL_POINT_BIT) {
  1646.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  1647.    }
  1648.    if (mask & GL_POLYGON_BIT) {
  1649.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  1650.    }
  1651.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1652.       /* Use loop instead of MEMCPY due to problem with Portland Group's
  1653.        * C compiler.  Reported by John Stone.
  1654.        */
  1655.       int i;
  1656.       for (i=0;i<32;i++) {
  1657.          dst->PolygonStipple[i] = src->PolygonStipple[i];
  1658.       }
  1659.    }
  1660.    if (mask & GL_SCISSOR_BIT) {
  1661.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  1662.    }
  1663.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1664.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  1665.    }
  1666.    if (mask & GL_TEXTURE_BIT) {
  1667.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  1668.    }
  1669.    if (mask & GL_TRANSFORM_BIT) {
  1670.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  1671.    }
  1672.    if (mask & GL_VIEWPORT_BIT) {
  1673.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  1674.    }
  1675. }
  1676.  
  1677.  
  1678.  
  1679. /*
  1680.  * Someday a GLS library or OpenGL-like debugger may call this function
  1681.  * to register it's own set of API entry points.
  1682.  * Input: ctx - the context to set API pointers for
  1683.  *        api - if NULL, restore original API pointers
  1684.  *              else, set API function table to this table.
  1685.  */
  1686. void gl_set_api_table( GLcontext *ctx, const struct gl_api_table *api )
  1687. {
  1688.    if (api) {
  1689.       MEMCPY( &ctx->API, api, sizeof(struct gl_api_table) );
  1690.    }
  1691.    else {
  1692.       MEMCPY( &ctx->API, &ctx->Exec, sizeof(struct gl_api_table) );
  1693.    }
  1694. }
  1695.  
  1696.  
  1697.  
  1698.  
  1699. /**********************************************************************/
  1700. /*****                Miscellaneous functions                     *****/
  1701. /**********************************************************************/
  1702.  
  1703.  
  1704. /*
  1705.  * This function is called when the Mesa user has stumbled into a code
  1706.  * path which may not be implemented fully or correctly.
  1707.  */
  1708. void gl_problem( const GLcontext *ctx, const char *s )
  1709. {
  1710.    fprintf( stderr, "Mesa implementation error: %s\n", s );
  1711.    fprintf( stderr, "Report to mesa-bugs@mesa3d.org\n" );
  1712.    (void) ctx;
  1713. }
  1714.  
  1715.  
  1716.  
  1717. /*
  1718.  * This is called to inform the user that he or she has tried to do
  1719.  * something illogical or if there's likely a bug in their program
  1720.  * (like enabled depth testing without a depth buffer).
  1721.  */
  1722. void gl_warning( const GLcontext *ctx, const char *s )
  1723. {
  1724.    GLboolean debug;
  1725. #ifdef DEBUG
  1726.    debug = GL_TRUE;
  1727. #else
  1728.    if (getenv("MESA_DEBUG")) {
  1729.       debug = GL_TRUE;
  1730.    }
  1731.    else {
  1732.       debug = GL_FALSE;
  1733.    }
  1734. #endif
  1735.    if (debug) {
  1736.       fprintf( stderr, "Mesa warning: %s\n", s );
  1737.    }
  1738.    (void) ctx;
  1739. }
  1740.  
  1741.  
  1742.  
  1743. void gl_compile_error( GLcontext *ctx, GLenum error, const char *s )
  1744. {
  1745.    if (ctx->CompileFlag)
  1746.       gl_save_error( ctx, error, s );
  1747.  
  1748.    if (ctx->ExecuteFlag)
  1749.       gl_error( ctx, error, s );
  1750. }
  1751.  
  1752.  
  1753. /*
  1754.  * This is Mesa's error handler.  Normally, all that's done is the updating
  1755.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  1756.  * environment variable "MESA_DEBUG" is defined then a real error message
  1757.  * is printed to stderr.
  1758.  * Input:  error - the error value
  1759.  *         s - a diagnostic string
  1760.  */
  1761. void gl_error( GLcontext *ctx, GLenum error, const char *s )
  1762. {
  1763.    GLboolean debug;
  1764.  
  1765. #ifdef DEBUG
  1766.    debug = GL_TRUE;
  1767. #else
  1768.    if (getenv("MESA_DEBUG")) {
  1769.       debug = GL_TRUE;
  1770.    }
  1771.    else {
  1772.       debug = GL_FALSE;
  1773.    }
  1774. #endif
  1775.  
  1776.    if (debug) {
  1777.       char errstr[1000];
  1778.  
  1779.       switch (error) {
  1780.      case GL_NO_ERROR:
  1781.         strcpy( errstr, "GL_NO_ERROR" );
  1782.         break;
  1783.      case GL_INVALID_VALUE:
  1784.         strcpy( errstr, "GL_INVALID_VALUE" );
  1785.         break;
  1786.      case GL_INVALID_ENUM:
  1787.         strcpy( errstr, "GL_INVALID_ENUM" );
  1788.         break;
  1789.      case GL_INVALID_OPERATION:
  1790.         strcpy( errstr, "GL_INVALID_OPERATION" );
  1791.         break;
  1792.      case GL_STACK_OVERFLOW:
  1793.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  1794.         break;
  1795.      case GL_STACK_UNDERFLOW:
  1796.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  1797.         break;
  1798.      case GL_OUT_OF_MEMORY:
  1799.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  1800.         break;
  1801.      default:
  1802.         strcpy( errstr, "unknown" );
  1803.         break;
  1804.       }
  1805.       fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
  1806.    }
  1807.  
  1808.    if (ctx->ErrorValue==GL_NO_ERROR) {
  1809.       ctx->ErrorValue = error;
  1810.    }
  1811.  
  1812.    /* Call device driver's error handler, if any.  This is used on the Mac. */
  1813.    if (ctx->Driver.Error) {
  1814.       (*ctx->Driver.Error)( ctx );
  1815.    }
  1816. }
  1817.  
  1818.  
  1819.  
  1820. /*
  1821.  * Execute a glGetError command
  1822.  */
  1823. GLenum gl_GetError( GLcontext *ctx )
  1824. {
  1825.    GLenum e = ctx->ErrorValue;
  1826.  
  1827.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL( ctx, "glGetError", (GLenum) 0);
  1828.  
  1829.    if (MESA_VERBOSE & VERBOSE_API)
  1830.       fprintf(stderr, "glGetError <-- %s\n", gl_lookup_enum_by_nr(e));
  1831.  
  1832.    ctx->ErrorValue = (GLenum) GL_NO_ERROR;
  1833.    return e;
  1834. }
  1835.  
  1836.  
  1837.  
  1838. void gl_ResizeBuffersMESA( GLcontext *ctx )
  1839. {
  1840.    GLuint buf_width, buf_height;
  1841.  
  1842.    if (MESA_VERBOSE & VERBOSE_API)
  1843.       fprintf(stderr, "glResizeBuffersMESA\n");
  1844.  
  1845.    /* ask device driver for size of output buffer */
  1846.    (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
  1847.  
  1848.    /* see if size of device driver's color buffer (window) has changed */
  1849.    if (ctx->Buffer->Width == (GLint) buf_width &&
  1850.        ctx->Buffer->Height == (GLint) buf_height)
  1851.       return;
  1852.  
  1853.    ctx->NewState |= NEW_RASTER_OPS;  /* to update scissor / window bounds */
  1854.  
  1855.    /* save buffer size */
  1856.    ctx->Buffer->Width = buf_width;
  1857.    ctx->Buffer->Height = buf_height;
  1858.  
  1859.    /* Reallocate other buffers if needed. */
  1860.    if (ctx->Visual->DepthBits>0) {
  1861.       /* reallocate depth buffer */
  1862.       (*ctx->Driver.AllocDepthBuffer)( ctx );
  1863.    }
  1864.    if (ctx->Visual->StencilBits>0) {
  1865.       /* reallocate stencil buffer */
  1866.       gl_alloc_stencil_buffer( ctx );
  1867.    }
  1868.    if (ctx->Visual->AccumBits>0) {
  1869.       /* reallocate accum buffer */
  1870.       gl_alloc_accum_buffer( ctx );
  1871.    }
  1872.    if (ctx->Visual->SoftwareAlpha) {
  1873.       gl_alloc_alpha_buffers( ctx );
  1874.    }
  1875. }
  1876.  
  1877.  
  1878.  
  1879.  
  1880. /**********************************************************************/
  1881. /*****                   State update logic                       *****/
  1882. /**********************************************************************/
  1883.  
  1884.  
  1885. /*
  1886.  * Since the device driver may or may not support pixel logic ops we
  1887.  * have to make some extensive tests to determine whether or not
  1888.  * software-implemented logic operations have to be used.
  1889.  */
  1890. static void update_pixel_logic( GLcontext *ctx )
  1891. {
  1892.    if (ctx->Visual->RGBAflag) {
  1893.       /* RGBA mode blending w/ Logic Op */
  1894.       if (ctx->Color.ColorLogicOpEnabled) {
  1895.      if (ctx->Driver.LogicOp
  1896.              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1897.         /* Device driver can do logic, don't have to do it in software */
  1898.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1899.      }
  1900.      else {
  1901.         /* Device driver can't do logic op so we do it in software */
  1902.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1903.      }
  1904.       }
  1905.       else {
  1906.      /* no logic op */
  1907.      if (ctx->Driver.LogicOp) {
  1908.             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1909.          }
  1910.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1911.       }
  1912.    }
  1913.    else {
  1914.       /* CI mode Logic Op */
  1915.       if (ctx->Color.IndexLogicOpEnabled) {
  1916.      if (ctx->Driver.LogicOp
  1917.              && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1918.         /* Device driver can do logic, don't have to do it in software */
  1919.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1920.      }
  1921.      else {
  1922.         /* Device driver can't do logic op so we do it in software */
  1923.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1924.      }
  1925.       }
  1926.       else {
  1927.      /* no logic op */
  1928.      if (ctx->Driver.LogicOp) {
  1929.             (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1930.          }
  1931.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1932.       }
  1933.    }
  1934. }
  1935.  
  1936.  
  1937.  
  1938. /*
  1939.  * Check if software implemented RGBA or Color Index masking is needed.
  1940.  */
  1941. static void update_pixel_masking( GLcontext *ctx )
  1942. {
  1943.    if (ctx->Visual->RGBAflag) {
  1944.       GLuint *colorMask = (GLuint *) ctx->Color.ColorMask;
  1945.       if (*colorMask == 0xffffffff) {
  1946.          /* disable masking */
  1947.          if (ctx->Driver.ColorMask) {
  1948.             (void) (*ctx->Driver.ColorMask)( ctx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  1949.          }
  1950.          ctx->Color.SWmasking = GL_FALSE;
  1951.       }
  1952.       else {
  1953.          /* Ask driver to do color masking, if it can't then
  1954.           * do it in software
  1955.           */
  1956.          GLboolean red   = ctx->Color.ColorMask[RCOMP] ? GL_TRUE : GL_FALSE;
  1957.          GLboolean green = ctx->Color.ColorMask[GCOMP] ? GL_TRUE : GL_FALSE;
  1958.          GLboolean blue  = ctx->Color.ColorMask[BCOMP] ? GL_TRUE : GL_FALSE;
  1959.          GLboolean alpha = ctx->Color.ColorMask[ACOMP] ? GL_TRUE : GL_FALSE;
  1960.          if (ctx->Driver.ColorMask
  1961.              && (*ctx->Driver.ColorMask)( ctx, red, green, blue, alpha )) {
  1962.             ctx->Color.SWmasking = GL_FALSE;
  1963.          }
  1964.          else {
  1965.             ctx->Color.SWmasking = GL_TRUE;
  1966.          }
  1967.       }
  1968.    }
  1969.    else {
  1970.       if (ctx->Color.IndexMask==0xffffffff) {
  1971.          /* disable masking */
  1972.          if (ctx->Driver.IndexMask) {
  1973.             (void) (*ctx->Driver.IndexMask)( ctx, 0xffffffff );
  1974.          }
  1975.          ctx->Color.SWmasking = GL_FALSE;
  1976.       }
  1977.       else {
  1978.          /* Ask driver to do index masking, if it can't then
  1979.           * do it in software
  1980.           */
  1981.          if (ctx->Driver.IndexMask
  1982.              && (*ctx->Driver.IndexMask)( ctx, ctx->Color.IndexMask )) {
  1983.             ctx->Color.SWmasking = GL_FALSE;
  1984.          }
  1985.          else {
  1986.             ctx->Color.SWmasking = GL_TRUE;
  1987.          }
  1988.       }
  1989.    }
  1990. }
  1991.  
  1992.  
  1993. static void update_fog_mode( GLcontext *ctx )
  1994. {
  1995.    int old_mode = ctx->FogMode;
  1996.    ctx->FogMode = FOG_NONE;
  1997.  
  1998.    if (ctx->Fog.Enabled) {
  1999.       ctx->FogMode = FOG_VERTEX;
  2000.  
  2001.       if (ctx->Texture.Enabled || ctx->Hint.Fog == GL_NICEST)
  2002.      ctx->FogMode = FOG_FRAGMENT;
  2003.  
  2004.       if ( ctx->Driver.GetParameteri && 
  2005.        ctx->Driver.GetParameteri( ctx, DD_HAVE_HARDWARE_FOG ) )
  2006.      ctx->FogMode = FOG_FRAGMENT;
  2007.    }
  2008.    
  2009.    if (old_mode != ctx->FogMode)
  2010.       ctx->NewState |= NEW_FOG;
  2011. }
  2012.  
  2013.  
  2014. /*
  2015.  * Recompute the value of ctx->RasterMask, etc. according to
  2016.  * the current context.
  2017.  */
  2018. static void update_rasterflags( GLcontext *ctx )
  2019. {
  2020.    ctx->RasterMask = 0;
  2021.  
  2022.    if (ctx->Color.AlphaEnabled)        ctx->RasterMask |= ALPHATEST_BIT;
  2023.    if (ctx->Color.BlendEnabled)        ctx->RasterMask |= BLEND_BIT;
  2024.    if (ctx->Depth.Test)            ctx->RasterMask |= DEPTH_BIT;
  2025.    if (ctx->FogMode==FOG_FRAGMENT)    ctx->RasterMask |= FOG_BIT;
  2026.    if (ctx->Color.SWLogicOpEnabled)    ctx->RasterMask |= LOGIC_OP_BIT;
  2027.    if (ctx->Scissor.Enabled)        ctx->RasterMask |= SCISSOR_BIT;
  2028.    if (ctx->Stencil.Enabled)        ctx->RasterMask |= STENCIL_BIT;
  2029.    if (ctx->Color.SWmasking)        ctx->RasterMask |= MASKING_BIT;
  2030.  
  2031.    if (ctx->Visual->SoftwareAlpha && ctx->Color.ColorMask[ACOMP]
  2032.        && ctx->Color.DrawBuffer != GL_NONE)
  2033.       ctx->RasterMask |= ALPHABUF_BIT;
  2034.  
  2035.    if (   ctx->Viewport.X<0
  2036.        || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
  2037.        || ctx->Viewport.Y<0
  2038.        || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
  2039.       ctx->RasterMask |= WINCLIP_BIT;
  2040.    }
  2041.  
  2042.    /* If we're not drawing to exactly one color buffer set the
  2043.     * MULTI_DRAW_BIT flag.  Also set it if we're drawing to no
  2044.     * buffers or the RGBA or CI mask disables all writes.
  2045.     */
  2046.  
  2047.    ctx->TriangleCaps &= ~DD_MULTIDRAW;
  2048.  
  2049.    if (ctx->Color.MultiDrawBuffer) {
  2050.       ctx->RasterMask |= MULTI_DRAW_BIT;
  2051.       ctx->TriangleCaps |= DD_MULTIDRAW;
  2052.    }
  2053.    else if (ctx->Color.DrawBuffer==GL_NONE) {
  2054.       ctx->RasterMask |= MULTI_DRAW_BIT;
  2055.       ctx->TriangleCaps |= DD_MULTIDRAW;
  2056.    }
  2057.    else if (ctx->Visual->RGBAflag && ctx->Color.ColorMask==0) {
  2058.       /* all RGBA channels disabled */
  2059.       ctx->RasterMask |= MULTI_DRAW_BIT;
  2060.       ctx->TriangleCaps |= DD_MULTIDRAW;
  2061.       ctx->Color.DrawDestMask = 0;
  2062.    }
  2063.    else if (!ctx->Visual->RGBAflag && ctx->Color.IndexMask==0) {
  2064.       /* all color index bits disabled */
  2065.       ctx->RasterMask |= MULTI_DRAW_BIT;
  2066.       ctx->TriangleCaps |= DD_MULTIDRAW;
  2067.       ctx->Color.DrawDestMask = 0;
  2068.    }
  2069. }
  2070.  
  2071.  
  2072. void gl_print_state( const char *msg, GLuint state )
  2073. {
  2074.    fprintf(stderr,
  2075.        "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
  2076.        msg,
  2077.        state,
  2078.        (state & NEW_LIGHTING)         ? "lighting, " : "",
  2079.        (state & NEW_RASTER_OPS)       ? "raster-ops, " : "",
  2080.        (state & NEW_TEXTURING)        ? "texturing, " : "",
  2081.        (state & NEW_POLYGON)          ? "polygon, " : "",
  2082.        (state & NEW_DRVSTATE0)        ? "driver-0, " : "",
  2083.        (state & NEW_DRVSTATE1)        ? "driver-1, " : "",
  2084.        (state & NEW_DRVSTATE2)        ? "driver-2, " : "",
  2085.        (state & NEW_DRVSTATE3)        ? "driver-3, " : "",
  2086.        (state & NEW_MODELVIEW)        ? "modelview, " : "",
  2087.        (state & NEW_PROJECTION)       ? "projection, " : "",
  2088.        (state & NEW_TEXTURE_MATRIX)   ? "texture-matrix, " : "",
  2089.        (state & NEW_USER_CLIP)        ? "user-clip, " : "",
  2090.        (state & NEW_TEXTURE_ENV)      ? "texture-env, " : "",
  2091.        (state & NEW_CLIENT_STATE)     ? "client-state, " : "",
  2092.        (state & NEW_FOG)              ? "fog, " : "",
  2093.        (state & NEW_NORMAL_TRANSFORM) ? "normal-transform, " : "",
  2094.        (state & NEW_VIEWPORT)         ? "viewport, " : "",
  2095.        (state & NEW_TEXTURE_ENABLE)   ? "texture-enable, " : "");
  2096. }
  2097.  
  2098. void gl_print_enable_flags( const char *msg, GLuint flags )
  2099. {
  2100.    fprintf(stderr,
  2101.        "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s\n",
  2102.        msg,
  2103.        flags,
  2104.        (flags & ENABLE_TEX0)       ? "tex-0, " : "",
  2105.        (flags & ENABLE_TEX1)       ? "tex-1, " : "",
  2106.        (flags & ENABLE_LIGHT)      ? "light, " : "",
  2107.        (flags & ENABLE_FOG)        ? "fog, " : "",
  2108.        (flags & ENABLE_USERCLIP)   ? "userclip, " : "",
  2109.        (flags & ENABLE_TEXGEN0)    ? "tex-gen-0, " : "",
  2110.        (flags & ENABLE_TEXGEN1)    ? "tex-gen-1, " : "",
  2111.        (flags & ENABLE_TEXMAT0)    ? "tex-mat-0, " : "",
  2112.        (flags & ENABLE_TEXMAT1)    ? "tex-mat-1, " : "",
  2113.        (flags & ENABLE_NORMALIZE)  ? "normalize, " : "",
  2114.        (flags & ENABLE_RESCALE)    ? "rescale, " : "");
  2115. }
  2116.  
  2117.  
  2118. /*
  2119.  * If ctx->NewState is non-zero then this function MUST be called before
  2120.  * rendering any primitive.  Basically, function pointers and miscellaneous
  2121.  * flags are updated to reflect the current state of the state machine.
  2122.  */
  2123. void gl_update_state( GLcontext *ctx )
  2124. {
  2125.    GLuint i;
  2126.  
  2127.    if (MESA_VERBOSE & VERBOSE_STATE)
  2128.       gl_print_state("", ctx->NewState);
  2129.  
  2130.    if (ctx->NewState & NEW_CLIENT_STATE)
  2131.       gl_update_client_state( ctx );
  2132.  
  2133.    if ((ctx->NewState & NEW_TEXTURE_ENABLE) &&
  2134.        (ctx->Enabled & ENABLE_TEX_ANY) != ctx->Texture.Enabled)
  2135.       ctx->NewState |= NEW_TEXTURING | NEW_RASTER_OPS;
  2136.  
  2137.    if (ctx->NewState & NEW_TEXTURE_ENV) {
  2138.       if (ctx->Texture.Unit[0].EnvMode == ctx->Texture.Unit[0].LastEnvMode &&
  2139.       ctx->Texture.Unit[1].EnvMode == ctx->Texture.Unit[1].LastEnvMode)
  2140.      ctx->NewState &= ~NEW_TEXTURE_ENV;
  2141.       ctx->Texture.Unit[0].LastEnvMode = ctx->Texture.Unit[0].EnvMode;
  2142.       ctx->Texture.Unit[1].LastEnvMode = ctx->Texture.Unit[1].EnvMode;
  2143.    }
  2144.  
  2145.    if (ctx->NewState & NEW_TEXTURE_MATRIX) {
  2146.       ctx->Enabled &= ~(ENABLE_TEXMAT0|ENABLE_TEXMAT1);
  2147.  
  2148.       for (i=0; i < MAX_TEXTURE_UNITS; i++) {
  2149.      if (ctx->TextureMatrix[i].flags & MAT_DIRTY_ALL_OVER)
  2150.      {
  2151.         gl_matrix_analyze( &ctx->TextureMatrix[i] );
  2152.         ctx->TextureMatrix[i].flags &= ~MAT_DIRTY_DEPENDENTS;
  2153.  
  2154.         if (ctx->Texture.Unit[i].Enabled &&
  2155.         ctx->TextureMatrix[i].type != MATRIX_IDENTITY)
  2156.            ctx->Enabled |= ENABLE_TEXMAT0 << i;
  2157.      }
  2158.       }
  2159.    }
  2160.  
  2161.    if (ctx->NewState & (NEW_TEXTURING | NEW_TEXTURE_ENABLE)) {
  2162.       ctx->Texture.NeedNormals = GL_FALSE;
  2163.       gl_update_dirty_texobjs(ctx);
  2164.       ctx->Enabled &= ~(ENABLE_TEXGEN0|ENABLE_TEXGEN1);
  2165.       ctx->Texture.ReallyEnabled = 0;
  2166.  
  2167.       for (i=0; i < MAX_TEXTURE_UNITS; i++) {
  2168.      if (ctx->Texture.Unit[i].Enabled) {
  2169.         gl_update_texture_unit( ctx, &ctx->Texture.Unit[i] );
  2170.  
  2171.         ctx->Texture.ReallyEnabled |=
  2172.            ctx->Texture.Unit[i].ReallyEnabled<<(i*4);
  2173.  
  2174.         if (ctx->Texture.Unit[i].GenFlags != 0) {
  2175.            ctx->Enabled |= ENABLE_TEXGEN0 << i;
  2176.  
  2177.            if (ctx->Texture.Unit[i].GenFlags & TEXGEN_NEED_NORMALS)
  2178.            {
  2179.           ctx->Texture.NeedNormals = GL_TRUE;
  2180.           ctx->Texture.NeedEyeCoords = GL_TRUE;
  2181.            }
  2182.  
  2183.            if (ctx->Texture.Unit[i].GenFlags & TEXGEN_NEED_EYE_COORD)
  2184.            {
  2185.           ctx->Texture.NeedEyeCoords = GL_TRUE;
  2186.            }
  2187.         }
  2188.      }
  2189.       }
  2190.  
  2191.       ctx->Texture.Enabled = ctx->Enabled & ENABLE_TEX_ANY;
  2192.       ctx->NeedNormals = (ctx->Light.Enabled || ctx->Texture.NeedNormals);
  2193.    }
  2194.  
  2195.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING | NEW_FOG)) {
  2196.  
  2197.  
  2198.       if (ctx->NewState & NEW_RASTER_OPS) {
  2199.      update_pixel_logic(ctx);
  2200.      update_pixel_masking(ctx);
  2201.      update_fog_mode(ctx);
  2202.      update_rasterflags(ctx);
  2203.      if (ctx->Driver.Dither) {
  2204.         (*ctx->Driver.Dither)( ctx, ctx->Color.DitherFlag );
  2205.      }
  2206.  
  2207.      /* Check if incoming colors can be modified during rasterization */
  2208.      if (ctx->Fog.Enabled ||
  2209.          ctx->Texture.Enabled ||
  2210.          ctx->Color.BlendEnabled ||
  2211.          ctx->Color.SWmasking ||
  2212.          ctx->Color.SWLogicOpEnabled) {
  2213.         ctx->MutablePixels = GL_TRUE;
  2214.      }
  2215.      else {
  2216.         ctx->MutablePixels = GL_FALSE;
  2217.      }
  2218.  
  2219.      /* update scissor region */
  2220.  
  2221.      ctx->Buffer->Xmin = 0;
  2222.      ctx->Buffer->Ymin = 0;
  2223.      ctx->Buffer->Xmax = ctx->Buffer->Width-1;
  2224.      ctx->Buffer->Ymax = ctx->Buffer->Height-1;
  2225.      if (ctx->Scissor.Enabled) {
  2226.         if (ctx->Scissor.X > ctx->Buffer->Xmin) {
  2227.            ctx->Buffer->Xmin = ctx->Scissor.X;
  2228.         }
  2229.         if (ctx->Scissor.Y > ctx->Buffer->Ymin) {
  2230.            ctx->Buffer->Ymin = ctx->Scissor.Y;
  2231.         }
  2232.         if (ctx->Scissor.X + ctx->Scissor.Width - 1 < ctx->Buffer->Xmax) {
  2233.            ctx->Buffer->Xmax = ctx->Scissor.X + ctx->Scissor.Width - 1;
  2234.         }
  2235.         if (ctx->Scissor.Y + ctx->Scissor.Height - 1 < ctx->Buffer->Ymax) {
  2236.            ctx->Buffer->Ymax = ctx->Scissor.Y + ctx->Scissor.Height - 1;
  2237.         }
  2238.      }
  2239.  
  2240.      /* The driver isn't managing the depth buffer.
  2241.       */
  2242.      if (ctx->Driver.AllocDepthBuffer == gl_alloc_depth_buffer) 
  2243.      {
  2244.      if (ctx->Depth.Mask) {
  2245.         switch (ctx->Depth.Func) {
  2246.         case GL_LESS:
  2247.            ctx->Driver.DepthTestSpan = gl_depth_test_span_less;
  2248.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_less;
  2249.            break;
  2250.         case GL_GREATER:
  2251.            ctx->Driver.DepthTestSpan = gl_depth_test_span_greater;
  2252.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_greater;
  2253.            break;
  2254.         default:
  2255.            ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2256.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2257.         }
  2258.      }
  2259.      else {
  2260.         ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2261.         ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2262.      }
  2263.      }
  2264.       }
  2265.  
  2266.       if (ctx->NewState & NEW_LIGHTING) {
  2267.      ctx->TriangleCaps &= ~(DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL);
  2268.      if (ctx->Light.Enabled) {
  2269.         if (ctx->Light.Model.TwoSide)
  2270.            ctx->TriangleCaps |= (DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL);
  2271.         gl_update_lighting(ctx);
  2272.      }
  2273.       }
  2274.    }
  2275.  
  2276.    if (ctx->NewState & (NEW_POLYGON | NEW_LIGHTING)) {
  2277.  
  2278.       ctx->TriangleCaps &= ~DD_TRI_CULL_FRONT_BACK;
  2279.  
  2280.       if (ctx->NewState & NEW_POLYGON) {
  2281.      /* Setup CullBits bitmask */
  2282.      if (ctx->Polygon.CullFlag) {
  2283.         ctx->backface_sign = 1;
  2284.         switch(ctx->Polygon.CullFaceMode) {
  2285.         case GL_BACK:
  2286.            if(ctx->Polygon.FrontFace==GL_CCW)
  2287.           ctx->backface_sign = -1;
  2288.            ctx->Polygon.CullBits = 1;
  2289.            break;
  2290.         case GL_FRONT:
  2291.            if(ctx->Polygon.FrontFace!=GL_CCW)
  2292.           ctx->backface_sign = -1;
  2293.            ctx->Polygon.CullBits = 2;
  2294.            break;
  2295.         default:
  2296.         case GL_FRONT_AND_BACK:
  2297.            ctx->backface_sign = 0;
  2298.            ctx->Polygon.CullBits = 0;
  2299.            ctx->TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
  2300.            break;
  2301.         }
  2302.      }
  2303.      else {
  2304.         ctx->Polygon.CullBits = 3;
  2305.         ctx->backface_sign = 0;
  2306.      }
  2307.  
  2308.      /* Any Polygon offsets enabled? */
  2309.      ctx->TriangleCaps &= ~DD_TRI_OFFSET;
  2310.  
  2311.      if (ctx->Polygon.OffsetPoint ||
  2312.          ctx->Polygon.OffsetLine ||
  2313.          ctx->Polygon.OffsetFill)
  2314.         ctx->TriangleCaps |= DD_TRI_OFFSET;
  2315.  
  2316.      /* reset Z offsets now */
  2317.      ctx->PointZoffset   = 0.0;
  2318.      ctx->LineZoffset    = 0.0;
  2319.      ctx->PolygonZoffset = 0.0;
  2320.       }
  2321.    }
  2322.  
  2323.    if (ctx->NewState & ~(NEW_CLIENT_STATE|
  2324.              NEW_DRIVER_STATE|NEW_USER_CLIP|
  2325.              NEW_POLYGON))
  2326.       gl_update_clipmask(ctx);
  2327.  
  2328.    if (ctx->NewState & (NEW_LIGHTING|
  2329.             NEW_RASTER_OPS|
  2330.             NEW_TEXTURING|
  2331.             NEW_TEXTURE_ENABLE|
  2332.             NEW_TEXTURE_ENV|
  2333.             NEW_POLYGON|
  2334.             NEW_DRVSTATE0|
  2335.             NEW_DRVSTATE1|
  2336.             NEW_DRVSTATE2|
  2337.             NEW_DRVSTATE3|
  2338.             NEW_USER_CLIP))
  2339.    {
  2340.       ctx->IndirectTriangles = ctx->TriangleCaps & ~ctx->Driver.TriangleCaps;
  2341.       ctx->IndirectTriangles |= DD_SW_RASTERIZE;
  2342.  
  2343.       if (MESA_VERBOSE&VERBOSE_CULL)
  2344.      gl_print_tri_caps("initial indirect tris", ctx->IndirectTriangles);
  2345.  
  2346.       ctx->Driver.PointsFunc = NULL;
  2347.       ctx->Driver.LineFunc = NULL;
  2348.       ctx->Driver.TriangleFunc = NULL;
  2349.       ctx->Driver.QuadFunc = NULL;
  2350.       ctx->Driver.RectFunc = NULL;
  2351.       ctx->Driver.RenderVBClippedTab = NULL;
  2352.       ctx->Driver.RenderVBCulledTab = NULL;
  2353.       ctx->Driver.RenderVBRawTab = NULL;
  2354.  
  2355.       /*
  2356.        * Here the driver sets up all the ctx->Driver function pointers to
  2357.        * it's specific, private functions.
  2358.        */
  2359.       ctx->Driver.UpdateState(ctx);
  2360.  
  2361.       if (MESA_VERBOSE&VERBOSE_CULL)
  2362.      gl_print_tri_caps("indirect tris", ctx->IndirectTriangles);
  2363.  
  2364.       /*
  2365.        * In case the driver didn't hook in an optimized point, line or
  2366.        * triangle function we'll now select "core/fallback" point, line
  2367.        * and triangle functions.
  2368.        */
  2369.       if (ctx->IndirectTriangles & DD_SW_RASTERIZE) {
  2370.      gl_set_point_function(ctx);
  2371.      gl_set_line_function(ctx);
  2372.      gl_set_triangle_function(ctx);
  2373.      gl_set_quad_function(ctx);
  2374.  
  2375.      if ((ctx->IndirectTriangles & 
  2376.           (DD_TRI_SW_RASTERIZE|DD_QUAD_SW_RASTERIZE|DD_TRI_CULL)) ==
  2377.          (DD_TRI_SW_RASTERIZE|DD_QUAD_SW_RASTERIZE|DD_TRI_CULL)) 
  2378.         ctx->IndirectTriangles &= ~DD_TRI_CULL;
  2379.       }
  2380.  
  2381.       if (MESA_VERBOSE&VERBOSE_CULL)
  2382.      gl_print_tri_caps("indirect tris 2", ctx->IndirectTriangles);
  2383.  
  2384.       gl_set_render_vb_function(ctx);
  2385.    }
  2386.  
  2387.    /* Should only be calc'd when !need_eye_coords and not culling.
  2388.     */
  2389.    if (ctx->NewState & (NEW_MODELVIEW|NEW_PROJECTION)) {
  2390.       if (ctx->NewState & NEW_MODELVIEW) {
  2391.      gl_matrix_analyze( &ctx->ModelView );
  2392.      ctx->ProjectionMatrix.flags &= ~MAT_DIRTY_DEPENDENTS;
  2393.       }
  2394.  
  2395.       if (ctx->NewState & NEW_PROJECTION) {
  2396.      gl_matrix_analyze( &ctx->ProjectionMatrix );
  2397.      ctx->ProjectionMatrix.flags &= ~MAT_DIRTY_DEPENDENTS;
  2398.  
  2399.      if (ctx->Transform.AnyClip) {
  2400.         gl_update_userclip( ctx );
  2401.      }
  2402.       }
  2403.  
  2404.       gl_calculate_model_project_matrix( ctx );
  2405.       ctx->ModelProjectWinMatrixUptodate = 0;
  2406.    }
  2407.  
  2408.    /* Figure out whether we can light in object space or not.  If we
  2409.     * can, find the current positions of the lights in object space
  2410.     */
  2411.    if ((ctx->Enabled & (ENABLE_POINT_ATTEN | ENABLE_LIGHT | ENABLE_FOG |
  2412.             ENABLE_TEXGEN0 | ENABLE_TEXGEN1)) &&
  2413.        (ctx->NewState & (NEW_LIGHTING | 
  2414.                          NEW_FOG |
  2415.              NEW_MODELVIEW | 
  2416.              NEW_PROJECTION |
  2417.              NEW_TEXTURING |
  2418.              NEW_RASTER_OPS |
  2419.              NEW_USER_CLIP)))
  2420.    {
  2421.       GLboolean oldcoord, oldnorm;
  2422.  
  2423.       oldcoord = ctx->NeedEyeCoords;
  2424.       oldnorm = ctx->NeedEyeNormals;
  2425.  
  2426.       ctx->NeedNormals = (ctx->Light.Enabled || ctx->Texture.NeedNormals);
  2427.       ctx->NeedEyeCoords = (ctx->FogMode == FOG_VERTEX ||
  2428.                 ctx->Point.Attenuated);
  2429.       ctx->NeedEyeNormals = GL_FALSE;
  2430.  
  2431.       if (ctx->Light.Enabled) {
  2432.      if (ctx->Light.Flags & LIGHT_POSITIONAL) {
  2433.         /* Need length for attenuation */
  2434.         if (!TEST_MAT_FLAGS( &ctx->ModelView, MAT_FLAGS_LENGTH_PRESERVING))
  2435.            ctx->NeedEyeCoords = GL_TRUE;
  2436.      } else if (ctx->Light.NeedVertices) {
  2437.         /* Need angle for spot calculations */
  2438.         if (!TEST_MAT_FLAGS( &ctx->ModelView, MAT_FLAGS_ANGLE_PRESERVING))
  2439.            ctx->NeedEyeCoords = GL_TRUE;
  2440.      }
  2441.      ctx->NeedEyeNormals = ctx->NeedEyeCoords;
  2442.       }
  2443.       if (ctx->Texture.Enabled || ctx->RenderMode==GL_FEEDBACK) {
  2444.      if (ctx->Texture.NeedEyeCoords) ctx->NeedEyeCoords = GL_TRUE;
  2445.      if (ctx->Texture.NeedNormals)
  2446.         ctx->NeedNormals = ctx->NeedEyeNormals = GL_TRUE;
  2447.       }
  2448.  
  2449.       ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
  2450.  
  2451.       if (ctx->NeedEyeCoords)
  2452.      ctx->vb_proj_matrix = &ctx->ProjectionMatrix;
  2453.  
  2454.       if (ctx->Light.Enabled) {
  2455.      gl_update_lighting_function(ctx);
  2456.  
  2457.      if ( (ctx->NewState & NEW_LIGHTING) ||
  2458.           ((ctx->NewState & (NEW_MODELVIEW| NEW_PROJECTION)) &&
  2459.            !ctx->NeedEyeCoords) ||
  2460.           oldcoord != ctx->NeedEyeCoords ||
  2461.           oldnorm != ctx->NeedEyeNormals) {
  2462.         gl_compute_light_positions(ctx);
  2463.      }
  2464.  
  2465.      ctx->rescale_factor = 1.0F;
  2466.  
  2467.      if (ctx->ModelView.flags & (MAT_FLAG_UNIFORM_SCALE |
  2468.                      MAT_FLAG_GENERAL_SCALE |
  2469.                      MAT_FLAG_GENERAL_3D |
  2470.                      MAT_FLAG_GENERAL) )
  2471.  
  2472.      {
  2473.         GLfloat *m = ctx->ModelView.inv;
  2474.         GLfloat f = m[2]*m[2] + m[6]*m[6] + m[10]*m[10];
  2475.         if (f > 1e-12 && (f-1)*(f-1) > 1e-12)
  2476.            ctx->rescale_factor = 1.0/GL_SQRT(f);
  2477.      }
  2478.       }
  2479.  
  2480.       gl_update_normal_transform( ctx );
  2481.    }
  2482.  
  2483.    gl_update_pipelines(ctx);
  2484.    ctx->NewState = 0;
  2485. }
  2486.