blob: 9151c74899fb5c6964d3a53c19478a6bd38f522f [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"
77#include "attrib.h"
78#include "blend.h"
Brian Paulb1394fa2000-09-26 20:53:53 +000079#include "buffers.h"
Brian Paul4bdcfe52000-04-17 17:57:04 +000080#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000081#include "context.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000082#include "debug.h"
83#include "depth.h"
jtgafb833d1999-08-19 00:55:39 +000084#include "dlist.h"
85#include "eval.h"
86#include "enums.h"
Brian Paul585a68c1999-09-11 11:31:34 +000087#include "extensions.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000088#include "feedback.h"
jtgafb833d1999-08-19 00:55:39 +000089#include "fog.h"
Brian Paulb7a43041999-11-30 20:34:51 +000090#include "get.h"
Brian Paul9560f052000-01-31 23:11:39 +000091#include "glthread.h"
Brian Paulf44898c2003-07-18 15:44:57 +000092#include "glapioffsets.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000093#include "histogram.h"
94#include "hint.h"
jtgafb833d1999-08-19 00:55:39 +000095#include "hash.h"
96#include "light.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000097#include "lines.h"
jtgafb833d1999-08-19 00:55:39 +000098#include "macros.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000099#include "matrix.h"
Brian Paul05944c02003-07-22 03:51:46 +0000100#include "occlude.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000101#include "pixel.h"
102#include "points.h"
103#include "polygon.h"
104#include "rastpos.h"
jtgafb833d1999-08-19 00:55:39 +0000105#include "simple_list.h"
Brian Paulfa9df402000-02-02 19:16:46 +0000106#include "state.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000107#include "stencil.h"
jtgafb833d1999-08-19 00:55:39 +0000108#include "teximage.h"
109#include "texobj.h"
Brian Paul85d81602002-06-17 23:36:31 +0000110#include "texstate.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000111#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +0000112#include "varray.h"
Brian Paul04a81da2003-08-31 18:52:47 +0000113#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
114#include "program.h"
Brian Paul3a212032002-11-19 15:25:00 +0000115#endif
Gareth Hughesd4eb6652001-03-12 01:32:20 +0000116#include "vtxfmt.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000117#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000118#include "math/m_translate.h"
Keith Whitwell23caf202000-11-16 21:05:34 +0000119#include "math/m_matrix.h"
120#include "math/m_xform.h"
Keith Whitwellf4b02d12001-01-05 05:31:42 +0000121#include "math/mathmod.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000122#endif
jtgafb833d1999-08-19 00:55:39 +0000123
Brian Paul3b18a362000-09-26 15:27:20 +0000124#if defined(MESA_TRACE)
Brian Paul45f36342000-09-05 20:28:06 +0000125#include "Trace/tr_context.h"
126#include "Trace/tr_wrapper.h"
127#endif
128
davem69775355a2001-06-05 23:54:00 +0000129#ifdef USE_SPARC_ASM
130#include "SPARC/sparc.h"
131#endif
jtgafb833d1999-08-19 00:55:39 +0000132
Keith Whitwell23caf202000-11-16 21:05:34 +0000133#ifndef MESA_VERBOSE
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000134int MESA_VERBOSE = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000135#endif
136
137#ifndef MESA_DEBUG_FLAGS
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000138int MESA_DEBUG_FLAGS = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000139#endif
Brian Paulb1394fa2000-09-26 20:53:53 +0000140
Brian Paul86b84272001-12-14 02:50:01 +0000141
Brian Paul27558a12003-03-01 01:50:20 +0000142/* ubyte -> float conversion */
143GLfloat _mesa_ubyte_to_float_color_tab[256];
144
Brian Paul9a33a112002-06-13 04:28:29 +0000145static void
146free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
147
Brian Paul86b84272001-12-14 02:50:01 +0000148
Brian Paulb1394fa2000-09-26 20:53:53 +0000149/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000150/** \name OpenGL SI-style interface (new in Mesa 3.5)
151 *
152 * \if subset
153 * \note Most of these functions are never called in the Mesa subset.
154 * \endif
155 */
156/*@{*/
Brian Paulb1394fa2000-09-26 20:53:53 +0000157
Keith Whitwell6dc85572003-07-17 13:43:59 +0000158/**
159 * Destroy context callback.
160 *
161 * \param gc context.
162 * \return GL_TRUE on success, or GL_FALSE on failure.
163 *
164 * \ifnot subset
165 * Called by window system/device driver (via __GLexports::destroyCurrent) when
166 * the rendering context is to be destroyed.
167 * \endif
168 *
169 * Frees the context data and the context structure.
Brian Paul9a33a112002-06-13 04:28:29 +0000170 */
171GLboolean
172_mesa_destroyContext(__GLcontext *gc)
Brian Paulb1394fa2000-09-26 20:53:53 +0000173{
174 if (gc) {
175 _mesa_free_context_data(gc);
Brian Paul3c634522002-10-24 23:57:19 +0000176 _mesa_free(gc);
Brian Paulb1394fa2000-09-26 20:53:53 +0000177 }
178 return GL_TRUE;
179}
180
Keith Whitwell6dc85572003-07-17 13:43:59 +0000181/**
182 * Unbind context callback.
183 *
184 * \param gc context.
185 * \return GL_TRUE on success, or GL_FALSE on failure.
186 *
187 * \ifnot subset
188 * Called by window system/device driver (via __GLexports::loseCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000189 * when the rendering context is made non-current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000190 * \endif
191 *
192 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000193 */
194GLboolean
195_mesa_loseCurrent(__GLcontext *gc)
196{
197 /* XXX unbind context from thread */
198 return GL_TRUE;
199}
200
Keith Whitwell6dc85572003-07-17 13:43:59 +0000201/**
202 * Bind context callback.
203 *
204 * \param gc context.
205 * \return GL_TRUE on success, or GL_FALSE on failure.
206 *
207 * \ifnot subset
208 * Called by window system/device driver (via __GLexports::makeCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000209 * when the rendering context is made current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000210 * \endif
211 *
212 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000213 */
214GLboolean
215_mesa_makeCurrent(__GLcontext *gc)
216{
217 /* XXX bind context to thread */
218 return GL_TRUE;
219}
220
Keith Whitwell6dc85572003-07-17 13:43:59 +0000221/**
222 * Share context callback.
223 *
224 * \param gc context.
225 * \param gcShare shared context.
226 * \return GL_TRUE on success, or GL_FALSE on failure.
227 *
228 * \ifnot subset
229 * Called by window system/device driver (via __GLexports::shareContext)
230 * \endif
231 *
232 * Update the shared context reference count, gl_shared_state::RefCount.
Brian Paul9a33a112002-06-13 04:28:29 +0000233 */
234GLboolean
235_mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
236{
237 if (gc && gcShare && gc->Shared && gcShare->Shared) {
238 gc->Shared->RefCount--;
239 if (gc->Shared->RefCount == 0) {
240 free_shared_state(gc, gc->Shared);
241 }
242 gc->Shared = gcShare->Shared;
243 gc->Shared->RefCount++;
244 return GL_TRUE;
245 }
246 else {
247 return GL_FALSE;
248 }
249}
250
Keith Whitwell6dc85572003-07-17 13:43:59 +0000251
252#if _HAVE_FULL_GL
253/**
254 * Copy context callback.
255 */
Brian Paul9a33a112002-06-13 04:28:29 +0000256GLboolean
257_mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
258{
259 if (dst && src) {
260 _mesa_copy_context( src, dst, mask );
261 return GL_TRUE;
262 }
263 else {
264 return GL_FALSE;
265 }
266}
Keith Whitwell6dc85572003-07-17 13:43:59 +0000267#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000268
Keith Whitwell6dc85572003-07-17 13:43:59 +0000269/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000270GLboolean
271_mesa_forceCurrent(__GLcontext *gc)
272{
273 return GL_TRUE;
274}
275
Keith Whitwell6dc85572003-07-17 13:43:59 +0000276/**
277 * Windows/buffer resizing notification callback.
278 *
279 * \param gc GL context.
280 * \return GL_TRUE on success, or GL_FALSE on failure.
281 */
Brian Paul9a33a112002-06-13 04:28:29 +0000282GLboolean
283_mesa_notifyResize(__GLcontext *gc)
284{
285 GLint x, y;
286 GLuint width, height;
287 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
288 if (!d || !d->getDrawableSize)
289 return GL_FALSE;
290 d->getDrawableSize( d, &x, &y, &width, &height );
291 /* update viewport, resize software buffers, etc. */
292 return GL_TRUE;
293}
294
Keith Whitwell6dc85572003-07-17 13:43:59 +0000295/**
296 * Window/buffer destruction notification callback.
297 *
298 * \param gc GL context.
299 *
300 * Called when the context's window/buffer is going to be destroyed.
301 *
302 * No-op
303 */
Brian Paul9a33a112002-06-13 04:28:29 +0000304void
305_mesa_notifyDestroy(__GLcontext *gc)
306{
Brian Paul60b6e4f2002-10-14 17:08:17 +0000307 /* Unbind from it. */
Brian Paul9a33a112002-06-13 04:28:29 +0000308}
309
Keith Whitwell6dc85572003-07-17 13:43:59 +0000310/**
311 * Swap buffers notification callback.
312 *
313 * \param gc GL context.
314 *
315 * Called by window system just before swapping buffers.
Brian Paul9a33a112002-06-13 04:28:29 +0000316 * We have to finish any pending rendering.
317 */
318void
319_mesa_notifySwapBuffers(__GLcontext *gc)
320{
321 FLUSH_VERTICES( gc, 0 );
322}
323
Keith Whitwell6dc85572003-07-17 13:43:59 +0000324/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000325struct __GLdispatchStateRec *
326_mesa_dispatchExec(__GLcontext *gc)
327{
328 return NULL;
329}
330
Keith Whitwell6dc85572003-07-17 13:43:59 +0000331/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000332void
333_mesa_beginDispatchOverride(__GLcontext *gc)
334{
335}
336
Keith Whitwell6dc85572003-07-17 13:43:59 +0000337/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000338void
339_mesa_endDispatchOverride(__GLcontext *gc)
340{
341}
342
Keith Whitwell6dc85572003-07-17 13:43:59 +0000343/**
344 * \ifnot subset
345 * Setup the exports.
346 *
347 * The window system will call these functions when it needs Mesa to do
348 * something.
349 *
350 * \note Device drivers should override these functions! For example,
Brian Paul9a33a112002-06-13 04:28:29 +0000351 * the Xlib driver should plug in the XMesa*-style functions into this
352 * structure. The XMesa-style functions should then call the _mesa_*
353 * version of these functions. This is an approximation to OO design
354 * (inheritance and virtual functions).
Keith Whitwell6dc85572003-07-17 13:43:59 +0000355 * \endif
356 *
357 * \if subset
358 * No-op.
359 *
360 * \endif
Brian Paul9a33a112002-06-13 04:28:29 +0000361 */
362static void
363_mesa_init_default_exports(__GLexports *exports)
364{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000365#if _HAVE_FULL_GL
Brian Paul9a33a112002-06-13 04:28:29 +0000366 exports->destroyContext = _mesa_destroyContext;
367 exports->loseCurrent = _mesa_loseCurrent;
368 exports->makeCurrent = _mesa_makeCurrent;
369 exports->shareContext = _mesa_shareContext;
370 exports->copyContext = _mesa_copyContext;
371 exports->forceCurrent = _mesa_forceCurrent;
372 exports->notifyResize = _mesa_notifyResize;
Brian Paul2f35d5e2002-06-13 04:31:09 +0000373 exports->notifyDestroy = _mesa_notifyDestroy;
Brian Paul9a33a112002-06-13 04:28:29 +0000374 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
375 exports->dispatchExec = _mesa_dispatchExec;
376 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
377 exports->endDispatchOverride = _mesa_endDispatchOverride;
Keith Whitwell6dc85572003-07-17 13:43:59 +0000378#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000379}
380
Keith Whitwell6dc85572003-07-17 13:43:59 +0000381/**
382 * Exported OpenGL SI interface.
383 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000384__GLcontext *
385__glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
386{
387 GLcontext *ctx;
388
Brian Paul4753d602002-06-15 02:38:15 +0000389 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
Brian Paulb1394fa2000-09-26 20:53:53 +0000390 if (ctx == NULL) {
391 return NULL;
392 }
Brian Paul60b6e4f2002-10-14 17:08:17 +0000393
Brian Paul3c634522002-10-24 23:57:19 +0000394 _mesa_initialize_context(ctx, modes, NULL, imports, GL_FALSE);
Brian Paulb1394fa2000-09-26 20:53:53 +0000395 ctx->imports = *imports;
Brian Paulb1394fa2000-09-26 20:53:53 +0000396
397 return ctx;
398}
399
Keith Whitwell6dc85572003-07-17 13:43:59 +0000400/**
401 * Exported OpenGL SI interface.
402 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000403void
404__glCoreNopDispatch(void)
405{
406#if 0
407 /* SI */
408 __gl_dispatch = __glNopDispatchState;
409#else
410 /* Mesa */
411 _glapi_set_dispatch(NULL);
412#endif
413}
414
Keith Whitwell6dc85572003-07-17 13:43:59 +0000415/*@}*/
416
Brian Paulb1394fa2000-09-26 20:53:53 +0000417
jtgafb833d1999-08-19 00:55:39 +0000418/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000419/** \name GL Visual allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000420/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000421/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000422
Keith Whitwell6dc85572003-07-17 13:43:59 +0000423/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000424 * Allocate a new GLvisual object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000425 *
426 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
427 * \param dbFlag double buffering
428 * \param stereoFlag stereo buffer
429 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
430 * is acceptable but the actual depth type will be GLushort or GLuint as
431 * needed.
432 * \param stencilBits requested minimum bits per stencil buffer value
433 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
434 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
435 * \param redBits number of bits per color component in frame buffer for RGB(A)
436 * mode. We always use 8 in core Mesa though.
437 * \param greenBits same as above.
438 * \param blueBits same as above.
439 * \param alphaBits same as above.
440 * \param numSamples not really used.
441 *
442 * \return pointer to new GLvisual or NULL if requested parameters can't be
443 * met.
444 *
445 * Allocates a GLvisual structure and initializes it via
446 * _mesa_initialize_visual().
Brian Paul4d053dd2000-01-14 04:45:47 +0000447 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000448GLvisual *
449_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000450 GLboolean dbFlag,
451 GLboolean stereoFlag,
452 GLint redBits,
453 GLint greenBits,
454 GLint blueBits,
455 GLint alphaBits,
456 GLint indexBits,
457 GLint depthBits,
458 GLint stencilBits,
459 GLint accumRedBits,
460 GLint accumGreenBits,
461 GLint accumBlueBits,
462 GLint accumAlphaBits,
463 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000464{
Brian Paul178a1c52000-04-22 01:05:00 +0000465 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
466 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000467 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000468 redBits, greenBits, blueBits, alphaBits,
469 indexBits, depthBits, stencilBits,
470 accumRedBits, accumGreenBits,
471 accumBlueBits, accumAlphaBits,
Brian Paulb1394fa2000-09-26 20:53:53 +0000472 numSamples)) {
Brian Paul178a1c52000-04-22 01:05:00 +0000473 FREE(vis);
474 return NULL;
475 }
476 }
477 return vis;
478}
479
Keith Whitwell6dc85572003-07-17 13:43:59 +0000480/**
Brian Paul178a1c52000-04-22 01:05:00 +0000481 * Initialize the fields of the given GLvisual.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000482 *
483 * \return GL_TRUE on success, or GL_FALSE on failure.
484 *
485 * \sa _mesa_create_visual() above for the parameter description.
486 *
487 * Makes some sanity checks and fills in the fields of the
488 * GLvisual structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000489 */
490GLboolean
491_mesa_initialize_visual( GLvisual *vis,
492 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000493 GLboolean dbFlag,
494 GLboolean stereoFlag,
495 GLint redBits,
496 GLint greenBits,
497 GLint blueBits,
498 GLint alphaBits,
499 GLint indexBits,
500 GLint depthBits,
501 GLint stencilBits,
502 GLint accumRedBits,
503 GLint accumGreenBits,
504 GLint accumBlueBits,
505 GLint accumAlphaBits,
506 GLint numSamples )
507{
Brian Paulb6bcae52001-01-23 23:39:36 +0000508 (void) numSamples;
509
Brian Paul178a1c52000-04-22 01:05:00 +0000510 assert(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000511
Brian Pauled30dfa2000-03-03 17:47:39 +0000512 /* This is to catch bad values from device drivers not updated for
513 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
514 * bad value now (a 1-bit depth buffer!?!).
515 */
516 assert(depthBits == 0 || depthBits > 1);
517
518 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000519 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000520 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000521 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000522 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000523 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000524 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000525 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000526 }
527 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000528 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000529 }
530 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000531 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000532 }
533 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000534 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000535 }
536
Brian Paulb6bcae52001-01-23 23:39:36 +0000537 vis->rgbMode = rgbFlag;
538 vis->doubleBufferMode = dbFlag;
539 vis->stereoMode = stereoFlag;
Brian Paul153f1542002-10-29 15:04:35 +0000540
Brian Paulb6bcae52001-01-23 23:39:36 +0000541 vis->redBits = redBits;
542 vis->greenBits = greenBits;
543 vis->blueBits = blueBits;
544 vis->alphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000545
Brian Paulb6bcae52001-01-23 23:39:36 +0000546 vis->indexBits = indexBits;
547 vis->depthBits = depthBits;
548 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
549 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
550 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
551 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
552 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Pauled30dfa2000-03-03 17:47:39 +0000553
Brian Paul153f1542002-10-29 15:04:35 +0000554 vis->haveAccumBuffer = accumRedBits > 0;
555 vis->haveDepthBuffer = depthBits > 0;
556 vis->haveStencilBuffer = stencilBits > 0;
557
558 vis->numAuxBuffers = 0;
559 vis->level = 0;
560 vis->pixmapMode = 0;
561
Brian Paul178a1c52000-04-22 01:05:00 +0000562 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000563}
564
Keith Whitwell6dc85572003-07-17 13:43:59 +0000565/**
566 * Destroy a visual.
567 *
568 * \param vis visual.
569 *
570 * Frees the visual structure.
571 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000572void
573_mesa_destroy_visual( GLvisual *vis )
574{
575 FREE(vis);
576}
577
Keith Whitwell6dc85572003-07-17 13:43:59 +0000578/*@}*/
579
Brian Paulb371e0d2000-03-31 01:05:51 +0000580
Brian Paul4d053dd2000-01-14 04:45:47 +0000581/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000582/** \name GL Framebuffer allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000583/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000584/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000585
Keith Whitwell6dc85572003-07-17 13:43:59 +0000586/**
587 * Create a new framebuffer.
588 *
589 * A GLframebuffer is a structure which encapsulates the depth, stencil and
590 * accum buffers and related parameters.
591 *
592 * \param visual a GLvisual pointer (we copy the struct contents)
593 * \param softwareDepth create/use a software depth buffer?
594 * \param softwareStencil create/use a software stencil buffer?
595 * \param softwareAccum create/use a software accum buffer?
596 * \param softwareAlpha create/use a software alpha buffer?
597 *
598 * \return pointer to new GLframebuffer struct or NULL if error.
599 *
600 * Allocate a GLframebuffer structure and initializes it via
601 * _mesa_initialize_framebuffer().
Brian Paul4d053dd2000-01-14 04:45:47 +0000602 */
Brian Paul178a1c52000-04-22 01:05:00 +0000603GLframebuffer *
Brian Paulbe3602d2001-02-28 00:27:48 +0000604_mesa_create_framebuffer( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +0000605 GLboolean softwareDepth,
606 GLboolean softwareStencil,
607 GLboolean softwareAccum,
608 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000609{
Brian Paul178a1c52000-04-22 01:05:00 +0000610 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
611 assert(visual);
612 if (buffer) {
613 _mesa_initialize_framebuffer(buffer, visual,
614 softwareDepth, softwareStencil,
615 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000616 }
Brian Paul178a1c52000-04-22 01:05:00 +0000617 return buffer;
618}
619
Keith Whitwell6dc85572003-07-17 13:43:59 +0000620/**
Brian Paul178a1c52000-04-22 01:05:00 +0000621 * Initialize a GLframebuffer object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000622 *
623 * \sa _mesa_create_framebuffer() above for the parameter description.
624 *
625 * Makes some sanity checks and fills in the fields of the
626 * GLframebuffer structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000627 */
628void
629_mesa_initialize_framebuffer( GLframebuffer *buffer,
Brian Paulbe3602d2001-02-28 00:27:48 +0000630 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +0000631 GLboolean softwareDepth,
632 GLboolean softwareStencil,
633 GLboolean softwareAccum,
634 GLboolean softwareAlpha )
635{
636 assert(buffer);
637 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000638
Brian Paul6ec6b842002-10-30 19:49:29 +0000639 _mesa_bzero(buffer, sizeof(GLframebuffer));
Brian Paul85d81602002-06-17 23:36:31 +0000640
Brian Paul4d053dd2000-01-14 04:45:47 +0000641 /* sanity checks */
642 if (softwareDepth ) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000643 assert(visual->depthBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000644 }
645 if (softwareStencil) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000646 assert(visual->stencilBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000647 }
648 if (softwareAccum) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000649 assert(visual->rgbMode);
650 assert(visual->accumRedBits > 0);
651 assert(visual->accumGreenBits > 0);
652 assert(visual->accumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000653 }
654 if (softwareAlpha) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000655 assert(visual->rgbMode);
656 assert(visual->alphaBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000657 }
658
Brian Paul75978bd2001-04-27 21:17:20 +0000659 buffer->Visual = *visual;
Brian Paul4d053dd2000-01-14 04:45:47 +0000660 buffer->UseSoftwareDepthBuffer = softwareDepth;
661 buffer->UseSoftwareStencilBuffer = softwareStencil;
662 buffer->UseSoftwareAccumBuffer = softwareAccum;
663 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul4d053dd2000-01-14 04:45:47 +0000664}
665
Keith Whitwell6dc85572003-07-17 13:43:59 +0000666/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000667 * Free a framebuffer struct and its buffers.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000668 *
669 * Calls _mesa_free_framebuffer_data() and frees the structure.
Brian Paul4d053dd2000-01-14 04:45:47 +0000670 */
Brian Paul178a1c52000-04-22 01:05:00 +0000671void
Brian Paulb1394fa2000-09-26 20:53:53 +0000672_mesa_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000673{
674 if (buffer) {
Brian Paul75978bd2001-04-27 21:17:20 +0000675 _mesa_free_framebuffer_data(buffer);
Brian Paul4d053dd2000-01-14 04:45:47 +0000676 FREE(buffer);
677 }
678}
679
Keith Whitwell6dc85572003-07-17 13:43:59 +0000680/**
681 * Free the data hanging off of \p buffer, but not \p buffer itself.
682 *
683 * \param buffer framebuffer.
684 *
685 * Frees all the buffers associated with the structure.
Brian Paul75978bd2001-04-27 21:17:20 +0000686 */
687void
688_mesa_free_framebuffer_data( GLframebuffer *buffer )
689{
690 if (!buffer)
691 return;
692
Brian Paul87506682003-05-27 15:20:43 +0000693 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
Brian Paulaeb44342002-03-19 16:47:04 +0000694 MESA_PBUFFER_FREE( buffer->DepthBuffer );
Brian Paul75978bd2001-04-27 21:17:20 +0000695 buffer->DepthBuffer = NULL;
696 }
Brian Paul87506682003-05-27 15:20:43 +0000697 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
Brian Paulaeb44342002-03-19 16:47:04 +0000698 MESA_PBUFFER_FREE( buffer->Accum );
Brian Paul75978bd2001-04-27 21:17:20 +0000699 buffer->Accum = NULL;
700 }
Brian Paul87506682003-05-27 15:20:43 +0000701 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
Brian Paulaeb44342002-03-19 16:47:04 +0000702 MESA_PBUFFER_FREE( buffer->Stencil );
Brian Paul75978bd2001-04-27 21:17:20 +0000703 buffer->Stencil = NULL;
704 }
Brian Paul87506682003-05-27 15:20:43 +0000705 if (buffer->UseSoftwareAlphaBuffers){
706 if (buffer->FrontLeftAlpha) {
707 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
708 buffer->FrontLeftAlpha = NULL;
709 }
710 if (buffer->BackLeftAlpha) {
711 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
712 buffer->BackLeftAlpha = NULL;
713 }
714 if (buffer->FrontRightAlpha) {
715 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
716 buffer->FrontRightAlpha = NULL;
717 }
718 if (buffer->BackRightAlpha) {
719 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
720 buffer->BackRightAlpha = NULL;
721 }
Brian Paul75978bd2001-04-27 21:17:20 +0000722 }
723}
724
Keith Whitwell6dc85572003-07-17 13:43:59 +0000725/*@}*/
Brian Paul75978bd2001-04-27 21:17:20 +0000726
Brian Paul4d053dd2000-01-14 04:45:47 +0000727
728/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000729/** \name Context allocation, initialization, destroying
730 *
731 * The purpose of the most initialization functions here is to provide the
732 * default state values according to the OpenGL specification.
733 */
jtgafb833d1999-08-19 00:55:39 +0000734/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000735/*@{*/
jtgafb833d1999-08-19 00:55:39 +0000736
Keith Whitwell6dc85572003-07-17 13:43:59 +0000737/**
738 * One-time initialization mutex lock.
739 *
740 * \sa Used by one_time_init().
741 */
Brian Paul9560f052000-01-31 23:11:39 +0000742_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
743
Keith Whitwell6dc85572003-07-17 13:43:59 +0000744/**
745 * Calls all the various one-time-init functions in Mesa.
746 *
747 * While holding a global mutex lock, calls several initialization functions,
748 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
749 * defined.
750 *
751 * \sa _mesa_init_lists(), _math_init().
jtgafb833d1999-08-19 00:55:39 +0000752 */
Brian Paul178a1c52000-04-22 01:05:00 +0000753static void
Brian Paul4753d602002-06-15 02:38:15 +0000754one_time_init( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000755{
756 static GLboolean alreadyCalled = GL_FALSE;
Brian Paul9560f052000-01-31 23:11:39 +0000757 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000758 if (!alreadyCalled) {
Brian Paul27558a12003-03-01 01:50:20 +0000759 GLuint i;
760
Brian Paul4d053dd2000-01-14 04:45:47 +0000761 /* do some implementation tests */
762 assert( sizeof(GLbyte) == 1 );
763 assert( sizeof(GLshort) >= 2 );
764 assert( sizeof(GLint) >= 4 );
765 assert( sizeof(GLubyte) == 1 );
766 assert( sizeof(GLushort) >= 2 );
767 assert( sizeof(GLuint) >= 4 );
768
Brian Paul08836342001-03-03 20:33:27 +0000769 _mesa_init_lists();
Keith Whitwell23caf202000-11-16 21:05:34 +0000770
Keith Whitwell6dc85572003-07-17 13:43:59 +0000771#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000772 _math_init();
Brian Paul27558a12003-03-01 01:50:20 +0000773
774 for (i = 0; i < 256; i++) {
775 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
776 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000777#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000778
davem69775355a2001-06-05 23:54:00 +0000779#ifdef USE_SPARC_ASM
780 _mesa_init_sparc_glapi_relocs();
781#endif
Brian Paul3c634522002-10-24 23:57:19 +0000782 if (_mesa_getenv("MESA_DEBUG")) {
Brian Paul68ee4bc2000-01-28 19:02:22 +0000783 _glapi_noop_enable_warnings(GL_TRUE);
Brian Paul71072be2002-10-10 00:22:13 +0000784#ifndef GLX_DIRECT_RENDERING
785 /* libGL from before 2002/06/28 don't have this function. Someday,
786 * when newer libGL libs are common, remove the #ifdef test. This
787 * only serves to print warnings when calling undefined GL functions.
788 */
Brian Paul4e9676f2002-06-29 19:48:15 +0000789 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
Brian Paul71072be2002-10-10 00:22:13 +0000790#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000791 }
792 else {
793 _glapi_noop_enable_warnings(GL_FALSE);
794 }
795
jtgafb833d1999-08-19 00:55:39 +0000796#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
Brian Paul4e9676f2002-06-29 19:48:15 +0000797 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
jtgafb833d1999-08-19 00:55:39 +0000798#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000799
800 alreadyCalled = GL_TRUE;
801 }
Brian Paul9560f052000-01-31 23:11:39 +0000802 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000803}
804
Keith Whitwell6dc85572003-07-17 13:43:59 +0000805/**
jtgafb833d1999-08-19 00:55:39 +0000806 * Allocate and initialize a shared context state structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000807 *
808 * \return pointer to a gl_shared_state structure on success, or NULL on
809 * failure.
810 *
811 * Initializes the display list, texture objects and vertex programs hash
812 * tables, allocates the texture objects. If it runs out of memory, frees
813 * everything already allocated before returning NULL.
jtgafb833d1999-08-19 00:55:39 +0000814 */
Brian Paula3f13702003-04-01 16:41:50 +0000815static GLboolean
816alloc_shared_state( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000817{
Brian Paula3f13702003-04-01 16:41:50 +0000818 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000819 if (!ss)
Brian Paula3f13702003-04-01 16:41:50 +0000820 return GL_FALSE;
821
822 ctx->Shared = ss;
jtgafb833d1999-08-19 00:55:39 +0000823
Brian Paule4b684c2000-09-12 21:07:40 +0000824 _glthread_INIT_MUTEX(ss->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000825
Brian Paule4b684c2000-09-12 21:07:40 +0000826 ss->DisplayList = _mesa_NewHashTable();
Brian Paulbb797902000-01-24 16:19:54 +0000827 ss->TexObjects = _mesa_NewHashTable();
Brian Paul451f3102003-04-17 01:48:19 +0000828#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
Brian Paul610d5992003-01-14 04:55:45 +0000829 ss->Programs = _mesa_NewHashTable();
Brian Paul8dfc5b92002-10-16 17:57:51 +0000830#endif
jtgafb833d1999-08-19 00:55:39 +0000831
Brian Paul451f3102003-04-17 01:48:19 +0000832#if FEATURE_ARB_vertex_program
833 ss->DefaultVertexProgram = _mesa_alloc_program(ctx, GL_VERTEX_PROGRAM_ARB, 0);
834 if (!ss->DefaultVertexProgram)
835 goto cleanup;
836#endif
837#if FEATURE_ARB_fragment_program
838 ss->DefaultFragmentProgram = _mesa_alloc_program(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
839 if (!ss->DefaultFragmentProgram)
840 goto cleanup;
841#endif
842
Brian Paula3f13702003-04-01 16:41:50 +0000843 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
844 if (!ss->Default1D)
845 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000846
Brian Paula3f13702003-04-01 16:41:50 +0000847 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
848 if (!ss->Default2D)
849 goto cleanup;
jtgafb833d1999-08-19 00:55:39 +0000850
Brian Paula3f13702003-04-01 16:41:50 +0000851 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
852 if (!ss->Default3D)
853 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000854
Brian Paula3f13702003-04-01 16:41:50 +0000855 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
856 if (!ss->DefaultCubeMap)
857 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000858
Brian Paula3f13702003-04-01 16:41:50 +0000859 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
860 if (!ss->DefaultRect)
861 goto cleanup;
Brian Paul413d6a22000-05-26 14:44:59 +0000862
Brian Paula3f13702003-04-01 16:41:50 +0000863#if 0
864 _mesa_save_texture_object(ctx, ss->Default1D);
865 _mesa_save_texture_object(ctx, ss->Default2D);
866 _mesa_save_texture_object(ctx, ss->Default3D);
867 _mesa_save_texture_object(ctx, ss->DefaultCubeMap);
868 _mesa_save_texture_object(ctx, ss->DefaultRect);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000869#endif
Brian Paula3f13702003-04-01 16:41:50 +0000870
871 /* Effectively bind the default textures to all texture units */
872 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
873 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
874 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
875 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
876 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
877
878 return GL_TRUE;
879
880 cleanup:
881 /* Ran out of memory at some point. Free everything and return NULL */
882 if (ss->DisplayList)
883 _mesa_DeleteHashTable(ss->DisplayList);
884 if (ss->TexObjects)
885 _mesa_DeleteHashTable(ss->TexObjects);
886#if FEATURE_NV_vertex_program
887 if (ss->Programs)
888 _mesa_DeleteHashTable(ss->Programs);
889#endif
Brian Paul451f3102003-04-17 01:48:19 +0000890#if FEATURE_ARB_vertex_program
891 if (ss->DefaultVertexProgram)
892 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
893#endif
894#if FEATURE_ARB_fragment_program
895 if (ss->DefaultFragmentProgram)
896 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
897#endif
Brian Paula3f13702003-04-01 16:41:50 +0000898 if (ss->Default1D)
899 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
900 if (ss->Default2D)
901 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
902 if (ss->Default3D)
903 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
904 if (ss->DefaultCubeMap)
905 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
906 if (ss->DefaultRect)
907 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
908 if (ss)
909 _mesa_free(ss);
910 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000911}
912
Keith Whitwell6dc85572003-07-17 13:43:59 +0000913/**
jtgafb833d1999-08-19 00:55:39 +0000914 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000915 *
916 * \param ctx GL context.
917 * \param ss shared state pointer.
918 *
919 * Frees the display lists, the texture objects (calling the driver texture
920 * deletion callback to free its private data) and the vertex programs, as well
921 * as their hash tables.
922 *
923 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000924 */
Brian Paul178a1c52000-04-22 01:05:00 +0000925static void
926free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000927{
928 /* Free display lists */
929 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000930 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000931 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000932 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000933 }
934 else {
935 break;
936 }
937 }
Brian Paulbb797902000-01-24 16:19:54 +0000938 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000939
940 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000941 ASSERT(ctx->Driver.DeleteTexture);
942 while (1) {
943 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
944 if (texName) {
945 struct gl_texture_object *texObj = (struct gl_texture_object *)
946 _mesa_HashLookup(ss->TexObjects, texName);
947 ASSERT(texObj);
948 (*ctx->Driver.DeleteTexture)(ctx, texObj);
949 _mesa_HashRemove(ss->TexObjects, texName);
950 }
951 else {
952 break;
953 }
jtgafb833d1999-08-19 00:55:39 +0000954 }
Brian Paulbb797902000-01-24 16:19:54 +0000955 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000956
Brian Paul8dfc5b92002-10-16 17:57:51 +0000957#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +0000958 /* Free vertex programs */
959 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +0000960 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +0000961 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +0000962 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
963 prog);
964 ASSERT(p);
965 _mesa_delete_program(ctx, p);
966 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +0000967 }
968 else {
969 break;
970 }
971 }
Brian Paul610d5992003-01-14 04:55:45 +0000972 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000973#endif
Brian Paul30f51ae2001-12-18 04:06:44 +0000974
Keith Whitwelle15fd852002-12-12 13:03:15 +0000975 _glthread_DESTROY_MUTEX(ss->Mutex);
976
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000977 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000978}
979
980
Keith Whitwell6dc85572003-07-17 13:43:59 +0000981static void _mesa_init_current( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000982{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000983 int i;
jtgafb833d1999-08-19 00:55:39 +0000984
Keith Whitwell6dc85572003-07-17 13:43:59 +0000985 /* Current group */
986 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
987 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +0000988 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000989 /* special cases: */
990 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
991 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
992 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
993 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 0.0 );
994 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
995 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
996 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i], 0.0, 0.0, 0.0, 1.0);
997 ctx->Current.Index = 1;
998 ctx->Current.EdgeFlag = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000999}
1000
1001
Keith Whitwell6dc85572003-07-17 13:43:59 +00001002static void
1003_mesa_init_constants( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001004{
Brian Paul4d053dd2000-01-14 04:45:47 +00001005 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +00001006
Brian Paulcd1cefa2001-06-13 14:56:14 +00001007 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1008 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1009
Brian Paul539cce52000-02-03 19:40:07 +00001010 /* Constants, may be overriden by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +00001011 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
Brian Paulcd1cefa2001-06-13 14:56:14 +00001012 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1013 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
Brian Paul8afe7de2002-06-15 03:03:06 +00001014 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001015 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
Brian Paul610d5992003-01-14 04:55:45 +00001016 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1017 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
Gareth Hughes2c3d34c2001-03-18 08:53:49 +00001018 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
Brian Paul87c964d2001-11-06 15:53:00 +00001019 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
Brian Paul4d053dd2000-01-14 04:45:47 +00001020 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +00001021 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1022 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1023 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1024 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1025 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001026 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001027 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1028 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1029 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1030 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001031 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001032 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
Brian Paul4bdcfe52000-04-17 17:57:04 +00001033 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +00001034 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1035 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
Brian Paula8644322000-11-27 18:22:13 +00001036 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1037 ctx->Const.MaxLights = MAX_LIGHTS;
Ian Romanick882caa12003-05-30 21:37:14 +00001038 ctx->Const.MaxSpotExponent = 128.0;
1039 ctx->Const.MaxShininess = 128.0;
Brian Pauld0492cf2003-04-11 01:20:06 +00001040#if FEATURE_ARB_vertex_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001041 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001042 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1043 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1044 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1045 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1046 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001047#endif
1048#if FEATURE_ARB_fragment_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001049 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001050 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1051 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1052 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1053 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1054 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1055 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1056 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1057 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001058#endif
Brian Pauledd67742003-04-18 18:02:43 +00001059 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1060 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
Brian Pauld0492cf2003-04-11 01:20:06 +00001061
Brian Paul92f97852003-05-01 22:44:02 +00001062 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
Keith Whitwell6dc85572003-07-17 13:43:59 +00001063}
jtgafb833d1999-08-19 00:55:39 +00001064
Keith Whitwell6dc85572003-07-17 13:43:59 +00001065/**
1066 * Initialize the attribute groups in a GL context.
1067 *
1068 * \param ctx GL context.
1069 *
1070 * Initializes all the attributes, calling the respective <tt>init*</tt>
1071 * functions for the more complex data structures.
1072 */
1073static GLboolean
1074init_attrib_groups( GLcontext *ctx )
1075{
1076 assert(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001077
Keith Whitwell6dc85572003-07-17 13:43:59 +00001078 /* Constants */
1079 _mesa_init_constants( ctx );
Brian Paul0771d152000-04-07 00:19:41 +00001080
Brian Paul4d053dd2000-01-14 04:45:47 +00001081 /* Extensions */
Brian Paulde4f4602003-07-03 02:15:06 +00001082 _mesa_init_extensions( ctx );
jtgafb833d1999-08-19 00:55:39 +00001083
Keith Whitwell6dc85572003-07-17 13:43:59 +00001084 /* Attribute Groups */
1085 _mesa_init_accum( ctx );
1086 _mesa_init_attrib( ctx );
1087 _mesa_init_buffers( ctx );
1088 _mesa_init_color( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001089 _mesa_init_colortables( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001090 _mesa_init_current( ctx );
1091 _mesa_init_depth( ctx );
1092 _mesa_init_debug( ctx );
1093 _mesa_init_display_list( ctx );
1094 _mesa_init_eval( ctx );
1095 _mesa_init_feedback( ctx );
1096 _mesa_init_fog( ctx );
1097 _mesa_init_histogram( ctx );
1098 _mesa_init_hint( ctx );
1099 _mesa_init_line( ctx );
1100 _mesa_init_lighting( ctx );
1101 _mesa_init_matrix( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001102 _mesa_init_occlude( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001103 _mesa_init_pixel( ctx );
1104 _mesa_init_point( ctx );
1105 _mesa_init_polygon( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001106 _mesa_init_program( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001107 _mesa_init_rastpos( ctx );
1108 _mesa_init_stencil( ctx );
1109 _mesa_init_transform( ctx );
1110 _mesa_init_varray( ctx );
1111 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001112
Keith Whitwell6dc85572003-07-17 13:43:59 +00001113 if (!_mesa_init_texture( ctx ))
1114 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001115
Brian Paul4d053dd2000-01-14 04:45:47 +00001116 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001117 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001118 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paul4d053dd2000-01-14 04:45:47 +00001119 ctx->CatchSignals = GL_TRUE;
Brian Paulf782b812002-10-04 17:37:45 +00001120 ctx->_Facing = 0;
Brian Paul4d053dd2000-01-14 04:45:47 +00001121
Brian Paula3f13702003-04-01 16:41:50 +00001122 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001123}
1124
1125
Brian Paulf44898c2003-07-18 15:44:57 +00001126/**
1127 * If the DRI libGL.so library is old, it may not have the entrypoints for
1128 * some recent OpenGL extensions. Dynamically add them now.
1129 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1130 * and driver code, this won't be an issue (and calling this function won't
1131 * do any harm).
1132 */
1133static void
1134add_newer_entrypoints(void)
1135{
Ian Romanick3baefe62003-08-22 23:28:03 +00001136 unsigned i;
1137 static const struct {
1138 const char * const name;
1139 unsigned offset;
1140 }
1141 newer_entrypoints[] = {
1142 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1143 { "glWindowPos2dARB", 513 },
1144 { "glWindowPos2dvARB", 514 },
1145 { "glWindowPos2fARB", 515 },
1146 { "glWindowPos2fvARB", 516 },
1147 { "glWindowPos2iARB", 517 },
1148 { "glWindowPos2ivARB", 518 },
1149 { "glWindowPos2sARB", 519 },
1150 { "glWindowPos2svARB", 520 },
1151 { "glWindowPos3dARB", 521 },
1152 { "glWindowPos3dvARB", 522 },
1153 { "glWindowPos3fARB", 523 },
1154 { "glWindowPos3fvARB", 524 },
1155 { "glWindowPos3iARB", 525 },
1156 { "glWindowPos3ivARB", 526 },
1157 { "glWindowPos3sARB", 527 },
1158 { "glWindowPos3svARB", 528 },
Brian Paulf44898c2003-07-18 15:44:57 +00001159#if FEATURE_NV_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001160 { "glAreProgramsResidentNV", 578 },
1161 { "glBindProgramNV", 579 },
1162 { "glDeleteProgramsNV", 580 },
1163 { "glExecuteProgramNV", 581 },
1164 { "glGenProgramsNV", 582 },
1165 { "glGetProgramParameterdvNV", 583 },
1166 { "glGetProgramParameterfvNV", 584 },
1167 { "glGetProgramivNV", 585 },
1168 { "glGetProgramStringNV", 586 },
1169 { "glGetTrackMatrixivNV", 587 },
1170 { "glGetVertexAttribdvNV", 588 },
1171 { "glGetVertexAttribfvNV", 589 },
1172 { "glGetVertexAttribivNV", 590 },
1173 { "glGetVertexAttribPointervNV", 591 },
1174 { "glIsProgramNV", 592 },
1175 { "glLoadProgramNV", 593 },
1176 { "glProgramParameter4dNV", 594 },
1177 { "glProgramParameter4dvNV", 595 },
1178 { "glProgramParameter4fNV", 596 },
1179 { "glProgramParameter4fvNV", 597 },
1180 { "glProgramParameters4dvNV", 598 },
1181 { "glProgramParameters4fvNV", 599 },
1182 { "glRequestResidentProgramsNV", 600 },
1183 { "glTrackMatrixNV", 601 },
1184 { "glVertexAttribPointerNV", 602 },
1185 { "glVertexAttrib1dNV", 603 },
1186 { "glVertexAttrib1dvNV", 604 },
1187 { "glVertexAttrib1fNV", 605 },
1188 { "glVertexAttrib1fvNV", 606 },
1189 { "glVertexAttrib1sNV", 607 },
1190 { "glVertexAttrib1svNV", 608 },
1191 { "glVertexAttrib2dNV", 609 },
1192 { "glVertexAttrib2dvNV", 610 },
1193 { "glVertexAttrib2fNV", 611 },
1194 { "glVertexAttrib2fvNV", 612 },
1195 { "glVertexAttrib2sNV", 613 },
1196 { "glVertexAttrib2svNV", 614 },
1197 { "glVertexAttrib3dNV", 615 },
1198 { "glVertexAttrib3dvNV", 616 },
1199 { "glVertexAttrib3fNV", 617 },
1200 { "glVertexAttrib3fvNV", 618 },
1201 { "glVertexAttrib3sNV", 619 },
1202 { "glVertexAttrib3svNV", 620 },
1203 { "glVertexAttrib4dNV", 621 },
1204 { "glVertexAttrib4dvNV", 622 },
1205 { "glVertexAttrib4fNV", 623 },
1206 { "glVertexAttrib4fvNV", 624 },
1207 { "glVertexAttrib4sNV", 625 },
1208 { "glVertexAttrib4svNV", 626 },
1209 { "glVertexAttrib4ubNV", 627 },
1210 { "glVertexAttrib4ubvNV", 628 },
1211 { "glVertexAttribs1dvNV", 629 },
1212 { "glVertexAttribs1fvNV", 630 },
1213 { "glVertexAttribs1svNV", 631 },
1214 { "glVertexAttribs2dvNV", 632 },
1215 { "glVertexAttribs2fvNV", 633 },
1216 { "glVertexAttribs2svNV", 634 },
1217 { "glVertexAttribs3dvNV", 635 },
1218 { "glVertexAttribs3fvNV", 636 },
1219 { "glVertexAttribs3svNV", 637 },
1220 { "glVertexAttribs4dvNV", 638 },
1221 { "glVertexAttribs4fvNV", 639 },
1222 { "glVertexAttribs4svNV", 640 },
1223 { "glVertexAttribs4ubvNV", 641 },
Brian Paulf44898c2003-07-18 15:44:57 +00001224#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001225 { "glPointParameteriNV", 642 },
1226 { "glPointParameterivNV", 643 },
1227 { "glMultiDrawArraysEXT", 644 },
1228 { "glMultiDrawElementsEXT", 645 },
1229 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1230 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1231 { "glActiveStencilFaceEXT", 646 },
Brian Paulf44898c2003-07-18 15:44:57 +00001232#if FEATURE_NV_fence
Ian Romanick3baefe62003-08-22 23:28:03 +00001233 { "glDeleteFencesNV", 647 },
1234 { "glGenFencesNV", 648 },
1235 { "glIsFenceNV", 649 },
1236 { "glTestFenceNV", 650 },
1237 { "glGetFenceivNV", 651 },
1238 { "glFinishFenceNV", 652 },
1239 { "glSetFenceNV", 653 },
Brian Paulf44898c2003-07-18 15:44:57 +00001240#endif
1241#if FEATURE_NV_fragment_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001242 { "glProgramNamedParameter4fNV", 682 },
1243 { "glProgramNamedParameter4dNV", 683 },
1244 { "glProgramNamedParameter4fvNV", 683 },
1245 { "glProgramNamedParameter4dvNV", 684 },
1246 { "glGetProgramNamedParameterfvNV", 685 },
1247 { "glGetProgramNamedParameterdvNV", 686 },
Brian Paulf44898c2003-07-18 15:44:57 +00001248#endif
1249#if FEATURE_ARB_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001250 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1251 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1252 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1253 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1254 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1255 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1256 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1257 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1258 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1259 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1260 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1261 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1262 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1263 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1264 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1265 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1266 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1267 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1268 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1269 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1270 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1271 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1272 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1273 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1274 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1275 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1276 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1277 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1278 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1279 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1280 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1281 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1282 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1283 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1284 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1285 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1286 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1287 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1288 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1289 { "glProgramStringARB", _gloffset_ProgramStringARB },
1290 { "glBindProgramARB", _gloffset_BindProgramNV },
1291 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1292 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1293 { "glIsProgramARB", _gloffset_IsProgramNV },
1294 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1295 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1296 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1297 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1298 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1299 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1300 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1301 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1302 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1303 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1304 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1305 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1306 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1307 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1308 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1309 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1310 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1311 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
Brian Paulf44898c2003-07-18 15:44:57 +00001312#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001313 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1314 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
1315 };
1316
1317 for ( i = 0 ; i < Elements(newer_entrypoints) ; i++ ) {
1318 _glapi_add_entrypoint( newer_entrypoints[i].name,
1319 newer_entrypoints[i].offset );
1320 }
Brian Paulf44898c2003-07-18 15:44:57 +00001321}
1322
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001323
Brian Paulde4f4602003-07-03 02:15:06 +00001324/**
Keith Whitwell6dc85572003-07-17 13:43:59 +00001325 * Initialize a GLcontext struct.
1326 *
1327 * This includes allocating all the other structs and arrays which hang off of
1328 * the context by pointers.
1329 *
1330 * \sa _mesa_create_context() for the parameter description.
1331 *
1332 * Performs the imports and exports callback tables initialization, and
1333 * miscellaneous one-time initializations. If no shared context is supplied one
1334 * is allocated, and increase its reference count. Setups the GL API dispatch
1335 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1336 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1337 * for debug flags.
1338 *
Brian Paulde4f4602003-07-03 02:15:06 +00001339 * \note the direct parameter is ignored (obsolete).
jtgafb833d1999-08-19 00:55:39 +00001340 */
Brian Paul178a1c52000-04-22 01:05:00 +00001341GLboolean
1342_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001343 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001344 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001345 void *driver_ctx,
1346 GLboolean direct )
jtgafb833d1999-08-19 00:55:39 +00001347{
Brian Paul5fb84d22000-05-24 15:04:45 +00001348 GLuint dispatchSize;
1349
Brian Paul3c634522002-10-24 23:57:19 +00001350 ASSERT(driver_ctx);
jtgafb833d1999-08-19 00:55:39 +00001351
Brian Paul3c634522002-10-24 23:57:19 +00001352 /* If the driver wants core Mesa to use special imports, it'll have to
1353 * override these defaults.
1354 */
1355 _mesa_init_default_imports( &(ctx->imports), driver_ctx );
jtgafb833d1999-08-19 00:55:39 +00001356
Brian Paul9a33a112002-06-13 04:28:29 +00001357 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001358 _mesa_init_default_exports( &(ctx->exports) );
1359
1360 /* misc one-time initializations */
1361 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001362
Brian Paul3c634522002-10-24 23:57:19 +00001363 ctx->DriverCtx = driver_ctx;
Brian Paulb1394fa2000-09-26 20:53:53 +00001364 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001365 ctx->DrawBuffer = NULL;
1366 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001367
Brian Paula3f13702003-04-01 16:41:50 +00001368 /* Set these pointers to defaults now in case they're not set since
1369 * we need them while creating the default textures.
1370 */
1371 if (!ctx->Driver.NewTextureObject)
1372 ctx->Driver.NewTextureObject = _mesa_new_texture_object;
1373 if (!ctx->Driver.DeleteTexture)
1374 ctx->Driver.DeleteTexture = _mesa_delete_texture_object;
1375 if (!ctx->Driver.NewTextureImage)
1376 ctx->Driver.NewTextureImage = _mesa_new_texture_image;
1377
jtgafb833d1999-08-19 00:55:39 +00001378 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001379 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001380 ctx->Shared = share_list->Shared;
1381 }
1382 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001383 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001384 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001385 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001386 }
1387 }
Brian Paul9560f052000-01-31 23:11:39 +00001388 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001389 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001390 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001391
Keith Whitwell6dc85572003-07-17 13:43:59 +00001392 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001393 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001394 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001395 }
jtgafb833d1999-08-19 00:55:39 +00001396
Brian Paulf44898c2003-07-18 15:44:57 +00001397 /* libGL ABI coordination */
1398 add_newer_entrypoints();
Keith Whitwell6dc85572003-07-17 13:43:59 +00001399
Brian Paul5fb84d22000-05-24 15:04:45 +00001400 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1401 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1402 * Mesa we do this to accomodate different versions of libGL and various
1403 * DRI drivers.
1404 */
1405 dispatchSize = MAX2(_glapi_get_dispatch_table_size(),
1406 sizeof(struct _glapi_table) / sizeof(void *));
1407
Brian Paulfbd8f211999-11-11 01:22:25 +00001408 /* setup API dispatch tables */
Brian Paul5fb84d22000-05-24 15:04:45 +00001409 ctx->Exec = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
1410 ctx->Save = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001411 if (!ctx->Exec || !ctx->Save) {
1412 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001413 if (ctx->Exec)
Brian Paul2d8db392000-06-27 22:10:00 +00001414 FREE( ctx->Exec );
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001415 }
Brian Paul5fb84d22000-05-24 15:04:45 +00001416 _mesa_init_exec_table(ctx->Exec, dispatchSize);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001417 ctx->CurrentDispatch = ctx->Exec;
jtgafb833d1999-08-19 00:55:39 +00001418
Keith Whitwell6dc85572003-07-17 13:43:59 +00001419#if _HAVE_FULL_GL
1420 _mesa_init_dlist_table(ctx->Save, dispatchSize);
1421
Keith Whitwellad2ac212000-11-24 10:25:05 +00001422 ctx->ExecPrefersFloat = GL_FALSE;
1423 ctx->SavePrefersFloat = GL_FALSE;
1424
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001425 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001426 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001427 ctx->TnlModule.Current = NULL;
1428 ctx->TnlModule.SwapCount = 0;
1429
Keith Whitwell6dc85572003-07-17 13:43:59 +00001430#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001431
Brian Paul4d053dd2000-01-14 04:45:47 +00001432 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001433}
1434
Brian Paulde4f4602003-07-03 02:15:06 +00001435/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001436 * Allocate and initialize a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001437 *
1438 * \param visual a GLvisual pointer (we copy the struct contents)
1439 * \param share_list another context to share display lists with or NULL
1440 * \param driver_ctx pointer to device driver's context state struct
1441 * \param direct obsolete, ignored
1442 *
1443 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001444 */
Brian Paul178a1c52000-04-22 01:05:00 +00001445GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001446_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001447 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001448 void *driver_ctx,
1449 GLboolean direct )
1450
Brian Paul4d053dd2000-01-14 04:45:47 +00001451{
Brian Paul4753d602002-06-15 02:38:15 +00001452 GLcontext *ctx;
1453
1454 ASSERT(visual);
Brian Paul3c634522002-10-24 23:57:19 +00001455 ASSERT(driver_ctx);
Brian Paul4753d602002-06-15 02:38:15 +00001456
Brian Paul3c634522002-10-24 23:57:19 +00001457 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001458 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001459 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001460
Brian Paul3c634522002-10-24 23:57:19 +00001461 if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001462 return ctx;
1463 }
1464 else {
Brian Paul3c634522002-10-24 23:57:19 +00001465 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001466 return NULL;
1467 }
1468}
1469
Keith Whitwell6dc85572003-07-17 13:43:59 +00001470/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001471 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001472 *
1473 * But doesn't free the GLcontext struct itself.
1474 *
1475 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001476 */
Brian Paul178a1c52000-04-22 01:05:00 +00001477void
Brian Paulb1394fa2000-09-26 20:53:53 +00001478_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001479{
Brian Paul4d053dd2000-01-14 04:45:47 +00001480 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001481 if (ctx == _mesa_get_current_context()) {
1482 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001483 }
1484
Keith Whitwell6dc85572003-07-17 13:43:59 +00001485 _mesa_free_lighting_data( ctx );
1486 _mesa_free_eval_data( ctx );
1487 _mesa_free_texture_data( ctx );
1488 _mesa_free_matrix_data( ctx );
1489 _mesa_free_viewport_data( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001490 _mesa_free_colortables_data( ctx );
Brian Paul8dfc5b92002-10-16 17:57:51 +00001491#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +00001492 if (ctx->VertexProgram.Current) {
Brian Paul610d5992003-01-14 04:55:45 +00001493 ctx->VertexProgram.Current->Base.RefCount--;
1494 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001495 _mesa_delete_program(ctx, &(ctx->VertexProgram.Current->Base));
Brian Paul610d5992003-01-14 04:55:45 +00001496 }
1497#endif
1498#if FEATURE_NV_fragment_program
1499 if (ctx->FragmentProgram.Current) {
1500 ctx->FragmentProgram.Current->Base.RefCount--;
1501 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001502 _mesa_delete_program(ctx, &(ctx->FragmentProgram.Current->Base));
Brian Paulfd284452001-07-19 15:54:34 +00001503 }
Brian Paul8dfc5b92002-10-16 17:57:51 +00001504#endif
Brian Paulfd284452001-07-19 15:54:34 +00001505
Brian Paul30f51ae2001-12-18 04:06:44 +00001506 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001507 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001508 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001509 assert(ctx->Shared->RefCount >= 0);
1510 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1511 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001512 /* free shared state */
1513 free_shared_state( ctx, ctx->Shared );
1514 }
1515
Brian Paul702ca202003-07-18 15:22:16 +00001516 if (ctx->Extensions.String)
1517 FREE((void *) ctx->Extensions.String);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001518
1519 FREE(ctx->Exec);
1520 FREE(ctx->Save);
Brian Paul4d053dd2000-01-14 04:45:47 +00001521}
1522
Keith Whitwell6dc85572003-07-17 13:43:59 +00001523/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001524 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001525 *
1526 * \param ctx GL context.
1527 *
1528 * Calls _mesa_free_context_data() and free the structure.
jtgafb833d1999-08-19 00:55:39 +00001529 */
Brian Paul178a1c52000-04-22 01:05:00 +00001530void
Brian Paulb1394fa2000-09-26 20:53:53 +00001531_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001532{
1533 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001534 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001535 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001536 }
1537}
1538
Keith Whitwell6dc85572003-07-17 13:43:59 +00001539#if _HAVE_FULL_GL
1540/**
jtgafb833d1999-08-19 00:55:39 +00001541 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001542 *
1543 * \param src source context
1544 * \param dst destination context
1545 * \param mask bitwise OR of GL_*_BIT flags
1546 *
1547 * According to the bits specified in \p mask, copies the corresponding
1548 * attributes from \p src into \dst. For many of the attributes a simple \c
1549 * memcpy is not enough due to the existence of internal pointers in their data
1550 * structures.
jtgafb833d1999-08-19 00:55:39 +00001551 */
Brian Paul178a1c52000-04-22 01:05:00 +00001552void
Brian Paulb1394fa2000-09-26 20:53:53 +00001553_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001554{
1555 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001556 /* OK to memcpy */
1557 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001558 }
1559 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001560 /* OK to memcpy */
1561 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001562 }
1563 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001564 /* OK to memcpy */
1565 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001566 }
1567 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001568 /* OK to memcpy */
1569 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001570 }
1571 if (mask & GL_ENABLE_BIT) {
1572 /* no op */
1573 }
1574 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001575 /* OK to memcpy */
1576 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001577 }
1578 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001579 /* OK to memcpy */
1580 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001581 }
1582 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001583 /* OK to memcpy */
1584 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001585 }
1586 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001587 GLuint i;
1588 /* begin with memcpy */
1589 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1590 /* fixup linked lists to prevent pointer insanity */
1591 make_empty_list( &(dst->Light.EnabledList) );
1592 for (i = 0; i < MAX_LIGHTS; i++) {
1593 if (dst->Light.Light[i].Enabled) {
1594 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1595 }
1596 }
jtgafb833d1999-08-19 00:55:39 +00001597 }
1598 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001599 /* OK to memcpy */
1600 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001601 }
1602 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001603 /* OK to memcpy */
1604 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001605 }
1606 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001607 /* OK to memcpy */
1608 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001609 }
1610 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001611 /* OK to memcpy */
1612 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001613 }
1614 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001615 /* OK to memcpy */
1616 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001617 }
1618 if (mask & GL_POLYGON_STIPPLE_BIT) {
1619 /* Use loop instead of MEMCPY due to problem with Portland Group's
1620 * C compiler. Reported by John Stone.
1621 */
Brian Paul85d81602002-06-17 23:36:31 +00001622 GLuint i;
1623 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001624 dst->PolygonStipple[i] = src->PolygonStipple[i];
1625 }
1626 }
1627 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001628 /* OK to memcpy */
1629 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001630 }
1631 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001632 /* OK to memcpy */
1633 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001634 }
1635 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001636 /* Cannot memcpy because of pointers */
1637 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001638 }
1639 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001640 /* OK to memcpy */
1641 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001642 }
1643 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001644 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1645 dst->Viewport.X = src->Viewport.X;
1646 dst->Viewport.Y = src->Viewport.Y;
1647 dst->Viewport.Width = src->Viewport.Width;
1648 dst->Viewport.Height = src->Viewport.Height;
1649 dst->Viewport.Near = src->Viewport.Near;
1650 dst->Viewport.Far = src->Viewport.Far;
1651 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001652 }
Brian Paul85d81602002-06-17 23:36:31 +00001653
Keith Whitwella96308c2000-10-30 13:31:59 +00001654 /* XXX FIXME: Call callbacks?
1655 */
1656 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001657}
Keith Whitwell23caf202000-11-16 21:05:34 +00001658#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001659
1660
Brian Paulb1d53d92003-06-11 18:48:19 +00001661/**
1662 * Check if the given context can render into the given framebuffer
1663 * by checking visual attributes.
1664 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1665 */
1666static GLboolean
1667check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1668{
1669 const GLvisual *ctxvis = &ctx->Visual;
1670 const GLvisual *bufvis = &buffer->Visual;
1671
1672 if (ctxvis == bufvis)
1673 return GL_TRUE;
1674
1675 if (ctxvis->rgbMode != bufvis->rgbMode)
1676 return GL_FALSE;
1677 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1678 return GL_FALSE;
1679 if (ctxvis->stereoMode && !bufvis->stereoMode)
1680 return GL_FALSE;
1681 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1682 return GL_FALSE;
1683 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1684 return GL_FALSE;
1685 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1686 return GL_FALSE;
1687 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1688 return GL_FALSE;
1689 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1690 return GL_FALSE;
1691 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1692 return GL_FALSE;
1693 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1694 return GL_FALSE;
1695 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1696 return GL_FALSE;
1697
1698 return GL_TRUE;
1699}
1700
1701
Keith Whitwell6dc85572003-07-17 13:43:59 +00001702/**
Brian Paul9a33a112002-06-13 04:28:29 +00001703 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001704 *
1705 * \param newCtx new GL context.
1706 * \param buffer framebuffer.
1707 *
1708 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001709 */
1710void
1711_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1712{
1713 _mesa_make_current2( newCtx, buffer, buffer );
1714}
1715
Keith Whitwell6dc85572003-07-17 13:43:59 +00001716/**
1717 * Bind the given context to the given draw-buffer and read-buffer and
1718 * make it the current context for this thread.
1719 *
1720 * \param newCtx new GL context. If NULL then there will be no current GL
1721 * context.
1722 * \param drawBuffer draw framebuffer.
1723 * \param readBuffer read framebuffer.
1724 *
1725 * Check that the context's and framebuffer's visuals are compatible, returning
1726 * immediately otherwise. Sets the glapi current context via
1727 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1728 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1729 * Calls dd_function_table::MakeCurrent callback if defined.
1730 *
1731 * When a context is bound by the first time and the \c MESA_INFO environment
1732 * variable is set it calls print_info() as an aid for remote user
1733 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001734 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001735void
1736_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1737 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001738{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001739 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001740 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001741
Brian Paulbe3602d2001-02-28 00:27:48 +00001742 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001743 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001744 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1745 if (!check_compatible(newCtx, drawBuffer))
1746 return;
1747 }
1748 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1749 if (!check_compatible(newCtx, readBuffer))
1750 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001751 }
1752
Brian Paul00037781999-12-17 14:52:35 +00001753 /* We call this function periodically (just here for now) in
1754 * order to detect when multithreading has begun.
1755 */
1756 _glapi_check_multithread();
1757
Brian Paulf9b97d92000-01-28 20:17:42 +00001758 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001759 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001760
1761
1762 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001763 _glapi_set_dispatch(NULL); /* none current */
1764 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001765 else {
1766 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001767
Keith Whitwell23caf202000-11-16 21:05:34 +00001768 if (drawBuffer && readBuffer) {
1769 /* TODO: check if newCtx and buffer's visual match??? */
1770 newCtx->DrawBuffer = drawBuffer;
1771 newCtx->ReadBuffer = readBuffer;
1772 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001773
Keith Whitwell6dc85572003-07-17 13:43:59 +00001774#if _HAVE_FULL_GL
Brian Paul10d7f542002-06-17 23:38:14 +00001775 if (drawBuffer->Width == 0 && drawBuffer->Height == 0) {
1776 /* get initial window size */
1777 GLuint bufWidth, bufHeight;
1778
1779 /* ask device driver for size of output buffer */
1780 (*newCtx->Driver.GetBufferSize)( drawBuffer, &bufWidth, &bufHeight );
1781
Keith Whitwell6dc85572003-07-17 13:43:59 +00001782 if (drawBuffer->Width != bufWidth ||
1783 drawBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001784
Keith Whitwell6dc85572003-07-17 13:43:59 +00001785 drawBuffer->Width = bufWidth;
1786 drawBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001787
Keith Whitwell6dc85572003-07-17 13:43:59 +00001788 newCtx->Driver.ResizeBuffers( drawBuffer );
1789 }
Brian Paul10d7f542002-06-17 23:38:14 +00001790 }
1791
1792 if (readBuffer != drawBuffer &&
1793 readBuffer->Width == 0 && readBuffer->Height == 0) {
1794 /* get initial window size */
1795 GLuint bufWidth, bufHeight;
1796
1797 /* ask device driver for size of output buffer */
1798 (*newCtx->Driver.GetBufferSize)( readBuffer, &bufWidth, &bufHeight );
1799
Keith Whitwell6dc85572003-07-17 13:43:59 +00001800 if (readBuffer->Width != bufWidth ||
1801 readBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001802
Keith Whitwell6dc85572003-07-17 13:43:59 +00001803 readBuffer->Width = bufWidth;
1804 readBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001805
Keith Whitwell6dc85572003-07-17 13:43:59 +00001806 newCtx->Driver.ResizeBuffers( readBuffer );
1807 }
Brian Paul10d7f542002-06-17 23:38:14 +00001808 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001809#endif
Brian Paul00037781999-12-17 14:52:35 +00001810 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001811
Keith Whitwell6dc85572003-07-17 13:43:59 +00001812 /* Alert the driver - usually passed on to the sw t&l module,
1813 * but also used to detect threaded cases in the radeon codegen
1814 * hw t&l module.
1815 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001816 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001817 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1818
1819 /* We can use this to help debug user's problems. Tell them to set
1820 * the MESA_INFO env variable before running their app. Then the
1821 * first time each context is made current we'll print some useful
1822 * information.
1823 */
1824 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001825 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001826 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001827 }
1828 newCtx->FirstTimeCurrent = GL_FALSE;
1829 }
Brian Paul00037781999-12-17 14:52:35 +00001830 }
1831}
1832
Keith Whitwell6dc85572003-07-17 13:43:59 +00001833/**
1834 * Get current context for the calling thread.
1835 *
1836 * \return pointer to the current GL context.
1837 *
1838 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1839 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00001840 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001841GLcontext *
1842_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00001843{
Brian Paulf9b97d92000-01-28 20:17:42 +00001844 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001845}
1846
Keith Whitwell6dc85572003-07-17 13:43:59 +00001847/**
1848 * Get context's current API dispatch table.
1849 *
1850 * It'll either be the immediate-mode execute dispatcher or the display list
1851 * compile dispatcher.
1852 *
1853 * \param ctx GL context.
1854 *
1855 * \return pointer to dispatch_table.
1856 *
1857 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00001858 */
1859struct _glapi_table *
1860_mesa_get_dispatch(GLcontext *ctx)
1861{
1862 return ctx->CurrentDispatch;
1863}
1864
Keith Whitwell6dc85572003-07-17 13:43:59 +00001865/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00001866
1867
jtgafb833d1999-08-19 00:55:39 +00001868/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001869/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00001870/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001871/*@{*/
jtgafb833d1999-08-19 00:55:39 +00001872
Keith Whitwell6dc85572003-07-17 13:43:59 +00001873/**
1874 * Record an error.
1875 *
1876 * \param ctx GL context.
1877 * \param error error code.
1878 *
1879 * Records the given error code and call the driver's dd_function_table::Error
1880 * function if defined.
1881 *
1882 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00001883 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00001884 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001885void
Brian Paul4e9676f2002-06-29 19:48:15 +00001886_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00001887{
Brian Paul18a285a2002-03-16 00:53:15 +00001888 if (!ctx)
1889 return;
1890
Brian Paul7eb06032000-07-14 04:13:40 +00001891 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00001892 ctx->ErrorValue = error;
1893 }
1894
1895 /* Call device driver's error handler, if any. This is used on the Mac. */
1896 if (ctx->Driver.Error) {
1897 (*ctx->Driver.Error)( ctx );
1898 }
1899}
1900
Keith Whitwell6dc85572003-07-17 13:43:59 +00001901/**
1902 * Execute glFinish().
1903 *
1904 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1905 * dd_function_table::Finish driver callback, if not NULL.
1906 */
Brian Paulfa9df402000-02-02 19:16:46 +00001907void
1908_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00001909{
Brian Paulfa9df402000-02-02 19:16:46 +00001910 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001911 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001912 if (ctx->Driver.Finish) {
1913 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001914 }
1915}
1916
Keith Whitwell6dc85572003-07-17 13:43:59 +00001917/**
1918 * Execute glFlush().
1919 *
1920 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1921 * dd_function_table::Flush driver callback, if not NULL.
1922 */
Brian Paulfa9df402000-02-02 19:16:46 +00001923void
1924_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00001925{
Brian Paulfa9df402000-02-02 19:16:46 +00001926 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001927 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001928 if (ctx->Driver.Flush) {
1929 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001930 }
jtgafb833d1999-08-19 00:55:39 +00001931}
Brian Paul48c6a6e2000-09-08 21:28:04 +00001932
1933
Keith Whitwell6dc85572003-07-17 13:43:59 +00001934/*@}*/