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.
81 #include "sparc/sparc.h"
89 #ifndef MESA_DEBUG_FLAGS
90 int MESA_DEBUG_FLAGS
= 0;
94 /* ubyte -> float conversion */
95 GLfloat _mesa_ubyte_to_float_color_tab
[256];
100 * Swap buffers notification callback.
102 * \param ctx GL context.
104 * Called by window system just before swapping buffers.
105 * We have to finish any pending rendering.
108 _mesa_notifySwapBuffers(struct gl_context
*ctx
)
110 if (MESA_VERBOSE
& VERBOSE_SWAPBUFFERS
)
111 _mesa_debug(ctx
, "SwapBuffers\n");
112 FLUSH_CURRENT( ctx
, 0 );
113 if (ctx
->Driver
.Flush
) {
114 ctx
->Driver
.Flush(ctx
);
119 /**********************************************************************/
120 /** \name GL Visual allocation/destruction */
121 /**********************************************************************/
125 * Allocates a struct gl_config structure and initializes it via
126 * _mesa_initialize_visual().
128 * \param dbFlag double buffering
129 * \param stereoFlag stereo buffer
130 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
131 * is acceptable but the actual depth type will be GLushort or GLuint as
133 * \param stencilBits requested minimum bits per stencil buffer value
134 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number
135 * of bits per color component in accum buffer.
136 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
137 * \param redBits number of bits per color component in frame buffer for RGB(A)
138 * mode. We always use 8 in core Mesa though.
139 * \param greenBits same as above.
140 * \param blueBits same as above.
141 * \param alphaBits same as above.
143 * \return pointer to new struct gl_config or NULL if requested parameters
146 * \note Need to add params for level and numAuxBuffers (at least)
149 _mesa_create_visual( GLboolean dbFlag
,
150 GLboolean stereoFlag
,
158 GLint accumGreenBits
,
160 GLint accumAlphaBits
)
162 struct gl_config
*vis
= CALLOC_STRUCT(gl_config
);
164 if (!_mesa_initialize_visual(vis
, dbFlag
, stereoFlag
,
165 redBits
, greenBits
, blueBits
, alphaBits
,
166 depthBits
, stencilBits
,
167 accumRedBits
, accumGreenBits
,
168 accumBlueBits
, accumAlphaBits
)) {
178 * Makes some sanity checks and fills in the fields of the struct
179 * gl_config object with the given parameters. If the caller needs to
180 * set additional fields, he should just probably init the whole
181 * gl_config object himself.
183 * \return GL_TRUE on success, or GL_FALSE on failure.
185 * \sa _mesa_create_visual() above for the parameter description.
188 _mesa_initialize_visual( struct gl_config
*vis
,
190 GLboolean stereoFlag
,
198 GLint accumGreenBits
,
200 GLint accumAlphaBits
)
204 if (depthBits
< 0 || depthBits
> 32) {
207 if (stencilBits
< 0 || stencilBits
> STENCIL_BITS
) {
210 assert(accumRedBits
>= 0);
211 assert(accumGreenBits
>= 0);
212 assert(accumBlueBits
>= 0);
213 assert(accumAlphaBits
>= 0);
215 vis
->rgbMode
= GL_TRUE
;
216 vis
->doubleBufferMode
= dbFlag
;
217 vis
->stereoMode
= stereoFlag
;
219 vis
->redBits
= redBits
;
220 vis
->greenBits
= greenBits
;
221 vis
->blueBits
= blueBits
;
222 vis
->alphaBits
= alphaBits
;
223 vis
->rgbBits
= redBits
+ greenBits
+ blueBits
;
226 vis
->depthBits
= depthBits
;
227 vis
->stencilBits
= stencilBits
;
229 vis
->accumRedBits
= accumRedBits
;
230 vis
->accumGreenBits
= accumGreenBits
;
231 vis
->accumBlueBits
= accumBlueBits
;
232 vis
->accumAlphaBits
= accumAlphaBits
;
234 vis
->haveAccumBuffer
= accumRedBits
> 0;
235 vis
->haveDepthBuffer
= depthBits
> 0;
236 vis
->haveStencilBuffer
= stencilBits
> 0;
238 vis
->numAuxBuffers
= 0;
246 * Destroy a visual and free its memory.
250 * Frees the visual structure.
253 _mesa_destroy_visual( struct gl_config
*vis
)
261 /**********************************************************************/
262 /** \name Context allocation, initialization, destroying
264 * The purpose of the most initialization functions here is to provide the
265 * default state values according to the OpenGL specification.
267 /**********************************************************************/
272 * This is lame. gdb only seems to recognize enum types that are
273 * actually used somewhere. We want to be able to print/use enum
274 * values such as TEXTURE_2D_INDEX in gdb. But we don't actually use
275 * the gl_texture_index type anywhere. Thus, this lame function.
278 dummy_enum_func(void)
280 gl_buffer_index bi
= BUFFER_FRONT_LEFT
;
281 gl_face_index fi
= FACE_POS_X
;
282 gl_frag_attrib fa
= FRAG_ATTRIB_WPOS
;
283 gl_vert_attrib va
= VERT_ATTRIB_POS
;
293 * One-time initialization mutex lock.
295 * \sa Used by one_time_init().
297 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock
);
302 * Calls all the various one-time-init functions in Mesa.
304 * While holding a global mutex lock, calls several initialization functions,
305 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
311 one_time_init( struct gl_context
*ctx
)
313 static GLboolean api_init
= GL_FALSE
;
315 _glthread_LOCK_MUTEX(OneTimeLock
);
317 /* truly one-time init */
321 /* do some implementation tests */
322 assert( sizeof(GLbyte
) == 1 );
323 assert( sizeof(GLubyte
) == 1 );
324 assert( sizeof(GLshort
) == 2 );
325 assert( sizeof(GLushort
) == 2 );
326 assert( sizeof(GLint
) == 4 );
327 assert( sizeof(GLuint
) == 4 );
329 _mesa_get_cpu_features();
331 _mesa_init_sqrt_table();
333 /* context dependence is never a one-time thing... */
334 _mesa_init_get_hash(ctx
);
336 for (i
= 0; i
< 256; i
++) {
337 _mesa_ubyte_to_float_color_tab
[i
] = (float) i
/ 255.0F
;
340 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
341 if (MESA_VERBOSE
!= 0) {
342 _mesa_debug(ctx
, "Mesa %s DEBUG build %s %s\n",
343 MESA_VERSION_STRING
, __DATE__
, __TIME__
);
348 _mesa_test_formats();
354 _glthread_UNLOCK_MUTEX(OneTimeLock
);
361 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
364 _mesa_init_current(struct gl_context
*ctx
)
368 /* Init all to (0,0,0,1) */
369 for (i
= 0; i
< Elements(ctx
->Current
.Attrib
); i
++) {
370 ASSIGN_4V( ctx
->Current
.Attrib
[i
], 0.0, 0.0, 0.0, 1.0 );
373 /* redo special cases: */
374 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_WEIGHT
], 1.0, 0.0, 0.0, 0.0 );
375 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
], 0.0, 0.0, 1.0, 1.0 );
376 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], 1.0, 1.0, 1.0, 1.0 );
377 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
], 0.0, 0.0, 0.0, 1.0 );
378 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
], 1.0, 0.0, 0.0, 1.0 );
379 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
], 1.0, 0.0, 0.0, 1.0 );
383 * Initialize fields of gl_constants (aka ctx->Const.*).
384 * Use defaults from config.h. The device drivers will often override
385 * some of these values (such as number of texture units).
388 _mesa_init_constants(struct gl_context
*ctx
)
392 /* Constants, may be overriden (usually only reduced) by device drivers */
393 ctx
->Const
.MaxTextureMbytes
= MAX_TEXTURE_MBYTES
;
394 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
395 ctx
->Const
.MaxCubeTextureLevels
= MAX_CUBE_TEXTURE_LEVELS
;
396 ctx
->Const
.MaxTextureMaxAnisotropy
= MAX_TEXTURE_MAX_ANISOTROPY
;
397 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
398 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
399 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
400 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
401 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
402 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
403 ctx
->Const
.PointSizeGranularity
= (GLfloat
) POINT_SIZE_GRANULARITY
;
404 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
405 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
406 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
407 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
408 ctx
->Const
.LineWidthGranularity
= (GLfloat
) LINE_WIDTH_GRANULARITY
;
409 ctx
->Const
.MaxColorTableSize
= MAX_COLOR_TABLE_SIZE
;
410 ctx
->Const
.MaxClipPlanes
= 6;
411 ctx
->Const
.MaxLights
= MAX_LIGHTS
;
412 ctx
->Const
.MaxShininess
= 128.0;
413 ctx
->Const
.MaxSpotExponent
= 128.0;
414 ctx
->Const
.MaxViewportWidth
= MAX_WIDTH
;
415 ctx
->Const
.MaxViewportHeight
= MAX_HEIGHT
;
417 /* CheckArrayBounds is overriden by drivers/x11 for X server */
418 ctx
->Const
.CheckArrayBounds
= GL_FALSE
;
420 /* GL 3.2: hard-coded for now: */
421 ctx
->Const
.ProfileMask
= GL_CONTEXT_COMPATIBILITY_PROFILE_BIT
;
426 * Do some sanity checks on the limits/constants for the given context.
427 * Only called the first time a context is bound.
430 check_context_limits(struct gl_context
*ctx
)
432 /* Texture size checks */
433 assert(ctx
->Const
.MaxTextureLevels
<= MAX_TEXTURE_LEVELS
);
434 assert(ctx
->Const
.Max3DTextureLevels
<= MAX_3D_TEXTURE_LEVELS
);
435 assert(ctx
->Const
.MaxCubeTextureLevels
<= MAX_CUBE_TEXTURE_LEVELS
);
437 /* make sure largest texture image is <= MAX_WIDTH in size */
438 assert((1 << (ctx
->Const
.MaxTextureLevels
- 1)) <= MAX_WIDTH
);
439 assert((1 << (ctx
->Const
.MaxCubeTextureLevels
- 1)) <= MAX_WIDTH
);
440 assert((1 << (ctx
->Const
.Max3DTextureLevels
- 1)) <= MAX_WIDTH
);
442 /* Texture level checks */
443 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
444 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
446 /* Max texture size should be <= max viewport size (render to texture) */
447 assert((1 << (MAX_TEXTURE_LEVELS
- 1)) <= MAX_WIDTH
);
449 assert(ctx
->Const
.MaxViewportWidth
<= MAX_WIDTH
);
450 assert(ctx
->Const
.MaxViewportHeight
<= MAX_WIDTH
);
455 * Initialize the attribute groups in a GL context.
457 * \param ctx GL context.
459 * Initializes all the attributes, calling the respective <tt>init*</tt>
460 * functions for the more complex data structures.
463 init_attrib_groups(struct gl_context
*ctx
)
468 _mesa_init_constants( ctx
);
471 _mesa_init_extensions( ctx
);
473 /* Attribute Groups */
474 _mesa_init_accum( ctx
);
475 _mesa_init_attrib( ctx
);
476 _mesa_init_buffer_objects( ctx
);
477 _mesa_init_color( ctx
);
478 _mesa_init_current( ctx
);
479 _mesa_init_depth( ctx
);
480 _mesa_init_display_list( ctx
);
481 _mesa_init_eval( ctx
);
482 _mesa_init_feedback( ctx
);
483 _mesa_init_fog( ctx
);
484 _mesa_init_hint( ctx
);
485 _mesa_init_line( ctx
);
486 _mesa_init_lighting( ctx
);
487 _mesa_init_matrix( ctx
);
488 _mesa_init_multisample( ctx
);
489 _mesa_init_pixel( ctx
);
490 _mesa_init_pixelstore( ctx
);
491 _mesa_init_point( ctx
);
492 _mesa_init_polygon( ctx
);
493 _mesa_init_rastpos( ctx
);
494 _mesa_init_scissor( ctx
);
495 _mesa_init_stencil( ctx
);
496 _mesa_init_transform( ctx
);
497 _mesa_init_varray( ctx
);
498 _mesa_init_viewport( ctx
);
500 if (!_mesa_init_texture( ctx
))
504 ctx
->NewState
= _NEW_ALL
;
505 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
512 * Update default objects in a GL context with respect to shared state.
514 * \param ctx GL context.
516 * Removes references to old default objects, (texture objects, program
517 * objects, etc.) and changes to reference those from the current shared
521 update_default_objects(struct gl_context
*ctx
)
525 _mesa_update_default_objects_texture(ctx
);
526 _mesa_update_default_objects_buffer_objects(ctx
);
533 * This is the default function we plug into all dispatch table slots
534 * This helps prevents a segfault when someone calls a GL function without
535 * first checking if the extension's supported.
540 _mesa_warning(NULL
, "User called no-op dispatch function (an unsupported extension function?)");
546 * Allocate and initialize a new dispatch table.
548 struct _glapi_table
*
549 _mesa_alloc_dispatch_table(int size
)
551 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
552 * In practice, this'll be the same for stand-alone Mesa. But for DRI
553 * Mesa we do this to accomodate different versions of libGL and various
556 GLint numEntries
= MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT
);
557 struct _glapi_table
*table
;
559 /* should never happen, but just in case */
560 numEntries
= MAX2(numEntries
, size
);
562 table
= (struct _glapi_table
*) malloc(numEntries
* sizeof(_glapi_proc
));
564 _glapi_proc
*entry
= (_glapi_proc
*) table
;
566 for (i
= 0; i
< numEntries
; i
++) {
567 entry
[i
] = (_glapi_proc
) generic_nop
;
575 * Initialize a struct gl_context struct (rendering context).
577 * This includes allocating all the other structs and arrays which hang off of
578 * the context by pointers.
579 * Note that the driver needs to pass in its dd_function_table here since
580 * we need to at least call driverFunctions->NewTextureObject to create the
581 * default texture objects.
583 * Called by _mesa_create_context().
585 * Performs the imports and exports callback tables initialization, and
586 * miscellaneous one-time initializations. If no shared context is supplied one
587 * is allocated, and increase its reference count. Setups the GL API dispatch
588 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
589 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
592 * \param ctx the context to initialize
593 * \param api the GL API type to create the context for
594 * \param visual describes the visual attributes for this context
595 * \param share_list points to context to share textures, display lists,
597 * \param driverFunctions table of device driver functions for this context
599 * \param driverContext pointer to driver-specific context data
602 _mesa_initialize_context(struct gl_context
*ctx
,
603 const struct gl_config
*visual
,
604 struct gl_context
*share_list
,
605 const struct dd_function_table
*driverFunctions
,
608 struct gl_shared_state
*shared
;
610 /*ASSERT(driverContext);*/
611 assert(driverFunctions
->NewTextureObject
);
612 assert(driverFunctions
->FreeTextureImageBuffer
);
614 ctx
->Visual
= *visual
;
615 ctx
->DrawBuffer
= NULL
;
616 ctx
->ReadBuffer
= NULL
;
617 ctx
->WinSysDrawBuffer
= NULL
;
618 ctx
->WinSysReadBuffer
= NULL
;
620 /* misc one-time initializations */
623 /* Plug in driver functions and context pointer here.
624 * This is important because when we call alloc_shared_state() below
625 * we'll call ctx->Driver.NewTextureObject() to create the default
628 ctx
->Driver
= *driverFunctions
;
629 ctx
->DriverCtx
= driverContext
;
632 /* share state with another context */
633 shared
= share_list
->Shared
;
636 /* allocate new, unshared state */
637 shared
= _mesa_alloc_shared_state(ctx
);
642 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, shared
);
644 if (!init_attrib_groups( ctx
)) {
645 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
649 ctx
->Exec
= _mesa_create_exec_table();
652 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
655 ctx
->CurrentDispatch
= ctx
->Exec
;
657 /* Mesa core handles all the formats that mesa core knows about.
658 * Drivers will want to override this list with just the formats
659 * they can handle, and confirm that appropriate fallbacks exist in
660 * _mesa_choose_tex_format().
662 memset(&ctx
->TextureFormatSupported
, GL_TRUE
,
663 sizeof(ctx
->TextureFormatSupported
));
665 ctx
->Save
= _mesa_create_save_table();
667 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
672 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
674 ctx
->FirstTimeCurrent
= GL_TRUE
;
681 * Allocate and initialize a struct gl_context structure.
682 * Note that the driver needs to pass in its dd_function_table here since
683 * we need to at least call driverFunctions->NewTextureObject to initialize
684 * the rendering context.
686 * \param api the GL API type to create the context for
687 * \param visual a struct gl_config pointer (we copy the struct contents)
688 * \param share_list another context to share display lists with or NULL
689 * \param driverFunctions points to the dd_function_table into which the
690 * driver has plugged in all its special functions.
691 * \param driverContext points to the device driver's private context state
693 * \return pointer to a new __struct gl_contextRec or NULL if error.
696 _mesa_create_context(const struct gl_config
*visual
,
697 struct gl_context
*share_list
,
698 const struct dd_function_table
*driverFunctions
,
701 struct gl_context
*ctx
;
704 /*ASSERT(driverContext);*/
706 ctx
= (struct gl_context
*) calloc(1, sizeof(struct gl_context
));
710 if (_mesa_initialize_context(ctx
, visual
, share_list
,
711 driverFunctions
, driverContext
)) {
722 * Free the data associated with the given context.
724 * But doesn't free the struct gl_context struct itself.
726 * \sa _mesa_initialize_context() and init_attrib_groups().
729 _mesa_free_context_data( struct gl_context
*ctx
)
731 if (!_mesa_get_current_context()){
732 /* No current context, but we may need one in order to delete
733 * texture objs, etc. So temporarily bind the context now.
735 _mesa_make_current(ctx
, NULL
, NULL
);
738 /* unreference WinSysDraw/Read buffers */
739 _mesa_reference_framebuffer(&ctx
->WinSysDrawBuffer
, NULL
);
740 _mesa_reference_framebuffer(&ctx
->WinSysReadBuffer
, NULL
);
741 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, NULL
);
742 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, NULL
);
744 _mesa_free_attrib_data(ctx
);
745 _mesa_free_buffer_objects(ctx
);
746 _mesa_free_lighting_data( ctx
);
747 _mesa_free_eval_data( ctx
);
748 _mesa_free_texture_data( ctx
);
749 _mesa_free_matrix_data( ctx
);
750 _mesa_free_viewport_data( ctx
);
751 _mesa_free_varray_data(ctx
);
753 _mesa_delete_array_object(ctx
, ctx
->Array
.DefaultArrayObj
);
755 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
757 /* free dispatch tables */
761 /* Shared context state (display lists, textures, etc) */
762 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
764 /* needs to be after freeing shared state */
765 _mesa_free_display_list_data(ctx
);
767 if (ctx
->Extensions
.String
)
768 free((void *) ctx
->Extensions
.String
);
770 if (ctx
->VersionString
)
771 free(ctx
->VersionString
);
773 /* unbind the context if it's currently bound */
774 if (ctx
== _mesa_get_current_context()) {
775 _mesa_make_current(NULL
, NULL
, NULL
);
781 * Destroy a struct gl_context structure.
783 * \param ctx GL context.
785 * Calls _mesa_free_context_data() and frees the gl_context object itself.
788 _mesa_destroy_context( struct gl_context
*ctx
)
791 _mesa_free_context_data(ctx
);
792 free( (void *) ctx
);
799 * Copy attribute groups from one context to another.
801 * \param src source context
802 * \param dst destination context
803 * \param mask bitwise OR of GL_*_BIT flags
805 * According to the bits specified in \p mask, copies the corresponding
806 * attributes from \p src into \p dst. For many of the attributes a simple \c
807 * memcpy is not enough due to the existence of internal pointers in their data
811 _mesa_copy_context( const struct gl_context
*src
, struct gl_context
*dst
,
814 if (mask
& GL_ACCUM_BUFFER_BIT
) {
816 dst
->Accum
= src
->Accum
;
818 if (mask
& GL_COLOR_BUFFER_BIT
) {
820 dst
->Color
= src
->Color
;
822 if (mask
& GL_CURRENT_BIT
) {
824 dst
->Current
= src
->Current
;
826 if (mask
& GL_DEPTH_BUFFER_BIT
) {
828 dst
->Depth
= src
->Depth
;
830 if (mask
& GL_ENABLE_BIT
) {
833 if (mask
& GL_EVAL_BIT
) {
835 dst
->Eval
= src
->Eval
;
837 if (mask
& GL_FOG_BIT
) {
841 if (mask
& GL_HINT_BIT
) {
843 dst
->Hint
= src
->Hint
;
845 if (mask
& GL_LIGHTING_BIT
) {
847 /* begin with memcpy */
848 dst
->Light
= src
->Light
;
849 /* fixup linked lists to prevent pointer insanity */
850 make_empty_list( &(dst
->Light
.EnabledList
) );
851 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
852 if (dst
->Light
.Light
[i
].Enabled
) {
853 insert_at_tail(&(dst
->Light
.EnabledList
), &(dst
->Light
.Light
[i
]));
857 if (mask
& GL_LINE_BIT
) {
859 dst
->Line
= src
->Line
;
861 if (mask
& GL_LIST_BIT
) {
863 dst
->List
= src
->List
;
865 if (mask
& GL_PIXEL_MODE_BIT
) {
867 dst
->Pixel
= src
->Pixel
;
869 if (mask
& GL_POINT_BIT
) {
871 dst
->Point
= src
->Point
;
873 if (mask
& GL_POLYGON_BIT
) {
875 dst
->Polygon
= src
->Polygon
;
877 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
878 /* Use loop instead of memcpy due to problem with Portland Group's
879 * C compiler. Reported by John Stone.
882 for (i
= 0; i
< 32; i
++) {
883 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
886 if (mask
& GL_SCISSOR_BIT
) {
888 dst
->Scissor
= src
->Scissor
;
890 if (mask
& GL_STENCIL_BUFFER_BIT
) {
892 dst
->Stencil
= src
->Stencil
;
894 if (mask
& GL_TEXTURE_BIT
) {
895 /* Cannot memcpy because of pointers */
896 _mesa_copy_texture_state(src
, dst
);
898 if (mask
& GL_TRANSFORM_BIT
) {
900 dst
->Transform
= src
->Transform
;
902 if (mask
& GL_VIEWPORT_BIT
) {
903 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
904 dst
->Viewport
.X
= src
->Viewport
.X
;
905 dst
->Viewport
.Y
= src
->Viewport
.Y
;
906 dst
->Viewport
.Width
= src
->Viewport
.Width
;
907 dst
->Viewport
.Height
= src
->Viewport
.Height
;
908 dst
->Viewport
.Near
= src
->Viewport
.Near
;
909 dst
->Viewport
.Far
= src
->Viewport
.Far
;
910 _math_matrix_copy(&dst
->Viewport
._WindowMap
, &src
->Viewport
._WindowMap
);
913 /* XXX FIXME: Call callbacks?
915 dst
->NewState
= _NEW_ALL
;
921 * Check if the given context can render into the given framebuffer
922 * by checking visual attributes.
924 * Most of these tests could go away because Mesa is now pretty flexible
925 * in terms of mixing rendering contexts with framebuffers. As long
926 * as RGB vs. CI mode agree, we're probably good.
928 * \return GL_TRUE if compatible, GL_FALSE otherwise.
931 check_compatible(const struct gl_context
*ctx
,
932 const struct gl_framebuffer
*buffer
)
934 const struct gl_config
*ctxvis
= &ctx
->Visual
;
935 const struct gl_config
*bufvis
= &buffer
->Visual
;
938 /* disabling this fixes the fgl_glxgears pbuffer demo */
939 if (ctxvis
->doubleBufferMode
&& !bufvis
->doubleBufferMode
)
942 if (ctxvis
->stereoMode
&& !bufvis
->stereoMode
)
944 if (ctxvis
->haveAccumBuffer
&& !bufvis
->haveAccumBuffer
)
946 if (ctxvis
->haveDepthBuffer
&& !bufvis
->haveDepthBuffer
)
948 if (ctxvis
->haveStencilBuffer
&& !bufvis
->haveStencilBuffer
)
950 if (ctxvis
->redMask
&& ctxvis
->redMask
!= bufvis
->redMask
)
952 if (ctxvis
->greenMask
&& ctxvis
->greenMask
!= bufvis
->greenMask
)
954 if (ctxvis
->blueMask
&& ctxvis
->blueMask
!= bufvis
->blueMask
)
957 /* disabled (see bug 11161) */
958 if (ctxvis
->depthBits
&& ctxvis
->depthBits
!= bufvis
->depthBits
)
961 if (ctxvis
->stencilBits
&& ctxvis
->stencilBits
!= bufvis
->stencilBits
)
969 * Do one-time initialization for the given framebuffer. Specifically,
970 * ask the driver for the window's current size and update the framebuffer
972 * Really, the device driver should totally take care of this.
975 initialize_framebuffer_size(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
977 GLuint width
, height
;
978 if (ctx
->Driver
.GetBufferSize
) {
979 ctx
->Driver
.GetBufferSize(fb
, &width
, &height
);
980 if (ctx
->Driver
.ResizeBuffers
)
981 ctx
->Driver
.ResizeBuffers(ctx
, fb
, width
, height
);
982 fb
->Initialized
= GL_TRUE
;
988 * Check if the viewport/scissor size has not yet been initialized.
989 * Initialize the size if the given width and height are non-zero.
992 _mesa_check_init_viewport(struct gl_context
*ctx
, GLuint width
, GLuint height
)
994 if (!ctx
->ViewportInitialized
&& width
> 0 && height
> 0) {
995 /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
996 * potential infinite recursion.
998 ctx
->ViewportInitialized
= GL_TRUE
;
999 _mesa_set_viewport(ctx
, 0, 0, width
, height
);
1000 _mesa_set_scissor(ctx
, 0, 0, width
, height
);
1006 * Bind the given context to the given drawBuffer and readBuffer and
1007 * make it the current context for the calling thread.
1008 * We'll render into the drawBuffer and read pixels from the
1009 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1011 * We check that the context's and framebuffer's visuals are compatible
1012 * and return immediately if they're not.
1014 * \param newCtx the new GL context. If NULL then there will be no current GL
1016 * \param drawBuffer the drawing framebuffer
1017 * \param readBuffer the reading framebuffer
1020 _mesa_make_current( struct gl_context
*newCtx
,
1021 struct gl_framebuffer
*drawBuffer
,
1022 struct gl_framebuffer
*readBuffer
)
1024 GET_CURRENT_CONTEXT(curCtx
);
1026 if (MESA_VERBOSE
& VERBOSE_API
)
1027 _mesa_debug(newCtx
, "_mesa_make_current()\n");
1029 /* Check that the context's and framebuffer's visuals are compatible.
1031 if (newCtx
&& drawBuffer
&& newCtx
->WinSysDrawBuffer
!= drawBuffer
) {
1032 if (!check_compatible(newCtx
, drawBuffer
)) {
1033 _mesa_warning(newCtx
,
1034 "MakeCurrent: incompatible visuals for context and drawbuffer");
1038 if (newCtx
&& readBuffer
&& newCtx
->WinSysReadBuffer
!= readBuffer
) {
1039 if (!check_compatible(newCtx
, readBuffer
)) {
1040 _mesa_warning(newCtx
,
1041 "MakeCurrent: incompatible visuals for context and readbuffer");
1047 (curCtx
->WinSysDrawBuffer
|| curCtx
->WinSysReadBuffer
) &&
1048 /* make sure this context is valid for flushing */
1050 _mesa_flush(curCtx
);
1052 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1053 _glapi_set_context((void *) newCtx
);
1054 ASSERT(_mesa_get_current_context() == newCtx
);
1057 _glapi_set_dispatch(NULL
); /* none current */
1060 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1062 if (drawBuffer
&& readBuffer
) {
1063 ASSERT(drawBuffer
->Name
== 0);
1064 ASSERT(readBuffer
->Name
== 0);
1065 _mesa_reference_framebuffer(&newCtx
->WinSysDrawBuffer
, drawBuffer
);
1066 _mesa_reference_framebuffer(&newCtx
->WinSysReadBuffer
, readBuffer
);
1069 * Only set the context's Draw/ReadBuffer fields if they're NULL
1070 * or not bound to a user-created FBO.
1072 _mesa_reference_framebuffer(&newCtx
->DrawBuffer
, drawBuffer
);
1073 /* Update the FBO's list of drawbuffers/renderbuffers.
1074 * For winsys FBOs this comes from the GL state (which may have
1075 * changed since the last time this FBO was bound).
1077 _mesa_update_draw_buffer(newCtx
);
1079 _mesa_reference_framebuffer(&newCtx
->ReadBuffer
, readBuffer
);
1081 /* XXX only set this flag if we're really changing the draw/read
1082 * framebuffer bindings.
1084 newCtx
->NewState
|= _NEW_BUFFERS
;
1087 /* We want to get rid of these lines: */
1090 if (!drawBuffer
->Initialized
) {
1091 initialize_framebuffer_size(newCtx
, drawBuffer
);
1093 if (readBuffer
!= drawBuffer
&& !readBuffer
->Initialized
) {
1094 initialize_framebuffer_size(newCtx
, readBuffer
);
1097 _mesa_resizebuffers(newCtx
);
1101 /* We want the drawBuffer and readBuffer to be initialized by
1103 * This generally means the Width and Height match the actual
1104 * window size and the renderbuffers (both hardware and software
1105 * based) are allocated to match. The later can generally be
1106 * done with a call to _mesa_resize_framebuffer().
1108 * It's theoretically possible for a buffer to have zero width
1109 * or height, but for now, assert check that the driver did what's
1112 ASSERT(drawBuffer
->Width
> 0);
1113 ASSERT(drawBuffer
->Height
> 0);
1117 _mesa_check_init_viewport(newCtx
,
1118 drawBuffer
->Width
, drawBuffer
->Height
);
1122 if (newCtx
->FirstTimeCurrent
) {
1123 _mesa_compute_version(newCtx
);
1125 newCtx
->Extensions
.String
= _mesa_make_extension_string(newCtx
);
1127 check_context_limits(newCtx
);
1129 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1138 * Make context 'ctx' share the display lists, textures and programs
1139 * that are associated with 'ctxToShare'.
1140 * Any display lists, textures or programs associated with 'ctx' will
1141 * be deleted if nobody else is sharing them.
1144 _mesa_share_state(struct gl_context
*ctx
, struct gl_context
*ctxToShare
)
1146 if (ctx
&& ctxToShare
&& ctx
->Shared
&& ctxToShare
->Shared
) {
1147 struct gl_shared_state
*oldShared
= NULL
;
1149 /* save ref to old state to prevent it from being deleted immediately */
1150 _mesa_reference_shared_state(ctx
, &oldShared
, ctx
->Shared
);
1152 /* update ctx's Shared pointer */
1153 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, ctxToShare
->Shared
);
1155 update_default_objects(ctx
);
1157 /* release the old shared state */
1158 _mesa_reference_shared_state(ctx
, &oldShared
, NULL
);
1170 * \return pointer to the current GL context for this thread.
1172 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1173 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1177 _mesa_get_current_context( void )
1179 return (struct gl_context
*) _glapi_get_context();
1184 * Get context's current API dispatch table.
1186 * It'll either be the immediate-mode execute dispatcher or the display list
1187 * compile dispatcher.
1189 * \param ctx GL context.
1191 * \return pointer to dispatch_table.
1193 * Simply returns __struct gl_contextRec::CurrentDispatch.
1195 struct _glapi_table
*
1196 _mesa_get_dispatch(struct gl_context
*ctx
)
1198 return ctx
->CurrentDispatch
;
1204 /**********************************************************************/
1205 /** \name Miscellaneous functions */
1206 /**********************************************************************/
1212 * \param ctx GL context.
1213 * \param error error code.
1215 * Records the given error code and call the driver's dd_function_table::Error
1216 * function if defined.
1219 * This is called via _mesa_error().
1222 _mesa_record_error(struct gl_context
*ctx
, GLenum error
)
1227 if (ctx
->ErrorValue
== GL_NO_ERROR
) {
1228 ctx
->ErrorValue
= error
;
1231 /* Call device driver's error handler, if any. This is used on the Mac. */
1232 if (ctx
->Driver
.Error
) {
1233 ctx
->Driver
.Error(ctx
);
1239 * Flush commands and wait for completion.
1242 _mesa_finish(struct gl_context
*ctx
)
1244 FLUSH_CURRENT( ctx
, 0 );
1245 if (ctx
->Driver
.Finish
) {
1246 ctx
->Driver
.Finish(ctx
);
1255 _mesa_flush(struct gl_context
*ctx
)
1257 FLUSH_CURRENT( ctx
, 0 );
1258 if (ctx
->Driver
.Flush
) {
1259 ctx
->Driver
.Flush(ctx
);
1266 * Execute glFinish().
1268 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1269 * dd_function_table::Finish driver callback, if not NULL.
1274 GET_CURRENT_CONTEXT(ctx
);
1275 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1281 * Execute glFlush().
1283 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1284 * dd_function_table::Flush driver callback, if not NULL.
1289 GET_CURRENT_CONTEXT(ctx
);
1290 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1296 * Set mvp_with_dp4 flag. If a driver has a preference for DP4 over
1297 * MUL/MAD, or vice versa, call this function to register that.
1298 * Otherwise we default to MUL/MAD.
1301 _mesa_set_mvp_with_dp4( struct gl_context
*ctx
,
1304 ctx
->mvp_with_dp4
= flag
;
1310 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
1311 * is called to see if it's valid to render. This involves checking that
1312 * the current shader is valid and the framebuffer is complete.
1313 * If an error is detected it'll be recorded here.
1314 * \return GL_TRUE if OK to render, GL_FALSE if not
1317 _mesa_valid_to_render(struct gl_context
*ctx
, const char *where
)
1319 /* This depends on having up to date derived state (shaders) */
1321 _mesa_update_state(ctx
);