3 * Mesa context/visual/framebuffer management functions.
8 * Mesa 3-D graphics library
11 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
20 * The above copyright notice and this permission notice shall be included
21 * in all copies or substantial portions of the Software.
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.
33 * \mainpage Mesa Main Module
35 * \section MainIntroduction Introduction
37 * The Mesa Main module consists of all the files in the main/ directory.
38 * Among the features of this module are:
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>
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.
52 * \section AboutDoxygen About Doxygen
54 * If you're viewing this information as Doxygen-generated HTML you'll
55 * see the documentation index at the top of this page.
57 * The first line lists the Mesa source code modules.
58 * The second line lists the indexes available for viewing the documentation
61 * Selecting the <b>Main page</b> link will display a summary of the module
64 * Selecting <b>Data Structures</b> will list all C structures.
66 * Selecting the <b>File List</b> link will list all the source files in
68 * Selecting a filename will show a list of all functions defined in that file.
70 * Selecting the <b>Data Fields</b> link will display a list of all
71 * documented structure members.
73 * Selecting the <b>Globals</b> link will display a list
74 * of all functions, structures, global variables and macros in the module.
85 #include "bufferobj.h"
93 #include "extensions.h"
98 #include "glapioffsets.h"
99 #include "histogram.h"
110 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
114 #include "simple_list.h"
117 #include "texcompress.h"
118 #include "teximage.h"
120 #include "texstate.h"
125 #include "math/m_translate.h"
126 #include "math/m_matrix.h"
127 #include "math/m_xform.h"
128 #include "math/mathmod.h"
130 #include "shaderobjects.h"
133 #include "sparc/sparc.h"
137 int MESA_VERBOSE = 0;
140 #ifndef MESA_DEBUG_FLAGS
141 int MESA_DEBUG_FLAGS = 0;
145 /* ubyte -> float conversion */
146 GLfloat _mesa_ubyte_to_float_color_tab[256];
149 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
152 /**********************************************************************/
153 /** \name OpenGL SI-style interface (new in Mesa 3.5)
156 * \note Most of these functions are never called in the Mesa subset.
162 * Destroy context callback.
165 * \return GL_TRUE on success, or GL_FALSE on failure.
168 * Called by window system/device driver (via __GLexports::destroyCurrent) when
169 * the rendering context is to be destroyed.
172 * Frees the context data and the context structure.
175 _mesa_destroyContext(__GLcontext *gc)
178 _mesa_free_context_data(gc);
185 * Unbind context callback.
188 * \return GL_TRUE on success, or GL_FALSE on failure.
191 * Called by window system/device driver (via __GLexports::loseCurrent)
192 * when the rendering context is made non-current.
198 _mesa_loseCurrent(__GLcontext *gc)
200 /* XXX unbind context from thread */
206 * Bind context callback.
209 * \return GL_TRUE on success, or GL_FALSE on failure.
212 * Called by window system/device driver (via __GLexports::makeCurrent)
213 * when the rendering context is made current.
219 _mesa_makeCurrent(__GLcontext *gc)
221 /* XXX bind context to thread */
227 * Share context callback.
230 * \param gcShare shared context.
231 * \return GL_TRUE on success, or GL_FALSE on failure.
234 * Called by window system/device driver (via __GLexports::shareContext)
237 * Update the shared context reference count, gl_shared_state::RefCount.
240 _mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
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);
247 gc->Shared = gcShare->Shared;
248 gc->Shared->RefCount++;
259 * Copy context callback.
262 _mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
265 _mesa_copy_context( src, dst, mask );
276 _mesa_forceCurrent(__GLcontext *gc)
283 * Windows/buffer resizing notification callback.
285 * \param gc GL context.
286 * \return GL_TRUE on success, or GL_FALSE on failure.
289 _mesa_notifyResize(__GLcontext *gc)
292 GLuint width, height;
293 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
294 if (!d || !d->getDrawableSize)
296 d->getDrawableSize( d, &x, &y, &width, &height );
297 /* update viewport, resize software buffers, etc. */
302 * Window/buffer destruction notification callback.
304 * \param gc GL context.
306 * Called when the context's window/buffer is going to be destroyed.
311 _mesa_notifyDestroy(__GLcontext *gc)
313 /* Unbind from it. */
318 * Swap buffers notification callback.
320 * \param gc GL context.
322 * Called by window system just before swapping buffers.
323 * We have to finish any pending rendering.
326 _mesa_notifySwapBuffers(__GLcontext *gc)
328 FLUSH_VERTICES( gc, 0 );
332 struct __GLdispatchStateRec *
333 _mesa_dispatchExec(__GLcontext *gc)
341 _mesa_beginDispatchOverride(__GLcontext *gc)
348 _mesa_endDispatchOverride(__GLcontext *gc)
357 * The window system will call these functions when it needs Mesa to do
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).
373 _mesa_init_default_exports(__GLexports *exports)
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;
394 * Exported OpenGL SI interface.
397 __glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
401 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
406 /* XXX doesn't work at this time */
407 _mesa_initialize_context(ctx, modes, NULL, NULL, NULL);
408 ctx->imports = *imports;
414 * Exported OpenGL SI interface.
417 __glCoreNopDispatch(void)
421 __gl_dispatch = __glNopDispatchState;
424 _glapi_set_dispatch(NULL);
431 /**********************************************************************/
432 /** \name GL Visual allocation/destruction */
433 /**********************************************************************/
437 * Allocates a GLvisual structure and initializes it via
438 * _mesa_initialize_visual().
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
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.
456 * \return pointer to new GLvisual or NULL if requested parameters can't be
459 * \note Need to add params for level and numAuxBuffers (at least)
462 _mesa_create_visual( GLboolean rgbFlag,
464 GLboolean stereoFlag,
473 GLint accumGreenBits,
475 GLint accumAlphaBits,
478 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
480 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
481 redBits, greenBits, blueBits, alphaBits,
482 indexBits, depthBits, stencilBits,
483 accumRedBits, accumGreenBits,
484 accumBlueBits, accumAlphaBits,
494 * Makes some sanity checks and fills in the fields of the
495 * GLvisual structure with the given parameters.
497 * \return GL_TRUE on success, or GL_FALSE on failure.
499 * \sa _mesa_create_visual() above for the parameter description.
501 * \note Need to add params for level and numAuxBuffers (at least)
504 _mesa_initialize_visual( GLvisual *vis,
507 GLboolean stereoFlag,
516 GLint accumGreenBits,
518 GLint accumAlphaBits,
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!?!).
527 assert(depthBits == 0 || depthBits > 1);
529 if (depthBits < 0 || depthBits > 32) {
532 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
535 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
538 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
541 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
544 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
548 vis->rgbMode = rgbFlag;
549 vis->doubleBufferMode = dbFlag;
550 vis->stereoMode = stereoFlag;
552 vis->redBits = redBits;
553 vis->greenBits = greenBits;
554 vis->blueBits = blueBits;
555 vis->alphaBits = alphaBits;
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;
565 vis->haveAccumBuffer = accumRedBits > 0;
566 vis->haveDepthBuffer = depthBits > 0;
567 vis->haveStencilBuffer = stencilBits > 0;
569 vis->numAuxBuffers = 0;
572 vis->samples = numSamples;
579 * Destroy a visual and free its memory.
583 * Frees the visual structure.
586 _mesa_destroy_visual( GLvisual *vis )
594 /**********************************************************************/
595 /** \name GL Framebuffer allocation/destruction */
596 /**********************************************************************/
600 * Allocate a GLframebuffer structure and initializes it via
601 * _mesa_initialize_framebuffer().
603 * A GLframebuffer is a structure which encapsulates the depth, stencil and
604 * accum buffers and related parameters.
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.
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?
616 * \return pointer to new GLframebuffer struct or NULL if error.
618 * \note Need to add softwareAuxBuffers parameter.
621 _mesa_create_framebuffer( const GLvisual *visual,
622 GLboolean softwareDepth,
623 GLboolean softwareStencil,
624 GLboolean softwareAccum,
625 GLboolean softwareAlpha )
627 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
630 _mesa_initialize_framebuffer(buffer, visual,
631 softwareDepth, softwareStencil,
632 softwareAccum, softwareAlpha );
639 * Makes some sanity checks and fills in the fields of the
640 * GLframebuffer structure with the given parameters.
642 * \sa _mesa_create_framebuffer() above for the parameter description.
645 _mesa_initialize_framebuffer( GLframebuffer *buffer,
646 const GLvisual *visual,
647 GLboolean softwareDepth,
648 GLboolean softwareStencil,
649 GLboolean softwareAccum,
650 GLboolean softwareAlpha )
652 GLboolean softwareAux = GL_FALSE;
656 _mesa_bzero(buffer, sizeof(GLframebuffer));
659 if (softwareDepth ) {
660 assert(visual->depthBits > 0);
662 if (softwareStencil) {
663 assert(visual->stencilBits > 0);
666 assert(visual->rgbMode);
667 assert(visual->accumRedBits > 0);
668 assert(visual->accumGreenBits > 0);
669 assert(visual->accumBlueBits > 0);
672 assert(visual->rgbMode);
673 assert(visual->alphaBits > 0);
676 buffer->Visual = *visual;
677 buffer->UseSoftwareDepthBuffer = softwareDepth;
678 buffer->UseSoftwareStencilBuffer = softwareStencil;
679 buffer->UseSoftwareAccumBuffer = softwareAccum;
680 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
681 buffer->UseSoftwareAuxBuffers = softwareAux;
686 * Free a framebuffer struct and its buffers.
688 * Calls _mesa_free_framebuffer_data() and frees the structure.
691 _mesa_destroy_framebuffer( GLframebuffer *buffer )
694 _mesa_free_framebuffer_data(buffer);
701 * Free the data hanging off of \p buffer, but not \p buffer itself.
703 * \param buffer framebuffer.
705 * Frees all the buffers associated with the structure.
708 _mesa_free_framebuffer_data( GLframebuffer *buffer )
713 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
714 MESA_PBUFFER_FREE( buffer->DepthBuffer );
715 buffer->DepthBuffer = NULL;
717 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
718 MESA_PBUFFER_FREE( buffer->Accum );
719 buffer->Accum = NULL;
721 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
722 MESA_PBUFFER_FREE( buffer->Stencil );
723 buffer->Stencil = NULL;
725 if (buffer->UseSoftwareAlphaBuffers){
726 if (buffer->FrontLeftAlpha) {
727 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
728 buffer->FrontLeftAlpha = NULL;
730 if (buffer->BackLeftAlpha) {
731 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
732 buffer->BackLeftAlpha = NULL;
734 if (buffer->FrontRightAlpha) {
735 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
736 buffer->FrontRightAlpha = NULL;
738 if (buffer->BackRightAlpha) {
739 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
740 buffer->BackRightAlpha = NULL;
748 /**********************************************************************/
749 /** \name Context allocation, initialization, destroying
751 * The purpose of the most initialization functions here is to provide the
752 * default state values according to the OpenGL specification.
754 /**********************************************************************/
758 * One-time initialization mutex lock.
760 * \sa Used by one_time_init().
762 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
765 * Calls all the various one-time-init functions in Mesa.
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
771 * \sa _mesa_init_lists(), _math_init().
774 one_time_init( GLcontext *ctx )
776 static GLboolean alreadyCalled = GL_FALSE;
778 _glthread_LOCK_MUTEX(OneTimeLock);
779 if (!alreadyCalled) {
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 );
795 for (i = 0; i < 256; i++) {
796 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
801 _mesa_init_sparc_glapi_relocs();
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.
810 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
814 _glapi_noop_enable_warnings(GL_FALSE);
817 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
818 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
821 alreadyCalled = GL_TRUE;
823 _glthread_UNLOCK_MUTEX(OneTimeLock);
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.
833 * \return pointer to a gl_shared_state structure on success, or NULL on
837 alloc_shared_state( GLcontext *ctx )
839 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
845 _glthread_INIT_MUTEX(ss->Mutex);
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();
853 #if FEATURE_ARB_vertex_program
854 ss->DefaultVertexProgram = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
855 if (!ss->DefaultVertexProgram)
858 #if FEATURE_ARB_fragment_program
859 ss->DefaultFragmentProgram = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
860 if (!ss->DefaultFragmentProgram)
863 #if FEATURE_ATI_fragment_shader
864 ss->DefaultFragmentShader = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_SHADER_ATI, 0);
865 if (!ss->DefaultFragmentShader)
869 ss->BufferObjects = _mesa_NewHashTable();
871 ss->GL2Objects = _mesa_NewHashTable ();
873 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
877 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
881 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
885 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
886 if (!ss->DefaultCubeMap)
889 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
890 if (!ss->DefaultRect)
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;
900 #if FEATURE_EXT_framebuffer_object
901 ss->FrameBuffers = _mesa_NewHashTable();
902 if (!ss->FrameBuffers)
904 ss->RenderBuffers = _mesa_NewHashTable();
905 if (!ss->RenderBuffers)
913 /* Ran out of memory at some point. Free everything and return NULL */
915 _mesa_DeleteHashTable(ss->DisplayList);
917 _mesa_DeleteHashTable(ss->TexObjects);
918 #if FEATURE_NV_vertex_program
920 _mesa_DeleteHashTable(ss->Programs);
922 #if FEATURE_ARB_vertex_program
923 if (ss->DefaultVertexProgram)
924 ctx->Driver.DeleteProgram(ctx, ss->DefaultVertexProgram);
926 #if FEATURE_ARB_fragment_program
927 if (ss->DefaultFragmentProgram)
928 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentProgram);
930 #if FEATURE_ATI_fragment_shader
931 if (ss->DefaultFragmentShader)
932 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentShader);
934 #if FEATURE_ARB_vertex_buffer_object
935 if (ss->BufferObjects)
936 _mesa_DeleteHashTable(ss->BufferObjects);
940 _mesa_DeleteHashTable (ss->GL2Objects);
942 #if FEATURE_EXT_framebuffer_object
943 if (ss->FrameBuffers)
944 _mesa_DeleteHashTable(ss->FrameBuffers);
945 if (ss->RenderBuffers)
946 _mesa_DeleteHashTable(ss->RenderBuffers);
950 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
952 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
954 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
955 if (ss->DefaultCubeMap)
956 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
958 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
965 * Deallocate a shared state context and all children structures.
967 * \param ctx GL context.
968 * \param ss shared state pointer.
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.
974 * \sa alloc_shared_state().
977 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
979 /* Free display lists */
981 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
983 _mesa_destroy_list(ctx, list);
989 _mesa_DeleteHashTable(ss->DisplayList);
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 */
1001 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
1003 struct gl_texture_object *texObj = (struct gl_texture_object *)
1004 _mesa_HashLookup(ss->TexObjects, texName);
1006 (*ctx->Driver.DeleteTexture)(ctx, texObj);
1007 _mesa_HashRemove(ss->TexObjects, texName);
1013 _mesa_DeleteHashTable(ss->TexObjects);
1015 #if FEATURE_NV_vertex_program
1016 /* Free vertex programs */
1018 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
1020 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
1023 ctx->Driver.DeleteProgram(ctx, p);
1024 _mesa_HashRemove(ss->Programs, prog);
1030 _mesa_DeleteHashTable(ss->Programs);
1032 #if FEATURE_ARB_vertex_program
1033 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
1035 #if FEATURE_ARB_fragment_program
1036 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
1038 #if FEATURE_ATI_fragment_shader
1039 _mesa_delete_program(ctx, ss->DefaultFragmentShader);
1042 #if FEATURE_ARB_vertex_buffer_object
1043 _mesa_DeleteHashTable(ss->BufferObjects);
1046 _mesa_DeleteHashTable (ss->GL2Objects);
1048 #if FEATURE_EXT_framebuffer_object
1049 _mesa_DeleteHashTable(ss->FrameBuffers);
1050 _mesa_DeleteHashTable(ss->RenderBuffers);
1053 _glthread_DESTROY_MUTEX(ss->Mutex);
1060 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
1063 _mesa_init_current( GLcontext *ctx )
1068 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
1069 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
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 );
1078 ctx->Current.Index = 1;
1079 ctx->Current.EdgeFlag = GL_TRUE;
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).
1089 _mesa_init_constants( GLcontext *ctx )
1093 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1094 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
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;
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;
1147 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1148 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
1150 /* If we're running in the X server, do bounds checking to prevent
1151 * segfaults and server crashes!
1153 #if defined(XFree86LOADER) && defined(IN_MODULE)
1154 ctx->Const.CheckArrayBounds = GL_TRUE;
1156 ctx->Const.CheckArrayBounds = GL_FALSE;
1159 ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
1161 ctx->Const.ColorReadFormat = GL_RGBA;
1162 ctx->Const.ColorReadType = GL_UNSIGNED_BYTE;
1165 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
1170 * Initialize the attribute groups in a GL context.
1172 * \param ctx GL context.
1174 * Initializes all the attributes, calling the respective <tt>init*</tt>
1175 * functions for the more complex data structures.
1178 init_attrib_groups( GLcontext *ctx )
1183 _mesa_init_constants( ctx );
1186 _mesa_init_extensions( ctx );
1188 /* Attribute Groups */
1189 _mesa_init_accum( ctx );
1190 _mesa_init_attrib( ctx );
1191 _mesa_init_buffers( ctx );
1192 _mesa_init_buffer_objects( ctx );
1193 _mesa_init_color( ctx );
1194 _mesa_init_colortables( ctx );
1195 _mesa_init_current( ctx );
1196 _mesa_init_depth( ctx );
1197 _mesa_init_debug( ctx );
1198 _mesa_init_display_list( ctx );
1199 _mesa_init_eval( ctx );
1200 _mesa_init_feedback( ctx );
1201 _mesa_init_fog( ctx );
1202 _mesa_init_histogram( ctx );
1203 _mesa_init_hint( ctx );
1204 _mesa_init_line( ctx );
1205 _mesa_init_lighting( ctx );
1206 _mesa_init_matrix( ctx );
1207 _mesa_init_occlude( ctx );
1208 _mesa_init_pixel( ctx );
1209 _mesa_init_point( ctx );
1210 _mesa_init_polygon( ctx );
1211 _mesa_init_program( ctx );
1212 _mesa_init_rastpos( ctx );
1213 _mesa_init_shaderobjects (ctx);
1214 _mesa_init_stencil( ctx );
1215 _mesa_init_transform( ctx );
1216 _mesa_init_varray( ctx );
1217 _mesa_init_viewport( ctx );
1219 if (!_mesa_init_texture( ctx ))
1222 _mesa_init_texture_s3tc( ctx );
1223 _mesa_init_texture_fxt1( ctx );
1226 ctx->NewState = _NEW_ALL;
1227 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1229 #if CHAN_TYPE == GL_FLOAT
1230 ctx->ClampFragmentColors = GL_FALSE; /* XXX temporary */
1232 ctx->ClampFragmentColors = GL_TRUE;
1234 ctx->ClampVertexColors = GL_TRUE;
1241 * If the DRI libGL.so library is old, it may not have the entrypoints for
1242 * some recent OpenGL extensions. Dynamically add them now.
1243 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1244 * and driver code, this won't be an issue (and calling this function won't
1248 add_newer_entrypoints(void)
1251 static const struct {
1252 const char * const name;
1255 newer_entrypoints[] = {
1256 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1257 { "glWindowPos2dARB", 513 },
1258 { "glWindowPos2dvARB", 514 },
1259 { "glWindowPos2fARB", 515 },
1260 { "glWindowPos2fvARB", 516 },
1261 { "glWindowPos2iARB", 517 },
1262 { "glWindowPos2ivARB", 518 },
1263 { "glWindowPos2sARB", 519 },
1264 { "glWindowPos2svARB", 520 },
1265 { "glWindowPos3dARB", 521 },
1266 { "glWindowPos3dvARB", 522 },
1267 { "glWindowPos3fARB", 523 },
1268 { "glWindowPos3fvARB", 524 },
1269 { "glWindowPos3iARB", 525 },
1270 { "glWindowPos3ivARB", 526 },
1271 { "glWindowPos3sARB", 527 },
1272 { "glWindowPos3svARB", 528 },
1273 #if FEATURE_NV_vertex_program
1274 { "glAreProgramsResidentNV", 578 },
1275 { "glBindProgramNV", 579 },
1276 { "glDeleteProgramsNV", 580 },
1277 { "glExecuteProgramNV", 581 },
1278 { "glGenProgramsNV", 582 },
1279 { "glGetProgramParameterdvNV", 583 },
1280 { "glGetProgramParameterfvNV", 584 },
1281 { "glGetProgramivNV", 585 },
1282 { "glGetProgramStringNV", 586 },
1283 { "glGetTrackMatrixivNV", 587 },
1284 { "glGetVertexAttribdvNV", 588 },
1285 { "glGetVertexAttribfvNV", 589 },
1286 { "glGetVertexAttribivNV", 590 },
1287 { "glGetVertexAttribPointervNV", 591 },
1288 { "glIsProgramNV", 592 },
1289 { "glLoadProgramNV", 593 },
1290 { "glProgramParameter4dNV", 594 },
1291 { "glProgramParameter4dvNV", 595 },
1292 { "glProgramParameter4fNV", 596 },
1293 { "glProgramParameter4fvNV", 597 },
1294 { "glProgramParameters4dvNV", 598 },
1295 { "glProgramParameters4fvNV", 599 },
1296 { "glRequestResidentProgramsNV", 600 },
1297 { "glTrackMatrixNV", 601 },
1298 { "glVertexAttribPointerNV", 602 },
1299 { "glVertexAttrib1dNV", 603 },
1300 { "glVertexAttrib1dvNV", 604 },
1301 { "glVertexAttrib1fNV", 605 },
1302 { "glVertexAttrib1fvNV", 606 },
1303 { "glVertexAttrib1sNV", 607 },
1304 { "glVertexAttrib1svNV", 608 },
1305 { "glVertexAttrib2dNV", 609 },
1306 { "glVertexAttrib2dvNV", 610 },
1307 { "glVertexAttrib2fNV", 611 },
1308 { "glVertexAttrib2fvNV", 612 },
1309 { "glVertexAttrib2sNV", 613 },
1310 { "glVertexAttrib2svNV", 614 },
1311 { "glVertexAttrib3dNV", 615 },
1312 { "glVertexAttrib3dvNV", 616 },
1313 { "glVertexAttrib3fNV", 617 },
1314 { "glVertexAttrib3fvNV", 618 },
1315 { "glVertexAttrib3sNV", 619 },
1316 { "glVertexAttrib3svNV", 620 },
1317 { "glVertexAttrib4dNV", 621 },
1318 { "glVertexAttrib4dvNV", 622 },
1319 { "glVertexAttrib4fNV", 623 },
1320 { "glVertexAttrib4fvNV", 624 },
1321 { "glVertexAttrib4sNV", 625 },
1322 { "glVertexAttrib4svNV", 626 },
1323 { "glVertexAttrib4ubNV", 627 },
1324 { "glVertexAttrib4ubvNV", 628 },
1325 { "glVertexAttribs1dvNV", 629 },
1326 { "glVertexAttribs1fvNV", 630 },
1327 { "glVertexAttribs1svNV", 631 },
1328 { "glVertexAttribs2dvNV", 632 },
1329 { "glVertexAttribs2fvNV", 633 },
1330 { "glVertexAttribs2svNV", 634 },
1331 { "glVertexAttribs3dvNV", 635 },
1332 { "glVertexAttribs3fvNV", 636 },
1333 { "glVertexAttribs3svNV", 637 },
1334 { "glVertexAttribs4dvNV", 638 },
1335 { "glVertexAttribs4fvNV", 639 },
1336 { "glVertexAttribs4svNV", 640 },
1337 { "glVertexAttribs4ubvNV", 641 },
1339 { "glPointParameteriNV", 642 },
1340 { "glPointParameterivNV", 643 },
1341 { "glMultiDrawArraysEXT", 644 },
1342 { "glMultiDrawElementsEXT", 645 },
1343 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1344 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1345 { "glActiveStencilFaceEXT", 646 },
1346 #if FEATURE_NV_fence
1347 { "glDeleteFencesNV", 647 },
1348 { "glGenFencesNV", 648 },
1349 { "glIsFenceNV", 649 },
1350 { "glTestFenceNV", 650 },
1351 { "glGetFenceivNV", 651 },
1352 { "glFinishFenceNV", 652 },
1353 { "glSetFenceNV", 653 },
1355 #if FEATURE_NV_fragment_program
1356 { "glProgramNamedParameter4fNV", 682 },
1357 { "glProgramNamedParameter4dNV", 683 },
1358 { "glProgramNamedParameter4fvNV", 683 },
1359 { "glProgramNamedParameter4dvNV", 684 },
1360 { "glGetProgramNamedParameterfvNV", 685 },
1361 { "glGetProgramNamedParameterdvNV", 686 },
1363 #if FEATURE_ARB_vertex_program
1364 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1365 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1366 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1367 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1368 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1369 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1370 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1371 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1372 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1373 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1374 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1375 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1376 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1377 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1378 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1379 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1380 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1381 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1382 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1383 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1384 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1385 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1386 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1387 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1388 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1389 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1390 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1391 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1392 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1393 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1394 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1395 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1396 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1397 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1398 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1399 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1400 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1401 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1402 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1403 { "glProgramStringARB", _gloffset_ProgramStringARB },
1404 { "glBindProgramARB", _gloffset_BindProgramNV },
1405 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1406 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1407 { "glIsProgramARB", _gloffset_IsProgramNV },
1408 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1409 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1410 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1411 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1412 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1413 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1414 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1415 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1416 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1417 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1418 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1419 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1420 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1421 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1422 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1423 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1424 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1425 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
1427 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1428 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
1429 /* GL_EXT_stencil_two_side */
1430 { "glActiveStencilFaceEXT", _gloffset_ActiveStencilFaceEXT },
1431 /* GL_ARB_draw_buffers */
1432 { "glDrawBuffersARB", _gloffset_DrawBuffersARB },
1433 #if FEATURE_ATI_fragment_shader
1434 { "glGenFragmentShadersATI", _gloffset_GenFragmentShadersATI },
1435 { "glBindFragmentShaderATI", _gloffset_BindFragmentShaderATI },
1436 { "glDeleteFragmentShaderATI", _gloffset_DeleteFragmentShaderATI },
1437 { "glBeginFragmentShaderATI", _gloffset_BeginFragmentShaderATI },
1438 { "glEndFragmentShaderATI", _gloffset_EndFragmentShaderATI },
1439 { "glPassTexCoordATI", _gloffset_PassTexCoordATI },
1440 { "glSampleMapATI", _gloffset_SampleMapATI },
1441 { "glColorFragmentOp1ATI", _gloffset_ColorFragmentOp1ATI },
1442 { "glColorFragmentOp2ATI", _gloffset_ColorFragmentOp2ATI },
1443 { "glColorFragmentOp3ATI", _gloffset_ColorFragmentOp3ATI },
1444 { "glAlphaFragmentOp1ATI", _gloffset_AlphaFragmentOp1ATI },
1445 { "glAlphaFragmentOp2ATI", _gloffset_AlphaFragmentOp2ATI },
1446 { "glAlphaFragmentOp3ATI", _gloffset_AlphaFragmentOp3ATI },
1447 { "glSetFragmentShaderConstantATI", _gloffset_SetFragmentShaderConstantATI },
1451 for (i = 0; i < Elements(newer_entrypoints); i++ ) {
1452 _glapi_add_entrypoint( newer_entrypoints[i].name,
1453 newer_entrypoints[i].offset );
1459 * This is the default function we plug into all dispatch table slots
1460 * This helps prevents a segfault when someone calls a GL function without
1461 * first checking if the extension's supported.
1466 _mesa_problem(NULL, "User called no-op dispatch function (an unsupported extension function?)");
1472 * Allocate and initialize a new dispatch table.
1474 static struct _glapi_table *
1475 alloc_dispatch_table(void)
1477 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1478 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1479 * Mesa we do this to accomodate different versions of libGL and various
1482 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
1483 sizeof(struct _glapi_table) / sizeof(_glapi_proc));
1484 struct _glapi_table *table =
1485 (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc));
1487 _glapi_proc *entry = (_glapi_proc *) table;
1489 for (i = 0; i < numEntries; i++) {
1490 entry[i] = (_glapi_proc) generic_nop;
1498 * Initialize a GLcontext struct (rendering context).
1500 * This includes allocating all the other structs and arrays which hang off of
1501 * the context by pointers.
1502 * Note that the driver needs to pass in its dd_function_table here since
1503 * we need to at least call driverFunctions->NewTextureObject to create the
1504 * default texture objects.
1506 * Called by _mesa_create_context().
1508 * Performs the imports and exports callback tables initialization, and
1509 * miscellaneous one-time initializations. If no shared context is supplied one
1510 * is allocated, and increase its reference count. Setups the GL API dispatch
1511 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1512 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1515 * \param ctx the context to initialize
1516 * \param visual describes the visual attributes for this context
1517 * \param share_list points to context to share textures, display lists,
1519 * \param driverFunctions table of device driver functions for this context
1521 * \param driverContext pointer to driver-specific context data
1524 _mesa_initialize_context( GLcontext *ctx,
1525 const GLvisual *visual,
1526 GLcontext *share_list,
1527 const struct dd_function_table *driverFunctions,
1528 void *driverContext )
1530 ASSERT(driverContext);
1531 assert(driverFunctions->NewTextureObject);
1533 /* If the driver wants core Mesa to use special imports, it'll have to
1534 * override these defaults.
1536 _mesa_init_default_imports( &(ctx->imports), driverContext );
1538 /* initialize the exports (Mesa functions called by the window system) */
1539 _mesa_init_default_exports( &(ctx->exports) );
1541 /* misc one-time initializations */
1544 ctx->Visual = *visual;
1545 ctx->DrawBuffer = NULL;
1546 ctx->ReadBuffer = NULL;
1548 /* Plug in driver functions and context pointer here.
1549 * This is important because when we call alloc_shared_state() below
1550 * we'll call ctx->Driver.NewTextureObject() to create the default
1553 ctx->Driver = *driverFunctions;
1554 ctx->DriverCtx = driverContext;
1557 /* share state with another context */
1558 ctx->Shared = share_list->Shared;
1561 /* allocate new, unshared state */
1562 if (!alloc_shared_state( ctx )) {
1566 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1567 ctx->Shared->RefCount++;
1568 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1570 if (!init_attrib_groups( ctx )) {
1571 free_shared_state(ctx, ctx->Shared);
1575 /* libGL ABI coordination */
1576 add_newer_entrypoints();
1578 /* setup the API dispatch tables */
1579 ctx->Exec = alloc_dispatch_table();
1580 ctx->Save = alloc_dispatch_table();
1581 if (!ctx->Exec || !ctx->Save) {
1582 free_shared_state(ctx, ctx->Shared);
1584 _mesa_free(ctx->Exec);
1586 _mesa_init_exec_table(ctx->Exec);
1587 ctx->CurrentDispatch = ctx->Exec;
1589 _mesa_init_dlist_table(ctx->Save);
1590 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
1591 /* Neutral tnl module stuff */
1592 _mesa_init_exec_vtxfmt( ctx );
1593 ctx->TnlModule.Current = NULL;
1594 ctx->TnlModule.SwapCount = 0;
1602 * Allocate and initialize a GLcontext structure.
1603 * Note that the driver needs to pass in its dd_function_table here since
1604 * we need to at least call driverFunctions->NewTextureObject to initialize
1605 * the rendering context.
1607 * \param visual a GLvisual pointer (we copy the struct contents)
1608 * \param share_list another context to share display lists with or NULL
1609 * \param driverFunctions points to the dd_function_table into which the
1610 * driver has plugged in all its special functions.
1611 * \param driverCtx points to the device driver's private context state
1613 * \return pointer to a new __GLcontextRec or NULL if error.
1616 _mesa_create_context( const GLvisual *visual,
1617 GLcontext *share_list,
1618 const struct dd_function_table *driverFunctions,
1619 void *driverContext )
1625 ASSERT(driverContext);
1627 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
1631 if (_mesa_initialize_context(ctx, visual, share_list,
1632 driverFunctions, driverContext)) {
1643 * Free the data associated with the given context.
1645 * But doesn't free the GLcontext struct itself.
1647 * \sa _mesa_initialize_context() and init_attrib_groups().
1650 _mesa_free_context_data( GLcontext *ctx )
1652 /* if we're destroying the current context, unbind it first */
1653 if (ctx == _mesa_get_current_context()) {
1654 _mesa_make_current(NULL, NULL);
1657 _mesa_free_lighting_data( ctx );
1658 _mesa_free_eval_data( ctx );
1659 _mesa_free_texture_data( ctx );
1660 _mesa_free_matrix_data( ctx );
1661 _mesa_free_viewport_data( ctx );
1662 _mesa_free_colortables_data( ctx );
1663 _mesa_free_program_data(ctx);
1664 _mesa_free_occlude_data(ctx);
1666 #if FEATURE_ARB_vertex_buffer_object
1667 _mesa_delete_buffer_object(ctx, ctx->Array.NullBufferObj);
1670 /* free dispatch tables */
1671 _mesa_free(ctx->Exec);
1672 _mesa_free(ctx->Save);
1674 /* Shared context state (display lists, textures, etc) */
1675 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1676 ctx->Shared->RefCount--;
1677 assert(ctx->Shared->RefCount >= 0);
1678 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1679 if (ctx->Shared->RefCount == 0) {
1680 /* free shared state */
1681 free_shared_state( ctx, ctx->Shared );
1684 if (ctx->Extensions.String)
1685 FREE((void *) ctx->Extensions.String);
1690 * Destroy a GLcontext structure.
1692 * \param ctx GL context.
1694 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1697 _mesa_destroy_context( GLcontext *ctx )
1700 _mesa_free_context_data(ctx);
1701 FREE( (void *) ctx );
1708 * Copy attribute groups from one context to another.
1710 * \param src source context
1711 * \param dst destination context
1712 * \param mask bitwise OR of GL_*_BIT flags
1714 * According to the bits specified in \p mask, copies the corresponding
1715 * attributes from \p src into \p dst. For many of the attributes a simple \c
1716 * memcpy is not enough due to the existence of internal pointers in their data
1720 _mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
1722 if (mask & GL_ACCUM_BUFFER_BIT) {
1724 dst->Accum = src->Accum;
1726 if (mask & GL_COLOR_BUFFER_BIT) {
1728 dst->Color = src->Color;
1730 if (mask & GL_CURRENT_BIT) {
1732 dst->Current = src->Current;
1734 if (mask & GL_DEPTH_BUFFER_BIT) {
1736 dst->Depth = src->Depth;
1738 if (mask & GL_ENABLE_BIT) {
1741 if (mask & GL_EVAL_BIT) {
1743 dst->Eval = src->Eval;
1745 if (mask & GL_FOG_BIT) {
1747 dst->Fog = src->Fog;
1749 if (mask & GL_HINT_BIT) {
1751 dst->Hint = src->Hint;
1753 if (mask & GL_LIGHTING_BIT) {
1755 /* begin with memcpy */
1756 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1757 /* fixup linked lists to prevent pointer insanity */
1758 make_empty_list( &(dst->Light.EnabledList) );
1759 for (i = 0; i < MAX_LIGHTS; i++) {
1760 if (dst->Light.Light[i].Enabled) {
1761 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1765 if (mask & GL_LINE_BIT) {
1767 dst->Line = src->Line;
1769 if (mask & GL_LIST_BIT) {
1771 dst->List = src->List;
1773 if (mask & GL_PIXEL_MODE_BIT) {
1775 dst->Pixel = src->Pixel;
1777 if (mask & GL_POINT_BIT) {
1779 dst->Point = src->Point;
1781 if (mask & GL_POLYGON_BIT) {
1783 dst->Polygon = src->Polygon;
1785 if (mask & GL_POLYGON_STIPPLE_BIT) {
1786 /* Use loop instead of MEMCPY due to problem with Portland Group's
1787 * C compiler. Reported by John Stone.
1790 for (i = 0; i < 32; i++) {
1791 dst->PolygonStipple[i] = src->PolygonStipple[i];
1794 if (mask & GL_SCISSOR_BIT) {
1796 dst->Scissor = src->Scissor;
1798 if (mask & GL_STENCIL_BUFFER_BIT) {
1800 dst->Stencil = src->Stencil;
1802 if (mask & GL_TEXTURE_BIT) {
1803 /* Cannot memcpy because of pointers */
1804 _mesa_copy_texture_state(src, dst);
1806 if (mask & GL_TRANSFORM_BIT) {
1808 dst->Transform = src->Transform;
1810 if (mask & GL_VIEWPORT_BIT) {
1811 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1812 dst->Viewport.X = src->Viewport.X;
1813 dst->Viewport.Y = src->Viewport.Y;
1814 dst->Viewport.Width = src->Viewport.Width;
1815 dst->Viewport.Height = src->Viewport.Height;
1816 dst->Viewport.Near = src->Viewport.Near;
1817 dst->Viewport.Far = src->Viewport.Far;
1818 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
1821 /* XXX FIXME: Call callbacks?
1823 dst->NewState = _NEW_ALL;
1829 * Check if the given context can render into the given framebuffer
1830 * by checking visual attributes.
1831 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1834 check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1836 const GLvisual *ctxvis = &ctx->Visual;
1837 const GLvisual *bufvis = &buffer->Visual;
1839 if (ctxvis == bufvis)
1842 if (ctxvis->rgbMode != bufvis->rgbMode)
1844 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1846 if (ctxvis->stereoMode && !bufvis->stereoMode)
1848 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1850 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1852 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1854 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1856 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1858 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1860 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1862 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1870 * Set the current context, binding the given frame buffer to the context.
1872 * \param newCtx new GL context.
1873 * \param buffer framebuffer.
1875 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
1878 _mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1880 _mesa_make_current2( newCtx, buffer, buffer );
1884 * Bind the given context to the given draw-buffer and read-buffer and
1885 * make it the current context for this thread.
1887 * \param newCtx new GL context. If NULL then there will be no current GL
1889 * \param drawBuffer draw framebuffer.
1890 * \param readBuffer read framebuffer.
1892 * Check that the context's and framebuffer's visuals are compatible, returning
1893 * immediately otherwise. Sets the glapi current context via
1894 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1895 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1896 * Calls dd_function_table::MakeCurrent callback if defined.
1898 * When a context is bound by the first time and the \c MESA_INFO environment
1899 * variable is set it calls print_info() as an aid for remote user
1903 _mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1904 GLframebuffer *readBuffer )
1907 _mesa_debug(newCtx, "_mesa_make_current2()\n");
1909 /* Check that the context's and framebuffer's visuals are compatible.
1911 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1912 if (!check_compatible(newCtx, drawBuffer))
1915 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1916 if (!check_compatible(newCtx, readBuffer))
1920 /* We call this function periodically (just here for now) in
1921 * order to detect when multithreading has begun.
1923 _glapi_check_multithread();
1925 _glapi_set_context((void *) newCtx);
1926 ASSERT(_mesa_get_current_context() == newCtx);
1930 _glapi_set_dispatch(NULL); /* none current */
1933 _glapi_set_dispatch(newCtx->CurrentDispatch);
1935 if (drawBuffer && readBuffer) {
1936 /* TODO: check if newCtx and buffer's visual match??? */
1937 newCtx->DrawBuffer = drawBuffer;
1938 newCtx->ReadBuffer = readBuffer;
1939 newCtx->NewState |= _NEW_BUFFERS;
1942 if (!drawBuffer->Initialized) {
1943 /* get initial window size */
1944 GLuint bufWidth, bufHeight;
1945 /* ask device driver for size of the buffer */
1946 (*newCtx->Driver.GetBufferSize)(drawBuffer, &bufWidth, &bufHeight);
1947 /* set initial buffer size */
1948 drawBuffer->Width = bufWidth;
1949 drawBuffer->Height = bufHeight;
1950 newCtx->Driver.ResizeBuffers( drawBuffer );
1951 drawBuffer->Initialized = GL_TRUE;
1954 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
1955 /* get initial window size */
1956 GLuint bufWidth, bufHeight;
1957 /* ask device driver for size of the buffer */
1958 (*newCtx->Driver.GetBufferSize)(readBuffer, &bufWidth, &bufHeight);
1959 /* set initial buffer size */
1960 readBuffer->Width = bufWidth;
1961 readBuffer->Height = bufHeight;
1962 newCtx->Driver.ResizeBuffers( readBuffer );
1963 readBuffer->Initialized = GL_TRUE;
1966 if (newCtx->FirstTimeCurrent) {
1967 /* set initial viewport and scissor size now */
1968 _mesa_set_viewport(newCtx, 0, 0, drawBuffer->Width, drawBuffer->Height);
1969 newCtx->Scissor.Width = drawBuffer->Width;
1970 newCtx->Scissor.Height = drawBuffer->Height;
1974 /* Alert the driver - usually passed on to the sw t&l module,
1975 * but also used to detect threaded cases in the radeon codegen
1978 if (newCtx->Driver.MakeCurrent)
1979 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1981 /* We can use this to help debug user's problems. Tell them to set
1982 * the MESA_INFO env variable before running their app. Then the
1983 * first time each context is made current we'll print some useful
1986 if (newCtx->FirstTimeCurrent) {
1987 if (_mesa_getenv("MESA_INFO")) {
1990 newCtx->FirstTimeCurrent = GL_FALSE;
1996 * Get current context for the calling thread.
1998 * \return pointer to the current GL context.
2000 * Calls _glapi_get_context(). This isn't the fastest way to get the current
2001 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
2004 _mesa_get_current_context( void )
2006 return (GLcontext *) _glapi_get_context();
2010 * Get context's current API dispatch table.
2012 * It'll either be the immediate-mode execute dispatcher or the display list
2013 * compile dispatcher.
2015 * \param ctx GL context.
2017 * \return pointer to dispatch_table.
2019 * Simply returns __GLcontextRec::CurrentDispatch.
2021 struct _glapi_table *
2022 _mesa_get_dispatch(GLcontext *ctx)
2024 return ctx->CurrentDispatch;
2030 /**********************************************************************/
2031 /** \name Miscellaneous functions */
2032 /**********************************************************************/
2038 * \param ctx GL context.
2039 * \param error error code.
2041 * Records the given error code and call the driver's dd_function_table::Error
2042 * function if defined.
2045 * This is called via _mesa_error().
2048 _mesa_record_error( GLcontext *ctx, GLenum error )
2053 if (ctx->ErrorValue == GL_NO_ERROR) {
2054 ctx->ErrorValue = error;
2057 /* Call device driver's error handler, if any. This is used on the Mac. */
2058 if (ctx->Driver.Error) {
2059 (*ctx->Driver.Error)( ctx );
2064 * Execute glFinish().
2066 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2067 * dd_function_table::Finish driver callback, if not NULL.
2070 _mesa_Finish( void )
2072 GET_CURRENT_CONTEXT(ctx);
2073 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2074 if (ctx->Driver.Finish) {
2075 (*ctx->Driver.Finish)( ctx );
2080 * Execute glFlush().
2082 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2083 * dd_function_table::Flush driver callback, if not NULL.
2088 GET_CURRENT_CONTEXT(ctx);
2089 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2090 if (ctx->Driver.Flush) {
2091 (*ctx->Driver.Flush)( ctx );