added Window-isms previously in gl.h
[mesa.git] / src / mesa / main / context.c
1 /* $Id: context.c,v 1.65 2000/05/18 18:12:36 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 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 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "accum.h"
33 #include "alphabuf.h"
34 #include "clip.h"
35 #include "colortab.h"
36 #include "context.h"
37 #include "cva.h"
38 #include "depth.h"
39 #include "dlist.h"
40 #include "eval.h"
41 #include "enums.h"
42 #include "extensions.h"
43 #include "fog.h"
44 #include "get.h"
45 #include "glapi.h"
46 #include "glapinoop.h"
47 #include "glthread.h"
48 #include "hash.h"
49 #include "light.h"
50 #include "macros.h"
51 #include "matrix.h"
52 #include "mem.h"
53 #include "mmath.h"
54 #include "pb.h"
55 #include "pipeline.h"
56 #include "shade.h"
57 #include "simple_list.h"
58 #include "stencil.h"
59 #include "stages.h"
60 #include "state.h"
61 #include "translate.h"
62 #include "teximage.h"
63 #include "texobj.h"
64 #include "texstate.h"
65 #include "texture.h"
66 #include "types.h"
67 #include "varray.h"
68 #include "vb.h"
69 #include "vbcull.h"
70 #include "vbrender.h"
71 #include "vbxform.h"
72 #include "vertices.h"
73 #include "xform.h"
74 #endif
75
76
77
78 /**********************************************************************/
79 /***** Context and Thread management *****/
80 /**********************************************************************/
81
82
83 #if !defined(THREADS)
84
85 struct immediate *_mesa_CurrentInput = NULL;
86
87 #endif
88
89
90 /**********************************************************************/
91 /***** GL Visual allocation/destruction *****/
92 /**********************************************************************/
93
94
95 /*
96 * Allocate a new GLvisual object.
97 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
98 * dbFlag - double buffering?
99 * stereoFlag - stereo buffer?
100 * depthBits - requested bits per depth buffer value
101 * Any value in [0, 32] is acceptable but the actual
102 * depth type will be GLushort or GLuint as needed.
103 * stencilBits - requested minimum bits per stencil buffer value
104 * accumBits - requested minimum bits per accum buffer component
105 * indexBits - number of bits per pixel if rgbFlag==GL_FALSE
106 * red/green/blue/alphaBits - number of bits per color component
107 * in frame buffer for RGB(A) mode.
108 * We always use 8 in core Mesa though.
109 * Return: pointer to new GLvisual or NULL if requested parameters can't
110 * be met.
111 */
112 GLvisual *
113 _mesa_create_visual( GLboolean rgbFlag,
114 GLboolean dbFlag,
115 GLboolean stereoFlag,
116 GLint redBits,
117 GLint greenBits,
118 GLint blueBits,
119 GLint alphaBits,
120 GLint indexBits,
121 GLint depthBits,
122 GLint stencilBits,
123 GLint accumRedBits,
124 GLint accumGreenBits,
125 GLint accumBlueBits,
126 GLint accumAlphaBits,
127 GLint numSamples )
128 {
129 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
130 if (vis) {
131 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
132 redBits, greenBits, blueBits, alphaBits,
133 indexBits, depthBits, stencilBits,
134 accumRedBits, accumGreenBits,
135 accumBlueBits, accumAlphaBits,
136 numSamples )) {
137 FREE(vis);
138 return NULL;
139 }
140 }
141 return vis;
142 }
143
144
145 /*
146 * Initialize the fields of the given GLvisual.
147 * Input: see _mesa_create_visual() above.
148 * Return: GL_TRUE = success
149 * GL_FALSE = failure.
150 */
151 GLboolean
152 _mesa_initialize_visual( GLvisual *vis,
153 GLboolean rgbFlag,
154 GLboolean dbFlag,
155 GLboolean stereoFlag,
156 GLint redBits,
157 GLint greenBits,
158 GLint blueBits,
159 GLint alphaBits,
160 GLint indexBits,
161 GLint depthBits,
162 GLint stencilBits,
163 GLint accumRedBits,
164 GLint accumGreenBits,
165 GLint accumBlueBits,
166 GLint accumAlphaBits,
167 GLint numSamples )
168 {
169 assert(vis);
170
171 /* This is to catch bad values from device drivers not updated for
172 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
173 * bad value now (a 1-bit depth buffer!?!).
174 */
175 assert(depthBits == 0 || depthBits > 1);
176
177 if (depthBits < 0 || depthBits > 32) {
178 return GL_FALSE;
179 }
180 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
181 return GL_FALSE;
182 }
183 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
184 return GL_FALSE;
185 }
186 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
187 return GL_FALSE;
188 }
189 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
190 return GL_FALSE;
191 }
192 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
193 return GL_FALSE;
194 }
195
196 vis->RGBAflag = rgbFlag;
197 vis->DBflag = dbFlag;
198 vis->StereoFlag = stereoFlag;
199 vis->RedBits = redBits;
200 vis->GreenBits = greenBits;
201 vis->BlueBits = blueBits;
202 vis->AlphaBits = alphaBits;
203
204 vis->IndexBits = indexBits;
205 vis->DepthBits = depthBits;
206 vis->AccumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
207 vis->AccumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
208 vis->AccumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
209 vis->AccumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
210 vis->StencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
211
212 if (depthBits == 0) {
213 /* Special case. Even if we don't have a depth buffer we need
214 * good values for DepthMax for Z vertex transformation purposes.
215 */
216 vis->DepthMax = 1;
217 vis->DepthMaxF = 1.0F;
218 }
219 else if (depthBits < 32) {
220 vis->DepthMax = (1 << depthBits) - 1;
221 vis->DepthMaxF = (GLfloat) vis->DepthMax;
222 }
223 else {
224 /* Special case since shift values greater than or equal to the
225 * number of bits in the left hand expression's type are
226 * undefined.
227 */
228 vis->DepthMax = 0xffffffff;
229 vis->DepthMaxF = (GLfloat) vis->DepthMax;
230 }
231
232 return GL_TRUE;
233 }
234
235
236 /* This function should no longer be used. Use _mesa_create_visual() instead */
237 GLvisual *
238 gl_create_visual( GLboolean rgbFlag,
239 GLboolean alphaFlag,
240 GLboolean dbFlag,
241 GLboolean stereoFlag,
242 GLint depthBits,
243 GLint stencilBits,
244 GLint accumBits,
245 GLint indexBits,
246 GLint redBits,
247 GLint greenBits,
248 GLint blueBits,
249 GLint alphaBits )
250 {
251 (void) alphaFlag;
252 return _mesa_create_visual(rgbFlag, dbFlag, stereoFlag,
253 redBits, greenBits, blueBits, alphaBits,
254 indexBits, depthBits, stencilBits,
255 accumBits, accumBits, accumBits, accumBits, 0);
256 }
257
258
259 void
260 _mesa_destroy_visual( GLvisual *vis )
261 {
262 FREE(vis);
263 }
264
265
266 /* obsolete */
267 void
268 gl_destroy_visual( GLvisual *vis )
269 {
270 _mesa_destroy_visual(vis);
271 }
272
273
274
275 /**********************************************************************/
276 /***** GL Framebuffer allocation/destruction *****/
277 /**********************************************************************/
278
279
280 /*
281 * Create a new framebuffer. A GLframebuffer is a struct which
282 * encapsulates the depth, stencil and accum buffers and related
283 * parameters.
284 * Input: visual - a GLvisual pointer
285 * softwareDepth - create/use a software depth buffer?
286 * softwareStencil - create/use a software stencil buffer?
287 * softwareAccum - create/use a software accum buffer?
288 * softwareAlpha - create/use a software alpha buffer?
289
290 * Return: pointer to new GLframebuffer struct or NULL if error.
291 */
292 GLframebuffer *
293 gl_create_framebuffer( GLvisual *visual,
294 GLboolean softwareDepth,
295 GLboolean softwareStencil,
296 GLboolean softwareAccum,
297 GLboolean softwareAlpha )
298 {
299 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
300 assert(visual);
301 if (buffer) {
302 _mesa_initialize_framebuffer(buffer, visual,
303 softwareDepth, softwareStencil,
304 softwareAccum, softwareAlpha );
305 }
306 return buffer;
307 }
308
309
310 /*
311 * Initialize a GLframebuffer object.
312 * Input: See gl_create_framebuffer() above.
313 */
314 void
315 _mesa_initialize_framebuffer( GLframebuffer *buffer,
316 GLvisual *visual,
317 GLboolean softwareDepth,
318 GLboolean softwareStencil,
319 GLboolean softwareAccum,
320 GLboolean softwareAlpha )
321 {
322 assert(buffer);
323 assert(visual);
324
325 /* sanity checks */
326 if (softwareDepth ) {
327 assert(visual->DepthBits > 0);
328 }
329 if (softwareStencil) {
330 assert(visual->StencilBits > 0);
331 }
332 if (softwareAccum) {
333 assert(visual->RGBAflag);
334 assert(visual->AccumRedBits > 0);
335 assert(visual->AccumGreenBits > 0);
336 assert(visual->AccumBlueBits > 0);
337 }
338 if (softwareAlpha) {
339 assert(visual->RGBAflag);
340 assert(visual->AlphaBits > 0);
341 }
342
343 buffer->Visual = visual;
344 buffer->UseSoftwareDepthBuffer = softwareDepth;
345 buffer->UseSoftwareStencilBuffer = softwareStencil;
346 buffer->UseSoftwareAccumBuffer = softwareAccum;
347 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
348 }
349
350
351 /*
352 * Free a framebuffer struct and its buffers.
353 */
354 void
355 gl_destroy_framebuffer( GLframebuffer *buffer )
356 {
357 if (buffer) {
358 if (buffer->DepthBuffer) {
359 FREE( buffer->DepthBuffer );
360 }
361 if (buffer->Accum) {
362 FREE( buffer->Accum );
363 }
364 if (buffer->Stencil) {
365 FREE( buffer->Stencil );
366 }
367 if (buffer->FrontLeftAlpha) {
368 FREE( buffer->FrontLeftAlpha );
369 }
370 if (buffer->BackLeftAlpha) {
371 FREE( buffer->BackLeftAlpha );
372 }
373 if (buffer->FrontRightAlpha) {
374 FREE( buffer->FrontRightAlpha );
375 }
376 if (buffer->BackRightAlpha) {
377 FREE( buffer->BackRightAlpha );
378 }
379 FREE(buffer);
380 }
381 }
382
383
384
385 /**********************************************************************/
386 /***** Context allocation, initialization, destroying *****/
387 /**********************************************************************/
388
389
390 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
391
392
393 /*
394 * This function just calls all the various one-time-init functions in Mesa.
395 */
396 static void
397 one_time_init( void )
398 {
399 static GLboolean alreadyCalled = GL_FALSE;
400 _glthread_LOCK_MUTEX(OneTimeLock);
401 if (!alreadyCalled) {
402 /* do some implementation tests */
403 assert( sizeof(GLbyte) == 1 );
404 assert( sizeof(GLshort) >= 2 );
405 assert( sizeof(GLint) >= 4 );
406 assert( sizeof(GLubyte) == 1 );
407 assert( sizeof(GLushort) >= 2 );
408 assert( sizeof(GLuint) >= 4 );
409
410 gl_init_clip();
411 gl_init_eval();
412 _mesa_init_fog();
413 _mesa_init_math();
414 gl_init_lists();
415 gl_init_shade();
416 gl_init_texture();
417 gl_init_transformation();
418 gl_init_translate();
419 gl_init_vbrender();
420 gl_init_vbxform();
421 gl_init_vertices();
422
423 if (getenv("MESA_DEBUG")) {
424 _glapi_noop_enable_warnings(GL_TRUE);
425 }
426 else {
427 _glapi_noop_enable_warnings(GL_FALSE);
428 }
429
430 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
431 fprintf(stderr, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
432 #endif
433
434 alreadyCalled = GL_TRUE;
435 }
436 _glthread_UNLOCK_MUTEX(OneTimeLock);
437 }
438
439
440
441 /*
442 * Allocate and initialize a shared context state structure.
443 */
444 static struct gl_shared_state *
445 alloc_shared_state( void )
446 {
447 GLuint d;
448 struct gl_shared_state *ss;
449 GLboolean outOfMemory;
450
451 ss = CALLOC_STRUCT(gl_shared_state);
452 if (!ss)
453 return NULL;
454
455 ss->DisplayList = _mesa_NewHashTable();
456
457 ss->TexObjects = _mesa_NewHashTable();
458
459 /* Default Texture objects */
460 outOfMemory = GL_FALSE;
461 for (d = 1 ; d <= 3 ; d++) {
462 ss->DefaultD[d] = gl_alloc_texture_object(ss, 0, d);
463 if (!ss->DefaultD[d]) {
464 outOfMemory = GL_TRUE;
465 break;
466 }
467 ss->DefaultD[d]->RefCount++; /* don't free if not in use */
468 }
469
470 if (!ss->DisplayList || !ss->TexObjects || outOfMemory) {
471 /* Ran out of memory at some point. Free everything and return NULL */
472 if (ss->DisplayList)
473 _mesa_DeleteHashTable(ss->DisplayList);
474 if (ss->TexObjects)
475 _mesa_DeleteHashTable(ss->TexObjects);
476 if (ss->DefaultD[1])
477 gl_free_texture_object(ss, ss->DefaultD[1]);
478 if (ss->DefaultD[2])
479 gl_free_texture_object(ss, ss->DefaultD[2]);
480 if (ss->DefaultD[3])
481 gl_free_texture_object(ss, ss->DefaultD[3]);
482 FREE(ss);
483 return NULL;
484 }
485 else {
486 return ss;
487 }
488 }
489
490
491 /*
492 * Deallocate a shared state context and all children structures.
493 */
494 static void
495 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
496 {
497 /* Free display lists */
498 while (1) {
499 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
500 if (list) {
501 gl_destroy_list(ctx, list);
502 }
503 else {
504 break;
505 }
506 }
507 _mesa_DeleteHashTable(ss->DisplayList);
508
509 /* Free texture objects */
510 while (ss->TexObjectList)
511 {
512 if (ctx->Driver.DeleteTexture)
513 (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
514 /* this function removes from linked list too! */
515 gl_free_texture_object(ss, ss->TexObjectList);
516 }
517 _mesa_DeleteHashTable(ss->TexObjects);
518
519 FREE(ss);
520 }
521
522
523
524 /*
525 * Initialize the nth light. Note that the defaults for light 0 are
526 * different than the other lights.
527 */
528 static void
529 init_light( struct gl_light *l, GLuint n )
530 {
531 make_empty_list( l );
532
533 ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
534 if (n==0) {
535 ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
536 ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
537 }
538 else {
539 ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
540 ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
541 }
542 ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
543 ASSIGN_3V( l->EyeDirection, 0.0, 0.0, -1.0 );
544 l->SpotExponent = 0.0;
545 gl_compute_spot_exp_table( l );
546 l->SpotCutoff = 180.0;
547 l->CosCutoff = 0.0; /* KW: -ve values not admitted */
548 l->ConstantAttenuation = 1.0;
549 l->LinearAttenuation = 0.0;
550 l->QuadraticAttenuation = 0.0;
551 l->Enabled = GL_FALSE;
552 }
553
554
555
556 static void
557 init_lightmodel( struct gl_lightmodel *lm )
558 {
559 ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
560 lm->LocalViewer = GL_FALSE;
561 lm->TwoSide = GL_FALSE;
562 lm->ColorControl = GL_SINGLE_COLOR;
563 }
564
565
566 static void
567 init_material( struct gl_material *m )
568 {
569 ASSIGN_4V( m->Ambient, 0.2, 0.2, 0.2, 1.0 );
570 ASSIGN_4V( m->Diffuse, 0.8, 0.8, 0.8, 1.0 );
571 ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
572 ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
573 m->Shininess = 0.0;
574 m->AmbientIndex = 0;
575 m->DiffuseIndex = 1;
576 m->SpecularIndex = 1;
577 }
578
579
580
581 static void
582 init_texture_unit( GLcontext *ctx, GLuint unit )
583 {
584 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
585
586 texUnit->EnvMode = GL_MODULATE;
587 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
588 texUnit->TexGenEnabled = 0;
589 texUnit->GenModeS = GL_EYE_LINEAR;
590 texUnit->GenModeT = GL_EYE_LINEAR;
591 texUnit->GenModeR = GL_EYE_LINEAR;
592 texUnit->GenModeQ = GL_EYE_LINEAR;
593 /* Yes, these plane coefficients are correct! */
594 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
595 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
596 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
597 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
598 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
599 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
600 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
601 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
602
603 texUnit->CurrentD[1] = ctx->Shared->DefaultD[1];
604 texUnit->CurrentD[2] = ctx->Shared->DefaultD[2];
605 texUnit->CurrentD[3] = ctx->Shared->DefaultD[3];
606 }
607
608
609 static void
610 init_fallback_arrays( GLcontext *ctx )
611 {
612 struct gl_client_array *cl;
613 GLuint i;
614
615 cl = &ctx->Fallback.Normal;
616 cl->Size = 3;
617 cl->Type = GL_FLOAT;
618 cl->Stride = 0;
619 cl->StrideB = 0;
620 cl->Ptr = (void *) ctx->Current.Normal;
621 cl->Enabled = 1;
622
623 cl = &ctx->Fallback.Color;
624 cl->Size = 4;
625 cl->Type = GL_UNSIGNED_BYTE;
626 cl->Stride = 0;
627 cl->StrideB = 0;
628 cl->Ptr = (void *) ctx->Current.ByteColor;
629 cl->Enabled = 1;
630
631 cl = &ctx->Fallback.Index;
632 cl->Size = 1;
633 cl->Type = GL_UNSIGNED_INT;
634 cl->Stride = 0;
635 cl->StrideB = 0;
636 cl->Ptr = (void *) &ctx->Current.Index;
637 cl->Enabled = 1;
638
639 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
640 cl = &ctx->Fallback.TexCoord[i];
641 cl->Size = 4;
642 cl->Type = GL_FLOAT;
643 cl->Stride = 0;
644 cl->StrideB = 0;
645 cl->Ptr = (void *) ctx->Current.Texcoord[i];
646 cl->Enabled = 1;
647 }
648
649 cl = &ctx->Fallback.EdgeFlag;
650 cl->Size = 1;
651 cl->Type = GL_UNSIGNED_BYTE;
652 cl->Stride = 0;
653 cl->StrideB = 0;
654 cl->Ptr = (void *) &ctx->Current.EdgeFlag;
655 cl->Enabled = 1;
656 }
657
658
659 /* Initialize a 1-D evaluator map */
660 static void
661 init_1d_map( struct gl_1d_map *map, int n, const float *initial )
662 {
663 map->Order = 1;
664 map->u1 = 0.0;
665 map->u2 = 1.0;
666 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
667 if (map->Points) {
668 GLint i;
669 for (i=0;i<n;i++)
670 map->Points[i] = initial[i];
671 }
672 }
673
674
675 /* Initialize a 2-D evaluator map */
676 static void
677 init_2d_map( struct gl_2d_map *map, int n, const float *initial )
678 {
679 map->Uorder = 1;
680 map->Vorder = 1;
681 map->u1 = 0.0;
682 map->u2 = 1.0;
683 map->v1 = 0.0;
684 map->v2 = 1.0;
685 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
686 if (map->Points) {
687 GLint i;
688 for (i=0;i<n;i++)
689 map->Points[i] = initial[i];
690 }
691 }
692
693
694 /*
695 * Initialize the attribute groups in a GLcontext.
696 */
697 static void
698 init_attrib_groups( GLcontext *ctx )
699 {
700 GLuint i, j;
701
702 assert(ctx);
703
704 /* Constants, may be overriden by device drivers */
705 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
706 ctx->Const.MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
707 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
708 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
709 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
710 ctx->Const.MinPointSize = MIN_POINT_SIZE;
711 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
712 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
713 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
714 ctx->Const.PointSizeGranularity = POINT_SIZE_GRANULARITY;
715 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
716 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
717 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
718 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
719 ctx->Const.LineWidthGranularity = LINE_WIDTH_GRANULARITY;
720 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
721 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
722 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
723 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
724
725 /* Modelview matrix */
726 gl_matrix_ctr( &ctx->ModelView );
727 gl_matrix_alloc_inv( &ctx->ModelView );
728
729 ctx->ModelViewStackDepth = 0;
730 for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
731 gl_matrix_ctr( &ctx->ModelViewStack[i] );
732 gl_matrix_alloc_inv( &ctx->ModelViewStack[i] );
733 }
734
735 /* Projection matrix - need inv for user clipping in clip space*/
736 gl_matrix_ctr( &ctx->ProjectionMatrix );
737 gl_matrix_alloc_inv( &ctx->ProjectionMatrix );
738
739 gl_matrix_ctr( &ctx->ModelProjectMatrix );
740 gl_matrix_ctr( &ctx->ModelProjectWinMatrix );
741 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
742
743 ctx->ProjectionStackDepth = 0;
744 ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
745 ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
746
747 for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
748 gl_matrix_ctr( &ctx->ProjectionStack[i] );
749 gl_matrix_alloc_inv( &ctx->ProjectionStack[i] );
750 }
751
752 /* Texture matrix */
753 for (i=0; i<MAX_TEXTURE_UNITS; i++) {
754 gl_matrix_ctr( &ctx->TextureMatrix[i] );
755 ctx->TextureStackDepth[i] = 0;
756 for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
757 ctx->TextureStack[i][j].inv = 0;
758 }
759 }
760
761 /* Color matrix */
762 gl_matrix_ctr(&ctx->ColorMatrix);
763 ctx->ColorStackDepth = 0;
764 for (j = 0; j < MAX_COLOR_STACK_DEPTH - 1; j++) {
765 gl_matrix_ctr(&ctx->ColorStack[j]);
766 }
767
768 /* Accumulate buffer group */
769 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
770
771 /* Color buffer group */
772 ctx->Color.IndexMask = 0xffffffff;
773 ctx->Color.ColorMask[0] = 0xff;
774 ctx->Color.ColorMask[1] = 0xff;
775 ctx->Color.ColorMask[2] = 0xff;
776 ctx->Color.ColorMask[3] = 0xff;
777 ctx->Color.SWmasking = GL_FALSE;
778 ctx->Color.ClearIndex = 0;
779 ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
780 ctx->Color.DrawBuffer = GL_FRONT;
781 ctx->Color.AlphaEnabled = GL_FALSE;
782 ctx->Color.AlphaFunc = GL_ALWAYS;
783 ctx->Color.AlphaRef = 0;
784 ctx->Color.BlendEnabled = GL_FALSE;
785 ctx->Color.BlendSrcRGB = GL_ONE;
786 ctx->Color.BlendDstRGB = GL_ZERO;
787 ctx->Color.BlendSrcA = GL_ONE;
788 ctx->Color.BlendDstA = GL_ZERO;
789 ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
790 ctx->Color.BlendFunc = NULL; /* this pointer set only when needed */
791 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
792 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
793 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
794 ctx->Color.SWLogicOpEnabled = GL_FALSE;
795 ctx->Color.LogicOp = GL_COPY;
796 ctx->Color.DitherFlag = GL_TRUE;
797 ctx->Color.MultiDrawBuffer = GL_FALSE;
798
799 /* Current group */
800 ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255);
801 ctx->Current.Index = 1;
802 for (i=0; i<MAX_TEXTURE_UNITS; i++)
803 ASSIGN_4V( ctx->Current.Texcoord[i], 0.0, 0.0, 0.0, 1.0 );
804 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
805 ctx->Current.RasterDistance = 0.0;
806 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
807 ctx->Current.RasterIndex = 1;
808 for (i=0; i<MAX_TEXTURE_UNITS; i++)
809 ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
810 ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
811 ctx->Current.RasterPosValid = GL_TRUE;
812 ctx->Current.EdgeFlag = GL_TRUE;
813 ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
814 ctx->Current.Primitive = (GLenum) (GL_POLYGON + 1);
815
816 ctx->Current.Flag = (VERT_NORM|VERT_INDEX|VERT_RGBA|VERT_EDGE|
817 VERT_TEX0_1|VERT_TEX1_1|VERT_MATERIAL);
818
819 init_fallback_arrays( ctx );
820
821 /* Depth buffer group */
822 ctx->Depth.Test = GL_FALSE;
823 ctx->Depth.Clear = 1.0;
824 ctx->Depth.Func = GL_LESS;
825 ctx->Depth.Mask = GL_TRUE;
826 ctx->Depth.OcclusionTest = GL_FALSE;
827
828 /* Evaluators group */
829 ctx->Eval.Map1Color4 = GL_FALSE;
830 ctx->Eval.Map1Index = GL_FALSE;
831 ctx->Eval.Map1Normal = GL_FALSE;
832 ctx->Eval.Map1TextureCoord1 = GL_FALSE;
833 ctx->Eval.Map1TextureCoord2 = GL_FALSE;
834 ctx->Eval.Map1TextureCoord3 = GL_FALSE;
835 ctx->Eval.Map1TextureCoord4 = GL_FALSE;
836 ctx->Eval.Map1Vertex3 = GL_FALSE;
837 ctx->Eval.Map1Vertex4 = GL_FALSE;
838 ctx->Eval.Map2Color4 = GL_FALSE;
839 ctx->Eval.Map2Index = GL_FALSE;
840 ctx->Eval.Map2Normal = GL_FALSE;
841 ctx->Eval.Map2TextureCoord1 = GL_FALSE;
842 ctx->Eval.Map2TextureCoord2 = GL_FALSE;
843 ctx->Eval.Map2TextureCoord3 = GL_FALSE;
844 ctx->Eval.Map2TextureCoord4 = GL_FALSE;
845 ctx->Eval.Map2Vertex3 = GL_FALSE;
846 ctx->Eval.Map2Vertex4 = GL_FALSE;
847 ctx->Eval.AutoNormal = GL_FALSE;
848 ctx->Eval.MapGrid1un = 1;
849 ctx->Eval.MapGrid1u1 = 0.0;
850 ctx->Eval.MapGrid1u2 = 1.0;
851 ctx->Eval.MapGrid2un = 1;
852 ctx->Eval.MapGrid2vn = 1;
853 ctx->Eval.MapGrid2u1 = 0.0;
854 ctx->Eval.MapGrid2u2 = 1.0;
855 ctx->Eval.MapGrid2v1 = 0.0;
856 ctx->Eval.MapGrid2v2 = 1.0;
857
858 /* Evaluator data */
859 {
860 static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
861 static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
862 static GLfloat index[1] = { 1.0 };
863 static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
864 static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
865
866 init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
867 init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
868 init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
869 init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
870 init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
871 init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
872 init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
873 init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
874 init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
875
876 init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
877 init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
878 init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
879 init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
880 init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
881 init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
882 init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
883 init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
884 init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
885 }
886
887 /* Fog group */
888 ctx->Fog.Enabled = GL_FALSE;
889 ctx->Fog.Mode = GL_EXP;
890 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
891 ctx->Fog.Index = 0.0;
892 ctx->Fog.Density = 1.0;
893 ctx->Fog.Start = 0.0;
894 ctx->Fog.End = 1.0;
895
896 /* Hint group */
897 ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
898 ctx->Hint.PointSmooth = GL_DONT_CARE;
899 ctx->Hint.LineSmooth = GL_DONT_CARE;
900 ctx->Hint.PolygonSmooth = GL_DONT_CARE;
901 ctx->Hint.Fog = GL_DONT_CARE;
902
903 ctx->Hint.AllowDrawWin = GL_TRUE;
904 ctx->Hint.AllowDrawFrg = GL_TRUE;
905 ctx->Hint.AllowDrawMem = GL_TRUE;
906 ctx->Hint.StrictLighting = GL_TRUE;
907
908 /* Histogram group */
909 ctx->Histogram.Width = 0;
910 ctx->Histogram.Format = GL_RGBA;
911 ctx->Histogram.Sink = GL_FALSE;
912 ctx->Histogram.RedSize = 0xffffffff;
913 ctx->Histogram.GreenSize = 0xffffffff;
914 ctx->Histogram.BlueSize = 0xffffffff;
915 ctx->Histogram.AlphaSize = 0xffffffff;
916 ctx->Histogram.LuminanceSize = 0xffffffff;
917 for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
918 ctx->Histogram.Count[i][0] = 0;
919 ctx->Histogram.Count[i][1] = 0;
920 ctx->Histogram.Count[i][2] = 0;
921 ctx->Histogram.Count[i][3] = 0;
922 }
923
924 /* Min/Max group */
925 ctx->MinMax.Format = GL_RGBA;
926 ctx->MinMax.Sink = GL_FALSE;
927 ctx->MinMax.Min[RCOMP] = 1000; ctx->MinMax.Max[RCOMP] = -1000;
928 ctx->MinMax.Min[GCOMP] = 1000; ctx->MinMax.Max[GCOMP] = -1000;
929 ctx->MinMax.Min[BCOMP] = 1000; ctx->MinMax.Max[BCOMP] = -1000;
930 ctx->MinMax.Min[ACOMP] = 1000; ctx->MinMax.Max[ACOMP] = -1000;
931
932
933
934 /* Pipeline */
935 gl_pipeline_init( ctx );
936 gl_cva_init( ctx );
937
938 /* Extensions */
939 gl_extensions_ctr( ctx );
940
941 ctx->AllowVertexCull = CLIP_CULLED_BIT;
942
943 /* Lighting group */
944 for (i=0;i<MAX_LIGHTS;i++) {
945 init_light( &ctx->Light.Light[i], i );
946 }
947 make_empty_list( &ctx->Light.EnabledList );
948
949 init_lightmodel( &ctx->Light.Model );
950 init_material( &ctx->Light.Material[0] );
951 init_material( &ctx->Light.Material[1] );
952 ctx->Light.ShadeModel = GL_SMOOTH;
953 ctx->Light.Enabled = GL_FALSE;
954 ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
955 ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
956 ctx->Light.ColorMaterialBitmask
957 = gl_material_bitmask( ctx,
958 GL_FRONT_AND_BACK,
959 GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
960
961 ctx->Light.ColorMaterialEnabled = GL_FALSE;
962
963 /* Lighting miscellaneous */
964 ctx->ShineTabList = MALLOC_STRUCT( gl_shine_tab );
965 make_empty_list( ctx->ShineTabList );
966 for (i = 0 ; i < 10 ; i++) {
967 struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
968 s->shininess = -1;
969 s->refcount = 0;
970 insert_at_tail( ctx->ShineTabList, s );
971 }
972 for (i = 0 ; i < 4 ; i++) {
973 ctx->ShineTable[i] = ctx->ShineTabList->prev;
974 ctx->ShineTable[i]->refcount++;
975 }
976
977
978 /* Line group */
979 ctx->Line.SmoothFlag = GL_FALSE;
980 ctx->Line.StippleFlag = GL_FALSE;
981 ctx->Line.Width = 1.0;
982 ctx->Line.StipplePattern = 0xffff;
983 ctx->Line.StippleFactor = 1;
984
985 /* Display List group */
986 ctx->List.ListBase = 0;
987
988 /* Pixel group */
989 ctx->Pixel.RedBias = 0.0;
990 ctx->Pixel.RedScale = 1.0;
991 ctx->Pixel.GreenBias = 0.0;
992 ctx->Pixel.GreenScale = 1.0;
993 ctx->Pixel.BlueBias = 0.0;
994 ctx->Pixel.BlueScale = 1.0;
995 ctx->Pixel.AlphaBias = 0.0;
996 ctx->Pixel.AlphaScale = 1.0;
997 ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
998 ctx->Pixel.DepthBias = 0.0;
999 ctx->Pixel.DepthScale = 1.0;
1000 ctx->Pixel.IndexOffset = 0;
1001 ctx->Pixel.IndexShift = 0;
1002 ctx->Pixel.ZoomX = 1.0;
1003 ctx->Pixel.ZoomY = 1.0;
1004 ctx->Pixel.MapColorFlag = GL_FALSE;
1005 ctx->Pixel.MapStencilFlag = GL_FALSE;
1006 ctx->Pixel.MapStoSsize = 1;
1007 ctx->Pixel.MapItoIsize = 1;
1008 ctx->Pixel.MapItoRsize = 1;
1009 ctx->Pixel.MapItoGsize = 1;
1010 ctx->Pixel.MapItoBsize = 1;
1011 ctx->Pixel.MapItoAsize = 1;
1012 ctx->Pixel.MapRtoRsize = 1;
1013 ctx->Pixel.MapGtoGsize = 1;
1014 ctx->Pixel.MapBtoBsize = 1;
1015 ctx->Pixel.MapAtoAsize = 1;
1016 ctx->Pixel.MapStoS[0] = 0;
1017 ctx->Pixel.MapItoI[0] = 0;
1018 ctx->Pixel.MapItoR[0] = 0.0;
1019 ctx->Pixel.MapItoG[0] = 0.0;
1020 ctx->Pixel.MapItoB[0] = 0.0;
1021 ctx->Pixel.MapItoA[0] = 0.0;
1022 ctx->Pixel.MapItoR8[0] = 0;
1023 ctx->Pixel.MapItoG8[0] = 0;
1024 ctx->Pixel.MapItoB8[0] = 0;
1025 ctx->Pixel.MapItoA8[0] = 0;
1026 ctx->Pixel.MapRtoR[0] = 0.0;
1027 ctx->Pixel.MapGtoG[0] = 0.0;
1028 ctx->Pixel.MapBtoB[0] = 0.0;
1029 ctx->Pixel.MapAtoA[0] = 0.0;
1030 ctx->Pixel.HistogramEnabled = GL_FALSE;
1031 ctx->Pixel.MinMaxEnabled = GL_FALSE;
1032 ctx->Pixel.PixelTextureEnabled = GL_FALSE;
1033 ctx->Pixel.FragmentRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
1034 ctx->Pixel.FragmentAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
1035 ASSIGN_4V(ctx->Pixel.PostColorMatrixScale, 1.0, 1.0, 1.0, 1.0);
1036 ASSIGN_4V(ctx->Pixel.PostColorMatrixBias, 0.0, 0.0, 0.0, 0.0);
1037 ASSIGN_4V(ctx->Pixel.ColorTableScale, 1.0, 1.0, 1.0, 1.0);
1038 ASSIGN_4V(ctx->Pixel.ColorTableBias, 0.0, 0.0, 0.0, 0.0);
1039 ctx->Pixel.ColorTableEnabled = GL_FALSE;
1040 ctx->Pixel.PostConvolutionColorTableEnabled = GL_FALSE;
1041 ctx->Pixel.PostColorMatrixColorTableEnabled = GL_FALSE;
1042 ctx->Pixel.Convolution1DEnabled = GL_FALSE;
1043 ctx->Pixel.Convolution2DEnabled = GL_FALSE;
1044 ctx->Pixel.Separable2DEnabled = GL_FALSE;
1045 for (i = 0; i < 3; i++) {
1046 ASSIGN_4V(ctx->Pixel.ConvolutionBorderColor[i], 0.0, 0.0, 0.0, 0.0);
1047 ctx->Pixel.ConvolutionBorderMode[i] = GL_REDUCE;
1048 ASSIGN_4V(ctx->Pixel.ConvolutionFilterScale[i], 1.0, 1.0, 1.0, 1.0);
1049 ASSIGN_4V(ctx->Pixel.ConvolutionFilterBias[i], 0.0, 0.0, 0.0, 0.0);
1050 }
1051 ASSIGN_4V(ctx->Pixel.PostConvolutionScale, 1.0, 1.0, 1.0, 1.0);
1052 ASSIGN_4V(ctx->Pixel.PostConvolutionBias, 0.0, 0.0, 0.0, 0.0);
1053
1054 /* Point group */
1055 ctx->Point.SmoothFlag = GL_FALSE;
1056 ctx->Point.Size = 1.0;
1057 ctx->Point.Params[0] = 1.0;
1058 ctx->Point.Params[1] = 0.0;
1059 ctx->Point.Params[2] = 0.0;
1060 ctx->Point.Attenuated = GL_FALSE;
1061 ctx->Point.MinSize = 0.0;
1062 ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
1063 ctx->Point.Threshold = 1.0;
1064
1065 /* Polygon group */
1066 ctx->Polygon.CullFlag = GL_FALSE;
1067 ctx->Polygon.CullFaceMode = GL_BACK;
1068 ctx->Polygon.FrontFace = GL_CCW;
1069 ctx->Polygon.FrontBit = 0;
1070 ctx->Polygon.FrontMode = GL_FILL;
1071 ctx->Polygon.BackMode = GL_FILL;
1072 ctx->Polygon.Unfilled = GL_FALSE;
1073 ctx->Polygon.SmoothFlag = GL_FALSE;
1074 ctx->Polygon.StippleFlag = GL_FALSE;
1075 ctx->Polygon.OffsetFactor = 0.0F;
1076 ctx->Polygon.OffsetUnits = 0.0F;
1077 ctx->Polygon.OffsetPoint = GL_FALSE;
1078 ctx->Polygon.OffsetLine = GL_FALSE;
1079 ctx->Polygon.OffsetFill = GL_FALSE;
1080
1081 /* Polygon Stipple group */
1082 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
1083
1084 /* Scissor group */
1085 ctx->Scissor.Enabled = GL_FALSE;
1086 ctx->Scissor.X = 0;
1087 ctx->Scissor.Y = 0;
1088 ctx->Scissor.Width = 0;
1089 ctx->Scissor.Height = 0;
1090
1091 /* Stencil group */
1092 ctx->Stencil.Enabled = GL_FALSE;
1093 ctx->Stencil.Function = GL_ALWAYS;
1094 ctx->Stencil.FailFunc = GL_KEEP;
1095 ctx->Stencil.ZPassFunc = GL_KEEP;
1096 ctx->Stencil.ZFailFunc = GL_KEEP;
1097 ctx->Stencil.Ref = 0;
1098 ctx->Stencil.ValueMask = STENCIL_MAX;
1099 ctx->Stencil.Clear = 0;
1100 ctx->Stencil.WriteMask = STENCIL_MAX;
1101
1102 /* Texture group */
1103 ctx->Texture.CurrentUnit = 0; /* multitexture */
1104 ctx->Texture.CurrentTransformUnit = 0; /* multitexture */
1105 ctx->Texture.Enabled = 0;
1106 for (i=0; i<MAX_TEXTURE_UNITS; i++)
1107 init_texture_unit( ctx, i );
1108 _mesa_init_colortable(&ctx->Texture.Palette);
1109
1110 /* Transformation group */
1111 ctx->Transform.MatrixMode = GL_MODELVIEW;
1112 ctx->Transform.Normalize = GL_FALSE;
1113 ctx->Transform.RescaleNormals = GL_FALSE;
1114 for (i=0;i<MAX_CLIP_PLANES;i++) {
1115 ctx->Transform.ClipEnabled[i] = GL_FALSE;
1116 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1117 }
1118 ctx->Transform.AnyClip = GL_FALSE;
1119
1120 /* Viewport group */
1121 ctx->Viewport.X = 0;
1122 ctx->Viewport.Y = 0;
1123 ctx->Viewport.Width = 0;
1124 ctx->Viewport.Height = 0;
1125 ctx->Viewport.Near = 0.0;
1126 ctx->Viewport.Far = 1.0;
1127 gl_matrix_ctr(&ctx->Viewport.WindowMap);
1128
1129 #define Sz 10
1130 #define Tz 14
1131 ctx->Viewport.WindowMap.m[Sz] = 0.5 * ctx->Visual->DepthMaxF;
1132 ctx->Viewport.WindowMap.m[Tz] = 0.5 * ctx->Visual->DepthMaxF;
1133 #undef Sz
1134 #undef Tz
1135
1136 ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1137 ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
1138
1139 /* Vertex arrays */
1140 ctx->Array.Vertex.Size = 4;
1141 ctx->Array.Vertex.Type = GL_FLOAT;
1142 ctx->Array.Vertex.Stride = 0;
1143 ctx->Array.Vertex.StrideB = 0;
1144 ctx->Array.Vertex.Ptr = NULL;
1145 ctx->Array.Vertex.Enabled = GL_FALSE;
1146 ctx->Array.Normal.Type = GL_FLOAT;
1147 ctx->Array.Normal.Stride = 0;
1148 ctx->Array.Normal.StrideB = 0;
1149 ctx->Array.Normal.Ptr = NULL;
1150 ctx->Array.Normal.Enabled = GL_FALSE;
1151 ctx->Array.Color.Size = 4;
1152 ctx->Array.Color.Type = GL_FLOAT;
1153 ctx->Array.Color.Stride = 0;
1154 ctx->Array.Color.StrideB = 0;
1155 ctx->Array.Color.Ptr = NULL;
1156 ctx->Array.Color.Enabled = GL_FALSE;
1157 ctx->Array.Index.Type = GL_FLOAT;
1158 ctx->Array.Index.Stride = 0;
1159 ctx->Array.Index.StrideB = 0;
1160 ctx->Array.Index.Ptr = NULL;
1161 ctx->Array.Index.Enabled = GL_FALSE;
1162 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1163 ctx->Array.TexCoord[i].Size = 4;
1164 ctx->Array.TexCoord[i].Type = GL_FLOAT;
1165 ctx->Array.TexCoord[i].Stride = 0;
1166 ctx->Array.TexCoord[i].StrideB = 0;
1167 ctx->Array.TexCoord[i].Ptr = NULL;
1168 ctx->Array.TexCoord[i].Enabled = GL_FALSE;
1169 }
1170 ctx->Array.TexCoordInterleaveFactor = 1;
1171 ctx->Array.EdgeFlag.Stride = 0;
1172 ctx->Array.EdgeFlag.StrideB = 0;
1173 ctx->Array.EdgeFlag.Ptr = NULL;
1174 ctx->Array.EdgeFlag.Enabled = GL_FALSE;
1175 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
1176
1177 /* Pixel transfer */
1178 ctx->Pack.Alignment = 4;
1179 ctx->Pack.RowLength = 0;
1180 ctx->Pack.ImageHeight = 0;
1181 ctx->Pack.SkipPixels = 0;
1182 ctx->Pack.SkipRows = 0;
1183 ctx->Pack.SkipImages = 0;
1184 ctx->Pack.SwapBytes = GL_FALSE;
1185 ctx->Pack.LsbFirst = GL_FALSE;
1186 ctx->Unpack.Alignment = 4;
1187 ctx->Unpack.RowLength = 0;
1188 ctx->Unpack.ImageHeight = 0;
1189 ctx->Unpack.SkipPixels = 0;
1190 ctx->Unpack.SkipRows = 0;
1191 ctx->Unpack.SkipImages = 0;
1192 ctx->Unpack.SwapBytes = GL_FALSE;
1193 ctx->Unpack.LsbFirst = GL_FALSE;
1194
1195 /* Feedback */
1196 ctx->Feedback.Type = GL_2D; /* TODO: verify */
1197 ctx->Feedback.Buffer = NULL;
1198 ctx->Feedback.BufferSize = 0;
1199 ctx->Feedback.Count = 0;
1200
1201 /* Selection/picking */
1202 ctx->Select.Buffer = NULL;
1203 ctx->Select.BufferSize = 0;
1204 ctx->Select.BufferCount = 0;
1205 ctx->Select.Hits = 0;
1206 ctx->Select.NameStackDepth = 0;
1207
1208 /* Optimized Accum buffer */
1209 ctx->IntegerAccumMode = GL_TRUE;
1210 ctx->IntegerAccumScaler = 0.0;
1211
1212 /* Renderer and client attribute stacks */
1213 ctx->AttribStackDepth = 0;
1214 ctx->ClientAttribStackDepth = 0;
1215
1216 /* Display list */
1217 ctx->CallDepth = 0;
1218 ctx->ExecuteFlag = GL_TRUE;
1219 ctx->CompileFlag = GL_FALSE;
1220 ctx->CurrentListPtr = NULL;
1221 ctx->CurrentBlock = NULL;
1222 ctx->CurrentListNum = 0;
1223 ctx->CurrentPos = 0;
1224
1225 /* Color tables */
1226 _mesa_init_colortable(&ctx->ColorTable);
1227 _mesa_init_colortable(&ctx->ProxyColorTable);
1228 _mesa_init_colortable(&ctx->PostConvolutionColorTable);
1229 _mesa_init_colortable(&ctx->ProxyPostConvolutionColorTable);
1230 _mesa_init_colortable(&ctx->PostColorMatrixColorTable);
1231 _mesa_init_colortable(&ctx->ProxyPostColorMatrixColorTable);
1232
1233 /* Miscellaneous */
1234 ctx->NewState = NEW_ALL;
1235 ctx->RenderMode = GL_RENDER;
1236 ctx->StippleCounter = 0;
1237 ctx->NeedNormals = GL_FALSE;
1238 ctx->DoViewportMapping = GL_TRUE;
1239
1240 ctx->NeedEyeCoords = GL_FALSE;
1241 ctx->NeedEyeNormals = GL_FALSE;
1242 ctx->vb_proj_matrix = &ctx->ModelProjectMatrix;
1243
1244 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1245
1246 ctx->CatchSignals = GL_TRUE;
1247 ctx->OcclusionResult = GL_FALSE;
1248 ctx->OcclusionResultSaved = GL_FALSE;
1249
1250 /* For debug/development only */
1251 ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
1252 ctx->FirstTimeCurrent = GL_TRUE;
1253
1254 /* Dither disable */
1255 ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
1256 if (ctx->NoDither) {
1257 if (getenv("MESA_DEBUG")) {
1258 fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
1259 }
1260 ctx->Color.DitherFlag = GL_FALSE;
1261 }
1262 }
1263
1264
1265
1266
1267 /*
1268 * Allocate the proxy textures. If we run out of memory part way through
1269 * the allocations clean up and return GL_FALSE.
1270 * Return: GL_TRUE=success, GL_FALSE=failure
1271 */
1272 static GLboolean
1273 alloc_proxy_textures( GLcontext *ctx )
1274 {
1275 GLboolean out_of_memory;
1276 GLint i;
1277
1278 ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
1279 if (!ctx->Texture.Proxy1D) {
1280 return GL_FALSE;
1281 }
1282
1283 ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
1284 if (!ctx->Texture.Proxy2D) {
1285 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1286 return GL_FALSE;
1287 }
1288
1289 ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
1290 if (!ctx->Texture.Proxy3D) {
1291 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1292 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1293 return GL_FALSE;
1294 }
1295
1296 out_of_memory = GL_FALSE;
1297 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1298 ctx->Texture.Proxy1D->Image[i] = _mesa_alloc_texture_image();
1299 ctx->Texture.Proxy2D->Image[i] = _mesa_alloc_texture_image();
1300 ctx->Texture.Proxy3D->Image[i] = _mesa_alloc_texture_image();
1301 if (!ctx->Texture.Proxy1D->Image[i]
1302 || !ctx->Texture.Proxy2D->Image[i]
1303 || !ctx->Texture.Proxy3D->Image[i]) {
1304 out_of_memory = GL_TRUE;
1305 }
1306 }
1307 if (out_of_memory) {
1308 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1309 if (ctx->Texture.Proxy1D->Image[i]) {
1310 _mesa_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
1311 }
1312 if (ctx->Texture.Proxy2D->Image[i]) {
1313 _mesa_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
1314 }
1315 if (ctx->Texture.Proxy3D->Image[i]) {
1316 _mesa_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
1317 }
1318 }
1319 gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
1320 gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
1321 gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
1322 return GL_FALSE;
1323 }
1324 else {
1325 return GL_TRUE;
1326 }
1327 }
1328
1329
1330
1331 /*
1332 * Initialize a GLcontext struct.
1333 */
1334 GLboolean
1335 _mesa_initialize_context( GLcontext *ctx,
1336 GLvisual *visual,
1337 GLcontext *share_list,
1338 void *driver_ctx,
1339 GLboolean direct )
1340 {
1341 (void) direct; /* not used */
1342
1343 /* misc one-time initializations */
1344 one_time_init();
1345
1346 ctx->DriverCtx = driver_ctx;
1347 ctx->Visual = visual;
1348 ctx->DrawBuffer = NULL;
1349 ctx->ReadBuffer = NULL;
1350
1351 ctx->VB = gl_vb_create_for_immediate( ctx );
1352 if (!ctx->VB) {
1353 FREE( ctx );
1354 return GL_FALSE;
1355 }
1356 ctx->input = ctx->VB->IM;
1357
1358 ctx->PB = gl_alloc_pb();
1359 if (!ctx->PB) {
1360 FREE( ctx->VB );
1361 FREE( ctx );
1362 return GL_FALSE;
1363 }
1364
1365 if (share_list) {
1366 /* share the group of display lists of another context */
1367 ctx->Shared = share_list->Shared;
1368 }
1369 else {
1370 /* allocate new group of display lists */
1371 ctx->Shared = alloc_shared_state();
1372 if (!ctx->Shared) {
1373 FREE(ctx->VB);
1374 FREE(ctx->PB);
1375 FREE(ctx);
1376 return GL_FALSE;
1377 }
1378 }
1379 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1380 ctx->Shared->RefCount++;
1381 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1382
1383 init_attrib_groups( ctx );
1384
1385 gl_reset_vb( ctx->VB );
1386 gl_reset_input( ctx );
1387
1388 if (visual->DBflag) {
1389 ctx->Color.DrawBuffer = GL_BACK;
1390 ctx->Color.DriverDrawBuffer = GL_BACK_LEFT;
1391 ctx->Color.DrawDestMask = BACK_LEFT_BIT;
1392 ctx->Pixel.ReadBuffer = GL_BACK;
1393 ctx->Pixel.DriverReadBuffer = GL_BACK_LEFT;
1394 }
1395 else {
1396 ctx->Color.DrawBuffer = GL_FRONT;
1397 ctx->Color.DriverDrawBuffer = GL_FRONT_LEFT;
1398 ctx->Color.DrawDestMask = FRONT_LEFT_BIT;
1399 ctx->Pixel.ReadBuffer = GL_FRONT;
1400 ctx->Pixel.DriverReadBuffer = GL_FRONT_LEFT;
1401 }
1402
1403 if (!alloc_proxy_textures(ctx)) {
1404 free_shared_state(ctx, ctx->Shared);
1405 FREE(ctx->VB);
1406 FREE(ctx->PB);
1407 FREE(ctx);
1408 return GL_FALSE;
1409 }
1410
1411 /* setup API dispatch tables */
1412 ctx->Exec = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
1413 ctx->Save = (struct _glapi_table *) CALLOC(_glapi_get_dispatch_table_size() * sizeof(void *));
1414 if (!ctx->Exec || !ctx->Save) {
1415 free_shared_state(ctx, ctx->Shared);
1416 FREE(ctx->VB);
1417 FREE(ctx->PB);
1418 if (ctx->Exec)
1419 FREE(ctx->Exec);
1420 FREE(ctx);
1421 }
1422 _mesa_init_exec_table( ctx->Exec );
1423 _mesa_init_dlist_table( ctx->Save );
1424 ctx->CurrentDispatch = ctx->Exec;
1425
1426 return GL_TRUE;
1427 }
1428
1429
1430
1431 /*
1432 * Allocate and initialize a GLcontext structure.
1433 * Input: visual - a GLvisual pointer
1434 * sharelist - another context to share display lists with or NULL
1435 * driver_ctx - pointer to device driver's context state struct
1436 * Return: pointer to a new gl_context struct or NULL if error.
1437 */
1438 GLcontext *
1439 gl_create_context( GLvisual *visual,
1440 GLcontext *share_list,
1441 void *driver_ctx,
1442 GLboolean direct )
1443 {
1444 GLcontext *ctx = (GLcontext *) CALLOC( sizeof(GLcontext) );
1445 if (!ctx) {
1446 return NULL;
1447 }
1448
1449 if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
1450 return ctx;
1451 }
1452 else {
1453 FREE(ctx);
1454 return NULL;
1455 }
1456 }
1457
1458
1459
1460 /*
1461 * Free the data associated with the given context.
1462 * But don't free() the GLcontext struct itself!
1463 */
1464 void
1465 gl_free_context_data( GLcontext *ctx )
1466 {
1467 struct gl_shine_tab *s, *tmps;
1468 GLuint i, j;
1469
1470 /* if we're destroying the current context, unbind it first */
1471 if (ctx == gl_get_current_context()) {
1472 gl_make_current(NULL, NULL);
1473 }
1474
1475 gl_matrix_dtr( &ctx->ModelView );
1476 for (i = 0; i < MAX_MODELVIEW_STACK_DEPTH - 1; i++) {
1477 gl_matrix_dtr( &ctx->ModelViewStack[i] );
1478 }
1479 gl_matrix_dtr( &ctx->ProjectionMatrix );
1480 for (i = 0; i < MAX_PROJECTION_STACK_DEPTH - 1; i++) {
1481 gl_matrix_dtr( &ctx->ProjectionStack[i] );
1482 }
1483 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1484 gl_matrix_dtr( &ctx->TextureMatrix[i] );
1485 for (j = 0; j < MAX_TEXTURE_STACK_DEPTH - 1; j++) {
1486 gl_matrix_dtr( &ctx->TextureStack[i][j] );
1487 }
1488 }
1489
1490 FREE( ctx->PB );
1491
1492 if (ctx->input != ctx->VB->IM)
1493 gl_immediate_free( ctx->input );
1494
1495 gl_vb_free( ctx->VB );
1496
1497 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1498 ctx->Shared->RefCount--;
1499 assert(ctx->Shared->RefCount >= 0);
1500 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1501 if (ctx->Shared->RefCount == 0) {
1502 /* free shared state */
1503 free_shared_state( ctx, ctx->Shared );
1504 }
1505
1506 foreach_s( s, tmps, ctx->ShineTabList ) {
1507 FREE( s );
1508 }
1509 FREE( ctx->ShineTabList );
1510
1511 /* Free proxy texture objects */
1512 gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
1513 gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
1514 gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
1515
1516 /* Free evaluator data */
1517 if (ctx->EvalMap.Map1Vertex3.Points)
1518 FREE( ctx->EvalMap.Map1Vertex3.Points );
1519 if (ctx->EvalMap.Map1Vertex4.Points)
1520 FREE( ctx->EvalMap.Map1Vertex4.Points );
1521 if (ctx->EvalMap.Map1Index.Points)
1522 FREE( ctx->EvalMap.Map1Index.Points );
1523 if (ctx->EvalMap.Map1Color4.Points)
1524 FREE( ctx->EvalMap.Map1Color4.Points );
1525 if (ctx->EvalMap.Map1Normal.Points)
1526 FREE( ctx->EvalMap.Map1Normal.Points );
1527 if (ctx->EvalMap.Map1Texture1.Points)
1528 FREE( ctx->EvalMap.Map1Texture1.Points );
1529 if (ctx->EvalMap.Map1Texture2.Points)
1530 FREE( ctx->EvalMap.Map1Texture2.Points );
1531 if (ctx->EvalMap.Map1Texture3.Points)
1532 FREE( ctx->EvalMap.Map1Texture3.Points );
1533 if (ctx->EvalMap.Map1Texture4.Points)
1534 FREE( ctx->EvalMap.Map1Texture4.Points );
1535
1536 if (ctx->EvalMap.Map2Vertex3.Points)
1537 FREE( ctx->EvalMap.Map2Vertex3.Points );
1538 if (ctx->EvalMap.Map2Vertex4.Points)
1539 FREE( ctx->EvalMap.Map2Vertex4.Points );
1540 if (ctx->EvalMap.Map2Index.Points)
1541 FREE( ctx->EvalMap.Map2Index.Points );
1542 if (ctx->EvalMap.Map2Color4.Points)
1543 FREE( ctx->EvalMap.Map2Color4.Points );
1544 if (ctx->EvalMap.Map2Normal.Points)
1545 FREE( ctx->EvalMap.Map2Normal.Points );
1546 if (ctx->EvalMap.Map2Texture1.Points)
1547 FREE( ctx->EvalMap.Map2Texture1.Points );
1548 if (ctx->EvalMap.Map2Texture2.Points)
1549 FREE( ctx->EvalMap.Map2Texture2.Points );
1550 if (ctx->EvalMap.Map2Texture3.Points)
1551 FREE( ctx->EvalMap.Map2Texture3.Points );
1552 if (ctx->EvalMap.Map2Texture4.Points)
1553 FREE( ctx->EvalMap.Map2Texture4.Points );
1554
1555 _mesa_free_colortable_data( &ctx->ColorTable );
1556 _mesa_free_colortable_data( &ctx->PostConvolutionColorTable );
1557 _mesa_free_colortable_data( &ctx->PostColorMatrixColorTable );
1558 _mesa_free_colortable_data( &ctx->Texture.Palette );
1559
1560 /* Free cache of immediate buffers. */
1561 while (ctx->nr_im_queued-- > 0) {
1562 struct immediate * next = ctx->freed_im_queue->next;
1563 FREE( ctx->freed_im_queue );
1564 ctx->freed_im_queue = next;
1565 }
1566 gl_extensions_dtr(ctx);
1567
1568 FREE(ctx->Exec);
1569 FREE(ctx->Save);
1570 }
1571
1572
1573
1574 /*
1575 * Destroy a GLcontext structure.
1576 */
1577 void
1578 gl_destroy_context( GLcontext *ctx )
1579 {
1580 if (ctx) {
1581 gl_free_context_data(ctx);
1582 FREE( (void *) ctx );
1583 }
1584 }
1585
1586
1587
1588 /*
1589 * Called by the driver after both the context and driver are fully
1590 * initialized. Currently just reads the config file.
1591 */
1592 void
1593 gl_context_initialize( GLcontext *ctx )
1594 {
1595 gl_read_config_file( ctx );
1596 }
1597
1598
1599
1600 /*
1601 * Copy attribute groups from one context to another.
1602 * Input: src - source context
1603 * dst - destination context
1604 * mask - bitwise OR of GL_*_BIT flags
1605 */
1606 void
1607 gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
1608 {
1609 if (mask & GL_ACCUM_BUFFER_BIT) {
1610 MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
1611 }
1612 if (mask & GL_COLOR_BUFFER_BIT) {
1613 MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
1614 }
1615 if (mask & GL_CURRENT_BIT) {
1616 MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
1617 }
1618 if (mask & GL_DEPTH_BUFFER_BIT) {
1619 MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
1620 }
1621 if (mask & GL_ENABLE_BIT) {
1622 /* no op */
1623 }
1624 if (mask & GL_EVAL_BIT) {
1625 MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
1626 }
1627 if (mask & GL_FOG_BIT) {
1628 MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
1629 }
1630 if (mask & GL_HINT_BIT) {
1631 MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
1632 }
1633 if (mask & GL_LIGHTING_BIT) {
1634 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
1635 /* gl_reinit_light_attrib( &dst->Light ); */
1636 }
1637 if (mask & GL_LINE_BIT) {
1638 MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
1639 }
1640 if (mask & GL_LIST_BIT) {
1641 MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
1642 }
1643 if (mask & GL_PIXEL_MODE_BIT) {
1644 MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
1645 }
1646 if (mask & GL_POINT_BIT) {
1647 MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
1648 }
1649 if (mask & GL_POLYGON_BIT) {
1650 MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
1651 }
1652 if (mask & GL_POLYGON_STIPPLE_BIT) {
1653 /* Use loop instead of MEMCPY due to problem with Portland Group's
1654 * C compiler. Reported by John Stone.
1655 */
1656 int i;
1657 for (i=0;i<32;i++) {
1658 dst->PolygonStipple[i] = src->PolygonStipple[i];
1659 }
1660 }
1661 if (mask & GL_SCISSOR_BIT) {
1662 MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
1663 }
1664 if (mask & GL_STENCIL_BUFFER_BIT) {
1665 MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
1666 }
1667 if (mask & GL_TEXTURE_BIT) {
1668 MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
1669 }
1670 if (mask & GL_TRANSFORM_BIT) {
1671 MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
1672 }
1673 if (mask & GL_VIEWPORT_BIT) {
1674 MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
1675 }
1676 }
1677
1678
1679 /*
1680 * Set the current context, binding the given frame buffer to the context.
1681 */
1682 void gl_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1683 {
1684 gl_make_current2( newCtx, buffer, buffer );
1685 }
1686
1687
1688 /*
1689 * Bind the given context to the given draw-buffer and read-buffer
1690 * and make it the current context for this thread.
1691 */
1692 void gl_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1693 GLframebuffer *readBuffer )
1694 {
1695 #if 0
1696 GLcontext *oldCtx = gl_get_context();
1697
1698 /* Flush the old context
1699 */
1700 if (oldCtx) {
1701 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(oldCtx, "gl_make_current");
1702
1703 /* unbind frame buffers from context */
1704 if (oldCtx->DrawBuffer) {
1705 oldCtx->DrawBuffer = NULL;
1706 }
1707 if (oldCtx->ReadBuffer) {
1708 oldCtx->ReadBuffer = NULL;
1709 }
1710 }
1711 #endif
1712
1713 /* We call this function periodically (just here for now) in
1714 * order to detect when multithreading has begun.
1715 */
1716 _glapi_check_multithread();
1717
1718 _glapi_set_context((void *) newCtx);
1719 ASSERT(gl_get_current_context() == newCtx);
1720 if (newCtx) {
1721 SET_IMMEDIATE(newCtx, newCtx->input);
1722 _glapi_set_dispatch(newCtx->CurrentDispatch);
1723 }
1724 else {
1725 _glapi_set_dispatch(NULL); /* none current */
1726 }
1727
1728 if (MESA_VERBOSE) fprintf(stderr, "gl_make_current()\n");
1729
1730 if (newCtx && drawBuffer && readBuffer) {
1731 /* TODO: check if newCtx and buffer's visual match??? */
1732 newCtx->DrawBuffer = drawBuffer;
1733 newCtx->ReadBuffer = readBuffer;
1734 newCtx->NewState = NEW_ALL; /* just to be safe */
1735 gl_update_state( newCtx );
1736 }
1737
1738 /* We can use this to help debug user's problems. Tell the to set
1739 * the MESA_INFO env variable before running their app. Then the
1740 * first time each context is made current we'll print some useful
1741 * information.
1742 */
1743 if (newCtx && newCtx->FirstTimeCurrent) {
1744 if (getenv("MESA_INFO")) {
1745 fprintf(stderr, "Mesa GL_VERSION = %s\n", (char *) _mesa_GetString(GL_VERSION));
1746 fprintf(stderr, "Mesa GL_RENDERER = %s\n", (char *) _mesa_GetString(GL_RENDERER));
1747 fprintf(stderr, "Mesa GL_VENDOR = %s\n", (char *) _mesa_GetString(GL_VENDOR));
1748 fprintf(stderr, "Mesa GL_EXTENSIONS = %s\n", (char *) _mesa_GetString(GL_EXTENSIONS));
1749 #if defined(THREADS)
1750 fprintf(stderr, "Mesa thread-safe: YES\n");
1751 #else
1752 fprintf(stderr, "Mesa thread-safe: NO\n");
1753 #endif
1754 #if defined(USE_X86_ASM)
1755 fprintf(stderr, "Mesa x86-optimized: YES\n");
1756 #else
1757 fprintf(stderr, "Mesa x86-optimized: NO\n");
1758 #endif
1759 }
1760 newCtx->FirstTimeCurrent = GL_FALSE;
1761 }
1762 }
1763
1764
1765
1766 /*
1767 * Return current context handle for the calling thread.
1768 * This isn't the fastest way to get the current context.
1769 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
1770 */
1771 GLcontext *gl_get_current_context( void )
1772 {
1773 return (GLcontext *) _glapi_get_context();
1774 }
1775
1776
1777
1778 /*
1779 * This should be called by device drivers just before they do a
1780 * swapbuffers. Any pending rendering commands will be executed.
1781 */
1782 void
1783 _mesa_swapbuffers(GLcontext *ctx)
1784 {
1785 FLUSH_VB( ctx, "swap buffers" );
1786 }
1787
1788
1789
1790 /*
1791 * Return pointer to this context's current API dispatch table.
1792 * It'll either be the immediate-mode execute dispatcher or the
1793 * display list compile dispatcher.
1794 */
1795 struct _glapi_table *
1796 _mesa_get_dispatch(GLcontext *ctx)
1797 {
1798 return ctx->CurrentDispatch;
1799 }
1800
1801
1802
1803 /**********************************************************************/
1804 /***** Miscellaneous functions *****/
1805 /**********************************************************************/
1806
1807
1808 /*
1809 * This function is called when the Mesa user has stumbled into a code
1810 * path which may not be implemented fully or correctly.
1811 */
1812 void gl_problem( const GLcontext *ctx, const char *s )
1813 {
1814 fprintf( stderr, "Mesa implementation error: %s\n", s );
1815 fprintf( stderr, "Report to mesa-bugs@mesa3d.org\n" );
1816 (void) ctx;
1817 }
1818
1819
1820
1821 /*
1822 * This is called to inform the user that he or she has tried to do
1823 * something illogical or if there's likely a bug in their program
1824 * (like enabled depth testing without a depth buffer).
1825 */
1826 void gl_warning( const GLcontext *ctx, const char *s )
1827 {
1828 GLboolean debug;
1829 #ifdef DEBUG
1830 debug = GL_TRUE;
1831 #else
1832 if (getenv("MESA_DEBUG")) {
1833 debug = GL_TRUE;
1834 }
1835 else {
1836 debug = GL_FALSE;
1837 }
1838 #endif
1839 if (debug) {
1840 fprintf( stderr, "Mesa warning: %s\n", s );
1841 }
1842 (void) ctx;
1843 }
1844
1845
1846
1847 /*
1848 * Compile an error into current display list.
1849 */
1850 void gl_compile_error( GLcontext *ctx, GLenum error, const char *s )
1851 {
1852 if (ctx->CompileFlag)
1853 gl_save_error( ctx, error, s );
1854
1855 if (ctx->ExecuteFlag)
1856 gl_error( ctx, error, s );
1857 }
1858
1859
1860
1861 /*
1862 * This is Mesa's error handler. Normally, all that's done is the updating
1863 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1864 * environment variable "MESA_DEBUG" is defined then a real error message
1865 * is printed to stderr.
1866 * Input: error - the error value
1867 * s - a diagnostic string
1868 */
1869 void gl_error( GLcontext *ctx, GLenum error, const char *s )
1870 {
1871 GLboolean debug;
1872
1873 #ifdef DEBUG
1874 debug = GL_TRUE;
1875 #else
1876 if (getenv("MESA_DEBUG")) {
1877 debug = GL_TRUE;
1878 }
1879 else {
1880 debug = GL_FALSE;
1881 }
1882 #endif
1883
1884 if (debug) {
1885 char errstr[1000];
1886
1887 switch (error) {
1888 case GL_NO_ERROR:
1889 strcpy( errstr, "GL_NO_ERROR" );
1890 break;
1891 case GL_INVALID_VALUE:
1892 strcpy( errstr, "GL_INVALID_VALUE" );
1893 break;
1894 case GL_INVALID_ENUM:
1895 strcpy( errstr, "GL_INVALID_ENUM" );
1896 break;
1897 case GL_INVALID_OPERATION:
1898 strcpy( errstr, "GL_INVALID_OPERATION" );
1899 break;
1900 case GL_STACK_OVERFLOW:
1901 strcpy( errstr, "GL_STACK_OVERFLOW" );
1902 break;
1903 case GL_STACK_UNDERFLOW:
1904 strcpy( errstr, "GL_STACK_UNDERFLOW" );
1905 break;
1906 case GL_OUT_OF_MEMORY:
1907 strcpy( errstr, "GL_OUT_OF_MEMORY" );
1908 break;
1909 default:
1910 strcpy( errstr, "unknown" );
1911 break;
1912 }
1913 fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
1914 }
1915
1916 if (ctx->ErrorValue==GL_NO_ERROR) {
1917 ctx->ErrorValue = error;
1918 }
1919
1920 /* Call device driver's error handler, if any. This is used on the Mac. */
1921 if (ctx->Driver.Error) {
1922 (*ctx->Driver.Error)( ctx );
1923 }
1924 }
1925
1926
1927
1928 void
1929 _mesa_Finish( void )
1930 {
1931 GET_CURRENT_CONTEXT(ctx);
1932 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFinish");
1933 if (ctx->Driver.Finish) {
1934 (*ctx->Driver.Finish)( ctx );
1935 }
1936 }
1937
1938
1939
1940 void
1941 _mesa_Flush( void )
1942 {
1943 GET_CURRENT_CONTEXT(ctx);
1944 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glFlush");
1945 if (ctx->Driver.Flush) {
1946 (*ctx->Driver.Flush)( ctx );
1947 }
1948 }