home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / src / context.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  41KB  |  1,420 lines

  1. /* $Id: context.c,v 1.54 1996/05/20 15:02:44 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995-1996  Brian Paul  (brianp@ssec.wisc.edu)
  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.54  1996/05/20  15:02:44  brianp
  27.  * Removed Reserved[] display list array
  28.  *
  29.  * Revision 1.53  1996/05/09  16:46:02  brianp
  30.  * removed CC.ComputePlane
  31.  *
  32.  * Revision 1.52  1996/05/01  15:46:31  brianp
  33.  * fixed bug in update_pixel_logic() for when DD.logicop is NULL
  34.  *
  35.  * Revision 1.51  1996/04/25  20:47:56  brianp
  36.  * added prototype for future gl_new_context()
  37.  * only call DD.color_mask and DD.index_mask if non-null
  38.  * call DD.update_state in gl_update_state()
  39.  *
  40.  * Revision 1.50  1996/03/22  20:54:47  brianp
  41.  * replaced CC.ClipSpans with WINCLIP_BIT RasterMask flag
  42.  *
  43.  * Revision 1.49  1996/02/26  15:14:32  brianp
  44.  * removed CC.Current.Color, replaced by CC.Current.IntColor
  45.  *
  46.  * Revision 1.48  1996/02/26  15:13:24  brianp
  47.  * added lookup tables and code for optimized lighting
  48.  *
  49.  * Revision 1.47  1996/02/19  21:49:26  brianp
  50.  * added support for software alpha buffering
  51.  *
  52.  * Revision 1.46  1996/02/13  17:46:49  brianp
  53.  * call gl_update_lighting() in gl_udpate_state()
  54.  *
  55.  * Revision 1.45  1996/02/06  04:13:02  brianp
  56.  * added CC.Polygon.CullBits code
  57.  *
  58.  * Revision 1.44  1996/02/06  03:24:36  brianp
  59.  * removed gamma correction code
  60.  *
  61.  * Revision 1.43  1996/01/29  19:04:54  brianp
  62.  * test for CC.Polygon.Unfilled for CC.ComputePlane
  63.  *
  64.  * Revision 1.42  1996/01/23  16:54:29  brianp
  65.  * CC.FastDrawPixels wasn't initialized correctly, added more comments
  66.  *
  67.  * Revision 1.41  1996/01/22  15:36:14  brianp
  68.  * new logic in gl_update_state() for CC.MutablePixels and CC.MonoPixels
  69.  *
  70.  * Revision 1.40  1996/01/12  22:31:10  brianp
  71.  * changed default stencil masks to 0xff (1 byte)
  72.  *
  73.  * Revision 1.39  1996/01/11  15:46:56  brianp
  74.  * free stencil buffer, if any, in gl_destroy_context()
  75.  *
  76.  * Revision 1.38  1996/01/09  19:53:04  brianp
  77.  * fixed a memory leak from failing to incr display list reference count
  78.  *
  79.  * Revision 1.37  1996/01/07  22:49:41  brianp
  80.  * call gl_init_lighting() when creating a context
  81.  *
  82.  * Revision 1.36  1996/01/05  01:21:20  brianp
  83.  * added profiling
  84.  *
  85.  * Revision 1.35  1995/12/30  17:14:23  brianp
  86.  * initialize CC.Current.IntColor
  87.  *
  88.  * Revision 1.34  1995/12/30  00:48:26  brianp
  89.  * check for EightBitColor
  90.  *
  91.  * Revision 1.33  1995/12/12  21:43:18  brianp
  92.  * default state of normalization changed to disabled
  93.  *
  94.  * Revision 1.32  1995/11/22  13:35:00  brianp
  95.  * added MutableColors flag, test for RGBA mode before setting GAMMA_BIT
  96.  *
  97.  * Revision 1.31  1995/11/08  22:09:05  brianp
  98.  * changed GL<type> assertions from == to >=
  99.  *
  100.  * Revision 1.30  1995/11/03  17:41:05  brianp
  101.  * removed unused variables
  102.  *
  103.  * Revision 1.29  1995/11/01  21:44:15  brianp
  104.  * added CC.Light.LastEnabled
  105.  *
  106.  * Revision 1.28  1995/10/27  20:28:27  brianp
  107.  * added glPolygonOffsetEXT() support
  108.  *
  109.  * Revision 1.27  1995/10/19  15:45:20  brianp
  110.  * added gamma support
  111.  * new arguments to gl_new_context()
  112.  *
  113.  * Revision 1.26  1995/10/14  16:26:13  brianp
  114.  * enable dithering by default
  115.  * added SWmasking code
  116.  *
  117.  * Revision 1.25  1995/09/25  16:31:54  brianp
  118.  * reorganized front and back material indexing
  119.  *
  120.  * Revision 1.24  1995/09/15  18:47:32  brianp
  121.  * introduced CC.NewState convention
  122.  * use bitmask flag for CC.Texture.TexGenEnabled
  123.  *
  124.  * Revision 1.23  1995/07/25  16:41:54  brianp
  125.  * made changes for using CC.VertexFunc pointer
  126.  *
  127.  * Revision 1.22  1995/07/24  20:34:16  brianp
  128.  * replaced memset() with MEMSET() and memcpy() with MEMCPY()
  129.  *
  130.  * Revision 1.21  1995/06/19  14:52:37  brianp
  131.  * initialize current texture coordinate, per Asif Khan
  132.  *
  133.  * Revision 1.20  1995/06/05  20:26:24  brianp
  134.  * added Unfilled field to gl_polygon struct
  135.  *
  136.  * Revision 1.19  1995/05/22  21:02:41  brianp
  137.  * Release 1.2
  138.  *
  139.  * Revision 1.18  1995/05/17  13:52:37  brianp
  140.  * implemented glIndexMask(0) and glColorMask(0,0,0,0)
  141.  *
  142.  * Revision 1.17  1995/05/17  13:17:22  brianp
  143.  * changed default CC.Mode value to allow use of real OpenGL headers
  144.  * removed need for CC.MajorMode variable
  145.  *
  146.  * Revision 1.16  1995/05/15  16:07:01  brianp
  147.  * implemented shared/nonshared display lists
  148.  *
  149.  * Revision 1.15  1995/05/12  16:30:14  brianp
  150.  * Texture images stored as bytes, not floats
  151.  *
  152.  * Revision 1.14  1995/04/17  13:51:19  brianp
  153.  * added gl_copy_context() function
  154.  *
  155.  * Revision 1.13  1995/03/27  20:31:26  brianp
  156.  * new Texture.Enabled scheme
  157.  *
  158.  * Revision 1.12  1995/03/24  16:59:56  brianp
  159.  * added gl_update_pixel_logic
  160.  *
  161.  * Revision 1.11  1995/03/24  16:11:41  brianp
  162.  * fixed logicop bug in gl_update_rasterflags
  163.  *
  164.  * Revision 1.10  1995/03/16  20:36:38  brianp
  165.  * added call to gl_update_rasterflags in gl_set_context
  166.  *
  167.  * Revision 1.9  1995/03/10  16:26:43  brianp
  168.  * updated for bleding extensions
  169.  *
  170.  * Revision 1.8  1995/03/09  21:40:14  brianp
  171.  * added ModelViewInvValid initializer
  172.  * added ComputePlane test to gl_update_rasterflags
  173.  *
  174.  * Revision 1.7  1995/03/09  19:07:16  brianp
  175.  * added MESA_DEBUG env var support
  176.  *
  177.  * Revision 1.6  1995/03/08  15:10:02  brianp
  178.  * added support for dd_logicop
  179.  *
  180.  * Revision 1.5  1995/03/04  19:29:44  brianp
  181.  * 1.1 beta revision
  182.  *
  183.  * Revision 1.4  1995/03/02  19:17:54  brianp
  184.  * new RasterMask logic, fixed some comments
  185.  *
  186.  * Revision 1.3  1995/02/27  22:48:28  brianp
  187.  * modified for PB
  188.  *
  189.  * Revision 1.2  1995/02/24  15:19:23  brianp
  190.  * *** empty log message ***
  191.  *
  192.  * Revision 1.1  1995/02/24  14:18:45  brianp
  193.  * Initial revision
  194.  *
  195.  */
  196.  
  197.  
  198. /*
  199.  * The gl_context structure holds the current state of the library.
  200.  * Typically, there will be a gl_context structure associated with each
  201.  * window into which we're rendering:
  202.  *    When we open a new rendering window we need a new gl_context.
  203.  *    When we close a rendering window we destroy its gl_context.
  204.  *    When we switch rendering to a different window we change gl_context.
  205.  *
  206.  * Throughout this implementation, references are made to CC which is
  207.  * the Current Context.
  208.  */
  209.  
  210.  
  211.  
  212. #include <assert.h>
  213. #include <math.h>
  214. #include <stdio.h>
  215. #include <stdlib.h>
  216. #include <string.h>
  217. #include "context.h"
  218. #include "dd.h"
  219. #include "draw.h"
  220. #include "eval.h"
  221. #include "light.h"
  222. #include "lines.h"
  223. #include "list.h"
  224. #include "macros.h"
  225. #include "pb.h"
  226. #include "points.h"
  227. #include "polygons.h"
  228. /*#include "triangles.h"*/
  229. #include "vb.h"
  230.  
  231.  
  232.  
  233. /* Copy of Current Context (PUBLIC) */
  234. struct gl_context CC;
  235.  
  236. /* Pointer to Current Context (PRIVATE) */
  237. static struct gl_context *CCptr = NULL;
  238.  
  239.  
  240.  
  241.  
  242. /**********************************************************************/
  243. /*****                   Profiling functions                      *****/
  244. /**********************************************************************/
  245.  
  246. #ifdef PROFILE
  247.  
  248. #include <sys/times.h>
  249. #include <sys/param.h>
  250.  
  251.  
  252. /*
  253.  * Return system time in seconds.
  254.  * NOTE:  this implementation may not be very portable!
  255.  */
  256. GLdouble gl_time( void )
  257. {
  258.    static GLdouble prev_time = 0.0;
  259.    static GLdouble time;
  260.    struct tms tm;
  261.    clock_t clk;
  262.  
  263.    clk = times(&tm);
  264.  
  265. #ifdef CLK_TCK
  266.    time = (double)clk / (double)CLK_TCK;
  267. #else
  268.    time = (double)clk / (double)HZ;
  269. #endif
  270.  
  271.    if (time>prev_time) {
  272.       prev_time = time;
  273.       return time;
  274.    }
  275.    else {
  276.       return prev_time;
  277.    }
  278. }
  279.  
  280.  
  281.  
  282. /*
  283.  * Reset the timing/profiling counters
  284.  */
  285. static void init_timings( struct gl_context *c )
  286. {
  287.    c->BeginEndCount = 0;
  288.    c->BeginEndTime = 0.0;
  289.    c->VertexCount = 0;
  290.    c->VertexTime = 0.0;
  291.    c->PointCount = 0;
  292.    c->PointTime = 0.0;
  293.    c->LineCount = 0;
  294.    c->LineTime = 0.0;
  295.    c->PolygonCount = 0;
  296.    c->PolygonTime = 0.0;
  297.    c->ClearCount = 0;
  298.    c->ClearTime = 0.0;
  299.    c->SwapCount = 0;
  300.    c->SwapTime = 0.0;
  301. }
  302.  
  303.  
  304.  
  305. /*
  306.  * Print the accumulated timing/profiling data.
  307.  */
  308. static void print_timings( struct gl_context *c )
  309. {
  310.    GLdouble beginendrate;
  311.    GLdouble vertexrate;
  312.    GLdouble pointrate;
  313.    GLdouble linerate;
  314.    GLdouble polygonrate;
  315.    GLdouble overhead;
  316.    GLdouble clearrate;
  317.    GLdouble swaprate;
  318.    GLdouble avgvertices;
  319.  
  320.    if (c->BeginEndTime>0.0) {
  321.       beginendrate = c->BeginEndCount / c->BeginEndTime;
  322.    }
  323.    else {
  324.       beginendrate = 0.0;
  325.    }
  326.    if (c->VertexTime>0.0) {
  327.       vertexrate = c->VertexCount / c->VertexTime;
  328.    }
  329.    else {
  330.       vertexrate = 0.0;
  331.    }
  332.    if (c->PointTime>0.0) {
  333.       pointrate = c->PointCount / c->PointTime;
  334.    }
  335.    else {
  336.       pointrate = 0.0;
  337.    }
  338.    if (c->LineTime>0.0) {
  339.       linerate = c->LineCount / c->LineTime;
  340.    }
  341.    else {
  342.       linerate = 0.0;
  343.    }
  344.    if (c->PolygonTime>0.0) {
  345.       polygonrate = c->PolygonCount / c->PolygonTime;
  346.    }
  347.    else {
  348.       polygonrate = 0.0;
  349.    }
  350.    if (c->ClearTime>0.0) {
  351.       clearrate = c->ClearCount / c->ClearTime;
  352.    }
  353.    else {
  354.       clearrate = 0.0;
  355.    }
  356.    if (c->SwapTime>0.0) {
  357.       swaprate = c->SwapCount / c->SwapTime;
  358.    }
  359.    else {
  360.       swaprate = 0.0;
  361.    }
  362.  
  363.    if (c->BeginEndCount>0) {
  364.       avgvertices = (GLdouble) c->VertexCount / (GLdouble) c->BeginEndCount;
  365.    }
  366.    else {
  367.       avgvertices = 0.0;
  368.    }
  369.  
  370.    overhead = c->BeginEndTime - c->VertexTime - c->PointTime
  371.               - c->LineTime - c->PolygonTime;
  372.  
  373.  
  374.    printf("                          Count   Time (s)    Rate (/s) \n");
  375.    printf("--------------------------------------------------------\n");
  376.    printf("glBegin/glEnd           %7d  %8.3f   %10.3f\n",
  377.           c->BeginEndCount, c->BeginEndTime, beginendrate);
  378.    printf("  vertexes transformed  %7d  %8.3f   %10.3f\n",
  379.           c->VertexCount, c->VertexTime, vertexrate );
  380.    printf("  points rasterized     %7d  %8.3f   %10.3f\n",
  381.           c->PointCount, c->PointTime, pointrate );
  382.    printf("  lines rasterized      %7d  %8.3f   %10.3f\n",
  383.           c->LineCount, c->LineTime, linerate );
  384.    printf("  polygons rasterized   %7d  %8.3f   %10.3f\n",
  385.           c->PolygonCount, c->PolygonTime, polygonrate );
  386.    printf("  overhead                       %8.3f\n", overhead );
  387.    printf("glClear                 %7d  %8.3f   %10.3f\n",
  388.           c->ClearCount, c->ClearTime, clearrate );
  389.    printf("SwapBuffers             %7d  %8.3f   %10.3f\n",
  390.           c->SwapCount, c->SwapTime, swaprate );
  391.    printf("\n");
  392.  
  393.    printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
  394. }
  395. #endif
  396.  
  397.  
  398.  
  399.  
  400.  
  401. /**********************************************************************/
  402. /*****       Context allocation, initialization, destroying       *****/
  403. /**********************************************************************/
  404.  
  405.  
  406. /*
  407.  * Allocate and initialize a display list group.
  408.  */
  409. static struct gl_list_group *alloc_display_list_group( void )
  410. {
  411.    struct gl_list_group *lg;
  412.    GLuint i;
  413.  
  414.    lg = (struct gl_list_group*) malloc( sizeof(struct gl_list_group) );
  415.    for (i=0;i<MAX_DISPLAYLISTS;i++) {
  416.       lg->List[i] = NULL;
  417.    }
  418.    lg->RefCount = 0;
  419.    return lg;
  420. }
  421.  
  422.  
  423.  
  424. /*
  425.  * Initialize the nth light.  Note that the defaults for light 0 are
  426.  * different than the other lights.
  427.  */
  428. static void init_light( struct gl_light *l, GLuint n )
  429. {
  430.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  431.    if (n==0) {
  432.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  433.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  434.    }
  435.    else {
  436.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  437.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  438.    }
  439.    ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
  440.    ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
  441.    l->SpotExponent = 0.0;
  442.    gl_compute_spot_exp_table( l );
  443.    l->SpotCutoff = 180.0;
  444.    l->CosCutoff = -1.0;
  445.    l->ConstantAttenuation = 1.0;
  446.    l->LinearAttenuation = 0.0;
  447.    l->QuadraticAttenuation = 0.0;
  448.    l->Enabled = GL_FALSE;
  449. }
  450.  
  451.  
  452.  
  453. static void init_lightmodel( struct gl_lightmodel *lm )
  454. {
  455.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  456.    lm->LocalViewer = GL_FALSE;
  457.    lm->TwoSide = GL_FALSE;
  458. }
  459.  
  460.  
  461. static void init_material( struct gl_material *m )
  462. {
  463.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  464.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  465.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  466.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  467.    m->Shininess = 0.0;
  468.    m->AmbientIndex = 0;
  469.    m->DiffuseIndex = 1;
  470.    m->SpecularIndex = 1;
  471.    gl_compute_material_shine_table( m );
  472. }
  473.  
  474.  
  475.  
  476. /*
  477.  * Initialize a gl_context structure to default values.
  478.  */
  479. void gl_initialize_context( struct gl_context *c )
  480. {
  481.    static GLfloat identity[16] = {
  482.     1.0, 0.0, 0.0, 0.0,
  483.     0.0, 1.0, 0.0, 0.0,
  484.     0.0, 0.0, 1.0, 0.0,
  485.     0.0, 0.0, 0.0, 1.0
  486.    };
  487.    GLuint i;
  488.  
  489.    if (c) {
  490.       /* Transformation matrices and stacks */
  491.       c->ModelViewStackDepth = 0;
  492.       MEMCPY( c->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
  493.       MEMCPY( c->ModelViewInv, identity, 16*sizeof(GLfloat) );
  494.       c->ModelViewInvValid = GL_TRUE;
  495.  
  496.       c->ProjectionStackDepth = 0;
  497.       MEMCPY( c->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
  498.  
  499.       c->TextureStackDepth = 0;
  500.       MEMCPY( c->TextureMatrix, identity, 16*sizeof(GLfloat) );
  501.       c->IdentityTexMat = GL_TRUE;
  502.  
  503.       /* Accumulate buffer group */
  504.       ASSIGN_4V( c->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  505.  
  506.       /* Color buffer group */
  507.       c->Color.IndexMask = 0xffffffff;
  508.       c->Color.ColorMask = 0xf;
  509.       c->Color.SWmasking = GL_FALSE;
  510.       c->Color.ClearIndex = 0;
  511.       ASSIGN_4V( c->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  512.       c->Color.DrawBuffer = GL_FRONT;
  513.       c->Color.AlphaEnabled = GL_FALSE;
  514.       c->Color.AlphaFunc = GL_ALWAYS;
  515.       c->Color.AlphaRef = 0.0;
  516.       c->Color.BlendEnabled = GL_FALSE;
  517.       c->Color.BlendSrc = GL_ONE;
  518.       c->Color.BlendDst = GL_ZERO;
  519.       c->Color.BlendEquation = GL_FUNC_ADD_EXT;
  520.       ASSIGN_4V( c->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  521.       c->Color.LogicOpEnabled = GL_FALSE;
  522.       c->Color.SWLogicOpEnabled = GL_FALSE;
  523.       c->Color.LogicOp = GL_COPY;
  524.       c->Color.DitherFlag = GL_TRUE;
  525.  
  526.       /* Current group */
  527.       c->Current.Index = 1;
  528.       ASSIGN_3V( c->Current.Normal, 0.0, 0.0, 1.0 );
  529.       c->Current.IntColor[0] = (GLint) c->RedScale;
  530.       c->Current.IntColor[1] = (GLint) c->GreenScale;
  531.       c->Current.IntColor[2] = (GLint) c->BlueScale;
  532.       c->Current.IntColor[3] = (GLint) c->AlphaScale;
  533.       ASSIGN_4V( c->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  534.       c->Current.RasterPosValid = GL_TRUE;
  535.       c->Current.RasterIndex = 1;
  536.       ASSIGN_4V( c->Current.TexCoord, 0.0, 0.0, 0.0, 1.0 );
  537.       ASSIGN_4V( c->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  538.       c->Current.EdgeFlag = GL_TRUE;
  539.  
  540.       /* Depth buffer group */
  541.       c->Depth.Test = GL_FALSE;
  542.       c->Depth.Clear = 1.0;
  543.       c->Depth.Func = GL_LESS;
  544.       c->Depth.Mask = GL_TRUE;
  545.  
  546.       /* Evaluators group */
  547.       c->Eval.Map1Color4 = GL_FALSE;
  548.       c->Eval.Map1Index = GL_FALSE;
  549.       c->Eval.Map1Normal = GL_FALSE;
  550.       c->Eval.Map1TextureCoord1 = GL_FALSE;
  551.       c->Eval.Map1TextureCoord2 = GL_FALSE;
  552.       c->Eval.Map1TextureCoord3 = GL_FALSE;
  553.       c->Eval.Map1TextureCoord4 = GL_FALSE;
  554.       c->Eval.Map1Vertex3 = GL_FALSE;
  555.       c->Eval.Map1Vertex4 = GL_FALSE;
  556.       c->Eval.Map2Color4 = GL_FALSE;
  557.       c->Eval.Map2Index = GL_FALSE;
  558.       c->Eval.Map2Normal = GL_FALSE;
  559.       c->Eval.Map2TextureCoord1 = GL_FALSE;
  560.       c->Eval.Map2TextureCoord2 = GL_FALSE;
  561.       c->Eval.Map2TextureCoord3 = GL_FALSE;
  562.       c->Eval.Map2TextureCoord4 = GL_FALSE;
  563.       c->Eval.Map2Vertex3 = GL_FALSE;
  564.       c->Eval.Map2Vertex4 = GL_FALSE;
  565.       c->Eval.AutoNormal = GL_FALSE;
  566.       c->Eval.MapGrid1un = 1;
  567.       c->Eval.MapGrid1u1 = 0.0;
  568.       c->Eval.MapGrid1u2 = 1.0;
  569.       c->Eval.MapGrid2un = 1;
  570.       c->Eval.MapGrid2vn = 1;
  571.       c->Eval.MapGrid2u1 = 0.0;
  572.       c->Eval.MapGrid2u2 = 1.0;
  573.       c->Eval.MapGrid2v1 = 0.0;
  574.       c->Eval.MapGrid2v2 = 1.0;
  575.  
  576.       /* Fog group */
  577.       c->Fog.Enabled = GL_FALSE;
  578.       c->Fog.Mode = GL_EXP;
  579.       ASSIGN_4V( c->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  580.       c->Fog.Index = 0.0;
  581.       c->Fog.Density = 1.0;
  582.       c->Fog.Start = 0.0;
  583.       c->Fog.End = 1.0;
  584.  
  585.       /* Hint group */
  586.       c->Hint.PerspectiveCorrection = GL_DONT_CARE;
  587.       c->Hint.PointSmooth = GL_DONT_CARE;
  588.       c->Hint.LineSmooth = GL_DONT_CARE;
  589.       c->Hint.PolygonSmooth = GL_DONT_CARE;
  590.       c->Hint.Fog = GL_DONT_CARE;
  591.  
  592.       /* Lighting group */
  593.       for (i=0;i<MAX_LIGHTS;i++) {
  594.      init_light( &c->Light.Light[i], i );
  595.       }
  596.       init_lightmodel( &c->Light.Model );
  597.       init_material( &c->Light.Material[0] );
  598.       init_material( &c->Light.Material[1] );
  599.       c->Light.ShadeModel = GL_SMOOTH;
  600.       c->Light.Enabled = GL_FALSE;
  601.       c->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  602.       c->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  603.       c->Light.ColorMaterialEnabled = GL_FALSE;
  604.       c->Light.LastEnabled = -1;
  605.  
  606.       /* Line group */
  607.       c->Line.SmoothFlag = GL_FALSE;
  608.       c->Line.StippleFlag = GL_FALSE;
  609.       c->Line.Width = 1.0;
  610.       c->Line.StipplePattern = 0xffff;
  611.       c->Line.StippleFactor = 1;
  612.  
  613.       /* Display List group */
  614.       c->List.ListBase = 0;
  615.  
  616.       /* Pixel group */
  617.       c->Pixel.RedBias = 0.0;
  618.       c->Pixel.RedScale = 1.0;
  619.       c->Pixel.GreenBias = 0.0;
  620.       c->Pixel.GreenScale = 1.0;
  621.       c->Pixel.BlueBias = 0.0;
  622.       c->Pixel.BlueScale = 1.0;
  623.       c->Pixel.AlphaBias = 0.0;
  624.       c->Pixel.AlphaScale = 1.0;
  625.       c->Pixel.DepthBias = 0.0;
  626.       c->Pixel.DepthScale = 1.0;
  627.       c->Pixel.IndexOffset = 0;
  628.       c->Pixel.IndexShift = 0;
  629.       c->Pixel.ZoomX = 1.0;
  630.       c->Pixel.ZoomY = 1.0;
  631.       c->Pixel.MapColorFlag = GL_FALSE;
  632.       c->Pixel.MapStencilFlag = GL_FALSE;
  633.       c->Pixel.MapStoSsize = 1;
  634.       c->Pixel.MapItoIsize = 1;
  635.       c->Pixel.MapItoRsize = 1;
  636.       c->Pixel.MapItoGsize = 1;
  637.       c->Pixel.MapItoBsize = 1;
  638.       c->Pixel.MapItoAsize = 1;
  639.       c->Pixel.MapRtoRsize = 1;
  640.       c->Pixel.MapGtoGsize = 1;
  641.       c->Pixel.MapBtoBsize = 1;
  642.       c->Pixel.MapAtoAsize = 1;
  643.       c->Pixel.MapStoS[0] = 0;
  644.       c->Pixel.MapItoI[0] = 0;
  645.       c->Pixel.MapItoR[0] = 0.0;
  646.       c->Pixel.MapItoG[0] = 0.0;
  647.       c->Pixel.MapItoB[0] = 0.0;
  648.       c->Pixel.MapItoA[0] = 0.0;
  649.       c->Pixel.MapRtoR[0] = 0.0;
  650.       c->Pixel.MapGtoG[0] = 0.0;
  651.       c->Pixel.MapBtoB[0] = 0.0;
  652.       c->Pixel.MapAtoA[0] = 0.0;
  653.  
  654.       /* Point group */
  655.       c->Point.SmoothFlag = GL_FALSE;
  656.       c->Point.Size = 1.0;
  657.  
  658.       /* Polygon group */
  659.       c->Polygon.CullFlag = GL_FALSE;
  660.       c->Polygon.CullFaceMode = GL_BACK;
  661.       c->Polygon.FrontFace = GL_CCW;
  662.       c->Polygon.FrontMode = GL_FILL;
  663.       c->Polygon.BackMode = GL_FILL;
  664.       c->Polygon.Unfilled = GL_FALSE;
  665.       c->Polygon.SmoothFlag = GL_FALSE;
  666.       c->Polygon.StippleFlag = GL_FALSE;
  667.       c->Polygon.OffsetFactor = 0.0;
  668.       c->Polygon.OffsetBias = 0.0;
  669.       c->Polygon.OffsetEnabled = GL_FALSE;
  670.  
  671.       /* Polygon Stipple group */
  672.       MEMSET( c->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  673.  
  674.       /* Scissor group */
  675.       c->Scissor.Enabled = GL_FALSE;
  676.       c->Scissor.X = 0;
  677.       c->Scissor.Y = 0;
  678.       c->Scissor.Width = 0;
  679.       c->Scissor.Height = 0;
  680.  
  681.       /* Stencil group */
  682.       c->Stencil.Enabled = GL_FALSE;
  683.       c->Stencil.Function = GL_ALWAYS;
  684.       c->Stencil.FailFunc = GL_KEEP;
  685.       c->Stencil.ZPassFunc = GL_KEEP;
  686.       c->Stencil.ZFailFunc = GL_KEEP;
  687.       c->Stencil.Ref = 0;
  688.       c->Stencil.ValueMask = 0xff;
  689.       c->Stencil.Clear = 0;
  690.       c->Stencil.WriteMask = 0xff;
  691.  
  692.       /* Texture group */
  693.       c->Texture.Enabled = 0;
  694.       c->Texture.EnvMode = GL_MODULATE;
  695.       ASSIGN_4V( c->Texture.EnvColor, 0.0, 0.0, 0.0, 0.0 );
  696.       ASSIGN_4V( c->Texture.BorderColor, 0.0, 0.0, 0.0, 0.0 );
  697.       c->Texture.TexGenEnabled = 0;
  698.       c->Texture.GenModeS = GL_EYE_LINEAR;
  699.       c->Texture.GenModeT = GL_EYE_LINEAR;
  700.       c->Texture.GenModeR = GL_EYE_LINEAR;
  701.       c->Texture.GenModeQ = GL_EYE_LINEAR;
  702.       ASSIGN_4V( c->Texture.ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  703.       ASSIGN_4V( c->Texture.ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  704.       ASSIGN_4V( c->Texture.ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  705.       ASSIGN_4V( c->Texture.ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  706.       ASSIGN_4V( c->Texture.EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  707.       ASSIGN_4V( c->Texture.EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  708.       ASSIGN_4V( c->Texture.EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  709.       ASSIGN_4V( c->Texture.EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  710.       c->Texture.WrapS1D = GL_REPEAT;
  711.       c->Texture.WrapT1D = GL_REPEAT;
  712.       c->Texture.WrapS2D = GL_REPEAT;
  713.       c->Texture.WrapT2D = GL_REPEAT;
  714.       c->Texture.MinFilter1D = GL_NEAREST_MIPMAP_LINEAR;
  715.       c->Texture.MagFilter1D = GL_LINEAR;
  716.       c->Texture.MinFilter2D = GL_NEAREST_MIPMAP_LINEAR;
  717.       c->Texture.MagFilter2D = GL_LINEAR;
  718.  
  719.       /* Transformation group */
  720.       c->Transform.MatrixMode = GL_MODELVIEW;
  721.       c->Transform.Normalize = GL_FALSE;
  722.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  723.      c->Transform.ClipEnabled[i] = GL_FALSE;
  724.          ASSIGN_4V( c->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
  725.       }
  726.       c->Transform.AnyClip = GL_FALSE;
  727.  
  728.       /* Viewport group */
  729.       c->Viewport.X = 0;
  730.       c->Viewport.Y = 0;
  731.       c->Viewport.Width = 0;
  732.       c->Viewport.Height = 0;   
  733.       c->Viewport.Near = 0.0;
  734.       c->Viewport.Far = 1.0;
  735.       c->Viewport.Sx = 0.0;  /* Sx, Tx, Sy, Ty are computed later */
  736.       c->Viewport.Tx = 0.0;
  737.       c->Viewport.Sy = 0.0;
  738.       c->Viewport.Ty = 0.0;
  739.       c->Viewport.Sz = 0.5;
  740.       c->Viewport.Tz = 0.5;
  741.  
  742.       /* Pixel transfer */
  743.       c->PackAlignment = 4;
  744.       c->PackRowLength = 0;
  745.       c->PackSkipPixels = 0;
  746.       c->PackSkipRows = 0;
  747.       c->PackSwapBytes = GL_FALSE;
  748.       c->PackLSBFirst = GL_FALSE;
  749.       c->UnpackAlignment = 4;
  750.       c->UnpackRowLength = 0;
  751.       c->UnpackSkipPixels = 0;
  752.       c->UnpackSkipRows = 0;
  753.       c->UnpackSwapBytes = GL_FALSE;
  754.       c->UnpackLSBFirst = GL_FALSE;
  755.  
  756.       /* Feedback */
  757.       c->FeedbackType = GL_2D;   /* TODO: verify */
  758.       c->FeedbackBuffer = NULL;
  759.       c->FeedbackBufferSize = 0;
  760.       c->FeedbackCount = 0;
  761.  
  762.       /* Selection/picking */
  763.       c->SelectBuffer = NULL;
  764.       c->SelectBufferSize = 0;
  765.       c->SelectBufferCount = 0;
  766.       c->SelectHits = 0;
  767.       c->NameStackDepth = 0;
  768.  
  769.       /* Attribute stack */
  770.       c->AttribStackDepth = 0;
  771.  
  772.       /* Texture maps */
  773.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  774.      c->TextureComponents1D[i] = 0;
  775.      c->TextureWidth1D[i] = 0;
  776.      c->TextureBorder1D[i] = 0;
  777.      c->TextureImage1D[i] = NULL;
  778.      c->TextureImage1DDeleteFlag[i] = GL_FALSE;
  779.      c->TextureComponents2D[i] = 0;
  780.      c->TextureWidth2D[i] = 0;
  781.      c->TextureHeight2D[i] = 0;
  782.      c->TextureBorder2D[i] = 0;
  783.      c->TextureImage2D[i] = NULL;
  784.      c->TextureImage2DDeleteFlag[i] = GL_FALSE;
  785.       }
  786.  
  787.       /*** Miscellaneous ***/
  788.       c->NewState = GL_TRUE;
  789.       c->RenderMode = GL_RENDER;
  790.       c->Mode = GL_BITMAP;
  791.  
  792.       c->StippleCounter = 0;
  793.       c->NeedNormals = GL_FALSE;
  794.  
  795.       if (c->RedScale==255.0F && c->GreenScale==255.0F && c->BlueScale==255.0F
  796.           && c->AlphaScale==255.0F) {
  797.          c->EightBitColor = GL_TRUE;
  798.       }
  799.       else {
  800.          c->EightBitColor = GL_FALSE;
  801.       }
  802.       c->FastDrawPixels = c->RGBAflag && c->EightBitColor;
  803.  
  804.       c->VertexFunc = gl_nop_vertex;
  805.       c->PointsFunc = NULL;
  806.       c->LineFunc = NULL;
  807.       c->PolygonFunc = NULL;
  808.       c->AuxPolygonFunc = NULL;
  809.  
  810.       c->CallDepth = 0;
  811.       c->ExecuteFlag = GL_TRUE;
  812.       c->CompileFlag = GL_FALSE;
  813.  
  814.       c->BufferWidth = 0;
  815.       c->BufferHeight = 0;
  816.       c->DepthBuffer = NULL;
  817.       c->AccumBuffer = NULL;
  818.       c->StencilBuffer = NULL;
  819.  
  820.       c->ErrorValue = GL_NO_ERROR;
  821.    }
  822. }
  823.  
  824.  
  825.  
  826. /*
  827.  * Allocate and initialize a gl_context structure.
  828.  * Input:  rgb_flag - GL_TRUE or GL_FALSE to indicate if using RGB mode
  829.  *         redscale, greenscale, bluescale, alphascale - if in RGB mode
  830.  *               these values are used to scale floating point color values
  831.  *               in [0,1] to integers in [0,scale]
  832.  *         db_flag - GL_TRUE or GL_FALSE to indicate if using double buffering
  833.  *         sharelist - another context to share display lists with or NULL
  834.  */
  835. struct gl_context *gl_new_context( GLboolean rgb_flag,
  836.                                    GLfloat red_scale,
  837.                                    GLfloat green_scale,
  838.                                    GLfloat blue_scale,
  839.                                    GLfloat alpha_scale,
  840.                                    GLboolean db_flag,
  841.                                    struct gl_context *share_list )
  842. #ifdef FUTURE
  843. /*
  844.  * Allocate and initialize a gl_context structure.
  845.  * Input:  rgb_flag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
  846.  *         alpha_flag - maintain alpha buffers?
  847.  *         db_flag - double buffering?
  848.  *         depth_size - requested depth buffer depth in bits
  849.  *         stencil_size - requested stencil buffer depth in bits
  850.  *         accum_size - requested bits per accumulation buffer component
  851.  *         red, green, blue_scale - float-to-int color scaling
  852.  *         alpha_scale - ignored if alpha_flag is true
  853.  *         sharelist - another context to share display lists with or NULL
  854.  * Return:  pointer to a new gl_context struct or NULL if error.
  855.  */
  856. struct gl_context *gl_new_context( GLboolean rgb_flag,
  857.                                    GLboolean alpha_flag,
  858.                                    GLboolean db_flag,
  859.                                    GLint depth_size,
  860.                                    GLint stencil_size,
  861.                                    GLint accum_size,
  862.                                    GLfloat red_scale,
  863.                                    GLfloat green_scale,
  864.                                    GLfloat blue_scale,
  865.                                    GLfloat alpha_scale,
  866.                                    struct gl_context *share_list );
  867. #endif
  868. {
  869.    struct gl_context *c;
  870.  
  871.    /* do some implementation tests */
  872.    assert( sizeof(GLbyte) >= 1 );
  873.    assert( sizeof(GLshort) >= 2 );
  874.    assert( sizeof(GLint) >= 4 );
  875.    assert( sizeof(GLubyte) >= 1 );
  876.    assert( sizeof(GLushort) >= 2 );
  877.    assert( sizeof(GLuint) >= 4 );
  878.  
  879.    assert( red_scale<=255.0 );
  880.    assert( green_scale<=255.0 );
  881.    assert( blue_scale<=255.0 );
  882.    assert( alpha_scale<=255.0 );
  883.  
  884. #ifdef FUTURE
  885.    if (depth_size > 8*sizeof(GLdepth)) {
  886.       /* can't meet depth buffer requirements */
  887.       return NULL;
  888.    }
  889.    if (stencil_size > 8*sizeof(GLstencil)) {
  890.       /* can't meet stencil buffer requirements */
  891.       return NULL;
  892.    }
  893.    if (accum_size > 8*sizeof(GLaccum)) {
  894.       /* can't meet accum buffer requirements */
  895.       return NULL;
  896.    }
  897. #endif
  898.  
  899.    gl_init_lists();
  900.    gl_init_eval();
  901.    gl_init_vb();
  902.  
  903.    c = (struct gl_context *) malloc( sizeof(struct gl_context) );
  904.    if (c) {
  905.       c->RGBAflag = rgb_flag;
  906.  
  907.       c->RedScale   = red_scale;
  908.       c->GreenScale = green_scale;
  909.       c->BlueScale  = blue_scale;
  910.       c->AlphaScale = alpha_scale;
  911.  
  912.       gl_initialize_context( c );
  913.  
  914.       if (db_flag) {
  915.          c->DBflag = GL_TRUE;
  916.          c->Color.DrawBuffer = GL_BACK;
  917.          c->Pixel.ReadBuffer = GL_BACK;
  918.       }
  919.       else {
  920.          c->DBflag = GL_FALSE;
  921.          c->Color.DrawBuffer = GL_FRONT;
  922.          c->Pixel.ReadBuffer = GL_FRONT;
  923.       }
  924.  
  925.       if (share_list) {
  926.      /* share the group of display lists of another context */
  927.      c->ListGroup = share_list->ListGroup;
  928.       }
  929.       else {
  930.      /* allocate new group of display lists */
  931.      c->ListGroup = alloc_display_list_group();
  932.       }
  933.       c->ListGroup->RefCount++;
  934.  
  935. #ifdef PROFILE
  936.       init_timings( c );
  937. #endif
  938.  
  939.       /* software alpha buffers */
  940.       {
  941.          char *alpha = getenv("MESA_ALPHA");
  942.          c->FrontAlphaEnabled = c->BackAlphaEnabled = GL_FALSE;
  943.          if (alpha) {
  944.             if (strstr(alpha,"front")) {
  945.                c->FrontAlphaEnabled = GL_TRUE;
  946.             }
  947.             if (strstr(alpha,"back")) {
  948.                c->BackAlphaEnabled = GL_TRUE;
  949.             }
  950.          }
  951.       }
  952.    }
  953.    return c;
  954. }
  955.  
  956.  
  957.  
  958.  
  959. /*
  960.  * Destroy a gl_context structure.
  961.  */
  962. void gl_destroy_context( struct gl_context *c )
  963. {
  964.    if (c) {
  965.  
  966. #ifdef PROFILE
  967.       if (getenv("MESA_PROFILE")) {
  968.          struct gl_context *cc;
  969.  
  970.          if (c==CCptr) {
  971.             cc = &CC;
  972.          }
  973.          else {
  974.             cc = c;
  975.          }
  976.          print_timings( cc );
  977.       }
  978. #endif
  979.  
  980.       if (c->DepthBuffer)
  981.      free(c->DepthBuffer);
  982.       if (c->AccumBuffer)
  983.      free(c->AccumBuffer);
  984.       if (c->StencilBuffer)
  985.          free(c->StencilBuffer);
  986.  
  987.       c->ListGroup->RefCount--;
  988.       assert(c->ListGroup->RefCount>=0);
  989.       if (c->ListGroup->RefCount==0) {
  990.      /* free display list group */
  991.      free( c->ListGroup );
  992.       }
  993.       free( (void *) c );
  994.       if (c==CCptr) {
  995.          CCptr = NULL;
  996.       }
  997.  
  998.    }
  999. }
  1000.  
  1001.  
  1002.  
  1003. /*
  1004.  * Set the current context.
  1005.  */
  1006. void gl_set_context( struct gl_context *c )
  1007. {
  1008.    if (c) {
  1009.       /* "write back" current context */
  1010.       if (CCptr) {
  1011.          MEMCPY( CCptr, &CC, sizeof(struct gl_context) );
  1012.       }
  1013.       /* "load" new context */
  1014.       CCptr = c;
  1015.       MEMCPY( &CC, c, sizeof(struct gl_context) );
  1016.    }
  1017.    PB_INIT( GL_BITMAP );
  1018.  
  1019.    /*
  1020.     * Initialize the Device Driver function pointer table.
  1021.     * This function (gl_set_context) is called by the device driver.
  1022.     * After this function (gl_set_context) has returned the device driver
  1023.     * should update the DD pointers as needed.
  1024.     * Why call gl_init_dd_function_table() here at all?  To support "old"
  1025.     * device drivers.
  1026.     */
  1027.    gl_init_dd_function_table();
  1028.  
  1029.    CC.NewState = GL_TRUE;
  1030. }
  1031.  
  1032.  
  1033.  
  1034. /*
  1035.  * Copy attribute groups from one context to another.
  1036.  * Input:  src - source context
  1037.  *         dst - destination context
  1038.  *         mask - bitwise OR of GL_*_BIT flags
  1039.  */
  1040. void gl_copy_context( struct gl_context *src, struct gl_context *dst,
  1041.               GLuint mask )
  1042. {
  1043.    if (src==CCptr) {
  1044.       src = &CC;
  1045.    }
  1046.    else if (dst==CCptr) {
  1047.       dst = &CC;
  1048.    }
  1049.  
  1050.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1051.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  1052.    }
  1053.    if (mask & GL_COLOR_BUFFER_BIT) {
  1054.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  1055.    }
  1056.    if (mask & GL_CURRENT_BIT) {
  1057.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  1058.    }
  1059.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1060.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  1061.    }
  1062.    if (mask & GL_ENABLE_BIT) {
  1063.       /* no op */
  1064.    }
  1065.    if (mask & GL_EVAL_BIT) {
  1066.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  1067.    }
  1068.    if (mask & GL_FOG_BIT) {
  1069.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  1070.    }
  1071.    if (mask & GL_HINT_BIT) {
  1072.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  1073.    }
  1074.    if (mask & GL_LIGHTING_BIT) {
  1075.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  1076.    }
  1077.    if (mask & GL_LINE_BIT) {
  1078.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  1079.    }
  1080.    if (mask & GL_LIST_BIT) {
  1081.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  1082.    }
  1083.    if (mask & GL_PIXEL_MODE_BIT) {
  1084.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  1085.    }
  1086.    if (mask & GL_POINT_BIT) {
  1087.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  1088.    }
  1089.    if (mask & GL_POLYGON_BIT) {
  1090.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  1091.    }
  1092.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1093.       MEMCPY( &dst->PolygonStipple, &src->PolygonStipple, 32*sizeof(GLuint) );
  1094.    }
  1095.    if (mask & GL_SCISSOR_BIT) {
  1096.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  1097.    }
  1098.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1099.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  1100.    }
  1101.    if (mask & GL_TEXTURE_BIT) {
  1102.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  1103.    }
  1104.    if (mask & GL_TRANSFORM_BIT) {
  1105.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  1106.    }
  1107.    if (mask & GL_VIEWPORT_BIT) {
  1108.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  1109.    }
  1110. }
  1111.  
  1112.  
  1113.  
  1114. /*
  1115.  * This is Mesa's error handler.  Normally, all that's done is the updating
  1116.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  1117.  * environment variable "MESA_DEBUG" is defined then a real error message
  1118.  * is printed to stderr.
  1119.  * Input:  error - the error value
  1120.  *         s - a diagnostic string
  1121.  */
  1122. void gl_error( GLenum error, char *s )
  1123. {
  1124.    GLboolean debug;
  1125.  
  1126. #ifdef DEBUG
  1127.    debug = GL_TRUE;
  1128. #else
  1129.    if (getenv("MESA_DEBUG")) {
  1130.       debug = GL_TRUE;
  1131.    }
  1132.    else {
  1133.       debug = GL_FALSE;
  1134.    }
  1135. #endif
  1136.  
  1137.    if (debug) {
  1138.       char errstr[1000];
  1139.  
  1140.       switch (error) {
  1141.      case GL_NO_ERROR:
  1142.         strcpy( errstr, "GL_NO_ERROR" );
  1143.         break;
  1144.      case GL_INVALID_VALUE:
  1145.         strcpy( errstr, "GL_INVALID_VALUE" );
  1146.         break;
  1147.      case GL_INVALID_ENUM:
  1148.         strcpy( errstr, "GL_INVALID_ENUM" );
  1149.         break;
  1150.      case GL_INVALID_OPERATION:
  1151.         strcpy( errstr, "GL_INVALID_OPERATION" );
  1152.         break;
  1153.      case GL_STACK_OVERFLOW:
  1154.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  1155.         break;
  1156.      case GL_STACK_UNDERFLOW:
  1157.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  1158.         break;
  1159.      case GL_OUT_OF_MEMORY:
  1160.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  1161.         break;
  1162.      default:
  1163.         strcpy( errstr, "unknown" );
  1164.         break;
  1165.       }
  1166.       fprintf( stderr, "Mesa Error (%s): %s\n", errstr, s );
  1167.    }
  1168.  
  1169.    if (CC.ErrorValue==GL_NO_ERROR) {
  1170.       CC.ErrorValue = error;
  1171.    }
  1172. }
  1173.  
  1174.  
  1175.  
  1176. GLenum glGetError( void )
  1177. {
  1178.    GLenum e;
  1179.  
  1180.    if (!CCptr) {
  1181.       /* No current context */
  1182.       return GL_NO_ERROR;
  1183.    }
  1184.  
  1185.    if (INSIDE_BEGIN_END) {
  1186.       gl_error( GL_INVALID_OPERATION, "glGetError" );
  1187.       return GL_INVALID_OPERATION;
  1188.    }
  1189.  
  1190.    e = CC.ErrorValue;
  1191.    CC.ErrorValue = GL_NO_ERROR;
  1192.    return e;
  1193. }
  1194.  
  1195.  
  1196.  
  1197. /*
  1198.  * Since the device driver may or may not support pixel logic ops we
  1199.  * have to make some extensive tests to determine whether or not
  1200.  * software-implemented logic operations have to be used.
  1201.  */
  1202. static void update_pixel_logic( void )
  1203. {
  1204.    if (CC.RGBAflag) {
  1205.       /* RGBA mode blending w/ Logic Op */
  1206.       if (CC.Color.BlendEnabled && CC.Color.BlendEquation==GL_LOGIC_OP) {
  1207.      if (DD.logicop && (*DD.logicop)( CC.Color.LogicOp )) {
  1208.         /* Device driver can do logic, don't have to do it in software */
  1209.         CC.Color.SWLogicOpEnabled = GL_FALSE;
  1210.      }
  1211.      else {
  1212.         /* Device driver can't do logic op so we do it in software */
  1213.         CC.Color.SWLogicOpEnabled = GL_TRUE;
  1214.      }
  1215.       }
  1216.       else {
  1217.      /* no logic op */
  1218.      if (DD.logicop) {
  1219.             (void) (*DD.logicop)( GL_COPY );
  1220.          }
  1221.      CC.Color.SWLogicOpEnabled = GL_FALSE;
  1222.       }
  1223.    }
  1224.    else {
  1225.       /* CI mode Logic Op */
  1226.       if (CC.Color.LogicOpEnabled) {
  1227.      if (DD.logicop && (*DD.logicop)( CC.Color.LogicOp )) {
  1228.         /* Device driver can do logic, don't have to do it in software */
  1229.         CC.Color.SWLogicOpEnabled = GL_FALSE;
  1230.      }
  1231.      else {
  1232.         /* Device driver can't do logic op so we do it in software */
  1233.         CC.Color.SWLogicOpEnabled = GL_TRUE;
  1234.      }
  1235.       }
  1236.       else {
  1237.      /* no logic op */
  1238.      if (DD.logicop) {
  1239.             (void) (*DD.logicop)( GL_COPY );
  1240.          }
  1241.      CC.Color.SWLogicOpEnabled = GL_FALSE;
  1242.       }
  1243.    }
  1244. }
  1245.  
  1246.  
  1247.  
  1248. /*
  1249.  * Check if software implemented RGBA or Color Index masking is needed.
  1250.  */
  1251. static void update_pixel_masking( void )
  1252. {
  1253.    if (CC.RGBAflag) {
  1254.       if (CC.Color.ColorMask==0xf) {
  1255.          /* disable masking */
  1256.          if (DD.color_mask) {
  1257.             (void) (*DD.color_mask)( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  1258.          }
  1259.          CC.Color.SWmasking = GL_FALSE;
  1260.       }
  1261.       else {
  1262.          /* Ask DD to do color masking, if it can't we'll do it in software */
  1263.          GLboolean red   = (CC.Color.ColorMask & 8) ? GL_TRUE : GL_FALSE;
  1264.          GLboolean green = (CC.Color.ColorMask & 4) ? GL_TRUE : GL_FALSE;
  1265.          GLboolean blue  = (CC.Color.ColorMask & 2) ? GL_TRUE : GL_FALSE;
  1266.          GLboolean alpha = (CC.Color.ColorMask & 1) ? GL_TRUE : GL_FALSE;
  1267.          if (DD.color_mask && (*DD.color_mask)( red, green, blue, alpha )) {
  1268.             CC.Color.SWmasking = GL_FALSE;
  1269.          }
  1270.          else {
  1271.             CC.Color.SWmasking = GL_TRUE;
  1272.          }
  1273.       }
  1274.    }
  1275.    else {
  1276.       if (CC.Color.IndexMask==0xffffffff) {
  1277.          /* disable masking */
  1278.          if (DD.index_mask) {
  1279.             (void) (*DD.index_mask)( 0xffffffff );
  1280.          }
  1281.          CC.Color.SWmasking = GL_FALSE;
  1282.       }
  1283.       else {
  1284.          /* Ask DD to do index masking, if it can't we'll do it in software */
  1285.          if (DD.index_mask && (*DD.index_mask)( CC.Color.IndexMask )) {
  1286.             CC.Color.SWmasking = GL_FALSE;
  1287.          }
  1288.          else {
  1289.             CC.Color.SWmasking = GL_TRUE;
  1290.          }
  1291.       }
  1292.    }
  1293. }
  1294.  
  1295.  
  1296.  
  1297. /*
  1298.  * Recompute the value of CC.RasterMask, CC.ClipMask, etc. according to
  1299.  * the current context.
  1300.  */
  1301. static void update_rasterflags( void )
  1302. {
  1303.    CC.RasterMask = 0;
  1304.  
  1305.    if (CC.Color.AlphaEnabled)        CC.RasterMask |= ALPHATEST_BIT;
  1306.    if (CC.Color.BlendEnabled)        CC.RasterMask |= BLEND_BIT;
  1307.    if (CC.Depth.Test)            CC.RasterMask |= DEPTH_BIT;
  1308.    if (CC.Fog.Enabled)            CC.RasterMask |= FOG_BIT;
  1309.    if (CC.Color.SWLogicOpEnabled)    CC.RasterMask |= LOGIC_OP_BIT;
  1310.    if (CC.Scissor.Enabled)        CC.RasterMask |= SCISSOR_BIT;
  1311.    if (CC.Stencil.Enabled)        CC.RasterMask |= STENCIL_BIT;
  1312.    if (CC.Color.SWmasking)        CC.RasterMask |= MASKING_BIT;
  1313.    if (CC.FrontAlphaEnabled)        CC.RasterMask |= ALPHABUF_BIT;
  1314.    if (CC.BackAlphaEnabled)        CC.RasterMask |= ALPHABUF_BIT;
  1315.  
  1316.    if (CC.Viewport.X<0 || CC.Viewport.X+CC.Viewport.Width>CC.BufferWidth ||
  1317.        CC.Viewport.Y<0 || CC.Viewport.Y+CC.Viewport.Height>CC.BufferHeight) {
  1318.       CC.RasterMask |= WINCLIP_BIT;
  1319.    }
  1320.  
  1321.    /* Recompute ClipMask (what has to be interpolated when clipping) */
  1322.    CC.ClipMask = 0;
  1323.    if (CC.Texture.Enabled) {
  1324.       CC.ClipMask |= CLIP_TEXTURE_BIT;
  1325.    }
  1326.    if (CC.Light.ShadeModel==GL_SMOOTH) {
  1327.       if (CC.RGBAflag) {
  1328.      CC.ClipMask |= CLIP_FCOLOR_BIT;
  1329.      if (CC.Light.Model.TwoSide) {
  1330.         CC.ClipMask |= CLIP_BCOLOR_BIT;
  1331.      }
  1332.       }
  1333.       else {
  1334.      CC.ClipMask |= CLIP_FINDEX_BIT;
  1335.      if (CC.Light.Model.TwoSide) {
  1336.         CC.ClipMask |= CLIP_BINDEX_BIT;
  1337.      }
  1338.       }
  1339.    }
  1340. }
  1341.  
  1342.  
  1343.  
  1344. /*
  1345.  * If CC.NewState==GL_TRUE then this function MUST be called before
  1346.  * rendering any primitive.  Basically, function pointers and miscellaneous
  1347.  * flags are updated to reflect the current state of the state machine.
  1348.  */
  1349. void gl_update_state( void )
  1350. {
  1351.    update_pixel_logic();
  1352.    update_pixel_masking();
  1353.    update_rasterflags();
  1354.  
  1355.    gl_update_lighting();
  1356.  
  1357.    /* Check if normal vectors are needed */
  1358.    if (CC.Light.Enabled
  1359.        || (CC.Texture.GenModeS==GL_SPHERE_MAP
  1360.            && (CC.Texture.TexGenEnabled & S_BIT))
  1361.        || (CC.Texture.GenModeT==GL_SPHERE_MAP
  1362.            && (CC.Texture.TexGenEnabled & T_BIT))) {
  1363.       CC.NeedNormals = GL_TRUE;
  1364.    }
  1365.    else {
  1366.       CC.NeedNormals = GL_FALSE;
  1367.    }
  1368.  
  1369.    /* Check if incoming colors can be modified during rasterization */
  1370.    if (CC.Fog.Enabled ||
  1371.        CC.Texture.Enabled ||
  1372.        CC.Color.BlendEnabled ||
  1373.        CC.Color.SWmasking ||
  1374.        CC.Color.SWLogicOpEnabled) {
  1375.       CC.MutablePixels = GL_TRUE;
  1376.    }
  1377.    else {
  1378.       CC.MutablePixels = GL_FALSE;
  1379.    }
  1380.  
  1381.    /* Check if all pixels generated are likely to be the same color */
  1382.    if (CC.Light.ShadeModel==GL_SMOOTH ||
  1383.        CC.Light.Enabled ||
  1384.        CC.Fog.Enabled ||
  1385.        CC.Texture.Enabled ||
  1386.        CC.Color.BlendEnabled ||
  1387.        CC.Color.SWmasking ||
  1388.        CC.Color.SWLogicOpEnabled) {
  1389.       CC.MonoPixels = GL_FALSE;       /* pixels probably multicolored */
  1390.    }
  1391.    else {
  1392.       /* pixels will all be same color, only glColor() can invalidate this. */
  1393.       CC.MonoPixels = GL_TRUE;
  1394.    }
  1395.  
  1396.    CC.Polygon.CullBits = 0;
  1397.    if (CC.Polygon.CullFlag) {
  1398.       if (CC.Polygon.CullFaceMode==GL_FRONT ||
  1399.           CC.Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1400.          CC.Polygon.CullBits |= 1;
  1401.       }
  1402.       if (CC.Polygon.CullFaceMode==GL_BACK ||
  1403.           CC.Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  1404.          CC.Polygon.CullBits |= 2;
  1405.       }
  1406.    }
  1407.  
  1408.    gl_set_point_function();
  1409.    gl_set_line_function();
  1410.    gl_set_polygon_function();
  1411.    /*gl_set_triangle_function();*/
  1412.  
  1413.    if (DD.update_state) {
  1414.       (*DD.update_state)();
  1415.    }
  1416.  
  1417.    CC.NewState = GL_FALSE;
  1418. }
  1419.  
  1420.