blob: ee299cd3113c707efc3501155bd826b473416016 [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 Paul148a2842003-09-17 03:40:11 +000080#include "bufferobj.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 Paul04a81da2003-08-31 18:52:47 +0000114#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
115#include "program.h"
Brian Paul3a212032002-11-19 15:25:00 +0000116#endif
Gareth Hughesd4eb6652001-03-12 01:32:20 +0000117#include "vtxfmt.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000118#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000119#include "math/m_translate.h"
Keith Whitwell23caf202000-11-16 21:05:34 +0000120#include "math/m_matrix.h"
121#include "math/m_xform.h"
Keith Whitwellf4b02d12001-01-05 05:31:42 +0000122#include "math/mathmod.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000123#endif
jtgafb833d1999-08-19 00:55:39 +0000124
Brian Paul3b18a362000-09-26 15:27:20 +0000125#if defined(MESA_TRACE)
Brian Paul45f36342000-09-05 20:28:06 +0000126#include "Trace/tr_context.h"
127#include "Trace/tr_wrapper.h"
128#endif
129
davem69775355a2001-06-05 23:54:00 +0000130#ifdef USE_SPARC_ASM
131#include "SPARC/sparc.h"
132#endif
jtgafb833d1999-08-19 00:55:39 +0000133
Keith Whitwell23caf202000-11-16 21:05:34 +0000134#ifndef MESA_VERBOSE
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000135int MESA_VERBOSE = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000136#endif
137
138#ifndef MESA_DEBUG_FLAGS
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000139int MESA_DEBUG_FLAGS = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000140#endif
Brian Paulb1394fa2000-09-26 20:53:53 +0000141
Brian Paul86b84272001-12-14 02:50:01 +0000142
Brian Paul27558a12003-03-01 01:50:20 +0000143/* ubyte -> float conversion */
144GLfloat _mesa_ubyte_to_float_color_tab[256];
145
Brian Paul9a33a112002-06-13 04:28:29 +0000146static void
147free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
148
Brian Paul86b84272001-12-14 02:50:01 +0000149
Brian Paulb1394fa2000-09-26 20:53:53 +0000150/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000151/** \name OpenGL SI-style interface (new in Mesa 3.5)
152 *
153 * \if subset
154 * \note Most of these functions are never called in the Mesa subset.
155 * \endif
156 */
157/*@{*/
Brian Paulb1394fa2000-09-26 20:53:53 +0000158
Keith Whitwell6dc85572003-07-17 13:43:59 +0000159/**
160 * Destroy context callback.
161 *
162 * \param gc context.
163 * \return GL_TRUE on success, or GL_FALSE on failure.
164 *
165 * \ifnot subset
166 * Called by window system/device driver (via __GLexports::destroyCurrent) when
167 * the rendering context is to be destroyed.
168 * \endif
169 *
170 * Frees the context data and the context structure.
Brian Paul9a33a112002-06-13 04:28:29 +0000171 */
172GLboolean
173_mesa_destroyContext(__GLcontext *gc)
Brian Paulb1394fa2000-09-26 20:53:53 +0000174{
175 if (gc) {
176 _mesa_free_context_data(gc);
Brian Paul3c634522002-10-24 23:57:19 +0000177 _mesa_free(gc);
Brian Paulb1394fa2000-09-26 20:53:53 +0000178 }
179 return GL_TRUE;
180}
181
Keith Whitwell6dc85572003-07-17 13:43:59 +0000182/**
183 * Unbind context callback.
184 *
185 * \param gc context.
186 * \return GL_TRUE on success, or GL_FALSE on failure.
187 *
188 * \ifnot subset
189 * Called by window system/device driver (via __GLexports::loseCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000190 * when the rendering context is made non-current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000191 * \endif
192 *
193 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000194 */
195GLboolean
196_mesa_loseCurrent(__GLcontext *gc)
197{
198 /* XXX unbind context from thread */
199 return GL_TRUE;
200}
201
Keith Whitwell6dc85572003-07-17 13:43:59 +0000202/**
203 * Bind context callback.
204 *
205 * \param gc context.
206 * \return GL_TRUE on success, or GL_FALSE on failure.
207 *
208 * \ifnot subset
209 * Called by window system/device driver (via __GLexports::makeCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000210 * when the rendering context is made current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000211 * \endif
212 *
213 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000214 */
215GLboolean
216_mesa_makeCurrent(__GLcontext *gc)
217{
218 /* XXX bind context to thread */
219 return GL_TRUE;
220}
221
Keith Whitwell6dc85572003-07-17 13:43:59 +0000222/**
223 * Share context callback.
224 *
225 * \param gc context.
226 * \param gcShare shared context.
227 * \return GL_TRUE on success, or GL_FALSE on failure.
228 *
229 * \ifnot subset
230 * Called by window system/device driver (via __GLexports::shareContext)
231 * \endif
232 *
233 * Update the shared context reference count, gl_shared_state::RefCount.
Brian Paul9a33a112002-06-13 04:28:29 +0000234 */
235GLboolean
236_mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
237{
238 if (gc && gcShare && gc->Shared && gcShare->Shared) {
239 gc->Shared->RefCount--;
240 if (gc->Shared->RefCount == 0) {
241 free_shared_state(gc, gc->Shared);
242 }
243 gc->Shared = gcShare->Shared;
244 gc->Shared->RefCount++;
245 return GL_TRUE;
246 }
247 else {
248 return GL_FALSE;
249 }
250}
251
Keith Whitwell6dc85572003-07-17 13:43:59 +0000252
253#if _HAVE_FULL_GL
254/**
255 * Copy context callback.
256 */
Brian Paul9a33a112002-06-13 04:28:29 +0000257GLboolean
258_mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
259{
260 if (dst && src) {
261 _mesa_copy_context( src, dst, mask );
262 return GL_TRUE;
263 }
264 else {
265 return GL_FALSE;
266 }
267}
Keith Whitwell6dc85572003-07-17 13:43:59 +0000268#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000269
Keith Whitwell6dc85572003-07-17 13:43:59 +0000270/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000271GLboolean
272_mesa_forceCurrent(__GLcontext *gc)
273{
274 return GL_TRUE;
275}
276
Keith Whitwell6dc85572003-07-17 13:43:59 +0000277/**
278 * Windows/buffer resizing notification callback.
279 *
280 * \param gc GL context.
281 * \return GL_TRUE on success, or GL_FALSE on failure.
282 */
Brian Paul9a33a112002-06-13 04:28:29 +0000283GLboolean
284_mesa_notifyResize(__GLcontext *gc)
285{
286 GLint x, y;
287 GLuint width, height;
288 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
289 if (!d || !d->getDrawableSize)
290 return GL_FALSE;
291 d->getDrawableSize( d, &x, &y, &width, &height );
292 /* update viewport, resize software buffers, etc. */
293 return GL_TRUE;
294}
295
Keith Whitwell6dc85572003-07-17 13:43:59 +0000296/**
297 * Window/buffer destruction notification callback.
298 *
299 * \param gc GL context.
300 *
301 * Called when the context's window/buffer is going to be destroyed.
302 *
303 * No-op
304 */
Brian Paul9a33a112002-06-13 04:28:29 +0000305void
306_mesa_notifyDestroy(__GLcontext *gc)
307{
Brian Paul60b6e4f2002-10-14 17:08:17 +0000308 /* Unbind from it. */
Brian Paul9a33a112002-06-13 04:28:29 +0000309}
310
Keith Whitwell6dc85572003-07-17 13:43:59 +0000311/**
312 * Swap buffers notification callback.
313 *
314 * \param gc GL context.
315 *
316 * Called by window system just before swapping buffers.
Brian Paul9a33a112002-06-13 04:28:29 +0000317 * We have to finish any pending rendering.
318 */
319void
320_mesa_notifySwapBuffers(__GLcontext *gc)
321{
322 FLUSH_VERTICES( gc, 0 );
323}
324
Keith Whitwell6dc85572003-07-17 13:43:59 +0000325/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000326struct __GLdispatchStateRec *
327_mesa_dispatchExec(__GLcontext *gc)
328{
329 return NULL;
330}
331
Keith Whitwell6dc85572003-07-17 13:43:59 +0000332/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000333void
334_mesa_beginDispatchOverride(__GLcontext *gc)
335{
336}
337
Keith Whitwell6dc85572003-07-17 13:43:59 +0000338/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000339void
340_mesa_endDispatchOverride(__GLcontext *gc)
341{
342}
343
Keith Whitwell6dc85572003-07-17 13:43:59 +0000344/**
345 * \ifnot subset
346 * Setup the exports.
347 *
348 * The window system will call these functions when it needs Mesa to do
349 * something.
350 *
351 * \note Device drivers should override these functions! For example,
Brian Paul9a33a112002-06-13 04:28:29 +0000352 * the Xlib driver should plug in the XMesa*-style functions into this
353 * structure. The XMesa-style functions should then call the _mesa_*
354 * version of these functions. This is an approximation to OO design
355 * (inheritance and virtual functions).
Keith Whitwell6dc85572003-07-17 13:43:59 +0000356 * \endif
357 *
358 * \if subset
359 * No-op.
360 *
361 * \endif
Brian Paul9a33a112002-06-13 04:28:29 +0000362 */
363static void
364_mesa_init_default_exports(__GLexports *exports)
365{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000366#if _HAVE_FULL_GL
Brian Paul9a33a112002-06-13 04:28:29 +0000367 exports->destroyContext = _mesa_destroyContext;
368 exports->loseCurrent = _mesa_loseCurrent;
369 exports->makeCurrent = _mesa_makeCurrent;
370 exports->shareContext = _mesa_shareContext;
371 exports->copyContext = _mesa_copyContext;
372 exports->forceCurrent = _mesa_forceCurrent;
373 exports->notifyResize = _mesa_notifyResize;
Brian Paul2f35d5e2002-06-13 04:31:09 +0000374 exports->notifyDestroy = _mesa_notifyDestroy;
Brian Paul9a33a112002-06-13 04:28:29 +0000375 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
376 exports->dispatchExec = _mesa_dispatchExec;
377 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
378 exports->endDispatchOverride = _mesa_endDispatchOverride;
Keith Whitwell6dc85572003-07-17 13:43:59 +0000379#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000380}
381
Keith Whitwell6dc85572003-07-17 13:43:59 +0000382/**
383 * Exported OpenGL SI interface.
384 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000385__GLcontext *
386__glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
387{
388 GLcontext *ctx;
389
Brian Paul4753d602002-06-15 02:38:15 +0000390 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
Brian Paulb1394fa2000-09-26 20:53:53 +0000391 if (ctx == NULL) {
392 return NULL;
393 }
Brian Paul60b6e4f2002-10-14 17:08:17 +0000394
Brian Paul3c634522002-10-24 23:57:19 +0000395 _mesa_initialize_context(ctx, modes, NULL, imports, GL_FALSE);
Brian Paulb1394fa2000-09-26 20:53:53 +0000396 ctx->imports = *imports;
Brian Paulb1394fa2000-09-26 20:53:53 +0000397
398 return ctx;
399}
400
Keith Whitwell6dc85572003-07-17 13:43:59 +0000401/**
402 * Exported OpenGL SI interface.
403 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000404void
405__glCoreNopDispatch(void)
406{
407#if 0
408 /* SI */
409 __gl_dispatch = __glNopDispatchState;
410#else
411 /* Mesa */
412 _glapi_set_dispatch(NULL);
413#endif
414}
415
Keith Whitwell6dc85572003-07-17 13:43:59 +0000416/*@}*/
417
Brian Paulb1394fa2000-09-26 20:53:53 +0000418
jtgafb833d1999-08-19 00:55:39 +0000419/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000420/** \name GL Visual allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000421/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000422/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000423
Keith Whitwell6dc85572003-07-17 13:43:59 +0000424/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000425 * Allocate a new GLvisual object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000426 *
427 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
428 * \param dbFlag double buffering
429 * \param stereoFlag stereo buffer
430 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
431 * is acceptable but the actual depth type will be GLushort or GLuint as
432 * needed.
433 * \param stencilBits requested minimum bits per stencil buffer value
434 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
435 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
436 * \param redBits number of bits per color component in frame buffer for RGB(A)
437 * mode. We always use 8 in core Mesa though.
438 * \param greenBits same as above.
439 * \param blueBits same as above.
440 * \param alphaBits same as above.
441 * \param numSamples not really used.
442 *
443 * \return pointer to new GLvisual or NULL if requested parameters can't be
444 * met.
445 *
446 * Allocates a GLvisual structure and initializes it via
447 * _mesa_initialize_visual().
Brian Paul4d053dd2000-01-14 04:45:47 +0000448 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000449GLvisual *
450_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000451 GLboolean dbFlag,
452 GLboolean stereoFlag,
453 GLint redBits,
454 GLint greenBits,
455 GLint blueBits,
456 GLint alphaBits,
457 GLint indexBits,
458 GLint depthBits,
459 GLint stencilBits,
460 GLint accumRedBits,
461 GLint accumGreenBits,
462 GLint accumBlueBits,
463 GLint accumAlphaBits,
464 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000465{
Brian Paul178a1c52000-04-22 01:05:00 +0000466 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
467 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000468 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000469 redBits, greenBits, blueBits, alphaBits,
470 indexBits, depthBits, stencilBits,
471 accumRedBits, accumGreenBits,
472 accumBlueBits, accumAlphaBits,
Brian Paulb1394fa2000-09-26 20:53:53 +0000473 numSamples)) {
Brian Paul178a1c52000-04-22 01:05:00 +0000474 FREE(vis);
475 return NULL;
476 }
477 }
478 return vis;
479}
480
Keith Whitwell6dc85572003-07-17 13:43:59 +0000481/**
Brian Paul178a1c52000-04-22 01:05:00 +0000482 * Initialize the fields of the given GLvisual.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000483 *
484 * \return GL_TRUE on success, or GL_FALSE on failure.
485 *
486 * \sa _mesa_create_visual() above for the parameter description.
487 *
488 * Makes some sanity checks and fills in the fields of the
489 * GLvisual structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000490 */
491GLboolean
492_mesa_initialize_visual( GLvisual *vis,
493 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000494 GLboolean dbFlag,
495 GLboolean stereoFlag,
496 GLint redBits,
497 GLint greenBits,
498 GLint blueBits,
499 GLint alphaBits,
500 GLint indexBits,
501 GLint depthBits,
502 GLint stencilBits,
503 GLint accumRedBits,
504 GLint accumGreenBits,
505 GLint accumBlueBits,
506 GLint accumAlphaBits,
507 GLint numSamples )
508{
Brian Paulb6bcae52001-01-23 23:39:36 +0000509 (void) numSamples;
510
Brian Paul178a1c52000-04-22 01:05:00 +0000511 assert(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000512
Brian Pauled30dfa2000-03-03 17:47:39 +0000513 /* This is to catch bad values from device drivers not updated for
514 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
515 * bad value now (a 1-bit depth buffer!?!).
516 */
517 assert(depthBits == 0 || depthBits > 1);
518
519 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000520 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000521 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000522 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000523 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000524 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000525 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000526 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000527 }
528 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000529 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000530 }
531 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000532 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000533 }
534 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000535 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000536 }
537
Brian Paulb6bcae52001-01-23 23:39:36 +0000538 vis->rgbMode = rgbFlag;
539 vis->doubleBufferMode = dbFlag;
540 vis->stereoMode = stereoFlag;
Brian Paul153f1542002-10-29 15:04:35 +0000541
Brian Paulb6bcae52001-01-23 23:39:36 +0000542 vis->redBits = redBits;
543 vis->greenBits = greenBits;
544 vis->blueBits = blueBits;
545 vis->alphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000546
Brian Paulb6bcae52001-01-23 23:39:36 +0000547 vis->indexBits = indexBits;
548 vis->depthBits = depthBits;
549 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
550 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
551 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
552 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
553 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Pauled30dfa2000-03-03 17:47:39 +0000554
Brian Paul153f1542002-10-29 15:04:35 +0000555 vis->haveAccumBuffer = accumRedBits > 0;
556 vis->haveDepthBuffer = depthBits > 0;
557 vis->haveStencilBuffer = stencilBits > 0;
558
559 vis->numAuxBuffers = 0;
560 vis->level = 0;
561 vis->pixmapMode = 0;
562
Brian Paul178a1c52000-04-22 01:05:00 +0000563 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000564}
565
Keith Whitwell6dc85572003-07-17 13:43:59 +0000566/**
567 * Destroy a visual.
568 *
569 * \param vis visual.
570 *
571 * Frees the visual structure.
572 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000573void
574_mesa_destroy_visual( GLvisual *vis )
575{
576 FREE(vis);
577}
578
Keith Whitwell6dc85572003-07-17 13:43:59 +0000579/*@}*/
580
Brian Paulb371e0d2000-03-31 01:05:51 +0000581
Brian Paul4d053dd2000-01-14 04:45:47 +0000582/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000583/** \name GL Framebuffer allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000584/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000585/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000586
Keith Whitwell6dc85572003-07-17 13:43:59 +0000587/**
588 * Create a new framebuffer.
589 *
590 * A GLframebuffer is a structure which encapsulates the depth, stencil and
591 * accum buffers and related parameters.
592 *
593 * \param visual a GLvisual pointer (we copy the struct contents)
594 * \param softwareDepth create/use a software depth buffer?
595 * \param softwareStencil create/use a software stencil buffer?
596 * \param softwareAccum create/use a software accum buffer?
597 * \param softwareAlpha create/use a software alpha buffer?
598 *
599 * \return pointer to new GLframebuffer struct or NULL if error.
600 *
601 * Allocate a GLframebuffer structure and initializes it via
602 * _mesa_initialize_framebuffer().
Brian Paul4d053dd2000-01-14 04:45:47 +0000603 */
Brian Paul178a1c52000-04-22 01:05:00 +0000604GLframebuffer *
Brian Paulbe3602d2001-02-28 00:27:48 +0000605_mesa_create_framebuffer( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +0000606 GLboolean softwareDepth,
607 GLboolean softwareStencil,
608 GLboolean softwareAccum,
609 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000610{
Brian Paul178a1c52000-04-22 01:05:00 +0000611 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
612 assert(visual);
613 if (buffer) {
614 _mesa_initialize_framebuffer(buffer, visual,
615 softwareDepth, softwareStencil,
616 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000617 }
Brian Paul178a1c52000-04-22 01:05:00 +0000618 return buffer;
619}
620
Keith Whitwell6dc85572003-07-17 13:43:59 +0000621/**
Brian Paul178a1c52000-04-22 01:05:00 +0000622 * Initialize a GLframebuffer object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000623 *
624 * \sa _mesa_create_framebuffer() above for the parameter description.
625 *
626 * Makes some sanity checks and fills in the fields of the
627 * GLframebuffer structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000628 */
629void
630_mesa_initialize_framebuffer( GLframebuffer *buffer,
Brian Paulbe3602d2001-02-28 00:27:48 +0000631 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +0000632 GLboolean softwareDepth,
633 GLboolean softwareStencil,
634 GLboolean softwareAccum,
635 GLboolean softwareAlpha )
636{
637 assert(buffer);
638 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000639
Brian Paul6ec6b842002-10-30 19:49:29 +0000640 _mesa_bzero(buffer, sizeof(GLframebuffer));
Brian Paul85d81602002-06-17 23:36:31 +0000641
Brian Paul4d053dd2000-01-14 04:45:47 +0000642 /* sanity checks */
643 if (softwareDepth ) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000644 assert(visual->depthBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000645 }
646 if (softwareStencil) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000647 assert(visual->stencilBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000648 }
649 if (softwareAccum) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000650 assert(visual->rgbMode);
651 assert(visual->accumRedBits > 0);
652 assert(visual->accumGreenBits > 0);
653 assert(visual->accumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000654 }
655 if (softwareAlpha) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000656 assert(visual->rgbMode);
657 assert(visual->alphaBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000658 }
659
Brian Paul75978bd2001-04-27 21:17:20 +0000660 buffer->Visual = *visual;
Brian Paul4d053dd2000-01-14 04:45:47 +0000661 buffer->UseSoftwareDepthBuffer = softwareDepth;
662 buffer->UseSoftwareStencilBuffer = softwareStencil;
663 buffer->UseSoftwareAccumBuffer = softwareAccum;
664 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul4d053dd2000-01-14 04:45:47 +0000665}
666
Keith Whitwell6dc85572003-07-17 13:43:59 +0000667/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000668 * Free a framebuffer struct and its buffers.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000669 *
670 * Calls _mesa_free_framebuffer_data() and frees the structure.
Brian Paul4d053dd2000-01-14 04:45:47 +0000671 */
Brian Paul178a1c52000-04-22 01:05:00 +0000672void
Brian Paulb1394fa2000-09-26 20:53:53 +0000673_mesa_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000674{
675 if (buffer) {
Brian Paul75978bd2001-04-27 21:17:20 +0000676 _mesa_free_framebuffer_data(buffer);
Brian Paul4d053dd2000-01-14 04:45:47 +0000677 FREE(buffer);
678 }
679}
680
Keith Whitwell6dc85572003-07-17 13:43:59 +0000681/**
682 * Free the data hanging off of \p buffer, but not \p buffer itself.
683 *
684 * \param buffer framebuffer.
685 *
686 * Frees all the buffers associated with the structure.
Brian Paul75978bd2001-04-27 21:17:20 +0000687 */
688void
689_mesa_free_framebuffer_data( GLframebuffer *buffer )
690{
691 if (!buffer)
692 return;
693
Brian Paul87506682003-05-27 15:20:43 +0000694 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
Brian Paulaeb44342002-03-19 16:47:04 +0000695 MESA_PBUFFER_FREE( buffer->DepthBuffer );
Brian Paul75978bd2001-04-27 21:17:20 +0000696 buffer->DepthBuffer = NULL;
697 }
Brian Paul87506682003-05-27 15:20:43 +0000698 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
Brian Paulaeb44342002-03-19 16:47:04 +0000699 MESA_PBUFFER_FREE( buffer->Accum );
Brian Paul75978bd2001-04-27 21:17:20 +0000700 buffer->Accum = NULL;
701 }
Brian Paul87506682003-05-27 15:20:43 +0000702 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
Brian Paulaeb44342002-03-19 16:47:04 +0000703 MESA_PBUFFER_FREE( buffer->Stencil );
Brian Paul75978bd2001-04-27 21:17:20 +0000704 buffer->Stencil = NULL;
705 }
Brian Paul87506682003-05-27 15:20:43 +0000706 if (buffer->UseSoftwareAlphaBuffers){
707 if (buffer->FrontLeftAlpha) {
708 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
709 buffer->FrontLeftAlpha = NULL;
710 }
711 if (buffer->BackLeftAlpha) {
712 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
713 buffer->BackLeftAlpha = NULL;
714 }
715 if (buffer->FrontRightAlpha) {
716 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
717 buffer->FrontRightAlpha = NULL;
718 }
719 if (buffer->BackRightAlpha) {
720 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
721 buffer->BackRightAlpha = NULL;
722 }
Brian Paul75978bd2001-04-27 21:17:20 +0000723 }
724}
725
Keith Whitwell6dc85572003-07-17 13:43:59 +0000726/*@}*/
Brian Paul75978bd2001-04-27 21:17:20 +0000727
Brian Paul4d053dd2000-01-14 04:45:47 +0000728
729/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000730/** \name Context allocation, initialization, destroying
731 *
732 * The purpose of the most initialization functions here is to provide the
733 * default state values according to the OpenGL specification.
734 */
jtgafb833d1999-08-19 00:55:39 +0000735/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000736/*@{*/
jtgafb833d1999-08-19 00:55:39 +0000737
Keith Whitwell6dc85572003-07-17 13:43:59 +0000738/**
739 * One-time initialization mutex lock.
740 *
741 * \sa Used by one_time_init().
742 */
Brian Paul9560f052000-01-31 23:11:39 +0000743_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
744
Keith Whitwell6dc85572003-07-17 13:43:59 +0000745/**
746 * Calls all the various one-time-init functions in Mesa.
747 *
748 * While holding a global mutex lock, calls several initialization functions,
749 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
750 * defined.
751 *
752 * \sa _mesa_init_lists(), _math_init().
jtgafb833d1999-08-19 00:55:39 +0000753 */
Brian Paul178a1c52000-04-22 01:05:00 +0000754static void
Brian Paul4753d602002-06-15 02:38:15 +0000755one_time_init( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000756{
757 static GLboolean alreadyCalled = GL_FALSE;
Brian Paul9560f052000-01-31 23:11:39 +0000758 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000759 if (!alreadyCalled) {
Brian Paul27558a12003-03-01 01:50:20 +0000760 GLuint i;
761
Brian Paul4d053dd2000-01-14 04:45:47 +0000762 /* do some implementation tests */
763 assert( sizeof(GLbyte) == 1 );
764 assert( sizeof(GLshort) >= 2 );
765 assert( sizeof(GLint) >= 4 );
766 assert( sizeof(GLubyte) == 1 );
767 assert( sizeof(GLushort) >= 2 );
768 assert( sizeof(GLuint) >= 4 );
769
Brian Paul08836342001-03-03 20:33:27 +0000770 _mesa_init_lists();
Keith Whitwell23caf202000-11-16 21:05:34 +0000771
Keith Whitwell6dc85572003-07-17 13:43:59 +0000772#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000773 _math_init();
Brian Paul27558a12003-03-01 01:50:20 +0000774
775 for (i = 0; i < 256; i++) {
776 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
777 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000778#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000779
davem69775355a2001-06-05 23:54:00 +0000780#ifdef USE_SPARC_ASM
781 _mesa_init_sparc_glapi_relocs();
782#endif
Brian Paul3c634522002-10-24 23:57:19 +0000783 if (_mesa_getenv("MESA_DEBUG")) {
Brian Paul68ee4bc2000-01-28 19:02:22 +0000784 _glapi_noop_enable_warnings(GL_TRUE);
Brian Paul71072be2002-10-10 00:22:13 +0000785#ifndef GLX_DIRECT_RENDERING
786 /* libGL from before 2002/06/28 don't have this function. Someday,
787 * when newer libGL libs are common, remove the #ifdef test. This
788 * only serves to print warnings when calling undefined GL functions.
789 */
Brian Paul4e9676f2002-06-29 19:48:15 +0000790 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
Brian Paul71072be2002-10-10 00:22:13 +0000791#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000792 }
793 else {
794 _glapi_noop_enable_warnings(GL_FALSE);
795 }
796
jtgafb833d1999-08-19 00:55:39 +0000797#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
Brian Paul4e9676f2002-06-29 19:48:15 +0000798 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
jtgafb833d1999-08-19 00:55:39 +0000799#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000800
801 alreadyCalled = GL_TRUE;
802 }
Brian Paul9560f052000-01-31 23:11:39 +0000803 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000804}
805
Keith Whitwell6dc85572003-07-17 13:43:59 +0000806/**
jtgafb833d1999-08-19 00:55:39 +0000807 * Allocate and initialize a shared context state structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000808 *
809 * \return pointer to a gl_shared_state structure on success, or NULL on
810 * failure.
811 *
812 * Initializes the display list, texture objects and vertex programs hash
813 * tables, allocates the texture objects. If it runs out of memory, frees
814 * everything already allocated before returning NULL.
jtgafb833d1999-08-19 00:55:39 +0000815 */
Brian Paula3f13702003-04-01 16:41:50 +0000816static GLboolean
817alloc_shared_state( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000818{
Brian Paula3f13702003-04-01 16:41:50 +0000819 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000820 if (!ss)
Brian Paula3f13702003-04-01 16:41:50 +0000821 return GL_FALSE;
822
823 ctx->Shared = ss;
jtgafb833d1999-08-19 00:55:39 +0000824
Brian Paule4b684c2000-09-12 21:07:40 +0000825 _glthread_INIT_MUTEX(ss->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000826
Brian Paule4b684c2000-09-12 21:07:40 +0000827 ss->DisplayList = _mesa_NewHashTable();
Brian Paulbb797902000-01-24 16:19:54 +0000828 ss->TexObjects = _mesa_NewHashTable();
Brian Paul451f3102003-04-17 01:48:19 +0000829#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
Brian Paul610d5992003-01-14 04:55:45 +0000830 ss->Programs = _mesa_NewHashTable();
Brian Paul8dfc5b92002-10-16 17:57:51 +0000831#endif
jtgafb833d1999-08-19 00:55:39 +0000832
Brian Paul451f3102003-04-17 01:48:19 +0000833#if FEATURE_ARB_vertex_program
834 ss->DefaultVertexProgram = _mesa_alloc_program(ctx, GL_VERTEX_PROGRAM_ARB, 0);
835 if (!ss->DefaultVertexProgram)
836 goto cleanup;
837#endif
838#if FEATURE_ARB_fragment_program
839 ss->DefaultFragmentProgram = _mesa_alloc_program(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
840 if (!ss->DefaultFragmentProgram)
841 goto cleanup;
842#endif
843
Ian Romanick0207b472003-09-09 00:10:12 +0000844 ss->BufferObjects = _mesa_NewHashTable();
845
Brian Paula3f13702003-04-01 16:41:50 +0000846 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
847 if (!ss->Default1D)
848 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000849
Brian Paula3f13702003-04-01 16:41:50 +0000850 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
851 if (!ss->Default2D)
852 goto cleanup;
jtgafb833d1999-08-19 00:55:39 +0000853
Brian Paula3f13702003-04-01 16:41:50 +0000854 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
855 if (!ss->Default3D)
856 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000857
Brian Paula3f13702003-04-01 16:41:50 +0000858 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
859 if (!ss->DefaultCubeMap)
860 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000861
Brian Paula3f13702003-04-01 16:41:50 +0000862 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
863 if (!ss->DefaultRect)
864 goto cleanup;
Brian Paul413d6a22000-05-26 14:44:59 +0000865
Brian Paula3f13702003-04-01 16:41:50 +0000866#if 0
867 _mesa_save_texture_object(ctx, ss->Default1D);
868 _mesa_save_texture_object(ctx, ss->Default2D);
869 _mesa_save_texture_object(ctx, ss->Default3D);
870 _mesa_save_texture_object(ctx, ss->DefaultCubeMap);
871 _mesa_save_texture_object(ctx, ss->DefaultRect);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000872#endif
Brian Paula3f13702003-04-01 16:41:50 +0000873
874 /* Effectively bind the default textures to all texture units */
875 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
876 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
877 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
878 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
879 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
880
881 return GL_TRUE;
882
883 cleanup:
884 /* Ran out of memory at some point. Free everything and return NULL */
885 if (ss->DisplayList)
886 _mesa_DeleteHashTable(ss->DisplayList);
887 if (ss->TexObjects)
888 _mesa_DeleteHashTable(ss->TexObjects);
889#if FEATURE_NV_vertex_program
890 if (ss->Programs)
891 _mesa_DeleteHashTable(ss->Programs);
892#endif
Brian Paul451f3102003-04-17 01:48:19 +0000893#if FEATURE_ARB_vertex_program
894 if (ss->DefaultVertexProgram)
895 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
896#endif
897#if FEATURE_ARB_fragment_program
898 if (ss->DefaultFragmentProgram)
899 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
900#endif
Ian Romanick0207b472003-09-09 00:10:12 +0000901 if (ss->BufferObjects)
902 _mesa_DeleteHashTable(ss->BufferObjects);
903
Brian Paula3f13702003-04-01 16:41:50 +0000904 if (ss->Default1D)
905 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
906 if (ss->Default2D)
907 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
908 if (ss->Default3D)
909 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
910 if (ss->DefaultCubeMap)
911 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
912 if (ss->DefaultRect)
913 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
914 if (ss)
915 _mesa_free(ss);
916 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000917}
918
Keith Whitwell6dc85572003-07-17 13:43:59 +0000919/**
jtgafb833d1999-08-19 00:55:39 +0000920 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000921 *
922 * \param ctx GL context.
923 * \param ss shared state pointer.
924 *
925 * Frees the display lists, the texture objects (calling the driver texture
926 * deletion callback to free its private data) and the vertex programs, as well
927 * as their hash tables.
928 *
929 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000930 */
Brian Paul178a1c52000-04-22 01:05:00 +0000931static void
932free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000933{
934 /* Free display lists */
935 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000936 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000937 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000938 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000939 }
940 else {
941 break;
942 }
943 }
Brian Paulbb797902000-01-24 16:19:54 +0000944 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000945
946 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000947 ASSERT(ctx->Driver.DeleteTexture);
948 while (1) {
949 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
950 if (texName) {
951 struct gl_texture_object *texObj = (struct gl_texture_object *)
952 _mesa_HashLookup(ss->TexObjects, texName);
953 ASSERT(texObj);
954 (*ctx->Driver.DeleteTexture)(ctx, texObj);
955 _mesa_HashRemove(ss->TexObjects, texName);
956 }
957 else {
958 break;
959 }
jtgafb833d1999-08-19 00:55:39 +0000960 }
Brian Paulbb797902000-01-24 16:19:54 +0000961 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000962
Brian Paul8dfc5b92002-10-16 17:57:51 +0000963#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +0000964 /* Free vertex programs */
965 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +0000966 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +0000967 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +0000968 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
969 prog);
970 ASSERT(p);
971 _mesa_delete_program(ctx, p);
972 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +0000973 }
974 else {
975 break;
976 }
977 }
Brian Paul610d5992003-01-14 04:55:45 +0000978 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000979#endif
Brian Paul30f51ae2001-12-18 04:06:44 +0000980
Ian Romanick0207b472003-09-09 00:10:12 +0000981 _mesa_DeleteHashTable(ss->BufferObjects);
982
Keith Whitwelle15fd852002-12-12 13:03:15 +0000983 _glthread_DESTROY_MUTEX(ss->Mutex);
984
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000985 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000986}
987
988
Keith Whitwell6dc85572003-07-17 13:43:59 +0000989static void _mesa_init_current( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000990{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000991 int i;
jtgafb833d1999-08-19 00:55:39 +0000992
Keith Whitwell6dc85572003-07-17 13:43:59 +0000993 /* Current group */
994 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
995 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +0000996 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000997 /* special cases: */
998 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
999 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
1000 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
1001 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 0.0 );
1002 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
1003 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
1004 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i], 0.0, 0.0, 0.0, 1.0);
1005 ctx->Current.Index = 1;
1006 ctx->Current.EdgeFlag = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001007}
1008
1009
Keith Whitwell6dc85572003-07-17 13:43:59 +00001010static void
1011_mesa_init_constants( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001012{
Brian Paul4d053dd2000-01-14 04:45:47 +00001013 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +00001014
Brian Paulcd1cefa2001-06-13 14:56:14 +00001015 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1016 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1017
Brian Paul539cce52000-02-03 19:40:07 +00001018 /* Constants, may be overriden by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +00001019 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
Brian Paulcd1cefa2001-06-13 14:56:14 +00001020 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1021 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
Brian Paul8afe7de2002-06-15 03:03:06 +00001022 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001023 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
Brian Paul610d5992003-01-14 04:55:45 +00001024 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1025 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
Gareth Hughes2c3d34c2001-03-18 08:53:49 +00001026 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
Brian Paul87c964d2001-11-06 15:53:00 +00001027 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
Brian Paul4d053dd2000-01-14 04:45:47 +00001028 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +00001029 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1030 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1031 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1032 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1033 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001034 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001035 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1036 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1037 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1038 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001039 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001040 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
Brian Paul4bdcfe52000-04-17 17:57:04 +00001041 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +00001042 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1043 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
Brian Paula8644322000-11-27 18:22:13 +00001044 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1045 ctx->Const.MaxLights = MAX_LIGHTS;
Ian Romanick882caa12003-05-30 21:37:14 +00001046 ctx->Const.MaxSpotExponent = 128.0;
1047 ctx->Const.MaxShininess = 128.0;
Brian Pauld0492cf2003-04-11 01:20:06 +00001048#if FEATURE_ARB_vertex_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001049 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001050 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1051 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1052 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1053 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1054 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001055#endif
1056#if FEATURE_ARB_fragment_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001057 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001058 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1059 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1060 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1061 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1062 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1063 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1064 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1065 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001066#endif
Brian Pauledd67742003-04-18 18:02:43 +00001067 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1068 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
Brian Pauld0492cf2003-04-11 01:20:06 +00001069
Brian Paul92f97852003-05-01 22:44:02 +00001070 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
Keith Whitwell6dc85572003-07-17 13:43:59 +00001071}
jtgafb833d1999-08-19 00:55:39 +00001072
Keith Whitwell6dc85572003-07-17 13:43:59 +00001073/**
1074 * Initialize the attribute groups in a GL context.
1075 *
1076 * \param ctx GL context.
1077 *
1078 * Initializes all the attributes, calling the respective <tt>init*</tt>
1079 * functions for the more complex data structures.
1080 */
1081static GLboolean
1082init_attrib_groups( GLcontext *ctx )
1083{
1084 assert(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001085
Keith Whitwell6dc85572003-07-17 13:43:59 +00001086 /* Constants */
1087 _mesa_init_constants( ctx );
Brian Paul0771d152000-04-07 00:19:41 +00001088
Brian Paul4d053dd2000-01-14 04:45:47 +00001089 /* Extensions */
Brian Paulde4f4602003-07-03 02:15:06 +00001090 _mesa_init_extensions( ctx );
jtgafb833d1999-08-19 00:55:39 +00001091
Keith Whitwell6dc85572003-07-17 13:43:59 +00001092 /* Attribute Groups */
1093 _mesa_init_accum( ctx );
1094 _mesa_init_attrib( ctx );
1095 _mesa_init_buffers( ctx );
Brian Paul148a2842003-09-17 03:40:11 +00001096 _mesa_init_buffer_objects( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001097 _mesa_init_color( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001098 _mesa_init_colortables( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001099 _mesa_init_current( ctx );
1100 _mesa_init_depth( ctx );
1101 _mesa_init_debug( ctx );
1102 _mesa_init_display_list( ctx );
1103 _mesa_init_eval( ctx );
1104 _mesa_init_feedback( ctx );
1105 _mesa_init_fog( ctx );
1106 _mesa_init_histogram( ctx );
1107 _mesa_init_hint( ctx );
1108 _mesa_init_line( ctx );
1109 _mesa_init_lighting( ctx );
1110 _mesa_init_matrix( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001111 _mesa_init_occlude( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001112 _mesa_init_pixel( ctx );
1113 _mesa_init_point( ctx );
1114 _mesa_init_polygon( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001115 _mesa_init_program( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001116 _mesa_init_rastpos( ctx );
1117 _mesa_init_stencil( ctx );
1118 _mesa_init_transform( ctx );
1119 _mesa_init_varray( ctx );
1120 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001121
Keith Whitwell6dc85572003-07-17 13:43:59 +00001122 if (!_mesa_init_texture( ctx ))
1123 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001124
Brian Paul4d053dd2000-01-14 04:45:47 +00001125 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001126 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001127 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paul4d053dd2000-01-14 04:45:47 +00001128 ctx->CatchSignals = GL_TRUE;
Brian Paulf782b812002-10-04 17:37:45 +00001129 ctx->_Facing = 0;
Brian Paul4d053dd2000-01-14 04:45:47 +00001130
Brian Paula3f13702003-04-01 16:41:50 +00001131 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001132}
1133
1134
Brian Paulf44898c2003-07-18 15:44:57 +00001135/**
1136 * If the DRI libGL.so library is old, it may not have the entrypoints for
1137 * some recent OpenGL extensions. Dynamically add them now.
1138 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1139 * and driver code, this won't be an issue (and calling this function won't
1140 * do any harm).
1141 */
1142static void
1143add_newer_entrypoints(void)
1144{
Ian Romanick3baefe62003-08-22 23:28:03 +00001145 unsigned i;
1146 static const struct {
1147 const char * const name;
1148 unsigned offset;
1149 }
1150 newer_entrypoints[] = {
1151 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1152 { "glWindowPos2dARB", 513 },
1153 { "glWindowPos2dvARB", 514 },
1154 { "glWindowPos2fARB", 515 },
1155 { "glWindowPos2fvARB", 516 },
1156 { "glWindowPos2iARB", 517 },
1157 { "glWindowPos2ivARB", 518 },
1158 { "glWindowPos2sARB", 519 },
1159 { "glWindowPos2svARB", 520 },
1160 { "glWindowPos3dARB", 521 },
1161 { "glWindowPos3dvARB", 522 },
1162 { "glWindowPos3fARB", 523 },
1163 { "glWindowPos3fvARB", 524 },
1164 { "glWindowPos3iARB", 525 },
1165 { "glWindowPos3ivARB", 526 },
1166 { "glWindowPos3sARB", 527 },
1167 { "glWindowPos3svARB", 528 },
Brian Paulf44898c2003-07-18 15:44:57 +00001168#if FEATURE_NV_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001169 { "glAreProgramsResidentNV", 578 },
1170 { "glBindProgramNV", 579 },
1171 { "glDeleteProgramsNV", 580 },
1172 { "glExecuteProgramNV", 581 },
1173 { "glGenProgramsNV", 582 },
1174 { "glGetProgramParameterdvNV", 583 },
1175 { "glGetProgramParameterfvNV", 584 },
1176 { "glGetProgramivNV", 585 },
1177 { "glGetProgramStringNV", 586 },
1178 { "glGetTrackMatrixivNV", 587 },
1179 { "glGetVertexAttribdvNV", 588 },
1180 { "glGetVertexAttribfvNV", 589 },
1181 { "glGetVertexAttribivNV", 590 },
1182 { "glGetVertexAttribPointervNV", 591 },
1183 { "glIsProgramNV", 592 },
1184 { "glLoadProgramNV", 593 },
1185 { "glProgramParameter4dNV", 594 },
1186 { "glProgramParameter4dvNV", 595 },
1187 { "glProgramParameter4fNV", 596 },
1188 { "glProgramParameter4fvNV", 597 },
1189 { "glProgramParameters4dvNV", 598 },
1190 { "glProgramParameters4fvNV", 599 },
1191 { "glRequestResidentProgramsNV", 600 },
1192 { "glTrackMatrixNV", 601 },
1193 { "glVertexAttribPointerNV", 602 },
1194 { "glVertexAttrib1dNV", 603 },
1195 { "glVertexAttrib1dvNV", 604 },
1196 { "glVertexAttrib1fNV", 605 },
1197 { "glVertexAttrib1fvNV", 606 },
1198 { "glVertexAttrib1sNV", 607 },
1199 { "glVertexAttrib1svNV", 608 },
1200 { "glVertexAttrib2dNV", 609 },
1201 { "glVertexAttrib2dvNV", 610 },
1202 { "glVertexAttrib2fNV", 611 },
1203 { "glVertexAttrib2fvNV", 612 },
1204 { "glVertexAttrib2sNV", 613 },
1205 { "glVertexAttrib2svNV", 614 },
1206 { "glVertexAttrib3dNV", 615 },
1207 { "glVertexAttrib3dvNV", 616 },
1208 { "glVertexAttrib3fNV", 617 },
1209 { "glVertexAttrib3fvNV", 618 },
1210 { "glVertexAttrib3sNV", 619 },
1211 { "glVertexAttrib3svNV", 620 },
1212 { "glVertexAttrib4dNV", 621 },
1213 { "glVertexAttrib4dvNV", 622 },
1214 { "glVertexAttrib4fNV", 623 },
1215 { "glVertexAttrib4fvNV", 624 },
1216 { "glVertexAttrib4sNV", 625 },
1217 { "glVertexAttrib4svNV", 626 },
1218 { "glVertexAttrib4ubNV", 627 },
1219 { "glVertexAttrib4ubvNV", 628 },
1220 { "glVertexAttribs1dvNV", 629 },
1221 { "glVertexAttribs1fvNV", 630 },
1222 { "glVertexAttribs1svNV", 631 },
1223 { "glVertexAttribs2dvNV", 632 },
1224 { "glVertexAttribs2fvNV", 633 },
1225 { "glVertexAttribs2svNV", 634 },
1226 { "glVertexAttribs3dvNV", 635 },
1227 { "glVertexAttribs3fvNV", 636 },
1228 { "glVertexAttribs3svNV", 637 },
1229 { "glVertexAttribs4dvNV", 638 },
1230 { "glVertexAttribs4fvNV", 639 },
1231 { "glVertexAttribs4svNV", 640 },
1232 { "glVertexAttribs4ubvNV", 641 },
Brian Paulf44898c2003-07-18 15:44:57 +00001233#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001234 { "glPointParameteriNV", 642 },
1235 { "glPointParameterivNV", 643 },
1236 { "glMultiDrawArraysEXT", 644 },
1237 { "glMultiDrawElementsEXT", 645 },
1238 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1239 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1240 { "glActiveStencilFaceEXT", 646 },
Brian Paulf44898c2003-07-18 15:44:57 +00001241#if FEATURE_NV_fence
Ian Romanick3baefe62003-08-22 23:28:03 +00001242 { "glDeleteFencesNV", 647 },
1243 { "glGenFencesNV", 648 },
1244 { "glIsFenceNV", 649 },
1245 { "glTestFenceNV", 650 },
1246 { "glGetFenceivNV", 651 },
1247 { "glFinishFenceNV", 652 },
1248 { "glSetFenceNV", 653 },
Brian Paulf44898c2003-07-18 15:44:57 +00001249#endif
1250#if FEATURE_NV_fragment_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001251 { "glProgramNamedParameter4fNV", 682 },
1252 { "glProgramNamedParameter4dNV", 683 },
1253 { "glProgramNamedParameter4fvNV", 683 },
1254 { "glProgramNamedParameter4dvNV", 684 },
1255 { "glGetProgramNamedParameterfvNV", 685 },
1256 { "glGetProgramNamedParameterdvNV", 686 },
Brian Paulf44898c2003-07-18 15:44:57 +00001257#endif
1258#if FEATURE_ARB_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001259 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1260 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1261 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1262 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1263 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1264 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1265 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1266 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1267 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1268 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1269 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1270 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1271 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1272 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1273 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1274 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1275 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1276 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1277 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1278 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1279 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1280 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1281 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1282 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1283 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1284 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1285 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1286 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1287 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1288 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1289 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1290 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1291 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1292 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1293 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1294 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1295 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1296 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1297 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1298 { "glProgramStringARB", _gloffset_ProgramStringARB },
1299 { "glBindProgramARB", _gloffset_BindProgramNV },
1300 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1301 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1302 { "glIsProgramARB", _gloffset_IsProgramNV },
1303 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1304 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1305 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1306 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1307 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1308 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1309 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1310 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1311 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1312 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1313 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1314 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1315 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1316 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1317 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1318 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1319 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1320 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
Brian Paulf44898c2003-07-18 15:44:57 +00001321#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001322 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1323 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
1324 };
1325
1326 for ( i = 0 ; i < Elements(newer_entrypoints) ; i++ ) {
1327 _glapi_add_entrypoint( newer_entrypoints[i].name,
1328 newer_entrypoints[i].offset );
1329 }
Brian Paulf44898c2003-07-18 15:44:57 +00001330}
1331
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001332
Brian Paulde4f4602003-07-03 02:15:06 +00001333/**
Keith Whitwell6dc85572003-07-17 13:43:59 +00001334 * Initialize a GLcontext struct.
1335 *
1336 * This includes allocating all the other structs and arrays which hang off of
1337 * the context by pointers.
1338 *
1339 * \sa _mesa_create_context() for the parameter description.
1340 *
1341 * Performs the imports and exports callback tables initialization, and
1342 * miscellaneous one-time initializations. If no shared context is supplied one
1343 * is allocated, and increase its reference count. Setups the GL API dispatch
1344 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1345 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1346 * for debug flags.
1347 *
Brian Paulde4f4602003-07-03 02:15:06 +00001348 * \note the direct parameter is ignored (obsolete).
jtgafb833d1999-08-19 00:55:39 +00001349 */
Brian Paul178a1c52000-04-22 01:05:00 +00001350GLboolean
1351_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001352 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001353 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001354 void *driver_ctx,
1355 GLboolean direct )
jtgafb833d1999-08-19 00:55:39 +00001356{
Brian Paul5fb84d22000-05-24 15:04:45 +00001357 GLuint dispatchSize;
1358
Brian Paul3c634522002-10-24 23:57:19 +00001359 ASSERT(driver_ctx);
jtgafb833d1999-08-19 00:55:39 +00001360
Brian Paul3c634522002-10-24 23:57:19 +00001361 /* If the driver wants core Mesa to use special imports, it'll have to
1362 * override these defaults.
1363 */
1364 _mesa_init_default_imports( &(ctx->imports), driver_ctx );
jtgafb833d1999-08-19 00:55:39 +00001365
Brian Paul9a33a112002-06-13 04:28:29 +00001366 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001367 _mesa_init_default_exports( &(ctx->exports) );
1368
1369 /* misc one-time initializations */
1370 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001371
Brian Paul3c634522002-10-24 23:57:19 +00001372 ctx->DriverCtx = driver_ctx;
Brian Paulb1394fa2000-09-26 20:53:53 +00001373 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001374 ctx->DrawBuffer = NULL;
1375 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001376
Brian Paula3f13702003-04-01 16:41:50 +00001377 /* Set these pointers to defaults now in case they're not set since
1378 * we need them while creating the default textures.
1379 */
1380 if (!ctx->Driver.NewTextureObject)
1381 ctx->Driver.NewTextureObject = _mesa_new_texture_object;
1382 if (!ctx->Driver.DeleteTexture)
1383 ctx->Driver.DeleteTexture = _mesa_delete_texture_object;
1384 if (!ctx->Driver.NewTextureImage)
1385 ctx->Driver.NewTextureImage = _mesa_new_texture_image;
1386
jtgafb833d1999-08-19 00:55:39 +00001387 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001388 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001389 ctx->Shared = share_list->Shared;
1390 }
1391 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001392 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001393 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001394 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001395 }
1396 }
Brian Paul9560f052000-01-31 23:11:39 +00001397 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001398 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001399 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001400
Keith Whitwell6dc85572003-07-17 13:43:59 +00001401 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001402 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001403 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001404 }
jtgafb833d1999-08-19 00:55:39 +00001405
Brian Paulf44898c2003-07-18 15:44:57 +00001406 /* libGL ABI coordination */
1407 add_newer_entrypoints();
Keith Whitwell6dc85572003-07-17 13:43:59 +00001408
Brian Paul5fb84d22000-05-24 15:04:45 +00001409 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1410 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1411 * Mesa we do this to accomodate different versions of libGL and various
1412 * DRI drivers.
1413 */
1414 dispatchSize = MAX2(_glapi_get_dispatch_table_size(),
1415 sizeof(struct _glapi_table) / sizeof(void *));
1416
Brian Paulfbd8f211999-11-11 01:22:25 +00001417 /* setup API dispatch tables */
Brian Paul5fb84d22000-05-24 15:04:45 +00001418 ctx->Exec = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
1419 ctx->Save = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001420 if (!ctx->Exec || !ctx->Save) {
1421 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001422 if (ctx->Exec)
Brian Paul2d8db392000-06-27 22:10:00 +00001423 FREE( ctx->Exec );
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001424 }
Brian Paul5fb84d22000-05-24 15:04:45 +00001425 _mesa_init_exec_table(ctx->Exec, dispatchSize);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001426 ctx->CurrentDispatch = ctx->Exec;
jtgafb833d1999-08-19 00:55:39 +00001427
Keith Whitwell6dc85572003-07-17 13:43:59 +00001428#if _HAVE_FULL_GL
1429 _mesa_init_dlist_table(ctx->Save, dispatchSize);
1430
Keith Whitwellad2ac212000-11-24 10:25:05 +00001431 ctx->ExecPrefersFloat = GL_FALSE;
1432 ctx->SavePrefersFloat = GL_FALSE;
1433
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001434 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001435 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001436 ctx->TnlModule.Current = NULL;
1437 ctx->TnlModule.SwapCount = 0;
1438
Keith Whitwell6dc85572003-07-17 13:43:59 +00001439#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001440
Brian Paul4d053dd2000-01-14 04:45:47 +00001441 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001442}
1443
Brian Paulde4f4602003-07-03 02:15:06 +00001444/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001445 * Allocate and initialize a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001446 *
1447 * \param visual a GLvisual pointer (we copy the struct contents)
1448 * \param share_list another context to share display lists with or NULL
1449 * \param driver_ctx pointer to device driver's context state struct
1450 * \param direct obsolete, ignored
1451 *
1452 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001453 */
Brian Paul178a1c52000-04-22 01:05:00 +00001454GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001455_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001456 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001457 void *driver_ctx,
1458 GLboolean direct )
1459
Brian Paul4d053dd2000-01-14 04:45:47 +00001460{
Brian Paul4753d602002-06-15 02:38:15 +00001461 GLcontext *ctx;
1462
1463 ASSERT(visual);
Brian Paul3c634522002-10-24 23:57:19 +00001464 ASSERT(driver_ctx);
Brian Paul4753d602002-06-15 02:38:15 +00001465
Brian Paul3c634522002-10-24 23:57:19 +00001466 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001467 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001468 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001469
Brian Paul3c634522002-10-24 23:57:19 +00001470 if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001471 return ctx;
1472 }
1473 else {
Brian Paul3c634522002-10-24 23:57:19 +00001474 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001475 return NULL;
1476 }
1477}
1478
Keith Whitwell6dc85572003-07-17 13:43:59 +00001479/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001480 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001481 *
1482 * But doesn't free the GLcontext struct itself.
1483 *
1484 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001485 */
Brian Paul178a1c52000-04-22 01:05:00 +00001486void
Brian Paulb1394fa2000-09-26 20:53:53 +00001487_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001488{
Brian Paul4d053dd2000-01-14 04:45:47 +00001489 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001490 if (ctx == _mesa_get_current_context()) {
1491 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001492 }
1493
Keith Whitwell6dc85572003-07-17 13:43:59 +00001494 _mesa_free_lighting_data( ctx );
1495 _mesa_free_eval_data( ctx );
1496 _mesa_free_texture_data( ctx );
1497 _mesa_free_matrix_data( ctx );
1498 _mesa_free_viewport_data( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001499 _mesa_free_colortables_data( ctx );
Brian Paul8dfc5b92002-10-16 17:57:51 +00001500#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +00001501 if (ctx->VertexProgram.Current) {
Brian Paul610d5992003-01-14 04:55:45 +00001502 ctx->VertexProgram.Current->Base.RefCount--;
1503 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001504 _mesa_delete_program(ctx, &(ctx->VertexProgram.Current->Base));
Brian Paul610d5992003-01-14 04:55:45 +00001505 }
1506#endif
1507#if FEATURE_NV_fragment_program
1508 if (ctx->FragmentProgram.Current) {
1509 ctx->FragmentProgram.Current->Base.RefCount--;
1510 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001511 _mesa_delete_program(ctx, &(ctx->FragmentProgram.Current->Base));
Brian Paulfd284452001-07-19 15:54:34 +00001512 }
Brian Paul8dfc5b92002-10-16 17:57:51 +00001513#endif
Brian Paulfd284452001-07-19 15:54:34 +00001514
Brian Paul30f51ae2001-12-18 04:06:44 +00001515 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001516 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001517 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001518 assert(ctx->Shared->RefCount >= 0);
1519 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1520 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001521 /* free shared state */
1522 free_shared_state( ctx, ctx->Shared );
1523 }
1524
Brian Paul702ca202003-07-18 15:22:16 +00001525 if (ctx->Extensions.String)
1526 FREE((void *) ctx->Extensions.String);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001527
1528 FREE(ctx->Exec);
1529 FREE(ctx->Save);
Brian Paul4d053dd2000-01-14 04:45:47 +00001530}
1531
Keith Whitwell6dc85572003-07-17 13:43:59 +00001532/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001533 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001534 *
1535 * \param ctx GL context.
1536 *
1537 * Calls _mesa_free_context_data() and free the structure.
jtgafb833d1999-08-19 00:55:39 +00001538 */
Brian Paul178a1c52000-04-22 01:05:00 +00001539void
Brian Paulb1394fa2000-09-26 20:53:53 +00001540_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001541{
1542 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001543 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001544 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001545 }
1546}
1547
Keith Whitwell6dc85572003-07-17 13:43:59 +00001548#if _HAVE_FULL_GL
1549/**
jtgafb833d1999-08-19 00:55:39 +00001550 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001551 *
1552 * \param src source context
1553 * \param dst destination context
1554 * \param mask bitwise OR of GL_*_BIT flags
1555 *
1556 * According to the bits specified in \p mask, copies the corresponding
1557 * attributes from \p src into \dst. For many of the attributes a simple \c
1558 * memcpy is not enough due to the existence of internal pointers in their data
1559 * structures.
jtgafb833d1999-08-19 00:55:39 +00001560 */
Brian Paul178a1c52000-04-22 01:05:00 +00001561void
Brian Paulb1394fa2000-09-26 20:53:53 +00001562_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001563{
1564 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001565 /* OK to memcpy */
1566 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001567 }
1568 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001569 /* OK to memcpy */
1570 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001571 }
1572 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001573 /* OK to memcpy */
1574 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001575 }
1576 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001577 /* OK to memcpy */
1578 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001579 }
1580 if (mask & GL_ENABLE_BIT) {
1581 /* no op */
1582 }
1583 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001584 /* OK to memcpy */
1585 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001586 }
1587 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001588 /* OK to memcpy */
1589 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001590 }
1591 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001592 /* OK to memcpy */
1593 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001594 }
1595 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001596 GLuint i;
1597 /* begin with memcpy */
1598 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1599 /* fixup linked lists to prevent pointer insanity */
1600 make_empty_list( &(dst->Light.EnabledList) );
1601 for (i = 0; i < MAX_LIGHTS; i++) {
1602 if (dst->Light.Light[i].Enabled) {
1603 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1604 }
1605 }
jtgafb833d1999-08-19 00:55:39 +00001606 }
1607 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001608 /* OK to memcpy */
1609 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001610 }
1611 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001612 /* OK to memcpy */
1613 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001614 }
1615 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001616 /* OK to memcpy */
1617 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001618 }
1619 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001620 /* OK to memcpy */
1621 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001622 }
1623 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001624 /* OK to memcpy */
1625 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001626 }
1627 if (mask & GL_POLYGON_STIPPLE_BIT) {
1628 /* Use loop instead of MEMCPY due to problem with Portland Group's
1629 * C compiler. Reported by John Stone.
1630 */
Brian Paul85d81602002-06-17 23:36:31 +00001631 GLuint i;
1632 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001633 dst->PolygonStipple[i] = src->PolygonStipple[i];
1634 }
1635 }
1636 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001637 /* OK to memcpy */
1638 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001639 }
1640 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001641 /* OK to memcpy */
1642 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001643 }
1644 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001645 /* Cannot memcpy because of pointers */
1646 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001647 }
1648 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001649 /* OK to memcpy */
1650 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001651 }
1652 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001653 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1654 dst->Viewport.X = src->Viewport.X;
1655 dst->Viewport.Y = src->Viewport.Y;
1656 dst->Viewport.Width = src->Viewport.Width;
1657 dst->Viewport.Height = src->Viewport.Height;
1658 dst->Viewport.Near = src->Viewport.Near;
1659 dst->Viewport.Far = src->Viewport.Far;
1660 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001661 }
Brian Paul85d81602002-06-17 23:36:31 +00001662
Keith Whitwella96308c2000-10-30 13:31:59 +00001663 /* XXX FIXME: Call callbacks?
1664 */
1665 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001666}
Keith Whitwell23caf202000-11-16 21:05:34 +00001667#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001668
1669
Brian Paulb1d53d92003-06-11 18:48:19 +00001670/**
1671 * Check if the given context can render into the given framebuffer
1672 * by checking visual attributes.
1673 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1674 */
1675static GLboolean
1676check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1677{
1678 const GLvisual *ctxvis = &ctx->Visual;
1679 const GLvisual *bufvis = &buffer->Visual;
1680
1681 if (ctxvis == bufvis)
1682 return GL_TRUE;
1683
1684 if (ctxvis->rgbMode != bufvis->rgbMode)
1685 return GL_FALSE;
1686 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1687 return GL_FALSE;
1688 if (ctxvis->stereoMode && !bufvis->stereoMode)
1689 return GL_FALSE;
1690 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1691 return GL_FALSE;
1692 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1693 return GL_FALSE;
1694 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1695 return GL_FALSE;
1696 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1697 return GL_FALSE;
1698 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1699 return GL_FALSE;
1700 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1701 return GL_FALSE;
1702 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1703 return GL_FALSE;
1704 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1705 return GL_FALSE;
1706
1707 return GL_TRUE;
1708}
1709
1710
Keith Whitwell6dc85572003-07-17 13:43:59 +00001711/**
Brian Paul9a33a112002-06-13 04:28:29 +00001712 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001713 *
1714 * \param newCtx new GL context.
1715 * \param buffer framebuffer.
1716 *
1717 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001718 */
1719void
1720_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1721{
1722 _mesa_make_current2( newCtx, buffer, buffer );
1723}
1724
Keith Whitwell6dc85572003-07-17 13:43:59 +00001725/**
1726 * Bind the given context to the given draw-buffer and read-buffer and
1727 * make it the current context for this thread.
1728 *
1729 * \param newCtx new GL context. If NULL then there will be no current GL
1730 * context.
1731 * \param drawBuffer draw framebuffer.
1732 * \param readBuffer read framebuffer.
1733 *
1734 * Check that the context's and framebuffer's visuals are compatible, returning
1735 * immediately otherwise. Sets the glapi current context via
1736 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1737 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1738 * Calls dd_function_table::MakeCurrent callback if defined.
1739 *
1740 * When a context is bound by the first time and the \c MESA_INFO environment
1741 * variable is set it calls print_info() as an aid for remote user
1742 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001743 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001744void
1745_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1746 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001747{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001748 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001749 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001750
Brian Paulbe3602d2001-02-28 00:27:48 +00001751 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001752 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001753 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1754 if (!check_compatible(newCtx, drawBuffer))
1755 return;
1756 }
1757 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1758 if (!check_compatible(newCtx, readBuffer))
1759 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001760 }
1761
Brian Paul00037781999-12-17 14:52:35 +00001762 /* We call this function periodically (just here for now) in
1763 * order to detect when multithreading has begun.
1764 */
1765 _glapi_check_multithread();
1766
Brian Paulf9b97d92000-01-28 20:17:42 +00001767 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001768 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001769
1770
1771 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001772 _glapi_set_dispatch(NULL); /* none current */
1773 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001774 else {
1775 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001776
Keith Whitwell23caf202000-11-16 21:05:34 +00001777 if (drawBuffer && readBuffer) {
1778 /* TODO: check if newCtx and buffer's visual match??? */
1779 newCtx->DrawBuffer = drawBuffer;
1780 newCtx->ReadBuffer = readBuffer;
1781 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001782
Keith Whitwell6dc85572003-07-17 13:43:59 +00001783#if _HAVE_FULL_GL
Brian Paul10d7f542002-06-17 23:38:14 +00001784 if (drawBuffer->Width == 0 && drawBuffer->Height == 0) {
1785 /* get initial window size */
1786 GLuint bufWidth, bufHeight;
1787
1788 /* ask device driver for size of output buffer */
1789 (*newCtx->Driver.GetBufferSize)( drawBuffer, &bufWidth, &bufHeight );
1790
Keith Whitwell6dc85572003-07-17 13:43:59 +00001791 if (drawBuffer->Width != bufWidth ||
1792 drawBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001793
Keith Whitwell6dc85572003-07-17 13:43:59 +00001794 drawBuffer->Width = bufWidth;
1795 drawBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001796
Keith Whitwell6dc85572003-07-17 13:43:59 +00001797 newCtx->Driver.ResizeBuffers( drawBuffer );
1798 }
Brian Paul10d7f542002-06-17 23:38:14 +00001799 }
1800
1801 if (readBuffer != drawBuffer &&
1802 readBuffer->Width == 0 && readBuffer->Height == 0) {
1803 /* get initial window size */
1804 GLuint bufWidth, bufHeight;
1805
1806 /* ask device driver for size of output buffer */
1807 (*newCtx->Driver.GetBufferSize)( readBuffer, &bufWidth, &bufHeight );
1808
Keith Whitwell6dc85572003-07-17 13:43:59 +00001809 if (readBuffer->Width != bufWidth ||
1810 readBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001811
Keith Whitwell6dc85572003-07-17 13:43:59 +00001812 readBuffer->Width = bufWidth;
1813 readBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001814
Keith Whitwell6dc85572003-07-17 13:43:59 +00001815 newCtx->Driver.ResizeBuffers( readBuffer );
1816 }
Brian Paul10d7f542002-06-17 23:38:14 +00001817 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001818#endif
Brian Paul00037781999-12-17 14:52:35 +00001819 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001820
Keith Whitwell6dc85572003-07-17 13:43:59 +00001821 /* Alert the driver - usually passed on to the sw t&l module,
1822 * but also used to detect threaded cases in the radeon codegen
1823 * hw t&l module.
1824 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001825 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001826 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1827
1828 /* We can use this to help debug user's problems. Tell them to set
1829 * the MESA_INFO env variable before running their app. Then the
1830 * first time each context is made current we'll print some useful
1831 * information.
1832 */
1833 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001834 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001835 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001836 }
1837 newCtx->FirstTimeCurrent = GL_FALSE;
1838 }
Brian Paul00037781999-12-17 14:52:35 +00001839 }
1840}
1841
Keith Whitwell6dc85572003-07-17 13:43:59 +00001842/**
1843 * Get current context for the calling thread.
1844 *
1845 * \return pointer to the current GL context.
1846 *
1847 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1848 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00001849 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001850GLcontext *
1851_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00001852{
Brian Paulf9b97d92000-01-28 20:17:42 +00001853 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001854}
1855
Keith Whitwell6dc85572003-07-17 13:43:59 +00001856/**
1857 * Get context's current API dispatch table.
1858 *
1859 * It'll either be the immediate-mode execute dispatcher or the display list
1860 * compile dispatcher.
1861 *
1862 * \param ctx GL context.
1863 *
1864 * \return pointer to dispatch_table.
1865 *
1866 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00001867 */
1868struct _glapi_table *
1869_mesa_get_dispatch(GLcontext *ctx)
1870{
1871 return ctx->CurrentDispatch;
1872}
1873
Keith Whitwell6dc85572003-07-17 13:43:59 +00001874/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00001875
1876
jtgafb833d1999-08-19 00:55:39 +00001877/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001878/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00001879/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001880/*@{*/
jtgafb833d1999-08-19 00:55:39 +00001881
Keith Whitwell6dc85572003-07-17 13:43:59 +00001882/**
1883 * Record an error.
1884 *
1885 * \param ctx GL context.
1886 * \param error error code.
1887 *
1888 * Records the given error code and call the driver's dd_function_table::Error
1889 * function if defined.
1890 *
1891 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00001892 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00001893 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001894void
Brian Paul4e9676f2002-06-29 19:48:15 +00001895_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00001896{
Brian Paul18a285a2002-03-16 00:53:15 +00001897 if (!ctx)
1898 return;
1899
Brian Paul7eb06032000-07-14 04:13:40 +00001900 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00001901 ctx->ErrorValue = error;
1902 }
1903
1904 /* Call device driver's error handler, if any. This is used on the Mac. */
1905 if (ctx->Driver.Error) {
1906 (*ctx->Driver.Error)( ctx );
1907 }
1908}
1909
Keith Whitwell6dc85572003-07-17 13:43:59 +00001910/**
1911 * Execute glFinish().
1912 *
1913 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1914 * dd_function_table::Finish driver callback, if not NULL.
1915 */
Brian Paulfa9df402000-02-02 19:16:46 +00001916void
1917_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00001918{
Brian Paulfa9df402000-02-02 19:16:46 +00001919 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001920 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001921 if (ctx->Driver.Finish) {
1922 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001923 }
1924}
1925
Keith Whitwell6dc85572003-07-17 13:43:59 +00001926/**
1927 * Execute glFlush().
1928 *
1929 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1930 * dd_function_table::Flush driver callback, if not NULL.
1931 */
Brian Paulfa9df402000-02-02 19:16:46 +00001932void
1933_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00001934{
Brian Paulfa9df402000-02-02 19:16:46 +00001935 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001936 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001937 if (ctx->Driver.Flush) {
1938 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001939 }
jtgafb833d1999-08-19 00:55:39 +00001940}
Brian Paul48c6a6e2000-09-08 21:28:04 +00001941
1942
Keith Whitwell6dc85572003-07-17 13:43:59 +00001943/*@}*/