set table size to 1023 and use new HASH_FUNC() macro
[mesa.git] / src / mesa / main / context.c
1 /**
2 * \file context.c
3 * Mesa context/visual/framebuffer management functions.
4 * \author Brian Paul
5 */
6
7 /*
8 * Mesa 3-D graphics library
9 * Version: 6.3
10 *
11 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included
21 * in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
27 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32 /**
33 * \mainpage Mesa Main Module
34 *
35 * \section MainIntroduction Introduction
36 *
37 * The Mesa Main module consists of all the files in the main/ directory.
38 * Among the features of this module are:
39 * <UL>
40 * <LI> Structures to represent most GL state </LI>
41 * <LI> State set/get functions </LI>
42 * <LI> Display lists </LI>
43 * <LI> Texture unit, object and image handling </LI>
44 * <LI> Matrix and attribute stacks </LI>
45 * </UL>
46 *
47 * Other modules are responsible for API dispatch, vertex transformation,
48 * point/line/triangle setup, rasterization, vertex array caching,
49 * vertex/fragment programs/shaders, etc.
50 *
51 *
52 * \section AboutDoxygen About Doxygen
53 *
54 * If you're viewing this information as Doxygen-generated HTML you'll
55 * see the documentation index at the top of this page.
56 *
57 * The first line lists the Mesa source code modules.
58 * The second line lists the indexes available for viewing the documentation
59 * for each module.
60 *
61 * Selecting the <b>Main page</b> link will display a summary of the module
62 * (this page).
63 *
64 * Selecting <b>Data Structures</b> will list all C structures.
65 *
66 * Selecting the <b>File List</b> link will list all the source files in
67 * the module.
68 * Selecting a filename will show a list of all functions defined in that file.
69 *
70 * Selecting the <b>Data Fields</b> link will display a list of all
71 * documented structure members.
72 *
73 * Selecting the <b>Globals</b> link will display a list
74 * of all functions, structures, global variables and macros in the module.
75 *
76 */
77
78
79 #include "glheader.h"
80 #include "imports.h"
81 #include "accum.h"
82 #include "attrib.h"
83 #include "blend.h"
84 #include "buffers.h"
85 #include "bufferobj.h"
86 #include "colortab.h"
87 #include "context.h"
88 #include "debug.h"
89 #include "depth.h"
90 #include "dlist.h"
91 #include "eval.h"
92 #include "enums.h"
93 #include "extensions.h"
94 #include "feedback.h"
95 #include "fog.h"
96 #include "get.h"
97 #include "glthread.h"
98 #include "glapioffsets.h"
99 #include "histogram.h"
100 #include "hint.h"
101 #include "hash.h"
102 #include "light.h"
103 #include "lines.h"
104 #include "macros.h"
105 #include "matrix.h"
106 #include "occlude.h"
107 #include "pixel.h"
108 #include "points.h"
109 #include "polygon.h"
110 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
111 #include "program.h"
112 #endif
113 #include "rastpos.h"
114 #include "simple_list.h"
115 #include "state.h"
116 #include "stencil.h"
117 #include "texcompress.h"
118 #include "teximage.h"
119 #include "texobj.h"
120 #include "texstate.h"
121 #include "mtypes.h"
122 #include "varray.h"
123 #include "vtxfmt.h"
124 #if _HAVE_FULL_GL
125 #include "math/m_translate.h"
126 #include "math/m_matrix.h"
127 #include "math/m_xform.h"
128 #include "math/mathmod.h"
129 #endif
130 #include "shaderobjects.h"
131
132 #ifdef USE_SPARC_ASM
133 #include "sparc/sparc.h"
134 #endif
135
136 #ifndef MESA_VERBOSE
137 int MESA_VERBOSE = 0;
138 #endif
139
140 #ifndef MESA_DEBUG_FLAGS
141 int MESA_DEBUG_FLAGS = 0;
142 #endif
143
144
145 /* ubyte -> float conversion */
146 GLfloat _mesa_ubyte_to_float_color_tab[256];
147
148 static void
149 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
150
151
152 /**********************************************************************/
153 /** \name OpenGL SI-style interface (new in Mesa 3.5)
154 *
155 * \if subset
156 * \note Most of these functions are never called in the Mesa subset.
157 * \endif
158 */
159 /*@{*/
160
161 /**
162 * Destroy context callback.
163 *
164 * \param gc context.
165 * \return GL_TRUE on success, or GL_FALSE on failure.
166 *
167 * \ifnot subset
168 * Called by window system/device driver (via __GLexports::destroyCurrent) when
169 * the rendering context is to be destroyed.
170 * \endif
171 *
172 * Frees the context data and the context structure.
173 */
174 GLboolean
175 _mesa_destroyContext(__GLcontext *gc)
176 {
177 if (gc) {
178 _mesa_free_context_data(gc);
179 _mesa_free(gc);
180 }
181 return GL_TRUE;
182 }
183
184 /**
185 * Unbind context callback.
186 *
187 * \param gc context.
188 * \return GL_TRUE on success, or GL_FALSE on failure.
189 *
190 * \ifnot subset
191 * Called by window system/device driver (via __GLexports::loseCurrent)
192 * when the rendering context is made non-current.
193 * \endif
194 *
195 * No-op
196 */
197 GLboolean
198 _mesa_loseCurrent(__GLcontext *gc)
199 {
200 /* XXX unbind context from thread */
201 (void) gc;
202 return GL_TRUE;
203 }
204
205 /**
206 * Bind context callback.
207 *
208 * \param gc context.
209 * \return GL_TRUE on success, or GL_FALSE on failure.
210 *
211 * \ifnot subset
212 * Called by window system/device driver (via __GLexports::makeCurrent)
213 * when the rendering context is made current.
214 * \endif
215 *
216 * No-op
217 */
218 GLboolean
219 _mesa_makeCurrent(__GLcontext *gc)
220 {
221 /* XXX bind context to thread */
222 (void) gc;
223 return GL_TRUE;
224 }
225
226 /**
227 * Share context callback.
228 *
229 * \param gc context.
230 * \param gcShare shared context.
231 * \return GL_TRUE on success, or GL_FALSE on failure.
232 *
233 * \ifnot subset
234 * Called by window system/device driver (via __GLexports::shareContext)
235 * \endif
236 *
237 * Update the shared context reference count, gl_shared_state::RefCount.
238 */
239 GLboolean
240 _mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
241 {
242 if (gc && gcShare && gc->Shared && gcShare->Shared) {
243 gc->Shared->RefCount--;
244 if (gc->Shared->RefCount == 0) {
245 free_shared_state(gc, gc->Shared);
246 }
247 gc->Shared = gcShare->Shared;
248 gc->Shared->RefCount++;
249 return GL_TRUE;
250 }
251 else {
252 return GL_FALSE;
253 }
254 }
255
256
257 #if _HAVE_FULL_GL
258 /**
259 * Copy context callback.
260 */
261 GLboolean
262 _mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
263 {
264 if (dst && src) {
265 _mesa_copy_context( src, dst, mask );
266 return GL_TRUE;
267 }
268 else {
269 return GL_FALSE;
270 }
271 }
272 #endif
273
274 /** No-op */
275 GLboolean
276 _mesa_forceCurrent(__GLcontext *gc)
277 {
278 (void) gc;
279 return GL_TRUE;
280 }
281
282 /**
283 * Windows/buffer resizing notification callback.
284 *
285 * \param gc GL context.
286 * \return GL_TRUE on success, or GL_FALSE on failure.
287 */
288 GLboolean
289 _mesa_notifyResize(__GLcontext *gc)
290 {
291 GLint x, y;
292 GLuint width, height;
293 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
294 if (!d || !d->getDrawableSize)
295 return GL_FALSE;
296 d->getDrawableSize( d, &x, &y, &width, &height );
297 /* update viewport, resize software buffers, etc. */
298 return GL_TRUE;
299 }
300
301 /**
302 * Window/buffer destruction notification callback.
303 *
304 * \param gc GL context.
305 *
306 * Called when the context's window/buffer is going to be destroyed.
307 *
308 * No-op
309 */
310 void
311 _mesa_notifyDestroy(__GLcontext *gc)
312 {
313 /* Unbind from it. */
314 (void) gc;
315 }
316
317 /**
318 * Swap buffers notification callback.
319 *
320 * \param gc GL context.
321 *
322 * Called by window system just before swapping buffers.
323 * We have to finish any pending rendering.
324 */
325 void
326 _mesa_notifySwapBuffers(__GLcontext *gc)
327 {
328 FLUSH_VERTICES( gc, 0 );
329 }
330
331 /** No-op */
332 struct __GLdispatchStateRec *
333 _mesa_dispatchExec(__GLcontext *gc)
334 {
335 (void) gc;
336 return NULL;
337 }
338
339 /** No-op */
340 void
341 _mesa_beginDispatchOverride(__GLcontext *gc)
342 {
343 (void) gc;
344 }
345
346 /** No-op */
347 void
348 _mesa_endDispatchOverride(__GLcontext *gc)
349 {
350 (void) gc;
351 }
352
353 /**
354 * \ifnot subset
355 * Setup the exports.
356 *
357 * The window system will call these functions when it needs Mesa to do
358 * something.
359 *
360 * \note Device drivers should override these functions! For example,
361 * the Xlib driver should plug in the XMesa*-style functions into this
362 * structure. The XMesa-style functions should then call the _mesa_*
363 * version of these functions. This is an approximation to OO design
364 * (inheritance and virtual functions).
365 * \endif
366 *
367 * \if subset
368 * No-op.
369 *
370 * \endif
371 */
372 static void
373 _mesa_init_default_exports(__GLexports *exports)
374 {
375 #if _HAVE_FULL_GL
376 exports->destroyContext = _mesa_destroyContext;
377 exports->loseCurrent = _mesa_loseCurrent;
378 exports->makeCurrent = _mesa_makeCurrent;
379 exports->shareContext = _mesa_shareContext;
380 exports->copyContext = _mesa_copyContext;
381 exports->forceCurrent = _mesa_forceCurrent;
382 exports->notifyResize = _mesa_notifyResize;
383 exports->notifyDestroy = _mesa_notifyDestroy;
384 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
385 exports->dispatchExec = _mesa_dispatchExec;
386 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
387 exports->endDispatchOverride = _mesa_endDispatchOverride;
388 #else
389 (void) exports;
390 #endif
391 }
392
393 /**
394 * Exported OpenGL SI interface.
395 */
396 __GLcontext *
397 __glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
398 {
399 GLcontext *ctx;
400
401 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
402 if (ctx == NULL) {
403 return NULL;
404 }
405
406 /* XXX doesn't work at this time */
407 _mesa_initialize_context(ctx, modes, NULL, NULL, NULL);
408 ctx->imports = *imports;
409
410 return ctx;
411 }
412
413 /**
414 * Exported OpenGL SI interface.
415 */
416 void
417 __glCoreNopDispatch(void)
418 {
419 #if 0
420 /* SI */
421 __gl_dispatch = __glNopDispatchState;
422 #else
423 /* Mesa */
424 _glapi_set_dispatch(NULL);
425 #endif
426 }
427
428 /*@}*/
429
430
431 /**********************************************************************/
432 /** \name GL Visual allocation/destruction */
433 /**********************************************************************/
434 /*@{*/
435
436 /**
437 * Allocates a GLvisual structure and initializes it via
438 * _mesa_initialize_visual().
439 *
440 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
441 * \param dbFlag double buffering
442 * \param stereoFlag stereo buffer
443 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
444 * is acceptable but the actual depth type will be GLushort or GLuint as
445 * needed.
446 * \param stencilBits requested minimum bits per stencil buffer value
447 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
448 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
449 * \param redBits number of bits per color component in frame buffer for RGB(A)
450 * mode. We always use 8 in core Mesa though.
451 * \param greenBits same as above.
452 * \param blueBits same as above.
453 * \param alphaBits same as above.
454 * \param numSamples not really used.
455 *
456 * \return pointer to new GLvisual or NULL if requested parameters can't be
457 * met.
458 *
459 * \note Need to add params for level and numAuxBuffers (at least)
460 */
461 GLvisual *
462 _mesa_create_visual( GLboolean rgbFlag,
463 GLboolean dbFlag,
464 GLboolean stereoFlag,
465 GLint redBits,
466 GLint greenBits,
467 GLint blueBits,
468 GLint alphaBits,
469 GLint indexBits,
470 GLint depthBits,
471 GLint stencilBits,
472 GLint accumRedBits,
473 GLint accumGreenBits,
474 GLint accumBlueBits,
475 GLint accumAlphaBits,
476 GLint numSamples )
477 {
478 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
479 if (vis) {
480 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
481 redBits, greenBits, blueBits, alphaBits,
482 indexBits, depthBits, stencilBits,
483 accumRedBits, accumGreenBits,
484 accumBlueBits, accumAlphaBits,
485 numSamples)) {
486 FREE(vis);
487 return NULL;
488 }
489 }
490 return vis;
491 }
492
493 /**
494 * Makes some sanity checks and fills in the fields of the
495 * GLvisual structure with the given parameters.
496 *
497 * \return GL_TRUE on success, or GL_FALSE on failure.
498 *
499 * \sa _mesa_create_visual() above for the parameter description.
500 *
501 * \note Need to add params for level and numAuxBuffers (at least)
502 */
503 GLboolean
504 _mesa_initialize_visual( GLvisual *vis,
505 GLboolean rgbFlag,
506 GLboolean dbFlag,
507 GLboolean stereoFlag,
508 GLint redBits,
509 GLint greenBits,
510 GLint blueBits,
511 GLint alphaBits,
512 GLint indexBits,
513 GLint depthBits,
514 GLint stencilBits,
515 GLint accumRedBits,
516 GLint accumGreenBits,
517 GLint accumBlueBits,
518 GLint accumAlphaBits,
519 GLint numSamples )
520 {
521 assert(vis);
522
523 /* This is to catch bad values from device drivers not updated for
524 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
525 * bad value now (a 1-bit depth buffer!?!).
526 */
527 assert(depthBits == 0 || depthBits > 1);
528
529 if (depthBits < 0 || depthBits > 32) {
530 return GL_FALSE;
531 }
532 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
533 return GL_FALSE;
534 }
535 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
536 return GL_FALSE;
537 }
538 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
539 return GL_FALSE;
540 }
541 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
542 return GL_FALSE;
543 }
544 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
545 return GL_FALSE;
546 }
547
548 vis->rgbMode = rgbFlag;
549 vis->doubleBufferMode = dbFlag;
550 vis->stereoMode = stereoFlag;
551
552 vis->redBits = redBits;
553 vis->greenBits = greenBits;
554 vis->blueBits = blueBits;
555 vis->alphaBits = alphaBits;
556
557 vis->indexBits = indexBits;
558 vis->depthBits = depthBits;
559 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
560 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
561 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
562 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
563 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
564
565 vis->haveAccumBuffer = accumRedBits > 0;
566 vis->haveDepthBuffer = depthBits > 0;
567 vis->haveStencilBuffer = stencilBits > 0;
568
569 vis->numAuxBuffers = 0;
570 vis->level = 0;
571 vis->pixmapMode = 0;
572 vis->samples = numSamples;
573
574 return GL_TRUE;
575 }
576
577
578 /**
579 * Destroy a visual and free its memory.
580 *
581 * \param vis visual.
582 *
583 * Frees the visual structure.
584 */
585 void
586 _mesa_destroy_visual( GLvisual *vis )
587 {
588 FREE(vis);
589 }
590
591 /*@}*/
592
593
594 /**********************************************************************/
595 /** \name GL Framebuffer allocation/destruction */
596 /**********************************************************************/
597 /*@{*/
598
599 /**
600 * Allocate a GLframebuffer structure and initializes it via
601 * _mesa_initialize_framebuffer().
602 *
603 * A GLframebuffer is a structure which encapsulates the depth, stencil and
604 * accum buffers and related parameters.
605 *
606 * Note that the actual depth/stencil/accum/etc buffers are not allocated
607 * at this time. It's up to the device driver and/or swrast module to
608 * allocate them as needed.
609 *
610 * \param visual a GLvisual pointer (we copy the struct contents)
611 * \param softwareDepth create/use a software depth buffer?
612 * \param softwareStencil create/use a software stencil buffer?
613 * \param softwareAccum create/use a software accum buffer?
614 * \param softwareAlpha create/use a software alpha buffer?
615 *
616 * \return pointer to new GLframebuffer struct or NULL if error.
617 *
618 * \note Need to add softwareAuxBuffers parameter.
619 */
620 GLframebuffer *
621 _mesa_create_framebuffer( const GLvisual *visual,
622 GLboolean softwareDepth,
623 GLboolean softwareStencil,
624 GLboolean softwareAccum,
625 GLboolean softwareAlpha )
626 {
627 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
628 assert(visual);
629 if (buffer) {
630 _mesa_initialize_framebuffer(buffer, visual,
631 softwareDepth, softwareStencil,
632 softwareAccum, softwareAlpha );
633 }
634 return buffer;
635 }
636
637
638 /**
639 * Makes some sanity checks and fills in the fields of the
640 * GLframebuffer structure with the given parameters.
641 *
642 * \sa _mesa_create_framebuffer() above for the parameter description.
643 */
644 void
645 _mesa_initialize_framebuffer( GLframebuffer *buffer,
646 const GLvisual *visual,
647 GLboolean softwareDepth,
648 GLboolean softwareStencil,
649 GLboolean softwareAccum,
650 GLboolean softwareAlpha )
651 {
652 GLboolean softwareAux = GL_FALSE;
653 assert(buffer);
654 assert(visual);
655
656 _mesa_bzero(buffer, sizeof(GLframebuffer));
657
658 /* sanity checks */
659 if (softwareDepth ) {
660 assert(visual->depthBits > 0);
661 }
662 if (softwareStencil) {
663 assert(visual->stencilBits > 0);
664 }
665 if (softwareAccum) {
666 assert(visual->rgbMode);
667 assert(visual->accumRedBits > 0);
668 assert(visual->accumGreenBits > 0);
669 assert(visual->accumBlueBits > 0);
670 }
671 if (softwareAlpha) {
672 assert(visual->rgbMode);
673 assert(visual->alphaBits > 0);
674 }
675
676 buffer->Visual = *visual;
677 buffer->UseSoftwareDepthBuffer = softwareDepth;
678 buffer->UseSoftwareStencilBuffer = softwareStencil;
679 buffer->UseSoftwareAccumBuffer = softwareAccum;
680 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
681 buffer->UseSoftwareAuxBuffers = softwareAux;
682 }
683
684
685 /**
686 * Free a framebuffer struct and its buffers.
687 *
688 * Calls _mesa_free_framebuffer_data() and frees the structure.
689 */
690 void
691 _mesa_destroy_framebuffer( GLframebuffer *buffer )
692 {
693 if (buffer) {
694 _mesa_free_framebuffer_data(buffer);
695 FREE(buffer);
696 }
697 }
698
699
700 /**
701 * Free the data hanging off of \p buffer, but not \p buffer itself.
702 *
703 * \param buffer framebuffer.
704 *
705 * Frees all the buffers associated with the structure.
706 */
707 void
708 _mesa_free_framebuffer_data( GLframebuffer *buffer )
709 {
710 if (!buffer)
711 return;
712
713 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
714 MESA_PBUFFER_FREE( buffer->DepthBuffer );
715 buffer->DepthBuffer = NULL;
716 }
717 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
718 MESA_PBUFFER_FREE( buffer->Accum );
719 buffer->Accum = NULL;
720 }
721 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
722 MESA_PBUFFER_FREE( buffer->Stencil );
723 buffer->Stencil = NULL;
724 }
725 if (buffer->UseSoftwareAlphaBuffers){
726 if (buffer->FrontLeftAlpha) {
727 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
728 buffer->FrontLeftAlpha = NULL;
729 }
730 if (buffer->BackLeftAlpha) {
731 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
732 buffer->BackLeftAlpha = NULL;
733 }
734 if (buffer->FrontRightAlpha) {
735 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
736 buffer->FrontRightAlpha = NULL;
737 }
738 if (buffer->BackRightAlpha) {
739 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
740 buffer->BackRightAlpha = NULL;
741 }
742 }
743 }
744
745 /*@}*/
746
747
748 /**********************************************************************/
749 /** \name Context allocation, initialization, destroying
750 *
751 * The purpose of the most initialization functions here is to provide the
752 * default state values according to the OpenGL specification.
753 */
754 /**********************************************************************/
755 /*@{*/
756
757 /**
758 * One-time initialization mutex lock.
759 *
760 * \sa Used by one_time_init().
761 */
762 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
763
764 /**
765 * Calls all the various one-time-init functions in Mesa.
766 *
767 * While holding a global mutex lock, calls several initialization functions,
768 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
769 * defined.
770 *
771 * \sa _mesa_init_lists(), _math_init().
772 */
773 static void
774 one_time_init( GLcontext *ctx )
775 {
776 static GLboolean alreadyCalled = GL_FALSE;
777 (void) ctx;
778 _glthread_LOCK_MUTEX(OneTimeLock);
779 if (!alreadyCalled) {
780 GLuint i;
781
782 /* do some implementation tests */
783 assert( sizeof(GLbyte) == 1 );
784 assert( sizeof(GLubyte) == 1 );
785 assert( sizeof(GLshort) == 2 );
786 assert( sizeof(GLushort) == 2 );
787 assert( sizeof(GLint) == 4 );
788 assert( sizeof(GLuint) == 4 );
789
790 _mesa_init_lists();
791
792 #if _HAVE_FULL_GL
793 _math_init();
794
795 for (i = 0; i < 256; i++) {
796 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
797 }
798 #endif
799
800 #ifdef USE_SPARC_ASM
801 _mesa_init_sparc_glapi_relocs();
802 #endif
803 if (_mesa_getenv("MESA_DEBUG")) {
804 _glapi_noop_enable_warnings(GL_TRUE);
805 #ifndef GLX_DIRECT_RENDERING
806 /* libGL from before 2002/06/28 don't have this function. Someday,
807 * when newer libGL libs are common, remove the #ifdef test. This
808 * only serves to print warnings when calling undefined GL functions.
809 */
810 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
811 #endif
812 }
813 else {
814 _glapi_noop_enable_warnings(GL_FALSE);
815 }
816
817 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
818 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
819 #endif
820
821 alreadyCalled = GL_TRUE;
822 }
823 _glthread_UNLOCK_MUTEX(OneTimeLock);
824 }
825
826
827 /**
828 * Allocate and initialize a shared context state structure.
829 * Initializes the display list, texture objects and vertex programs hash
830 * tables, allocates the texture objects. If it runs out of memory, frees
831 * everything already allocated before returning NULL.
832 *
833 * \return pointer to a gl_shared_state structure on success, or NULL on
834 * failure.
835 */
836 static GLboolean
837 alloc_shared_state( GLcontext *ctx )
838 {
839 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
840 if (!ss)
841 return GL_FALSE;
842
843 ctx->Shared = ss;
844
845 _glthread_INIT_MUTEX(ss->Mutex);
846
847 ss->DisplayList = _mesa_NewHashTable();
848 ss->TexObjects = _mesa_NewHashTable();
849 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
850 ss->Programs = _mesa_NewHashTable();
851 #endif
852
853 #if FEATURE_ARB_vertex_program
854 ss->DefaultVertexProgram = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
855 if (!ss->DefaultVertexProgram)
856 goto cleanup;
857 #endif
858 #if FEATURE_ARB_fragment_program
859 ss->DefaultFragmentProgram = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
860 if (!ss->DefaultFragmentProgram)
861 goto cleanup;
862 #endif
863 #if FEATURE_ATI_fragment_shader
864 ss->DefaultFragmentShader = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_SHADER_ATI, 0);
865 if (!ss->DefaultFragmentShader)
866 goto cleanup;
867 #endif
868
869 ss->BufferObjects = _mesa_NewHashTable();
870
871 ss->GL2Objects = _mesa_NewHashTable ();
872
873 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
874 if (!ss->Default1D)
875 goto cleanup;
876
877 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
878 if (!ss->Default2D)
879 goto cleanup;
880
881 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
882 if (!ss->Default3D)
883 goto cleanup;
884
885 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
886 if (!ss->DefaultCubeMap)
887 goto cleanup;
888
889 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
890 if (!ss->DefaultRect)
891 goto cleanup;
892
893 /* Effectively bind the default textures to all texture units */
894 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
895 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
896 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
897 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
898 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
899
900 #if FEATURE_EXT_framebuffer_object
901 ss->FrameBuffers = _mesa_NewHashTable();
902 if (!ss->FrameBuffers)
903 goto cleanup;
904 ss->RenderBuffers = _mesa_NewHashTable();
905 if (!ss->RenderBuffers)
906 goto cleanup;
907 #endif
908
909
910 return GL_TRUE;
911
912 cleanup:
913 /* Ran out of memory at some point. Free everything and return NULL */
914 if (ss->DisplayList)
915 _mesa_DeleteHashTable(ss->DisplayList);
916 if (ss->TexObjects)
917 _mesa_DeleteHashTable(ss->TexObjects);
918 #if FEATURE_NV_vertex_program
919 if (ss->Programs)
920 _mesa_DeleteHashTable(ss->Programs);
921 #endif
922 #if FEATURE_ARB_vertex_program
923 if (ss->DefaultVertexProgram)
924 ctx->Driver.DeleteProgram(ctx, ss->DefaultVertexProgram);
925 #endif
926 #if FEATURE_ARB_fragment_program
927 if (ss->DefaultFragmentProgram)
928 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentProgram);
929 #endif
930 #if FEATURE_ATI_fragment_shader
931 if (ss->DefaultFragmentShader)
932 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentShader);
933 #endif
934 #if FEATURE_ARB_vertex_buffer_object
935 if (ss->BufferObjects)
936 _mesa_DeleteHashTable(ss->BufferObjects);
937 #endif
938
939 if (ss->GL2Objects)
940 _mesa_DeleteHashTable (ss->GL2Objects);
941
942 #if FEATURE_EXT_framebuffer_object
943 if (ss->FrameBuffers)
944 _mesa_DeleteHashTable(ss->FrameBuffers);
945 if (ss->RenderBuffers)
946 _mesa_DeleteHashTable(ss->RenderBuffers);
947 #endif
948
949 if (ss->Default1D)
950 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
951 if (ss->Default2D)
952 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
953 if (ss->Default3D)
954 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
955 if (ss->DefaultCubeMap)
956 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
957 if (ss->DefaultRect)
958 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
959 if (ss)
960 _mesa_free(ss);
961 return GL_FALSE;
962 }
963
964 /**
965 * Deallocate a shared state context and all children structures.
966 *
967 * \param ctx GL context.
968 * \param ss shared state pointer.
969 *
970 * Frees the display lists, the texture objects (calling the driver texture
971 * deletion callback to free its private data) and the vertex programs, as well
972 * as their hash tables.
973 *
974 * \sa alloc_shared_state().
975 */
976 static void
977 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
978 {
979 /* Free display lists */
980 while (1) {
981 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
982 if (list) {
983 _mesa_destroy_list(ctx, list);
984 }
985 else {
986 break;
987 }
988 }
989 _mesa_DeleteHashTable(ss->DisplayList);
990
991 /* Free texture objects */
992 ASSERT(ctx->Driver.DeleteTexture);
993 /* the default textures */
994 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
995 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
996 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
997 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
998 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
999 /* all other textures */
1000 while (1) {
1001 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
1002 if (texName) {
1003 struct gl_texture_object *texObj = (struct gl_texture_object *)
1004 _mesa_HashLookup(ss->TexObjects, texName);
1005 ASSERT(texObj);
1006 (*ctx->Driver.DeleteTexture)(ctx, texObj);
1007 _mesa_HashRemove(ss->TexObjects, texName);
1008 }
1009 else {
1010 break;
1011 }
1012 }
1013 _mesa_DeleteHashTable(ss->TexObjects);
1014
1015 #if FEATURE_NV_vertex_program
1016 /* Free vertex programs */
1017 while (1) {
1018 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
1019 if (prog) {
1020 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
1021 prog);
1022 ASSERT(p);
1023 ctx->Driver.DeleteProgram(ctx, p);
1024 _mesa_HashRemove(ss->Programs, prog);
1025 }
1026 else {
1027 break;
1028 }
1029 }
1030 _mesa_DeleteHashTable(ss->Programs);
1031 #endif
1032 #if FEATURE_ARB_vertex_program
1033 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
1034 #endif
1035 #if FEATURE_ARB_fragment_program
1036 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
1037 #endif
1038 #if FEATURE_ATI_fragment_shader
1039 _mesa_delete_program(ctx, ss->DefaultFragmentShader);
1040 #endif
1041
1042 #if FEATURE_ARB_vertex_buffer_object
1043 _mesa_DeleteHashTable(ss->BufferObjects);
1044 #endif
1045
1046 _mesa_DeleteHashTable (ss->GL2Objects);
1047
1048 #if FEATURE_EXT_framebuffer_object
1049 _mesa_DeleteHashTable(ss->FrameBuffers);
1050 _mesa_DeleteHashTable(ss->RenderBuffers);
1051 #endif
1052
1053 _glthread_DESTROY_MUTEX(ss->Mutex);
1054
1055 FREE(ss);
1056 }
1057
1058
1059 /**
1060 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
1061 */
1062 static void
1063 _mesa_init_current( GLcontext *ctx )
1064 {
1065 GLuint i;
1066
1067 /* Current group */
1068 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
1069 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
1070 }
1071 /* special cases: */
1072 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
1073 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
1074 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
1075 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
1076 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
1077
1078 ctx->Current.Index = 1;
1079 ctx->Current.EdgeFlag = GL_TRUE;
1080 }
1081
1082
1083 /**
1084 * Initialize fields of gl_constants (aka ctx->Const.*).
1085 * Use defaults from config.h. The device drivers will often override
1086 * some of these values (such as number of texture units).
1087 */
1088 static void
1089 _mesa_init_constants( GLcontext *ctx )
1090 {
1091 assert(ctx);
1092
1093 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1094 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1095
1096 /* Constants, may be overriden (usually only reduced) by device drivers */
1097 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
1098 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1099 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
1100 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
1101 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
1102 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1103 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
1104 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
1105 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
1106 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
1107 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1108 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1109 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1110 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1111 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
1112 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
1113 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1114 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1115 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1116 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
1117 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
1118 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
1119 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1120 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
1121 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1122 ctx->Const.MaxLights = MAX_LIGHTS;
1123 ctx->Const.MaxShininess = 128.0;
1124 ctx->Const.MaxSpotExponent = 128.0;
1125 ctx->Const.MaxViewportWidth = MAX_WIDTH;
1126 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
1127 #if FEATURE_ARB_vertex_program
1128 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
1129 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1130 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1131 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1132 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1133 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
1134 #endif
1135 #if FEATURE_ARB_fragment_program
1136 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
1137 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1138 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1139 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1140 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1141 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1142 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1143 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1144 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
1145 #endif
1146
1147 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1148 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
1149
1150 /* If we're running in the X server, do bounds checking to prevent
1151 * segfaults and server crashes!
1152 */
1153 #if defined(XFree86LOADER) && defined(IN_MODULE)
1154 ctx->Const.CheckArrayBounds = GL_TRUE;
1155 #else
1156 ctx->Const.CheckArrayBounds = GL_FALSE;
1157 #endif
1158
1159 ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
1160
1161 /* GL_OES_read_format */
1162 ctx->Const.ColorReadFormat = GL_RGBA;
1163 ctx->Const.ColorReadType = GL_UNSIGNED_BYTE;
1164
1165 #if FEATURE_EXT_framebuffer_object
1166 ctx->Const.MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
1167 ctx->Const.MaxRenderbufferSize = MAX_WIDTH;
1168 #endif
1169
1170 /* sanity checks */
1171 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
1172 }
1173
1174
1175 /**
1176 * Initialize the attribute groups in a GL context.
1177 *
1178 * \param ctx GL context.
1179 *
1180 * Initializes all the attributes, calling the respective <tt>init*</tt>
1181 * functions for the more complex data structures.
1182 */
1183 static GLboolean
1184 init_attrib_groups( GLcontext *ctx )
1185 {
1186 assert(ctx);
1187
1188 /* Constants */
1189 _mesa_init_constants( ctx );
1190
1191 /* Extensions */
1192 _mesa_init_extensions( ctx );
1193
1194 /* Attribute Groups */
1195 _mesa_init_accum( ctx );
1196 _mesa_init_attrib( ctx );
1197 _mesa_init_buffer_objects( ctx );
1198 _mesa_init_color( ctx );
1199 _mesa_init_colortables( ctx );
1200 _mesa_init_current( ctx );
1201 _mesa_init_depth( ctx );
1202 _mesa_init_debug( ctx );
1203 _mesa_init_display_list( ctx );
1204 _mesa_init_eval( ctx );
1205 _mesa_init_feedback( ctx );
1206 _mesa_init_fog( ctx );
1207 _mesa_init_histogram( ctx );
1208 _mesa_init_hint( ctx );
1209 _mesa_init_line( ctx );
1210 _mesa_init_lighting( ctx );
1211 _mesa_init_matrix( ctx );
1212 _mesa_init_multisample( ctx );
1213 _mesa_init_occlude( ctx );
1214 _mesa_init_pixel( ctx );
1215 _mesa_init_point( ctx );
1216 _mesa_init_polygon( ctx );
1217 _mesa_init_program( ctx );
1218 _mesa_init_rastpos( ctx );
1219 _mesa_init_scissor( ctx );
1220 _mesa_init_shaderobjects (ctx);
1221 _mesa_init_stencil( ctx );
1222 _mesa_init_transform( ctx );
1223 _mesa_init_varray( ctx );
1224 _mesa_init_viewport( ctx );
1225
1226 if (!_mesa_init_texture( ctx ))
1227 return GL_FALSE;
1228
1229 _mesa_init_texture_s3tc( ctx );
1230 _mesa_init_texture_fxt1( ctx );
1231
1232 /* Miscellaneous */
1233 ctx->NewState = _NEW_ALL;
1234 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1235 ctx->_Facing = 0;
1236 #if CHAN_TYPE == GL_FLOAT
1237 ctx->ClampFragmentColors = GL_FALSE; /* XXX temporary */
1238 #else
1239 ctx->ClampFragmentColors = GL_TRUE;
1240 #endif
1241 ctx->ClampVertexColors = GL_TRUE;
1242
1243 return GL_TRUE;
1244 }
1245
1246
1247 /**
1248 * If the DRI libGL.so library is old, it may not have the entrypoints for
1249 * some recent OpenGL extensions. Dynamically add them now.
1250 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1251 * and driver code, this won't be an issue (and calling this function won't
1252 * do any harm).
1253 */
1254 static void
1255 add_newer_entrypoints(void)
1256 {
1257 unsigned i;
1258 static const struct {
1259 const char * const name;
1260 unsigned offset;
1261 }
1262 newer_entrypoints[] = {
1263 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1264 { "glWindowPos2dARB", 513 },
1265 { "glWindowPos2dvARB", 514 },
1266 { "glWindowPos2fARB", 515 },
1267 { "glWindowPos2fvARB", 516 },
1268 { "glWindowPos2iARB", 517 },
1269 { "glWindowPos2ivARB", 518 },
1270 { "glWindowPos2sARB", 519 },
1271 { "glWindowPos2svARB", 520 },
1272 { "glWindowPos3dARB", 521 },
1273 { "glWindowPos3dvARB", 522 },
1274 { "glWindowPos3fARB", 523 },
1275 { "glWindowPos3fvARB", 524 },
1276 { "glWindowPos3iARB", 525 },
1277 { "glWindowPos3ivARB", 526 },
1278 { "glWindowPos3sARB", 527 },
1279 { "glWindowPos3svARB", 528 },
1280 #if FEATURE_NV_vertex_program
1281 { "glAreProgramsResidentNV", 578 },
1282 { "glBindProgramNV", 579 },
1283 { "glDeleteProgramsNV", 580 },
1284 { "glExecuteProgramNV", 581 },
1285 { "glGenProgramsNV", 582 },
1286 { "glGetProgramParameterdvNV", 583 },
1287 { "glGetProgramParameterfvNV", 584 },
1288 { "glGetProgramivNV", 585 },
1289 { "glGetProgramStringNV", 586 },
1290 { "glGetTrackMatrixivNV", 587 },
1291 { "glGetVertexAttribdvNV", 588 },
1292 { "glGetVertexAttribfvNV", 589 },
1293 { "glGetVertexAttribivNV", 590 },
1294 { "glGetVertexAttribPointervNV", 591 },
1295 { "glIsProgramNV", 592 },
1296 { "glLoadProgramNV", 593 },
1297 { "glProgramParameter4dNV", 594 },
1298 { "glProgramParameter4dvNV", 595 },
1299 { "glProgramParameter4fNV", 596 },
1300 { "glProgramParameter4fvNV", 597 },
1301 { "glProgramParameters4dvNV", 598 },
1302 { "glProgramParameters4fvNV", 599 },
1303 { "glRequestResidentProgramsNV", 600 },
1304 { "glTrackMatrixNV", 601 },
1305 { "glVertexAttribPointerNV", 602 },
1306 { "glVertexAttrib1dNV", 603 },
1307 { "glVertexAttrib1dvNV", 604 },
1308 { "glVertexAttrib1fNV", 605 },
1309 { "glVertexAttrib1fvNV", 606 },
1310 { "glVertexAttrib1sNV", 607 },
1311 { "glVertexAttrib1svNV", 608 },
1312 { "glVertexAttrib2dNV", 609 },
1313 { "glVertexAttrib2dvNV", 610 },
1314 { "glVertexAttrib2fNV", 611 },
1315 { "glVertexAttrib2fvNV", 612 },
1316 { "glVertexAttrib2sNV", 613 },
1317 { "glVertexAttrib2svNV", 614 },
1318 { "glVertexAttrib3dNV", 615 },
1319 { "glVertexAttrib3dvNV", 616 },
1320 { "glVertexAttrib3fNV", 617 },
1321 { "glVertexAttrib3fvNV", 618 },
1322 { "glVertexAttrib3sNV", 619 },
1323 { "glVertexAttrib3svNV", 620 },
1324 { "glVertexAttrib4dNV", 621 },
1325 { "glVertexAttrib4dvNV", 622 },
1326 { "glVertexAttrib4fNV", 623 },
1327 { "glVertexAttrib4fvNV", 624 },
1328 { "glVertexAttrib4sNV", 625 },
1329 { "glVertexAttrib4svNV", 626 },
1330 { "glVertexAttrib4ubNV", 627 },
1331 { "glVertexAttrib4ubvNV", 628 },
1332 { "glVertexAttribs1dvNV", 629 },
1333 { "glVertexAttribs1fvNV", 630 },
1334 { "glVertexAttribs1svNV", 631 },
1335 { "glVertexAttribs2dvNV", 632 },
1336 { "glVertexAttribs2fvNV", 633 },
1337 { "glVertexAttribs2svNV", 634 },
1338 { "glVertexAttribs3dvNV", 635 },
1339 { "glVertexAttribs3fvNV", 636 },
1340 { "glVertexAttribs3svNV", 637 },
1341 { "glVertexAttribs4dvNV", 638 },
1342 { "glVertexAttribs4fvNV", 639 },
1343 { "glVertexAttribs4svNV", 640 },
1344 { "glVertexAttribs4ubvNV", 641 },
1345 #endif
1346 { "glPointParameteriNV", 642 },
1347 { "glPointParameterivNV", 643 },
1348 { "glMultiDrawArraysEXT", 644 },
1349 { "glMultiDrawElementsEXT", 645 },
1350 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1351 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1352 { "glActiveStencilFaceEXT", 646 },
1353 #if FEATURE_NV_fence
1354 { "glDeleteFencesNV", 647 },
1355 { "glGenFencesNV", 648 },
1356 { "glIsFenceNV", 649 },
1357 { "glTestFenceNV", 650 },
1358 { "glGetFenceivNV", 651 },
1359 { "glFinishFenceNV", 652 },
1360 { "glSetFenceNV", 653 },
1361 #endif
1362 #if FEATURE_NV_fragment_program
1363 { "glProgramNamedParameter4fNV", 682 },
1364 { "glProgramNamedParameter4dNV", 683 },
1365 { "glProgramNamedParameter4fvNV", 683 },
1366 { "glProgramNamedParameter4dvNV", 684 },
1367 { "glGetProgramNamedParameterfvNV", 685 },
1368 { "glGetProgramNamedParameterdvNV", 686 },
1369 #endif
1370 #if FEATURE_ARB_vertex_program
1371 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1372 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1373 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1374 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1375 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1376 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1377 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1378 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1379 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1380 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1381 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1382 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1383 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1384 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1385 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1386 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1387 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1388 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1389 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1390 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1391 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1392 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1393 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1394 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1395 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1396 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1397 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1398 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1399 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1400 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1401 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1402 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1403 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1404 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1405 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1406 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1407 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1408 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1409 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1410 { "glProgramStringARB", _gloffset_ProgramStringARB },
1411 { "glBindProgramARB", _gloffset_BindProgramNV },
1412 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1413 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1414 { "glIsProgramARB", _gloffset_IsProgramNV },
1415 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1416 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1417 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1418 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1419 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1420 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1421 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1422 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1423 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1424 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1425 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1426 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1427 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1428 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1429 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1430 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1431 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1432 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
1433 #endif
1434 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1435 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
1436 /* GL_EXT_stencil_two_side */
1437 { "glActiveStencilFaceEXT", _gloffset_ActiveStencilFaceEXT },
1438 /* GL_ARB_draw_buffers */
1439 { "glDrawBuffersARB", _gloffset_DrawBuffersARB },
1440 #if FEATURE_ATI_fragment_shader
1441 { "glGenFragmentShadersATI", _gloffset_GenFragmentShadersATI },
1442 { "glBindFragmentShaderATI", _gloffset_BindFragmentShaderATI },
1443 { "glDeleteFragmentShaderATI", _gloffset_DeleteFragmentShaderATI },
1444 { "glBeginFragmentShaderATI", _gloffset_BeginFragmentShaderATI },
1445 { "glEndFragmentShaderATI", _gloffset_EndFragmentShaderATI },
1446 { "glPassTexCoordATI", _gloffset_PassTexCoordATI },
1447 { "glSampleMapATI", _gloffset_SampleMapATI },
1448 { "glColorFragmentOp1ATI", _gloffset_ColorFragmentOp1ATI },
1449 { "glColorFragmentOp2ATI", _gloffset_ColorFragmentOp2ATI },
1450 { "glColorFragmentOp3ATI", _gloffset_ColorFragmentOp3ATI },
1451 { "glAlphaFragmentOp1ATI", _gloffset_AlphaFragmentOp1ATI },
1452 { "glAlphaFragmentOp2ATI", _gloffset_AlphaFragmentOp2ATI },
1453 { "glAlphaFragmentOp3ATI", _gloffset_AlphaFragmentOp3ATI },
1454 { "glSetFragmentShaderConstantATI", _gloffset_SetFragmentShaderConstantATI },
1455 #endif
1456 };
1457
1458 for (i = 0; i < Elements(newer_entrypoints); i++ ) {
1459 _glapi_add_entrypoint( newer_entrypoints[i].name,
1460 newer_entrypoints[i].offset );
1461 }
1462 }
1463
1464
1465 /**
1466 * This is the default function we plug into all dispatch table slots
1467 * This helps prevents a segfault when someone calls a GL function without
1468 * first checking if the extension's supported.
1469 */
1470 static int
1471 generic_nop(void)
1472 {
1473 _mesa_problem(NULL, "User called no-op dispatch function (an unsupported extension function?)");
1474 return 0;
1475 }
1476
1477
1478 /**
1479 * Allocate and initialize a new dispatch table.
1480 */
1481 static struct _glapi_table *
1482 alloc_dispatch_table(void)
1483 {
1484 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1485 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1486 * Mesa we do this to accomodate different versions of libGL and various
1487 * DRI drivers.
1488 */
1489 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
1490 sizeof(struct _glapi_table) / sizeof(_glapi_proc));
1491 struct _glapi_table *table =
1492 (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc));
1493 if (table) {
1494 _glapi_proc *entry = (_glapi_proc *) table;
1495 GLint i;
1496 for (i = 0; i < numEntries; i++) {
1497 entry[i] = (_glapi_proc) generic_nop;
1498 }
1499 }
1500 return table;
1501 }
1502
1503
1504 /**
1505 * Initialize a GLcontext struct (rendering context).
1506 *
1507 * This includes allocating all the other structs and arrays which hang off of
1508 * the context by pointers.
1509 * Note that the driver needs to pass in its dd_function_table here since
1510 * we need to at least call driverFunctions->NewTextureObject to create the
1511 * default texture objects.
1512 *
1513 * Called by _mesa_create_context().
1514 *
1515 * Performs the imports and exports callback tables initialization, and
1516 * miscellaneous one-time initializations. If no shared context is supplied one
1517 * is allocated, and increase its reference count. Setups the GL API dispatch
1518 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1519 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1520 * for debug flags.
1521 *
1522 * \param ctx the context to initialize
1523 * \param visual describes the visual attributes for this context
1524 * \param share_list points to context to share textures, display lists,
1525 * etc with, or NULL
1526 * \param driverFunctions table of device driver functions for this context
1527 * to use
1528 * \param driverContext pointer to driver-specific context data
1529 */
1530 GLboolean
1531 _mesa_initialize_context( GLcontext *ctx,
1532 const GLvisual *visual,
1533 GLcontext *share_list,
1534 const struct dd_function_table *driverFunctions,
1535 void *driverContext )
1536 {
1537 ASSERT(driverContext);
1538 assert(driverFunctions->NewTextureObject);
1539 assert(driverFunctions->FreeTexImageData);
1540
1541 /* If the driver wants core Mesa to use special imports, it'll have to
1542 * override these defaults.
1543 */
1544 _mesa_init_default_imports( &(ctx->imports), driverContext );
1545
1546 /* initialize the exports (Mesa functions called by the window system) */
1547 _mesa_init_default_exports( &(ctx->exports) );
1548
1549 /* misc one-time initializations */
1550 one_time_init(ctx);
1551
1552 ctx->Visual = *visual;
1553 ctx->DrawBuffer = NULL;
1554 ctx->ReadBuffer = NULL;
1555
1556 /* Plug in driver functions and context pointer here.
1557 * This is important because when we call alloc_shared_state() below
1558 * we'll call ctx->Driver.NewTextureObject() to create the default
1559 * textures.
1560 */
1561 ctx->Driver = *driverFunctions;
1562 ctx->DriverCtx = driverContext;
1563
1564 if (share_list) {
1565 /* share state with another context */
1566 ctx->Shared = share_list->Shared;
1567 }
1568 else {
1569 /* allocate new, unshared state */
1570 if (!alloc_shared_state( ctx )) {
1571 return GL_FALSE;
1572 }
1573 }
1574 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1575 ctx->Shared->RefCount++;
1576 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1577
1578 if (!init_attrib_groups( ctx )) {
1579 free_shared_state(ctx, ctx->Shared);
1580 return GL_FALSE;
1581 }
1582
1583 /* libGL ABI coordination */
1584 add_newer_entrypoints();
1585
1586 /* setup the API dispatch tables */
1587 ctx->Exec = alloc_dispatch_table();
1588 ctx->Save = alloc_dispatch_table();
1589 if (!ctx->Exec || !ctx->Save) {
1590 free_shared_state(ctx, ctx->Shared);
1591 if (ctx->Exec)
1592 _mesa_free(ctx->Exec);
1593 }
1594 _mesa_init_exec_table(ctx->Exec);
1595 ctx->CurrentDispatch = ctx->Exec;
1596 #if _HAVE_FULL_GL
1597 _mesa_init_dlist_table(ctx->Save);
1598 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
1599 /* Neutral tnl module stuff */
1600 _mesa_init_exec_vtxfmt( ctx );
1601 ctx->TnlModule.Current = NULL;
1602 ctx->TnlModule.SwapCount = 0;
1603 #endif
1604
1605 return GL_TRUE;
1606 }
1607
1608
1609 /**
1610 * Allocate and initialize a GLcontext structure.
1611 * Note that the driver needs to pass in its dd_function_table here since
1612 * we need to at least call driverFunctions->NewTextureObject to initialize
1613 * the rendering context.
1614 *
1615 * \param visual a GLvisual pointer (we copy the struct contents)
1616 * \param share_list another context to share display lists with or NULL
1617 * \param driverFunctions points to the dd_function_table into which the
1618 * driver has plugged in all its special functions.
1619 * \param driverCtx points to the device driver's private context state
1620 *
1621 * \return pointer to a new __GLcontextRec or NULL if error.
1622 */
1623 GLcontext *
1624 _mesa_create_context( const GLvisual *visual,
1625 GLcontext *share_list,
1626 const struct dd_function_table *driverFunctions,
1627 void *driverContext )
1628
1629 {
1630 GLcontext *ctx;
1631
1632 ASSERT(visual);
1633 ASSERT(driverContext);
1634
1635 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
1636 if (!ctx)
1637 return NULL;
1638
1639 if (_mesa_initialize_context(ctx, visual, share_list,
1640 driverFunctions, driverContext)) {
1641 return ctx;
1642 }
1643 else {
1644 _mesa_free(ctx);
1645 return NULL;
1646 }
1647 }
1648
1649
1650 /**
1651 * Free the data associated with the given context.
1652 *
1653 * But doesn't free the GLcontext struct itself.
1654 *
1655 * \sa _mesa_initialize_context() and init_attrib_groups().
1656 */
1657 void
1658 _mesa_free_context_data( GLcontext *ctx )
1659 {
1660 /* if we're destroying the current context, unbind it first */
1661 if (ctx == _mesa_get_current_context()) {
1662 _mesa_make_current(NULL, NULL);
1663 }
1664
1665 _mesa_free_lighting_data( ctx );
1666 _mesa_free_eval_data( ctx );
1667 _mesa_free_texture_data( ctx );
1668 _mesa_free_matrix_data( ctx );
1669 _mesa_free_viewport_data( ctx );
1670 _mesa_free_colortables_data( ctx );
1671 _mesa_free_program_data(ctx);
1672 _mesa_free_occlude_data(ctx);
1673
1674 #if FEATURE_ARB_vertex_buffer_object
1675 _mesa_delete_buffer_object(ctx, ctx->Array.NullBufferObj);
1676 #endif
1677
1678 /* free dispatch tables */
1679 _mesa_free(ctx->Exec);
1680 _mesa_free(ctx->Save);
1681
1682 /* Shared context state (display lists, textures, etc) */
1683 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1684 ctx->Shared->RefCount--;
1685 assert(ctx->Shared->RefCount >= 0);
1686 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1687 if (ctx->Shared->RefCount == 0) {
1688 /* free shared state */
1689 free_shared_state( ctx, ctx->Shared );
1690 }
1691
1692 if (ctx->Extensions.String)
1693 FREE((void *) ctx->Extensions.String);
1694 }
1695
1696
1697 /**
1698 * Destroy a GLcontext structure.
1699 *
1700 * \param ctx GL context.
1701 *
1702 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1703 */
1704 void
1705 _mesa_destroy_context( GLcontext *ctx )
1706 {
1707 if (ctx) {
1708 _mesa_free_context_data(ctx);
1709 FREE( (void *) ctx );
1710 }
1711 }
1712
1713
1714 #if _HAVE_FULL_GL
1715 /**
1716 * Copy attribute groups from one context to another.
1717 *
1718 * \param src source context
1719 * \param dst destination context
1720 * \param mask bitwise OR of GL_*_BIT flags
1721 *
1722 * According to the bits specified in \p mask, copies the corresponding
1723 * attributes from \p src into \p dst. For many of the attributes a simple \c
1724 * memcpy is not enough due to the existence of internal pointers in their data
1725 * structures.
1726 */
1727 void
1728 _mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
1729 {
1730 if (mask & GL_ACCUM_BUFFER_BIT) {
1731 /* OK to memcpy */
1732 dst->Accum = src->Accum;
1733 }
1734 if (mask & GL_COLOR_BUFFER_BIT) {
1735 /* OK to memcpy */
1736 dst->Color = src->Color;
1737 }
1738 if (mask & GL_CURRENT_BIT) {
1739 /* OK to memcpy */
1740 dst->Current = src->Current;
1741 }
1742 if (mask & GL_DEPTH_BUFFER_BIT) {
1743 /* OK to memcpy */
1744 dst->Depth = src->Depth;
1745 }
1746 if (mask & GL_ENABLE_BIT) {
1747 /* no op */
1748 }
1749 if (mask & GL_EVAL_BIT) {
1750 /* OK to memcpy */
1751 dst->Eval = src->Eval;
1752 }
1753 if (mask & GL_FOG_BIT) {
1754 /* OK to memcpy */
1755 dst->Fog = src->Fog;
1756 }
1757 if (mask & GL_HINT_BIT) {
1758 /* OK to memcpy */
1759 dst->Hint = src->Hint;
1760 }
1761 if (mask & GL_LIGHTING_BIT) {
1762 GLuint i;
1763 /* begin with memcpy */
1764 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1765 /* fixup linked lists to prevent pointer insanity */
1766 make_empty_list( &(dst->Light.EnabledList) );
1767 for (i = 0; i < MAX_LIGHTS; i++) {
1768 if (dst->Light.Light[i].Enabled) {
1769 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1770 }
1771 }
1772 }
1773 if (mask & GL_LINE_BIT) {
1774 /* OK to memcpy */
1775 dst->Line = src->Line;
1776 }
1777 if (mask & GL_LIST_BIT) {
1778 /* OK to memcpy */
1779 dst->List = src->List;
1780 }
1781 if (mask & GL_PIXEL_MODE_BIT) {
1782 /* OK to memcpy */
1783 dst->Pixel = src->Pixel;
1784 }
1785 if (mask & GL_POINT_BIT) {
1786 /* OK to memcpy */
1787 dst->Point = src->Point;
1788 }
1789 if (mask & GL_POLYGON_BIT) {
1790 /* OK to memcpy */
1791 dst->Polygon = src->Polygon;
1792 }
1793 if (mask & GL_POLYGON_STIPPLE_BIT) {
1794 /* Use loop instead of MEMCPY due to problem with Portland Group's
1795 * C compiler. Reported by John Stone.
1796 */
1797 GLuint i;
1798 for (i = 0; i < 32; i++) {
1799 dst->PolygonStipple[i] = src->PolygonStipple[i];
1800 }
1801 }
1802 if (mask & GL_SCISSOR_BIT) {
1803 /* OK to memcpy */
1804 dst->Scissor = src->Scissor;
1805 }
1806 if (mask & GL_STENCIL_BUFFER_BIT) {
1807 /* OK to memcpy */
1808 dst->Stencil = src->Stencil;
1809 }
1810 if (mask & GL_TEXTURE_BIT) {
1811 /* Cannot memcpy because of pointers */
1812 _mesa_copy_texture_state(src, dst);
1813 }
1814 if (mask & GL_TRANSFORM_BIT) {
1815 /* OK to memcpy */
1816 dst->Transform = src->Transform;
1817 }
1818 if (mask & GL_VIEWPORT_BIT) {
1819 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1820 dst->Viewport.X = src->Viewport.X;
1821 dst->Viewport.Y = src->Viewport.Y;
1822 dst->Viewport.Width = src->Viewport.Width;
1823 dst->Viewport.Height = src->Viewport.Height;
1824 dst->Viewport.Near = src->Viewport.Near;
1825 dst->Viewport.Far = src->Viewport.Far;
1826 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
1827 }
1828
1829 /* XXX FIXME: Call callbacks?
1830 */
1831 dst->NewState = _NEW_ALL;
1832 }
1833 #endif
1834
1835
1836 /**
1837 * Check if the given context can render into the given framebuffer
1838 * by checking visual attributes.
1839 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1840 */
1841 static GLboolean
1842 check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1843 {
1844 const GLvisual *ctxvis = &ctx->Visual;
1845 const GLvisual *bufvis = &buffer->Visual;
1846
1847 if (ctxvis == bufvis)
1848 return GL_TRUE;
1849
1850 if (ctxvis->rgbMode != bufvis->rgbMode)
1851 return GL_FALSE;
1852 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1853 return GL_FALSE;
1854 if (ctxvis->stereoMode && !bufvis->stereoMode)
1855 return GL_FALSE;
1856 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1857 return GL_FALSE;
1858 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1859 return GL_FALSE;
1860 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1861 return GL_FALSE;
1862 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1863 return GL_FALSE;
1864 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1865 return GL_FALSE;
1866 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1867 return GL_FALSE;
1868 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1869 return GL_FALSE;
1870 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1871 return GL_FALSE;
1872
1873 return GL_TRUE;
1874 }
1875
1876
1877 /**
1878 * Set the current context, binding the given frame buffer to the context.
1879 *
1880 * \param newCtx new GL context.
1881 * \param buffer framebuffer.
1882 *
1883 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
1884 */
1885 void
1886 _mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1887 {
1888 _mesa_make_current2( newCtx, buffer, buffer );
1889 }
1890
1891 /**
1892 * Bind the given context to the given draw-buffer and read-buffer and
1893 * make it the current context for this thread.
1894 *
1895 * \param newCtx new GL context. If NULL then there will be no current GL
1896 * context.
1897 * \param drawBuffer draw framebuffer.
1898 * \param readBuffer read framebuffer.
1899 *
1900 * Check that the context's and framebuffer's visuals are compatible, returning
1901 * immediately otherwise. Sets the glapi current context via
1902 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1903 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1904 * Calls dd_function_table::MakeCurrent callback if defined.
1905 *
1906 * When a context is bound by the first time and the \c MESA_INFO environment
1907 * variable is set it calls print_info() as an aid for remote user
1908 * troubleshooting.
1909 */
1910 void
1911 _mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1912 GLframebuffer *readBuffer )
1913 {
1914 if (MESA_VERBOSE)
1915 _mesa_debug(newCtx, "_mesa_make_current2()\n");
1916
1917 /* Check that the context's and framebuffer's visuals are compatible.
1918 */
1919 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1920 if (!check_compatible(newCtx, drawBuffer))
1921 return;
1922 }
1923 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1924 if (!check_compatible(newCtx, readBuffer))
1925 return;
1926 }
1927
1928 /* We call this function periodically (just here for now) in
1929 * order to detect when multithreading has begun.
1930 */
1931 _glapi_check_multithread();
1932
1933 _glapi_set_context((void *) newCtx);
1934 ASSERT(_mesa_get_current_context() == newCtx);
1935
1936
1937 if (!newCtx) {
1938 _glapi_set_dispatch(NULL); /* none current */
1939 }
1940 else {
1941 _glapi_set_dispatch(newCtx->CurrentDispatch);
1942
1943 if (drawBuffer && readBuffer) {
1944 /* TODO: check if newCtx and buffer's visual match??? */
1945 newCtx->DrawBuffer = drawBuffer;
1946 newCtx->ReadBuffer = readBuffer;
1947 newCtx->NewState |= _NEW_BUFFERS;
1948
1949 #if _HAVE_FULL_GL
1950 if (!drawBuffer->Initialized) {
1951 /* get initial window size */
1952 GLuint bufWidth, bufHeight;
1953 /* ask device driver for size of the buffer */
1954 (*newCtx->Driver.GetBufferSize)(drawBuffer, &bufWidth, &bufHeight);
1955 /* set initial buffer size */
1956 drawBuffer->Width = bufWidth;
1957 drawBuffer->Height = bufHeight;
1958 newCtx->Driver.ResizeBuffers( drawBuffer );
1959 drawBuffer->Initialized = GL_TRUE;
1960 }
1961
1962 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
1963 /* get initial window size */
1964 GLuint bufWidth, bufHeight;
1965 /* ask device driver for size of the buffer */
1966 (*newCtx->Driver.GetBufferSize)(readBuffer, &bufWidth, &bufHeight);
1967 /* set initial buffer size */
1968 readBuffer->Width = bufWidth;
1969 readBuffer->Height = bufHeight;
1970 newCtx->Driver.ResizeBuffers( readBuffer );
1971 readBuffer->Initialized = GL_TRUE;
1972 }
1973 #endif
1974 if (newCtx->FirstTimeCurrent) {
1975 /* set initial viewport and scissor size now */
1976 _mesa_set_viewport(newCtx, 0, 0, drawBuffer->Width, drawBuffer->Height);
1977 newCtx->Scissor.Width = drawBuffer->Width;
1978 newCtx->Scissor.Height = drawBuffer->Height;
1979 }
1980 }
1981
1982 /* Alert the driver - usually passed on to the sw t&l module,
1983 * but also used to detect threaded cases in the radeon codegen
1984 * hw t&l module.
1985 */
1986 if (newCtx->Driver.MakeCurrent)
1987 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1988
1989 /* We can use this to help debug user's problems. Tell them to set
1990 * the MESA_INFO env variable before running their app. Then the
1991 * first time each context is made current we'll print some useful
1992 * information.
1993 */
1994 if (newCtx->FirstTimeCurrent) {
1995 if (_mesa_getenv("MESA_INFO")) {
1996 _mesa_print_info();
1997 }
1998 newCtx->FirstTimeCurrent = GL_FALSE;
1999 }
2000 }
2001 }
2002
2003 /**
2004 * Get current context for the calling thread.
2005 *
2006 * \return pointer to the current GL context.
2007 *
2008 * Calls _glapi_get_context(). This isn't the fastest way to get the current
2009 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
2010 */
2011 GLcontext *
2012 _mesa_get_current_context( void )
2013 {
2014 return (GLcontext *) _glapi_get_context();
2015 }
2016
2017 /**
2018 * Get context's current API dispatch table.
2019 *
2020 * It'll either be the immediate-mode execute dispatcher or the display list
2021 * compile dispatcher.
2022 *
2023 * \param ctx GL context.
2024 *
2025 * \return pointer to dispatch_table.
2026 *
2027 * Simply returns __GLcontextRec::CurrentDispatch.
2028 */
2029 struct _glapi_table *
2030 _mesa_get_dispatch(GLcontext *ctx)
2031 {
2032 return ctx->CurrentDispatch;
2033 }
2034
2035 /*@}*/
2036
2037
2038 /**********************************************************************/
2039 /** \name Miscellaneous functions */
2040 /**********************************************************************/
2041 /*@{*/
2042
2043 /**
2044 * Record an error.
2045 *
2046 * \param ctx GL context.
2047 * \param error error code.
2048 *
2049 * Records the given error code and call the driver's dd_function_table::Error
2050 * function if defined.
2051 *
2052 * \sa
2053 * This is called via _mesa_error().
2054 */
2055 void
2056 _mesa_record_error( GLcontext *ctx, GLenum error )
2057 {
2058 if (!ctx)
2059 return;
2060
2061 if (ctx->ErrorValue == GL_NO_ERROR) {
2062 ctx->ErrorValue = error;
2063 }
2064
2065 /* Call device driver's error handler, if any. This is used on the Mac. */
2066 if (ctx->Driver.Error) {
2067 (*ctx->Driver.Error)( ctx );
2068 }
2069 }
2070
2071 /**
2072 * Execute glFinish().
2073 *
2074 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2075 * dd_function_table::Finish driver callback, if not NULL.
2076 */
2077 void GLAPIENTRY
2078 _mesa_Finish( void )
2079 {
2080 GET_CURRENT_CONTEXT(ctx);
2081 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2082 if (ctx->Driver.Finish) {
2083 (*ctx->Driver.Finish)( ctx );
2084 }
2085 }
2086
2087 /**
2088 * Execute glFlush().
2089 *
2090 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2091 * dd_function_table::Flush driver callback, if not NULL.
2092 */
2093 void GLAPIENTRY
2094 _mesa_Flush( void )
2095 {
2096 GET_CURRENT_CONTEXT(ctx);
2097 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2098 if (ctx->Driver.Flush) {
2099 (*ctx->Driver.Flush)( ctx );
2100 }
2101 }
2102
2103
2104 /*@}*/