XFree86 fixes (clib macro wrappers)
[mesa.git] / src / mesa / main / context.c
1 /* $Id: context.c,v 1.181 2002/10/09 19:36:24 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 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 "buffers.h"
33 #include "clip.h"
34 #include "colortab.h"
35 #include "context.h"
36 #include "dlist.h"
37 #include "eval.h"
38 #include "enums.h"
39 #include "extensions.h"
40 #include "fog.h"
41 #include "get.h"
42 #include "glthread.h"
43 #include "hash.h"
44 #include "light.h"
45 #include "macros.h"
46 #include "mem.h"
47 #include "mmath.h"
48 #include "simple_list.h"
49 #include "state.h"
50 #include "teximage.h"
51 #include "texobj.h"
52 #include "texstate.h"
53 #include "mtypes.h"
54 #include "varray.h"
55 #include "vpstate.h"
56 #include "vtxfmt.h"
57 #include "math/m_translate.h"
58 #include "math/m_matrix.h"
59 #include "math/m_xform.h"
60 #include "math/mathmod.h"
61 #endif
62
63 #if defined(MESA_TRACE)
64 #include "Trace/tr_context.h"
65 #include "Trace/tr_wrapper.h"
66 #endif
67
68 #ifdef USE_SPARC_ASM
69 #include "SPARC/sparc.h"
70 #endif
71
72 #ifndef MESA_VERBOSE
73 int MESA_VERBOSE = 0;
74 #endif
75
76 #ifndef MESA_DEBUG_FLAGS
77 int MESA_DEBUG_FLAGS = 0;
78 #endif
79
80 /* XFree86 stuff */
81 #ifdef getenv
82 #undef getenv
83 #endif
84 #ifdef calloc
85 #undef calloc
86 extern void *calloc(size_t, size_t);
87 #endif
88 #ifdef free
89 #undef free
90 extern void free(void *);
91 #endif
92
93
94 static void
95 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
96
97
98 /**********************************************************************/
99 /***** OpenGL SI-style interface (new in Mesa 3.5) *****/
100 /**********************************************************************/
101
102 /* Called by window system/device driver (via gc->exports.destroyCurrent())
103 * when the rendering context is to be destroyed.
104 */
105 GLboolean
106 _mesa_destroyContext(__GLcontext *gc)
107 {
108 if (gc) {
109 _mesa_free_context_data(gc);
110 (*gc->imports.free)(gc, gc);
111 }
112 return GL_TRUE;
113 }
114
115 /* Called by window system/device driver (via gc->exports.loseCurrent())
116 * when the rendering context is made non-current.
117 */
118 GLboolean
119 _mesa_loseCurrent(__GLcontext *gc)
120 {
121 /* XXX unbind context from thread */
122 return GL_TRUE;
123 }
124
125 /* Called by window system/device driver (via gc->exports.makeCurrent())
126 * when the rendering context is made current.
127 */
128 GLboolean
129 _mesa_makeCurrent(__GLcontext *gc)
130 {
131 /* XXX bind context to thread */
132 return GL_TRUE;
133 }
134
135 /* Called by window system/device driver - yadda, yadda, yadda.
136 * See above comments.
137 */
138 GLboolean
139 _mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
140 {
141 if (gc && gcShare && gc->Shared && gcShare->Shared) {
142 gc->Shared->RefCount--;
143 if (gc->Shared->RefCount == 0) {
144 free_shared_state(gc, gc->Shared);
145 }
146 gc->Shared = gcShare->Shared;
147 gc->Shared->RefCount++;
148 return GL_TRUE;
149 }
150 else {
151 return GL_FALSE;
152 }
153 }
154
155 GLboolean
156 _mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
157 {
158 if (dst && src) {
159 _mesa_copy_context( src, dst, mask );
160 return GL_TRUE;
161 }
162 else {
163 return GL_FALSE;
164 }
165 }
166
167 GLboolean
168 _mesa_forceCurrent(__GLcontext *gc)
169 {
170 return GL_TRUE;
171 }
172
173 GLboolean
174 _mesa_notifyResize(__GLcontext *gc)
175 {
176 GLint x, y;
177 GLuint width, height;
178 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
179 if (!d || !d->getDrawableSize)
180 return GL_FALSE;
181 d->getDrawableSize( d, &x, &y, &width, &height );
182 /* update viewport, resize software buffers, etc. */
183 return GL_TRUE;
184 }
185
186 void
187 _mesa_notifyDestroy(__GLcontext *gc)
188 {
189 }
190
191 /* Called by window system just before swapping buffers.
192 * We have to finish any pending rendering.
193 */
194 void
195 _mesa_notifySwapBuffers(__GLcontext *gc)
196 {
197 FLUSH_VERTICES( gc, 0 );
198 }
199
200 struct __GLdispatchStateRec *
201 _mesa_dispatchExec(__GLcontext *gc)
202 {
203 return NULL;
204 }
205
206 void
207 _mesa_beginDispatchOverride(__GLcontext *gc)
208 {
209 }
210
211 void
212 _mesa_endDispatchOverride(__GLcontext *gc)
213 {
214 }
215
216 /* Setup the exports. The window system will call these functions
217 * when it needs Mesa to do something.
218 * NOTE: Device drivers should override these functions! For example,
219 * the Xlib driver should plug in the XMesa*-style functions into this
220 * structure. The XMesa-style functions should then call the _mesa_*
221 * version of these functions. This is an approximation to OO design
222 * (inheritance and virtual functions).
223 */
224 static void
225 _mesa_init_default_exports(__GLexports *exports)
226 {
227 exports->destroyContext = _mesa_destroyContext;
228 exports->loseCurrent = _mesa_loseCurrent;
229 exports->makeCurrent = _mesa_makeCurrent;
230 exports->shareContext = _mesa_shareContext;
231 exports->copyContext = _mesa_copyContext;
232 exports->forceCurrent = _mesa_forceCurrent;
233 exports->notifyResize = _mesa_notifyResize;
234 exports->notifyDestroy = _mesa_notifyDestroy;
235 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
236 exports->dispatchExec = _mesa_dispatchExec;
237 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
238 exports->endDispatchOverride = _mesa_endDispatchOverride;
239 }
240
241
242
243 /* exported OpenGL SI interface */
244 __GLcontext *
245 __glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
246 {
247 GLcontext *ctx;
248
249 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
250 if (ctx == NULL) {
251 return NULL;
252 }
253 ctx->Driver.CurrentExecPrimitive=0; /* XXX why is this here??? */
254 ctx->imports = *imports;
255 _mesa_init_default_exports(&(ctx->exports));
256
257 _mesa_initialize_visual(&ctx->Visual,
258 modes->rgbMode,
259 modes->doubleBufferMode,
260 modes->stereoMode,
261 modes->redBits,
262 modes->greenBits,
263 modes->blueBits,
264 modes->alphaBits,
265 modes->indexBits,
266 modes->depthBits,
267 modes->stencilBits,
268 modes->accumRedBits,
269 modes->accumGreenBits,
270 modes->accumBlueBits,
271 modes->accumAlphaBits,
272 0);
273
274 _mesa_initialize_context(ctx, &ctx->Visual, NULL, imports);
275
276 return ctx;
277 }
278
279
280 /* exported OpenGL SI interface */
281 void
282 __glCoreNopDispatch(void)
283 {
284 #if 0
285 /* SI */
286 __gl_dispatch = __glNopDispatchState;
287 #else
288 /* Mesa */
289 _glapi_set_dispatch(NULL);
290 #endif
291 }
292
293
294 /**********************************************************************/
295 /***** GL Visual allocation/destruction *****/
296 /**********************************************************************/
297
298
299 /*
300 * Allocate a new GLvisual object.
301 * Input: rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
302 * dbFlag - double buffering?
303 * stereoFlag - stereo buffer?
304 * depthBits - requested bits per depth buffer value
305 * Any value in [0, 32] is acceptable but the actual
306 * depth type will be GLushort or GLuint as needed.
307 * stencilBits - requested minimum bits per stencil buffer value
308 * accumBits - requested minimum bits per accum buffer component
309 * indexBits - number of bits per pixel if rgbFlag==GL_FALSE
310 * red/green/blue/alphaBits - number of bits per color component
311 * in frame buffer for RGB(A) mode.
312 * We always use 8 in core Mesa though.
313 * Return: pointer to new GLvisual or NULL if requested parameters can't
314 * be met.
315 */
316 GLvisual *
317 _mesa_create_visual( GLboolean rgbFlag,
318 GLboolean dbFlag,
319 GLboolean stereoFlag,
320 GLint redBits,
321 GLint greenBits,
322 GLint blueBits,
323 GLint alphaBits,
324 GLint indexBits,
325 GLint depthBits,
326 GLint stencilBits,
327 GLint accumRedBits,
328 GLint accumGreenBits,
329 GLint accumBlueBits,
330 GLint accumAlphaBits,
331 GLint numSamples )
332 {
333 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
334 if (vis) {
335 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
336 redBits, greenBits, blueBits, alphaBits,
337 indexBits, depthBits, stencilBits,
338 accumRedBits, accumGreenBits,
339 accumBlueBits, accumAlphaBits,
340 numSamples)) {
341 FREE(vis);
342 return NULL;
343 }
344 }
345 return vis;
346 }
347
348
349 /*
350 * Initialize the fields of the given GLvisual.
351 * Input: see _mesa_create_visual() above.
352 * Return: GL_TRUE = success
353 * GL_FALSE = failure.
354 */
355 GLboolean
356 _mesa_initialize_visual( GLvisual *vis,
357 GLboolean rgbFlag,
358 GLboolean dbFlag,
359 GLboolean stereoFlag,
360 GLint redBits,
361 GLint greenBits,
362 GLint blueBits,
363 GLint alphaBits,
364 GLint indexBits,
365 GLint depthBits,
366 GLint stencilBits,
367 GLint accumRedBits,
368 GLint accumGreenBits,
369 GLint accumBlueBits,
370 GLint accumAlphaBits,
371 GLint numSamples )
372 {
373 (void) numSamples;
374
375 assert(vis);
376
377 /* This is to catch bad values from device drivers not updated for
378 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
379 * bad value now (a 1-bit depth buffer!?!).
380 */
381 assert(depthBits == 0 || depthBits > 1);
382
383 if (depthBits < 0 || depthBits > 32) {
384 return GL_FALSE;
385 }
386 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
387 return GL_FALSE;
388 }
389 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
390 return GL_FALSE;
391 }
392 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
393 return GL_FALSE;
394 }
395 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
396 return GL_FALSE;
397 }
398 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
399 return GL_FALSE;
400 }
401
402 vis->rgbMode = rgbFlag;
403 vis->doubleBufferMode = dbFlag;
404 vis->stereoMode = stereoFlag;
405 vis->redBits = redBits;
406 vis->greenBits = greenBits;
407 vis->blueBits = blueBits;
408 vis->alphaBits = alphaBits;
409
410 vis->indexBits = indexBits;
411 vis->depthBits = depthBits;
412 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
413 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
414 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
415 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
416 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
417
418 return GL_TRUE;
419 }
420
421
422 void
423 _mesa_destroy_visual( GLvisual *vis )
424 {
425 FREE(vis);
426 }
427
428
429 /**********************************************************************/
430 /***** GL Framebuffer allocation/destruction *****/
431 /**********************************************************************/
432
433
434 /*
435 * Create a new framebuffer. A GLframebuffer is a struct which
436 * encapsulates the depth, stencil and accum buffers and related
437 * parameters.
438 * Input: visual - a GLvisual pointer (we copy the struct contents)
439 * softwareDepth - create/use a software depth buffer?
440 * softwareStencil - create/use a software stencil buffer?
441 * softwareAccum - create/use a software accum buffer?
442 * softwareAlpha - create/use a software alpha buffer?
443 * Return: pointer to new GLframebuffer struct or NULL if error.
444 */
445 GLframebuffer *
446 _mesa_create_framebuffer( const GLvisual *visual,
447 GLboolean softwareDepth,
448 GLboolean softwareStencil,
449 GLboolean softwareAccum,
450 GLboolean softwareAlpha )
451 {
452 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
453 assert(visual);
454 if (buffer) {
455 _mesa_initialize_framebuffer(buffer, visual,
456 softwareDepth, softwareStencil,
457 softwareAccum, softwareAlpha );
458 }
459 return buffer;
460 }
461
462
463 /*
464 * Initialize a GLframebuffer object.
465 * Input: See _mesa_create_framebuffer() above.
466 */
467 void
468 _mesa_initialize_framebuffer( GLframebuffer *buffer,
469 const GLvisual *visual,
470 GLboolean softwareDepth,
471 GLboolean softwareStencil,
472 GLboolean softwareAccum,
473 GLboolean softwareAlpha )
474 {
475 assert(buffer);
476 assert(visual);
477
478 BZERO(buffer, sizeof(GLframebuffer));
479
480 /* sanity checks */
481 if (softwareDepth ) {
482 assert(visual->depthBits > 0);
483 }
484 if (softwareStencil) {
485 assert(visual->stencilBits > 0);
486 }
487 if (softwareAccum) {
488 assert(visual->rgbMode);
489 assert(visual->accumRedBits > 0);
490 assert(visual->accumGreenBits > 0);
491 assert(visual->accumBlueBits > 0);
492 }
493 if (softwareAlpha) {
494 assert(visual->rgbMode);
495 assert(visual->alphaBits > 0);
496 }
497
498 buffer->Visual = *visual;
499 buffer->UseSoftwareDepthBuffer = softwareDepth;
500 buffer->UseSoftwareStencilBuffer = softwareStencil;
501 buffer->UseSoftwareAccumBuffer = softwareAccum;
502 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
503 }
504
505
506 /*
507 * Free a framebuffer struct and its buffers.
508 */
509 void
510 _mesa_destroy_framebuffer( GLframebuffer *buffer )
511 {
512 if (buffer) {
513 _mesa_free_framebuffer_data(buffer);
514 FREE(buffer);
515 }
516 }
517
518
519 /*
520 * Free the data hanging off of <buffer>, but not <buffer> itself.
521 */
522 void
523 _mesa_free_framebuffer_data( GLframebuffer *buffer )
524 {
525 if (!buffer)
526 return;
527
528 if (buffer->DepthBuffer) {
529 MESA_PBUFFER_FREE( buffer->DepthBuffer );
530 buffer->DepthBuffer = NULL;
531 }
532 if (buffer->Accum) {
533 MESA_PBUFFER_FREE( buffer->Accum );
534 buffer->Accum = NULL;
535 }
536 if (buffer->Stencil) {
537 MESA_PBUFFER_FREE( buffer->Stencil );
538 buffer->Stencil = NULL;
539 }
540 if (buffer->FrontLeftAlpha) {
541 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
542 buffer->FrontLeftAlpha = NULL;
543 }
544 if (buffer->BackLeftAlpha) {
545 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
546 buffer->BackLeftAlpha = NULL;
547 }
548 if (buffer->FrontRightAlpha) {
549 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
550 buffer->FrontRightAlpha = NULL;
551 }
552 if (buffer->BackRightAlpha) {
553 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
554 buffer->BackRightAlpha = NULL;
555 }
556 }
557
558
559
560 /**********************************************************************/
561 /***** Context allocation, initialization, destroying *****/
562 /**********************************************************************/
563
564
565 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
566
567
568 /*
569 * This function just calls all the various one-time-init functions in Mesa.
570 */
571 static void
572 one_time_init( GLcontext *ctx )
573 {
574 static GLboolean alreadyCalled = GL_FALSE;
575 _glthread_LOCK_MUTEX(OneTimeLock);
576 if (!alreadyCalled) {
577 /* do some implementation tests */
578 assert( sizeof(GLbyte) == 1 );
579 assert( sizeof(GLshort) >= 2 );
580 assert( sizeof(GLint) >= 4 );
581 assert( sizeof(GLubyte) == 1 );
582 assert( sizeof(GLushort) >= 2 );
583 assert( sizeof(GLuint) >= 4 );
584
585 _mesa_init_lists();
586
587 _math_init();
588 _mesa_init_math();
589
590 #ifdef USE_SPARC_ASM
591 _mesa_init_sparc_glapi_relocs();
592 #endif
593 if (ctx->imports.getenv(ctx, "MESA_DEBUG")) {
594 _glapi_noop_enable_warnings(GL_TRUE);
595 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
596 }
597 else {
598 _glapi_noop_enable_warnings(GL_FALSE);
599 }
600
601 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
602 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
603 #endif
604
605 alreadyCalled = GL_TRUE;
606 }
607 _glthread_UNLOCK_MUTEX(OneTimeLock);
608 }
609
610
611 static void
612 init_matrix_stack( struct matrix_stack *stack,
613 GLuint maxDepth, GLuint dirtyFlag )
614 {
615 GLuint i;
616
617 stack->Depth = 0;
618 stack->MaxDepth = maxDepth;
619 stack->DirtyFlag = dirtyFlag;
620 /* The stack */
621 stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
622 for (i = 0; i < maxDepth; i++) {
623 _math_matrix_ctr(&stack->Stack[i]);
624 _math_matrix_alloc_inv(&stack->Stack[i]);
625 }
626 stack->Top = stack->Stack;
627 }
628
629
630 static void
631 free_matrix_stack( struct matrix_stack *stack )
632 {
633 GLuint i;
634 for (i = 0; i < stack->MaxDepth; i++) {
635 _math_matrix_dtr(&stack->Stack[i]);
636 }
637 FREE(stack->Stack);
638 stack->Stack = stack->Top = NULL;
639 }
640
641
642 /*
643 * Allocate and initialize a shared context state structure.
644 */
645 static struct gl_shared_state *
646 alloc_shared_state( void )
647 {
648 struct gl_shared_state *ss;
649 GLboolean outOfMemory;
650
651 ss = CALLOC_STRUCT(gl_shared_state);
652 if (!ss)
653 return NULL;
654
655 _glthread_INIT_MUTEX(ss->Mutex);
656
657 ss->DisplayList = _mesa_NewHashTable();
658 ss->TexObjects = _mesa_NewHashTable();
659 ss->VertexPrograms = _mesa_NewHashTable();
660
661 /* Default Texture objects */
662 outOfMemory = GL_FALSE;
663
664 ss->Default1D = _mesa_alloc_texture_object(ss, 0, GL_TEXTURE_1D);
665 if (!ss->Default1D) {
666 outOfMemory = GL_TRUE;
667 }
668
669 ss->Default2D = _mesa_alloc_texture_object(ss, 0, GL_TEXTURE_2D);
670 if (!ss->Default2D) {
671 outOfMemory = GL_TRUE;
672 }
673
674 ss->Default3D = _mesa_alloc_texture_object(ss, 0, GL_TEXTURE_3D);
675 if (!ss->Default3D) {
676 outOfMemory = GL_TRUE;
677 }
678
679 ss->DefaultCubeMap = _mesa_alloc_texture_object(ss, 0,
680 GL_TEXTURE_CUBE_MAP_ARB);
681 if (!ss->DefaultCubeMap) {
682 outOfMemory = GL_TRUE;
683 }
684
685 ss->DefaultRect = _mesa_alloc_texture_object(ss, 0,
686 GL_TEXTURE_RECTANGLE_NV);
687 if (!ss->DefaultRect) {
688 outOfMemory = GL_TRUE;
689 }
690
691 if (!ss->DisplayList || !ss->TexObjects || !ss->VertexPrograms
692 || outOfMemory) {
693 /* Ran out of memory at some point. Free everything and return NULL */
694 if (ss->DisplayList)
695 _mesa_DeleteHashTable(ss->DisplayList);
696 if (ss->TexObjects)
697 _mesa_DeleteHashTable(ss->TexObjects);
698 if (ss->VertexPrograms)
699 _mesa_DeleteHashTable(ss->VertexPrograms);
700 if (ss->Default1D)
701 _mesa_free_texture_object(ss, ss->Default1D);
702 if (ss->Default2D)
703 _mesa_free_texture_object(ss, ss->Default2D);
704 if (ss->Default3D)
705 _mesa_free_texture_object(ss, ss->Default3D);
706 if (ss->DefaultCubeMap)
707 _mesa_free_texture_object(ss, ss->DefaultCubeMap);
708 if (ss->DefaultRect)
709 _mesa_free_texture_object(ss, ss->DefaultRect);
710 FREE(ss);
711 return NULL;
712 }
713 else {
714 return ss;
715 }
716 }
717
718
719 /*
720 * Deallocate a shared state context and all children structures.
721 */
722 static void
723 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
724 {
725 /* Free display lists */
726 while (1) {
727 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
728 if (list) {
729 _mesa_destroy_list(ctx, list);
730 }
731 else {
732 break;
733 }
734 }
735 _mesa_DeleteHashTable(ss->DisplayList);
736
737 /* Free texture objects */
738 while (ss->TexObjectList) {
739 if (ctx->Driver.DeleteTexture)
740 (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
741 /* this function removes from linked list too! */
742 _mesa_free_texture_object(ss, ss->TexObjectList);
743 }
744 _mesa_DeleteHashTable(ss->TexObjects);
745
746 /* Free vertex programs */
747 while (1) {
748 GLuint prog = _mesa_HashFirstEntry(ss->VertexPrograms);
749 if (prog) {
750 _mesa_delete_program(ctx, prog);
751 }
752 else {
753 break;
754 }
755 }
756 _mesa_DeleteHashTable(ss->VertexPrograms);
757
758 FREE(ss);
759 }
760
761
762
763 /*
764 * Initialize the nth light. Note that the defaults for light 0 are
765 * different than the other lights.
766 */
767 static void
768 init_light( struct gl_light *l, GLuint n )
769 {
770 make_empty_list( l );
771
772 ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
773 if (n==0) {
774 ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
775 ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
776 }
777 else {
778 ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
779 ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
780 }
781 ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
782 ASSIGN_3V( l->EyeDirection, 0.0, 0.0, -1.0 );
783 l->SpotExponent = 0.0;
784 _mesa_invalidate_spot_exp_table( l );
785 l->SpotCutoff = 180.0;
786 l->_CosCutoff = 0.0; /* KW: -ve values not admitted */
787 l->ConstantAttenuation = 1.0;
788 l->LinearAttenuation = 0.0;
789 l->QuadraticAttenuation = 0.0;
790 l->Enabled = GL_FALSE;
791 }
792
793
794
795 static void
796 init_lightmodel( struct gl_lightmodel *lm )
797 {
798 ASSIGN_4V( lm->Ambient, 0.2F, 0.2F, 0.2F, 1.0F );
799 lm->LocalViewer = GL_FALSE;
800 lm->TwoSide = GL_FALSE;
801 lm->ColorControl = GL_SINGLE_COLOR;
802 }
803
804
805 static void
806 init_material( struct gl_material *m )
807 {
808 ASSIGN_4V( m->Ambient, 0.2F, 0.2F, 0.2F, 1.0F );
809 ASSIGN_4V( m->Diffuse, 0.8F, 0.8F, 0.8F, 1.0F );
810 ASSIGN_4V( m->Specular, 0.0F, 0.0F, 0.0F, 1.0F );
811 ASSIGN_4V( m->Emission, 0.0F, 0.0F, 0.0F, 1.0F );
812 m->Shininess = 0.0;
813 m->AmbientIndex = 0;
814 m->DiffuseIndex = 1;
815 m->SpecularIndex = 1;
816 }
817
818
819
820 static void
821 init_texture_unit( GLcontext *ctx, GLuint unit )
822 {
823 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
824
825 texUnit->EnvMode = GL_MODULATE;
826 texUnit->CombineModeRGB = GL_MODULATE;
827 texUnit->CombineModeA = GL_MODULATE;
828 texUnit->CombineSourceRGB[0] = GL_TEXTURE;
829 texUnit->CombineSourceRGB[1] = GL_PREVIOUS_EXT;
830 texUnit->CombineSourceRGB[2] = GL_CONSTANT_EXT;
831 texUnit->CombineSourceA[0] = GL_TEXTURE;
832 texUnit->CombineSourceA[1] = GL_PREVIOUS_EXT;
833 texUnit->CombineSourceA[2] = GL_CONSTANT_EXT;
834 texUnit->CombineOperandRGB[0] = GL_SRC_COLOR;
835 texUnit->CombineOperandRGB[1] = GL_SRC_COLOR;
836 texUnit->CombineOperandRGB[2] = GL_SRC_ALPHA;
837 texUnit->CombineOperandA[0] = GL_SRC_ALPHA;
838 texUnit->CombineOperandA[1] = GL_SRC_ALPHA;
839 texUnit->CombineOperandA[2] = GL_SRC_ALPHA;
840 texUnit->CombineScaleShiftRGB = 0;
841 texUnit->CombineScaleShiftA = 0;
842
843 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
844 texUnit->TexGenEnabled = 0;
845 texUnit->GenModeS = GL_EYE_LINEAR;
846 texUnit->GenModeT = GL_EYE_LINEAR;
847 texUnit->GenModeR = GL_EYE_LINEAR;
848 texUnit->GenModeQ = GL_EYE_LINEAR;
849 texUnit->_GenBitS = TEXGEN_EYE_LINEAR;
850 texUnit->_GenBitT = TEXGEN_EYE_LINEAR;
851 texUnit->_GenBitR = TEXGEN_EYE_LINEAR;
852 texUnit->_GenBitQ = TEXGEN_EYE_LINEAR;
853
854 /* Yes, these plane coefficients are correct! */
855 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
856 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
857 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
858 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
859 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
860 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
861 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
862 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
863
864 texUnit->Current1D = ctx->Shared->Default1D;
865 texUnit->Current2D = ctx->Shared->Default2D;
866 texUnit->Current3D = ctx->Shared->Default3D;
867 texUnit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
868 texUnit->CurrentRect = ctx->Shared->DefaultRect;
869 }
870
871
872
873
874 /* Initialize a 1-D evaluator map */
875 static void
876 init_1d_map( struct gl_1d_map *map, int n, const float *initial )
877 {
878 map->Order = 1;
879 map->u1 = 0.0;
880 map->u2 = 1.0;
881 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
882 if (map->Points) {
883 GLint i;
884 for (i=0;i<n;i++)
885 map->Points[i] = initial[i];
886 }
887 }
888
889
890 /* Initialize a 2-D evaluator map */
891 static void
892 init_2d_map( struct gl_2d_map *map, int n, const float *initial )
893 {
894 map->Uorder = 1;
895 map->Vorder = 1;
896 map->u1 = 0.0;
897 map->u2 = 1.0;
898 map->v1 = 0.0;
899 map->v2 = 1.0;
900 map->Points = (GLfloat *) MALLOC(n * sizeof(GLfloat));
901 if (map->Points) {
902 GLint i;
903 for (i=0;i<n;i++)
904 map->Points[i] = initial[i];
905 }
906 }
907
908
909 /*
910 * Initialize the attribute groups in a GLcontext.
911 */
912 static void
913 init_attrib_groups( GLcontext *ctx )
914 {
915 GLuint i;
916
917 assert(ctx);
918
919 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
920 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
921
922 /* Constants, may be overriden by device drivers */
923 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
924 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
925 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
926 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
927 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
928 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
929 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
930 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
931 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
932 ctx->Const.MinPointSize = MIN_POINT_SIZE;
933 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
934 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
935 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
936 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
937 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
938 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
939 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
940 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
941 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
942 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
943 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
944 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
945 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
946 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
947 ctx->Const.MaxLights = MAX_LIGHTS;
948
949 /* Initialize matrix stacks */
950 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
951 _NEW_MODELVIEW);
952 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
953 _NEW_PROJECTION);
954 init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
955 _NEW_COLOR_MATRIX);
956 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
957 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
958 _NEW_TEXTURE_MATRIX);
959 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
960 init_matrix_stack(&ctx->ProgramMatrixStack[i], MAX_PROGRAM_STACK_DEPTH,
961 _NEW_TRACK_MATRIX);
962 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
963
964 /* Init combined Modelview*Projection matrix */
965 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
966
967 /* Accumulate buffer group */
968 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
969
970 /* Color buffer group */
971 ctx->Color.IndexMask = 0xffffffff;
972 ctx->Color.ColorMask[0] = 0xff;
973 ctx->Color.ColorMask[1] = 0xff;
974 ctx->Color.ColorMask[2] = 0xff;
975 ctx->Color.ColorMask[3] = 0xff;
976 ctx->Color.ClearIndex = 0;
977 ASSIGN_4V( ctx->Color.ClearColor, 0, 0, 0, 0 );
978 ctx->Color.DrawBuffer = GL_FRONT;
979 ctx->Color.AlphaEnabled = GL_FALSE;
980 ctx->Color.AlphaFunc = GL_ALWAYS;
981 ctx->Color.AlphaRef = 0;
982 ctx->Color.BlendEnabled = GL_FALSE;
983 ctx->Color.BlendSrcRGB = GL_ONE;
984 ctx->Color.BlendDstRGB = GL_ZERO;
985 ctx->Color.BlendSrcA = GL_ONE;
986 ctx->Color.BlendDstA = GL_ZERO;
987 ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
988 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
989 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
990 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
991 ctx->Color.LogicOp = GL_COPY;
992 ctx->Color.DitherFlag = GL_TRUE;
993
994 /* Current group */
995 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 0.0, 0.0, 0.0, 0.0 );
996 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 0.0 );
997 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
998 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 0.0 );
999 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
1000 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
1001 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i], 0.0, 0.0, 0.0, 1.0 );
1002 ctx->Current.Index = 1;
1003 ctx->Current.EdgeFlag = GL_TRUE;
1004
1005 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
1006 ctx->Current.RasterDistance = 0.0;
1007 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
1008 ctx->Current.RasterIndex = 1;
1009 for (i=0; i<MAX_TEXTURE_UNITS; i++)
1010 ASSIGN_4V( ctx->Current.RasterTexCoords[i], 0.0, 0.0, 0.0, 1.0 );
1011 ctx->Current.RasterPosValid = GL_TRUE;
1012
1013
1014 /* Depth buffer group */
1015 ctx->Depth.Test = GL_FALSE;
1016 ctx->Depth.Clear = 1.0;
1017 ctx->Depth.Func = GL_LESS;
1018 ctx->Depth.Mask = GL_TRUE;
1019 ctx->Depth.OcclusionTest = GL_FALSE;
1020
1021 /* Evaluators group */
1022 ctx->Eval.Map1Color4 = GL_FALSE;
1023 ctx->Eval.Map1Index = GL_FALSE;
1024 ctx->Eval.Map1Normal = GL_FALSE;
1025 ctx->Eval.Map1TextureCoord1 = GL_FALSE;
1026 ctx->Eval.Map1TextureCoord2 = GL_FALSE;
1027 ctx->Eval.Map1TextureCoord3 = GL_FALSE;
1028 ctx->Eval.Map1TextureCoord4 = GL_FALSE;
1029 ctx->Eval.Map1Vertex3 = GL_FALSE;
1030 ctx->Eval.Map1Vertex4 = GL_FALSE;
1031 MEMSET(ctx->Eval.Map1Attrib, 0, sizeof(ctx->Eval.Map1Attrib));
1032 ctx->Eval.Map2Color4 = GL_FALSE;
1033 ctx->Eval.Map2Index = GL_FALSE;
1034 ctx->Eval.Map2Normal = GL_FALSE;
1035 ctx->Eval.Map2TextureCoord1 = GL_FALSE;
1036 ctx->Eval.Map2TextureCoord2 = GL_FALSE;
1037 ctx->Eval.Map2TextureCoord3 = GL_FALSE;
1038 ctx->Eval.Map2TextureCoord4 = GL_FALSE;
1039 ctx->Eval.Map2Vertex3 = GL_FALSE;
1040 ctx->Eval.Map2Vertex4 = GL_FALSE;
1041 MEMSET(ctx->Eval.Map2Attrib, 0, sizeof(ctx->Eval.Map2Attrib));
1042 ctx->Eval.AutoNormal = GL_FALSE;
1043 ctx->Eval.MapGrid1un = 1;
1044 ctx->Eval.MapGrid1u1 = 0.0;
1045 ctx->Eval.MapGrid1u2 = 1.0;
1046 ctx->Eval.MapGrid2un = 1;
1047 ctx->Eval.MapGrid2vn = 1;
1048 ctx->Eval.MapGrid2u1 = 0.0;
1049 ctx->Eval.MapGrid2u2 = 1.0;
1050 ctx->Eval.MapGrid2v1 = 0.0;
1051 ctx->Eval.MapGrid2v2 = 1.0;
1052
1053 /* Evaluator data */
1054 {
1055 static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
1056 static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
1057 static GLfloat index[1] = { 1.0 };
1058 static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
1059 static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
1060 static GLfloat attrib[4] = { 0.0, 0.0, 0.0, 1.0 };
1061
1062 init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
1063 init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
1064 init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
1065 init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
1066 init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
1067 init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
1068 init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
1069 init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
1070 init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
1071 for (i = 0; i < 16; i++)
1072 init_1d_map( ctx->EvalMap.Map1Attrib + i, 4, attrib );
1073
1074 init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
1075 init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
1076 init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
1077 init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
1078 init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
1079 init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
1080 init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
1081 init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
1082 init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
1083 for (i = 0; i < 16; i++)
1084 init_2d_map( ctx->EvalMap.Map2Attrib + i, 4, attrib );
1085 }
1086
1087 /* Fog group */
1088 ctx->Fog.Enabled = GL_FALSE;
1089 ctx->Fog.Mode = GL_EXP;
1090 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
1091 ctx->Fog.Index = 0.0;
1092 ctx->Fog.Density = 1.0;
1093 ctx->Fog.Start = 0.0;
1094 ctx->Fog.End = 1.0;
1095 ctx->Fog.ColorSumEnabled = GL_FALSE;
1096 ctx->Fog.FogCoordinateSource = GL_FRAGMENT_DEPTH_EXT;
1097
1098 /* Hint group */
1099 ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
1100 ctx->Hint.PointSmooth = GL_DONT_CARE;
1101 ctx->Hint.LineSmooth = GL_DONT_CARE;
1102 ctx->Hint.PolygonSmooth = GL_DONT_CARE;
1103 ctx->Hint.Fog = GL_DONT_CARE;
1104 ctx->Hint.ClipVolumeClipping = GL_DONT_CARE;
1105 ctx->Hint.TextureCompression = GL_DONT_CARE;
1106 ctx->Hint.GenerateMipmap = GL_DONT_CARE;
1107
1108 /* Histogram group */
1109 ctx->Histogram.Width = 0;
1110 ctx->Histogram.Format = GL_RGBA;
1111 ctx->Histogram.Sink = GL_FALSE;
1112 ctx->Histogram.RedSize = 0;
1113 ctx->Histogram.GreenSize = 0;
1114 ctx->Histogram.BlueSize = 0;
1115 ctx->Histogram.AlphaSize = 0;
1116 ctx->Histogram.LuminanceSize = 0;
1117 for (i = 0; i < HISTOGRAM_TABLE_SIZE; i++) {
1118 ctx->Histogram.Count[i][0] = 0;
1119 ctx->Histogram.Count[i][1] = 0;
1120 ctx->Histogram.Count[i][2] = 0;
1121 ctx->Histogram.Count[i][3] = 0;
1122 }
1123
1124 /* Min/Max group */
1125 ctx->MinMax.Format = GL_RGBA;
1126 ctx->MinMax.Sink = GL_FALSE;
1127 ctx->MinMax.Min[RCOMP] = 1000; ctx->MinMax.Max[RCOMP] = -1000;
1128 ctx->MinMax.Min[GCOMP] = 1000; ctx->MinMax.Max[GCOMP] = -1000;
1129 ctx->MinMax.Min[BCOMP] = 1000; ctx->MinMax.Max[BCOMP] = -1000;
1130 ctx->MinMax.Min[ACOMP] = 1000; ctx->MinMax.Max[ACOMP] = -1000;
1131
1132 /* Extensions */
1133 _mesa_extensions_ctr( ctx );
1134
1135 /* Lighting group */
1136 for (i=0;i<MAX_LIGHTS;i++) {
1137 init_light( &ctx->Light.Light[i], i );
1138 }
1139 make_empty_list( &ctx->Light.EnabledList );
1140
1141 init_lightmodel( &ctx->Light.Model );
1142 init_material( &ctx->Light.Material[0] );
1143 init_material( &ctx->Light.Material[1] );
1144 ctx->Light.ShadeModel = GL_SMOOTH;
1145 ctx->Light.Enabled = GL_FALSE;
1146 ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
1147 ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
1148 ctx->Light.ColorMaterialBitmask = _mesa_material_bitmask( ctx,
1149 GL_FRONT_AND_BACK,
1150 GL_AMBIENT_AND_DIFFUSE, ~0, 0 );
1151
1152 ctx->Light.ColorMaterialEnabled = GL_FALSE;
1153
1154 /* Lighting miscellaneous */
1155 ctx->_ShineTabList = MALLOC_STRUCT( gl_shine_tab );
1156 make_empty_list( ctx->_ShineTabList );
1157 for (i = 0 ; i < 10 ; i++) {
1158 struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
1159 s->shininess = -1;
1160 s->refcount = 0;
1161 insert_at_tail( ctx->_ShineTabList, s );
1162 }
1163
1164
1165 /* Line group */
1166 ctx->Line.SmoothFlag = GL_FALSE;
1167 ctx->Line.StippleFlag = GL_FALSE;
1168 ctx->Line.Width = 1.0;
1169 ctx->Line._Width = 1.0;
1170 ctx->Line.StipplePattern = 0xffff;
1171 ctx->Line.StippleFactor = 1;
1172
1173 /* Display List group */
1174 ctx->List.ListBase = 0;
1175
1176 /* Multisample */
1177 ctx->Multisample.Enabled = GL_FALSE;
1178 ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
1179 ctx->Multisample.SampleAlphaToOne = GL_FALSE;
1180 ctx->Multisample.SampleCoverage = GL_FALSE;
1181 ctx->Multisample.SampleCoverageValue = 1.0;
1182 ctx->Multisample.SampleCoverageInvert = GL_FALSE;
1183
1184 /* Pixel group */
1185 ctx->Pixel.RedBias = 0.0;
1186 ctx->Pixel.RedScale = 1.0;
1187 ctx->Pixel.GreenBias = 0.0;
1188 ctx->Pixel.GreenScale = 1.0;
1189 ctx->Pixel.BlueBias = 0.0;
1190 ctx->Pixel.BlueScale = 1.0;
1191 ctx->Pixel.AlphaBias = 0.0;
1192 ctx->Pixel.AlphaScale = 1.0;
1193 ctx->Pixel.DepthBias = 0.0;
1194 ctx->Pixel.DepthScale = 1.0;
1195 ctx->Pixel.IndexOffset = 0;
1196 ctx->Pixel.IndexShift = 0;
1197 ctx->Pixel.ZoomX = 1.0;
1198 ctx->Pixel.ZoomY = 1.0;
1199 ctx->Pixel.MapColorFlag = GL_FALSE;
1200 ctx->Pixel.MapStencilFlag = GL_FALSE;
1201 ctx->Pixel.MapStoSsize = 1;
1202 ctx->Pixel.MapItoIsize = 1;
1203 ctx->Pixel.MapItoRsize = 1;
1204 ctx->Pixel.MapItoGsize = 1;
1205 ctx->Pixel.MapItoBsize = 1;
1206 ctx->Pixel.MapItoAsize = 1;
1207 ctx->Pixel.MapRtoRsize = 1;
1208 ctx->Pixel.MapGtoGsize = 1;
1209 ctx->Pixel.MapBtoBsize = 1;
1210 ctx->Pixel.MapAtoAsize = 1;
1211 ctx->Pixel.MapStoS[0] = 0;
1212 ctx->Pixel.MapItoI[0] = 0;
1213 ctx->Pixel.MapItoR[0] = 0.0;
1214 ctx->Pixel.MapItoG[0] = 0.0;
1215 ctx->Pixel.MapItoB[0] = 0.0;
1216 ctx->Pixel.MapItoA[0] = 0.0;
1217 ctx->Pixel.MapItoR8[0] = 0;
1218 ctx->Pixel.MapItoG8[0] = 0;
1219 ctx->Pixel.MapItoB8[0] = 0;
1220 ctx->Pixel.MapItoA8[0] = 0;
1221 ctx->Pixel.MapRtoR[0] = 0.0;
1222 ctx->Pixel.MapGtoG[0] = 0.0;
1223 ctx->Pixel.MapBtoB[0] = 0.0;
1224 ctx->Pixel.MapAtoA[0] = 0.0;
1225 ctx->Pixel.HistogramEnabled = GL_FALSE;
1226 ctx->Pixel.MinMaxEnabled = GL_FALSE;
1227 ctx->Pixel.PixelTextureEnabled = GL_FALSE;
1228 ctx->Pixel.FragmentRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
1229 ctx->Pixel.FragmentAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
1230 ASSIGN_4V(ctx->Pixel.PostColorMatrixScale, 1.0, 1.0, 1.0, 1.0);
1231 ASSIGN_4V(ctx->Pixel.PostColorMatrixBias, 0.0, 0.0, 0.0, 0.0);
1232 ASSIGN_4V(ctx->Pixel.ColorTableScale, 1.0, 1.0, 1.0, 1.0);
1233 ASSIGN_4V(ctx->Pixel.ColorTableBias, 0.0, 0.0, 0.0, 0.0);
1234 ASSIGN_4V(ctx->Pixel.PCCTscale, 1.0, 1.0, 1.0, 1.0);
1235 ASSIGN_4V(ctx->Pixel.PCCTbias, 0.0, 0.0, 0.0, 0.0);
1236 ASSIGN_4V(ctx->Pixel.PCMCTscale, 1.0, 1.0, 1.0, 1.0);
1237 ASSIGN_4V(ctx->Pixel.PCMCTbias, 0.0, 0.0, 0.0, 0.0);
1238 ctx->Pixel.ColorTableEnabled = GL_FALSE;
1239 ctx->Pixel.PostConvolutionColorTableEnabled = GL_FALSE;
1240 ctx->Pixel.PostColorMatrixColorTableEnabled = GL_FALSE;
1241 ctx->Pixel.Convolution1DEnabled = GL_FALSE;
1242 ctx->Pixel.Convolution2DEnabled = GL_FALSE;
1243 ctx->Pixel.Separable2DEnabled = GL_FALSE;
1244 for (i = 0; i < 3; i++) {
1245 ASSIGN_4V(ctx->Pixel.ConvolutionBorderColor[i], 0.0, 0.0, 0.0, 0.0);
1246 ctx->Pixel.ConvolutionBorderMode[i] = GL_REDUCE;
1247 ASSIGN_4V(ctx->Pixel.ConvolutionFilterScale[i], 1.0, 1.0, 1.0, 1.0);
1248 ASSIGN_4V(ctx->Pixel.ConvolutionFilterBias[i], 0.0, 0.0, 0.0, 0.0);
1249 }
1250 for (i = 0; i < MAX_CONVOLUTION_WIDTH * MAX_CONVOLUTION_WIDTH * 4; i++) {
1251 ctx->Convolution1D.Filter[i] = 0.0;
1252 ctx->Convolution2D.Filter[i] = 0.0;
1253 ctx->Separable2D.Filter[i] = 0.0;
1254 }
1255 ASSIGN_4V(ctx->Pixel.PostConvolutionScale, 1.0, 1.0, 1.0, 1.0);
1256 ASSIGN_4V(ctx->Pixel.PostConvolutionBias, 0.0, 0.0, 0.0, 0.0);
1257
1258 /* Point group */
1259 ctx->Point.SmoothFlag = GL_FALSE;
1260 ctx->Point.Size = 1.0;
1261 ctx->Point._Size = 1.0;
1262 ctx->Point.Params[0] = 1.0;
1263 ctx->Point.Params[1] = 0.0;
1264 ctx->Point.Params[2] = 0.0;
1265 ctx->Point._Attenuated = GL_FALSE;
1266 ctx->Point.MinSize = 0.0;
1267 ctx->Point.MaxSize = ctx->Const.MaxPointSize;
1268 ctx->Point.Threshold = 1.0;
1269 ctx->Point.PointSprite = GL_FALSE; /* GL_NV_point_sprite */
1270 ctx->Point.SpriteRMode = GL_ZERO; /* GL_NV_point_sprite */
1271 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1272 ctx->Point.CoordReplace[i] = GL_FALSE; /* GL_NV_point_sprite */
1273 }
1274
1275 /* Polygon group */
1276 ctx->Polygon.CullFlag = GL_FALSE;
1277 ctx->Polygon.CullFaceMode = GL_BACK;
1278 ctx->Polygon.FrontFace = GL_CCW;
1279 ctx->Polygon._FrontBit = 0;
1280 ctx->Polygon.FrontMode = GL_FILL;
1281 ctx->Polygon.BackMode = GL_FILL;
1282 ctx->Polygon.SmoothFlag = GL_FALSE;
1283 ctx->Polygon.StippleFlag = GL_FALSE;
1284 ctx->Polygon.OffsetFactor = 0.0F;
1285 ctx->Polygon.OffsetUnits = 0.0F;
1286 ctx->Polygon.OffsetPoint = GL_FALSE;
1287 ctx->Polygon.OffsetLine = GL_FALSE;
1288 ctx->Polygon.OffsetFill = GL_FALSE;
1289
1290 /* Polygon Stipple group */
1291 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
1292
1293 /* Scissor group */
1294 ctx->Scissor.Enabled = GL_FALSE;
1295 ctx->Scissor.X = 0;
1296 ctx->Scissor.Y = 0;
1297 ctx->Scissor.Width = 0;
1298 ctx->Scissor.Height = 0;
1299
1300 /* Stencil group */
1301 ctx->Stencil.Enabled = GL_FALSE;
1302 ctx->Stencil.TestTwoSide = GL_FALSE;
1303 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 1 = GL_BACK */
1304 ctx->Stencil.Function[0] = GL_ALWAYS;
1305 ctx->Stencil.Function[1] = GL_ALWAYS;
1306 ctx->Stencil.FailFunc[0] = GL_KEEP;
1307 ctx->Stencil.FailFunc[1] = GL_KEEP;
1308 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
1309 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
1310 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
1311 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
1312 ctx->Stencil.Ref[0] = 0;
1313 ctx->Stencil.Ref[1] = 0;
1314 ctx->Stencil.ValueMask[0] = STENCIL_MAX;
1315 ctx->Stencil.ValueMask[1] = STENCIL_MAX;
1316 ctx->Stencil.WriteMask[0] = STENCIL_MAX;
1317 ctx->Stencil.WriteMask[1] = STENCIL_MAX;
1318 ctx->Stencil.Clear = 0;
1319
1320 /* Texture group */
1321 ctx->Texture.CurrentUnit = 0; /* multitexture */
1322 ctx->Texture._EnabledUnits = 0;
1323 for (i=0; i<MAX_TEXTURE_UNITS; i++)
1324 init_texture_unit( ctx, i );
1325 ctx->Texture.SharedPalette = GL_FALSE;
1326 _mesa_init_colortable(&ctx->Texture.Palette);
1327
1328 /* Transformation group */
1329 ctx->Transform.MatrixMode = GL_MODELVIEW;
1330 ctx->Transform.Normalize = GL_FALSE;
1331 ctx->Transform.RescaleNormals = GL_FALSE;
1332 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
1333 for (i=0;i<MAX_CLIP_PLANES;i++) {
1334 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1335 }
1336 ctx->Transform.ClipPlanesEnabled = 0;
1337
1338 /* Viewport group */
1339 ctx->Viewport.X = 0;
1340 ctx->Viewport.Y = 0;
1341 ctx->Viewport.Width = 0;
1342 ctx->Viewport.Height = 0;
1343 ctx->Viewport.Near = 0.0;
1344 ctx->Viewport.Far = 1.0;
1345 _math_matrix_ctr(&ctx->Viewport._WindowMap);
1346
1347 #define Sz 10
1348 #define Tz 14
1349 ctx->Viewport._WindowMap.m[Sz] = 0.5F * ctx->DepthMaxF;
1350 ctx->Viewport._WindowMap.m[Tz] = 0.5F * ctx->DepthMaxF;
1351 #undef Sz
1352 #undef Tz
1353
1354 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1355 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
1356
1357 /* Vertex arrays */
1358 ctx->Array.Vertex.Size = 4;
1359 ctx->Array.Vertex.Type = GL_FLOAT;
1360 ctx->Array.Vertex.Stride = 0;
1361 ctx->Array.Vertex.StrideB = 0;
1362 ctx->Array.Vertex.Ptr = NULL;
1363 ctx->Array.Vertex.Enabled = GL_FALSE;
1364 ctx->Array.Vertex.Flags = CA_CLIENT_DATA;
1365 ctx->Array.Normal.Type = GL_FLOAT;
1366 ctx->Array.Normal.Stride = 0;
1367 ctx->Array.Normal.StrideB = 0;
1368 ctx->Array.Normal.Ptr = NULL;
1369 ctx->Array.Normal.Enabled = GL_FALSE;
1370 ctx->Array.Normal.Flags = CA_CLIENT_DATA;
1371 ctx->Array.Color.Size = 4;
1372 ctx->Array.Color.Type = GL_FLOAT;
1373 ctx->Array.Color.Stride = 0;
1374 ctx->Array.Color.StrideB = 0;
1375 ctx->Array.Color.Ptr = NULL;
1376 ctx->Array.Color.Enabled = GL_FALSE;
1377 ctx->Array.Color.Flags = CA_CLIENT_DATA;
1378 ctx->Array.SecondaryColor.Size = 4;
1379 ctx->Array.SecondaryColor.Type = GL_FLOAT;
1380 ctx->Array.SecondaryColor.Stride = 0;
1381 ctx->Array.SecondaryColor.StrideB = 0;
1382 ctx->Array.SecondaryColor.Ptr = NULL;
1383 ctx->Array.SecondaryColor.Enabled = GL_FALSE;
1384 ctx->Array.SecondaryColor.Flags = CA_CLIENT_DATA;
1385 ctx->Array.FogCoord.Size = 1;
1386 ctx->Array.FogCoord.Type = GL_FLOAT;
1387 ctx->Array.FogCoord.Stride = 0;
1388 ctx->Array.FogCoord.StrideB = 0;
1389 ctx->Array.FogCoord.Ptr = NULL;
1390 ctx->Array.FogCoord.Enabled = GL_FALSE;
1391 ctx->Array.FogCoord.Flags = CA_CLIENT_DATA;
1392 ctx->Array.Index.Type = GL_FLOAT;
1393 ctx->Array.Index.Stride = 0;
1394 ctx->Array.Index.StrideB = 0;
1395 ctx->Array.Index.Ptr = NULL;
1396 ctx->Array.Index.Enabled = GL_FALSE;
1397 ctx->Array.Index.Flags = CA_CLIENT_DATA;
1398 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1399 ctx->Array.TexCoord[i].Size = 4;
1400 ctx->Array.TexCoord[i].Type = GL_FLOAT;
1401 ctx->Array.TexCoord[i].Stride = 0;
1402 ctx->Array.TexCoord[i].StrideB = 0;
1403 ctx->Array.TexCoord[i].Ptr = NULL;
1404 ctx->Array.TexCoord[i].Enabled = GL_FALSE;
1405 ctx->Array.TexCoord[i].Flags = CA_CLIENT_DATA;
1406 }
1407 ctx->Array.TexCoordInterleaveFactor = 1;
1408 ctx->Array.EdgeFlag.Stride = 0;
1409 ctx->Array.EdgeFlag.StrideB = 0;
1410 ctx->Array.EdgeFlag.Ptr = NULL;
1411 ctx->Array.EdgeFlag.Enabled = GL_FALSE;
1412 ctx->Array.EdgeFlag.Flags = CA_CLIENT_DATA;
1413 ctx->Array.ActiveTexture = 0; /* GL_ARB_multitexture */
1414
1415 /* Pixel transfer */
1416 ctx->Pack.Alignment = 4;
1417 ctx->Pack.RowLength = 0;
1418 ctx->Pack.ImageHeight = 0;
1419 ctx->Pack.SkipPixels = 0;
1420 ctx->Pack.SkipRows = 0;
1421 ctx->Pack.SkipImages = 0;
1422 ctx->Pack.SwapBytes = GL_FALSE;
1423 ctx->Pack.LsbFirst = GL_FALSE;
1424 ctx->Unpack.Alignment = 4;
1425 ctx->Unpack.RowLength = 0;
1426 ctx->Unpack.ImageHeight = 0;
1427 ctx->Unpack.SkipPixels = 0;
1428 ctx->Unpack.SkipRows = 0;
1429 ctx->Unpack.SkipImages = 0;
1430 ctx->Unpack.SwapBytes = GL_FALSE;
1431 ctx->Unpack.LsbFirst = GL_FALSE;
1432
1433 /* Feedback */
1434 ctx->Feedback.Type = GL_2D; /* TODO: verify */
1435 ctx->Feedback.Buffer = NULL;
1436 ctx->Feedback.BufferSize = 0;
1437 ctx->Feedback.Count = 0;
1438
1439 /* Selection/picking */
1440 ctx->Select.Buffer = NULL;
1441 ctx->Select.BufferSize = 0;
1442 ctx->Select.BufferCount = 0;
1443 ctx->Select.Hits = 0;
1444 ctx->Select.NameStackDepth = 0;
1445
1446 /* Renderer and client attribute stacks */
1447 ctx->AttribStackDepth = 0;
1448 ctx->ClientAttribStackDepth = 0;
1449
1450 /* Display list */
1451 ctx->CallDepth = 0;
1452 ctx->ExecuteFlag = GL_TRUE;
1453 ctx->CompileFlag = GL_FALSE;
1454 ctx->CurrentListPtr = NULL;
1455 ctx->CurrentBlock = NULL;
1456 ctx->CurrentListNum = 0;
1457 ctx->CurrentPos = 0;
1458
1459 /* Color tables */
1460 _mesa_init_colortable(&ctx->ColorTable);
1461 _mesa_init_colortable(&ctx->ProxyColorTable);
1462 _mesa_init_colortable(&ctx->PostConvolutionColorTable);
1463 _mesa_init_colortable(&ctx->ProxyPostConvolutionColorTable);
1464 _mesa_init_colortable(&ctx->PostColorMatrixColorTable);
1465 _mesa_init_colortable(&ctx->ProxyPostColorMatrixColorTable);
1466
1467 /* GL_NV_vertex_program */
1468 ctx->VertexProgram.Enabled = GL_FALSE;
1469 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
1470 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
1471 ctx->VertexProgram.CurrentID = 0;
1472 ctx->VertexProgram.ErrorPos = -1;
1473 ctx->VertexProgram.Current = NULL;
1474 for (i = 0; i < VP_NUM_PROG_REGS / 4; i++) {
1475 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
1476 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
1477 }
1478
1479 /* Miscellaneous */
1480 ctx->NewState = _NEW_ALL;
1481 ctx->RenderMode = GL_RENDER;
1482 ctx->_ImageTransferState = 0;
1483
1484 ctx->_NeedNormals = 0;
1485 ctx->_NeedEyeCoords = 0;
1486 ctx->_ModelViewInvScale = 1.0;
1487
1488 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1489
1490 ctx->CatchSignals = GL_TRUE;
1491 ctx->OcclusionResult = GL_FALSE;
1492 ctx->OcclusionResultSaved = GL_FALSE;
1493 ctx->_Facing = 0;
1494
1495 /* For debug/development only */
1496 ctx->NoRaster = ctx->imports.getenv(ctx, "MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
1497 ctx->FirstTimeCurrent = GL_TRUE;
1498
1499 /* Dither disable */
1500 ctx->NoDither = ctx->imports.getenv(ctx, "MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
1501 if (ctx->NoDither) {
1502 if (ctx->imports.getenv(ctx, "MESA_DEBUG")) {
1503 _mesa_debug(ctx, "MESA_NO_DITHER set - dithering disabled\n");
1504 }
1505 ctx->Color.DitherFlag = GL_FALSE;
1506 }
1507 }
1508
1509
1510
1511
1512 /*
1513 * Allocate the proxy textures. If we run out of memory part way through
1514 * the allocations clean up and return GL_FALSE.
1515 * Return: GL_TRUE=success, GL_FALSE=failure
1516 */
1517 static GLboolean
1518 alloc_proxy_textures( GLcontext *ctx )
1519 {
1520 GLboolean out_of_memory;
1521 GLint i;
1522
1523 ctx->Texture.Proxy1D = _mesa_alloc_texture_object(NULL, 0, GL_TEXTURE_1D);
1524 if (!ctx->Texture.Proxy1D) {
1525 return GL_FALSE;
1526 }
1527
1528 ctx->Texture.Proxy2D = _mesa_alloc_texture_object(NULL, 0, GL_TEXTURE_2D);
1529 if (!ctx->Texture.Proxy2D) {
1530 _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
1531 return GL_FALSE;
1532 }
1533
1534 ctx->Texture.Proxy3D = _mesa_alloc_texture_object(NULL, 0, GL_TEXTURE_3D);
1535 if (!ctx->Texture.Proxy3D) {
1536 _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
1537 _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
1538 return GL_FALSE;
1539 }
1540
1541 ctx->Texture.ProxyCubeMap = _mesa_alloc_texture_object(NULL, 0,
1542 GL_TEXTURE_CUBE_MAP_ARB);
1543 if (!ctx->Texture.ProxyCubeMap) {
1544 _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
1545 _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
1546 _mesa_free_texture_object(NULL, ctx->Texture.Proxy3D);
1547 return GL_FALSE;
1548 }
1549
1550 ctx->Texture.ProxyRect = _mesa_alloc_texture_object(NULL, 0,
1551 GL_TEXTURE_RECTANGLE_NV);
1552 if (!ctx->Texture.ProxyRect) {
1553 _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
1554 _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
1555 _mesa_free_texture_object(NULL, ctx->Texture.Proxy3D);
1556 _mesa_free_texture_object(NULL, ctx->Texture.ProxyCubeMap);
1557 return GL_FALSE;
1558 }
1559
1560 out_of_memory = GL_FALSE;
1561 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1562 ctx->Texture.Proxy1D->Image[i] = _mesa_alloc_texture_image();
1563 ctx->Texture.Proxy2D->Image[i] = _mesa_alloc_texture_image();
1564 ctx->Texture.Proxy3D->Image[i] = _mesa_alloc_texture_image();
1565 ctx->Texture.ProxyCubeMap->Image[i] = _mesa_alloc_texture_image();
1566 if (!ctx->Texture.Proxy1D->Image[i]
1567 || !ctx->Texture.Proxy2D->Image[i]
1568 || !ctx->Texture.Proxy3D->Image[i]
1569 || !ctx->Texture.ProxyCubeMap->Image[i]) {
1570 out_of_memory = GL_TRUE;
1571 }
1572 }
1573 ctx->Texture.ProxyRect->Image[0] = _mesa_alloc_texture_image();
1574 if (!ctx->Texture.ProxyRect->Image[0])
1575 out_of_memory = GL_TRUE;
1576
1577 if (out_of_memory) {
1578 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1579 if (ctx->Texture.Proxy1D->Image[i]) {
1580 _mesa_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
1581 }
1582 if (ctx->Texture.Proxy2D->Image[i]) {
1583 _mesa_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
1584 }
1585 if (ctx->Texture.Proxy3D->Image[i]) {
1586 _mesa_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
1587 }
1588 if (ctx->Texture.ProxyCubeMap->Image[i]) {
1589 _mesa_free_texture_image(ctx->Texture.ProxyCubeMap->Image[i]);
1590 }
1591 }
1592 if (ctx->Texture.ProxyRect->Image[0]) {
1593 _mesa_free_texture_image(ctx->Texture.ProxyRect->Image[0]);
1594 }
1595 _mesa_free_texture_object(NULL, ctx->Texture.Proxy1D);
1596 _mesa_free_texture_object(NULL, ctx->Texture.Proxy2D);
1597 _mesa_free_texture_object(NULL, ctx->Texture.Proxy3D);
1598 _mesa_free_texture_object(NULL, ctx->Texture.ProxyCubeMap);
1599 _mesa_free_texture_object(NULL, ctx->Texture.ProxyRect);
1600 return GL_FALSE;
1601 }
1602 else {
1603 return GL_TRUE;
1604 }
1605 }
1606
1607
1608 static void add_debug_flags( const char *debug )
1609 {
1610 #ifdef MESA_DEBUG
1611 if (strstr(debug, "varray"))
1612 MESA_VERBOSE |= VERBOSE_VARRAY;
1613
1614 if (strstr(debug, "tex"))
1615 MESA_VERBOSE |= VERBOSE_TEXTURE;
1616
1617 if (strstr(debug, "imm"))
1618 MESA_VERBOSE |= VERBOSE_IMMEDIATE;
1619
1620 if (strstr(debug, "pipe"))
1621 MESA_VERBOSE |= VERBOSE_PIPELINE;
1622
1623 if (strstr(debug, "driver"))
1624 MESA_VERBOSE |= VERBOSE_DRIVER;
1625
1626 if (strstr(debug, "state"))
1627 MESA_VERBOSE |= VERBOSE_STATE;
1628
1629 if (strstr(debug, "api"))
1630 MESA_VERBOSE |= VERBOSE_API;
1631
1632 if (strstr(debug, "list"))
1633 MESA_VERBOSE |= VERBOSE_DISPLAY_LIST;
1634
1635 if (strstr(debug, "lighting"))
1636 MESA_VERBOSE |= VERBOSE_LIGHTING;
1637
1638 /* Debug flag:
1639 */
1640 if (strstr(debug, "flush"))
1641 MESA_DEBUG_FLAGS |= DEBUG_ALWAYS_FLUSH;
1642 #endif
1643 }
1644
1645
1646 /*
1647 * Initialize a GLcontext struct. This includes allocating all the
1648 * other structs and arrays which hang off of the context by pointers.
1649 */
1650 GLboolean
1651 _mesa_initialize_context( GLcontext *ctx,
1652 const GLvisual *visual,
1653 GLcontext *share_list,
1654 const __GLimports *imports )
1655 {
1656 GLuint dispatchSize;
1657
1658 ASSERT(imports);
1659 ASSERT(imports->other); /* other points to the device driver's context */
1660
1661 /* assing imports */
1662 ctx->imports = *imports;
1663
1664 /* initialize the exports (Mesa functions called by the window system) */
1665 _mesa_init_default_exports( &(ctx->exports) );
1666
1667 /* misc one-time initializations */
1668 one_time_init(ctx);
1669
1670 #if 0
1671 /**
1672 ** OpenGL SI stuff
1673 **/
1674 if (!ctx->imports.malloc) {
1675 _mesa_init_default_imports(&ctx->imports, driver_ctx);
1676 }
1677 /* exports are setup by the device driver */
1678 #endif
1679
1680 ctx->DriverCtx = imports->other;
1681 ctx->Visual = *visual;
1682 ctx->DrawBuffer = NULL;
1683 ctx->ReadBuffer = NULL;
1684
1685 if (share_list) {
1686 /* share state with another context */
1687 ctx->Shared = share_list->Shared;
1688 }
1689 else {
1690 /* allocate new, unshared state */
1691 ctx->Shared = alloc_shared_state();
1692 if (!ctx->Shared) {
1693 return GL_FALSE;
1694 }
1695 }
1696 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1697 ctx->Shared->RefCount++;
1698 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1699
1700 /* Effectively bind the default textures to all texture units */
1701 ctx->Shared->Default1D->RefCount += MAX_TEXTURE_UNITS;
1702 ctx->Shared->Default2D->RefCount += MAX_TEXTURE_UNITS;
1703 ctx->Shared->Default3D->RefCount += MAX_TEXTURE_UNITS;
1704 ctx->Shared->DefaultCubeMap->RefCount += MAX_TEXTURE_UNITS;
1705 ctx->Shared->DefaultRect->RefCount += MAX_TEXTURE_UNITS;
1706
1707 init_attrib_groups( ctx );
1708
1709 if (visual->doubleBufferMode) {
1710 ctx->Color.DrawBuffer = GL_BACK;
1711 ctx->Color._DriverDrawBuffer = GL_BACK_LEFT;
1712 ctx->Color._DrawDestMask = BACK_LEFT_BIT;
1713 ctx->Pixel.ReadBuffer = GL_BACK;
1714 ctx->Pixel._DriverReadBuffer = GL_BACK_LEFT;
1715 }
1716 else {
1717 ctx->Color.DrawBuffer = GL_FRONT;
1718 ctx->Color._DriverDrawBuffer = GL_FRONT_LEFT;
1719 ctx->Color._DrawDestMask = FRONT_LEFT_BIT;
1720 ctx->Pixel.ReadBuffer = GL_FRONT;
1721 ctx->Pixel._DriverReadBuffer = GL_FRONT_LEFT;
1722 }
1723
1724 if (!alloc_proxy_textures(ctx)) {
1725 free_shared_state(ctx, ctx->Shared);
1726 return GL_FALSE;
1727 }
1728
1729 /* register the most recent extension functions with libGL */
1730 _glapi_add_entrypoint("glTbufferMask3DFX", 553);
1731 _glapi_add_entrypoint("glCompressedTexImage3DARB", 554);
1732 _glapi_add_entrypoint("glCompressedTexImage2DARB", 555);
1733 _glapi_add_entrypoint("glCompressedTexImage1DARB", 556);
1734 _glapi_add_entrypoint("glCompressedTexSubImage3DARB", 557);
1735 _glapi_add_entrypoint("glCompressedTexSubImage2DARB", 558);
1736 _glapi_add_entrypoint("glCompressedTexSubImage1DARB", 559);
1737 _glapi_add_entrypoint("glGetCompressedTexImageARB", 560);
1738 /* XXX we should add a bunch of new functions here */
1739
1740
1741 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1742 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1743 * Mesa we do this to accomodate different versions of libGL and various
1744 * DRI drivers.
1745 */
1746 dispatchSize = MAX2(_glapi_get_dispatch_table_size(),
1747 sizeof(struct _glapi_table) / sizeof(void *));
1748
1749 /* setup API dispatch tables */
1750 ctx->Exec = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
1751 ctx->Save = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
1752 if (!ctx->Exec || !ctx->Save) {
1753 free_shared_state(ctx, ctx->Shared);
1754 if (ctx->Exec)
1755 FREE( ctx->Exec );
1756 }
1757 _mesa_init_exec_table(ctx->Exec, dispatchSize);
1758 _mesa_init_dlist_table(ctx->Save, dispatchSize);
1759 ctx->CurrentDispatch = ctx->Exec;
1760
1761 ctx->ExecPrefersFloat = GL_FALSE;
1762 ctx->SavePrefersFloat = GL_FALSE;
1763
1764 /* Neutral tnl module stuff */
1765 _mesa_init_exec_vtxfmt( ctx );
1766 ctx->TnlModule.Current = NULL;
1767 ctx->TnlModule.SwapCount = 0;
1768
1769 /* Z buffer stuff */
1770 if (ctx->Visual.depthBits == 0) {
1771 /* Special case. Even if we don't have a depth buffer we need
1772 * good values for DepthMax for Z vertex transformation purposes
1773 * and for per-fragment fog computation.
1774 */
1775 ctx->DepthMax = 1 << 16;
1776 ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
1777 }
1778 else if (ctx->Visual.depthBits < 32) {
1779 ctx->DepthMax = (1 << ctx->Visual.depthBits) - 1;
1780 ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
1781 }
1782 else {
1783 /* Special case since shift values greater than or equal to the
1784 * number of bits in the left hand expression's type are undefined.
1785 */
1786 ctx->DepthMax = 0xffffffff;
1787 ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
1788 }
1789 ctx->MRD = 1.0; /* Minimum resolvable depth value, for polygon offset */
1790
1791
1792 #if defined(MESA_TRACE)
1793 ctx->TraceCtx = (trace_context_t *) CALLOC( sizeof(trace_context_t) );
1794 #if 0
1795 /* Brian: do you want to have CreateContext fail here,
1796 or should we just trap in NewTrace (currently done)? */
1797 if (!(ctx->TraceCtx)) {
1798 free_shared_state(ctx, ctx->Shared);
1799 FREE( ctx->Exec );
1800 FREE( ctx->Save );
1801 return GL_FALSE;
1802 }
1803 #endif
1804 trInitContext(ctx->TraceCtx);
1805
1806 ctx->TraceDispatch = (struct _glapi_table *)
1807 CALLOC(dispatchSize * sizeof(void*));
1808 #if 0
1809 if (!(ctx->TraceCtx)) {
1810 free_shared_state(ctx, ctx->Shared);
1811 FREE( ctx->Exec );
1812 FREE( ctx->Save );
1813 FREE( ctx->TraceCtx );
1814 return GL_FALSE;
1815 }
1816 #endif
1817 trInitDispatch(ctx->TraceDispatch);
1818 #endif
1819
1820
1821 if (ctx->imports.getenv(ctx, "MESA_DEBUG"))
1822 add_debug_flags(ctx->imports.getenv(ctx, "MESA_DEBUG"));
1823
1824 if (ctx->imports.getenv(ctx, "MESA_VERBOSE"))
1825 add_debug_flags(ctx->imports.getenv(ctx, "MESA_VERBOSE"));
1826
1827 return GL_TRUE;
1828 }
1829
1830
1831
1832 /*
1833 * Allocate and initialize a GLcontext structure.
1834 * Input: visual - a GLvisual pointer (we copy the struct contents)
1835 * sharelist - another context to share display lists with or NULL
1836 * imports - points to a fully-initialized __GLimports object.
1837 * Return: pointer to a new __GLcontextRec or NULL if error.
1838 */
1839 GLcontext *
1840 _mesa_create_context( const GLvisual *visual,
1841 GLcontext *share_list,
1842 const __GLimports *imports )
1843 {
1844 GLcontext *ctx;
1845
1846 ASSERT(visual);
1847 ASSERT(imports);
1848 ASSERT(imports->calloc);
1849
1850 ctx = (GLcontext *) imports->calloc(NULL, 1, sizeof(GLcontext));
1851 if (!ctx)
1852 return NULL;
1853
1854 ctx->Driver.CurrentExecPrimitive = 0; /* XXX why is this here??? */
1855
1856 if (_mesa_initialize_context(ctx, visual, share_list, imports)) {
1857 return ctx;
1858 }
1859 else {
1860 imports->free(NULL, ctx);
1861 return NULL;
1862 }
1863 }
1864
1865
1866
1867 /*
1868 * Free the data associated with the given context.
1869 * But don't free() the GLcontext struct itself!
1870 */
1871 void
1872 _mesa_free_context_data( GLcontext *ctx )
1873 {
1874 struct gl_shine_tab *s, *tmps;
1875 GLuint i;
1876
1877 /* if we're destroying the current context, unbind it first */
1878 if (ctx == _mesa_get_current_context()) {
1879 _mesa_make_current(NULL, NULL);
1880 }
1881
1882 /*
1883 * Free transformation matrix stacks
1884 */
1885 free_matrix_stack(&ctx->ModelviewMatrixStack);
1886 free_matrix_stack(&ctx->ProjectionMatrixStack);
1887 free_matrix_stack(&ctx->ColorMatrixStack);
1888 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
1889 free_matrix_stack(&ctx->TextureMatrixStack[i]);
1890 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
1891 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
1892 /* combined Modelview*Projection matrix */
1893 _math_matrix_dtr( &ctx->_ModelProjectMatrix );
1894
1895
1896 if (ctx->VertexProgram.Current) {
1897 ctx->VertexProgram.Current->RefCount--;
1898 if (ctx->VertexProgram.Current->RefCount <= 0)
1899 _mesa_delete_program(ctx, ctx->VertexProgram.CurrentID);
1900 }
1901
1902 /* Shared context state (display lists, textures, etc) */
1903 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1904 ctx->Shared->RefCount--;
1905 assert(ctx->Shared->RefCount >= 0);
1906 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1907 if (ctx->Shared->RefCount == 0) {
1908 /* free shared state */
1909 free_shared_state( ctx, ctx->Shared );
1910 }
1911
1912 /* Free lighting shininess exponentiation table */
1913 foreach_s( s, tmps, ctx->_ShineTabList ) {
1914 FREE( s );
1915 }
1916 FREE( ctx->_ShineTabList );
1917
1918 /* Free proxy texture objects */
1919 _mesa_free_texture_object( NULL, ctx->Texture.Proxy1D );
1920 _mesa_free_texture_object( NULL, ctx->Texture.Proxy2D );
1921 _mesa_free_texture_object( NULL, ctx->Texture.Proxy3D );
1922 _mesa_free_texture_object( NULL, ctx->Texture.ProxyCubeMap );
1923 _mesa_free_texture_object( NULL, ctx->Texture.ProxyRect );
1924
1925 /* Free evaluator data */
1926 if (ctx->EvalMap.Map1Vertex3.Points)
1927 FREE( ctx->EvalMap.Map1Vertex3.Points );
1928 if (ctx->EvalMap.Map1Vertex4.Points)
1929 FREE( ctx->EvalMap.Map1Vertex4.Points );
1930 if (ctx->EvalMap.Map1Index.Points)
1931 FREE( ctx->EvalMap.Map1Index.Points );
1932 if (ctx->EvalMap.Map1Color4.Points)
1933 FREE( ctx->EvalMap.Map1Color4.Points );
1934 if (ctx->EvalMap.Map1Normal.Points)
1935 FREE( ctx->EvalMap.Map1Normal.Points );
1936 if (ctx->EvalMap.Map1Texture1.Points)
1937 FREE( ctx->EvalMap.Map1Texture1.Points );
1938 if (ctx->EvalMap.Map1Texture2.Points)
1939 FREE( ctx->EvalMap.Map1Texture2.Points );
1940 if (ctx->EvalMap.Map1Texture3.Points)
1941 FREE( ctx->EvalMap.Map1Texture3.Points );
1942 if (ctx->EvalMap.Map1Texture4.Points)
1943 FREE( ctx->EvalMap.Map1Texture4.Points );
1944 for (i = 0; i < 16; i++)
1945 FREE((ctx->EvalMap.Map1Attrib[i].Points));
1946
1947 if (ctx->EvalMap.Map2Vertex3.Points)
1948 FREE( ctx->EvalMap.Map2Vertex3.Points );
1949 if (ctx->EvalMap.Map2Vertex4.Points)
1950 FREE( ctx->EvalMap.Map2Vertex4.Points );
1951 if (ctx->EvalMap.Map2Index.Points)
1952 FREE( ctx->EvalMap.Map2Index.Points );
1953 if (ctx->EvalMap.Map2Color4.Points)
1954 FREE( ctx->EvalMap.Map2Color4.Points );
1955 if (ctx->EvalMap.Map2Normal.Points)
1956 FREE( ctx->EvalMap.Map2Normal.Points );
1957 if (ctx->EvalMap.Map2Texture1.Points)
1958 FREE( ctx->EvalMap.Map2Texture1.Points );
1959 if (ctx->EvalMap.Map2Texture2.Points)
1960 FREE( ctx->EvalMap.Map2Texture2.Points );
1961 if (ctx->EvalMap.Map2Texture3.Points)
1962 FREE( ctx->EvalMap.Map2Texture3.Points );
1963 if (ctx->EvalMap.Map2Texture4.Points)
1964 FREE( ctx->EvalMap.Map2Texture4.Points );
1965 for (i = 0; i < 16; i++)
1966 FREE((ctx->EvalMap.Map2Attrib[i].Points));
1967
1968 _mesa_free_colortable_data( &ctx->ColorTable );
1969 _mesa_free_colortable_data( &ctx->PostConvolutionColorTable );
1970 _mesa_free_colortable_data( &ctx->PostColorMatrixColorTable );
1971 _mesa_free_colortable_data( &ctx->Texture.Palette );
1972
1973 _math_matrix_dtr(&ctx->Viewport._WindowMap);
1974
1975 _mesa_extensions_dtr(ctx);
1976
1977 FREE(ctx->Exec);
1978 FREE(ctx->Save);
1979 }
1980
1981
1982
1983 /*
1984 * Destroy a GLcontext structure.
1985 */
1986 void
1987 _mesa_destroy_context( GLcontext *ctx )
1988 {
1989 if (ctx) {
1990 _mesa_free_context_data(ctx);
1991 FREE( (void *) ctx );
1992 }
1993 }
1994
1995
1996
1997 /*
1998 * Copy attribute groups from one context to another.
1999 * Input: src - source context
2000 * dst - destination context
2001 * mask - bitwise OR of GL_*_BIT flags
2002 */
2003 void
2004 _mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
2005 {
2006 if (mask & GL_ACCUM_BUFFER_BIT) {
2007 /* OK to memcpy */
2008 dst->Accum = src->Accum;
2009 }
2010 if (mask & GL_COLOR_BUFFER_BIT) {
2011 /* OK to memcpy */
2012 dst->Color = src->Color;
2013 }
2014 if (mask & GL_CURRENT_BIT) {
2015 /* OK to memcpy */
2016 dst->Current = src->Current;
2017 }
2018 if (mask & GL_DEPTH_BUFFER_BIT) {
2019 /* OK to memcpy */
2020 dst->Depth = src->Depth;
2021 }
2022 if (mask & GL_ENABLE_BIT) {
2023 /* no op */
2024 }
2025 if (mask & GL_EVAL_BIT) {
2026 /* OK to memcpy */
2027 dst->Eval = src->Eval;
2028 }
2029 if (mask & GL_FOG_BIT) {
2030 /* OK to memcpy */
2031 dst->Fog = src->Fog;
2032 }
2033 if (mask & GL_HINT_BIT) {
2034 /* OK to memcpy */
2035 dst->Hint = src->Hint;
2036 }
2037 if (mask & GL_LIGHTING_BIT) {
2038 GLuint i;
2039 /* begin with memcpy */
2040 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
2041 /* fixup linked lists to prevent pointer insanity */
2042 make_empty_list( &(dst->Light.EnabledList) );
2043 for (i = 0; i < MAX_LIGHTS; i++) {
2044 if (dst->Light.Light[i].Enabled) {
2045 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
2046 }
2047 }
2048 }
2049 if (mask & GL_LINE_BIT) {
2050 /* OK to memcpy */
2051 dst->Line = src->Line;
2052 }
2053 if (mask & GL_LIST_BIT) {
2054 /* OK to memcpy */
2055 dst->List = src->List;
2056 }
2057 if (mask & GL_PIXEL_MODE_BIT) {
2058 /* OK to memcpy */
2059 dst->Pixel = src->Pixel;
2060 }
2061 if (mask & GL_POINT_BIT) {
2062 /* OK to memcpy */
2063 dst->Point = src->Point;
2064 }
2065 if (mask & GL_POLYGON_BIT) {
2066 /* OK to memcpy */
2067 dst->Polygon = src->Polygon;
2068 }
2069 if (mask & GL_POLYGON_STIPPLE_BIT) {
2070 /* Use loop instead of MEMCPY due to problem with Portland Group's
2071 * C compiler. Reported by John Stone.
2072 */
2073 GLuint i;
2074 for (i = 0; i < 32; i++) {
2075 dst->PolygonStipple[i] = src->PolygonStipple[i];
2076 }
2077 }
2078 if (mask & GL_SCISSOR_BIT) {
2079 /* OK to memcpy */
2080 dst->Scissor = src->Scissor;
2081 }
2082 if (mask & GL_STENCIL_BUFFER_BIT) {
2083 /* OK to memcpy */
2084 dst->Stencil = src->Stencil;
2085 }
2086 if (mask & GL_TEXTURE_BIT) {
2087 /* Cannot memcpy because of pointers */
2088 _mesa_copy_texture_state(src, dst);
2089 }
2090 if (mask & GL_TRANSFORM_BIT) {
2091 /* OK to memcpy */
2092 dst->Transform = src->Transform;
2093 }
2094 if (mask & GL_VIEWPORT_BIT) {
2095 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
2096 dst->Viewport.X = src->Viewport.X;
2097 dst->Viewport.Y = src->Viewport.Y;
2098 dst->Viewport.Width = src->Viewport.Width;
2099 dst->Viewport.Height = src->Viewport.Height;
2100 dst->Viewport.Near = src->Viewport.Near;
2101 dst->Viewport.Far = src->Viewport.Far;
2102 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
2103 }
2104
2105 /* XXX FIXME: Call callbacks?
2106 */
2107 dst->NewState = _NEW_ALL;
2108 }
2109
2110
2111
2112 static void print_info( void )
2113 {
2114 _mesa_debug(NULL, "Mesa GL_VERSION = %s\n",
2115 (char *) _mesa_GetString(GL_VERSION));
2116 _mesa_debug(NULL, "Mesa GL_RENDERER = %s\n",
2117 (char *) _mesa_GetString(GL_RENDERER));
2118 _mesa_debug(NULL, "Mesa GL_VENDOR = %s\n",
2119 (char *) _mesa_GetString(GL_VENDOR));
2120 _mesa_debug(NULL, "Mesa GL_EXTENSIONS = %s\n",
2121 (char *) _mesa_GetString(GL_EXTENSIONS));
2122 #if defined(THREADS)
2123 _mesa_debug(NULL, "Mesa thread-safe: YES\n");
2124 #else
2125 _mesa_debug(NULL, "Mesa thread-safe: NO\n");
2126 #endif
2127 #if defined(USE_X86_ASM)
2128 _mesa_debug(NULL, "Mesa x86-optimized: YES\n");
2129 #else
2130 _mesa_debug(NULL, "Mesa x86-optimized: NO\n");
2131 #endif
2132 #if defined(USE_SPARC_ASM)
2133 _mesa_debug(NULL, "Mesa sparc-optimized: YES\n");
2134 #else
2135 _mesa_debug(NULL, "Mesa sparc-optimized: NO\n");
2136 #endif
2137 }
2138
2139
2140 /*
2141 * Set the current context, binding the given frame buffer to the context.
2142 */
2143 void
2144 _mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
2145 {
2146 _mesa_make_current2( newCtx, buffer, buffer );
2147 }
2148
2149
2150 /*
2151 * Bind the given context to the given draw-buffer and read-buffer
2152 * and make it the current context for this thread.
2153 */
2154 void
2155 _mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
2156 GLframebuffer *readBuffer )
2157 {
2158 if (MESA_VERBOSE)
2159 _mesa_debug(newCtx, "_mesa_make_current2()\n");
2160
2161 /* Check that the context's and framebuffer's visuals are compatible.
2162 * We could do a lot more checking here but this'll catch obvious
2163 * problems.
2164 */
2165 if (newCtx && drawBuffer && readBuffer) {
2166 if (newCtx->Visual.rgbMode != drawBuffer->Visual.rgbMode ||
2167 newCtx->Visual.redBits != drawBuffer->Visual.redBits ||
2168 newCtx->Visual.depthBits != drawBuffer->Visual.depthBits ||
2169 newCtx->Visual.stencilBits != drawBuffer->Visual.stencilBits ||
2170 newCtx->Visual.accumRedBits != drawBuffer->Visual.accumRedBits) {
2171 return; /* incompatible */
2172 }
2173 }
2174
2175 /* We call this function periodically (just here for now) in
2176 * order to detect when multithreading has begun.
2177 */
2178 _glapi_check_multithread();
2179
2180 _glapi_set_context((void *) newCtx);
2181 ASSERT(_mesa_get_current_context() == newCtx);
2182
2183
2184 if (!newCtx) {
2185 _glapi_set_dispatch(NULL); /* none current */
2186 }
2187 else {
2188 _glapi_set_dispatch(newCtx->CurrentDispatch);
2189
2190 if (drawBuffer && readBuffer) {
2191 /* TODO: check if newCtx and buffer's visual match??? */
2192 newCtx->DrawBuffer = drawBuffer;
2193 newCtx->ReadBuffer = readBuffer;
2194 newCtx->NewState |= _NEW_BUFFERS;
2195
2196 if (drawBuffer->Width == 0 && drawBuffer->Height == 0) {
2197 /* get initial window size */
2198 GLuint bufWidth, bufHeight;
2199
2200 /* ask device driver for size of output buffer */
2201 (*newCtx->Driver.GetBufferSize)( drawBuffer, &bufWidth, &bufHeight );
2202
2203 if (drawBuffer->Width == bufWidth && drawBuffer->Height == bufHeight)
2204 return; /* size is as expected */
2205
2206 drawBuffer->Width = bufWidth;
2207 drawBuffer->Height = bufHeight;
2208
2209 newCtx->Driver.ResizeBuffers( drawBuffer );
2210 }
2211
2212 if (readBuffer != drawBuffer &&
2213 readBuffer->Width == 0 && readBuffer->Height == 0) {
2214 /* get initial window size */
2215 GLuint bufWidth, bufHeight;
2216
2217 /* ask device driver for size of output buffer */
2218 (*newCtx->Driver.GetBufferSize)( readBuffer, &bufWidth, &bufHeight );
2219
2220 if (readBuffer->Width == bufWidth && readBuffer->Height == bufHeight)
2221 return; /* size is as expected */
2222
2223 readBuffer->Width = bufWidth;
2224 readBuffer->Height = bufHeight;
2225
2226 newCtx->Driver.ResizeBuffers( readBuffer );
2227 }
2228 }
2229
2230 /* This is only for T&L - a bit out of place, or misnamed (BP) */
2231 if (newCtx->Driver.MakeCurrent)
2232 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
2233
2234 /* We can use this to help debug user's problems. Tell them to set
2235 * the MESA_INFO env variable before running their app. Then the
2236 * first time each context is made current we'll print some useful
2237 * information.
2238 */
2239 if (newCtx->FirstTimeCurrent) {
2240 if (newCtx->imports.getenv(newCtx, "MESA_INFO")) {
2241 print_info();
2242 }
2243 newCtx->FirstTimeCurrent = GL_FALSE;
2244 }
2245 }
2246 }
2247
2248
2249
2250 /*
2251 * Return current context handle for the calling thread.
2252 * This isn't the fastest way to get the current context.
2253 * If you need speed, see the GET_CURRENT_CONTEXT() macro in context.h
2254 */
2255 GLcontext *
2256 _mesa_get_current_context( void )
2257 {
2258 return (GLcontext *) _glapi_get_context();
2259 }
2260
2261
2262
2263 /*
2264 * This should be called by device drivers just before they do a
2265 * swapbuffers. Any pending rendering commands will be executed.
2266 * XXX we should really rename this function to _mesa_flush() or something.
2267 */
2268 void
2269 _mesa_swapbuffers(GLcontext *ctx)
2270 {
2271 FLUSH_VERTICES( ctx, 0 );
2272 }
2273
2274
2275
2276 /*
2277 * Return pointer to this context's current API dispatch table.
2278 * It'll either be the immediate-mode execute dispatcher or the
2279 * display list compile dispatcher.
2280 */
2281 struct _glapi_table *
2282 _mesa_get_dispatch(GLcontext *ctx)
2283 {
2284 return ctx->CurrentDispatch;
2285 }
2286
2287
2288
2289 /**********************************************************************/
2290 /***** Miscellaneous functions *****/
2291 /**********************************************************************/
2292
2293
2294 /*
2295 * Record the given error code and call the driver's Error function if defined.
2296 * This is called via _mesa_error().
2297 */
2298 void
2299 _mesa_record_error( GLcontext *ctx, GLenum error )
2300 {
2301 if (!ctx)
2302 return;
2303
2304 if (ctx->ErrorValue == GL_NO_ERROR) {
2305 ctx->ErrorValue = error;
2306 }
2307
2308 /* Call device driver's error handler, if any. This is used on the Mac. */
2309 if (ctx->Driver.Error) {
2310 (*ctx->Driver.Error)( ctx );
2311 }
2312 }
2313
2314
2315 void
2316 _mesa_Finish( void )
2317 {
2318 GET_CURRENT_CONTEXT(ctx);
2319 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2320 if (ctx->Driver.Finish) {
2321 (*ctx->Driver.Finish)( ctx );
2322 }
2323 }
2324
2325
2326
2327 void
2328 _mesa_Flush( void )
2329 {
2330 GET_CURRENT_CONTEXT(ctx);
2331 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2332 if (ctx->Driver.Flush) {
2333 (*ctx->Driver.Flush)( ctx );
2334 }
2335 }
2336
2337
2338
2339 const char *_mesa_prim_name[GL_POLYGON+4] = {
2340 "GL_POINTS",
2341 "GL_LINES",
2342 "GL_LINE_LOOP",
2343 "GL_LINE_STRIP",
2344 "GL_TRIANGLES",
2345 "GL_TRIANGLE_STRIP",
2346 "GL_TRIANGLE_FAN",
2347 "GL_QUADS",
2348 "GL_QUAD_STRIP",
2349 "GL_POLYGON",
2350 "outside begin/end",
2351 "inside unkown primitive",
2352 "unknown state"
2353 };