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_matrix.h"
158 #include "sparc/sparc.h"
162 int MESA_VERBOSE
= 0;
165 #ifndef MESA_DEBUG_FLAGS
166 int MESA_DEBUG_FLAGS
= 0;
170 /* ubyte -> float conversion */
171 GLfloat _mesa_ubyte_to_float_color_tab
[256];
176 * Swap buffers notification callback.
178 * \param gc GL context.
180 * Called by window system just before swapping buffers.
181 * We have to finish any pending rendering.
184 _mesa_notifySwapBuffers(__GLcontext
*ctx
)
186 if (ctx
->Driver
.Flush
) {
187 ctx
->Driver
.Flush(ctx
);
192 /**********************************************************************/
193 /** \name GL Visual allocation/destruction */
194 /**********************************************************************/
198 * Allocates a GLvisual structure and initializes it via
199 * _mesa_initialize_visual().
201 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
202 * \param dbFlag double buffering
203 * \param stereoFlag stereo buffer
204 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
205 * is acceptable but the actual depth type will be GLushort or GLuint as
207 * \param stencilBits requested minimum bits per stencil buffer value
208 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
209 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
210 * \param redBits number of bits per color component in frame buffer for RGB(A)
211 * mode. We always use 8 in core Mesa though.
212 * \param greenBits same as above.
213 * \param blueBits same as above.
214 * \param alphaBits same as above.
215 * \param numSamples not really used.
217 * \return pointer to new GLvisual or NULL if requested parameters can't be
220 * \note Need to add params for level and numAuxBuffers (at least)
223 _mesa_create_visual( GLboolean rgbFlag
,
225 GLboolean stereoFlag
,
234 GLint accumGreenBits
,
236 GLint accumAlphaBits
,
239 GLvisual
*vis
= (GLvisual
*) _mesa_calloc(sizeof(GLvisual
));
241 if (!_mesa_initialize_visual(vis
, rgbFlag
, dbFlag
, stereoFlag
,
242 redBits
, greenBits
, blueBits
, alphaBits
,
243 indexBits
, depthBits
, stencilBits
,
244 accumRedBits
, accumGreenBits
,
245 accumBlueBits
, accumAlphaBits
,
255 * Makes some sanity checks and fills in the fields of the
256 * GLvisual object with the given parameters. If the caller needs
257 * to set additional fields, he should just probably init the whole GLvisual
259 * \return GL_TRUE on success, or GL_FALSE on failure.
261 * \sa _mesa_create_visual() above for the parameter description.
264 _mesa_initialize_visual( GLvisual
*vis
,
267 GLboolean stereoFlag
,
276 GLint accumGreenBits
,
278 GLint accumAlphaBits
,
283 if (depthBits
< 0 || depthBits
> 32) {
286 if (stencilBits
< 0 || stencilBits
> STENCIL_BITS
) {
289 assert(accumRedBits
>= 0);
290 assert(accumGreenBits
>= 0);
291 assert(accumBlueBits
>= 0);
292 assert(accumAlphaBits
>= 0);
294 vis
->rgbMode
= rgbFlag
;
295 vis
->doubleBufferMode
= dbFlag
;
296 vis
->stereoMode
= stereoFlag
;
298 vis
->redBits
= redBits
;
299 vis
->greenBits
= greenBits
;
300 vis
->blueBits
= blueBits
;
301 vis
->alphaBits
= alphaBits
;
302 vis
->rgbBits
= redBits
+ greenBits
+ blueBits
;
304 vis
->indexBits
= indexBits
;
305 vis
->depthBits
= depthBits
;
306 vis
->stencilBits
= stencilBits
;
308 vis
->accumRedBits
= accumRedBits
;
309 vis
->accumGreenBits
= accumGreenBits
;
310 vis
->accumBlueBits
= accumBlueBits
;
311 vis
->accumAlphaBits
= accumAlphaBits
;
313 vis
->haveAccumBuffer
= accumRedBits
> 0;
314 vis
->haveDepthBuffer
= depthBits
> 0;
315 vis
->haveStencilBuffer
= stencilBits
> 0;
317 vis
->numAuxBuffers
= 0;
320 vis
->sampleBuffers
= numSamples
> 0 ? 1 : 0;
321 vis
->samples
= numSamples
;
328 * Destroy a visual and free its memory.
332 * Frees the visual structure.
335 _mesa_destroy_visual( GLvisual
*vis
)
343 /**********************************************************************/
344 /** \name Context allocation, initialization, destroying
346 * The purpose of the most initialization functions here is to provide the
347 * default state values according to the OpenGL specification.
349 /**********************************************************************/
353 * One-time initialization mutex lock.
355 * \sa Used by one_time_init().
357 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock
);
360 * Calls all the various one-time-init functions in Mesa.
362 * While holding a global mutex lock, calls several initialization functions,
363 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
369 one_time_init( GLcontext
*ctx
)
371 static GLboolean alreadyCalled
= GL_FALSE
;
373 _glthread_LOCK_MUTEX(OneTimeLock
);
374 if (!alreadyCalled
) {
377 /* do some implementation tests */
378 assert( sizeof(GLbyte
) == 1 );
379 assert( sizeof(GLubyte
) == 1 );
380 assert( sizeof(GLshort
) == 2 );
381 assert( sizeof(GLushort
) == 2 );
382 assert( sizeof(GLint
) == 4 );
383 assert( sizeof(GLuint
) == 4 );
385 _mesa_init_sqrt_table();
387 for (i
= 0; i
< 256; i
++) {
388 _mesa_ubyte_to_float_color_tab
[i
] = (float) i
/ 255.0F
;
392 _mesa_init_sparc_glapi_relocs();
394 if (_mesa_getenv("MESA_DEBUG")) {
395 _glapi_noop_enable_warnings(GL_TRUE
);
396 _glapi_set_warning_func( (_glapi_warning_func
) _mesa_warning
);
399 _glapi_noop_enable_warnings(GL_FALSE
);
402 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
403 _mesa_debug(ctx
, "Mesa %s DEBUG build %s %s\n",
404 MESA_VERSION_STRING
, __DATE__
, __TIME__
);
407 alreadyCalled
= GL_TRUE
;
409 _glthread_UNLOCK_MUTEX(OneTimeLock
);
414 * Allocate and initialize a shared context state structure.
415 * Initializes the display list, texture objects and vertex programs hash
416 * tables, allocates the texture objects. If it runs out of memory, frees
417 * everything already allocated before returning NULL.
419 * \return pointer to a gl_shared_state structure on success, or NULL on
423 alloc_shared_state( GLcontext
*ctx
)
425 struct gl_shared_state
*ss
= CALLOC_STRUCT(gl_shared_state
);
431 _glthread_INIT_MUTEX(ss
->Mutex
);
433 ss
->DisplayList
= _mesa_NewHashTable();
434 ss
->TexObjects
= _mesa_NewHashTable();
435 ss
->Programs
= _mesa_NewHashTable();
437 #if FEATURE_ARB_vertex_program
438 ss
->DefaultVertexProgram
= (struct gl_vertex_program
*)
439 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
440 if (!ss
->DefaultVertexProgram
)
443 #if FEATURE_ARB_fragment_program
444 ss
->DefaultFragmentProgram
= (struct gl_fragment_program
*)
445 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
446 if (!ss
->DefaultFragmentProgram
)
449 #if FEATURE_ATI_fragment_shader
450 ss
->ATIShaders
= _mesa_NewHashTable();
451 ss
->DefaultFragmentShader
= _mesa_new_ati_fragment_shader(ctx
, 0);
452 if (!ss
->DefaultFragmentShader
)
456 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
457 ss
->BufferObjects
= _mesa_NewHashTable();
460 ss
->ArrayObjects
= _mesa_NewHashTable();
462 #if FEATURE_ARB_shader_objects
463 ss
->ShaderObjects
= _mesa_NewHashTable();
466 ss
->Default1D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_1D
);
470 ss
->Default2D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_2D
);
474 ss
->Default3D
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_3D
);
478 ss
->DefaultCubeMap
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_CUBE_MAP_ARB
);
479 if (!ss
->DefaultCubeMap
)
482 ss
->DefaultRect
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_RECTANGLE_NV
);
483 if (!ss
->DefaultRect
)
486 ss
->Default1DArray
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_1D_ARRAY_EXT
);
487 if (!ss
->Default1DArray
)
490 ss
->Default2DArray
= (*ctx
->Driver
.NewTextureObject
)(ctx
, 0, GL_TEXTURE_2D_ARRAY_EXT
);
491 if (!ss
->Default2DArray
)
495 assert(ss
->Default1D
->RefCount
== 1);
497 _glthread_INIT_MUTEX(ss
->TexMutex
);
498 ss
->TextureStateStamp
= 0;
500 #if FEATURE_EXT_framebuffer_object
501 ss
->FrameBuffers
= _mesa_NewHashTable();
502 if (!ss
->FrameBuffers
)
504 ss
->RenderBuffers
= _mesa_NewHashTable();
505 if (!ss
->RenderBuffers
)
512 /* Ran out of memory at some point. Free everything and return NULL */
514 _mesa_DeleteHashTable(ss
->DisplayList
);
516 _mesa_DeleteHashTable(ss
->TexObjects
);
518 _mesa_DeleteHashTable(ss
->Programs
);
519 #if FEATURE_ARB_vertex_program
520 _mesa_reference_vertprog(ctx
, &ss
->DefaultVertexProgram
, NULL
);
522 #if FEATURE_ARB_fragment_program
523 _mesa_reference_fragprog(ctx
, &ss
->DefaultFragmentProgram
, NULL
);
525 #if FEATURE_ATI_fragment_shader
526 if (ss
->DefaultFragmentShader
)
527 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
529 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
530 if (ss
->BufferObjects
)
531 _mesa_DeleteHashTable(ss
->BufferObjects
);
534 if (ss
->ArrayObjects
)
535 _mesa_DeleteHashTable (ss
->ArrayObjects
);
537 #if FEATURE_ARB_shader_objects
538 if (ss
->ShaderObjects
)
539 _mesa_DeleteHashTable (ss
->ShaderObjects
);
542 #if FEATURE_EXT_framebuffer_object
543 if (ss
->FrameBuffers
)
544 _mesa_DeleteHashTable(ss
->FrameBuffers
);
545 if (ss
->RenderBuffers
)
546 _mesa_DeleteHashTable(ss
->RenderBuffers
);
550 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default1D
);
552 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default2D
);
554 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default3D
);
555 if (ss
->DefaultCubeMap
)
556 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultCubeMap
);
558 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->DefaultRect
);
559 if (ss
->Default1DArray
)
560 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default1DArray
);
561 if (ss
->Default2DArray
)
562 (*ctx
->Driver
.DeleteTexture
)(ctx
, ss
->Default2DArray
);
571 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
574 delete_displaylist_cb(GLuint id
, void *data
, void *userData
)
577 struct gl_display_list
*list
= (struct gl_display_list
*) data
;
578 GLcontext
*ctx
= (GLcontext
*) userData
;
579 _mesa_delete_list(ctx
, list
);
584 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
587 delete_texture_cb(GLuint id
, void *data
, void *userData
)
589 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
590 GLcontext
*ctx
= (GLcontext
*) userData
;
591 ctx
->Driver
.DeleteTexture(ctx
, texObj
);
595 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
598 delete_program_cb(GLuint id
, void *data
, void *userData
)
600 struct gl_program
*prog
= (struct gl_program
*) data
;
601 GLcontext
*ctx
= (GLcontext
*) userData
;
602 ASSERT(prog
->RefCount
== 1); /* should only be referenced by hash table */
603 prog
->RefCount
= 0; /* now going away */
604 ctx
->Driver
.DeleteProgram(ctx
, prog
);
607 #if FEATURE_ATI_fragment_shader
609 * Callback for deleting an ATI fragment shader object.
610 * Called by _mesa_HashDeleteAll().
613 delete_fragshader_cb(GLuint id
, void *data
, void *userData
)
615 struct ati_fragment_shader
*shader
= (struct ati_fragment_shader
*) data
;
616 GLcontext
*ctx
= (GLcontext
*) userData
;
617 _mesa_delete_ati_fragment_shader(ctx
, shader
);
622 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
625 delete_bufferobj_cb(GLuint id
, void *data
, void *userData
)
627 struct gl_buffer_object
*bufObj
= (struct gl_buffer_object
*) data
;
628 GLcontext
*ctx
= (GLcontext
*) userData
;
629 ctx
->Driver
.DeleteBuffer(ctx
, bufObj
);
633 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
636 delete_arrayobj_cb(GLuint id
, void *data
, void *userData
)
638 struct gl_array_object
*arrayObj
= (struct gl_array_object
*) data
;
639 GLcontext
*ctx
= (GLcontext
*) userData
;
640 _mesa_delete_array_object(ctx
, arrayObj
);
644 * Callback for freeing shader program data. Call it before delete_shader_cb
645 * to avoid memory access error.
648 free_shader_program_data_cb(GLuint id
, void *data
, void *userData
)
650 GLcontext
*ctx
= (GLcontext
*) userData
;
651 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
653 if (shProg
->Type
== GL_SHADER_PROGRAM_MESA
) {
654 _mesa_free_shader_program_data(ctx
, shProg
);
659 * Callback for deleting shader and shader programs objects.
660 * Called by _mesa_HashDeleteAll().
663 delete_shader_cb(GLuint id
, void *data
, void *userData
)
665 GLcontext
*ctx
= (GLcontext
*) userData
;
666 struct gl_shader
*sh
= (struct gl_shader
*) data
;
667 if (sh
->Type
== GL_FRAGMENT_SHADER
|| sh
->Type
== GL_VERTEX_SHADER
) {
668 _mesa_free_shader(ctx
, sh
);
671 struct gl_shader_program
*shProg
= (struct gl_shader_program
*) data
;
672 ASSERT(shProg
->Type
== GL_SHADER_PROGRAM_MESA
);
673 _mesa_free_shader_program(ctx
, shProg
);
678 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
681 delete_framebuffer_cb(GLuint id
, void *data
, void *userData
)
683 struct gl_framebuffer
*fb
= (struct gl_framebuffer
*) data
;
684 /* The fact that the framebuffer is in the hashtable means its refcount
685 * is one, but we're removing from the hashtable now. So clear refcount.
687 /*assert(fb->RefCount == 1);*/
690 /* NOTE: Delete should always be defined but there are two reports
691 * of it being NULL (bugs 13507, 14293). Work-around for now.
698 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
701 delete_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
703 struct gl_renderbuffer
*rb
= (struct gl_renderbuffer
*) data
;
704 rb
->RefCount
= 0; /* see comment for FBOs above */
711 * Deallocate a shared state object and all children structures.
713 * \param ctx GL context.
714 * \param ss shared state pointer.
716 * Frees the display lists, the texture objects (calling the driver texture
717 * deletion callback to free its private data) and the vertex programs, as well
718 * as their hash tables.
720 * \sa alloc_shared_state().
723 free_shared_state( GLcontext
*ctx
, struct gl_shared_state
*ss
)
728 _mesa_HashDeleteAll(ss
->DisplayList
, delete_displaylist_cb
, ctx
);
729 _mesa_DeleteHashTable(ss
->DisplayList
);
731 #if FEATURE_ARB_shader_objects
732 _mesa_HashWalk(ss
->ShaderObjects
, free_shader_program_data_cb
, ctx
);
733 _mesa_HashDeleteAll(ss
->ShaderObjects
, delete_shader_cb
, ctx
);
734 _mesa_DeleteHashTable(ss
->ShaderObjects
);
737 _mesa_HashDeleteAll(ss
->Programs
, delete_program_cb
, ctx
);
738 _mesa_DeleteHashTable(ss
->Programs
);
740 #if FEATURE_ARB_vertex_program
741 _mesa_reference_vertprog(ctx
, &ss
->DefaultVertexProgram
, NULL
);
743 #if FEATURE_ARB_fragment_program
744 _mesa_reference_fragprog(ctx
, &ss
->DefaultFragmentProgram
, NULL
);
747 #if FEATURE_ATI_fragment_shader
748 _mesa_HashDeleteAll(ss
->ATIShaders
, delete_fragshader_cb
, ctx
);
749 _mesa_DeleteHashTable(ss
->ATIShaders
);
750 _mesa_delete_ati_fragment_shader(ctx
, ss
->DefaultFragmentShader
);
753 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
754 _mesa_HashDeleteAll(ss
->BufferObjects
, delete_bufferobj_cb
, ctx
);
755 _mesa_DeleteHashTable(ss
->BufferObjects
);
758 _mesa_HashDeleteAll(ss
->ArrayObjects
, delete_arrayobj_cb
, ctx
);
759 _mesa_DeleteHashTable(ss
->ArrayObjects
);
761 #if FEATURE_EXT_framebuffer_object
762 _mesa_HashDeleteAll(ss
->FrameBuffers
, delete_framebuffer_cb
, ctx
);
763 _mesa_DeleteHashTable(ss
->FrameBuffers
);
764 _mesa_HashDeleteAll(ss
->RenderBuffers
, delete_renderbuffer_cb
, ctx
);
765 _mesa_DeleteHashTable(ss
->RenderBuffers
);
769 * Free texture objects (after FBOs since some textures might have
770 * been bound to FBOs).
772 ASSERT(ctx
->Driver
.DeleteTexture
);
773 /* the default textures */
774 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default1D
);
775 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default2D
);
776 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default3D
);
777 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultCubeMap
);
778 ctx
->Driver
.DeleteTexture(ctx
, ss
->DefaultRect
);
779 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default1DArray
);
780 ctx
->Driver
.DeleteTexture(ctx
, ss
->Default2DArray
);
781 /* all other textures */
782 _mesa_HashDeleteAll(ss
->TexObjects
, delete_texture_cb
, ctx
);
783 _mesa_DeleteHashTable(ss
->TexObjects
);
785 _glthread_DESTROY_MUTEX(ss
->Mutex
);
792 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
795 _mesa_init_current(GLcontext
*ctx
)
799 /* Init all to (0,0,0,1) */
800 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
801 ASSIGN_4V( ctx
->Current
.Attrib
[i
], 0.0, 0.0, 0.0, 1.0 );
804 /* redo special cases: */
805 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_WEIGHT
], 1.0, 0.0, 0.0, 0.0 );
806 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
], 0.0, 0.0, 1.0, 1.0 );
807 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], 1.0, 1.0, 1.0, 1.0 );
808 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
], 0.0, 0.0, 0.0, 1.0 );
809 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
], 1.0, 0.0, 0.0, 1.0 );
810 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
], 1.0, 0.0, 0.0, 1.0 );
815 * Init vertex/fragment program limits.
816 * Important: drivers should override these with actual limits.
819 init_program_limits(GLenum type
, struct gl_program_constants
*prog
)
821 prog
->MaxInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
822 prog
->MaxAluInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
823 prog
->MaxTexInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
824 prog
->MaxTexIndirections
= MAX_PROGRAM_INSTRUCTIONS
;
825 prog
->MaxTemps
= MAX_PROGRAM_TEMPS
;
826 prog
->MaxEnvParams
= MAX_PROGRAM_ENV_PARAMS
;
827 prog
->MaxLocalParams
= MAX_PROGRAM_LOCAL_PARAMS
;
828 prog
->MaxUniformComponents
= 4 * MAX_UNIFORMS
;
830 if (type
== GL_VERTEX_PROGRAM_ARB
) {
831 prog
->MaxParameters
= MAX_NV_VERTEX_PROGRAM_PARAMS
;
832 prog
->MaxAttribs
= MAX_NV_VERTEX_PROGRAM_INPUTS
;
833 prog
->MaxAddressRegs
= MAX_VERTEX_PROGRAM_ADDRESS_REGS
;
836 prog
->MaxParameters
= MAX_NV_FRAGMENT_PROGRAM_PARAMS
;
837 prog
->MaxAttribs
= MAX_NV_FRAGMENT_PROGRAM_INPUTS
;
838 prog
->MaxAddressRegs
= MAX_FRAGMENT_PROGRAM_ADDRESS_REGS
;
841 /* copy the above limits to init native limits */
842 prog
->MaxNativeInstructions
= prog
->MaxInstructions
;
843 prog
->MaxNativeAluInstructions
= prog
->MaxAluInstructions
;
844 prog
->MaxNativeTexInstructions
= prog
->MaxTexInstructions
;
845 prog
->MaxNativeTexIndirections
= prog
->MaxTexIndirections
;
846 prog
->MaxNativeAttribs
= prog
->MaxAttribs
;
847 prog
->MaxNativeTemps
= prog
->MaxTemps
;
848 prog
->MaxNativeAddressRegs
= prog
->MaxAddressRegs
;
849 prog
->MaxNativeParameters
= prog
->MaxParameters
;
854 * Initialize fields of gl_constants (aka ctx->Const.*).
855 * Use defaults from config.h. The device drivers will often override
856 * some of these values (such as number of texture units).
859 _mesa_init_constants(GLcontext
*ctx
)
863 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
864 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
866 /* Constants, may be overriden (usually only reduced) by device drivers */
867 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
868 ctx
->Const
.Max3DTextureLevels
= MAX_3D_TEXTURE_LEVELS
;
869 ctx
->Const
.MaxCubeTextureLevels
= MAX_CUBE_TEXTURE_LEVELS
;
870 ctx
->Const
.MaxTextureRectSize
= MAX_TEXTURE_RECT_SIZE
;
871 ctx
->Const
.MaxArrayTextureLayers
= MAX_ARRAY_TEXTURE_LAYERS
;
872 ctx
->Const
.MaxTextureCoordUnits
= MAX_TEXTURE_COORD_UNITS
;
873 ctx
->Const
.MaxTextureImageUnits
= MAX_TEXTURE_IMAGE_UNITS
;
874 ctx
->Const
.MaxTextureUnits
= MIN2(ctx
->Const
.MaxTextureCoordUnits
,
875 ctx
->Const
.MaxTextureImageUnits
);
876 ctx
->Const
.MaxTextureMaxAnisotropy
= MAX_TEXTURE_MAX_ANISOTROPY
;
877 ctx
->Const
.MaxTextureLodBias
= MAX_TEXTURE_LOD_BIAS
;
878 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
879 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
880 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
881 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
882 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
883 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
884 ctx
->Const
.PointSizeGranularity
= (GLfloat
) POINT_SIZE_GRANULARITY
;
885 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
886 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
887 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
888 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
889 ctx
->Const
.LineWidthGranularity
= (GLfloat
) LINE_WIDTH_GRANULARITY
;
890 ctx
->Const
.MaxColorTableSize
= MAX_COLOR_TABLE_SIZE
;
891 ctx
->Const
.MaxConvolutionWidth
= MAX_CONVOLUTION_WIDTH
;
892 ctx
->Const
.MaxConvolutionHeight
= MAX_CONVOLUTION_HEIGHT
;
893 ctx
->Const
.MaxClipPlanes
= MAX_CLIP_PLANES
;
894 ctx
->Const
.MaxLights
= MAX_LIGHTS
;
895 ctx
->Const
.MaxShininess
= 128.0;
896 ctx
->Const
.MaxSpotExponent
= 128.0;
897 ctx
->Const
.MaxViewportWidth
= MAX_WIDTH
;
898 ctx
->Const
.MaxViewportHeight
= MAX_HEIGHT
;
899 #if FEATURE_ARB_vertex_program
900 init_program_limits(GL_VERTEX_PROGRAM_ARB
, &ctx
->Const
.VertexProgram
);
902 #if FEATURE_ARB_fragment_program
903 init_program_limits(GL_FRAGMENT_PROGRAM_ARB
, &ctx
->Const
.FragmentProgram
);
905 ctx
->Const
.MaxProgramMatrices
= MAX_PROGRAM_MATRICES
;
906 ctx
->Const
.MaxProgramMatrixStackDepth
= MAX_PROGRAM_MATRIX_STACK_DEPTH
;
908 /* CheckArrayBounds is overriden by drivers/x11 for X server */
909 ctx
->Const
.CheckArrayBounds
= GL_FALSE
;
911 /* GL_ARB_draw_buffers */
912 ctx
->Const
.MaxDrawBuffers
= MAX_DRAW_BUFFERS
;
914 /* GL_OES_read_format */
915 ctx
->Const
.ColorReadFormat
= GL_RGBA
;
916 ctx
->Const
.ColorReadType
= GL_UNSIGNED_BYTE
;
918 #if FEATURE_EXT_framebuffer_object
919 ctx
->Const
.MaxColorAttachments
= MAX_COLOR_ATTACHMENTS
;
920 ctx
->Const
.MaxRenderbufferSize
= MAX_WIDTH
;
923 #if FEATURE_ARB_vertex_shader
924 ctx
->Const
.MaxVertexTextureImageUnits
= MAX_VERTEX_TEXTURE_IMAGE_UNITS
;
925 ctx
->Const
.MaxVarying
= MAX_VARYING
;
928 /* GL_ARB_framebuffer_object */
929 ctx
->Const
.MaxSamples
= 0;
932 ASSERT(ctx
->Const
.MaxTextureUnits
== MIN2(ctx
->Const
.MaxTextureImageUnits
,
933 ctx
->Const
.MaxTextureCoordUnits
));
934 ASSERT(ctx
->Const
.FragmentProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
935 ASSERT(ctx
->Const
.VertexProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
937 ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
938 ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
939 ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS
<= VERT_ATTRIB_MAX
);
940 ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS
<= VERT_RESULT_MAX
);
945 * Do some sanity checks on the limits/constants for the given context.
946 * Only called the first time a context is bound.
949 check_context_limits(GLcontext
*ctx
)
951 /* Many context limits/constants are limited by the size of
954 assert(ctx
->Const
.MaxTextureImageUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
955 assert(ctx
->Const
.MaxTextureCoordUnits
<= MAX_TEXTURE_COORD_UNITS
);
956 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
957 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_COORD_UNITS
);
959 /* number of coord units cannot be greater than number of image units */
960 assert(ctx
->Const
.MaxTextureCoordUnits
<= ctx
->Const
.MaxTextureImageUnits
);
962 assert(ctx
->Const
.MaxViewportWidth
<= MAX_WIDTH
);
963 assert(ctx
->Const
.MaxViewportHeight
<= MAX_WIDTH
);
965 /* make sure largest texture image is <= MAX_WIDTH in size */
966 assert((1 << (ctx
->Const
.MaxTextureLevels
-1 )) <= MAX_WIDTH
);
967 assert((1 << (ctx
->Const
.MaxCubeTextureLevels
-1 )) <= MAX_WIDTH
);
968 assert((1 << (ctx
->Const
.Max3DTextureLevels
-1 )) <= MAX_WIDTH
);
970 assert(ctx
->Const
.MaxDrawBuffers
<= MAX_DRAW_BUFFERS
);
972 /* XXX probably add more tests */
977 * Initialize the attribute groups in a GL context.
979 * \param ctx GL context.
981 * Initializes all the attributes, calling the respective <tt>init*</tt>
982 * functions for the more complex data structures.
985 init_attrib_groups(GLcontext
*ctx
)
990 _mesa_init_constants( ctx
);
993 _mesa_init_extensions( ctx
);
995 /* Attribute Groups */
997 _mesa_init_accum( ctx
);
999 #if FEATURE_attrib_stack
1000 _mesa_init_attrib( ctx
);
1002 _mesa_init_buffer_objects( ctx
);
1003 _mesa_init_color( ctx
);
1004 #if FEATURE_colortable
1005 _mesa_init_colortables( ctx
);
1007 _mesa_init_current( ctx
);
1008 _mesa_init_depth( ctx
);
1009 _mesa_init_debug( ctx
);
1011 _mesa_init_display_list( ctx
);
1013 #if FEATURE_evaluators
1014 _mesa_init_eval( ctx
);
1016 _mesa_init_fbobjects( ctx
);
1017 #if FEATURE_feedback
1018 _mesa_init_feedback( ctx
);
1020 ctx
->RenderMode
= GL_RENDER
;
1022 _mesa_init_fog( ctx
);
1023 #if FEATURE_histogram
1024 _mesa_init_histogram( ctx
);
1026 _mesa_init_hint( ctx
);
1027 _mesa_init_line( ctx
);
1028 _mesa_init_lighting( ctx
);
1029 _mesa_init_matrix( ctx
);
1030 _mesa_init_multisample( ctx
);
1031 _mesa_init_pixel( ctx
);
1032 _mesa_init_pixelstore( ctx
);
1033 _mesa_init_point( ctx
);
1034 _mesa_init_polygon( ctx
);
1035 _mesa_init_program( ctx
);
1036 #if FEATURE_ARB_occlusion_query
1037 _mesa_init_query( ctx
);
1040 _mesa_init_rastpos( ctx
);
1042 _mesa_init_scissor( ctx
);
1043 _mesa_init_shader_state( ctx
);
1044 _mesa_init_stencil( ctx
);
1045 _mesa_init_transform( ctx
);
1046 _mesa_init_varray( ctx
);
1047 _mesa_init_viewport( ctx
);
1049 if (!_mesa_init_texture( ctx
))
1052 #if FEATURE_texture_s3tc
1053 _mesa_init_texture_s3tc( ctx
);
1055 #if FEATURE_texture_fxt1
1056 _mesa_init_texture_fxt1( ctx
);
1060 ctx
->NewState
= _NEW_ALL
;
1061 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
1062 ctx
->varying_vp_inputs
= ~0;
1069 * Update default objects in a GL context with respect to shared state.
1071 * \param ctx GL context.
1073 * Removes references to old default objects, (texture objects, program
1074 * objects, etc.) and changes to reference those from the current shared
1078 update_default_objects(GLcontext
*ctx
)
1082 _mesa_update_default_objects_program(ctx
);
1083 _mesa_update_default_objects_texture(ctx
);
1084 _mesa_update_default_objects_buffer_objects(ctx
);
1091 * This is the default function we plug into all dispatch table slots
1092 * This helps prevents a segfault when someone calls a GL function without
1093 * first checking if the extension's supported.
1098 _mesa_warning(NULL
, "User called no-op dispatch function (an unsupported extension function?)");
1104 * Allocate and initialize a new dispatch table.
1106 static struct _glapi_table
*
1107 alloc_dispatch_table(void)
1109 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1110 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1111 * Mesa we do this to accomodate different versions of libGL and various
1114 GLint numEntries
= MAX2(_glapi_get_dispatch_table_size(),
1115 sizeof(struct _glapi_table
) / sizeof(_glapi_proc
));
1116 struct _glapi_table
*table
=
1117 (struct _glapi_table
*) _mesa_malloc(numEntries
* sizeof(_glapi_proc
));
1119 _glapi_proc
*entry
= (_glapi_proc
*) table
;
1121 for (i
= 0; i
< numEntries
; i
++) {
1122 entry
[i
] = (_glapi_proc
) generic_nop
;
1130 * Initialize a GLcontext struct (rendering context).
1132 * This includes allocating all the other structs and arrays which hang off of
1133 * the context by pointers.
1134 * Note that the driver needs to pass in its dd_function_table here since
1135 * we need to at least call driverFunctions->NewTextureObject to create the
1136 * default texture objects.
1138 * Called by _mesa_create_context().
1140 * Performs the imports and exports callback tables initialization, and
1141 * miscellaneous one-time initializations. If no shared context is supplied one
1142 * is allocated, and increase its reference count. Setups the GL API dispatch
1143 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1144 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1147 * \param ctx the context to initialize
1148 * \param visual describes the visual attributes for this context
1149 * \param share_list points to context to share textures, display lists,
1151 * \param driverFunctions table of device driver functions for this context
1153 * \param driverContext pointer to driver-specific context data
1156 _mesa_initialize_context(GLcontext
*ctx
,
1157 const GLvisual
*visual
,
1158 GLcontext
*share_list
,
1159 const struct dd_function_table
*driverFunctions
,
1160 void *driverContext
)
1162 /*ASSERT(driverContext);*/
1163 assert(driverFunctions
->NewTextureObject
);
1164 assert(driverFunctions
->FreeTexImageData
);
1166 /* misc one-time initializations */
1169 ctx
->Visual
= *visual
;
1170 ctx
->DrawBuffer
= NULL
;
1171 ctx
->ReadBuffer
= NULL
;
1172 ctx
->WinSysDrawBuffer
= NULL
;
1173 ctx
->WinSysReadBuffer
= NULL
;
1175 /* Plug in driver functions and context pointer here.
1176 * This is important because when we call alloc_shared_state() below
1177 * we'll call ctx->Driver.NewTextureObject() to create the default
1180 ctx
->Driver
= *driverFunctions
;
1181 ctx
->DriverCtx
= driverContext
;
1184 /* share state with another context */
1185 ctx
->Shared
= share_list
->Shared
;
1188 /* allocate new, unshared state */
1189 if (!alloc_shared_state( ctx
)) {
1193 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1194 ctx
->Shared
->RefCount
++;
1195 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1197 if (!init_attrib_groups( ctx
)) {
1198 free_shared_state(ctx
, ctx
->Shared
);
1202 /* setup the API dispatch tables */
1203 ctx
->Exec
= alloc_dispatch_table();
1204 ctx
->Save
= alloc_dispatch_table();
1205 if (!ctx
->Exec
|| !ctx
->Save
) {
1206 free_shared_state(ctx
, ctx
->Shared
);
1208 _mesa_free(ctx
->Exec
);
1210 #if FEATURE_dispatch
1211 _mesa_init_exec_table(ctx
->Exec
);
1213 ctx
->CurrentDispatch
= ctx
->Exec
;
1215 _mesa_init_dlist_table(ctx
->Save
);
1216 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
1218 /* Neutral tnl module stuff */
1219 _mesa_init_exec_vtxfmt( ctx
);
1220 ctx
->TnlModule
.Current
= NULL
;
1221 ctx
->TnlModule
.SwapCount
= 0;
1223 ctx
->FragmentProgram
._MaintainTexEnvProgram
1224 = (_mesa_getenv("MESA_TEX_PROG") != NULL
);
1226 ctx
->VertexProgram
._MaintainTnlProgram
1227 = (_mesa_getenv("MESA_TNL_PROG") != NULL
);
1228 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
1229 /* this is required... */
1230 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
1233 #ifdef FEATURE_extra_context_init
1234 _mesa_initialize_context_extra(ctx
);
1237 ctx
->FirstTimeCurrent
= GL_TRUE
;
1244 * Allocate and initialize a GLcontext structure.
1245 * Note that the driver needs to pass in its dd_function_table here since
1246 * we need to at least call driverFunctions->NewTextureObject to initialize
1247 * the rendering context.
1249 * \param visual a GLvisual pointer (we copy the struct contents)
1250 * \param share_list another context to share display lists with or NULL
1251 * \param driverFunctions points to the dd_function_table into which the
1252 * driver has plugged in all its special functions.
1253 * \param driverCtx points to the device driver's private context state
1255 * \return pointer to a new __GLcontextRec or NULL if error.
1258 _mesa_create_context(const GLvisual
*visual
,
1259 GLcontext
*share_list
,
1260 const struct dd_function_table
*driverFunctions
,
1261 void *driverContext
)
1266 /*ASSERT(driverContext);*/
1268 ctx
= (GLcontext
*) _mesa_calloc(sizeof(GLcontext
));
1272 if (_mesa_initialize_context(ctx
, visual
, share_list
,
1273 driverFunctions
, driverContext
)) {
1284 * Free the data associated with the given context.
1286 * But doesn't free the GLcontext struct itself.
1288 * \sa _mesa_initialize_context() and init_attrib_groups().
1291 _mesa_free_context_data( GLcontext
*ctx
)
1293 if (!_mesa_get_current_context()){
1294 /* No current context, but we may need one in order to delete
1295 * texture objs, etc. So temporarily bind the context now.
1297 _mesa_make_current(ctx
, NULL
, NULL
);
1300 /* unreference WinSysDraw/Read buffers */
1301 _mesa_reference_framebuffer(&ctx
->WinSysDrawBuffer
, NULL
);
1302 _mesa_reference_framebuffer(&ctx
->WinSysReadBuffer
, NULL
);
1303 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, NULL
);
1304 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, NULL
);
1306 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
1307 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
1308 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._TnlProgram
, NULL
);
1310 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
1311 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
1312 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
, NULL
);
1314 #if FEATURE_attrib_stack
1315 _mesa_free_attrib_data(ctx
);
1317 _mesa_free_lighting_data( ctx
);
1318 #if FEATURE_evaluators
1319 _mesa_free_eval_data( ctx
);
1321 _mesa_free_texture_data( ctx
);
1322 _mesa_free_matrix_data( ctx
);
1323 _mesa_free_viewport_data( ctx
);
1324 #if FEATURE_colortable
1325 _mesa_free_colortables_data( ctx
);
1327 _mesa_free_program_data(ctx
);
1328 _mesa_free_shader_state(ctx
);
1329 #if FEATURE_ARB_occlusion_query
1330 _mesa_free_query_data(ctx
);
1333 #if FEATURE_ARB_vertex_buffer_object
1334 _mesa_delete_buffer_object(ctx
, ctx
->Array
.NullBufferObj
);
1336 _mesa_delete_array_object(ctx
, ctx
->Array
.DefaultArrayObj
);
1338 /* free dispatch tables */
1339 _mesa_free(ctx
->Exec
);
1340 _mesa_free(ctx
->Save
);
1342 /* Shared context state (display lists, textures, etc) */
1343 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1344 ctx
->Shared
->RefCount
--;
1345 assert(ctx
->Shared
->RefCount
>= 0);
1346 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1347 if (ctx
->Shared
->RefCount
== 0) {
1348 /* free shared state */
1349 free_shared_state( ctx
, ctx
->Shared
);
1352 if (ctx
->Extensions
.String
)
1353 _mesa_free((void *) ctx
->Extensions
.String
);
1355 /* unbind the context if it's currently bound */
1356 if (ctx
== _mesa_get_current_context()) {
1357 _mesa_make_current(NULL
, NULL
, NULL
);
1363 * Destroy a GLcontext structure.
1365 * \param ctx GL context.
1367 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1370 _mesa_destroy_context( GLcontext
*ctx
)
1373 _mesa_free_context_data(ctx
);
1374 _mesa_free( (void *) ctx
);
1381 * Copy attribute groups from one context to another.
1383 * \param src source context
1384 * \param dst destination context
1385 * \param mask bitwise OR of GL_*_BIT flags
1387 * According to the bits specified in \p mask, copies the corresponding
1388 * attributes from \p src into \p dst. For many of the attributes a simple \c
1389 * memcpy is not enough due to the existence of internal pointers in their data
1393 _mesa_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1395 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1397 dst
->Accum
= src
->Accum
;
1399 if (mask
& GL_COLOR_BUFFER_BIT
) {
1401 dst
->Color
= src
->Color
;
1403 if (mask
& GL_CURRENT_BIT
) {
1405 dst
->Current
= src
->Current
;
1407 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1409 dst
->Depth
= src
->Depth
;
1411 if (mask
& GL_ENABLE_BIT
) {
1414 if (mask
& GL_EVAL_BIT
) {
1416 dst
->Eval
= src
->Eval
;
1418 if (mask
& GL_FOG_BIT
) {
1420 dst
->Fog
= src
->Fog
;
1422 if (mask
& GL_HINT_BIT
) {
1424 dst
->Hint
= src
->Hint
;
1426 if (mask
& GL_LIGHTING_BIT
) {
1428 /* begin with memcpy */
1429 dst
->Light
= src
->Light
;
1430 /* fixup linked lists to prevent pointer insanity */
1431 make_empty_list( &(dst
->Light
.EnabledList
) );
1432 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1433 if (dst
->Light
.Light
[i
].Enabled
) {
1434 insert_at_tail(&(dst
->Light
.EnabledList
), &(dst
->Light
.Light
[i
]));
1438 if (mask
& GL_LINE_BIT
) {
1440 dst
->Line
= src
->Line
;
1442 if (mask
& GL_LIST_BIT
) {
1444 dst
->List
= src
->List
;
1446 if (mask
& GL_PIXEL_MODE_BIT
) {
1448 dst
->Pixel
= src
->Pixel
;
1450 if (mask
& GL_POINT_BIT
) {
1452 dst
->Point
= src
->Point
;
1454 if (mask
& GL_POLYGON_BIT
) {
1456 dst
->Polygon
= src
->Polygon
;
1458 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1459 /* Use loop instead of MEMCPY due to problem with Portland Group's
1460 * C compiler. Reported by John Stone.
1463 for (i
= 0; i
< 32; i
++) {
1464 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1467 if (mask
& GL_SCISSOR_BIT
) {
1469 dst
->Scissor
= src
->Scissor
;
1471 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1473 dst
->Stencil
= src
->Stencil
;
1475 if (mask
& GL_TEXTURE_BIT
) {
1476 /* Cannot memcpy because of pointers */
1477 _mesa_copy_texture_state(src
, dst
);
1479 if (mask
& GL_TRANSFORM_BIT
) {
1481 dst
->Transform
= src
->Transform
;
1483 if (mask
& GL_VIEWPORT_BIT
) {
1484 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1485 dst
->Viewport
.X
= src
->Viewport
.X
;
1486 dst
->Viewport
.Y
= src
->Viewport
.Y
;
1487 dst
->Viewport
.Width
= src
->Viewport
.Width
;
1488 dst
->Viewport
.Height
= src
->Viewport
.Height
;
1489 dst
->Viewport
.Near
= src
->Viewport
.Near
;
1490 dst
->Viewport
.Far
= src
->Viewport
.Far
;
1491 _math_matrix_copy(&dst
->Viewport
._WindowMap
, &src
->Viewport
._WindowMap
);
1494 /* XXX FIXME: Call callbacks?
1496 dst
->NewState
= _NEW_ALL
;
1502 * Check if the given context can render into the given framebuffer
1503 * by checking visual attributes.
1505 * Most of these tests could go away because Mesa is now pretty flexible
1506 * in terms of mixing rendering contexts with framebuffers. As long
1507 * as RGB vs. CI mode agree, we're probably good.
1509 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1512 check_compatible(const GLcontext
*ctx
, const GLframebuffer
*buffer
)
1514 const GLvisual
*ctxvis
= &ctx
->Visual
;
1515 const GLvisual
*bufvis
= &buffer
->Visual
;
1517 if (ctxvis
== bufvis
)
1520 if (ctxvis
->rgbMode
!= bufvis
->rgbMode
)
1523 /* disabling this fixes the fgl_glxgears pbuffer demo */
1524 if (ctxvis
->doubleBufferMode
&& !bufvis
->doubleBufferMode
)
1527 if (ctxvis
->stereoMode
&& !bufvis
->stereoMode
)
1529 if (ctxvis
->haveAccumBuffer
&& !bufvis
->haveAccumBuffer
)
1531 if (ctxvis
->haveDepthBuffer
&& !bufvis
->haveDepthBuffer
)
1533 if (ctxvis
->haveStencilBuffer
&& !bufvis
->haveStencilBuffer
)
1535 if (ctxvis
->redMask
&& ctxvis
->redMask
!= bufvis
->redMask
)
1537 if (ctxvis
->greenMask
&& ctxvis
->greenMask
!= bufvis
->greenMask
)
1539 if (ctxvis
->blueMask
&& ctxvis
->blueMask
!= bufvis
->blueMask
)
1542 /* disabled (see bug 11161) */
1543 if (ctxvis
->depthBits
&& ctxvis
->depthBits
!= bufvis
->depthBits
)
1546 if (ctxvis
->stencilBits
&& ctxvis
->stencilBits
!= bufvis
->stencilBits
)
1554 * Do one-time initialization for the given framebuffer. Specifically,
1555 * ask the driver for the window's current size and update the framebuffer
1557 * Really, the device driver should totally take care of this.
1560 initialize_framebuffer_size(GLcontext
*ctx
, GLframebuffer
*fb
)
1562 GLuint width
, height
;
1563 if (ctx
->Driver
.GetBufferSize
) {
1564 ctx
->Driver
.GetBufferSize(fb
, &width
, &height
);
1565 if (ctx
->Driver
.ResizeBuffers
)
1566 ctx
->Driver
.ResizeBuffers(ctx
, fb
, width
, height
);
1567 fb
->Initialized
= GL_TRUE
;
1573 * Bind the given context to the given drawBuffer and readBuffer and
1574 * make it the current context for the calling thread.
1575 * We'll render into the drawBuffer and read pixels from the
1576 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1578 * We check that the context's and framebuffer's visuals are compatible
1579 * and return immediately if they're not.
1581 * \param newCtx the new GL context. If NULL then there will be no current GL
1583 * \param drawBuffer the drawing framebuffer
1584 * \param readBuffer the reading framebuffer
1587 _mesa_make_current( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1588 GLframebuffer
*readBuffer
)
1590 if (MESA_VERBOSE
& VERBOSE_API
)
1591 _mesa_debug(newCtx
, "_mesa_make_current()\n");
1593 /* Check that the context's and framebuffer's visuals are compatible.
1595 if (newCtx
&& drawBuffer
&& newCtx
->WinSysDrawBuffer
!= drawBuffer
) {
1596 if (!check_compatible(newCtx
, drawBuffer
)) {
1597 _mesa_warning(newCtx
,
1598 "MakeCurrent: incompatible visuals for context and drawbuffer");
1602 if (newCtx
&& readBuffer
&& newCtx
->WinSysReadBuffer
!= readBuffer
) {
1603 if (!check_compatible(newCtx
, readBuffer
)) {
1604 _mesa_warning(newCtx
,
1605 "MakeCurrent: incompatible visuals for context and readbuffer");
1610 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1611 _glapi_set_context((void *) newCtx
);
1612 ASSERT(_mesa_get_current_context() == newCtx
);
1615 _glapi_set_dispatch(NULL
); /* none current */
1618 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1620 if (drawBuffer
&& readBuffer
) {
1621 /* TODO: check if newCtx and buffer's visual match??? */
1623 ASSERT(drawBuffer
->Name
== 0);
1624 ASSERT(readBuffer
->Name
== 0);
1625 _mesa_reference_framebuffer(&newCtx
->WinSysDrawBuffer
, drawBuffer
);
1626 _mesa_reference_framebuffer(&newCtx
->WinSysReadBuffer
, readBuffer
);
1629 * Only set the context's Draw/ReadBuffer fields if they're NULL
1630 * or not bound to a user-created FBO.
1632 if (!newCtx
->DrawBuffer
|| newCtx
->DrawBuffer
->Name
== 0) {
1633 /* KW: merge conflict here, revisit.
1635 /* fix up the fb fields - these will end up wrong otherwise
1636 * if the DRIdrawable changes, and everything relies on them.
1637 * This is a bit messy (same as needed in _mesa_BindFramebufferEXT)
1640 GLenum buffers
[MAX_DRAW_BUFFERS
];
1642 _mesa_reference_framebuffer(&newCtx
->DrawBuffer
, drawBuffer
);
1644 for(i
= 0; i
< newCtx
->Const
.MaxDrawBuffers
; i
++) {
1645 buffers
[i
] = newCtx
->Color
.DrawBuffer
[i
];
1648 _mesa_drawbuffers(newCtx
, newCtx
->Const
.MaxDrawBuffers
, buffers
, NULL
);
1650 if (!newCtx
->ReadBuffer
|| newCtx
->ReadBuffer
->Name
== 0) {
1651 _mesa_reference_framebuffer(&newCtx
->ReadBuffer
, readBuffer
);
1654 /* XXX only set this flag if we're really changing the draw/read
1655 * framebuffer bindings.
1657 newCtx
->NewState
|= _NEW_BUFFERS
;
1660 /* We want to get rid of these lines: */
1663 if (!drawBuffer
->Initialized
) {
1664 initialize_framebuffer_size(newCtx
, drawBuffer
);
1666 if (readBuffer
!= drawBuffer
&& !readBuffer
->Initialized
) {
1667 initialize_framebuffer_size(newCtx
, readBuffer
);
1670 _mesa_resizebuffers(newCtx
);
1674 /* We want the drawBuffer and readBuffer to be initialized by
1676 * This generally means the Width and Height match the actual
1677 * window size and the renderbuffers (both hardware and software
1678 * based) are allocated to match. The later can generally be
1679 * done with a call to _mesa_resize_framebuffer().
1681 * It's theoretically possible for a buffer to have zero width
1682 * or height, but for now, assert check that the driver did what's
1685 ASSERT(drawBuffer
->Width
> 0);
1686 ASSERT(drawBuffer
->Height
> 0);
1689 if (newCtx
->FirstTimeCurrent
) {
1690 /* set initial viewport and scissor size now */
1691 _mesa_set_viewport(newCtx
, 0, 0,
1692 drawBuffer
->Width
, drawBuffer
->Height
);
1693 _mesa_set_scissor(newCtx
, 0, 0,
1694 drawBuffer
->Width
, drawBuffer
->Height
);
1695 check_context_limits(newCtx
);
1699 /* We can use this to help debug user's problems. Tell them to set
1700 * the MESA_INFO env variable before running their app. Then the
1701 * first time each context is made current we'll print some useful
1704 if (newCtx
->FirstTimeCurrent
) {
1705 if (_mesa_getenv("MESA_INFO")) {
1708 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1715 * Make context 'ctx' share the display lists, textures and programs
1716 * that are associated with 'ctxToShare'.
1717 * Any display lists, textures or programs associated with 'ctx' will
1718 * be deleted if nobody else is sharing them.
1721 _mesa_share_state(GLcontext
*ctx
, GLcontext
*ctxToShare
)
1723 if (ctx
&& ctxToShare
&& ctx
->Shared
&& ctxToShare
->Shared
) {
1724 struct gl_shared_state
*oldSharedState
= ctx
->Shared
;
1726 ctx
->Shared
= ctxToShare
->Shared
;
1727 ctx
->Shared
->RefCount
++;
1729 update_default_objects(ctx
);
1731 oldSharedState
->RefCount
--;
1732 if (oldSharedState
->RefCount
== 0) {
1733 free_shared_state(ctx
, oldSharedState
);
1746 * \return pointer to the current GL context for this thread.
1748 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1749 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1753 _mesa_get_current_context( void )
1755 return (GLcontext
*) _glapi_get_context();
1760 * Get context's current API dispatch table.
1762 * It'll either be the immediate-mode execute dispatcher or the display list
1763 * compile dispatcher.
1765 * \param ctx GL context.
1767 * \return pointer to dispatch_table.
1769 * Simply returns __GLcontextRec::CurrentDispatch.
1771 struct _glapi_table
*
1772 _mesa_get_dispatch(GLcontext
*ctx
)
1774 return ctx
->CurrentDispatch
;
1780 /**********************************************************************/
1781 /** \name Miscellaneous functions */
1782 /**********************************************************************/
1788 * \param ctx GL context.
1789 * \param error error code.
1791 * Records the given error code and call the driver's dd_function_table::Error
1792 * function if defined.
1795 * This is called via _mesa_error().
1798 _mesa_record_error(GLcontext
*ctx
, GLenum error
)
1803 if (ctx
->ErrorValue
== GL_NO_ERROR
) {
1804 ctx
->ErrorValue
= error
;
1807 /* Call device driver's error handler, if any. This is used on the Mac. */
1808 if (ctx
->Driver
.Error
) {
1809 ctx
->Driver
.Error(ctx
);
1815 * Execute glFinish().
1817 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1818 * dd_function_table::Finish driver callback, if not NULL.
1823 GET_CURRENT_CONTEXT(ctx
);
1824 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1825 if (ctx
->Driver
.Finish
) {
1826 ctx
->Driver
.Finish(ctx
);
1832 * Execute glFlush().
1834 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1835 * dd_function_table::Flush driver callback, if not NULL.
1840 GET_CURRENT_CONTEXT(ctx
);
1841 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1842 if (ctx
->Driver
.Flush
) {
1843 ctx
->Driver
.Flush(ctx
);