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