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