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 #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"
123 #if FEATURE_pixel_transfer
126 #include "pixelstore.h"
129 #if FEATURE_ARB_occlusion_query
130 #include "queryobj.h"
136 #include "simple_list.h"
139 #include "texcompress.h"
140 #include "teximage.h"
142 #include "texstate.h"
147 #include "glapi/glthread.h"
148 #include "glapi/glapioffsets.h"
149 #include "glapi/glapitable.h"
150 #include "shader/program.h"
151 #include "shader/shader_api.h"
152 #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 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
443 ss
->Programs
= _mesa_NewHashTable();
446 #if FEATURE_ARB_vertex_program
447 ss
->DefaultVertexProgram
= (struct gl_vertex_program
*)
448 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
449 if (!ss
->DefaultVertexProgram
)
452 #if FEATURE_ARB_fragment_program
453 ss
->DefaultFragmentProgram
= (struct gl_fragment_program
*)
454 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
455 if (!ss
->DefaultFragmentProgram
)
458 #if FEATURE_ATI_fragment_shader
459 ss
->ATIShaders
= _mesa_NewHashTable();
460 ss
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
461 if (!ss
->DefaultFragmentShader
)
465 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
466 ss
->BufferObjects
= _mesa_NewHashTable();
469 ss
->ArrayObjects
= _mesa_NewHashTable();
471 #if FEATURE_ARB_shader_objects
472 ss
->ShaderObjects
= _mesa_NewHashTable();
475 ss
->Default1D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_1D
);
479 ss
->Default2D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_2D
);
483 ss
->Default3D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_3D
);
487 ss
->DefaultCubeMap
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_CUBE_MAP_ARB
);
488 if (!ss
->DefaultCubeMap
)
491 ss
->DefaultRect
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_RECTANGLE_NV
);
492 if (!ss
->DefaultRect
)
495 ss
->Default1DArray
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_1D_ARRAY_EXT
);
496 if (!ss
->Default1DArray
)
499 ss
->Default2DArray
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_2D_ARRAY_EXT
);
500 if (!ss
->Default2DArray
)
503 /* Effectively bind the default textures to all texture units */
504 ss
->Default1D
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
505 ss
->Default2D
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
506 ss
->Default3D
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
507 ss
->DefaultCubeMap
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
508 ss
->DefaultRect
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
509 ss
->Default1DArray
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
510 ss
->Default2DArray
->RefCount
+= MAX_TEXTURE_IMAGE_UNITS
;
512 _glthread_INIT_MUTEX(ss
->TexMutex
);
513 ss
->TextureStateStamp
= 0;
516 #if FEATURE_EXT_framebuffer_object
517 ss
->FrameBuffers
= _mesa_NewHashTable();
518 if (!ss
->FrameBuffers
)
520 ss
->RenderBuffers
= _mesa_NewHashTable();
521 if (!ss
->RenderBuffers
)
529 /* Ran out of memory at some point. Free everything and return NULL */
531 _mesa_DeleteHashTable(ss
->DisplayList
);
533 _mesa_DeleteHashTable(ss
->TexObjects
);
534 #if FEATURE_NV_vertex_program
536 _mesa_DeleteHashTable(ss
->Programs
);
538 #if FEATURE_ARB_vertex_program
539 _mesa_reference_vertprog(ctx
, &ss
->DefaultVertexProgram
, NULL
);
541 #if FEATURE_ARB_fragment_program
542 _mesa_reference_fragprog(ctx
, &ss
->DefaultFragmentProgram
, NULL
);
544 #if FEATURE_ATI_fragment_shader
545 if (ss
->DefaultFragmentShader
)
546 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
548 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
549 if (ss
->BufferObjects
)
550 _mesa_DeleteHashTable(ss
->BufferObjects
);
553 if (ss
->ArrayObjects
)
554 _mesa_DeleteHashTable (ss
->ArrayObjects
);
556 #if FEATURE_ARB_shader_objects
557 if (ss
->ShaderObjects
)
558 _mesa_DeleteHashTable (ss
->ShaderObjects
);
561 #if FEATURE_EXT_framebuffer_object
562 if (ss
->FrameBuffers
)
563 _mesa_DeleteHashTable(ss
->FrameBuffers
);
564 if (ss
->RenderBuffers
)
565 _mesa_DeleteHashTable(ss
->RenderBuffers
);
569 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default1D
);
571 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default2D
);
573 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default3D
);
574 if (ss
->DefaultCubeMap
)
575 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultCubeMap
);
577 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultRect
);
578 if (ss
->Default1DArray
)
579 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default1DArray
);
580 if (ss
->Default2DArray
)
581 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default2DArray
);
590 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
593 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
596 struct mesa_display_list
*list
= (struct mesa_display_list
*) data
;
597 GLcontext
*ctx
= (GLcontext
*) userData
;
598 _mesa_delete_list(ctx
, list
);
603 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
606 delete_texture_cb(GLuint id
, void *data
, void *userData
)
608 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
609 GLcontext
*ctx
= (GLcontext
*) userData
;
610 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
614 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
617 delete_program_cb(GLuint id
, void *data
, void *userData
)
619 struct gl_program
*prog
= (struct gl_program
*) data
;
620 GLcontext
*ctx
= (GLcontext
*) userData
;
621 ASSERT(prog
->RefCount
== 1); /* should only be referenced by hash table */
622 prog
->RefCount
= 0; /* now going away */
623 ctx
->Driver
.DeleteProgram(ctx
, prog
);
626 #if FEATURE_ATI_fragment_shader
628 * Callback for deleting an ATI fragment shader object.
629 * Called by _mesa_HashDeleteAll().
632 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
634 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
635 GLcontext
*ctx
= (GLcontext
*) userData
;
636 _mesa_delete_ati_fragment_shader(ctx
, shader
);
641 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
644 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
646 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
647 GLcontext
*ctx
= (GLcontext
*) userData
;
648 ctx
->Driver
.DeleteBuffer(ctx
, bufObj
);
652 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
655 delete_arrayobj_cb(GLuint id
, void *data
, void *userData
)
657 struct gl_array_object
*arrayObj
= (struct gl_array_object
*) data
;
658 GLcontext
*ctx
= (GLcontext
*) userData
;
659 _mesa_delete_array_object(ctx
, arrayObj
);
663 * Callback for freeing shader program data. Call it before delete_shader_cb
664 * to avoid memory access error.
667 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
669 GLcontext
*ctx
= (GLcontext
*) userData
;
670 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
672 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
673 _mesa_free_shader_program_data(ctx
, shProg
);
678 * Callback for deleting shader and shader programs objects.
679 * Called by _mesa_HashDeleteAll().
682 delete_shader_cb(GLuint id
, void *data
, void *userData
)
684 GLcontext
*ctx
= (GLcontext
*) userData
;
685 struct gl_shader
*sh
= (struct gl_shader
*) data
;
686 if (sh
->Type
== GL_FRAGMENT_SHADER
|| sh
->Type
== GL_VERTEX_SHADER
) {
687 _mesa_free_shader(ctx
, sh
);
690 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
691 ASSERT(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
692 _mesa_free_shader_program(ctx
, shProg
);
698 * Deallocate a shared state object and all children structures.
700 * \param ctx GL context.
701 * \param ss shared state pointer.
703 * Frees the display lists, the texture objects (calling the driver texture
704 * deletion callback to free its private data) and the vertex programs, as well
705 * as their hash tables.
707 * \sa alloc_shared_state().
710 free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
715 _mesa_HashDeleteAll(ss
->DisplayList
, delete_displaylist_cb
, ctx
);
716 _mesa_DeleteHashTable(ss
->DisplayList
);
718 #if FEATURE_ARB_shader_objects
719 _mesa_HashWalk(ss
->ShaderObjects
, free_shader_program_data_cb
, ctx
);
720 _mesa_HashDeleteAll(ss
->ShaderObjects
, delete_shader_cb
, ctx
);
721 _mesa_DeleteHashTable(ss
->ShaderObjects
);
724 #if defined(FEATURE_NV_vertex_program) || defined(FEATURE_NV_fragment_program)
725 _mesa_HashDeleteAll(ss
->Programs
, delete_program_cb
, ctx
);
726 _mesa_DeleteHashTable(ss
->Programs
);
728 #if FEATURE_ARB_vertex_program
729 _mesa_reference_vertprog(ctx
, &ss
->DefaultVertexProgram
, NULL
);
731 #if FEATURE_ARB_fragment_program
732 _mesa_reference_fragprog(ctx
, &ss
->DefaultFragmentProgram
, NULL
);
735 #if FEATURE_ATI_fragment_shader
736 _mesa_HashDeleteAll(ss
->ATIShaders
, delete_fragshader_cb
, ctx
);
737 _mesa_DeleteHashTable(ss
->ATIShaders
);
738 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
741 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
742 _mesa_HashDeleteAll(ss
->BufferObjects
, delete_bufferobj_cb
, ctx
);
743 _mesa_DeleteHashTable(ss
->BufferObjects
);
746 _mesa_HashDeleteAll(ss
->ArrayObjects
, delete_arrayobj_cb
, ctx
);
747 _mesa_DeleteHashTable(ss
->ArrayObjects
);
749 #if FEATURE_EXT_framebuffer_object
750 _mesa_DeleteHashTable(ss
->FrameBuffers
);
751 _mesa_DeleteHashTable(ss
->RenderBuffers
);
755 * Free texture objects (after FBOs since some textures might have
756 * been bound to FBOs).
758 ASSERT(ctx
->Driver
.DeleteTexture
);
759 /* the default textures */
760 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default1D
);
761 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default2D
);
762 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default3D
);
763 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultCubeMap
);
764 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultRect
);
765 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default1DArray
);
766 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default2DArray
);
767 /* all other textures */
768 _mesa_HashDeleteAll(ss
->TexObjects
, delete_texture_cb
, ctx
);
769 _mesa_DeleteHashTable(ss
->TexObjects
);
771 _glthread_DESTROY_MUTEX(ss
->Mutex
);
778 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
781 _mesa_init_current(GLcontext
*ctx
)
785 /* Init all to (0,0,0,1) */
786 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
787 ASSIGN_4V( ctx
->Current
.Attrib
[i
], 0.0, 0.0, 0.0, 1.0 );
790 /* redo special cases: */
791 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_WEIGHT
], 1.0, 0.0, 0.0, 1.0 );
792 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
], 0.0, 0.0, 1.0, 1.0 );
793 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], 1.0, 1.0, 1.0, 1.0 );
794 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
], 0.0, 0.0, 0.0, 1.0 );
795 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
], 1.0, 0.0, 0.0, 1.0 );
796 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
], 1.0, 0.0, 0.0, 1.0 );
801 * Init vertex/fragment program native limits from logical limits.
804 init_natives(struct gl_program_constants
*prog
)
806 prog
->MaxNativeInstructions
= prog
->MaxInstructions
;
807 prog
->MaxNativeAluInstructions
= prog
->MaxAluInstructions
;
808 prog
->MaxNativeTexInstructions
= prog
->MaxTexInstructions
;
809 prog
->MaxNativeTexIndirections
= prog
->MaxTexIndirections
;
810 prog
->MaxNativeAttribs
= prog
->MaxAttribs
;
811 prog
->MaxNativeTemps
= prog
->MaxTemps
;
812 prog
->MaxNativeAddressRegs
= prog
->MaxAddressRegs
;
813 prog
->MaxNativeParameters
= prog
->MaxParameters
;
818 * Initialize fields of gl_constants (aka ctx->Const.*).
819 * Use defaults from config.h. The device drivers will often override
820 * some of these values (such as number of texture units).
823 _mesa_init_constants(GLcontext
*ctx
)
827 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
828 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
830 assert(MAX_TEXTURE_UNITS
>= MAX_TEXTURE_COORD_UNITS
);
831 assert(MAX_TEXTURE_UNITS
>= MAX_TEXTURE_IMAGE_UNITS
);
833 /* Constants, may be overriden (usually only reduced) by device drivers */
834 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
835 ctx
->Const
.Max3DTextureLevels
= MAX_3D_TEXTURE_LEVELS
;
836 ctx
->Const
.MaxCubeTextureLevels
= MAX_CUBE_TEXTURE_LEVELS
;
837 ctx
->Const
.MaxTextureRectSize
= MAX_TEXTURE_RECT_SIZE
;
838 ctx
->Const
.MaxArrayTextureLayers
= MAX_ARRAY_TEXTURE_LAYERS
;
839 ctx
->Const
.MaxTextureCoordUnits
= MAX_TEXTURE_COORD_UNITS
;
840 ctx
->Const
.MaxTextureImageUnits
= MAX_TEXTURE_IMAGE_UNITS
;
841 ctx
->Const
.MaxTextureUnits
= MIN2(ctx
->Const
.MaxTextureCoordUnits
,
842 ctx
->Const
.MaxTextureImageUnits
);
843 ctx
->Const
.MaxTextureMaxAnisotropy
= MAX_TEXTURE_MAX_ANISOTROPY
;
844 ctx
->Const
.MaxTextureLodBias
= MAX_TEXTURE_LOD_BIAS
;
845 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
846 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
847 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
848 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
849 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
850 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
851 ctx
->Const
.PointSizeGranularity
= (GLfloat
) POINT_SIZE_GRANULARITY
;
852 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
853 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
854 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
855 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
856 ctx
->Const
.LineWidthGranularity
= (GLfloat
) LINE_WIDTH_GRANULARITY
;
857 ctx
->Const
.MaxColorTableSize
= MAX_COLOR_TABLE_SIZE
;
858 ctx
->Const
.MaxConvolutionWidth
= MAX_CONVOLUTION_WIDTH
;
859 ctx
->Const
.MaxConvolutionHeight
= MAX_CONVOLUTION_HEIGHT
;
860 ctx
->Const
.MaxClipPlanes
= MAX_CLIP_PLANES
;
861 ctx
->Const
.MaxLights
= MAX_LIGHTS
;
862 ctx
->Const
.MaxShininess
= 128.0;
863 ctx
->Const
.MaxSpotExponent
= 128.0;
864 ctx
->Const
.MaxViewportWidth
= MAX_WIDTH
;
865 ctx
->Const
.MaxViewportHeight
= MAX_HEIGHT
;
866 #if FEATURE_ARB_vertex_program
867 ctx
->Const
.VertexProgram
.MaxInstructions
= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
;
868 ctx
->Const
.VertexProgram
.MaxAluInstructions
= 0;
869 ctx
->Const
.VertexProgram
.MaxTexInstructions
= 0;
870 ctx
->Const
.VertexProgram
.MaxTexIndirections
= 0;
871 ctx
->Const
.VertexProgram
.MaxAttribs
= MAX_NV_VERTEX_PROGRAM_INPUTS
;
872 ctx
->Const
.VertexProgram
.MaxTemps
= MAX_PROGRAM_TEMPS
;
873 ctx
->Const
.VertexProgram
.MaxParameters
= MAX_NV_VERTEX_PROGRAM_PARAMS
;
874 ctx
->Const
.VertexProgram
.MaxLocalParams
= MAX_PROGRAM_LOCAL_PARAMS
;
875 ctx
->Const
.VertexProgram
.MaxEnvParams
= MAX_PROGRAM_ENV_PARAMS
;
876 ctx
->Const
.VertexProgram
.MaxAddressRegs
= MAX_VERTEX_PROGRAM_ADDRESS_REGS
;
877 ctx
->Const
.VertexProgram
.MaxUniformComponents
= 4 * MAX_UNIFORMS
;
878 init_natives(&ctx
->Const
.VertexProgram
);
881 #if FEATURE_ARB_fragment_program
882 ctx
->Const
.FragmentProgram
.MaxInstructions
= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
;
883 ctx
->Const
.FragmentProgram
.MaxAluInstructions
= MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS
;
884 ctx
->Const
.FragmentProgram
.MaxTexInstructions
= MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS
;
885 ctx
->Const
.FragmentProgram
.MaxTexIndirections
= MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS
;
886 ctx
->Const
.FragmentProgram
.MaxAttribs
= MAX_NV_FRAGMENT_PROGRAM_INPUTS
;
887 ctx
->Const
.FragmentProgram
.MaxTemps
= MAX_PROGRAM_TEMPS
;
888 ctx
->Const
.FragmentProgram
.MaxParameters
= MAX_NV_FRAGMENT_PROGRAM_PARAMS
;
889 ctx
->Const
.FragmentProgram
.MaxLocalParams
= MAX_PROGRAM_LOCAL_PARAMS
;
890 ctx
->Const
.FragmentProgram
.MaxEnvParams
= MAX_PROGRAM_ENV_PARAMS
;
891 ctx
->Const
.FragmentProgram
.MaxAddressRegs
= MAX_FRAGMENT_PROGRAM_ADDRESS_REGS
;
892 ctx
->Const
.FragmentProgram
.MaxUniformComponents
= 4 * MAX_UNIFORMS
;
893 init_natives(&ctx
->Const
.FragmentProgram
);
895 ctx
->Const
.MaxProgramMatrices
= MAX_PROGRAM_MATRICES
;
896 ctx
->Const
.MaxProgramMatrixStackDepth
= MAX_PROGRAM_MATRIX_STACK_DEPTH
;
898 /* CheckArrayBounds is overriden by drivers/x11 for X server */
899 ctx
->Const
.CheckArrayBounds
= GL_FALSE
;
901 /* GL_ARB_draw_buffers */
902 ctx
->Const
.MaxDrawBuffers
= MAX_DRAW_BUFFERS
;
904 /* GL_OES_read_format */
905 ctx
->Const
.ColorReadFormat
= GL_RGBA
;
906 ctx
->Const
.ColorReadType
= GL_UNSIGNED_BYTE
;
908 #if FEATURE_EXT_framebuffer_object
909 ctx
->Const
.MaxColorAttachments
= MAX_COLOR_ATTACHMENTS
;
910 ctx
->Const
.MaxRenderbufferSize
= MAX_WIDTH
;
913 #if FEATURE_ARB_vertex_shader
914 ctx
->Const
.MaxVertexTextureImageUnits
= MAX_VERTEX_TEXTURE_IMAGE_UNITS
;
915 ctx
->Const
.MaxVarying
= MAX_VARYING
;
919 ASSERT(ctx
->Const
.MaxTextureUnits
== MIN2(ctx
->Const
.MaxTextureImageUnits
,
920 ctx
->Const
.MaxTextureCoordUnits
));
921 ASSERT(ctx
->Const
.FragmentProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
922 ASSERT(ctx
->Const
.VertexProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
924 ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
925 ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
926 ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS
<= VERT_ATTRIB_MAX
);
927 ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS
<= VERT_RESULT_MAX
);
932 * Do some sanity checks on the limits/constants for the given context.
933 * Only called the first time a context is bound.
936 check_context_limits(GLcontext
*ctx
)
938 /* Many context limits/constants are limited by the size of
941 assert(ctx
->Const
.MaxTextureImageUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
942 assert(ctx
->Const
.MaxTextureCoordUnits
<= MAX_TEXTURE_COORD_UNITS
);
943 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
944 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_COORD_UNITS
);
946 assert(ctx
->Const
.MaxViewportWidth
<= MAX_WIDTH
);
947 assert(ctx
->Const
.MaxViewportHeight
<= MAX_WIDTH
);
949 /* make sure largest texture image is <= MAX_WIDTH in size */
950 assert((1 << (ctx
->Const
.MaxTextureLevels
-1 )) <= MAX_WIDTH
);
951 assert((1 << (ctx
->Const
.MaxCubeTextureLevels
-1 )) <= MAX_WIDTH
);
952 assert((1 << (ctx
->Const
.Max3DTextureLevels
-1 )) <= MAX_WIDTH
);
954 assert(ctx
->Const
.MaxDrawBuffers
<= MAX_DRAW_BUFFERS
);
956 /* XXX probably add more tests */
961 * Initialize the attribute groups in a GL context.
963 * \param ctx GL context.
965 * Initializes all the attributes, calling the respective <tt>init*</tt>
966 * functions for the more complex data structures.
969 init_attrib_groups(GLcontext
*ctx
)
974 _mesa_init_constants( ctx
);
977 _mesa_init_extensions( ctx
);
979 /* Attribute Groups */
981 _mesa_init_accum( ctx
);
983 #if FEATURE_attrib_stack
984 _mesa_init_attrib( ctx
);
986 _mesa_init_buffer_objects( ctx
);
987 _mesa_init_color( ctx
);
988 #if FEATURE_colortable
989 _mesa_init_colortables( ctx
);
991 _mesa_init_current( ctx
);
992 _mesa_init_depth( ctx
);
993 _mesa_init_debug( ctx
);
995 _mesa_init_display_list( ctx
);
997 #if FEATURE_evaluators
998 _mesa_init_eval( ctx
);
1000 #if FEATURE_feedback
1001 _mesa_init_feedback( ctx
);
1003 _mesa_init_fog( ctx
);
1004 #if FEATURE_histogram
1005 _mesa_init_histogram( ctx
);
1007 _mesa_init_hint( ctx
);
1008 _mesa_init_line( ctx
);
1009 _mesa_init_lighting( ctx
);
1010 _mesa_init_matrix( ctx
);
1011 _mesa_init_multisample( ctx
);
1012 #if FEATURE_pixel_transfer
1013 _mesa_init_pixel( ctx
);
1015 _mesa_init_pixelstore( ctx
);
1016 _mesa_init_point( ctx
);
1017 _mesa_init_polygon( ctx
);
1018 _mesa_init_program( ctx
);
1019 #if FEATURE_ARB_occlusion_query
1020 _mesa_init_query( ctx
);
1023 _mesa_init_rastpos( ctx
);
1025 _mesa_init_scissor( ctx
);
1026 _mesa_init_shader_state( ctx
);
1027 _mesa_init_stencil( ctx
);
1028 _mesa_init_transform( ctx
);
1029 _mesa_init_varray( ctx
);
1030 _mesa_init_viewport( ctx
);
1032 if (!_mesa_init_texture( ctx
))
1035 #if FEATURE_texture_s3tc
1036 _mesa_init_texture_s3tc( ctx
);
1038 #if FEATURE_texture_fxt1
1039 _mesa_init_texture_fxt1( ctx
);
1043 ctx
->NewState
= _NEW_ALL
;
1044 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1052 * This is the default function we plug into all dispatch table slots
1053 * This helps prevents a segfault when someone calls a GL function without
1054 * first checking if the extension's supported.
1059 _mesa_warning(NULL
, "User called no-op dispatch function (an unsupported extension function?)");
1065 * Allocate and initialize a new dispatch table.
1067 static struct _glapi_table
*
1068 alloc_dispatch_table(void)
1070 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1071 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1072 * Mesa we do this to accomodate different versions of libGL and various
1075 GLint numEntries
= MAX2(_glapi_get_dispatch_table_size(),
1076 sizeof(struct _glapi_table
) / sizeof(_glapi_proc
));
1077 struct _glapi_table
*table
=
1078 (struct _glapi_table
*) _mesa_malloc(numEntries
* sizeof(_glapi_proc
));
1080 _glapi_proc
*entry
= (_glapi_proc
*) table
;
1082 for (i
= 0; i
< numEntries
; i
++) {
1083 entry
[i
] = (_glapi_proc
) generic_nop
;
1091 * Initialize a GLcontext struct (rendering context).
1093 * This includes allocating all the other structs and arrays which hang off of
1094 * the context by pointers.
1095 * Note that the driver needs to pass in its dd_function_table here since
1096 * we need to at least call driverFunctions->NewTextureObject to create the
1097 * default texture objects.
1099 * Called by _mesa_create_context().
1101 * Performs the imports and exports callback tables initialization, and
1102 * miscellaneous one-time initializations. If no shared context is supplied one
1103 * is allocated, and increase its reference count. Setups the GL API dispatch
1104 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1105 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1108 * \param ctx the context to initialize
1109 * \param visual describes the visual attributes for this context
1110 * \param share_list points to context to share textures, display lists,
1112 * \param driverFunctions table of device driver functions for this context
1114 * \param driverContext pointer to driver-specific context data
1117 _mesa_initialize_context(GLcontext
*ctx
,
1118 const GLvisual
*visual
,
1119 GLcontext
*share_list
,
1120 const struct dd_function_table
*driverFunctions
,
1121 void *driverContext
)
1123 /*ASSERT(driverContext);*/
1124 assert(driverFunctions
->NewTextureObject
);
1125 assert(driverFunctions
->FreeTexImageData
);
1127 /* misc one-time initializations */
1130 ctx
->Visual
= *visual
;
1131 ctx
->DrawBuffer
= NULL
;
1132 ctx
->ReadBuffer
= NULL
;
1133 ctx
->WinSysDrawBuffer
= NULL
;
1134 ctx
->WinSysReadBuffer
= NULL
;
1136 /* Plug in driver functions and context pointer here.
1137 * This is important because when we call alloc_shared_state() below
1138 * we'll call ctx->Driver.NewTextureObject() to create the default
1141 ctx
->Driver
= *driverFunctions
;
1142 ctx
->DriverCtx
= driverContext
;
1145 /* share state with another context */
1146 ctx
->Shared
= share_list
->Shared
;
1149 /* allocate new, unshared state */
1150 if (!alloc_shared_state( ctx
)) {
1154 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1155 ctx
->Shared
->RefCount
++;
1156 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1158 if (!init_attrib_groups( ctx
)) {
1159 free_shared_state(ctx
, ctx
->Shared
);
1163 /* setup the API dispatch tables */
1164 ctx
->Exec
= alloc_dispatch_table();
1165 ctx
->Save
= alloc_dispatch_table();
1166 if (!ctx
->Exec
|| !ctx
->Save
) {
1167 free_shared_state(ctx
, ctx
->Shared
);
1169 _mesa_free(ctx
->Exec
);
1171 _mesa_init_exec_table(ctx
->Exec
);
1172 ctx
->CurrentDispatch
= ctx
->Exec
;
1174 _mesa_init_dlist_table(ctx
->Save
);
1175 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
1177 /* Neutral tnl module stuff */
1178 _mesa_init_exec_vtxfmt( ctx
);
1179 ctx
->TnlModule
.Current
= NULL
;
1180 ctx
->TnlModule
.SwapCount
= 0;
1182 ctx
->FragmentProgram
._MaintainTexEnvProgram
1183 = (_mesa_getenv("MESA_TEX_PROG") != NULL
);
1184 ctx
->FragmentProgram
._UseTexEnvProgram
= ctx
->FragmentProgram
._MaintainTexEnvProgram
;
1186 ctx
->VertexProgram
._MaintainTnlProgram
1187 = (_mesa_getenv("MESA_TNL_PROG") != NULL
);
1188 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
1189 /* this is required... */
1190 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
1193 ctx
->FirstTimeCurrent
= GL_TRUE
;
1200 * Allocate and initialize a GLcontext structure.
1201 * Note that the driver needs to pass in its dd_function_table here since
1202 * we need to at least call driverFunctions->NewTextureObject to initialize
1203 * the rendering context.
1205 * \param visual a GLvisual pointer (we copy the struct contents)
1206 * \param share_list another context to share display lists with or NULL
1207 * \param driverFunctions points to the dd_function_table into which the
1208 * driver has plugged in all its special functions.
1209 * \param driverCtx points to the device driver's private context state
1211 * \return pointer to a new __GLcontextRec or NULL if error.
1214 _mesa_create_context(const GLvisual
*visual
,
1215 GLcontext
*share_list
,
1216 const struct dd_function_table
*driverFunctions
,
1217 void *driverContext
)
1222 /*ASSERT(driverContext);*/
1224 ctx
= (GLcontext
*) _mesa_calloc(sizeof(GLcontext
));
1228 if (_mesa_initialize_context(ctx
, visual
, share_list
,
1229 driverFunctions
, driverContext
)) {
1240 * Free the data associated with the given context.
1242 * But doesn't free the GLcontext struct itself.
1244 * \sa _mesa_initialize_context() and init_attrib_groups().
1247 _mesa_free_context_data( GLcontext
*ctx
)
1249 /* if we're destroying the current context, unbind it first */
1250 if (ctx
== _mesa_get_current_context()) {
1251 _mesa_make_current(NULL
, NULL
, NULL
);
1254 /* unreference WinSysDraw/Read buffers */
1255 _mesa_unreference_framebuffer(&ctx
->WinSysDrawBuffer
);
1256 _mesa_unreference_framebuffer(&ctx
->WinSysReadBuffer
);
1257 _mesa_unreference_framebuffer(&ctx
->DrawBuffer
);
1258 _mesa_unreference_framebuffer(&ctx
->ReadBuffer
);
1261 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
1262 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
1263 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._TnlProgram
, NULL
);
1265 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
1266 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
1267 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
, NULL
);
1269 _mesa_free_lighting_data( ctx
);
1270 #if FEATURE_evaluators
1271 _mesa_free_eval_data( ctx
);
1273 _mesa_free_texture_data( ctx
);
1274 _mesa_free_matrix_data( ctx
);
1275 _mesa_free_viewport_data( ctx
);
1276 #if FEATURE_colortable
1277 _mesa_free_colortables_data( ctx
);
1279 _mesa_free_program_data(ctx
);
1280 _mesa_free_shader_state(ctx
);
1281 #if FEATURE_ARB_occlusion_query
1282 _mesa_free_query_data(ctx
);
1285 #if FEATURE_ARB_vertex_buffer_object
1286 _mesa_delete_buffer_object(ctx
, ctx
->Array
.NullBufferObj
);
1288 _mesa_delete_array_object(ctx
, ctx
->Array
.DefaultArrayObj
);
1290 /* free dispatch tables */
1291 _mesa_free(ctx
->Exec
);
1292 _mesa_free(ctx
->Save
);
1294 /* Shared context state (display lists, textures, etc) */
1295 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1296 ctx
->Shared
->RefCount
--;
1297 assert(ctx
->Shared
->RefCount
>= 0);
1298 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1299 if (ctx
->Shared
->RefCount
== 0) {
1300 /* free shared state */
1301 free_shared_state( ctx
, ctx
->Shared
);
1304 if (ctx
->Extensions
.String
)
1305 _mesa_free((void *) ctx
->Extensions
.String
);
1310 * Destroy a GLcontext structure.
1312 * \param ctx GL context.
1314 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1317 _mesa_destroy_context( GLcontext
*ctx
)
1320 _mesa_free_context_data(ctx
);
1321 _mesa_free( (void *) ctx
);
1328 * Copy attribute groups from one context to another.
1330 * \param src source context
1331 * \param dst destination context
1332 * \param mask bitwise OR of GL_*_BIT flags
1334 * According to the bits specified in \p mask, copies the corresponding
1335 * attributes from \p src into \p dst. For many of the attributes a simple \c
1336 * memcpy is not enough due to the existence of internal pointers in their data
1340 _mesa_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1342 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1344 dst
->Accum
= src
->Accum
;
1346 if (mask
& GL_COLOR_BUFFER_BIT
) {
1348 dst
->Color
= src
->Color
;
1350 if (mask
& GL_CURRENT_BIT
) {
1352 dst
->Current
= src
->Current
;
1354 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1356 dst
->Depth
= src
->Depth
;
1358 if (mask
& GL_ENABLE_BIT
) {
1361 if (mask
& GL_EVAL_BIT
) {
1363 dst
->Eval
= src
->Eval
;
1365 if (mask
& GL_FOG_BIT
) {
1367 dst
->Fog
= src
->Fog
;
1369 if (mask
& GL_HINT_BIT
) {
1371 dst
->Hint
= src
->Hint
;
1373 if (mask
& GL_LIGHTING_BIT
) {
1375 /* begin with memcpy */
1376 dst
->Light
= src
->Light
;
1377 /* fixup linked lists to prevent pointer insanity */
1378 make_empty_list( &(dst
->Light
.EnabledList
) );
1379 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1380 if (dst
->Light
.Light
[i
].Enabled
) {
1381 insert_at_tail(&(dst
->Light
.EnabledList
), &(dst
->Light
.Light
[i
]));
1385 if (mask
& GL_LINE_BIT
) {
1387 dst
->Line
= src
->Line
;
1389 if (mask
& GL_LIST_BIT
) {
1391 dst
->List
= src
->List
;
1393 if (mask
& GL_PIXEL_MODE_BIT
) {
1395 dst
->Pixel
= src
->Pixel
;
1397 if (mask
& GL_POINT_BIT
) {
1399 dst
->Point
= src
->Point
;
1401 if (mask
& GL_POLYGON_BIT
) {
1403 dst
->Polygon
= src
->Polygon
;
1405 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1406 /* Use loop instead of MEMCPY due to problem with Portland Group's
1407 * C compiler. Reported by John Stone.
1410 for (i
= 0; i
< 32; i
++) {
1411 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1414 if (mask
& GL_SCISSOR_BIT
) {
1416 dst
->Scissor
= src
->Scissor
;
1418 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1420 dst
->Stencil
= src
->Stencil
;
1422 if (mask
& GL_TEXTURE_BIT
) {
1423 /* Cannot memcpy because of pointers */
1424 _mesa_copy_texture_state(src
, dst
);
1426 if (mask
& GL_TRANSFORM_BIT
) {
1428 dst
->Transform
= src
->Transform
;
1430 if (mask
& GL_VIEWPORT_BIT
) {
1431 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1432 dst
->Viewport
.X
= src
->Viewport
.X
;
1433 dst
->Viewport
.Y
= src
->Viewport
.Y
;
1434 dst
->Viewport
.Width
= src
->Viewport
.Width
;
1435 dst
->Viewport
.Height
= src
->Viewport
.Height
;
1436 dst
->Viewport
.Near
= src
->Viewport
.Near
;
1437 dst
->Viewport
.Far
= src
->Viewport
.Far
;
1438 _math_matrix_copy(&dst
->Viewport
._WindowMap
, &src
->Viewport
._WindowMap
);
1441 /* XXX FIXME: Call callbacks?
1443 dst
->NewState
= _NEW_ALL
;
1449 * Check if the given context can render into the given framebuffer
1450 * by checking visual attributes.
1452 * Most of these tests could go away because Mesa is now pretty flexible
1453 * in terms of mixing rendering contexts with framebuffers. As long
1454 * as RGB vs. CI mode agree, we're probably good.
1456 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1459 check_compatible(const GLcontext
*ctx
, const GLframebuffer
*buffer
)
1461 const GLvisual
*ctxvis
= &ctx
->Visual
;
1462 const GLvisual
*bufvis
= &buffer
->Visual
;
1464 if (ctxvis
== bufvis
)
1467 if (ctxvis
->rgbMode
!= bufvis
->rgbMode
)
1470 /* disabling this fixes the fgl_glxgears pbuffer demo */
1471 if (ctxvis
->doubleBufferMode
&& !bufvis
->doubleBufferMode
)
1474 if (ctxvis
->stereoMode
&& !bufvis
->stereoMode
)
1476 if (ctxvis
->haveAccumBuffer
&& !bufvis
->haveAccumBuffer
)
1478 if (ctxvis
->haveDepthBuffer
&& !bufvis
->haveDepthBuffer
)
1480 if (ctxvis
->haveStencilBuffer
&& !bufvis
->haveStencilBuffer
)
1482 if (ctxvis
->redMask
&& ctxvis
->redMask
!= bufvis
->redMask
)
1484 if (ctxvis
->greenMask
&& ctxvis
->greenMask
!= bufvis
->greenMask
)
1486 if (ctxvis
->blueMask
&& ctxvis
->blueMask
!= bufvis
->blueMask
)
1489 /* disabled (see bug 11161) */
1490 if (ctxvis
->depthBits
&& ctxvis
->depthBits
!= bufvis
->depthBits
)
1493 if (ctxvis
->stencilBits
&& ctxvis
->stencilBits
!= bufvis
->stencilBits
)
1501 * Do one-time initialization for the given framebuffer. Specifically,
1502 * ask the driver for the window's current size and update the framebuffer
1504 * Really, the device driver should totally take care of this.
1507 initialize_framebuffer_size(GLcontext
*ctx
, GLframebuffer
*fb
)
1509 GLuint width
, height
;
1510 if (ctx
->Driver
.GetBufferSize
) {
1511 ctx
->Driver
.GetBufferSize(fb
, &width
, &height
);
1512 if (ctx
->Driver
.ResizeBuffers
)
1513 ctx
->Driver
.ResizeBuffers(ctx
, fb
, width
, height
);
1514 fb
->Initialized
= GL_TRUE
;
1520 * Bind the given context to the given drawBuffer and readBuffer and
1521 * make it the current context for the calling thread.
1522 * We'll render into the drawBuffer and read pixels from the
1523 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1525 * We check that the context's and framebuffer's visuals are compatible
1526 * and return immediately if they're not.
1528 * \param newCtx the new GL context. If NULL then there will be no current GL
1530 * \param drawBuffer the drawing framebuffer
1531 * \param readBuffer the reading framebuffer
1534 _mesa_make_current( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1535 GLframebuffer
*readBuffer
)
1537 GET_CURRENT_CONTEXT(oldCtx
);
1539 if (MESA_VERBOSE
& VERBOSE_API
)
1540 _mesa_debug(newCtx
, "_mesa_make_current()\n");
1542 /* Check that the context's and framebuffer's visuals are compatible.
1544 if (newCtx
&& drawBuffer
&& newCtx
->WinSysDrawBuffer
!= drawBuffer
) {
1545 if (!check_compatible(newCtx
, drawBuffer
)) {
1546 _mesa_warning(newCtx
,
1547 "MakeCurrent: incompatible visuals for context and drawbuffer");
1551 if (newCtx
&& readBuffer
&& newCtx
->WinSysReadBuffer
!= readBuffer
) {
1552 if (!check_compatible(newCtx
, readBuffer
)) {
1553 _mesa_warning(newCtx
,
1554 "MakeCurrent: incompatible visuals for context and readbuffer");
1559 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1560 _glapi_set_context((void *) newCtx
);
1561 ASSERT(_mesa_get_current_context() == newCtx
);
1564 _mesa_unreference_framebuffer(&oldCtx
->WinSysDrawBuffer
);
1565 _mesa_unreference_framebuffer(&oldCtx
->WinSysReadBuffer
);
1566 _mesa_unreference_framebuffer(&oldCtx
->DrawBuffer
);
1567 _mesa_unreference_framebuffer(&oldCtx
->ReadBuffer
);
1571 _glapi_set_dispatch(NULL
); /* none current */
1574 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1576 if (drawBuffer
&& readBuffer
) {
1577 /* TODO: check if newCtx and buffer's visual match??? */
1579 ASSERT(drawBuffer
->Name
== 0);
1580 ASSERT(readBuffer
->Name
== 0);
1581 _mesa_reference_framebuffer(&newCtx
->WinSysDrawBuffer
, drawBuffer
);
1582 _mesa_reference_framebuffer(&newCtx
->WinSysReadBuffer
, readBuffer
);
1585 * Only set the context's Draw/ReadBuffer fields if they're NULL
1586 * or not bound to a user-created FBO.
1588 if (!newCtx
->DrawBuffer
|| newCtx
->DrawBuffer
->Name
== 0) {
1589 /* fix up the fb fields - these will end up wrong otherwise
1590 * if the DRIdrawable changes, and everything relies on them.
1591 * This is a bit messy (same as needed in _mesa_BindFramebufferEXT)
1594 GLenum buffers
[MAX_DRAW_BUFFERS
];
1596 _mesa_reference_framebuffer(&newCtx
->DrawBuffer
, drawBuffer
);
1598 for(i
= 0; i
< newCtx
->Const
.MaxDrawBuffers
; i
++) {
1599 buffers
[i
] = newCtx
->Color
.DrawBuffer
[i
];
1602 _mesa_drawbuffers(newCtx
, newCtx
->Const
.MaxDrawBuffers
, buffers
, NULL
);
1604 if (!newCtx
->ReadBuffer
|| newCtx
->ReadBuffer
->Name
== 0) {
1605 _mesa_reference_framebuffer(&newCtx
->ReadBuffer
, readBuffer
);
1606 _mesa_readbuffer_update_fields(newCtx
, newCtx
->Pixel
.ReadBuffer
);
1609 newCtx
->NewState
|= _NEW_BUFFERS
;
1612 /* We want to get rid of these lines: */
1615 if (!drawBuffer
->Initialized
) {
1616 initialize_framebuffer_size(newCtx
, drawBuffer
);
1618 if (readBuffer
!= drawBuffer
&& !readBuffer
->Initialized
) {
1619 initialize_framebuffer_size(newCtx
, readBuffer
);
1622 _mesa_resizebuffers(newCtx
);
1626 /* We want the drawBuffer and readBuffer to be initialized by
1628 * This generally means the Width and Height match the actual
1629 * window size and the renderbuffers (both hardware and software
1630 * based) are allocated to match. The later can generally be
1631 * done with a call to _mesa_resize_framebuffer().
1633 * It's theoretically possible for a buffer to have zero width
1634 * or height, but for now, assert check that the driver did what's
1637 ASSERT(drawBuffer
->Width
> 0);
1638 ASSERT(drawBuffer
->Height
> 0);
1641 if (newCtx
->FirstTimeCurrent
) {
1642 /* set initial viewport and scissor size now */
1643 _mesa_set_viewport(newCtx
, 0, 0,
1644 drawBuffer
->Width
, drawBuffer
->Height
);
1645 _mesa_set_scissor(newCtx
, 0, 0,
1646 drawBuffer
->Width
, drawBuffer
->Height
);
1647 check_context_limits(newCtx
);
1651 /* We can use this to help debug user's problems. Tell them to set
1652 * the MESA_INFO env variable before running their app. Then the
1653 * first time each context is made current we'll print some useful
1656 if (newCtx
->FirstTimeCurrent
) {
1657 if (_mesa_getenv("MESA_INFO")) {
1660 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1667 * Make context 'ctx' share the display lists, textures and programs
1668 * that are associated with 'ctxToShare'.
1669 * Any display lists, textures or programs associated with 'ctx' will
1670 * be deleted if nobody else is sharing them.
1673 _mesa_share_state(GLcontext
*ctx
, GLcontext
*ctxToShare
)
1675 if (ctx
&& ctxToShare
&& ctx
->Shared
&& ctxToShare
->Shared
) {
1676 ctx
->Shared
->RefCount
--;
1677 if (ctx
->Shared
->RefCount
== 0) {
1678 free_shared_state(ctx
, ctx
->Shared
);
1680 ctx
->Shared
= ctxToShare
->Shared
;
1681 ctx
->Shared
->RefCount
++;
1692 * \return pointer to the current GL context for this thread.
1694 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1695 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1699 _mesa_get_current_context( void )
1701 return (GLcontext
*) _glapi_get_context();
1706 * Get context's current API dispatch table.
1708 * It'll either be the immediate-mode execute dispatcher or the display list
1709 * compile dispatcher.
1711 * \param ctx GL context.
1713 * \return pointer to dispatch_table.
1715 * Simply returns __GLcontextRec::CurrentDispatch.
1717 struct _glapi_table
*
1718 _mesa_get_dispatch(GLcontext
*ctx
)
1720 return ctx
->CurrentDispatch
;
1726 /**********************************************************************/
1727 /** \name Miscellaneous functions */
1728 /**********************************************************************/
1734 * \param ctx GL context.
1735 * \param error error code.
1737 * Records the given error code and call the driver's dd_function_table::Error
1738 * function if defined.
1741 * This is called via _mesa_error().
1744 _mesa_record_error(GLcontext
*ctx
, GLenum error
)
1749 if (ctx
->ErrorValue
== GL_NO_ERROR
) {
1750 ctx
->ErrorValue
= error
;
1753 /* Call device driver's error handler, if any. This is used on the Mac. */
1754 if (ctx
->Driver
.Error
) {
1755 ctx
->Driver
.Error(ctx
);
1761 * Execute glFinish().
1763 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1764 * dd_function_table::Finish driver callback, if not NULL.
1769 GET_CURRENT_CONTEXT(ctx
);
1770 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1771 if (ctx
->Driver
.Finish
) {
1772 ctx
->Driver
.Finish(ctx
);
1778 * Execute glFlush().
1780 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1781 * dd_function_table::Flush driver callback, if not NULL.
1786 GET_CURRENT_CONTEXT(ctx
);
1787 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1788 if (ctx
->Driver
.Flush
) {
1789 ctx
->Driver
.Flush(ctx
);