blob: 98e684d393625d082181049e49d358772a120a38 [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
Dave Airlie7f752fe2004-12-19 03:06:59 +0000862#if FEATURE_ATI_fragment_shader
863 ss->DefaultFragmentShader = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_SHADER_ATI, 0);
864 if (!ss->DefaultFragmentShader)
865 goto cleanup;
866#endif
Brian Paul451f3102003-04-17 01:48:19 +0000867
Ian Romanick0207b472003-09-09 00:10:12 +0000868 ss->BufferObjects = _mesa_NewHashTable();
869
Brian Paula3f13702003-04-01 16:41:50 +0000870 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
871 if (!ss->Default1D)
872 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000873
Brian Paula3f13702003-04-01 16:41:50 +0000874 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
875 if (!ss->Default2D)
876 goto cleanup;
jtgafb833d1999-08-19 00:55:39 +0000877
Brian Paula3f13702003-04-01 16:41:50 +0000878 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
879 if (!ss->Default3D)
880 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000881
Brian Paula3f13702003-04-01 16:41:50 +0000882 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
883 if (!ss->DefaultCubeMap)
884 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000885
Brian Paula3f13702003-04-01 16:41:50 +0000886 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
887 if (!ss->DefaultRect)
888 goto cleanup;
Brian Paul413d6a22000-05-26 14:44:59 +0000889
Brian Paula3f13702003-04-01 16:41:50 +0000890 /* Effectively bind the default textures to all texture units */
891 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
892 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
893 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
894 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
895 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
896
897 return GL_TRUE;
898
899 cleanup:
900 /* Ran out of memory at some point. Free everything and return NULL */
901 if (ss->DisplayList)
902 _mesa_DeleteHashTable(ss->DisplayList);
903 if (ss->TexObjects)
904 _mesa_DeleteHashTable(ss->TexObjects);
905#if FEATURE_NV_vertex_program
906 if (ss->Programs)
907 _mesa_DeleteHashTable(ss->Programs);
908#endif
Brian Paul451f3102003-04-17 01:48:19 +0000909#if FEATURE_ARB_vertex_program
910 if (ss->DefaultVertexProgram)
Brian Paul4d859f72004-01-23 18:57:05 +0000911 ctx->Driver.DeleteProgram(ctx, ss->DefaultVertexProgram);
Brian Paul451f3102003-04-17 01:48:19 +0000912#endif
913#if FEATURE_ARB_fragment_program
914 if (ss->DefaultFragmentProgram)
Brian Paul4d859f72004-01-23 18:57:05 +0000915 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentProgram);
Brian Paul451f3102003-04-17 01:48:19 +0000916#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000917#if FEATURE_ATI_fragment_shader
918 if (ss->DefaultFragmentShader)
919 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentShader);
920#endif
Brian Paul21841f02004-08-14 14:28:11 +0000921#if FEATURE_ARB_vertex_buffer_object
Ian Romanick0207b472003-09-09 00:10:12 +0000922 if (ss->BufferObjects)
923 _mesa_DeleteHashTable(ss->BufferObjects);
Brian Paul21841f02004-08-14 14:28:11 +0000924#endif
Ian Romanick0207b472003-09-09 00:10:12 +0000925
Brian Paula3f13702003-04-01 16:41:50 +0000926 if (ss->Default1D)
927 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
928 if (ss->Default2D)
929 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
930 if (ss->Default3D)
931 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
932 if (ss->DefaultCubeMap)
933 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
934 if (ss->DefaultRect)
935 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
936 if (ss)
937 _mesa_free(ss);
938 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000939}
940
Keith Whitwell6dc85572003-07-17 13:43:59 +0000941/**
jtgafb833d1999-08-19 00:55:39 +0000942 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000943 *
944 * \param ctx GL context.
945 * \param ss shared state pointer.
946 *
947 * Frees the display lists, the texture objects (calling the driver texture
948 * deletion callback to free its private data) and the vertex programs, as well
949 * as their hash tables.
950 *
951 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000952 */
Brian Paul178a1c52000-04-22 01:05:00 +0000953static void
954free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000955{
956 /* Free display lists */
957 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000958 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000959 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000960 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000961 }
962 else {
963 break;
964 }
965 }
Brian Paulbb797902000-01-24 16:19:54 +0000966 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000967
968 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000969 ASSERT(ctx->Driver.DeleteTexture);
Brian Paul21841f02004-08-14 14:28:11 +0000970 /* the default textures */
971 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
972 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
973 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
974 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
975 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
976 /* all other textures */
Brian Paula3f13702003-04-01 16:41:50 +0000977 while (1) {
978 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
979 if (texName) {
980 struct gl_texture_object *texObj = (struct gl_texture_object *)
981 _mesa_HashLookup(ss->TexObjects, texName);
982 ASSERT(texObj);
983 (*ctx->Driver.DeleteTexture)(ctx, texObj);
984 _mesa_HashRemove(ss->TexObjects, texName);
985 }
986 else {
987 break;
988 }
jtgafb833d1999-08-19 00:55:39 +0000989 }
Brian Paulbb797902000-01-24 16:19:54 +0000990 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000991
Brian Paul8dfc5b92002-10-16 17:57:51 +0000992#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +0000993 /* Free vertex programs */
994 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +0000995 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +0000996 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +0000997 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
998 prog);
999 ASSERT(p);
Brian Paul4d859f72004-01-23 18:57:05 +00001000 ctx->Driver.DeleteProgram(ctx, p);
Brian Paul451f3102003-04-17 01:48:19 +00001001 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +00001002 }
1003 else {
1004 break;
1005 }
1006 }
Brian Paul610d5992003-01-14 04:55:45 +00001007 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +00001008#endif
Brian Paul21841f02004-08-14 14:28:11 +00001009#if FEATURE_ARB_vertex_program
1010 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
1011#endif
1012#if FEATURE_ARB_fragment_program
1013 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
1014#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +00001015#if FEATURE_ATI_fragment_shader
1016 _mesa_delete_program(ctx, ss->DefaultFragmentShader);
1017#endif
Brian Paul30f51ae2001-12-18 04:06:44 +00001018
Brian Paul21841f02004-08-14 14:28:11 +00001019#if FEATURE_ARB_vertex_buffer_object
Ian Romanick0207b472003-09-09 00:10:12 +00001020 _mesa_DeleteHashTable(ss->BufferObjects);
Brian Paul21841f02004-08-14 14:28:11 +00001021#endif
Keith Whitwelle15fd852002-12-12 13:03:15 +00001022 _glthread_DESTROY_MUTEX(ss->Mutex);
1023
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001024 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +00001025}
1026
1027
Brian Paul4d859f72004-01-23 18:57:05 +00001028/**
1029 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
1030 */
1031static void
1032_mesa_init_current( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001033{
Brian Paul88bf0382004-02-13 15:30:08 +00001034 GLuint i;
jtgafb833d1999-08-19 00:55:39 +00001035
Keith Whitwell6dc85572003-07-17 13:43:59 +00001036 /* Current group */
1037 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
1038 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +00001039 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001040 /* special cases: */
1041 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
1042 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
1043 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
Brian Paul88bf0382004-02-13 15:30:08 +00001044 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001045 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
Brian Paul88bf0382004-02-13 15:30:08 +00001046
Keith Whitwell6dc85572003-07-17 13:43:59 +00001047 ctx->Current.Index = 1;
1048 ctx->Current.EdgeFlag = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001049}
1050
1051
Brian Paul4d859f72004-01-23 18:57:05 +00001052/**
1053 * Initialize fields of gl_constants (aka ctx->Const.*).
1054 * Use defaults from config.h. The device drivers will often override
1055 * some of these values (such as number of texture units).
1056 */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001057static void
1058_mesa_init_constants( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001059{
Brian Paul4d053dd2000-01-14 04:45:47 +00001060 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +00001061
Brian Paulcd1cefa2001-06-13 14:56:14 +00001062 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1063 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1064
Brian Paul53f82c52004-10-02 16:39:09 +00001065 /* Constants, may be overriden (usually only reduced) by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +00001066 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
Brian Paulcd1cefa2001-06-13 14:56:14 +00001067 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1068 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
Brian Paul8afe7de2002-06-15 03:03:06 +00001069 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001070 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
Brian Paul610d5992003-01-14 04:55:45 +00001071 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1072 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
Gareth Hughes2c3d34c2001-03-18 08:53:49 +00001073 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
Brian Paul87c964d2001-11-06 15:53:00 +00001074 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
Brian Paul4d053dd2000-01-14 04:45:47 +00001075 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +00001076 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1077 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1078 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1079 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1080 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001081 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001082 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1083 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1084 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1085 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001086 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
Brian Paul4bdcfe52000-04-17 17:57:04 +00001087 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +00001088 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1089 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
Brian Paula8644322000-11-27 18:22:13 +00001090 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1091 ctx->Const.MaxLights = MAX_LIGHTS;
Ian Romanick882caa12003-05-30 21:37:14 +00001092 ctx->Const.MaxShininess = 128.0;
Brian Paul53f82c52004-10-02 16:39:09 +00001093 ctx->Const.MaxSpotExponent = 128.0;
1094 ctx->Const.MaxViewportWidth = MAX_WIDTH;
1095 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
Brian Pauld0492cf2003-04-11 01:20:06 +00001096#if FEATURE_ARB_vertex_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001097 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001098 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1099 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1100 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1101 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1102 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001103#endif
1104#if FEATURE_ARB_fragment_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001105 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001106 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1107 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1108 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1109 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1110 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1111 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1112 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1113 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001114#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +00001115
Brian Pauledd67742003-04-18 18:02:43 +00001116 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1117 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
Brian Pauld0492cf2003-04-11 01:20:06 +00001118
Brian Paula2b9bad2003-11-10 19:08:37 +00001119 /* If we're running in the X server, do bounds checking to prevent
1120 * segfaults and server crashes!
1121 */
1122#if defined(XFree86LOADER) && defined(IN_MODULE)
1123 ctx->Const.CheckArrayBounds = GL_TRUE;
1124#else
1125 ctx->Const.CheckArrayBounds = GL_FALSE;
1126#endif
1127
Brian Paul53f82c52004-10-02 16:39:09 +00001128 ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
1129
Ian Romanick33899b72004-10-16 01:16:54 +00001130 ctx->Const.ColorReadFormat = GL_RGBA;
1131 ctx->Const.ColorReadType = GL_UNSIGNED_BYTE;
1132
Brian Paul53f82c52004-10-02 16:39:09 +00001133 /* sanity checks */
Brian Paul92f97852003-05-01 22:44:02 +00001134 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
Keith Whitwell6dc85572003-07-17 13:43:59 +00001135}
jtgafb833d1999-08-19 00:55:39 +00001136
Brian Paul4d859f72004-01-23 18:57:05 +00001137
Keith Whitwell6dc85572003-07-17 13:43:59 +00001138/**
1139 * Initialize the attribute groups in a GL context.
1140 *
1141 * \param ctx GL context.
1142 *
1143 * Initializes all the attributes, calling the respective <tt>init*</tt>
1144 * functions for the more complex data structures.
1145 */
1146static GLboolean
1147init_attrib_groups( GLcontext *ctx )
1148{
1149 assert(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001150
Keith Whitwell6dc85572003-07-17 13:43:59 +00001151 /* Constants */
1152 _mesa_init_constants( ctx );
Brian Paul0771d152000-04-07 00:19:41 +00001153
Brian Paul4d053dd2000-01-14 04:45:47 +00001154 /* Extensions */
Brian Paulde4f4602003-07-03 02:15:06 +00001155 _mesa_init_extensions( ctx );
jtgafb833d1999-08-19 00:55:39 +00001156
Keith Whitwell6dc85572003-07-17 13:43:59 +00001157 /* Attribute Groups */
1158 _mesa_init_accum( ctx );
1159 _mesa_init_attrib( ctx );
1160 _mesa_init_buffers( ctx );
Brian Paul148a2842003-09-17 03:40:11 +00001161 _mesa_init_buffer_objects( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001162 _mesa_init_color( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001163 _mesa_init_colortables( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001164 _mesa_init_current( ctx );
1165 _mesa_init_depth( ctx );
1166 _mesa_init_debug( ctx );
1167 _mesa_init_display_list( ctx );
1168 _mesa_init_eval( ctx );
1169 _mesa_init_feedback( ctx );
1170 _mesa_init_fog( ctx );
1171 _mesa_init_histogram( ctx );
1172 _mesa_init_hint( ctx );
1173 _mesa_init_line( ctx );
1174 _mesa_init_lighting( ctx );
1175 _mesa_init_matrix( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001176 _mesa_init_occlude( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001177 _mesa_init_pixel( ctx );
1178 _mesa_init_point( ctx );
1179 _mesa_init_polygon( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001180 _mesa_init_program( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001181 _mesa_init_rastpos( ctx );
1182 _mesa_init_stencil( ctx );
1183 _mesa_init_transform( ctx );
1184 _mesa_init_varray( ctx );
1185 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001186
Keith Whitwell6dc85572003-07-17 13:43:59 +00001187 if (!_mesa_init_texture( ctx ))
1188 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001189
Brian Paul8f04c122004-04-27 13:39:20 +00001190 _mesa_init_texture_s3tc( ctx );
1191 _mesa_init_texture_fxt1( ctx );
1192
Brian Paul4d053dd2000-01-14 04:45:47 +00001193 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001194 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001195 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paulf782b812002-10-04 17:37:45 +00001196 ctx->_Facing = 0;
Brian Paul4923e192004-02-28 22:30:58 +00001197#if CHAN_TYPE == GL_FLOAT
1198 ctx->ClampFragmentColors = GL_FALSE; /* XXX temporary */
1199#else
1200 ctx->ClampFragmentColors = GL_TRUE;
1201#endif
1202 ctx->ClampVertexColors = GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001203
Brian Paula3f13702003-04-01 16:41:50 +00001204 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001205}
1206
1207
Brian Paulf44898c2003-07-18 15:44:57 +00001208/**
1209 * If the DRI libGL.so library is old, it may not have the entrypoints for
1210 * some recent OpenGL extensions. Dynamically add them now.
1211 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1212 * and driver code, this won't be an issue (and calling this function won't
1213 * do any harm).
1214 */
1215static void
1216add_newer_entrypoints(void)
1217{
Brian Paulde434842004-11-27 20:14:03 +00001218 unsigned i;
Ian Romanick3baefe62003-08-22 23:28:03 +00001219 static const struct {
1220 const char * const name;
Brian Paulde434842004-11-27 20:14:03 +00001221 unsigned offset;
Ian Romanick3baefe62003-08-22 23:28:03 +00001222 }
1223 newer_entrypoints[] = {
1224 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1225 { "glWindowPos2dARB", 513 },
1226 { "glWindowPos2dvARB", 514 },
1227 { "glWindowPos2fARB", 515 },
1228 { "glWindowPos2fvARB", 516 },
1229 { "glWindowPos2iARB", 517 },
1230 { "glWindowPos2ivARB", 518 },
1231 { "glWindowPos2sARB", 519 },
1232 { "glWindowPos2svARB", 520 },
1233 { "glWindowPos3dARB", 521 },
1234 { "glWindowPos3dvARB", 522 },
1235 { "glWindowPos3fARB", 523 },
1236 { "glWindowPos3fvARB", 524 },
1237 { "glWindowPos3iARB", 525 },
1238 { "glWindowPos3ivARB", 526 },
1239 { "glWindowPos3sARB", 527 },
1240 { "glWindowPos3svARB", 528 },
Brian Paulf44898c2003-07-18 15:44:57 +00001241#if FEATURE_NV_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001242 { "glAreProgramsResidentNV", 578 },
1243 { "glBindProgramNV", 579 },
1244 { "glDeleteProgramsNV", 580 },
1245 { "glExecuteProgramNV", 581 },
1246 { "glGenProgramsNV", 582 },
1247 { "glGetProgramParameterdvNV", 583 },
1248 { "glGetProgramParameterfvNV", 584 },
1249 { "glGetProgramivNV", 585 },
1250 { "glGetProgramStringNV", 586 },
1251 { "glGetTrackMatrixivNV", 587 },
1252 { "glGetVertexAttribdvNV", 588 },
1253 { "glGetVertexAttribfvNV", 589 },
1254 { "glGetVertexAttribivNV", 590 },
1255 { "glGetVertexAttribPointervNV", 591 },
1256 { "glIsProgramNV", 592 },
1257 { "glLoadProgramNV", 593 },
1258 { "glProgramParameter4dNV", 594 },
1259 { "glProgramParameter4dvNV", 595 },
1260 { "glProgramParameter4fNV", 596 },
1261 { "glProgramParameter4fvNV", 597 },
1262 { "glProgramParameters4dvNV", 598 },
1263 { "glProgramParameters4fvNV", 599 },
1264 { "glRequestResidentProgramsNV", 600 },
1265 { "glTrackMatrixNV", 601 },
1266 { "glVertexAttribPointerNV", 602 },
1267 { "glVertexAttrib1dNV", 603 },
1268 { "glVertexAttrib1dvNV", 604 },
1269 { "glVertexAttrib1fNV", 605 },
1270 { "glVertexAttrib1fvNV", 606 },
1271 { "glVertexAttrib1sNV", 607 },
1272 { "glVertexAttrib1svNV", 608 },
1273 { "glVertexAttrib2dNV", 609 },
1274 { "glVertexAttrib2dvNV", 610 },
1275 { "glVertexAttrib2fNV", 611 },
1276 { "glVertexAttrib2fvNV", 612 },
1277 { "glVertexAttrib2sNV", 613 },
1278 { "glVertexAttrib2svNV", 614 },
1279 { "glVertexAttrib3dNV", 615 },
1280 { "glVertexAttrib3dvNV", 616 },
1281 { "glVertexAttrib3fNV", 617 },
1282 { "glVertexAttrib3fvNV", 618 },
1283 { "glVertexAttrib3sNV", 619 },
1284 { "glVertexAttrib3svNV", 620 },
1285 { "glVertexAttrib4dNV", 621 },
1286 { "glVertexAttrib4dvNV", 622 },
1287 { "glVertexAttrib4fNV", 623 },
1288 { "glVertexAttrib4fvNV", 624 },
1289 { "glVertexAttrib4sNV", 625 },
1290 { "glVertexAttrib4svNV", 626 },
1291 { "glVertexAttrib4ubNV", 627 },
1292 { "glVertexAttrib4ubvNV", 628 },
1293 { "glVertexAttribs1dvNV", 629 },
1294 { "glVertexAttribs1fvNV", 630 },
1295 { "glVertexAttribs1svNV", 631 },
1296 { "glVertexAttribs2dvNV", 632 },
1297 { "glVertexAttribs2fvNV", 633 },
1298 { "glVertexAttribs2svNV", 634 },
1299 { "glVertexAttribs3dvNV", 635 },
1300 { "glVertexAttribs3fvNV", 636 },
1301 { "glVertexAttribs3svNV", 637 },
1302 { "glVertexAttribs4dvNV", 638 },
1303 { "glVertexAttribs4fvNV", 639 },
1304 { "glVertexAttribs4svNV", 640 },
1305 { "glVertexAttribs4ubvNV", 641 },
Brian Paulf44898c2003-07-18 15:44:57 +00001306#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001307 { "glPointParameteriNV", 642 },
1308 { "glPointParameterivNV", 643 },
1309 { "glMultiDrawArraysEXT", 644 },
1310 { "glMultiDrawElementsEXT", 645 },
1311 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1312 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1313 { "glActiveStencilFaceEXT", 646 },
Brian Paulf44898c2003-07-18 15:44:57 +00001314#if FEATURE_NV_fence
Ian Romanick3baefe62003-08-22 23:28:03 +00001315 { "glDeleteFencesNV", 647 },
1316 { "glGenFencesNV", 648 },
1317 { "glIsFenceNV", 649 },
1318 { "glTestFenceNV", 650 },
1319 { "glGetFenceivNV", 651 },
1320 { "glFinishFenceNV", 652 },
1321 { "glSetFenceNV", 653 },
Brian Paulf44898c2003-07-18 15:44:57 +00001322#endif
1323#if FEATURE_NV_fragment_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001324 { "glProgramNamedParameter4fNV", 682 },
1325 { "glProgramNamedParameter4dNV", 683 },
1326 { "glProgramNamedParameter4fvNV", 683 },
1327 { "glProgramNamedParameter4dvNV", 684 },
1328 { "glGetProgramNamedParameterfvNV", 685 },
1329 { "glGetProgramNamedParameterdvNV", 686 },
Brian Paulf44898c2003-07-18 15:44:57 +00001330#endif
1331#if FEATURE_ARB_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001332 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1333 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1334 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1335 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1336 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1337 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1338 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1339 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1340 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1341 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1342 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1343 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1344 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1345 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1346 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1347 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1348 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1349 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1350 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1351 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1352 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1353 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1354 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1355 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1356 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1357 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1358 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1359 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1360 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1361 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1362 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1363 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1364 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1365 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1366 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1367 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1368 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1369 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1370 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1371 { "glProgramStringARB", _gloffset_ProgramStringARB },
1372 { "glBindProgramARB", _gloffset_BindProgramNV },
1373 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1374 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1375 { "glIsProgramARB", _gloffset_IsProgramNV },
1376 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1377 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1378 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1379 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1380 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1381 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1382 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1383 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1384 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1385 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1386 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1387 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1388 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1389 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1390 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1391 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1392 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1393 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
Brian Paulf44898c2003-07-18 15:44:57 +00001394#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001395 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1396 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
Brian Paulde434842004-11-27 20:14:03 +00001397 /* GL_EXT_stencil_two_side */
1398 { "glActiveStencilFaceEXT", _gloffset_ActiveStencilFaceEXT },
1399 /* GL_ARB_draw_buffers */
1400 { "glDrawBuffersARB", _gloffset_DrawBuffersARB },
Dave Airlie7f752fe2004-12-19 03:06:59 +00001401#if FEATURE_ATI_fragment_shader
1402 { "glGenFragmentShadersATI", _gloffset_GenFragmentShadersATI },
1403 { "glBindFragmentShaderATI", _gloffset_BindFragmentShaderATI },
1404 { "glDeleteFragmentShaderATI", _gloffset_DeleteFragmentShaderATI },
1405 { "glBeginFragmentShaderATI", _gloffset_BeginFragmentShaderATI },
1406 { "glEndFragmentShaderATI", _gloffset_EndFragmentShaderATI },
1407 { "glPassTexCoordATI", _gloffset_PassTexCoordATI },
1408 { "glSampleMapATI", _gloffset_SampleMapATI },
1409 { "glColorFragmentOp1ATI", _gloffset_ColorFragmentOp1ATI },
1410 { "glColorFragmentOp2ATI", _gloffset_ColorFragmentOp2ATI },
1411 { "glColorFragmentOp3ATI", _gloffset_ColorFragmentOp3ATI },
1412 { "glAlphaFragmentOp1ATI", _gloffset_AlphaFragmentOp1ATI },
1413 { "glAlphaFragmentOp2ATI", _gloffset_AlphaFragmentOp2ATI },
1414 { "glAlphaFragmentOp3ATI", _gloffset_AlphaFragmentOp3ATI },
1415 { "glSetFragmentShaderConstantATI", _gloffset_SetFragmentShaderConstantATI },
1416#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001417 };
1418
Brian Paulde434842004-11-27 20:14:03 +00001419 for (i = 0; i < Elements(newer_entrypoints); i++ ) {
Ian Romanick3baefe62003-08-22 23:28:03 +00001420 _glapi_add_entrypoint( newer_entrypoints[i].name,
Brian Paulde434842004-11-27 20:14:03 +00001421 newer_entrypoints[i].offset );
Ian Romanick3baefe62003-08-22 23:28:03 +00001422 }
Brian Paulf44898c2003-07-18 15:44:57 +00001423}
1424
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001425
Brian Paulde4f4602003-07-03 02:15:06 +00001426/**
Brian Paul21f69782004-11-27 05:05:32 +00001427 * This is the default function we plug into all dispatch table slots
1428 * This helps prevents a segfault when someone calls a GL function without
1429 * first checking if the extension's supported.
1430 */
1431static int
1432generic_nop(void)
1433{
1434 _mesa_problem(NULL, "User called no-op dispatch function (an unsupported extension function?)");
1435 return 0;
1436}
1437
1438
1439/**
1440 * Allocate and initialize a new dispatch table.
1441 */
1442static struct _glapi_table *
1443alloc_dispatch_table(void)
1444{
1445 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1446 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1447 * Mesa we do this to accomodate different versions of libGL and various
1448 * DRI drivers.
1449 */
1450 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
1451 sizeof(struct _glapi_table) / sizeof(_glapi_proc));
1452 struct _glapi_table *table =
1453 (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc));
1454 if (table) {
1455 _glapi_proc *entry = (_glapi_proc *) table;
Brian Paula760ccf2004-12-03 15:24:34 +00001456 GLint i;
Brian Paul21f69782004-11-27 05:05:32 +00001457 for (i = 0; i < numEntries; i++) {
1458 entry[i] = (_glapi_proc) generic_nop;
1459 }
1460 }
1461 return table;
1462}
1463
1464
1465/**
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001466 * Initialize a GLcontext struct (rendering context).
Keith Whitwell6dc85572003-07-17 13:43:59 +00001467 *
1468 * This includes allocating all the other structs and arrays which hang off of
1469 * the context by pointers.
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001470 * Note that the driver needs to pass in its dd_function_table here since
1471 * we need to at least call driverFunctions->NewTextureObject to create the
1472 * default texture objects.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001473 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001474 * Called by _mesa_create_context().
Keith Whitwell6dc85572003-07-17 13:43:59 +00001475 *
1476 * Performs the imports and exports callback tables initialization, and
1477 * miscellaneous one-time initializations. If no shared context is supplied one
1478 * is allocated, and increase its reference count. Setups the GL API dispatch
1479 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1480 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1481 * for debug flags.
1482 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001483 * \param ctx the context to initialize
1484 * \param visual describes the visual attributes for this context
1485 * \param share_list points to context to share textures, display lists,
1486 * etc with, or NULL
1487 * \param driverFunctions table of device driver functions for this context
1488 * to use
1489 * \param driverContext pointer to driver-specific context data
jtgafb833d1999-08-19 00:55:39 +00001490 */
Brian Paul178a1c52000-04-22 01:05:00 +00001491GLboolean
1492_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001493 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001494 GLcontext *share_list,
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001495 const struct dd_function_table *driverFunctions,
1496 void *driverContext )
jtgafb833d1999-08-19 00:55:39 +00001497{
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001498 ASSERT(driverContext);
1499 assert(driverFunctions->NewTextureObject);
jtgafb833d1999-08-19 00:55:39 +00001500
Brian Paul3c634522002-10-24 23:57:19 +00001501 /* If the driver wants core Mesa to use special imports, it'll have to
1502 * override these defaults.
1503 */
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001504 _mesa_init_default_imports( &(ctx->imports), driverContext );
jtgafb833d1999-08-19 00:55:39 +00001505
Brian Paul9a33a112002-06-13 04:28:29 +00001506 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001507 _mesa_init_default_exports( &(ctx->exports) );
1508
1509 /* misc one-time initializations */
1510 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001511
Brian Paulb1394fa2000-09-26 20:53:53 +00001512 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001513 ctx->DrawBuffer = NULL;
1514 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001515
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001516 /* Plug in driver functions and context pointer here.
1517 * This is important because when we call alloc_shared_state() below
1518 * we'll call ctx->Driver.NewTextureObject() to create the default
1519 * textures.
Brian Paula3f13702003-04-01 16:41:50 +00001520 */
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001521 ctx->Driver = *driverFunctions;
1522 ctx->DriverCtx = driverContext;
Brian Paula3f13702003-04-01 16:41:50 +00001523
jtgafb833d1999-08-19 00:55:39 +00001524 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001525 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001526 ctx->Shared = share_list->Shared;
1527 }
1528 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001529 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001530 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001531 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001532 }
1533 }
Brian Paul9560f052000-01-31 23:11:39 +00001534 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001535 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001536 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001537
Keith Whitwell6dc85572003-07-17 13:43:59 +00001538 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001539 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001540 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001541 }
jtgafb833d1999-08-19 00:55:39 +00001542
Brian Paulf44898c2003-07-18 15:44:57 +00001543 /* libGL ABI coordination */
1544 add_newer_entrypoints();
Keith Whitwell6dc85572003-07-17 13:43:59 +00001545
Brian Paul21f69782004-11-27 05:05:32 +00001546 /* setup the API dispatch tables */
1547 ctx->Exec = alloc_dispatch_table();
1548 ctx->Save = alloc_dispatch_table();
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001549 if (!ctx->Exec || !ctx->Save) {
1550 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001551 if (ctx->Exec)
Brian Paul21f69782004-11-27 05:05:32 +00001552 _mesa_free(ctx->Exec);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001553 }
Brian Paul21f69782004-11-27 05:05:32 +00001554 _mesa_init_exec_table(ctx->Exec);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001555 ctx->CurrentDispatch = ctx->Exec;
Keith Whitwell6dc85572003-07-17 13:43:59 +00001556#if _HAVE_FULL_GL
Brian Paul21f69782004-11-27 05:05:32 +00001557 _mesa_init_dlist_table(ctx->Save);
Keith Whitwellae0eaf92003-11-24 15:23:18 +00001558 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001559 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001560 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001561 ctx->TnlModule.Current = NULL;
1562 ctx->TnlModule.SwapCount = 0;
Keith Whitwell6dc85572003-07-17 13:43:59 +00001563#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001564
Brian Paul4d053dd2000-01-14 04:45:47 +00001565 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001566}
1567
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001568
Brian Paulde4f4602003-07-03 02:15:06 +00001569/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001570 * Allocate and initialize a GLcontext structure.
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001571 * Note that the driver needs to pass in its dd_function_table here since
1572 * we need to at least call driverFunctions->NewTextureObject to initialize
1573 * the rendering context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001574 *
1575 * \param visual a GLvisual pointer (we copy the struct contents)
1576 * \param share_list another context to share display lists with or NULL
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001577 * \param driverFunctions points to the dd_function_table into which the
1578 * driver has plugged in all its special functions.
1579 * \param driverCtx points to the device driver's private context state
Keith Whitwell6dc85572003-07-17 13:43:59 +00001580 *
1581 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001582 */
Brian Paul178a1c52000-04-22 01:05:00 +00001583GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001584_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001585 GLcontext *share_list,
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001586 const struct dd_function_table *driverFunctions,
1587 void *driverContext )
Brian Paul3c634522002-10-24 23:57:19 +00001588
Brian Paul4d053dd2000-01-14 04:45:47 +00001589{
Brian Paul4753d602002-06-15 02:38:15 +00001590 GLcontext *ctx;
1591
1592 ASSERT(visual);
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001593 ASSERT(driverContext);
Brian Paul4753d602002-06-15 02:38:15 +00001594
Brian Paul3c634522002-10-24 23:57:19 +00001595 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001596 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001597 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001598
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001599 if (_mesa_initialize_context(ctx, visual, share_list,
1600 driverFunctions, driverContext)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001601 return ctx;
1602 }
1603 else {
Brian Paul3c634522002-10-24 23:57:19 +00001604 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001605 return NULL;
1606 }
1607}
1608
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001609
Keith Whitwell6dc85572003-07-17 13:43:59 +00001610/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001611 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001612 *
1613 * But doesn't free the GLcontext struct itself.
1614 *
1615 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001616 */
Brian Paul178a1c52000-04-22 01:05:00 +00001617void
Brian Paulb1394fa2000-09-26 20:53:53 +00001618_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001619{
Brian Paul4d053dd2000-01-14 04:45:47 +00001620 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001621 if (ctx == _mesa_get_current_context()) {
1622 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001623 }
1624
Keith Whitwell6dc85572003-07-17 13:43:59 +00001625 _mesa_free_lighting_data( ctx );
1626 _mesa_free_eval_data( ctx );
1627 _mesa_free_texture_data( ctx );
1628 _mesa_free_matrix_data( ctx );
1629 _mesa_free_viewport_data( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001630 _mesa_free_colortables_data( ctx );
Brian Paul21841f02004-08-14 14:28:11 +00001631 _mesa_free_program_data(ctx);
1632 _mesa_free_occlude_data(ctx);
1633
1634#if FEATURE_ARB_vertex_buffer_object
1635 _mesa_delete_buffer_object(ctx, ctx->Array.NullBufferObj);
Brian Paul8dfc5b92002-10-16 17:57:51 +00001636#endif
Brian Paulfd284452001-07-19 15:54:34 +00001637
Brian Paul65a66f52004-11-27 22:47:59 +00001638 /* free dispatch tables */
1639 _mesa_free(ctx->Exec);
1640 _mesa_free(ctx->Save);
1641
Brian Paul30f51ae2001-12-18 04:06:44 +00001642 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001643 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001644 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001645 assert(ctx->Shared->RefCount >= 0);
1646 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1647 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001648 /* free shared state */
1649 free_shared_state( ctx, ctx->Shared );
1650 }
1651
Brian Paul702ca202003-07-18 15:22:16 +00001652 if (ctx->Extensions.String)
1653 FREE((void *) ctx->Extensions.String);
Brian Paul4d053dd2000-01-14 04:45:47 +00001654}
1655
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001656
Keith Whitwell6dc85572003-07-17 13:43:59 +00001657/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001658 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001659 *
1660 * \param ctx GL context.
1661 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001662 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
jtgafb833d1999-08-19 00:55:39 +00001663 */
Brian Paul178a1c52000-04-22 01:05:00 +00001664void
Brian Paulb1394fa2000-09-26 20:53:53 +00001665_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001666{
1667 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001668 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001669 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001670 }
1671}
1672
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001673
Keith Whitwell6dc85572003-07-17 13:43:59 +00001674#if _HAVE_FULL_GL
1675/**
jtgafb833d1999-08-19 00:55:39 +00001676 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001677 *
1678 * \param src source context
1679 * \param dst destination context
1680 * \param mask bitwise OR of GL_*_BIT flags
1681 *
1682 * According to the bits specified in \p mask, copies the corresponding
Jose Fonseca375457b2004-09-09 22:23:24 +00001683 * attributes from \p src into \p dst. For many of the attributes a simple \c
Keith Whitwell6dc85572003-07-17 13:43:59 +00001684 * memcpy is not enough due to the existence of internal pointers in their data
1685 * structures.
jtgafb833d1999-08-19 00:55:39 +00001686 */
Brian Paul178a1c52000-04-22 01:05:00 +00001687void
Brian Paulb1394fa2000-09-26 20:53:53 +00001688_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001689{
1690 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001691 /* OK to memcpy */
1692 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001693 }
1694 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001695 /* OK to memcpy */
1696 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001697 }
1698 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001699 /* OK to memcpy */
1700 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001701 }
1702 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001703 /* OK to memcpy */
1704 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001705 }
1706 if (mask & GL_ENABLE_BIT) {
1707 /* no op */
1708 }
1709 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001710 /* OK to memcpy */
1711 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001712 }
1713 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001714 /* OK to memcpy */
1715 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001716 }
1717 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001718 /* OK to memcpy */
1719 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001720 }
1721 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001722 GLuint i;
1723 /* begin with memcpy */
1724 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1725 /* fixup linked lists to prevent pointer insanity */
1726 make_empty_list( &(dst->Light.EnabledList) );
1727 for (i = 0; i < MAX_LIGHTS; i++) {
1728 if (dst->Light.Light[i].Enabled) {
1729 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1730 }
1731 }
jtgafb833d1999-08-19 00:55:39 +00001732 }
1733 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001734 /* OK to memcpy */
1735 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001736 }
1737 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001738 /* OK to memcpy */
1739 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001740 }
1741 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001742 /* OK to memcpy */
1743 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001744 }
1745 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001746 /* OK to memcpy */
1747 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001748 }
1749 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001750 /* OK to memcpy */
1751 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001752 }
1753 if (mask & GL_POLYGON_STIPPLE_BIT) {
1754 /* Use loop instead of MEMCPY due to problem with Portland Group's
1755 * C compiler. Reported by John Stone.
1756 */
Brian Paul85d81602002-06-17 23:36:31 +00001757 GLuint i;
1758 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001759 dst->PolygonStipple[i] = src->PolygonStipple[i];
1760 }
1761 }
1762 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001763 /* OK to memcpy */
1764 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001765 }
1766 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001767 /* OK to memcpy */
1768 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001769 }
1770 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001771 /* Cannot memcpy because of pointers */
1772 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001773 }
1774 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001775 /* OK to memcpy */
1776 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001777 }
1778 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001779 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1780 dst->Viewport.X = src->Viewport.X;
1781 dst->Viewport.Y = src->Viewport.Y;
1782 dst->Viewport.Width = src->Viewport.Width;
1783 dst->Viewport.Height = src->Viewport.Height;
1784 dst->Viewport.Near = src->Viewport.Near;
1785 dst->Viewport.Far = src->Viewport.Far;
1786 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001787 }
Brian Paul85d81602002-06-17 23:36:31 +00001788
Keith Whitwella96308c2000-10-30 13:31:59 +00001789 /* XXX FIXME: Call callbacks?
1790 */
1791 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001792}
Keith Whitwell23caf202000-11-16 21:05:34 +00001793#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001794
1795
Brian Paulb1d53d92003-06-11 18:48:19 +00001796/**
1797 * Check if the given context can render into the given framebuffer
1798 * by checking visual attributes.
1799 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1800 */
1801static GLboolean
1802check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1803{
1804 const GLvisual *ctxvis = &ctx->Visual;
1805 const GLvisual *bufvis = &buffer->Visual;
1806
1807 if (ctxvis == bufvis)
1808 return GL_TRUE;
1809
1810 if (ctxvis->rgbMode != bufvis->rgbMode)
1811 return GL_FALSE;
1812 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1813 return GL_FALSE;
1814 if (ctxvis->stereoMode && !bufvis->stereoMode)
1815 return GL_FALSE;
1816 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1817 return GL_FALSE;
1818 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1819 return GL_FALSE;
1820 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1821 return GL_FALSE;
1822 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1823 return GL_FALSE;
1824 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1825 return GL_FALSE;
1826 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1827 return GL_FALSE;
1828 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1829 return GL_FALSE;
1830 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1831 return GL_FALSE;
1832
1833 return GL_TRUE;
1834}
1835
1836
Keith Whitwell6dc85572003-07-17 13:43:59 +00001837/**
Brian Paul9a33a112002-06-13 04:28:29 +00001838 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001839 *
1840 * \param newCtx new GL context.
1841 * \param buffer framebuffer.
1842 *
1843 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001844 */
1845void
1846_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1847{
1848 _mesa_make_current2( newCtx, buffer, buffer );
1849}
1850
Keith Whitwell6dc85572003-07-17 13:43:59 +00001851/**
1852 * Bind the given context to the given draw-buffer and read-buffer and
1853 * make it the current context for this thread.
1854 *
1855 * \param newCtx new GL context. If NULL then there will be no current GL
1856 * context.
1857 * \param drawBuffer draw framebuffer.
1858 * \param readBuffer read framebuffer.
1859 *
1860 * Check that the context's and framebuffer's visuals are compatible, returning
1861 * immediately otherwise. Sets the glapi current context via
1862 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1863 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1864 * Calls dd_function_table::MakeCurrent callback if defined.
1865 *
1866 * When a context is bound by the first time and the \c MESA_INFO environment
1867 * variable is set it calls print_info() as an aid for remote user
1868 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001869 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001870void
1871_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1872 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001873{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001874 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001875 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001876
Brian Paulbe3602d2001-02-28 00:27:48 +00001877 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001878 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001879 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1880 if (!check_compatible(newCtx, drawBuffer))
1881 return;
1882 }
1883 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1884 if (!check_compatible(newCtx, readBuffer))
1885 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001886 }
1887
Brian Paul00037781999-12-17 14:52:35 +00001888 /* We call this function periodically (just here for now) in
1889 * order to detect when multithreading has begun.
1890 */
1891 _glapi_check_multithread();
1892
Brian Paulf9b97d92000-01-28 20:17:42 +00001893 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001894 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001895
1896
1897 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001898 _glapi_set_dispatch(NULL); /* none current */
1899 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001900 else {
1901 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001902
Keith Whitwell23caf202000-11-16 21:05:34 +00001903 if (drawBuffer && readBuffer) {
1904 /* TODO: check if newCtx and buffer's visual match??? */
1905 newCtx->DrawBuffer = drawBuffer;
1906 newCtx->ReadBuffer = readBuffer;
1907 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001908
Keith Whitwell6dc85572003-07-17 13:43:59 +00001909#if _HAVE_FULL_GL
Brian Paul65a66f52004-11-27 22:47:59 +00001910 if (!drawBuffer->Initialized) {
Brian Paul10d7f542002-06-17 23:38:14 +00001911 /* get initial window size */
1912 GLuint bufWidth, bufHeight;
Brian Paul65a66f52004-11-27 22:47:59 +00001913 /* ask device driver for size of the buffer */
1914 (*newCtx->Driver.GetBufferSize)(drawBuffer, &bufWidth, &bufHeight);
1915 /* set initial buffer size */
1916 drawBuffer->Width = bufWidth;
1917 drawBuffer->Height = bufHeight;
1918 newCtx->Driver.ResizeBuffers( drawBuffer );
1919 drawBuffer->Initialized = GL_TRUE;
Brian Paul10d7f542002-06-17 23:38:14 +00001920 }
1921
Brian Paul65a66f52004-11-27 22:47:59 +00001922 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
Brian Paul10d7f542002-06-17 23:38:14 +00001923 /* get initial window size */
1924 GLuint bufWidth, bufHeight;
Brian Paul65a66f52004-11-27 22:47:59 +00001925 /* ask device driver for size of the buffer */
1926 (*newCtx->Driver.GetBufferSize)(readBuffer, &bufWidth, &bufHeight);
1927 /* set initial buffer size */
1928 readBuffer->Width = bufWidth;
1929 readBuffer->Height = bufHeight;
1930 newCtx->Driver.ResizeBuffers( readBuffer );
1931 readBuffer->Initialized = GL_TRUE;
Brian Paul10d7f542002-06-17 23:38:14 +00001932 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001933#endif
Brian Paul65a66f52004-11-27 22:47:59 +00001934 if (newCtx->FirstTimeCurrent) {
1935 /* set initial viewport and scissor size now */
1936 _mesa_set_viewport(newCtx, 0, 0, drawBuffer->Width, drawBuffer->Height);
1937 newCtx->Scissor.Width = drawBuffer->Width;
1938 newCtx->Scissor.Height = drawBuffer->Height;
1939 }
Brian Paul00037781999-12-17 14:52:35 +00001940 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001941
Keith Whitwell6dc85572003-07-17 13:43:59 +00001942 /* Alert the driver - usually passed on to the sw t&l module,
1943 * but also used to detect threaded cases in the radeon codegen
1944 * hw t&l module.
1945 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001946 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001947 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1948
1949 /* We can use this to help debug user's problems. Tell them to set
1950 * the MESA_INFO env variable before running their app. Then the
1951 * first time each context is made current we'll print some useful
1952 * information.
1953 */
1954 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001955 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001956 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001957 }
1958 newCtx->FirstTimeCurrent = GL_FALSE;
1959 }
Brian Paul00037781999-12-17 14:52:35 +00001960 }
1961}
1962
Keith Whitwell6dc85572003-07-17 13:43:59 +00001963/**
1964 * Get current context for the calling thread.
1965 *
1966 * \return pointer to the current GL context.
1967 *
1968 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1969 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00001970 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001971GLcontext *
1972_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00001973{
Brian Paulf9b97d92000-01-28 20:17:42 +00001974 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001975}
1976
Keith Whitwell6dc85572003-07-17 13:43:59 +00001977/**
1978 * Get context's current API dispatch table.
1979 *
1980 * It'll either be the immediate-mode execute dispatcher or the display list
1981 * compile dispatcher.
1982 *
1983 * \param ctx GL context.
1984 *
1985 * \return pointer to dispatch_table.
1986 *
1987 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00001988 */
1989struct _glapi_table *
1990_mesa_get_dispatch(GLcontext *ctx)
1991{
1992 return ctx->CurrentDispatch;
1993}
1994
Keith Whitwell6dc85572003-07-17 13:43:59 +00001995/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00001996
1997
jtgafb833d1999-08-19 00:55:39 +00001998/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001999/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00002000/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00002001/*@{*/
jtgafb833d1999-08-19 00:55:39 +00002002
Keith Whitwell6dc85572003-07-17 13:43:59 +00002003/**
2004 * Record an error.
2005 *
2006 * \param ctx GL context.
2007 * \param error error code.
2008 *
2009 * Records the given error code and call the driver's dd_function_table::Error
2010 * function if defined.
2011 *
2012 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00002013 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00002014 */
Brian Paulb1394fa2000-09-26 20:53:53 +00002015void
Brian Paul4e9676f2002-06-29 19:48:15 +00002016_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00002017{
Brian Paul18a285a2002-03-16 00:53:15 +00002018 if (!ctx)
2019 return;
2020
Brian Paul7eb06032000-07-14 04:13:40 +00002021 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00002022 ctx->ErrorValue = error;
2023 }
2024
2025 /* Call device driver's error handler, if any. This is used on the Mac. */
2026 if (ctx->Driver.Error) {
2027 (*ctx->Driver.Error)( ctx );
2028 }
2029}
2030
Keith Whitwell6dc85572003-07-17 13:43:59 +00002031/**
2032 * Execute glFinish().
2033 *
2034 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2035 * dd_function_table::Finish driver callback, if not NULL.
2036 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002037void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00002038_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00002039{
Brian Paulfa9df402000-02-02 19:16:46 +00002040 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002041 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00002042 if (ctx->Driver.Finish) {
2043 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00002044 }
2045}
2046
Keith Whitwell6dc85572003-07-17 13:43:59 +00002047/**
2048 * Execute glFlush().
2049 *
2050 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2051 * dd_function_table::Flush driver callback, if not NULL.
2052 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002053void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00002054_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00002055{
Brian Paulfa9df402000-02-02 19:16:46 +00002056 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002057 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00002058 if (ctx->Driver.Flush) {
2059 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00002060 }
jtgafb833d1999-08-19 00:55:39 +00002061}
Brian Paul48c6a6e2000-09-08 21:28:04 +00002062
2063
Keith Whitwell6dc85572003-07-17 13:43:59 +00002064/*@}*/