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