3 * Mesa context/visual/framebuffer management functions.
8 * Mesa 3-D graphics library
11 * Copyright (C) 1999-2006 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.
86 #include "bufferobj.h"
94 #include "extensions.h"
98 #include "framebuffer.h"
100 #include "glthread.h"
101 #include "glapioffsets.h"
102 #include "histogram.h"
105 #include "atifragshader.h"
114 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
118 #include "simple_list.h"
121 #include "texcompress.h"
122 #include "teximage.h"
124 #include "texstate.h"
130 #include "math/m_translate.h"
131 #include "math/m_matrix.h"
132 #include "math/m_xform.h"
133 #include "math/mathmod.h"
135 #include "shader_api.h"
138 #include "sparc/sparc.h"
142 int MESA_VERBOSE
= 0;
145 #ifndef MESA_DEBUG_FLAGS
146 int MESA_DEBUG_FLAGS
= 0;
150 /* ubyte -> float conversion */
151 GLfloat _mesa_ubyte_to_float_color_tab
[256];
154 free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
);
157 /**********************************************************************/
158 /** \name OpenGL SI-style interface (new in Mesa 3.5)
161 * \note Most of these functions are never called in the Mesa subset.
167 * Destroy context callback.
170 * \return GL_TRUE on success, or GL_FALSE on failure.
173 * Called by window system/device driver (via __GLexports::destroyCurrent) when
174 * the rendering context is to be destroyed.
177 * Frees the context data and the context structure.
180 _mesa_destroyContext(__GLcontext
*gc
)
183 _mesa_free_context_data(gc
);
190 * Unbind context callback.
193 * \return GL_TRUE on success, or GL_FALSE on failure.
196 * Called by window system/device driver (via __GLexports::loseCurrent)
197 * when the rendering context is made non-current.
203 _mesa_loseCurrent(__GLcontext
*gc
)
205 /* XXX unbind context from thread */
211 * Bind context callback.
214 * \return GL_TRUE on success, or GL_FALSE on failure.
217 * Called by window system/device driver (via __GLexports::makeCurrent)
218 * when the rendering context is made current.
224 _mesa_makeCurrent(__GLcontext
*gc
)
226 /* XXX bind context to thread */
232 * Share context callback.
235 * \param gcShare shared context.
236 * \return GL_TRUE on success, or GL_FALSE on failure.
239 * Called by window system/device driver (via __GLexports::shareContext)
242 * Update the shared context reference count, gl_shared_state::RefCount.
245 _mesa_shareContext(__GLcontext
*gc
, __GLcontext
*gcShare
)
247 if (gc
&& gcShare
&& gc
->Shared
&& gcShare
->Shared
) {
248 gc
->Shared
->RefCount
--;
249 if (gc
->Shared
->RefCount
== 0) {
250 free_shared_state(gc
, gc
->Shared
);
252 gc
->Shared
= gcShare
->Shared
;
253 gc
->Shared
->RefCount
++;
264 * Copy context callback.
267 _mesa_copyContext(__GLcontext
*dst
, const __GLcontext
*src
, GLuint mask
)
270 _mesa_copy_context( src
, dst
, mask
);
281 _mesa_forceCurrent(__GLcontext
*gc
)
288 * Windows/buffer resizing notification callback.
290 * \param gc GL context.
291 * \return GL_TRUE on success, or GL_FALSE on failure.
294 _mesa_notifyResize(__GLcontext
*gc
)
296 /* 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
*) _mesa_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 object with the given parameters. If the caller needs
496 * to set additional fields, he should just probably init the whole GLvisual
498 * \return GL_TRUE on success, or GL_FALSE on failure.
500 * \sa _mesa_create_visual() above for the parameter description.
503 _mesa_initialize_visual( GLvisual
*vis
,
506 GLboolean stereoFlag
,
515 GLint accumGreenBits
,
517 GLint accumAlphaBits
,
522 if (depthBits
< 0 || depthBits
> 32) {
525 if (stencilBits
< 0 || stencilBits
> STENCIL_BITS
) {
528 assert(accumRedBits
>= 0);
529 assert(accumGreenBits
>= 0);
530 assert(accumBlueBits
>= 0);
531 assert(accumAlphaBits
>= 0);
533 vis
->rgbMode
= rgbFlag
;
534 vis
->doubleBufferMode
= dbFlag
;
535 vis
->stereoMode
= stereoFlag
;
537 vis
->redBits
= redBits
;
538 vis
->greenBits
= greenBits
;
539 vis
->blueBits
= blueBits
;
540 vis
->alphaBits
= alphaBits
;
541 vis
->rgbBits
= redBits
+ greenBits
+ blueBits
;
543 vis
->indexBits
= indexBits
;
544 vis
->depthBits
= depthBits
;
545 vis
->stencilBits
= stencilBits
;
547 vis
->accumRedBits
= accumRedBits
;
548 vis
->accumGreenBits
= accumGreenBits
;
549 vis
->accumBlueBits
= accumBlueBits
;
550 vis
->accumAlphaBits
= accumAlphaBits
;
552 vis
->haveAccumBuffer
= accumRedBits
> 0;
553 vis
->haveDepthBuffer
= depthBits
> 0;
554 vis
->haveStencilBuffer
= stencilBits
> 0;
556 vis
->numAuxBuffers
= 0;
559 vis
->sampleBuffers
= numSamples
> 0 ? 1 : 0;
560 vis
->samples
= numSamples
;
567 * Destroy a visual and free its memory.
571 * Frees the visual structure.
574 _mesa_destroy_visual( GLvisual
*vis
)
582 /**********************************************************************/
583 /** \name Context allocation, initialization, destroying
585 * The purpose of the most initialization functions here is to provide the
586 * default state values according to the OpenGL specification.
588 /**********************************************************************/
592 * One-time initialization mutex lock.
594 * \sa Used by one_time_init().
596 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock
);
599 * Calls all the various one-time-init functions in Mesa.
601 * While holding a global mutex lock, calls several initialization functions,
602 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
608 one_time_init( GLcontext
*ctx
)
610 static GLboolean alreadyCalled
= GL_FALSE
;
612 _glthread_LOCK_MUTEX(OneTimeLock
);
613 if (!alreadyCalled
) {
616 /* do some implementation tests */
617 assert( sizeof(GLbyte
) == 1 );
618 assert( sizeof(GLubyte
) == 1 );
619 assert( sizeof(GLshort
) == 2 );
620 assert( sizeof(GLushort
) == 2 );
621 assert( sizeof(GLint
) == 4 );
622 assert( sizeof(GLuint
) == 4 );
627 for (i
= 0; i
< 256; i
++) {
628 _mesa_ubyte_to_float_color_tab
[i
] = (float) i
/ 255.0F
;
633 _mesa_init_sparc_glapi_relocs();
635 if (_mesa_getenv("MESA_DEBUG")) {
636 _glapi_noop_enable_warnings(GL_TRUE
);
637 _glapi_set_warning_func( (_glapi_warning_func
) _mesa_warning
);
640 _glapi_noop_enable_warnings(GL_FALSE
);
643 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
644 _mesa_debug(ctx
, "Mesa %s DEBUG build %s %s\n",
645 MESA_VERSION_STRING
, __DATE__
, __TIME__
);
648 alreadyCalled
= GL_TRUE
;
650 _glthread_UNLOCK_MUTEX(OneTimeLock
);
655 * Allocate and initialize a shared context state structure.
656 * Initializes the display list, texture objects and vertex programs hash
657 * tables, allocates the texture objects. If it runs out of memory, frees
658 * everything already allocated before returning NULL.
660 * \return pointer to a gl_shared_state structure on success, or NULL on
664 alloc_shared_state( GLcontext
*ctx
)
666 struct gl_shared_state
*ss
= CALLOC_STRUCT(gl_shared_state
);
672 _glthread_INIT_MUTEX(ss
->Mutex
);
674 ss
->DisplayList
= _mesa_NewHashTable();
675 ss
->TexObjects
= _mesa_NewHashTable();
676 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
677 ss
->Programs
= _mesa_NewHashTable();
680 #if FEATURE_ARB_vertex_program
681 ss
->DefaultVertexProgram
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
682 if (!ss
->DefaultVertexProgram
)
685 #if FEATURE_ARB_fragment_program
686 ss
->DefaultFragmentProgram
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
687 if (!ss
->DefaultFragmentProgram
)
690 #if FEATURE_ATI_fragment_shader
691 ss
->ATIShaders
= _mesa_NewHashTable();
692 ss
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
693 if (!ss
->DefaultFragmentShader
)
697 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
698 ss
->BufferObjects
= _mesa_NewHashTable();
701 ss
->ArrayObjects
= _mesa_NewHashTable();
703 #if FEATURE_ARB_shader_objects
704 ss
->ShaderObjects
= _mesa_NewHashTable();
707 ss
->Default1D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_1D
);
711 ss
->Default2D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_2D
);
715 ss
->Default3D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_3D
);
719 ss
->DefaultCubeMap
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_CUBE_MAP_ARB
);
720 if (!ss
->DefaultCubeMap
)
723 ss
->DefaultRect
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_RECTANGLE_NV
);
724 if (!ss
->DefaultRect
)
727 /* Effectively bind the default textures to all texture units */
728 ss
->Default1D
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
729 ss
->Default2D
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
730 ss
->Default3D
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
731 ss
->DefaultCubeMap
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
732 ss
->DefaultRect
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
734 _glthread_INIT_MUTEX(ss
->TexMutex
);
735 ss
->TextureStateStamp
= 0;
738 #if FEATURE_EXT_framebuffer_object
739 ss
->FrameBuffers
= _mesa_NewHashTable();
740 if (!ss
->FrameBuffers
)
742 ss
->RenderBuffers
= _mesa_NewHashTable();
743 if (!ss
->RenderBuffers
)
751 /* Ran out of memory at some point. Free everything and return NULL */
753 _mesa_DeleteHashTable(ss
->DisplayList
);
755 _mesa_DeleteHashTable(ss
->TexObjects
);
756 #if FEATURE_NV_vertex_program
758 _mesa_DeleteHashTable(ss
->Programs
);
760 #if FEATURE_ARB_vertex_program
761 if (ss
->DefaultVertexProgram
)
762 ctx
->Driver
.DeleteProgram(ctx
, ss
->DefaultVertexProgram
);
764 #if FEATURE_ARB_fragment_program
765 if (ss
->DefaultFragmentProgram
)
766 ctx
->Driver
.DeleteProgram(ctx
, ss
->DefaultFragmentProgram
);
768 #if FEATURE_ATI_fragment_shader
769 if (ss
->DefaultFragmentShader
)
770 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
772 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
773 if (ss
->BufferObjects
)
774 _mesa_DeleteHashTable(ss
->BufferObjects
);
777 if (ss
->ArrayObjects
)
778 _mesa_DeleteHashTable (ss
->ArrayObjects
);
780 #if FEATURE_ARB_shader_objects
781 if (ss
->ShaderObjects
)
782 _mesa_DeleteHashTable (ss
->ShaderObjects
);
785 #if FEATURE_EXT_framebuffer_object
786 if (ss
->FrameBuffers
)
787 _mesa_DeleteHashTable(ss
->FrameBuffers
);
788 if (ss
->RenderBuffers
)
789 _mesa_DeleteHashTable(ss
->RenderBuffers
);
793 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default1D
);
795 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default2D
);
797 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default3D
);
798 if (ss
->DefaultCubeMap
)
799 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultCubeMap
);
801 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultRect
);
809 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
812 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
814 struct mesa_display_list
*list
= (struct mesa_display_list
*) data
;
815 GLcontext
*ctx
= (GLcontext
*) userData
;
816 _mesa_delete_list(ctx
, list
);
820 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
823 delete_texture_cb(GLuint id
, void *data
, void *userData
)
825 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
826 GLcontext
*ctx
= (GLcontext
*) userData
;
827 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
831 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
834 delete_program_cb(GLuint id
, void *data
, void *userData
)
836 struct gl_program
*prog
= (struct gl_program
*) data
;
837 GLcontext
*ctx
= (GLcontext
*) userData
;
838 ctx
->Driver
.DeleteProgram(ctx
, prog
);
842 * Callback for deleting an ATI fragment shader object.
843 * Called by _mesa_HashDeleteAll().
846 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
848 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
849 GLcontext
*ctx
= (GLcontext
*) userData
;
850 _mesa_delete_ati_fragment_shader(ctx
, shader
);
854 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
857 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
859 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
860 GLcontext
*ctx
= (GLcontext
*) userData
;
861 ctx
->Driver
.DeleteBuffer(ctx
, bufObj
);
865 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
868 delete_arrayobj_cb(GLuint id
, void *data
, void *userData
)
870 struct gl_array_object
*arrayObj
= (struct gl_array_object
*) data
;
871 GLcontext
*ctx
= (GLcontext
*) userData
;
872 _mesa_delete_array_object(ctx
, arrayObj
);
876 * Callback for deleting shader and shader programs objects.
877 * Called by _mesa_HashDeleteAll().
880 delete_shader_cb(GLuint id
, void *data
, void *userData
)
882 GLcontext
*ctx
= (GLcontext
*) userData
;
883 struct gl_shader
*sh
= (struct gl_shader
*) data
;
884 if (sh
->Type
== GL_FRAGMENT_SHADER
|| sh
->Type
== GL_VERTEX_SHADER
) {
885 _mesa_free_shader(ctx
, sh
);
888 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
889 ASSERT(shProg
->Type
== GL_SHADER_PROGRAM
);
890 _mesa_free_shader_program(ctx
, shProg
);
896 * Deallocate a shared state object and all children structures.
898 * \param ctx GL context.
899 * \param ss shared state pointer.
901 * Frees the display lists, the texture objects (calling the driver texture
902 * deletion callback to free its private data) and the vertex programs, as well
903 * as their hash tables.
905 * \sa alloc_shared_state().
908 free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
913 _mesa_HashDeleteAll(ss
->DisplayList
, delete_displaylist_cb
, ctx
);
914 _mesa_DeleteHashTable(ss
->DisplayList
);
917 * Free texture objects
919 ASSERT(ctx
->Driver
.DeleteTexture
);
920 /* the default textures */
921 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default1D
);
922 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default2D
);
923 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default3D
);
924 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultCubeMap
);
925 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultRect
);
926 /* all other textures */
927 _mesa_HashDeleteAll(ss
->TexObjects
, delete_texture_cb
, ctx
);
928 _mesa_DeleteHashTable(ss
->TexObjects
);
930 #if defined(FEATURE_NV_vertex_program) || defined(FEATURE_NV_fragment_program)
931 _mesa_HashDeleteAll(ss
->Programs
, delete_program_cb
, ctx
);
932 _mesa_DeleteHashTable(ss
->Programs
);
934 #if FEATURE_ARB_vertex_program
935 _mesa_delete_program(ctx
, ss
->DefaultVertexProgram
);
937 #if FEATURE_ARB_fragment_program
938 _mesa_delete_program(ctx
, ss
->DefaultFragmentProgram
);
941 #if FEATURE_ATI_fragment_shader
942 _mesa_HashDeleteAll(ss
->ATIShaders
, delete_fragshader_cb
, ctx
);
943 _mesa_DeleteHashTable(ss
->ATIShaders
);
944 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
947 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
948 _mesa_HashDeleteAll(ss
->BufferObjects
, delete_bufferobj_cb
, ctx
);
949 _mesa_DeleteHashTable(ss
->BufferObjects
);
952 _mesa_HashDeleteAll(ss
->ArrayObjects
, delete_arrayobj_cb
, ctx
);
953 _mesa_DeleteHashTable(ss
->ArrayObjects
);
955 #if FEATURE_ARB_shader_objects
956 _mesa_HashDeleteAll(ss
->ShaderObjects
, delete_shader_cb
, ctx
);
957 _mesa_DeleteHashTable(ss
->ShaderObjects
);
960 #if FEATURE_EXT_framebuffer_object
961 _mesa_DeleteHashTable(ss
->FrameBuffers
);
962 _mesa_DeleteHashTable(ss
->RenderBuffers
);
965 _glthread_DESTROY_MUTEX(ss
->Mutex
);
972 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
975 _mesa_init_current(GLcontext
*ctx
)
979 /* Init all to (0,0,0,1) */
980 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
981 ASSIGN_4V( ctx
->Current
.Attrib
[i
], 0.0, 0.0, 0.0, 1.0 );
984 /* redo special cases: */
985 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_WEIGHT
], 1.0, 0.0, 0.0, 1.0 );
986 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
], 0.0, 0.0, 1.0, 1.0 );
987 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], 1.0, 1.0, 1.0, 1.0 );
988 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
], 0.0, 0.0, 0.0, 1.0 );
989 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
], 1.0, 0.0, 0.0, 1.0 );
990 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
], 1.0, 0.0, 0.0, 1.0 );
995 * Init vertex/fragment program native limits from logical limits.
998 init_natives(struct gl_program_constants
*prog
)
1000 prog
->MaxNativeInstructions
= prog
->MaxInstructions
;
1001 prog
->MaxNativeAluInstructions
= prog
->MaxAluInstructions
;
1002 prog
->MaxNativeTexInstructions
= prog
->MaxTexInstructions
;
1003 prog
->MaxNativeTexIndirections
= prog
->MaxTexIndirections
;
1004 prog
->MaxNativeAttribs
= prog
->MaxAttribs
;
1005 prog
->MaxNativeTemps
= prog
->MaxTemps
;
1006 prog
->MaxNativeAddressRegs
= prog
->MaxAddressRegs
;
1007 prog
->MaxNativeParameters
= prog
->MaxParameters
;
1012 * Initialize fields of gl_constants (aka ctx->Const.*).
1013 * Use defaults from config.h. The device drivers will often override
1014 * some of these values (such as number of texture units).
1017 _mesa_init_constants(GLcontext
*ctx
)
1021 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
1022 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
1024 assert(MAX_TEXTURE_UNITS
>= MAX_TEXTURE_COORD_UNITS
);
1025 assert(MAX_TEXTURE_UNITS
>= MAX_TEXTURE_IMAGE_UNITS
);
1027 /* Constants, may be overriden (usually only reduced) by device drivers */
1028 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
1029 ctx
->Const
.Max3DTextureLevels
= MAX_3D_TEXTURE_LEVELS
;
1030 ctx
->Const
.MaxCubeTextureLevels
= MAX_CUBE_TEXTURE_LEVELS
;
1031 ctx
->Const
.MaxTextureRectSize
= MAX_TEXTURE_RECT_SIZE
;
1032 ctx
->Const
.MaxTextureCoordUnits
= MAX_TEXTURE_COORD_UNITS
;
1033 ctx
->Const
.MaxTextureImageUnits
= MAX_TEXTURE_IMAGE_UNITS
;
1034 ctx
->Const
.MaxTextureUnits
= MIN2(ctx
->Const
.MaxTextureCoordUnits
,
1035 ctx
->Const
.MaxTextureImageUnits
);
1036 ctx
->Const
.MaxTextureMaxAnisotropy
= MAX_TEXTURE_MAX_ANISOTROPY
;
1037 ctx
->Const
.MaxTextureLodBias
= MAX_TEXTURE_LOD_BIAS
;
1038 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
1039 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
1040 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
1041 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
1042 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
1043 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
1044 ctx
->Const
.PointSizeGranularity
= (GLfloat
) POINT_SIZE_GRANULARITY
;
1045 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
1046 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
1047 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
1048 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
1049 ctx
->Const
.LineWidthGranularity
= (GLfloat
) LINE_WIDTH_GRANULARITY
;
1050 ctx
->Const
.MaxColorTableSize
= MAX_COLOR_TABLE_SIZE
;
1051 ctx
->Const
.MaxConvolutionWidth
= MAX_CONVOLUTION_WIDTH
;
1052 ctx
->Const
.MaxConvolutionHeight
= MAX_CONVOLUTION_HEIGHT
;
1053 ctx
->Const
.MaxClipPlanes
= MAX_CLIP_PLANES
;
1054 ctx
->Const
.MaxLights
= MAX_LIGHTS
;
1055 ctx
->Const
.MaxShininess
= 128.0;
1056 ctx
->Const
.MaxSpotExponent
= 128.0;
1057 ctx
->Const
.MaxViewportWidth
= MAX_WIDTH
;
1058 ctx
->Const
.MaxViewportHeight
= MAX_HEIGHT
;
1059 #if FEATURE_ARB_vertex_program
1060 ctx
->Const
.VertexProgram
.MaxInstructions
= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
;
1061 ctx
->Const
.VertexProgram
.MaxAluInstructions
= 0;
1062 ctx
->Const
.VertexProgram
.MaxTexInstructions
= 0;
1063 ctx
->Const
.VertexProgram
.MaxTexIndirections
= 0;
1064 ctx
->Const
.VertexProgram
.MaxAttribs
= MAX_NV_VERTEX_PROGRAM_INPUTS
;
1065 ctx
->Const
.VertexProgram
.MaxTemps
= MAX_NV_VERTEX_PROGRAM_TEMPS
;
1066 ctx
->Const
.VertexProgram
.MaxParameters
= MAX_NV_VERTEX_PROGRAM_PARAMS
;
1067 ctx
->Const
.VertexProgram
.MaxLocalParams
= MAX_PROGRAM_LOCAL_PARAMS
;
1068 ctx
->Const
.VertexProgram
.MaxEnvParams
= MAX_NV_VERTEX_PROGRAM_PARAMS
;
1069 ctx
->Const
.VertexProgram
.MaxAddressRegs
= MAX_VERTEX_PROGRAM_ADDRESS_REGS
;
1070 ctx
->Const
.VertexProgram
.MaxUniformComponents
= 4 * MAX_UNIFORMS
;
1071 init_natives(&ctx
->Const
.VertexProgram
);
1074 #if FEATURE_ARB_fragment_program
1075 ctx
->Const
.FragmentProgram
.MaxInstructions
= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
;
1076 ctx
->Const
.FragmentProgram
.MaxAluInstructions
= MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS
;
1077 ctx
->Const
.FragmentProgram
.MaxTexInstructions
= MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS
;
1078 ctx
->Const
.FragmentProgram
.MaxTexIndirections
= MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS
;
1079 ctx
->Const
.FragmentProgram
.MaxAttribs
= MAX_NV_FRAGMENT_PROGRAM_INPUTS
;
1080 ctx
->Const
.FragmentProgram
.MaxTemps
= MAX_NV_FRAGMENT_PROGRAM_TEMPS
;
1081 ctx
->Const
.FragmentProgram
.MaxParameters
= MAX_NV_FRAGMENT_PROGRAM_PARAMS
;
1082 ctx
->Const
.FragmentProgram
.MaxLocalParams
= MAX_PROGRAM_LOCAL_PARAMS
;
1083 ctx
->Const
.FragmentProgram
.MaxEnvParams
= MAX_NV_FRAGMENT_PROGRAM_PARAMS
;
1084 ctx
->Const
.FragmentProgram
.MaxAddressRegs
= MAX_FRAGMENT_PROGRAM_ADDRESS_REGS
;
1085 ctx
->Const
.FragmentProgram
.MaxUniformComponents
= 4 * MAX_UNIFORMS
;
1086 init_natives(&ctx
->Const
.FragmentProgram
);
1088 ctx
->Const
.MaxProgramMatrices
= MAX_PROGRAM_MATRICES
;
1089 ctx
->Const
.MaxProgramMatrixStackDepth
= MAX_PROGRAM_MATRIX_STACK_DEPTH
;
1091 /* CheckArrayBounds is overriden by drivers/x11 for X server */
1092 ctx
->Const
.CheckArrayBounds
= GL_FALSE
;
1094 /* GL_ARB_draw_buffers */
1095 ctx
->Const
.MaxDrawBuffers
= MAX_DRAW_BUFFERS
;
1097 /* GL_OES_read_format */
1098 ctx
->Const
.ColorReadFormat
= GL_RGBA
;
1099 ctx
->Const
.ColorReadType
= GL_UNSIGNED_BYTE
;
1101 #if FEATURE_EXT_framebuffer_object
1102 ctx
->Const
.MaxColorAttachments
= MAX_COLOR_ATTACHMENTS
;
1103 ctx
->Const
.MaxRenderbufferSize
= MAX_WIDTH
;
1106 #if FEATURE_ARB_vertex_shader
1107 ctx
->Const
.MaxVertexTextureImageUnits
= MAX_VERTEX_TEXTURE_IMAGE_UNITS
;
1108 ctx
->Const
.MaxVarying
= MAX_VARYING
;
1112 ASSERT(ctx
->Const
.MaxTextureUnits
== MIN2(ctx
->Const
.MaxTextureImageUnits
,
1113 ctx
->Const
.MaxTextureCoordUnits
));
1114 ASSERT(ctx
->Const
.FragmentProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
1115 ASSERT(ctx
->Const
.VertexProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
1117 ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
1118 ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
1119 ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS
<= VERT_ATTRIB_MAX
);
1120 ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS
<= VERT_RESULT_MAX
);
1125 * Do some sanity checks on the limits/constants for the given context.
1126 * Only called the first time a context is bound.
1129 check_context_limits(GLcontext
*ctx
)
1131 /* Many context limits/constants are limited by the size of
1134 assert(ctx
->Const
.MaxTextureImageUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
1135 assert(ctx
->Const
.MaxTextureCoordUnits
<= MAX_TEXTURE_COORD_UNITS
);
1136 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
1137 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_COORD_UNITS
);
1139 assert(ctx
->Const
.MaxViewportWidth
<= MAX_WIDTH
);
1140 assert(ctx
->Const
.MaxViewportHeight
<= MAX_WIDTH
);
1142 /* make sure largest texture image is <= MAX_WIDTH in size */
1143 assert((1 << (ctx
->Const
.MaxTextureLevels
-1 )) <= MAX_WIDTH
);
1144 assert((1 << (ctx
->Const
.MaxCubeTextureLevels
-1 )) <= MAX_WIDTH
);
1145 assert((1 << (ctx
->Const
.Max3DTextureLevels
-1 )) <= MAX_WIDTH
);
1147 assert(ctx
->Const
.MaxDrawBuffers
<= MAX_DRAW_BUFFERS
);
1149 /* XXX probably add more tests */
1154 * Initialize the attribute groups in a GL context.
1156 * \param ctx GL context.
1158 * Initializes all the attributes, calling the respective <tt>init*</tt>
1159 * functions for the more complex data structures.
1162 init_attrib_groups(GLcontext
*ctx
)
1167 _mesa_init_constants( ctx
);
1170 _mesa_init_extensions( ctx
);
1172 /* Attribute Groups */
1173 _mesa_init_accum( ctx
);
1174 _mesa_init_attrib( ctx
);
1175 _mesa_init_buffer_objects( ctx
);
1176 _mesa_init_color( ctx
);
1177 _mesa_init_colortables( ctx
);
1178 _mesa_init_current( ctx
);
1179 _mesa_init_depth( ctx
);
1180 _mesa_init_debug( ctx
);
1181 _mesa_init_display_list( ctx
);
1182 _mesa_init_eval( ctx
);
1183 _mesa_init_feedback( ctx
);
1184 _mesa_init_fog( ctx
);
1185 _mesa_init_histogram( ctx
);
1186 _mesa_init_hint( ctx
);
1187 _mesa_init_line( ctx
);
1188 _mesa_init_lighting( ctx
);
1189 _mesa_init_matrix( ctx
);
1190 _mesa_init_multisample( ctx
);
1191 _mesa_init_pixel( ctx
);
1192 _mesa_init_point( ctx
);
1193 _mesa_init_polygon( ctx
);
1194 _mesa_init_program( ctx
);
1195 _mesa_init_query( ctx
);
1196 _mesa_init_rastpos( ctx
);
1197 _mesa_init_scissor( ctx
);
1198 _mesa_init_shader_state( ctx
);
1199 _mesa_init_stencil( ctx
);
1200 _mesa_init_transform( ctx
);
1201 _mesa_init_varray( ctx
);
1202 _mesa_init_viewport( ctx
);
1204 if (!_mesa_init_texture( ctx
))
1207 _mesa_init_texture_s3tc( ctx
);
1208 _mesa_init_texture_fxt1( ctx
);
1211 ctx
->NewState
= _NEW_ALL
;
1212 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1220 * This is the default function we plug into all dispatch table slots
1221 * This helps prevents a segfault when someone calls a GL function without
1222 * first checking if the extension's supported.
1227 _mesa_problem(NULL
, "User called no-op dispatch function (an unsupported extension function?)");
1233 * Allocate and initialize a new dispatch table.
1235 static struct _glapi_table
*
1236 alloc_dispatch_table(void)
1238 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1239 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1240 * Mesa we do this to accomodate different versions of libGL and various
1243 GLint numEntries
= MAX2(_glapi_get_dispatch_table_size(),
1244 sizeof(struct _glapi_table
) / sizeof(_glapi_proc
));
1245 struct _glapi_table
*table
=
1246 (struct _glapi_table
*) _mesa_malloc(numEntries
* sizeof(_glapi_proc
));
1248 _glapi_proc
*entry
= (_glapi_proc
*) table
;
1250 for (i
= 0; i
< numEntries
; i
++) {
1251 entry
[i
] = (_glapi_proc
) generic_nop
;
1259 * Initialize a GLcontext struct (rendering context).
1261 * This includes allocating all the other structs and arrays which hang off of
1262 * the context by pointers.
1263 * Note that the driver needs to pass in its dd_function_table here since
1264 * we need to at least call driverFunctions->NewTextureObject to create the
1265 * default texture objects.
1267 * Called by _mesa_create_context().
1269 * Performs the imports and exports callback tables initialization, and
1270 * miscellaneous one-time initializations. If no shared context is supplied one
1271 * is allocated, and increase its reference count. Setups the GL API dispatch
1272 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1273 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1276 * \param ctx the context to initialize
1277 * \param visual describes the visual attributes for this context
1278 * \param share_list points to context to share textures, display lists,
1280 * \param driverFunctions table of device driver functions for this context
1282 * \param driverContext pointer to driver-specific context data
1285 _mesa_initialize_context(GLcontext
*ctx
,
1286 const GLvisual
*visual
,
1287 GLcontext
*share_list
,
1288 const struct dd_function_table
*driverFunctions
,
1289 void *driverContext
)
1291 ASSERT(driverContext
);
1292 assert(driverFunctions
->NewTextureObject
);
1293 assert(driverFunctions
->FreeTexImageData
);
1295 /* If the driver wants core Mesa to use special imports, it'll have to
1296 * override these defaults.
1298 _mesa_init_default_imports( &(ctx
->imports
), driverContext
);
1300 /* initialize the exports (Mesa functions called by the window system) */
1301 _mesa_init_default_exports( &(ctx
->exports
) );
1303 /* misc one-time initializations */
1306 ctx
->Visual
= *visual
;
1307 ctx
->DrawBuffer
= NULL
;
1308 ctx
->ReadBuffer
= NULL
;
1309 ctx
->WinSysDrawBuffer
= NULL
;
1310 ctx
->WinSysReadBuffer
= NULL
;
1312 /* Plug in driver functions and context pointer here.
1313 * This is important because when we call alloc_shared_state() below
1314 * we'll call ctx->Driver.NewTextureObject() to create the default
1317 ctx
->Driver
= *driverFunctions
;
1318 ctx
->DriverCtx
= driverContext
;
1321 /* share state with another context */
1322 ctx
->Shared
= share_list
->Shared
;
1325 /* allocate new, unshared state */
1326 if (!alloc_shared_state( ctx
)) {
1330 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1331 ctx
->Shared
->RefCount
++;
1332 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1334 if (!init_attrib_groups( ctx
)) {
1335 free_shared_state(ctx
, ctx
->Shared
);
1339 /* setup the API dispatch tables */
1340 ctx
->Exec
= alloc_dispatch_table();
1341 ctx
->Save
= alloc_dispatch_table();
1342 if (!ctx
->Exec
|| !ctx
->Save
) {
1343 free_shared_state(ctx
, ctx
->Shared
);
1345 _mesa_free(ctx
->Exec
);
1347 _mesa_init_exec_table(ctx
->Exec
);
1348 ctx
->CurrentDispatch
= ctx
->Exec
;
1350 _mesa_init_dlist_table(ctx
->Save
);
1351 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
1352 /* Neutral tnl module stuff */
1353 _mesa_init_exec_vtxfmt( ctx
);
1354 ctx
->TnlModule
.Current
= NULL
;
1355 ctx
->TnlModule
.SwapCount
= 0;
1358 ctx
->FragmentProgram
._MaintainTexEnvProgram
1359 = (_mesa_getenv("MESA_TEX_PROG") != NULL
);
1360 ctx
->FragmentProgram
._UseTexEnvProgram
= ctx
->FragmentProgram
._MaintainTexEnvProgram
;
1362 ctx
->VertexProgram
._MaintainTnlProgram
1363 = (_mesa_getenv("MESA_TNL_PROG") != NULL
);
1364 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
1365 /* this is required... */
1366 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
1369 ctx
->FirstTimeCurrent
= GL_TRUE
;
1376 * Allocate and initialize a GLcontext structure.
1377 * Note that the driver needs to pass in its dd_function_table here since
1378 * we need to at least call driverFunctions->NewTextureObject to initialize
1379 * the rendering context.
1381 * \param visual a GLvisual pointer (we copy the struct contents)
1382 * \param share_list another context to share display lists with or NULL
1383 * \param driverFunctions points to the dd_function_table into which the
1384 * driver has plugged in all its special functions.
1385 * \param driverCtx points to the device driver's private context state
1387 * \return pointer to a new __GLcontextRec or NULL if error.
1390 _mesa_create_context(const GLvisual
*visual
,
1391 GLcontext
*share_list
,
1392 const struct dd_function_table
*driverFunctions
,
1393 void *driverContext
)
1398 ASSERT(driverContext
);
1400 ctx
= (GLcontext
*) _mesa_calloc(sizeof(GLcontext
));
1404 if (_mesa_initialize_context(ctx
, visual
, share_list
,
1405 driverFunctions
, driverContext
)) {
1416 * Free the data associated with the given context.
1418 * But doesn't free the GLcontext struct itself.
1420 * \sa _mesa_initialize_context() and init_attrib_groups().
1423 _mesa_free_context_data( GLcontext
*ctx
)
1425 /* if we're destroying the current context, unbind it first */
1426 if (ctx
== _mesa_get_current_context()) {
1427 _mesa_make_current(NULL
, NULL
, NULL
);
1430 /* unreference WinSysDraw/Read buffers */
1431 _mesa_unreference_framebuffer(&ctx
->WinSysDrawBuffer
);
1432 _mesa_unreference_framebuffer(&ctx
->WinSysReadBuffer
);
1433 _mesa_unreference_framebuffer(&ctx
->DrawBuffer
);
1434 _mesa_unreference_framebuffer(&ctx
->ReadBuffer
);
1437 _mesa_free_lighting_data( ctx
);
1438 _mesa_free_eval_data( ctx
);
1439 _mesa_free_texture_data( ctx
);
1440 _mesa_free_matrix_data( ctx
);
1441 _mesa_free_viewport_data( ctx
);
1442 _mesa_free_colortables_data( ctx
);
1443 _mesa_free_program_data(ctx
);
1444 _mesa_free_shader_state(ctx
);
1445 _mesa_free_query_data(ctx
);
1447 #if FEATURE_ARB_vertex_buffer_object
1448 _mesa_delete_buffer_object(ctx
, ctx
->Array
.NullBufferObj
);
1450 _mesa_delete_array_object(ctx
, ctx
->Array
.DefaultArrayObj
);
1452 /* free dispatch tables */
1453 _mesa_free(ctx
->Exec
);
1454 _mesa_free(ctx
->Save
);
1456 /* Shared context state (display lists, textures, etc) */
1457 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1458 ctx
->Shared
->RefCount
--;
1459 assert(ctx
->Shared
->RefCount
>= 0);
1460 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1461 if (ctx
->Shared
->RefCount
== 0) {
1462 /* free shared state */
1463 free_shared_state( ctx
, ctx
->Shared
);
1466 if (ctx
->Extensions
.String
)
1467 _mesa_free((void *) ctx
->Extensions
.String
);
1472 * Destroy a GLcontext structure.
1474 * \param ctx GL context.
1476 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1479 _mesa_destroy_context( GLcontext
*ctx
)
1482 _mesa_free_context_data(ctx
);
1483 _mesa_free( (void *) ctx
);
1490 * Copy attribute groups from one context to another.
1492 * \param src source context
1493 * \param dst destination context
1494 * \param mask bitwise OR of GL_*_BIT flags
1496 * According to the bits specified in \p mask, copies the corresponding
1497 * attributes from \p src into \p dst. For many of the attributes a simple \c
1498 * memcpy is not enough due to the existence of internal pointers in their data
1502 _mesa_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1504 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1506 dst
->Accum
= src
->Accum
;
1508 if (mask
& GL_COLOR_BUFFER_BIT
) {
1510 dst
->Color
= src
->Color
;
1512 if (mask
& GL_CURRENT_BIT
) {
1514 dst
->Current
= src
->Current
;
1516 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1518 dst
->Depth
= src
->Depth
;
1520 if (mask
& GL_ENABLE_BIT
) {
1523 if (mask
& GL_EVAL_BIT
) {
1525 dst
->Eval
= src
->Eval
;
1527 if (mask
& GL_FOG_BIT
) {
1529 dst
->Fog
= src
->Fog
;
1531 if (mask
& GL_HINT_BIT
) {
1533 dst
->Hint
= src
->Hint
;
1535 if (mask
& GL_LIGHTING_BIT
) {
1537 /* begin with memcpy */
1538 dst
->Light
= src
->Light
;
1539 /* fixup linked lists to prevent pointer insanity */
1540 make_empty_list( &(dst
->Light
.EnabledList
) );
1541 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1542 if (dst
->Light
.Light
[i
].Enabled
) {
1543 insert_at_tail(&(dst
->Light
.EnabledList
), &(dst
->Light
.Light
[i
]));
1547 if (mask
& GL_LINE_BIT
) {
1549 dst
->Line
= src
->Line
;
1551 if (mask
& GL_LIST_BIT
) {
1553 dst
->List
= src
->List
;
1555 if (mask
& GL_PIXEL_MODE_BIT
) {
1557 dst
->Pixel
= src
->Pixel
;
1559 if (mask
& GL_POINT_BIT
) {
1561 dst
->Point
= src
->Point
;
1563 if (mask
& GL_POLYGON_BIT
) {
1565 dst
->Polygon
= src
->Polygon
;
1567 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1568 /* Use loop instead of MEMCPY due to problem with Portland Group's
1569 * C compiler. Reported by John Stone.
1572 for (i
= 0; i
< 32; i
++) {
1573 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1576 if (mask
& GL_SCISSOR_BIT
) {
1578 dst
->Scissor
= src
->Scissor
;
1580 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1582 dst
->Stencil
= src
->Stencil
;
1584 if (mask
& GL_TEXTURE_BIT
) {
1585 /* Cannot memcpy because of pointers */
1586 _mesa_copy_texture_state(src
, dst
);
1588 if (mask
& GL_TRANSFORM_BIT
) {
1590 dst
->Transform
= src
->Transform
;
1592 if (mask
& GL_VIEWPORT_BIT
) {
1593 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1594 dst
->Viewport
.X
= src
->Viewport
.X
;
1595 dst
->Viewport
.Y
= src
->Viewport
.Y
;
1596 dst
->Viewport
.Width
= src
->Viewport
.Width
;
1597 dst
->Viewport
.Height
= src
->Viewport
.Height
;
1598 dst
->Viewport
.Near
= src
->Viewport
.Near
;
1599 dst
->Viewport
.Far
= src
->Viewport
.Far
;
1600 _math_matrix_copy(&dst
->Viewport
._WindowMap
, &src
->Viewport
._WindowMap
);
1603 /* XXX FIXME: Call callbacks?
1605 dst
->NewState
= _NEW_ALL
;
1611 * Check if the given context can render into the given framebuffer
1612 * by checking visual attributes.
1614 * XXX this may go away someday because we're moving toward more freedom
1615 * in binding contexts to drawables with different visual attributes.
1616 * The GL_EXT_f_b_o extension is prompting some of that.
1618 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1621 check_compatible(const GLcontext
*ctx
, const GLframebuffer
*buffer
)
1623 const GLvisual
*ctxvis
= &ctx
->Visual
;
1624 const GLvisual
*bufvis
= &buffer
->Visual
;
1626 if (ctxvis
== bufvis
)
1629 if (ctxvis
->rgbMode
!= bufvis
->rgbMode
)
1632 /* disabling this fixes the fgl_glxgears pbuffer demo */
1633 if (ctxvis
->doubleBufferMode
&& !bufvis
->doubleBufferMode
)
1636 if (ctxvis
->stereoMode
&& !bufvis
->stereoMode
)
1638 if (ctxvis
->haveAccumBuffer
&& !bufvis
->haveAccumBuffer
)
1640 if (ctxvis
->haveDepthBuffer
&& !bufvis
->haveDepthBuffer
)
1642 if (ctxvis
->haveStencilBuffer
&& !bufvis
->haveStencilBuffer
)
1644 if (ctxvis
->redMask
&& ctxvis
->redMask
!= bufvis
->redMask
)
1646 if (ctxvis
->greenMask
&& ctxvis
->greenMask
!= bufvis
->greenMask
)
1648 if (ctxvis
->blueMask
&& ctxvis
->blueMask
!= bufvis
->blueMask
)
1650 if (ctxvis
->depthBits
&& ctxvis
->depthBits
!= bufvis
->depthBits
)
1652 if (ctxvis
->stencilBits
&& ctxvis
->stencilBits
!= bufvis
->stencilBits
)
1660 * Do one-time initialization for the given framebuffer. Specifically,
1661 * ask the driver for the window's current size and update the framebuffer
1663 * Really, the device driver should totally take care of this.
1666 initialize_framebuffer_size(GLcontext
*ctx
, GLframebuffer
*fb
)
1668 GLuint width
, height
;
1669 if (ctx
->Driver
.GetBufferSize
) {
1670 ctx
->Driver
.GetBufferSize(fb
, &width
, &height
);
1671 if (ctx
->Driver
.ResizeBuffers
)
1672 ctx
->Driver
.ResizeBuffers(ctx
, fb
, width
, height
);
1673 fb
->Initialized
= GL_TRUE
;
1679 * Bind the given context to the given drawBuffer and readBuffer and
1680 * make it the current context for the calling thread.
1681 * We'll render into the drawBuffer and read pixels from the
1682 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1684 * We check that the context's and framebuffer's visuals are compatible
1685 * and return immediately if they're not.
1687 * \param newCtx the new GL context. If NULL then there will be no current GL
1689 * \param drawBuffer the drawing framebuffer
1690 * \param readBuffer the reading framebuffer
1693 _mesa_make_current( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1694 GLframebuffer
*readBuffer
)
1696 GET_CURRENT_CONTEXT(oldCtx
);
1698 if (MESA_VERBOSE
& VERBOSE_API
)
1699 _mesa_debug(newCtx
, "_mesa_make_current()\n");
1701 /* Check that the context's and framebuffer's visuals are compatible.
1703 if (newCtx
&& drawBuffer
&& newCtx
->WinSysDrawBuffer
!= drawBuffer
) {
1704 if (!check_compatible(newCtx
, drawBuffer
)) {
1705 _mesa_warning(newCtx
,
1706 "MakeCurrent: incompatible visuals for context and drawbuffer");
1710 if (newCtx
&& readBuffer
&& newCtx
->WinSysReadBuffer
!= readBuffer
) {
1711 if (!check_compatible(newCtx
, readBuffer
)) {
1712 _mesa_warning(newCtx
,
1713 "MakeCurrent: incompatible visuals for context and readbuffer");
1718 #if 0 /** XXX enable this someday */
1719 if (oldCtx
&& oldCtx
!= newCtx
) {
1720 /* unbind old context's draw/read buffers */
1721 if (oldCtx
->DrawBuffer
&& oldCtx
->DrawBuffer
->Name
== 0) {
1722 oldCtx
->DrawBuffer
->RefCount
--;
1723 oldCtx
->DrawBuffer
= NULL
;
1725 if (oldCtx
->ReadBuffer
&& oldCtx
->ReadBuffer
->Name
== 0) {
1726 oldCtx
->ReadBuffer
->RefCount
--;
1727 oldCtx
->ReadBuffer
= NULL
;
1729 if (oldCtx
->WinSysDrawBuffer
) {
1730 ASSERT(oldCtx
->WinSysDrawBuffer
->Name
== 0);
1731 oldCtx
->WinSysDrawBuffer
->RefCount
--;
1732 oldCtx
->WinSysDrawBuffer
= NULL
;
1734 if (oldCtx
->WinSysReadBuffer
) {
1735 ASSERT(oldCtx
->WinSysReadBuffer
->Name
== 0);
1736 oldCtx
->WinSysReadBuffer
->RefCount
--;
1737 oldCtx
->WinSysReadBuffer
= NULL
;
1742 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1743 _glapi_set_context((void *) newCtx
);
1744 ASSERT(_mesa_get_current_context() == newCtx
);
1747 _mesa_unreference_framebuffer(&oldCtx
->WinSysDrawBuffer
);
1748 _mesa_unreference_framebuffer(&oldCtx
->WinSysReadBuffer
);
1752 _glapi_set_dispatch(NULL
); /* none current */
1755 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1757 if (drawBuffer
&& readBuffer
) {
1758 /* TODO: check if newCtx and buffer's visual match??? */
1760 ASSERT(drawBuffer
->Name
== 0);
1761 ASSERT(readBuffer
->Name
== 0);
1762 _mesa_reference_framebuffer(&newCtx
->WinSysDrawBuffer
, drawBuffer
);
1763 _mesa_reference_framebuffer(&newCtx
->WinSysReadBuffer
, readBuffer
);
1766 * Only set the context's Draw/ReadBuffer fields if they're NULL
1767 * or not bound to a user-created FBO.
1769 if (!newCtx
->DrawBuffer
|| newCtx
->DrawBuffer
->Name
== 0) {
1770 _mesa_reference_framebuffer(&newCtx
->DrawBuffer
, drawBuffer
);
1772 if (!newCtx
->ReadBuffer
|| newCtx
->ReadBuffer
->Name
== 0) {
1773 _mesa_reference_framebuffer(&newCtx
->ReadBuffer
, readBuffer
);
1776 newCtx
->NewState
|= _NEW_BUFFERS
;
1779 /* We want to get rid of these lines: */
1782 if (!drawBuffer
->Initialized
) {
1783 initialize_framebuffer_size(newCtx
, drawBuffer
);
1785 if (readBuffer
!= drawBuffer
&& !readBuffer
->Initialized
) {
1786 initialize_framebuffer_size(newCtx
, readBuffer
);
1789 _mesa_resizebuffers(newCtx
);
1793 /* We want the drawBuffer and readBuffer to be initialized by
1795 * This generally means the Width and Height match the actual
1796 * window size and the renderbuffers (both hardware and software
1797 * based) are allocated to match. The later can generally be
1798 * done with a call to _mesa_resize_framebuffer().
1800 * It's theoretically possible for a buffer to have zero width
1801 * or height, but for now, assert check that the driver did what's
1804 ASSERT(drawBuffer
->Width
> 0);
1805 ASSERT(drawBuffer
->Height
> 0);
1808 if (newCtx
->FirstTimeCurrent
) {
1809 /* set initial viewport and scissor size now */
1810 _mesa_set_viewport(newCtx
, 0, 0,
1811 drawBuffer
->Width
, drawBuffer
->Height
);
1812 _mesa_set_scissor(newCtx
, 0, 0,
1813 drawBuffer
->Width
, drawBuffer
->Height
);
1814 check_context_limits(newCtx
);
1818 /* We can use this to help debug user's problems. Tell them to set
1819 * the MESA_INFO env variable before running their app. Then the
1820 * first time each context is made current we'll print some useful
1823 if (newCtx
->FirstTimeCurrent
) {
1824 if (_mesa_getenv("MESA_INFO")) {
1827 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1834 * Make context 'ctx' share the display lists, textures and programs
1835 * that are associated with 'ctxToShare'.
1836 * Any display lists, textures or programs associated with 'ctx' will
1837 * be deleted if nobody else is sharing them.
1840 _mesa_share_state(GLcontext
*ctx
, GLcontext
*ctxToShare
)
1842 if (ctx
&& ctxToShare
&& ctx
->Shared
&& ctxToShare
->Shared
) {
1843 ctx
->Shared
->RefCount
--;
1844 if (ctx
->Shared
->RefCount
== 0) {
1845 free_shared_state(ctx
, ctx
->Shared
);
1847 ctx
->Shared
= ctxToShare
->Shared
;
1848 ctx
->Shared
->RefCount
++;
1859 * \return pointer to the current GL context for this thread.
1861 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1862 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1866 _mesa_get_current_context( void )
1868 return (GLcontext
*) _glapi_get_context();
1873 * Get context's current API dispatch table.
1875 * It'll either be the immediate-mode execute dispatcher or the display list
1876 * compile dispatcher.
1878 * \param ctx GL context.
1880 * \return pointer to dispatch_table.
1882 * Simply returns __GLcontextRec::CurrentDispatch.
1884 struct _glapi_table
*
1885 _mesa_get_dispatch(GLcontext
*ctx
)
1887 return ctx
->CurrentDispatch
;
1893 /**********************************************************************/
1894 /** \name Miscellaneous functions */
1895 /**********************************************************************/
1901 * \param ctx GL context.
1902 * \param error error code.
1904 * Records the given error code and call the driver's dd_function_table::Error
1905 * function if defined.
1908 * This is called via _mesa_error().
1911 _mesa_record_error(GLcontext
*ctx
, GLenum error
)
1916 if (ctx
->ErrorValue
== GL_NO_ERROR
) {
1917 ctx
->ErrorValue
= error
;
1920 /* Call device driver's error handler, if any. This is used on the Mac. */
1921 if (ctx
->Driver
.Error
) {
1922 ctx
->Driver
.Error(ctx
);
1928 * Execute glFinish().
1930 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1931 * dd_function_table::Finish driver callback, if not NULL.
1936 GET_CURRENT_CONTEXT(ctx
);
1937 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1938 if (ctx
->Driver
.Finish
) {
1939 ctx
->Driver
.Finish(ctx
);
1945 * Execute glFlush().
1947 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1948 * dd_function_table::Flush driver callback, if not NULL.
1953 GET_CURRENT_CONTEXT(ctx
);
1954 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1955 if (ctx
->Driver
.Flush
) {
1956 ctx
->Driver
.Flush(ctx
);