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.
80 #include "mfeatures.h"
87 #if FEATURE_attrib_stack
92 #include "bufferobj.h"
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"
138 #include "simple_list.h"
141 #include "texcompress.h"
142 #include "teximage.h"
144 #include "texstate.h"
148 #include "viewport.h"
150 #include "glapi/glthread.h"
151 #include "glapi/glapioffsets.h"
152 #include "glapi/glapitable.h"
153 #include "shader/program.h"
154 #include "shader/prog_print.h"
155 #include "shader/shader_api.h"
156 #if FEATURE_ATI_fragment_shader
157 #include "shader/atifragshader.h"
160 #include "math/m_matrix.h"
164 #include "sparc/sparc.h"
168 int MESA_VERBOSE
= 0;
171 #ifndef MESA_DEBUG_FLAGS
172 int MESA_DEBUG_FLAGS
= 0;
176 /* ubyte -> float conversion */
177 GLfloat _mesa_ubyte_to_float_color_tab
[256];
182 * Swap buffers notification callback.
184 * \param ctx GL context.
186 * Called by window system just before swapping buffers.
187 * We have to finish any pending rendering.
190 _mesa_notifySwapBuffers(__GLcontext
*ctx
)
192 FLUSH_CURRENT( ctx
, 0 );
193 if (ctx
->Driver
.Flush
) {
194 ctx
->Driver
.Flush(ctx
);
199 /**********************************************************************/
200 /** \name GL Visual allocation/destruction */
201 /**********************************************************************/
205 * Allocates a GLvisual structure and initializes it via
206 * _mesa_initialize_visual().
208 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
209 * \param dbFlag double buffering
210 * \param stereoFlag stereo buffer
211 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
212 * is acceptable but the actual depth type will be GLushort or GLuint as
214 * \param stencilBits requested minimum bits per stencil buffer value
215 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
216 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
217 * \param redBits number of bits per color component in frame buffer for RGB(A)
218 * mode. We always use 8 in core Mesa though.
219 * \param greenBits same as above.
220 * \param blueBits same as above.
221 * \param alphaBits same as above.
222 * \param numSamples not really used.
224 * \return pointer to new GLvisual or NULL if requested parameters can't be
227 * \note Need to add params for level and numAuxBuffers (at least)
230 _mesa_create_visual( GLboolean rgbFlag
,
232 GLboolean stereoFlag
,
241 GLint accumGreenBits
,
243 GLint accumAlphaBits
,
246 GLvisual
*vis
= (GLvisual
*) _mesa_calloc(sizeof(GLvisual
));
248 if (!_mesa_initialize_visual(vis
, rgbFlag
, dbFlag
, stereoFlag
,
249 redBits
, greenBits
, blueBits
, alphaBits
,
250 indexBits
, depthBits
, stencilBits
,
251 accumRedBits
, accumGreenBits
,
252 accumBlueBits
, accumAlphaBits
,
262 * Makes some sanity checks and fills in the fields of the
263 * GLvisual object with the given parameters. If the caller needs
264 * to set additional fields, he should just probably init the whole GLvisual
266 * \return GL_TRUE on success, or GL_FALSE on failure.
268 * \sa _mesa_create_visual() above for the parameter description.
271 _mesa_initialize_visual( GLvisual
*vis
,
274 GLboolean stereoFlag
,
283 GLint accumGreenBits
,
285 GLint accumAlphaBits
,
290 if (depthBits
< 0 || depthBits
> 32) {
293 if (stencilBits
< 0 || stencilBits
> STENCIL_BITS
) {
296 assert(accumRedBits
>= 0);
297 assert(accumGreenBits
>= 0);
298 assert(accumBlueBits
>= 0);
299 assert(accumAlphaBits
>= 0);
301 vis
->rgbMode
= rgbFlag
;
302 vis
->doubleBufferMode
= dbFlag
;
303 vis
->stereoMode
= stereoFlag
;
305 vis
->redBits
= redBits
;
306 vis
->greenBits
= greenBits
;
307 vis
->blueBits
= blueBits
;
308 vis
->alphaBits
= alphaBits
;
309 vis
->rgbBits
= redBits
+ greenBits
+ blueBits
;
311 vis
->indexBits
= indexBits
;
312 vis
->depthBits
= depthBits
;
313 vis
->stencilBits
= stencilBits
;
315 vis
->accumRedBits
= accumRedBits
;
316 vis
->accumGreenBits
= accumGreenBits
;
317 vis
->accumBlueBits
= accumBlueBits
;
318 vis
->accumAlphaBits
= accumAlphaBits
;
320 vis
->haveAccumBuffer
= accumRedBits
> 0;
321 vis
->haveDepthBuffer
= depthBits
> 0;
322 vis
->haveStencilBuffer
= stencilBits
> 0;
324 vis
->numAuxBuffers
= 0;
327 vis
->sampleBuffers
= numSamples
> 0 ? 1 : 0;
328 vis
->samples
= numSamples
;
335 * Destroy a visual and free its memory.
339 * Frees the visual structure.
342 _mesa_destroy_visual( GLvisual
*vis
)
350 /**********************************************************************/
351 /** \name Context allocation, initialization, destroying
353 * The purpose of the most initialization functions here is to provide the
354 * default state values according to the OpenGL specification.
356 /**********************************************************************/
361 * This is lame. gdb only seems to recognize enum types that are
362 * actually used somewhere. We want to be able to print/use enum
363 * values such as TEXTURE_2D_INDEX in gdb. But we don't actually use
364 * the gl_texture_index type anywhere. Thus, this lame function.
367 dummy_enum_func(void)
370 gl_colortable_index ci
;
390 * One-time initialization mutex lock.
392 * \sa Used by one_time_init().
394 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock
);
397 * Calls all the various one-time-init functions in Mesa.
399 * While holding a global mutex lock, calls several initialization functions,
400 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
406 one_time_init( GLcontext
*ctx
)
408 static GLboolean alreadyCalled
= GL_FALSE
;
410 _glthread_LOCK_MUTEX(OneTimeLock
);
411 if (!alreadyCalled
) {
414 /* do some implementation tests */
415 assert( sizeof(GLbyte
) == 1 );
416 assert( sizeof(GLubyte
) == 1 );
417 assert( sizeof(GLshort
) == 2 );
418 assert( sizeof(GLushort
) == 2 );
419 assert( sizeof(GLint
) == 4 );
420 assert( sizeof(GLuint
) == 4 );
422 _mesa_get_cpu_features();
424 _mesa_init_sqrt_table();
426 for (i
= 0; i
< 256; i
++) {
427 _mesa_ubyte_to_float_color_tab
[i
] = (float) i
/ 255.0F
;
430 if (_mesa_getenv("MESA_DEBUG")) {
431 _glapi_noop_enable_warnings(GL_TRUE
);
432 _glapi_set_warning_func( (_glapi_warning_func
) _mesa_warning
);
435 _glapi_noop_enable_warnings(GL_FALSE
);
438 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
439 _mesa_debug(ctx
, "Mesa %s DEBUG build %s %s\n",
440 MESA_VERSION_STRING
, __DATE__
, __TIME__
);
443 alreadyCalled
= GL_TRUE
;
445 _glthread_UNLOCK_MUTEX(OneTimeLock
);
452 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
455 _mesa_init_current(GLcontext
*ctx
)
459 /* Init all to (0,0,0,1) */
460 for (i
= 0; i
< Elements(ctx
->Current
.Attrib
); i
++) {
461 ASSIGN_4V( ctx
->Current
.Attrib
[i
], 0.0, 0.0, 0.0, 1.0 );
464 /* redo special cases: */
465 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_WEIGHT
], 1.0, 0.0, 0.0, 0.0 );
466 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_NORMAL
], 0.0, 0.0, 1.0, 1.0 );
467 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
], 1.0, 1.0, 1.0, 1.0 );
468 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR1
], 0.0, 0.0, 0.0, 1.0 );
469 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR_INDEX
], 1.0, 0.0, 0.0, 1.0 );
470 ASSIGN_4V( ctx
->Current
.Attrib
[VERT_ATTRIB_EDGEFLAG
], 1.0, 0.0, 0.0, 1.0 );
475 * Init vertex/fragment program limits.
476 * Important: drivers should override these with actual limits.
479 init_program_limits(GLenum type
, struct gl_program_constants
*prog
)
481 prog
->MaxInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
482 prog
->MaxAluInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
483 prog
->MaxTexInstructions
= MAX_PROGRAM_INSTRUCTIONS
;
484 prog
->MaxTexIndirections
= MAX_PROGRAM_INSTRUCTIONS
;
485 prog
->MaxTemps
= MAX_PROGRAM_TEMPS
;
486 prog
->MaxEnvParams
= MAX_PROGRAM_ENV_PARAMS
;
487 prog
->MaxLocalParams
= MAX_PROGRAM_LOCAL_PARAMS
;
488 prog
->MaxUniformComponents
= 4 * MAX_UNIFORMS
;
490 if (type
== GL_VERTEX_PROGRAM_ARB
) {
491 prog
->MaxParameters
= MAX_VERTEX_PROGRAM_PARAMS
;
492 prog
->MaxAttribs
= MAX_NV_VERTEX_PROGRAM_INPUTS
;
493 prog
->MaxAddressRegs
= MAX_VERTEX_PROGRAM_ADDRESS_REGS
;
496 prog
->MaxParameters
= MAX_NV_FRAGMENT_PROGRAM_PARAMS
;
497 prog
->MaxAttribs
= MAX_NV_FRAGMENT_PROGRAM_INPUTS
;
498 prog
->MaxAddressRegs
= MAX_FRAGMENT_PROGRAM_ADDRESS_REGS
;
501 /* Set the native limits to zero. This implies that there is no native
502 * support for shaders. Let the drivers fill in the actual values.
504 prog
->MaxNativeInstructions
= 0;
505 prog
->MaxNativeAluInstructions
= 0;
506 prog
->MaxNativeTexInstructions
= 0;
507 prog
->MaxNativeTexIndirections
= 0;
508 prog
->MaxNativeAttribs
= 0;
509 prog
->MaxNativeTemps
= 0;
510 prog
->MaxNativeAddressRegs
= 0;
511 prog
->MaxNativeParameters
= 0;
516 * Initialize fields of gl_constants (aka ctx->Const.*).
517 * Use defaults from config.h. The device drivers will often override
518 * some of these values (such as number of texture units).
521 _mesa_init_constants(GLcontext
*ctx
)
525 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
526 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
528 /* Max texture size should be <= max viewport size (render to texture) */
529 assert((1 << (MAX_TEXTURE_LEVELS
- 1)) <= MAX_WIDTH
);
531 /* Constants, may be overriden (usually only reduced) by device drivers */
532 ctx
->Const
.MaxTextureLevels
= MAX_TEXTURE_LEVELS
;
533 ctx
->Const
.Max3DTextureLevels
= MAX_3D_TEXTURE_LEVELS
;
534 ctx
->Const
.MaxCubeTextureLevels
= MAX_CUBE_TEXTURE_LEVELS
;
535 ctx
->Const
.MaxTextureRectSize
= MAX_TEXTURE_RECT_SIZE
;
536 ctx
->Const
.MaxArrayTextureLayers
= MAX_ARRAY_TEXTURE_LAYERS
;
537 ctx
->Const
.MaxTextureCoordUnits
= MAX_TEXTURE_COORD_UNITS
;
538 ctx
->Const
.MaxTextureImageUnits
= MAX_TEXTURE_IMAGE_UNITS
;
539 ctx
->Const
.MaxTextureUnits
= MIN2(ctx
->Const
.MaxTextureCoordUnits
,
540 ctx
->Const
.MaxTextureImageUnits
);
541 ctx
->Const
.MaxTextureMaxAnisotropy
= MAX_TEXTURE_MAX_ANISOTROPY
;
542 ctx
->Const
.MaxTextureLodBias
= MAX_TEXTURE_LOD_BIAS
;
543 ctx
->Const
.MaxArrayLockSize
= MAX_ARRAY_LOCK_SIZE
;
544 ctx
->Const
.SubPixelBits
= SUB_PIXEL_BITS
;
545 ctx
->Const
.MinPointSize
= MIN_POINT_SIZE
;
546 ctx
->Const
.MaxPointSize
= MAX_POINT_SIZE
;
547 ctx
->Const
.MinPointSizeAA
= MIN_POINT_SIZE
;
548 ctx
->Const
.MaxPointSizeAA
= MAX_POINT_SIZE
;
549 ctx
->Const
.PointSizeGranularity
= (GLfloat
) POINT_SIZE_GRANULARITY
;
550 ctx
->Const
.MinLineWidth
= MIN_LINE_WIDTH
;
551 ctx
->Const
.MaxLineWidth
= MAX_LINE_WIDTH
;
552 ctx
->Const
.MinLineWidthAA
= MIN_LINE_WIDTH
;
553 ctx
->Const
.MaxLineWidthAA
= MAX_LINE_WIDTH
;
554 ctx
->Const
.LineWidthGranularity
= (GLfloat
) LINE_WIDTH_GRANULARITY
;
555 ctx
->Const
.MaxColorTableSize
= MAX_COLOR_TABLE_SIZE
;
556 ctx
->Const
.MaxConvolutionWidth
= MAX_CONVOLUTION_WIDTH
;
557 ctx
->Const
.MaxConvolutionHeight
= MAX_CONVOLUTION_HEIGHT
;
558 ctx
->Const
.MaxClipPlanes
= MAX_CLIP_PLANES
;
559 ctx
->Const
.MaxLights
= MAX_LIGHTS
;
560 ctx
->Const
.MaxShininess
= 128.0;
561 ctx
->Const
.MaxSpotExponent
= 128.0;
562 ctx
->Const
.MaxViewportWidth
= MAX_WIDTH
;
563 ctx
->Const
.MaxViewportHeight
= MAX_HEIGHT
;
564 #if FEATURE_ARB_vertex_program
565 init_program_limits(GL_VERTEX_PROGRAM_ARB
, &ctx
->Const
.VertexProgram
);
567 #if FEATURE_ARB_fragment_program
568 init_program_limits(GL_FRAGMENT_PROGRAM_ARB
, &ctx
->Const
.FragmentProgram
);
570 ctx
->Const
.MaxProgramMatrices
= MAX_PROGRAM_MATRICES
;
571 ctx
->Const
.MaxProgramMatrixStackDepth
= MAX_PROGRAM_MATRIX_STACK_DEPTH
;
573 /* CheckArrayBounds is overriden by drivers/x11 for X server */
574 ctx
->Const
.CheckArrayBounds
= GL_FALSE
;
576 /* GL_ARB_draw_buffers */
577 ctx
->Const
.MaxDrawBuffers
= MAX_DRAW_BUFFERS
;
579 /* GL_OES_read_format */
580 ctx
->Const
.ColorReadFormat
= GL_RGBA
;
581 ctx
->Const
.ColorReadType
= GL_UNSIGNED_BYTE
;
583 #if FEATURE_EXT_framebuffer_object
584 ctx
->Const
.MaxColorAttachments
= MAX_COLOR_ATTACHMENTS
;
585 ctx
->Const
.MaxRenderbufferSize
= MAX_WIDTH
;
588 #if FEATURE_ARB_vertex_shader
589 ctx
->Const
.MaxVertexTextureImageUnits
= MAX_VERTEX_TEXTURE_IMAGE_UNITS
;
590 ctx
->Const
.MaxVarying
= MAX_VARYING
;
593 /* GL_ARB_framebuffer_object */
594 ctx
->Const
.MaxSamples
= 0;
597 ctx
->Const
.MaxServerWaitTimeout
= (GLuint64
) ~0;
599 /* GL_ATI_envmap_bumpmap */
600 ctx
->Const
.SupportedBumpUnits
= SUPPORTED_ATI_BUMP_UNITS
;
602 /* GL_EXT_provoking_vertex */
603 ctx
->Const
.QuadsFollowProvokingVertexConvention
= GL_TRUE
;
606 ASSERT(ctx
->Const
.MaxTextureUnits
== MIN2(ctx
->Const
.MaxTextureImageUnits
,
607 ctx
->Const
.MaxTextureCoordUnits
));
608 ASSERT(ctx
->Const
.FragmentProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
609 ASSERT(ctx
->Const
.VertexProgram
.MaxLocalParams
<= MAX_PROGRAM_LOCAL_PARAMS
);
611 ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
612 ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS
<= MAX_PROGRAM_TEMPS
);
613 ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS
<= VERT_ATTRIB_MAX
);
614 ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS
<= VERT_RESULT_MAX
);
616 /* check that we don't exceed various 32-bit bitfields */
617 ASSERT(VERT_RESULT_MAX
<= 32);
618 ASSERT(FRAG_ATTRIB_MAX
<= 32);
623 * Do some sanity checks on the limits/constants for the given context.
624 * Only called the first time a context is bound.
627 check_context_limits(GLcontext
*ctx
)
629 /* Many context limits/constants are limited by the size of
632 assert(ctx
->Const
.MaxTextureImageUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
633 assert(ctx
->Const
.MaxTextureCoordUnits
<= MAX_TEXTURE_COORD_UNITS
);
634 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_IMAGE_UNITS
);
635 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_COORD_UNITS
);
637 /* number of coord units cannot be greater than number of image units */
638 assert(ctx
->Const
.MaxTextureCoordUnits
<= ctx
->Const
.MaxTextureImageUnits
);
640 assert(ctx
->Const
.MaxTextureLevels
<= MAX_TEXTURE_LEVELS
);
641 assert(ctx
->Const
.Max3DTextureLevels
<= MAX_3D_TEXTURE_LEVELS
);
642 assert(ctx
->Const
.MaxCubeTextureLevels
<= MAX_CUBE_TEXTURE_LEVELS
);
643 assert(ctx
->Const
.MaxTextureRectSize
<= MAX_TEXTURE_RECT_SIZE
);
645 /* make sure largest texture image is <= MAX_WIDTH in size */
646 assert((1 << (ctx
->Const
.MaxTextureLevels
- 1)) <= MAX_WIDTH
);
647 assert((1 << (ctx
->Const
.MaxCubeTextureLevels
- 1)) <= MAX_WIDTH
);
648 assert((1 << (ctx
->Const
.Max3DTextureLevels
- 1)) <= MAX_WIDTH
);
650 assert(ctx
->Const
.MaxViewportWidth
<= MAX_WIDTH
);
651 assert(ctx
->Const
.MaxViewportHeight
<= MAX_WIDTH
);
653 assert(ctx
->Const
.MaxDrawBuffers
<= MAX_DRAW_BUFFERS
);
655 /* XXX probably add more tests */
660 * Initialize the attribute groups in a GL context.
662 * \param ctx GL context.
664 * Initializes all the attributes, calling the respective <tt>init*</tt>
665 * functions for the more complex data structures.
668 init_attrib_groups(GLcontext
*ctx
)
673 _mesa_init_constants( ctx
);
676 _mesa_init_extensions( ctx
);
678 /* Attribute Groups */
680 _mesa_init_accum( ctx
);
682 #if FEATURE_attrib_stack
683 _mesa_init_attrib( ctx
);
685 _mesa_init_buffer_objects( ctx
);
686 _mesa_init_color( ctx
);
687 _mesa_init_colortables( ctx
);
688 _mesa_init_current( ctx
);
689 _mesa_init_depth( ctx
);
690 _mesa_init_debug( ctx
);
692 _mesa_init_display_list( ctx
);
694 #if FEATURE_evaluators
695 _mesa_init_eval( ctx
);
697 _mesa_init_fbobjects( ctx
);
699 _mesa_init_feedback( ctx
);
701 ctx
->RenderMode
= GL_RENDER
;
703 _mesa_init_fog( ctx
);
704 #if FEATURE_histogram
705 _mesa_init_histogram( ctx
);
707 _mesa_init_hint( ctx
);
708 _mesa_init_line( ctx
);
709 _mesa_init_lighting( ctx
);
710 _mesa_init_matrix( ctx
);
711 _mesa_init_multisample( ctx
);
712 _mesa_init_pixel( ctx
);
713 _mesa_init_pixelstore( ctx
);
714 _mesa_init_point( ctx
);
715 _mesa_init_polygon( ctx
);
716 _mesa_init_program( ctx
);
717 #if FEATURE_ARB_occlusion_query
718 _mesa_init_query( ctx
);
721 _mesa_init_sync( ctx
);
724 _mesa_init_rastpos( ctx
);
726 _mesa_init_scissor( ctx
);
727 _mesa_init_shader_state( ctx
);
728 _mesa_init_stencil( ctx
);
729 _mesa_init_transform( ctx
);
730 _mesa_init_varray( ctx
);
731 _mesa_init_viewport( ctx
);
733 if (!_mesa_init_texture( ctx
))
736 #if FEATURE_texture_s3tc
737 _mesa_init_texture_s3tc( ctx
);
739 #if FEATURE_texture_fxt1
740 _mesa_init_texture_fxt1( ctx
);
744 ctx
->NewState
= _NEW_ALL
;
745 ctx
->ErrorValue
= (GLenum
) GL_NO_ERROR
;
746 ctx
->varying_vp_inputs
= ~0;
753 * Update default objects in a GL context with respect to shared state.
755 * \param ctx GL context.
757 * Removes references to old default objects, (texture objects, program
758 * objects, etc.) and changes to reference those from the current shared
762 update_default_objects(GLcontext
*ctx
)
766 _mesa_update_default_objects_program(ctx
);
767 _mesa_update_default_objects_texture(ctx
);
768 _mesa_update_default_objects_buffer_objects(ctx
);
775 * This is the default function we plug into all dispatch table slots
776 * This helps prevents a segfault when someone calls a GL function without
777 * first checking if the extension's supported.
782 _mesa_warning(NULL
, "User called no-op dispatch function (an unsupported extension function?)");
788 * Allocate and initialize a new dispatch table.
790 static struct _glapi_table
*
791 alloc_dispatch_table(void)
793 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
794 * In practice, this'll be the same for stand-alone Mesa. But for DRI
795 * Mesa we do this to accomodate different versions of libGL and various
798 GLint numEntries
= MAX2(_glapi_get_dispatch_table_size(),
799 sizeof(struct _glapi_table
) / sizeof(_glapi_proc
));
800 struct _glapi_table
*table
=
801 (struct _glapi_table
*) _mesa_malloc(numEntries
* sizeof(_glapi_proc
));
803 _glapi_proc
*entry
= (_glapi_proc
*) table
;
805 for (i
= 0; i
< numEntries
; i
++) {
806 entry
[i
] = (_glapi_proc
) generic_nop
;
814 * Initialize a GLcontext struct (rendering context).
816 * This includes allocating all the other structs and arrays which hang off of
817 * the context by pointers.
818 * Note that the driver needs to pass in its dd_function_table here since
819 * we need to at least call driverFunctions->NewTextureObject to create the
820 * default texture objects.
822 * Called by _mesa_create_context().
824 * Performs the imports and exports callback tables initialization, and
825 * miscellaneous one-time initializations. If no shared context is supplied one
826 * is allocated, and increase its reference count. Setups the GL API dispatch
827 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
828 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
831 * \param ctx the context to initialize
832 * \param visual describes the visual attributes for this context
833 * \param share_list points to context to share textures, display lists,
835 * \param driverFunctions table of device driver functions for this context
837 * \param driverContext pointer to driver-specific context data
840 _mesa_initialize_context(GLcontext
*ctx
,
841 const GLvisual
*visual
,
842 GLcontext
*share_list
,
843 const struct dd_function_table
*driverFunctions
,
846 struct gl_shared_state
*shared
;
848 /*ASSERT(driverContext);*/
849 assert(driverFunctions
->NewTextureObject
);
850 assert(driverFunctions
->FreeTexImageData
);
852 /* misc one-time initializations */
855 ctx
->Visual
= *visual
;
856 ctx
->DrawBuffer
= NULL
;
857 ctx
->ReadBuffer
= NULL
;
858 ctx
->WinSysDrawBuffer
= NULL
;
859 ctx
->WinSysReadBuffer
= NULL
;
861 /* Plug in driver functions and context pointer here.
862 * This is important because when we call alloc_shared_state() below
863 * we'll call ctx->Driver.NewTextureObject() to create the default
866 ctx
->Driver
= *driverFunctions
;
867 ctx
->DriverCtx
= driverContext
;
870 /* share state with another context */
871 shared
= share_list
->Shared
;
874 /* allocate new, unshared state */
875 shared
= _mesa_alloc_shared_state(ctx
);
880 _glthread_LOCK_MUTEX(shared
->Mutex
);
881 ctx
->Shared
= shared
;
883 _glthread_UNLOCK_MUTEX(shared
->Mutex
);
885 if (!init_attrib_groups( ctx
)) {
886 _mesa_free_shared_state(ctx
, ctx
->Shared
);
890 /* setup the API dispatch tables */
891 ctx
->Exec
= alloc_dispatch_table();
892 ctx
->Save
= alloc_dispatch_table();
893 if (!ctx
->Exec
|| !ctx
->Save
) {
894 _mesa_free_shared_state(ctx
, ctx
->Shared
);
896 _mesa_free(ctx
->Exec
);
899 _mesa_init_exec_table(ctx
->Exec
);
901 ctx
->CurrentDispatch
= ctx
->Exec
;
903 _mesa_init_dlist_table(ctx
->Save
);
904 _mesa_install_save_vtxfmt( ctx
, &ctx
->ListState
.ListVtxfmt
);
906 /* Neutral tnl module stuff */
907 _mesa_init_exec_vtxfmt( ctx
);
908 ctx
->TnlModule
.Current
= NULL
;
909 ctx
->TnlModule
.SwapCount
= 0;
911 ctx
->FragmentProgram
._MaintainTexEnvProgram
912 = (_mesa_getenv("MESA_TEX_PROG") != NULL
);
914 ctx
->VertexProgram
._MaintainTnlProgram
915 = (_mesa_getenv("MESA_TNL_PROG") != NULL
);
916 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
917 /* this is required... */
918 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
921 #ifdef FEATURE_extra_context_init
922 _mesa_initialize_context_extra(ctx
);
925 ctx
->FirstTimeCurrent
= GL_TRUE
;
932 * Allocate and initialize a GLcontext structure.
933 * Note that the driver needs to pass in its dd_function_table here since
934 * we need to at least call driverFunctions->NewTextureObject to initialize
935 * the rendering context.
937 * \param visual a GLvisual pointer (we copy the struct contents)
938 * \param share_list another context to share display lists with or NULL
939 * \param driverFunctions points to the dd_function_table into which the
940 * driver has plugged in all its special functions.
941 * \param driverContext points to the device driver's private context state
943 * \return pointer to a new __GLcontextRec or NULL if error.
946 _mesa_create_context(const GLvisual
*visual
,
947 GLcontext
*share_list
,
948 const struct dd_function_table
*driverFunctions
,
954 /*ASSERT(driverContext);*/
956 ctx
= (GLcontext
*) _mesa_calloc(sizeof(GLcontext
));
960 if (_mesa_initialize_context(ctx
, visual
, share_list
,
961 driverFunctions
, driverContext
)) {
972 * Free the data associated with the given context.
974 * But doesn't free the GLcontext struct itself.
976 * \sa _mesa_initialize_context() and init_attrib_groups().
979 _mesa_free_context_data( GLcontext
*ctx
)
983 if (!_mesa_get_current_context()){
984 /* No current context, but we may need one in order to delete
985 * texture objs, etc. So temporarily bind the context now.
987 _mesa_make_current(ctx
, NULL
, NULL
);
990 /* unreference WinSysDraw/Read buffers */
991 _mesa_reference_framebuffer(&ctx
->WinSysDrawBuffer
, NULL
);
992 _mesa_reference_framebuffer(&ctx
->WinSysReadBuffer
, NULL
);
993 _mesa_reference_framebuffer(&ctx
->DrawBuffer
, NULL
);
994 _mesa_reference_framebuffer(&ctx
->ReadBuffer
, NULL
);
996 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
997 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
998 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._TnlProgram
, NULL
);
1000 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
1001 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
1002 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
, NULL
);
1004 #if FEATURE_attrib_stack
1005 _mesa_free_attrib_data(ctx
);
1007 _mesa_free_lighting_data( ctx
);
1008 #if FEATURE_evaluators
1009 _mesa_free_eval_data( ctx
);
1011 _mesa_free_texture_data( ctx
);
1012 _mesa_free_matrix_data( ctx
);
1013 _mesa_free_viewport_data( ctx
);
1014 _mesa_free_colortables_data( ctx
);
1015 _mesa_free_program_data(ctx
);
1016 _mesa_free_shader_state(ctx
);
1017 #if FEATURE_ARB_occlusion_query
1018 _mesa_free_query_data(ctx
);
1020 #if FEATURE_ARB_sync
1021 _mesa_free_sync_data(ctx
);
1023 _mesa_free_varray_data(ctx
);
1025 _mesa_delete_array_object(ctx
, ctx
->Array
.DefaultArrayObj
);
1027 #if FEATURE_ARB_pixel_buffer_object
1028 _mesa_reference_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, NULL
);
1029 _mesa_reference_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, NULL
);
1032 #if FEATURE_ARB_vertex_buffer_object
1033 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
1034 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
1037 /* free dispatch tables */
1038 _mesa_free(ctx
->Exec
);
1039 _mesa_free(ctx
->Save
);
1041 /* Shared context state (display lists, textures, etc) */
1042 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1043 RefCount
= --ctx
->Shared
->RefCount
;
1044 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1045 assert(RefCount
>= 0);
1046 if (RefCount
== 0) {
1047 /* free shared state */
1048 _mesa_free_shared_state( ctx
, ctx
->Shared
);
1051 if (ctx
->Extensions
.String
)
1052 _mesa_free((void *) ctx
->Extensions
.String
);
1054 /* unbind the context if it's currently bound */
1055 if (ctx
== _mesa_get_current_context()) {
1056 _mesa_make_current(NULL
, NULL
, NULL
);
1062 * Destroy a GLcontext structure.
1064 * \param ctx GL context.
1066 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1069 _mesa_destroy_context( GLcontext
*ctx
)
1072 _mesa_free_context_data(ctx
);
1073 _mesa_free( (void *) ctx
);
1080 * Copy attribute groups from one context to another.
1082 * \param src source context
1083 * \param dst destination context
1084 * \param mask bitwise OR of GL_*_BIT flags
1086 * According to the bits specified in \p mask, copies the corresponding
1087 * attributes from \p src into \p dst. For many of the attributes a simple \c
1088 * memcpy is not enough due to the existence of internal pointers in their data
1092 _mesa_copy_context( const GLcontext
*src
, GLcontext
*dst
, GLuint mask
)
1094 if (mask
& GL_ACCUM_BUFFER_BIT
) {
1096 dst
->Accum
= src
->Accum
;
1098 if (mask
& GL_COLOR_BUFFER_BIT
) {
1100 dst
->Color
= src
->Color
;
1102 if (mask
& GL_CURRENT_BIT
) {
1104 dst
->Current
= src
->Current
;
1106 if (mask
& GL_DEPTH_BUFFER_BIT
) {
1108 dst
->Depth
= src
->Depth
;
1110 if (mask
& GL_ENABLE_BIT
) {
1113 if (mask
& GL_EVAL_BIT
) {
1115 dst
->Eval
= src
->Eval
;
1117 if (mask
& GL_FOG_BIT
) {
1119 dst
->Fog
= src
->Fog
;
1121 if (mask
& GL_HINT_BIT
) {
1123 dst
->Hint
= src
->Hint
;
1125 if (mask
& GL_LIGHTING_BIT
) {
1127 /* begin with memcpy */
1128 dst
->Light
= src
->Light
;
1129 /* fixup linked lists to prevent pointer insanity */
1130 make_empty_list( &(dst
->Light
.EnabledList
) );
1131 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1132 if (dst
->Light
.Light
[i
].Enabled
) {
1133 insert_at_tail(&(dst
->Light
.EnabledList
), &(dst
->Light
.Light
[i
]));
1137 if (mask
& GL_LINE_BIT
) {
1139 dst
->Line
= src
->Line
;
1141 if (mask
& GL_LIST_BIT
) {
1143 dst
->List
= src
->List
;
1145 if (mask
& GL_PIXEL_MODE_BIT
) {
1147 dst
->Pixel
= src
->Pixel
;
1149 if (mask
& GL_POINT_BIT
) {
1151 dst
->Point
= src
->Point
;
1153 if (mask
& GL_POLYGON_BIT
) {
1155 dst
->Polygon
= src
->Polygon
;
1157 if (mask
& GL_POLYGON_STIPPLE_BIT
) {
1158 /* Use loop instead of MEMCPY due to problem with Portland Group's
1159 * C compiler. Reported by John Stone.
1162 for (i
= 0; i
< 32; i
++) {
1163 dst
->PolygonStipple
[i
] = src
->PolygonStipple
[i
];
1166 if (mask
& GL_SCISSOR_BIT
) {
1168 dst
->Scissor
= src
->Scissor
;
1170 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1172 dst
->Stencil
= src
->Stencil
;
1174 if (mask
& GL_TEXTURE_BIT
) {
1175 /* Cannot memcpy because of pointers */
1176 _mesa_copy_texture_state(src
, dst
);
1178 if (mask
& GL_TRANSFORM_BIT
) {
1180 dst
->Transform
= src
->Transform
;
1182 if (mask
& GL_VIEWPORT_BIT
) {
1183 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1184 dst
->Viewport
.X
= src
->Viewport
.X
;
1185 dst
->Viewport
.Y
= src
->Viewport
.Y
;
1186 dst
->Viewport
.Width
= src
->Viewport
.Width
;
1187 dst
->Viewport
.Height
= src
->Viewport
.Height
;
1188 dst
->Viewport
.Near
= src
->Viewport
.Near
;
1189 dst
->Viewport
.Far
= src
->Viewport
.Far
;
1190 _math_matrix_copy(&dst
->Viewport
._WindowMap
, &src
->Viewport
._WindowMap
);
1193 /* XXX FIXME: Call callbacks?
1195 dst
->NewState
= _NEW_ALL
;
1201 * Check if the given context can render into the given framebuffer
1202 * by checking visual attributes.
1204 * Most of these tests could go away because Mesa is now pretty flexible
1205 * in terms of mixing rendering contexts with framebuffers. As long
1206 * as RGB vs. CI mode agree, we're probably good.
1208 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1211 check_compatible(const GLcontext
*ctx
, const GLframebuffer
*buffer
)
1213 const GLvisual
*ctxvis
= &ctx
->Visual
;
1214 const GLvisual
*bufvis
= &buffer
->Visual
;
1216 if (ctxvis
== bufvis
)
1219 if (ctxvis
->rgbMode
!= bufvis
->rgbMode
)
1222 /* disabling this fixes the fgl_glxgears pbuffer demo */
1223 if (ctxvis
->doubleBufferMode
&& !bufvis
->doubleBufferMode
)
1226 if (ctxvis
->stereoMode
&& !bufvis
->stereoMode
)
1228 if (ctxvis
->haveAccumBuffer
&& !bufvis
->haveAccumBuffer
)
1230 if (ctxvis
->haveDepthBuffer
&& !bufvis
->haveDepthBuffer
)
1232 if (ctxvis
->haveStencilBuffer
&& !bufvis
->haveStencilBuffer
)
1234 if (ctxvis
->redMask
&& ctxvis
->redMask
!= bufvis
->redMask
)
1236 if (ctxvis
->greenMask
&& ctxvis
->greenMask
!= bufvis
->greenMask
)
1238 if (ctxvis
->blueMask
&& ctxvis
->blueMask
!= bufvis
->blueMask
)
1241 /* disabled (see bug 11161) */
1242 if (ctxvis
->depthBits
&& ctxvis
->depthBits
!= bufvis
->depthBits
)
1245 if (ctxvis
->stencilBits
&& ctxvis
->stencilBits
!= bufvis
->stencilBits
)
1253 * Do one-time initialization for the given framebuffer. Specifically,
1254 * ask the driver for the window's current size and update the framebuffer
1256 * Really, the device driver should totally take care of this.
1259 initialize_framebuffer_size(GLcontext
*ctx
, GLframebuffer
*fb
)
1261 GLuint width
, height
;
1262 if (ctx
->Driver
.GetBufferSize
) {
1263 ctx
->Driver
.GetBufferSize(fb
, &width
, &height
);
1264 if (ctx
->Driver
.ResizeBuffers
)
1265 ctx
->Driver
.ResizeBuffers(ctx
, fb
, width
, height
);
1266 fb
->Initialized
= GL_TRUE
;
1272 * Check if the viewport/scissor size has not yet been initialized.
1273 * Initialize the size if the given width and height are non-zero.
1276 _mesa_check_init_viewport(GLcontext
*ctx
, GLuint width
, GLuint height
)
1278 if (!ctx
->ViewportInitialized
&& width
> 0 && height
> 0) {
1279 /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
1280 * potential infinite recursion.
1282 ctx
->ViewportInitialized
= GL_TRUE
;
1283 _mesa_set_viewport(ctx
, 0, 0, width
, height
);
1284 _mesa_set_scissor(ctx
, 0, 0, width
, height
);
1290 * Bind the given context to the given drawBuffer and readBuffer and
1291 * make it the current context for the calling thread.
1292 * We'll render into the drawBuffer and read pixels from the
1293 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1295 * We check that the context's and framebuffer's visuals are compatible
1296 * and return immediately if they're not.
1298 * \param newCtx the new GL context. If NULL then there will be no current GL
1300 * \param drawBuffer the drawing framebuffer
1301 * \param readBuffer the reading framebuffer
1304 _mesa_make_current( GLcontext
*newCtx
, GLframebuffer
*drawBuffer
,
1305 GLframebuffer
*readBuffer
)
1307 if (MESA_VERBOSE
& VERBOSE_API
)
1308 _mesa_debug(newCtx
, "_mesa_make_current()\n");
1310 /* Check that the context's and framebuffer's visuals are compatible.
1312 if (newCtx
&& drawBuffer
&& newCtx
->WinSysDrawBuffer
!= drawBuffer
) {
1313 if (!check_compatible(newCtx
, drawBuffer
)) {
1314 _mesa_warning(newCtx
,
1315 "MakeCurrent: incompatible visuals for context and drawbuffer");
1319 if (newCtx
&& readBuffer
&& newCtx
->WinSysReadBuffer
!= readBuffer
) {
1320 if (!check_compatible(newCtx
, readBuffer
)) {
1321 _mesa_warning(newCtx
,
1322 "MakeCurrent: incompatible visuals for context and readbuffer");
1327 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1328 _glapi_set_context((void *) newCtx
);
1329 ASSERT(_mesa_get_current_context() == newCtx
);
1332 _glapi_set_dispatch(NULL
); /* none current */
1335 _glapi_set_dispatch(newCtx
->CurrentDispatch
);
1337 if (drawBuffer
&& readBuffer
) {
1338 /* TODO: check if newCtx and buffer's visual match??? */
1340 ASSERT(drawBuffer
->Name
== 0);
1341 ASSERT(readBuffer
->Name
== 0);
1342 _mesa_reference_framebuffer(&newCtx
->WinSysDrawBuffer
, drawBuffer
);
1343 _mesa_reference_framebuffer(&newCtx
->WinSysReadBuffer
, readBuffer
);
1346 * Only set the context's Draw/ReadBuffer fields if they're NULL
1347 * or not bound to a user-created FBO.
1349 if (!newCtx
->DrawBuffer
|| newCtx
->DrawBuffer
->Name
== 0) {
1350 /* KW: merge conflict here, revisit.
1352 /* fix up the fb fields - these will end up wrong otherwise
1353 * if the DRIdrawable changes, and everything relies on them.
1354 * This is a bit messy (same as needed in _mesa_BindFramebufferEXT)
1357 GLenum buffers
[MAX_DRAW_BUFFERS
];
1359 _mesa_reference_framebuffer(&newCtx
->DrawBuffer
, drawBuffer
);
1361 for(i
= 0; i
< newCtx
->Const
.MaxDrawBuffers
; i
++) {
1362 buffers
[i
] = newCtx
->Color
.DrawBuffer
[i
];
1365 _mesa_drawbuffers(newCtx
, newCtx
->Const
.MaxDrawBuffers
, buffers
, NULL
);
1367 if (!newCtx
->ReadBuffer
|| newCtx
->ReadBuffer
->Name
== 0) {
1368 _mesa_reference_framebuffer(&newCtx
->ReadBuffer
, readBuffer
);
1371 /* XXX only set this flag if we're really changing the draw/read
1372 * framebuffer bindings.
1374 newCtx
->NewState
|= _NEW_BUFFERS
;
1377 /* We want to get rid of these lines: */
1380 if (!drawBuffer
->Initialized
) {
1381 initialize_framebuffer_size(newCtx
, drawBuffer
);
1383 if (readBuffer
!= drawBuffer
&& !readBuffer
->Initialized
) {
1384 initialize_framebuffer_size(newCtx
, readBuffer
);
1387 _mesa_resizebuffers(newCtx
);
1391 /* We want the drawBuffer and readBuffer to be initialized by
1393 * This generally means the Width and Height match the actual
1394 * window size and the renderbuffers (both hardware and software
1395 * based) are allocated to match. The later can generally be
1396 * done with a call to _mesa_resize_framebuffer().
1398 * It's theoretically possible for a buffer to have zero width
1399 * or height, but for now, assert check that the driver did what's
1402 ASSERT(drawBuffer
->Width
> 0);
1403 ASSERT(drawBuffer
->Height
> 0);
1407 _mesa_check_init_viewport(newCtx
,
1408 drawBuffer
->Width
, drawBuffer
->Height
);
1412 if (newCtx
->FirstTimeCurrent
) {
1413 check_context_limits(newCtx
);
1415 /* We can use this to help debug user's problems. Tell them to set
1416 * the MESA_INFO env variable before running their app. Then the
1417 * first time each context is made current we'll print some useful
1420 if (_mesa_getenv("MESA_INFO")) {
1424 newCtx
->FirstTimeCurrent
= GL_FALSE
;
1433 * Make context 'ctx' share the display lists, textures and programs
1434 * that are associated with 'ctxToShare'.
1435 * Any display lists, textures or programs associated with 'ctx' will
1436 * be deleted if nobody else is sharing them.
1439 _mesa_share_state(GLcontext
*ctx
, GLcontext
*ctxToShare
)
1441 if (ctx
&& ctxToShare
&& ctx
->Shared
&& ctxToShare
->Shared
) {
1442 struct gl_shared_state
*oldSharedState
= ctx
->Shared
;
1445 ctx
->Shared
= ctxToShare
->Shared
;
1447 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1448 ctx
->Shared
->RefCount
++;
1449 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1451 update_default_objects(ctx
);
1453 _glthread_LOCK_MUTEX(oldSharedState
->Mutex
);
1454 RefCount
= --oldSharedState
->RefCount
;
1455 _glthread_UNLOCK_MUTEX(oldSharedState
->Mutex
);
1457 if (RefCount
== 0) {
1458 _mesa_free_shared_state(ctx
, oldSharedState
);
1471 * \return pointer to the current GL context for this thread.
1473 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1474 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1478 _mesa_get_current_context( void )
1480 return (GLcontext
*) _glapi_get_context();
1485 * Get context's current API dispatch table.
1487 * It'll either be the immediate-mode execute dispatcher or the display list
1488 * compile dispatcher.
1490 * \param ctx GL context.
1492 * \return pointer to dispatch_table.
1494 * Simply returns __GLcontextRec::CurrentDispatch.
1496 struct _glapi_table
*
1497 _mesa_get_dispatch(GLcontext
*ctx
)
1499 return ctx
->CurrentDispatch
;
1505 /**********************************************************************/
1506 /** \name Miscellaneous functions */
1507 /**********************************************************************/
1513 * \param ctx GL context.
1514 * \param error error code.
1516 * Records the given error code and call the driver's dd_function_table::Error
1517 * function if defined.
1520 * This is called via _mesa_error().
1523 _mesa_record_error(GLcontext
*ctx
, GLenum error
)
1528 if (ctx
->ErrorValue
== GL_NO_ERROR
) {
1529 ctx
->ErrorValue
= error
;
1532 /* Call device driver's error handler, if any. This is used on the Mac. */
1533 if (ctx
->Driver
.Error
) {
1534 ctx
->Driver
.Error(ctx
);
1540 * Execute glFinish().
1542 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1543 * dd_function_table::Finish driver callback, if not NULL.
1548 GET_CURRENT_CONTEXT(ctx
);
1549 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1550 FLUSH_CURRENT( ctx
, 0 );
1551 if (ctx
->Driver
.Finish
) {
1552 ctx
->Driver
.Finish(ctx
);
1558 * Execute glFlush().
1560 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1561 * dd_function_table::Flush driver callback, if not NULL.
1566 GET_CURRENT_CONTEXT(ctx
);
1567 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1568 FLUSH_CURRENT( ctx
, 0 );
1569 if (ctx
->Driver
.Flush
) {
1570 ctx
->Driver
.Flush(ctx
);
1576 * Set mvp_with_dp4 flag. If a driver has a preference for DP4 over
1577 * MUL/MAD, or vice versa, call this function to register that.
1578 * Otherwise we default to MUL/MAD.
1581 _mesa_set_mvp_with_dp4( GLcontext
*ctx
,
1584 ctx
->mvp_with_dp4
= flag
;
1590 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
1591 * is called to see if it's valid to render. This involves checking that
1592 * the current shader is valid and the framebuffer is complete.
1593 * If an error is detected it'll be recorded here.
1594 * \return GL_TRUE if OK to render, GL_FALSE if not
1597 _mesa_valid_to_render(GLcontext
*ctx
, const char *where
)
1599 if (ctx
->Shader
.CurrentProgram
) {
1601 if (!ctx
->Shader
.CurrentProgram
->LinkStatus
) {
1602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1603 "%s(shader not linked), where");
1606 #if 0 /* not normally enabled */
1609 if (!_mesa_validate_shader_program(ctx
, ctx
->Shader
.CurrentProgram
,
1611 _mesa_warning(ctx
, "Shader program %u is invalid: %s",
1612 ctx
->Shader
.CurrentProgram
->Name
, errMsg
);
1618 if (ctx
->VertexProgram
.Enabled
&& !ctx
->VertexProgram
._Enabled
) {
1619 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1620 "%s(vertex program not valid)", where
);
1623 if (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
) {
1624 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1625 "%s(fragment program not valid)", where
);
1630 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1631 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1632 "%s(incomplete framebuffer)", where
);
1637 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
1638 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
1640 if (!shProg
->_Used
) {
1641 /* This is the first time this shader is being used.
1642 * Append shader's constants/uniforms to log file.
1645 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
1646 struct gl_shader
*sh
= shProg
->Shaders
[i
];
1647 if (sh
->Type
== GL_VERTEX_SHADER
) {
1648 _mesa_append_uniforms_to_file(sh
,
1649 &shProg
->VertexProgram
->Base
);
1651 else if (sh
->Type
== GL_FRAGMENT_SHADER
) {
1652 _mesa_append_uniforms_to_file(sh
,
1653 &shProg
->FragmentProgram
->Base
);
1656 shProg
->_Used
= GL_TRUE
;