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