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