blob: a8352bbe2f4548c326cf88b912cde96c5570bb5f [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"
Keith Whitwell6dc85572003-07-17 13:43:59 +000080/*#include "clip.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"
Keith Whitwell6dc85572003-07-17 13:43:59 +000093#include "histogram.h"
94#include "hint.h"
jtgafb833d1999-08-19 00:55:39 +000095#include "hash.h"
96#include "light.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000097#include "lines.h"
jtgafb833d1999-08-19 00:55:39 +000098#include "macros.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000099#include "matrix.h"
100#include "pixel.h"
101#include "points.h"
102#include "polygon.h"
103#include "rastpos.h"
jtgafb833d1999-08-19 00:55:39 +0000104#include "simple_list.h"
Brian Paulfa9df402000-02-02 19:16:46 +0000105#include "state.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000106#include "stencil.h"
jtgafb833d1999-08-19 00:55:39 +0000107#include "teximage.h"
108#include "texobj.h"
Brian Paul85d81602002-06-17 23:36:31 +0000109#include "texstate.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000110#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +0000111#include "varray.h"
Brian Paul3a212032002-11-19 15:25:00 +0000112#if FEATURE_NV_vertex_program
Brian Paul610d5992003-01-14 04:55:45 +0000113#include "nvprogram.h"
114#include "nvvertprog.h"
115#endif
116#if FEATURE_NV_fragment_program
117#include "nvfragprog.h"
Brian Paul3a212032002-11-19 15:25:00 +0000118#endif
Gareth Hughesd4eb6652001-03-12 01:32:20 +0000119#include "vtxfmt.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000120#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000121#include "math/m_translate.h"
Keith Whitwell23caf202000-11-16 21:05:34 +0000122#include "math/m_matrix.h"
123#include "math/m_xform.h"
Keith Whitwellf4b02d12001-01-05 05:31:42 +0000124#include "math/mathmod.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000125#endif
jtgafb833d1999-08-19 00:55:39 +0000126
Brian Paul3b18a362000-09-26 15:27:20 +0000127#if defined(MESA_TRACE)
Brian Paul45f36342000-09-05 20:28:06 +0000128#include "Trace/tr_context.h"
129#include "Trace/tr_wrapper.h"
130#endif
131
davem69775355a2001-06-05 23:54:00 +0000132#ifdef USE_SPARC_ASM
133#include "SPARC/sparc.h"
134#endif
jtgafb833d1999-08-19 00:55:39 +0000135
Keith Whitwell23caf202000-11-16 21:05:34 +0000136#ifndef MESA_VERBOSE
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000137int MESA_VERBOSE = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000138#endif
139
140#ifndef MESA_DEBUG_FLAGS
Keith Whitwell306d3fc2002-04-09 16:56:50 +0000141int MESA_DEBUG_FLAGS = 0;
Keith Whitwell23caf202000-11-16 21:05:34 +0000142#endif
Brian Paulb1394fa2000-09-26 20:53:53 +0000143
Brian Paul86b84272001-12-14 02:50:01 +0000144
Brian Paul27558a12003-03-01 01:50:20 +0000145/* ubyte -> float conversion */
146GLfloat _mesa_ubyte_to_float_color_tab[256];
147
Brian Paul9a33a112002-06-13 04:28:29 +0000148static void
149free_shared_state( GLcontext *ctx, struct gl_shared_state *ss );
150
Brian Paul86b84272001-12-14 02:50:01 +0000151
Brian Paulb1394fa2000-09-26 20:53:53 +0000152/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000153/** \name OpenGL SI-style interface (new in Mesa 3.5)
154 *
155 * \if subset
156 * \note Most of these functions are never called in the Mesa subset.
157 * \endif
158 */
159/*@{*/
Brian Paulb1394fa2000-09-26 20:53:53 +0000160
Keith Whitwell6dc85572003-07-17 13:43:59 +0000161/**
162 * Destroy context callback.
163 *
164 * \param gc context.
165 * \return GL_TRUE on success, or GL_FALSE on failure.
166 *
167 * \ifnot subset
168 * Called by window system/device driver (via __GLexports::destroyCurrent) when
169 * the rendering context is to be destroyed.
170 * \endif
171 *
172 * Frees the context data and the context structure.
Brian Paul9a33a112002-06-13 04:28:29 +0000173 */
174GLboolean
175_mesa_destroyContext(__GLcontext *gc)
Brian Paulb1394fa2000-09-26 20:53:53 +0000176{
177 if (gc) {
178 _mesa_free_context_data(gc);
Brian Paul3c634522002-10-24 23:57:19 +0000179 _mesa_free(gc);
Brian Paulb1394fa2000-09-26 20:53:53 +0000180 }
181 return GL_TRUE;
182}
183
Keith Whitwell6dc85572003-07-17 13:43:59 +0000184/**
185 * Unbind context callback.
186 *
187 * \param gc context.
188 * \return GL_TRUE on success, or GL_FALSE on failure.
189 *
190 * \ifnot subset
191 * Called by window system/device driver (via __GLexports::loseCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000192 * when the rendering context is made non-current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000193 * \endif
194 *
195 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000196 */
197GLboolean
198_mesa_loseCurrent(__GLcontext *gc)
199{
200 /* XXX unbind context from thread */
201 return GL_TRUE;
202}
203
Keith Whitwell6dc85572003-07-17 13:43:59 +0000204/**
205 * Bind context callback.
206 *
207 * \param gc context.
208 * \return GL_TRUE on success, or GL_FALSE on failure.
209 *
210 * \ifnot subset
211 * Called by window system/device driver (via __GLexports::makeCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000212 * when the rendering context is made current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000213 * \endif
214 *
215 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000216 */
217GLboolean
218_mesa_makeCurrent(__GLcontext *gc)
219{
220 /* XXX bind context to thread */
221 return GL_TRUE;
222}
223
Keith Whitwell6dc85572003-07-17 13:43:59 +0000224/**
225 * Share context callback.
226 *
227 * \param gc context.
228 * \param gcShare shared context.
229 * \return GL_TRUE on success, or GL_FALSE on failure.
230 *
231 * \ifnot subset
232 * Called by window system/device driver (via __GLexports::shareContext)
233 * \endif
234 *
235 * Update the shared context reference count, gl_shared_state::RefCount.
Brian Paul9a33a112002-06-13 04:28:29 +0000236 */
237GLboolean
238_mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
239{
240 if (gc && gcShare && gc->Shared && gcShare->Shared) {
241 gc->Shared->RefCount--;
242 if (gc->Shared->RefCount == 0) {
243 free_shared_state(gc, gc->Shared);
244 }
245 gc->Shared = gcShare->Shared;
246 gc->Shared->RefCount++;
247 return GL_TRUE;
248 }
249 else {
250 return GL_FALSE;
251 }
252}
253
Keith Whitwell6dc85572003-07-17 13:43:59 +0000254
255#if _HAVE_FULL_GL
256/**
257 * Copy context callback.
258 */
Brian Paul9a33a112002-06-13 04:28:29 +0000259GLboolean
260_mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
261{
262 if (dst && src) {
263 _mesa_copy_context( src, dst, mask );
264 return GL_TRUE;
265 }
266 else {
267 return GL_FALSE;
268 }
269}
Keith Whitwell6dc85572003-07-17 13:43:59 +0000270#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000271
Keith Whitwell6dc85572003-07-17 13:43:59 +0000272/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000273GLboolean
274_mesa_forceCurrent(__GLcontext *gc)
275{
276 return GL_TRUE;
277}
278
Keith Whitwell6dc85572003-07-17 13:43:59 +0000279/**
280 * Windows/buffer resizing notification callback.
281 *
282 * \param gc GL context.
283 * \return GL_TRUE on success, or GL_FALSE on failure.
284 */
Brian Paul9a33a112002-06-13 04:28:29 +0000285GLboolean
286_mesa_notifyResize(__GLcontext *gc)
287{
288 GLint x, y;
289 GLuint width, height;
290 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
291 if (!d || !d->getDrawableSize)
292 return GL_FALSE;
293 d->getDrawableSize( d, &x, &y, &width, &height );
294 /* update viewport, resize software buffers, etc. */
295 return GL_TRUE;
296}
297
Keith Whitwell6dc85572003-07-17 13:43:59 +0000298/**
299 * Window/buffer destruction notification callback.
300 *
301 * \param gc GL context.
302 *
303 * Called when the context's window/buffer is going to be destroyed.
304 *
305 * No-op
306 */
Brian Paul9a33a112002-06-13 04:28:29 +0000307void
308_mesa_notifyDestroy(__GLcontext *gc)
309{
Brian Paul60b6e4f2002-10-14 17:08:17 +0000310 /* Unbind from it. */
Brian Paul9a33a112002-06-13 04:28:29 +0000311}
312
Keith Whitwell6dc85572003-07-17 13:43:59 +0000313/**
314 * Swap buffers notification callback.
315 *
316 * \param gc GL context.
317 *
318 * Called by window system just before swapping buffers.
Brian Paul9a33a112002-06-13 04:28:29 +0000319 * We have to finish any pending rendering.
320 */
321void
322_mesa_notifySwapBuffers(__GLcontext *gc)
323{
324 FLUSH_VERTICES( gc, 0 );
325}
326
Keith Whitwell6dc85572003-07-17 13:43:59 +0000327/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000328struct __GLdispatchStateRec *
329_mesa_dispatchExec(__GLcontext *gc)
330{
331 return NULL;
332}
333
Keith Whitwell6dc85572003-07-17 13:43:59 +0000334/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000335void
336_mesa_beginDispatchOverride(__GLcontext *gc)
337{
338}
339
Keith Whitwell6dc85572003-07-17 13:43:59 +0000340/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000341void
342_mesa_endDispatchOverride(__GLcontext *gc)
343{
344}
345
Keith Whitwell6dc85572003-07-17 13:43:59 +0000346/**
347 * \ifnot subset
348 * Setup the exports.
349 *
350 * The window system will call these functions when it needs Mesa to do
351 * something.
352 *
353 * \note Device drivers should override these functions! For example,
Brian Paul9a33a112002-06-13 04:28:29 +0000354 * the Xlib driver should plug in the XMesa*-style functions into this
355 * structure. The XMesa-style functions should then call the _mesa_*
356 * version of these functions. This is an approximation to OO design
357 * (inheritance and virtual functions).
Keith Whitwell6dc85572003-07-17 13:43:59 +0000358 * \endif
359 *
360 * \if subset
361 * No-op.
362 *
363 * \endif
Brian Paul9a33a112002-06-13 04:28:29 +0000364 */
365static void
366_mesa_init_default_exports(__GLexports *exports)
367{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000368#if _HAVE_FULL_GL
Brian Paul9a33a112002-06-13 04:28:29 +0000369 exports->destroyContext = _mesa_destroyContext;
370 exports->loseCurrent = _mesa_loseCurrent;
371 exports->makeCurrent = _mesa_makeCurrent;
372 exports->shareContext = _mesa_shareContext;
373 exports->copyContext = _mesa_copyContext;
374 exports->forceCurrent = _mesa_forceCurrent;
375 exports->notifyResize = _mesa_notifyResize;
Brian Paul2f35d5e2002-06-13 04:31:09 +0000376 exports->notifyDestroy = _mesa_notifyDestroy;
Brian Paul9a33a112002-06-13 04:28:29 +0000377 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
378 exports->dispatchExec = _mesa_dispatchExec;
379 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
380 exports->endDispatchOverride = _mesa_endDispatchOverride;
Keith Whitwell6dc85572003-07-17 13:43:59 +0000381#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000382}
383
Keith Whitwell6dc85572003-07-17 13:43:59 +0000384/**
385 * Exported OpenGL SI interface.
386 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000387__GLcontext *
388__glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
389{
390 GLcontext *ctx;
391
Brian Paul4753d602002-06-15 02:38:15 +0000392 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
Brian Paulb1394fa2000-09-26 20:53:53 +0000393 if (ctx == NULL) {
394 return NULL;
395 }
Brian Paul60b6e4f2002-10-14 17:08:17 +0000396
Brian Paul3c634522002-10-24 23:57:19 +0000397 _mesa_initialize_context(ctx, modes, NULL, imports, GL_FALSE);
Brian Paulb1394fa2000-09-26 20:53:53 +0000398 ctx->imports = *imports;
Brian Paulb1394fa2000-09-26 20:53:53 +0000399
400 return ctx;
401}
402
Keith Whitwell6dc85572003-07-17 13:43:59 +0000403/**
404 * Exported OpenGL SI interface.
405 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000406void
407__glCoreNopDispatch(void)
408{
409#if 0
410 /* SI */
411 __gl_dispatch = __glNopDispatchState;
412#else
413 /* Mesa */
414 _glapi_set_dispatch(NULL);
415#endif
416}
417
Keith Whitwell6dc85572003-07-17 13:43:59 +0000418/*@}*/
419
Brian Paulb1394fa2000-09-26 20:53:53 +0000420
jtgafb833d1999-08-19 00:55:39 +0000421/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000422/** \name GL Visual allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000423/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000424/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000425
Keith Whitwell6dc85572003-07-17 13:43:59 +0000426/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000427 * Allocate a new GLvisual object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000428 *
429 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
430 * \param dbFlag double buffering
431 * \param stereoFlag stereo buffer
432 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
433 * is acceptable but the actual depth type will be GLushort or GLuint as
434 * needed.
435 * \param stencilBits requested minimum bits per stencil buffer value
436 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
437 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
438 * \param redBits number of bits per color component in frame buffer for RGB(A)
439 * mode. We always use 8 in core Mesa though.
440 * \param greenBits same as above.
441 * \param blueBits same as above.
442 * \param alphaBits same as above.
443 * \param numSamples not really used.
444 *
445 * \return pointer to new GLvisual or NULL if requested parameters can't be
446 * met.
447 *
448 * Allocates a GLvisual structure and initializes it via
449 * _mesa_initialize_visual().
Brian Paul4d053dd2000-01-14 04:45:47 +0000450 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000451GLvisual *
452_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000453 GLboolean dbFlag,
454 GLboolean stereoFlag,
455 GLint redBits,
456 GLint greenBits,
457 GLint blueBits,
458 GLint alphaBits,
459 GLint indexBits,
460 GLint depthBits,
461 GLint stencilBits,
462 GLint accumRedBits,
463 GLint accumGreenBits,
464 GLint accumBlueBits,
465 GLint accumAlphaBits,
466 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000467{
Brian Paul178a1c52000-04-22 01:05:00 +0000468 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
469 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000470 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000471 redBits, greenBits, blueBits, alphaBits,
472 indexBits, depthBits, stencilBits,
473 accumRedBits, accumGreenBits,
474 accumBlueBits, accumAlphaBits,
Brian Paulb1394fa2000-09-26 20:53:53 +0000475 numSamples)) {
Brian Paul178a1c52000-04-22 01:05:00 +0000476 FREE(vis);
477 return NULL;
478 }
479 }
480 return vis;
481}
482
Keith Whitwell6dc85572003-07-17 13:43:59 +0000483/**
Brian Paul178a1c52000-04-22 01:05:00 +0000484 * Initialize the fields of the given GLvisual.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000485 *
486 * \return GL_TRUE on success, or GL_FALSE on failure.
487 *
488 * \sa _mesa_create_visual() above for the parameter description.
489 *
490 * Makes some sanity checks and fills in the fields of the
491 * GLvisual structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000492 */
493GLboolean
494_mesa_initialize_visual( GLvisual *vis,
495 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000496 GLboolean dbFlag,
497 GLboolean stereoFlag,
498 GLint redBits,
499 GLint greenBits,
500 GLint blueBits,
501 GLint alphaBits,
502 GLint indexBits,
503 GLint depthBits,
504 GLint stencilBits,
505 GLint accumRedBits,
506 GLint accumGreenBits,
507 GLint accumBlueBits,
508 GLint accumAlphaBits,
509 GLint numSamples )
510{
Brian Paulb6bcae52001-01-23 23:39:36 +0000511 (void) numSamples;
512
Brian Paul178a1c52000-04-22 01:05:00 +0000513 assert(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000514
Brian Pauled30dfa2000-03-03 17:47:39 +0000515 /* This is to catch bad values from device drivers not updated for
516 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
517 * bad value now (a 1-bit depth buffer!?!).
518 */
519 assert(depthBits == 0 || depthBits > 1);
520
521 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000522 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000523 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000524 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000525 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000526 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000527 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000528 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000529 }
530 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000531 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000532 }
533 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000534 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000535 }
536 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000537 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000538 }
539
Brian Paulb6bcae52001-01-23 23:39:36 +0000540 vis->rgbMode = rgbFlag;
541 vis->doubleBufferMode = dbFlag;
542 vis->stereoMode = stereoFlag;
Brian Paul153f1542002-10-29 15:04:35 +0000543
Brian Paulb6bcae52001-01-23 23:39:36 +0000544 vis->redBits = redBits;
545 vis->greenBits = greenBits;
546 vis->blueBits = blueBits;
547 vis->alphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000548
Brian Paulb6bcae52001-01-23 23:39:36 +0000549 vis->indexBits = indexBits;
550 vis->depthBits = depthBits;
551 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
552 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
553 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
554 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
555 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Pauled30dfa2000-03-03 17:47:39 +0000556
Brian Paul153f1542002-10-29 15:04:35 +0000557 vis->haveAccumBuffer = accumRedBits > 0;
558 vis->haveDepthBuffer = depthBits > 0;
559 vis->haveStencilBuffer = stencilBits > 0;
560
561 vis->numAuxBuffers = 0;
562 vis->level = 0;
563 vis->pixmapMode = 0;
564
Brian Paul178a1c52000-04-22 01:05:00 +0000565 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000566}
567
Keith Whitwell6dc85572003-07-17 13:43:59 +0000568/**
569 * Destroy a visual.
570 *
571 * \param vis visual.
572 *
573 * Frees the visual structure.
574 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000575void
576_mesa_destroy_visual( GLvisual *vis )
577{
578 FREE(vis);
579}
580
Keith Whitwell6dc85572003-07-17 13:43:59 +0000581/*@}*/
582
Brian Paulb371e0d2000-03-31 01:05:51 +0000583
Brian Paul4d053dd2000-01-14 04:45:47 +0000584/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000585/** \name GL Framebuffer allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000586/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000587/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000588
Keith Whitwell6dc85572003-07-17 13:43:59 +0000589/**
590 * Create a new framebuffer.
591 *
592 * A GLframebuffer is a structure which encapsulates the depth, stencil and
593 * accum buffers and related parameters.
594 *
595 * \param visual a GLvisual pointer (we copy the struct contents)
596 * \param softwareDepth create/use a software depth buffer?
597 * \param softwareStencil create/use a software stencil buffer?
598 * \param softwareAccum create/use a software accum buffer?
599 * \param softwareAlpha create/use a software alpha buffer?
600 *
601 * \return pointer to new GLframebuffer struct or NULL if error.
602 *
603 * Allocate a GLframebuffer structure and initializes it via
604 * _mesa_initialize_framebuffer().
Brian Paul4d053dd2000-01-14 04:45:47 +0000605 */
Brian Paul178a1c52000-04-22 01:05:00 +0000606GLframebuffer *
Brian Paulbe3602d2001-02-28 00:27:48 +0000607_mesa_create_framebuffer( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +0000608 GLboolean softwareDepth,
609 GLboolean softwareStencil,
610 GLboolean softwareAccum,
611 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000612{
Brian Paul178a1c52000-04-22 01:05:00 +0000613 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
614 assert(visual);
615 if (buffer) {
616 _mesa_initialize_framebuffer(buffer, visual,
617 softwareDepth, softwareStencil,
618 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000619 }
Brian Paul178a1c52000-04-22 01:05:00 +0000620 return buffer;
621}
622
Keith Whitwell6dc85572003-07-17 13:43:59 +0000623/**
Brian Paul178a1c52000-04-22 01:05:00 +0000624 * Initialize a GLframebuffer object.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000625 *
626 * \sa _mesa_create_framebuffer() above for the parameter description.
627 *
628 * Makes some sanity checks and fills in the fields of the
629 * GLframebuffer structure with the given parameters.
Brian Paul178a1c52000-04-22 01:05:00 +0000630 */
631void
632_mesa_initialize_framebuffer( GLframebuffer *buffer,
Brian Paulbe3602d2001-02-28 00:27:48 +0000633 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +0000634 GLboolean softwareDepth,
635 GLboolean softwareStencil,
636 GLboolean softwareAccum,
637 GLboolean softwareAlpha )
638{
639 assert(buffer);
640 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000641
Brian Paul6ec6b842002-10-30 19:49:29 +0000642 _mesa_bzero(buffer, sizeof(GLframebuffer));
Brian Paul85d81602002-06-17 23:36:31 +0000643
Brian Paul4d053dd2000-01-14 04:45:47 +0000644 /* sanity checks */
645 if (softwareDepth ) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000646 assert(visual->depthBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000647 }
648 if (softwareStencil) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000649 assert(visual->stencilBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000650 }
651 if (softwareAccum) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000652 assert(visual->rgbMode);
653 assert(visual->accumRedBits > 0);
654 assert(visual->accumGreenBits > 0);
655 assert(visual->accumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000656 }
657 if (softwareAlpha) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000658 assert(visual->rgbMode);
659 assert(visual->alphaBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000660 }
661
Brian Paul75978bd2001-04-27 21:17:20 +0000662 buffer->Visual = *visual;
Brian Paul4d053dd2000-01-14 04:45:47 +0000663 buffer->UseSoftwareDepthBuffer = softwareDepth;
664 buffer->UseSoftwareStencilBuffer = softwareStencil;
665 buffer->UseSoftwareAccumBuffer = softwareAccum;
666 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul4d053dd2000-01-14 04:45:47 +0000667}
668
Keith Whitwell6dc85572003-07-17 13:43:59 +0000669/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000670 * Free a framebuffer struct and its buffers.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000671 *
672 * Calls _mesa_free_framebuffer_data() and frees the structure.
Brian Paul4d053dd2000-01-14 04:45:47 +0000673 */
Brian Paul178a1c52000-04-22 01:05:00 +0000674void
Brian Paulb1394fa2000-09-26 20:53:53 +0000675_mesa_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000676{
677 if (buffer) {
Brian Paul75978bd2001-04-27 21:17:20 +0000678 _mesa_free_framebuffer_data(buffer);
Brian Paul4d053dd2000-01-14 04:45:47 +0000679 FREE(buffer);
680 }
681}
682
Keith Whitwell6dc85572003-07-17 13:43:59 +0000683/**
684 * Free the data hanging off of \p buffer, but not \p buffer itself.
685 *
686 * \param buffer framebuffer.
687 *
688 * Frees all the buffers associated with the structure.
Brian Paul75978bd2001-04-27 21:17:20 +0000689 */
690void
691_mesa_free_framebuffer_data( GLframebuffer *buffer )
692{
693 if (!buffer)
694 return;
695
Brian Paul87506682003-05-27 15:20:43 +0000696 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
Brian Paulaeb44342002-03-19 16:47:04 +0000697 MESA_PBUFFER_FREE( buffer->DepthBuffer );
Brian Paul75978bd2001-04-27 21:17:20 +0000698 buffer->DepthBuffer = NULL;
699 }
Brian Paul87506682003-05-27 15:20:43 +0000700 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
Brian Paulaeb44342002-03-19 16:47:04 +0000701 MESA_PBUFFER_FREE( buffer->Accum );
Brian Paul75978bd2001-04-27 21:17:20 +0000702 buffer->Accum = NULL;
703 }
Brian Paul87506682003-05-27 15:20:43 +0000704 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
Brian Paulaeb44342002-03-19 16:47:04 +0000705 MESA_PBUFFER_FREE( buffer->Stencil );
Brian Paul75978bd2001-04-27 21:17:20 +0000706 buffer->Stencil = NULL;
707 }
Brian Paul87506682003-05-27 15:20:43 +0000708 if (buffer->UseSoftwareAlphaBuffers){
709 if (buffer->FrontLeftAlpha) {
710 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
711 buffer->FrontLeftAlpha = NULL;
712 }
713 if (buffer->BackLeftAlpha) {
714 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
715 buffer->BackLeftAlpha = NULL;
716 }
717 if (buffer->FrontRightAlpha) {
718 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
719 buffer->FrontRightAlpha = NULL;
720 }
721 if (buffer->BackRightAlpha) {
722 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
723 buffer->BackRightAlpha = NULL;
724 }
Brian Paul75978bd2001-04-27 21:17:20 +0000725 }
726}
727
Keith Whitwell6dc85572003-07-17 13:43:59 +0000728/*@}*/
Brian Paul75978bd2001-04-27 21:17:20 +0000729
Brian Paul4d053dd2000-01-14 04:45:47 +0000730
731/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000732/** \name Context allocation, initialization, destroying
733 *
734 * The purpose of the most initialization functions here is to provide the
735 * default state values according to the OpenGL specification.
736 */
jtgafb833d1999-08-19 00:55:39 +0000737/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000738/*@{*/
jtgafb833d1999-08-19 00:55:39 +0000739
Keith Whitwell6dc85572003-07-17 13:43:59 +0000740/**
741 * One-time initialization mutex lock.
742 *
743 * \sa Used by one_time_init().
744 */
Brian Paul9560f052000-01-31 23:11:39 +0000745_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
746
Keith Whitwell6dc85572003-07-17 13:43:59 +0000747/**
748 * Calls all the various one-time-init functions in Mesa.
749 *
750 * While holding a global mutex lock, calls several initialization functions,
751 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
752 * defined.
753 *
754 * \sa _mesa_init_lists(), _math_init().
jtgafb833d1999-08-19 00:55:39 +0000755 */
Brian Paul178a1c52000-04-22 01:05:00 +0000756static void
Brian Paul4753d602002-06-15 02:38:15 +0000757one_time_init( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000758{
759 static GLboolean alreadyCalled = GL_FALSE;
Brian Paul9560f052000-01-31 23:11:39 +0000760 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000761 if (!alreadyCalled) {
Brian Paul27558a12003-03-01 01:50:20 +0000762 GLuint i;
763
Brian Paul4d053dd2000-01-14 04:45:47 +0000764 /* do some implementation tests */
765 assert( sizeof(GLbyte) == 1 );
766 assert( sizeof(GLshort) >= 2 );
767 assert( sizeof(GLint) >= 4 );
768 assert( sizeof(GLubyte) == 1 );
769 assert( sizeof(GLushort) >= 2 );
770 assert( sizeof(GLuint) >= 4 );
771
Brian Paul08836342001-03-03 20:33:27 +0000772 _mesa_init_lists();
Keith Whitwell23caf202000-11-16 21:05:34 +0000773
Keith Whitwell6dc85572003-07-17 13:43:59 +0000774#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000775 _math_init();
Brian Paul27558a12003-03-01 01:50:20 +0000776
777 for (i = 0; i < 256; i++) {
778 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
779 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000780#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000781
davem69775355a2001-06-05 23:54:00 +0000782#ifdef USE_SPARC_ASM
783 _mesa_init_sparc_glapi_relocs();
784#endif
Brian Paul3c634522002-10-24 23:57:19 +0000785 if (_mesa_getenv("MESA_DEBUG")) {
Brian Paul68ee4bc2000-01-28 19:02:22 +0000786 _glapi_noop_enable_warnings(GL_TRUE);
Brian Paul71072be2002-10-10 00:22:13 +0000787#ifndef GLX_DIRECT_RENDERING
788 /* libGL from before 2002/06/28 don't have this function. Someday,
789 * when newer libGL libs are common, remove the #ifdef test. This
790 * only serves to print warnings when calling undefined GL functions.
791 */
Brian Paul4e9676f2002-06-29 19:48:15 +0000792 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
Brian Paul71072be2002-10-10 00:22:13 +0000793#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000794 }
795 else {
796 _glapi_noop_enable_warnings(GL_FALSE);
797 }
798
jtgafb833d1999-08-19 00:55:39 +0000799#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
Brian Paul4e9676f2002-06-29 19:48:15 +0000800 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
jtgafb833d1999-08-19 00:55:39 +0000801#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000802
803 alreadyCalled = GL_TRUE;
804 }
Brian Paul9560f052000-01-31 23:11:39 +0000805 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000806}
807
Keith Whitwell6dc85572003-07-17 13:43:59 +0000808/**
jtgafb833d1999-08-19 00:55:39 +0000809 * Allocate and initialize a shared context state structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000810 *
811 * \return pointer to a gl_shared_state structure on success, or NULL on
812 * failure.
813 *
814 * Initializes the display list, texture objects and vertex programs hash
815 * tables, allocates the texture objects. If it runs out of memory, frees
816 * everything already allocated before returning NULL.
jtgafb833d1999-08-19 00:55:39 +0000817 */
Brian Paula3f13702003-04-01 16:41:50 +0000818static GLboolean
819alloc_shared_state( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000820{
Brian Paula3f13702003-04-01 16:41:50 +0000821 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000822 if (!ss)
Brian Paula3f13702003-04-01 16:41:50 +0000823 return GL_FALSE;
824
825 ctx->Shared = ss;
jtgafb833d1999-08-19 00:55:39 +0000826
Brian Paule4b684c2000-09-12 21:07:40 +0000827 _glthread_INIT_MUTEX(ss->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000828
Brian Paule4b684c2000-09-12 21:07:40 +0000829 ss->DisplayList = _mesa_NewHashTable();
Brian Paulbb797902000-01-24 16:19:54 +0000830 ss->TexObjects = _mesa_NewHashTable();
Brian Paul451f3102003-04-17 01:48:19 +0000831#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
Brian Paul610d5992003-01-14 04:55:45 +0000832 ss->Programs = _mesa_NewHashTable();
Brian Paul8dfc5b92002-10-16 17:57:51 +0000833#endif
jtgafb833d1999-08-19 00:55:39 +0000834
Brian Paul451f3102003-04-17 01:48:19 +0000835#if FEATURE_ARB_vertex_program
836 ss->DefaultVertexProgram = _mesa_alloc_program(ctx, GL_VERTEX_PROGRAM_ARB, 0);
837 if (!ss->DefaultVertexProgram)
838 goto cleanup;
839#endif
840#if FEATURE_ARB_fragment_program
841 ss->DefaultFragmentProgram = _mesa_alloc_program(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
842 if (!ss->DefaultFragmentProgram)
843 goto cleanup;
844#endif
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
Brian Paula3f13702003-04-01 16:41:50 +0000901 if (ss->Default1D)
902 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
903 if (ss->Default2D)
904 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
905 if (ss->Default3D)
906 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
907 if (ss->DefaultCubeMap)
908 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
909 if (ss->DefaultRect)
910 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
911 if (ss)
912 _mesa_free(ss);
913 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000914}
915
Keith Whitwell6dc85572003-07-17 13:43:59 +0000916/**
jtgafb833d1999-08-19 00:55:39 +0000917 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000918 *
919 * \param ctx GL context.
920 * \param ss shared state pointer.
921 *
922 * Frees the display lists, the texture objects (calling the driver texture
923 * deletion callback to free its private data) and the vertex programs, as well
924 * as their hash tables.
925 *
926 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000927 */
Brian Paul178a1c52000-04-22 01:05:00 +0000928static void
929free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000930{
931 /* Free display lists */
932 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000933 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000934 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000935 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000936 }
937 else {
938 break;
939 }
940 }
Brian Paulbb797902000-01-24 16:19:54 +0000941 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000942
943 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000944 ASSERT(ctx->Driver.DeleteTexture);
945 while (1) {
946 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
947 if (texName) {
948 struct gl_texture_object *texObj = (struct gl_texture_object *)
949 _mesa_HashLookup(ss->TexObjects, texName);
950 ASSERT(texObj);
951 (*ctx->Driver.DeleteTexture)(ctx, texObj);
952 _mesa_HashRemove(ss->TexObjects, texName);
953 }
954 else {
955 break;
956 }
jtgafb833d1999-08-19 00:55:39 +0000957 }
Brian Paulbb797902000-01-24 16:19:54 +0000958 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +0000959
Brian Paul8dfc5b92002-10-16 17:57:51 +0000960#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +0000961 /* Free vertex programs */
962 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +0000963 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +0000964 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +0000965 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
966 prog);
967 ASSERT(p);
968 _mesa_delete_program(ctx, p);
969 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +0000970 }
971 else {
972 break;
973 }
974 }
Brian Paul610d5992003-01-14 04:55:45 +0000975 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +0000976#endif
Brian Paul30f51ae2001-12-18 04:06:44 +0000977
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 );
1091 _mesa_init_color( ctx );
1092 _mesa_init_colortable( ctx );
1093 _mesa_init_current( ctx );
1094 _mesa_init_depth( ctx );
1095 _mesa_init_debug( ctx );
1096 _mesa_init_display_list( ctx );
1097 _mesa_init_eval( ctx );
1098 _mesa_init_feedback( ctx );
1099 _mesa_init_fog( ctx );
1100 _mesa_init_histogram( ctx );
1101 _mesa_init_hint( ctx );
1102 _mesa_init_line( ctx );
1103 _mesa_init_lighting( ctx );
1104 _mesa_init_matrix( ctx );
1105 _mesa_init_pixel( ctx );
1106 _mesa_init_point( ctx );
1107 _mesa_init_polygon( ctx );
1108 _mesa_init_rastpos( ctx );
1109 _mesa_init_stencil( ctx );
1110 _mesa_init_transform( ctx );
1111 _mesa_init_varray( ctx );
1112 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001113
Keith Whitwell6dc85572003-07-17 13:43:59 +00001114 if (!_mesa_init_texture( ctx ))
1115 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001116
Brian Paul4d053dd2000-01-14 04:45:47 +00001117 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001118 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001119 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paul4d053dd2000-01-14 04:45:47 +00001120 ctx->CatchSignals = GL_TRUE;
Brian Paulf782b812002-10-04 17:37:45 +00001121 ctx->_Facing = 0;
Brian Paul4d053dd2000-01-14 04:45:47 +00001122
Brian Paula3f13702003-04-01 16:41:50 +00001123 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001124}
1125
1126
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001127
Brian Paulde4f4602003-07-03 02:15:06 +00001128/**
Keith Whitwell6dc85572003-07-17 13:43:59 +00001129 * Initialize a GLcontext struct.
1130 *
1131 * This includes allocating all the other structs and arrays which hang off of
1132 * the context by pointers.
1133 *
1134 * \sa _mesa_create_context() for the parameter description.
1135 *
1136 * Performs the imports and exports callback tables initialization, and
1137 * miscellaneous one-time initializations. If no shared context is supplied one
1138 * is allocated, and increase its reference count. Setups the GL API dispatch
1139 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1140 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1141 * for debug flags.
1142 *
Brian Paulde4f4602003-07-03 02:15:06 +00001143 * \note the direct parameter is ignored (obsolete).
jtgafb833d1999-08-19 00:55:39 +00001144 */
Brian Paul178a1c52000-04-22 01:05:00 +00001145GLboolean
1146_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001147 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001148 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001149 void *driver_ctx,
1150 GLboolean direct )
jtgafb833d1999-08-19 00:55:39 +00001151{
Brian Paul5fb84d22000-05-24 15:04:45 +00001152 GLuint dispatchSize;
1153
Brian Paul3c634522002-10-24 23:57:19 +00001154 ASSERT(driver_ctx);
jtgafb833d1999-08-19 00:55:39 +00001155
Brian Paul3c634522002-10-24 23:57:19 +00001156 /* If the driver wants core Mesa to use special imports, it'll have to
1157 * override these defaults.
1158 */
1159 _mesa_init_default_imports( &(ctx->imports), driver_ctx );
jtgafb833d1999-08-19 00:55:39 +00001160
Brian Paul9a33a112002-06-13 04:28:29 +00001161 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001162 _mesa_init_default_exports( &(ctx->exports) );
1163
1164 /* misc one-time initializations */
1165 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001166
Brian Paul3c634522002-10-24 23:57:19 +00001167 ctx->DriverCtx = driver_ctx;
Brian Paulb1394fa2000-09-26 20:53:53 +00001168 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001169 ctx->DrawBuffer = NULL;
1170 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001171
Brian Paula3f13702003-04-01 16:41:50 +00001172 /* Set these pointers to defaults now in case they're not set since
1173 * we need them while creating the default textures.
1174 */
1175 if (!ctx->Driver.NewTextureObject)
1176 ctx->Driver.NewTextureObject = _mesa_new_texture_object;
1177 if (!ctx->Driver.DeleteTexture)
1178 ctx->Driver.DeleteTexture = _mesa_delete_texture_object;
1179 if (!ctx->Driver.NewTextureImage)
1180 ctx->Driver.NewTextureImage = _mesa_new_texture_image;
1181
jtgafb833d1999-08-19 00:55:39 +00001182 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001183 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001184 ctx->Shared = share_list->Shared;
1185 }
1186 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001187 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001188 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001189 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001190 }
1191 }
Brian Paul9560f052000-01-31 23:11:39 +00001192 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001193 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001194 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001195
Keith Whitwell6dc85572003-07-17 13:43:59 +00001196 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001197 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001198 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001199 }
jtgafb833d1999-08-19 00:55:39 +00001200
Keith Whitwell6dc85572003-07-17 13:43:59 +00001201
1202 /* TODO: move this to somewhere program-specific.
Brian Paul8ad10762002-10-11 17:41:03 +00001203 */
Brian Paul8ad10762002-10-11 17:41:03 +00001204 _glapi_add_entrypoint("glAreProgramsResidentNV", 578);
1205 _glapi_add_entrypoint("glBindProgramNV", 579);
1206 _glapi_add_entrypoint("glDeleteProgramsNV", 580);
1207 _glapi_add_entrypoint("glExecuteProgramNV", 581);
1208 _glapi_add_entrypoint("glGenProgramsNV", 582);
1209 _glapi_add_entrypoint("glGetProgramParameterdvNV", 583);
1210 _glapi_add_entrypoint("glGetProgramParameterfvNV", 584);
1211 _glapi_add_entrypoint("glGetProgramivNV", 585);
1212 _glapi_add_entrypoint("glGetProgramStringNV", 586);
1213 _glapi_add_entrypoint("glGetTrackMatrixivNV", 587);
1214 _glapi_add_entrypoint("glGetVertexAttribdvNV", 588);
1215 _glapi_add_entrypoint("glGetVertexAttribfvNV", 589);
1216 _glapi_add_entrypoint("glGetVertexAttribivNV", 590);
1217 _glapi_add_entrypoint("glGetVertexAttribPointervNV", 591);
1218 _glapi_add_entrypoint("glIsProgramNV", 592);
1219 _glapi_add_entrypoint("glLoadProgramNV", 593);
1220 _glapi_add_entrypoint("glProgramParameter4dNV", 594);
1221 _glapi_add_entrypoint("glProgramParameter4dvNV", 595);
1222 _glapi_add_entrypoint("glProgramParameter4fNV", 596);
1223 _glapi_add_entrypoint("glProgramParameter4fvNV", 597);
1224 _glapi_add_entrypoint("glProgramParameters4dvNV", 598);
1225 _glapi_add_entrypoint("glProgramParameters4fvNV", 599);
1226 _glapi_add_entrypoint("glRequestResidentProgramsNV", 600);
1227 _glapi_add_entrypoint("glTrackMatrixNV", 601);
1228 _glapi_add_entrypoint("glVertexAttribPointerNV", 602);
1229 _glapi_add_entrypoint("glVertexAttrib1dNV", 603);
1230 _glapi_add_entrypoint("glVertexAttrib1dvNV", 604);
1231 _glapi_add_entrypoint("glVertexAttrib1fNV", 605);
1232 _glapi_add_entrypoint("glVertexAttrib1fvNV", 606);
1233 _glapi_add_entrypoint("glVertexAttrib1sNV", 607);
1234 _glapi_add_entrypoint("glVertexAttrib1svNV", 608);
1235 _glapi_add_entrypoint("glVertexAttrib2dNV", 609);
1236 _glapi_add_entrypoint("glVertexAttrib2dvNV", 610);
1237 _glapi_add_entrypoint("glVertexAttrib2fNV", 611);
1238 _glapi_add_entrypoint("glVertexAttrib2fvNV", 612);
1239 _glapi_add_entrypoint("glVertexAttrib2sNV", 613);
1240 _glapi_add_entrypoint("glVertexAttrib2svNV", 614);
1241 _glapi_add_entrypoint("glVertexAttrib3dNV", 615);
1242 _glapi_add_entrypoint("glVertexAttrib3dvNV", 616);
1243 _glapi_add_entrypoint("glVertexAttrib3fNV", 617);
1244 _glapi_add_entrypoint("glVertexAttrib3fvNV", 618);
1245 _glapi_add_entrypoint("glVertexAttrib3sNV", 619);
1246 _glapi_add_entrypoint("glVertexAttrib3svNV", 620);
1247 _glapi_add_entrypoint("glVertexAttrib4dNV", 621);
1248 _glapi_add_entrypoint("glVertexAttrib4dvNV", 622);
1249 _glapi_add_entrypoint("glVertexAttrib4fNV", 623);
1250 _glapi_add_entrypoint("glVertexAttrib4fvNV", 624);
1251 _glapi_add_entrypoint("glVertexAttrib4sNV", 625);
1252 _glapi_add_entrypoint("glVertexAttrib4svNV", 626);
1253 _glapi_add_entrypoint("glVertexAttrib4ubNV", 627);
1254 _glapi_add_entrypoint("glVertexAttrib4ubvNV", 628);
1255 _glapi_add_entrypoint("glVertexAttribs1dvNV", 629);
1256 _glapi_add_entrypoint("glVertexAttribs1fvNV", 630);
1257 _glapi_add_entrypoint("glVertexAttribs1svNV", 631);
1258 _glapi_add_entrypoint("glVertexAttribs2dvNV", 632);
1259 _glapi_add_entrypoint("glVertexAttribs2fvNV", 633);
1260 _glapi_add_entrypoint("glVertexAttribs2svNV", 634);
1261 _glapi_add_entrypoint("glVertexAttribs3dvNV", 635);
1262 _glapi_add_entrypoint("glVertexAttribs3fvNV", 636);
1263 _glapi_add_entrypoint("glVertexAttribs3svNV", 637);
1264 _glapi_add_entrypoint("glVertexAttribs4dvNV", 638);
1265 _glapi_add_entrypoint("glVertexAttribs4fvNV", 639);
1266 _glapi_add_entrypoint("glVertexAttribs4svNV", 640);
1267 _glapi_add_entrypoint("glVertexAttribs4ubvNV", 641);
1268 _glapi_add_entrypoint("glPointParameteriNV", 642);
1269 _glapi_add_entrypoint("glPointParameterivNV", 643);
1270 _glapi_add_entrypoint("glMultiDrawArraysEXT", 644);
1271 _glapi_add_entrypoint("glMultiDrawElementsEXT", 645);
1272 _glapi_add_entrypoint("glActiveStencilFaceEXT", 646);
1273 _glapi_add_entrypoint("glDeleteFencesNV", 647);
1274 _glapi_add_entrypoint("glGenFencesNV", 648);
1275 _glapi_add_entrypoint("glIsFenceNV", 649);
1276 _glapi_add_entrypoint("glTestFenceNV", 650);
1277 _glapi_add_entrypoint("glGetFenceivNV", 651);
1278 _glapi_add_entrypoint("glFinishFenceNV", 652);
1279 _glapi_add_entrypoint("glSetFenceNV", 653);
Brian Paula3f13702003-04-01 16:41:50 +00001280 /* XXX add NV_fragment_program and ARB_vertex_program functions */
Brian Paulf59afc92000-05-23 23:23:00 +00001281
Keith Whitwell6dc85572003-07-17 13:43:59 +00001282
Brian Paul5fb84d22000-05-24 15:04:45 +00001283 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1284 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1285 * Mesa we do this to accomodate different versions of libGL and various
1286 * DRI drivers.
1287 */
1288 dispatchSize = MAX2(_glapi_get_dispatch_table_size(),
1289 sizeof(struct _glapi_table) / sizeof(void *));
1290
Brian Paulfbd8f211999-11-11 01:22:25 +00001291 /* setup API dispatch tables */
Brian Paul5fb84d22000-05-24 15:04:45 +00001292 ctx->Exec = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
1293 ctx->Save = (struct _glapi_table *) CALLOC(dispatchSize * sizeof(void*));
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001294 if (!ctx->Exec || !ctx->Save) {
1295 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001296 if (ctx->Exec)
Brian Paul2d8db392000-06-27 22:10:00 +00001297 FREE( ctx->Exec );
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001298 }
Brian Paul5fb84d22000-05-24 15:04:45 +00001299 _mesa_init_exec_table(ctx->Exec, dispatchSize);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001300 ctx->CurrentDispatch = ctx->Exec;
jtgafb833d1999-08-19 00:55:39 +00001301
Keith Whitwell6dc85572003-07-17 13:43:59 +00001302#if _HAVE_FULL_GL
1303 _mesa_init_dlist_table(ctx->Save, dispatchSize);
1304
Keith Whitwellad2ac212000-11-24 10:25:05 +00001305 ctx->ExecPrefersFloat = GL_FALSE;
1306 ctx->SavePrefersFloat = GL_FALSE;
1307
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001308 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001309 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001310 ctx->TnlModule.Current = NULL;
1311 ctx->TnlModule.SwapCount = 0;
1312
Keith Whitwell6dc85572003-07-17 13:43:59 +00001313#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001314
Brian Paul4d053dd2000-01-14 04:45:47 +00001315 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001316}
1317
Brian Paulde4f4602003-07-03 02:15:06 +00001318/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001319 * Allocate and initialize a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001320 *
1321 * \param visual a GLvisual pointer (we copy the struct contents)
1322 * \param share_list another context to share display lists with or NULL
1323 * \param driver_ctx pointer to device driver's context state struct
1324 * \param direct obsolete, ignored
1325 *
1326 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001327 */
Brian Paul178a1c52000-04-22 01:05:00 +00001328GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001329_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001330 GLcontext *share_list,
Brian Paul3c634522002-10-24 23:57:19 +00001331 void *driver_ctx,
1332 GLboolean direct )
1333
Brian Paul4d053dd2000-01-14 04:45:47 +00001334{
Brian Paul4753d602002-06-15 02:38:15 +00001335 GLcontext *ctx;
1336
1337 ASSERT(visual);
Brian Paul3c634522002-10-24 23:57:19 +00001338 ASSERT(driver_ctx);
Brian Paul4753d602002-06-15 02:38:15 +00001339
Brian Paul3c634522002-10-24 23:57:19 +00001340 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001341 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001342 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001343
Brian Paul3c634522002-10-24 23:57:19 +00001344 if (_mesa_initialize_context(ctx, visual, share_list, driver_ctx, direct)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001345 return ctx;
1346 }
1347 else {
Brian Paul3c634522002-10-24 23:57:19 +00001348 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001349 return NULL;
1350 }
1351}
1352
Keith Whitwell6dc85572003-07-17 13:43:59 +00001353/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001354 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001355 *
1356 * But doesn't free the GLcontext struct itself.
1357 *
1358 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001359 */
Brian Paul178a1c52000-04-22 01:05:00 +00001360void
Brian Paulb1394fa2000-09-26 20:53:53 +00001361_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001362{
Brian Paul4d053dd2000-01-14 04:45:47 +00001363 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001364 if (ctx == _mesa_get_current_context()) {
1365 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001366 }
1367
Keith Whitwell6dc85572003-07-17 13:43:59 +00001368 _mesa_free_lighting_data( ctx );
1369 _mesa_free_eval_data( ctx );
1370 _mesa_free_texture_data( ctx );
1371 _mesa_free_matrix_data( ctx );
1372 _mesa_free_viewport_data( ctx );
1373 _mesa_free_colortable_data( ctx );
Brian Paul8dfc5b92002-10-16 17:57:51 +00001374#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +00001375 if (ctx->VertexProgram.Current) {
Brian Paul610d5992003-01-14 04:55:45 +00001376 ctx->VertexProgram.Current->Base.RefCount--;
1377 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001378 _mesa_delete_program(ctx, &(ctx->VertexProgram.Current->Base));
Brian Paul610d5992003-01-14 04:55:45 +00001379 }
1380#endif
1381#if FEATURE_NV_fragment_program
1382 if (ctx->FragmentProgram.Current) {
1383 ctx->FragmentProgram.Current->Base.RefCount--;
1384 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
Brian Paul451f3102003-04-17 01:48:19 +00001385 _mesa_delete_program(ctx, &(ctx->FragmentProgram.Current->Base));
Brian Paulfd284452001-07-19 15:54:34 +00001386 }
Brian Paul8dfc5b92002-10-16 17:57:51 +00001387#endif
Brian Paulfd284452001-07-19 15:54:34 +00001388
Brian Paul30f51ae2001-12-18 04:06:44 +00001389 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001390 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001391 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001392 assert(ctx->Shared->RefCount >= 0);
1393 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1394 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001395 /* free shared state */
1396 free_shared_state( ctx, ctx->Shared );
1397 }
1398
Brian Paul702ca202003-07-18 15:22:16 +00001399 if (ctx->Extensions.String)
1400 FREE((void *) ctx->Extensions.String);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001401
1402 FREE(ctx->Exec);
1403 FREE(ctx->Save);
Brian Paul4d053dd2000-01-14 04:45:47 +00001404}
1405
Keith Whitwell6dc85572003-07-17 13:43:59 +00001406/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001407 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001408 *
1409 * \param ctx GL context.
1410 *
1411 * Calls _mesa_free_context_data() and free the structure.
jtgafb833d1999-08-19 00:55:39 +00001412 */
Brian Paul178a1c52000-04-22 01:05:00 +00001413void
Brian Paulb1394fa2000-09-26 20:53:53 +00001414_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001415{
1416 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001417 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001418 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001419 }
1420}
1421
Keith Whitwell6dc85572003-07-17 13:43:59 +00001422#if _HAVE_FULL_GL
1423/**
jtgafb833d1999-08-19 00:55:39 +00001424 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001425 *
1426 * \param src source context
1427 * \param dst destination context
1428 * \param mask bitwise OR of GL_*_BIT flags
1429 *
1430 * According to the bits specified in \p mask, copies the corresponding
1431 * attributes from \p src into \dst. For many of the attributes a simple \c
1432 * memcpy is not enough due to the existence of internal pointers in their data
1433 * structures.
jtgafb833d1999-08-19 00:55:39 +00001434 */
Brian Paul178a1c52000-04-22 01:05:00 +00001435void
Brian Paulb1394fa2000-09-26 20:53:53 +00001436_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001437{
1438 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001439 /* OK to memcpy */
1440 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001441 }
1442 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001443 /* OK to memcpy */
1444 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001445 }
1446 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001447 /* OK to memcpy */
1448 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001449 }
1450 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001451 /* OK to memcpy */
1452 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001453 }
1454 if (mask & GL_ENABLE_BIT) {
1455 /* no op */
1456 }
1457 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001458 /* OK to memcpy */
1459 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001460 }
1461 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001462 /* OK to memcpy */
1463 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001464 }
1465 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001466 /* OK to memcpy */
1467 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001468 }
1469 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001470 GLuint i;
1471 /* begin with memcpy */
1472 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1473 /* fixup linked lists to prevent pointer insanity */
1474 make_empty_list( &(dst->Light.EnabledList) );
1475 for (i = 0; i < MAX_LIGHTS; i++) {
1476 if (dst->Light.Light[i].Enabled) {
1477 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1478 }
1479 }
jtgafb833d1999-08-19 00:55:39 +00001480 }
1481 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001482 /* OK to memcpy */
1483 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001484 }
1485 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001486 /* OK to memcpy */
1487 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001488 }
1489 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001490 /* OK to memcpy */
1491 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001492 }
1493 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001494 /* OK to memcpy */
1495 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001496 }
1497 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001498 /* OK to memcpy */
1499 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001500 }
1501 if (mask & GL_POLYGON_STIPPLE_BIT) {
1502 /* Use loop instead of MEMCPY due to problem with Portland Group's
1503 * C compiler. Reported by John Stone.
1504 */
Brian Paul85d81602002-06-17 23:36:31 +00001505 GLuint i;
1506 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001507 dst->PolygonStipple[i] = src->PolygonStipple[i];
1508 }
1509 }
1510 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001511 /* OK to memcpy */
1512 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001513 }
1514 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001515 /* OK to memcpy */
1516 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001517 }
1518 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001519 /* Cannot memcpy because of pointers */
1520 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001521 }
1522 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001523 /* OK to memcpy */
1524 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001525 }
1526 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001527 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1528 dst->Viewport.X = src->Viewport.X;
1529 dst->Viewport.Y = src->Viewport.Y;
1530 dst->Viewport.Width = src->Viewport.Width;
1531 dst->Viewport.Height = src->Viewport.Height;
1532 dst->Viewport.Near = src->Viewport.Near;
1533 dst->Viewport.Far = src->Viewport.Far;
1534 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001535 }
Brian Paul85d81602002-06-17 23:36:31 +00001536
Keith Whitwella96308c2000-10-30 13:31:59 +00001537 /* XXX FIXME: Call callbacks?
1538 */
1539 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001540}
Keith Whitwell23caf202000-11-16 21:05:34 +00001541#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001542
1543
Brian Paulb1d53d92003-06-11 18:48:19 +00001544/**
1545 * Check if the given context can render into the given framebuffer
1546 * by checking visual attributes.
1547 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1548 */
1549static GLboolean
1550check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1551{
1552 const GLvisual *ctxvis = &ctx->Visual;
1553 const GLvisual *bufvis = &buffer->Visual;
1554
1555 if (ctxvis == bufvis)
1556 return GL_TRUE;
1557
1558 if (ctxvis->rgbMode != bufvis->rgbMode)
1559 return GL_FALSE;
1560 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1561 return GL_FALSE;
1562 if (ctxvis->stereoMode && !bufvis->stereoMode)
1563 return GL_FALSE;
1564 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1565 return GL_FALSE;
1566 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1567 return GL_FALSE;
1568 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1569 return GL_FALSE;
1570 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1571 return GL_FALSE;
1572 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1573 return GL_FALSE;
1574 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1575 return GL_FALSE;
1576 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1577 return GL_FALSE;
1578 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1579 return GL_FALSE;
1580
1581 return GL_TRUE;
1582}
1583
1584
Keith Whitwell6dc85572003-07-17 13:43:59 +00001585/**
Brian Paul9a33a112002-06-13 04:28:29 +00001586 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001587 *
1588 * \param newCtx new GL context.
1589 * \param buffer framebuffer.
1590 *
1591 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001592 */
1593void
1594_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1595{
1596 _mesa_make_current2( newCtx, buffer, buffer );
1597}
1598
Keith Whitwell6dc85572003-07-17 13:43:59 +00001599/**
1600 * Bind the given context to the given draw-buffer and read-buffer and
1601 * make it the current context for this thread.
1602 *
1603 * \param newCtx new GL context. If NULL then there will be no current GL
1604 * context.
1605 * \param drawBuffer draw framebuffer.
1606 * \param readBuffer read framebuffer.
1607 *
1608 * Check that the context's and framebuffer's visuals are compatible, returning
1609 * immediately otherwise. Sets the glapi current context via
1610 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1611 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1612 * Calls dd_function_table::MakeCurrent callback if defined.
1613 *
1614 * When a context is bound by the first time and the \c MESA_INFO environment
1615 * variable is set it calls print_info() as an aid for remote user
1616 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001617 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001618void
1619_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1620 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001621{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001622 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001623 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001624
Brian Paulbe3602d2001-02-28 00:27:48 +00001625 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001626 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001627 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1628 if (!check_compatible(newCtx, drawBuffer))
1629 return;
1630 }
1631 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1632 if (!check_compatible(newCtx, readBuffer))
1633 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001634 }
1635
Brian Paul00037781999-12-17 14:52:35 +00001636 /* We call this function periodically (just here for now) in
1637 * order to detect when multithreading has begun.
1638 */
1639 _glapi_check_multithread();
1640
Brian Paulf9b97d92000-01-28 20:17:42 +00001641 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001642 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001643
1644
1645 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001646 _glapi_set_dispatch(NULL); /* none current */
1647 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001648 else {
1649 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001650
Keith Whitwell23caf202000-11-16 21:05:34 +00001651 if (drawBuffer && readBuffer) {
1652 /* TODO: check if newCtx and buffer's visual match??? */
1653 newCtx->DrawBuffer = drawBuffer;
1654 newCtx->ReadBuffer = readBuffer;
1655 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001656
Keith Whitwell6dc85572003-07-17 13:43:59 +00001657#if _HAVE_FULL_GL
Brian Paul10d7f542002-06-17 23:38:14 +00001658 if (drawBuffer->Width == 0 && drawBuffer->Height == 0) {
1659 /* get initial window size */
1660 GLuint bufWidth, bufHeight;
1661
1662 /* ask device driver for size of output buffer */
1663 (*newCtx->Driver.GetBufferSize)( drawBuffer, &bufWidth, &bufHeight );
1664
Keith Whitwell6dc85572003-07-17 13:43:59 +00001665 if (drawBuffer->Width != bufWidth ||
1666 drawBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001667
Keith Whitwell6dc85572003-07-17 13:43:59 +00001668 drawBuffer->Width = bufWidth;
1669 drawBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001670
Keith Whitwell6dc85572003-07-17 13:43:59 +00001671 newCtx->Driver.ResizeBuffers( drawBuffer );
1672 }
Brian Paul10d7f542002-06-17 23:38:14 +00001673 }
1674
1675 if (readBuffer != drawBuffer &&
1676 readBuffer->Width == 0 && readBuffer->Height == 0) {
1677 /* get initial window size */
1678 GLuint bufWidth, bufHeight;
1679
1680 /* ask device driver for size of output buffer */
1681 (*newCtx->Driver.GetBufferSize)( readBuffer, &bufWidth, &bufHeight );
1682
Keith Whitwell6dc85572003-07-17 13:43:59 +00001683 if (readBuffer->Width != bufWidth ||
1684 readBuffer->Height != bufHeight) {
Brian Paul10d7f542002-06-17 23:38:14 +00001685
Keith Whitwell6dc85572003-07-17 13:43:59 +00001686 readBuffer->Width = bufWidth;
1687 readBuffer->Height = bufHeight;
Brian Paul10d7f542002-06-17 23:38:14 +00001688
Keith Whitwell6dc85572003-07-17 13:43:59 +00001689 newCtx->Driver.ResizeBuffers( readBuffer );
1690 }
Brian Paul10d7f542002-06-17 23:38:14 +00001691 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001692#endif
Brian Paul00037781999-12-17 14:52:35 +00001693 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001694
Keith Whitwell6dc85572003-07-17 13:43:59 +00001695 /* Alert the driver - usually passed on to the sw t&l module,
1696 * but also used to detect threaded cases in the radeon codegen
1697 * hw t&l module.
1698 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001699 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001700 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1701
1702 /* We can use this to help debug user's problems. Tell them to set
1703 * the MESA_INFO env variable before running their app. Then the
1704 * first time each context is made current we'll print some useful
1705 * information.
1706 */
1707 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001708 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001709 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001710 }
1711 newCtx->FirstTimeCurrent = GL_FALSE;
1712 }
Brian Paul00037781999-12-17 14:52:35 +00001713 }
1714}
1715
Keith Whitwell6dc85572003-07-17 13:43:59 +00001716/**
1717 * Get current context for the calling thread.
1718 *
1719 * \return pointer to the current GL context.
1720 *
1721 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1722 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00001723 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001724GLcontext *
1725_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00001726{
Brian Paulf9b97d92000-01-28 20:17:42 +00001727 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00001728}
1729
Keith Whitwell6dc85572003-07-17 13:43:59 +00001730/**
1731 * Get context's current API dispatch table.
1732 *
1733 * It'll either be the immediate-mode execute dispatcher or the display list
1734 * compile dispatcher.
1735 *
1736 * \param ctx GL context.
1737 *
1738 * \return pointer to dispatch_table.
1739 *
1740 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00001741 */
1742struct _glapi_table *
1743_mesa_get_dispatch(GLcontext *ctx)
1744{
1745 return ctx->CurrentDispatch;
1746}
1747
Keith Whitwell6dc85572003-07-17 13:43:59 +00001748/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00001749
1750
jtgafb833d1999-08-19 00:55:39 +00001751/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001752/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00001753/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00001754/*@{*/
jtgafb833d1999-08-19 00:55:39 +00001755
Keith Whitwell6dc85572003-07-17 13:43:59 +00001756/**
1757 * Record an error.
1758 *
1759 * \param ctx GL context.
1760 * \param error error code.
1761 *
1762 * Records the given error code and call the driver's dd_function_table::Error
1763 * function if defined.
1764 *
1765 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00001766 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00001767 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001768void
Brian Paul4e9676f2002-06-29 19:48:15 +00001769_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00001770{
Brian Paul18a285a2002-03-16 00:53:15 +00001771 if (!ctx)
1772 return;
1773
Brian Paul7eb06032000-07-14 04:13:40 +00001774 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00001775 ctx->ErrorValue = error;
1776 }
1777
1778 /* Call device driver's error handler, if any. This is used on the Mac. */
1779 if (ctx->Driver.Error) {
1780 (*ctx->Driver.Error)( ctx );
1781 }
1782}
1783
Keith Whitwell6dc85572003-07-17 13:43:59 +00001784/**
1785 * Execute glFinish().
1786 *
1787 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1788 * dd_function_table::Finish driver callback, if not NULL.
1789 */
Brian Paulfa9df402000-02-02 19:16:46 +00001790void
1791_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00001792{
Brian Paulfa9df402000-02-02 19:16:46 +00001793 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001794 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001795 if (ctx->Driver.Finish) {
1796 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001797 }
1798}
1799
Keith Whitwell6dc85572003-07-17 13:43:59 +00001800/**
1801 * Execute glFlush().
1802 *
1803 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1804 * dd_function_table::Flush driver callback, if not NULL.
1805 */
Brian Paulfa9df402000-02-02 19:16:46 +00001806void
1807_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00001808{
Brian Paulfa9df402000-02-02 19:16:46 +00001809 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00001810 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00001811 if (ctx->Driver.Flush) {
1812 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00001813 }
jtgafb833d1999-08-19 00:55:39 +00001814}
Brian Paul48c6a6e2000-09-08 21:28:04 +00001815
1816
Keith Whitwell6dc85572003-07-17 13:43:59 +00001817/*@}*/