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_COLOR
], 1.0, 1.0, 1.0, 1.0 );
377 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
], 1.0, 0.0, 0.0, 1.0 );
378 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
], 1.0, 0.0, 0.0, 1.0 );
382 * Initialize fields of gl_constants (aka ctx->Const.*).
383 * Use defaults from config.h. The device drivers will often override
384 * some of these values (such as number of texture units).
387 _mesa_init_constants(struct gl_context
*ctx
)
391 /* Constants, may be overriden (usually only reduced) by device drivers */
392 ctx
->Const
.MaxTextureMbytes
= MAX_TEXTURE_MBYTES
;
393 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
394 ctx
->Const
.MaxCubeTextureLevels
= MAX_CUBE_TEXTURE_LEVELS
;
395 ctx
->Const
.MaxTextureMaxAnisotropy
= MAX_TEXTURE_MAX_ANISOTROPY
;
396 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
397 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
398 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
399 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
400 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
401 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
402 ctx
->Const
.PointSizeGranularity
= (GLfloat
) POINT_SIZE_GRANULARITY
;
403 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
404 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
405 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
406 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
407 ctx
->Const
.LineWidthGranularity
= (GLfloat
) LINE_WIDTH_GRANULARITY
;
408 ctx
->Const
.MaxClipPlanes
= 6;
409 ctx
->Const
.MaxLights
= MAX_LIGHTS
;
410 ctx
->Const
.MaxShininess
= 128.0;
411 ctx
->Const
.MaxSpotExponent
= 128.0;
412 ctx
->Const
.MaxViewportWidth
= MAX_WIDTH
;
413 ctx
->Const
.MaxViewportHeight
= MAX_HEIGHT
;
415 /* CheckArrayBounds is overriden by drivers/x11 for X server */
416 ctx
->Const
.CheckArrayBounds
= GL_FALSE
;
418 /* GL 3.2: hard-coded for now: */
419 ctx
->Const
.ProfileMask
= GL_CONTEXT_COMPATIBILITY_PROFILE_BIT
;
424 * Do some sanity checks on the limits/constants for the given context.
425 * Only called the first time a context is bound.
428 check_context_limits(struct gl_context
*ctx
)
430 /* Texture size checks */
431 assert(ctx
->Const
.MaxTextureLevels
<= MAX_TEXTURE_LEVELS
);
432 assert(ctx
->Const
.Max3DTextureLevels
<= MAX_3D_TEXTURE_LEVELS
);
433 assert(ctx
->Const
.MaxCubeTextureLevels
<= MAX_CUBE_TEXTURE_LEVELS
);
435 /* make sure largest texture image is <= MAX_WIDTH in size */
436 assert((1 << (ctx
->Const
.MaxTextureLevels
- 1)) <= MAX_WIDTH
);
437 assert((1 << (ctx
->Const
.MaxCubeTextureLevels
- 1)) <= MAX_WIDTH
);
438 assert((1 << (ctx
->Const
.Max3DTextureLevels
- 1)) <= MAX_WIDTH
);
440 /* Texture level checks */
441 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
442 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
444 /* Max texture size should be <= max viewport size (render to texture) */
445 assert((1 << (MAX_TEXTURE_LEVELS
- 1)) <= MAX_WIDTH
);
447 assert(ctx
->Const
.MaxViewportWidth
<= MAX_WIDTH
);
448 assert(ctx
->Const
.MaxViewportHeight
<= MAX_WIDTH
);
453 * Initialize the attribute groups in a GL context.
455 * \param ctx GL context.
457 * Initializes all the attributes, calling the respective <tt>init*</tt>
458 * functions for the more complex data structures.
461 init_attrib_groups(struct gl_context
*ctx
)
466 _mesa_init_constants( ctx
);
469 _mesa_init_extensions( ctx
);
471 /* Attribute Groups */
472 _mesa_init_accum( ctx
);
473 _mesa_init_attrib( ctx
);
474 _mesa_init_buffer_objects( ctx
);
475 _mesa_init_color( ctx
);
476 _mesa_init_current( ctx
);
477 _mesa_init_depth( ctx
);
478 _mesa_init_display_list( ctx
);
479 _mesa_init_eval( ctx
);
480 _mesa_init_feedback( ctx
);
481 _mesa_init_fog( ctx
);
482 _mesa_init_hint( ctx
);
483 _mesa_init_line( ctx
);
484 _mesa_init_lighting( ctx
);
485 _mesa_init_matrix( ctx
);
486 _mesa_init_multisample( ctx
);
487 _mesa_init_pixel( ctx
);
488 _mesa_init_pixelstore( ctx
);
489 _mesa_init_point( ctx
);
490 _mesa_init_polygon( ctx
);
491 _mesa_init_rastpos( ctx
);
492 _mesa_init_scissor( ctx
);
493 _mesa_init_stencil( ctx
);
494 _mesa_init_transform( ctx
);
495 _mesa_init_varray(ctx
, &ctx
->Array
);
496 _mesa_init_viewport( ctx
);
498 if (!_mesa_init_texture( ctx
))
502 ctx
->NewState
= _NEW_ALL
;
503 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
510 * Update default objects in a GL context with respect to shared state.
512 * \param ctx GL context.
514 * Removes references to old default objects, (texture objects, program
515 * objects, etc.) and changes to reference those from the current shared
519 update_default_objects(struct gl_context
*ctx
)
523 _mesa_update_default_objects_texture(ctx
);
524 _mesa_update_default_objects_buffer_objects(ctx
);
531 * This is the default function we plug into all dispatch table slots
532 * This helps prevents a segfault when someone calls a GL function without
533 * first checking if the extension's supported.
538 _mesa_warning(NULL
, "User called no-op dispatch function (an unsupported extension function?)");
544 * Allocate and initialize a new dispatch table.
546 struct _glapi_table
*
547 _mesa_alloc_dispatch_table(int size
)
549 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
550 * In practice, this'll be the same for stand-alone Mesa. But for DRI
551 * Mesa we do this to accomodate different versions of libGL and various
554 GLint numEntries
= MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT
);
555 struct _glapi_table
*table
;
557 /* should never happen, but just in case */
558 numEntries
= MAX2(numEntries
, size
);
560 table
= (struct _glapi_table
*) malloc(numEntries
* sizeof(_glapi_proc
));
562 _glapi_proc
*entry
= (_glapi_proc
*) table
;
564 for (i
= 0; i
< numEntries
; i
++) {
565 entry
[i
] = (_glapi_proc
) generic_nop
;
573 * Initialize a struct gl_context struct (rendering context).
575 * This includes allocating all the other structs and arrays which hang off of
576 * the context by pointers.
577 * Note that the driver needs to pass in its dd_function_table here since
578 * we need to at least call driverFunctions->NewTextureObject to create the
579 * default texture objects.
581 * Called by _mesa_create_context().
583 * Performs the imports and exports callback tables initialization, and
584 * miscellaneous one-time initializations. If no shared context is supplied one
585 * is allocated, and increase its reference count. Setups the GL API dispatch
586 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
587 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
590 * \param ctx the context to initialize
591 * \param api the GL API type to create the context for
592 * \param visual describes the visual attributes for this context
593 * \param share_list points to context to share textures, display lists,
595 * \param driverFunctions table of device driver functions for this context
597 * \param driverContext pointer to driver-specific context data
600 _mesa_initialize_context(struct gl_context
*ctx
,
601 const struct gl_config
*visual
,
602 struct gl_context
*share_list
,
603 const struct dd_function_table
*driverFunctions
,
606 struct gl_shared_state
*shared
;
608 /*ASSERT(driverContext);*/
609 assert(driverFunctions
->NewTextureObject
);
610 assert(driverFunctions
->FreeTextureImageBuffer
);
612 ctx
->Visual
= *visual
;
613 ctx
->DrawBuffer
= NULL
;
614 ctx
->ReadBuffer
= NULL
;
615 ctx
->WinSysDrawBuffer
= NULL
;
616 ctx
->WinSysReadBuffer
= NULL
;
618 /* misc one-time initializations */
621 /* Plug in driver functions and context pointer here.
622 * This is important because when we call alloc_shared_state() below
623 * we'll call ctx->Driver.NewTextureObject() to create the default
626 ctx
->Driver
= *driverFunctions
;
627 ctx
->DriverCtx
= driverContext
;
630 /* share state with another context */
631 shared
= share_list
->Shared
;
634 /* allocate new, unshared state */
635 shared
= _mesa_alloc_shared_state(ctx
);
640 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, shared
);
642 if (!init_attrib_groups( ctx
)) {
643 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
647 ctx
->Exec
= _mesa_create_exec_table();
650 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
653 ctx
->CurrentDispatch
= ctx
->Exec
;
655 /* Mesa core handles all the formats that mesa core knows about.
656 * Drivers will want to override this list with just the formats
657 * they can handle, and confirm that appropriate fallbacks exist in
658 * _mesa_choose_tex_format().
660 memset(&ctx
->TextureFormatSupported
, GL_TRUE
,
661 sizeof(ctx
->TextureFormatSupported
));
663 ctx
->Save
= _mesa_create_save_table();
665 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
670 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
672 ctx
->FirstTimeCurrent
= GL_TRUE
;
679 * Allocate and initialize a struct gl_context structure.
680 * Note that the driver needs to pass in its dd_function_table here since
681 * we need to at least call driverFunctions->NewTextureObject to initialize
682 * the rendering context.
684 * \param api the GL API type to create the context for
685 * \param visual a struct gl_config pointer (we copy the struct contents)
686 * \param share_list another context to share display lists with or NULL
687 * \param driverFunctions points to the dd_function_table into which the
688 * driver has plugged in all its special functions.
689 * \param driverContext points to the device driver's private context state
691 * \return pointer to a new __struct gl_contextRec or NULL if error.
694 _mesa_create_context(const struct gl_config
*visual
,
695 struct gl_context
*share_list
,
696 const struct dd_function_table
*driverFunctions
,
699 struct gl_context
*ctx
;
702 /*ASSERT(driverContext);*/
704 ctx
= (struct gl_context
*) calloc(1, sizeof(struct gl_context
));
708 if (_mesa_initialize_context(ctx
, visual
, share_list
,
709 driverFunctions
, driverContext
)) {
720 * Free the data associated with the given context.
722 * But doesn't free the struct gl_context struct itself.
724 * \sa _mesa_initialize_context() and init_attrib_groups().
727 _mesa_free_context_data( struct gl_context
*ctx
)
729 if (!_mesa_get_current_context()){
730 /* No current context, but we may need one in order to delete
731 * texture objs, etc. So temporarily bind the context now.
733 _mesa_make_current(ctx
, NULL
, NULL
);
736 /* unreference WinSysDraw/Read buffers */
737 _mesa_reference_framebuffer(&ctx
->WinSysDrawBuffer
, NULL
);
738 _mesa_reference_framebuffer(&ctx
->WinSysReadBuffer
, NULL
);
739 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, NULL
);
740 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, NULL
);
742 _mesa_free_attrib_data(ctx
);
743 _mesa_free_lighting_data( ctx
);
744 _mesa_free_eval_data( ctx
);
745 _mesa_free_texture_data( ctx
);
746 _mesa_free_matrix_data( ctx
);
747 _mesa_free_viewport_data( ctx
);
748 _mesa_free_varray_data(ctx
, &ctx
->Array
);
750 /* free dispatch tables */
754 /* Shared context state (display lists, textures, etc) */
755 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, NULL
);
757 /* needs to be after freeing shared state */
758 _mesa_free_display_list_data(ctx
);
760 if (ctx
->Extensions
.String
)
761 free((void *) ctx
->Extensions
.String
);
763 if (ctx
->VersionString
)
764 free(ctx
->VersionString
);
766 /* unbind the context if it's currently bound */
767 if (ctx
== _mesa_get_current_context()) {
768 _mesa_make_current(NULL
, NULL
, NULL
);
774 * Destroy a struct gl_context structure.
776 * \param ctx GL context.
778 * Calls _mesa_free_context_data() and frees the gl_context object itself.
781 _mesa_destroy_context( struct gl_context
*ctx
)
784 _mesa_free_context_data(ctx
);
785 free( (void *) ctx
);
792 * Copy attribute groups from one context to another.
794 * \param src source context
795 * \param dst destination context
796 * \param mask bitwise OR of GL_*_BIT flags
798 * According to the bits specified in \p mask, copies the corresponding
799 * attributes from \p src into \p dst. For many of the attributes a simple \c
800 * memcpy is not enough due to the existence of internal pointers in their data
804 _mesa_copy_context( const struct gl_context
*src
, struct gl_context
*dst
,
807 if (mask
& GL_ACCUM_BUFFER_BIT
) {
809 dst
->Accum
= src
->Accum
;
811 if (mask
& GL_COLOR_BUFFER_BIT
) {
813 dst
->Color
= src
->Color
;
815 if (mask
& GL_CURRENT_BIT
) {
817 dst
->Current
= src
->Current
;
819 if (mask
& GL_DEPTH_BUFFER_BIT
) {
821 dst
->Depth
= src
->Depth
;
823 if (mask
& GL_ENABLE_BIT
) {
826 if (mask
& GL_EVAL_BIT
) {
828 dst
->Eval
= src
->Eval
;
830 if (mask
& GL_FOG_BIT
) {
834 if (mask
& GL_HINT_BIT
) {
836 dst
->Hint
= src
->Hint
;
838 if (mask
& GL_LIGHTING_BIT
) {
840 /* begin with memcpy */
841 dst
->Light
= src
->Light
;
842 /* fixup linked lists to prevent pointer insanity */
843 make_empty_list( &(dst
->Light
.EnabledList
) );
844 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
845 if (dst
->Light
.Light
[i
].Enabled
) {
846 insert_at_tail(&(dst
->Light
.EnabledList
), &(dst
->Light
.Light
[i
]));
850 if (mask
& GL_LINE_BIT
) {
852 dst
->Line
= src
->Line
;
854 if (mask
& GL_LIST_BIT
) {
856 dst
->List
= src
->List
;
858 if (mask
& GL_PIXEL_MODE_BIT
) {
860 dst
->Pixel
= src
->Pixel
;
862 if (mask
& GL_POINT_BIT
) {
864 dst
->Point
= src
->Point
;
866 if (mask
& GL_POLYGON_BIT
) {
868 dst
->Polygon
= src
->Polygon
;
870 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
871 /* Use loop instead of memcpy due to problem with Portland Group's
872 * C compiler. Reported by John Stone.
875 for (i
= 0; i
< 32; i
++) {
876 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
879 if (mask
& GL_SCISSOR_BIT
) {
881 dst
->Scissor
= src
->Scissor
;
883 if (mask
& GL_STENCIL_BUFFER_BIT
) {
885 dst
->Stencil
= src
->Stencil
;
887 if (mask
& GL_TEXTURE_BIT
) {
888 /* Cannot memcpy because of pointers */
889 _mesa_copy_texture_state(src
, dst
);
891 if (mask
& GL_TRANSFORM_BIT
) {
893 dst
->Transform
= src
->Transform
;
895 if (mask
& GL_VIEWPORT_BIT
) {
896 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
897 dst
->Viewport
.X
= src
->Viewport
.X
;
898 dst
->Viewport
.Y
= src
->Viewport
.Y
;
899 dst
->Viewport
.Width
= src
->Viewport
.Width
;
900 dst
->Viewport
.Height
= src
->Viewport
.Height
;
901 dst
->Viewport
.Near
= src
->Viewport
.Near
;
902 dst
->Viewport
.Far
= src
->Viewport
.Far
;
903 _math_matrix_copy(&dst
->Viewport
._WindowMap
, &src
->Viewport
._WindowMap
);
906 /* XXX FIXME: Call callbacks?
908 dst
->NewState
= _NEW_ALL
;
914 * Check if the given context can render into the given framebuffer
915 * by checking visual attributes.
917 * Most of these tests could go away because Mesa is now pretty flexible
918 * in terms of mixing rendering contexts with framebuffers. As long
919 * as RGB vs. CI mode agree, we're probably good.
921 * \return GL_TRUE if compatible, GL_FALSE otherwise.
924 check_compatible(const struct gl_context
*ctx
,
925 const struct gl_framebuffer
*buffer
)
927 const struct gl_config
*ctxvis
= &ctx
->Visual
;
928 const struct gl_config
*bufvis
= &buffer
->Visual
;
931 /* disabling this fixes the fgl_glxgears pbuffer demo */
932 if (ctxvis
->doubleBufferMode
&& !bufvis
->doubleBufferMode
)
935 if (ctxvis
->stereoMode
&& !bufvis
->stereoMode
)
937 if (ctxvis
->haveAccumBuffer
&& !bufvis
->haveAccumBuffer
)
939 if (ctxvis
->haveDepthBuffer
&& !bufvis
->haveDepthBuffer
)
941 if (ctxvis
->haveStencilBuffer
&& !bufvis
->haveStencilBuffer
)
943 if (ctxvis
->redMask
&& ctxvis
->redMask
!= bufvis
->redMask
)
945 if (ctxvis
->greenMask
&& ctxvis
->greenMask
!= bufvis
->greenMask
)
947 if (ctxvis
->blueMask
&& ctxvis
->blueMask
!= bufvis
->blueMask
)
950 /* disabled (see bug 11161) */
951 if (ctxvis
->depthBits
&& ctxvis
->depthBits
!= bufvis
->depthBits
)
954 if (ctxvis
->stencilBits
&& ctxvis
->stencilBits
!= bufvis
->stencilBits
)
962 * Do one-time initialization for the given framebuffer. Specifically,
963 * ask the driver for the window's current size and update the framebuffer
965 * Really, the device driver should totally take care of this.
968 initialize_framebuffer_size(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
970 GLuint width
, height
;
971 if (ctx
->Driver
.GetBufferSize
) {
972 ctx
->Driver
.GetBufferSize(fb
, &width
, &height
);
973 if (ctx
->Driver
.ResizeBuffers
)
974 ctx
->Driver
.ResizeBuffers(ctx
, fb
, width
, height
);
975 fb
->Initialized
= GL_TRUE
;
981 * Check if the viewport/scissor size has not yet been initialized.
982 * Initialize the size if the given width and height are non-zero.
985 _mesa_check_init_viewport(struct gl_context
*ctx
, GLuint width
, GLuint height
)
987 if (!ctx
->ViewportInitialized
&& width
> 0 && height
> 0) {
988 /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
989 * potential infinite recursion.
991 ctx
->ViewportInitialized
= GL_TRUE
;
992 _mesa_set_viewport(ctx
, 0, 0, width
, height
);
993 _mesa_set_scissor(ctx
, 0, 0, width
, height
);
999 * Bind the given context to the given drawBuffer and readBuffer and
1000 * make it the current context for the calling thread.
1001 * We'll render into the drawBuffer and read pixels from the
1002 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1004 * We check that the context's and framebuffer's visuals are compatible
1005 * and return immediately if they're not.
1007 * \param newCtx the new GL context. If NULL then there will be no current GL
1009 * \param drawBuffer the drawing framebuffer
1010 * \param readBuffer the reading framebuffer
1013 _mesa_make_current( struct gl_context
*newCtx
,
1014 struct gl_framebuffer
*drawBuffer
,
1015 struct gl_framebuffer
*readBuffer
)
1017 GET_CURRENT_CONTEXT(curCtx
);
1019 if (MESA_VERBOSE
& VERBOSE_API
)
1020 _mesa_debug(newCtx
, "_mesa_make_current()\n");
1022 /* Check that the context's and framebuffer's visuals are compatible.
1024 if (newCtx
&& drawBuffer
&& newCtx
->WinSysDrawBuffer
!= drawBuffer
) {
1025 if (!check_compatible(newCtx
, drawBuffer
)) {
1026 _mesa_warning(newCtx
,
1027 "MakeCurrent: incompatible visuals for context and drawbuffer");
1031 if (newCtx
&& readBuffer
&& newCtx
->WinSysReadBuffer
!= readBuffer
) {
1032 if (!check_compatible(newCtx
, readBuffer
)) {
1033 _mesa_warning(newCtx
,
1034 "MakeCurrent: incompatible visuals for context and readbuffer");
1040 (curCtx
->WinSysDrawBuffer
|| curCtx
->WinSysReadBuffer
) &&
1041 /* make sure this context is valid for flushing */
1043 _mesa_flush(curCtx
);
1045 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1046 _glapi_set_context((void *) newCtx
);
1047 ASSERT(_mesa_get_current_context() == newCtx
);
1050 _glapi_set_dispatch(NULL
); /* none current */
1053 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1055 if (drawBuffer
&& readBuffer
) {
1056 ASSERT(drawBuffer
->Name
== 0);
1057 ASSERT(readBuffer
->Name
== 0);
1058 _mesa_reference_framebuffer(&newCtx
->WinSysDrawBuffer
, drawBuffer
);
1059 _mesa_reference_framebuffer(&newCtx
->WinSysReadBuffer
, readBuffer
);
1062 * Only set the context's Draw/ReadBuffer fields if they're NULL
1063 * or not bound to a user-created FBO.
1065 _mesa_reference_framebuffer(&newCtx
->DrawBuffer
, drawBuffer
);
1066 /* Update the FBO's list of drawbuffers/renderbuffers.
1067 * For winsys FBOs this comes from the GL state (which may have
1068 * changed since the last time this FBO was bound).
1070 _mesa_update_draw_buffer(newCtx
);
1072 _mesa_reference_framebuffer(&newCtx
->ReadBuffer
, readBuffer
);
1074 /* XXX only set this flag if we're really changing the draw/read
1075 * framebuffer bindings.
1077 newCtx
->NewState
|= _NEW_BUFFERS
;
1080 /* We want to get rid of these lines: */
1083 if (!drawBuffer
->Initialized
) {
1084 initialize_framebuffer_size(newCtx
, drawBuffer
);
1086 if (readBuffer
!= drawBuffer
&& !readBuffer
->Initialized
) {
1087 initialize_framebuffer_size(newCtx
, readBuffer
);
1090 _mesa_resizebuffers(newCtx
);
1094 /* We want the drawBuffer and readBuffer to be initialized by
1096 * This generally means the Width and Height match the actual
1097 * window size and the renderbuffers (both hardware and software
1098 * based) are allocated to match. The later can generally be
1099 * done with a call to _mesa_resize_framebuffer().
1101 * It's theoretically possible for a buffer to have zero width
1102 * or height, but for now, assert check that the driver did what's
1105 ASSERT(drawBuffer
->Width
> 0);
1106 ASSERT(drawBuffer
->Height
> 0);
1110 _mesa_check_init_viewport(newCtx
,
1111 drawBuffer
->Width
, drawBuffer
->Height
);
1115 if (newCtx
->FirstTimeCurrent
) {
1116 _mesa_compute_version(newCtx
);
1118 newCtx
->Extensions
.String
= _mesa_make_extension_string(newCtx
);
1120 check_context_limits(newCtx
);
1122 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1131 * Make context 'ctx' share the display lists, textures and programs
1132 * that are associated with 'ctxToShare'.
1133 * Any display lists, textures or programs associated with 'ctx' will
1134 * be deleted if nobody else is sharing them.
1137 _mesa_share_state(struct gl_context
*ctx
, struct gl_context
*ctxToShare
)
1139 if (ctx
&& ctxToShare
&& ctx
->Shared
&& ctxToShare
->Shared
) {
1140 struct gl_shared_state
*oldShared
= NULL
;
1142 /* save ref to old state to prevent it from being deleted immediately */
1143 _mesa_reference_shared_state(ctx
, &oldShared
, ctx
->Shared
);
1145 /* update ctx's Shared pointer */
1146 _mesa_reference_shared_state(ctx
, &ctx
->Shared
, ctxToShare
->Shared
);
1148 update_default_objects(ctx
);
1150 /* release the old shared state */
1151 _mesa_reference_shared_state(ctx
, &oldShared
, NULL
);
1163 * \return pointer to the current GL context for this thread.
1165 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1166 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1170 _mesa_get_current_context( void )
1172 return (struct gl_context
*) _glapi_get_context();
1177 * Get context's current API dispatch table.
1179 * It'll either be the immediate-mode execute dispatcher or the display list
1180 * compile dispatcher.
1182 * \param ctx GL context.
1184 * \return pointer to dispatch_table.
1186 * Simply returns __struct gl_contextRec::CurrentDispatch.
1188 struct _glapi_table
*
1189 _mesa_get_dispatch(struct gl_context
*ctx
)
1191 return ctx
->CurrentDispatch
;
1197 /**********************************************************************/
1198 /** \name Miscellaneous functions */
1199 /**********************************************************************/
1205 * \param ctx GL context.
1206 * \param error error code.
1208 * Records the given error code and call the driver's dd_function_table::Error
1209 * function if defined.
1212 * This is called via _mesa_error().
1215 _mesa_record_error(struct gl_context
*ctx
, GLenum error
)
1220 if (ctx
->ErrorValue
== GL_NO_ERROR
) {
1221 ctx
->ErrorValue
= error
;
1224 /* Call device driver's error handler, if any. This is used on the Mac. */
1225 if (ctx
->Driver
.Error
) {
1226 ctx
->Driver
.Error(ctx
);
1232 * Flush commands and wait for completion.
1235 _mesa_finish(struct gl_context
*ctx
)
1237 FLUSH_CURRENT( ctx
, 0 );
1238 if (ctx
->Driver
.Finish
) {
1239 ctx
->Driver
.Finish(ctx
);
1248 _mesa_flush(struct gl_context
*ctx
)
1250 FLUSH_CURRENT( ctx
, 0 );
1251 if (ctx
->Driver
.Flush
) {
1252 ctx
->Driver
.Flush(ctx
);
1259 * Execute glFinish().
1261 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1262 * dd_function_table::Finish driver callback, if not NULL.
1267 GET_CURRENT_CONTEXT(ctx
);
1268 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1274 * Execute glFlush().
1276 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1277 * dd_function_table::Flush driver callback, if not NULL.
1282 GET_CURRENT_CONTEXT(ctx
);
1283 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1289 * Set mvp_with_dp4 flag. If a driver has a preference for DP4 over
1290 * MUL/MAD, or vice versa, call this function to register that.
1291 * Otherwise we default to MUL/MAD.
1294 _mesa_set_mvp_with_dp4( struct gl_context
*ctx
,
1297 ctx
->mvp_with_dp4
= flag
;
1303 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
1304 * is called to see if it's valid to render. This involves checking that
1305 * the current shader is valid and the framebuffer is complete.
1306 * If an error is detected it'll be recorded here.
1307 * \return GL_TRUE if OK to render, GL_FALSE if not
1310 _mesa_valid_to_render(struct gl_context
*ctx
, const char *where
)
1312 /* This depends on having up to date derived state (shaders) */
1314 _mesa_update_state(ctx
);