home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / context.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  55.8 KB  |  2,048 lines

  1. /* $Id: context.c,v 1.58 1997/10/29 01:29:09 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.5
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: context.c,v $
  26.  * Revision 1.58  1997/10/29 01:29:09  brianp
  27.  * added GL_EXT_point_parameters extension from Daniel Barrero
  28.  *
  29.  * Revision 1.57  1997/10/16 01:59:08  brianp
  30.  * added GL_EXT_shared_texture_palette extension
  31.  *
  32.  * Revision 1.56  1997/10/15 03:08:50  brianp
  33.  * don't clear depth buffer in gl_ResizeBuffersMESA()
  34.  *
  35.  * Revision 1.55  1997/09/29 22:25:28  brianp
  36.  * added const to a few function parameters
  37.  *
  38.  * Revision 1.54  1997/09/27 00:15:20  brianp
  39.  * added ctx->DirectContext flag
  40.  *
  41.  * Revision 1.53  1997/09/23 00:58:15  brianp
  42.  * now using hash table for texture objects
  43.  *
  44.  * Revision 1.52  1997/09/22 02:33:58  brianp
  45.  * display lists now implemented with hash table
  46.  *
  47.  * Revision 1.51  1997/09/17 01:39:11  brianp
  48.  * added update_clipmask() function from Michael Pichler
  49.  *
  50.  * Revision 1.50  1997/09/10 00:29:19  brianp
  51.  * set all NewState flags in gl_ResizeBuffersMESA() (Miklos Fazekas)
  52.  *
  53.  * Revision 1.49  1997/07/24 01:24:45  brianp
  54.  * changed precompiled header symbol from PCH to PC_HEADER
  55.  *
  56.  * Revision 1.48  1997/06/20 02:19:49  brianp
  57.  * replaced Current.IntColor with Current.ByteColor
  58.  *
  59.  * Revision 1.47  1997/06/20 01:57:28  brianp
  60.  * free_shared_state() didn't free display lists
  61.  *
  62.  * Revision 1.46  1997/05/31 16:57:14  brianp
  63.  * added MESA_NO_RASTER env var support
  64.  *
  65.  * Revision 1.45  1997/05/28 04:06:03  brianp
  66.  * implemented projection near/far value stack for Driver.NearFar() function
  67.  *
  68.  * Revision 1.44  1997/05/28 03:23:48  brianp
  69.  * added precompiled header (PCH) support
  70.  *
  71.  * Revision 1.43  1997/05/26 21:18:54  brianp
  72.  * better comments
  73.  *
  74.  * Revision 1.42  1997/05/26 21:14:13  brianp
  75.  * gl_create_visual() now takes red/green/blue/alpha_bits arguments
  76.  *
  77.  * Revision 1.41  1997/05/24 12:07:07  brianp
  78.  * removed unused vars
  79.  *
  80.  * Revision 1.40  1997/05/14 03:27:24  brianp
  81.  * miscellaneous alloc/free clean-ups
  82.  *
  83.  * Revision 1.39  1997/05/09 22:42:09  brianp
  84.  * replaced gl_init_vb() with gl_alloc_vb(), similarly for pb
  85.  *
  86.  * Revision 1.38  1997/05/01 02:07:35  brianp
  87.  * added shared state variable NextFreeTextureName
  88.  *
  89.  * Revision 1.37  1997/05/01 01:25:33  brianp
  90.  * added call to gl_init_math()
  91.  *
  92.  * Revision 1.36  1997/04/28 02:06:14  brianp
  93.  * also check if texturing enabled in the test for NeedNormals
  94.  *
  95.  * Revision 1.35  1997/04/26 04:34:43  brianp
  96.  * fixed problem with Depth/Accum/StencilBits initialization (Mark Kilgard)
  97.  *
  98.  * Revision 1.34  1997/04/24 01:49:02  brianp
  99.  * call gl_set_color_function() instead of directly setting pointers
  100.  *
  101.  * Revision 1.33  1997/04/24 00:18:10  brianp
  102.  * Proxy2D tex object was allocated twice.  reported by Randy Frank.
  103.  *
  104.  * Revision 1.32  1997/04/16 23:55:06  brianp
  105.  * added gl_set_api_table()
  106.  *
  107.  * Revision 1.31  1997/04/14 02:01:03  brianp
  108.  * #include "texstate.h" instead of "texture.h"
  109.  *
  110.  * Revision 1.30  1997/04/12 17:11:38  brianp
  111.  * added gl_get_current_context()
  112.  *
  113.  * Revision 1.29  1997/04/12 16:54:05  brianp
  114.  * new NEW_POLYGON state flag
  115.  *
  116.  * Revision 1.28  1997/04/12 16:20:51  brianp
  117.  * added call to Driver.Dither() in gl_update_state()
  118.  *
  119.  * Revision 1.27  1997/04/12 12:25:37  brianp
  120.  * added Driver.QuadFunc and Driver.RectFunc
  121.  *
  122.  * Revision 1.26  1997/04/01 04:20:22  brianp
  123.  * added new matrix type code
  124.  *
  125.  * Revision 1.25  1997/03/14 00:24:37  brianp
  126.  * only print runtime warnings if MESA_DEBUG env var is set
  127.  *
  128.  * Revision 1.24  1997/03/13 03:06:14  brianp
  129.  * init AlphaRefUbyte to zero
  130.  *
  131.  * Revision 1.23  1997/03/08 01:59:00  brianp
  132.  * if RenderMode != GL_RENDER then don't enable ctx->DirectTriangles
  133.  *
  134.  * Revision 1.22  1997/02/27 19:57:38  brianp
  135.  * added gl_problem() function
  136.  *
  137.  * Revision 1.21  1997/02/10 19:49:50  brianp
  138.  * added gl_ResizeBuffersMESA()
  139.  *
  140.  * Revision 1.20  1997/02/10 19:22:47  brianp
  141.  * added device driver Error() function
  142.  *
  143.  * Revision 1.19  1997/02/09 18:50:05  brianp
  144.  * added GL_EXT_texture3D support
  145.  *
  146.  * Revision 1.18  1997/01/28 22:15:14  brianp
  147.  * now there's separate state for CI and RGBA logic op enabled
  148.  *
  149.  * Revision 1.17  1997/01/09 19:56:09  brianp
  150.  * new error message format in gl_error()
  151.  *
  152.  * Revision 1.16  1997/01/09 19:47:32  brianp
  153.  * better checking and handling of out-of-memory errors
  154.  *
  155.  * Revision 1.15  1996/12/18 20:00:12  brianp
  156.  * added code to initialize ColorMaterialBitmask
  157.  *
  158.  * Revision 1.14  1996/12/11 20:18:53  brianp
  159.  * changed formatting of messages in gl_warning()
  160.  *
  161.  * Revision 1.13  1996/11/08 02:20:52  brianp
  162.  * added check for MESA_NO_RASTER env var
  163.  *
  164.  * Revision 1.12  1996/11/04 02:18:47  brianp
  165.  * multiply Viewport.Sz and .Tz by DEPTH_SCALE
  166.  *
  167.  * Revision 1.11  1996/10/16 00:52:22  brianp
  168.  * gl_initialize_api_function_pointers() now gl_init_api_function_pointers()
  169.  *
  170.  * Revision 1.10  1996/10/11 03:41:28  brianp
  171.  * removed Polygon.OffsetBias initialization
  172.  *
  173.  * Revision 1.9  1996/10/11 00:26:34  brianp
  174.  * initialize Point/Line/PolygonZoffset to 0.0
  175.  *
  176.  * Revision 1.8  1996/10/01 01:42:31  brianp
  177.  * moved device driver initialization into gl_update_state()
  178.  *
  179.  * Revision 1.7  1996/09/27 01:24:33  brianp
  180.  * removed unneeded set_thread_context(), added call to gl_init_vb()
  181.  *
  182.  * Revision 1.6  1996/09/25 03:22:53  brianp
  183.  * added NO_DRAW_BIT for glDrawBuffer(GL_NONE)
  184.  *
  185.  * Revision 1.5  1996/09/20 02:55:52  brianp
  186.  * fixed profiling bug
  187.  *
  188.  * Revision 1.4  1996/09/19 03:14:49  brianp
  189.  * now just one parameter for gl_create_framebuffer()
  190.  *
  191.  * Revision 1.3  1996/09/15 14:20:22  brianp
  192.  * added new functions for GLframebuffer and GLvisual support
  193.  *
  194.  * Revision 1.2  1996/09/14 20:12:05  brianp
  195.  * wasn't initializing a few pieces of context state
  196.  *
  197.  * Revision 1.1  1996/09/13 01:38:16  brianp
  198.  * Initial revision
  199.  *
  200.  */
  201.  
  202.  
  203. /*
  204.  * If multi-threading is enabled (-DTHREADS) then each thread has it's
  205.  * own rendering context.  A thread obtains the pointer to its GLcontext
  206.  * with the gl_get_thread_context() function.  Otherwise, the global
  207.  * pointer, CC, points to the current context used by all threads in
  208.  * the address space.
  209.  */
  210.  
  211.  
  212. #ifdef PC_HEADER
  213. #include "all.h"
  214. #else
  215. #include <assert.h>
  216. #include <math.h>
  217. #include <stdio.h>
  218. #include <stdlib.h>
  219. #include <string.h>
  220. #include "accum.h"
  221. #include "alphabuf.h"
  222. #include "context.h"
  223. #include "depth.h"
  224. #include "eval.h"
  225. #include "hash.h"
  226. #include "light.h"
  227. #include "lines.h"
  228. #include "dlist.h"
  229. #include "macros.h"
  230. #include "mmath.h"
  231. #include "pb.h"
  232. #include "points.h"
  233. #include "pointers.h"
  234. #include "quads.h"
  235. #include "stencil.h"
  236. #include "triangle.h"
  237. #include "teximage.h"
  238. #include "texobj.h"
  239. #include "texstate.h"
  240. #include "types.h"
  241. #include "vb.h"
  242. #include "vbfill.h"
  243. #endif
  244.  
  245.  
  246.  
  247. /**********************************************************************/
  248. /*****                  Context and Thread management             *****/
  249. /**********************************************************************/
  250.  
  251.  
  252. #ifdef THREADS
  253.  
  254. /* A different context may be bound to each thread */
  255.  
  256. #ifndef AMIGAWARP
  257. #include <sys/types.h>
  258. #include <unistd.h>
  259. #endif
  260.  
  261. #define MAX_THREADS 20
  262. #ifdef AMIGAWARP
  263. typedef unsigned pid_t;
  264. pid_t getpid(void);
  265. #endif
  266. static pid_t ThreadID[MAX_THREADS];
  267. static GLcontext *ThreadContext[MAX_THREADS];
  268. static int NumThreads = 0;
  269.  
  270. GLcontext *gl_get_thread_context( void )
  271. {
  272.    pid_t id;
  273.    int i;
  274.  
  275.    id = getpid();
  276.  
  277.    for (i=0;i<MAX_THREADS;i++) {
  278.       if (ThreadID[i]==id) {
  279.      return ThreadContext[i];
  280.       }
  281.    }
  282.  
  283.    /* No context for this thread */
  284.    return NULL;
  285. }
  286.  
  287. static void set_thread_context( GLcontext *ctx )
  288. {
  289.    pid_t id = getpid();
  290.    int i;
  291.  
  292.    for (i=0;i<NumThreads;i++) {
  293.       if (ThreadID[i]==id) {
  294.      ThreadContext[i] = ctx;
  295.      return;
  296.       }
  297.    }
  298.    if (i<MAX_THREADS) {
  299.       ThreadID[i] = getpid();
  300.       ThreadContext[i] = ctx;
  301.       NumThreads++;
  302.    }
  303. }
  304.  
  305. #else
  306.  
  307. /* One Current Context pointer for all threads in the address space */
  308. GLcontext *CC = NULL;
  309.  
  310. #endif /*THREADS*/
  311.  
  312.  
  313.  
  314.  
  315. /**********************************************************************/
  316. /*****                   Profiling functions                      *****/
  317. /**********************************************************************/
  318.  
  319. #ifdef PROFILE
  320.  
  321. #ifndef AMIGAWARP
  322. #include <sys/times.h>
  323. #include <sys/param.h>
  324.  
  325.  
  326. /*
  327.  * Return system time in seconds.
  328.  * NOTE:  this implementation may not be very portable!
  329.  */
  330. GLdouble gl_time( void )
  331. {
  332.    static GLdouble prev_time = 0.0;
  333.    static GLdouble time;
  334.    struct tms tm;
  335.    clock_t clk;
  336.  
  337.    clk = times(&tm);
  338.  
  339. #ifdef CLK_TCK
  340.    time = (double)clk / (double)CLK_TCK;
  341. #else
  342.    time = (double)clk / (double)HZ;
  343. #endif
  344.  
  345.    if (time>prev_time) {
  346.       prev_time = time;
  347.       return time;
  348.    }
  349.    else {
  350.       return prev_time;
  351.    }
  352. }
  353.  
  354. #else
  355.  
  356. #ifdef __PPC__
  357.  
  358. #include <devices/timer.h>
  359. #include <clib/powerpc_protos.h>
  360.  
  361.  
  362. GLdouble gl_time( void )
  363. {
  364.    static GLdouble prev_time = 0.0;
  365.    static GLdouble time;
  366.    struct timeval tv;
  367.  
  368.    GetSysTimePPC(&tv);
  369.    time = ((GLdouble)tv.tv_secs + (GLdouble)tv.tv_micro / 1000000);
  370.    if (time>prev_time) {
  371.       prev_time = time;
  372.       return time;
  373.    }
  374.    else {
  375.       return prev_time;
  376.    }
  377. }
  378.  
  379. #endif
  380. #endif
  381.  
  382. /*
  383.  * Reset the timing/profiling counters
  384.  */
  385. static void init_timings( GLcontext *ctx )
  386. {
  387.    ctx->BeginEndCount = 0;
  388.    ctx->BeginEndTime = 0.0;
  389.    ctx->VertexCount = 0;
  390.    ctx->VertexTime = 0.0;
  391.    ctx->PointCount = 0;
  392.    ctx->PointTime = 0.0;
  393.    ctx->LineCount = 0;
  394.    ctx->LineTime = 0.0;
  395.    ctx->PolygonCount = 0;
  396.    ctx->PolygonTime = 0.0;
  397.    ctx->ClearCount = 0;
  398.    ctx->ClearTime = 0.0;
  399.    ctx->SwapCount = 0;
  400.    ctx->SwapTime = 0.0;
  401. }
  402.  
  403.  
  404.  
  405. /*
  406.  * Print the accumulated timing/profiling data.
  407.  */
  408. static void print_timings( GLcontext *ctx )
  409. {
  410.    GLdouble beginendrate;
  411.    GLdouble vertexrate;
  412.    GLdouble pointrate;
  413.    GLdouble linerate;
  414.    GLdouble polygonrate;
  415.    GLdouble overhead;
  416.    GLdouble clearrate;
  417.    GLdouble swaprate;
  418.    GLdouble avgvertices;
  419.  
  420.    if (ctx->BeginEndTime>0.0) {
  421.       beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
  422.    }
  423.    else {
  424.       beginendrate = 0.0;
  425.    }
  426.    if (ctx->VertexTime>0.0) {
  427.       vertexrate = ctx->VertexCount / ctx->VertexTime;
  428.    }
  429.    else {
  430.       vertexrate = 0.0;
  431.    }
  432.    if (ctx->PointTime>0.0) {
  433.       pointrate = ctx->PointCount / ctx->PointTime;
  434.    }
  435.    else {
  436.       pointrate = 0.0;
  437.    }
  438.    if (ctx->LineTime>0.0) {
  439.       linerate = ctx->LineCount / ctx->LineTime;
  440.    }
  441.    else {
  442.       linerate = 0.0;
  443.    }
  444.    if (ctx->PolygonTime>0.0) {
  445.       polygonrate = ctx->PolygonCount / ctx->PolygonTime;
  446.    }
  447.    else {
  448.       polygonrate = 0.0;
  449.    }
  450.    if (ctx->ClearTime>0.0) {
  451.       clearrate = ctx->ClearCount / ctx->ClearTime;
  452.    }
  453.    else {
  454.       clearrate = 0.0;
  455.    }
  456.    if (ctx->SwapTime>0.0) {
  457.       swaprate = ctx->SwapCount / ctx->SwapTime;
  458.    }
  459.    else {
  460.       swaprate = 0.0;
  461.    }
  462.  
  463.    if (ctx->BeginEndCount>0) {
  464.       avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
  465.    }
  466.    else {
  467.       avgvertices = 0.0;
  468.    }
  469.  
  470.    overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
  471.           - ctx->LineTime - ctx->PolygonTime;
  472.  
  473.  
  474.    printf("                          Count   Time (s)    Rate (/s) \n");
  475.    printf("--------------------------------------------------------\n");
  476.    printf("glBegin/glEnd           %7d  %8.3f   %10.3f\n",
  477.       ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
  478.    printf("  vertexes transformed  %7d  %8.3f   %10.3f\n",
  479.       ctx->VertexCount, ctx->VertexTime, vertexrate );
  480.    printf("  points rasterized     %7d  %8.3f   %10.3f\n",
  481.       ctx->PointCount, ctx->PointTime, pointrate );
  482.    printf("  lines rasterized      %7d  %8.3f   %10.3f\n",
  483.       ctx->LineCount, ctx->LineTime, linerate );
  484.    printf("  polygons rasterized   %7d  %8.3f   %10.3f\n",
  485.       ctx->PolygonCount, ctx->PolygonTime, polygonrate );
  486.    printf("  overhead                       %8.3f\n", overhead );
  487.    printf("glClear                 %7d  %8.3f   %10.3f\n",
  488.       ctx->ClearCount, ctx->ClearTime, clearrate );
  489.    printf("SwapBuffers             %7d  %8.3f   %10.3f\n",
  490.       ctx->SwapCount, ctx->SwapTime, swaprate );
  491.    printf("\n");
  492.  
  493.    printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
  494. }
  495. #endif
  496.  
  497.  
  498.  
  499.  
  500.  
  501. /**********************************************************************/
  502. /*****       Context allocation, initialization, destroying       *****/
  503. /**********************************************************************/
  504.  
  505.  
  506. /*
  507.  * Allocate and initialize a shared context state structure.
  508.  */
  509. static struct gl_shared_state *alloc_shared_state( void )
  510. {
  511.    struct gl_shared_state *ss;
  512.  
  513.    ss = (struct gl_shared_state*) calloc( 1, sizeof(struct gl_shared_state) );
  514.    if (!ss)
  515.       return NULL;
  516.  
  517.    ss->DisplayList = NewHashTable();
  518.  
  519.    ss->TexObjects = NewHashTable();
  520.  
  521.    /* Default Texture objects */
  522.    ss->Default1D = gl_alloc_texture_object(ss, 0, 1);
  523.    ss->Default2D = gl_alloc_texture_object(ss, 0, 2);
  524.    ss->Default3D = gl_alloc_texture_object(ss, 0, 3);
  525.  
  526.    if (!ss->DisplayList || !ss->TexObjects
  527.        || !ss->Default1D || !ss->Default2D || !ss->Default3D) {
  528.       /* Ran out of memory at some point.  Free everything and return NULL */
  529.       if (!ss->DisplayList)
  530.      DeleteHashTable(ss->DisplayList);
  531.       if (!ss->TexObjects)
  532.      DeleteHashTable(ss->TexObjects);
  533.       if (!ss->Default1D)
  534.      gl_free_texture_object(ss, ss->Default1D);
  535.       if (!ss->Default2D)
  536.      gl_free_texture_object(ss, ss->Default2D);
  537.       if (!ss->Default3D)
  538.      gl_free_texture_object(ss, ss->Default3D);
  539.       free(ss);
  540.       return NULL;
  541.    }
  542.    else {
  543.       return ss;
  544.    }
  545. }
  546.  
  547.  
  548. /*
  549.  * Deallocate a shared state context and all children structures.
  550.  */
  551. static void free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
  552. {
  553.    /* Free display lists */
  554.    while (1) {
  555.       GLuint list = HashFirstEntry(ss->DisplayList);
  556.       if (list) {
  557.      gl_destroy_list(ctx, list);
  558.       }
  559.       else {
  560.      break;
  561.       }
  562.    }
  563.    DeleteHashTable(ss->DisplayList);
  564.  
  565.    /* Free texture objects */
  566.    while (ss->TexObjectList)
  567.    {
  568.       /* this function removes from linked list too! */
  569.       gl_free_texture_object(ss, ss->TexObjectList);
  570.    }
  571.    DeleteHashTable(ss->TexObjects);
  572.  
  573.    free(ss);
  574. }
  575.  
  576.  
  577.  
  578.  
  579. /*
  580.  * Initialize the nth light.  Note that the defaults for light 0 are
  581.  * different than the other lights.
  582.  */
  583. static void init_light( struct gl_light *l, GLuint n )
  584. {
  585.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  586.    if (n==0) {
  587.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  588.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  589.    }
  590.    else {
  591.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  592.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  593.    }
  594.    ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
  595.    ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
  596.    l->SpotExponent = 0.0;
  597.    gl_compute_spot_exp_table( l );
  598.    l->SpotCutoff = 180.0;
  599.    l->CosCutoff = -1.0;
  600.    l->ConstantAttenuation = 1.0;
  601.    l->LinearAttenuation = 0.0;
  602.    l->QuadraticAttenuation = 0.0;
  603.    l->Enabled = GL_FALSE;
  604. }
  605.  
  606.  
  607.  
  608. static void init_lightmodel( struct gl_lightmodel *lm )
  609. {
  610.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  611.    lm->LocalViewer = GL_FALSE;
  612.    lm->TwoSide = GL_FALSE;
  613. }
  614.  
  615.  
  616. static void init_material( struct gl_material *m )
  617. {
  618.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  619.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  620.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  621.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  622.    m->Shininess = 0.0;
  623.    m->AmbientIndex = 0;
  624.    m->DiffuseIndex = 1;
  625.    m->SpecularIndex = 1;
  626.    gl_compute_material_shine_table( m );
  627. }
  628.  
  629.  
  630.  
  631. /*
  632.  * Initialize a gl_context structure to default values.
  633.  */
  634. static void initialize_context( GLcontext *ctx )
  635. {
  636.    static GLfloat identity[16] = {
  637.     1.0, 0.0, 0.0, 0.0,
  638.     0.0, 1.0, 0.0, 0.0,
  639.     0.0, 0.0, 1.0, 0.0,
  640.     0.0, 0.0, 0.0, 1.0
  641.    };
  642.    GLuint i;
  643.  
  644.    if (ctx) {
  645.       /* Modelview matrix stuff */
  646.       ctx->NewModelViewMatrix = GL_FALSE;
  647.       ctx->ModelViewMatrixType = MATRIX_IDENTITY;
  648.       MEMCPY( ctx->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
  649.       MEMCPY( ctx->ModelViewInv, identity, 16*sizeof(GLfloat) );
  650.       ctx->ModelViewStackDepth = 0;
  651.  
  652.       /* Projection matrix stuff */
  653.       ctx->NewProjectionMatrix = GL_FALSE;
  654.       ctx->ProjectionMatrixType = MATRIX_IDENTITY;
  655.       MEMCPY( ctx->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
  656.       ctx->ProjectionStackDepth = 0;
  657.       ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
  658.       ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
  659.  
  660.       /* Texture matrix stuff */
  661.       ctx->NewTextureMatrix = GL_FALSE;
  662.       ctx->TextureMatrixType = MATRIX_IDENTITY;
  663.       MEMCPY( ctx->TextureMatrix, identity, 16*sizeof(GLfloat) );
  664.       ctx->TextureStackDepth = 0;
  665.  
  666.       /* Accumulate buffer group */
  667.       ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  668.  
  669.       /* Color buffer group */
  670.       ctx->Color.IndexMask = 0xffffffff;
  671.       ctx->Color.ColorMask = 0xf;
  672.       ctx->Color.SWmasking = GL_FALSE;
  673.       ctx->Color.ClearIndex = 0;
  674.       ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  675.       ctx->Color.DrawBuffer = GL_FRONT;
  676.       ctx->Color.AlphaEnabled = GL_FALSE;
  677.       ctx->Color.AlphaFunc = GL_ALWAYS;
  678.       ctx->Color.AlphaRef = 0.0;
  679.       ctx->Color.AlphaRefUbyte = 0;
  680.       ctx->Color.BlendEnabled = GL_FALSE;
  681.       ctx->Color.BlendSrc = GL_ONE;
  682.       ctx->Color.BlendDst = GL_ZERO;
  683.       ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
  684.       ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  685.       ctx->Color.IndexLogicOpEnabled = GL_FALSE;
  686.       ctx->Color.ColorLogicOpEnabled = GL_FALSE;
  687.       ctx->Color.SWLogicOpEnabled = GL_FALSE;
  688.       ctx->Color.LogicOp = GL_COPY;
  689.       ctx->Color.DitherFlag = GL_TRUE;
  690.  
  691.       /* Current group */
  692.       ctx->Current.Index = 1;
  693.       ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
  694.       ctx->Current.ByteColor[0] = (GLint) ctx->Visual->RedScale;
  695.       ctx->Current.ByteColor[1] = (GLint) ctx->Visual->GreenScale;
  696.       ctx->Current.ByteColor[2] = (GLint) ctx->Visual->BlueScale;
  697.       ctx->Current.ByteColor[3] = (GLint) ctx->Visual->AlphaScale;
  698.       ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  699.       ctx->Current.RasterPosValid = GL_TRUE;
  700.       ctx->Current.RasterIndex = 1;
  701.       ASSIGN_4V( ctx->Current.TexCoord, 0.0, 0.0, 0.0, 1.0 );
  702.       ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  703.       ctx->Current.EdgeFlag = GL_TRUE;
  704.  
  705.       /* Depth buffer group */
  706.       ctx->Depth.Test = GL_FALSE;
  707.       ctx->Depth.Clear = 1.0;
  708.       ctx->Depth.Func = GL_LESS;
  709.       ctx->Depth.Mask = GL_TRUE;
  710.  
  711.       /* Evaluators group */
  712.       ctx->Eval.Map1Color4 = GL_FALSE;
  713.       ctx->Eval.Map1Index = GL_FALSE;
  714.       ctx->Eval.Map1Normal = GL_FALSE;
  715.       ctx->Eval.Map1TextureCoord1 = GL_FALSE;
  716.       ctx->Eval.Map1TextureCoord2 = GL_FALSE;
  717.       ctx->Eval.Map1TextureCoord3 = GL_FALSE;
  718.       ctx->Eval.Map1TextureCoord4 = GL_FALSE;
  719.       ctx->Eval.Map1Vertex3 = GL_FALSE;
  720.       ctx->Eval.Map1Vertex4 = GL_FALSE;
  721.       ctx->Eval.Map2Color4 = GL_FALSE;
  722.       ctx->Eval.Map2Index = GL_FALSE;
  723.       ctx->Eval.Map2Normal = GL_FALSE;
  724.       ctx->Eval.Map2TextureCoord1 = GL_FALSE;
  725.       ctx->Eval.Map2TextureCoord2 = GL_FALSE;
  726.       ctx->Eval.Map2TextureCoord3 = GL_FALSE;
  727.       ctx->Eval.Map2TextureCoord4 = GL_FALSE;
  728.       ctx->Eval.Map2Vertex3 = GL_FALSE;
  729.       ctx->Eval.Map2Vertex4 = GL_FALSE;
  730.       ctx->Eval.AutoNormal = GL_FALSE;
  731.       ctx->Eval.MapGrid1un = 1;
  732.       ctx->Eval.MapGrid1u1 = 0.0;
  733.       ctx->Eval.MapGrid1u2 = 1.0;
  734.       ctx->Eval.MapGrid2un = 1;
  735.       ctx->Eval.MapGrid2vn = 1;
  736.       ctx->Eval.MapGrid2u1 = 0.0;
  737.       ctx->Eval.MapGrid2u2 = 1.0;
  738.       ctx->Eval.MapGrid2v1 = 0.0;
  739.       ctx->Eval.MapGrid2v2 = 1.0;
  740.  
  741.       /* Fog group */
  742.       ctx->Fog.Enabled = GL_FALSE;
  743.       ctx->Fog.Mode = GL_EXP;
  744.       ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  745.       ctx->Fog.Index = 0.0;
  746.       ctx->Fog.Density = 1.0;
  747.       ctx->Fog.Start = 0.0;
  748.       ctx->Fog.End = 1.0;
  749.  
  750.       /* Hint group */
  751.       ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
  752.       ctx->Hint.PointSmooth = GL_DONT_CARE;
  753.       ctx->Hint.LineSmooth = GL_DONT_CARE;
  754.       ctx->Hint.PolygonSmooth = GL_DONT_CARE;
  755.       ctx->Hint.Fog = GL_DONT_CARE;
  756.  
  757.       /* Lighting group */
  758.       for (i=0;i<MAX_LIGHTS;i++) {
  759.      init_light( &ctx->Light.Light[i], i );
  760.       }
  761.       init_lightmodel( &ctx->Light.Model );
  762.       init_material( &ctx->Light.Material[0] );
  763.       init_material( &ctx->Light.Material[1] );
  764.       ctx->Light.ShadeModel = GL_SMOOTH;
  765.       ctx->Light.Enabled = GL_FALSE;
  766.       ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  767.       ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  768.       ctx->Light.ColorMaterialBitmask
  769.      = gl_material_bitmask( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
  770.  
  771.       ctx->Light.ColorMaterialEnabled = GL_FALSE;
  772.  
  773.       /* Line group */
  774.       ctx->Line.SmoothFlag = GL_FALSE;
  775.       ctx->Line.StippleFlag = GL_FALSE;
  776.       ctx->Line.Width = 1.0;
  777.       ctx->Line.StipplePattern = 0xffff;
  778.       ctx->Line.StippleFactor = 1;
  779.  
  780.       /* Display List group */
  781.       ctx->List.ListBase = 0;
  782.  
  783.       /* Pixel group */
  784.       ctx->Pixel.RedBias = 0.0;
  785.       ctx->Pixel.RedScale = 1.0;
  786.       ctx->Pixel.GreenBias = 0.0;
  787.       ctx->Pixel.GreenScale = 1.0;
  788.       ctx->Pixel.BlueBias = 0.0;
  789.       ctx->Pixel.BlueScale = 1.0;
  790.       ctx->Pixel.AlphaBias = 0.0;
  791.       ctx->Pixel.AlphaScale = 1.0;
  792.       ctx->Pixel.DepthBias = 0.0;
  793.       ctx->Pixel.DepthScale = 1.0;
  794.       ctx->Pixel.IndexOffset = 0;
  795.       ctx->Pixel.IndexShift = 0;
  796.       ctx->Pixel.ZoomX = 1.0;
  797.       ctx->Pixel.ZoomY = 1.0;
  798.       ctx->Pixel.MapColorFlag = GL_FALSE;
  799.       ctx->Pixel.MapStencilFlag = GL_FALSE;
  800.       ctx->Pixel.MapStoSsize = 1;
  801.       ctx->Pixel.MapItoIsize = 1;
  802.       ctx->Pixel.MapItoRsize = 1;
  803.       ctx->Pixel.MapItoGsize = 1;
  804.       ctx->Pixel.MapItoBsize = 1;
  805.       ctx->Pixel.MapItoAsize = 1;
  806.       ctx->Pixel.MapRtoRsize = 1;
  807.       ctx->Pixel.MapGtoGsize = 1;
  808.       ctx->Pixel.MapBtoBsize = 1;
  809.       ctx->Pixel.MapAtoAsize = 1;
  810.       ctx->Pixel.MapStoS[0] = 0;
  811.       ctx->Pixel.MapItoI[0] = 0;
  812.       ctx->Pixel.MapItoR[0] = 0.0;
  813.       ctx->Pixel.MapItoG[0] = 0.0;
  814.       ctx->Pixel.MapItoB[0] = 0.0;
  815.       ctx->Pixel.MapItoA[0] = 0.0;
  816.       ctx->Pixel.MapRtoR[0] = 0.0;
  817.       ctx->Pixel.MapGtoG[0] = 0.0;
  818.       ctx->Pixel.MapBtoB[0] = 0.0;
  819.       ctx->Pixel.MapAtoA[0] = 0.0;
  820.  
  821.       /* Point group */
  822.       ctx->Point.SmoothFlag = GL_FALSE;
  823.       ctx->Point.Size = 1.0;
  824.       ctx->Point.Params[0] = 1.0;
  825.       ctx->Point.Params[1] = 0.0;
  826.       ctx->Point.Params[2] = 0.0;
  827.       ctx->Point.MinSize = 0.0;
  828.       ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
  829.       ctx->Point.Threshold = 1.0;
  830.  
  831.       /* Polygon group */
  832.       ctx->Polygon.CullFlag = GL_FALSE;
  833.       ctx->Polygon.CullFaceMode = GL_BACK;
  834.       ctx->Polygon.FrontFace = GL_CCW;
  835.       ctx->Polygon.FrontMode = GL_FILL;
  836.       ctx->Polygon.BackMode = GL_FILL;
  837.       ctx->Polygon.Unfilled = GL_FALSE;
  838.       ctx->Polygon.SmoothFlag = GL_FALSE;
  839.       ctx->Polygon.StippleFlag = GL_FALSE;
  840.       ctx->Polygon.OffsetFactor = 0.0F;
  841.       ctx->Polygon.OffsetUnits = 0.0F;
  842.       ctx->Polygon.OffsetPoint = GL_FALSE;
  843.       ctx->Polygon.OffsetLine = GL_FALSE;
  844.       ctx->Polygon.OffsetFill = GL_FALSE;
  845.       ctx->Polygon.OffsetAny = GL_FALSE;
  846.  
  847.       /* Polygon Stipple group */
  848.       MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  849.  
  850.       /* Scissor group */
  851.       ctx->Scissor.Enabled = GL_FALSE;
  852.       ctx->Scissor.X = 0;
  853.       ctx->Scissor.Y = 0;
  854.       ctx->Scissor.Width = 0;
  855.       ctx->Scissor.Height = 0;
  856.  
  857.       /* Stencil group */
  858.       ctx->Stencil.Enabled = GL_FALSE;
  859.       ctx->Stencil.Function = GL_ALWAYS;
  860.       ctx->Stencil.FailFunc = GL_KEEP;
  861.       ctx->Stencil.ZPassFunc = GL_KEEP;
  862.       ctx->Stencil.ZFailFunc = GL_KEEP;
  863.       ctx->Stencil.Ref = 0;
  864.       ctx->Stencil.ValueMask = 0xff;
  865.       ctx->Stencil.Clear = 0;
  866.       ctx->Stencil.WriteMask = 0xff;
  867.  
  868.       /* Texture group */
  869.       ctx->Texture.Enabled = 0;
  870.       ctx->Texture.EnvMode = GL_MODULATE;
  871.       ASSIGN_4V( ctx->Texture.EnvColor, 0.0, 0.0, 0.0, 0.0 );
  872.       ctx->Texture.TexGenEnabled = 0;
  873.       ctx->Texture.GenModeS = GL_EYE_LINEAR;
  874.       ctx->Texture.GenModeT = GL_EYE_LINEAR;
  875.       ctx->Texture.GenModeR = GL_EYE_LINEAR;
  876.       ctx->Texture.GenModeQ = GL_EYE_LINEAR;
  877.       ASSIGN_4V( ctx->Texture.ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  878.       ASSIGN_4V( ctx->Texture.ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  879.       ASSIGN_4V( ctx->Texture.ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  880.       ASSIGN_4V( ctx->Texture.ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  881.       ASSIGN_4V( ctx->Texture.EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  882.       ASSIGN_4V( ctx->Texture.EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  883.       ASSIGN_4V( ctx->Texture.EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  884.       ASSIGN_4V( ctx->Texture.EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  885.  
  886. #if 00
  887.       assert( ctx->Shared->TexObjectList );
  888.       assert( ctx->Shared->TexObjectList->Next );
  889.       ctx->Texture.Current1D = ctx->Shared->TexObjectList;
  890.       ctx->Texture.Current2D = ctx->Shared->TexObjectList->Next;
  891.       ctx->Texture.Current3D = ctx->Shared->TexObjectList->Next->Next;
  892. #else
  893.       ctx->Texture.Current1D = ctx->Shared->Default1D;
  894.       ctx->Texture.Current2D = ctx->Shared->Default2D;
  895.       ctx->Texture.Current3D = ctx->Shared->Default3D;
  896. #endif
  897.  
  898.       ctx->Texture.SharedPalette = GL_FALSE;
  899.       ctx->Texture.Palette[0] = 255;
  900.       ctx->Texture.Palette[1] = 255;
  901.       ctx->Texture.Palette[2] = 255;
  902.       ctx->Texture.Palette[3] = 255;
  903.       ctx->Texture.PaletteSize = 1;
  904.       ctx->Texture.PaletteIntFormat = GL_RGBA;
  905.       ctx->Texture.PaletteFormat = GL_RGBA;
  906.  
  907.  
  908.       /* Transformation group */
  909.       ctx->Transform.MatrixMode = GL_MODELVIEW;
  910.       ctx->Transform.Normalize = GL_FALSE;
  911.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  912.      ctx->Transform.ClipEnabled[i] = GL_FALSE;
  913.      ASSIGN_4V( ctx->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
  914.       }
  915.       ctx->Transform.AnyClip = GL_FALSE;
  916.  
  917.       /* Viewport group */
  918.       ctx->Viewport.X = 0;
  919.       ctx->Viewport.Y = 0;
  920.       ctx->Viewport.Width = 0;
  921.       ctx->Viewport.Height = 0;   
  922.       ctx->Viewport.Near = 0.0;
  923.       ctx->Viewport.Far = 1.0;
  924.       ctx->Viewport.Sx = 0.0;  /* Sx, Tx, Sy, Ty are computed later */
  925.       ctx->Viewport.Tx = 0.0;
  926.       ctx->Viewport.Sy = 0.0;
  927.       ctx->Viewport.Ty = 0.0;
  928.       ctx->Viewport.Sz = 0.5 * DEPTH_SCALE;
  929.       ctx->Viewport.Tz = 0.5 * DEPTH_SCALE;
  930.  
  931.       /* Pixel transfer */
  932.       ctx->Pack.Alignment = 4;
  933.       ctx->Pack.RowLength = 0;
  934.       ctx->Pack.SkipPixels = 0;
  935.       ctx->Pack.SkipRows = 0;
  936.       ctx->Pack.SwapBytes = GL_FALSE;
  937.       ctx->Pack.LsbFirst = GL_FALSE;
  938.       ctx->Unpack.Alignment = 4;
  939.       ctx->Unpack.RowLength = 0;
  940.       ctx->Unpack.SkipPixels = 0;
  941.       ctx->Unpack.SkipRows = 0;
  942.       ctx->Unpack.SwapBytes = GL_FALSE;
  943.       ctx->Unpack.LsbFirst = GL_FALSE;
  944.  
  945.       /* Feedback */
  946.       ctx->Feedback.Type = GL_2D;   /* TODO: verify */
  947.       ctx->Feedback.Buffer = NULL;
  948.       ctx->Feedback.BufferSize = 0;
  949.       ctx->Feedback.Count = 0;
  950.  
  951.       /* Selection/picking */
  952.       ctx->Select.Buffer = NULL;
  953.       ctx->Select.BufferSize = 0;
  954.       ctx->Select.BufferCount = 0;
  955.       ctx->Select.Hits = 0;
  956.       ctx->Select.NameStackDepth = 0;
  957.  
  958.       /* Renderer and client attribute stacks */
  959.       ctx->AttribStackDepth = 0;
  960.       ctx->ClientAttribStackDepth = 0;
  961.  
  962.       /*** Miscellaneous ***/
  963.       ctx->NewState = NEW_ALL;
  964.       ctx->RenderMode = GL_RENDER;
  965.       ctx->Primitive = GL_BITMAP;
  966.  
  967.       ctx->StippleCounter = 0;
  968.       ctx->NeedNormals = GL_FALSE;
  969.  
  970.       if (   ctx->Visual->RedScale==255.0F
  971.       && ctx->Visual->GreenScale==255.0F
  972.       && ctx->Visual->BlueScale==255.0F
  973.       && ctx->Visual->AlphaScale==255.0F) {
  974.      ctx->Visual->EightBitColor = GL_TRUE;
  975.       }
  976.       else {
  977.      ctx->Visual->EightBitColor = GL_FALSE;
  978.       }
  979.       ctx->FastDrawPixels = ctx->Visual->RGBAflag && ctx->Visual->EightBitColor;
  980.  
  981. #if JUNK
  982.       ctx->PointsFunc = NULL;
  983.       ctx->LineFunc = NULL;
  984.       ctx->TriangleFunc = NULL;
  985. #endif
  986.  
  987.       ctx->DirectContext = GL_TRUE;  /* XXXX temp */
  988.  
  989.       ctx->CallDepth = 0;
  990.       ctx->ExecuteFlag = GL_TRUE;
  991.       ctx->CompileFlag = GL_FALSE;
  992.  
  993.       ctx->ErrorValue = GL_NO_ERROR;
  994. #ifndef AMIGAWARP       /* getenv/StormC is buggy and causes free twice */
  995.       /* For debug/development only */
  996.       ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
  997.  
  998.       /* Dither disable */
  999.       ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
  1000.       if (ctx->NoDither) {
  1001.      if (getenv("MESA_DEBUG")) {
  1002.         fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
  1003.      }
  1004.      ctx->Color.DitherFlag = GL_FALSE;
  1005.       }
  1006. #else
  1007.       ctx->NoRaster = GL_FALSE;
  1008.       ctx->NoDither = GL_FALSE;
  1009. #endif
  1010.    }
  1011. }
  1012.  
  1013.  
  1014.  
  1015. /*
  1016.  * Allocate a new GLvisual object.
  1017.  * Input:  rgb_flag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
  1018.  *         alpha_flag - alloc software alpha buffers?
  1019.  *         db_flag - double buffering?
  1020.  *         depth_bits - requested minimum bits per depth buffer value
  1021.  *         stencil_bits - requested minimum bits per stencil buffer value
  1022.  *         accum_bits - requested minimum bits per accum buffer component
  1023.  *         index_bits - number of bits per pixel if rgb_flag==GL_FALSE
  1024.  *         red/green/blue_scale - float-to-int color scaling
  1025.  *         alpha_scale - ignored if alpha_flag is true
  1026.  *         red/green/blue/alpha_bits - number of bits per color component
  1027.  *                                     in frame buffer for RGB(A) mode.
  1028.  * Return:  pointer to new GLvisual or NULL if requested parameters can't
  1029.  *          be met.
  1030.  */
  1031. GLvisual *gl_create_visual( GLboolean rgb_flag,
  1032.                 GLboolean alpha_flag,
  1033.                 GLboolean db_flag,
  1034.                 GLint depth_bits,
  1035.                 GLint stencil_bits,
  1036.                 GLint accum_bits,
  1037.                 GLint index_bits,
  1038.                 GLfloat red_scale,
  1039.                 GLfloat green_scale,
  1040.                 GLfloat blue_scale,
  1041.                 GLfloat alpha_scale,
  1042.                 GLint red_bits,
  1043.                 GLint green_bits,
  1044.                 GLint blue_bits,
  1045.                 GLint alpha_bits )
  1046. {
  1047.    GLvisual *vis;
  1048.  
  1049.    /* Can't do better than 8-bit/channel color at this time */
  1050.    assert( red_scale<=255.0 );
  1051.    assert( green_scale<=255.0 );
  1052.    assert( blue_scale<=255.0 );
  1053.    assert( alpha_scale<=255.0 );
  1054.  
  1055.    if (depth_bits > 8*sizeof(GLdepth)) {
  1056.       /* can't meet depth buffer requirements */
  1057.       return NULL;
  1058.    }
  1059.    if (stencil_bits > 8*sizeof(GLstencil)) {
  1060.       /* can't meet stencil buffer requirements */
  1061.       return NULL;
  1062.    }
  1063.    if (accum_bits > 8*sizeof(GLaccum)) {
  1064.       /* can't meet accum buffer requirements */
  1065.       return NULL;
  1066.    }
  1067.  
  1068.    vis = (GLvisual *) calloc( 1, sizeof(GLvisual) );
  1069.    if (!vis) {
  1070.       return NULL;
  1071.    }
  1072.  
  1073.    vis->RGBAflag      = rgb_flag;
  1074.    vis->DBflag        = db_flag;
  1075.    vis->RedScale      = red_scale;
  1076.    vis->GreenScale    = green_scale;
  1077.    vis->BlueScale     = blue_scale;
  1078.    vis->AlphaScale    = alpha_scale;
  1079.    if (red_scale) {
  1080.       vis->InvRedScale   = 1.0F / red_scale;
  1081.    }
  1082.    if (green_scale) {
  1083.       vis->InvGreenScale = 1.0F / green_scale;
  1084.    }
  1085.    if (blue_scale) {
  1086.       vis->InvBlueScale  = 1.0F / blue_scale;
  1087.    }
  1088.    if (alpha_scale) {
  1089.       vis->InvAlphaScale = 1.0F / alpha_scale;
  1090.    }
  1091.  
  1092.    vis->RedBits   = red_bits;
  1093.    vis->GreenBits = green_bits;
  1094.    vis->BlueBits  = blue_bits;
  1095.    vis->AlphaBits = alpha_flag ? 8*sizeof(GLubyte) : alpha_bits;
  1096.  
  1097.    vis->IndexBits   = index_bits;
  1098.    vis->DepthBits   = (depth_bits>0) ? 8*sizeof(GLdepth) : 0;
  1099.    vis->AccumBits   = (accum_bits>0) ? 8*sizeof(GLaccum) : 0;
  1100.    vis->StencilBits = (stencil_bits>0) ? 8*sizeof(GLstencil) : 0;
  1101.  
  1102.    if (red_scale==255.0F && green_scale==255.0F
  1103.        && blue_scale==255.0F && alpha_scale==255.0F) {
  1104.       vis->EightBitColor = GL_TRUE;
  1105.    }
  1106.    else {
  1107.       vis->EightBitColor = GL_FALSE;
  1108.    }
  1109.  
  1110.    /* software alpha buffers */
  1111.    if (alpha_flag) {
  1112.       vis->FrontAlphaEnabled = GL_TRUE;
  1113.       if (db_flag) {
  1114.      vis->BackAlphaEnabled = GL_TRUE;
  1115.       }
  1116.    }
  1117.  
  1118.    return vis;
  1119. }
  1120.  
  1121.  
  1122.  
  1123.  
  1124. void gl_destroy_visual( GLvisual *vis )
  1125. {
  1126.    free( vis );
  1127. }
  1128.  
  1129.  
  1130.  
  1131. /*
  1132.  * Allocate the proxy textures.  If we run out of memory part way through
  1133.  * the allocations clean up and return GL_FALSE.
  1134.  * Return:  GL_TRUE=success, GL_FALSE=failure
  1135.  */
  1136. static GLboolean alloc_proxy_textures( GLcontext *ctx )
  1137. {
  1138.    GLboolean out_of_memory;
  1139.    GLint i;
  1140.  
  1141.    ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
  1142.    if (!ctx->Texture.Proxy1D) {
  1143.       return GL_FALSE;
  1144.    }
  1145.  
  1146.    ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
  1147.    if (!ctx->Texture.Proxy2D) {
  1148.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1149.       return GL_FALSE;
  1150.    }
  1151.  
  1152.    ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
  1153.    if (!ctx->Texture.Proxy3D) {
  1154.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1155.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1156.       return GL_FALSE;
  1157.    }
  1158.  
  1159.    out_of_memory = GL_FALSE;
  1160.    for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1161.       ctx->Texture.Proxy1D->Image[i] = gl_alloc_texture_image();
  1162.       ctx->Texture.Proxy2D->Image[i] = gl_alloc_texture_image();
  1163.       ctx->Texture.Proxy3D->Image[i] = gl_alloc_texture_image();
  1164.       if (!ctx->Texture.Proxy1D->Image[i]
  1165.       || !ctx->Texture.Proxy2D->Image[i]
  1166.       || !ctx->Texture.Proxy3D->Image[i]) {
  1167.      out_of_memory = GL_TRUE;
  1168.       }
  1169.    }
  1170.    if (out_of_memory) {
  1171.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1172.      if (ctx->Texture.Proxy1D->Image[i]) {
  1173.         gl_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
  1174.      }
  1175.      if (ctx->Texture.Proxy2D->Image[i]) {
  1176.         gl_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
  1177.      }
  1178.      if (ctx->Texture.Proxy3D->Image[i]) {
  1179.         gl_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
  1180.      }
  1181.       }
  1182.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1183.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1184.       gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
  1185.       return GL_FALSE;
  1186.    }
  1187.    else {
  1188.       return GL_TRUE;
  1189.    }
  1190. }
  1191.  
  1192.  
  1193.  
  1194. /*
  1195.  * Allocate and initialize a GLcontext structure.
  1196.  * Input:  visual - a GLvisual pointer
  1197.  *         sharelist - another context to share display lists with or NULL
  1198.  *         driver_ctx - pointer to device driver's context state struct
  1199.  * Return:  pointer to a new gl_context struct or NULL if error.
  1200.  */
  1201. GLcontext *gl_create_context( GLvisual *visual,
  1202.                   GLcontext *share_list,
  1203.                   void *driver_ctx )
  1204. {
  1205.    GLcontext *ctx;
  1206.  
  1207.    /* do some implementation tests */
  1208.    assert( sizeof(GLbyte) == 1 );
  1209.    assert( sizeof(GLshort) >= 2 );
  1210.    assert( sizeof(GLint) >= 4 );
  1211.    assert( sizeof(GLubyte) == 1 );
  1212.    assert( sizeof(GLushort) >= 2 );
  1213.    assert( sizeof(GLuint) >= 4 );
  1214.  
  1215.    /* misc one-time initializations */
  1216.    gl_init_math();
  1217.    gl_init_lists();
  1218.    gl_init_eval();
  1219.  
  1220.    ctx = (GLcontext *) calloc( 1, sizeof(GLcontext) );
  1221.    if (!ctx) {
  1222.       return NULL;
  1223.    }
  1224.  
  1225.    ctx->DriverCtx = driver_ctx;
  1226.    ctx->Visual = visual;
  1227.    ctx->Buffer = NULL;
  1228.  
  1229.    ctx->VB = gl_alloc_vb();
  1230.    if (!ctx->VB) {
  1231.       free( ctx );
  1232.       return NULL;
  1233.    }
  1234.  
  1235.    ctx->PB = gl_alloc_pb();
  1236.    if (!ctx->PB) {
  1237.       free( ctx->VB );
  1238.       free( ctx );
  1239.       return NULL;
  1240.    }
  1241.  
  1242.    if (share_list) {
  1243.       /* share the group of display lists of another context */
  1244.       ctx->Shared = share_list->Shared;
  1245.    }
  1246.    else {
  1247.       /* allocate new group of display lists */
  1248.       ctx->Shared = alloc_shared_state();
  1249.       if (!ctx->Shared) {
  1250.      free(ctx->VB);
  1251.      free(ctx->PB);
  1252.      free(ctx);
  1253.      return NULL;
  1254.       }
  1255.    }
  1256.    ctx->Shared->RefCount++;
  1257.  
  1258.    initialize_context( ctx );
  1259.  
  1260.    if (visual->DBflag) {
  1261.       ctx->Color.DrawBuffer = GL_BACK;
  1262.       ctx->Pixel.ReadBuffer = GL_BACK;
  1263.    }
  1264.    else {
  1265.       ctx->Color.DrawBuffer = GL_FRONT;
  1266.       ctx->Pixel.ReadBuffer = GL_FRONT;
  1267.    }
  1268.  
  1269. #ifdef PROFILE
  1270.    init_timings( ctx );
  1271. #endif
  1272.  
  1273. #ifdef GL_VERSION_1_1
  1274.    if (!alloc_proxy_textures(ctx)) {
  1275.       free_shared_state(ctx, ctx->Shared);
  1276.       free(ctx->VB);
  1277.       free(ctx->PB);
  1278.       free(ctx);
  1279.       return NULL;
  1280.    }
  1281. #endif
  1282.  
  1283.    gl_init_api_function_pointers( ctx );
  1284.    ctx->API = ctx->Exec;   /* GL_EXECUTE is default */
  1285.  
  1286.    return ctx;
  1287. }
  1288.  
  1289.  
  1290.  
  1291. /*
  1292.  * Destroy a gl_context structure.
  1293.  */
  1294. void gl_destroy_context( GLcontext *ctx )
  1295. {
  1296.    if (ctx) {
  1297. #ifdef PROFILE
  1298.       if (getenv("MESA_PROFILE")) {
  1299.      print_timings( ctx );
  1300.       }
  1301. #endif
  1302.  
  1303.       free( ctx->PB );
  1304.       free( ctx->VB );
  1305.  
  1306.       ctx->Shared->RefCount--;
  1307.       assert(ctx->Shared->RefCount>=0);
  1308.       if (ctx->Shared->RefCount==0) {
  1309.      /* free shared state */
  1310.      free_shared_state( ctx, ctx->Shared );
  1311.       }
  1312.  
  1313.       /* Free proxy texture objects */
  1314.       gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
  1315.       gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
  1316.       gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
  1317.  
  1318.       free( (void *) ctx );
  1319.  
  1320. #ifndef THREADS
  1321.       if (ctx==CC) {
  1322.      CC = NULL;
  1323.       }
  1324. #endif
  1325.  
  1326.    }
  1327. }
  1328.  
  1329.  
  1330.  
  1331. /*
  1332.  * Create a new framebuffer.  A GLframebuffer is a struct which
  1333.  * encapsulates the depth, stencil and accum buffers and related
  1334.  * parameters.
  1335.  * Input:  visual - a GLvisual pointer
  1336.  * Return:  pointer to new GLframebuffer struct or NULL if error.
  1337.  */
  1338. GLframebuffer *gl_create_framebuffer( GLvisual *visual )
  1339. {
  1340.    GLframebuffer *buffer;
  1341.  
  1342.    buffer = (GLframebuffer *) calloc( 1, sizeof(GLframebuffer) );
  1343.    if (!buffer) {
  1344.       return NULL;
  1345.    }
  1346.  
  1347.    buffer->Visual = visual;
  1348.  
  1349.    return buffer;
  1350. }
  1351.  
  1352.  
  1353.  
  1354. /*
  1355.  * Free a framebuffer struct and its buffers.
  1356.  */
  1357. void gl_destroy_framebuffer( GLframebuffer *buffer )
  1358. {
  1359.    if (buffer) {
  1360.       if (buffer->Depth) {
  1361.      free( buffer->Depth );
  1362.       }
  1363.       if (buffer->Accum) {
  1364.      free( buffer->Accum );
  1365.       }
  1366.       if (buffer->Stencil) {
  1367.      free( buffer->Stencil );
  1368.       }
  1369.       if (buffer->FrontAlpha) {
  1370.      free( buffer->FrontAlpha );
  1371.       }
  1372.       if (buffer->BackAlpha) {
  1373.      free( buffer->BackAlpha );
  1374.       }
  1375.       free(buffer);
  1376.    }
  1377. }
  1378.  
  1379.  
  1380.  
  1381. /*
  1382.  * Set the current context, binding the given frame buffer to the context.
  1383.  */
  1384. void gl_make_current( GLcontext *ctx, GLframebuffer *buffer )
  1385. {
  1386. #ifdef THREADS
  1387.    /* TODO: unbind old buffer from context? */
  1388.    set_thread_context( ctx );
  1389. #else
  1390.    if (CC && CC->Buffer) {
  1391.       /* unbind frame buffer from context */
  1392.       CC->Buffer = NULL;
  1393.    }
  1394.    CC = ctx;
  1395. #endif
  1396.  
  1397.    if (ctx && buffer) {
  1398.       /* TODO: check if ctx and buffer's visual match??? */
  1399.       ctx->Buffer = buffer;      /* Bind the frame buffer to the context */
  1400.       ctx->NewState = NEW_ALL;   /* just to be safe */
  1401.       gl_update_state( ctx );
  1402.    }
  1403. }
  1404.  
  1405.  
  1406. /*
  1407.  * Return current context handle.
  1408.  */
  1409. GLcontext *gl_get_current_context( void )
  1410. {
  1411. #ifdef MULTI_THREADING
  1412.    return gl_get_thread_context();
  1413. #else
  1414.    return CC;
  1415. #endif
  1416. }
  1417.  
  1418.  
  1419.  
  1420. /*
  1421.  * Copy attribute groups from one context to another.
  1422.  * Input:  src - source context
  1423.  *         dst - destination context
  1424.  *         mask - bitwise OR of GL_*_BIT flags
  1425.  */
  1426. void gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
  1427. {
  1428.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1429.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  1430.    }
  1431.    if (mask & GL_COLOR_BUFFER_BIT) {
  1432.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  1433.    }
  1434.    if (mask & GL_CURRENT_BIT) {
  1435.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  1436.    }
  1437.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1438.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  1439.    }
  1440.    if (mask & GL_ENABLE_BIT) {
  1441.       /* no op */
  1442.    }
  1443.    if (mask & GL_EVAL_BIT) {
  1444.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  1445.    }
  1446.    if (mask & GL_FOG_BIT) {
  1447.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  1448.    }
  1449.    if (mask & GL_HINT_BIT) {
  1450.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  1451.    }
  1452.    if (mask & GL_LIGHTING_BIT) {
  1453.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  1454.    }
  1455.    if (mask & GL_LINE_BIT) {
  1456.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  1457.    }
  1458.    if (mask & GL_LIST_BIT) {
  1459.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  1460.    }
  1461.    if (mask & GL_PIXEL_MODE_BIT) {
  1462.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  1463.    }
  1464.    if (mask & GL_POINT_BIT) {
  1465.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  1466.    }
  1467.    if (mask & GL_POLYGON_BIT) {
  1468.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  1469.    }
  1470.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1471.       MEMCPY( &dst->PolygonStipple, &src->PolygonStipple, 32*sizeof(GLuint) );
  1472.    }
  1473.    if (mask & GL_SCISSOR_BIT) {
  1474.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  1475.    }
  1476.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1477.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  1478.    }
  1479.    if (mask & GL_TEXTURE_BIT) {
  1480.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  1481.    }
  1482.    if (mask & GL_TRANSFORM_BIT) {
  1483.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  1484.    }
  1485.    if (mask & GL_VIEWPORT_BIT) {
  1486.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  1487.    }
  1488. }
  1489.  
  1490.  
  1491.  
  1492. /*
  1493.  * Someday a GLS library or OpenGL-like debugger may call this function
  1494.  * to register it's own set of API entry points.
  1495.  * Input: ctx - the context to set API pointers for
  1496.  *        api - if NULL, restore original API pointers
  1497.  *              else, set API function table to this table.
  1498.  */
  1499. void gl_set_api_table( GLcontext *ctx, const struct gl_api_table *api )
  1500. {
  1501.    if (api) {
  1502.       MEMCPY( &ctx->API, api, sizeof(struct gl_api_table) );
  1503.    }
  1504.    else {
  1505.       MEMCPY( &ctx->API, &ctx->Exec, sizeof(struct gl_api_table) );
  1506.    }
  1507. }
  1508.  
  1509.  
  1510.  
  1511.  
  1512. /**********************************************************************/
  1513. /*****                Miscellaneous functions                     *****/
  1514. /**********************************************************************/
  1515.  
  1516.  
  1517. /*
  1518.  * This function is called when the Mesa user has stumbled into a code
  1519.  * path which may not be implemented fully or correctly.
  1520.  */
  1521. void gl_problem( const GLcontext *ctx, const char *s )
  1522. {
  1523.    fprintf( stderr, "Mesa implementation error: %s\n", s );
  1524.    fprintf( stderr, "Report to Mesa author.\n" );
  1525. }
  1526.  
  1527.  
  1528.  
  1529. /*
  1530.  * This is called to inform the user that he or she has tried to do
  1531.  * something illogical or if there's likely a bug in their program
  1532.  * (like enabled depth testing without a depth buffer).
  1533.  */
  1534. void gl_warning( const GLcontext *ctx, const char *s )
  1535. {
  1536.    GLboolean debug;
  1537. #ifdef DEBUG
  1538.    debug = GL_TRUE;
  1539. #else
  1540.    if (getenv("MESA_DEBUG")) {
  1541.       debug = GL_TRUE;
  1542.    }
  1543.    else {
  1544.       debug = GL_FALSE;
  1545.    }
  1546. #endif
  1547.    if (debug) {
  1548.       fprintf( stderr, "Mesa warning: %s\n", s );
  1549.    }
  1550. }
  1551.  
  1552.  
  1553.  
  1554. /*
  1555.  * This is Mesa's error handler.  Normally, all that's done is the updating
  1556.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  1557.  * environment variable "MESA_DEBUG" is defined then a real error message
  1558.  * is printed to stderr.
  1559.  * Input:  error - the error value
  1560.  *         s - a diagnostic string
  1561.  */
  1562. void gl_error( GLcontext *ctx, GLenum error, const char *s )
  1563. {
  1564.    GLboolean debug;
  1565.  
  1566. #ifdef DEBUG
  1567.    debug = GL_TRUE;
  1568. #else
  1569.    if (getenv("MESA_DEBUG")) {
  1570.       debug = GL_TRUE;
  1571.    }
  1572.    else {
  1573.       debug = GL_FALSE;
  1574.    }
  1575. #endif
  1576.  
  1577.    if (debug) {
  1578.       char errstr[1000];
  1579.  
  1580.       switch (error) {
  1581.      case GL_NO_ERROR:
  1582.         strcpy( errstr, "GL_NO_ERROR" );
  1583.         break;
  1584.      case GL_INVALID_VALUE:
  1585.         strcpy( errstr, "GL_INVALID_VALUE" );
  1586.         break;
  1587.      case GL_INVALID_ENUM:
  1588.         strcpy( errstr, "GL_INVALID_ENUM" );
  1589.         break;
  1590.      case GL_INVALID_OPERATION:
  1591.         strcpy( errstr, "GL_INVALID_OPERATION" );
  1592.         break;
  1593.      case GL_STACK_OVERFLOW:
  1594.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  1595.         break;
  1596.      case GL_STACK_UNDERFLOW:
  1597.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  1598.         break;
  1599.      case GL_OUT_OF_MEMORY:
  1600.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  1601.         break;
  1602.      default:
  1603.         strcpy( errstr, "unknown" );
  1604.         break;
  1605.       }
  1606.       fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
  1607.    }
  1608.  
  1609.    if (ctx->ErrorValue==GL_NO_ERROR) {
  1610.       ctx->ErrorValue = error;
  1611.    }
  1612.  
  1613.    /* Call device driver's error handler, if any.  This is used on the Mac. */
  1614.    if (ctx->Driver.Error) {
  1615.       (*ctx->Driver.Error)( ctx );
  1616.    }
  1617. }
  1618.  
  1619.  
  1620.  
  1621.  
  1622. GLenum gl_GetError( GLcontext *ctx )
  1623. {
  1624.    GLenum e;
  1625.  
  1626.    if (INSIDE_BEGIN_END(ctx)) {
  1627.       gl_error( ctx, GL_INVALID_OPERATION, "glGetError" );
  1628.       return GL_INVALID_OPERATION;
  1629.    }
  1630.  
  1631.    e = ctx->ErrorValue;
  1632.    ctx->ErrorValue = GL_NO_ERROR;
  1633.    return e;
  1634. }
  1635.  
  1636.  
  1637.  
  1638. void gl_ResizeBuffersMESA( GLcontext *ctx )
  1639. {
  1640.    GLint newsize;
  1641.    GLuint buf_width, buf_height;
  1642.    
  1643.    ctx->NewState |= NEW_ALL;   /* just to be safe */
  1644.  
  1645.    /* ask device driver for size of output buffer */
  1646.    (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
  1647.  
  1648.    /* see if size of device driver's color buffer (window) has changed */
  1649.    newsize = ctx->Buffer->Width!=buf_width || ctx->Buffer->Height!=buf_height;
  1650.  
  1651.    /* save buffer size */
  1652.    ctx->Buffer->Width = buf_width;
  1653.    ctx->Buffer->Height = buf_height;
  1654.  
  1655.    /* Reallocate other buffers if needed. */
  1656.    if (newsize && ctx->Visual->DepthBits>0) {
  1657.       /* reallocate depth buffer */
  1658.       (*ctx->Driver.AllocDepthBuffer)( ctx );
  1659.       /*** if scissoring enabled then clearing can cause a problem ***/
  1660.       /***(*ctx->Driver.ClearDepthBuffer)( ctx );***/
  1661.    }
  1662.    if (newsize && ctx->Visual->StencilBits>0) {
  1663.       /* reallocate stencil buffer */
  1664.       gl_alloc_stencil_buffer( ctx );
  1665.    }
  1666.    if (newsize && ctx->Visual->AccumBits>0) {
  1667.       /* reallocate accum buffer */
  1668.       gl_alloc_accum_buffer( ctx );
  1669.    }
  1670.    if (newsize
  1671.        && (ctx->Visual->FrontAlphaEnabled || ctx->Visual->BackAlphaEnabled)) {
  1672.       gl_alloc_alpha_buffers( ctx );
  1673.    }
  1674. }
  1675.  
  1676.  
  1677.  
  1678.  
  1679. /**********************************************************************/
  1680. /*****                   State update logic                       *****/
  1681. /**********************************************************************/
  1682.  
  1683.  
  1684. /*
  1685.  * Since the device driver may or may not support pixel logic ops we
  1686.  * have to make some extensive tests to determine whether or not
  1687.  * software-implemented logic operations have to be used.
  1688.  */
  1689. static void update_pixel_logic( GLcontext *ctx )
  1690. {
  1691.    if (ctx->Visual->RGBAflag) {
  1692.       /* RGBA mode blending w/ Logic Op */
  1693.       if (ctx->Color.ColorLogicOpEnabled) {
  1694.      if (ctx->Driver.LogicOp
  1695.          && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1696.         /* Device driver can do logic, don't have to do it in software */
  1697.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1698.      }
  1699.      else {
  1700.         /* Device driver can't do logic op so we do it in software */
  1701.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1702.      }
  1703.       }
  1704.       else {
  1705.      /* no logic op */
  1706.      if (ctx->Driver.LogicOp) {
  1707.         (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1708.      }
  1709.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1710.       }
  1711.    }
  1712.    else {
  1713.       /* CI mode Logic Op */
  1714.       if (ctx->Color.IndexLogicOpEnabled) {
  1715.      if (ctx->Driver.LogicOp
  1716.          && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1717.         /* Device driver can do logic, don't have to do it in software */
  1718.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1719.      }
  1720.      else {
  1721.         /* Device driver can't do logic op so we do it in software */
  1722.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1723.      }
  1724.       }
  1725.       else {
  1726.      /* no logic op */
  1727.      if (ctx->Driver.LogicOp) {
  1728.         (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1729.      }
  1730.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1731.       }
  1732.    }
  1733. }
  1734.  
  1735.  
  1736.  
  1737. /*
  1738.  * Check if software implemented RGBA or Color Index masking is needed.
  1739.  */
  1740. static void update_pixel_masking( GLcontext *ctx )
  1741. {
  1742.    if (ctx->Visual->RGBAflag) {
  1743.       if (ctx->Color.ColorMask==0xf) {
  1744.      /* disable masking */
  1745.      if (ctx->Driver.ColorMask) {
  1746.         (void) (*ctx->Driver.ColorMask)( ctx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  1747.      }
  1748.      ctx->Color.SWmasking = GL_FALSE;
  1749.       }
  1750.       else {
  1751.      /* Ask driver to do color masking, if it can't then
  1752.       * do it in software
  1753.       */
  1754.      GLboolean red   = (ctx->Color.ColorMask & 8) ? GL_TRUE : GL_FALSE;
  1755.      GLboolean green = (ctx->Color.ColorMask & 4) ? GL_TRUE : GL_FALSE;
  1756.      GLboolean blue  = (ctx->Color.ColorMask & 2) ? GL_TRUE : GL_FALSE;
  1757.      GLboolean alpha = (ctx->Color.ColorMask & 1) ? GL_TRUE : GL_FALSE;
  1758.      if (ctx->Driver.ColorMask
  1759.          && (*ctx->Driver.ColorMask)( ctx, red, green, blue, alpha )) {
  1760.         ctx->Color.SWmasking = GL_FALSE;
  1761.      }
  1762.      else {
  1763.         ctx->Color.SWmasking = GL_TRUE;
  1764.      }
  1765.       }
  1766.    }
  1767.    else {
  1768.       if (ctx->Color.IndexMask==0xffffffff) {
  1769.      /* disable masking */
  1770.      if (ctx->Driver.IndexMask) {
  1771.         (void) (*ctx->Driver.IndexMask)( ctx, 0xffffffff );
  1772.      }
  1773.      ctx->Color.SWmasking = GL_FALSE;
  1774.       }
  1775.       else {
  1776.      /* Ask driver to do index masking, if it can't then
  1777.       * do it in software
  1778.       */
  1779.      if (ctx->Driver.IndexMask
  1780.          && (*ctx->Driver.IndexMask)( ctx, ctx->Color.IndexMask )) {
  1781.         ctx->Color.SWmasking = GL_FALSE;
  1782.      }
  1783.      else {
  1784.         ctx->Color.SWmasking = GL_TRUE;
  1785.      }
  1786.       }
  1787.    }
  1788. }
  1789.  
  1790.  
  1791.  
  1792. /*
  1793.  * Recompute the value of ctx->RasterMask, etc. according to
  1794.  * the current context.
  1795.  */
  1796. static void update_rasterflags( GLcontext *ctx )
  1797. {
  1798.    ctx->RasterMask = 0;
  1799.  
  1800.    if (ctx->Color.AlphaEnabled)         ctx->RasterMask |= ALPHATEST_BIT;
  1801.    if (ctx->Color.BlendEnabled)         ctx->RasterMask |= BLEND_BIT;
  1802.    if (ctx->Depth.Test)                 ctx->RasterMask |= DEPTH_BIT;
  1803.    if (ctx->Fog.Enabled)                ctx->RasterMask |= FOG_BIT;
  1804.    if (ctx->Color.SWLogicOpEnabled)     ctx->RasterMask |= LOGIC_OP_BIT;
  1805.    if (ctx->Scissor.Enabled)            ctx->RasterMask |= SCISSOR_BIT;
  1806.    if (ctx->Stencil.Enabled)            ctx->RasterMask |= STENCIL_BIT;
  1807.    if (ctx->Color.SWmasking)            ctx->RasterMask |= MASKING_BIT;
  1808.    if (ctx->Visual->FrontAlphaEnabled)  ctx->RasterMask |= ALPHABUF_BIT;
  1809.    if (ctx->Visual->BackAlphaEnabled)   ctx->RasterMask |= ALPHABUF_BIT;
  1810.  
  1811.    if (   ctx->Viewport.X<0
  1812.        || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
  1813.        || ctx->Viewport.Y<0
  1814.        || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
  1815.       ctx->RasterMask |= WINCLIP_BIT;
  1816.    }
  1817.  
  1818.    /* check if drawing to front and back buffers */
  1819.    if (ctx->Color.DrawBuffer==GL_FRONT_AND_BACK) {
  1820.       ctx->RasterMask |= FRONT_AND_BACK_BIT;
  1821.    }
  1822.  
  1823.    /* check if writing to color buffer(s) is disabled */
  1824.    if (ctx->Color.DrawBuffer==GL_NONE) {
  1825.       ctx->RasterMask |= NO_DRAW_BIT;
  1826.    }
  1827.    else if (ctx->Visual->RGBAflag && ctx->Color.ColorMask==0) {
  1828.       ctx->RasterMask |= NO_DRAW_BIT;
  1829.    }
  1830.    else if (!ctx->Visual->RGBAflag && ctx->Color.IndexMask==0) {
  1831.       ctx->RasterMask |= NO_DRAW_BIT;
  1832.    }
  1833. }
  1834.  
  1835.  
  1836. /*
  1837.  * Recompute the value of ctx->ClipMask according to the current context.
  1838.  * ClipMask depends on Texturing and Lighting.
  1839.  */
  1840. static void update_clipmask(GLcontext *ctx)
  1841. {
  1842.    /* Recompute ClipMask (what has to be interpolated when clipping) */
  1843.    ctx->ClipMask = 0;
  1844.    if (ctx->Texture.Enabled) {
  1845.       ctx->ClipMask |= CLIP_TEXTURE_BIT;
  1846.    }
  1847.    if (ctx->Light.ShadeModel==GL_SMOOTH) {
  1848.       if (ctx->Visual->RGBAflag) {
  1849.      ctx->ClipMask |= CLIP_FCOLOR_BIT;
  1850.      if (ctx->Light.Model.TwoSide) {
  1851.         ctx->ClipMask |= CLIP_BCOLOR_BIT;
  1852.      }
  1853.       }
  1854.       else {
  1855.      ctx->ClipMask |= CLIP_FINDEX_BIT;
  1856.      if (ctx->Light.Model.TwoSide) {
  1857.         ctx->ClipMask |= CLIP_BINDEX_BIT;
  1858.      }
  1859.       }
  1860.    }
  1861. }
  1862.  
  1863.  
  1864.  
  1865. /*
  1866.  * If ctx->NewState is non-zero then this function MUST be called before
  1867.  * rendering any primitive.  Basically, function pointers and miscellaneous
  1868.  * flags are updated to reflect the current state of the state machine.
  1869.  */
  1870. void gl_update_state( GLcontext *ctx )
  1871. {
  1872.    if (ctx->NewState & NEW_RASTER_OPS) {
  1873.       update_pixel_logic(ctx);
  1874.       update_pixel_masking(ctx);
  1875.       update_rasterflags(ctx);
  1876.       if (ctx->Driver.Dither) {
  1877.      (*ctx->Driver.Dither)( ctx, ctx->Color.DitherFlag );
  1878.       }
  1879.    }
  1880.  
  1881.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1882.       update_clipmask(ctx);
  1883.    }
  1884.  
  1885.    if (ctx->NewState & NEW_LIGHTING) {
  1886.       gl_update_lighting(ctx);
  1887.       gl_set_color_function(ctx);
  1888.    }
  1889.  
  1890.    if (ctx->NewState & NEW_TEXTURING) {
  1891.       gl_update_texture_state(ctx);
  1892.    }
  1893.  
  1894.    if (ctx->NewState & (NEW_LIGHTING | NEW_TEXTURING)) {
  1895.       /* Check if normal vectors are needed */
  1896.       GLboolean sphereGen = ctx->Texture.Enabled
  1897.      && ((ctx->Texture.GenModeS==GL_SPHERE_MAP
  1898.           && (ctx->Texture.TexGenEnabled & S_BIT))
  1899.          || (ctx->Texture.GenModeT==GL_SPHERE_MAP
  1900.          && (ctx->Texture.TexGenEnabled & T_BIT)));
  1901.       if (ctx->Light.Enabled || sphereGen) {
  1902.      ctx->NeedNormals = GL_TRUE;
  1903.       }
  1904.       else {
  1905.      ctx->NeedNormals = GL_FALSE;
  1906.       }
  1907.    }
  1908.  
  1909.    if (ctx->NewState & NEW_RASTER_OPS) {
  1910.       /* Check if incoming colors can be modified during rasterization */
  1911.       if (ctx->Fog.Enabled ||
  1912.       ctx->Texture.Enabled ||
  1913.       ctx->Color.BlendEnabled ||
  1914.       ctx->Color.SWmasking ||
  1915.       ctx->Color.SWLogicOpEnabled) {
  1916.      ctx->MutablePixels = GL_TRUE;
  1917.       }
  1918.       else {
  1919.      ctx->MutablePixels = GL_FALSE;
  1920.       }
  1921.    }
  1922.  
  1923.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1924.       /* Check if all pixels generated are likely to be the same color */
  1925.       if (ctx->Light.ShadeModel==GL_SMOOTH ||
  1926.       ctx->Light.Enabled ||
  1927.       ctx->Fog.Enabled ||
  1928.       ctx->Texture.Enabled ||
  1929.       ctx->Color.BlendEnabled ||
  1930.       ctx->Color.SWmasking ||
  1931.       ctx->Color.SWLogicOpEnabled) {
  1932.      ctx->MonoPixels = GL_FALSE;       /* pixels probably multicolored */
  1933.       }
  1934.       else {
  1935.      /* pixels will all be same color,
  1936.       * only glColor() can invalidate this.
  1937.       */
  1938.      ctx->MonoPixels = GL_TRUE;
  1939.       }
  1940.    }
  1941.  
  1942.    if (ctx->NewState & NEW_POLYGON) {
  1943.       /* Setup CullBits bitmask */
  1944.       ctx->Polygon.CullBits = 0;
  1945.       if (ctx->Polygon.CullFlag) {
  1946.      if (ctx->Polygon.CullFaceMode==GL_FRONT ||
  1947.          ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1948.         ctx->Polygon.CullBits |= 1;
  1949.      }
  1950.      if (ctx->Polygon.CullFaceMode==GL_BACK ||
  1951.          ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1952.         ctx->Polygon.CullBits |= 2;
  1953.      }
  1954.       }
  1955.       /* Any Polygon offsets enabled? */
  1956.       ctx->Polygon.OffsetAny = ctx->Polygon.OffsetPoint ||
  1957.                    ctx->Polygon.OffsetLine ||
  1958.                    ctx->Polygon.OffsetFill;
  1959.       /* reset Z offsets now */
  1960.       ctx->PointZoffset   = 0.0;
  1961.       ctx->LineZoffset    = 0.0;
  1962.       ctx->PolygonZoffset = 0.0;
  1963.    }
  1964.  
  1965.    if (ctx->NewState & (NEW_POLYGON | NEW_LIGHTING)) {
  1966.       /* Determine if we can directly call the triangle rasterizer */
  1967.       if (   ctx->Polygon.Unfilled
  1968.       || ctx->Polygon.OffsetAny
  1969.       || ctx->Polygon.CullFlag
  1970.       || ctx->Light.Model.TwoSide
  1971.       || ctx->RenderMode!=GL_RENDER) {
  1972.      ctx->DirectTriangles = GL_FALSE;
  1973.       }
  1974.       else {
  1975.      ctx->DirectTriangles = GL_TRUE;
  1976.       }
  1977.    }
  1978.  
  1979.    /* update scissor region */
  1980.    ctx->Buffer->Xmin = 0;
  1981.    ctx->Buffer->Ymin = 0;
  1982.    ctx->Buffer->Xmax = ctx->Buffer->Width-1;
  1983.    ctx->Buffer->Ymax = ctx->Buffer->Height-1;
  1984.    if (ctx->Scissor.Enabled) {
  1985.       if (ctx->Scissor.X > ctx->Buffer->Xmin) {
  1986.      ctx->Buffer->Xmin = ctx->Scissor.X;
  1987.       }
  1988.       if (ctx->Scissor.Y > ctx->Buffer->Ymin) {
  1989.      ctx->Buffer->Ymin = ctx->Scissor.Y;
  1990.       }
  1991.       if (ctx->Scissor.X + ctx->Scissor.Width - 1 < ctx->Buffer->Xmax) {
  1992.      ctx->Buffer->Xmax = ctx->Scissor.X + ctx->Scissor.Width - 1;
  1993.       }
  1994.       if (ctx->Scissor.Y + ctx->Scissor.Height - 1 < ctx->Buffer->Ymax) {
  1995.      ctx->Buffer->Ymax = ctx->Scissor.Y + ctx->Scissor.Height - 1;
  1996.       }
  1997.    }
  1998.  
  1999.    /*
  2000.     * Update Device Driver interface
  2001.     */
  2002.    if (ctx->NewState & NEW_RASTER_OPS) {
  2003.       ctx->Driver.AllocDepthBuffer = gl_alloc_depth_buffer;
  2004.       ctx->Driver.ClearDepthBuffer = gl_clear_depth_buffer;
  2005.       if (ctx->Depth.Mask) {
  2006.      switch (ctx->Depth.Func) {
  2007.         case GL_LESS:
  2008.            ctx->Driver.DepthTestSpan = gl_depth_test_span_less;
  2009.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_less;
  2010.            break;
  2011.         case GL_GREATER:
  2012.            ctx->Driver.DepthTestSpan = gl_depth_test_span_greater;
  2013.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_greater;
  2014.            break;
  2015.         default:
  2016.            ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2017.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2018.      }
  2019.       }
  2020.       else {
  2021.      ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2022.      ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2023.       }
  2024.       ctx->Driver.ReadDepthSpanFloat = gl_read_depth_span_float;
  2025.       ctx->Driver.ReadDepthSpanInt = gl_read_depth_span_int;
  2026.    }
  2027.  
  2028.    ctx->Driver.PointsFunc = NULL;
  2029.    ctx->Driver.LineFunc = NULL;
  2030.    ctx->Driver.TriangleFunc = NULL;
  2031.    ctx->Driver.QuadFunc = NULL;
  2032.    ctx->Driver.RectFunc = NULL;
  2033.  
  2034.    if (ctx->Driver.UpdateState) {
  2035.       (*ctx->Driver.UpdateState)(ctx);
  2036.    }
  2037.  
  2038.    gl_set_point_function(ctx);
  2039.    gl_set_line_function(ctx);
  2040.    gl_set_triangle_function(ctx);
  2041.    gl_set_quad_function(ctx);
  2042.  
  2043.    gl_set_vertex_function(ctx);
  2044.  
  2045.    ctx->NewState = 0;
  2046. }
  2047.  
  2048.