added memory macros
[mesa.git] / src / mesa / main / attrib.c
1 /* $Id: attrib.c,v 1.5 1999/10/08 09:27:10 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28
29
30
31 #ifdef PC_HEADER
32 #include "all.h"
33 #else
34 #ifndef XFree86Server
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #else
39 #include "GL/xf86glx.h"
40 #endif
41 #include "attrib.h"
42 #include "context.h"
43 #include "glmisc.h"
44 #include "enable.h"
45 #include "enums.h"
46 #include "macros.h"
47 #include "simple_list.h"
48 #include "texstate.h"
49 #include "types.h"
50 #ifdef XFree86Server
51 #undef MISC_H
52 #include "GL/xf86glx.h"
53 #endif
54 #endif
55
56
57 #define MALLOC_STRUCT(T) (struct T *) malloc( sizeof(struct T) )
58
59
60
61 /*
62 * Allocate a new attribute state node. These nodes have a
63 * "kind" value and a pointer to a struct of state data.
64 */
65 static struct gl_attrib_node *new_attrib_node( GLbitfield kind )
66 {
67 struct gl_attrib_node *an;
68
69 an = (struct gl_attrib_node *) malloc( sizeof(struct gl_attrib_node) );
70 if (an) {
71 an->kind = kind;
72 }
73 return an;
74 }
75
76
77
78 /*
79 * Copy texture object state from one texture object to another.
80 */
81 static void copy_texobj_state( struct gl_texture_object *dest,
82 const struct gl_texture_object *src )
83 {
84 /*
85 dest->Name = src->Name;
86 dest->Dimensions = src->Dimensions;
87 */
88 dest->Priority = src->Priority;
89 dest->BorderColor[0] = src->BorderColor[0];
90 dest->BorderColor[1] = src->BorderColor[1];
91 dest->BorderColor[2] = src->BorderColor[2];
92 dest->BorderColor[3] = src->BorderColor[3];
93 dest->WrapS = src->WrapS;
94 dest->WrapT = src->WrapT;
95 dest->WrapR = src->WrapR;
96 dest->MinFilter = src->MinFilter;
97 dest->MagFilter = src->MagFilter;
98 dest->MinLod = src->MinLod;
99 dest->MaxLod = src->MaxLod;
100 dest->BaseLevel = src->BaseLevel;
101 dest->MaxLevel = src->MaxLevel;
102 dest->P = src->P;
103 dest->M = src->M;
104 dest->MinMagThresh = src->MinMagThresh;
105 memcpy( dest->Palette, src->Palette,
106 sizeof(GLubyte) * MAX_TEXTURE_PALETTE_SIZE * 4 );
107 dest->PaletteSize = src->PaletteSize;
108 dest->PaletteIntFormat = src->PaletteIntFormat;
109 dest->PaletteFormat = src->PaletteFormat;
110 dest->Complete = src->Complete;
111 dest->SampleFunc = src->SampleFunc;
112 }
113
114
115
116 void gl_PushAttrib( GLcontext* ctx, GLbitfield mask )
117 {
118 struct gl_attrib_node *newnode;
119 struct gl_attrib_node *head;
120
121 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushAttrib");
122
123 if (MESA_VERBOSE&VERBOSE_API)
124 fprintf(stderr, "glPushAttrib %x\n", mask);
125
126 if (ctx->AttribStackDepth>=MAX_ATTRIB_STACK_DEPTH) {
127 gl_error( ctx, GL_STACK_OVERFLOW, "glPushAttrib" );
128 return;
129 }
130
131 /* Build linked list of attribute nodes which save all attribute */
132 /* groups specified by the mask. */
133 head = NULL;
134
135 if (mask & GL_ACCUM_BUFFER_BIT) {
136 struct gl_accum_attrib *attr;
137 attr = MALLOC_STRUCT( gl_accum_attrib );
138 MEMCPY( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
139 newnode = new_attrib_node( GL_ACCUM_BUFFER_BIT );
140 newnode->data = attr;
141 newnode->next = head;
142 head = newnode;
143 }
144
145 if (mask & GL_COLOR_BUFFER_BIT) {
146 struct gl_colorbuffer_attrib *attr;
147 attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
148 MEMCPY( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
149 newnode = new_attrib_node( GL_COLOR_BUFFER_BIT );
150 newnode->data = attr;
151 newnode->next = head;
152 head = newnode;
153 }
154
155 if (mask & GL_CURRENT_BIT) {
156 struct gl_current_attrib *attr;
157 attr = MALLOC_STRUCT( gl_current_attrib );
158 MEMCPY( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
159 newnode = new_attrib_node( GL_CURRENT_BIT );
160 newnode->data = attr;
161 newnode->next = head;
162 head = newnode;
163 }
164
165 if (mask & GL_DEPTH_BUFFER_BIT) {
166 struct gl_depthbuffer_attrib *attr;
167 attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
168 MEMCPY( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
169 newnode = new_attrib_node( GL_DEPTH_BUFFER_BIT );
170 newnode->data = attr;
171 newnode->next = head;
172 head = newnode;
173 }
174
175 if (mask & GL_ENABLE_BIT) {
176 struct gl_enable_attrib *attr;
177 GLuint i;
178 attr = MALLOC_STRUCT( gl_enable_attrib );
179 /* Copy enable flags from all other attributes into the enable struct. */
180 attr->AlphaTest = ctx->Color.AlphaEnabled;
181 attr->AutoNormal = ctx->Eval.AutoNormal;
182 attr->Blend = ctx->Color.BlendEnabled;
183 for (i=0;i<MAX_CLIP_PLANES;i++) {
184 attr->ClipPlane[i] = ctx->Transform.ClipEnabled[i];
185 }
186 attr->ColorMaterial = ctx->Light.ColorMaterialEnabled;
187 attr->CullFace = ctx->Polygon.CullFlag;
188 attr->DepthTest = ctx->Depth.Test;
189 attr->Dither = ctx->Color.DitherFlag;
190 attr->Fog = ctx->Fog.Enabled;
191 for (i=0;i<MAX_LIGHTS;i++) {
192 attr->Light[i] = ctx->Light.Light[i].Enabled;
193 }
194 attr->Lighting = ctx->Light.Enabled;
195 attr->LineSmooth = ctx->Line.SmoothFlag;
196 attr->LineStipple = ctx->Line.StippleFlag;
197 attr->IndexLogicOp = ctx->Color.IndexLogicOpEnabled;
198 attr->ColorLogicOp = ctx->Color.ColorLogicOpEnabled;
199 attr->Map1Color4 = ctx->Eval.Map1Color4;
200 attr->Map1Index = ctx->Eval.Map1Index;
201 attr->Map1Normal = ctx->Eval.Map1Normal;
202 attr->Map1TextureCoord1 = ctx->Eval.Map1TextureCoord1;
203 attr->Map1TextureCoord2 = ctx->Eval.Map1TextureCoord2;
204 attr->Map1TextureCoord3 = ctx->Eval.Map1TextureCoord3;
205 attr->Map1TextureCoord4 = ctx->Eval.Map1TextureCoord4;
206 attr->Map1Vertex3 = ctx->Eval.Map1Vertex3;
207 attr->Map1Vertex4 = ctx->Eval.Map1Vertex4;
208 attr->Map2Color4 = ctx->Eval.Map2Color4;
209 attr->Map2Index = ctx->Eval.Map2Index;
210 attr->Map2Normal = ctx->Eval.Map2Normal;
211 attr->Map2TextureCoord1 = ctx->Eval.Map2TextureCoord1;
212 attr->Map2TextureCoord2 = ctx->Eval.Map2TextureCoord2;
213 attr->Map2TextureCoord3 = ctx->Eval.Map2TextureCoord3;
214 attr->Map2TextureCoord4 = ctx->Eval.Map2TextureCoord4;
215 attr->Map2Vertex3 = ctx->Eval.Map2Vertex3;
216 attr->Map2Vertex4 = ctx->Eval.Map2Vertex4;
217 attr->Normalize = ctx->Transform.Normalize;
218 attr->PointSmooth = ctx->Point.SmoothFlag;
219 attr->PolygonOffsetPoint = ctx->Polygon.OffsetPoint;
220 attr->PolygonOffsetLine = ctx->Polygon.OffsetLine;
221 attr->PolygonOffsetFill = ctx->Polygon.OffsetFill;
222 attr->PolygonSmooth = ctx->Polygon.SmoothFlag;
223 attr->PolygonStipple = ctx->Polygon.StippleFlag;
224 attr->RescaleNormals = ctx->Transform.RescaleNormals;
225 attr->Scissor = ctx->Scissor.Enabled;
226 attr->Stencil = ctx->Stencil.Enabled;
227 attr->Texture = ctx->Texture.Enabled;
228 for (i=0; i<MAX_TEXTURE_UNITS; i++) {
229 attr->TexGen[i] = ctx->Texture.Unit[i].TexGenEnabled;
230 }
231 newnode = new_attrib_node( GL_ENABLE_BIT );
232 newnode->data = attr;
233 newnode->next = head;
234 head = newnode;
235 }
236
237 if (mask & GL_EVAL_BIT) {
238 struct gl_eval_attrib *attr;
239 attr = MALLOC_STRUCT( gl_eval_attrib );
240 MEMCPY( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
241 newnode = new_attrib_node( GL_EVAL_BIT );
242 newnode->data = attr;
243 newnode->next = head;
244 head = newnode;
245 }
246
247 if (mask & GL_FOG_BIT) {
248 struct gl_fog_attrib *attr;
249 attr = MALLOC_STRUCT( gl_fog_attrib );
250 MEMCPY( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
251 newnode = new_attrib_node( GL_FOG_BIT );
252 newnode->data = attr;
253 newnode->next = head;
254 head = newnode;
255 }
256
257 if (mask & GL_HINT_BIT) {
258 struct gl_hint_attrib *attr;
259 attr = MALLOC_STRUCT( gl_hint_attrib );
260 MEMCPY( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
261 newnode = new_attrib_node( GL_HINT_BIT );
262 newnode->data = attr;
263 newnode->next = head;
264 head = newnode;
265 }
266
267 if (mask & GL_LIGHTING_BIT) {
268 struct gl_light_attrib *attr;
269 attr = MALLOC_STRUCT( gl_light_attrib );
270 MEMCPY( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
271 newnode = new_attrib_node( GL_LIGHTING_BIT );
272 newnode->data = attr;
273 newnode->next = head;
274 head = newnode;
275 }
276
277 if (mask & GL_LINE_BIT) {
278 struct gl_line_attrib *attr;
279 attr = MALLOC_STRUCT( gl_line_attrib );
280 MEMCPY( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
281 newnode = new_attrib_node( GL_LINE_BIT );
282 newnode->data = attr;
283 newnode->next = head;
284 head = newnode;
285 }
286
287 if (mask & GL_LIST_BIT) {
288 struct gl_list_attrib *attr;
289 attr = MALLOC_STRUCT( gl_list_attrib );
290 MEMCPY( attr, &ctx->List, sizeof(struct gl_list_attrib) );
291 newnode = new_attrib_node( GL_LIST_BIT );
292 newnode->data = attr;
293 newnode->next = head;
294 head = newnode;
295 }
296
297 if (mask & GL_PIXEL_MODE_BIT) {
298 struct gl_pixel_attrib *attr;
299 attr = MALLOC_STRUCT( gl_pixel_attrib );
300 MEMCPY( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
301 newnode = new_attrib_node( GL_PIXEL_MODE_BIT );
302 newnode->data = attr;
303 newnode->next = head;
304 head = newnode;
305 }
306
307 if (mask & GL_POINT_BIT) {
308 struct gl_point_attrib *attr;
309 attr = MALLOC_STRUCT( gl_point_attrib );
310 MEMCPY( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
311 newnode = new_attrib_node( GL_POINT_BIT );
312 newnode->data = attr;
313 newnode->next = head;
314 head = newnode;
315 }
316
317 if (mask & GL_POLYGON_BIT) {
318 struct gl_polygon_attrib *attr;
319 attr = MALLOC_STRUCT( gl_polygon_attrib );
320 MEMCPY( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
321 newnode = new_attrib_node( GL_POLYGON_BIT );
322 newnode->data = attr;
323 newnode->next = head;
324 head = newnode;
325 }
326
327 if (mask & GL_POLYGON_STIPPLE_BIT) {
328 GLuint *stipple;
329 stipple = (GLuint *) malloc( 32*sizeof(GLuint) );
330 MEMCPY( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
331 newnode = new_attrib_node( GL_POLYGON_STIPPLE_BIT );
332 newnode->data = stipple;
333 newnode->next = head;
334 head = newnode;
335 }
336
337 if (mask & GL_SCISSOR_BIT) {
338 struct gl_scissor_attrib *attr;
339 attr = MALLOC_STRUCT( gl_scissor_attrib );
340 MEMCPY( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
341 newnode = new_attrib_node( GL_SCISSOR_BIT );
342 newnode->data = attr;
343 newnode->next = head;
344 head = newnode;
345 }
346
347 if (mask & GL_STENCIL_BUFFER_BIT) {
348 struct gl_stencil_attrib *attr;
349 attr = MALLOC_STRUCT( gl_stencil_attrib );
350 MEMCPY( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
351 newnode = new_attrib_node( GL_STENCIL_BUFFER_BIT );
352 newnode->data = attr;
353 newnode->next = head;
354 head = newnode;
355 }
356
357 if (mask & GL_TEXTURE_BIT) {
358 struct gl_texture_attrib *attr;
359 GLuint u;
360 /* Take care of texture object reference counters */
361 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
362 ctx->Texture.Unit[u].CurrentD[1]->RefCount++;
363 ctx->Texture.Unit[u].CurrentD[2]->RefCount++;
364 ctx->Texture.Unit[u].CurrentD[3]->RefCount++;
365 }
366 attr = MALLOC_STRUCT( gl_texture_attrib );
367 MEMCPY( attr, &ctx->Texture, sizeof(struct gl_texture_attrib) );
368 /* copy state of the currently bound texture objects */
369 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
370 copy_texobj_state(&attr->Unit[u].Saved1D, attr->Unit[u].CurrentD[1]);
371 copy_texobj_state(&attr->Unit[u].Saved2D, attr->Unit[u].CurrentD[2]);
372 copy_texobj_state(&attr->Unit[u].Saved3D, attr->Unit[u].CurrentD[3]);
373 }
374 newnode = new_attrib_node( GL_TEXTURE_BIT );
375 newnode->data = attr;
376 newnode->next = head;
377 head = newnode;
378 }
379
380 if (mask & GL_TRANSFORM_BIT) {
381 struct gl_transform_attrib *attr;
382 attr = MALLOC_STRUCT( gl_transform_attrib );
383 MEMCPY( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
384 newnode = new_attrib_node( GL_TRANSFORM_BIT );
385 newnode->data = attr;
386 newnode->next = head;
387 head = newnode;
388 }
389
390 if (mask & GL_VIEWPORT_BIT) {
391 struct gl_viewport_attrib *attr;
392 attr = MALLOC_STRUCT( gl_viewport_attrib );
393 MEMCPY( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
394 newnode = new_attrib_node( GL_VIEWPORT_BIT );
395 newnode->data = attr;
396 newnode->next = head;
397 head = newnode;
398 }
399
400 ctx->AttribStack[ctx->AttribStackDepth] = head;
401 ctx->AttribStackDepth++;
402 }
403
404
405
406 /*
407 * This function is kind of long just because we have to call a lot
408 * of device driver functions to update device driver state.
409 */
410 void gl_PopAttrib( GLcontext* ctx )
411 {
412 struct gl_attrib_node *attr, *next;
413
414 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopAttrib");
415
416
417 if (ctx->AttribStackDepth==0) {
418 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopAttrib" );
419 return;
420 }
421
422 ctx->AttribStackDepth--;
423 attr = ctx->AttribStack[ctx->AttribStackDepth];
424
425 while (attr) {
426
427 if (MESA_VERBOSE&VERBOSE_API)
428 fprintf(stderr, "glPopAttrib %s\n", gl_lookup_enum_by_nr(attr->kind));
429
430 switch (attr->kind) {
431 case GL_ACCUM_BUFFER_BIT:
432 MEMCPY( &ctx->Accum, attr->data, sizeof(struct gl_accum_attrib) );
433 break;
434 case GL_COLOR_BUFFER_BIT:
435 {
436 GLenum oldDrawBuffer = ctx->Color.DrawBuffer;
437 GLenum oldAlphaFunc = ctx->Color.AlphaFunc;
438 GLubyte oldAlphaRef = ctx->Color.AlphaRef;
439 GLenum oldBlendSrc = ctx->Color.BlendSrcRGB;
440 GLenum oldBlendDst = ctx->Color.BlendDstRGB;
441 MEMCPY( &ctx->Color, attr->data,
442 sizeof(struct gl_colorbuffer_attrib) );
443 if (ctx->Color.DrawBuffer != oldDrawBuffer) {
444 gl_DrawBuffer(ctx, ctx->Color.DrawBuffer);
445 }
446 if ((ctx->Color.AlphaFunc != oldAlphaFunc ||
447 ctx->Color.AlphaRef != oldAlphaRef) &&
448 ctx->Driver.AlphaFunc)
449 (*ctx->Driver.AlphaFunc)( ctx, ctx->Color.AlphaFunc,
450 ctx->Color.AlphaRef / 255.0F);
451 if ((ctx->Color.BlendSrcRGB != oldBlendSrc ||
452 ctx->Color.BlendSrcRGB != oldBlendDst) &&
453 ctx->Driver.BlendFunc)
454 (*ctx->Driver.BlendFunc)( ctx, ctx->Color.BlendSrcRGB,
455 ctx->Color.BlendDstRGB);
456 }
457 break;
458 case GL_CURRENT_BIT:
459 MEMCPY( &ctx->Current, attr->data,
460 sizeof(struct gl_current_attrib) );
461 break;
462 case GL_DEPTH_BUFFER_BIT:
463 {
464 GLenum oldDepthFunc = ctx->Depth.Func;
465 GLboolean oldDepthMask = ctx->Depth.Mask;
466 GLfloat oldDepthClear = ctx->Depth.Clear;
467 MEMCPY( &ctx->Depth, attr->data,
468 sizeof(struct gl_depthbuffer_attrib) );
469 if (ctx->Depth.Func != oldDepthFunc && ctx->Driver.DepthFunc)
470 (*ctx->Driver.DepthFunc)( ctx, ctx->Depth.Func );
471 if (ctx->Depth.Mask != oldDepthMask && ctx->Driver.DepthMask)
472 (*ctx->Driver.DepthMask)( ctx, ctx->Depth.Mask );
473 if (ctx->Depth.Clear != oldDepthClear && ctx->Driver.ClearDepth)
474 (*ctx->Driver.ClearDepth)( ctx, ctx->Depth.Clear );
475 }
476 break;
477 case GL_ENABLE_BIT:
478 {
479 const struct gl_enable_attrib *enable;
480 enable = (const struct gl_enable_attrib *) attr->data;
481
482 #define TEST_AND_UPDATE(VALUE, NEWVALUE, ENUM) \
483 if ((VALUE) != (NEWVALUE)) { \
484 gl_set_enable( ctx, ENUM, (NEWVALUE) ); \
485 }
486
487 TEST_AND_UPDATE(ctx->Color.AlphaEnabled, enable->AlphaTest, GL_ALPHA_TEST);
488 TEST_AND_UPDATE(ctx->Transform.Normalize, enable->AutoNormal, GL_NORMALIZE);
489 TEST_AND_UPDATE(ctx->Color.BlendEnabled, enable->Blend, GL_BLEND);
490 {
491 GLuint i;
492 for (i=0;i<MAX_CLIP_PLANES;i++) {
493 if (ctx->Transform.ClipEnabled[i] != enable->ClipPlane[i])
494 gl_set_enable( ctx, (GLenum) (GL_CLIP_PLANE0 + i), enable->ClipPlane[i] );
495 }
496 }
497 TEST_AND_UPDATE(ctx->Light.ColorMaterialEnabled, enable->ColorMaterial, GL_COLOR_MATERIAL);
498 TEST_AND_UPDATE(ctx->Polygon.CullFlag, enable->CullFace, GL_CULL_FACE);
499 TEST_AND_UPDATE(ctx->Depth.Test, enable->DepthTest, GL_DEPTH_TEST);
500 TEST_AND_UPDATE(ctx->Color.DitherFlag, enable->Dither, GL_DITHER);
501 TEST_AND_UPDATE(ctx->Fog.Enabled, enable->Fog, GL_FOG);
502 TEST_AND_UPDATE(ctx->Light.Enabled, enable->Lighting, GL_LIGHTING);
503 TEST_AND_UPDATE(ctx->Line.SmoothFlag, enable->LineSmooth, GL_LINE_SMOOTH);
504 TEST_AND_UPDATE(ctx->Line.StippleFlag, enable->LineStipple, GL_LINE_STIPPLE);
505 TEST_AND_UPDATE(ctx->Color.IndexLogicOpEnabled, enable->IndexLogicOp, GL_INDEX_LOGIC_OP);
506 TEST_AND_UPDATE(ctx->Color.ColorLogicOpEnabled, enable->ColorLogicOp, GL_COLOR_LOGIC_OP);
507 TEST_AND_UPDATE(ctx->Eval.Map1Color4, enable->Map1Color4, GL_MAP1_COLOR_4);
508 TEST_AND_UPDATE(ctx->Eval.Map1Index, enable->Map1Index, GL_MAP1_INDEX);
509 TEST_AND_UPDATE(ctx->Eval.Map1Normal, enable->Map1Normal, GL_MAP1_NORMAL);
510 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord1, enable->Map1TextureCoord1, GL_MAP1_TEXTURE_COORD_1);
511 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord2, enable->Map1TextureCoord2, GL_MAP1_TEXTURE_COORD_2);
512 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord3, enable->Map1TextureCoord3, GL_MAP1_TEXTURE_COORD_3);
513 TEST_AND_UPDATE(ctx->Eval.Map1TextureCoord4, enable->Map1TextureCoord4, GL_MAP1_TEXTURE_COORD_4);
514 TEST_AND_UPDATE(ctx->Eval.Map1Vertex3, enable->Map1Vertex3, GL_MAP1_VERTEX_3);
515 TEST_AND_UPDATE(ctx->Eval.Map1Vertex4, enable->Map1Vertex4, GL_MAP1_VERTEX_4);
516 TEST_AND_UPDATE(ctx->Eval.Map2Color4, enable->Map2Color4, GL_MAP2_COLOR_4);
517 TEST_AND_UPDATE(ctx->Eval.Map2Index, enable->Map2Index, GL_MAP2_INDEX);
518 TEST_AND_UPDATE(ctx->Eval.Map2Normal, enable->Map2Normal, GL_MAP2_NORMAL);
519 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord1, enable->Map2TextureCoord1, GL_MAP2_TEXTURE_COORD_1);
520 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord2, enable->Map2TextureCoord2, GL_MAP2_TEXTURE_COORD_2);
521 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord3, enable->Map2TextureCoord3, GL_MAP2_TEXTURE_COORD_3);
522 TEST_AND_UPDATE(ctx->Eval.Map2TextureCoord4, enable->Map2TextureCoord4, GL_MAP2_TEXTURE_COORD_4);
523 TEST_AND_UPDATE(ctx->Eval.Map2Vertex3, enable->Map2Vertex3, GL_MAP2_VERTEX_3);
524 TEST_AND_UPDATE(ctx->Eval.Map2Vertex4, enable->Map2Vertex4, GL_MAP2_VERTEX_4);
525 TEST_AND_UPDATE(ctx->Transform.Normalize, enable->Normalize, GL_NORMALIZE);
526 TEST_AND_UPDATE(ctx->Transform.RescaleNormals, enable->RescaleNormals, GL_RESCALE_NORMAL_EXT);
527 TEST_AND_UPDATE(ctx->Point.SmoothFlag, enable->PointSmooth, GL_POINT_SMOOTH);
528 TEST_AND_UPDATE(ctx->Polygon.OffsetPoint, enable->PolygonOffsetPoint, GL_POLYGON_OFFSET_POINT);
529 TEST_AND_UPDATE(ctx->Polygon.OffsetLine, enable->PolygonOffsetLine, GL_POLYGON_OFFSET_LINE);
530 TEST_AND_UPDATE(ctx->Polygon.OffsetFill, enable->PolygonOffsetFill, GL_POLYGON_OFFSET_FILL);
531 TEST_AND_UPDATE(ctx->Polygon.SmoothFlag, enable->PolygonSmooth, GL_POLYGON_SMOOTH);
532 TEST_AND_UPDATE(ctx->Polygon.StippleFlag, enable->PolygonStipple, GL_POLYGON_STIPPLE);
533 TEST_AND_UPDATE(ctx->Scissor.Enabled, enable->Scissor, GL_SCISSOR_TEST);
534 TEST_AND_UPDATE(ctx->Stencil.Enabled, enable->Stencil, GL_STENCIL_TEST);
535 if (ctx->Texture.Enabled != enable->Texture) {
536 ctx->Texture.Enabled = enable->Texture;
537 if (ctx->Driver.Enable) {
538 if (ctx->Driver.ActiveTexture)
539 (*ctx->Driver.ActiveTexture)( ctx, 0 );
540 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_1D, (GLboolean) (enable->Texture & TEXTURE0_1D) );
541 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_2D, (GLboolean) (enable->Texture & TEXTURE0_2D) );
542 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_3D, (GLboolean) (enable->Texture & TEXTURE0_3D) );
543 if (ctx->Driver.ActiveTexture)
544 (*ctx->Driver.ActiveTexture)( ctx, 1 );
545 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_1D, (GLboolean) (enable->Texture & TEXTURE1_1D) );
546 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_2D, (GLboolean) (enable->Texture & TEXTURE1_2D) );
547 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_3D, (GLboolean) (enable->Texture & TEXTURE1_3D) );
548 if (ctx->Driver.ActiveTexture)
549 (*ctx->Driver.ActiveTexture)( ctx, ctx->Texture.CurrentUnit );
550 }
551 }
552 #undef TEST_AND_UPDATE
553 {
554 GLuint i;
555 for (i=0; i<MAX_TEXTURE_UNITS; i++) {
556 if (ctx->Texture.Unit[i].TexGenEnabled != enable->TexGen[i]) {
557 ctx->Texture.Unit[i].TexGenEnabled = enable->TexGen[i];
558
559 /* ctx->Enabled recalculated in state change
560 processing */
561
562 if (ctx->Driver.Enable) {
563 if (ctx->Driver.ActiveTexture)
564 (*ctx->Driver.ActiveTexture)( ctx, i );
565 if (enable->TexGen[i] & S_BIT)
566 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_S, GL_TRUE);
567 else
568 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_S, GL_FALSE);
569 if (enable->TexGen[i] & T_BIT)
570 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_T, GL_TRUE);
571 else
572 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_T, GL_FALSE);
573 if (enable->TexGen[i] & R_BIT)
574 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_R, GL_TRUE);
575 else
576 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_R, GL_FALSE);
577 if (enable->TexGen[i] & Q_BIT)
578 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_Q, GL_TRUE);
579 else
580 (*ctx->Driver.Enable)( ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
581 }
582 }
583 }
584 if (ctx->Driver.ActiveTexture)
585 (*ctx->Driver.ActiveTexture)( ctx, ctx->Texture.CurrentUnit );
586 }
587 }
588 break;
589 case GL_EVAL_BIT:
590 MEMCPY( &ctx->Eval, attr->data, sizeof(struct gl_eval_attrib) );
591 break;
592 case GL_FOG_BIT:
593 {
594 GLboolean anyChange = (memcmp( &ctx->Fog, attr->data, sizeof(struct gl_fog_attrib) ) != 0);
595 MEMCPY( &ctx->Fog, attr->data, sizeof(struct gl_fog_attrib) );
596 if (anyChange && ctx->Driver.Fogfv) {
597 const GLfloat mode = ctx->Fog.Mode;
598 const GLfloat density = ctx->Fog.Density;
599 const GLfloat start = ctx->Fog.Start;
600 const GLfloat end = ctx->Fog.End;
601 const GLfloat index = ctx->Fog.Index;
602 (*ctx->Driver.Fogfv)( ctx, GL_FOG_MODE, &mode);
603 (*ctx->Driver.Fogfv)( ctx, GL_FOG_DENSITY, &density );
604 (*ctx->Driver.Fogfv)( ctx, GL_FOG_START, &start );
605 (*ctx->Driver.Fogfv)( ctx, GL_FOG_END, &end );
606 (*ctx->Driver.Fogfv)( ctx, GL_FOG_INDEX, &index );
607 (*ctx->Driver.Fogfv)( ctx, GL_FOG_COLOR, ctx->Fog.Color );
608 }
609 ctx->Enabled &= ENABLE_FOG;
610 if (ctx->Fog.Enabled) ctx->Enabled |= ENABLE_FOG;
611 }
612 break;
613 case GL_HINT_BIT:
614 MEMCPY( &ctx->Hint, attr->data, sizeof(struct gl_hint_attrib) );
615 if (ctx->Driver.Hint) {
616 (*ctx->Driver.Hint)( ctx, GL_PERSPECTIVE_CORRECTION_HINT,
617 ctx->Hint.PerspectiveCorrection );
618 (*ctx->Driver.Hint)( ctx, GL_POINT_SMOOTH_HINT,
619 ctx->Hint.PointSmooth);
620 (*ctx->Driver.Hint)( ctx, GL_LINE_SMOOTH_HINT,
621 ctx->Hint.LineSmooth );
622 (*ctx->Driver.Hint)( ctx, GL_POLYGON_SMOOTH_HINT,
623 ctx->Hint.PolygonSmooth );
624 (*ctx->Driver.Hint)( ctx, GL_FOG_HINT, ctx->Hint.Fog );
625 }
626 break;
627 case GL_LIGHTING_BIT:
628 MEMCPY( &ctx->Light, attr->data, sizeof(struct gl_light_attrib) );
629 if (ctx->Driver.Enable) {
630 GLuint i;
631 for (i = 0; i < MAX_LIGHTS; i++) {
632 GLenum light = (GLenum) (GL_LIGHT0 + i);
633 (*ctx->Driver.Enable)( ctx, light, ctx->Light.Light[i].Enabled );
634 }
635 (*ctx->Driver.Enable)( ctx, GL_LIGHTING, ctx->Light.Enabled );
636 }
637 ctx->Enabled &= ENABLE_LIGHT;
638 if (ctx->Light.Enabled && !is_empty_list(&ctx->Light.EnabledList))
639 ctx->Enabled |= ENABLE_LIGHT;
640 break;
641 case GL_LINE_BIT:
642 MEMCPY( &ctx->Line, attr->data, sizeof(struct gl_line_attrib) );
643 if (ctx->Driver.Enable) {
644 (*ctx->Driver.Enable)( ctx, GL_LINE_SMOOTH, ctx->Line.SmoothFlag );
645 (*ctx->Driver.Enable)( ctx, GL_LINE_STIPPLE, ctx->Line.StippleFlag );
646 }
647 break;
648 case GL_LIST_BIT:
649 MEMCPY( &ctx->List, attr->data, sizeof(struct gl_list_attrib) );
650 break;
651 case GL_PIXEL_MODE_BIT:
652 MEMCPY( &ctx->Pixel, attr->data, sizeof(struct gl_pixel_attrib) );
653 break;
654 case GL_POINT_BIT:
655 MEMCPY( &ctx->Point, attr->data, sizeof(struct gl_point_attrib) );
656 if (ctx->Driver.Enable)
657 (*ctx->Driver.Enable)( ctx, GL_POINT_SMOOTH, ctx->Point.SmoothFlag );
658 break;
659 case GL_POLYGON_BIT:
660 {
661 GLenum oldFrontMode = ctx->Polygon.FrontMode;
662 GLenum oldBackMode = ctx->Polygon.BackMode;
663 MEMCPY( &ctx->Polygon, attr->data,
664 sizeof(struct gl_polygon_attrib) );
665 if ((ctx->Polygon.FrontMode != oldFrontMode ||
666 ctx->Polygon.BackMode != oldBackMode) &&
667 ctx->Driver.PolygonMode) {
668 (*ctx->Driver.PolygonMode)( ctx, GL_FRONT, ctx->Polygon.FrontMode);
669 (*ctx->Driver.PolygonMode)( ctx, GL_BACK, ctx->Polygon.BackMode);
670 }
671 if (ctx->Driver.CullFace)
672 ctx->Driver.CullFace( ctx, ctx->Polygon.CullFaceMode );
673
674 if (ctx->Driver.FrontFace)
675 ctx->Driver.FrontFace( ctx, ctx->Polygon.FrontFace );
676
677 if (ctx->Driver.Enable)
678 (*ctx->Driver.Enable)( ctx, GL_POLYGON_SMOOTH, ctx->Polygon.SmoothFlag );
679 }
680 break;
681 case GL_POLYGON_STIPPLE_BIT:
682 MEMCPY( ctx->PolygonStipple, attr->data, 32*sizeof(GLuint) );
683 break;
684 case GL_SCISSOR_BIT:
685 MEMCPY( &ctx->Scissor, attr->data,
686 sizeof(struct gl_scissor_attrib) );
687 if (ctx->Driver.Enable)
688 (*ctx->Driver.Enable)( ctx, GL_SCISSOR_TEST, ctx->Scissor.Enabled );
689 if (ctx->Driver.Scissor)
690 ctx->Driver.Scissor( ctx, ctx->Scissor.X, ctx->Scissor.Y,
691 ctx->Scissor.Width, ctx->Scissor.Height );
692 break;
693 case GL_STENCIL_BUFFER_BIT:
694 MEMCPY( &ctx->Stencil, attr->data,
695 sizeof(struct gl_stencil_attrib) );
696 if (ctx->Driver.StencilFunc)
697 (*ctx->Driver.StencilFunc)( ctx, ctx->Stencil.Function,
698 ctx->Stencil.Ref, ctx->Stencil.ValueMask);
699 if (ctx->Driver.StencilMask)
700 (*ctx->Driver.StencilMask)( ctx, ctx->Stencil.WriteMask );
701 if (ctx->Driver.StencilOp)
702 (*ctx->Driver.StencilOp)( ctx, ctx->Stencil.FailFunc,
703 ctx->Stencil.ZFailFunc, ctx->Stencil.ZPassFunc);
704 if (ctx->Driver.ClearStencil)
705 (*ctx->Driver.ClearStencil)( ctx, ctx->Stencil.Clear );
706 if (ctx->Driver.Enable)
707 (*ctx->Driver.Enable)( ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled );
708 ctx->TriangleCaps &= ~DD_STENCIL;
709 if (ctx->Stencil.Enabled)
710 ctx->TriangleCaps |= DD_STENCIL;
711
712 break;
713 case GL_TRANSFORM_BIT:
714 MEMCPY( &ctx->Transform, attr->data,
715 sizeof(struct gl_transform_attrib) );
716 if (ctx->Driver.Enable) {
717 (*ctx->Driver.Enable)( ctx, GL_NORMALIZE, ctx->Transform.Normalize );
718 (*ctx->Driver.Enable)( ctx, GL_RESCALE_NORMAL_EXT, ctx->Transform.RescaleNormals );
719 }
720 ctx->Enabled &= ~(ENABLE_NORMALIZE|ENABLE_RESCALE);
721 if (ctx->Transform.Normalize) ctx->Enabled |= ENABLE_NORMALIZE;
722 if (ctx->Transform.RescaleNormals) ctx->Enabled |= ENABLE_RESCALE;
723 break;
724 case GL_TEXTURE_BIT:
725 /* Take care of texture object reference counters */
726 {
727 GLuint u;
728 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
729 ctx->Texture.Unit[u].CurrentD[1]->RefCount--;
730 ctx->Texture.Unit[u].CurrentD[2]->RefCount--;
731 ctx->Texture.Unit[u].CurrentD[3]->RefCount--;
732 }
733 MEMCPY( &ctx->Texture, attr->data,
734 sizeof(struct gl_texture_attrib) );
735 /* restore state of the currently bound texture objects */
736 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
737 copy_texobj_state( ctx->Texture.Unit[u].CurrentD[1],
738 &(ctx->Texture.Unit[u].Saved1D) );
739 copy_texobj_state( ctx->Texture.Unit[u].CurrentD[2],
740 &(ctx->Texture.Unit[u].Saved2D) );
741 copy_texobj_state( ctx->Texture.Unit[u].CurrentD[3],
742 &(ctx->Texture.Unit[u].Saved3D) );
743 gl_put_texobj_on_dirty_list( ctx, ctx->Texture.Unit[u].CurrentD[1] );
744 gl_put_texobj_on_dirty_list( ctx, ctx->Texture.Unit[u].CurrentD[2] );
745 gl_put_texobj_on_dirty_list( ctx, ctx->Texture.Unit[u].CurrentD[3] );
746
747 }
748 }
749 break;
750 case GL_VIEWPORT_BIT:
751 {
752 struct gl_viewport_attrib *v =
753 (struct gl_viewport_attrib *)attr->data;
754
755 gl_Viewport( ctx, v->X, v->Y, v->Width, v->Height );
756 gl_DepthRange( ctx, v->Near, v->Far );
757 break;
758 }
759 default:
760 gl_problem( ctx, "Bad attrib flag in PopAttrib");
761 break;
762 }
763
764 next = attr->next;
765 free( (void *) attr->data );
766 free( (void *) attr );
767 attr = next;
768 }
769
770 ctx->NewState = NEW_ALL;
771 }
772
773
774 #define GL_CLIENT_PACK_BIT (1<<20)
775 #define GL_CLIENT_UNPACK_BIT (1<<21)
776
777
778 void gl_PushClientAttrib( GLcontext *ctx, GLbitfield mask )
779 {
780 struct gl_attrib_node *newnode;
781 struct gl_attrib_node *head;
782
783 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushClientAttrib");
784
785 if (ctx->ClientAttribStackDepth>=MAX_CLIENT_ATTRIB_STACK_DEPTH) {
786 gl_error( ctx, GL_STACK_OVERFLOW, "glPushClientAttrib" );
787 return;
788 }
789
790 /* Build linked list of attribute nodes which save all attribute */
791 /* groups specified by the mask. */
792 head = NULL;
793
794 if (mask & GL_CLIENT_PIXEL_STORE_BIT) {
795 struct gl_pixelstore_attrib *attr;
796 /* packing attribs */
797 attr = MALLOC_STRUCT( gl_pixelstore_attrib );
798 MEMCPY( attr, &ctx->Pack, sizeof(struct gl_pixelstore_attrib) );
799 newnode = new_attrib_node( GL_CLIENT_PACK_BIT );
800 newnode->data = attr;
801 newnode->next = head;
802 head = newnode;
803 /* unpacking attribs */
804 attr = MALLOC_STRUCT( gl_pixelstore_attrib );
805 MEMCPY( attr, &ctx->Unpack, sizeof(struct gl_pixelstore_attrib) );
806 newnode = new_attrib_node( GL_CLIENT_UNPACK_BIT );
807 newnode->data = attr;
808 newnode->next = head;
809 head = newnode;
810 }
811 if (mask & GL_CLIENT_VERTEX_ARRAY_BIT) {
812 struct gl_array_attrib *attr;
813 attr = MALLOC_STRUCT( gl_array_attrib );
814 MEMCPY( attr, &ctx->Array, sizeof(struct gl_array_attrib) );
815 newnode = new_attrib_node( GL_CLIENT_VERTEX_ARRAY_BIT );
816 newnode->data = attr;
817 newnode->next = head;
818 head = newnode;
819 }
820
821 ctx->ClientAttribStack[ctx->ClientAttribStackDepth] = head;
822 ctx->ClientAttribStackDepth++;
823 }
824
825
826
827
828 void gl_PopClientAttrib( GLcontext *ctx )
829 {
830 struct gl_attrib_node *attr, *next;
831
832 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopClientAttrib");
833
834 if (ctx->ClientAttribStackDepth==0) {
835 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopClientAttrib" );
836 return;
837 }
838
839 ctx->ClientAttribStackDepth--;
840 attr = ctx->ClientAttribStack[ctx->ClientAttribStackDepth];
841
842 while (attr) {
843 switch (attr->kind) {
844 case GL_CLIENT_PACK_BIT:
845 MEMCPY( &ctx->Pack, attr->data,
846 sizeof(struct gl_pixelstore_attrib) );
847 break;
848 case GL_CLIENT_UNPACK_BIT:
849 MEMCPY( &ctx->Unpack, attr->data,
850 sizeof(struct gl_pixelstore_attrib) );
851 break;
852 case GL_CLIENT_VERTEX_ARRAY_BIT:
853 MEMCPY( &ctx->Array, attr->data,
854 sizeof(struct gl_array_attrib) );
855 break;
856 default:
857 gl_problem( ctx, "Bad attrib flag in PopClientAttrib");
858 break;
859 }
860
861 next = attr->next;
862 free( (void *) attr->data );
863 free( (void *) attr );
864 attr = next;
865 }
866
867 ctx->NewState = NEW_ALL;
868 }
869