blob: 51abf86072aeeaf4d0699b6637d611edf3d77236 [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 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000059 * Selecting <b>Data Structures</b> will list all C structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +000060 *
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 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000065 * Selecting the <b>Data Fields</b> link will display a list of all
Keith Whitwell6dc85572003-07-17 13:43:59 +000066 * documented structure members.
67 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000068 * Selecting the <b>Globals</b> link will display a list
Keith Whitwell6dc85572003-07-17 13:43:59 +000069 * 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"
Brian Paul31732772003-09-18 16:39:09 +0000105#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
106#include "program.h"
107#endif
Keith Whitwell6dc85572003-07-17 13:43:59 +0000108#include "rastpos.h"
jtgafb833d1999-08-19 00:55:39 +0000109#include "simple_list.h"
Brian Paulfa9df402000-02-02 19:16:46 +0000110#include "state.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000111#include "stencil.h"
jtgafb833d1999-08-19 00:55:39 +0000112#include "teximage.h"
113#include "texobj.h"
Brian Paul85d81602002-06-17 23:36:31 +0000114#include "texstate.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000115#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +0000116#include "varray.h"
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
davem69775355a2001-06-05 23:54:00 +0000125#ifdef USE_SPARC_ASM
126#include "SPARC/sparc.h"
127#endif
jtgafb833d1999-08-19 00:55:39 +0000128
Keith Whitwell23caf202000-11-16 21:05:34 +0000129#ifndef MESA_VERBOSE
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000130int MESA_VERBOSE = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000131#endif
132
133#ifndef MESA_DEBUG_FLAGS
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000134int MESA_DEBUG_FLAGS = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000135#endif
Brian Paulb1394fa2000-09-26 20:53:53 +0000136
Brian Paul86b84272001-12-14 02:50:01 +0000137
Brian Paul27558a12003-03-01 01:50:20 +0000138/* ubyte -> float conversion */
139GLfloat _mesa_ubyte_to_float_color_tab[256];
140
Brian Paul9a33a112002-06-13 04:28:29 +0000141static void
142free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
143
Brian Paul86b84272001-12-14 02:50:01 +0000144
Brian Paulb1394fa2000-09-26 20:53:53 +0000145/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000146/** \name OpenGL SI-style interface (new in Mesa 3.5)
147 *
148 * \if subset
149 * \note Most of these functions are never called in the Mesa subset.
150 * \endif
151 */
152/*@{*/
Brian Paulb1394fa2000-09-26 20:53:53 +0000153
Keith Whitwell6dc85572003-07-17 13:43:59 +0000154/**
155 * Destroy context callback.
156 *
157 * \param gc context.
158 * \return GL_TRUE on success, or GL_FALSE on failure.
159 *
160 * \ifnot subset
161 * Called by window system/device driver (via __GLexports::destroyCurrent) when
162 * the rendering context is to be destroyed.
163 * \endif
164 *
165 * Frees the context data and the context structure.
Brian Paul9a33a112002-06-13 04:28:29 +0000166 */
167GLboolean
168_mesa_destroyContext(__GLcontext *gc)
Brian Paulb1394fa2000-09-26 20:53:53 +0000169{
170 if (gc) {
171 _mesa_free_context_data(gc);
Brian Paul3c634522002-10-24 23:57:19 +0000172 _mesa_free(gc);
Brian Paulb1394fa2000-09-26 20:53:53 +0000173 }
174 return GL_TRUE;
175}
176
Keith Whitwell6dc85572003-07-17 13:43:59 +0000177/**
178 * Unbind context callback.
179 *
180 * \param gc context.
181 * \return GL_TRUE on success, or GL_FALSE on failure.
182 *
183 * \ifnot subset
184 * Called by window system/device driver (via __GLexports::loseCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000185 * when the rendering context is made non-current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000186 * \endif
187 *
188 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000189 */
190GLboolean
191_mesa_loseCurrent(__GLcontext *gc)
192{
193 /* XXX unbind context from thread */
194 return GL_TRUE;
195}
196
Keith Whitwell6dc85572003-07-17 13:43:59 +0000197/**
198 * Bind context callback.
199 *
200 * \param gc context.
201 * \return GL_TRUE on success, or GL_FALSE on failure.
202 *
203 * \ifnot subset
204 * Called by window system/device driver (via __GLexports::makeCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000205 * when the rendering context is made current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000206 * \endif
207 *
208 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000209 */
210GLboolean
211_mesa_makeCurrent(__GLcontext *gc)
212{
213 /* XXX bind context to thread */
214 return GL_TRUE;
215}
216
Keith Whitwell6dc85572003-07-17 13:43:59 +0000217/**
218 * Share context callback.
219 *
220 * \param gc context.
221 * \param gcShare shared context.
222 * \return GL_TRUE on success, or GL_FALSE on failure.
223 *
224 * \ifnot subset
225 * Called by window system/device driver (via __GLexports::shareContext)
226 * \endif
227 *
228 * Update the shared context reference count, gl_shared_state::RefCount.
Brian Paul9a33a112002-06-13 04:28:29 +0000229 */
230GLboolean
231_mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
232{
233 if (gc && gcShare && gc->Shared && gcShare->Shared) {
234 gc->Shared->RefCount--;
235 if (gc->Shared->RefCount == 0) {
236 free_shared_state(gc, gc->Shared);
237 }
238 gc->Shared = gcShare->Shared;
239 gc->Shared->RefCount++;
240 return GL_TRUE;
241 }
242 else {
243 return GL_FALSE;
244 }
245}
246
Keith Whitwell6dc85572003-07-17 13:43:59 +0000247
248#if _HAVE_FULL_GL
249/**
250 * Copy context callback.
251 */
Brian Paul9a33a112002-06-13 04:28:29 +0000252GLboolean
253_mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
254{
255 if (dst && src) {
256 _mesa_copy_context( src, dst, mask );
257 return GL_TRUE;
258 }
259 else {
260 return GL_FALSE;
261 }
262}
Keith Whitwell6dc85572003-07-17 13:43:59 +0000263#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000264
Keith Whitwell6dc85572003-07-17 13:43:59 +0000265/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000266GLboolean
267_mesa_forceCurrent(__GLcontext *gc)
268{
269 return GL_TRUE;
270}
271
Keith Whitwell6dc85572003-07-17 13:43:59 +0000272/**
273 * Windows/buffer resizing notification callback.
274 *
275 * \param gc GL context.
276 * \return GL_TRUE on success, or GL_FALSE on failure.
277 */
Brian Paul9a33a112002-06-13 04:28:29 +0000278GLboolean
279_mesa_notifyResize(__GLcontext *gc)
280{
281 GLint x, y;
282 GLuint width, height;
283 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
284 if (!d || !d->getDrawableSize)
285 return GL_FALSE;
286 d->getDrawableSize( d, &x, &y, &width, &height );
287 /* update viewport, resize software buffers, etc. */
288 return GL_TRUE;
289}
290
Keith Whitwell6dc85572003-07-17 13:43:59 +0000291/**
292 * Window/buffer destruction notification callback.
293 *
294 * \param gc GL context.
295 *
296 * Called when the context's window/buffer is going to be destroyed.
297 *
298 * No-op
299 */
Brian Paul9a33a112002-06-13 04:28:29 +0000300void
301_mesa_notifyDestroy(__GLcontext *gc)
302{
Brian Paul60b6e4f2002-10-14 17:08:17 +0000303 /* Unbind from it. */
Brian Paul9a33a112002-06-13 04:28:29 +0000304}
305
Keith Whitwell6dc85572003-07-17 13:43:59 +0000306/**
307 * Swap buffers notification callback.
308 *
309 * \param gc GL context.
310 *
311 * Called by window system just before swapping buffers.
Brian Paul9a33a112002-06-13 04:28:29 +0000312 * We have to finish any pending rendering.
313 */
314void
315_mesa_notifySwapBuffers(__GLcontext *gc)
316{
317 FLUSH_VERTICES( gc, 0 );
318}
319
Keith Whitwell6dc85572003-07-17 13:43:59 +0000320/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000321struct __GLdispatchStateRec *
322_mesa_dispatchExec(__GLcontext *gc)
323{
324 return NULL;
325}
326
Keith Whitwell6dc85572003-07-17 13:43:59 +0000327/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000328void
329_mesa_beginDispatchOverride(__GLcontext *gc)
330{
331}
332
Keith Whitwell6dc85572003-07-17 13:43:59 +0000333/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000334void
335_mesa_endDispatchOverride(__GLcontext *gc)
336{
337}
338
Keith Whitwell6dc85572003-07-17 13:43:59 +0000339/**
340 * \ifnot subset
341 * Setup the exports.
342 *
343 * The window system will call these functions when it needs Mesa to do
344 * something.
345 *
346 * \note Device drivers should override these functions! For example,
Brian Paul9a33a112002-06-13 04:28:29 +0000347 * the Xlib driver should plug in the XMesa*-style functions into this
348 * structure. The XMesa-style functions should then call the _mesa_*
349 * version of these functions. This is an approximation to OO design
350 * (inheritance and virtual functions).
Keith Whitwell6dc85572003-07-17 13:43:59 +0000351 * \endif
352 *
353 * \if subset
354 * No-op.
355 *
356 * \endif
Brian Paul9a33a112002-06-13 04:28:29 +0000357 */
358static void
359_mesa_init_default_exports(__GLexports *exports)
360{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000361#if _HAVE_FULL_GL
Brian Paul9a33a112002-06-13 04:28:29 +0000362 exports->destroyContext = _mesa_destroyContext;
363 exports->loseCurrent = _mesa_loseCurrent;
364 exports->makeCurrent = _mesa_makeCurrent;
365 exports->shareContext = _mesa_shareContext;
366 exports->copyContext = _mesa_copyContext;
367 exports->forceCurrent = _mesa_forceCurrent;
368 exports->notifyResize = _mesa_notifyResize;
Brian Paul2f35d5e2002-06-13 04:31:09 +0000369 exports->notifyDestroy = _mesa_notifyDestroy;
Brian Paul9a33a112002-06-13 04:28:29 +0000370 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
371 exports->dispatchExec = _mesa_dispatchExec;
372 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
373 exports->endDispatchOverride = _mesa_endDispatchOverride;
Keith Whitwell6dc85572003-07-17 13:43:59 +0000374#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000375}
376
Keith Whitwell6dc85572003-07-17 13:43:59 +0000377/**
378 * Exported OpenGL SI interface.
379 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000380__GLcontext *
381__glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
382{
383 GLcontext *ctx;
384
Brian Paul4753d602002-06-15 02:38:15 +0000385 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
Brian Paulb1394fa2000-09-26 20:53:53 +0000386 if (ctx == NULL) {
387 return NULL;
388 }
Brian Paul60b6e4f2002-10-14 17:08:17 +0000389
Brian Paul3c634522002-10-24 23:57:19 +0000390 _mesa_initialize_context(ctx, modes, NULL, imports, GL_FALSE);
Brian Paulb1394fa2000-09-26 20:53:53 +0000391 ctx->imports = *imports;
Brian Paulb1394fa2000-09-26 20:53:53 +0000392
393 return ctx;
394}
395
Keith Whitwell6dc85572003-07-17 13:43:59 +0000396/**
397 * Exported OpenGL SI interface.
398 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000399void
400__glCoreNopDispatch(void)
401{
402#if 0
403 /* SI */
404 __gl_dispatch = __glNopDispatchState;
405#else
406 /* Mesa */
407 _glapi_set_dispatch(NULL);
408#endif
409}
410
Keith Whitwell6dc85572003-07-17 13:43:59 +0000411/*@}*/
412
Brian Paulb1394fa2000-09-26 20:53:53 +0000413
jtgafb833d1999-08-19 00:55:39 +0000414/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000415/** \name GL Visual allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000416/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000417/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000418
Keith Whitwell6dc85572003-07-17 13:43:59 +0000419/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000420 * Allocate a new GLvisual object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000421 *
422 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
423 * \param dbFlag double buffering
424 * \param stereoFlag stereo buffer
425 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
426 * is acceptable but the actual depth type will be GLushort or GLuint as
427 * needed.
428 * \param stencilBits requested minimum bits per stencil buffer value
429 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
430 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
431 * \param redBits number of bits per color component in frame buffer for RGB(A)
432 * mode. We always use 8 in core Mesa though.
433 * \param greenBits same as above.
434 * \param blueBits same as above.
435 * \param alphaBits same as above.
436 * \param numSamples not really used.
437 *
438 * \return pointer to new GLvisual or NULL if requested parameters can't be
439 * met.
440 *
441 * Allocates a GLvisual structure and initializes it via
442 * _mesa_initialize_visual().
Brian Paul4d053dd2000-01-14 04:45:47 +0000443 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000444GLvisual *
445_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000446 GLboolean dbFlag,
447 GLboolean stereoFlag,
448 GLint redBits,
449 GLint greenBits,
450 GLint blueBits,
451 GLint alphaBits,
452 GLint indexBits,
453 GLint depthBits,
454 GLint stencilBits,
455 GLint accumRedBits,
456 GLint accumGreenBits,
457 GLint accumBlueBits,
458 GLint accumAlphaBits,
459 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000460{
Brian Paul178a1c52000-04-22 01:05:00 +0000461 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
462 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000463 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000464 redBits, greenBits, blueBits, alphaBits,
465 indexBits, depthBits, stencilBits,
466 accumRedBits, accumGreenBits,
467 accumBlueBits, accumAlphaBits,
Brian Paulb1394fa2000-09-26 20:53:53 +0000468 numSamples)) {
Brian Paul178a1c52000-04-22 01:05:00 +0000469 FREE(vis);
470 return NULL;
471 }
472 }
473 return vis;
474}
475
Keith Whitwell6dc85572003-07-17 13:43:59 +0000476/**
Brian Paul178a1c52000-04-22 01:05:00 +0000477 * Initialize the fields of the given GLvisual.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000478 *
479 * \return GL_TRUE on success, or GL_FALSE on failure.
480 *
481 * \sa _mesa_create_visual() above for the parameter description.
482 *
483 * Makes some sanity checks and fills in the fields of the
484 * GLvisual structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000485 */
486GLboolean
487_mesa_initialize_visual( GLvisual *vis,
488 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000489 GLboolean dbFlag,
490 GLboolean stereoFlag,
491 GLint redBits,
492 GLint greenBits,
493 GLint blueBits,
494 GLint alphaBits,
495 GLint indexBits,
496 GLint depthBits,
497 GLint stencilBits,
498 GLint accumRedBits,
499 GLint accumGreenBits,
500 GLint accumBlueBits,
501 GLint accumAlphaBits,
502 GLint numSamples )
503{
Brian Paulb6bcae52001-01-23 23:39:36 +0000504 (void) numSamples;
505
Brian Paul178a1c52000-04-22 01:05:00 +0000506 assert(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000507
Brian Pauled30dfa2000-03-03 17:47:39 +0000508 /* This is to catch bad values from device drivers not updated for
509 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
510 * bad value now (a 1-bit depth buffer!?!).
511 */
512 assert(depthBits == 0 || depthBits > 1);
513
514 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000515 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000516 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000517 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000518 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000519 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000520 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000521 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000522 }
523 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000524 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000525 }
526 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000527 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000528 }
529 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000530 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000531 }
532
Brian Paulb6bcae52001-01-23 23:39:36 +0000533 vis->rgbMode = rgbFlag;
534 vis->doubleBufferMode = dbFlag;
535 vis->stereoMode = stereoFlag;
Brian Paul153f1542002-10-29 15:04:35 +0000536
Brian Paulb6bcae52001-01-23 23:39:36 +0000537 vis->redBits = redBits;
538 vis->greenBits = greenBits;
539 vis->blueBits = blueBits;
540 vis->alphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000541
Brian Paulb6bcae52001-01-23 23:39:36 +0000542 vis->indexBits = indexBits;
543 vis->depthBits = depthBits;
544 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
545 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
546 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
547 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
548 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Pauled30dfa2000-03-03 17:47:39 +0000549
Brian Paul153f1542002-10-29 15:04:35 +0000550 vis->haveAccumBuffer = accumRedBits > 0;
551 vis->haveDepthBuffer = depthBits > 0;
552 vis->haveStencilBuffer = stencilBits > 0;
553
554 vis->numAuxBuffers = 0;
555 vis->level = 0;
556 vis->pixmapMode = 0;
557
Brian Paul178a1c52000-04-22 01:05:00 +0000558 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000559}
560
Keith Whitwell6dc85572003-07-17 13:43:59 +0000561/**
562 * Destroy a visual.
563 *
564 * \param vis visual.
565 *
566 * Frees the visual structure.
567 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000568void
569_mesa_destroy_visual( GLvisual *vis )
570{
571 FREE(vis);
572}
573
Keith Whitwell6dc85572003-07-17 13:43:59 +0000574/*@}*/
575
Brian Paulb371e0d2000-03-31 01:05:51 +0000576
Brian Paul4d053dd2000-01-14 04:45:47 +0000577/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000578/** \name GL Framebuffer allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000579/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000580/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000581
Keith Whitwell6dc85572003-07-17 13:43:59 +0000582/**
583 * Create a new framebuffer.
584 *
585 * A GLframebuffer is a structure which encapsulates the depth, stencil and
586 * accum buffers and related parameters.
587 *
588 * \param visual a GLvisual pointer (we copy the struct contents)
589 * \param softwareDepth create/use a software depth buffer?
590 * \param softwareStencil create/use a software stencil buffer?
591 * \param softwareAccum create/use a software accum buffer?
592 * \param softwareAlpha create/use a software alpha buffer?
593 *
594 * \return pointer to new GLframebuffer struct or NULL if error.
595 *
596 * Allocate a GLframebuffer structure and initializes it via
597 * _mesa_initialize_framebuffer().
Brian Paul4d053dd2000-01-14 04:45:47 +0000598 */
Brian Paul178a1c52000-04-22 01:05:00 +0000599GLframebuffer *
Brian Paulbe3602d2001-02-28 00:27:48 +0000600_mesa_create_framebuffer( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +0000601 GLboolean softwareDepth,
602 GLboolean softwareStencil,
603 GLboolean softwareAccum,
604 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000605{
Brian Paul178a1c52000-04-22 01:05:00 +0000606 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
607 assert(visual);
608 if (buffer) {
609 _mesa_initialize_framebuffer(buffer, visual,
610 softwareDepth, softwareStencil,
611 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000612 }
Brian Paul178a1c52000-04-22 01:05:00 +0000613 return buffer;
614}
615
Keith Whitwell6dc85572003-07-17 13:43:59 +0000616/**
Brian Paul178a1c52000-04-22 01:05:00 +0000617 * Initialize a GLframebuffer object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000618 *
619 * \sa _mesa_create_framebuffer() above for the parameter description.
620 *
621 * Makes some sanity checks and fills in the fields of the
622 * GLframebuffer structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000623 */
624void
625_mesa_initialize_framebuffer( GLframebuffer *buffer,
Brian Paulbe3602d2001-02-28 00:27:48 +0000626 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +0000627 GLboolean softwareDepth,
628 GLboolean softwareStencil,
629 GLboolean softwareAccum,
630 GLboolean softwareAlpha )
631{
632 assert(buffer);
633 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000634
Brian Paul6ec6b842002-10-30 19:49:29 +0000635 _mesa_bzero(buffer, sizeof(GLframebuffer));
Brian Paul85d81602002-06-17 23:36:31 +0000636
Brian Paul4d053dd2000-01-14 04:45:47 +0000637 /* sanity checks */
638 if (softwareDepth ) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000639 assert(visual->depthBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000640 }
641 if (softwareStencil) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000642 assert(visual->stencilBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000643 }
644 if (softwareAccum) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000645 assert(visual->rgbMode);
646 assert(visual->accumRedBits > 0);
647 assert(visual->accumGreenBits > 0);
648 assert(visual->accumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000649 }
650 if (softwareAlpha) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000651 assert(visual->rgbMode);
652 assert(visual->alphaBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000653 }
654
Brian Paul75978bd2001-04-27 21:17:20 +0000655 buffer->Visual = *visual;
Brian Paul4d053dd2000-01-14 04:45:47 +0000656 buffer->UseSoftwareDepthBuffer = softwareDepth;
657 buffer->UseSoftwareStencilBuffer = softwareStencil;
658 buffer->UseSoftwareAccumBuffer = softwareAccum;
659 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul4d053dd2000-01-14 04:45:47 +0000660}
661
Keith Whitwell6dc85572003-07-17 13:43:59 +0000662/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000663 * Free a framebuffer struct and its buffers.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000664 *
665 * Calls _mesa_free_framebuffer_data() and frees the structure.
Brian Paul4d053dd2000-01-14 04:45:47 +0000666 */
Brian Paul178a1c52000-04-22 01:05:00 +0000667void
Brian Paulb1394fa2000-09-26 20:53:53 +0000668_mesa_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000669{
670 if (buffer) {
Brian Paul75978bd2001-04-27 21:17:20 +0000671 _mesa_free_framebuffer_data(buffer);
Brian Paul4d053dd2000-01-14 04:45:47 +0000672 FREE(buffer);
673 }
674}
675
Keith Whitwell6dc85572003-07-17 13:43:59 +0000676/**
677 * Free the data hanging off of \p buffer, but not \p buffer itself.
678 *
679 * \param buffer framebuffer.
680 *
681 * Frees all the buffers associated with the structure.
Brian Paul75978bd2001-04-27 21:17:20 +0000682 */
683void
684_mesa_free_framebuffer_data( GLframebuffer *buffer )
685{
686 if (!buffer)
687 return;
688
Brian Paul87506682003-05-27 15:20:43 +0000689 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
Brian Paulaeb44342002-03-19 16:47:04 +0000690 MESA_PBUFFER_FREE( buffer->DepthBuffer );
Brian Paul75978bd2001-04-27 21:17:20 +0000691 buffer->DepthBuffer = NULL;
692 }
Brian Paul87506682003-05-27 15:20:43 +0000693 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
Brian Paulaeb44342002-03-19 16:47:04 +0000694 MESA_PBUFFER_FREE( buffer->Accum );
Brian Paul75978bd2001-04-27 21:17:20 +0000695 buffer->Accum = NULL;
696 }
Brian Paul87506682003-05-27 15:20:43 +0000697 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
Brian Paulaeb44342002-03-19 16:47:04 +0000698 MESA_PBUFFER_FREE( buffer->Stencil );
Brian Paul75978bd2001-04-27 21:17:20 +0000699 buffer->Stencil = NULL;
700 }
Brian Paul87506682003-05-27 15:20:43 +0000701 if (buffer->UseSoftwareAlphaBuffers){
702 if (buffer->FrontLeftAlpha) {
703 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
704 buffer->FrontLeftAlpha = NULL;
705 }
706 if (buffer->BackLeftAlpha) {
707 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
708 buffer->BackLeftAlpha = NULL;
709 }
710 if (buffer->FrontRightAlpha) {
711 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
712 buffer->FrontRightAlpha = NULL;
713 }
714 if (buffer->BackRightAlpha) {
715 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
716 buffer->BackRightAlpha = NULL;
717 }
Brian Paul75978bd2001-04-27 21:17:20 +0000718 }
719}
720
Keith Whitwell6dc85572003-07-17 13:43:59 +0000721/*@}*/
Brian Paul75978bd2001-04-27 21:17:20 +0000722
Brian Paul4d053dd2000-01-14 04:45:47 +0000723
724/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000725/** \name Context allocation, initialization, destroying
726 *
727 * The purpose of the most initialization functions here is to provide the
728 * default state values according to the OpenGL specification.
729 */
jtgafb833d1999-08-19 00:55:39 +0000730/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000731/*@{*/
jtgafb833d1999-08-19 00:55:39 +0000732
Keith Whitwell6dc85572003-07-17 13:43:59 +0000733/**
734 * One-time initialization mutex lock.
735 *
736 * \sa Used by one_time_init().
737 */
Brian Paul9560f052000-01-31 23:11:39 +0000738_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
739
Keith Whitwell6dc85572003-07-17 13:43:59 +0000740/**
741 * Calls all the various one-time-init functions in Mesa.
742 *
743 * While holding a global mutex lock, calls several initialization functions,
744 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
745 * defined.
746 *
747 * \sa _mesa_init_lists(), _math_init().
jtgafb833d1999-08-19 00:55:39 +0000748 */
Brian Paul178a1c52000-04-22 01:05:00 +0000749static void
Brian Paul4753d602002-06-15 02:38:15 +0000750one_time_init( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000751{
752 static GLboolean alreadyCalled = GL_FALSE;
Brian Paul9560f052000-01-31 23:11:39 +0000753 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000754 if (!alreadyCalled) {
Brian Paul27558a12003-03-01 01:50:20 +0000755 GLuint i;
756
Brian Paul4d053dd2000-01-14 04:45:47 +0000757 /* do some implementation tests */
758 assert( sizeof(GLbyte) == 1 );
759 assert( sizeof(GLshort) >= 2 );
760 assert( sizeof(GLint) >= 4 );
761 assert( sizeof(GLubyte) == 1 );
762 assert( sizeof(GLushort) >= 2 );
763 assert( sizeof(GLuint) >= 4 );
764
Brian Paul08836342001-03-03 20:33:27 +0000765 _mesa_init_lists();
Keith Whitwell23caf202000-11-16 21:05:34 +0000766
Keith Whitwell6dc85572003-07-17 13:43:59 +0000767#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000768 _math_init();
Brian Paul27558a12003-03-01 01:50:20 +0000769
770 for (i = 0; i < 256; i++) {
771 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
772 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000773#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000774
davem69775355a2001-06-05 23:54:00 +0000775#ifdef USE_SPARC_ASM
776 _mesa_init_sparc_glapi_relocs();
777#endif
Brian Paul3c634522002-10-24 23:57:19 +0000778 if (_mesa_getenv("MESA_DEBUG")) {
Brian Paul68ee4bc2000-01-28 19:02:22 +0000779 _glapi_noop_enable_warnings(GL_TRUE);
Brian Paul71072be2002-10-10 00:22:13 +0000780#ifndef GLX_DIRECT_RENDERING
781 /* libGL from before 2002/06/28 don't have this function. Someday,
782 * when newer libGL libs are common, remove the #ifdef test. This
783 * only serves to print warnings when calling undefined GL functions.
784 */
Brian Paul4e9676f2002-06-29 19:48:15 +0000785 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
Brian Paul71072be2002-10-10 00:22:13 +0000786#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000787 }
788 else {
789 _glapi_noop_enable_warnings(GL_FALSE);
790 }
791
jtgafb833d1999-08-19 00:55:39 +0000792#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
Brian Paul4e9676f2002-06-29 19:48:15 +0000793 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
jtgafb833d1999-08-19 00:55:39 +0000794#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000795
796 alreadyCalled = GL_TRUE;
797 }
Brian Paul9560f052000-01-31 23:11:39 +0000798 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000799}
800
Keith Whitwell6dc85572003-07-17 13:43:59 +0000801/**
jtgafb833d1999-08-19 00:55:39 +0000802 * Allocate and initialize a shared context state structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000803 *
804 * \return pointer to a gl_shared_state structure on success, or NULL on
805 * failure.
806 *
807 * Initializes the display list, texture objects and vertex programs hash
808 * tables, allocates the texture objects. If it runs out of memory, frees
809 * everything already allocated before returning NULL.
jtgafb833d1999-08-19 00:55:39 +0000810 */
Brian Paula3f13702003-04-01 16:41:50 +0000811static GLboolean
812alloc_shared_state( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000813{
Brian Paula3f13702003-04-01 16:41:50 +0000814 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000815 if (!ss)
Brian Paula3f13702003-04-01 16:41:50 +0000816 return GL_FALSE;
817
818 ctx->Shared = ss;
jtgafb833d1999-08-19 00:55:39 +0000819
Brian Paule4b684c2000-09-12 21:07:40 +0000820 _glthread_INIT_MUTEX(ss->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000821
Brian Paule4b684c2000-09-12 21:07:40 +0000822 ss->DisplayList = _mesa_NewHashTable();
Brian Paulbb797902000-01-24 16:19:54 +0000823 ss->TexObjects = _mesa_NewHashTable();
Brian Paul451f3102003-04-17 01:48:19 +0000824#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
Brian Paul610d5992003-01-14 04:55:45 +0000825 ss->Programs = _mesa_NewHashTable();
Brian Paul8dfc5b92002-10-16 17:57:51 +0000826#endif
jtgafb833d1999-08-19 00:55:39 +0000827
Brian Paul451f3102003-04-17 01:48:19 +0000828#if FEATURE_ARB_vertex_program
829 ss->DefaultVertexProgram = _mesa_alloc_program(ctx, GL_VERTEX_PROGRAM_ARB, 0);
830 if (!ss->DefaultVertexProgram)
831 goto cleanup;
832#endif
833#if FEATURE_ARB_fragment_program
834 ss->DefaultFragmentProgram = _mesa_alloc_program(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
835 if (!ss->DefaultFragmentProgram)
836 goto cleanup;
837#endif
838
Ian Romanick0207b472003-09-09 00:10:12 +0000839 ss->BufferObjects = _mesa_NewHashTable();
840
Brian Paula3f13702003-04-01 16:41:50 +0000841 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
842 if (!ss->Default1D)
843 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000844
Brian Paula3f13702003-04-01 16:41:50 +0000845 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
846 if (!ss->Default2D)
847 goto cleanup;
jtgafb833d1999-08-19 00:55:39 +0000848
Brian Paula3f13702003-04-01 16:41:50 +0000849 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
850 if (!ss->Default3D)
851 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000852
Brian Paula3f13702003-04-01 16:41:50 +0000853 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
854 if (!ss->DefaultCubeMap)
855 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000856
Brian Paula3f13702003-04-01 16:41:50 +0000857 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
858 if (!ss->DefaultRect)
859 goto cleanup;
Brian Paul413d6a22000-05-26 14:44:59 +0000860
Brian Paula3f13702003-04-01 16:41:50 +0000861#if 0
862 _mesa_save_texture_object(ctx, ss->Default1D);
863 _mesa_save_texture_object(ctx, ss->Default2D);
864 _mesa_save_texture_object(ctx, ss->Default3D);
865 _mesa_save_texture_object(ctx, ss->DefaultCubeMap);
866 _mesa_save_texture_object(ctx, ss->DefaultRect);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000867#endif
Brian Paula3f13702003-04-01 16:41:50 +0000868
869 /* Effectively bind the default textures to all texture units */
870 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
871 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
872 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
873 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
874 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
875
876 return GL_TRUE;
877
878 cleanup:
879 /* Ran out of memory at some point. Free everything and return NULL */
880 if (ss->DisplayList)
881 _mesa_DeleteHashTable(ss->DisplayList);
882 if (ss->TexObjects)
883 _mesa_DeleteHashTable(ss->TexObjects);
884#if FEATURE_NV_vertex_program
885 if (ss->Programs)
886 _mesa_DeleteHashTable(ss->Programs);
887#endif
Brian Paul451f3102003-04-17 01:48:19 +0000888#if FEATURE_ARB_vertex_program
889 if (ss->DefaultVertexProgram)
890 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
891#endif
892#if FEATURE_ARB_fragment_program
893 if (ss->DefaultFragmentProgram)
894 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
895#endif
Ian Romanick0207b472003-09-09 00:10:12 +0000896 if (ss->BufferObjects)
897 _mesa_DeleteHashTable(ss->BufferObjects);
898
Brian Paula3f13702003-04-01 16:41:50 +0000899 if (ss->Default1D)
900 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
901 if (ss->Default2D)
902 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
903 if (ss->Default3D)
904 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
905 if (ss->DefaultCubeMap)
906 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
907 if (ss->DefaultRect)
908 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
909 if (ss)
910 _mesa_free(ss);
911 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000912}
913
Keith Whitwell6dc85572003-07-17 13:43:59 +0000914/**
jtgafb833d1999-08-19 00:55:39 +0000915 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000916 *
917 * \param ctx GL context.
918 * \param ss shared state pointer.
919 *
920 * Frees the display lists, the texture objects (calling the driver texture
921 * deletion callback to free its private data) and the vertex programs, as well
922 * as their hash tables.
923 *
924 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000925 */
Brian Paul178a1c52000-04-22 01:05:00 +0000926static void
927free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000928{
929 /* Free display lists */
930 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000931 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000932 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000933 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000934 }
935 else {
936 break;
937 }
938 }
Brian Paulbb797902000-01-24 16:19:54 +0000939 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000940
941 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000942 ASSERT(ctx->Driver.DeleteTexture);
943 while (1) {
944 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
945 if (texName) {
946 struct gl_texture_object *texObj = (struct gl_texture_object *)
947 _mesa_HashLookup(ss->TexObjects, texName);
948 ASSERT(texObj);
949 (*ctx->Driver.DeleteTexture)(ctx, texObj);
950 _mesa_HashRemove(ss->TexObjects, texName);
951 }
952 else {
953 break;
954 }
jtgafb833d1999-08-19 00:55:39 +0000955 }
Brian Paulbb797902000-01-24 16:19:54 +0000956 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000957
Brian Paul8dfc5b92002-10-16 17:57:51 +0000958#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +0000959 /* Free vertex programs */
960 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +0000961 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +0000962 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +0000963 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
964 prog);
965 ASSERT(p);
966 _mesa_delete_program(ctx, p);
967 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +0000968 }
969 else {
970 break;
971 }
972 }
Brian Paul610d5992003-01-14 04:55:45 +0000973 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000974#endif
Brian Paul30f51ae2001-12-18 04:06:44 +0000975
Ian Romanick0207b472003-09-09 00:10:12 +0000976 _mesa_DeleteHashTable(ss->BufferObjects);
977
Keith Whitwelle15fd852002-12-12 13:03:15 +0000978 _glthread_DESTROY_MUTEX(ss->Mutex);
979
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000980 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +0000981}
982
983
Keith Whitwell6dc85572003-07-17 13:43:59 +0000984static void _mesa_init_current( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000985{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000986 int i;
jtgafb833d1999-08-19 00:55:39 +0000987
Keith Whitwell6dc85572003-07-17 13:43:59 +0000988 /* Current group */
989 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
990 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +0000991 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000992 /* special cases: */
993 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
994 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
995 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
996 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 0.0 );
997 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
998 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
999 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i], 0.0, 0.0, 0.0, 1.0);
1000 ctx->Current.Index = 1;
1001 ctx->Current.EdgeFlag = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001002}
1003
1004
Keith Whitwell6dc85572003-07-17 13:43:59 +00001005static void
1006_mesa_init_constants( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001007{
Brian Paul4d053dd2000-01-14 04:45:47 +00001008 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +00001009
Brian Paulcd1cefa2001-06-13 14:56:14 +00001010 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1011 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1012
Brian Paul539cce52000-02-03 19:40:07 +00001013 /* Constants, may be overriden by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +00001014 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
Brian Paulcd1cefa2001-06-13 14:56:14 +00001015 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1016 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
Brian Paul8afe7de2002-06-15 03:03:06 +00001017 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001018 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
Brian Paul610d5992003-01-14 04:55:45 +00001019 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1020 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
Gareth Hughes2c3d34c2001-03-18 08:53:49 +00001021 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
Brian Paul87c964d2001-11-06 15:53:00 +00001022 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
Brian Paul4d053dd2000-01-14 04:45:47 +00001023 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +00001024 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1025 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1026 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1027 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1028 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001029 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001030 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1031 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1032 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1033 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001034 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001035 ctx->Const.NumAuxBuffers = NUM_AUX_BUFFERS;
Brian Paul4bdcfe52000-04-17 17:57:04 +00001036 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +00001037 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1038 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
Brian Paula8644322000-11-27 18:22:13 +00001039 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1040 ctx->Const.MaxLights = MAX_LIGHTS;
Ian Romanick882caa12003-05-30 21:37:14 +00001041 ctx->Const.MaxSpotExponent = 128.0;
1042 ctx->Const.MaxShininess = 128.0;
Brian Pauld0492cf2003-04-11 01:20:06 +00001043#if FEATURE_ARB_vertex_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001044 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001045 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1046 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1047 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1048 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1049 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001050#endif
1051#if FEATURE_ARB_fragment_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001052 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001053 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1054 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1055 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1056 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1057 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1058 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1059 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1060 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001061#endif
Brian Pauledd67742003-04-18 18:02:43 +00001062 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1063 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
Brian Pauld0492cf2003-04-11 01:20:06 +00001064
Brian Paul92f97852003-05-01 22:44:02 +00001065 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
Keith Whitwell6dc85572003-07-17 13:43:59 +00001066}
jtgafb833d1999-08-19 00:55:39 +00001067
Keith Whitwell6dc85572003-07-17 13:43:59 +00001068/**
1069 * Initialize the attribute groups in a GL context.
1070 *
1071 * \param ctx GL context.
1072 *
1073 * Initializes all the attributes, calling the respective <tt>init*</tt>
1074 * functions for the more complex data structures.
1075 */
1076static GLboolean
1077init_attrib_groups( GLcontext *ctx )
1078{
1079 assert(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001080
Keith Whitwell6dc85572003-07-17 13:43:59 +00001081 /* Constants */
1082 _mesa_init_constants( ctx );
Brian Paul0771d152000-04-07 00:19:41 +00001083
Brian Paul4d053dd2000-01-14 04:45:47 +00001084 /* Extensions */
Brian Paulde4f4602003-07-03 02:15:06 +00001085 _mesa_init_extensions( ctx );
jtgafb833d1999-08-19 00:55:39 +00001086
Keith Whitwell6dc85572003-07-17 13:43:59 +00001087 /* Attribute Groups */
1088 _mesa_init_accum( ctx );
1089 _mesa_init_attrib( ctx );
1090 _mesa_init_buffers( ctx );
Brian Paul148a2842003-09-17 03:40:11 +00001091 _mesa_init_buffer_objects( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001092 _mesa_init_color( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001093 _mesa_init_colortables( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001094 _mesa_init_current( ctx );
1095 _mesa_init_depth( ctx );
1096 _mesa_init_debug( ctx );
1097 _mesa_init_display_list( ctx );
1098 _mesa_init_eval( ctx );
1099 _mesa_init_feedback( ctx );
1100 _mesa_init_fog( ctx );
1101 _mesa_init_histogram( ctx );
1102 _mesa_init_hint( ctx );
1103 _mesa_init_line( ctx );
1104 _mesa_init_lighting( ctx );
1105 _mesa_init_matrix( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001106 _mesa_init_occlude( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001107 _mesa_init_pixel( ctx );
1108 _mesa_init_point( ctx );
1109 _mesa_init_polygon( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001110 _mesa_init_program( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001111 _mesa_init_rastpos( ctx );
1112 _mesa_init_stencil( ctx );
1113 _mesa_init_transform( ctx );
1114 _mesa_init_varray( ctx );
1115 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001116
Keith Whitwell6dc85572003-07-17 13:43:59 +00001117 if (!_mesa_init_texture( ctx ))
1118 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001119
Brian Paul4d053dd2000-01-14 04:45:47 +00001120 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001121 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001122 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paul4d053dd2000-01-14 04:45:47 +00001123 ctx->CatchSignals = GL_TRUE;
Brian Paulf782b812002-10-04 17:37:45 +00001124 ctx->_Facing = 0;
Brian Paul4d053dd2000-01-14 04:45:47 +00001125
Brian Paula3f13702003-04-01 16:41:50 +00001126 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001127}
1128
1129
Brian Paulf44898c2003-07-18 15:44:57 +00001130/**
1131 * If the DRI libGL.so library is old, it may not have the entrypoints for
1132 * some recent OpenGL extensions. Dynamically add them now.
1133 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1134 * and driver code, this won't be an issue (and calling this function won't
1135 * do any harm).
1136 */
1137static void
1138add_newer_entrypoints(void)
1139{
Ian Romanick3baefe62003-08-22 23:28:03 +00001140 unsigned i;
1141 static const struct {
1142 const char * const name;
1143 unsigned offset;
1144 }
1145 newer_entrypoints[] = {
1146 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1147 { "glWindowPos2dARB", 513 },
1148 { "glWindowPos2dvARB", 514 },
1149 { "glWindowPos2fARB", 515 },
1150 { "glWindowPos2fvARB", 516 },
1151 { "glWindowPos2iARB", 517 },
1152 { "glWindowPos2ivARB", 518 },
1153 { "glWindowPos2sARB", 519 },
1154 { "glWindowPos2svARB", 520 },
1155 { "glWindowPos3dARB", 521 },
1156 { "glWindowPos3dvARB", 522 },
1157 { "glWindowPos3fARB", 523 },
1158 { "glWindowPos3fvARB", 524 },
1159 { "glWindowPos3iARB", 525 },
1160 { "glWindowPos3ivARB", 526 },
1161 { "glWindowPos3sARB", 527 },
1162 { "glWindowPos3svARB", 528 },
Brian Paulf44898c2003-07-18 15:44:57 +00001163#if FEATURE_NV_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001164 { "glAreProgramsResidentNV", 578 },
1165 { "glBindProgramNV", 579 },
1166 { "glDeleteProgramsNV", 580 },
1167 { "glExecuteProgramNV", 581 },
1168 { "glGenProgramsNV", 582 },
1169 { "glGetProgramParameterdvNV", 583 },
1170 { "glGetProgramParameterfvNV", 584 },
1171 { "glGetProgramivNV", 585 },
1172 { "glGetProgramStringNV", 586 },
1173 { "glGetTrackMatrixivNV", 587 },
1174 { "glGetVertexAttribdvNV", 588 },
1175 { "glGetVertexAttribfvNV", 589 },
1176 { "glGetVertexAttribivNV", 590 },
1177 { "glGetVertexAttribPointervNV", 591 },
1178 { "glIsProgramNV", 592 },
1179 { "glLoadProgramNV", 593 },
1180 { "glProgramParameter4dNV", 594 },
1181 { "glProgramParameter4dvNV", 595 },
1182 { "glProgramParameter4fNV", 596 },
1183 { "glProgramParameter4fvNV", 597 },
1184 { "glProgramParameters4dvNV", 598 },
1185 { "glProgramParameters4fvNV", 599 },
1186 { "glRequestResidentProgramsNV", 600 },
1187 { "glTrackMatrixNV", 601 },
1188 { "glVertexAttribPointerNV", 602 },
1189 { "glVertexAttrib1dNV", 603 },
1190 { "glVertexAttrib1dvNV", 604 },
1191 { "glVertexAttrib1fNV", 605 },
1192 { "glVertexAttrib1fvNV", 606 },
1193 { "glVertexAttrib1sNV", 607 },
1194 { "glVertexAttrib1svNV", 608 },
1195 { "glVertexAttrib2dNV", 609 },
1196 { "glVertexAttrib2dvNV", 610 },
1197 { "glVertexAttrib2fNV", 611 },
1198 { "glVertexAttrib2fvNV", 612 },
1199 { "glVertexAttrib2sNV", 613 },
1200 { "glVertexAttrib2svNV", 614 },
1201 { "glVertexAttrib3dNV", 615 },
1202 { "glVertexAttrib3dvNV", 616 },
1203 { "glVertexAttrib3fNV", 617 },
1204 { "glVertexAttrib3fvNV", 618 },
1205 { "glVertexAttrib3sNV", 619 },
1206 { "glVertexAttrib3svNV", 620 },
1207 { "glVertexAttrib4dNV", 621 },
1208 { "glVertexAttrib4dvNV", 622 },
1209 { "glVertexAttrib4fNV", 623 },
1210 { "glVertexAttrib4fvNV", 624 },
1211 { "glVertexAttrib4sNV", 625 },
1212 { "glVertexAttrib4svNV", 626 },
1213 { "glVertexAttrib4ubNV", 627 },
1214 { "glVertexAttrib4ubvNV", 628 },
1215 { "glVertexAttribs1dvNV", 629 },
1216 { "glVertexAttribs1fvNV", 630 },
1217 { "glVertexAttribs1svNV", 631 },
1218 { "glVertexAttribs2dvNV", 632 },
1219 { "glVertexAttribs2fvNV", 633 },
1220 { "glVertexAttribs2svNV", 634 },
1221 { "glVertexAttribs3dvNV", 635 },
1222 { "glVertexAttribs3fvNV", 636 },
1223 { "glVertexAttribs3svNV", 637 },
1224 { "glVertexAttribs4dvNV", 638 },
1225 { "glVertexAttribs4fvNV", 639 },
1226 { "glVertexAttribs4svNV", 640 },
1227 { "glVertexAttribs4ubvNV", 641 },
Brian Paulf44898c2003-07-18 15:44:57 +00001228#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001229 { "glPointParameteriNV", 642 },
1230 { "glPointParameterivNV", 643 },
1231 { "glMultiDrawArraysEXT", 644 },
1232 { "glMultiDrawElementsEXT", 645 },
1233 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1234 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1235 { "glActiveStencilFaceEXT", 646 },
Brian Paulf44898c2003-07-18 15:44:57 +00001236#if FEATURE_NV_fence
Ian Romanick3baefe62003-08-22 23:28:03 +00001237 { "glDeleteFencesNV", 647 },
1238 { "glGenFencesNV", 648 },
1239 { "glIsFenceNV", 649 },
1240 { "glTestFenceNV", 650 },
1241 { "glGetFenceivNV", 651 },
1242 { "glFinishFenceNV", 652 },
1243 { "glSetFenceNV", 653 },
Brian Paulf44898c2003-07-18 15:44:57 +00001244#endif
1245#if FEATURE_NV_fragment_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001246 { "glProgramNamedParameter4fNV", 682 },
1247 { "glProgramNamedParameter4dNV", 683 },
1248 { "glProgramNamedParameter4fvNV", 683 },
1249 { "glProgramNamedParameter4dvNV", 684 },
1250 { "glGetProgramNamedParameterfvNV", 685 },
1251 { "glGetProgramNamedParameterdvNV", 686 },
Brian Paulf44898c2003-07-18 15:44:57 +00001252#endif
1253#if FEATURE_ARB_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001254 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1255 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1256 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1257 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1258 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1259 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1260 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1261 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1262 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1263 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1264 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1265 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1266 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1267 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1268 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1269 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1270 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1271 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1272 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1273 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1274 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1275 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1276 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1277 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1278 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1279 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1280 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1281 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1282 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1283 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1284 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1285 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1286 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1287 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1288 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1289 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1290 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1291 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1292 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1293 { "glProgramStringARB", _gloffset_ProgramStringARB },
1294 { "glBindProgramARB", _gloffset_BindProgramNV },
1295 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1296 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1297 { "glIsProgramARB", _gloffset_IsProgramNV },
1298 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1299 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1300 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1301 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1302 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1303 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1304 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1305 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1306 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1307 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1308 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1309 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1310 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1311 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1312 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1313 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1314 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1315 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
Brian Paulf44898c2003-07-18 15:44:57 +00001316#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001317 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1318 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
1319 };
1320
1321 for ( i = 0 ; i < Elements(newer_entrypoints) ; i++ ) {
1322 _glapi_add_entrypoint( newer_entrypoints[i].name,
1323 newer_entrypoints[i].offset );
1324 }
Brian Paulf44898c2003-07-18 15:44:57 +00001325}
1326
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001327
Brian Paulde4f4602003-07-03 02:15:06 +00001328/**
Keith Whitwell6dc85572003-07-17 13:43:59 +00001329 * Initialize a GLcontext struct.
1330 *
1331 * This includes allocating all the other structs and arrays which hang off of
1332 * the context by pointers.
1333 *
1334 * \sa _mesa_create_context() for the parameter description.
1335 *
1336 * Performs the imports and exports callback tables initialization, and
1337 * miscellaneous one-time initializations. If no shared context is supplied one
1338 * is allocated, and increase its reference count. Setups the GL API dispatch
1339 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1340 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1341 * for debug flags.
1342 *
Brian Paulde4f4602003-07-03 02:15:06 +00001343 * \note the direct parameter is ignored (obsolete).
jtgafb833d1999-08-19 00:55:39 +00001344 */
Brian Paul178a1c52000-04-22 01:05:00 +00001345GLboolean
1346_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001347 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001348 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001349 void *driver_ctx,
1350 GLboolean direct )
jtgafb833d1999-08-19 00:55:39 +00001351{
Brian Paul5fb84d22000-05-24 15:04:45 +00001352 GLuint dispatchSize;
1353
Brian Paul3c634522002-10-24 23:57:19 +00001354 ASSERT(driver_ctx);
jtgafb833d1999-08-19 00:55:39 +00001355
Brian Paul3c634522002-10-24 23:57:19 +00001356 /* If the driver wants core Mesa to use special imports, it'll have to
1357 * override these defaults.
1358 */
1359 _mesa_init_default_imports( &(ctx->imports), driver_ctx );
jtgafb833d1999-08-19 00:55:39 +00001360
Brian Paul9a33a112002-06-13 04:28:29 +00001361 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001362 _mesa_init_default_exports( &(ctx->exports) );
1363
1364 /* misc one-time initializations */
1365 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001366
Brian Paul3c634522002-10-24 23:57:19 +00001367 ctx->DriverCtx = driver_ctx;
Brian Paulb1394fa2000-09-26 20:53:53 +00001368 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001369 ctx->DrawBuffer = NULL;
1370 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001371
Brian Paula3f13702003-04-01 16:41:50 +00001372 /* Set these pointers to defaults now in case they're not set since
1373 * we need them while creating the default textures.
1374 */
1375 if (!ctx->Driver.NewTextureObject)
1376 ctx->Driver.NewTextureObject = _mesa_new_texture_object;
1377 if (!ctx->Driver.DeleteTexture)
1378 ctx->Driver.DeleteTexture = _mesa_delete_texture_object;
1379 if (!ctx->Driver.NewTextureImage)
1380 ctx->Driver.NewTextureImage = _mesa_new_texture_image;
1381
jtgafb833d1999-08-19 00:55:39 +00001382 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001383 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001384 ctx->Shared = share_list->Shared;
1385 }
1386 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001387 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001388 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001389 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001390 }
1391 }
Brian Paul9560f052000-01-31 23:11:39 +00001392 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001393 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001394 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001395
Keith Whitwell6dc85572003-07-17 13:43:59 +00001396 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001397 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001398 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001399 }
jtgafb833d1999-08-19 00:55:39 +00001400
Brian Paulf44898c2003-07-18 15:44:57 +00001401 /* libGL ABI coordination */
1402 add_newer_entrypoints();
Keith Whitwell6dc85572003-07-17 13:43:59 +00001403
Brian Paul5fb84d22000-05-24 15:04:45 +00001404 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1405 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1406 * Mesa we do this to accomodate different versions of libGL and various
1407 * DRI drivers.
1408 */
1409 dispatchSize = MAX2(_glapi_get_dispatch_table_size(),
1410 sizeof(struct _glapi_table) / sizeof(void *));
1411
Brian Paulfbd8f211999-11-11 01:22:25 +00001412 /* setup API dispatch tables */
Brian Paul5fb84d22000-05-24 15:04:45 +00001413 ctx->Exec = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
1414 ctx->Save = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001415 if (!ctx->Exec || !ctx->Save) {
1416 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001417 if (ctx->Exec)
Brian Paul2d8db392000-06-27 22:10:00 +00001418 FREE( ctx->Exec );
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001419 }
Brian Paul5fb84d22000-05-24 15:04:45 +00001420 _mesa_init_exec_table(ctx->Exec, dispatchSize);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001421 ctx->CurrentDispatch = ctx->Exec;
jtgafb833d1999-08-19 00:55:39 +00001422
Keith Whitwell6dc85572003-07-17 13:43:59 +00001423#if _HAVE_FULL_GL
1424 _mesa_init_dlist_table(ctx->Save, dispatchSize);
1425
Keith Whitwellad2ac212000-11-24 10:25:05 +00001426 ctx->ExecPrefersFloat = GL_FALSE;
1427 ctx->SavePrefersFloat = GL_FALSE;
1428
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001429 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001430 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001431 ctx->TnlModule.Current = NULL;
1432 ctx->TnlModule.SwapCount = 0;
1433
Keith Whitwell6dc85572003-07-17 13:43:59 +00001434#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001435
Brian Paul4d053dd2000-01-14 04:45:47 +00001436 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001437}
1438
Brian Paulde4f4602003-07-03 02:15:06 +00001439/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001440 * Allocate and initialize a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001441 *
1442 * \param visual a GLvisual pointer (we copy the struct contents)
1443 * \param share_list another context to share display lists with or NULL
1444 * \param driver_ctx pointer to device driver's context state struct
1445 * \param direct obsolete, ignored
1446 *
1447 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001448 */
Brian Paul178a1c52000-04-22 01:05:00 +00001449GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001450_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001451 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001452 void *driver_ctx,
1453 GLboolean direct )
1454
Brian Paul4d053dd2000-01-14 04:45:47 +00001455{
Brian Paul4753d602002-06-15 02:38:15 +00001456 GLcontext *ctx;
1457
1458 ASSERT(visual);
Brian Paul3c634522002-10-24 23:57:19 +00001459 ASSERT(driver_ctx);
Brian Paul4753d602002-06-15 02:38:15 +00001460
Brian Paul3c634522002-10-24 23:57:19 +00001461 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001462 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001463 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001464
Brian Paul3c634522002-10-24 23:57:19 +00001465 if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001466 return ctx;
1467 }
1468 else {
Brian Paul3c634522002-10-24 23:57:19 +00001469 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001470 return NULL;
1471 }
1472}
1473
Keith Whitwell6dc85572003-07-17 13:43:59 +00001474/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001475 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001476 *
1477 * But doesn't free the GLcontext struct itself.
1478 *
1479 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001480 */
Brian Paul178a1c52000-04-22 01:05:00 +00001481void
Brian Paulb1394fa2000-09-26 20:53:53 +00001482_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001483{
Brian Paul4d053dd2000-01-14 04:45:47 +00001484 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001485 if (ctx == _mesa_get_current_context()) {
1486 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001487 }
1488
Keith Whitwell6dc85572003-07-17 13:43:59 +00001489 _mesa_free_lighting_data( ctx );
1490 _mesa_free_eval_data( ctx );
1491 _mesa_free_texture_data( ctx );
1492 _mesa_free_matrix_data( ctx );
1493 _mesa_free_viewport_data( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001494 _mesa_free_colortables_data( ctx );
Brian Paul8dfc5b92002-10-16 17:57:51 +00001495#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +00001496 if (ctx->VertexProgram.Current) {
Brian Paul610d5992003-01-14 04:55:45 +00001497 ctx->VertexProgram.Current->Base.RefCount--;
1498 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001499 _mesa_delete_program(ctx, &(ctx->VertexProgram.Current->Base));
Brian Paul610d5992003-01-14 04:55:45 +00001500 }
1501#endif
1502#if FEATURE_NV_fragment_program
1503 if (ctx->FragmentProgram.Current) {
1504 ctx->FragmentProgram.Current->Base.RefCount--;
1505 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001506 _mesa_delete_program(ctx, &(ctx->FragmentProgram.Current->Base));
Brian Paulfd284452001-07-19 15:54:34 +00001507 }
Brian Paul8dfc5b92002-10-16 17:57:51 +00001508#endif
Brian Paulfd284452001-07-19 15:54:34 +00001509
Brian Paul30f51ae2001-12-18 04:06:44 +00001510 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001511 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001512 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001513 assert(ctx->Shared->RefCount >= 0);
1514 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1515 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001516 /* free shared state */
1517 free_shared_state( ctx, ctx->Shared );
1518 }
1519
Brian Paul702ca202003-07-18 15:22:16 +00001520 if (ctx->Extensions.String)
1521 FREE((void *) ctx->Extensions.String);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001522
1523 FREE(ctx->Exec);
1524 FREE(ctx->Save);
Brian Paul4d053dd2000-01-14 04:45:47 +00001525}
1526
Keith Whitwell6dc85572003-07-17 13:43:59 +00001527/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001528 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001529 *
1530 * \param ctx GL context.
1531 *
1532 * Calls _mesa_free_context_data() and free the structure.
jtgafb833d1999-08-19 00:55:39 +00001533 */
Brian Paul178a1c52000-04-22 01:05:00 +00001534void
Brian Paulb1394fa2000-09-26 20:53:53 +00001535_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001536{
1537 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001538 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001539 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001540 }
1541}
1542
Keith Whitwell6dc85572003-07-17 13:43:59 +00001543#if _HAVE_FULL_GL
1544/**
jtgafb833d1999-08-19 00:55:39 +00001545 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001546 *
1547 * \param src source context
1548 * \param dst destination context
1549 * \param mask bitwise OR of GL_*_BIT flags
1550 *
1551 * According to the bits specified in \p mask, copies the corresponding
1552 * attributes from \p src into \dst. For many of the attributes a simple \c
1553 * memcpy is not enough due to the existence of internal pointers in their data
1554 * structures.
jtgafb833d1999-08-19 00:55:39 +00001555 */
Brian Paul178a1c52000-04-22 01:05:00 +00001556void
Brian Paulb1394fa2000-09-26 20:53:53 +00001557_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001558{
1559 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001560 /* OK to memcpy */
1561 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001562 }
1563 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001564 /* OK to memcpy */
1565 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001566 }
1567 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001568 /* OK to memcpy */
1569 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001570 }
1571 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001572 /* OK to memcpy */
1573 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001574 }
1575 if (mask & GL_ENABLE_BIT) {
1576 /* no op */
1577 }
1578 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001579 /* OK to memcpy */
1580 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001581 }
1582 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001583 /* OK to memcpy */
1584 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001585 }
1586 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001587 /* OK to memcpy */
1588 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001589 }
1590 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001591 GLuint i;
1592 /* begin with memcpy */
1593 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1594 /* fixup linked lists to prevent pointer insanity */
1595 make_empty_list( &(dst->Light.EnabledList) );
1596 for (i = 0; i < MAX_LIGHTS; i++) {
1597 if (dst->Light.Light[i].Enabled) {
1598 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1599 }
1600 }
jtgafb833d1999-08-19 00:55:39 +00001601 }
1602 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001603 /* OK to memcpy */
1604 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001605 }
1606 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001607 /* OK to memcpy */
1608 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001609 }
1610 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001611 /* OK to memcpy */
1612 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001613 }
1614 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001615 /* OK to memcpy */
1616 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001617 }
1618 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001619 /* OK to memcpy */
1620 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001621 }
1622 if (mask & GL_POLYGON_STIPPLE_BIT) {
1623 /* Use loop instead of MEMCPY due to problem with Portland Group's
1624 * C compiler. Reported by John Stone.
1625 */
Brian Paul85d81602002-06-17 23:36:31 +00001626 GLuint i;
1627 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001628 dst->PolygonStipple[i] = src->PolygonStipple[i];
1629 }
1630 }
1631 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001632 /* OK to memcpy */
1633 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001634 }
1635 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001636 /* OK to memcpy */
1637 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001638 }
1639 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001640 /* Cannot memcpy because of pointers */
1641 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001642 }
1643 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001644 /* OK to memcpy */
1645 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001646 }
1647 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001648 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1649 dst->Viewport.X = src->Viewport.X;
1650 dst->Viewport.Y = src->Viewport.Y;
1651 dst->Viewport.Width = src->Viewport.Width;
1652 dst->Viewport.Height = src->Viewport.Height;
1653 dst->Viewport.Near = src->Viewport.Near;
1654 dst->Viewport.Far = src->Viewport.Far;
1655 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001656 }
Brian Paul85d81602002-06-17 23:36:31 +00001657
Keith Whitwella96308c2000-10-30 13:31:59 +00001658 /* XXX FIXME: Call callbacks?
1659 */
1660 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001661}
Keith Whitwell23caf202000-11-16 21:05:34 +00001662#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001663
1664
Brian Paulb1d53d92003-06-11 18:48:19 +00001665/**
1666 * Check if the given context can render into the given framebuffer
1667 * by checking visual attributes.
1668 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1669 */
1670static GLboolean
1671check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1672{
1673 const GLvisual *ctxvis = &ctx->Visual;
1674 const GLvisual *bufvis = &buffer->Visual;
1675
1676 if (ctxvis == bufvis)
1677 return GL_TRUE;
1678
1679 if (ctxvis->rgbMode != bufvis->rgbMode)
1680 return GL_FALSE;
1681 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1682 return GL_FALSE;
1683 if (ctxvis->stereoMode && !bufvis->stereoMode)
1684 return GL_FALSE;
1685 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1686 return GL_FALSE;
1687 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1688 return GL_FALSE;
1689 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1690 return GL_FALSE;
1691 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1692 return GL_FALSE;
1693 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1694 return GL_FALSE;
1695 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1696 return GL_FALSE;
1697 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1698 return GL_FALSE;
1699 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1700 return GL_FALSE;
1701
1702 return GL_TRUE;
1703}
1704
1705
Keith Whitwell6dc85572003-07-17 13:43:59 +00001706/**
Brian Paul9a33a112002-06-13 04:28:29 +00001707 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001708 *
1709 * \param newCtx new GL context.
1710 * \param buffer framebuffer.
1711 *
1712 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001713 */
1714void
1715_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1716{
1717 _mesa_make_current2( newCtx, buffer, buffer );
1718}
1719
Keith Whitwell6dc85572003-07-17 13:43:59 +00001720/**
1721 * Bind the given context to the given draw-buffer and read-buffer and
1722 * make it the current context for this thread.
1723 *
1724 * \param newCtx new GL context. If NULL then there will be no current GL
1725 * context.
1726 * \param drawBuffer draw framebuffer.
1727 * \param readBuffer read framebuffer.
1728 *
1729 * Check that the context's and framebuffer's visuals are compatible, returning
1730 * immediately otherwise. Sets the glapi current context via
1731 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1732 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1733 * Calls dd_function_table::MakeCurrent callback if defined.
1734 *
1735 * When a context is bound by the first time and the \c MESA_INFO environment
1736 * variable is set it calls print_info() as an aid for remote user
1737 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001738 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001739void
1740_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1741 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001742{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001743 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001744 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001745
Brian Paulbe3602d2001-02-28 00:27:48 +00001746 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001747 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001748 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1749 if (!check_compatible(newCtx, drawBuffer))
1750 return;
1751 }
1752 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1753 if (!check_compatible(newCtx, readBuffer))
1754 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001755 }
1756
Brian Paul00037781999-12-17 14:52:35 +00001757 /* We call this function periodically (just here for now) in
1758 * order to detect when multithreading has begun.
1759 */
1760 _glapi_check_multithread();
1761
Brian Paulf9b97d92000-01-28 20:17:42 +00001762 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001763 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001764
1765
1766 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001767 _glapi_set_dispatch(NULL); /* none current */
1768 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001769 else {
1770 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001771
Keith Whitwell23caf202000-11-16 21:05:34 +00001772 if (drawBuffer && readBuffer) {
1773 /* TODO: check if newCtx and buffer's visual match??? */
1774 newCtx->DrawBuffer = drawBuffer;
1775 newCtx->ReadBuffer = readBuffer;
1776 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001777
Keith Whitwell6dc85572003-07-17 13:43:59 +00001778#if _HAVE_FULL_GL
Brian Paul10d7f542002-06-17 23:38:14 +00001779 if (drawBuffer->Width == 0 && drawBuffer->Height == 0) {
1780 /* get initial window size */
1781 GLuint bufWidth, bufHeight;
1782
1783 /* ask device driver for size of output buffer */
1784 (*newCtx->Driver.GetBufferSize)( drawBuffer, &bufWidth, &bufHeight );
1785
Keith Whitwell6dc85572003-07-17 13:43:59 +00001786 if (drawBuffer->Width != bufWidth ||
1787 drawBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001788
Keith Whitwell6dc85572003-07-17 13:43:59 +00001789 drawBuffer->Width = bufWidth;
1790 drawBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001791
Keith Whitwell6dc85572003-07-17 13:43:59 +00001792 newCtx->Driver.ResizeBuffers( drawBuffer );
1793 }
Brian Paul10d7f542002-06-17 23:38:14 +00001794 }
1795
1796 if (readBuffer != drawBuffer &&
1797 readBuffer->Width == 0 && readBuffer->Height == 0) {
1798 /* get initial window size */
1799 GLuint bufWidth, bufHeight;
1800
1801 /* ask device driver for size of output buffer */
1802 (*newCtx->Driver.GetBufferSize)( readBuffer, &bufWidth, &bufHeight );
1803
Keith Whitwell6dc85572003-07-17 13:43:59 +00001804 if (readBuffer->Width != bufWidth ||
1805 readBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001806
Keith Whitwell6dc85572003-07-17 13:43:59 +00001807 readBuffer->Width = bufWidth;
1808 readBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001809
Keith Whitwell6dc85572003-07-17 13:43:59 +00001810 newCtx->Driver.ResizeBuffers( readBuffer );
1811 }
Brian Paul10d7f542002-06-17 23:38:14 +00001812 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001813#endif
Brian Paul00037781999-12-17 14:52:35 +00001814 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001815
Keith Whitwell6dc85572003-07-17 13:43:59 +00001816 /* Alert the driver - usually passed on to the sw t&l module,
1817 * but also used to detect threaded cases in the radeon codegen
1818 * hw t&l module.
1819 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001820 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001821 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1822
1823 /* We can use this to help debug user's problems. Tell them to set
1824 * the MESA_INFO env variable before running their app. Then the
1825 * first time each context is made current we'll print some useful
1826 * information.
1827 */
1828 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001829 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001830 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001831 }
1832 newCtx->FirstTimeCurrent = GL_FALSE;
1833 }
Brian Paul00037781999-12-17 14:52:35 +00001834 }
1835}
1836
Keith Whitwell6dc85572003-07-17 13:43:59 +00001837/**
1838 * Get current context for the calling thread.
1839 *
1840 * \return pointer to the current GL context.
1841 *
1842 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1843 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00001844 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001845GLcontext *
1846_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00001847{
Brian Paulf9b97d92000-01-28 20:17:42 +00001848 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001849}
1850
Keith Whitwell6dc85572003-07-17 13:43:59 +00001851/**
1852 * Get context's current API dispatch table.
1853 *
1854 * It'll either be the immediate-mode execute dispatcher or the display list
1855 * compile dispatcher.
1856 *
1857 * \param ctx GL context.
1858 *
1859 * \return pointer to dispatch_table.
1860 *
1861 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00001862 */
1863struct _glapi_table *
1864_mesa_get_dispatch(GLcontext *ctx)
1865{
1866 return ctx->CurrentDispatch;
1867}
1868
Keith Whitwell6dc85572003-07-17 13:43:59 +00001869/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00001870
1871
jtgafb833d1999-08-19 00:55:39 +00001872/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001873/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00001874/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001875/*@{*/
jtgafb833d1999-08-19 00:55:39 +00001876
Keith Whitwell6dc85572003-07-17 13:43:59 +00001877/**
1878 * Record an error.
1879 *
1880 * \param ctx GL context.
1881 * \param error error code.
1882 *
1883 * Records the given error code and call the driver's dd_function_table::Error
1884 * function if defined.
1885 *
1886 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00001887 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00001888 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001889void
Brian Paul4e9676f2002-06-29 19:48:15 +00001890_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00001891{
Brian Paul18a285a2002-03-16 00:53:15 +00001892 if (!ctx)
1893 return;
1894
Brian Paul7eb06032000-07-14 04:13:40 +00001895 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00001896 ctx->ErrorValue = error;
1897 }
1898
1899 /* Call device driver's error handler, if any. This is used on the Mac. */
1900 if (ctx->Driver.Error) {
1901 (*ctx->Driver.Error)( ctx );
1902 }
1903}
1904
Keith Whitwell6dc85572003-07-17 13:43:59 +00001905/**
1906 * Execute glFinish().
1907 *
1908 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1909 * dd_function_table::Finish driver callback, if not NULL.
1910 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001911void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00001912_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00001913{
Brian Paulfa9df402000-02-02 19:16:46 +00001914 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001915 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001916 if (ctx->Driver.Finish) {
1917 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001918 }
1919}
1920
Keith Whitwell6dc85572003-07-17 13:43:59 +00001921/**
1922 * Execute glFlush().
1923 *
1924 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1925 * dd_function_table::Flush driver callback, if not NULL.
1926 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001927void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00001928_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00001929{
Brian Paulfa9df402000-02-02 19:16:46 +00001930 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001931 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001932 if (ctx->Driver.Flush) {
1933 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001934 }
jtgafb833d1999-08-19 00:55:39 +00001935}
Brian Paul48c6a6e2000-09-08 21:28:04 +00001936
1937
Keith Whitwell6dc85572003-07-17 13:43:59 +00001938/*@}*/