blob: 13b4d193d1e75bfe66eaf87189432dc545315ffa [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 Paul3a212032002-11-19 15:25:00 +00009 * Version: 5.1
jtgafb833d1999-08-19 00:55:39 +000010 *
Brian Paulbc42c192002-01-05 21:53:20 +000011 * Copyright (C) 1999-2002 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/**
33 * \mainpage Mesa Core Module
34 *
35 * \section CoreIntroduction Introduction
36 *
37 * The Mesa core module consists of all the top-level files in the src
38 * directory. The core module basically takes care of API dispatch,
39 * and OpenGL state management.
40 *
41 * For example, calls to glPolygonMode() are routed to _mesa_PolygonMode()
42 * which updates the state related to polygonmode. Furthermore, dirty
43 * state flags related to polygon mode are set and if the device driver
44 * implements a special routine for PolygonMode, it will be called.
45 *
46 *
47 * \section AboutDoxygen About Doxygen
48 *
49 * If you're viewing this information as Doxygen-generated HTML you'll
50 * see the documentation index at the top of this page.
51 *
52 * The first line lists the Mesa source code modules.
53 * The second line lists the indexes available for viewing the documentation
54 * for each module.
55 *
56 * Selecting the <b>Main page</b> link will display a summary of the module
57 * (this page).
58 *
59 * Selecting <b>Compound List</b> will list all C structures.
60 *
61 * Selecting the <b>File List</b> link will list all the source files in
62 * the module.
63 * Selecting a filename will show a list of all functions defined in that file.
64 *
65 * Selecting the <b>Compound Members</b> link will display a list of all
66 * documented structure members.
67 *
68 * Selecting the <b>File Members</b> link will display a list
69 * of all functions, structures, global variables and macros in the module.
70 *
71 */
72
73
Brian Paulfbd8f211999-11-11 01:22:25 +000074#include "glheader.h"
Brian Paul3c634522002-10-24 23:57:19 +000075#include "imports.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000076#include "accum.h"
Brian Paul05944c02003-07-22 03:51:46 +000077#include "arbprogram.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000078#include "attrib.h"
79#include "blend.h"
Brian Paulb1394fa2000-09-26 20:53:53 +000080#include "buffers.h"
Brian Paul4bdcfe52000-04-17 17:57:04 +000081#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000082#include "context.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000083#include "debug.h"
84#include "depth.h"
jtgafb833d1999-08-19 00:55:39 +000085#include "dlist.h"
86#include "eval.h"
87#include "enums.h"
Brian Paul585a68c1999-09-11 11:31:34 +000088#include "extensions.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000089#include "feedback.h"
jtgafb833d1999-08-19 00:55:39 +000090#include "fog.h"
Brian Paulb7a43041999-11-30 20:34:51 +000091#include "get.h"
Brian Paul9560f052000-01-31 23:11:39 +000092#include "glthread.h"
Brian Paulf44898c2003-07-18 15:44:57 +000093#include "glapioffsets.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000094#include "histogram.h"
95#include "hint.h"
jtgafb833d1999-08-19 00:55:39 +000096#include "hash.h"
97#include "light.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000098#include "lines.h"
jtgafb833d1999-08-19 00:55:39 +000099#include "macros.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000100#include "matrix.h"
Brian Paul05944c02003-07-22 03:51:46 +0000101#include "occlude.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000102#include "pixel.h"
103#include "points.h"
104#include "polygon.h"
105#include "rastpos.h"
jtgafb833d1999-08-19 00:55:39 +0000106#include "simple_list.h"
Brian Paulfa9df402000-02-02 19:16:46 +0000107#include "state.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000108#include "stencil.h"
jtgafb833d1999-08-19 00:55:39 +0000109#include "teximage.h"
110#include "texobj.h"
Brian Paul85d81602002-06-17 23:36:31 +0000111#include "texstate.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000112#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +0000113#include "varray.h"
Brian Paul3a212032002-11-19 15:25:00 +0000114#if FEATURE_NV_vertex_program
Brian Paul610d5992003-01-14 04:55:45 +0000115#include "nvprogram.h"
116#include "nvvertprog.h"
117#endif
118#if FEATURE_NV_fragment_program
119#include "nvfragprog.h"
Brian Paul3a212032002-11-19 15:25:00 +0000120#endif
Gareth Hughesd4eb6652001-03-12 01:32:20 +0000121#include "vtxfmt.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000122#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000123#include "math/m_translate.h"
Keith Whitwell23caf202000-11-16 21:05:34 +0000124#include "math/m_matrix.h"
125#include "math/m_xform.h"
Keith Whitwellf4b02d12001-01-05 05:31:42 +0000126#include "math/mathmod.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000127#endif
jtgafb833d1999-08-19 00:55:39 +0000128
Brian Paul3b18a362000-09-26 15:27:20 +0000129#if defined(MESA_TRACE)
Brian Paul45f36342000-09-05 20:28:06 +0000130#include "Trace/tr_context.h"
131#include "Trace/tr_wrapper.h"
132#endif
133
davem69775355a2001-06-05 23:54:00 +0000134#ifdef USE_SPARC_ASM
135#include "SPARC/sparc.h"
136#endif
jtgafb833d1999-08-19 00:55:39 +0000137
Keith Whitwell23caf202000-11-16 21:05:34 +0000138#ifndef MESA_VERBOSE
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000139int MESA_VERBOSE = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000140#endif
141
142#ifndef MESA_DEBUG_FLAGS
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000143int MESA_DEBUG_FLAGS = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000144#endif
Brian Paulb1394fa2000-09-26 20:53:53 +0000145
Brian Paul86b84272001-12-14 02:50:01 +0000146
Brian Paul27558a12003-03-01 01:50:20 +0000147/* ubyte -> float conversion */
148GLfloat _mesa_ubyte_to_float_color_tab[256];
149
Brian Paul9a33a112002-06-13 04:28:29 +0000150static void
151free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
152
Brian Paul86b84272001-12-14 02:50:01 +0000153
Brian Paulb1394fa2000-09-26 20:53:53 +0000154/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000155/** \name OpenGL SI-style interface (new in Mesa 3.5)
156 *
157 * \if subset
158 * \note Most of these functions are never called in the Mesa subset.
159 * \endif
160 */
161/*@{*/
Brian Paulb1394fa2000-09-26 20:53:53 +0000162
Keith Whitwell6dc85572003-07-17 13:43:59 +0000163/**
164 * Destroy context callback.
165 *
166 * \param gc context.
167 * \return GL_TRUE on success, or GL_FALSE on failure.
168 *
169 * \ifnot subset
170 * Called by window system/device driver (via __GLexports::destroyCurrent) when
171 * the rendering context is to be destroyed.
172 * \endif
173 *
174 * Frees the context data and the context structure.
Brian Paul9a33a112002-06-13 04:28:29 +0000175 */
176GLboolean
177_mesa_destroyContext(__GLcontext *gc)
Brian Paulb1394fa2000-09-26 20:53:53 +0000178{
179 if (gc) {
180 _mesa_free_context_data(gc);
Brian Paul3c634522002-10-24 23:57:19 +0000181 _mesa_free(gc);
Brian Paulb1394fa2000-09-26 20:53:53 +0000182 }
183 return GL_TRUE;
184}
185
Keith Whitwell6dc85572003-07-17 13:43:59 +0000186/**
187 * Unbind context callback.
188 *
189 * \param gc context.
190 * \return GL_TRUE on success, or GL_FALSE on failure.
191 *
192 * \ifnot subset
193 * Called by window system/device driver (via __GLexports::loseCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000194 * when the rendering context is made non-current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000195 * \endif
196 *
197 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000198 */
199GLboolean
200_mesa_loseCurrent(__GLcontext *gc)
201{
202 /* XXX unbind context from thread */
203 return GL_TRUE;
204}
205
Keith Whitwell6dc85572003-07-17 13:43:59 +0000206/**
207 * Bind context callback.
208 *
209 * \param gc context.
210 * \return GL_TRUE on success, or GL_FALSE on failure.
211 *
212 * \ifnot subset
213 * Called by window system/device driver (via __GLexports::makeCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000214 * when the rendering context is made current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000215 * \endif
216 *
217 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000218 */
219GLboolean
220_mesa_makeCurrent(__GLcontext *gc)
221{
222 /* XXX bind context to thread */
223 return GL_TRUE;
224}
225
Keith Whitwell6dc85572003-07-17 13:43:59 +0000226/**
227 * Share context callback.
228 *
229 * \param gc context.
230 * \param gcShare shared context.
231 * \return GL_TRUE on success, or GL_FALSE on failure.
232 *
233 * \ifnot subset
234 * Called by window system/device driver (via __GLexports::shareContext)
235 * \endif
236 *
237 * Update the shared context reference count, gl_shared_state::RefCount.
Brian Paul9a33a112002-06-13 04:28:29 +0000238 */
239GLboolean
240_mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
241{
242 if (gc && gcShare && gc->Shared && gcShare->Shared) {
243 gc->Shared->RefCount--;
244 if (gc->Shared->RefCount == 0) {
245 free_shared_state(gc, gc->Shared);
246 }
247 gc->Shared = gcShare->Shared;
248 gc->Shared->RefCount++;
249 return GL_TRUE;
250 }
251 else {
252 return GL_FALSE;
253 }
254}
255
Keith Whitwell6dc85572003-07-17 13:43:59 +0000256
257#if _HAVE_FULL_GL
258/**
259 * Copy context callback.
260 */
Brian Paul9a33a112002-06-13 04:28:29 +0000261GLboolean
262_mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
263{
264 if (dst && src) {
265 _mesa_copy_context( src, dst, mask );
266 return GL_TRUE;
267 }
268 else {
269 return GL_FALSE;
270 }
271}
Keith Whitwell6dc85572003-07-17 13:43:59 +0000272#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000273
Keith Whitwell6dc85572003-07-17 13:43:59 +0000274/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000275GLboolean
276_mesa_forceCurrent(__GLcontext *gc)
277{
278 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 Paul9a33a112002-06-13 04:28:29 +0000313}
314
Keith Whitwell6dc85572003-07-17 13:43:59 +0000315/**
316 * Swap buffers notification callback.
317 *
318 * \param gc GL context.
319 *
320 * Called by window system just before swapping buffers.
Brian Paul9a33a112002-06-13 04:28:29 +0000321 * We have to finish any pending rendering.
322 */
323void
324_mesa_notifySwapBuffers(__GLcontext *gc)
325{
326 FLUSH_VERTICES( gc, 0 );
327}
328
Keith Whitwell6dc85572003-07-17 13:43:59 +0000329/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000330struct __GLdispatchStateRec *
331_mesa_dispatchExec(__GLcontext *gc)
332{
333 return NULL;
334}
335
Keith Whitwell6dc85572003-07-17 13:43:59 +0000336/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000337void
338_mesa_beginDispatchOverride(__GLcontext *gc)
339{
340}
341
Keith Whitwell6dc85572003-07-17 13:43:59 +0000342/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000343void
344_mesa_endDispatchOverride(__GLcontext *gc)
345{
346}
347
Keith Whitwell6dc85572003-07-17 13:43:59 +0000348/**
349 * \ifnot subset
350 * Setup the exports.
351 *
352 * The window system will call these functions when it needs Mesa to do
353 * something.
354 *
355 * \note Device drivers should override these functions! For example,
Brian Paul9a33a112002-06-13 04:28:29 +0000356 * the Xlib driver should plug in the XMesa*-style functions into this
357 * structure. The XMesa-style functions should then call the _mesa_*
358 * version of these functions. This is an approximation to OO design
359 * (inheritance and virtual functions).
Keith Whitwell6dc85572003-07-17 13:43:59 +0000360 * \endif
361 *
362 * \if subset
363 * No-op.
364 *
365 * \endif
Brian Paul9a33a112002-06-13 04:28:29 +0000366 */
367static void
368_mesa_init_default_exports(__GLexports *exports)
369{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000370#if _HAVE_FULL_GL
Brian Paul9a33a112002-06-13 04:28:29 +0000371 exports->destroyContext = _mesa_destroyContext;
372 exports->loseCurrent = _mesa_loseCurrent;
373 exports->makeCurrent = _mesa_makeCurrent;
374 exports->shareContext = _mesa_shareContext;
375 exports->copyContext = _mesa_copyContext;
376 exports->forceCurrent = _mesa_forceCurrent;
377 exports->notifyResize = _mesa_notifyResize;
Brian Paul2f35d5e2002-06-13 04:31:09 +0000378 exports->notifyDestroy = _mesa_notifyDestroy;
Brian Paul9a33a112002-06-13 04:28:29 +0000379 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
380 exports->dispatchExec = _mesa_dispatchExec;
381 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
382 exports->endDispatchOverride = _mesa_endDispatchOverride;
Keith Whitwell6dc85572003-07-17 13:43:59 +0000383#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000384}
385
Keith Whitwell6dc85572003-07-17 13:43:59 +0000386/**
387 * Exported OpenGL SI interface.
388 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000389__GLcontext *
390__glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
391{
392 GLcontext *ctx;
393
Brian Paul4753d602002-06-15 02:38:15 +0000394 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
Brian Paulb1394fa2000-09-26 20:53:53 +0000395 if (ctx == NULL) {
396 return NULL;
397 }
Brian Paul60b6e4f2002-10-14 17:08:17 +0000398
Brian Paul3c634522002-10-24 23:57:19 +0000399 _mesa_initialize_context(ctx, modes, NULL, imports, GL_FALSE);
Brian Paulb1394fa2000-09-26 20:53:53 +0000400 ctx->imports = *imports;
Brian Paulb1394fa2000-09-26 20:53:53 +0000401
402 return ctx;
403}
404
Keith Whitwell6dc85572003-07-17 13:43:59 +0000405/**
406 * Exported OpenGL SI interface.
407 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000408void
409__glCoreNopDispatch(void)
410{
411#if 0
412 /* SI */
413 __gl_dispatch = __glNopDispatchState;
414#else
415 /* Mesa */
416 _glapi_set_dispatch(NULL);
417#endif
418}
419
Keith Whitwell6dc85572003-07-17 13:43:59 +0000420/*@}*/
421
Brian Paulb1394fa2000-09-26 20:53:53 +0000422
jtgafb833d1999-08-19 00:55:39 +0000423/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000424/** \name GL Visual allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000425/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000426/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000427
Keith Whitwell6dc85572003-07-17 13:43:59 +0000428/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000429 * Allocate a new GLvisual object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000430 *
431 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
432 * \param dbFlag double buffering
433 * \param stereoFlag stereo buffer
434 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
435 * is acceptable but the actual depth type will be GLushort or GLuint as
436 * needed.
437 * \param stencilBits requested minimum bits per stencil buffer value
438 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
439 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
440 * \param redBits number of bits per color component in frame buffer for RGB(A)
441 * mode. We always use 8 in core Mesa though.
442 * \param greenBits same as above.
443 * \param blueBits same as above.
444 * \param alphaBits same as above.
445 * \param numSamples not really used.
446 *
447 * \return pointer to new GLvisual or NULL if requested parameters can't be
448 * met.
449 *
450 * Allocates a GLvisual structure and initializes it via
451 * _mesa_initialize_visual().
Brian Paul4d053dd2000-01-14 04:45:47 +0000452 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000453GLvisual *
454_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000455 GLboolean dbFlag,
456 GLboolean stereoFlag,
457 GLint redBits,
458 GLint greenBits,
459 GLint blueBits,
460 GLint alphaBits,
461 GLint indexBits,
462 GLint depthBits,
463 GLint stencilBits,
464 GLint accumRedBits,
465 GLint accumGreenBits,
466 GLint accumBlueBits,
467 GLint accumAlphaBits,
468 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000469{
Brian Paul178a1c52000-04-22 01:05:00 +0000470 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
471 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000472 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000473 redBits, greenBits, blueBits, alphaBits,
474 indexBits, depthBits, stencilBits,
475 accumRedBits, accumGreenBits,
476 accumBlueBits, accumAlphaBits,
Brian Paulb1394fa2000-09-26 20:53:53 +0000477 numSamples)) {
Brian Paul178a1c52000-04-22 01:05:00 +0000478 FREE(vis);
479 return NULL;
480 }
481 }
482 return vis;
483}
484
Keith Whitwell6dc85572003-07-17 13:43:59 +0000485/**
Brian Paul178a1c52000-04-22 01:05:00 +0000486 * Initialize the fields of the given GLvisual.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000487 *
488 * \return GL_TRUE on success, or GL_FALSE on failure.
489 *
490 * \sa _mesa_create_visual() above for the parameter description.
491 *
492 * Makes some sanity checks and fills in the fields of the
493 * GLvisual structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000494 */
495GLboolean
496_mesa_initialize_visual( GLvisual *vis,
497 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000498 GLboolean dbFlag,
499 GLboolean stereoFlag,
500 GLint redBits,
501 GLint greenBits,
502 GLint blueBits,
503 GLint alphaBits,
504 GLint indexBits,
505 GLint depthBits,
506 GLint stencilBits,
507 GLint accumRedBits,
508 GLint accumGreenBits,
509 GLint accumBlueBits,
510 GLint accumAlphaBits,
511 GLint numSamples )
512{
Brian Paulb6bcae52001-01-23 23:39:36 +0000513 (void) numSamples;
514
Brian Paul178a1c52000-04-22 01:05:00 +0000515 assert(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000516
Brian Pauled30dfa2000-03-03 17:47:39 +0000517 /* This is to catch bad values from device drivers not updated for
518 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
519 * bad value now (a 1-bit depth buffer!?!).
520 */
521 assert(depthBits == 0 || depthBits > 1);
522
523 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000524 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000525 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000526 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000527 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000528 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000529 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000530 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000531 }
532 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000533 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000534 }
535 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000536 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000537 }
538 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000539 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000540 }
541
Brian Paulb6bcae52001-01-23 23:39:36 +0000542 vis->rgbMode = rgbFlag;
543 vis->doubleBufferMode = dbFlag;
544 vis->stereoMode = stereoFlag;
Brian Paul153f1542002-10-29 15:04:35 +0000545
Brian Paulb6bcae52001-01-23 23:39:36 +0000546 vis->redBits = redBits;
547 vis->greenBits = greenBits;
548 vis->blueBits = blueBits;
549 vis->alphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000550
Brian Paulb6bcae52001-01-23 23:39:36 +0000551 vis->indexBits = indexBits;
552 vis->depthBits = depthBits;
553 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
554 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
555 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
556 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
557 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Pauled30dfa2000-03-03 17:47:39 +0000558
Brian Paul153f1542002-10-29 15:04:35 +0000559 vis->haveAccumBuffer = accumRedBits > 0;
560 vis->haveDepthBuffer = depthBits > 0;
561 vis->haveStencilBuffer = stencilBits > 0;
562
563 vis->numAuxBuffers = 0;
564 vis->level = 0;
565 vis->pixmapMode = 0;
566
Brian Paul178a1c52000-04-22 01:05:00 +0000567 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000568}
569
Keith Whitwell6dc85572003-07-17 13:43:59 +0000570/**
571 * Destroy a visual.
572 *
573 * \param vis visual.
574 *
575 * Frees the visual structure.
576 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000577void
578_mesa_destroy_visual( GLvisual *vis )
579{
580 FREE(vis);
581}
582
Keith Whitwell6dc85572003-07-17 13:43:59 +0000583/*@}*/
584
Brian Paulb371e0d2000-03-31 01:05:51 +0000585
Brian Paul4d053dd2000-01-14 04:45:47 +0000586/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000587/** \name GL Framebuffer allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000588/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000589/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000590
Keith Whitwell6dc85572003-07-17 13:43:59 +0000591/**
592 * Create a new framebuffer.
593 *
594 * A GLframebuffer is a structure which encapsulates the depth, stencil and
595 * accum buffers and related parameters.
596 *
597 * \param visual a GLvisual pointer (we copy the struct contents)
598 * \param softwareDepth create/use a software depth buffer?
599 * \param softwareStencil create/use a software stencil buffer?
600 * \param softwareAccum create/use a software accum buffer?
601 * \param softwareAlpha create/use a software alpha buffer?
602 *
603 * \return pointer to new GLframebuffer struct or NULL if error.
604 *
605 * Allocate a GLframebuffer structure and initializes it via
606 * _mesa_initialize_framebuffer().
Brian Paul4d053dd2000-01-14 04:45:47 +0000607 */
Brian Paul178a1c52000-04-22 01:05:00 +0000608GLframebuffer *
Brian Paulbe3602d2001-02-28 00:27:48 +0000609_mesa_create_framebuffer( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +0000610 GLboolean softwareDepth,
611 GLboolean softwareStencil,
612 GLboolean softwareAccum,
613 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000614{
Brian Paul178a1c52000-04-22 01:05:00 +0000615 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
616 assert(visual);
617 if (buffer) {
618 _mesa_initialize_framebuffer(buffer, visual,
619 softwareDepth, softwareStencil,
620 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000621 }
Brian Paul178a1c52000-04-22 01:05:00 +0000622 return buffer;
623}
624
Keith Whitwell6dc85572003-07-17 13:43:59 +0000625/**
Brian Paul178a1c52000-04-22 01:05:00 +0000626 * Initialize a GLframebuffer object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000627 *
628 * \sa _mesa_create_framebuffer() above for the parameter description.
629 *
630 * Makes some sanity checks and fills in the fields of the
631 * GLframebuffer structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000632 */
633void
634_mesa_initialize_framebuffer( GLframebuffer *buffer,
Brian Paulbe3602d2001-02-28 00:27:48 +0000635 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +0000636 GLboolean softwareDepth,
637 GLboolean softwareStencil,
638 GLboolean softwareAccum,
639 GLboolean softwareAlpha )
640{
641 assert(buffer);
642 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000643
Brian Paul6ec6b842002-10-30 19:49:29 +0000644 _mesa_bzero(buffer, sizeof(GLframebuffer));
Brian Paul85d81602002-06-17 23:36:31 +0000645
Brian Paul4d053dd2000-01-14 04:45:47 +0000646 /* sanity checks */
647 if (softwareDepth ) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000648 assert(visual->depthBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000649 }
650 if (softwareStencil) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000651 assert(visual->stencilBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000652 }
653 if (softwareAccum) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000654 assert(visual->rgbMode);
655 assert(visual->accumRedBits > 0);
656 assert(visual->accumGreenBits > 0);
657 assert(visual->accumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000658 }
659 if (softwareAlpha) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000660 assert(visual->rgbMode);
661 assert(visual->alphaBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000662 }
663
Brian Paul75978bd2001-04-27 21:17:20 +0000664 buffer->Visual = *visual;
Brian Paul4d053dd2000-01-14 04:45:47 +0000665 buffer->UseSoftwareDepthBuffer = softwareDepth;
666 buffer->UseSoftwareStencilBuffer = softwareStencil;
667 buffer->UseSoftwareAccumBuffer = softwareAccum;
668 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul4d053dd2000-01-14 04:45:47 +0000669}
670
Keith Whitwell6dc85572003-07-17 13:43:59 +0000671/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000672 * Free a framebuffer struct and its buffers.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000673 *
674 * Calls _mesa_free_framebuffer_data() and frees the structure.
Brian Paul4d053dd2000-01-14 04:45:47 +0000675 */
Brian Paul178a1c52000-04-22 01:05:00 +0000676void
Brian Paulb1394fa2000-09-26 20:53:53 +0000677_mesa_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000678{
679 if (buffer) {
Brian Paul75978bd2001-04-27 21:17:20 +0000680 _mesa_free_framebuffer_data(buffer);
Brian Paul4d053dd2000-01-14 04:45:47 +0000681 FREE(buffer);
682 }
683}
684
Keith Whitwell6dc85572003-07-17 13:43:59 +0000685/**
686 * Free the data hanging off of \p buffer, but not \p buffer itself.
687 *
688 * \param buffer framebuffer.
689 *
690 * Frees all the buffers associated with the structure.
Brian Paul75978bd2001-04-27 21:17:20 +0000691 */
692void
693_mesa_free_framebuffer_data( GLframebuffer *buffer )
694{
695 if (!buffer)
696 return;
697
Brian Paul87506682003-05-27 15:20:43 +0000698 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
Brian Paulaeb44342002-03-19 16:47:04 +0000699 MESA_PBUFFER_FREE( buffer->DepthBuffer );
Brian Paul75978bd2001-04-27 21:17:20 +0000700 buffer->DepthBuffer = NULL;
701 }
Brian Paul87506682003-05-27 15:20:43 +0000702 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
Brian Paulaeb44342002-03-19 16:47:04 +0000703 MESA_PBUFFER_FREE( buffer->Accum );
Brian Paul75978bd2001-04-27 21:17:20 +0000704 buffer->Accum = NULL;
705 }
Brian Paul87506682003-05-27 15:20:43 +0000706 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
Brian Paulaeb44342002-03-19 16:47:04 +0000707 MESA_PBUFFER_FREE( buffer->Stencil );
Brian Paul75978bd2001-04-27 21:17:20 +0000708 buffer->Stencil = NULL;
709 }
Brian Paul87506682003-05-27 15:20:43 +0000710 if (buffer->UseSoftwareAlphaBuffers){
711 if (buffer->FrontLeftAlpha) {
712 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
713 buffer->FrontLeftAlpha = NULL;
714 }
715 if (buffer->BackLeftAlpha) {
716 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
717 buffer->BackLeftAlpha = NULL;
718 }
719 if (buffer->FrontRightAlpha) {
720 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
721 buffer->FrontRightAlpha = NULL;
722 }
723 if (buffer->BackRightAlpha) {
724 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
725 buffer->BackRightAlpha = NULL;
726 }
Brian Paul75978bd2001-04-27 21:17:20 +0000727 }
728}
729
Keith Whitwell6dc85572003-07-17 13:43:59 +0000730/*@}*/
Brian Paul75978bd2001-04-27 21:17:20 +0000731
Brian Paul4d053dd2000-01-14 04:45:47 +0000732
733/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000734/** \name Context allocation, initialization, destroying
735 *
736 * The purpose of the most initialization functions here is to provide the
737 * default state values according to the OpenGL specification.
738 */
jtgafb833d1999-08-19 00:55:39 +0000739/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000740/*@{*/
jtgafb833d1999-08-19 00:55:39 +0000741
Keith Whitwell6dc85572003-07-17 13:43:59 +0000742/**
743 * One-time initialization mutex lock.
744 *
745 * \sa Used by one_time_init().
746 */
Brian Paul9560f052000-01-31 23:11:39 +0000747_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
748
Keith Whitwell6dc85572003-07-17 13:43:59 +0000749/**
750 * Calls all the various one-time-init functions in Mesa.
751 *
752 * While holding a global mutex lock, calls several initialization functions,
753 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
754 * defined.
755 *
756 * \sa _mesa_init_lists(), _math_init().
jtgafb833d1999-08-19 00:55:39 +0000757 */
Brian Paul178a1c52000-04-22 01:05:00 +0000758static void
Brian Paul4753d602002-06-15 02:38:15 +0000759one_time_init( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000760{
761 static GLboolean alreadyCalled = GL_FALSE;
Brian Paul9560f052000-01-31 23:11:39 +0000762 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000763 if (!alreadyCalled) {
Brian Paul27558a12003-03-01 01:50:20 +0000764 GLuint i;
765
Brian Paul4d053dd2000-01-14 04:45:47 +0000766 /* do some implementation tests */
767 assert( sizeof(GLbyte) == 1 );
768 assert( sizeof(GLshort) >= 2 );
769 assert( sizeof(GLint) >= 4 );
770 assert( sizeof(GLubyte) == 1 );
771 assert( sizeof(GLushort) >= 2 );
772 assert( sizeof(GLuint) >= 4 );
773
Brian Paul08836342001-03-03 20:33:27 +0000774 _mesa_init_lists();
Keith Whitwell23caf202000-11-16 21:05:34 +0000775
Keith Whitwell6dc85572003-07-17 13:43:59 +0000776#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000777 _math_init();
Brian Paul27558a12003-03-01 01:50:20 +0000778
779 for (i = 0; i < 256; i++) {
780 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
781 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000782#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000783
davem69775355a2001-06-05 23:54:00 +0000784#ifdef USE_SPARC_ASM
785 _mesa_init_sparc_glapi_relocs();
786#endif
Brian Paul3c634522002-10-24 23:57:19 +0000787 if (_mesa_getenv("MESA_DEBUG")) {
Brian Paul68ee4bc2000-01-28 19:02:22 +0000788 _glapi_noop_enable_warnings(GL_TRUE);
Brian Paul71072be2002-10-10 00:22:13 +0000789#ifndef GLX_DIRECT_RENDERING
790 /* libGL from before 2002/06/28 don't have this function. Someday,
791 * when newer libGL libs are common, remove the #ifdef test. This
792 * only serves to print warnings when calling undefined GL functions.
793 */
Brian Paul4e9676f2002-06-29 19:48:15 +0000794 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
Brian Paul71072be2002-10-10 00:22:13 +0000795#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000796 }
797 else {
798 _glapi_noop_enable_warnings(GL_FALSE);
799 }
800
jtgafb833d1999-08-19 00:55:39 +0000801#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
Brian Paul4e9676f2002-06-29 19:48:15 +0000802 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
jtgafb833d1999-08-19 00:55:39 +0000803#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000804
805 alreadyCalled = GL_TRUE;
806 }
Brian Paul9560f052000-01-31 23:11:39 +0000807 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000808}
809
Keith Whitwell6dc85572003-07-17 13:43:59 +0000810/**
jtgafb833d1999-08-19 00:55:39 +0000811 * Allocate and initialize a shared context state structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000812 *
813 * \return pointer to a gl_shared_state structure on success, or NULL on
814 * failure.
815 *
816 * Initializes the display list, texture objects and vertex programs hash
817 * tables, allocates the texture objects. If it runs out of memory, frees
818 * everything already allocated before returning NULL.
jtgafb833d1999-08-19 00:55:39 +0000819 */
Brian Paula3f13702003-04-01 16:41:50 +0000820static GLboolean
821alloc_shared_state( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000822{
Brian Paula3f13702003-04-01 16:41:50 +0000823 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000824 if (!ss)
Brian Paula3f13702003-04-01 16:41:50 +0000825 return GL_FALSE;
826
827 ctx->Shared = ss;
jtgafb833d1999-08-19 00:55:39 +0000828
Brian Paule4b684c2000-09-12 21:07:40 +0000829 _glthread_INIT_MUTEX(ss->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000830
Brian Paule4b684c2000-09-12 21:07:40 +0000831 ss->DisplayList = _mesa_NewHashTable();
Brian Paulbb797902000-01-24 16:19:54 +0000832 ss->TexObjects = _mesa_NewHashTable();
Brian Paul451f3102003-04-17 01:48:19 +0000833#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
Brian Paul610d5992003-01-14 04:55:45 +0000834 ss->Programs = _mesa_NewHashTable();
Brian Paul8dfc5b92002-10-16 17:57:51 +0000835#endif
jtgafb833d1999-08-19 00:55:39 +0000836
Brian Paul451f3102003-04-17 01:48:19 +0000837#if FEATURE_ARB_vertex_program
838 ss->DefaultVertexProgram = _mesa_alloc_program(ctx, GL_VERTEX_PROGRAM_ARB, 0);
839 if (!ss->DefaultVertexProgram)
840 goto cleanup;
841#endif
842#if FEATURE_ARB_fragment_program
843 ss->DefaultFragmentProgram = _mesa_alloc_program(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
844 if (!ss->DefaultFragmentProgram)
845 goto cleanup;
846#endif
847
Brian Paula3f13702003-04-01 16:41:50 +0000848 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
849 if (!ss->Default1D)
850 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000851
Brian Paula3f13702003-04-01 16:41:50 +0000852 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
853 if (!ss->Default2D)
854 goto cleanup;
jtgafb833d1999-08-19 00:55:39 +0000855
Brian Paula3f13702003-04-01 16:41:50 +0000856 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
857 if (!ss->Default3D)
858 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000859
Brian Paula3f13702003-04-01 16:41:50 +0000860 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
861 if (!ss->DefaultCubeMap)
862 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000863
Brian Paula3f13702003-04-01 16:41:50 +0000864 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
865 if (!ss->DefaultRect)
866 goto cleanup;
Brian Paul413d6a22000-05-26 14:44:59 +0000867
Brian Paula3f13702003-04-01 16:41:50 +0000868#if 0
869 _mesa_save_texture_object(ctx, ss->Default1D);
870 _mesa_save_texture_object(ctx, ss->Default2D);
871 _mesa_save_texture_object(ctx, ss->Default3D);
872 _mesa_save_texture_object(ctx, ss->DefaultCubeMap);
873 _mesa_save_texture_object(ctx, ss->DefaultRect);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000874#endif
Brian Paula3f13702003-04-01 16:41:50 +0000875
876 /* Effectively bind the default textures to all texture units */
877 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
878 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
879 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
880 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
881 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
882
883 return GL_TRUE;
884
885 cleanup:
886 /* Ran out of memory at some point. Free everything and return NULL */
887 if (ss->DisplayList)
888 _mesa_DeleteHashTable(ss->DisplayList);
889 if (ss->TexObjects)
890 _mesa_DeleteHashTable(ss->TexObjects);
891#if FEATURE_NV_vertex_program
892 if (ss->Programs)
893 _mesa_DeleteHashTable(ss->Programs);
894#endif
Brian Paul451f3102003-04-17 01:48:19 +0000895#if FEATURE_ARB_vertex_program
896 if (ss->DefaultVertexProgram)
897 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
898#endif
899#if FEATURE_ARB_fragment_program
900 if (ss->DefaultFragmentProgram)
901 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
902#endif
Brian Paula3f13702003-04-01 16:41:50 +0000903 if (ss->Default1D)
904 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
905 if (ss->Default2D)
906 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
907 if (ss->Default3D)
908 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
909 if (ss->DefaultCubeMap)
910 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
911 if (ss->DefaultRect)
912 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
913 if (ss)
914 _mesa_free(ss);
915 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000916}
917
Keith Whitwell6dc85572003-07-17 13:43:59 +0000918/**
jtgafb833d1999-08-19 00:55:39 +0000919 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000920 *
921 * \param ctx GL context.
922 * \param ss shared state pointer.
923 *
924 * Frees the display lists, the texture objects (calling the driver texture
925 * deletion callback to free its private data) and the vertex programs, as well
926 * as their hash tables.
927 *
928 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000929 */
Brian Paul178a1c52000-04-22 01:05:00 +0000930static void
931free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000932{
933 /* Free display lists */
934 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000935 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000936 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000937 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000938 }
939 else {
940 break;
941 }
942 }
Brian Paulbb797902000-01-24 16:19:54 +0000943 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000944
945 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000946 ASSERT(ctx->Driver.DeleteTexture);
947 while (1) {
948 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
949 if (texName) {
950 struct gl_texture_object *texObj = (struct gl_texture_object *)
951 _mesa_HashLookup(ss->TexObjects, texName);
952 ASSERT(texObj);
953 (*ctx->Driver.DeleteTexture)(ctx, texObj);
954 _mesa_HashRemove(ss->TexObjects, texName);
955 }
956 else {
957 break;
958 }
jtgafb833d1999-08-19 00:55:39 +0000959 }
Brian Paulbb797902000-01-24 16:19:54 +0000960 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000961
Brian Paul8dfc5b92002-10-16 17:57:51 +0000962#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +0000963 /* Free vertex programs */
964 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +0000965 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +0000966 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +0000967 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
968 prog);
969 ASSERT(p);
970 _mesa_delete_program(ctx, p);
971 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +0000972 }
973 else {
974 break;
975 }
976 }
Brian Paul610d5992003-01-14 04:55:45 +0000977 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000978#endif
Brian Paul30f51ae2001-12-18 04:06:44 +0000979
Keith Whitwelle15fd852002-12-12 13:03:15 +0000980 _glthread_DESTROY_MUTEX(ss->Mutex);
981
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000982 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000983}
984
985
Keith Whitwell6dc85572003-07-17 13:43:59 +0000986static void _mesa_init_current( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000987{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000988 int i;
jtgafb833d1999-08-19 00:55:39 +0000989
Keith Whitwell6dc85572003-07-17 13:43:59 +0000990 /* Current group */
991 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
992 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +0000993 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000994 /* special cases: */
995 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
996 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
997 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
998 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 0.0 );
999 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
1000 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
1001 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i], 0.0, 0.0, 0.0, 1.0);
1002 ctx->Current.Index = 1;
1003 ctx->Current.EdgeFlag = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001004}
1005
1006
Keith Whitwell6dc85572003-07-17 13:43:59 +00001007static void
1008_mesa_init_constants( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001009{
Brian Paul4d053dd2000-01-14 04:45:47 +00001010 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +00001011
Brian Paulcd1cefa2001-06-13 14:56:14 +00001012 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1013 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1014
Brian Paul539cce52000-02-03 19:40:07 +00001015 /* Constants, may be overriden by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +00001016 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
Brian Paulcd1cefa2001-06-13 14:56:14 +00001017 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1018 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
Brian Paul8afe7de2002-06-15 03:03:06 +00001019 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001020 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
Brian Paul610d5992003-01-14 04:55:45 +00001021 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1022 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
Gareth Hughes2c3d34c2001-03-18 08:53:49 +00001023 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
Brian Paul87c964d2001-11-06 15:53:00 +00001024 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
Brian Paul4d053dd2000-01-14 04:45:47 +00001025 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +00001026 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1027 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1028 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1029 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1030 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001031 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001032 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1033 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1034 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1035 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001036 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001037 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
Brian Paul4bdcfe52000-04-17 17:57:04 +00001038 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +00001039 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1040 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
Brian Paula8644322000-11-27 18:22:13 +00001041 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1042 ctx->Const.MaxLights = MAX_LIGHTS;
Ian Romanick882caa12003-05-30 21:37:14 +00001043 ctx->Const.MaxSpotExponent = 128.0;
1044 ctx->Const.MaxShininess = 128.0;
Brian Pauld0492cf2003-04-11 01:20:06 +00001045#if FEATURE_ARB_vertex_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001046 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001047 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1048 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1049 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1050 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1051 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001052#endif
1053#if FEATURE_ARB_fragment_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001054 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001055 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1056 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1057 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1058 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1059 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1060 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1061 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1062 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001063#endif
Brian Pauledd67742003-04-18 18:02:43 +00001064 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1065 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
Brian Pauld0492cf2003-04-11 01:20:06 +00001066
Brian Paul92f97852003-05-01 22:44:02 +00001067 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
Keith Whitwell6dc85572003-07-17 13:43:59 +00001068}
jtgafb833d1999-08-19 00:55:39 +00001069
Keith Whitwell6dc85572003-07-17 13:43:59 +00001070/**
1071 * Initialize the attribute groups in a GL context.
1072 *
1073 * \param ctx GL context.
1074 *
1075 * Initializes all the attributes, calling the respective <tt>init*</tt>
1076 * functions for the more complex data structures.
1077 */
1078static GLboolean
1079init_attrib_groups( GLcontext *ctx )
1080{
1081 assert(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001082
Keith Whitwell6dc85572003-07-17 13:43:59 +00001083 /* Constants */
1084 _mesa_init_constants( ctx );
Brian Paul0771d152000-04-07 00:19:41 +00001085
Brian Paul4d053dd2000-01-14 04:45:47 +00001086 /* Extensions */
Brian Paulde4f4602003-07-03 02:15:06 +00001087 _mesa_init_extensions( ctx );
jtgafb833d1999-08-19 00:55:39 +00001088
Keith Whitwell6dc85572003-07-17 13:43:59 +00001089 /* Attribute Groups */
1090 _mesa_init_accum( ctx );
1091 _mesa_init_attrib( ctx );
1092 _mesa_init_buffers( ctx );
1093 _mesa_init_color( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001094 _mesa_init_colortables( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001095 _mesa_init_current( ctx );
1096 _mesa_init_depth( ctx );
1097 _mesa_init_debug( ctx );
1098 _mesa_init_display_list( ctx );
1099 _mesa_init_eval( ctx );
1100 _mesa_init_feedback( ctx );
1101 _mesa_init_fog( ctx );
1102 _mesa_init_histogram( ctx );
1103 _mesa_init_hint( ctx );
1104 _mesa_init_line( ctx );
1105 _mesa_init_lighting( ctx );
1106 _mesa_init_matrix( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001107 _mesa_init_occlude( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001108 _mesa_init_pixel( ctx );
1109 _mesa_init_point( ctx );
1110 _mesa_init_polygon( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001111 _mesa_init_program( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001112 _mesa_init_rastpos( ctx );
1113 _mesa_init_stencil( ctx );
1114 _mesa_init_transform( ctx );
1115 _mesa_init_varray( ctx );
1116 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001117
Keith Whitwell6dc85572003-07-17 13:43:59 +00001118 if (!_mesa_init_texture( ctx ))
1119 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001120
Brian Paul4d053dd2000-01-14 04:45:47 +00001121 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001122 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001123 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paul4d053dd2000-01-14 04:45:47 +00001124 ctx->CatchSignals = GL_TRUE;
Brian Paulf782b812002-10-04 17:37:45 +00001125 ctx->_Facing = 0;
Brian Paul4d053dd2000-01-14 04:45:47 +00001126
Brian Paula3f13702003-04-01 16:41:50 +00001127 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001128}
1129
1130
Brian Paulf44898c2003-07-18 15:44:57 +00001131/**
1132 * If the DRI libGL.so library is old, it may not have the entrypoints for
1133 * some recent OpenGL extensions. Dynamically add them now.
1134 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1135 * and driver code, this won't be an issue (and calling this function won't
1136 * do any harm).
1137 */
1138static void
1139add_newer_entrypoints(void)
1140{
1141 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1142 _glapi_add_entrypoint("glWindowPos2dARB", 513);
1143 _glapi_add_entrypoint("glWindowPos2dvARB", 514);
1144 _glapi_add_entrypoint("glWindowPos2fARB", 515);
1145 _glapi_add_entrypoint("glWindowPos2fvARB", 516);
1146 _glapi_add_entrypoint("glWindowPos2iARB", 517);
1147 _glapi_add_entrypoint("glWindowPos2ivARB", 518);
1148 _glapi_add_entrypoint("glWindowPos2sARB", 519);
1149 _glapi_add_entrypoint("glWindowPos2svARB", 520);
1150 _glapi_add_entrypoint("glWindowPos3dARB", 521);
1151 _glapi_add_entrypoint("glWindowPos3dvARB", 522);
1152 _glapi_add_entrypoint("glWindowPos3fARB", 523);
1153 _glapi_add_entrypoint("glWindowPos3fvARB", 524);
1154 _glapi_add_entrypoint("glWindowPos3iARB", 525);
1155 _glapi_add_entrypoint("glWindowPos3ivARB", 526);
1156 _glapi_add_entrypoint("glWindowPos3sARB", 527);
1157 _glapi_add_entrypoint("glWindowPos3svARB", 528);
1158#if FEATURE_NV_vertex_program
1159 _glapi_add_entrypoint("glAreProgramsResidentNV", 578);
1160 _glapi_add_entrypoint("glBindProgramNV", 579);
1161 _glapi_add_entrypoint("glDeleteProgramsNV", 580);
1162 _glapi_add_entrypoint("glExecuteProgramNV", 581);
1163 _glapi_add_entrypoint("glGenProgramsNV", 582);
1164 _glapi_add_entrypoint("glGetProgramParameterdvNV", 583);
1165 _glapi_add_entrypoint("glGetProgramParameterfvNV", 584);
1166 _glapi_add_entrypoint("glGetProgramivNV", 585);
1167 _glapi_add_entrypoint("glGetProgramStringNV", 586);
1168 _glapi_add_entrypoint("glGetTrackMatrixivNV", 587);
1169 _glapi_add_entrypoint("glGetVertexAttribdvNV", 588);
1170 _glapi_add_entrypoint("glGetVertexAttribfvNV", 589);
1171 _glapi_add_entrypoint("glGetVertexAttribivNV", 590);
1172 _glapi_add_entrypoint("glGetVertexAttribPointervNV", 591);
1173 _glapi_add_entrypoint("glIsProgramNV", 592);
1174 _glapi_add_entrypoint("glLoadProgramNV", 593);
1175 _glapi_add_entrypoint("glProgramParameter4dNV", 594);
1176 _glapi_add_entrypoint("glProgramParameter4dvNV", 595);
1177 _glapi_add_entrypoint("glProgramParameter4fNV", 596);
1178 _glapi_add_entrypoint("glProgramParameter4fvNV", 597);
1179 _glapi_add_entrypoint("glProgramParameters4dvNV", 598);
1180 _glapi_add_entrypoint("glProgramParameters4fvNV", 599);
1181 _glapi_add_entrypoint("glRequestResidentProgramsNV", 600);
1182 _glapi_add_entrypoint("glTrackMatrixNV", 601);
1183 _glapi_add_entrypoint("glVertexAttribPointerNV", 602);
1184 _glapi_add_entrypoint("glVertexAttrib1dNV", 603);
1185 _glapi_add_entrypoint("glVertexAttrib1dvNV", 604);
1186 _glapi_add_entrypoint("glVertexAttrib1fNV", 605);
1187 _glapi_add_entrypoint("glVertexAttrib1fvNV", 606);
1188 _glapi_add_entrypoint("glVertexAttrib1sNV", 607);
1189 _glapi_add_entrypoint("glVertexAttrib1svNV", 608);
1190 _glapi_add_entrypoint("glVertexAttrib2dNV", 609);
1191 _glapi_add_entrypoint("glVertexAttrib2dvNV", 610);
1192 _glapi_add_entrypoint("glVertexAttrib2fNV", 611);
1193 _glapi_add_entrypoint("glVertexAttrib2fvNV", 612);
1194 _glapi_add_entrypoint("glVertexAttrib2sNV", 613);
1195 _glapi_add_entrypoint("glVertexAttrib2svNV", 614);
1196 _glapi_add_entrypoint("glVertexAttrib3dNV", 615);
1197 _glapi_add_entrypoint("glVertexAttrib3dvNV", 616);
1198 _glapi_add_entrypoint("glVertexAttrib3fNV", 617);
1199 _glapi_add_entrypoint("glVertexAttrib3fvNV", 618);
1200 _glapi_add_entrypoint("glVertexAttrib3sNV", 619);
1201 _glapi_add_entrypoint("glVertexAttrib3svNV", 620);
1202 _glapi_add_entrypoint("glVertexAttrib4dNV", 621);
1203 _glapi_add_entrypoint("glVertexAttrib4dvNV", 622);
1204 _glapi_add_entrypoint("glVertexAttrib4fNV", 623);
1205 _glapi_add_entrypoint("glVertexAttrib4fvNV", 624);
1206 _glapi_add_entrypoint("glVertexAttrib4sNV", 625);
1207 _glapi_add_entrypoint("glVertexAttrib4svNV", 626);
1208 _glapi_add_entrypoint("glVertexAttrib4ubNV", 627);
1209 _glapi_add_entrypoint("glVertexAttrib4ubvNV", 628);
1210 _glapi_add_entrypoint("glVertexAttribs1dvNV", 629);
1211 _glapi_add_entrypoint("glVertexAttribs1fvNV", 630);
1212 _glapi_add_entrypoint("glVertexAttribs1svNV", 631);
1213 _glapi_add_entrypoint("glVertexAttribs2dvNV", 632);
1214 _glapi_add_entrypoint("glVertexAttribs2fvNV", 633);
1215 _glapi_add_entrypoint("glVertexAttribs2svNV", 634);
1216 _glapi_add_entrypoint("glVertexAttribs3dvNV", 635);
1217 _glapi_add_entrypoint("glVertexAttribs3fvNV", 636);
1218 _glapi_add_entrypoint("glVertexAttribs3svNV", 637);
1219 _glapi_add_entrypoint("glVertexAttribs4dvNV", 638);
1220 _glapi_add_entrypoint("glVertexAttribs4fvNV", 639);
1221 _glapi_add_entrypoint("glVertexAttribs4svNV", 640);
1222 _glapi_add_entrypoint("glVertexAttribs4ubvNV", 641);
1223#endif
1224 _glapi_add_entrypoint("glPointParameteriNV", 642);
1225 _glapi_add_entrypoint("glPointParameterivNV", 643);
1226 _glapi_add_entrypoint("glMultiDrawArraysEXT", 644);
1227 _glapi_add_entrypoint("glMultiDrawElementsEXT", 645);
1228 _glapi_add_entrypoint("glActiveStencilFaceEXT", 646);
1229#if FEATURE_NV_fence
1230 _glapi_add_entrypoint("glDeleteFencesNV", 647);
1231 _glapi_add_entrypoint("glGenFencesNV", 648);
1232 _glapi_add_entrypoint("glIsFenceNV", 649);
1233 _glapi_add_entrypoint("glTestFenceNV", 650);
1234 _glapi_add_entrypoint("glGetFenceivNV", 651);
1235 _glapi_add_entrypoint("glFinishFenceNV", 652);
1236 _glapi_add_entrypoint("glSetFenceNV", 653);
1237#endif
1238#if FEATURE_NV_fragment_program
1239 _glapi_add_entrypoint("glProgramNamedParameter4fNV", 682);
1240 _glapi_add_entrypoint("glProgramNamedParameter4dNV", 683);
1241 _glapi_add_entrypoint("glProgramNamedParameter4fvNV", 683);
1242 _glapi_add_entrypoint("glProgramNamedParameter4dvNV", 684);
1243 _glapi_add_entrypoint("glGetProgramNamedParameterfvNV", 685);
1244 _glapi_add_entrypoint("glGetProgramNamedParameterdvNV", 686);
1245#endif
1246#if FEATURE_ARB_vertex_program
1247 _glapi_add_entrypoint("glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV);
1248 _glapi_add_entrypoint("glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV);
1249 _glapi_add_entrypoint("glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV);
1250 _glapi_add_entrypoint("glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV);
1251 _glapi_add_entrypoint("glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV);
1252 _glapi_add_entrypoint("glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV);
1253 _glapi_add_entrypoint("glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV);
1254 _glapi_add_entrypoint("glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV);
1255 _glapi_add_entrypoint("glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV);
1256 _glapi_add_entrypoint("glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV);
1257 _glapi_add_entrypoint("glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV);
1258 _glapi_add_entrypoint("glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV);
1259 _glapi_add_entrypoint("glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV);
1260 _glapi_add_entrypoint("glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV);
1261 _glapi_add_entrypoint("glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV);
1262 _glapi_add_entrypoint("glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV);
1263 _glapi_add_entrypoint("glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV);
1264 _glapi_add_entrypoint("glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV);
1265 _glapi_add_entrypoint("glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV);
1266 _glapi_add_entrypoint("glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV);
1267 _glapi_add_entrypoint("glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV);
1268 _glapi_add_entrypoint("glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV);
1269 _glapi_add_entrypoint("glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB);
1270 _glapi_add_entrypoint("glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV);
1271 _glapi_add_entrypoint("glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB);
1272 _glapi_add_entrypoint("glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB);
1273 _glapi_add_entrypoint("glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB);
1274 _glapi_add_entrypoint("glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB);
1275 _glapi_add_entrypoint("glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV);
1276 _glapi_add_entrypoint("glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV);
1277 _glapi_add_entrypoint("glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB);
1278 _glapi_add_entrypoint("glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB);
1279 _glapi_add_entrypoint("glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB);
1280 _glapi_add_entrypoint("glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV);
1281 _glapi_add_entrypoint("glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB);
1282 _glapi_add_entrypoint("glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB);
1283 _glapi_add_entrypoint("glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB);
1284 _glapi_add_entrypoint("glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB);
1285 _glapi_add_entrypoint("glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB);
1286 _glapi_add_entrypoint("glProgramStringARB", _gloffset_ProgramStringARB);
1287 _glapi_add_entrypoint("glBindProgramARB", _gloffset_BindProgramNV);
1288 _glapi_add_entrypoint("glDeleteProgramsARB", _gloffset_DeleteProgramsNV);
1289 _glapi_add_entrypoint("glGenProgramsARB", _gloffset_GenProgramsNV);
1290 _glapi_add_entrypoint("glIsProgramARB", _gloffset_IsProgramNV);
1291 _glapi_add_entrypoint("glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB);
1292 _glapi_add_entrypoint("glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB);
1293 _glapi_add_entrypoint("glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB);
1294 _glapi_add_entrypoint("glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB);
1295 _glapi_add_entrypoint("glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB);
1296 _glapi_add_entrypoint("glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB);
1297 _glapi_add_entrypoint("glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB);
1298 _glapi_add_entrypoint("glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB);
1299 _glapi_add_entrypoint("glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB);
1300 _glapi_add_entrypoint("glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB);
1301 _glapi_add_entrypoint("glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB);
1302 _glapi_add_entrypoint("glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB);
1303 _glapi_add_entrypoint("glGetProgramivARB", _gloffset_GetProgramivARB);
1304 _glapi_add_entrypoint("glGetProgramStringARB", _gloffset_GetProgramStringARB);
1305 _glapi_add_entrypoint("glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV);
1306 _glapi_add_entrypoint("glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV);
1307 _glapi_add_entrypoint("glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV);
1308 _glapi_add_entrypoint("glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV);
1309#endif
1310}
1311
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001312
Brian Paulde4f4602003-07-03 02:15:06 +00001313/**
Keith Whitwell6dc85572003-07-17 13:43:59 +00001314 * Initialize a GLcontext struct.
1315 *
1316 * This includes allocating all the other structs and arrays which hang off of
1317 * the context by pointers.
1318 *
1319 * \sa _mesa_create_context() for the parameter description.
1320 *
1321 * Performs the imports and exports callback tables initialization, and
1322 * miscellaneous one-time initializations. If no shared context is supplied one
1323 * is allocated, and increase its reference count. Setups the GL API dispatch
1324 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1325 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1326 * for debug flags.
1327 *
Brian Paulde4f4602003-07-03 02:15:06 +00001328 * \note the direct parameter is ignored (obsolete).
jtgafb833d1999-08-19 00:55:39 +00001329 */
Brian Paul178a1c52000-04-22 01:05:00 +00001330GLboolean
1331_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001332 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001333 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001334 void *driver_ctx,
1335 GLboolean direct )
jtgafb833d1999-08-19 00:55:39 +00001336{
Brian Paul5fb84d22000-05-24 15:04:45 +00001337 GLuint dispatchSize;
1338
Brian Paul3c634522002-10-24 23:57:19 +00001339 ASSERT(driver_ctx);
jtgafb833d1999-08-19 00:55:39 +00001340
Brian Paul3c634522002-10-24 23:57:19 +00001341 /* If the driver wants core Mesa to use special imports, it'll have to
1342 * override these defaults.
1343 */
1344 _mesa_init_default_imports( &(ctx->imports), driver_ctx );
jtgafb833d1999-08-19 00:55:39 +00001345
Brian Paul9a33a112002-06-13 04:28:29 +00001346 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001347 _mesa_init_default_exports( &(ctx->exports) );
1348
1349 /* misc one-time initializations */
1350 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001351
Brian Paul3c634522002-10-24 23:57:19 +00001352 ctx->DriverCtx = driver_ctx;
Brian Paulb1394fa2000-09-26 20:53:53 +00001353 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001354 ctx->DrawBuffer = NULL;
1355 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001356
Brian Paula3f13702003-04-01 16:41:50 +00001357 /* Set these pointers to defaults now in case they're not set since
1358 * we need them while creating the default textures.
1359 */
1360 if (!ctx->Driver.NewTextureObject)
1361 ctx->Driver.NewTextureObject = _mesa_new_texture_object;
1362 if (!ctx->Driver.DeleteTexture)
1363 ctx->Driver.DeleteTexture = _mesa_delete_texture_object;
1364 if (!ctx->Driver.NewTextureImage)
1365 ctx->Driver.NewTextureImage = _mesa_new_texture_image;
1366
jtgafb833d1999-08-19 00:55:39 +00001367 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001368 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001369 ctx->Shared = share_list->Shared;
1370 }
1371 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001372 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001373 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001374 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001375 }
1376 }
Brian Paul9560f052000-01-31 23:11:39 +00001377 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001378 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001379 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001380
Keith Whitwell6dc85572003-07-17 13:43:59 +00001381 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001382 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001383 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001384 }
jtgafb833d1999-08-19 00:55:39 +00001385
Brian Paulf44898c2003-07-18 15:44:57 +00001386 /* libGL ABI coordination */
1387 add_newer_entrypoints();
Keith Whitwell6dc85572003-07-17 13:43:59 +00001388
Brian Paul5fb84d22000-05-24 15:04:45 +00001389 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1390 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1391 * Mesa we do this to accomodate different versions of libGL and various
1392 * DRI drivers.
1393 */
1394 dispatchSize = MAX2(_glapi_get_dispatch_table_size(),
1395 sizeof(struct _glapi_table) / sizeof(void *));
1396
Brian Paulfbd8f211999-11-11 01:22:25 +00001397 /* setup API dispatch tables */
Brian Paul5fb84d22000-05-24 15:04:45 +00001398 ctx->Exec = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
1399 ctx->Save = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001400 if (!ctx->Exec || !ctx->Save) {
1401 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001402 if (ctx->Exec)
Brian Paul2d8db392000-06-27 22:10:00 +00001403 FREE( ctx->Exec );
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001404 }
Brian Paul5fb84d22000-05-24 15:04:45 +00001405 _mesa_init_exec_table(ctx->Exec, dispatchSize);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001406 ctx->CurrentDispatch = ctx->Exec;
jtgafb833d1999-08-19 00:55:39 +00001407
Keith Whitwell6dc85572003-07-17 13:43:59 +00001408#if _HAVE_FULL_GL
1409 _mesa_init_dlist_table(ctx->Save, dispatchSize);
1410
Keith Whitwellad2ac212000-11-24 10:25:05 +00001411 ctx->ExecPrefersFloat = GL_FALSE;
1412 ctx->SavePrefersFloat = GL_FALSE;
1413
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001414 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001415 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001416 ctx->TnlModule.Current = NULL;
1417 ctx->TnlModule.SwapCount = 0;
1418
Keith Whitwell6dc85572003-07-17 13:43:59 +00001419#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001420
Brian Paul4d053dd2000-01-14 04:45:47 +00001421 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001422}
1423
Brian Paulde4f4602003-07-03 02:15:06 +00001424/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001425 * Allocate and initialize a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001426 *
1427 * \param visual a GLvisual pointer (we copy the struct contents)
1428 * \param share_list another context to share display lists with or NULL
1429 * \param driver_ctx pointer to device driver's context state struct
1430 * \param direct obsolete, ignored
1431 *
1432 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001433 */
Brian Paul178a1c52000-04-22 01:05:00 +00001434GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001435_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001436 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001437 void *driver_ctx,
1438 GLboolean direct )
1439
Brian Paul4d053dd2000-01-14 04:45:47 +00001440{
Brian Paul4753d602002-06-15 02:38:15 +00001441 GLcontext *ctx;
1442
1443 ASSERT(visual);
Brian Paul3c634522002-10-24 23:57:19 +00001444 ASSERT(driver_ctx);
Brian Paul4753d602002-06-15 02:38:15 +00001445
Brian Paul3c634522002-10-24 23:57:19 +00001446 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001447 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001448 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001449
Brian Paul3c634522002-10-24 23:57:19 +00001450 if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001451 return ctx;
1452 }
1453 else {
Brian Paul3c634522002-10-24 23:57:19 +00001454 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001455 return NULL;
1456 }
1457}
1458
Keith Whitwell6dc85572003-07-17 13:43:59 +00001459/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001460 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001461 *
1462 * But doesn't free the GLcontext struct itself.
1463 *
1464 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001465 */
Brian Paul178a1c52000-04-22 01:05:00 +00001466void
Brian Paulb1394fa2000-09-26 20:53:53 +00001467_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001468{
Brian Paul4d053dd2000-01-14 04:45:47 +00001469 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001470 if (ctx == _mesa_get_current_context()) {
1471 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001472 }
1473
Keith Whitwell6dc85572003-07-17 13:43:59 +00001474 _mesa_free_lighting_data( ctx );
1475 _mesa_free_eval_data( ctx );
1476 _mesa_free_texture_data( ctx );
1477 _mesa_free_matrix_data( ctx );
1478 _mesa_free_viewport_data( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001479 _mesa_free_colortables_data( ctx );
Brian Paul8dfc5b92002-10-16 17:57:51 +00001480#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +00001481 if (ctx->VertexProgram.Current) {
Brian Paul610d5992003-01-14 04:55:45 +00001482 ctx->VertexProgram.Current->Base.RefCount--;
1483 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001484 _mesa_delete_program(ctx, &(ctx->VertexProgram.Current->Base));
Brian Paul610d5992003-01-14 04:55:45 +00001485 }
1486#endif
1487#if FEATURE_NV_fragment_program
1488 if (ctx->FragmentProgram.Current) {
1489 ctx->FragmentProgram.Current->Base.RefCount--;
1490 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001491 _mesa_delete_program(ctx, &(ctx->FragmentProgram.Current->Base));
Brian Paulfd284452001-07-19 15:54:34 +00001492 }
Brian Paul8dfc5b92002-10-16 17:57:51 +00001493#endif
Brian Paulfd284452001-07-19 15:54:34 +00001494
Brian Paul30f51ae2001-12-18 04:06:44 +00001495 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001496 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001497 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001498 assert(ctx->Shared->RefCount >= 0);
1499 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1500 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001501 /* free shared state */
1502 free_shared_state( ctx, ctx->Shared );
1503 }
1504
Brian Paul702ca202003-07-18 15:22:16 +00001505 if (ctx->Extensions.String)
1506 FREE((void *) ctx->Extensions.String);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001507
1508 FREE(ctx->Exec);
1509 FREE(ctx->Save);
Brian Paul4d053dd2000-01-14 04:45:47 +00001510}
1511
Keith Whitwell6dc85572003-07-17 13:43:59 +00001512/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001513 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001514 *
1515 * \param ctx GL context.
1516 *
1517 * Calls _mesa_free_context_data() and free the structure.
jtgafb833d1999-08-19 00:55:39 +00001518 */
Brian Paul178a1c52000-04-22 01:05:00 +00001519void
Brian Paulb1394fa2000-09-26 20:53:53 +00001520_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001521{
1522 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001523 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001524 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001525 }
1526}
1527
Keith Whitwell6dc85572003-07-17 13:43:59 +00001528#if _HAVE_FULL_GL
1529/**
jtgafb833d1999-08-19 00:55:39 +00001530 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001531 *
1532 * \param src source context
1533 * \param dst destination context
1534 * \param mask bitwise OR of GL_*_BIT flags
1535 *
1536 * According to the bits specified in \p mask, copies the corresponding
1537 * attributes from \p src into \dst. For many of the attributes a simple \c
1538 * memcpy is not enough due to the existence of internal pointers in their data
1539 * structures.
jtgafb833d1999-08-19 00:55:39 +00001540 */
Brian Paul178a1c52000-04-22 01:05:00 +00001541void
Brian Paulb1394fa2000-09-26 20:53:53 +00001542_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001543{
1544 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001545 /* OK to memcpy */
1546 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001547 }
1548 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001549 /* OK to memcpy */
1550 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001551 }
1552 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001553 /* OK to memcpy */
1554 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001555 }
1556 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001557 /* OK to memcpy */
1558 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001559 }
1560 if (mask & GL_ENABLE_BIT) {
1561 /* no op */
1562 }
1563 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001564 /* OK to memcpy */
1565 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001566 }
1567 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001568 /* OK to memcpy */
1569 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001570 }
1571 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001572 /* OK to memcpy */
1573 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001574 }
1575 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001576 GLuint i;
1577 /* begin with memcpy */
1578 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1579 /* fixup linked lists to prevent pointer insanity */
1580 make_empty_list( &(dst->Light.EnabledList) );
1581 for (i = 0; i < MAX_LIGHTS; i++) {
1582 if (dst->Light.Light[i].Enabled) {
1583 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1584 }
1585 }
jtgafb833d1999-08-19 00:55:39 +00001586 }
1587 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001588 /* OK to memcpy */
1589 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001590 }
1591 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001592 /* OK to memcpy */
1593 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001594 }
1595 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001596 /* OK to memcpy */
1597 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001598 }
1599 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001600 /* OK to memcpy */
1601 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001602 }
1603 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001604 /* OK to memcpy */
1605 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001606 }
1607 if (mask & GL_POLYGON_STIPPLE_BIT) {
1608 /* Use loop instead of MEMCPY due to problem with Portland Group's
1609 * C compiler. Reported by John Stone.
1610 */
Brian Paul85d81602002-06-17 23:36:31 +00001611 GLuint i;
1612 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001613 dst->PolygonStipple[i] = src->PolygonStipple[i];
1614 }
1615 }
1616 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001617 /* OK to memcpy */
1618 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001619 }
1620 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001621 /* OK to memcpy */
1622 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001623 }
1624 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001625 /* Cannot memcpy because of pointers */
1626 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001627 }
1628 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001629 /* OK to memcpy */
1630 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001631 }
1632 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001633 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1634 dst->Viewport.X = src->Viewport.X;
1635 dst->Viewport.Y = src->Viewport.Y;
1636 dst->Viewport.Width = src->Viewport.Width;
1637 dst->Viewport.Height = src->Viewport.Height;
1638 dst->Viewport.Near = src->Viewport.Near;
1639 dst->Viewport.Far = src->Viewport.Far;
1640 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001641 }
Brian Paul85d81602002-06-17 23:36:31 +00001642
Keith Whitwella96308c2000-10-30 13:31:59 +00001643 /* XXX FIXME: Call callbacks?
1644 */
1645 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001646}
Keith Whitwell23caf202000-11-16 21:05:34 +00001647#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001648
1649
Brian Paulb1d53d92003-06-11 18:48:19 +00001650/**
1651 * Check if the given context can render into the given framebuffer
1652 * by checking visual attributes.
1653 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1654 */
1655static GLboolean
1656check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1657{
1658 const GLvisual *ctxvis = &ctx->Visual;
1659 const GLvisual *bufvis = &buffer->Visual;
1660
1661 if (ctxvis == bufvis)
1662 return GL_TRUE;
1663
1664 if (ctxvis->rgbMode != bufvis->rgbMode)
1665 return GL_FALSE;
1666 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1667 return GL_FALSE;
1668 if (ctxvis->stereoMode && !bufvis->stereoMode)
1669 return GL_FALSE;
1670 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1671 return GL_FALSE;
1672 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1673 return GL_FALSE;
1674 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1675 return GL_FALSE;
1676 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1677 return GL_FALSE;
1678 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1679 return GL_FALSE;
1680 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1681 return GL_FALSE;
1682 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1683 return GL_FALSE;
1684 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1685 return GL_FALSE;
1686
1687 return GL_TRUE;
1688}
1689
1690
Keith Whitwell6dc85572003-07-17 13:43:59 +00001691/**
Brian Paul9a33a112002-06-13 04:28:29 +00001692 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001693 *
1694 * \param newCtx new GL context.
1695 * \param buffer framebuffer.
1696 *
1697 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001698 */
1699void
1700_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1701{
1702 _mesa_make_current2( newCtx, buffer, buffer );
1703}
1704
Keith Whitwell6dc85572003-07-17 13:43:59 +00001705/**
1706 * Bind the given context to the given draw-buffer and read-buffer and
1707 * make it the current context for this thread.
1708 *
1709 * \param newCtx new GL context. If NULL then there will be no current GL
1710 * context.
1711 * \param drawBuffer draw framebuffer.
1712 * \param readBuffer read framebuffer.
1713 *
1714 * Check that the context's and framebuffer's visuals are compatible, returning
1715 * immediately otherwise. Sets the glapi current context via
1716 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1717 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1718 * Calls dd_function_table::MakeCurrent callback if defined.
1719 *
1720 * When a context is bound by the first time and the \c MESA_INFO environment
1721 * variable is set it calls print_info() as an aid for remote user
1722 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001723 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001724void
1725_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1726 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001727{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001728 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001729 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001730
Brian Paulbe3602d2001-02-28 00:27:48 +00001731 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001732 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001733 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1734 if (!check_compatible(newCtx, drawBuffer))
1735 return;
1736 }
1737 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1738 if (!check_compatible(newCtx, readBuffer))
1739 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001740 }
1741
Brian Paul00037781999-12-17 14:52:35 +00001742 /* We call this function periodically (just here for now) in
1743 * order to detect when multithreading has begun.
1744 */
1745 _glapi_check_multithread();
1746
Brian Paulf9b97d92000-01-28 20:17:42 +00001747 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001748 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001749
1750
1751 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001752 _glapi_set_dispatch(NULL); /* none current */
1753 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001754 else {
1755 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001756
Keith Whitwell23caf202000-11-16 21:05:34 +00001757 if (drawBuffer && readBuffer) {
1758 /* TODO: check if newCtx and buffer's visual match??? */
1759 newCtx->DrawBuffer = drawBuffer;
1760 newCtx->ReadBuffer = readBuffer;
1761 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001762
Keith Whitwell6dc85572003-07-17 13:43:59 +00001763#if _HAVE_FULL_GL
Brian Paul10d7f542002-06-17 23:38:14 +00001764 if (drawBuffer->Width == 0 && drawBuffer->Height == 0) {
1765 /* get initial window size */
1766 GLuint bufWidth, bufHeight;
1767
1768 /* ask device driver for size of output buffer */
1769 (*newCtx->Driver.GetBufferSize)( drawBuffer, &bufWidth, &bufHeight );
1770
Keith Whitwell6dc85572003-07-17 13:43:59 +00001771 if (drawBuffer->Width != bufWidth ||
1772 drawBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001773
Keith Whitwell6dc85572003-07-17 13:43:59 +00001774 drawBuffer->Width = bufWidth;
1775 drawBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001776
Keith Whitwell6dc85572003-07-17 13:43:59 +00001777 newCtx->Driver.ResizeBuffers( drawBuffer );
1778 }
Brian Paul10d7f542002-06-17 23:38:14 +00001779 }
1780
1781 if (readBuffer != drawBuffer &&
1782 readBuffer->Width == 0 && readBuffer->Height == 0) {
1783 /* get initial window size */
1784 GLuint bufWidth, bufHeight;
1785
1786 /* ask device driver for size of output buffer */
1787 (*newCtx->Driver.GetBufferSize)( readBuffer, &bufWidth, &bufHeight );
1788
Keith Whitwell6dc85572003-07-17 13:43:59 +00001789 if (readBuffer->Width != bufWidth ||
1790 readBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001791
Keith Whitwell6dc85572003-07-17 13:43:59 +00001792 readBuffer->Width = bufWidth;
1793 readBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001794
Keith Whitwell6dc85572003-07-17 13:43:59 +00001795 newCtx->Driver.ResizeBuffers( readBuffer );
1796 }
Brian Paul10d7f542002-06-17 23:38:14 +00001797 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001798#endif
Brian Paul00037781999-12-17 14:52:35 +00001799 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001800
Keith Whitwell6dc85572003-07-17 13:43:59 +00001801 /* Alert the driver - usually passed on to the sw t&l module,
1802 * but also used to detect threaded cases in the radeon codegen
1803 * hw t&l module.
1804 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001805 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001806 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1807
1808 /* We can use this to help debug user's problems. Tell them to set
1809 * the MESA_INFO env variable before running their app. Then the
1810 * first time each context is made current we'll print some useful
1811 * information.
1812 */
1813 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001814 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001815 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001816 }
1817 newCtx->FirstTimeCurrent = GL_FALSE;
1818 }
Brian Paul00037781999-12-17 14:52:35 +00001819 }
1820}
1821
Keith Whitwell6dc85572003-07-17 13:43:59 +00001822/**
1823 * Get current context for the calling thread.
1824 *
1825 * \return pointer to the current GL context.
1826 *
1827 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1828 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00001829 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001830GLcontext *
1831_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00001832{
Brian Paulf9b97d92000-01-28 20:17:42 +00001833 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001834}
1835
Keith Whitwell6dc85572003-07-17 13:43:59 +00001836/**
1837 * Get context's current API dispatch table.
1838 *
1839 * It'll either be the immediate-mode execute dispatcher or the display list
1840 * compile dispatcher.
1841 *
1842 * \param ctx GL context.
1843 *
1844 * \return pointer to dispatch_table.
1845 *
1846 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00001847 */
1848struct _glapi_table *
1849_mesa_get_dispatch(GLcontext *ctx)
1850{
1851 return ctx->CurrentDispatch;
1852}
1853
Keith Whitwell6dc85572003-07-17 13:43:59 +00001854/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00001855
1856
jtgafb833d1999-08-19 00:55:39 +00001857/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001858/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00001859/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001860/*@{*/
jtgafb833d1999-08-19 00:55:39 +00001861
Keith Whitwell6dc85572003-07-17 13:43:59 +00001862/**
1863 * Record an error.
1864 *
1865 * \param ctx GL context.
1866 * \param error error code.
1867 *
1868 * Records the given error code and call the driver's dd_function_table::Error
1869 * function if defined.
1870 *
1871 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00001872 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00001873 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001874void
Brian Paul4e9676f2002-06-29 19:48:15 +00001875_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00001876{
Brian Paul18a285a2002-03-16 00:53:15 +00001877 if (!ctx)
1878 return;
1879
Brian Paul7eb06032000-07-14 04:13:40 +00001880 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00001881 ctx->ErrorValue = error;
1882 }
1883
1884 /* Call device driver's error handler, if any. This is used on the Mac. */
1885 if (ctx->Driver.Error) {
1886 (*ctx->Driver.Error)( ctx );
1887 }
1888}
1889
Keith Whitwell6dc85572003-07-17 13:43:59 +00001890/**
1891 * Execute glFinish().
1892 *
1893 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1894 * dd_function_table::Finish driver callback, if not NULL.
1895 */
Brian Paulfa9df402000-02-02 19:16:46 +00001896void
1897_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00001898{
Brian Paulfa9df402000-02-02 19:16:46 +00001899 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001900 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001901 if (ctx->Driver.Finish) {
1902 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001903 }
1904}
1905
Keith Whitwell6dc85572003-07-17 13:43:59 +00001906/**
1907 * Execute glFlush().
1908 *
1909 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1910 * dd_function_table::Flush driver callback, if not NULL.
1911 */
Brian Paulfa9df402000-02-02 19:16:46 +00001912void
1913_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00001914{
Brian Paulfa9df402000-02-02 19:16:46 +00001915 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001916 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001917 if (ctx->Driver.Flush) {
1918 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001919 }
jtgafb833d1999-08-19 00:55:39 +00001920}
Brian Paul48c6a6e2000-09-08 21:28:04 +00001921
1922
Keith Whitwell6dc85572003-07-17 13:43:59 +00001923/*@}*/