2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Mesa context/visual/framebuffer management functions.
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 #if FEATURE_attrib_stack
91 #include "bufferobj.h"
92 #if FEATURE_colortable
101 #if FEATURE_evaluators
105 #include "extensions.h"
106 #include "fbobject.h"
108 #include "feedback.h"
111 #include "framebuffer.h"
113 #if FEATURE_histogram
114 #include "histogram.h"
122 #include "multisample.h"
124 #include "pixelstore.h"
127 #if FEATURE_ARB_occlusion_query
128 #include "queryobj.h"
134 #include "simple_list.h"
137 #include "texcompress.h"
138 #include "teximage.h"
140 #include "texstate.h"
145 #include "glapi/glthread.h"
146 #include "glapi/glapioffsets.h"
147 #include "glapi/glapitable.h"
148 #include "shader/program.h"
149 #include "shader/shader_api.h"
150 #if FEATURE_ATI_fragment_shader
151 #include "shader/atifragshader.h"
154 #include "math/m_translate.h"
155 #include "math/m_matrix.h"
156 #include "math/m_xform.h"
157 #include "math/mathmod.h"
161 #include "sparc/sparc.h"
165 int MESA_VERBOSE
= 0;
168 #ifndef MESA_DEBUG_FLAGS
169 int MESA_DEBUG_FLAGS
= 0;
173 /* ubyte -> float conversion */
174 GLfloat _mesa_ubyte_to_float_color_tab
[256];
179 * Swap buffers notification callback.
181 * \param gc GL context.
183 * Called by window system just before swapping buffers.
184 * We have to finish any pending rendering.
187 _mesa_notifySwapBuffers(__GLcontext
*ctx
)
189 if (ctx
->Driver
.Flush
) {
190 ctx
->Driver
.Flush(ctx
);
195 /**********************************************************************/
196 /** \name GL Visual allocation/destruction */
197 /**********************************************************************/
201 * Allocates a GLvisual structure and initializes it via
202 * _mesa_initialize_visual().
204 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
205 * \param dbFlag double buffering
206 * \param stereoFlag stereo buffer
207 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
208 * is acceptable but the actual depth type will be GLushort or GLuint as
210 * \param stencilBits requested minimum bits per stencil buffer value
211 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
212 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
213 * \param redBits number of bits per color component in frame buffer for RGB(A)
214 * mode. We always use 8 in core Mesa though.
215 * \param greenBits same as above.
216 * \param blueBits same as above.
217 * \param alphaBits same as above.
218 * \param numSamples not really used.
220 * \return pointer to new GLvisual or NULL if requested parameters can't be
223 * \note Need to add params for level and numAuxBuffers (at least)
226 _mesa_create_visual( GLboolean rgbFlag
,
228 GLboolean stereoFlag
,
237 GLint accumGreenBits
,
239 GLint accumAlphaBits
,
242 GLvisual
*vis
= (GLvisual
*) _mesa_calloc(sizeof(GLvisual
));
244 if (!_mesa_initialize_visual(vis
, rgbFlag
, dbFlag
, stereoFlag
,
245 redBits
, greenBits
, blueBits
, alphaBits
,
246 indexBits
, depthBits
, stencilBits
,
247 accumRedBits
, accumGreenBits
,
248 accumBlueBits
, accumAlphaBits
,
258 * Makes some sanity checks and fills in the fields of the
259 * GLvisual object with the given parameters. If the caller needs
260 * to set additional fields, he should just probably init the whole GLvisual
262 * \return GL_TRUE on success, or GL_FALSE on failure.
264 * \sa _mesa_create_visual() above for the parameter description.
267 _mesa_initialize_visual( GLvisual
*vis
,
270 GLboolean stereoFlag
,
279 GLint accumGreenBits
,
281 GLint accumAlphaBits
,
286 if (depthBits
< 0 || depthBits
> 32) {
289 if (stencilBits
< 0 || stencilBits
> STENCIL_BITS
) {
292 assert(accumRedBits
>= 0);
293 assert(accumGreenBits
>= 0);
294 assert(accumBlueBits
>= 0);
295 assert(accumAlphaBits
>= 0);
297 vis
->rgbMode
= rgbFlag
;
298 vis
->doubleBufferMode
= dbFlag
;
299 vis
->stereoMode
= stereoFlag
;
301 vis
->redBits
= redBits
;
302 vis
->greenBits
= greenBits
;
303 vis
->blueBits
= blueBits
;
304 vis
->alphaBits
= alphaBits
;
305 vis
->rgbBits
= redBits
+ greenBits
+ blueBits
;
307 vis
->indexBits
= indexBits
;
308 vis
->depthBits
= depthBits
;
309 vis
->stencilBits
= stencilBits
;
311 vis
->accumRedBits
= accumRedBits
;
312 vis
->accumGreenBits
= accumGreenBits
;
313 vis
->accumBlueBits
= accumBlueBits
;
314 vis
->accumAlphaBits
= accumAlphaBits
;
316 vis
->haveAccumBuffer
= accumRedBits
> 0;
317 vis
->haveDepthBuffer
= depthBits
> 0;
318 vis
->haveStencilBuffer
= stencilBits
> 0;
320 vis
->numAuxBuffers
= 0;
323 vis
->sampleBuffers
= numSamples
> 0 ? 1 : 0;
324 vis
->samples
= numSamples
;
331 * Destroy a visual and free its memory.
335 * Frees the visual structure.
338 _mesa_destroy_visual( GLvisual
*vis
)
346 /**********************************************************************/
347 /** \name Context allocation, initialization, destroying
349 * The purpose of the most initialization functions here is to provide the
350 * default state values according to the OpenGL specification.
352 /**********************************************************************/
356 * One-time initialization mutex lock.
358 * \sa Used by one_time_init().
360 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock
);
363 * Calls all the various one-time-init functions in Mesa.
365 * While holding a global mutex lock, calls several initialization functions,
366 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
372 one_time_init( GLcontext
*ctx
)
374 static GLboolean alreadyCalled
= GL_FALSE
;
376 _glthread_LOCK_MUTEX(OneTimeLock
);
377 if (!alreadyCalled
) {
380 /* do some implementation tests */
381 assert( sizeof(GLbyte
) == 1 );
382 assert( sizeof(GLubyte
) == 1 );
383 assert( sizeof(GLshort
) == 2 );
384 assert( sizeof(GLushort
) == 2 );
385 assert( sizeof(GLint
) == 4 );
386 assert( sizeof(GLuint
) == 4 );
388 _mesa_init_sqrt_table();
393 for (i
= 0; i
< 256; i
++) {
394 _mesa_ubyte_to_float_color_tab
[i
] = (float) i
/ 255.0F
;
399 _mesa_init_sparc_glapi_relocs();
401 if (_mesa_getenv("MESA_DEBUG")) {
402 _glapi_noop_enable_warnings(GL_TRUE
);
403 _glapi_set_warning_func( (_glapi_warning_func
) _mesa_warning
);
406 _glapi_noop_enable_warnings(GL_FALSE
);
409 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
410 _mesa_debug(ctx
, "Mesa %s DEBUG build %s %s\n",
411 MESA_VERSION_STRING
, __DATE__
, __TIME__
);
414 alreadyCalled
= GL_TRUE
;
416 _glthread_UNLOCK_MUTEX(OneTimeLock
);
421 * Allocate and initialize a shared context state structure.
422 * Initializes the display list, texture objects and vertex programs hash
423 * tables, allocates the texture objects. If it runs out of memory, frees
424 * everything already allocated before returning NULL.
426 * \return pointer to a gl_shared_state structure on success, or NULL on
430 alloc_shared_state( GLcontext
*ctx
)
432 struct gl_shared_state
*ss
= CALLOC_STRUCT(gl_shared_state
);
438 _glthread_INIT_MUTEX(ss
->Mutex
);
440 ss
->DisplayList
= _mesa_NewHashTable();
441 ss
->TexObjects
= _mesa_NewHashTable();
442 ss
->Programs
= _mesa_NewHashTable();
444 #if FEATURE_ARB_vertex_program
445 ss
->DefaultVertexProgram
= (struct gl_vertex_program
*)
446 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
447 if (!ss
->DefaultVertexProgram
)
450 #if FEATURE_ARB_fragment_program
451 ss
->DefaultFragmentProgram
= (struct gl_fragment_program
*)
452 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
453 if (!ss
->DefaultFragmentProgram
)
456 #if FEATURE_ATI_fragment_shader
457 ss
->ATIShaders
= _mesa_NewHashTable();
458 ss
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
459 if (!ss
->DefaultFragmentShader
)
463 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
464 ss
->BufferObjects
= _mesa_NewHashTable();
467 ss
->ArrayObjects
= _mesa_NewHashTable();
469 #if FEATURE_ARB_shader_objects
470 ss
->ShaderObjects
= _mesa_NewHashTable();
473 ss
->Default1D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_1D
);
477 ss
->Default2D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_2D
);
481 ss
->Default3D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_3D
);
485 ss
->DefaultCubeMap
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_CUBE_MAP_ARB
);
486 if (!ss
->DefaultCubeMap
)
489 ss
->DefaultRect
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_RECTANGLE_NV
);
490 if (!ss
->DefaultRect
)
493 ss
->Default1DArray
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_1D_ARRAY_EXT
);
494 if (!ss
->Default1DArray
)
497 ss
->Default2DArray
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_2D_ARRAY_EXT
);
498 if (!ss
->Default2DArray
)
502 assert(ss
->Default1D
->RefCount
== 1);
504 _glthread_INIT_MUTEX(ss
->TexMutex
);
505 ss
->TextureStateStamp
= 0;
507 #if FEATURE_EXT_framebuffer_object
508 ss
->FrameBuffers
= _mesa_NewHashTable();
509 if (!ss
->FrameBuffers
)
511 ss
->RenderBuffers
= _mesa_NewHashTable();
512 if (!ss
->RenderBuffers
)
519 /* Ran out of memory at some point. Free everything and return NULL */
521 _mesa_DeleteHashTable(ss
->DisplayList
);
523 _mesa_DeleteHashTable(ss
->TexObjects
);
525 _mesa_DeleteHashTable(ss
->Programs
);
526 #if FEATURE_ARB_vertex_program
527 _mesa_reference_vertprog(ctx
, &ss
->DefaultVertexProgram
, NULL
);
529 #if FEATURE_ARB_fragment_program
530 _mesa_reference_fragprog(ctx
, &ss
->DefaultFragmentProgram
, NULL
);
532 #if FEATURE_ATI_fragment_shader
533 if (ss
->DefaultFragmentShader
)
534 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
536 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
537 if (ss
->BufferObjects
)
538 _mesa_DeleteHashTable(ss
->BufferObjects
);
541 if (ss
->ArrayObjects
)
542 _mesa_DeleteHashTable (ss
->ArrayObjects
);
544 #if FEATURE_ARB_shader_objects
545 if (ss
->ShaderObjects
)
546 _mesa_DeleteHashTable (ss
->ShaderObjects
);
549 #if FEATURE_EXT_framebuffer_object
550 if (ss
->FrameBuffers
)
551 _mesa_DeleteHashTable(ss
->FrameBuffers
);
552 if (ss
->RenderBuffers
)
553 _mesa_DeleteHashTable(ss
->RenderBuffers
);
557 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default1D
);
559 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default2D
);
561 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default3D
);
562 if (ss
->DefaultCubeMap
)
563 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultCubeMap
);
565 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultRect
);
566 if (ss
->Default1DArray
)
567 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default1DArray
);
568 if (ss
->Default2DArray
)
569 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default2DArray
);
578 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
581 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
584 struct mesa_display_list
*list
= (struct mesa_display_list
*) data
;
585 GLcontext
*ctx
= (GLcontext
*) userData
;
586 _mesa_delete_list(ctx
, list
);
591 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
594 delete_texture_cb(GLuint id
, void *data
, void *userData
)
596 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
597 GLcontext
*ctx
= (GLcontext
*) userData
;
598 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
602 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
605 delete_program_cb(GLuint id
, void *data
, void *userData
)
607 struct gl_program
*prog
= (struct gl_program
*) data
;
608 GLcontext
*ctx
= (GLcontext
*) userData
;
609 ASSERT(prog
->RefCount
== 1); /* should only be referenced by hash table */
610 prog
->RefCount
= 0; /* now going away */
611 ctx
->Driver
.DeleteProgram(ctx
, prog
);
614 #if FEATURE_ATI_fragment_shader
616 * Callback for deleting an ATI fragment shader object.
617 * Called by _mesa_HashDeleteAll().
620 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
622 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
623 GLcontext
*ctx
= (GLcontext
*) userData
;
624 _mesa_delete_ati_fragment_shader(ctx
, shader
);
629 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
632 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
634 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
635 GLcontext
*ctx
= (GLcontext
*) userData
;
636 ctx
->Driver
.DeleteBuffer(ctx
, bufObj
);
640 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
643 delete_arrayobj_cb(GLuint id
, void *data
, void *userData
)
645 struct gl_array_object
*arrayObj
= (struct gl_array_object
*) data
;
646 GLcontext
*ctx
= (GLcontext
*) userData
;
647 _mesa_delete_array_object(ctx
, arrayObj
);
651 * Callback for freeing shader program data. Call it before delete_shader_cb
652 * to avoid memory access error.
655 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
657 GLcontext
*ctx
= (GLcontext
*) userData
;
658 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
660 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
661 _mesa_free_shader_program_data(ctx
, shProg
);
666 * Callback for deleting shader and shader programs objects.
667 * Called by _mesa_HashDeleteAll().
670 delete_shader_cb(GLuint id
, void *data
, void *userData
)
672 GLcontext
*ctx
= (GLcontext
*) userData
;
673 struct gl_shader
*sh
= (struct gl_shader
*) data
;
674 if (sh
->Type
== GL_FRAGMENT_SHADER
|| sh
->Type
== GL_VERTEX_SHADER
) {
675 _mesa_free_shader(ctx
, sh
);
678 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
679 ASSERT(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
680 _mesa_free_shader_program(ctx
, shProg
);
685 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
688 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
690 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
691 /* The fact that the framebuffer is in the hashtable means its refcount
692 * is one, but we're removing from the hashtable now. So clear refcount.
694 /*assert(fb->RefCount == 1);*/
697 /* NOTE: Delete should always be defined but there are two reports
698 * of it being NULL (bugs 13507, 14293). Work-around for now.
705 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
708 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
710 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
711 rb
->RefCount
= 0; /* see comment for FBOs above */
718 * Deallocate a shared state object and all children structures.
720 * \param ctx GL context.
721 * \param ss shared state pointer.
723 * Frees the display lists, the texture objects (calling the driver texture
724 * deletion callback to free its private data) and the vertex programs, as well
725 * as their hash tables.
727 * \sa alloc_shared_state().
730 free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
735 _mesa_HashDeleteAll(ss
->DisplayList
, delete_displaylist_cb
, ctx
);
736 _mesa_DeleteHashTable(ss
->DisplayList
);
738 #if FEATURE_ARB_shader_objects
739 _mesa_HashWalk(ss
->ShaderObjects
, free_shader_program_data_cb
, ctx
);
740 _mesa_HashDeleteAll(ss
->ShaderObjects
, delete_shader_cb
, ctx
);
741 _mesa_DeleteHashTable(ss
->ShaderObjects
);
744 _mesa_HashDeleteAll(ss
->Programs
, delete_program_cb
, ctx
);
745 _mesa_DeleteHashTable(ss
->Programs
);
747 #if FEATURE_ARB_vertex_program
748 _mesa_reference_vertprog(ctx
, &ss
->DefaultVertexProgram
, NULL
);
750 #if FEATURE_ARB_fragment_program
751 _mesa_reference_fragprog(ctx
, &ss
->DefaultFragmentProgram
, NULL
);
754 #if FEATURE_ATI_fragment_shader
755 _mesa_HashDeleteAll(ss
->ATIShaders
, delete_fragshader_cb
, ctx
);
756 _mesa_DeleteHashTable(ss
->ATIShaders
);
757 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
760 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
761 _mesa_HashDeleteAll(ss
->BufferObjects
, delete_bufferobj_cb
, ctx
);
762 _mesa_DeleteHashTable(ss
->BufferObjects
);
765 _mesa_HashDeleteAll(ss
->ArrayObjects
, delete_arrayobj_cb
, ctx
);
766 _mesa_DeleteHashTable(ss
->ArrayObjects
);
768 #if FEATURE_EXT_framebuffer_object
769 _mesa_HashDeleteAll(ss
->FrameBuffers
, delete_framebuffer_cb
, ctx
);
770 _mesa_DeleteHashTable(ss
->FrameBuffers
);
771 _mesa_HashDeleteAll(ss
->RenderBuffers
, delete_renderbuffer_cb
, ctx
);
772 _mesa_DeleteHashTable(ss
->RenderBuffers
);
776 * Free texture objects (after FBOs since some textures might have
777 * been bound to FBOs).
779 ASSERT(ctx
->Driver
.DeleteTexture
);
780 /* the default textures */
781 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default1D
);
782 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default2D
);
783 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default3D
);
784 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultCubeMap
);
785 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultRect
);
786 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default1DArray
);
787 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default2DArray
);
788 /* all other textures */
789 _mesa_HashDeleteAll(ss
->TexObjects
, delete_texture_cb
, ctx
);
790 _mesa_DeleteHashTable(ss
->TexObjects
);
792 _glthread_DESTROY_MUTEX(ss
->Mutex
);
799 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
802 _mesa_init_current(GLcontext
*ctx
)
806 /* Init all to (0,0,0,1) */
807 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
808 ASSIGN_4V( ctx
->Current
.Attrib
[i
], 0.0, 0.0, 0.0, 1.0 );
811 /* redo special cases: */
812 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_WEIGHT
], 1.0, 0.0, 0.0, 0.0 );
813 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
], 0.0, 0.0, 1.0, 1.0 );
814 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], 1.0, 1.0, 1.0, 1.0 );
815 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
], 0.0, 0.0, 0.0, 1.0 );
816 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
], 1.0, 0.0, 0.0, 1.0 );
817 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
], 1.0, 0.0, 0.0, 1.0 );
822 * Init vertex/fragment program limits.
823 * Important: drivers should override these with actual limits.
826 init_program_limits(GLenum type
, struct gl_program_constants
*prog
)
828 prog
->MaxInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
829 prog
->MaxAluInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
830 prog
->MaxTexInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
831 prog
->MaxTexIndirections
= MAX_PROGRAM_INSTRUCTIONS
;
832 prog
->MaxTemps
= MAX_PROGRAM_TEMPS
;
833 prog
->MaxEnvParams
= MAX_PROGRAM_ENV_PARAMS
;
834 prog
->MaxLocalParams
= MAX_PROGRAM_LOCAL_PARAMS
;
835 prog
->MaxUniformComponents
= 4 * MAX_UNIFORMS
;
837 if (type
== GL_VERTEX_PROGRAM_ARB
) {
838 prog
->MaxParameters
= MAX_NV_VERTEX_PROGRAM_PARAMS
;
839 prog
->MaxAttribs
= MAX_NV_VERTEX_PROGRAM_INPUTS
;
840 prog
->MaxAddressRegs
= MAX_VERTEX_PROGRAM_ADDRESS_REGS
;
843 prog
->MaxParameters
= MAX_NV_FRAGMENT_PROGRAM_PARAMS
;
844 prog
->MaxAttribs
= MAX_NV_FRAGMENT_PROGRAM_INPUTS
;
845 prog
->MaxAddressRegs
= MAX_FRAGMENT_PROGRAM_ADDRESS_REGS
;
848 /* copy the above limits to init native limits */
849 prog
->MaxNativeInstructions
= prog
->MaxInstructions
;
850 prog
->MaxNativeAluInstructions
= prog
->MaxAluInstructions
;
851 prog
->MaxNativeTexInstructions
= prog
->MaxTexInstructions
;
852 prog
->MaxNativeTexIndirections
= prog
->MaxTexIndirections
;
853 prog
->MaxNativeAttribs
= prog
->MaxAttribs
;
854 prog
->MaxNativeTemps
= prog
->MaxTemps
;
855 prog
->MaxNativeAddressRegs
= prog
->MaxAddressRegs
;
856 prog
->MaxNativeParameters
= prog
->MaxParameters
;
861 * Initialize fields of gl_constants (aka ctx->Const.*).
862 * Use defaults from config.h. The device drivers will often override
863 * some of these values (such as number of texture units).
866 _mesa_init_constants(GLcontext
*ctx
)
870 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
871 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
873 /* Constants, may be overriden (usually only reduced) by device drivers */
874 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
875 ctx
->Const
.Max3DTextureLevels
= MAX_3D_TEXTURE_LEVELS
;
876 ctx
->Const
.MaxCubeTextureLevels
= MAX_CUBE_TEXTURE_LEVELS
;
877 ctx
->Const
.MaxTextureRectSize
= MAX_TEXTURE_RECT_SIZE
;
878 ctx
->Const
.MaxArrayTextureLayers
= MAX_ARRAY_TEXTURE_LAYERS
;
879 ctx
->Const
.MaxTextureCoordUnits
= MAX_TEXTURE_COORD_UNITS
;
880 ctx
->Const
.MaxTextureImageUnits
= MAX_TEXTURE_IMAGE_UNITS
;
881 ctx
->Const
.MaxTextureUnits
= MIN2(ctx
->Const
.MaxTextureCoordUnits
,
882 ctx
->Const
.MaxTextureImageUnits
);
883 ctx
->Const
.MaxTextureMaxAnisotropy
= MAX_TEXTURE_MAX_ANISOTROPY
;
884 ctx
->Const
.MaxTextureLodBias
= MAX_TEXTURE_LOD_BIAS
;
885 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
886 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
887 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
888 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
889 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
890 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
891 ctx
->Const
.PointSizeGranularity
= (GLfloat
) POINT_SIZE_GRANULARITY
;
892 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
893 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
894 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
895 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
896 ctx
->Const
.LineWidthGranularity
= (GLfloat
) LINE_WIDTH_GRANULARITY
;
897 ctx
->Const
.MaxColorTableSize
= MAX_COLOR_TABLE_SIZE
;
898 ctx
->Const
.MaxConvolutionWidth
= MAX_CONVOLUTION_WIDTH
;
899 ctx
->Const
.MaxConvolutionHeight
= MAX_CONVOLUTION_HEIGHT
;
900 ctx
->Const
.MaxClipPlanes
= MAX_CLIP_PLANES
;
901 ctx
->Const
.MaxLights
= MAX_LIGHTS
;
902 ctx
->Const
.MaxShininess
= 128.0;
903 ctx
->Const
.MaxSpotExponent
= 128.0;
904 ctx
->Const
.MaxViewportWidth
= MAX_WIDTH
;
905 ctx
->Const
.MaxViewportHeight
= MAX_HEIGHT
;
906 #if FEATURE_ARB_vertex_program
907 init_program_limits(GL_VERTEX_PROGRAM_ARB
, &ctx
->Const
.VertexProgram
);
909 #if FEATURE_ARB_fragment_program
910 init_program_limits(GL_FRAGMENT_PROGRAM_ARB
, &ctx
->Const
.FragmentProgram
);
912 ctx
->Const
.MaxProgramMatrices
= MAX_PROGRAM_MATRICES
;
913 ctx
->Const
.MaxProgramMatrixStackDepth
= MAX_PROGRAM_MATRIX_STACK_DEPTH
;
915 /* CheckArrayBounds is overriden by drivers/x11 for X server */
916 ctx
->Const
.CheckArrayBounds
= GL_FALSE
;
918 /* GL_ARB_draw_buffers */
919 ctx
->Const
.MaxDrawBuffers
= MAX_DRAW_BUFFERS
;
921 /* GL_OES_read_format */
922 ctx
->Const
.ColorReadFormat
= GL_RGBA
;
923 ctx
->Const
.ColorReadType
= GL_UNSIGNED_BYTE
;
925 #if FEATURE_EXT_framebuffer_object
926 ctx
->Const
.MaxColorAttachments
= MAX_COLOR_ATTACHMENTS
;
927 ctx
->Const
.MaxRenderbufferSize
= MAX_WIDTH
;
930 #if FEATURE_ARB_vertex_shader
931 ctx
->Const
.MaxVertexTextureImageUnits
= MAX_VERTEX_TEXTURE_IMAGE_UNITS
;
932 ctx
->Const
.MaxVarying
= MAX_VARYING
;
936 ASSERT(ctx
->Const
.MaxTextureUnits
== MIN2(ctx
->Const
.MaxTextureImageUnits
,
937 ctx
->Const
.MaxTextureCoordUnits
));
938 ASSERT(ctx
->Const
.FragmentProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
939 ASSERT(ctx
->Const
.VertexProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
941 ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
942 ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
943 ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS
<= VERT_ATTRIB_MAX
);
944 ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS
<= VERT_RESULT_MAX
);
949 * Do some sanity checks on the limits/constants for the given context.
950 * Only called the first time a context is bound.
953 check_context_limits(GLcontext
*ctx
)
955 /* Many context limits/constants are limited by the size of
958 assert(ctx
->Const
.MaxTextureImageUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
959 assert(ctx
->Const
.MaxTextureCoordUnits
<= MAX_TEXTURE_COORD_UNITS
);
960 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
961 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_COORD_UNITS
);
963 /* number of coord units cannot be greater than number of image units */
964 assert(ctx
->Const
.MaxTextureCoordUnits
<= ctx
->Const
.MaxTextureImageUnits
);
966 assert(ctx
->Const
.MaxViewportWidth
<= MAX_WIDTH
);
967 assert(ctx
->Const
.MaxViewportHeight
<= MAX_WIDTH
);
969 /* make sure largest texture image is <= MAX_WIDTH in size */
970 assert((1 << (ctx
->Const
.MaxTextureLevels
-1 )) <= MAX_WIDTH
);
971 assert((1 << (ctx
->Const
.MaxCubeTextureLevels
-1 )) <= MAX_WIDTH
);
972 assert((1 << (ctx
->Const
.Max3DTextureLevels
-1 )) <= MAX_WIDTH
);
974 assert(ctx
->Const
.MaxDrawBuffers
<= MAX_DRAW_BUFFERS
);
976 /* XXX probably add more tests */
981 * Initialize the attribute groups in a GL context.
983 * \param ctx GL context.
985 * Initializes all the attributes, calling the respective <tt>init*</tt>
986 * functions for the more complex data structures.
989 init_attrib_groups(GLcontext
*ctx
)
994 _mesa_init_constants( ctx
);
997 _mesa_init_extensions( ctx
);
999 /* Attribute Groups */
1001 _mesa_init_accum( ctx
);
1003 #if FEATURE_attrib_stack
1004 _mesa_init_attrib( ctx
);
1006 _mesa_init_buffer_objects( ctx
);
1007 _mesa_init_color( ctx
);
1008 #if FEATURE_colortable
1009 _mesa_init_colortables( ctx
);
1011 _mesa_init_current( ctx
);
1012 _mesa_init_depth( ctx
);
1013 _mesa_init_debug( ctx
);
1015 _mesa_init_display_list( ctx
);
1017 #if FEATURE_evaluators
1018 _mesa_init_eval( ctx
);
1020 _mesa_init_fbobjects( ctx
);
1021 #if FEATURE_feedback
1022 _mesa_init_feedback( ctx
);
1024 ctx
->RenderMode
= GL_RENDER
;
1026 _mesa_init_fog( ctx
);
1027 #if FEATURE_histogram
1028 _mesa_init_histogram( ctx
);
1030 _mesa_init_hint( ctx
);
1031 _mesa_init_line( ctx
);
1032 _mesa_init_lighting( ctx
);
1033 _mesa_init_matrix( ctx
);
1034 _mesa_init_multisample( ctx
);
1035 _mesa_init_pixel( ctx
);
1036 _mesa_init_pixelstore( ctx
);
1037 _mesa_init_point( ctx
);
1038 _mesa_init_polygon( ctx
);
1039 _mesa_init_program( ctx
);
1040 #if FEATURE_ARB_occlusion_query
1041 _mesa_init_query( ctx
);
1044 _mesa_init_rastpos( ctx
);
1046 _mesa_init_scissor( ctx
);
1047 _mesa_init_shader_state( ctx
);
1048 _mesa_init_stencil( ctx
);
1049 _mesa_init_transform( ctx
);
1050 _mesa_init_varray( ctx
);
1051 _mesa_init_viewport( ctx
);
1053 if (!_mesa_init_texture( ctx
))
1056 #if FEATURE_texture_s3tc
1057 _mesa_init_texture_s3tc( ctx
);
1059 #if FEATURE_texture_fxt1
1060 _mesa_init_texture_fxt1( ctx
);
1064 ctx
->NewState
= _NEW_ALL
;
1065 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1066 ctx
->varying_vp_inputs
= ~0;
1073 * Update default objects in a GL context with respect to shared state.
1075 * \param ctx GL context.
1077 * Removes references to old default objects, (texture objects, program
1078 * objects, etc.) and changes to reference those from the current shared
1082 update_default_objects(GLcontext
*ctx
)
1086 _mesa_update_default_objects_program(ctx
);
1087 _mesa_update_default_objects_texture(ctx
);
1088 _mesa_update_default_objects_buffer_objects(ctx
);
1095 * This is the default function we plug into all dispatch table slots
1096 * This helps prevents a segfault when someone calls a GL function without
1097 * first checking if the extension's supported.
1102 _mesa_warning(NULL
, "User called no-op dispatch function (an unsupported extension function?)");
1108 * Allocate and initialize a new dispatch table.
1110 static struct _glapi_table
*
1111 alloc_dispatch_table(void)
1113 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1114 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1115 * Mesa we do this to accomodate different versions of libGL and various
1118 GLint numEntries
= MAX2(_glapi_get_dispatch_table_size(),
1119 sizeof(struct _glapi_table
) / sizeof(_glapi_proc
));
1120 struct _glapi_table
*table
=
1121 (struct _glapi_table
*) _mesa_malloc(numEntries
* sizeof(_glapi_proc
));
1123 _glapi_proc
*entry
= (_glapi_proc
*) table
;
1125 for (i
= 0; i
< numEntries
; i
++) {
1126 entry
[i
] = (_glapi_proc
) generic_nop
;
1134 * Initialize a GLcontext struct (rendering context).
1136 * This includes allocating all the other structs and arrays which hang off of
1137 * the context by pointers.
1138 * Note that the driver needs to pass in its dd_function_table here since
1139 * we need to at least call driverFunctions->NewTextureObject to create the
1140 * default texture objects.
1142 * Called by _mesa_create_context().
1144 * Performs the imports and exports callback tables initialization, and
1145 * miscellaneous one-time initializations. If no shared context is supplied one
1146 * is allocated, and increase its reference count. Setups the GL API dispatch
1147 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1148 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1151 * \param ctx the context to initialize
1152 * \param visual describes the visual attributes for this context
1153 * \param share_list points to context to share textures, display lists,
1155 * \param driverFunctions table of device driver functions for this context
1157 * \param driverContext pointer to driver-specific context data
1160 _mesa_initialize_context(GLcontext
*ctx
,
1161 const GLvisual
*visual
,
1162 GLcontext
*share_list
,
1163 const struct dd_function_table
*driverFunctions
,
1164 void *driverContext
)
1166 /*ASSERT(driverContext);*/
1167 assert(driverFunctions
->NewTextureObject
);
1168 assert(driverFunctions
->FreeTexImageData
);
1170 /* misc one-time initializations */
1173 ctx
->Visual
= *visual
;
1174 ctx
->DrawBuffer
= NULL
;
1175 ctx
->ReadBuffer
= NULL
;
1176 ctx
->WinSysDrawBuffer
= NULL
;
1177 ctx
->WinSysReadBuffer
= NULL
;
1179 /* Plug in driver functions and context pointer here.
1180 * This is important because when we call alloc_shared_state() below
1181 * we'll call ctx->Driver.NewTextureObject() to create the default
1184 ctx
->Driver
= *driverFunctions
;
1185 ctx
->DriverCtx
= driverContext
;
1188 /* share state with another context */
1189 ctx
->Shared
= share_list
->Shared
;
1192 /* allocate new, unshared state */
1193 if (!alloc_shared_state( ctx
)) {
1197 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1198 ctx
->Shared
->RefCount
++;
1199 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1201 if (!init_attrib_groups( ctx
)) {
1202 free_shared_state(ctx
, ctx
->Shared
);
1206 /* setup the API dispatch tables */
1207 ctx
->Exec
= alloc_dispatch_table();
1208 ctx
->Save
= alloc_dispatch_table();
1209 if (!ctx
->Exec
|| !ctx
->Save
) {
1210 free_shared_state(ctx
, ctx
->Shared
);
1212 _mesa_free(ctx
->Exec
);
1214 #if FEATURE_dispatch
1215 _mesa_init_exec_table(ctx
->Exec
);
1217 ctx
->CurrentDispatch
= ctx
->Exec
;
1219 _mesa_init_dlist_table(ctx
->Save
);
1220 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
1222 /* Neutral tnl module stuff */
1223 _mesa_init_exec_vtxfmt( ctx
);
1224 ctx
->TnlModule
.Current
= NULL
;
1225 ctx
->TnlModule
.SwapCount
= 0;
1227 ctx
->FragmentProgram
._MaintainTexEnvProgram
1228 = (_mesa_getenv("MESA_TEX_PROG") != NULL
);
1229 ctx
->FragmentProgram
._UseTexEnvProgram
= ctx
->FragmentProgram
._MaintainTexEnvProgram
;
1231 ctx
->VertexProgram
._MaintainTnlProgram
1232 = (_mesa_getenv("MESA_TNL_PROG") != NULL
);
1233 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
1234 /* this is required... */
1235 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
1238 #ifdef FEATURE_extra_context_init
1239 _mesa_initialize_context_extra(ctx
);
1242 ctx
->FirstTimeCurrent
= GL_TRUE
;
1249 * Allocate and initialize a GLcontext structure.
1250 * Note that the driver needs to pass in its dd_function_table here since
1251 * we need to at least call driverFunctions->NewTextureObject to initialize
1252 * the rendering context.
1254 * \param visual a GLvisual pointer (we copy the struct contents)
1255 * \param share_list another context to share display lists with or NULL
1256 * \param driverFunctions points to the dd_function_table into which the
1257 * driver has plugged in all its special functions.
1258 * \param driverCtx points to the device driver's private context state
1260 * \return pointer to a new __GLcontextRec or NULL if error.
1263 _mesa_create_context(const GLvisual
*visual
,
1264 GLcontext
*share_list
,
1265 const struct dd_function_table
*driverFunctions
,
1266 void *driverContext
)
1271 /*ASSERT(driverContext);*/
1273 ctx
= (GLcontext
*) _mesa_calloc(sizeof(GLcontext
));
1277 if (_mesa_initialize_context(ctx
, visual
, share_list
,
1278 driverFunctions
, driverContext
)) {
1289 * Free the data associated with the given context.
1291 * But doesn't free the GLcontext struct itself.
1293 * \sa _mesa_initialize_context() and init_attrib_groups().
1296 _mesa_free_context_data( GLcontext
*ctx
)
1298 if (!_mesa_get_current_context()){
1299 /* No current context, but we may need one in order to delete
1300 * texture objs, etc. So temporarily bind the context now.
1302 _mesa_make_current(ctx
, NULL
, NULL
);
1305 /* unreference WinSysDraw/Read buffers */
1306 _mesa_unreference_framebuffer(&ctx
->WinSysDrawBuffer
);
1307 _mesa_unreference_framebuffer(&ctx
->WinSysReadBuffer
);
1308 _mesa_unreference_framebuffer(&ctx
->DrawBuffer
);
1309 _mesa_unreference_framebuffer(&ctx
->ReadBuffer
);
1311 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
1312 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
1313 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._TnlProgram
, NULL
);
1315 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
1316 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
1317 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
, NULL
);
1319 #if FEATURE_attrib_stack
1320 _mesa_free_attrib_data(ctx
);
1322 _mesa_free_lighting_data( ctx
);
1323 #if FEATURE_evaluators
1324 _mesa_free_eval_data( ctx
);
1326 _mesa_free_texture_data( ctx
);
1327 _mesa_free_matrix_data( ctx
);
1328 _mesa_free_viewport_data( ctx
);
1329 #if FEATURE_colortable
1330 _mesa_free_colortables_data( ctx
);
1332 _mesa_free_program_data(ctx
);
1333 _mesa_free_shader_state(ctx
);
1334 #if FEATURE_ARB_occlusion_query
1335 _mesa_free_query_data(ctx
);
1338 #if FEATURE_ARB_vertex_buffer_object
1339 _mesa_delete_buffer_object(ctx
, ctx
->Array
.NullBufferObj
);
1341 _mesa_delete_array_object(ctx
, ctx
->Array
.DefaultArrayObj
);
1343 /* free dispatch tables */
1344 _mesa_free(ctx
->Exec
);
1345 _mesa_free(ctx
->Save
);
1347 /* Shared context state (display lists, textures, etc) */
1348 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1349 ctx
->Shared
->RefCount
--;
1350 assert(ctx
->Shared
->RefCount
>= 0);
1351 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1352 if (ctx
->Shared
->RefCount
== 0) {
1353 /* free shared state */
1354 free_shared_state( ctx
, ctx
->Shared
);
1357 if (ctx
->Extensions
.String
)
1358 _mesa_free((void *) ctx
->Extensions
.String
);
1360 /* unbind the context if it's currently bound */
1361 if (ctx
== _mesa_get_current_context()) {
1362 _mesa_make_current(NULL
, NULL
, NULL
);
1368 * Destroy a GLcontext structure.
1370 * \param ctx GL context.
1372 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1375 _mesa_destroy_context( GLcontext
*ctx
)
1378 _mesa_free_context_data(ctx
);
1379 _mesa_free( (void *) ctx
);
1386 * Copy attribute groups from one context to another.
1388 * \param src source context
1389 * \param dst destination context
1390 * \param mask bitwise OR of GL_*_BIT flags
1392 * According to the bits specified in \p mask, copies the corresponding
1393 * attributes from \p src into \p dst. For many of the attributes a simple \c
1394 * memcpy is not enough due to the existence of internal pointers in their data
1398 _mesa_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1400 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1402 dst
->Accum
= src
->Accum
;
1404 if (mask
& GL_COLOR_BUFFER_BIT
) {
1406 dst
->Color
= src
->Color
;
1408 if (mask
& GL_CURRENT_BIT
) {
1410 dst
->Current
= src
->Current
;
1412 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1414 dst
->Depth
= src
->Depth
;
1416 if (mask
& GL_ENABLE_BIT
) {
1419 if (mask
& GL_EVAL_BIT
) {
1421 dst
->Eval
= src
->Eval
;
1423 if (mask
& GL_FOG_BIT
) {
1425 dst
->Fog
= src
->Fog
;
1427 if (mask
& GL_HINT_BIT
) {
1429 dst
->Hint
= src
->Hint
;
1431 if (mask
& GL_LIGHTING_BIT
) {
1433 /* begin with memcpy */
1434 dst
->Light
= src
->Light
;
1435 /* fixup linked lists to prevent pointer insanity */
1436 make_empty_list( &(dst
->Light
.EnabledList
) );
1437 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1438 if (dst
->Light
.Light
[i
].Enabled
) {
1439 insert_at_tail(&(dst
->Light
.EnabledList
), &(dst
->Light
.Light
[i
]));
1443 if (mask
& GL_LINE_BIT
) {
1445 dst
->Line
= src
->Line
;
1447 if (mask
& GL_LIST_BIT
) {
1449 dst
->List
= src
->List
;
1451 if (mask
& GL_PIXEL_MODE_BIT
) {
1453 dst
->Pixel
= src
->Pixel
;
1455 if (mask
& GL_POINT_BIT
) {
1457 dst
->Point
= src
->Point
;
1459 if (mask
& GL_POLYGON_BIT
) {
1461 dst
->Polygon
= src
->Polygon
;
1463 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1464 /* Use loop instead of MEMCPY due to problem with Portland Group's
1465 * C compiler. Reported by John Stone.
1468 for (i
= 0; i
< 32; i
++) {
1469 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1472 if (mask
& GL_SCISSOR_BIT
) {
1474 dst
->Scissor
= src
->Scissor
;
1476 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1478 dst
->Stencil
= src
->Stencil
;
1480 if (mask
& GL_TEXTURE_BIT
) {
1481 /* Cannot memcpy because of pointers */
1482 _mesa_copy_texture_state(src
, dst
);
1484 if (mask
& GL_TRANSFORM_BIT
) {
1486 dst
->Transform
= src
->Transform
;
1488 if (mask
& GL_VIEWPORT_BIT
) {
1489 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1490 dst
->Viewport
.X
= src
->Viewport
.X
;
1491 dst
->Viewport
.Y
= src
->Viewport
.Y
;
1492 dst
->Viewport
.Width
= src
->Viewport
.Width
;
1493 dst
->Viewport
.Height
= src
->Viewport
.Height
;
1494 dst
->Viewport
.Near
= src
->Viewport
.Near
;
1495 dst
->Viewport
.Far
= src
->Viewport
.Far
;
1496 _math_matrix_copy(&dst
->Viewport
._WindowMap
, &src
->Viewport
._WindowMap
);
1499 /* XXX FIXME: Call callbacks?
1501 dst
->NewState
= _NEW_ALL
;
1507 * Check if the given context can render into the given framebuffer
1508 * by checking visual attributes.
1510 * Most of these tests could go away because Mesa is now pretty flexible
1511 * in terms of mixing rendering contexts with framebuffers. As long
1512 * as RGB vs. CI mode agree, we're probably good.
1514 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1517 check_compatible(const GLcontext
*ctx
, const GLframebuffer
*buffer
)
1519 const GLvisual
*ctxvis
= &ctx
->Visual
;
1520 const GLvisual
*bufvis
= &buffer
->Visual
;
1522 if (ctxvis
== bufvis
)
1525 if (ctxvis
->rgbMode
!= bufvis
->rgbMode
)
1528 /* disabling this fixes the fgl_glxgears pbuffer demo */
1529 if (ctxvis
->doubleBufferMode
&& !bufvis
->doubleBufferMode
)
1532 if (ctxvis
->stereoMode
&& !bufvis
->stereoMode
)
1534 if (ctxvis
->haveAccumBuffer
&& !bufvis
->haveAccumBuffer
)
1536 if (ctxvis
->haveDepthBuffer
&& !bufvis
->haveDepthBuffer
)
1538 if (ctxvis
->haveStencilBuffer
&& !bufvis
->haveStencilBuffer
)
1540 if (ctxvis
->redMask
&& ctxvis
->redMask
!= bufvis
->redMask
)
1542 if (ctxvis
->greenMask
&& ctxvis
->greenMask
!= bufvis
->greenMask
)
1544 if (ctxvis
->blueMask
&& ctxvis
->blueMask
!= bufvis
->blueMask
)
1547 /* disabled (see bug 11161) */
1548 if (ctxvis
->depthBits
&& ctxvis
->depthBits
!= bufvis
->depthBits
)
1551 if (ctxvis
->stencilBits
&& ctxvis
->stencilBits
!= bufvis
->stencilBits
)
1559 * Do one-time initialization for the given framebuffer. Specifically,
1560 * ask the driver for the window's current size and update the framebuffer
1562 * Really, the device driver should totally take care of this.
1565 initialize_framebuffer_size(GLcontext
*ctx
, GLframebuffer
*fb
)
1567 GLuint width
, height
;
1568 if (ctx
->Driver
.GetBufferSize
) {
1569 ctx
->Driver
.GetBufferSize(fb
, &width
, &height
);
1570 if (ctx
->Driver
.ResizeBuffers
)
1571 ctx
->Driver
.ResizeBuffers(ctx
, fb
, width
, height
);
1572 fb
->Initialized
= GL_TRUE
;
1578 * Bind the given context to the given drawBuffer and readBuffer and
1579 * make it the current context for the calling thread.
1580 * We'll render into the drawBuffer and read pixels from the
1581 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1583 * We check that the context's and framebuffer's visuals are compatible
1584 * and return immediately if they're not.
1586 * \param newCtx the new GL context. If NULL then there will be no current GL
1588 * \param drawBuffer the drawing framebuffer
1589 * \param readBuffer the reading framebuffer
1592 _mesa_make_current( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1593 GLframebuffer
*readBuffer
)
1595 if (MESA_VERBOSE
& VERBOSE_API
)
1596 _mesa_debug(newCtx
, "_mesa_make_current()\n");
1598 /* Check that the context's and framebuffer's visuals are compatible.
1600 if (newCtx
&& drawBuffer
&& newCtx
->WinSysDrawBuffer
!= drawBuffer
) {
1601 if (!check_compatible(newCtx
, drawBuffer
)) {
1602 _mesa_warning(newCtx
,
1603 "MakeCurrent: incompatible visuals for context and drawbuffer");
1607 if (newCtx
&& readBuffer
&& newCtx
->WinSysReadBuffer
!= readBuffer
) {
1608 if (!check_compatible(newCtx
, readBuffer
)) {
1609 _mesa_warning(newCtx
,
1610 "MakeCurrent: incompatible visuals for context and readbuffer");
1615 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1616 _glapi_set_context((void *) newCtx
);
1617 ASSERT(_mesa_get_current_context() == newCtx
);
1620 _glapi_set_dispatch(NULL
); /* none current */
1623 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1625 if (drawBuffer
&& readBuffer
) {
1626 /* TODO: check if newCtx and buffer's visual match??? */
1628 ASSERT(drawBuffer
->Name
== 0);
1629 ASSERT(readBuffer
->Name
== 0);
1630 _mesa_reference_framebuffer(&newCtx
->WinSysDrawBuffer
, drawBuffer
);
1631 _mesa_reference_framebuffer(&newCtx
->WinSysReadBuffer
, readBuffer
);
1634 * Only set the context's Draw/ReadBuffer fields if they're NULL
1635 * or not bound to a user-created FBO.
1637 if (!newCtx
->DrawBuffer
|| newCtx
->DrawBuffer
->Name
== 0) {
1638 /* KW: merge conflict here, revisit.
1640 /* fix up the fb fields - these will end up wrong otherwise
1641 * if the DRIdrawable changes, and everything relies on them.
1642 * This is a bit messy (same as needed in _mesa_BindFramebufferEXT)
1645 GLenum buffers
[MAX_DRAW_BUFFERS
];
1647 _mesa_reference_framebuffer(&newCtx
->DrawBuffer
, drawBuffer
);
1649 for(i
= 0; i
< newCtx
->Const
.MaxDrawBuffers
; i
++) {
1650 buffers
[i
] = newCtx
->Color
.DrawBuffer
[i
];
1653 _mesa_drawbuffers(newCtx
, newCtx
->Const
.MaxDrawBuffers
, buffers
, NULL
);
1655 if (!newCtx
->ReadBuffer
|| newCtx
->ReadBuffer
->Name
== 0) {
1656 _mesa_reference_framebuffer(&newCtx
->ReadBuffer
, readBuffer
);
1659 /* XXX only set this flag if we're really changing the draw/read
1660 * framebuffer bindings.
1662 newCtx
->NewState
|= _NEW_BUFFERS
;
1665 /* We want to get rid of these lines: */
1668 if (!drawBuffer
->Initialized
) {
1669 initialize_framebuffer_size(newCtx
, drawBuffer
);
1671 if (readBuffer
!= drawBuffer
&& !readBuffer
->Initialized
) {
1672 initialize_framebuffer_size(newCtx
, readBuffer
);
1675 _mesa_resizebuffers(newCtx
);
1679 /* We want the drawBuffer and readBuffer to be initialized by
1681 * This generally means the Width and Height match the actual
1682 * window size and the renderbuffers (both hardware and software
1683 * based) are allocated to match. The later can generally be
1684 * done with a call to _mesa_resize_framebuffer().
1686 * It's theoretically possible for a buffer to have zero width
1687 * or height, but for now, assert check that the driver did what's
1690 ASSERT(drawBuffer
->Width
> 0);
1691 ASSERT(drawBuffer
->Height
> 0);
1694 if (newCtx
->FirstTimeCurrent
) {
1695 /* set initial viewport and scissor size now */
1696 _mesa_set_viewport(newCtx
, 0, 0,
1697 drawBuffer
->Width
, drawBuffer
->Height
);
1698 _mesa_set_scissor(newCtx
, 0, 0,
1699 drawBuffer
->Width
, drawBuffer
->Height
);
1700 check_context_limits(newCtx
);
1704 /* We can use this to help debug user's problems. Tell them to set
1705 * the MESA_INFO env variable before running their app. Then the
1706 * first time each context is made current we'll print some useful
1709 if (newCtx
->FirstTimeCurrent
) {
1710 if (_mesa_getenv("MESA_INFO")) {
1713 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1720 * Make context 'ctx' share the display lists, textures and programs
1721 * that are associated with 'ctxToShare'.
1722 * Any display lists, textures or programs associated with 'ctx' will
1723 * be deleted if nobody else is sharing them.
1726 _mesa_share_state(GLcontext
*ctx
, GLcontext
*ctxToShare
)
1728 if (ctx
&& ctxToShare
&& ctx
->Shared
&& ctxToShare
->Shared
) {
1729 struct gl_shared_state
*oldSharedState
= ctx
->Shared
;
1731 ctx
->Shared
= ctxToShare
->Shared
;
1732 ctx
->Shared
->RefCount
++;
1734 update_default_objects(ctx
);
1736 oldSharedState
->RefCount
--;
1737 if (oldSharedState
->RefCount
== 0) {
1738 free_shared_state(ctx
, oldSharedState
);
1751 * \return pointer to the current GL context for this thread.
1753 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1754 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1758 _mesa_get_current_context( void )
1760 return (GLcontext
*) _glapi_get_context();
1765 * Get context's current API dispatch table.
1767 * It'll either be the immediate-mode execute dispatcher or the display list
1768 * compile dispatcher.
1770 * \param ctx GL context.
1772 * \return pointer to dispatch_table.
1774 * Simply returns __GLcontextRec::CurrentDispatch.
1776 struct _glapi_table
*
1777 _mesa_get_dispatch(GLcontext
*ctx
)
1779 return ctx
->CurrentDispatch
;
1785 /**********************************************************************/
1786 /** \name Miscellaneous functions */
1787 /**********************************************************************/
1793 * \param ctx GL context.
1794 * \param error error code.
1796 * Records the given error code and call the driver's dd_function_table::Error
1797 * function if defined.
1800 * This is called via _mesa_error().
1803 _mesa_record_error(GLcontext
*ctx
, GLenum error
)
1808 if (ctx
->ErrorValue
== GL_NO_ERROR
) {
1809 ctx
->ErrorValue
= error
;
1812 /* Call device driver's error handler, if any. This is used on the Mac. */
1813 if (ctx
->Driver
.Error
) {
1814 ctx
->Driver
.Error(ctx
);
1820 * Execute glFinish().
1822 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1823 * dd_function_table::Finish driver callback, if not NULL.
1828 GET_CURRENT_CONTEXT(ctx
);
1829 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1830 if (ctx
->Driver
.Finish
) {
1831 ctx
->Driver
.Finish(ctx
);
1837 * Execute glFlush().
1839 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1840 * dd_function_table::Flush driver callback, if not NULL.
1845 GET_CURRENT_CONTEXT(ctx
);
1846 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1847 if (ctx
->Driver
.Flush
) {
1848 ctx
->Driver
.Flush(ctx
);