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