ae1c576248e9f65e62fe6bc73b5b942b285a7125
[mesa.git] / src / mesa / main / context.c
1 /**
2 * \file context.c
3 * Mesa context/visual/framebuffer management functions.
4 * \author Brian Paul
5 */
6
7 /*
8 * Mesa 3-D graphics library
9 * Version: 6.5
10 *
11 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included
21 * in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
27 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32 /**
33 * \mainpage Mesa Main Module
34 *
35 * \section MainIntroduction Introduction
36 *
37 * The Mesa Main module consists of all the files in the main/ directory.
38 * Among the features of this module are:
39 * <UL>
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>
45 * </UL>
46 *
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.
50 *
51 *
52 * \section AboutDoxygen About Doxygen
53 *
54 * If you're viewing this information as Doxygen-generated HTML you'll
55 * see the documentation index at the top of this page.
56 *
57 * The first line lists the Mesa source code modules.
58 * The second line lists the indexes available for viewing the documentation
59 * for each module.
60 *
61 * Selecting the <b>Main page</b> link will display a summary of the module
62 * (this page).
63 *
64 * Selecting <b>Data Structures</b> will list all C structures.
65 *
66 * Selecting the <b>File List</b> link will list all the source files in
67 * the module.
68 * Selecting a filename will show a list of all functions defined in that file.
69 *
70 * Selecting the <b>Data Fields</b> link will display a list of all
71 * documented structure members.
72 *
73 * Selecting the <b>Globals</b> link will display a list
74 * of all functions, structures, global variables and macros in the module.
75 *
76 */
77
78
79 #include "glheader.h"
80 #include "imports.h"
81 #include "accum.h"
82 #include "arrayobj.h"
83 #include "attrib.h"
84 #include "blend.h"
85 #include "buffers.h"
86 #include "bufferobj.h"
87 #include "colortab.h"
88 #include "context.h"
89 #include "debug.h"
90 #include "depth.h"
91 #include "dlist.h"
92 #include "eval.h"
93 #include "enums.h"
94 #include "extensions.h"
95 #include "fbobject.h"
96 #include "feedback.h"
97 #include "fog.h"
98 #include "framebuffer.h"
99 #include "get.h"
100 #include "glthread.h"
101 #include "glapioffsets.h"
102 #include "histogram.h"
103 #include "hint.h"
104 #include "hash.h"
105 #include "atifragshader.h"
106 #include "light.h"
107 #include "lines.h"
108 #include "macros.h"
109 #include "matrix.h"
110 #include "occlude.h"
111 #include "pixel.h"
112 #include "points.h"
113 #include "polygon.h"
114 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
115 #include "program.h"
116 #endif
117 #include "rastpos.h"
118 #include "simple_list.h"
119 #include "state.h"
120 #include "stencil.h"
121 #include "texcompress.h"
122 #include "teximage.h"
123 #include "texobj.h"
124 #include "texstate.h"
125 #include "mtypes.h"
126 #include "varray.h"
127 #include "version.h"
128 #include "vtxfmt.h"
129 #if _HAVE_FULL_GL
130 #include "math/m_translate.h"
131 #include "math/m_matrix.h"
132 #include "math/m_xform.h"
133 #include "math/mathmod.h"
134 #endif
135 #include "shader_api.h"
136
137 #ifdef USE_SPARC_ASM
138 #include "sparc/sparc.h"
139 #endif
140
141 #ifndef MESA_VERBOSE
142 int MESA_VERBOSE = 0;
143 #endif
144
145 #ifndef MESA_DEBUG_FLAGS
146 int MESA_DEBUG_FLAGS = 0;
147 #endif
148
149
150 /* ubyte -> float conversion */
151 GLfloat _mesa_ubyte_to_float_color_tab[256];
152
153 static void
154 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
155
156
157 /**********************************************************************/
158 /** \name OpenGL SI-style interface (new in Mesa 3.5)
159 *
160 * \if subset
161 * \note Most of these functions are never called in the Mesa subset.
162 * \endif
163 */
164 /*@{*/
165
166 /**
167 * Destroy context callback.
168 *
169 * \param gc context.
170 * \return GL_TRUE on success, or GL_FALSE on failure.
171 *
172 * \ifnot subset
173 * Called by window system/device driver (via __GLexports::destroyCurrent) when
174 * the rendering context is to be destroyed.
175 * \endif
176 *
177 * Frees the context data and the context structure.
178 */
179 GLboolean
180 _mesa_destroyContext(__GLcontext *gc)
181 {
182 if (gc) {
183 _mesa_free_context_data(gc);
184 _mesa_free(gc);
185 }
186 return GL_TRUE;
187 }
188
189 /**
190 * Unbind context callback.
191 *
192 * \param gc context.
193 * \return GL_TRUE on success, or GL_FALSE on failure.
194 *
195 * \ifnot subset
196 * Called by window system/device driver (via __GLexports::loseCurrent)
197 * when the rendering context is made non-current.
198 * \endif
199 *
200 * No-op
201 */
202 GLboolean
203 _mesa_loseCurrent(__GLcontext *gc)
204 {
205 /* XXX unbind context from thread */
206 (void) gc;
207 return GL_TRUE;
208 }
209
210 /**
211 * Bind context callback.
212 *
213 * \param gc context.
214 * \return GL_TRUE on success, or GL_FALSE on failure.
215 *
216 * \ifnot subset
217 * Called by window system/device driver (via __GLexports::makeCurrent)
218 * when the rendering context is made current.
219 * \endif
220 *
221 * No-op
222 */
223 GLboolean
224 _mesa_makeCurrent(__GLcontext *gc)
225 {
226 /* XXX bind context to thread */
227 (void) gc;
228 return GL_TRUE;
229 }
230
231 /**
232 * Share context callback.
233 *
234 * \param gc context.
235 * \param gcShare shared context.
236 * \return GL_TRUE on success, or GL_FALSE on failure.
237 *
238 * \ifnot subset
239 * Called by window system/device driver (via __GLexports::shareContext)
240 * \endif
241 *
242 * Update the shared context reference count, gl_shared_state::RefCount.
243 */
244 GLboolean
245 _mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
246 {
247 if (gc && gcShare && gc->Shared && gcShare->Shared) {
248 gc->Shared->RefCount--;
249 if (gc->Shared->RefCount == 0) {
250 free_shared_state(gc, gc->Shared);
251 }
252 gc->Shared = gcShare->Shared;
253 gc->Shared->RefCount++;
254 return GL_TRUE;
255 }
256 else {
257 return GL_FALSE;
258 }
259 }
260
261
262 #if _HAVE_FULL_GL
263 /**
264 * Copy context callback.
265 */
266 GLboolean
267 _mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
268 {
269 if (dst && src) {
270 _mesa_copy_context( src, dst, mask );
271 return GL_TRUE;
272 }
273 else {
274 return GL_FALSE;
275 }
276 }
277 #endif
278
279 /** No-op */
280 GLboolean
281 _mesa_forceCurrent(__GLcontext *gc)
282 {
283 (void) gc;
284 return GL_TRUE;
285 }
286
287 /**
288 * Windows/buffer resizing notification callback.
289 *
290 * \param gc GL context.
291 * \return GL_TRUE on success, or GL_FALSE on failure.
292 */
293 GLboolean
294 _mesa_notifyResize(__GLcontext *gc)
295 {
296 /* update viewport, resize software buffers, etc. */
297 (void) gc;
298 return GL_TRUE;
299 }
300
301 /**
302 * Window/buffer destruction notification callback.
303 *
304 * \param gc GL context.
305 *
306 * Called when the context's window/buffer is going to be destroyed.
307 *
308 * No-op
309 */
310 void
311 _mesa_notifyDestroy(__GLcontext *gc)
312 {
313 /* Unbind from it. */
314 (void) gc;
315 }
316
317 /**
318 * Swap buffers notification callback.
319 *
320 * \param gc GL context.
321 *
322 * Called by window system just before swapping buffers.
323 * We have to finish any pending rendering.
324 */
325 void
326 _mesa_notifySwapBuffers(__GLcontext *gc)
327 {
328 FLUSH_VERTICES( gc, 0 );
329 }
330
331 /** No-op */
332 struct __GLdispatchStateRec *
333 _mesa_dispatchExec(__GLcontext *gc)
334 {
335 (void) gc;
336 return NULL;
337 }
338
339 /** No-op */
340 void
341 _mesa_beginDispatchOverride(__GLcontext *gc)
342 {
343 (void) gc;
344 }
345
346 /** No-op */
347 void
348 _mesa_endDispatchOverride(__GLcontext *gc)
349 {
350 (void) gc;
351 }
352
353 /**
354 * \ifnot subset
355 * Setup the exports.
356 *
357 * The window system will call these functions when it needs Mesa to do
358 * something.
359 *
360 * \note Device drivers should override these functions! For example,
361 * the Xlib driver should plug in the XMesa*-style functions into this
362 * structure. The XMesa-style functions should then call the _mesa_*
363 * version of these functions. This is an approximation to OO design
364 * (inheritance and virtual functions).
365 * \endif
366 *
367 * \if subset
368 * No-op.
369 *
370 * \endif
371 */
372 static void
373 _mesa_init_default_exports(__GLexports *exports)
374 {
375 #if _HAVE_FULL_GL
376 exports->destroyContext = _mesa_destroyContext;
377 exports->loseCurrent = _mesa_loseCurrent;
378 exports->makeCurrent = _mesa_makeCurrent;
379 exports->shareContext = _mesa_shareContext;
380 exports->copyContext = _mesa_copyContext;
381 exports->forceCurrent = _mesa_forceCurrent;
382 exports->notifyResize = _mesa_notifyResize;
383 exports->notifyDestroy = _mesa_notifyDestroy;
384 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
385 exports->dispatchExec = _mesa_dispatchExec;
386 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
387 exports->endDispatchOverride = _mesa_endDispatchOverride;
388 #else
389 (void) exports;
390 #endif
391 }
392
393 /**
394 * Exported OpenGL SI interface.
395 */
396 __GLcontext *
397 __glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
398 {
399 GLcontext *ctx;
400
401 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
402 if (ctx == NULL) {
403 return NULL;
404 }
405
406 /* XXX doesn't work at this time */
407 _mesa_initialize_context(ctx, modes, NULL, NULL, NULL);
408 ctx->imports = *imports;
409
410 return ctx;
411 }
412
413 /**
414 * Exported OpenGL SI interface.
415 */
416 void
417 __glCoreNopDispatch(void)
418 {
419 #if 0
420 /* SI */
421 __gl_dispatch = __glNopDispatchState;
422 #else
423 /* Mesa */
424 _glapi_set_dispatch(NULL);
425 #endif
426 }
427
428 /*@}*/
429
430
431 /**********************************************************************/
432 /** \name GL Visual allocation/destruction */
433 /**********************************************************************/
434 /*@{*/
435
436 /**
437 * Allocates a GLvisual structure and initializes it via
438 * _mesa_initialize_visual().
439 *
440 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
441 * \param dbFlag double buffering
442 * \param stereoFlag stereo buffer
443 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
444 * is acceptable but the actual depth type will be GLushort or GLuint as
445 * needed.
446 * \param stencilBits requested minimum bits per stencil buffer value
447 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
448 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
449 * \param redBits number of bits per color component in frame buffer for RGB(A)
450 * mode. We always use 8 in core Mesa though.
451 * \param greenBits same as above.
452 * \param blueBits same as above.
453 * \param alphaBits same as above.
454 * \param numSamples not really used.
455 *
456 * \return pointer to new GLvisual or NULL if requested parameters can't be
457 * met.
458 *
459 * \note Need to add params for level and numAuxBuffers (at least)
460 */
461 GLvisual *
462 _mesa_create_visual( GLboolean rgbFlag,
463 GLboolean dbFlag,
464 GLboolean stereoFlag,
465 GLint redBits,
466 GLint greenBits,
467 GLint blueBits,
468 GLint alphaBits,
469 GLint indexBits,
470 GLint depthBits,
471 GLint stencilBits,
472 GLint accumRedBits,
473 GLint accumGreenBits,
474 GLint accumBlueBits,
475 GLint accumAlphaBits,
476 GLint numSamples )
477 {
478 GLvisual *vis = (GLvisual *) _mesa_calloc(sizeof(GLvisual));
479 if (vis) {
480 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
481 redBits, greenBits, blueBits, alphaBits,
482 indexBits, depthBits, stencilBits,
483 accumRedBits, accumGreenBits,
484 accumBlueBits, accumAlphaBits,
485 numSamples)) {
486 _mesa_free(vis);
487 return NULL;
488 }
489 }
490 return vis;
491 }
492
493 /**
494 * Makes some sanity checks and fills in the fields of the
495 * GLvisual object with the given parameters. If the caller needs
496 * to set additional fields, he should just probably init the whole GLvisual
497 * object himself.
498 * \return GL_TRUE on success, or GL_FALSE on failure.
499 *
500 * \sa _mesa_create_visual() above for the parameter description.
501 */
502 GLboolean
503 _mesa_initialize_visual( GLvisual *vis,
504 GLboolean rgbFlag,
505 GLboolean dbFlag,
506 GLboolean stereoFlag,
507 GLint redBits,
508 GLint greenBits,
509 GLint blueBits,
510 GLint alphaBits,
511 GLint indexBits,
512 GLint depthBits,
513 GLint stencilBits,
514 GLint accumRedBits,
515 GLint accumGreenBits,
516 GLint accumBlueBits,
517 GLint accumAlphaBits,
518 GLint numSamples )
519 {
520 assert(vis);
521
522 if (depthBits < 0 || depthBits > 32) {
523 return GL_FALSE;
524 }
525 if (stencilBits < 0 || stencilBits > STENCIL_BITS) {
526 return GL_FALSE;
527 }
528 assert(accumRedBits >= 0);
529 assert(accumGreenBits >= 0);
530 assert(accumBlueBits >= 0);
531 assert(accumAlphaBits >= 0);
532
533 vis->rgbMode = rgbFlag;
534 vis->doubleBufferMode = dbFlag;
535 vis->stereoMode = stereoFlag;
536
537 vis->redBits = redBits;
538 vis->greenBits = greenBits;
539 vis->blueBits = blueBits;
540 vis->alphaBits = alphaBits;
541 vis->rgbBits = redBits + greenBits + blueBits;
542
543 vis->indexBits = indexBits;
544 vis->depthBits = depthBits;
545 vis->stencilBits = stencilBits;
546
547 vis->accumRedBits = accumRedBits;
548 vis->accumGreenBits = accumGreenBits;
549 vis->accumBlueBits = accumBlueBits;
550 vis->accumAlphaBits = accumAlphaBits;
551
552 vis->haveAccumBuffer = accumRedBits > 0;
553 vis->haveDepthBuffer = depthBits > 0;
554 vis->haveStencilBuffer = stencilBits > 0;
555
556 vis->numAuxBuffers = 0;
557 vis->level = 0;
558 vis->pixmapMode = 0;
559 vis->sampleBuffers = numSamples > 0 ? 1 : 0;
560 vis->samples = numSamples;
561
562 return GL_TRUE;
563 }
564
565
566 /**
567 * Destroy a visual and free its memory.
568 *
569 * \param vis visual.
570 *
571 * Frees the visual structure.
572 */
573 void
574 _mesa_destroy_visual( GLvisual *vis )
575 {
576 _mesa_free(vis);
577 }
578
579 /*@}*/
580
581
582 /**********************************************************************/
583 /** \name Context allocation, initialization, destroying
584 *
585 * The purpose of the most initialization functions here is to provide the
586 * default state values according to the OpenGL specification.
587 */
588 /**********************************************************************/
589 /*@{*/
590
591 /**
592 * One-time initialization mutex lock.
593 *
594 * \sa Used by one_time_init().
595 */
596 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
597
598 /**
599 * Calls all the various one-time-init functions in Mesa.
600 *
601 * While holding a global mutex lock, calls several initialization functions,
602 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
603 * defined.
604 *
605 * \sa _math_init().
606 */
607 static void
608 one_time_init( GLcontext *ctx )
609 {
610 static GLboolean alreadyCalled = GL_FALSE;
611 (void) ctx;
612 _glthread_LOCK_MUTEX(OneTimeLock);
613 if (!alreadyCalled) {
614 GLuint i;
615
616 /* do some implementation tests */
617 assert( sizeof(GLbyte) == 1 );
618 assert( sizeof(GLubyte) == 1 );
619 assert( sizeof(GLshort) == 2 );
620 assert( sizeof(GLushort) == 2 );
621 assert( sizeof(GLint) == 4 );
622 assert( sizeof(GLuint) == 4 );
623
624 #if _HAVE_FULL_GL
625 _math_init();
626
627 for (i = 0; i < 256; i++) {
628 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
629 }
630 #endif
631
632 #ifdef USE_SPARC_ASM
633 _mesa_init_sparc_glapi_relocs();
634 #endif
635 if (_mesa_getenv("MESA_DEBUG")) {
636 _glapi_noop_enable_warnings(GL_TRUE);
637 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
638 }
639 else {
640 _glapi_noop_enable_warnings(GL_FALSE);
641 }
642
643 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
644 _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
645 MESA_VERSION_STRING, __DATE__, __TIME__);
646 #endif
647
648 alreadyCalled = GL_TRUE;
649 }
650 _glthread_UNLOCK_MUTEX(OneTimeLock);
651 }
652
653
654 /**
655 * Allocate and initialize a shared context state structure.
656 * Initializes the display list, texture objects and vertex programs hash
657 * tables, allocates the texture objects. If it runs out of memory, frees
658 * everything already allocated before returning NULL.
659 *
660 * \return pointer to a gl_shared_state structure on success, or NULL on
661 * failure.
662 */
663 static GLboolean
664 alloc_shared_state( GLcontext *ctx )
665 {
666 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
667 if (!ss)
668 return GL_FALSE;
669
670 ctx->Shared = ss;
671
672 _glthread_INIT_MUTEX(ss->Mutex);
673
674 ss->DisplayList = _mesa_NewHashTable();
675 ss->TexObjects = _mesa_NewHashTable();
676 #if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
677 ss->Programs = _mesa_NewHashTable();
678 #endif
679
680 #if FEATURE_ARB_vertex_program
681 ss->DefaultVertexProgram = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
682 if (!ss->DefaultVertexProgram)
683 goto cleanup;
684 #endif
685 #if FEATURE_ARB_fragment_program
686 ss->DefaultFragmentProgram = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
687 if (!ss->DefaultFragmentProgram)
688 goto cleanup;
689 #endif
690 #if FEATURE_ATI_fragment_shader
691 ss->ATIShaders = _mesa_NewHashTable();
692 ss->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
693 if (!ss->DefaultFragmentShader)
694 goto cleanup;
695 #endif
696
697 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
698 ss->BufferObjects = _mesa_NewHashTable();
699 #endif
700
701 ss->ArrayObjects = _mesa_NewHashTable();
702
703 #if FEATURE_ARB_shader_objects
704 ss->ShaderObjects = _mesa_NewHashTable();
705 #endif
706
707 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
708 if (!ss->Default1D)
709 goto cleanup;
710
711 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
712 if (!ss->Default2D)
713 goto cleanup;
714
715 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
716 if (!ss->Default3D)
717 goto cleanup;
718
719 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
720 if (!ss->DefaultCubeMap)
721 goto cleanup;
722
723 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
724 if (!ss->DefaultRect)
725 goto cleanup;
726
727 /* Effectively bind the default textures to all texture units */
728 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
729 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
730 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
731 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
732 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
733
734 _glthread_INIT_MUTEX(ss->TexMutex);
735 ss->TextureStateStamp = 0;
736
737
738 #if FEATURE_EXT_framebuffer_object
739 ss->FrameBuffers = _mesa_NewHashTable();
740 if (!ss->FrameBuffers)
741 goto cleanup;
742 ss->RenderBuffers = _mesa_NewHashTable();
743 if (!ss->RenderBuffers)
744 goto cleanup;
745 #endif
746
747
748 return GL_TRUE;
749
750 cleanup:
751 /* Ran out of memory at some point. Free everything and return NULL */
752 if (ss->DisplayList)
753 _mesa_DeleteHashTable(ss->DisplayList);
754 if (ss->TexObjects)
755 _mesa_DeleteHashTable(ss->TexObjects);
756 #if FEATURE_NV_vertex_program
757 if (ss->Programs)
758 _mesa_DeleteHashTable(ss->Programs);
759 #endif
760 #if FEATURE_ARB_vertex_program
761 if (ss->DefaultVertexProgram)
762 ctx->Driver.DeleteProgram(ctx, ss->DefaultVertexProgram);
763 #endif
764 #if FEATURE_ARB_fragment_program
765 if (ss->DefaultFragmentProgram)
766 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentProgram);
767 #endif
768 #if FEATURE_ATI_fragment_shader
769 if (ss->DefaultFragmentShader)
770 _mesa_delete_ati_fragment_shader(ctx, ss->DefaultFragmentShader);
771 #endif
772 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
773 if (ss->BufferObjects)
774 _mesa_DeleteHashTable(ss->BufferObjects);
775 #endif
776
777 if (ss->ArrayObjects)
778 _mesa_DeleteHashTable (ss->ArrayObjects);
779
780 #if FEATURE_ARB_shader_objects
781 if (ss->ShaderObjects)
782 _mesa_DeleteHashTable (ss->ShaderObjects);
783 #endif
784
785 #if FEATURE_EXT_framebuffer_object
786 if (ss->FrameBuffers)
787 _mesa_DeleteHashTable(ss->FrameBuffers);
788 if (ss->RenderBuffers)
789 _mesa_DeleteHashTable(ss->RenderBuffers);
790 #endif
791
792 if (ss->Default1D)
793 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
794 if (ss->Default2D)
795 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
796 if (ss->Default3D)
797 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
798 if (ss->DefaultCubeMap)
799 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
800 if (ss->DefaultRect)
801 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
802 if (ss)
803 _mesa_free(ss);
804 return GL_FALSE;
805 }
806
807
808 /**
809 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
810 */
811 static void
812 delete_displaylist_cb(GLuint id, void *data, void *userData)
813 {
814 struct mesa_display_list *list = (struct mesa_display_list *) data;
815 GLcontext *ctx = (GLcontext *) userData;
816 _mesa_delete_list(ctx, list);
817 }
818
819 /**
820 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
821 */
822 static void
823 delete_texture_cb(GLuint id, void *data, void *userData)
824 {
825 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
826 GLcontext *ctx = (GLcontext *) userData;
827 ctx->Driver.DeleteTexture(ctx, texObj);
828 }
829
830 /**
831 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
832 */
833 static void
834 delete_program_cb(GLuint id, void *data, void *userData)
835 {
836 struct gl_program *prog = (struct gl_program *) data;
837 GLcontext *ctx = (GLcontext *) userData;
838 ctx->Driver.DeleteProgram(ctx, prog);
839 }
840
841 /**
842 * Callback for deleting an ATI fragment shader object.
843 * Called by _mesa_HashDeleteAll().
844 */
845 static void
846 delete_fragshader_cb(GLuint id, void *data, void *userData)
847 {
848 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
849 GLcontext *ctx = (GLcontext *) userData;
850 _mesa_delete_ati_fragment_shader(ctx, shader);
851 }
852
853 /**
854 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
855 */
856 static void
857 delete_bufferobj_cb(GLuint id, void *data, void *userData)
858 {
859 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
860 GLcontext *ctx = (GLcontext *) userData;
861 ctx->Driver.DeleteBuffer(ctx, bufObj);
862 }
863
864 /**
865 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
866 */
867 static void
868 delete_arrayobj_cb(GLuint id, void *data, void *userData)
869 {
870 struct gl_array_object *arrayObj = (struct gl_array_object *) data;
871 GLcontext *ctx = (GLcontext *) userData;
872 _mesa_delete_array_object(ctx, arrayObj);
873 }
874
875 /**
876 * Callback for deleting shader and shader programs objects.
877 * Called by _mesa_HashDeleteAll().
878 */
879 static void
880 delete_shader_cb(GLuint id, void *data, void *userData)
881 {
882 GLcontext *ctx = (GLcontext *) userData;
883 struct gl_shader *sh = (struct gl_shader *) data;
884 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
885 _mesa_free_shader(ctx, sh);
886 }
887 else {
888 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
889 ASSERT(shProg->Type == GL_SHADER_PROGRAM);
890 _mesa_free_shader_program(ctx, shProg);
891 }
892 }
893
894
895 /**
896 * Deallocate a shared state object and all children structures.
897 *
898 * \param ctx GL context.
899 * \param ss shared state pointer.
900 *
901 * Frees the display lists, the texture objects (calling the driver texture
902 * deletion callback to free its private data) and the vertex programs, as well
903 * as their hash tables.
904 *
905 * \sa alloc_shared_state().
906 */
907 static void
908 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
909 {
910 /*
911 * Free display lists
912 */
913 _mesa_HashDeleteAll(ss->DisplayList, delete_displaylist_cb, ctx);
914 _mesa_DeleteHashTable(ss->DisplayList);
915
916 /*
917 * Free texture objects
918 */
919 ASSERT(ctx->Driver.DeleteTexture);
920 /* the default textures */
921 ctx->Driver.DeleteTexture(ctx, ss->Default1D);
922 ctx->Driver.DeleteTexture(ctx, ss->Default2D);
923 ctx->Driver.DeleteTexture(ctx, ss->Default3D);
924 ctx->Driver.DeleteTexture(ctx, ss->DefaultCubeMap);
925 ctx->Driver.DeleteTexture(ctx, ss->DefaultRect);
926 /* all other textures */
927 _mesa_HashDeleteAll(ss->TexObjects, delete_texture_cb, ctx);
928 _mesa_DeleteHashTable(ss->TexObjects);
929
930 #if defined(FEATURE_NV_vertex_program) || defined(FEATURE_NV_fragment_program)
931 _mesa_HashDeleteAll(ss->Programs, delete_program_cb, ctx);
932 _mesa_DeleteHashTable(ss->Programs);
933 #endif
934 #if FEATURE_ARB_vertex_program
935 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
936 #endif
937 #if FEATURE_ARB_fragment_program
938 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
939 #endif
940
941 #if FEATURE_ATI_fragment_shader
942 _mesa_HashDeleteAll(ss->ATIShaders, delete_fragshader_cb, ctx);
943 _mesa_DeleteHashTable(ss->ATIShaders);
944 _mesa_delete_ati_fragment_shader(ctx, ss->DefaultFragmentShader);
945 #endif
946
947 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
948 _mesa_HashDeleteAll(ss->BufferObjects, delete_bufferobj_cb, ctx);
949 _mesa_DeleteHashTable(ss->BufferObjects);
950 #endif
951
952 _mesa_HashDeleteAll(ss->ArrayObjects, delete_arrayobj_cb, ctx);
953 _mesa_DeleteHashTable(ss->ArrayObjects);
954
955 #if FEATURE_ARB_shader_objects
956 _mesa_HashDeleteAll(ss->ShaderObjects, delete_shader_cb, ctx);
957 _mesa_DeleteHashTable(ss->ShaderObjects);
958 #endif
959
960 #if FEATURE_EXT_framebuffer_object
961 _mesa_DeleteHashTable(ss->FrameBuffers);
962 _mesa_DeleteHashTable(ss->RenderBuffers);
963 #endif
964
965 _glthread_DESTROY_MUTEX(ss->Mutex);
966
967 _mesa_free(ss);
968 }
969
970
971 /**
972 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
973 */
974 static void
975 _mesa_init_current(GLcontext *ctx)
976 {
977 GLuint i;
978
979 /* Init all to (0,0,0,1) */
980 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
981 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
982 }
983
984 /* redo special cases: */
985 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
986 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
987 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
988 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
989 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
990 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
991 }
992
993
994 /**
995 * Init vertex/fragment program native limits from logical limits.
996 */
997 static void
998 init_natives(struct gl_program_constants *prog)
999 {
1000 prog->MaxNativeInstructions = prog->MaxInstructions;
1001 prog->MaxNativeAluInstructions = prog->MaxAluInstructions;
1002 prog->MaxNativeTexInstructions = prog->MaxTexInstructions;
1003 prog->MaxNativeTexIndirections = prog->MaxTexIndirections;
1004 prog->MaxNativeAttribs = prog->MaxAttribs;
1005 prog->MaxNativeTemps = prog->MaxTemps;
1006 prog->MaxNativeAddressRegs = prog->MaxAddressRegs;
1007 prog->MaxNativeParameters = prog->MaxParameters;
1008 }
1009
1010
1011 /**
1012 * Initialize fields of gl_constants (aka ctx->Const.*).
1013 * Use defaults from config.h. The device drivers will often override
1014 * some of these values (such as number of texture units).
1015 */
1016 static void
1017 _mesa_init_constants(GLcontext *ctx)
1018 {
1019 assert(ctx);
1020
1021 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1022 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1023
1024 assert(MAX_TEXTURE_UNITS >= MAX_TEXTURE_COORD_UNITS);
1025 assert(MAX_TEXTURE_UNITS >= MAX_TEXTURE_IMAGE_UNITS);
1026
1027 /* Constants, may be overriden (usually only reduced) by device drivers */
1028 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
1029 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1030 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
1031 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
1032 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1033 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
1034 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureCoordUnits,
1035 ctx->Const.MaxTextureImageUnits);
1036 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
1037 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
1038 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
1039 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1040 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1041 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1042 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1043 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
1044 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
1045 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1046 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1047 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1048 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
1049 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
1050 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
1051 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1052 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
1053 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1054 ctx->Const.MaxLights = MAX_LIGHTS;
1055 ctx->Const.MaxShininess = 128.0;
1056 ctx->Const.MaxSpotExponent = 128.0;
1057 ctx->Const.MaxViewportWidth = MAX_WIDTH;
1058 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
1059 #if FEATURE_ARB_vertex_program
1060 ctx->Const.VertexProgram.MaxInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
1061 ctx->Const.VertexProgram.MaxAluInstructions = 0;
1062 ctx->Const.VertexProgram.MaxTexInstructions = 0;
1063 ctx->Const.VertexProgram.MaxTexIndirections = 0;
1064 ctx->Const.VertexProgram.MaxAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1065 ctx->Const.VertexProgram.MaxTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1066 ctx->Const.VertexProgram.MaxParameters = MAX_NV_VERTEX_PROGRAM_PARAMS;
1067 ctx->Const.VertexProgram.MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
1068 ctx->Const.VertexProgram.MaxEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1069 ctx->Const.VertexProgram.MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
1070 ctx->Const.VertexProgram.MaxUniformComponents = 4 * MAX_UNIFORMS;
1071 init_natives(&ctx->Const.VertexProgram);
1072 #endif
1073
1074 #if FEATURE_ARB_fragment_program
1075 ctx->Const.FragmentProgram.MaxInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
1076 ctx->Const.FragmentProgram.MaxAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1077 ctx->Const.FragmentProgram.MaxTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1078 ctx->Const.FragmentProgram.MaxTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
1079 ctx->Const.FragmentProgram.MaxAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1080 ctx->Const.FragmentProgram.MaxTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1081 ctx->Const.FragmentProgram.MaxParameters = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1082 ctx->Const.FragmentProgram.MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
1083 ctx->Const.FragmentProgram.MaxEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1084 ctx->Const.FragmentProgram.MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1085 ctx->Const.FragmentProgram.MaxUniformComponents = 4 * MAX_UNIFORMS;
1086 init_natives(&ctx->Const.FragmentProgram);
1087 #endif
1088 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1089 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
1090
1091 /* CheckArrayBounds is overriden by drivers/x11 for X server */
1092 ctx->Const.CheckArrayBounds = GL_FALSE;
1093
1094 /* GL_ARB_draw_buffers */
1095 ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
1096
1097 /* GL_OES_read_format */
1098 ctx->Const.ColorReadFormat = GL_RGBA;
1099 ctx->Const.ColorReadType = GL_UNSIGNED_BYTE;
1100
1101 #if FEATURE_EXT_framebuffer_object
1102 ctx->Const.MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
1103 ctx->Const.MaxRenderbufferSize = MAX_WIDTH;
1104 #endif
1105
1106 #if FEATURE_ARB_vertex_shader
1107 ctx->Const.MaxVertexTextureImageUnits = MAX_VERTEX_TEXTURE_IMAGE_UNITS;
1108 ctx->Const.MaxVarying = MAX_VARYING;
1109 #endif
1110
1111 /* sanity checks */
1112 ASSERT(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.MaxTextureImageUnits,
1113 ctx->Const.MaxTextureCoordUnits));
1114 ASSERT(ctx->Const.FragmentProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
1115 ASSERT(ctx->Const.VertexProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
1116
1117 ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
1118 ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
1119 ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS <= VERT_ATTRIB_MAX);
1120 ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS <= VERT_RESULT_MAX);
1121 }
1122
1123
1124 /**
1125 * Do some sanity checks on the limits/constants for the given context.
1126 * Only called the first time a context is bound.
1127 */
1128 static void
1129 check_context_limits(GLcontext *ctx)
1130 {
1131 /* Many context limits/constants are limited by the size of
1132 * internal arrays.
1133 */
1134 assert(ctx->Const.MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS);
1135 assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS);
1136 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS);
1137 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS);
1138
1139 assert(ctx->Const.MaxViewportWidth <= MAX_WIDTH);
1140 assert(ctx->Const.MaxViewportHeight <= MAX_WIDTH);
1141
1142 /* make sure largest texture image is <= MAX_WIDTH in size */
1143 assert((1 << (ctx->Const.MaxTextureLevels -1 )) <= MAX_WIDTH);
1144 assert((1 << (ctx->Const.MaxCubeTextureLevels -1 )) <= MAX_WIDTH);
1145 assert((1 << (ctx->Const.Max3DTextureLevels -1 )) <= MAX_WIDTH);
1146
1147 assert(ctx->Const.MaxDrawBuffers <= MAX_DRAW_BUFFERS);
1148
1149 /* XXX probably add more tests */
1150 }
1151
1152
1153 /**
1154 * Initialize the attribute groups in a GL context.
1155 *
1156 * \param ctx GL context.
1157 *
1158 * Initializes all the attributes, calling the respective <tt>init*</tt>
1159 * functions for the more complex data structures.
1160 */
1161 static GLboolean
1162 init_attrib_groups(GLcontext *ctx)
1163 {
1164 assert(ctx);
1165
1166 /* Constants */
1167 _mesa_init_constants( ctx );
1168
1169 /* Extensions */
1170 _mesa_init_extensions( ctx );
1171
1172 /* Attribute Groups */
1173 _mesa_init_accum( ctx );
1174 _mesa_init_attrib( ctx );
1175 _mesa_init_buffer_objects( ctx );
1176 _mesa_init_color( ctx );
1177 _mesa_init_colortables( ctx );
1178 _mesa_init_current( ctx );
1179 _mesa_init_depth( ctx );
1180 _mesa_init_debug( ctx );
1181 _mesa_init_display_list( ctx );
1182 _mesa_init_eval( ctx );
1183 _mesa_init_feedback( ctx );
1184 _mesa_init_fog( ctx );
1185 _mesa_init_histogram( ctx );
1186 _mesa_init_hint( ctx );
1187 _mesa_init_line( ctx );
1188 _mesa_init_lighting( ctx );
1189 _mesa_init_matrix( ctx );
1190 _mesa_init_multisample( ctx );
1191 _mesa_init_pixel( ctx );
1192 _mesa_init_point( ctx );
1193 _mesa_init_polygon( ctx );
1194 _mesa_init_program( ctx );
1195 _mesa_init_query( ctx );
1196 _mesa_init_rastpos( ctx );
1197 _mesa_init_scissor( ctx );
1198 _mesa_init_shader_state( ctx );
1199 _mesa_init_stencil( ctx );
1200 _mesa_init_transform( ctx );
1201 _mesa_init_varray( ctx );
1202 _mesa_init_viewport( ctx );
1203
1204 if (!_mesa_init_texture( ctx ))
1205 return GL_FALSE;
1206
1207 _mesa_init_texture_s3tc( ctx );
1208 _mesa_init_texture_fxt1( ctx );
1209
1210 /* Miscellaneous */
1211 ctx->NewState = _NEW_ALL;
1212 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1213 ctx->_Facing = 0;
1214
1215 return GL_TRUE;
1216 }
1217
1218
1219 /**
1220 * This is the default function we plug into all dispatch table slots
1221 * This helps prevents a segfault when someone calls a GL function without
1222 * first checking if the extension's supported.
1223 */
1224 static int
1225 generic_nop(void)
1226 {
1227 _mesa_problem(NULL, "User called no-op dispatch function (an unsupported extension function?)");
1228 return 0;
1229 }
1230
1231
1232 /**
1233 * Allocate and initialize a new dispatch table.
1234 */
1235 static struct _glapi_table *
1236 alloc_dispatch_table(void)
1237 {
1238 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1239 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1240 * Mesa we do this to accomodate different versions of libGL and various
1241 * DRI drivers.
1242 */
1243 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
1244 sizeof(struct _glapi_table) / sizeof(_glapi_proc));
1245 struct _glapi_table *table =
1246 (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc));
1247 if (table) {
1248 _glapi_proc *entry = (_glapi_proc *) table;
1249 GLint i;
1250 for (i = 0; i < numEntries; i++) {
1251 entry[i] = (_glapi_proc) generic_nop;
1252 }
1253 }
1254 return table;
1255 }
1256
1257
1258 /**
1259 * Initialize a GLcontext struct (rendering context).
1260 *
1261 * This includes allocating all the other structs and arrays which hang off of
1262 * the context by pointers.
1263 * Note that the driver needs to pass in its dd_function_table here since
1264 * we need to at least call driverFunctions->NewTextureObject to create the
1265 * default texture objects.
1266 *
1267 * Called by _mesa_create_context().
1268 *
1269 * Performs the imports and exports callback tables initialization, and
1270 * miscellaneous one-time initializations. If no shared context is supplied one
1271 * is allocated, and increase its reference count. Setups the GL API dispatch
1272 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1273 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1274 * for debug flags.
1275 *
1276 * \param ctx the context to initialize
1277 * \param visual describes the visual attributes for this context
1278 * \param share_list points to context to share textures, display lists,
1279 * etc with, or NULL
1280 * \param driverFunctions table of device driver functions for this context
1281 * to use
1282 * \param driverContext pointer to driver-specific context data
1283 */
1284 GLboolean
1285 _mesa_initialize_context(GLcontext *ctx,
1286 const GLvisual *visual,
1287 GLcontext *share_list,
1288 const struct dd_function_table *driverFunctions,
1289 void *driverContext)
1290 {
1291 ASSERT(driverContext);
1292 assert(driverFunctions->NewTextureObject);
1293 assert(driverFunctions->FreeTexImageData);
1294
1295 /* If the driver wants core Mesa to use special imports, it'll have to
1296 * override these defaults.
1297 */
1298 _mesa_init_default_imports( &(ctx->imports), driverContext );
1299
1300 /* initialize the exports (Mesa functions called by the window system) */
1301 _mesa_init_default_exports( &(ctx->exports) );
1302
1303 /* misc one-time initializations */
1304 one_time_init(ctx);
1305
1306 ctx->Visual = *visual;
1307 ctx->DrawBuffer = NULL;
1308 ctx->ReadBuffer = NULL;
1309 ctx->WinSysDrawBuffer = NULL;
1310 ctx->WinSysReadBuffer = NULL;
1311
1312 /* Plug in driver functions and context pointer here.
1313 * This is important because when we call alloc_shared_state() below
1314 * we'll call ctx->Driver.NewTextureObject() to create the default
1315 * textures.
1316 */
1317 ctx->Driver = *driverFunctions;
1318 ctx->DriverCtx = driverContext;
1319
1320 if (share_list) {
1321 /* share state with another context */
1322 ctx->Shared = share_list->Shared;
1323 }
1324 else {
1325 /* allocate new, unshared state */
1326 if (!alloc_shared_state( ctx )) {
1327 return GL_FALSE;
1328 }
1329 }
1330 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1331 ctx->Shared->RefCount++;
1332 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1333
1334 if (!init_attrib_groups( ctx )) {
1335 free_shared_state(ctx, ctx->Shared);
1336 return GL_FALSE;
1337 }
1338
1339 /* setup the API dispatch tables */
1340 ctx->Exec = alloc_dispatch_table();
1341 ctx->Save = alloc_dispatch_table();
1342 if (!ctx->Exec || !ctx->Save) {
1343 free_shared_state(ctx, ctx->Shared);
1344 if (ctx->Exec)
1345 _mesa_free(ctx->Exec);
1346 }
1347 _mesa_init_exec_table(ctx->Exec);
1348 ctx->CurrentDispatch = ctx->Exec;
1349 #if _HAVE_FULL_GL
1350 _mesa_init_dlist_table(ctx->Save);
1351 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
1352 /* Neutral tnl module stuff */
1353 _mesa_init_exec_vtxfmt( ctx );
1354 ctx->TnlModule.Current = NULL;
1355 ctx->TnlModule.SwapCount = 0;
1356 #endif
1357
1358 ctx->FragmentProgram._MaintainTexEnvProgram
1359 = (_mesa_getenv("MESA_TEX_PROG") != NULL);
1360 ctx->FragmentProgram._UseTexEnvProgram = ctx->FragmentProgram._MaintainTexEnvProgram;
1361
1362 ctx->VertexProgram._MaintainTnlProgram
1363 = (_mesa_getenv("MESA_TNL_PROG") != NULL);
1364 if (ctx->VertexProgram._MaintainTnlProgram) {
1365 /* this is required... */
1366 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
1367 }
1368
1369 ctx->FirstTimeCurrent = GL_TRUE;
1370
1371 return GL_TRUE;
1372 }
1373
1374
1375 /**
1376 * Allocate and initialize a GLcontext structure.
1377 * Note that the driver needs to pass in its dd_function_table here since
1378 * we need to at least call driverFunctions->NewTextureObject to initialize
1379 * the rendering context.
1380 *
1381 * \param visual a GLvisual pointer (we copy the struct contents)
1382 * \param share_list another context to share display lists with or NULL
1383 * \param driverFunctions points to the dd_function_table into which the
1384 * driver has plugged in all its special functions.
1385 * \param driverCtx points to the device driver's private context state
1386 *
1387 * \return pointer to a new __GLcontextRec or NULL if error.
1388 */
1389 GLcontext *
1390 _mesa_create_context(const GLvisual *visual,
1391 GLcontext *share_list,
1392 const struct dd_function_table *driverFunctions,
1393 void *driverContext)
1394 {
1395 GLcontext *ctx;
1396
1397 ASSERT(visual);
1398 ASSERT(driverContext);
1399
1400 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
1401 if (!ctx)
1402 return NULL;
1403
1404 if (_mesa_initialize_context(ctx, visual, share_list,
1405 driverFunctions, driverContext)) {
1406 return ctx;
1407 }
1408 else {
1409 _mesa_free(ctx);
1410 return NULL;
1411 }
1412 }
1413
1414
1415 /**
1416 * Free the data associated with the given context.
1417 *
1418 * But doesn't free the GLcontext struct itself.
1419 *
1420 * \sa _mesa_initialize_context() and init_attrib_groups().
1421 */
1422 void
1423 _mesa_free_context_data( GLcontext *ctx )
1424 {
1425 /* if we're destroying the current context, unbind it first */
1426 if (ctx == _mesa_get_current_context()) {
1427 _mesa_make_current(NULL, NULL, NULL);
1428 }
1429 else {
1430 /* unreference WinSysDraw/Read buffers */
1431 _mesa_unreference_framebuffer(&ctx->WinSysDrawBuffer);
1432 _mesa_unreference_framebuffer(&ctx->WinSysReadBuffer);
1433 _mesa_unreference_framebuffer(&ctx->DrawBuffer);
1434 _mesa_unreference_framebuffer(&ctx->ReadBuffer);
1435 }
1436
1437 _mesa_free_lighting_data( ctx );
1438 _mesa_free_eval_data( ctx );
1439 _mesa_free_texture_data( ctx );
1440 _mesa_free_matrix_data( ctx );
1441 _mesa_free_viewport_data( ctx );
1442 _mesa_free_colortables_data( ctx );
1443 _mesa_free_program_data(ctx);
1444 _mesa_free_shader_state(ctx);
1445 _mesa_free_query_data(ctx);
1446
1447 #if FEATURE_ARB_vertex_buffer_object
1448 _mesa_delete_buffer_object(ctx, ctx->Array.NullBufferObj);
1449 #endif
1450 _mesa_delete_array_object(ctx, ctx->Array.DefaultArrayObj);
1451
1452 /* free dispatch tables */
1453 _mesa_free(ctx->Exec);
1454 _mesa_free(ctx->Save);
1455
1456 /* Shared context state (display lists, textures, etc) */
1457 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1458 ctx->Shared->RefCount--;
1459 assert(ctx->Shared->RefCount >= 0);
1460 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1461 if (ctx->Shared->RefCount == 0) {
1462 /* free shared state */
1463 free_shared_state( ctx, ctx->Shared );
1464 }
1465
1466 if (ctx->Extensions.String)
1467 _mesa_free((void *) ctx->Extensions.String);
1468 }
1469
1470
1471 /**
1472 * Destroy a GLcontext structure.
1473 *
1474 * \param ctx GL context.
1475 *
1476 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1477 */
1478 void
1479 _mesa_destroy_context( GLcontext *ctx )
1480 {
1481 if (ctx) {
1482 _mesa_free_context_data(ctx);
1483 _mesa_free( (void *) ctx );
1484 }
1485 }
1486
1487
1488 #if _HAVE_FULL_GL
1489 /**
1490 * Copy attribute groups from one context to another.
1491 *
1492 * \param src source context
1493 * \param dst destination context
1494 * \param mask bitwise OR of GL_*_BIT flags
1495 *
1496 * According to the bits specified in \p mask, copies the corresponding
1497 * attributes from \p src into \p dst. For many of the attributes a simple \c
1498 * memcpy is not enough due to the existence of internal pointers in their data
1499 * structures.
1500 */
1501 void
1502 _mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
1503 {
1504 if (mask & GL_ACCUM_BUFFER_BIT) {
1505 /* OK to memcpy */
1506 dst->Accum = src->Accum;
1507 }
1508 if (mask & GL_COLOR_BUFFER_BIT) {
1509 /* OK to memcpy */
1510 dst->Color = src->Color;
1511 }
1512 if (mask & GL_CURRENT_BIT) {
1513 /* OK to memcpy */
1514 dst->Current = src->Current;
1515 }
1516 if (mask & GL_DEPTH_BUFFER_BIT) {
1517 /* OK to memcpy */
1518 dst->Depth = src->Depth;
1519 }
1520 if (mask & GL_ENABLE_BIT) {
1521 /* no op */
1522 }
1523 if (mask & GL_EVAL_BIT) {
1524 /* OK to memcpy */
1525 dst->Eval = src->Eval;
1526 }
1527 if (mask & GL_FOG_BIT) {
1528 /* OK to memcpy */
1529 dst->Fog = src->Fog;
1530 }
1531 if (mask & GL_HINT_BIT) {
1532 /* OK to memcpy */
1533 dst->Hint = src->Hint;
1534 }
1535 if (mask & GL_LIGHTING_BIT) {
1536 GLuint i;
1537 /* begin with memcpy */
1538 dst->Light = src->Light;
1539 /* fixup linked lists to prevent pointer insanity */
1540 make_empty_list( &(dst->Light.EnabledList) );
1541 for (i = 0; i < MAX_LIGHTS; i++) {
1542 if (dst->Light.Light[i].Enabled) {
1543 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1544 }
1545 }
1546 }
1547 if (mask & GL_LINE_BIT) {
1548 /* OK to memcpy */
1549 dst->Line = src->Line;
1550 }
1551 if (mask & GL_LIST_BIT) {
1552 /* OK to memcpy */
1553 dst->List = src->List;
1554 }
1555 if (mask & GL_PIXEL_MODE_BIT) {
1556 /* OK to memcpy */
1557 dst->Pixel = src->Pixel;
1558 }
1559 if (mask & GL_POINT_BIT) {
1560 /* OK to memcpy */
1561 dst->Point = src->Point;
1562 }
1563 if (mask & GL_POLYGON_BIT) {
1564 /* OK to memcpy */
1565 dst->Polygon = src->Polygon;
1566 }
1567 if (mask & GL_POLYGON_STIPPLE_BIT) {
1568 /* Use loop instead of MEMCPY due to problem with Portland Group's
1569 * C compiler. Reported by John Stone.
1570 */
1571 GLuint i;
1572 for (i = 0; i < 32; i++) {
1573 dst->PolygonStipple[i] = src->PolygonStipple[i];
1574 }
1575 }
1576 if (mask & GL_SCISSOR_BIT) {
1577 /* OK to memcpy */
1578 dst->Scissor = src->Scissor;
1579 }
1580 if (mask & GL_STENCIL_BUFFER_BIT) {
1581 /* OK to memcpy */
1582 dst->Stencil = src->Stencil;
1583 }
1584 if (mask & GL_TEXTURE_BIT) {
1585 /* Cannot memcpy because of pointers */
1586 _mesa_copy_texture_state(src, dst);
1587 }
1588 if (mask & GL_TRANSFORM_BIT) {
1589 /* OK to memcpy */
1590 dst->Transform = src->Transform;
1591 }
1592 if (mask & GL_VIEWPORT_BIT) {
1593 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1594 dst->Viewport.X = src->Viewport.X;
1595 dst->Viewport.Y = src->Viewport.Y;
1596 dst->Viewport.Width = src->Viewport.Width;
1597 dst->Viewport.Height = src->Viewport.Height;
1598 dst->Viewport.Near = src->Viewport.Near;
1599 dst->Viewport.Far = src->Viewport.Far;
1600 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
1601 }
1602
1603 /* XXX FIXME: Call callbacks?
1604 */
1605 dst->NewState = _NEW_ALL;
1606 }
1607 #endif
1608
1609
1610 /**
1611 * Check if the given context can render into the given framebuffer
1612 * by checking visual attributes.
1613 *
1614 * XXX this may go away someday because we're moving toward more freedom
1615 * in binding contexts to drawables with different visual attributes.
1616 * The GL_EXT_f_b_o extension is prompting some of that.
1617 *
1618 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1619 */
1620 static GLboolean
1621 check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1622 {
1623 const GLvisual *ctxvis = &ctx->Visual;
1624 const GLvisual *bufvis = &buffer->Visual;
1625
1626 if (ctxvis == bufvis)
1627 return GL_TRUE;
1628
1629 if (ctxvis->rgbMode != bufvis->rgbMode)
1630 return GL_FALSE;
1631 #if 0
1632 /* disabling this fixes the fgl_glxgears pbuffer demo */
1633 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1634 return GL_FALSE;
1635 #endif
1636 if (ctxvis->stereoMode && !bufvis->stereoMode)
1637 return GL_FALSE;
1638 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1639 return GL_FALSE;
1640 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1641 return GL_FALSE;
1642 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1643 return GL_FALSE;
1644 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1645 return GL_FALSE;
1646 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1647 return GL_FALSE;
1648 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1649 return GL_FALSE;
1650 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1651 return GL_FALSE;
1652 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1653 return GL_FALSE;
1654
1655 return GL_TRUE;
1656 }
1657
1658
1659 /**
1660 * Do one-time initialization for the given framebuffer. Specifically,
1661 * ask the driver for the window's current size and update the framebuffer
1662 * object to match.
1663 * Really, the device driver should totally take care of this.
1664 */
1665 static void
1666 initialize_framebuffer_size(GLcontext *ctx, GLframebuffer *fb)
1667 {
1668 GLuint width, height;
1669 if (ctx->Driver.GetBufferSize) {
1670 ctx->Driver.GetBufferSize(fb, &width, &height);
1671 if (ctx->Driver.ResizeBuffers)
1672 ctx->Driver.ResizeBuffers(ctx, fb, width, height);
1673 fb->Initialized = GL_TRUE;
1674 }
1675 }
1676
1677
1678 /**
1679 * Bind the given context to the given drawBuffer and readBuffer and
1680 * make it the current context for the calling thread.
1681 * We'll render into the drawBuffer and read pixels from the
1682 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1683 *
1684 * We check that the context's and framebuffer's visuals are compatible
1685 * and return immediately if they're not.
1686 *
1687 * \param newCtx the new GL context. If NULL then there will be no current GL
1688 * context.
1689 * \param drawBuffer the drawing framebuffer
1690 * \param readBuffer the reading framebuffer
1691 */
1692 void
1693 _mesa_make_current( GLcontext *newCtx, GLframebuffer *drawBuffer,
1694 GLframebuffer *readBuffer )
1695 {
1696 GET_CURRENT_CONTEXT(oldCtx);
1697
1698 if (MESA_VERBOSE & VERBOSE_API)
1699 _mesa_debug(newCtx, "_mesa_make_current()\n");
1700
1701 /* Check that the context's and framebuffer's visuals are compatible.
1702 */
1703 if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
1704 if (!check_compatible(newCtx, drawBuffer)) {
1705 _mesa_warning(newCtx,
1706 "MakeCurrent: incompatible visuals for context and drawbuffer");
1707 return;
1708 }
1709 }
1710 if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
1711 if (!check_compatible(newCtx, readBuffer)) {
1712 _mesa_warning(newCtx,
1713 "MakeCurrent: incompatible visuals for context and readbuffer");
1714 return;
1715 }
1716 }
1717
1718 #if 0 /** XXX enable this someday */
1719 if (oldCtx && oldCtx != newCtx) {
1720 /* unbind old context's draw/read buffers */
1721 if (oldCtx->DrawBuffer && oldCtx->DrawBuffer->Name == 0) {
1722 oldCtx->DrawBuffer->RefCount--;
1723 oldCtx->DrawBuffer = NULL;
1724 }
1725 if (oldCtx->ReadBuffer && oldCtx->ReadBuffer->Name == 0) {
1726 oldCtx->ReadBuffer->RefCount--;
1727 oldCtx->ReadBuffer = NULL;
1728 }
1729 if (oldCtx->WinSysDrawBuffer) {
1730 ASSERT(oldCtx->WinSysDrawBuffer->Name == 0);
1731 oldCtx->WinSysDrawBuffer->RefCount--;
1732 oldCtx->WinSysDrawBuffer = NULL;
1733 }
1734 if (oldCtx->WinSysReadBuffer) {
1735 ASSERT(oldCtx->WinSysReadBuffer->Name == 0);
1736 oldCtx->WinSysReadBuffer->RefCount--;
1737 oldCtx->WinSysReadBuffer = NULL;
1738 }
1739 }
1740 #endif
1741
1742 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1743 _glapi_set_context((void *) newCtx);
1744 ASSERT(_mesa_get_current_context() == newCtx);
1745
1746 if (oldCtx) {
1747 _mesa_unreference_framebuffer(&oldCtx->WinSysDrawBuffer);
1748 _mesa_unreference_framebuffer(&oldCtx->WinSysReadBuffer);
1749 }
1750
1751 if (!newCtx) {
1752 _glapi_set_dispatch(NULL); /* none current */
1753 }
1754 else {
1755 _glapi_set_dispatch(newCtx->CurrentDispatch);
1756
1757 if (drawBuffer && readBuffer) {
1758 /* TODO: check if newCtx and buffer's visual match??? */
1759
1760 ASSERT(drawBuffer->Name == 0);
1761 ASSERT(readBuffer->Name == 0);
1762 _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
1763 _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
1764
1765 /*
1766 * Only set the context's Draw/ReadBuffer fields if they're NULL
1767 * or not bound to a user-created FBO.
1768 */
1769 if (!newCtx->DrawBuffer || newCtx->DrawBuffer->Name == 0) {
1770 _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
1771 }
1772 if (!newCtx->ReadBuffer || newCtx->ReadBuffer->Name == 0) {
1773 _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
1774 }
1775
1776 newCtx->NewState |= _NEW_BUFFERS;
1777
1778 #if 1
1779 /* We want to get rid of these lines: */
1780
1781 #if _HAVE_FULL_GL
1782 if (!drawBuffer->Initialized) {
1783 initialize_framebuffer_size(newCtx, drawBuffer);
1784 }
1785 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
1786 initialize_framebuffer_size(newCtx, readBuffer);
1787 }
1788
1789 _mesa_resizebuffers(newCtx);
1790 #endif
1791
1792 #else
1793 /* We want the drawBuffer and readBuffer to be initialized by
1794 * the driver.
1795 * This generally means the Width and Height match the actual
1796 * window size and the renderbuffers (both hardware and software
1797 * based) are allocated to match. The later can generally be
1798 * done with a call to _mesa_resize_framebuffer().
1799 *
1800 * It's theoretically possible for a buffer to have zero width
1801 * or height, but for now, assert check that the driver did what's
1802 * expected of it.
1803 */
1804 ASSERT(drawBuffer->Width > 0);
1805 ASSERT(drawBuffer->Height > 0);
1806 #endif
1807
1808 if (newCtx->FirstTimeCurrent) {
1809 /* set initial viewport and scissor size now */
1810 _mesa_set_viewport(newCtx, 0, 0,
1811 drawBuffer->Width, drawBuffer->Height);
1812 _mesa_set_scissor(newCtx, 0, 0,
1813 drawBuffer->Width, drawBuffer->Height );
1814 check_context_limits(newCtx);
1815 }
1816 }
1817
1818 /* We can use this to help debug user's problems. Tell them to set
1819 * the MESA_INFO env variable before running their app. Then the
1820 * first time each context is made current we'll print some useful
1821 * information.
1822 */
1823 if (newCtx->FirstTimeCurrent) {
1824 if (_mesa_getenv("MESA_INFO")) {
1825 _mesa_print_info();
1826 }
1827 newCtx->FirstTimeCurrent = GL_FALSE;
1828 }
1829 }
1830 }
1831
1832
1833 /**
1834 * Make context 'ctx' share the display lists, textures and programs
1835 * that are associated with 'ctxToShare'.
1836 * Any display lists, textures or programs associated with 'ctx' will
1837 * be deleted if nobody else is sharing them.
1838 */
1839 GLboolean
1840 _mesa_share_state(GLcontext *ctx, GLcontext *ctxToShare)
1841 {
1842 if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1843 ctx->Shared->RefCount--;
1844 if (ctx->Shared->RefCount == 0) {
1845 free_shared_state(ctx, ctx->Shared);
1846 }
1847 ctx->Shared = ctxToShare->Shared;
1848 ctx->Shared->RefCount++;
1849 return GL_TRUE;
1850 }
1851 else {
1852 return GL_FALSE;
1853 }
1854 }
1855
1856
1857
1858 /**
1859 * \return pointer to the current GL context for this thread.
1860 *
1861 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1862 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1863 * context.h.
1864 */
1865 GLcontext *
1866 _mesa_get_current_context( void )
1867 {
1868 return (GLcontext *) _glapi_get_context();
1869 }
1870
1871
1872 /**
1873 * Get context's current API dispatch table.
1874 *
1875 * It'll either be the immediate-mode execute dispatcher or the display list
1876 * compile dispatcher.
1877 *
1878 * \param ctx GL context.
1879 *
1880 * \return pointer to dispatch_table.
1881 *
1882 * Simply returns __GLcontextRec::CurrentDispatch.
1883 */
1884 struct _glapi_table *
1885 _mesa_get_dispatch(GLcontext *ctx)
1886 {
1887 return ctx->CurrentDispatch;
1888 }
1889
1890 /*@}*/
1891
1892
1893 /**********************************************************************/
1894 /** \name Miscellaneous functions */
1895 /**********************************************************************/
1896 /*@{*/
1897
1898 /**
1899 * Record an error.
1900 *
1901 * \param ctx GL context.
1902 * \param error error code.
1903 *
1904 * Records the given error code and call the driver's dd_function_table::Error
1905 * function if defined.
1906 *
1907 * \sa
1908 * This is called via _mesa_error().
1909 */
1910 void
1911 _mesa_record_error(GLcontext *ctx, GLenum error)
1912 {
1913 if (!ctx)
1914 return;
1915
1916 if (ctx->ErrorValue == GL_NO_ERROR) {
1917 ctx->ErrorValue = error;
1918 }
1919
1920 /* Call device driver's error handler, if any. This is used on the Mac. */
1921 if (ctx->Driver.Error) {
1922 ctx->Driver.Error(ctx);
1923 }
1924 }
1925
1926
1927 /**
1928 * Execute glFinish().
1929 *
1930 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1931 * dd_function_table::Finish driver callback, if not NULL.
1932 */
1933 void GLAPIENTRY
1934 _mesa_Finish(void)
1935 {
1936 GET_CURRENT_CONTEXT(ctx);
1937 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1938 if (ctx->Driver.Finish) {
1939 ctx->Driver.Finish(ctx);
1940 }
1941 }
1942
1943
1944 /**
1945 * Execute glFlush().
1946 *
1947 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1948 * dd_function_table::Flush driver callback, if not NULL.
1949 */
1950 void GLAPIENTRY
1951 _mesa_Flush(void)
1952 {
1953 GET_CURRENT_CONTEXT(ctx);
1954 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1955 if (ctx->Driver.Flush) {
1956 ctx->Driver.Flush(ctx);
1957 }
1958 }
1959
1960
1961 /*@}*/