Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  5.  * Copyright (C) 2009  VMware, Inc.   All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included
  15.  * in all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23.  * OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26. #include "glheader.h"
  27. #include "imports.h"
  28. #include "accum.h"
  29. #include "arrayobj.h"
  30. #include "attrib.h"
  31. #include "blend.h"
  32. #include "buffers.h"
  33. #include "bufferobj.h"
  34. #include "clear.h"
  35. #include "colormac.h"
  36. #include "context.h"
  37. #include "depth.h"
  38. #include "enable.h"
  39. #include "enums.h"
  40. #include "fog.h"
  41. #include "hint.h"
  42. #include "light.h"
  43. #include "lines.h"
  44. #include "macros.h"
  45. #include "matrix.h"
  46. #include "multisample.h"
  47. #include "points.h"
  48. #include "polygon.h"
  49. #include "shared.h"
  50. #include "scissor.h"
  51. #include "stencil.h"
  52. #include "texenv.h"
  53. #include "texgen.h"
  54. #include "texobj.h"
  55. #include "texparam.h"
  56. #include "texstate.h"
  57. #include "varray.h"
  58. #include "viewport.h"
  59. #include "mtypes.h"
  60. #include "main/dispatch.h"
  61. #include "hash.h"
  62. #include <stdbool.h>
  63.  
  64.  
  65. /**
  66.  * glEnable()/glDisable() attribute group (GL_ENABLE_BIT).
  67.  */
  68. struct gl_enable_attrib
  69. {
  70.    GLboolean AlphaTest;
  71.    GLboolean AutoNormal;
  72.    GLboolean Blend;
  73.    GLbitfield ClipPlanes;
  74.    GLboolean ColorMaterial;
  75.    GLboolean CullFace;
  76.    GLboolean DepthClamp;
  77.    GLboolean DepthTest;
  78.    GLboolean Dither;
  79.    GLboolean Fog;
  80.    GLboolean Light[MAX_LIGHTS];
  81.    GLboolean Lighting;
  82.    GLboolean LineSmooth;
  83.    GLboolean LineStipple;
  84.    GLboolean IndexLogicOp;
  85.    GLboolean ColorLogicOp;
  86.  
  87.    GLboolean Map1Color4;
  88.    GLboolean Map1Index;
  89.    GLboolean Map1Normal;
  90.    GLboolean Map1TextureCoord1;
  91.    GLboolean Map1TextureCoord2;
  92.    GLboolean Map1TextureCoord3;
  93.    GLboolean Map1TextureCoord4;
  94.    GLboolean Map1Vertex3;
  95.    GLboolean Map1Vertex4;
  96.    GLboolean Map2Color4;
  97.    GLboolean Map2Index;
  98.    GLboolean Map2Normal;
  99.    GLboolean Map2TextureCoord1;
  100.    GLboolean Map2TextureCoord2;
  101.    GLboolean Map2TextureCoord3;
  102.    GLboolean Map2TextureCoord4;
  103.    GLboolean Map2Vertex3;
  104.    GLboolean Map2Vertex4;
  105.  
  106.    GLboolean Normalize;
  107.    GLboolean PixelTexture;
  108.    GLboolean PointSmooth;
  109.    GLboolean PolygonOffsetPoint;
  110.    GLboolean PolygonOffsetLine;
  111.    GLboolean PolygonOffsetFill;
  112.    GLboolean PolygonSmooth;
  113.    GLboolean PolygonStipple;
  114.    GLboolean RescaleNormals;
  115.    GLboolean Scissor;
  116.    GLboolean Stencil;
  117.    GLboolean StencilTwoSide;          /* GL_EXT_stencil_two_side */
  118.    GLboolean MultisampleEnabled;      /* GL_ARB_multisample */
  119.    GLboolean SampleAlphaToCoverage;   /* GL_ARB_multisample */
  120.    GLboolean SampleAlphaToOne;        /* GL_ARB_multisample */
  121.    GLboolean SampleCoverage;          /* GL_ARB_multisample */
  122.    GLboolean RasterPositionUnclipped; /* GL_IBM_rasterpos_clip */
  123.  
  124.    GLbitfield Texture[MAX_TEXTURE_UNITS];
  125.    GLbitfield TexGen[MAX_TEXTURE_UNITS];
  126.  
  127.    /* GL_ARB_vertex_program */
  128.    GLboolean VertexProgram;
  129.    GLboolean VertexProgramPointSize;
  130.    GLboolean VertexProgramTwoSide;
  131.  
  132.    /* GL_ARB_fragment_program */
  133.    GLboolean FragmentProgram;
  134.  
  135.    /* GL_ARB_point_sprite / GL_NV_point_sprite */
  136.    GLboolean PointSprite;
  137.    GLboolean FragmentShaderATI;
  138.  
  139.    /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
  140.    GLboolean sRGBEnabled;
  141. };
  142.  
  143.  
  144. /**
  145.  * Node for the attribute stack.
  146.  */
  147. struct gl_attrib_node
  148. {
  149.    GLbitfield kind;
  150.    void *data;
  151.    struct gl_attrib_node *next;
  152. };
  153.  
  154.  
  155.  
  156. /**
  157.  * Special struct for saving/restoring texture state (GL_TEXTURE_BIT)
  158.  */
  159. struct texture_state
  160. {
  161.    struct gl_texture_attrib Texture;  /**< The usual context state */
  162.  
  163.    /** to save per texture object state (wrap modes, filters, etc): */
  164.    struct gl_texture_object SavedObj[MAX_TEXTURE_UNITS][NUM_TEXTURE_TARGETS];
  165.  
  166.    /**
  167.     * To save references to texture objects (so they don't get accidentally
  168.     * deleted while saved in the attribute stack).
  169.     */
  170.    struct gl_texture_object *SavedTexRef[MAX_TEXTURE_UNITS][NUM_TEXTURE_TARGETS];
  171.  
  172.    /* We need to keep a reference to the shared state.  That's where the
  173.     * default texture objects are kept.  We don't want that state to be
  174.     * freed while the attribute stack contains pointers to any default
  175.     * texture objects.
  176.     */
  177.    struct gl_shared_state *SharedRef;
  178. };
  179.  
  180.  
  181. /**
  182.  * Allocate new attribute node of given type/kind.  Attach payload data.
  183.  * Insert it into the linked list named by 'head'.
  184.  */
  185. static void
  186. save_attrib_data(struct gl_attrib_node **head,
  187.                  GLbitfield kind, void *payload)
  188. {
  189.    struct gl_attrib_node *n = MALLOC_STRUCT(gl_attrib_node);
  190.    if (n) {
  191.       n->kind = kind;
  192.       n->data = payload;
  193.       /* insert at head */
  194.       n->next = *head;
  195.       *head = n;
  196.    }
  197.    else {
  198.       /* out of memory! */
  199.    }
  200. }
  201.  
  202.  
  203. void GLAPIENTRY
  204. _mesa_PushAttrib(GLbitfield mask)
  205. {
  206.    struct gl_attrib_node *head;
  207.  
  208.    GET_CURRENT_CONTEXT(ctx);
  209.  
  210.    if (MESA_VERBOSE & VERBOSE_API)
  211.       _mesa_debug(ctx, "glPushAttrib %x\n", (int) mask);
  212.  
  213.    if (ctx->AttribStackDepth >= MAX_ATTRIB_STACK_DEPTH) {
  214.       _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
  215.       return;
  216.    }
  217.  
  218.    /* Build linked list of attribute nodes which save all attribute */
  219.    /* groups specified by the mask. */
  220.    head = NULL;
  221.  
  222.    if (mask & GL_ACCUM_BUFFER_BIT) {
  223.       struct gl_accum_attrib *attr;
  224.       attr = MALLOC_STRUCT( gl_accum_attrib );
  225.       memcpy( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
  226.       save_attrib_data(&head, GL_ACCUM_BUFFER_BIT, attr);
  227.    }
  228.  
  229.    if (mask & GL_COLOR_BUFFER_BIT) {
  230.       GLuint i;
  231.       struct gl_colorbuffer_attrib *attr;
  232.       attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
  233.       memcpy( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
  234.       /* push the Draw FBO's DrawBuffer[] state, not ctx->Color.DrawBuffer[] */
  235.       for (i = 0; i < ctx->Const.MaxDrawBuffers; i ++)
  236.          attr->DrawBuffer[i] = ctx->DrawBuffer->ColorDrawBuffer[i];
  237.       save_attrib_data(&head, GL_COLOR_BUFFER_BIT, attr);
  238.    }
  239.  
  240.    if (mask & GL_CURRENT_BIT) {
  241.       struct gl_current_attrib *attr;
  242.       FLUSH_CURRENT( ctx, 0 );
  243.       attr = MALLOC_STRUCT( gl_current_attrib );
  244.       memcpy( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
  245.       save_attrib_data(&head, GL_CURRENT_BIT, attr);
  246.    }
  247.  
  248.    if (mask & GL_DEPTH_BUFFER_BIT) {
  249.       struct gl_depthbuffer_attrib *attr;
  250.       attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
  251.       memcpy( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
  252.       save_attrib_data(&head, GL_DEPTH_BUFFER_BIT, attr);
  253.    }
  254.  
  255.    if (mask & GL_ENABLE_BIT) {
  256.       struct gl_enable_attrib *attr;
  257.       GLuint i;
  258.       attr = MALLOC_STRUCT( gl_enable_attrib );
  259.       /* Copy enable flags from all other attributes into the enable struct. */
  260.       attr->AlphaTest = ctx->Color.AlphaEnabled;
  261.       attr->AutoNormal = ctx->Eval.AutoNormal;
  262.       attr->Blend = ctx->Color.BlendEnabled;
  263.       attr->ClipPlanes = ctx->Transform.ClipPlanesEnabled;
  264.       attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
  265.       attr->CullFace = ctx->Polygon.CullFlag;
  266.       attr->DepthClamp = ctx->Transform.DepthClamp;
  267.       attr->DepthTest = ctx->Depth.Test;
  268.       attr->Dither = ctx->Color.DitherFlag;
  269.       attr->Fog = ctx->Fog.Enabled;
  270.       for (i = 0; i < ctx->Const.MaxLights; i++) {
  271.          attr->Light[i] = ctx->Light.Light[i].Enabled;
  272.       }
  273.       attr->Lighting = ctx->Light.Enabled;
  274.       attr->LineSmooth = ctx->Line.SmoothFlag;
  275.       attr->LineStipple = ctx->Line.StippleFlag;
  276.       attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
  277.       attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
  278.       attr->Map1Color4 = ctx->Eval.Map1Color4;
  279.       attr->Map1Index = ctx->Eval.Map1Index;
  280.       attr->Map1Normal = ctx->Eval.Map1Normal;
  281.       attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
  282.       attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
  283.       attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
  284.       attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
  285.       attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
  286.       attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
  287.       attr->Map2Color4 = ctx->Eval.Map2Color4;
  288.       attr->Map2Index = ctx->Eval.Map2Index;
  289.       attr->Map2Normal = ctx->Eval.Map2Normal;
  290.       attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
  291.       attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
  292.       attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
  293.       attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
  294.       attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
  295.       attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
  296.       attr->Normalize = ctx->Transform.Normalize;
  297.       attr->RasterPositionUnclipped = ctx->Transform.RasterPositionUnclipped;
  298.       attr->PointSmooth = ctx->Point.SmoothFlag;
  299.       attr->PointSprite = ctx->Point.PointSprite;
  300.       attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
  301.       attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
  302.       attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
  303.       attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
  304.       attr->PolygonStipple = ctx->Polygon.StippleFlag;
  305.       attr->RescaleNormals = ctx->Transform.RescaleNormals;
  306.       attr->Scissor = ctx->Scissor.Enabled;
  307.       attr->Stencil = ctx->Stencil.Enabled;
  308.       attr->StencilTwoSide = ctx->Stencil.TestTwoSide;
  309.       attr->MultisampleEnabled = ctx->Multisample.Enabled;
  310.       attr->SampleAlphaToCoverage = ctx->Multisample.SampleAlphaToCoverage;
  311.       attr->SampleAlphaToOne = ctx->Multisample.SampleAlphaToOne;
  312.       attr->SampleCoverage = ctx->Multisample.SampleCoverage;
  313.       for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
  314.          attr->Texture[i] = ctx->Texture.Unit[i].Enabled;
  315.          attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
  316.       }
  317.       /* GL_ARB_vertex_program */
  318.       attr->VertexProgram = ctx->VertexProgram.Enabled;
  319.       attr->VertexProgramPointSize = ctx->VertexProgram.PointSizeEnabled;
  320.       attr->VertexProgramTwoSide = ctx->VertexProgram.TwoSideEnabled;
  321.  
  322.       /* GL_ARB_fragment_program */
  323.       attr->FragmentProgram = ctx->FragmentProgram.Enabled;
  324.  
  325.       save_attrib_data(&head, GL_ENABLE_BIT, attr);
  326.  
  327.       /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
  328.       attr->sRGBEnabled = ctx->Color.sRGBEnabled;
  329.    }
  330.  
  331.    if (mask & GL_EVAL_BIT) {
  332.       struct gl_eval_attrib *attr;
  333.       attr = MALLOC_STRUCT( gl_eval_attrib );
  334.       memcpy( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
  335.       save_attrib_data(&head, GL_EVAL_BIT, attr);
  336.    }
  337.  
  338.    if (mask & GL_FOG_BIT) {
  339.       struct gl_fog_attrib *attr;
  340.       attr = MALLOC_STRUCT( gl_fog_attrib );
  341.       memcpy( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
  342.       save_attrib_data(&head, GL_FOG_BIT, attr);
  343.    }
  344.  
  345.    if (mask & GL_HINT_BIT) {
  346.       struct gl_hint_attrib *attr;
  347.       attr = MALLOC_STRUCT( gl_hint_attrib );
  348.       memcpy( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
  349.       save_attrib_data(&head, GL_HINT_BIT, attr);
  350.    }
  351.  
  352.    if (mask & GL_LIGHTING_BIT) {
  353.       struct gl_light_attrib *attr;
  354.       FLUSH_CURRENT(ctx, 0);    /* flush material changes */
  355.       attr = MALLOC_STRUCT( gl_light_attrib );
  356.       memcpy( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
  357.       save_attrib_data(&head, GL_LIGHTING_BIT, attr);
  358.    }
  359.  
  360.    if (mask & GL_LINE_BIT) {
  361.       struct gl_line_attrib *attr;
  362.       attr = MALLOC_STRUCT( gl_line_attrib );
  363.       memcpy( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
  364.       save_attrib_data(&head, GL_LINE_BIT, attr);
  365.    }
  366.  
  367.    if (mask & GL_LIST_BIT) {
  368.       struct gl_list_attrib *attr;
  369.       attr = MALLOC_STRUCT( gl_list_attrib );
  370.       memcpy( attr, &ctx->List, sizeof(struct gl_list_attrib) );
  371.       save_attrib_data(&head, GL_LIST_BIT, attr);
  372.    }
  373.  
  374.    if (mask & GL_PIXEL_MODE_BIT) {
  375.       struct gl_pixel_attrib *attr;
  376.       attr = MALLOC_STRUCT( gl_pixel_attrib );
  377.       memcpy( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
  378.       /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
  379.       attr->ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
  380.       save_attrib_data(&head, GL_PIXEL_MODE_BIT, attr);
  381.    }
  382.  
  383.    if (mask & GL_POINT_BIT) {
  384.       struct gl_point_attrib *attr;
  385.       attr = MALLOC_STRUCT( gl_point_attrib );
  386.       memcpy( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
  387.       save_attrib_data(&head, GL_POINT_BIT, attr);
  388.    }
  389.  
  390.    if (mask & GL_POLYGON_BIT) {
  391.       struct gl_polygon_attrib *attr;
  392.       attr = MALLOC_STRUCT( gl_polygon_attrib );
  393.       memcpy( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
  394.       save_attrib_data(&head, GL_POLYGON_BIT, attr);
  395.    }
  396.  
  397.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  398.       GLuint *stipple;
  399.       stipple = malloc( 32*sizeof(GLuint) );
  400.       memcpy( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
  401.       save_attrib_data(&head, GL_POLYGON_STIPPLE_BIT, stipple);
  402.    }
  403.  
  404.    if (mask & GL_SCISSOR_BIT) {
  405.       struct gl_scissor_attrib *attr;
  406.       attr = MALLOC_STRUCT( gl_scissor_attrib );
  407.       memcpy( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
  408.       save_attrib_data(&head, GL_SCISSOR_BIT, attr);
  409.    }
  410.  
  411.    if (mask & GL_STENCIL_BUFFER_BIT) {
  412.       struct gl_stencil_attrib *attr;
  413.       attr = MALLOC_STRUCT( gl_stencil_attrib );
  414.       memcpy( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
  415.       save_attrib_data(&head, GL_STENCIL_BUFFER_BIT, attr);
  416.    }
  417.  
  418.    if (mask & GL_TEXTURE_BIT) {
  419.       struct texture_state *texstate = CALLOC_STRUCT(texture_state);
  420.       GLuint u, tex;
  421.  
  422.       if (!texstate) {
  423.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib(GL_TEXTURE_BIT)");
  424.          goto end;
  425.       }
  426.  
  427.       _mesa_lock_context_textures(ctx);
  428.  
  429.       /* copy/save the bulk of texture state here */
  430.       memcpy(&texstate->Texture, &ctx->Texture, sizeof(ctx->Texture));
  431.  
  432.       /* Save references to the currently bound texture objects so they don't
  433.        * accidentally get deleted while referenced in the attribute stack.
  434.        */
  435.       for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
  436.          for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
  437.             _mesa_reference_texobj(&texstate->SavedTexRef[u][tex],
  438.                                    ctx->Texture.Unit[u].CurrentTex[tex]);
  439.          }
  440.       }
  441.  
  442.       /* copy state/contents of the currently bound texture objects */
  443.       for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
  444.          for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
  445.             _mesa_copy_texture_object(&texstate->SavedObj[u][tex],
  446.                                       ctx->Texture.Unit[u].CurrentTex[tex]);
  447.          }
  448.       }
  449.  
  450.       _mesa_reference_shared_state(ctx, &texstate->SharedRef, ctx->Shared);
  451.  
  452.       _mesa_unlock_context_textures(ctx);
  453.  
  454.       save_attrib_data(&head, GL_TEXTURE_BIT, texstate);
  455.    }
  456.  
  457.    if (mask & GL_TRANSFORM_BIT) {
  458.       struct gl_transform_attrib *attr;
  459.       attr = MALLOC_STRUCT( gl_transform_attrib );
  460.       memcpy( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
  461.       save_attrib_data(&head, GL_TRANSFORM_BIT, attr);
  462.    }
  463.  
  464.    if (mask & GL_VIEWPORT_BIT) {
  465.       struct gl_viewport_attrib *attr;
  466.       attr = MALLOC_STRUCT( gl_viewport_attrib );
  467.       memcpy( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
  468.       save_attrib_data(&head, GL_VIEWPORT_BIT, attr);
  469.    }
  470.  
  471.    /* GL_ARB_multisample */
  472.    if (mask & GL_MULTISAMPLE_BIT_ARB) {
  473.       struct gl_multisample_attrib *attr;
  474.       attr = MALLOC_STRUCT( gl_multisample_attrib );
  475.       memcpy( attr, &ctx->Multisample, sizeof(struct gl_multisample_attrib) );
  476.       save_attrib_data(&head, GL_MULTISAMPLE_BIT_ARB, attr);
  477.    }
  478.  
  479. end:
  480.    ctx->AttribStack[ctx->AttribStackDepth] = head;
  481.    ctx->AttribStackDepth++;
  482. }
  483.  
  484.  
  485.  
  486. static void
  487. pop_enable_group(struct gl_context *ctx, const struct gl_enable_attrib *enable)
  488. {
  489.    const GLuint curTexUnitSave = ctx->Texture.CurrentUnit;
  490.    GLuint i;
  491.  
  492. #define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM)          \
  493.         if ((VALUE) != (NEWVALUE)) {                    \
  494.            _mesa_set_enable( ctx, ENUM, (NEWVALUE) );   \
  495.         }
  496.  
  497.    TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
  498.    if (ctx->Color.BlendEnabled != enable->Blend) {
  499.       if (ctx->Extensions.EXT_draw_buffers2) {
  500.          GLuint i;
  501.          for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
  502.             _mesa_set_enablei(ctx, GL_BLEND, i, (enable->Blend >> i) & 1);
  503.          }
  504.       }
  505.       else {
  506.          _mesa_set_enable(ctx, GL_BLEND, (enable->Blend & 1));
  507.       }
  508.    }
  509.  
  510.    for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
  511.       const GLuint mask = 1 << i;
  512.       if ((ctx->Transform.ClipPlanesEnabled & mask) != (enable->ClipPlanes & mask))
  513.           _mesa_set_enable(ctx, (GLenum) (GL_CLIP_PLANE0 + i),
  514.                            !!(enable->ClipPlanes & mask));
  515.    }
  516.  
  517.    TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial,
  518.                    GL_COLOR_MATERIAL);
  519.    TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
  520.    TEST_AND_UPDATE(ctx->Transform.DepthClamp, enable->DepthClamp,
  521.                    GL_DEPTH_CLAMP);
  522.    TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
  523.    TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
  524.    TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
  525.    TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
  526.    TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
  527.    TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple,
  528.                    GL_LINE_STIPPLE);
  529.    TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp,
  530.                    GL_INDEX_LOGIC_OP);
  531.    TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp,
  532.                    GL_COLOR_LOGIC_OP);
  533.  
  534.    TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
  535.    TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
  536.    TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
  537.    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord1, enable->Map1TextureCoord1,
  538.                    GL_MAP1_TEXTURE_COORD_1);
  539.    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord2, enable->Map1TextureCoord2,
  540.                    GL_MAP1_TEXTURE_COORD_2);
  541.    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord3, enable->Map1TextureCoord3,
  542.                    GL_MAP1_TEXTURE_COORD_3);
  543.    TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord4, enable->Map1TextureCoord4,
  544.                    GL_MAP1_TEXTURE_COORD_4);
  545.    TEST_AND_UPDATE(ctx->Eval.Map1Vertex3, enable->Map1Vertex3,
  546.                    GL_MAP1_VERTEX_3);
  547.    TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4,
  548.                    GL_MAP1_VERTEX_4);
  549.  
  550.    TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
  551.    TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
  552.    TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
  553.    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1,
  554.                    GL_MAP2_TEXTURE_COORD_1);
  555.    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2,
  556.                    GL_MAP2_TEXTURE_COORD_2);
  557.    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3,
  558.                    GL_MAP2_TEXTURE_COORD_3);
  559.    TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4,
  560.                    GL_MAP2_TEXTURE_COORD_4);
  561.    TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3,
  562.                    GL_MAP2_VERTEX_3);
  563.    TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4,
  564.                    GL_MAP2_VERTEX_4);
  565.  
  566.    TEST_AND_UPDATE(ctx->Eval.AutoNormal, enable->AutoNormal, GL_AUTO_NORMAL);
  567.    TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
  568.    TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals,
  569.                    GL_RESCALE_NORMAL_EXT);
  570.    TEST_AND_UPDATE(ctx->Transform.RasterPositionUnclipped,
  571.                    enable->RasterPositionUnclipped,
  572.                    GL_RASTER_POSITION_UNCLIPPED_IBM);
  573.    TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth,
  574.                    GL_POINT_SMOOTH);
  575.    if (ctx->Extensions.NV_point_sprite || ctx->Extensions.ARB_point_sprite) {
  576.       TEST_AND_UPDATE(ctx->Point.PointSprite, enable->PointSprite,
  577.                       GL_POINT_SPRITE_NV);
  578.    }
  579.    TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint,
  580.                    GL_POLYGON_OFFSET_POINT);
  581.    TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine,
  582.                    GL_POLYGON_OFFSET_LINE);
  583.    TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill,
  584.                    GL_POLYGON_OFFSET_FILL);
  585.    TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth,
  586.                    GL_POLYGON_SMOOTH);
  587.    TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple,
  588.                    GL_POLYGON_STIPPLE);
  589.    TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
  590.    TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
  591.    if (ctx->Extensions.EXT_stencil_two_side) {
  592.       TEST_AND_UPDATE(ctx->Stencil.TestTwoSide, enable->StencilTwoSide, GL_STENCIL_TEST_TWO_SIDE_EXT);
  593.    }
  594.    TEST_AND_UPDATE(ctx->Multisample.Enabled, enable->MultisampleEnabled,
  595.                    GL_MULTISAMPLE_ARB);
  596.    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
  597.                    enable->SampleAlphaToCoverage,
  598.                    GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
  599.    TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
  600.                    enable->SampleAlphaToOne,
  601.                    GL_SAMPLE_ALPHA_TO_ONE_ARB);
  602.    TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
  603.                    enable->SampleCoverage,
  604.                    GL_SAMPLE_COVERAGE_ARB);
  605.    /* GL_ARB_vertex_program */
  606.    TEST_AND_UPDATE(ctx->VertexProgram.Enabled,
  607.                    enable->VertexProgram,
  608.                    GL_VERTEX_PROGRAM_ARB);
  609.    TEST_AND_UPDATE(ctx->VertexProgram.PointSizeEnabled,
  610.                    enable->VertexProgramPointSize,
  611.                    GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
  612.    TEST_AND_UPDATE(ctx->VertexProgram.TwoSideEnabled,
  613.                    enable->VertexProgramTwoSide,
  614.                    GL_VERTEX_PROGRAM_TWO_SIDE_ARB);
  615.  
  616.    /* GL_ARB_fragment_program */
  617.    TEST_AND_UPDATE(ctx->FragmentProgram.Enabled,
  618.                    enable->FragmentProgram,
  619.                    GL_FRAGMENT_PROGRAM_ARB);
  620.  
  621.    /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
  622.    TEST_AND_UPDATE(ctx->Color.sRGBEnabled, enable->sRGBEnabled,
  623.                    GL_FRAMEBUFFER_SRGB);
  624.  
  625.    /* texture unit enables */
  626.    for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
  627.       const GLbitfield enabled = enable->Texture[i];
  628.       const GLbitfield genEnabled = enable->TexGen[i];
  629.  
  630.       if (ctx->Texture.Unit[i].Enabled != enabled) {
  631.          _mesa_ActiveTexture(GL_TEXTURE0 + i);
  632.  
  633.          _mesa_set_enable(ctx, GL_TEXTURE_1D, !!(enabled & TEXTURE_1D_BIT));
  634.          _mesa_set_enable(ctx, GL_TEXTURE_2D, !!(enabled & TEXTURE_2D_BIT));
  635.          _mesa_set_enable(ctx, GL_TEXTURE_3D, !!(enabled & TEXTURE_3D_BIT));
  636.          if (ctx->Extensions.NV_texture_rectangle) {
  637.             _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_ARB,
  638.                              !!(enabled & TEXTURE_RECT_BIT));
  639.          }
  640.          if (ctx->Extensions.ARB_texture_cube_map) {
  641.             _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP,
  642.                              !!(enabled & TEXTURE_CUBE_BIT));
  643.          }
  644.          if (ctx->Extensions.MESA_texture_array) {
  645.             _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
  646.                              !!(enabled & TEXTURE_1D_ARRAY_BIT));
  647.             _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
  648.                              !!(enabled & TEXTURE_2D_ARRAY_BIT));
  649.          }
  650.       }
  651.  
  652.       if (ctx->Texture.Unit[i].TexGenEnabled != genEnabled) {
  653.          _mesa_ActiveTexture(GL_TEXTURE0 + i);
  654.          _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, !!(genEnabled & S_BIT));
  655.          _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, !!(genEnabled & T_BIT));
  656.          _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, !!(genEnabled & R_BIT));
  657.          _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, !!(genEnabled & Q_BIT));
  658.       }
  659.    }
  660.  
  661.    _mesa_ActiveTexture(GL_TEXTURE0 + curTexUnitSave);
  662. }
  663.  
  664.  
  665. /**
  666.  * Pop/restore texture attribute/group state.
  667.  */
  668. static void
  669. pop_texture_group(struct gl_context *ctx, struct texture_state *texstate)
  670. {
  671.    GLuint u;
  672.  
  673.    _mesa_lock_context_textures(ctx);
  674.  
  675.    for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
  676.       const struct gl_texture_unit *unit = &texstate->Texture.Unit[u];
  677.       GLuint tgt;
  678.  
  679.       _mesa_ActiveTexture(GL_TEXTURE0_ARB + u);
  680.       _mesa_set_enable(ctx, GL_TEXTURE_1D, !!(unit->Enabled & TEXTURE_1D_BIT));
  681.       _mesa_set_enable(ctx, GL_TEXTURE_2D, !!(unit->Enabled & TEXTURE_2D_BIT));
  682.       _mesa_set_enable(ctx, GL_TEXTURE_3D, !!(unit->Enabled & TEXTURE_3D_BIT));
  683.       if (ctx->Extensions.ARB_texture_cube_map) {
  684.          _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP_ARB,
  685.                           !!(unit->Enabled & TEXTURE_CUBE_BIT));
  686.       }
  687.       if (ctx->Extensions.NV_texture_rectangle) {
  688.          _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE_NV,
  689.                           !!(unit->Enabled & TEXTURE_RECT_BIT));
  690.       }
  691.       if (ctx->Extensions.MESA_texture_array) {
  692.          _mesa_set_enable(ctx, GL_TEXTURE_1D_ARRAY_EXT,
  693.                           !!(unit->Enabled & TEXTURE_1D_ARRAY_BIT));
  694.          _mesa_set_enable(ctx, GL_TEXTURE_2D_ARRAY_EXT,
  695.                           !!(unit->Enabled & TEXTURE_2D_ARRAY_BIT));
  696.       }
  697.       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->EnvMode);
  698.       _mesa_TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, unit->EnvColor);
  699.       _mesa_TexGeni(GL_S, GL_TEXTURE_GEN_MODE, unit->GenS.Mode);
  700.       _mesa_TexGeni(GL_T, GL_TEXTURE_GEN_MODE, unit->GenT.Mode);
  701.       _mesa_TexGeni(GL_R, GL_TEXTURE_GEN_MODE, unit->GenR.Mode);
  702.       _mesa_TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, unit->GenQ.Mode);
  703.       _mesa_TexGenfv(GL_S, GL_OBJECT_PLANE, unit->GenS.ObjectPlane);
  704.       _mesa_TexGenfv(GL_T, GL_OBJECT_PLANE, unit->GenT.ObjectPlane);
  705.       _mesa_TexGenfv(GL_R, GL_OBJECT_PLANE, unit->GenR.ObjectPlane);
  706.       _mesa_TexGenfv(GL_Q, GL_OBJECT_PLANE, unit->GenQ.ObjectPlane);
  707.       /* Eye plane done differently to avoid re-transformation */
  708.       {
  709.          struct gl_texture_unit *destUnit = &ctx->Texture.Unit[u];
  710.          COPY_4FV(destUnit->GenS.EyePlane, unit->GenS.EyePlane);
  711.          COPY_4FV(destUnit->GenT.EyePlane, unit->GenT.EyePlane);
  712.          COPY_4FV(destUnit->GenR.EyePlane, unit->GenR.EyePlane);
  713.          COPY_4FV(destUnit->GenQ.EyePlane, unit->GenQ.EyePlane);
  714.          if (ctx->Driver.TexGen) {
  715.             ctx->Driver.TexGen(ctx, GL_S, GL_EYE_PLANE, unit->GenS.EyePlane);
  716.             ctx->Driver.TexGen(ctx, GL_T, GL_EYE_PLANE, unit->GenT.EyePlane);
  717.             ctx->Driver.TexGen(ctx, GL_R, GL_EYE_PLANE, unit->GenR.EyePlane);
  718.             ctx->Driver.TexGen(ctx, GL_Q, GL_EYE_PLANE, unit->GenQ.EyePlane);
  719.          }
  720.       }
  721.       _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, !!(unit->TexGenEnabled & S_BIT));
  722.       _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, !!(unit->TexGenEnabled & T_BIT));
  723.       _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, !!(unit->TexGenEnabled & R_BIT));
  724.       _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, !!(unit->TexGenEnabled & Q_BIT));
  725.       _mesa_TexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS,
  726.                     unit->LodBias);
  727.       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
  728.                     unit->Combine.ModeRGB);
  729.       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
  730.                     unit->Combine.ModeA);
  731.       {
  732.          const GLuint n = ctx->Extensions.NV_texture_env_combine4 ? 4 : 3;
  733.          GLuint i;
  734.          for (i = 0; i < n; i++) {
  735.             _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB + i,
  736.                           unit->Combine.SourceRGB[i]);
  737.             _mesa_TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA + i,
  738.                           unit->Combine.SourceA[i]);
  739.             _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB + i,
  740.                           unit->Combine.OperandRGB[i]);
  741.             _mesa_TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA + i,
  742.                           unit->Combine.OperandA[i]);
  743.          }
  744.       }
  745.       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,
  746.                     1 << unit->Combine.ScaleShiftRGB);
  747.       _mesa_TexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE,
  748.                     1 << unit->Combine.ScaleShiftA);
  749.  
  750.       /* Restore texture object state for each target */
  751.       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
  752.          const struct gl_texture_object *obj = NULL;
  753.          const struct gl_sampler_object *samp;
  754.          GLenum target;
  755.  
  756.          obj = &texstate->SavedObj[u][tgt];
  757.  
  758.          /* don't restore state for unsupported targets to prevent
  759.           * raising GL errors.
  760.           */
  761.          if (obj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
  762.              !ctx->Extensions.ARB_texture_cube_map) {
  763.             continue;
  764.          }
  765.          else if (obj->Target == GL_TEXTURE_RECTANGLE_NV &&
  766.                   !ctx->Extensions.NV_texture_rectangle) {
  767.             continue;
  768.          }
  769.          else if ((obj->Target == GL_TEXTURE_1D_ARRAY_EXT ||
  770.                    obj->Target == GL_TEXTURE_2D_ARRAY_EXT) &&
  771.                   !ctx->Extensions.MESA_texture_array) {
  772.             continue;
  773.          }
  774.          else if (obj->Target == GL_TEXTURE_CUBE_MAP_ARRAY &&
  775.              !ctx->Extensions.ARB_texture_cube_map_array) {
  776.             continue;
  777.          } else if (obj->Target == GL_TEXTURE_BUFFER)
  778.             continue;
  779.          else if (obj->Target == GL_TEXTURE_EXTERNAL_OES)
  780.             continue;
  781.          else if (obj->Target == GL_TEXTURE_2D_MULTISAMPLE ||
  782.                   obj->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
  783.             continue;
  784.  
  785.          target = obj->Target;
  786.  
  787.          _mesa_BindTexture(target, obj->Name);
  788.  
  789.          samp = &obj->Sampler;
  790.  
  791.          _mesa_TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, samp->BorderColor.f);
  792.          _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, samp->WrapS);
  793.          _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, samp->WrapT);
  794.          _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, samp->WrapR);
  795.          _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, samp->MinFilter);
  796.          _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, samp->MagFilter);
  797.          _mesa_TexParameterf(target, GL_TEXTURE_MIN_LOD, samp->MinLod);
  798.          _mesa_TexParameterf(target, GL_TEXTURE_MAX_LOD, samp->MaxLod);
  799.          _mesa_TexParameterf(target, GL_TEXTURE_LOD_BIAS, samp->LodBias);
  800.          _mesa_TexParameterf(target, GL_TEXTURE_PRIORITY, obj->Priority);
  801.          _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, obj->BaseLevel);
  802.          if (target != GL_TEXTURE_RECTANGLE_ARB)
  803.             _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, obj->MaxLevel);
  804.          if (ctx->Extensions.EXT_texture_filter_anisotropic) {
  805.             _mesa_TexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  806.                                 samp->MaxAnisotropy);
  807.          }
  808.          if (ctx->Extensions.ARB_shadow) {
  809.             _mesa_TexParameteri(target, GL_TEXTURE_COMPARE_MODE,
  810.                                 samp->CompareMode);
  811.             _mesa_TexParameteri(target, GL_TEXTURE_COMPARE_FUNC,
  812.                                 samp->CompareFunc);
  813.          }
  814.          if (ctx->Extensions.ARB_depth_texture)
  815.             _mesa_TexParameteri(target, GL_DEPTH_TEXTURE_MODE, obj->DepthMode);
  816.       }
  817.  
  818.       /* remove saved references to the texture objects */
  819.       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
  820.          _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
  821.       }
  822.    }
  823.  
  824.    _mesa_ActiveTexture(GL_TEXTURE0_ARB + texstate->Texture.CurrentUnit);
  825.  
  826.    _mesa_reference_shared_state(ctx, &texstate->SharedRef, NULL);
  827.  
  828.    _mesa_unlock_context_textures(ctx);
  829. }
  830.  
  831.  
  832. /*
  833.  * This function is kind of long just because we have to call a lot
  834.  * of device driver functions to update device driver state.
  835.  *
  836.  * XXX As it is now, most of the pop-code calls immediate-mode Mesa functions
  837.  * in order to restore GL state.  This isn't terribly efficient but it
  838.  * ensures that dirty flags and any derived state gets updated correctly.
  839.  * We could at least check if the value to restore equals the current value
  840.  * and then skip the Mesa call.
  841.  */
  842. void GLAPIENTRY
  843. _mesa_PopAttrib(void)
  844. {
  845.    struct gl_attrib_node *attr, *next;
  846.    GET_CURRENT_CONTEXT(ctx);
  847.    FLUSH_VERTICES(ctx, 0);
  848.  
  849.    if (ctx->AttribStackDepth == 0) {
  850.       _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
  851.       return;
  852.    }
  853.  
  854.    ctx->AttribStackDepth--;
  855.    attr = ctx->AttribStack[ctx->AttribStackDepth];
  856.  
  857.    while (attr) {
  858.  
  859.       if (MESA_VERBOSE & VERBOSE_API) {
  860.          _mesa_debug(ctx, "glPopAttrib %s\n",
  861.                      _mesa_lookup_enum_by_nr(attr->kind));
  862.       }
  863.  
  864.       switch (attr->kind) {
  865.          case GL_ACCUM_BUFFER_BIT:
  866.             {
  867.                const struct gl_accum_attrib *accum;
  868.                accum = (const struct gl_accum_attrib *) attr->data;
  869.                _mesa_ClearAccum(accum->ClearColor[0],
  870.                                 accum->ClearColor[1],
  871.                                 accum->ClearColor[2],
  872.                                 accum->ClearColor[3]);
  873.             }
  874.             break;
  875.          case GL_COLOR_BUFFER_BIT:
  876.             {
  877.                const struct gl_colorbuffer_attrib *color;
  878.  
  879.                color = (const struct gl_colorbuffer_attrib *) attr->data;
  880.                _mesa_ClearIndex((GLfloat) color->ClearIndex);
  881.                _mesa_ClearColor(color->ClearColor.f[0],
  882.                                 color->ClearColor.f[1],
  883.                                 color->ClearColor.f[2],
  884.                                 color->ClearColor.f[3]);
  885.                _mesa_IndexMask(color->IndexMask);
  886.                if (!ctx->Extensions.EXT_draw_buffers2) {
  887.                   _mesa_ColorMask((GLboolean) (color->ColorMask[0][0] != 0),
  888.                                   (GLboolean) (color->ColorMask[0][1] != 0),
  889.                                   (GLboolean) (color->ColorMask[0][2] != 0),
  890.                                   (GLboolean) (color->ColorMask[0][3] != 0));
  891.                }
  892.                else {
  893.                   GLuint i;
  894.                   for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
  895.                      _mesa_ColorMaski(i,
  896.                                   (GLboolean) (color->ColorMask[i][0] != 0),
  897.                                   (GLboolean) (color->ColorMask[i][1] != 0),
  898.                                   (GLboolean) (color->ColorMask[i][2] != 0),
  899.                                   (GLboolean) (color->ColorMask[i][3] != 0));
  900.                   }
  901.                }
  902.                {
  903.                   /* Need to determine if more than one color output is
  904.                    * specified.  If so, call glDrawBuffersARB, else call
  905.                    * glDrawBuffer().  This is a subtle, but essential point
  906.                    * since GL_FRONT (for example) is illegal for the former
  907.                    * function, but legal for the later.
  908.                    */
  909.                   GLboolean multipleBuffers = GL_FALSE;
  910.                   GLuint i;
  911.  
  912.                   for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
  913.                      if (color->DrawBuffer[i] != GL_NONE) {
  914.                         multipleBuffers = GL_TRUE;
  915.                         break;
  916.                      }
  917.                   }
  918.                   /* Call the API_level functions, not _mesa_drawbuffers()
  919.                    * since we need to do error checking on the pop'd
  920.                    * GL_DRAW_BUFFER.
  921.                    * Ex: if GL_FRONT were pushed, but we're popping with a
  922.                    * user FBO bound, GL_FRONT will be illegal and we'll need
  923.                    * to record that error.  Per OpenGL ARB decision.
  924.                    */
  925.                   if (multipleBuffers)
  926.                      _mesa_DrawBuffers(ctx->Const.MaxDrawBuffers,
  927.                                           color->DrawBuffer);
  928.                   else
  929.                      _mesa_DrawBuffer(color->DrawBuffer[0]);
  930.                }
  931.                _mesa_set_enable(ctx, GL_ALPHA_TEST, color->AlphaEnabled);
  932.                _mesa_AlphaFunc(color->AlphaFunc, color->AlphaRefUnclamped);
  933.                if (ctx->Color.BlendEnabled != color->BlendEnabled) {
  934.                   if (ctx->Extensions.EXT_draw_buffers2) {
  935.                      GLuint i;
  936.                      for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
  937.                         _mesa_set_enablei(ctx, GL_BLEND, i,
  938.                                           (color->BlendEnabled >> i) & 1);
  939.                      }
  940.                   }
  941.                   else {
  942.                      _mesa_set_enable(ctx, GL_BLEND, (color->BlendEnabled & 1));
  943.                   }
  944.                }
  945.                if (ctx->Color._BlendFuncPerBuffer ||
  946.                    ctx->Color._BlendEquationPerBuffer) {
  947.                   /* set blend per buffer */
  948.                   GLuint buf;
  949.                   for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
  950.                      _mesa_BlendFuncSeparateiARB(buf, color->Blend[buf].SrcRGB,
  951.                                               color->Blend[buf].DstRGB,
  952.                                               color->Blend[buf].SrcA,
  953.                                               color->Blend[buf].DstA);
  954.                      _mesa_BlendEquationSeparateiARB(buf,
  955.                                                   color->Blend[buf].EquationRGB,
  956.                                                   color->Blend[buf].EquationA);
  957.                   }
  958.                }
  959.                else {
  960.                   /* set same blend modes for all buffers */
  961.                   _mesa_BlendFuncSeparate(color->Blend[0].SrcRGB,
  962.                                              color->Blend[0].DstRGB,
  963.                                              color->Blend[0].SrcA,
  964.                                              color->Blend[0].DstA);
  965.                   /* This special case is because glBlendEquationSeparateEXT
  966.                    * cannot take GL_LOGIC_OP as a parameter.
  967.                    */
  968.                   if (color->Blend[0].EquationRGB ==
  969.                       color->Blend[0].EquationA) {
  970.                      _mesa_BlendEquation(color->Blend[0].EquationRGB);
  971.                   }
  972.                   else {
  973.                      _mesa_BlendEquationSeparate(
  974.                                                  color->Blend[0].EquationRGB,
  975.                                                  color->Blend[0].EquationA);
  976.                   }
  977.                }
  978.                _mesa_BlendColor(color->BlendColorUnclamped[0],
  979.                                 color->BlendColorUnclamped[1],
  980.                                 color->BlendColorUnclamped[2],
  981.                                 color->BlendColorUnclamped[3]);
  982.                _mesa_LogicOp(color->LogicOp);
  983.                _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP,
  984.                                 color->ColorLogicOpEnabled);
  985.                _mesa_set_enable(ctx, GL_INDEX_LOGIC_OP,
  986.                                 color->IndexLogicOpEnabled);
  987.                _mesa_set_enable(ctx, GL_DITHER, color->DitherFlag);
  988.                if (ctx->Extensions.ARB_color_buffer_float)
  989.                   _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR_ARB,
  990.                                    color->ClampFragmentColor);
  991.                _mesa_ClampColor(GL_CLAMP_READ_COLOR_ARB, color->ClampReadColor);
  992.  
  993.                /* GL_ARB_framebuffer_sRGB / GL_EXT_framebuffer_sRGB */
  994.                if (ctx->Extensions.EXT_framebuffer_sRGB)
  995.                   _mesa_set_enable(ctx, GL_FRAMEBUFFER_SRGB, color->sRGBEnabled);
  996.             }
  997.             break;
  998.          case GL_CURRENT_BIT:
  999.             FLUSH_CURRENT( ctx, 0 );
  1000.             memcpy( &ctx->Current, attr->data,
  1001.                     sizeof(struct gl_current_attrib) );
  1002.             break;
  1003.          case GL_DEPTH_BUFFER_BIT:
  1004.             {
  1005.                const struct gl_depthbuffer_attrib *depth;
  1006.                depth = (const struct gl_depthbuffer_attrib *) attr->data;
  1007.                _mesa_DepthFunc(depth->Func);
  1008.                _mesa_ClearDepth(depth->Clear);
  1009.                _mesa_set_enable(ctx, GL_DEPTH_TEST, depth->Test);
  1010.                _mesa_DepthMask(depth->Mask);
  1011.             }
  1012.             break;
  1013.          case GL_ENABLE_BIT:
  1014.             {
  1015.                const struct gl_enable_attrib *enable;
  1016.                enable = (const struct gl_enable_attrib *) attr->data;
  1017.                pop_enable_group(ctx, enable);
  1018.                ctx->NewState |= _NEW_ALL;
  1019.             }
  1020.             break;
  1021.          case GL_EVAL_BIT:
  1022.             memcpy( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
  1023.             ctx->NewState |= _NEW_EVAL;
  1024.             break;
  1025.          case GL_FOG_BIT:
  1026.             {
  1027.                const struct gl_fog_attrib *fog;
  1028.                fog = (const struct gl_fog_attrib *) attr->data;
  1029.                _mesa_set_enable(ctx, GL_FOG, fog->Enabled);
  1030.                _mesa_Fogfv(GL_FOG_COLOR, fog->Color);
  1031.                _mesa_Fogf(GL_FOG_DENSITY, fog->Density);
  1032.                _mesa_Fogf(GL_FOG_START, fog->Start);
  1033.                _mesa_Fogf(GL_FOG_END, fog->End);
  1034.                _mesa_Fogf(GL_FOG_INDEX, fog->Index);
  1035.                _mesa_Fogi(GL_FOG_MODE, fog->Mode);
  1036.             }
  1037.             break;
  1038.          case GL_HINT_BIT:
  1039.             {
  1040.                const struct gl_hint_attrib *hint;
  1041.                hint = (const struct gl_hint_attrib *) attr->data;
  1042.                _mesa_Hint(GL_PERSPECTIVE_CORRECTION_HINT,
  1043.                           hint->PerspectiveCorrection );
  1044.                _mesa_Hint(GL_POINT_SMOOTH_HINT, hint->PointSmooth);
  1045.                _mesa_Hint(GL_LINE_SMOOTH_HINT, hint->LineSmooth);
  1046.                _mesa_Hint(GL_POLYGON_SMOOTH_HINT, hint->PolygonSmooth);
  1047.                _mesa_Hint(GL_FOG_HINT, hint->Fog);
  1048.                _mesa_Hint(GL_TEXTURE_COMPRESSION_HINT_ARB,
  1049.                           hint->TextureCompression);
  1050.             }
  1051.             break;
  1052.          case GL_LIGHTING_BIT:
  1053.             {
  1054.                GLuint i;
  1055.                const struct gl_light_attrib *light;
  1056.                light = (const struct gl_light_attrib *) attr->data;
  1057.                /* lighting enable */
  1058.                _mesa_set_enable(ctx, GL_LIGHTING, light->Enabled);
  1059.                /* per-light state */
  1060.                if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top))
  1061.                   _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
  1062.                
  1063.                for (i = 0; i < ctx->Const.MaxLights; i++) {
  1064.                   const struct gl_light *l = &light->Light[i];
  1065.                   _mesa_set_enable(ctx, GL_LIGHT0 + i, l->Enabled);
  1066.                   _mesa_light(ctx, i, GL_AMBIENT, l->Ambient);
  1067.                   _mesa_light(ctx, i, GL_DIFFUSE, l->Diffuse);
  1068.                   _mesa_light(ctx, i, GL_SPECULAR, l->Specular );
  1069.                   _mesa_light(ctx, i, GL_POSITION, l->EyePosition);
  1070.                   _mesa_light(ctx, i, GL_SPOT_DIRECTION, l->SpotDirection);
  1071.                   {
  1072.                      GLfloat p[4] = { 0 };
  1073.                      p[0] = l->SpotExponent;
  1074.                      _mesa_light(ctx, i, GL_SPOT_EXPONENT, p);
  1075.                   }
  1076.                   {
  1077.                      GLfloat p[4] = { 0 };
  1078.                      p[0] = l->SpotCutoff;
  1079.                      _mesa_light(ctx, i, GL_SPOT_CUTOFF, p);
  1080.                   }
  1081.                   {
  1082.                      GLfloat p[4] = { 0 };
  1083.                      p[0] = l->ConstantAttenuation;
  1084.                      _mesa_light(ctx, i, GL_CONSTANT_ATTENUATION, p);
  1085.                   }
  1086.                   {
  1087.                      GLfloat p[4] = { 0 };
  1088.                      p[0] = l->LinearAttenuation;
  1089.                      _mesa_light(ctx, i, GL_LINEAR_ATTENUATION, p);
  1090.                   }
  1091.                   {
  1092.                      GLfloat p[4] = { 0 };
  1093.                      p[0] = l->QuadraticAttenuation;
  1094.                      _mesa_light(ctx, i, GL_QUADRATIC_ATTENUATION, p);
  1095.                   }
  1096.                }
  1097.                /* light model */
  1098.                _mesa_LightModelfv(GL_LIGHT_MODEL_AMBIENT,
  1099.                                   light->Model.Ambient);
  1100.                _mesa_LightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,
  1101.                                  (GLfloat) light->Model.LocalViewer);
  1102.                _mesa_LightModelf(GL_LIGHT_MODEL_TWO_SIDE,
  1103.                                  (GLfloat) light->Model.TwoSide);
  1104.                _mesa_LightModelf(GL_LIGHT_MODEL_COLOR_CONTROL,
  1105.                                  (GLfloat) light->Model.ColorControl);
  1106.                /* shade model */
  1107.                _mesa_ShadeModel(light->ShadeModel);
  1108.                /* color material */
  1109.                _mesa_ColorMaterial(light->ColorMaterialFace,
  1110.                                    light->ColorMaterialMode);
  1111.                _mesa_set_enable(ctx, GL_COLOR_MATERIAL,
  1112.                                 light->ColorMaterialEnabled);
  1113.                /* materials */
  1114.                memcpy(&ctx->Light.Material, &light->Material,
  1115.                       sizeof(struct gl_material));
  1116.                if (ctx->Extensions.ARB_color_buffer_float) {
  1117.                   _mesa_ClampColor(GL_CLAMP_VERTEX_COLOR_ARB,
  1118.                                    light->ClampVertexColor);
  1119.                }
  1120.             }
  1121.             break;
  1122.          case GL_LINE_BIT:
  1123.             {
  1124.                const struct gl_line_attrib *line;
  1125.                line = (const struct gl_line_attrib *) attr->data;
  1126.                _mesa_set_enable(ctx, GL_LINE_SMOOTH, line->SmoothFlag);
  1127.                _mesa_set_enable(ctx, GL_LINE_STIPPLE, line->StippleFlag);
  1128.                _mesa_LineStipple(line->StippleFactor, line->StipplePattern);
  1129.                _mesa_LineWidth(line->Width);
  1130.             }
  1131.             break;
  1132.          case GL_LIST_BIT:
  1133.             memcpy( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
  1134.             break;
  1135.          case GL_PIXEL_MODE_BIT:
  1136.             memcpy( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
  1137.             /* XXX what other pixel state needs to be set by function calls? */
  1138.             _mesa_ReadBuffer(ctx->Pixel.ReadBuffer);
  1139.             ctx->NewState |= _NEW_PIXEL;
  1140.             break;
  1141.          case GL_POINT_BIT:
  1142.             {
  1143.                const struct gl_point_attrib *point;
  1144.                point = (const struct gl_point_attrib *) attr->data;
  1145.                _mesa_PointSize(point->Size);
  1146.                _mesa_set_enable(ctx, GL_POINT_SMOOTH, point->SmoothFlag);
  1147.                if (ctx->Extensions.EXT_point_parameters) {
  1148.                   _mesa_PointParameterfv(GL_DISTANCE_ATTENUATION_EXT,
  1149.                                          point->Params);
  1150.                   _mesa_PointParameterf(GL_POINT_SIZE_MIN_EXT,
  1151.                                         point->MinSize);
  1152.                   _mesa_PointParameterf(GL_POINT_SIZE_MAX_EXT,
  1153.                                         point->MaxSize);
  1154.                   _mesa_PointParameterf(GL_POINT_FADE_THRESHOLD_SIZE_EXT,
  1155.                                         point->Threshold);
  1156.                }
  1157.                if (ctx->Extensions.NV_point_sprite
  1158.                    || ctx->Extensions.ARB_point_sprite) {
  1159.                   GLuint u;
  1160.                   for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
  1161.                      _mesa_TexEnvi(GL_POINT_SPRITE_NV, GL_COORD_REPLACE_NV,
  1162.                                    (GLint) point->CoordReplace[u]);
  1163.                   }
  1164.                   _mesa_set_enable(ctx, GL_POINT_SPRITE_NV,point->PointSprite);
  1165.                   if (ctx->Extensions.NV_point_sprite)
  1166.                      _mesa_PointParameteri(GL_POINT_SPRITE_R_MODE_NV,
  1167.                                            ctx->Point.SpriteRMode);
  1168.  
  1169.                   if ((ctx->API == API_OPENGL_COMPAT && ctx->Version >= 20)
  1170.                       || ctx->API == API_OPENGL_CORE)
  1171.                      _mesa_PointParameterf(GL_POINT_SPRITE_COORD_ORIGIN,
  1172.                                            (GLfloat)ctx->Point.SpriteOrigin);
  1173.                }
  1174.             }
  1175.             break;
  1176.          case GL_POLYGON_BIT:
  1177.             {
  1178.                const struct gl_polygon_attrib *polygon;
  1179.                polygon = (const struct gl_polygon_attrib *) attr->data;
  1180.                _mesa_CullFace(polygon->CullFaceMode);
  1181.                _mesa_FrontFace(polygon->FrontFace);
  1182.                _mesa_PolygonMode(GL_FRONT, polygon->FrontMode);
  1183.                _mesa_PolygonMode(GL_BACK, polygon->BackMode);
  1184.                _mesa_PolygonOffset(polygon->OffsetFactor,
  1185.                                    polygon->OffsetUnits);
  1186.                _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, polygon->SmoothFlag);
  1187.                _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, polygon->StippleFlag);
  1188.                _mesa_set_enable(ctx, GL_CULL_FACE, polygon->CullFlag);
  1189.                _mesa_set_enable(ctx, GL_POLYGON_OFFSET_POINT,
  1190.                                 polygon->OffsetPoint);
  1191.                _mesa_set_enable(ctx, GL_POLYGON_OFFSET_LINE,
  1192.                                 polygon->OffsetLine);
  1193.                _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL,
  1194.                                 polygon->OffsetFill);
  1195.             }
  1196.             break;
  1197.          case GL_POLYGON_STIPPLE_BIT:
  1198.             memcpy( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
  1199.             ctx->NewState |= _NEW_POLYGONSTIPPLE;
  1200.             if (ctx->Driver.PolygonStipple)
  1201.                ctx->Driver.PolygonStipple( ctx, (const GLubyte *) attr->data );
  1202.             break;
  1203.          case GL_SCISSOR_BIT:
  1204.             {
  1205.                const struct gl_scissor_attrib *scissor;
  1206.                scissor = (const struct gl_scissor_attrib *) attr->data;
  1207.                _mesa_Scissor(scissor->X, scissor->Y,
  1208.                              scissor->Width, scissor->Height);
  1209.                _mesa_set_enable(ctx, GL_SCISSOR_TEST, scissor->Enabled);
  1210.             }
  1211.             break;
  1212.          case GL_STENCIL_BUFFER_BIT:
  1213.             {
  1214.                const struct gl_stencil_attrib *stencil;
  1215.                stencil = (const struct gl_stencil_attrib *) attr->data;
  1216.                _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
  1217.                _mesa_ClearStencil(stencil->Clear);
  1218.                if (ctx->Extensions.EXT_stencil_two_side) {
  1219.                   _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
  1220.                                    stencil->TestTwoSide);
  1221.                   _mesa_ActiveStencilFaceEXT(stencil->ActiveFace
  1222.                                              ? GL_BACK : GL_FRONT);
  1223.                }
  1224.                /* front state */
  1225.                _mesa_StencilFuncSeparate(GL_FRONT,
  1226.                                          stencil->Function[0],
  1227.                                          stencil->Ref[0],
  1228.                                          stencil->ValueMask[0]);
  1229.                _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
  1230.                _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
  1231.                                        stencil->ZFailFunc[0],
  1232.                                        stencil->ZPassFunc[0]);
  1233.                /* back state */
  1234.                _mesa_StencilFuncSeparate(GL_BACK,
  1235.                                          stencil->Function[1],
  1236.                                          stencil->Ref[1],
  1237.                                          stencil->ValueMask[1]);
  1238.                _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
  1239.                _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
  1240.                                        stencil->ZFailFunc[1],
  1241.                                        stencil->ZPassFunc[1]);
  1242.             }
  1243.             break;
  1244.          case GL_TRANSFORM_BIT:
  1245.             {
  1246.                GLuint i;
  1247.                const struct gl_transform_attrib *xform;
  1248.                xform = (const struct gl_transform_attrib *) attr->data;
  1249.                _mesa_MatrixMode(xform->MatrixMode);
  1250.                if (_math_matrix_is_dirty(ctx->ProjectionMatrixStack.Top))
  1251.                   _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
  1252.  
  1253.                /* restore clip planes */
  1254.                for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
  1255.                   const GLuint mask = 1 << i;
  1256.                   const GLfloat *eyePlane = xform->EyeUserPlane[i];
  1257.                   COPY_4V(ctx->Transform.EyeUserPlane[i], eyePlane);
  1258.                   _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i,
  1259.                                    !!(xform->ClipPlanesEnabled & mask));
  1260.                   if (ctx->Driver.ClipPlane)
  1261.                      ctx->Driver.ClipPlane( ctx, GL_CLIP_PLANE0 + i, eyePlane );
  1262.                }
  1263.  
  1264.                /* normalize/rescale */
  1265.                if (xform->Normalize != ctx->Transform.Normalize)
  1266.                   _mesa_set_enable(ctx, GL_NORMALIZE,ctx->Transform.Normalize);
  1267.                if (xform->RescaleNormals != ctx->Transform.RescaleNormals)
  1268.                   _mesa_set_enable(ctx, GL_RESCALE_NORMAL_EXT,
  1269.                                    ctx->Transform.RescaleNormals);
  1270.                if (xform->DepthClamp != ctx->Transform.DepthClamp)
  1271.                   _mesa_set_enable(ctx, GL_DEPTH_CLAMP,
  1272.                                    ctx->Transform.DepthClamp);
  1273.             }
  1274.             break;
  1275.          case GL_TEXTURE_BIT:
  1276.             {
  1277.                struct texture_state *texstate
  1278.                   = (struct texture_state *) attr->data;
  1279.                pop_texture_group(ctx, texstate);
  1280.                ctx->NewState |= _NEW_TEXTURE;
  1281.             }
  1282.             break;
  1283.          case GL_VIEWPORT_BIT:
  1284.             {
  1285.                const struct gl_viewport_attrib *vp;
  1286.                vp = (const struct gl_viewport_attrib *) attr->data;
  1287.                _mesa_Viewport(vp->X, vp->Y, vp->Width, vp->Height);
  1288.                _mesa_DepthRange(vp->Near, vp->Far);
  1289.             }
  1290.             break;
  1291.          case GL_MULTISAMPLE_BIT_ARB:
  1292.             {
  1293.                const struct gl_multisample_attrib *ms;
  1294.                ms = (const struct gl_multisample_attrib *) attr->data;
  1295.  
  1296.                TEST_AND_UPDATE(ctx->Multisample.Enabled,
  1297.                                ms->Enabled,
  1298.                                GL_MULTISAMPLE);
  1299.  
  1300.                TEST_AND_UPDATE(ctx->Multisample.SampleCoverage,
  1301.                                ms->SampleCoverage,
  1302.                                GL_SAMPLE_COVERAGE);
  1303.  
  1304.                TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToCoverage,
  1305.                                ms->SampleAlphaToCoverage,
  1306.                                GL_SAMPLE_ALPHA_TO_COVERAGE);
  1307.  
  1308.                TEST_AND_UPDATE(ctx->Multisample.SampleAlphaToOne,
  1309.                                ms->SampleAlphaToOne,
  1310.                                GL_SAMPLE_ALPHA_TO_ONE);
  1311.  
  1312.                _mesa_SampleCoverage(ms->SampleCoverageValue,
  1313.                                        ms->SampleCoverageInvert);
  1314.             }
  1315.             break;
  1316.  
  1317.          default:
  1318.             _mesa_problem( ctx, "Bad attrib flag in PopAttrib");
  1319.             break;
  1320.       }
  1321.  
  1322.       next = attr->next;
  1323.       free(attr->data);
  1324.       free(attr);
  1325.       attr = next;
  1326.    }
  1327. }
  1328.  
  1329.  
  1330. /**
  1331.  * Copy gl_pixelstore_attrib from src to dst, updating buffer
  1332.  * object refcounts.
  1333.  */
  1334. static void
  1335. copy_pixelstore(struct gl_context *ctx,
  1336.                 struct gl_pixelstore_attrib *dst,
  1337.                 const struct gl_pixelstore_attrib *src)
  1338. {
  1339.    dst->Alignment = src->Alignment;
  1340.    dst->RowLength = src->RowLength;
  1341.    dst->SkipPixels = src->SkipPixels;
  1342.    dst->SkipRows = src->SkipRows;
  1343.    dst->ImageHeight = src->ImageHeight;
  1344.    dst->SkipImages = src->SkipImages;
  1345.    dst->SwapBytes = src->SwapBytes;
  1346.    dst->LsbFirst = src->LsbFirst;
  1347.    dst->Invert = src->Invert;
  1348.    _mesa_reference_buffer_object(ctx, &dst->BufferObj, src->BufferObj);
  1349. }
  1350.  
  1351.  
  1352. #define GL_CLIENT_PACK_BIT (1<<20)
  1353. #define GL_CLIENT_UNPACK_BIT (1<<21)
  1354.  
  1355. /**
  1356.  * Copy gl_array_object from src to dest.
  1357.  * 'dest' must be in an initialized state.
  1358.  */
  1359. static void
  1360. copy_array_object(struct gl_context *ctx,
  1361.                   struct gl_array_object *dest,
  1362.                   struct gl_array_object *src)
  1363. {
  1364.    GLuint i;
  1365.  
  1366.    /* skip Name */
  1367.    /* skip RefCount */
  1368.  
  1369.    /* In theory must be the same anyway, but on recreate make sure it matches */
  1370.    dest->ARBsemantics = src->ARBsemantics;
  1371.  
  1372.    for (i = 0; i < Elements(src->VertexAttrib); i++)
  1373.       _mesa_copy_client_array(ctx, &dest->VertexAttrib[i], &src->VertexAttrib[i]);
  1374.  
  1375.    /* _Enabled must be the same than on push */
  1376.    dest->_Enabled = src->_Enabled;
  1377.    dest->_MaxElement = src->_MaxElement;
  1378. }
  1379.  
  1380. /**
  1381.  * Copy gl_array_attrib from src to dest.
  1382.  * 'dest' must be in an initialized state.
  1383.  */
  1384. static void
  1385. copy_array_attrib(struct gl_context *ctx,
  1386.                   struct gl_array_attrib *dest,
  1387.                   struct gl_array_attrib *src,
  1388.                   bool vbo_deleted)
  1389. {
  1390.    /* skip ArrayObj */
  1391.    /* skip DefaultArrayObj, Objects */
  1392.    dest->ActiveTexture = src->ActiveTexture;
  1393.    dest->LockFirst = src->LockFirst;
  1394.    dest->LockCount = src->LockCount;
  1395.    dest->PrimitiveRestart = src->PrimitiveRestart;
  1396.    dest->PrimitiveRestartFixedIndex = src->PrimitiveRestartFixedIndex;
  1397.    dest->_PrimitiveRestart = src->_PrimitiveRestart;
  1398.    dest->RestartIndex = src->RestartIndex;
  1399.    /* skip NewState */
  1400.    /* skip RebindArrays */
  1401.  
  1402.    if (!vbo_deleted)
  1403.       copy_array_object(ctx, dest->ArrayObj, src->ArrayObj);
  1404.  
  1405.    /* skip ArrayBufferObj */
  1406.    /* skip ElementArrayBufferObj */
  1407. }
  1408.  
  1409. /**
  1410.  * Save the content of src to dest.
  1411.  */
  1412. static void
  1413. save_array_attrib(struct gl_context *ctx,
  1414.                   struct gl_array_attrib *dest,
  1415.                   struct gl_array_attrib *src)
  1416. {
  1417.    /* Set the Name, needed for restore, but do never overwrite.
  1418.     * Needs to match value in the object hash. */
  1419.    dest->ArrayObj->Name = src->ArrayObj->Name;
  1420.    /* And copy all of the rest. */
  1421.    copy_array_attrib(ctx, dest, src, false);
  1422.  
  1423.    /* Just reference them here */
  1424.    _mesa_reference_buffer_object(ctx, &dest->ArrayBufferObj,
  1425.                                  src->ArrayBufferObj);
  1426.    _mesa_reference_buffer_object(ctx, &dest->ArrayObj->ElementArrayBufferObj,
  1427.                                  src->ArrayObj->ElementArrayBufferObj);
  1428. }
  1429.  
  1430. /**
  1431.  * Restore the content of src to dest.
  1432.  */
  1433. static void
  1434. restore_array_attrib(struct gl_context *ctx,
  1435.                      struct gl_array_attrib *dest,
  1436.                      struct gl_array_attrib *src)
  1437. {
  1438.    /* The ARB_vertex_array_object spec says:
  1439.     *
  1440.     *     "BindVertexArray fails and an INVALID_OPERATION error is generated
  1441.     *     if array is not a name returned from a previous call to
  1442.     *     GenVertexArrays, or if such a name has since been deleted with
  1443.     *     DeleteVertexArrays."
  1444.     *
  1445.     * Therefore popping a deleted VAO cannot magically recreate it.
  1446.     *
  1447.     * The semantics of objects created using APPLE_vertex_array_objects behave
  1448.     * differently.  These objects expect to be recreated by pop.  Alas.
  1449.     */
  1450.    const bool arb_vao = (src->ArrayObj->Name != 0
  1451.                          && src->ArrayObj->ARBsemantics);
  1452.  
  1453.    if (arb_vao && !_mesa_IsVertexArray(src->ArrayObj->Name))
  1454.       return;
  1455.  
  1456.    _mesa_BindVertexArrayAPPLE(src->ArrayObj->Name);
  1457.  
  1458.    /* Restore or recreate the buffer objects by the names ... */
  1459.    if (!arb_vao
  1460.        || src->ArrayBufferObj->Name == 0
  1461.        || _mesa_IsBuffer(src->ArrayBufferObj->Name)) {
  1462.       /* ... and restore its content */
  1463.       copy_array_attrib(ctx, dest, src, false);
  1464.  
  1465.       _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB,
  1466.                           src->ArrayBufferObj->Name);
  1467.    } else {
  1468.       copy_array_attrib(ctx, dest, src, true);
  1469.    }
  1470.  
  1471.    if (!arb_vao
  1472.        || src->ArrayObj->ElementArrayBufferObj->Name == 0
  1473.        || _mesa_IsBuffer(src->ArrayObj->ElementArrayBufferObj->Name))
  1474.       _mesa_BindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,
  1475.                           src->ArrayObj->ElementArrayBufferObj->Name);
  1476. }
  1477.  
  1478. /**
  1479.  * init/alloc the fields of 'attrib'.
  1480.  * Needs to the init part matching free_array_attrib_data below.
  1481.  */
  1482. static void
  1483. init_array_attrib_data(struct gl_context *ctx,
  1484.                        struct gl_array_attrib *attrib)
  1485. {
  1486.    /* Get a non driver gl_array_object. */
  1487.    attrib->ArrayObj = CALLOC_STRUCT( gl_array_object );
  1488.    _mesa_initialize_array_object(ctx, attrib->ArrayObj, 0);
  1489. }
  1490.  
  1491. /**
  1492.  * Free/unreference the fields of 'attrib' but don't delete it (that's
  1493.  * done later in the calling code).
  1494.  * Needs to the cleanup part matching init_array_attrib_data above.
  1495.  */
  1496. static void
  1497. free_array_attrib_data(struct gl_context *ctx,
  1498.                        struct gl_array_attrib *attrib)
  1499. {
  1500.    /* We use a non driver array object, so don't just unref since we would
  1501.     * end up using the drivers DeleteArrayObject function for deletion. */
  1502.    _mesa_delete_array_object(ctx, attrib->ArrayObj);
  1503.    attrib->ArrayObj = 0;
  1504.    _mesa_reference_buffer_object(ctx, &attrib->ArrayBufferObj, NULL);
  1505. }
  1506.  
  1507.  
  1508. void GLAPIENTRY
  1509. _mesa_PushClientAttrib(GLbitfield mask)
  1510. {
  1511.    struct gl_attrib_node *head;
  1512.  
  1513.    GET_CURRENT_CONTEXT(ctx);
  1514.  
  1515.    if (ctx->ClientAttribStackDepth >= MAX_CLIENT_ATTRIB_STACK_DEPTH) {
  1516.       _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
  1517.       return;
  1518.    }
  1519.  
  1520.    /* Build linked list of attribute nodes which save all attribute
  1521.     * groups specified by the mask.
  1522.     */
  1523.    head = NULL;
  1524.  
  1525.    if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
  1526.       struct gl_pixelstore_attrib *attr;
  1527.       /* packing attribs */
  1528.       attr = CALLOC_STRUCT( gl_pixelstore_attrib );
  1529.       copy_pixelstore(ctx, attr, &ctx->Pack);
  1530.       save_attrib_data(&head, GL_CLIENT_PACK_BIT, attr);
  1531.       /* unpacking attribs */
  1532.       attr = CALLOC_STRUCT( gl_pixelstore_attrib );
  1533.       copy_pixelstore(ctx, attr, &ctx->Unpack);
  1534.       save_attrib_data(&head, GL_CLIENT_UNPACK_BIT, attr);
  1535.    }
  1536.  
  1537.    if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
  1538.       struct gl_array_attrib *attr;
  1539.       attr = CALLOC_STRUCT( gl_array_attrib );
  1540.       init_array_attrib_data(ctx, attr);
  1541.       save_array_attrib(ctx, attr, &ctx->Array);
  1542.       save_attrib_data(&head, GL_CLIENT_VERTEX_ARRAY_BIT, attr);
  1543.    }
  1544.  
  1545.    ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
  1546.    ctx->ClientAttribStackDepth++;
  1547. }
  1548.  
  1549.  
  1550.  
  1551.  
  1552. void GLAPIENTRY
  1553. _mesa_PopClientAttrib(void)
  1554. {
  1555.    struct gl_attrib_node *node, *next;
  1556.  
  1557.    GET_CURRENT_CONTEXT(ctx);
  1558.    FLUSH_VERTICES(ctx, 0);
  1559.  
  1560.    if (ctx->ClientAttribStackDepth == 0) {
  1561.       _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
  1562.       return;
  1563.    }
  1564.  
  1565.    ctx->ClientAttribStackDepth--;
  1566.    node = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
  1567.  
  1568.    while (node) {
  1569.       switch (node->kind) {
  1570.          case GL_CLIENT_PACK_BIT:
  1571.             {
  1572.                struct gl_pixelstore_attrib *store =
  1573.                   (struct gl_pixelstore_attrib *) node->data;
  1574.                copy_pixelstore(ctx, &ctx->Pack, store);
  1575.                _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
  1576.             }
  1577.             break;
  1578.          case GL_CLIENT_UNPACK_BIT:
  1579.             {
  1580.                struct gl_pixelstore_attrib *store =
  1581.                   (struct gl_pixelstore_attrib *) node->data;
  1582.                copy_pixelstore(ctx, &ctx->Unpack, store);
  1583.                _mesa_reference_buffer_object(ctx, &store->BufferObj, NULL);
  1584.             }
  1585.             break;
  1586.          case GL_CLIENT_VERTEX_ARRAY_BIT: {
  1587.             struct gl_array_attrib * attr =
  1588.               (struct gl_array_attrib *) node->data;
  1589.             restore_array_attrib(ctx, &ctx->Array, attr);
  1590.             free_array_attrib_data(ctx, attr);
  1591.             ctx->NewState |= _NEW_ARRAY;
  1592.             break;
  1593.          }
  1594.          default:
  1595.             _mesa_problem( ctx, "Bad attrib flag in PopClientAttrib");
  1596.             break;
  1597.       }
  1598.  
  1599.       next = node->next;
  1600.       free(node->data);
  1601.       free(node);
  1602.       node = next;
  1603.    }
  1604. }
  1605.  
  1606.  
  1607. /**
  1608.  * Free any attribute state data that might be attached to the context.
  1609.  */
  1610. void
  1611. _mesa_free_attrib_data(struct gl_context *ctx)
  1612. {
  1613.    while (ctx->AttribStackDepth > 0) {
  1614.       struct gl_attrib_node *attr, *next;
  1615.  
  1616.       ctx->AttribStackDepth--;
  1617.       attr = ctx->AttribStack[ctx->AttribStackDepth];
  1618.  
  1619.       while (attr) {
  1620.          if (attr->kind == GL_TEXTURE_BIT) {
  1621.             struct texture_state *texstate = (struct texture_state*)attr->data;
  1622.             GLuint u, tgt;
  1623.             /* clear references to the saved texture objects */
  1624.             for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
  1625.                for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
  1626.                   _mesa_reference_texobj(&texstate->SavedTexRef[u][tgt], NULL);
  1627.                }
  1628.             }
  1629.             _mesa_reference_shared_state(ctx, &texstate->SharedRef, NULL);
  1630.          }
  1631.          else {
  1632.             /* any other chunks of state that requires special handling? */
  1633.          }
  1634.  
  1635.          next = attr->next;
  1636.          free(attr->data);
  1637.          free(attr);
  1638.          attr = next;
  1639.       }
  1640.    }
  1641. }
  1642.  
  1643.  
  1644. void _mesa_init_attrib( struct gl_context *ctx )
  1645. {
  1646.    /* Renderer and client attribute stacks */
  1647.    ctx->AttribStackDepth = 0;
  1648.    ctx->ClientAttribStackDepth = 0;
  1649. }
  1650.