blob: 3116cb95a1fff7437254855048cd856030a01a47 [file] [log] [blame]
Keith Whitwell6dc85572003-07-17 13:43:59 +00001/**
2 * \file context.c
3 * Mesa context/visual/framebuffer management functions.
4 * \author Brian Paul
5 */
6
jtgafb833d1999-08-19 00:55:39 +00007/*
8 * Mesa 3-D graphics library
Brian Paulde434842004-11-27 20:14:03 +00009 * Version: 6.3
jtgafb833d1999-08-19 00:55:39 +000010 *
Brian Paul4d859f72004-01-23 18:57:05 +000011 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
jtgafb833d1999-08-19 00:55:39 +000012 *
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
Keith Whitwell6dc85572003-07-17 13:43:59 +000032/**
Brian Paul253204f2004-09-10 00:45:12 +000033 * \mainpage Mesa Main Module
Keith Whitwell6dc85572003-07-17 13:43:59 +000034 *
Brian Paul253204f2004-09-10 00:45:12 +000035 * \section MainIntroduction Introduction
Keith Whitwell6dc85572003-07-17 13:43:59 +000036 *
Brian Paul253204f2004-09-10 00:45:12 +000037 * 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>
Keith Whitwell6dc85572003-07-17 13:43:59 +000046 *
Brian Paul253204f2004-09-10 00:45:12 +000047 * Other modules are responsible for API dispatch, vertex transformation,
48 * point/line/triangle setup, rasterization, vertex array caching,
49 * vertex/fragment programs/shaders, etc.
Keith Whitwell6dc85572003-07-17 13:43:59 +000050 *
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 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000064 * Selecting <b>Data Structures</b> will list all C structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +000065 *
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 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000070 * Selecting the <b>Data Fields</b> link will display a list of all
Keith Whitwell6dc85572003-07-17 13:43:59 +000071 * documented structure members.
72 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000073 * Selecting the <b>Globals</b> link will display a list
Keith Whitwell6dc85572003-07-17 13:43:59 +000074 * of all functions, structures, global variables and macros in the module.
75 *
76 */
77
78
Brian Paulfbd8f211999-11-11 01:22:25 +000079#include "glheader.h"
Brian Paul3c634522002-10-24 23:57:19 +000080#include "imports.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000081#include "accum.h"
82#include "attrib.h"
83#include "blend.h"
Brian Paulb1394fa2000-09-26 20:53:53 +000084#include "buffers.h"
Brian Paul148a2842003-09-17 03:40:11 +000085#include "bufferobj.h"
Brian Paul4bdcfe52000-04-17 17:57:04 +000086#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000087#include "context.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000088#include "debug.h"
89#include "depth.h"
jtgafb833d1999-08-19 00:55:39 +000090#include "dlist.h"
91#include "eval.h"
92#include "enums.h"
Brian Paul585a68c1999-09-11 11:31:34 +000093#include "extensions.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000094#include "feedback.h"
jtgafb833d1999-08-19 00:55:39 +000095#include "fog.h"
Brian Paulb7a43041999-11-30 20:34:51 +000096#include "get.h"
Brian Paul9560f052000-01-31 23:11:39 +000097#include "glthread.h"
Brian Paulf44898c2003-07-18 15:44:57 +000098#include "glapioffsets.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000099#include "histogram.h"
100#include "hint.h"
jtgafb833d1999-08-19 00:55:39 +0000101#include "hash.h"
102#include "light.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000103#include "lines.h"
jtgafb833d1999-08-19 00:55:39 +0000104#include "macros.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000105#include "matrix.h"
Brian Paul05944c02003-07-22 03:51:46 +0000106#include "occlude.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000107#include "pixel.h"
108#include "points.h"
109#include "polygon.h"
Brian Paul31732772003-09-18 16:39:09 +0000110#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
111#include "program.h"
112#endif
Keith Whitwell6dc85572003-07-17 13:43:59 +0000113#include "rastpos.h"
jtgafb833d1999-08-19 00:55:39 +0000114#include "simple_list.h"
Brian Paulfa9df402000-02-02 19:16:46 +0000115#include "state.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000116#include "stencil.h"
Brian Paul8f04c122004-04-27 13:39:20 +0000117#include "texcompress.h"
jtgafb833d1999-08-19 00:55:39 +0000118#include "teximage.h"
119#include "texobj.h"
Brian Paul85d81602002-06-17 23:36:31 +0000120#include "texstate.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000121#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +0000122#include "varray.h"
Gareth Hughesd4eb6652001-03-12 01:32:20 +0000123#include "vtxfmt.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000124#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000125#include "math/m_translate.h"
Keith Whitwell23caf202000-11-16 21:05:34 +0000126#include "math/m_matrix.h"
127#include "math/m_xform.h"
Keith Whitwellf4b02d12001-01-05 05:31:42 +0000128#include "math/mathmod.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000129#endif
jtgafb833d1999-08-19 00:55:39 +0000130
davem69775355a2001-06-05 23:54:00 +0000131#ifdef USE_SPARC_ASM
Ian Romanicke16f6e32004-06-26 00:02:51 +0000132#include "sparc/sparc.h"
davem69775355a2001-06-05 23:54:00 +0000133#endif
jtgafb833d1999-08-19 00:55:39 +0000134
Keith Whitwell23caf202000-11-16 21:05:34 +0000135#ifndef MESA_VERBOSE
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000136int MESA_VERBOSE = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000137#endif
138
139#ifndef MESA_DEBUG_FLAGS
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000140int MESA_DEBUG_FLAGS = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000141#endif
Brian Paulb1394fa2000-09-26 20:53:53 +0000142
Brian Paul86b84272001-12-14 02:50:01 +0000143
Brian Paul27558a12003-03-01 01:50:20 +0000144/* ubyte -> float conversion */
145GLfloat _mesa_ubyte_to_float_color_tab[256];
146
Brian Paul9a33a112002-06-13 04:28:29 +0000147static void
148free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
149
Brian Paul86b84272001-12-14 02:50:01 +0000150
Brian Paulb1394fa2000-09-26 20:53:53 +0000151/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000152/** \name OpenGL SI-style interface (new in Mesa 3.5)
153 *
154 * \if subset
155 * \note Most of these functions are never called in the Mesa subset.
156 * \endif
157 */
158/*@{*/
Brian Paulb1394fa2000-09-26 20:53:53 +0000159
Keith Whitwell6dc85572003-07-17 13:43:59 +0000160/**
161 * Destroy context callback.
162 *
163 * \param gc context.
164 * \return GL_TRUE on success, or GL_FALSE on failure.
165 *
166 * \ifnot subset
167 * Called by window system/device driver (via __GLexports::destroyCurrent) when
168 * the rendering context is to be destroyed.
169 * \endif
170 *
171 * Frees the context data and the context structure.
Brian Paul9a33a112002-06-13 04:28:29 +0000172 */
173GLboolean
174_mesa_destroyContext(__GLcontext *gc)
Brian Paulb1394fa2000-09-26 20:53:53 +0000175{
176 if (gc) {
177 _mesa_free_context_data(gc);
Brian Paul3c634522002-10-24 23:57:19 +0000178 _mesa_free(gc);
Brian Paulb1394fa2000-09-26 20:53:53 +0000179 }
180 return GL_TRUE;
181}
182
Keith Whitwell6dc85572003-07-17 13:43:59 +0000183/**
184 * Unbind context callback.
185 *
186 * \param gc context.
187 * \return GL_TRUE on success, or GL_FALSE on failure.
188 *
189 * \ifnot subset
190 * Called by window system/device driver (via __GLexports::loseCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000191 * when the rendering context is made non-current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000192 * \endif
193 *
194 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000195 */
196GLboolean
197_mesa_loseCurrent(__GLcontext *gc)
198{
199 /* XXX unbind context from thread */
Brian Paula6c423d2004-08-25 15:59:48 +0000200 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000201 return GL_TRUE;
202}
203
Keith Whitwell6dc85572003-07-17 13:43:59 +0000204/**
205 * Bind context callback.
206 *
207 * \param gc context.
208 * \return GL_TRUE on success, or GL_FALSE on failure.
209 *
210 * \ifnot subset
211 * Called by window system/device driver (via __GLexports::makeCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000212 * when the rendering context is made current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000213 * \endif
214 *
215 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000216 */
217GLboolean
218_mesa_makeCurrent(__GLcontext *gc)
219{
220 /* XXX bind context to thread */
Brian Paula6c423d2004-08-25 15:59:48 +0000221 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000222 return GL_TRUE;
223}
224
Keith Whitwell6dc85572003-07-17 13:43:59 +0000225/**
226 * Share context callback.
227 *
228 * \param gc context.
229 * \param gcShare shared context.
230 * \return GL_TRUE on success, or GL_FALSE on failure.
231 *
232 * \ifnot subset
233 * Called by window system/device driver (via __GLexports::shareContext)
234 * \endif
235 *
236 * Update the shared context reference count, gl_shared_state::RefCount.
Brian Paul9a33a112002-06-13 04:28:29 +0000237 */
238GLboolean
239_mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
240{
241 if (gc && gcShare && gc->Shared && gcShare->Shared) {
242 gc->Shared->RefCount--;
243 if (gc->Shared->RefCount == 0) {
244 free_shared_state(gc, gc->Shared);
245 }
246 gc->Shared = gcShare->Shared;
247 gc->Shared->RefCount++;
248 return GL_TRUE;
249 }
250 else {
251 return GL_FALSE;
252 }
253}
254
Keith Whitwell6dc85572003-07-17 13:43:59 +0000255
256#if _HAVE_FULL_GL
257/**
258 * Copy context callback.
259 */
Brian Paul9a33a112002-06-13 04:28:29 +0000260GLboolean
261_mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
262{
263 if (dst && src) {
264 _mesa_copy_context( src, dst, mask );
265 return GL_TRUE;
266 }
267 else {
268 return GL_FALSE;
269 }
270}
Keith Whitwell6dc85572003-07-17 13:43:59 +0000271#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000272
Keith Whitwell6dc85572003-07-17 13:43:59 +0000273/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000274GLboolean
275_mesa_forceCurrent(__GLcontext *gc)
276{
Brian Paula6c423d2004-08-25 15:59:48 +0000277 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000278 return GL_TRUE;
279}
280
Keith Whitwell6dc85572003-07-17 13:43:59 +0000281/**
282 * Windows/buffer resizing notification callback.
283 *
284 * \param gc GL context.
285 * \return GL_TRUE on success, or GL_FALSE on failure.
286 */
Brian Paul9a33a112002-06-13 04:28:29 +0000287GLboolean
288_mesa_notifyResize(__GLcontext *gc)
289{
290 GLint x, y;
291 GLuint width, height;
292 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
293 if (!d || !d->getDrawableSize)
294 return GL_FALSE;
295 d->getDrawableSize( d, &x, &y, &width, &height );
296 /* update viewport, resize software buffers, etc. */
297 return GL_TRUE;
298}
299
Keith Whitwell6dc85572003-07-17 13:43:59 +0000300/**
301 * Window/buffer destruction notification callback.
302 *
303 * \param gc GL context.
304 *
305 * Called when the context's window/buffer is going to be destroyed.
306 *
307 * No-op
308 */
Brian Paul9a33a112002-06-13 04:28:29 +0000309void
310_mesa_notifyDestroy(__GLcontext *gc)
311{
Brian Paul60b6e4f2002-10-14 17:08:17 +0000312 /* Unbind from it. */
Brian Paula6c423d2004-08-25 15:59:48 +0000313 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000314}
315
Keith Whitwell6dc85572003-07-17 13:43:59 +0000316/**
317 * Swap buffers notification callback.
318 *
319 * \param gc GL context.
320 *
321 * Called by window system just before swapping buffers.
Brian Paul9a33a112002-06-13 04:28:29 +0000322 * We have to finish any pending rendering.
323 */
324void
325_mesa_notifySwapBuffers(__GLcontext *gc)
326{
327 FLUSH_VERTICES( gc, 0 );
328}
329
Keith Whitwell6dc85572003-07-17 13:43:59 +0000330/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000331struct __GLdispatchStateRec *
332_mesa_dispatchExec(__GLcontext *gc)
333{
Brian Paula6c423d2004-08-25 15:59:48 +0000334 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000335 return NULL;
336}
337
Keith Whitwell6dc85572003-07-17 13:43:59 +0000338/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000339void
340_mesa_beginDispatchOverride(__GLcontext *gc)
341{
Brian Paula6c423d2004-08-25 15:59:48 +0000342 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000343}
344
Keith Whitwell6dc85572003-07-17 13:43:59 +0000345/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000346void
347_mesa_endDispatchOverride(__GLcontext *gc)
348{
Brian Paula6c423d2004-08-25 15:59:48 +0000349 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000350}
351
Keith Whitwell6dc85572003-07-17 13:43:59 +0000352/**
353 * \ifnot subset
354 * Setup the exports.
355 *
356 * The window system will call these functions when it needs Mesa to do
357 * something.
358 *
359 * \note Device drivers should override these functions! For example,
Brian Paul9a33a112002-06-13 04:28:29 +0000360 * the Xlib driver should plug in the XMesa*-style functions into this
361 * structure. The XMesa-style functions should then call the _mesa_*
362 * version of these functions. This is an approximation to OO design
363 * (inheritance and virtual functions).
Keith Whitwell6dc85572003-07-17 13:43:59 +0000364 * \endif
365 *
366 * \if subset
367 * No-op.
368 *
369 * \endif
Brian Paul9a33a112002-06-13 04:28:29 +0000370 */
371static void
372_mesa_init_default_exports(__GLexports *exports)
373{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000374#if _HAVE_FULL_GL
Brian Paul9a33a112002-06-13 04:28:29 +0000375 exports->destroyContext = _mesa_destroyContext;
376 exports->loseCurrent = _mesa_loseCurrent;
377 exports->makeCurrent = _mesa_makeCurrent;
378 exports->shareContext = _mesa_shareContext;
379 exports->copyContext = _mesa_copyContext;
380 exports->forceCurrent = _mesa_forceCurrent;
381 exports->notifyResize = _mesa_notifyResize;
Brian Paul2f35d5e2002-06-13 04:31:09 +0000382 exports->notifyDestroy = _mesa_notifyDestroy;
Brian Paul9a33a112002-06-13 04:28:29 +0000383 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
384 exports->dispatchExec = _mesa_dispatchExec;
385 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
386 exports->endDispatchOverride = _mesa_endDispatchOverride;
Brian Paula6c423d2004-08-25 15:59:48 +0000387#else
388 (void) exports;
Keith Whitwell6dc85572003-07-17 13:43:59 +0000389#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000390}
391
Keith Whitwell6dc85572003-07-17 13:43:59 +0000392/**
393 * Exported OpenGL SI interface.
394 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000395__GLcontext *
396__glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
397{
398 GLcontext *ctx;
399
Brian Paul4753d602002-06-15 02:38:15 +0000400 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
Brian Paulb1394fa2000-09-26 20:53:53 +0000401 if (ctx == NULL) {
402 return NULL;
403 }
Brian Paul60b6e4f2002-10-14 17:08:17 +0000404
Brian Pauld3fd7ba2004-01-20 02:49:27 +0000405 /* XXX doesn't work at this time */
406 _mesa_initialize_context(ctx, modes, NULL, NULL, NULL);
Brian Paulb1394fa2000-09-26 20:53:53 +0000407 ctx->imports = *imports;
Brian Paulb1394fa2000-09-26 20:53:53 +0000408
409 return ctx;
410}
411
Keith Whitwell6dc85572003-07-17 13:43:59 +0000412/**
413 * Exported OpenGL SI interface.
414 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000415void
416__glCoreNopDispatch(void)
417{
418#if 0
419 /* SI */
420 __gl_dispatch = __glNopDispatchState;
421#else
422 /* Mesa */
423 _glapi_set_dispatch(NULL);
424#endif
425}
426
Keith Whitwell6dc85572003-07-17 13:43:59 +0000427/*@}*/
428
Brian Paulb1394fa2000-09-26 20:53:53 +0000429
jtgafb833d1999-08-19 00:55:39 +0000430/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000431/** \name GL Visual allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000432/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000433/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000434
Keith Whitwell6dc85572003-07-17 13:43:59 +0000435/**
Brian Paul894844a2004-03-21 17:05:03 +0000436 * Allocates a GLvisual structure and initializes it via
437 * _mesa_initialize_visual().
Keith Whitwell6dc85572003-07-17 13:43:59 +0000438 *
439 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
440 * \param dbFlag double buffering
441 * \param stereoFlag stereo buffer
442 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
443 * is acceptable but the actual depth type will be GLushort or GLuint as
444 * needed.
445 * \param stencilBits requested minimum bits per stencil buffer value
446 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
447 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
448 * \param redBits number of bits per color component in frame buffer for RGB(A)
449 * mode. We always use 8 in core Mesa though.
450 * \param greenBits same as above.
451 * \param blueBits same as above.
452 * \param alphaBits same as above.
453 * \param numSamples not really used.
454 *
455 * \return pointer to new GLvisual or NULL if requested parameters can't be
456 * met.
457 *
Brian Paul894844a2004-03-21 17:05:03 +0000458 * \note Need to add params for level and numAuxBuffers (at least)
Brian Paul4d053dd2000-01-14 04:45:47 +0000459 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000460GLvisual *
461_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000462 GLboolean dbFlag,
463 GLboolean stereoFlag,
464 GLint redBits,
465 GLint greenBits,
466 GLint blueBits,
467 GLint alphaBits,
468 GLint indexBits,
469 GLint depthBits,
470 GLint stencilBits,
471 GLint accumRedBits,
472 GLint accumGreenBits,
473 GLint accumBlueBits,
474 GLint accumAlphaBits,
475 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000476{
Brian Paul178a1c52000-04-22 01:05:00 +0000477 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
478 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000479 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000480 redBits, greenBits, blueBits, alphaBits,
481 indexBits, depthBits, stencilBits,
482 accumRedBits, accumGreenBits,
483 accumBlueBits, accumAlphaBits,
Brian Paulb1394fa2000-09-26 20:53:53 +0000484 numSamples)) {
Brian Paul178a1c52000-04-22 01:05:00 +0000485 FREE(vis);
486 return NULL;
487 }
488 }
489 return vis;
490}
491
Keith Whitwell6dc85572003-07-17 13:43:59 +0000492/**
Brian Paul894844a2004-03-21 17:05:03 +0000493 * Makes some sanity checks and fills in the fields of the
494 * GLvisual structure with the given parameters.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000495 *
496 * \return GL_TRUE on success, or GL_FALSE on failure.
497 *
498 * \sa _mesa_create_visual() above for the parameter description.
499 *
Brian Paul894844a2004-03-21 17:05:03 +0000500 * \note Need to add params for level and numAuxBuffers (at least)
Brian Paul178a1c52000-04-22 01:05:00 +0000501 */
502GLboolean
503_mesa_initialize_visual( GLvisual *vis,
504 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000505 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);
Brian Paul4d053dd2000-01-14 04:45:47 +0000521
Brian Pauled30dfa2000-03-03 17:47:39 +0000522 /* This is to catch bad values from device drivers not updated for
523 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
524 * bad value now (a 1-bit depth buffer!?!).
525 */
526 assert(depthBits == 0 || depthBits > 1);
527
528 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000529 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000530 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000531 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000532 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000533 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000534 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000535 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000536 }
537 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000538 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000539 }
540 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000541 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000542 }
543 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000544 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000545 }
546
Brian Paulb6bcae52001-01-23 23:39:36 +0000547 vis->rgbMode = rgbFlag;
548 vis->doubleBufferMode = dbFlag;
549 vis->stereoMode = stereoFlag;
Brian Paul153f1542002-10-29 15:04:35 +0000550
Brian Paulb6bcae52001-01-23 23:39:36 +0000551 vis->redBits = redBits;
552 vis->greenBits = greenBits;
553 vis->blueBits = blueBits;
554 vis->alphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000555
Brian Paulb6bcae52001-01-23 23:39:36 +0000556 vis->indexBits = indexBits;
557 vis->depthBits = depthBits;
558 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
559 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
560 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
561 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
562 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Pauled30dfa2000-03-03 17:47:39 +0000563
Brian Paul153f1542002-10-29 15:04:35 +0000564 vis->haveAccumBuffer = accumRedBits > 0;
565 vis->haveDepthBuffer = depthBits > 0;
566 vis->haveStencilBuffer = stencilBits > 0;
567
568 vis->numAuxBuffers = 0;
569 vis->level = 0;
570 vis->pixmapMode = 0;
Brian Paul894844a2004-03-21 17:05:03 +0000571 vis->samples = numSamples;
Brian Paul153f1542002-10-29 15:04:35 +0000572
Brian Paul178a1c52000-04-22 01:05:00 +0000573 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000574}
575
Brian Paul894844a2004-03-21 17:05:03 +0000576
Keith Whitwell6dc85572003-07-17 13:43:59 +0000577/**
Brian Paul894844a2004-03-21 17:05:03 +0000578 * Destroy a visual and free its memory.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000579 *
580 * \param vis visual.
581 *
582 * Frees the visual structure.
583 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000584void
585_mesa_destroy_visual( GLvisual *vis )
586{
587 FREE(vis);
588}
589
Keith Whitwell6dc85572003-07-17 13:43:59 +0000590/*@}*/
591
Brian Paulb371e0d2000-03-31 01:05:51 +0000592
Brian Paul4d053dd2000-01-14 04:45:47 +0000593/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000594/** \name GL Framebuffer allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000595/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000596/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000597
Keith Whitwell6dc85572003-07-17 13:43:59 +0000598/**
Brian Paul894844a2004-03-21 17:05:03 +0000599 * Allocate a GLframebuffer structure and initializes it via
600 * _mesa_initialize_framebuffer().
Keith Whitwell6dc85572003-07-17 13:43:59 +0000601 *
602 * A GLframebuffer is a structure which encapsulates the depth, stencil and
603 * accum buffers and related parameters.
604 *
Brian Paul8b332582004-06-11 22:44:22 +0000605 * Note that the actual depth/stencil/accum/etc buffers are not allocated
606 * at this time. It's up to the device driver and/or swrast module to
607 * allocate them as needed.
608 *
Keith Whitwell6dc85572003-07-17 13:43:59 +0000609 * \param visual a GLvisual pointer (we copy the struct contents)
610 * \param softwareDepth create/use a software depth buffer?
611 * \param softwareStencil create/use a software stencil buffer?
612 * \param softwareAccum create/use a software accum buffer?
613 * \param softwareAlpha create/use a software alpha buffer?
614 *
615 * \return pointer to new GLframebuffer struct or NULL if error.
616 *
Brian Paul894844a2004-03-21 17:05:03 +0000617 * \note Need to add softwareAuxBuffers parameter.
Brian Paul4d053dd2000-01-14 04:45:47 +0000618 */
Brian Paul178a1c52000-04-22 01:05:00 +0000619GLframebuffer *
Brian Paulbe3602d2001-02-28 00:27:48 +0000620_mesa_create_framebuffer( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +0000621 GLboolean softwareDepth,
622 GLboolean softwareStencil,
623 GLboolean softwareAccum,
624 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000625{
Brian Paul178a1c52000-04-22 01:05:00 +0000626 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
627 assert(visual);
628 if (buffer) {
629 _mesa_initialize_framebuffer(buffer, visual,
630 softwareDepth, softwareStencil,
631 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000632 }
Brian Paul178a1c52000-04-22 01:05:00 +0000633 return buffer;
634}
635
Brian Paul894844a2004-03-21 17:05:03 +0000636
Keith Whitwell6dc85572003-07-17 13:43:59 +0000637/**
Keith Whitwell6dc85572003-07-17 13:43:59 +0000638 * Makes some sanity checks and fills in the fields of the
639 * GLframebuffer structure with the given parameters.
Brian Paul894844a2004-03-21 17:05:03 +0000640 *
641 * \sa _mesa_create_framebuffer() above for the parameter description.
Brian Paul178a1c52000-04-22 01:05:00 +0000642 */
643void
644_mesa_initialize_framebuffer( GLframebuffer *buffer,
Brian Paulbe3602d2001-02-28 00:27:48 +0000645 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +0000646 GLboolean softwareDepth,
647 GLboolean softwareStencil,
648 GLboolean softwareAccum,
649 GLboolean softwareAlpha )
650{
Brian Paul894844a2004-03-21 17:05:03 +0000651 GLboolean softwareAux = GL_FALSE;
Brian Paul178a1c52000-04-22 01:05:00 +0000652 assert(buffer);
653 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000654
Brian Paul6ec6b842002-10-30 19:49:29 +0000655 _mesa_bzero(buffer, sizeof(GLframebuffer));
Brian Paul85d81602002-06-17 23:36:31 +0000656
Brian Paul4d053dd2000-01-14 04:45:47 +0000657 /* sanity checks */
658 if (softwareDepth ) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000659 assert(visual->depthBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000660 }
661 if (softwareStencil) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000662 assert(visual->stencilBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000663 }
664 if (softwareAccum) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000665 assert(visual->rgbMode);
666 assert(visual->accumRedBits > 0);
667 assert(visual->accumGreenBits > 0);
668 assert(visual->accumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000669 }
670 if (softwareAlpha) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000671 assert(visual->rgbMode);
672 assert(visual->alphaBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000673 }
674
Brian Paul75978bd2001-04-27 21:17:20 +0000675 buffer->Visual = *visual;
Brian Paul4d053dd2000-01-14 04:45:47 +0000676 buffer->UseSoftwareDepthBuffer = softwareDepth;
677 buffer->UseSoftwareStencilBuffer = softwareStencil;
678 buffer->UseSoftwareAccumBuffer = softwareAccum;
679 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul894844a2004-03-21 17:05:03 +0000680 buffer->UseSoftwareAuxBuffers = softwareAux;
Brian Paul4d053dd2000-01-14 04:45:47 +0000681}
682
Brian Paul894844a2004-03-21 17:05:03 +0000683
Keith Whitwell6dc85572003-07-17 13:43:59 +0000684/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000685 * Free a framebuffer struct and its buffers.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000686 *
687 * Calls _mesa_free_framebuffer_data() and frees the structure.
Brian Paul4d053dd2000-01-14 04:45:47 +0000688 */
Brian Paul178a1c52000-04-22 01:05:00 +0000689void
Brian Paulb1394fa2000-09-26 20:53:53 +0000690_mesa_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000691{
692 if (buffer) {
Brian Paul75978bd2001-04-27 21:17:20 +0000693 _mesa_free_framebuffer_data(buffer);
Brian Paul4d053dd2000-01-14 04:45:47 +0000694 FREE(buffer);
695 }
696}
697
Brian Paul894844a2004-03-21 17:05:03 +0000698
Keith Whitwell6dc85572003-07-17 13:43:59 +0000699/**
700 * Free the data hanging off of \p buffer, but not \p buffer itself.
701 *
702 * \param buffer framebuffer.
703 *
704 * Frees all the buffers associated with the structure.
Brian Paul75978bd2001-04-27 21:17:20 +0000705 */
706void
707_mesa_free_framebuffer_data( GLframebuffer *buffer )
708{
709 if (!buffer)
710 return;
711
Brian Paul87506682003-05-27 15:20:43 +0000712 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
Brian Paulaeb44342002-03-19 16:47:04 +0000713 MESA_PBUFFER_FREE( buffer->DepthBuffer );
Brian Paul75978bd2001-04-27 21:17:20 +0000714 buffer->DepthBuffer = NULL;
715 }
Brian Paul87506682003-05-27 15:20:43 +0000716 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
Brian Paulaeb44342002-03-19 16:47:04 +0000717 MESA_PBUFFER_FREE( buffer->Accum );
Brian Paul75978bd2001-04-27 21:17:20 +0000718 buffer->Accum = NULL;
719 }
Brian Paul87506682003-05-27 15:20:43 +0000720 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
Brian Paulaeb44342002-03-19 16:47:04 +0000721 MESA_PBUFFER_FREE( buffer->Stencil );
Brian Paul75978bd2001-04-27 21:17:20 +0000722 buffer->Stencil = NULL;
723 }
Brian Paul87506682003-05-27 15:20:43 +0000724 if (buffer->UseSoftwareAlphaBuffers){
725 if (buffer->FrontLeftAlpha) {
726 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
727 buffer->FrontLeftAlpha = NULL;
728 }
729 if (buffer->BackLeftAlpha) {
730 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
731 buffer->BackLeftAlpha = NULL;
732 }
733 if (buffer->FrontRightAlpha) {
734 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
735 buffer->FrontRightAlpha = NULL;
736 }
737 if (buffer->BackRightAlpha) {
738 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
739 buffer->BackRightAlpha = NULL;
740 }
Brian Paul75978bd2001-04-27 21:17:20 +0000741 }
742}
743
Keith Whitwell6dc85572003-07-17 13:43:59 +0000744/*@}*/
Brian Paul75978bd2001-04-27 21:17:20 +0000745
Brian Paul4d053dd2000-01-14 04:45:47 +0000746
747/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000748/** \name Context allocation, initialization, destroying
749 *
750 * The purpose of the most initialization functions here is to provide the
751 * default state values according to the OpenGL specification.
752 */
jtgafb833d1999-08-19 00:55:39 +0000753/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000754/*@{*/
jtgafb833d1999-08-19 00:55:39 +0000755
Keith Whitwell6dc85572003-07-17 13:43:59 +0000756/**
757 * One-time initialization mutex lock.
758 *
759 * \sa Used by one_time_init().
760 */
Brian Paul9560f052000-01-31 23:11:39 +0000761_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
762
Keith Whitwell6dc85572003-07-17 13:43:59 +0000763/**
764 * Calls all the various one-time-init functions in Mesa.
765 *
766 * While holding a global mutex lock, calls several initialization functions,
767 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
768 * defined.
769 *
770 * \sa _mesa_init_lists(), _math_init().
jtgafb833d1999-08-19 00:55:39 +0000771 */
Brian Paul178a1c52000-04-22 01:05:00 +0000772static void
Brian Paul4753d602002-06-15 02:38:15 +0000773one_time_init( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000774{
775 static GLboolean alreadyCalled = GL_FALSE;
Brian Paula6c423d2004-08-25 15:59:48 +0000776 (void) ctx;
Brian Paul9560f052000-01-31 23:11:39 +0000777 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000778 if (!alreadyCalled) {
Brian Paul27558a12003-03-01 01:50:20 +0000779 GLuint i;
780
Brian Paul4d053dd2000-01-14 04:45:47 +0000781 /* do some implementation tests */
782 assert( sizeof(GLbyte) == 1 );
Brian Paul4d053dd2000-01-14 04:45:47 +0000783 assert( sizeof(GLubyte) == 1 );
Brian Paul894844a2004-03-21 17:05:03 +0000784 assert( sizeof(GLshort) == 2 );
785 assert( sizeof(GLushort) == 2 );
786 assert( sizeof(GLint) == 4 );
787 assert( sizeof(GLuint) == 4 );
Brian Paul4d053dd2000-01-14 04:45:47 +0000788
Brian Paul08836342001-03-03 20:33:27 +0000789 _mesa_init_lists();
Keith Whitwell23caf202000-11-16 21:05:34 +0000790
Keith Whitwell6dc85572003-07-17 13:43:59 +0000791#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000792 _math_init();
Brian Paul27558a12003-03-01 01:50:20 +0000793
794 for (i = 0; i < 256; i++) {
795 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
796 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000797#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000798
davem69775355a2001-06-05 23:54:00 +0000799#ifdef USE_SPARC_ASM
800 _mesa_init_sparc_glapi_relocs();
801#endif
Brian Paul3c634522002-10-24 23:57:19 +0000802 if (_mesa_getenv("MESA_DEBUG")) {
Brian Paul68ee4bc2000-01-28 19:02:22 +0000803 _glapi_noop_enable_warnings(GL_TRUE);
Brian Paul71072be2002-10-10 00:22:13 +0000804#ifndef GLX_DIRECT_RENDERING
805 /* libGL from before 2002/06/28 don't have this function. Someday,
806 * when newer libGL libs are common, remove the #ifdef test. This
807 * only serves to print warnings when calling undefined GL functions.
808 */
Brian Paul4e9676f2002-06-29 19:48:15 +0000809 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
Brian Paul71072be2002-10-10 00:22:13 +0000810#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000811 }
812 else {
813 _glapi_noop_enable_warnings(GL_FALSE);
814 }
815
jtgafb833d1999-08-19 00:55:39 +0000816#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
Brian Paul4e9676f2002-06-29 19:48:15 +0000817 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
jtgafb833d1999-08-19 00:55:39 +0000818#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000819
820 alreadyCalled = GL_TRUE;
821 }
Brian Paul9560f052000-01-31 23:11:39 +0000822 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000823}
824
Brian Paul894844a2004-03-21 17:05:03 +0000825
Keith Whitwell6dc85572003-07-17 13:43:59 +0000826/**
jtgafb833d1999-08-19 00:55:39 +0000827 * Allocate and initialize a shared context state structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000828 * Initializes the display list, texture objects and vertex programs hash
829 * tables, allocates the texture objects. If it runs out of memory, frees
830 * everything already allocated before returning NULL.
Brian Paul894844a2004-03-21 17:05:03 +0000831 *
832 * \return pointer to a gl_shared_state structure on success, or NULL on
833 * failure.
jtgafb833d1999-08-19 00:55:39 +0000834 */
Brian Paula3f13702003-04-01 16:41:50 +0000835static GLboolean
836alloc_shared_state( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000837{
Brian Paula3f13702003-04-01 16:41:50 +0000838 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000839 if (!ss)
Brian Paula3f13702003-04-01 16:41:50 +0000840 return GL_FALSE;
841
842 ctx->Shared = ss;
jtgafb833d1999-08-19 00:55:39 +0000843
Brian Paule4b684c2000-09-12 21:07:40 +0000844 _glthread_INIT_MUTEX(ss->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000845
Brian Paule4b684c2000-09-12 21:07:40 +0000846 ss->DisplayList = _mesa_NewHashTable();
Brian Paulbb797902000-01-24 16:19:54 +0000847 ss->TexObjects = _mesa_NewHashTable();
Brian Paul451f3102003-04-17 01:48:19 +0000848#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
Brian Paul610d5992003-01-14 04:55:45 +0000849 ss->Programs = _mesa_NewHashTable();
Brian Paul8dfc5b92002-10-16 17:57:51 +0000850#endif
jtgafb833d1999-08-19 00:55:39 +0000851
Brian Paul451f3102003-04-17 01:48:19 +0000852#if FEATURE_ARB_vertex_program
Brian Paul4d859f72004-01-23 18:57:05 +0000853 ss->DefaultVertexProgram = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
Brian Paul451f3102003-04-17 01:48:19 +0000854 if (!ss->DefaultVertexProgram)
855 goto cleanup;
856#endif
857#if FEATURE_ARB_fragment_program
Brian Paul4d859f72004-01-23 18:57:05 +0000858 ss->DefaultFragmentProgram = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
Brian Paul451f3102003-04-17 01:48:19 +0000859 if (!ss->DefaultFragmentProgram)
860 goto cleanup;
861#endif
862
Ian Romanick0207b472003-09-09 00:10:12 +0000863 ss->BufferObjects = _mesa_NewHashTable();
864
Brian Paula3f13702003-04-01 16:41:50 +0000865 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
866 if (!ss->Default1D)
867 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000868
Brian Paula3f13702003-04-01 16:41:50 +0000869 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
870 if (!ss->Default2D)
871 goto cleanup;
jtgafb833d1999-08-19 00:55:39 +0000872
Brian Paula3f13702003-04-01 16:41:50 +0000873 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
874 if (!ss->Default3D)
875 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000876
Brian Paula3f13702003-04-01 16:41:50 +0000877 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
878 if (!ss->DefaultCubeMap)
879 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000880
Brian Paula3f13702003-04-01 16:41:50 +0000881 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
882 if (!ss->DefaultRect)
883 goto cleanup;
Brian Paul413d6a22000-05-26 14:44:59 +0000884
Brian Paula3f13702003-04-01 16:41:50 +0000885 /* Effectively bind the default textures to all texture units */
886 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
887 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
888 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
889 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
890 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
891
892 return GL_TRUE;
893
894 cleanup:
895 /* Ran out of memory at some point. Free everything and return NULL */
896 if (ss->DisplayList)
897 _mesa_DeleteHashTable(ss->DisplayList);
898 if (ss->TexObjects)
899 _mesa_DeleteHashTable(ss->TexObjects);
900#if FEATURE_NV_vertex_program
901 if (ss->Programs)
902 _mesa_DeleteHashTable(ss->Programs);
903#endif
Brian Paul451f3102003-04-17 01:48:19 +0000904#if FEATURE_ARB_vertex_program
905 if (ss->DefaultVertexProgram)
Brian Paul4d859f72004-01-23 18:57:05 +0000906 ctx->Driver.DeleteProgram(ctx, ss->DefaultVertexProgram);
Brian Paul451f3102003-04-17 01:48:19 +0000907#endif
908#if FEATURE_ARB_fragment_program
909 if (ss->DefaultFragmentProgram)
Brian Paul4d859f72004-01-23 18:57:05 +0000910 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentProgram);
Brian Paul451f3102003-04-17 01:48:19 +0000911#endif
Brian Paul21841f02004-08-14 14:28:11 +0000912#if FEATURE_ARB_vertex_buffer_object
Ian Romanick0207b472003-09-09 00:10:12 +0000913 if (ss->BufferObjects)
914 _mesa_DeleteHashTable(ss->BufferObjects);
Brian Paul21841f02004-08-14 14:28:11 +0000915#endif
Ian Romanick0207b472003-09-09 00:10:12 +0000916
Brian Paula3f13702003-04-01 16:41:50 +0000917 if (ss->Default1D)
918 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
919 if (ss->Default2D)
920 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
921 if (ss->Default3D)
922 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
923 if (ss->DefaultCubeMap)
924 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
925 if (ss->DefaultRect)
926 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
927 if (ss)
928 _mesa_free(ss);
929 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000930}
931
Keith Whitwell6dc85572003-07-17 13:43:59 +0000932/**
jtgafb833d1999-08-19 00:55:39 +0000933 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000934 *
935 * \param ctx GL context.
936 * \param ss shared state pointer.
937 *
938 * Frees the display lists, the texture objects (calling the driver texture
939 * deletion callback to free its private data) and the vertex programs, as well
940 * as their hash tables.
941 *
942 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000943 */
Brian Paul178a1c52000-04-22 01:05:00 +0000944static void
945free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000946{
947 /* Free display lists */
948 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000949 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000950 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000951 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000952 }
953 else {
954 break;
955 }
956 }
Brian Paulbb797902000-01-24 16:19:54 +0000957 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000958
959 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000960 ASSERT(ctx->Driver.DeleteTexture);
Brian Paul21841f02004-08-14 14:28:11 +0000961 /* the default textures */
962 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
963 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
964 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
965 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
966 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
967 /* all other textures */
Brian Paula3f13702003-04-01 16:41:50 +0000968 while (1) {
969 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
970 if (texName) {
971 struct gl_texture_object *texObj = (struct gl_texture_object *)
972 _mesa_HashLookup(ss->TexObjects, texName);
973 ASSERT(texObj);
974 (*ctx->Driver.DeleteTexture)(ctx, texObj);
975 _mesa_HashRemove(ss->TexObjects, texName);
976 }
977 else {
978 break;
979 }
jtgafb833d1999-08-19 00:55:39 +0000980 }
Brian Paulbb797902000-01-24 16:19:54 +0000981 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000982
Brian Paul8dfc5b92002-10-16 17:57:51 +0000983#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +0000984 /* Free vertex programs */
985 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +0000986 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +0000987 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +0000988 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
989 prog);
990 ASSERT(p);
Brian Paul4d859f72004-01-23 18:57:05 +0000991 ctx->Driver.DeleteProgram(ctx, p);
Brian Paul451f3102003-04-17 01:48:19 +0000992 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +0000993 }
994 else {
995 break;
996 }
997 }
Brian Paul610d5992003-01-14 04:55:45 +0000998 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000999#endif
Brian Paul21841f02004-08-14 14:28:11 +00001000#if FEATURE_ARB_vertex_program
1001 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
1002#endif
1003#if FEATURE_ARB_fragment_program
1004 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
1005#endif
Brian Paul30f51ae2001-12-18 04:06:44 +00001006
Brian Paul21841f02004-08-14 14:28:11 +00001007#if FEATURE_ARB_vertex_buffer_object
Ian Romanick0207b472003-09-09 00:10:12 +00001008 _mesa_DeleteHashTable(ss->BufferObjects);
Brian Paul21841f02004-08-14 14:28:11 +00001009#endif
Keith Whitwelle15fd852002-12-12 13:03:15 +00001010 _glthread_DESTROY_MUTEX(ss->Mutex);
1011
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001012 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +00001013}
1014
1015
Brian Paul4d859f72004-01-23 18:57:05 +00001016/**
1017 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
1018 */
1019static void
1020_mesa_init_current( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001021{
Brian Paul88bf0382004-02-13 15:30:08 +00001022 GLuint i;
jtgafb833d1999-08-19 00:55:39 +00001023
Keith Whitwell6dc85572003-07-17 13:43:59 +00001024 /* Current group */
1025 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
1026 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +00001027 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001028 /* special cases: */
1029 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
1030 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
1031 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
Brian Paul88bf0382004-02-13 15:30:08 +00001032 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001033 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
Brian Paul88bf0382004-02-13 15:30:08 +00001034
Keith Whitwell6dc85572003-07-17 13:43:59 +00001035 ctx->Current.Index = 1;
1036 ctx->Current.EdgeFlag = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001037}
1038
1039
Brian Paul4d859f72004-01-23 18:57:05 +00001040/**
1041 * Initialize fields of gl_constants (aka ctx->Const.*).
1042 * Use defaults from config.h. The device drivers will often override
1043 * some of these values (such as number of texture units).
1044 */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001045static void
1046_mesa_init_constants( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001047{
Brian Paul4d053dd2000-01-14 04:45:47 +00001048 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +00001049
Brian Paulcd1cefa2001-06-13 14:56:14 +00001050 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1051 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1052
Brian Paul53f82c52004-10-02 16:39:09 +00001053 /* Constants, may be overriden (usually only reduced) by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +00001054 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
Brian Paulcd1cefa2001-06-13 14:56:14 +00001055 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1056 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
Brian Paul8afe7de2002-06-15 03:03:06 +00001057 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001058 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
Brian Paul610d5992003-01-14 04:55:45 +00001059 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1060 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
Gareth Hughes2c3d34c2001-03-18 08:53:49 +00001061 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
Brian Paul87c964d2001-11-06 15:53:00 +00001062 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
Brian Paul4d053dd2000-01-14 04:45:47 +00001063 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +00001064 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1065 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1066 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1067 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1068 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001069 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001070 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1071 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1072 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1073 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001074 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
Brian Paul4bdcfe52000-04-17 17:57:04 +00001075 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +00001076 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1077 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
Brian Paula8644322000-11-27 18:22:13 +00001078 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1079 ctx->Const.MaxLights = MAX_LIGHTS;
Ian Romanick882caa12003-05-30 21:37:14 +00001080 ctx->Const.MaxShininess = 128.0;
Brian Paul53f82c52004-10-02 16:39:09 +00001081 ctx->Const.MaxSpotExponent = 128.0;
1082 ctx->Const.MaxViewportWidth = MAX_WIDTH;
1083 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
Brian Pauld0492cf2003-04-11 01:20:06 +00001084#if FEATURE_ARB_vertex_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001085 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001086 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1087 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1088 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1089 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1090 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001091#endif
1092#if FEATURE_ARB_fragment_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001093 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001094 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1095 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1096 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1097 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1098 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1099 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1100 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1101 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001102#endif
Brian Pauledd67742003-04-18 18:02:43 +00001103 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1104 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
Brian Pauld0492cf2003-04-11 01:20:06 +00001105
Brian Paula2b9bad2003-11-10 19:08:37 +00001106 /* If we're running in the X server, do bounds checking to prevent
1107 * segfaults and server crashes!
1108 */
1109#if defined(XFree86LOADER) && defined(IN_MODULE)
1110 ctx->Const.CheckArrayBounds = GL_TRUE;
1111#else
1112 ctx->Const.CheckArrayBounds = GL_FALSE;
1113#endif
1114
Brian Paul53f82c52004-10-02 16:39:09 +00001115 ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
1116
Ian Romanick33899b72004-10-16 01:16:54 +00001117 ctx->Const.ColorReadFormat = GL_RGBA;
1118 ctx->Const.ColorReadType = GL_UNSIGNED_BYTE;
1119
Brian Paul53f82c52004-10-02 16:39:09 +00001120 /* sanity checks */
Brian Paul92f97852003-05-01 22:44:02 +00001121 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
Keith Whitwell6dc85572003-07-17 13:43:59 +00001122}
jtgafb833d1999-08-19 00:55:39 +00001123
Brian Paul4d859f72004-01-23 18:57:05 +00001124
Keith Whitwell6dc85572003-07-17 13:43:59 +00001125/**
1126 * Initialize the attribute groups in a GL context.
1127 *
1128 * \param ctx GL context.
1129 *
1130 * Initializes all the attributes, calling the respective <tt>init*</tt>
1131 * functions for the more complex data structures.
1132 */
1133static GLboolean
1134init_attrib_groups( GLcontext *ctx )
1135{
1136 assert(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001137
Keith Whitwell6dc85572003-07-17 13:43:59 +00001138 /* Constants */
1139 _mesa_init_constants( ctx );
Brian Paul0771d152000-04-07 00:19:41 +00001140
Brian Paul4d053dd2000-01-14 04:45:47 +00001141 /* Extensions */
Brian Paulde4f4602003-07-03 02:15:06 +00001142 _mesa_init_extensions( ctx );
jtgafb833d1999-08-19 00:55:39 +00001143
Keith Whitwell6dc85572003-07-17 13:43:59 +00001144 /* Attribute Groups */
1145 _mesa_init_accum( ctx );
1146 _mesa_init_attrib( ctx );
1147 _mesa_init_buffers( ctx );
Brian Paul148a2842003-09-17 03:40:11 +00001148 _mesa_init_buffer_objects( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001149 _mesa_init_color( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001150 _mesa_init_colortables( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001151 _mesa_init_current( ctx );
1152 _mesa_init_depth( ctx );
1153 _mesa_init_debug( ctx );
1154 _mesa_init_display_list( ctx );
1155 _mesa_init_eval( ctx );
1156 _mesa_init_feedback( ctx );
1157 _mesa_init_fog( ctx );
1158 _mesa_init_histogram( ctx );
1159 _mesa_init_hint( ctx );
1160 _mesa_init_line( ctx );
1161 _mesa_init_lighting( ctx );
1162 _mesa_init_matrix( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001163 _mesa_init_occlude( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001164 _mesa_init_pixel( ctx );
1165 _mesa_init_point( ctx );
1166 _mesa_init_polygon( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001167 _mesa_init_program( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001168 _mesa_init_rastpos( ctx );
1169 _mesa_init_stencil( ctx );
1170 _mesa_init_transform( ctx );
1171 _mesa_init_varray( ctx );
1172 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001173
Keith Whitwell6dc85572003-07-17 13:43:59 +00001174 if (!_mesa_init_texture( ctx ))
1175 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001176
Brian Paul8f04c122004-04-27 13:39:20 +00001177 _mesa_init_texture_s3tc( ctx );
1178 _mesa_init_texture_fxt1( ctx );
1179
Brian Paul4d053dd2000-01-14 04:45:47 +00001180 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001181 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001182 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paulf782b812002-10-04 17:37:45 +00001183 ctx->_Facing = 0;
Brian Paul4923e192004-02-28 22:30:58 +00001184#if CHAN_TYPE == GL_FLOAT
1185 ctx->ClampFragmentColors = GL_FALSE; /* XXX temporary */
1186#else
1187 ctx->ClampFragmentColors = GL_TRUE;
1188#endif
1189 ctx->ClampVertexColors = GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001190
Brian Paula3f13702003-04-01 16:41:50 +00001191 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001192}
1193
1194
Brian Paulf44898c2003-07-18 15:44:57 +00001195/**
1196 * If the DRI libGL.so library is old, it may not have the entrypoints for
1197 * some recent OpenGL extensions. Dynamically add them now.
1198 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1199 * and driver code, this won't be an issue (and calling this function won't
1200 * do any harm).
1201 */
1202static void
1203add_newer_entrypoints(void)
1204{
Brian Paulde434842004-11-27 20:14:03 +00001205 unsigned i;
Ian Romanick3baefe62003-08-22 23:28:03 +00001206 static const struct {
1207 const char * const name;
Brian Paulde434842004-11-27 20:14:03 +00001208 unsigned offset;
Ian Romanick3baefe62003-08-22 23:28:03 +00001209 }
1210 newer_entrypoints[] = {
1211 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1212 { "glWindowPos2dARB", 513 },
1213 { "glWindowPos2dvARB", 514 },
1214 { "glWindowPos2fARB", 515 },
1215 { "glWindowPos2fvARB", 516 },
1216 { "glWindowPos2iARB", 517 },
1217 { "glWindowPos2ivARB", 518 },
1218 { "glWindowPos2sARB", 519 },
1219 { "glWindowPos2svARB", 520 },
1220 { "glWindowPos3dARB", 521 },
1221 { "glWindowPos3dvARB", 522 },
1222 { "glWindowPos3fARB", 523 },
1223 { "glWindowPos3fvARB", 524 },
1224 { "glWindowPos3iARB", 525 },
1225 { "glWindowPos3ivARB", 526 },
1226 { "glWindowPos3sARB", 527 },
1227 { "glWindowPos3svARB", 528 },
Brian Paulf44898c2003-07-18 15:44:57 +00001228#if FEATURE_NV_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001229 { "glAreProgramsResidentNV", 578 },
1230 { "glBindProgramNV", 579 },
1231 { "glDeleteProgramsNV", 580 },
1232 { "glExecuteProgramNV", 581 },
1233 { "glGenProgramsNV", 582 },
1234 { "glGetProgramParameterdvNV", 583 },
1235 { "glGetProgramParameterfvNV", 584 },
1236 { "glGetProgramivNV", 585 },
1237 { "glGetProgramStringNV", 586 },
1238 { "glGetTrackMatrixivNV", 587 },
1239 { "glGetVertexAttribdvNV", 588 },
1240 { "glGetVertexAttribfvNV", 589 },
1241 { "glGetVertexAttribivNV", 590 },
1242 { "glGetVertexAttribPointervNV", 591 },
1243 { "glIsProgramNV", 592 },
1244 { "glLoadProgramNV", 593 },
1245 { "glProgramParameter4dNV", 594 },
1246 { "glProgramParameter4dvNV", 595 },
1247 { "glProgramParameter4fNV", 596 },
1248 { "glProgramParameter4fvNV", 597 },
1249 { "glProgramParameters4dvNV", 598 },
1250 { "glProgramParameters4fvNV", 599 },
1251 { "glRequestResidentProgramsNV", 600 },
1252 { "glTrackMatrixNV", 601 },
1253 { "glVertexAttribPointerNV", 602 },
1254 { "glVertexAttrib1dNV", 603 },
1255 { "glVertexAttrib1dvNV", 604 },
1256 { "glVertexAttrib1fNV", 605 },
1257 { "glVertexAttrib1fvNV", 606 },
1258 { "glVertexAttrib1sNV", 607 },
1259 { "glVertexAttrib1svNV", 608 },
1260 { "glVertexAttrib2dNV", 609 },
1261 { "glVertexAttrib2dvNV", 610 },
1262 { "glVertexAttrib2fNV", 611 },
1263 { "glVertexAttrib2fvNV", 612 },
1264 { "glVertexAttrib2sNV", 613 },
1265 { "glVertexAttrib2svNV", 614 },
1266 { "glVertexAttrib3dNV", 615 },
1267 { "glVertexAttrib3dvNV", 616 },
1268 { "glVertexAttrib3fNV", 617 },
1269 { "glVertexAttrib3fvNV", 618 },
1270 { "glVertexAttrib3sNV", 619 },
1271 { "glVertexAttrib3svNV", 620 },
1272 { "glVertexAttrib4dNV", 621 },
1273 { "glVertexAttrib4dvNV", 622 },
1274 { "glVertexAttrib4fNV", 623 },
1275 { "glVertexAttrib4fvNV", 624 },
1276 { "glVertexAttrib4sNV", 625 },
1277 { "glVertexAttrib4svNV", 626 },
1278 { "glVertexAttrib4ubNV", 627 },
1279 { "glVertexAttrib4ubvNV", 628 },
1280 { "glVertexAttribs1dvNV", 629 },
1281 { "glVertexAttribs1fvNV", 630 },
1282 { "glVertexAttribs1svNV", 631 },
1283 { "glVertexAttribs2dvNV", 632 },
1284 { "glVertexAttribs2fvNV", 633 },
1285 { "glVertexAttribs2svNV", 634 },
1286 { "glVertexAttribs3dvNV", 635 },
1287 { "glVertexAttribs3fvNV", 636 },
1288 { "glVertexAttribs3svNV", 637 },
1289 { "glVertexAttribs4dvNV", 638 },
1290 { "glVertexAttribs4fvNV", 639 },
1291 { "glVertexAttribs4svNV", 640 },
1292 { "glVertexAttribs4ubvNV", 641 },
Brian Paulf44898c2003-07-18 15:44:57 +00001293#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001294 { "glPointParameteriNV", 642 },
1295 { "glPointParameterivNV", 643 },
1296 { "glMultiDrawArraysEXT", 644 },
1297 { "glMultiDrawElementsEXT", 645 },
1298 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1299 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1300 { "glActiveStencilFaceEXT", 646 },
Brian Paulf44898c2003-07-18 15:44:57 +00001301#if FEATURE_NV_fence
Ian Romanick3baefe62003-08-22 23:28:03 +00001302 { "glDeleteFencesNV", 647 },
1303 { "glGenFencesNV", 648 },
1304 { "glIsFenceNV", 649 },
1305 { "glTestFenceNV", 650 },
1306 { "glGetFenceivNV", 651 },
1307 { "glFinishFenceNV", 652 },
1308 { "glSetFenceNV", 653 },
Brian Paulf44898c2003-07-18 15:44:57 +00001309#endif
1310#if FEATURE_NV_fragment_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001311 { "glProgramNamedParameter4fNV", 682 },
1312 { "glProgramNamedParameter4dNV", 683 },
1313 { "glProgramNamedParameter4fvNV", 683 },
1314 { "glProgramNamedParameter4dvNV", 684 },
1315 { "glGetProgramNamedParameterfvNV", 685 },
1316 { "glGetProgramNamedParameterdvNV", 686 },
Brian Paulf44898c2003-07-18 15:44:57 +00001317#endif
1318#if FEATURE_ARB_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001319 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1320 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1321 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1322 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1323 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1324 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1325 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1326 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1327 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1328 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1329 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1330 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1331 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1332 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1333 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1334 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1335 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1336 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1337 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1338 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1339 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1340 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1341 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1342 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1343 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1344 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1345 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1346 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1347 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1348 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1349 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1350 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1351 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1352 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1353 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1354 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1355 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1356 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1357 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1358 { "glProgramStringARB", _gloffset_ProgramStringARB },
1359 { "glBindProgramARB", _gloffset_BindProgramNV },
1360 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1361 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1362 { "glIsProgramARB", _gloffset_IsProgramNV },
1363 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1364 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1365 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1366 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1367 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1368 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1369 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1370 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1371 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1372 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1373 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1374 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1375 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1376 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1377 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1378 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1379 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1380 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
Brian Paulf44898c2003-07-18 15:44:57 +00001381#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001382 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1383 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
Brian Paulde434842004-11-27 20:14:03 +00001384 /* GL_EXT_stencil_two_side */
1385 { "glActiveStencilFaceEXT", _gloffset_ActiveStencilFaceEXT },
1386 /* GL_ARB_draw_buffers */
1387 { "glDrawBuffersARB", _gloffset_DrawBuffersARB },
Ian Romanick3baefe62003-08-22 23:28:03 +00001388 };
1389
Brian Paulde434842004-11-27 20:14:03 +00001390 for (i = 0; i < Elements(newer_entrypoints); i++ ) {
Ian Romanick3baefe62003-08-22 23:28:03 +00001391 _glapi_add_entrypoint( newer_entrypoints[i].name,
Brian Paulde434842004-11-27 20:14:03 +00001392 newer_entrypoints[i].offset );
Ian Romanick3baefe62003-08-22 23:28:03 +00001393 }
Brian Paulf44898c2003-07-18 15:44:57 +00001394}
1395
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001396
Brian Paulde4f4602003-07-03 02:15:06 +00001397/**
Brian Paul21f69782004-11-27 05:05:32 +00001398 * This is the default function we plug into all dispatch table slots
1399 * This helps prevents a segfault when someone calls a GL function without
1400 * first checking if the extension's supported.
1401 */
1402static int
1403generic_nop(void)
1404{
1405 _mesa_problem(NULL, "User called no-op dispatch function (an unsupported extension function?)");
1406 return 0;
1407}
1408
1409
1410/**
1411 * Allocate and initialize a new dispatch table.
1412 */
1413static struct _glapi_table *
1414alloc_dispatch_table(void)
1415{
1416 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1417 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1418 * Mesa we do this to accomodate different versions of libGL and various
1419 * DRI drivers.
1420 */
1421 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
1422 sizeof(struct _glapi_table) / sizeof(_glapi_proc));
1423 struct _glapi_table *table =
1424 (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc));
1425 if (table) {
1426 _glapi_proc *entry = (_glapi_proc *) table;
Brian Paula760ccf2004-12-03 15:24:34 +00001427 GLint i;
Brian Paul21f69782004-11-27 05:05:32 +00001428 for (i = 0; i < numEntries; i++) {
1429 entry[i] = (_glapi_proc) generic_nop;
1430 }
1431 }
1432 return table;
1433}
1434
1435
1436/**
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001437 * Initialize a GLcontext struct (rendering context).
Keith Whitwell6dc85572003-07-17 13:43:59 +00001438 *
1439 * This includes allocating all the other structs and arrays which hang off of
1440 * the context by pointers.
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001441 * Note that the driver needs to pass in its dd_function_table here since
1442 * we need to at least call driverFunctions->NewTextureObject to create the
1443 * default texture objects.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001444 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001445 * Called by _mesa_create_context().
Keith Whitwell6dc85572003-07-17 13:43:59 +00001446 *
1447 * Performs the imports and exports callback tables initialization, and
1448 * miscellaneous one-time initializations. If no shared context is supplied one
1449 * is allocated, and increase its reference count. Setups the GL API dispatch
1450 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1451 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1452 * for debug flags.
1453 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001454 * \param ctx the context to initialize
1455 * \param visual describes the visual attributes for this context
1456 * \param share_list points to context to share textures, display lists,
1457 * etc with, or NULL
1458 * \param driverFunctions table of device driver functions for this context
1459 * to use
1460 * \param driverContext pointer to driver-specific context data
jtgafb833d1999-08-19 00:55:39 +00001461 */
Brian Paul178a1c52000-04-22 01:05:00 +00001462GLboolean
1463_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001464 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001465 GLcontext *share_list,
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001466 const struct dd_function_table *driverFunctions,
1467 void *driverContext )
jtgafb833d1999-08-19 00:55:39 +00001468{
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001469 ASSERT(driverContext);
1470 assert(driverFunctions->NewTextureObject);
jtgafb833d1999-08-19 00:55:39 +00001471
Brian Paul3c634522002-10-24 23:57:19 +00001472 /* If the driver wants core Mesa to use special imports, it'll have to
1473 * override these defaults.
1474 */
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001475 _mesa_init_default_imports( &(ctx->imports), driverContext );
jtgafb833d1999-08-19 00:55:39 +00001476
Brian Paul9a33a112002-06-13 04:28:29 +00001477 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001478 _mesa_init_default_exports( &(ctx->exports) );
1479
1480 /* misc one-time initializations */
1481 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001482
Brian Paulb1394fa2000-09-26 20:53:53 +00001483 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001484 ctx->DrawBuffer = NULL;
1485 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001486
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001487 /* Plug in driver functions and context pointer here.
1488 * This is important because when we call alloc_shared_state() below
1489 * we'll call ctx->Driver.NewTextureObject() to create the default
1490 * textures.
Brian Paula3f13702003-04-01 16:41:50 +00001491 */
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001492 ctx->Driver = *driverFunctions;
1493 ctx->DriverCtx = driverContext;
Brian Paula3f13702003-04-01 16:41:50 +00001494
jtgafb833d1999-08-19 00:55:39 +00001495 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001496 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001497 ctx->Shared = share_list->Shared;
1498 }
1499 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001500 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001501 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001502 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001503 }
1504 }
Brian Paul9560f052000-01-31 23:11:39 +00001505 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001506 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001507 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001508
Keith Whitwell6dc85572003-07-17 13:43:59 +00001509 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001510 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001511 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001512 }
jtgafb833d1999-08-19 00:55:39 +00001513
Brian Paulf44898c2003-07-18 15:44:57 +00001514 /* libGL ABI coordination */
1515 add_newer_entrypoints();
Keith Whitwell6dc85572003-07-17 13:43:59 +00001516
Brian Paul21f69782004-11-27 05:05:32 +00001517 /* setup the API dispatch tables */
1518 ctx->Exec = alloc_dispatch_table();
1519 ctx->Save = alloc_dispatch_table();
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001520 if (!ctx->Exec || !ctx->Save) {
1521 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001522 if (ctx->Exec)
Brian Paul21f69782004-11-27 05:05:32 +00001523 _mesa_free(ctx->Exec);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001524 }
Brian Paul21f69782004-11-27 05:05:32 +00001525 _mesa_init_exec_table(ctx->Exec);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001526 ctx->CurrentDispatch = ctx->Exec;
Keith Whitwell6dc85572003-07-17 13:43:59 +00001527#if _HAVE_FULL_GL
Brian Paul21f69782004-11-27 05:05:32 +00001528 _mesa_init_dlist_table(ctx->Save);
Keith Whitwellae0eaf92003-11-24 15:23:18 +00001529 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001530 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001531 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001532 ctx->TnlModule.Current = NULL;
1533 ctx->TnlModule.SwapCount = 0;
Keith Whitwell6dc85572003-07-17 13:43:59 +00001534#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001535
Brian Paul4d053dd2000-01-14 04:45:47 +00001536 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001537}
1538
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001539
Brian Paulde4f4602003-07-03 02:15:06 +00001540/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001541 * Allocate and initialize a GLcontext structure.
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001542 * Note that the driver needs to pass in its dd_function_table here since
1543 * we need to at least call driverFunctions->NewTextureObject to initialize
1544 * the rendering context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001545 *
1546 * \param visual a GLvisual pointer (we copy the struct contents)
1547 * \param share_list another context to share display lists with or NULL
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001548 * \param driverFunctions points to the dd_function_table into which the
1549 * driver has plugged in all its special functions.
1550 * \param driverCtx points to the device driver's private context state
Keith Whitwell6dc85572003-07-17 13:43:59 +00001551 *
1552 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001553 */
Brian Paul178a1c52000-04-22 01:05:00 +00001554GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001555_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001556 GLcontext *share_list,
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001557 const struct dd_function_table *driverFunctions,
1558 void *driverContext )
Brian Paul3c634522002-10-24 23:57:19 +00001559
Brian Paul4d053dd2000-01-14 04:45:47 +00001560{
Brian Paul4753d602002-06-15 02:38:15 +00001561 GLcontext *ctx;
1562
1563 ASSERT(visual);
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001564 ASSERT(driverContext);
Brian Paul4753d602002-06-15 02:38:15 +00001565
Brian Paul3c634522002-10-24 23:57:19 +00001566 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001567 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001568 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001569
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001570 if (_mesa_initialize_context(ctx, visual, share_list,
1571 driverFunctions, driverContext)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001572 return ctx;
1573 }
1574 else {
Brian Paul3c634522002-10-24 23:57:19 +00001575 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001576 return NULL;
1577 }
1578}
1579
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001580
Keith Whitwell6dc85572003-07-17 13:43:59 +00001581/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001582 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001583 *
1584 * But doesn't free the GLcontext struct itself.
1585 *
1586 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001587 */
Brian Paul178a1c52000-04-22 01:05:00 +00001588void
Brian Paulb1394fa2000-09-26 20:53:53 +00001589_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001590{
Brian Paul4d053dd2000-01-14 04:45:47 +00001591 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001592 if (ctx == _mesa_get_current_context()) {
1593 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001594 }
1595
Keith Whitwell6dc85572003-07-17 13:43:59 +00001596 _mesa_free_lighting_data( ctx );
1597 _mesa_free_eval_data( ctx );
1598 _mesa_free_texture_data( ctx );
1599 _mesa_free_matrix_data( ctx );
1600 _mesa_free_viewport_data( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001601 _mesa_free_colortables_data( ctx );
Brian Paul21841f02004-08-14 14:28:11 +00001602 _mesa_free_program_data(ctx);
1603 _mesa_free_occlude_data(ctx);
1604
1605#if FEATURE_ARB_vertex_buffer_object
1606 _mesa_delete_buffer_object(ctx, ctx->Array.NullBufferObj);
Brian Paul8dfc5b92002-10-16 17:57:51 +00001607#endif
Brian Paulfd284452001-07-19 15:54:34 +00001608
Brian Paul65a66f52004-11-27 22:47:59 +00001609 /* free dispatch tables */
1610 _mesa_free(ctx->Exec);
1611 _mesa_free(ctx->Save);
1612
Brian Paul30f51ae2001-12-18 04:06:44 +00001613 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001614 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001615 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001616 assert(ctx->Shared->RefCount >= 0);
1617 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1618 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001619 /* free shared state */
1620 free_shared_state( ctx, ctx->Shared );
1621 }
1622
Brian Paul702ca202003-07-18 15:22:16 +00001623 if (ctx->Extensions.String)
1624 FREE((void *) ctx->Extensions.String);
Brian Paul4d053dd2000-01-14 04:45:47 +00001625}
1626
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001627
Keith Whitwell6dc85572003-07-17 13:43:59 +00001628/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001629 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001630 *
1631 * \param ctx GL context.
1632 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001633 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
jtgafb833d1999-08-19 00:55:39 +00001634 */
Brian Paul178a1c52000-04-22 01:05:00 +00001635void
Brian Paulb1394fa2000-09-26 20:53:53 +00001636_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001637{
1638 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001639 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001640 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001641 }
1642}
1643
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001644
Keith Whitwell6dc85572003-07-17 13:43:59 +00001645#if _HAVE_FULL_GL
1646/**
jtgafb833d1999-08-19 00:55:39 +00001647 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001648 *
1649 * \param src source context
1650 * \param dst destination context
1651 * \param mask bitwise OR of GL_*_BIT flags
1652 *
1653 * According to the bits specified in \p mask, copies the corresponding
Jose Fonseca375457b2004-09-09 22:23:24 +00001654 * attributes from \p src into \p dst. For many of the attributes a simple \c
Keith Whitwell6dc85572003-07-17 13:43:59 +00001655 * memcpy is not enough due to the existence of internal pointers in their data
1656 * structures.
jtgafb833d1999-08-19 00:55:39 +00001657 */
Brian Paul178a1c52000-04-22 01:05:00 +00001658void
Brian Paulb1394fa2000-09-26 20:53:53 +00001659_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001660{
1661 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001662 /* OK to memcpy */
1663 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001664 }
1665 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001666 /* OK to memcpy */
1667 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001668 }
1669 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001670 /* OK to memcpy */
1671 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001672 }
1673 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001674 /* OK to memcpy */
1675 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001676 }
1677 if (mask & GL_ENABLE_BIT) {
1678 /* no op */
1679 }
1680 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001681 /* OK to memcpy */
1682 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001683 }
1684 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001685 /* OK to memcpy */
1686 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001687 }
1688 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001689 /* OK to memcpy */
1690 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001691 }
1692 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001693 GLuint i;
1694 /* begin with memcpy */
1695 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1696 /* fixup linked lists to prevent pointer insanity */
1697 make_empty_list( &(dst->Light.EnabledList) );
1698 for (i = 0; i < MAX_LIGHTS; i++) {
1699 if (dst->Light.Light[i].Enabled) {
1700 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1701 }
1702 }
jtgafb833d1999-08-19 00:55:39 +00001703 }
1704 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001705 /* OK to memcpy */
1706 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001707 }
1708 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001709 /* OK to memcpy */
1710 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001711 }
1712 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001713 /* OK to memcpy */
1714 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001715 }
1716 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001717 /* OK to memcpy */
1718 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001719 }
1720 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001721 /* OK to memcpy */
1722 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001723 }
1724 if (mask & GL_POLYGON_STIPPLE_BIT) {
1725 /* Use loop instead of MEMCPY due to problem with Portland Group's
1726 * C compiler. Reported by John Stone.
1727 */
Brian Paul85d81602002-06-17 23:36:31 +00001728 GLuint i;
1729 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001730 dst->PolygonStipple[i] = src->PolygonStipple[i];
1731 }
1732 }
1733 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001734 /* OK to memcpy */
1735 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001736 }
1737 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001738 /* OK to memcpy */
1739 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001740 }
1741 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001742 /* Cannot memcpy because of pointers */
1743 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001744 }
1745 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001746 /* OK to memcpy */
1747 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001748 }
1749 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001750 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1751 dst->Viewport.X = src->Viewport.X;
1752 dst->Viewport.Y = src->Viewport.Y;
1753 dst->Viewport.Width = src->Viewport.Width;
1754 dst->Viewport.Height = src->Viewport.Height;
1755 dst->Viewport.Near = src->Viewport.Near;
1756 dst->Viewport.Far = src->Viewport.Far;
1757 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001758 }
Brian Paul85d81602002-06-17 23:36:31 +00001759
Keith Whitwella96308c2000-10-30 13:31:59 +00001760 /* XXX FIXME: Call callbacks?
1761 */
1762 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001763}
Keith Whitwell23caf202000-11-16 21:05:34 +00001764#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001765
1766
Brian Paulb1d53d92003-06-11 18:48:19 +00001767/**
1768 * Check if the given context can render into the given framebuffer
1769 * by checking visual attributes.
1770 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1771 */
1772static GLboolean
1773check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1774{
1775 const GLvisual *ctxvis = &ctx->Visual;
1776 const GLvisual *bufvis = &buffer->Visual;
1777
1778 if (ctxvis == bufvis)
1779 return GL_TRUE;
1780
1781 if (ctxvis->rgbMode != bufvis->rgbMode)
1782 return GL_FALSE;
1783 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1784 return GL_FALSE;
1785 if (ctxvis->stereoMode && !bufvis->stereoMode)
1786 return GL_FALSE;
1787 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1788 return GL_FALSE;
1789 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1790 return GL_FALSE;
1791 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1792 return GL_FALSE;
1793 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1794 return GL_FALSE;
1795 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1796 return GL_FALSE;
1797 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1798 return GL_FALSE;
1799 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1800 return GL_FALSE;
1801 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1802 return GL_FALSE;
1803
1804 return GL_TRUE;
1805}
1806
1807
Keith Whitwell6dc85572003-07-17 13:43:59 +00001808/**
Brian Paul9a33a112002-06-13 04:28:29 +00001809 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001810 *
1811 * \param newCtx new GL context.
1812 * \param buffer framebuffer.
1813 *
1814 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001815 */
1816void
1817_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1818{
1819 _mesa_make_current2( newCtx, buffer, buffer );
1820}
1821
Keith Whitwell6dc85572003-07-17 13:43:59 +00001822/**
1823 * Bind the given context to the given draw-buffer and read-buffer and
1824 * make it the current context for this thread.
1825 *
1826 * \param newCtx new GL context. If NULL then there will be no current GL
1827 * context.
1828 * \param drawBuffer draw framebuffer.
1829 * \param readBuffer read framebuffer.
1830 *
1831 * Check that the context's and framebuffer's visuals are compatible, returning
1832 * immediately otherwise. Sets the glapi current context via
1833 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1834 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1835 * Calls dd_function_table::MakeCurrent callback if defined.
1836 *
1837 * When a context is bound by the first time and the \c MESA_INFO environment
1838 * variable is set it calls print_info() as an aid for remote user
1839 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001840 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001841void
1842_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1843 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001844{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001845 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001846 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001847
Brian Paulbe3602d2001-02-28 00:27:48 +00001848 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001849 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001850 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1851 if (!check_compatible(newCtx, drawBuffer))
1852 return;
1853 }
1854 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1855 if (!check_compatible(newCtx, readBuffer))
1856 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001857 }
1858
Brian Paul00037781999-12-17 14:52:35 +00001859 /* We call this function periodically (just here for now) in
1860 * order to detect when multithreading has begun.
1861 */
1862 _glapi_check_multithread();
1863
Brian Paulf9b97d92000-01-28 20:17:42 +00001864 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001865 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001866
1867
1868 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001869 _glapi_set_dispatch(NULL); /* none current */
1870 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001871 else {
1872 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001873
Keith Whitwell23caf202000-11-16 21:05:34 +00001874 if (drawBuffer && readBuffer) {
1875 /* TODO: check if newCtx and buffer's visual match??? */
1876 newCtx->DrawBuffer = drawBuffer;
1877 newCtx->ReadBuffer = readBuffer;
1878 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001879
Keith Whitwell6dc85572003-07-17 13:43:59 +00001880#if _HAVE_FULL_GL
Brian Paul65a66f52004-11-27 22:47:59 +00001881 if (!drawBuffer->Initialized) {
Brian Paul10d7f542002-06-17 23:38:14 +00001882 /* get initial window size */
1883 GLuint bufWidth, bufHeight;
Brian Paul65a66f52004-11-27 22:47:59 +00001884 /* ask device driver for size of the buffer */
1885 (*newCtx->Driver.GetBufferSize)(drawBuffer, &bufWidth, &bufHeight);
1886 /* set initial buffer size */
1887 drawBuffer->Width = bufWidth;
1888 drawBuffer->Height = bufHeight;
1889 newCtx->Driver.ResizeBuffers( drawBuffer );
1890 drawBuffer->Initialized = GL_TRUE;
Brian Paul10d7f542002-06-17 23:38:14 +00001891 }
1892
Brian Paul65a66f52004-11-27 22:47:59 +00001893 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
Brian Paul10d7f542002-06-17 23:38:14 +00001894 /* get initial window size */
1895 GLuint bufWidth, bufHeight;
Brian Paul65a66f52004-11-27 22:47:59 +00001896 /* ask device driver for size of the buffer */
1897 (*newCtx->Driver.GetBufferSize)(readBuffer, &bufWidth, &bufHeight);
1898 /* set initial buffer size */
1899 readBuffer->Width = bufWidth;
1900 readBuffer->Height = bufHeight;
1901 newCtx->Driver.ResizeBuffers( readBuffer );
1902 readBuffer->Initialized = GL_TRUE;
Brian Paul10d7f542002-06-17 23:38:14 +00001903 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001904#endif
Brian Paul65a66f52004-11-27 22:47:59 +00001905 if (newCtx->FirstTimeCurrent) {
1906 /* set initial viewport and scissor size now */
1907 _mesa_set_viewport(newCtx, 0, 0, drawBuffer->Width, drawBuffer->Height);
1908 newCtx->Scissor.Width = drawBuffer->Width;
1909 newCtx->Scissor.Height = drawBuffer->Height;
1910 }
Brian Paul00037781999-12-17 14:52:35 +00001911 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001912
Keith Whitwell6dc85572003-07-17 13:43:59 +00001913 /* Alert the driver - usually passed on to the sw t&l module,
1914 * but also used to detect threaded cases in the radeon codegen
1915 * hw t&l module.
1916 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001917 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001918 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1919
1920 /* We can use this to help debug user's problems. Tell them to set
1921 * the MESA_INFO env variable before running their app. Then the
1922 * first time each context is made current we'll print some useful
1923 * information.
1924 */
1925 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001926 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001927 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001928 }
1929 newCtx->FirstTimeCurrent = GL_FALSE;
1930 }
Brian Paul00037781999-12-17 14:52:35 +00001931 }
1932}
1933
Keith Whitwell6dc85572003-07-17 13:43:59 +00001934/**
1935 * Get current context for the calling thread.
1936 *
1937 * \return pointer to the current GL context.
1938 *
1939 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1940 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00001941 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001942GLcontext *
1943_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00001944{
Brian Paulf9b97d92000-01-28 20:17:42 +00001945 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001946}
1947
Keith Whitwell6dc85572003-07-17 13:43:59 +00001948/**
1949 * Get context's current API dispatch table.
1950 *
1951 * It'll either be the immediate-mode execute dispatcher or the display list
1952 * compile dispatcher.
1953 *
1954 * \param ctx GL context.
1955 *
1956 * \return pointer to dispatch_table.
1957 *
1958 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00001959 */
1960struct _glapi_table *
1961_mesa_get_dispatch(GLcontext *ctx)
1962{
1963 return ctx->CurrentDispatch;
1964}
1965
Keith Whitwell6dc85572003-07-17 13:43:59 +00001966/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00001967
1968
jtgafb833d1999-08-19 00:55:39 +00001969/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001970/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00001971/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001972/*@{*/
jtgafb833d1999-08-19 00:55:39 +00001973
Keith Whitwell6dc85572003-07-17 13:43:59 +00001974/**
1975 * Record an error.
1976 *
1977 * \param ctx GL context.
1978 * \param error error code.
1979 *
1980 * Records the given error code and call the driver's dd_function_table::Error
1981 * function if defined.
1982 *
1983 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00001984 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00001985 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001986void
Brian Paul4e9676f2002-06-29 19:48:15 +00001987_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00001988{
Brian Paul18a285a2002-03-16 00:53:15 +00001989 if (!ctx)
1990 return;
1991
Brian Paul7eb06032000-07-14 04:13:40 +00001992 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00001993 ctx->ErrorValue = error;
1994 }
1995
1996 /* Call device driver's error handler, if any. This is used on the Mac. */
1997 if (ctx->Driver.Error) {
1998 (*ctx->Driver.Error)( ctx );
1999 }
2000}
2001
Keith Whitwell6dc85572003-07-17 13:43:59 +00002002/**
2003 * Execute glFinish().
2004 *
2005 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2006 * dd_function_table::Finish driver callback, if not NULL.
2007 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002008void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00002009_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00002010{
Brian Paulfa9df402000-02-02 19:16:46 +00002011 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002012 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00002013 if (ctx->Driver.Finish) {
2014 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00002015 }
2016}
2017
Keith Whitwell6dc85572003-07-17 13:43:59 +00002018/**
2019 * Execute glFlush().
2020 *
2021 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2022 * dd_function_table::Flush driver callback, if not NULL.
2023 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002024void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00002025_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00002026{
Brian Paulfa9df402000-02-02 19:16:46 +00002027 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002028 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00002029 if (ctx->Driver.Flush) {
2030 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00002031 }
jtgafb833d1999-08-19 00:55:39 +00002032}
Brian Paul48c6a6e2000-09-08 21:28:04 +00002033
2034
Keith Whitwell6dc85572003-07-17 13:43:59 +00002035/*@}*/