blob: cb04a7bab9a955ed07128b489c2f939cad872acc [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 Paulde434842004-11-27 20:14:03 +00009 * Version: 6.3
jtgafb833d1999-08-19 00:55:39 +000010 *
Michal Krol9b3752c2005-01-13 14:08:47 +000011 * Copyright (C) 1999-2005 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/**
Brian Paul253204f2004-09-10 00:45:12 +000033 * \mainpage Mesa Main Module
Keith Whitwell6dc85572003-07-17 13:43:59 +000034 *
Brian Paul253204f2004-09-10 00:45:12 +000035 * \section MainIntroduction Introduction
Keith Whitwell6dc85572003-07-17 13:43:59 +000036 *
Brian Paul253204f2004-09-10 00:45:12 +000037 * The Mesa Main module consists of all the files in the main/ directory.
38 * Among the features of this module are:
39 * <UL>
40 * <LI> Structures to represent most GL state </LI>
41 * <LI> State set/get functions </LI>
42 * <LI> Display lists </LI>
43 * <LI> Texture unit, object and image handling </LI>
44 * <LI> Matrix and attribute stacks </LI>
45 * </UL>
Keith Whitwell6dc85572003-07-17 13:43:59 +000046 *
Brian Paul253204f2004-09-10 00:45:12 +000047 * Other modules are responsible for API dispatch, vertex transformation,
48 * point/line/triangle setup, rasterization, vertex array caching,
49 * vertex/fragment programs/shaders, etc.
Keith Whitwell6dc85572003-07-17 13:43:59 +000050 *
51 *
52 * \section AboutDoxygen About Doxygen
53 *
54 * If you're viewing this information as Doxygen-generated HTML you'll
55 * see the documentation index at the top of this page.
56 *
57 * The first line lists the Mesa source code modules.
58 * The second line lists the indexes available for viewing the documentation
59 * for each module.
60 *
61 * Selecting the <b>Main page</b> link will display a summary of the module
62 * (this page).
63 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000064 * Selecting <b>Data Structures</b> will list all C structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +000065 *
66 * Selecting the <b>File List</b> link will list all the source files in
67 * the module.
68 * Selecting a filename will show a list of all functions defined in that file.
69 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000070 * Selecting the <b>Data Fields</b> link will display a list of all
Keith Whitwell6dc85572003-07-17 13:43:59 +000071 * documented structure members.
72 *
Jose Fonseca1a5709d2003-09-17 17:14:11 +000073 * Selecting the <b>Globals</b> link will display a list
Keith Whitwell6dc85572003-07-17 13:43:59 +000074 * of all functions, structures, global variables and macros in the module.
75 *
76 */
77
78
Brian Paulfbd8f211999-11-11 01:22:25 +000079#include "glheader.h"
Brian Paul3c634522002-10-24 23:57:19 +000080#include "imports.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000081#include "accum.h"
82#include "attrib.h"
83#include "blend.h"
Brian Paulb1394fa2000-09-26 20:53:53 +000084#include "buffers.h"
Brian Paul148a2842003-09-17 03:40:11 +000085#include "bufferobj.h"
Brian Paul4bdcfe52000-04-17 17:57:04 +000086#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000087#include "context.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000088#include "debug.h"
89#include "depth.h"
jtgafb833d1999-08-19 00:55:39 +000090#include "dlist.h"
91#include "eval.h"
92#include "enums.h"
Brian Paul585a68c1999-09-11 11:31:34 +000093#include "extensions.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000094#include "feedback.h"
jtgafb833d1999-08-19 00:55:39 +000095#include "fog.h"
Brian Paulb7a43041999-11-30 20:34:51 +000096#include "get.h"
Brian Paul9560f052000-01-31 23:11:39 +000097#include "glthread.h"
Brian Paulf44898c2003-07-18 15:44:57 +000098#include "glapioffsets.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +000099#include "histogram.h"
100#include "hint.h"
jtgafb833d1999-08-19 00:55:39 +0000101#include "hash.h"
102#include "light.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000103#include "lines.h"
jtgafb833d1999-08-19 00:55:39 +0000104#include "macros.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000105#include "matrix.h"
Brian Paul05944c02003-07-22 03:51:46 +0000106#include "occlude.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000107#include "pixel.h"
108#include "points.h"
109#include "polygon.h"
Brian Paul31732772003-09-18 16:39:09 +0000110#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
111#include "program.h"
112#endif
Keith Whitwell6dc85572003-07-17 13:43:59 +0000113#include "rastpos.h"
jtgafb833d1999-08-19 00:55:39 +0000114#include "simple_list.h"
Brian Paulfa9df402000-02-02 19:16:46 +0000115#include "state.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000116#include "stencil.h"
Brian Paul8f04c122004-04-27 13:39:20 +0000117#include "texcompress.h"
jtgafb833d1999-08-19 00:55:39 +0000118#include "teximage.h"
119#include "texobj.h"
Brian Paul85d81602002-06-17 23:36:31 +0000120#include "texstate.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000121#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +0000122#include "varray.h"
Gareth Hughesd4eb6652001-03-12 01:32:20 +0000123#include "vtxfmt.h"
Keith Whitwell6dc85572003-07-17 13:43:59 +0000124#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000125#include "math/m_translate.h"
Keith Whitwell23caf202000-11-16 21:05:34 +0000126#include "math/m_matrix.h"
127#include "math/m_xform.h"
Keith Whitwellf4b02d12001-01-05 05:31:42 +0000128#include "math/mathmod.h"
Brian Paulddc82ee2005-02-05 19:56:45 +0000129#endif
Michal Krol9b3752c2005-01-13 14:08:47 +0000130#include "shaderobjects.h"
jtgafb833d1999-08-19 00:55:39 +0000131
davem69775355a2001-06-05 23:54:00 +0000132#ifdef USE_SPARC_ASM
Ian Romanicke16f6e32004-06-26 00:02:51 +0000133#include "sparc/sparc.h"
davem69775355a2001-06-05 23:54:00 +0000134#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 */
Brian Paula6c423d2004-08-25 15:59:48 +0000201 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000202 return GL_TRUE;
203}
204
Keith Whitwell6dc85572003-07-17 13:43:59 +0000205/**
206 * Bind context callback.
207 *
208 * \param gc context.
209 * \return GL_TRUE on success, or GL_FALSE on failure.
210 *
211 * \ifnot subset
212 * Called by window system/device driver (via __GLexports::makeCurrent)
Brian Paul9a33a112002-06-13 04:28:29 +0000213 * when the rendering context is made current.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000214 * \endif
215 *
216 * No-op
Brian Paul9a33a112002-06-13 04:28:29 +0000217 */
218GLboolean
219_mesa_makeCurrent(__GLcontext *gc)
220{
221 /* XXX bind context to thread */
Brian Paula6c423d2004-08-25 15:59:48 +0000222 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000223 return GL_TRUE;
224}
225
Keith Whitwell6dc85572003-07-17 13:43:59 +0000226/**
227 * Share context callback.
228 *
229 * \param gc context.
230 * \param gcShare shared context.
231 * \return GL_TRUE on success, or GL_FALSE on failure.
232 *
233 * \ifnot subset
234 * Called by window system/device driver (via __GLexports::shareContext)
235 * \endif
236 *
237 * Update the shared context reference count, gl_shared_state::RefCount.
Brian Paul9a33a112002-06-13 04:28:29 +0000238 */
239GLboolean
240_mesa_shareContext(__GLcontext *gc, __GLcontext *gcShare)
241{
242 if (gc && gcShare && gc->Shared && gcShare->Shared) {
243 gc->Shared->RefCount--;
244 if (gc->Shared->RefCount == 0) {
245 free_shared_state(gc, gc->Shared);
246 }
247 gc->Shared = gcShare->Shared;
248 gc->Shared->RefCount++;
249 return GL_TRUE;
250 }
251 else {
252 return GL_FALSE;
253 }
254}
255
Keith Whitwell6dc85572003-07-17 13:43:59 +0000256
257#if _HAVE_FULL_GL
258/**
259 * Copy context callback.
260 */
Brian Paul9a33a112002-06-13 04:28:29 +0000261GLboolean
262_mesa_copyContext(__GLcontext *dst, const __GLcontext *src, GLuint mask)
263{
264 if (dst && src) {
265 _mesa_copy_context( src, dst, mask );
266 return GL_TRUE;
267 }
268 else {
269 return GL_FALSE;
270 }
271}
Keith Whitwell6dc85572003-07-17 13:43:59 +0000272#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000273
Keith Whitwell6dc85572003-07-17 13:43:59 +0000274/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000275GLboolean
276_mesa_forceCurrent(__GLcontext *gc)
277{
Brian Paula6c423d2004-08-25 15:59:48 +0000278 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000279 return GL_TRUE;
280}
281
Keith Whitwell6dc85572003-07-17 13:43:59 +0000282/**
283 * Windows/buffer resizing notification callback.
284 *
285 * \param gc GL context.
286 * \return GL_TRUE on success, or GL_FALSE on failure.
287 */
Brian Paul9a33a112002-06-13 04:28:29 +0000288GLboolean
289_mesa_notifyResize(__GLcontext *gc)
290{
291 GLint x, y;
292 GLuint width, height;
293 __GLdrawablePrivate *d = gc->imports.getDrawablePrivate(gc);
294 if (!d || !d->getDrawableSize)
295 return GL_FALSE;
296 d->getDrawableSize( d, &x, &y, &width, &height );
297 /* update viewport, resize software buffers, etc. */
298 return GL_TRUE;
299}
300
Keith Whitwell6dc85572003-07-17 13:43:59 +0000301/**
302 * Window/buffer destruction notification callback.
303 *
304 * \param gc GL context.
305 *
306 * Called when the context's window/buffer is going to be destroyed.
307 *
308 * No-op
309 */
Brian Paul9a33a112002-06-13 04:28:29 +0000310void
311_mesa_notifyDestroy(__GLcontext *gc)
312{
Brian Paul60b6e4f2002-10-14 17:08:17 +0000313 /* Unbind from it. */
Brian Paula6c423d2004-08-25 15:59:48 +0000314 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000315}
316
Keith Whitwell6dc85572003-07-17 13:43:59 +0000317/**
318 * Swap buffers notification callback.
319 *
320 * \param gc GL context.
321 *
322 * Called by window system just before swapping buffers.
Brian Paul9a33a112002-06-13 04:28:29 +0000323 * We have to finish any pending rendering.
324 */
325void
326_mesa_notifySwapBuffers(__GLcontext *gc)
327{
328 FLUSH_VERTICES( gc, 0 );
329}
330
Keith Whitwell6dc85572003-07-17 13:43:59 +0000331/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000332struct __GLdispatchStateRec *
333_mesa_dispatchExec(__GLcontext *gc)
334{
Brian Paula6c423d2004-08-25 15:59:48 +0000335 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000336 return NULL;
337}
338
Keith Whitwell6dc85572003-07-17 13:43:59 +0000339/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000340void
341_mesa_beginDispatchOverride(__GLcontext *gc)
342{
Brian Paula6c423d2004-08-25 15:59:48 +0000343 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000344}
345
Keith Whitwell6dc85572003-07-17 13:43:59 +0000346/** No-op */
Brian Paul9a33a112002-06-13 04:28:29 +0000347void
348_mesa_endDispatchOverride(__GLcontext *gc)
349{
Brian Paula6c423d2004-08-25 15:59:48 +0000350 (void) gc;
Brian Paul9a33a112002-06-13 04:28:29 +0000351}
352
Keith Whitwell6dc85572003-07-17 13:43:59 +0000353/**
354 * \ifnot subset
355 * Setup the exports.
356 *
357 * The window system will call these functions when it needs Mesa to do
358 * something.
359 *
360 * \note Device drivers should override these functions! For example,
Brian Paul9a33a112002-06-13 04:28:29 +0000361 * the Xlib driver should plug in the XMesa*-style functions into this
362 * structure. The XMesa-style functions should then call the _mesa_*
363 * version of these functions. This is an approximation to OO design
364 * (inheritance and virtual functions).
Keith Whitwell6dc85572003-07-17 13:43:59 +0000365 * \endif
366 *
367 * \if subset
368 * No-op.
369 *
370 * \endif
Brian Paul9a33a112002-06-13 04:28:29 +0000371 */
372static void
373_mesa_init_default_exports(__GLexports *exports)
374{
Keith Whitwell6dc85572003-07-17 13:43:59 +0000375#if _HAVE_FULL_GL
Brian Paul9a33a112002-06-13 04:28:29 +0000376 exports->destroyContext = _mesa_destroyContext;
377 exports->loseCurrent = _mesa_loseCurrent;
378 exports->makeCurrent = _mesa_makeCurrent;
379 exports->shareContext = _mesa_shareContext;
380 exports->copyContext = _mesa_copyContext;
381 exports->forceCurrent = _mesa_forceCurrent;
382 exports->notifyResize = _mesa_notifyResize;
Brian Paul2f35d5e2002-06-13 04:31:09 +0000383 exports->notifyDestroy = _mesa_notifyDestroy;
Brian Paul9a33a112002-06-13 04:28:29 +0000384 exports->notifySwapBuffers = _mesa_notifySwapBuffers;
385 exports->dispatchExec = _mesa_dispatchExec;
386 exports->beginDispatchOverride = _mesa_beginDispatchOverride;
387 exports->endDispatchOverride = _mesa_endDispatchOverride;
Brian Paula6c423d2004-08-25 15:59:48 +0000388#else
389 (void) exports;
Keith Whitwell6dc85572003-07-17 13:43:59 +0000390#endif
Brian Paul9a33a112002-06-13 04:28:29 +0000391}
392
Keith Whitwell6dc85572003-07-17 13:43:59 +0000393/**
394 * Exported OpenGL SI interface.
395 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000396__GLcontext *
397__glCoreCreateContext(__GLimports *imports, __GLcontextModes *modes)
398{
399 GLcontext *ctx;
400
Brian Paul4753d602002-06-15 02:38:15 +0000401 ctx = (GLcontext *) (*imports->calloc)(NULL, 1, sizeof(GLcontext));
Brian Paulb1394fa2000-09-26 20:53:53 +0000402 if (ctx == NULL) {
403 return NULL;
404 }
Brian Paul60b6e4f2002-10-14 17:08:17 +0000405
Brian Pauld3fd7ba2004-01-20 02:49:27 +0000406 /* XXX doesn't work at this time */
407 _mesa_initialize_context(ctx, modes, NULL, NULL, NULL);
Brian Paulb1394fa2000-09-26 20:53:53 +0000408 ctx->imports = *imports;
Brian Paulb1394fa2000-09-26 20:53:53 +0000409
410 return ctx;
411}
412
Keith Whitwell6dc85572003-07-17 13:43:59 +0000413/**
414 * Exported OpenGL SI interface.
415 */
Brian Paulb1394fa2000-09-26 20:53:53 +0000416void
417__glCoreNopDispatch(void)
418{
419#if 0
420 /* SI */
421 __gl_dispatch = __glNopDispatchState;
422#else
423 /* Mesa */
424 _glapi_set_dispatch(NULL);
425#endif
426}
427
Keith Whitwell6dc85572003-07-17 13:43:59 +0000428/*@}*/
429
Brian Paulb1394fa2000-09-26 20:53:53 +0000430
jtgafb833d1999-08-19 00:55:39 +0000431/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000432/** \name GL Visual allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000433/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000434/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000435
Keith Whitwell6dc85572003-07-17 13:43:59 +0000436/**
Brian Paul894844a2004-03-21 17:05:03 +0000437 * Allocates a GLvisual structure and initializes it via
438 * _mesa_initialize_visual().
Keith Whitwell6dc85572003-07-17 13:43:59 +0000439 *
440 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
441 * \param dbFlag double buffering
442 * \param stereoFlag stereo buffer
443 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
444 * is acceptable but the actual depth type will be GLushort or GLuint as
445 * needed.
446 * \param stencilBits requested minimum bits per stencil buffer value
447 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
448 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
449 * \param redBits number of bits per color component in frame buffer for RGB(A)
450 * mode. We always use 8 in core Mesa though.
451 * \param greenBits same as above.
452 * \param blueBits same as above.
453 * \param alphaBits same as above.
454 * \param numSamples not really used.
455 *
456 * \return pointer to new GLvisual or NULL if requested parameters can't be
457 * met.
458 *
Brian Paul894844a2004-03-21 17:05:03 +0000459 * \note Need to add params for level and numAuxBuffers (at least)
Brian Paul4d053dd2000-01-14 04:45:47 +0000460 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000461GLvisual *
462_mesa_create_visual( GLboolean rgbFlag,
Brian Paulb371e0d2000-03-31 01:05:51 +0000463 GLboolean dbFlag,
464 GLboolean stereoFlag,
465 GLint redBits,
466 GLint greenBits,
467 GLint blueBits,
468 GLint alphaBits,
469 GLint indexBits,
470 GLint depthBits,
471 GLint stencilBits,
472 GLint accumRedBits,
473 GLint accumGreenBits,
474 GLint accumBlueBits,
475 GLint accumAlphaBits,
476 GLint numSamples )
Brian Paul4d053dd2000-01-14 04:45:47 +0000477{
Brian Paul178a1c52000-04-22 01:05:00 +0000478 GLvisual *vis = (GLvisual *) CALLOC( sizeof(GLvisual) );
479 if (vis) {
Brian Paule70c6232000-05-04 13:53:55 +0000480 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000481 redBits, greenBits, blueBits, alphaBits,
482 indexBits, depthBits, stencilBits,
483 accumRedBits, accumGreenBits,
484 accumBlueBits, accumAlphaBits,
Brian Paulb1394fa2000-09-26 20:53:53 +0000485 numSamples)) {
Brian Paul178a1c52000-04-22 01:05:00 +0000486 FREE(vis);
487 return NULL;
488 }
489 }
490 return vis;
491}
492
Keith Whitwell6dc85572003-07-17 13:43:59 +0000493/**
Brian Paul894844a2004-03-21 17:05:03 +0000494 * Makes some sanity checks and fills in the fields of the
495 * GLvisual structure with the given parameters.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000496 *
497 * \return GL_TRUE on success, or GL_FALSE on failure.
498 *
499 * \sa _mesa_create_visual() above for the parameter description.
500 *
Brian Paul894844a2004-03-21 17:05:03 +0000501 * \note Need to add params for level and numAuxBuffers (at least)
Brian Paul178a1c52000-04-22 01:05:00 +0000502 */
503GLboolean
504_mesa_initialize_visual( GLvisual *vis,
505 GLboolean rgbFlag,
Brian Paul178a1c52000-04-22 01:05:00 +0000506 GLboolean dbFlag,
507 GLboolean stereoFlag,
508 GLint redBits,
509 GLint greenBits,
510 GLint blueBits,
511 GLint alphaBits,
512 GLint indexBits,
513 GLint depthBits,
514 GLint stencilBits,
515 GLint accumRedBits,
516 GLint accumGreenBits,
517 GLint accumBlueBits,
518 GLint accumAlphaBits,
519 GLint numSamples )
520{
521 assert(vis);
Brian Paul4d053dd2000-01-14 04:45:47 +0000522
Brian Pauled30dfa2000-03-03 17:47:39 +0000523 /* This is to catch bad values from device drivers not updated for
524 * Mesa 3.3. Some device drivers just passed 1. That's a REALLY
525 * bad value now (a 1-bit depth buffer!?!).
526 */
527 assert(depthBits == 0 || depthBits > 1);
528
529 if (depthBits < 0 || depthBits > 32) {
Brian Paul178a1c52000-04-22 01:05:00 +0000530 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000531 }
Brian Pauled30dfa2000-03-03 17:47:39 +0000532 if (stencilBits < 0 || stencilBits > (GLint) (8 * sizeof(GLstencil))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000533 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000534 }
Brian Paulb371e0d2000-03-31 01:05:51 +0000535 if (accumRedBits < 0 || accumRedBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000536 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000537 }
538 if (accumGreenBits < 0 || accumGreenBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000539 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000540 }
541 if (accumBlueBits < 0 || accumBlueBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000542 return GL_FALSE;
Brian Paulb371e0d2000-03-31 01:05:51 +0000543 }
544 if (accumAlphaBits < 0 || accumAlphaBits > (GLint) (8 * sizeof(GLaccum))) {
Brian Paul178a1c52000-04-22 01:05:00 +0000545 return GL_FALSE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000546 }
547
Brian Paulb6bcae52001-01-23 23:39:36 +0000548 vis->rgbMode = rgbFlag;
549 vis->doubleBufferMode = dbFlag;
550 vis->stereoMode = stereoFlag;
Brian Paul153f1542002-10-29 15:04:35 +0000551
Brian Paulb6bcae52001-01-23 23:39:36 +0000552 vis->redBits = redBits;
553 vis->greenBits = greenBits;
554 vis->blueBits = blueBits;
555 vis->alphaBits = alphaBits;
Brian Paul4d053dd2000-01-14 04:45:47 +0000556
Brian Paulb6bcae52001-01-23 23:39:36 +0000557 vis->indexBits = indexBits;
558 vis->depthBits = depthBits;
559 vis->accumRedBits = (accumRedBits > 0) ? (8 * sizeof(GLaccum)) : 0;
560 vis->accumGreenBits = (accumGreenBits > 0) ? (8 * sizeof(GLaccum)) : 0;
561 vis->accumBlueBits = (accumBlueBits > 0) ? (8 * sizeof(GLaccum)) : 0;
562 vis->accumAlphaBits = (accumAlphaBits > 0) ? (8 * sizeof(GLaccum)) : 0;
563 vis->stencilBits = (stencilBits > 0) ? (8 * sizeof(GLstencil)) : 0;
Brian Pauled30dfa2000-03-03 17:47:39 +0000564
Brian Paul153f1542002-10-29 15:04:35 +0000565 vis->haveAccumBuffer = accumRedBits > 0;
566 vis->haveDepthBuffer = depthBits > 0;
567 vis->haveStencilBuffer = stencilBits > 0;
568
569 vis->numAuxBuffers = 0;
570 vis->level = 0;
571 vis->pixmapMode = 0;
Brian Paul894844a2004-03-21 17:05:03 +0000572 vis->samples = numSamples;
Brian Paul153f1542002-10-29 15:04:35 +0000573
Brian Paul178a1c52000-04-22 01:05:00 +0000574 return GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +0000575}
576
Brian Paul894844a2004-03-21 17:05:03 +0000577
Keith Whitwell6dc85572003-07-17 13:43:59 +0000578/**
Brian Paul894844a2004-03-21 17:05:03 +0000579 * Destroy a visual and free its memory.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000580 *
581 * \param vis visual.
582 *
583 * Frees the visual structure.
584 */
Brian Paulb371e0d2000-03-31 01:05:51 +0000585void
586_mesa_destroy_visual( GLvisual *vis )
587{
588 FREE(vis);
589}
590
Keith Whitwell6dc85572003-07-17 13:43:59 +0000591/*@}*/
592
Brian Paulb371e0d2000-03-31 01:05:51 +0000593
Brian Paul4d053dd2000-01-14 04:45:47 +0000594/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000595/** \name GL Framebuffer allocation/destruction */
Brian Paul4d053dd2000-01-14 04:45:47 +0000596/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000597/*@{*/
Brian Paul4d053dd2000-01-14 04:45:47 +0000598
Keith Whitwell6dc85572003-07-17 13:43:59 +0000599/**
Brian Paul894844a2004-03-21 17:05:03 +0000600 * Allocate a GLframebuffer structure and initializes it via
601 * _mesa_initialize_framebuffer().
Keith Whitwell6dc85572003-07-17 13:43:59 +0000602 *
603 * A GLframebuffer is a structure which encapsulates the depth, stencil and
604 * accum buffers and related parameters.
605 *
Brian Paul8b332582004-06-11 22:44:22 +0000606 * Note that the actual depth/stencil/accum/etc buffers are not allocated
607 * at this time. It's up to the device driver and/or swrast module to
608 * allocate them as needed.
609 *
Keith Whitwell6dc85572003-07-17 13:43:59 +0000610 * \param visual a GLvisual pointer (we copy the struct contents)
611 * \param softwareDepth create/use a software depth buffer?
612 * \param softwareStencil create/use a software stencil buffer?
613 * \param softwareAccum create/use a software accum buffer?
614 * \param softwareAlpha create/use a software alpha buffer?
615 *
616 * \return pointer to new GLframebuffer struct or NULL if error.
617 *
Brian Paul894844a2004-03-21 17:05:03 +0000618 * \note Need to add softwareAuxBuffers parameter.
Brian Paul4d053dd2000-01-14 04:45:47 +0000619 */
Brian Paul178a1c52000-04-22 01:05:00 +0000620GLframebuffer *
Brian Paulbe3602d2001-02-28 00:27:48 +0000621_mesa_create_framebuffer( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +0000622 GLboolean softwareDepth,
623 GLboolean softwareStencil,
624 GLboolean softwareAccum,
625 GLboolean softwareAlpha )
Brian Paul4d053dd2000-01-14 04:45:47 +0000626{
Brian Paul178a1c52000-04-22 01:05:00 +0000627 GLframebuffer *buffer = CALLOC_STRUCT(gl_frame_buffer);
628 assert(visual);
629 if (buffer) {
630 _mesa_initialize_framebuffer(buffer, visual,
631 softwareDepth, softwareStencil,
632 softwareAccum, softwareAlpha );
Brian Paul4d053dd2000-01-14 04:45:47 +0000633 }
Brian Paul178a1c52000-04-22 01:05:00 +0000634 return buffer;
635}
636
Brian Paul894844a2004-03-21 17:05:03 +0000637
Keith Whitwell6dc85572003-07-17 13:43:59 +0000638/**
Keith Whitwell6dc85572003-07-17 13:43:59 +0000639 * Makes some sanity checks and fills in the fields of the
640 * GLframebuffer structure with the given parameters.
Brian Paul894844a2004-03-21 17:05:03 +0000641 *
642 * \sa _mesa_create_framebuffer() above for the parameter description.
Brian Paul178a1c52000-04-22 01:05:00 +0000643 */
644void
645_mesa_initialize_framebuffer( GLframebuffer *buffer,
Brian Paulbe3602d2001-02-28 00:27:48 +0000646 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +0000647 GLboolean softwareDepth,
648 GLboolean softwareStencil,
649 GLboolean softwareAccum,
650 GLboolean softwareAlpha )
651{
Brian Paul894844a2004-03-21 17:05:03 +0000652 GLboolean softwareAux = GL_FALSE;
Brian Paul178a1c52000-04-22 01:05:00 +0000653 assert(buffer);
654 assert(visual);
Brian Paul4d053dd2000-01-14 04:45:47 +0000655
Brian Paul6ec6b842002-10-30 19:49:29 +0000656 _mesa_bzero(buffer, sizeof(GLframebuffer));
Brian Paul85d81602002-06-17 23:36:31 +0000657
Brian Paul4d053dd2000-01-14 04:45:47 +0000658 /* sanity checks */
659 if (softwareDepth ) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000660 assert(visual->depthBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000661 }
662 if (softwareStencil) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000663 assert(visual->stencilBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000664 }
665 if (softwareAccum) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000666 assert(visual->rgbMode);
667 assert(visual->accumRedBits > 0);
668 assert(visual->accumGreenBits > 0);
669 assert(visual->accumBlueBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000670 }
671 if (softwareAlpha) {
Brian Paulb6bcae52001-01-23 23:39:36 +0000672 assert(visual->rgbMode);
673 assert(visual->alphaBits > 0);
Brian Paul4d053dd2000-01-14 04:45:47 +0000674 }
675
Brian Paul75978bd2001-04-27 21:17:20 +0000676 buffer->Visual = *visual;
Brian Paul4d053dd2000-01-14 04:45:47 +0000677 buffer->UseSoftwareDepthBuffer = softwareDepth;
678 buffer->UseSoftwareStencilBuffer = softwareStencil;
679 buffer->UseSoftwareAccumBuffer = softwareAccum;
680 buffer->UseSoftwareAlphaBuffers = softwareAlpha;
Brian Paul894844a2004-03-21 17:05:03 +0000681 buffer->UseSoftwareAuxBuffers = softwareAux;
Brian Paul4d053dd2000-01-14 04:45:47 +0000682}
683
Brian Paul894844a2004-03-21 17:05:03 +0000684
Keith Whitwell6dc85572003-07-17 13:43:59 +0000685/**
Brian Paul4d053dd2000-01-14 04:45:47 +0000686 * Free a framebuffer struct and its buffers.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000687 *
688 * Calls _mesa_free_framebuffer_data() and frees the structure.
Brian Paul4d053dd2000-01-14 04:45:47 +0000689 */
Brian Paul178a1c52000-04-22 01:05:00 +0000690void
Brian Paulb1394fa2000-09-26 20:53:53 +0000691_mesa_destroy_framebuffer( GLframebuffer *buffer )
Brian Paul4d053dd2000-01-14 04:45:47 +0000692{
693 if (buffer) {
Brian Paul75978bd2001-04-27 21:17:20 +0000694 _mesa_free_framebuffer_data(buffer);
Brian Paul4d053dd2000-01-14 04:45:47 +0000695 FREE(buffer);
696 }
697}
698
Brian Paul894844a2004-03-21 17:05:03 +0000699
Keith Whitwell6dc85572003-07-17 13:43:59 +0000700/**
701 * Free the data hanging off of \p buffer, but not \p buffer itself.
702 *
703 * \param buffer framebuffer.
704 *
705 * Frees all the buffers associated with the structure.
Brian Paul75978bd2001-04-27 21:17:20 +0000706 */
707void
708_mesa_free_framebuffer_data( GLframebuffer *buffer )
709{
710 if (!buffer)
711 return;
712
Brian Paul87506682003-05-27 15:20:43 +0000713 if (buffer->UseSoftwareDepthBuffer && buffer->DepthBuffer) {
Brian Paulaeb44342002-03-19 16:47:04 +0000714 MESA_PBUFFER_FREE( buffer->DepthBuffer );
Brian Paul75978bd2001-04-27 21:17:20 +0000715 buffer->DepthBuffer = NULL;
716 }
Brian Paul87506682003-05-27 15:20:43 +0000717 if (buffer->UseSoftwareAccumBuffer && buffer->Accum) {
Brian Paulaeb44342002-03-19 16:47:04 +0000718 MESA_PBUFFER_FREE( buffer->Accum );
Brian Paul75978bd2001-04-27 21:17:20 +0000719 buffer->Accum = NULL;
720 }
Brian Paul87506682003-05-27 15:20:43 +0000721 if (buffer->UseSoftwareStencilBuffer && buffer->Stencil) {
Brian Paulaeb44342002-03-19 16:47:04 +0000722 MESA_PBUFFER_FREE( buffer->Stencil );
Brian Paul75978bd2001-04-27 21:17:20 +0000723 buffer->Stencil = NULL;
724 }
Brian Paul87506682003-05-27 15:20:43 +0000725 if (buffer->UseSoftwareAlphaBuffers){
726 if (buffer->FrontLeftAlpha) {
727 MESA_PBUFFER_FREE( buffer->FrontLeftAlpha );
728 buffer->FrontLeftAlpha = NULL;
729 }
730 if (buffer->BackLeftAlpha) {
731 MESA_PBUFFER_FREE( buffer->BackLeftAlpha );
732 buffer->BackLeftAlpha = NULL;
733 }
734 if (buffer->FrontRightAlpha) {
735 MESA_PBUFFER_FREE( buffer->FrontRightAlpha );
736 buffer->FrontRightAlpha = NULL;
737 }
738 if (buffer->BackRightAlpha) {
739 MESA_PBUFFER_FREE( buffer->BackRightAlpha );
740 buffer->BackRightAlpha = NULL;
741 }
Brian Paul75978bd2001-04-27 21:17:20 +0000742 }
743}
744
Keith Whitwell6dc85572003-07-17 13:43:59 +0000745/*@}*/
Brian Paul75978bd2001-04-27 21:17:20 +0000746
Brian Paul4d053dd2000-01-14 04:45:47 +0000747
748/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000749/** \name Context allocation, initialization, destroying
750 *
751 * The purpose of the most initialization functions here is to provide the
752 * default state values according to the OpenGL specification.
753 */
jtgafb833d1999-08-19 00:55:39 +0000754/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +0000755/*@{*/
jtgafb833d1999-08-19 00:55:39 +0000756
Keith Whitwell6dc85572003-07-17 13:43:59 +0000757/**
758 * One-time initialization mutex lock.
759 *
760 * \sa Used by one_time_init().
761 */
Brian Paul9560f052000-01-31 23:11:39 +0000762_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
763
Keith Whitwell6dc85572003-07-17 13:43:59 +0000764/**
765 * Calls all the various one-time-init functions in Mesa.
766 *
767 * While holding a global mutex lock, calls several initialization functions,
768 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
769 * defined.
770 *
771 * \sa _mesa_init_lists(), _math_init().
jtgafb833d1999-08-19 00:55:39 +0000772 */
Brian Paul178a1c52000-04-22 01:05:00 +0000773static void
Brian Paul4753d602002-06-15 02:38:15 +0000774one_time_init( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000775{
776 static GLboolean alreadyCalled = GL_FALSE;
Brian Paula6c423d2004-08-25 15:59:48 +0000777 (void) ctx;
Brian Paul9560f052000-01-31 23:11:39 +0000778 _glthread_LOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000779 if (!alreadyCalled) {
Brian Paul27558a12003-03-01 01:50:20 +0000780 GLuint i;
781
Brian Paul4d053dd2000-01-14 04:45:47 +0000782 /* do some implementation tests */
783 assert( sizeof(GLbyte) == 1 );
Brian Paul4d053dd2000-01-14 04:45:47 +0000784 assert( sizeof(GLubyte) == 1 );
Brian Paul894844a2004-03-21 17:05:03 +0000785 assert( sizeof(GLshort) == 2 );
786 assert( sizeof(GLushort) == 2 );
787 assert( sizeof(GLint) == 4 );
788 assert( sizeof(GLuint) == 4 );
Brian Paul4d053dd2000-01-14 04:45:47 +0000789
Brian Paul08836342001-03-03 20:33:27 +0000790 _mesa_init_lists();
Keith Whitwell23caf202000-11-16 21:05:34 +0000791
Keith Whitwell6dc85572003-07-17 13:43:59 +0000792#if _HAVE_FULL_GL
Keith Whitwell23caf202000-11-16 21:05:34 +0000793 _math_init();
Brian Paul27558a12003-03-01 01:50:20 +0000794
795 for (i = 0; i < 256; i++) {
796 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
797 }
Keith Whitwell6dc85572003-07-17 13:43:59 +0000798#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000799
davem69775355a2001-06-05 23:54:00 +0000800#ifdef USE_SPARC_ASM
801 _mesa_init_sparc_glapi_relocs();
802#endif
Brian Paul3c634522002-10-24 23:57:19 +0000803 if (_mesa_getenv("MESA_DEBUG")) {
Brian Paul68ee4bc2000-01-28 19:02:22 +0000804 _glapi_noop_enable_warnings(GL_TRUE);
Brian Paul71072be2002-10-10 00:22:13 +0000805#ifndef GLX_DIRECT_RENDERING
806 /* libGL from before 2002/06/28 don't have this function. Someday,
807 * when newer libGL libs are common, remove the #ifdef test. This
808 * only serves to print warnings when calling undefined GL functions.
809 */
Brian Paul4e9676f2002-06-29 19:48:15 +0000810 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
Brian Paul71072be2002-10-10 00:22:13 +0000811#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000812 }
813 else {
814 _glapi_noop_enable_warnings(GL_FALSE);
815 }
816
jtgafb833d1999-08-19 00:55:39 +0000817#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
Brian Paul4e9676f2002-06-29 19:48:15 +0000818 _mesa_debug(ctx, "Mesa DEBUG build %s %s\n", __DATE__, __TIME__);
jtgafb833d1999-08-19 00:55:39 +0000819#endif
Brian Paul68ee4bc2000-01-28 19:02:22 +0000820
821 alreadyCalled = GL_TRUE;
822 }
Brian Paul9560f052000-01-31 23:11:39 +0000823 _glthread_UNLOCK_MUTEX(OneTimeLock);
jtgafb833d1999-08-19 00:55:39 +0000824}
825
Brian Paul894844a2004-03-21 17:05:03 +0000826
Keith Whitwell6dc85572003-07-17 13:43:59 +0000827/**
jtgafb833d1999-08-19 00:55:39 +0000828 * Allocate and initialize a shared context state structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000829 * Initializes the display list, texture objects and vertex programs hash
830 * tables, allocates the texture objects. If it runs out of memory, frees
831 * everything already allocated before returning NULL.
Brian Paul894844a2004-03-21 17:05:03 +0000832 *
833 * \return pointer to a gl_shared_state structure on success, or NULL on
834 * failure.
jtgafb833d1999-08-19 00:55:39 +0000835 */
Brian Paula3f13702003-04-01 16:41:50 +0000836static GLboolean
837alloc_shared_state( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +0000838{
Brian Paula3f13702003-04-01 16:41:50 +0000839 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
jtgafb833d1999-08-19 00:55:39 +0000840 if (!ss)
Brian Paula3f13702003-04-01 16:41:50 +0000841 return GL_FALSE;
842
843 ctx->Shared = ss;
jtgafb833d1999-08-19 00:55:39 +0000844
Brian Paule4b684c2000-09-12 21:07:40 +0000845 _glthread_INIT_MUTEX(ss->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000846
Brian Paule4b684c2000-09-12 21:07:40 +0000847 ss->DisplayList = _mesa_NewHashTable();
Brian Paulbb797902000-01-24 16:19:54 +0000848 ss->TexObjects = _mesa_NewHashTable();
Brian Paul451f3102003-04-17 01:48:19 +0000849#if FEATURE_NV_vertex_program || FEATURE_NV_fragment_program
Brian Paul610d5992003-01-14 04:55:45 +0000850 ss->Programs = _mesa_NewHashTable();
Brian Paul8dfc5b92002-10-16 17:57:51 +0000851#endif
jtgafb833d1999-08-19 00:55:39 +0000852
Brian Paul451f3102003-04-17 01:48:19 +0000853#if FEATURE_ARB_vertex_program
Brian Paul4d859f72004-01-23 18:57:05 +0000854 ss->DefaultVertexProgram = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
Brian Paul451f3102003-04-17 01:48:19 +0000855 if (!ss->DefaultVertexProgram)
856 goto cleanup;
857#endif
858#if FEATURE_ARB_fragment_program
Brian Paul4d859f72004-01-23 18:57:05 +0000859 ss->DefaultFragmentProgram = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
Brian Paul451f3102003-04-17 01:48:19 +0000860 if (!ss->DefaultFragmentProgram)
861 goto cleanup;
862#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000863#if FEATURE_ATI_fragment_shader
864 ss->DefaultFragmentShader = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_SHADER_ATI, 0);
865 if (!ss->DefaultFragmentShader)
866 goto cleanup;
867#endif
Brian Paul451f3102003-04-17 01:48:19 +0000868
Brian Paulddc82ee2005-02-05 19:56:45 +0000869 ss->BufferObjects = _mesa_NewHashTable();
870
Michal Krol9b3752c2005-01-13 14:08:47 +0000871 ss->GL2Objects = _mesa_NewHashTable ();
Ian Romanick0207b472003-09-09 00:10:12 +0000872
Brian Paula3f13702003-04-01 16:41:50 +0000873 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
874 if (!ss->Default1D)
875 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000876
Brian Paula3f13702003-04-01 16:41:50 +0000877 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
878 if (!ss->Default2D)
879 goto cleanup;
jtgafb833d1999-08-19 00:55:39 +0000880
Brian Paula3f13702003-04-01 16:41:50 +0000881 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
882 if (!ss->Default3D)
883 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000884
Brian Paula3f13702003-04-01 16:41:50 +0000885 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
886 if (!ss->DefaultCubeMap)
887 goto cleanup;
Brian Paula8523782000-11-19 23:10:25 +0000888
Brian Paula3f13702003-04-01 16:41:50 +0000889 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
890 if (!ss->DefaultRect)
891 goto cleanup;
Brian Paul413d6a22000-05-26 14:44:59 +0000892
Brian Paula3f13702003-04-01 16:41:50 +0000893 /* Effectively bind the default textures to all texture units */
894 ss->Default1D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
895 ss->Default2D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
896 ss->Default3D->RefCount += MAX_TEXTURE_IMAGE_UNITS;
897 ss->DefaultCubeMap->RefCount += MAX_TEXTURE_IMAGE_UNITS;
898 ss->DefaultRect->RefCount += MAX_TEXTURE_IMAGE_UNITS;
899
Brian Paulddc82ee2005-02-05 19:56:45 +0000900#if FEATURE_EXT_framebuffer_object
901 ss->FrameBuffers = _mesa_NewHashTable();
902 if (!ss->FrameBuffers)
903 goto cleanup;
904 ss->RenderBuffers = _mesa_NewHashTable();
905 if (!ss->RenderBuffers)
906 goto cleanup;
907#endif
908
909
Brian Paula3f13702003-04-01 16:41:50 +0000910 return GL_TRUE;
911
912 cleanup:
913 /* Ran out of memory at some point. Free everything and return NULL */
914 if (ss->DisplayList)
915 _mesa_DeleteHashTable(ss->DisplayList);
916 if (ss->TexObjects)
917 _mesa_DeleteHashTable(ss->TexObjects);
918#if FEATURE_NV_vertex_program
919 if (ss->Programs)
920 _mesa_DeleteHashTable(ss->Programs);
921#endif
Brian Paul451f3102003-04-17 01:48:19 +0000922#if FEATURE_ARB_vertex_program
923 if (ss->DefaultVertexProgram)
Brian Paul4d859f72004-01-23 18:57:05 +0000924 ctx->Driver.DeleteProgram(ctx, ss->DefaultVertexProgram);
Brian Paul451f3102003-04-17 01:48:19 +0000925#endif
926#if FEATURE_ARB_fragment_program
927 if (ss->DefaultFragmentProgram)
Brian Paul4d859f72004-01-23 18:57:05 +0000928 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentProgram);
Brian Paul451f3102003-04-17 01:48:19 +0000929#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000930#if FEATURE_ATI_fragment_shader
931 if (ss->DefaultFragmentShader)
932 ctx->Driver.DeleteProgram(ctx, ss->DefaultFragmentShader);
933#endif
Brian Paul21841f02004-08-14 14:28:11 +0000934#if FEATURE_ARB_vertex_buffer_object
Ian Romanick0207b472003-09-09 00:10:12 +0000935 if (ss->BufferObjects)
936 _mesa_DeleteHashTable(ss->BufferObjects);
Brian Paulddc82ee2005-02-05 19:56:45 +0000937#endif
938
939 if (ss->GL2Objects)
Michal Krol9b3752c2005-01-13 14:08:47 +0000940 _mesa_DeleteHashTable (ss->GL2Objects);
Ian Romanick0207b472003-09-09 00:10:12 +0000941
Brian Paulddc82ee2005-02-05 19:56:45 +0000942#if FEATURE_EXT_framebuffer_object
943 if (ss->FrameBuffers)
944 _mesa_DeleteHashTable(ss->FrameBuffers);
945 if (ss->RenderBuffers)
946 _mesa_DeleteHashTable(ss->RenderBuffers);
947#endif
948
Brian Paula3f13702003-04-01 16:41:50 +0000949 if (ss->Default1D)
950 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
951 if (ss->Default2D)
952 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
953 if (ss->Default3D)
954 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
955 if (ss->DefaultCubeMap)
956 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
957 if (ss->DefaultRect)
958 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
959 if (ss)
960 _mesa_free(ss);
961 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +0000962}
963
Keith Whitwell6dc85572003-07-17 13:43:59 +0000964/**
jtgafb833d1999-08-19 00:55:39 +0000965 * Deallocate a shared state context and all children structures.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000966 *
967 * \param ctx GL context.
968 * \param ss shared state pointer.
969 *
970 * Frees the display lists, the texture objects (calling the driver texture
971 * deletion callback to free its private data) and the vertex programs, as well
972 * as their hash tables.
973 *
974 * \sa alloc_shared_state().
jtgafb833d1999-08-19 00:55:39 +0000975 */
Brian Paul178a1c52000-04-22 01:05:00 +0000976static void
977free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
jtgafb833d1999-08-19 00:55:39 +0000978{
979 /* Free display lists */
980 while (1) {
Brian Paulbb797902000-01-24 16:19:54 +0000981 GLuint list = _mesa_HashFirstEntry(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000982 if (list) {
Brian Paul08836342001-03-03 20:33:27 +0000983 _mesa_destroy_list(ctx, list);
jtgafb833d1999-08-19 00:55:39 +0000984 }
985 else {
986 break;
987 }
988 }
Brian Paulbb797902000-01-24 16:19:54 +0000989 _mesa_DeleteHashTable(ss->DisplayList);
jtgafb833d1999-08-19 00:55:39 +0000990
991 /* Free texture objects */
Brian Paula3f13702003-04-01 16:41:50 +0000992 ASSERT(ctx->Driver.DeleteTexture);
Brian Paul21841f02004-08-14 14:28:11 +0000993 /* the default textures */
994 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
995 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
996 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
997 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
998 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
999 /* all other textures */
Brian Paula3f13702003-04-01 16:41:50 +00001000 while (1) {
1001 GLuint texName = _mesa_HashFirstEntry(ss->TexObjects);
1002 if (texName) {
1003 struct gl_texture_object *texObj = (struct gl_texture_object *)
1004 _mesa_HashLookup(ss->TexObjects, texName);
1005 ASSERT(texObj);
1006 (*ctx->Driver.DeleteTexture)(ctx, texObj);
1007 _mesa_HashRemove(ss->TexObjects, texName);
1008 }
1009 else {
1010 break;
1011 }
jtgafb833d1999-08-19 00:55:39 +00001012 }
Brian Paulbb797902000-01-24 16:19:54 +00001013 _mesa_DeleteHashTable(ss->TexObjects);
jtgafb833d1999-08-19 00:55:39 +00001014
Brian Paul8dfc5b92002-10-16 17:57:51 +00001015#if FEATURE_NV_vertex_program
Brian Paul30f51ae2001-12-18 04:06:44 +00001016 /* Free vertex programs */
1017 while (1) {
Brian Paul610d5992003-01-14 04:55:45 +00001018 GLuint prog = _mesa_HashFirstEntry(ss->Programs);
Brian Paul30f51ae2001-12-18 04:06:44 +00001019 if (prog) {
Brian Paul451f3102003-04-17 01:48:19 +00001020 struct program *p = (struct program *) _mesa_HashLookup(ss->Programs,
1021 prog);
1022 ASSERT(p);
Brian Paul4d859f72004-01-23 18:57:05 +00001023 ctx->Driver.DeleteProgram(ctx, p);
Brian Paul451f3102003-04-17 01:48:19 +00001024 _mesa_HashRemove(ss->Programs, prog);
Brian Paul30f51ae2001-12-18 04:06:44 +00001025 }
1026 else {
1027 break;
1028 }
1029 }
Brian Paul610d5992003-01-14 04:55:45 +00001030 _mesa_DeleteHashTable(ss->Programs);
Brian Paul8dfc5b92002-10-16 17:57:51 +00001031#endif
Brian Paul21841f02004-08-14 14:28:11 +00001032#if FEATURE_ARB_vertex_program
1033 _mesa_delete_program(ctx, ss->DefaultVertexProgram);
1034#endif
1035#if FEATURE_ARB_fragment_program
1036 _mesa_delete_program(ctx, ss->DefaultFragmentProgram);
1037#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +00001038#if FEATURE_ATI_fragment_shader
1039 _mesa_delete_program(ctx, ss->DefaultFragmentShader);
1040#endif
Brian Paul30f51ae2001-12-18 04:06:44 +00001041
Brian Paul21841f02004-08-14 14:28:11 +00001042#if FEATURE_ARB_vertex_buffer_object
Ian Romanick0207b472003-09-09 00:10:12 +00001043 _mesa_DeleteHashTable(ss->BufferObjects);
Brian Paulddc82ee2005-02-05 19:56:45 +00001044#endif
1045
1046 _mesa_DeleteHashTable (ss->GL2Objects);
1047
1048#if FEATURE_EXT_framebuffer_object
1049 _mesa_DeleteHashTable(ss->FrameBuffers);
1050 _mesa_DeleteHashTable(ss->RenderBuffers);
1051#endif
Michal Krol9b3752c2005-01-13 14:08:47 +00001052
Keith Whitwelle15fd852002-12-12 13:03:15 +00001053 _glthread_DESTROY_MUTEX(ss->Mutex);
1054
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001055 FREE(ss);
jtgafb833d1999-08-19 00:55:39 +00001056}
1057
1058
Brian Paul4d859f72004-01-23 18:57:05 +00001059/**
1060 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
1061 */
1062static void
1063_mesa_init_current( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001064{
Brian Paul88bf0382004-02-13 15:30:08 +00001065 GLuint i;
jtgafb833d1999-08-19 00:55:39 +00001066
Keith Whitwell6dc85572003-07-17 13:43:59 +00001067 /* Current group */
1068 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
1069 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
jtgafb833d1999-08-19 00:55:39 +00001070 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001071 /* special cases: */
1072 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 1.0 );
1073 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
1074 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
Brian Paul88bf0382004-02-13 15:30:08 +00001075 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001076 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_FOG], 0.0, 0.0, 0.0, 0.0 );
Brian Paul88bf0382004-02-13 15:30:08 +00001077
Keith Whitwell6dc85572003-07-17 13:43:59 +00001078 ctx->Current.Index = 1;
1079 ctx->Current.EdgeFlag = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001080}
1081
1082
Brian Paul4d859f72004-01-23 18:57:05 +00001083/**
1084 * Initialize fields of gl_constants (aka ctx->Const.*).
1085 * Use defaults from config.h. The device drivers will often override
1086 * some of these values (such as number of texture units).
1087 */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001088static void
1089_mesa_init_constants( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001090{
Brian Paul4d053dd2000-01-14 04:45:47 +00001091 assert(ctx);
jtgafb833d1999-08-19 00:55:39 +00001092
Brian Paulcd1cefa2001-06-13 14:56:14 +00001093 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1094 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1095
Brian Paul53f82c52004-10-02 16:39:09 +00001096 /* Constants, may be overriden (usually only reduced) by device drivers */
Brian Paul4d053dd2000-01-14 04:45:47 +00001097 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
Brian Paulcd1cefa2001-06-13 14:56:14 +00001098 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
1099 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
Brian Paul8afe7de2002-06-15 03:03:06 +00001100 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001101 ctx->Const.MaxTextureUnits = MAX_TEXTURE_UNITS;
Brian Paul610d5992003-01-14 04:55:45 +00001102 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
1103 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
Gareth Hughes2c3d34c2001-03-18 08:53:49 +00001104 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
Brian Paul87c964d2001-11-06 15:53:00 +00001105 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
Brian Paul4d053dd2000-01-14 04:45:47 +00001106 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
Brian Paul539cce52000-02-03 19:40:07 +00001107 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
1108 ctx->Const.MinPointSize = MIN_POINT_SIZE;
1109 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
1110 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
1111 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001112 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
Brian Paul539cce52000-02-03 19:40:07 +00001113 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
1114 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
1115 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
1116 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
Brian Paulfde5e2c2001-09-15 18:02:49 +00001117 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
Brian Paul4bdcfe52000-04-17 17:57:04 +00001118 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
Brian Paul82b02f02000-05-07 20:37:40 +00001119 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
1120 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
Brian Paula8644322000-11-27 18:22:13 +00001121 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
1122 ctx->Const.MaxLights = MAX_LIGHTS;
Ian Romanick882caa12003-05-30 21:37:14 +00001123 ctx->Const.MaxShininess = 128.0;
Brian Paul53f82c52004-10-02 16:39:09 +00001124 ctx->Const.MaxSpotExponent = 128.0;
1125 ctx->Const.MaxViewportWidth = MAX_WIDTH;
1126 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
Brian Pauld0492cf2003-04-11 01:20:06 +00001127#if FEATURE_ARB_vertex_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001128 ctx->Const.MaxVertexProgramInstructions = MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001129 ctx->Const.MaxVertexProgramAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
1130 ctx->Const.MaxVertexProgramTemps = MAX_NV_VERTEX_PROGRAM_TEMPS;
1131 ctx->Const.MaxVertexProgramLocalParams = MAX_NV_VERTEX_PROGRAM_PARAMS;
1132 ctx->Const.MaxVertexProgramEnvParams = MAX_NV_VERTEX_PROGRAM_PARAMS;/*XXX*/
1133 ctx->Const.MaxVertexProgramAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001134#endif
1135#if FEATURE_ARB_fragment_program
Brian Pauld0492cf2003-04-11 01:20:06 +00001136 ctx->Const.MaxFragmentProgramInstructions = MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS;
Brian Paul451f3102003-04-17 01:48:19 +00001137 ctx->Const.MaxFragmentProgramAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
1138 ctx->Const.MaxFragmentProgramTemps = MAX_NV_FRAGMENT_PROGRAM_TEMPS;
1139 ctx->Const.MaxFragmentProgramLocalParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
1140 ctx->Const.MaxFragmentProgramEnvParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;/*XXX*/
1141 ctx->Const.MaxFragmentProgramAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
1142 ctx->Const.MaxFragmentProgramAluInstructions = MAX_FRAGMENT_PROGRAM_ALU_INSTRUCTIONS;
1143 ctx->Const.MaxFragmentProgramTexInstructions = MAX_FRAGMENT_PROGRAM_TEX_INSTRUCTIONS;
1144 ctx->Const.MaxFragmentProgramTexIndirections = MAX_FRAGMENT_PROGRAM_TEX_INDIRECTIONS;
Brian Pauld0492cf2003-04-11 01:20:06 +00001145#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +00001146
Brian Pauledd67742003-04-18 18:02:43 +00001147 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
1148 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
Brian Pauld0492cf2003-04-11 01:20:06 +00001149
Brian Paula2b9bad2003-11-10 19:08:37 +00001150 /* If we're running in the X server, do bounds checking to prevent
1151 * segfaults and server crashes!
1152 */
1153#if defined(XFree86LOADER) && defined(IN_MODULE)
1154 ctx->Const.CheckArrayBounds = GL_TRUE;
1155#else
1156 ctx->Const.CheckArrayBounds = GL_FALSE;
1157#endif
1158
Brian Paul53f82c52004-10-02 16:39:09 +00001159 ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
1160
Brian Paul3deaa012005-02-07 05:08:24 +00001161 /* GL_OES_read_format */
Ian Romanick33899b72004-10-16 01:16:54 +00001162 ctx->Const.ColorReadFormat = GL_RGBA;
1163 ctx->Const.ColorReadType = GL_UNSIGNED_BYTE;
1164
Brian Paul3deaa012005-02-07 05:08:24 +00001165#if FEATURE_EXT_framebuffer_object
1166 ctx->Const.MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
1167 ctx->Const.MaxRenderbufferSize = MAX_WIDTH;
1168#endif
1169
Brian Paul53f82c52004-10-02 16:39:09 +00001170 /* sanity checks */
Brian Paul92f97852003-05-01 22:44:02 +00001171 ASSERT(ctx->Const.MaxTextureUnits == MAX2(ctx->Const.MaxTextureImageUnits, ctx->Const.MaxTextureCoordUnits));
Keith Whitwell6dc85572003-07-17 13:43:59 +00001172}
jtgafb833d1999-08-19 00:55:39 +00001173
Brian Paul4d859f72004-01-23 18:57:05 +00001174
Keith Whitwell6dc85572003-07-17 13:43:59 +00001175/**
1176 * Initialize the attribute groups in a GL context.
1177 *
1178 * \param ctx GL context.
1179 *
1180 * Initializes all the attributes, calling the respective <tt>init*</tt>
1181 * functions for the more complex data structures.
1182 */
1183static GLboolean
1184init_attrib_groups( GLcontext *ctx )
1185{
1186 assert(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001187
Keith Whitwell6dc85572003-07-17 13:43:59 +00001188 /* Constants */
1189 _mesa_init_constants( ctx );
Brian Paul0771d152000-04-07 00:19:41 +00001190
Brian Paul4d053dd2000-01-14 04:45:47 +00001191 /* Extensions */
Brian Paulde4f4602003-07-03 02:15:06 +00001192 _mesa_init_extensions( ctx );
jtgafb833d1999-08-19 00:55:39 +00001193
Keith Whitwell6dc85572003-07-17 13:43:59 +00001194 /* Attribute Groups */
1195 _mesa_init_accum( ctx );
1196 _mesa_init_attrib( ctx );
1197 _mesa_init_buffers( ctx );
Brian Paul148a2842003-09-17 03:40:11 +00001198 _mesa_init_buffer_objects( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001199 _mesa_init_color( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001200 _mesa_init_colortables( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001201 _mesa_init_current( ctx );
1202 _mesa_init_depth( ctx );
1203 _mesa_init_debug( ctx );
1204 _mesa_init_display_list( ctx );
1205 _mesa_init_eval( ctx );
1206 _mesa_init_feedback( ctx );
1207 _mesa_init_fog( ctx );
1208 _mesa_init_histogram( ctx );
1209 _mesa_init_hint( ctx );
1210 _mesa_init_line( ctx );
1211 _mesa_init_lighting( ctx );
1212 _mesa_init_matrix( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001213 _mesa_init_occlude( ctx );
Keith Whitwell6dc85572003-07-17 13:43:59 +00001214 _mesa_init_pixel( ctx );
1215 _mesa_init_point( ctx );
1216 _mesa_init_polygon( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001217 _mesa_init_program( ctx );
Brian Paulddc82ee2005-02-05 19:56:45 +00001218 _mesa_init_rastpos( ctx );
Michal Krol9b3752c2005-01-13 14:08:47 +00001219 _mesa_init_shaderobjects (ctx);
Keith Whitwell6dc85572003-07-17 13:43:59 +00001220 _mesa_init_stencil( ctx );
1221 _mesa_init_transform( ctx );
1222 _mesa_init_varray( ctx );
1223 _mesa_init_viewport( ctx );
jtgafb833d1999-08-19 00:55:39 +00001224
Keith Whitwell6dc85572003-07-17 13:43:59 +00001225 if (!_mesa_init_texture( ctx ))
1226 return GL_FALSE;
Brian Paulb17a7222003-06-13 02:37:27 +00001227
Brian Paul8f04c122004-04-27 13:39:20 +00001228 _mesa_init_texture_s3tc( ctx );
1229 _mesa_init_texture_fxt1( ctx );
1230
Brian Paul4d053dd2000-01-14 04:45:47 +00001231 /* Miscellaneous */
Keith Whitwella96308c2000-10-30 13:31:59 +00001232 ctx->NewState = _NEW_ALL;
Brian Paul4d053dd2000-01-14 04:45:47 +00001233 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
Brian Paulf782b812002-10-04 17:37:45 +00001234 ctx->_Facing = 0;
Brian Paul4923e192004-02-28 22:30:58 +00001235#if CHAN_TYPE == GL_FLOAT
1236 ctx->ClampFragmentColors = GL_FALSE; /* XXX temporary */
1237#else
1238 ctx->ClampFragmentColors = GL_TRUE;
1239#endif
1240 ctx->ClampVertexColors = GL_TRUE;
Brian Paul4d053dd2000-01-14 04:45:47 +00001241
Brian Paula3f13702003-04-01 16:41:50 +00001242 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001243}
1244
1245
Brian Paulf44898c2003-07-18 15:44:57 +00001246/**
1247 * If the DRI libGL.so library is old, it may not have the entrypoints for
1248 * some recent OpenGL extensions. Dynamically add them now.
1249 * If we're building stand-alone Mesa where libGL.so has both the dispatcher
1250 * and driver code, this won't be an issue (and calling this function won't
1251 * do any harm).
1252 */
1253static void
1254add_newer_entrypoints(void)
1255{
Brian Paulde434842004-11-27 20:14:03 +00001256 unsigned i;
Ian Romanick3baefe62003-08-22 23:28:03 +00001257 static const struct {
1258 const char * const name;
Brian Paulde434842004-11-27 20:14:03 +00001259 unsigned offset;
Ian Romanick3baefe62003-08-22 23:28:03 +00001260 }
1261 newer_entrypoints[] = {
1262 /* GL_ARB_window_pos aliases with GL_MESA_window_pos */
1263 { "glWindowPos2dARB", 513 },
1264 { "glWindowPos2dvARB", 514 },
1265 { "glWindowPos2fARB", 515 },
1266 { "glWindowPos2fvARB", 516 },
1267 { "glWindowPos2iARB", 517 },
1268 { "glWindowPos2ivARB", 518 },
1269 { "glWindowPos2sARB", 519 },
1270 { "glWindowPos2svARB", 520 },
1271 { "glWindowPos3dARB", 521 },
1272 { "glWindowPos3dvARB", 522 },
1273 { "glWindowPos3fARB", 523 },
1274 { "glWindowPos3fvARB", 524 },
1275 { "glWindowPos3iARB", 525 },
1276 { "glWindowPos3ivARB", 526 },
1277 { "glWindowPos3sARB", 527 },
1278 { "glWindowPos3svARB", 528 },
Brian Paulf44898c2003-07-18 15:44:57 +00001279#if FEATURE_NV_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001280 { "glAreProgramsResidentNV", 578 },
1281 { "glBindProgramNV", 579 },
1282 { "glDeleteProgramsNV", 580 },
1283 { "glExecuteProgramNV", 581 },
1284 { "glGenProgramsNV", 582 },
1285 { "glGetProgramParameterdvNV", 583 },
1286 { "glGetProgramParameterfvNV", 584 },
1287 { "glGetProgramivNV", 585 },
1288 { "glGetProgramStringNV", 586 },
1289 { "glGetTrackMatrixivNV", 587 },
1290 { "glGetVertexAttribdvNV", 588 },
1291 { "glGetVertexAttribfvNV", 589 },
1292 { "glGetVertexAttribivNV", 590 },
1293 { "glGetVertexAttribPointervNV", 591 },
1294 { "glIsProgramNV", 592 },
1295 { "glLoadProgramNV", 593 },
1296 { "glProgramParameter4dNV", 594 },
1297 { "glProgramParameter4dvNV", 595 },
1298 { "glProgramParameter4fNV", 596 },
1299 { "glProgramParameter4fvNV", 597 },
1300 { "glProgramParameters4dvNV", 598 },
1301 { "glProgramParameters4fvNV", 599 },
1302 { "glRequestResidentProgramsNV", 600 },
1303 { "glTrackMatrixNV", 601 },
1304 { "glVertexAttribPointerNV", 602 },
1305 { "glVertexAttrib1dNV", 603 },
1306 { "glVertexAttrib1dvNV", 604 },
1307 { "glVertexAttrib1fNV", 605 },
1308 { "glVertexAttrib1fvNV", 606 },
1309 { "glVertexAttrib1sNV", 607 },
1310 { "glVertexAttrib1svNV", 608 },
1311 { "glVertexAttrib2dNV", 609 },
1312 { "glVertexAttrib2dvNV", 610 },
1313 { "glVertexAttrib2fNV", 611 },
1314 { "glVertexAttrib2fvNV", 612 },
1315 { "glVertexAttrib2sNV", 613 },
1316 { "glVertexAttrib2svNV", 614 },
1317 { "glVertexAttrib3dNV", 615 },
1318 { "glVertexAttrib3dvNV", 616 },
1319 { "glVertexAttrib3fNV", 617 },
1320 { "glVertexAttrib3fvNV", 618 },
1321 { "glVertexAttrib3sNV", 619 },
1322 { "glVertexAttrib3svNV", 620 },
1323 { "glVertexAttrib4dNV", 621 },
1324 { "glVertexAttrib4dvNV", 622 },
1325 { "glVertexAttrib4fNV", 623 },
1326 { "glVertexAttrib4fvNV", 624 },
1327 { "glVertexAttrib4sNV", 625 },
1328 { "glVertexAttrib4svNV", 626 },
1329 { "glVertexAttrib4ubNV", 627 },
1330 { "glVertexAttrib4ubvNV", 628 },
1331 { "glVertexAttribs1dvNV", 629 },
1332 { "glVertexAttribs1fvNV", 630 },
1333 { "glVertexAttribs1svNV", 631 },
1334 { "glVertexAttribs2dvNV", 632 },
1335 { "glVertexAttribs2fvNV", 633 },
1336 { "glVertexAttribs2svNV", 634 },
1337 { "glVertexAttribs3dvNV", 635 },
1338 { "glVertexAttribs3fvNV", 636 },
1339 { "glVertexAttribs3svNV", 637 },
1340 { "glVertexAttribs4dvNV", 638 },
1341 { "glVertexAttribs4fvNV", 639 },
1342 { "glVertexAttribs4svNV", 640 },
1343 { "glVertexAttribs4ubvNV", 641 },
Brian Paulf44898c2003-07-18 15:44:57 +00001344#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001345 { "glPointParameteriNV", 642 },
1346 { "glPointParameterivNV", 643 },
1347 { "glMultiDrawArraysEXT", 644 },
1348 { "glMultiDrawElementsEXT", 645 },
1349 { "glMultiDrawArraysSUN", _gloffset_MultiDrawArraysEXT },
1350 { "glMultiDrawElementsSUN", _gloffset_MultiDrawElementsEXT },
1351 { "glActiveStencilFaceEXT", 646 },
Brian Paulf44898c2003-07-18 15:44:57 +00001352#if FEATURE_NV_fence
Ian Romanick3baefe62003-08-22 23:28:03 +00001353 { "glDeleteFencesNV", 647 },
1354 { "glGenFencesNV", 648 },
1355 { "glIsFenceNV", 649 },
1356 { "glTestFenceNV", 650 },
1357 { "glGetFenceivNV", 651 },
1358 { "glFinishFenceNV", 652 },
1359 { "glSetFenceNV", 653 },
Brian Paulf44898c2003-07-18 15:44:57 +00001360#endif
1361#if FEATURE_NV_fragment_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001362 { "glProgramNamedParameter4fNV", 682 },
1363 { "glProgramNamedParameter4dNV", 683 },
1364 { "glProgramNamedParameter4fvNV", 683 },
1365 { "glProgramNamedParameter4dvNV", 684 },
1366 { "glGetProgramNamedParameterfvNV", 685 },
1367 { "glGetProgramNamedParameterdvNV", 686 },
Brian Paulf44898c2003-07-18 15:44:57 +00001368#endif
1369#if FEATURE_ARB_vertex_program
Ian Romanick3baefe62003-08-22 23:28:03 +00001370 { "glVertexAttrib1sARB", _gloffset_VertexAttrib1sNV },
1371 { "glVertexAttrib1fARB", _gloffset_VertexAttrib1fNV },
1372 { "glVertexAttrib1dARB", _gloffset_VertexAttrib1dNV },
1373 { "glVertexAttrib2sARB", _gloffset_VertexAttrib2sNV },
1374 { "glVertexAttrib2fARB", _gloffset_VertexAttrib2fNV },
1375 { "glVertexAttrib2dARB", _gloffset_VertexAttrib2dNV },
1376 { "glVertexAttrib3sARB", _gloffset_VertexAttrib3sNV },
1377 { "glVertexAttrib3fARB", _gloffset_VertexAttrib3fNV },
1378 { "glVertexAttrib3dARB", _gloffset_VertexAttrib3dNV },
1379 { "glVertexAttrib4sARB", _gloffset_VertexAttrib4sNV },
1380 { "glVertexAttrib4fARB", _gloffset_VertexAttrib4fNV },
1381 { "glVertexAttrib4dARB", _gloffset_VertexAttrib4dNV },
1382 { "glVertexAttrib4NubARB", _gloffset_VertexAttrib4ubNV },
1383 { "glVertexAttrib1svARB", _gloffset_VertexAttrib1svNV },
1384 { "glVertexAttrib1fvARB", _gloffset_VertexAttrib1fvNV },
1385 { "glVertexAttrib1dvARB", _gloffset_VertexAttrib1dvNV },
1386 { "glVertexAttrib2svARB", _gloffset_VertexAttrib2svNV },
1387 { "glVertexAttrib2fvARB", _gloffset_VertexAttrib2fvNV },
1388 { "glVertexAttrib2dvARB", _gloffset_VertexAttrib2dvNV },
1389 { "glVertexAttrib3svARB", _gloffset_VertexAttrib3svNV },
1390 { "glVertexAttrib3fvARB", _gloffset_VertexAttrib3fvNV },
1391 { "glVertexAttrib3dvARB", _gloffset_VertexAttrib3dvNV },
1392 { "glVertexAttrib4bvARB", _gloffset_VertexAttrib4bvARB },
1393 { "glVertexAttrib4svARB", _gloffset_VertexAttrib4svNV },
1394 { "glVertexAttrib4ivARB", _gloffset_VertexAttrib4ivARB },
1395 { "glVertexAttrib4ubvARB", _gloffset_VertexAttrib4ubvARB },
1396 { "glVertexAttrib4usvARB", _gloffset_VertexAttrib4usvARB },
1397 { "glVertexAttrib4uivARB", _gloffset_VertexAttrib4uivARB },
1398 { "glVertexAttrib4fvARB", _gloffset_VertexAttrib4fvNV },
1399 { "glVertexAttrib4dvARB", _gloffset_VertexAttrib4dvNV },
1400 { "glVertexAttrib4NbvARB", _gloffset_VertexAttrib4NbvARB },
1401 { "glVertexAttrib4NsvARB", _gloffset_VertexAttrib4NsvARB },
1402 { "glVertexAttrib4NivARB", _gloffset_VertexAttrib4NivARB },
1403 { "glVertexAttrib4NubvARB", _gloffset_VertexAttrib4ubvNV },
1404 { "glVertexAttrib4NusvARB", _gloffset_VertexAttrib4NusvARB },
1405 { "glVertexAttrib4NuivARB", _gloffset_VertexAttrib4NuivARB },
1406 { "glVertexAttribPointerARB", _gloffset_VertexAttribPointerARB },
1407 { "glEnableVertexAttribArrayARB", _gloffset_EnableVertexAttribArrayARB },
1408 { "glDisableVertexAttribArrayARB", _gloffset_DisableVertexAttribArrayARB },
1409 { "glProgramStringARB", _gloffset_ProgramStringARB },
1410 { "glBindProgramARB", _gloffset_BindProgramNV },
1411 { "glDeleteProgramsARB", _gloffset_DeleteProgramsNV },
1412 { "glGenProgramsARB", _gloffset_GenProgramsNV },
1413 { "glIsProgramARB", _gloffset_IsProgramNV },
1414 { "glProgramEnvParameter4dARB", _gloffset_ProgramEnvParameter4dARB },
1415 { "glProgramEnvParameter4dvARB", _gloffset_ProgramEnvParameter4dvARB },
1416 { "glProgramEnvParameter4fARB", _gloffset_ProgramEnvParameter4fARB },
1417 { "glProgramEnvParameter4fvARB", _gloffset_ProgramEnvParameter4fvARB },
1418 { "glProgramLocalParameter4dARB", _gloffset_ProgramLocalParameter4dARB },
1419 { "glProgramLocalParameter4dvARB", _gloffset_ProgramLocalParameter4dvARB },
1420 { "glProgramLocalParameter4fARB", _gloffset_ProgramLocalParameter4fARB },
1421 { "glProgramLocalParameter4fvARB", _gloffset_ProgramLocalParameter4fvARB },
1422 { "glGetProgramEnvParameterdvARB", _gloffset_GetProgramEnvParameterdvARB },
1423 { "glGetProgramEnvParameterfvARB", _gloffset_GetProgramEnvParameterfvARB },
1424 { "glGetProgramLocalParameterdvARB", _gloffset_GetProgramLocalParameterdvARB },
1425 { "glGetProgramLocalParameterfvARB", _gloffset_GetProgramLocalParameterfvARB },
1426 { "glGetProgramivARB", _gloffset_GetProgramivARB },
1427 { "glGetProgramStringARB", _gloffset_GetProgramStringARB },
1428 { "glGetVertexAttribdvARB", _gloffset_GetVertexAttribdvNV },
1429 { "glGetVertexAttribfvARB", _gloffset_GetVertexAttribfvNV },
1430 { "glGetVertexAttribivARB", _gloffset_GetVertexAttribivNV },
1431 { "glGetVertexAttribPointervARB", _gloffset_GetVertexAttribPointervNV },
Brian Paulf44898c2003-07-18 15:44:57 +00001432#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001433 { "glMultiModeDrawArraysIBM", _gloffset_MultiModeDrawArraysIBM },
1434 { "glMultiModeDrawElementsIBM", _gloffset_MultiModeDrawElementsIBM },
Brian Paulde434842004-11-27 20:14:03 +00001435 /* GL_EXT_stencil_two_side */
1436 { "glActiveStencilFaceEXT", _gloffset_ActiveStencilFaceEXT },
1437 /* GL_ARB_draw_buffers */
1438 { "glDrawBuffersARB", _gloffset_DrawBuffersARB },
Dave Airlie7f752fe2004-12-19 03:06:59 +00001439#if FEATURE_ATI_fragment_shader
1440 { "glGenFragmentShadersATI", _gloffset_GenFragmentShadersATI },
1441 { "glBindFragmentShaderATI", _gloffset_BindFragmentShaderATI },
1442 { "glDeleteFragmentShaderATI", _gloffset_DeleteFragmentShaderATI },
1443 { "glBeginFragmentShaderATI", _gloffset_BeginFragmentShaderATI },
1444 { "glEndFragmentShaderATI", _gloffset_EndFragmentShaderATI },
1445 { "glPassTexCoordATI", _gloffset_PassTexCoordATI },
1446 { "glSampleMapATI", _gloffset_SampleMapATI },
1447 { "glColorFragmentOp1ATI", _gloffset_ColorFragmentOp1ATI },
1448 { "glColorFragmentOp2ATI", _gloffset_ColorFragmentOp2ATI },
1449 { "glColorFragmentOp3ATI", _gloffset_ColorFragmentOp3ATI },
1450 { "glAlphaFragmentOp1ATI", _gloffset_AlphaFragmentOp1ATI },
1451 { "glAlphaFragmentOp2ATI", _gloffset_AlphaFragmentOp2ATI },
1452 { "glAlphaFragmentOp3ATI", _gloffset_AlphaFragmentOp3ATI },
1453 { "glSetFragmentShaderConstantATI", _gloffset_SetFragmentShaderConstantATI },
1454#endif
Ian Romanick3baefe62003-08-22 23:28:03 +00001455 };
1456
Brian Paulde434842004-11-27 20:14:03 +00001457 for (i = 0; i < Elements(newer_entrypoints); i++ ) {
Ian Romanick3baefe62003-08-22 23:28:03 +00001458 _glapi_add_entrypoint( newer_entrypoints[i].name,
Brian Paulde434842004-11-27 20:14:03 +00001459 newer_entrypoints[i].offset );
Ian Romanick3baefe62003-08-22 23:28:03 +00001460 }
Brian Paulf44898c2003-07-18 15:44:57 +00001461}
1462
Keith Whitwell306d3fc2002-04-09 16:56:50 +00001463
Brian Paulde4f4602003-07-03 02:15:06 +00001464/**
Brian Paul21f69782004-11-27 05:05:32 +00001465 * This is the default function we plug into all dispatch table slots
1466 * This helps prevents a segfault when someone calls a GL function without
1467 * first checking if the extension's supported.
1468 */
1469static int
1470generic_nop(void)
1471{
1472 _mesa_problem(NULL, "User called no-op dispatch function (an unsupported extension function?)");
1473 return 0;
1474}
1475
1476
1477/**
1478 * Allocate and initialize a new dispatch table.
1479 */
1480static struct _glapi_table *
1481alloc_dispatch_table(void)
1482{
1483 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1484 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1485 * Mesa we do this to accomodate different versions of libGL and various
1486 * DRI drivers.
1487 */
1488 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
1489 sizeof(struct _glapi_table) / sizeof(_glapi_proc));
1490 struct _glapi_table *table =
1491 (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc));
1492 if (table) {
1493 _glapi_proc *entry = (_glapi_proc *) table;
Brian Paula760ccf2004-12-03 15:24:34 +00001494 GLint i;
Brian Paul21f69782004-11-27 05:05:32 +00001495 for (i = 0; i < numEntries; i++) {
1496 entry[i] = (_glapi_proc) generic_nop;
1497 }
1498 }
1499 return table;
1500}
1501
1502
1503/**
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001504 * Initialize a GLcontext struct (rendering context).
Keith Whitwell6dc85572003-07-17 13:43:59 +00001505 *
1506 * This includes allocating all the other structs and arrays which hang off of
1507 * the context by pointers.
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001508 * Note that the driver needs to pass in its dd_function_table here since
1509 * we need to at least call driverFunctions->NewTextureObject to create the
1510 * default texture objects.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001511 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001512 * Called by _mesa_create_context().
Keith Whitwell6dc85572003-07-17 13:43:59 +00001513 *
1514 * Performs the imports and exports callback tables initialization, and
1515 * miscellaneous one-time initializations. If no shared context is supplied one
1516 * is allocated, and increase its reference count. Setups the GL API dispatch
1517 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1518 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1519 * for debug flags.
1520 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001521 * \param ctx the context to initialize
1522 * \param visual describes the visual attributes for this context
1523 * \param share_list points to context to share textures, display lists,
1524 * etc with, or NULL
1525 * \param driverFunctions table of device driver functions for this context
1526 * to use
1527 * \param driverContext pointer to driver-specific context data
jtgafb833d1999-08-19 00:55:39 +00001528 */
Brian Paul178a1c52000-04-22 01:05:00 +00001529GLboolean
1530_mesa_initialize_context( GLcontext *ctx,
Brian Paulbe3602d2001-02-28 00:27:48 +00001531 const GLvisual *visual,
Brian Paul178a1c52000-04-22 01:05:00 +00001532 GLcontext *share_list,
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001533 const struct dd_function_table *driverFunctions,
1534 void *driverContext )
jtgafb833d1999-08-19 00:55:39 +00001535{
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001536 ASSERT(driverContext);
1537 assert(driverFunctions->NewTextureObject);
jtgafb833d1999-08-19 00:55:39 +00001538
Brian Paul3c634522002-10-24 23:57:19 +00001539 /* If the driver wants core Mesa to use special imports, it'll have to
1540 * override these defaults.
1541 */
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001542 _mesa_init_default_imports( &(ctx->imports), driverContext );
jtgafb833d1999-08-19 00:55:39 +00001543
Brian Paul9a33a112002-06-13 04:28:29 +00001544 /* initialize the exports (Mesa functions called by the window system) */
Brian Paul4753d602002-06-15 02:38:15 +00001545 _mesa_init_default_exports( &(ctx->exports) );
1546
1547 /* misc one-time initializations */
1548 one_time_init(ctx);
Brian Paul9a33a112002-06-13 04:28:29 +00001549
Brian Paulb1394fa2000-09-26 20:53:53 +00001550 ctx->Visual = *visual;
Brian Paul3f02f901999-11-24 18:48:30 +00001551 ctx->DrawBuffer = NULL;
1552 ctx->ReadBuffer = NULL;
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001553
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001554 /* Plug in driver functions and context pointer here.
1555 * This is important because when we call alloc_shared_state() below
1556 * we'll call ctx->Driver.NewTextureObject() to create the default
1557 * textures.
Brian Paula3f13702003-04-01 16:41:50 +00001558 */
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001559 ctx->Driver = *driverFunctions;
1560 ctx->DriverCtx = driverContext;
Brian Paula3f13702003-04-01 16:41:50 +00001561
jtgafb833d1999-08-19 00:55:39 +00001562 if (share_list) {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001563 /* share state with another context */
jtgafb833d1999-08-19 00:55:39 +00001564 ctx->Shared = share_list->Shared;
1565 }
1566 else {
Brian Paul5a2f32b2001-04-25 18:21:05 +00001567 /* allocate new, unshared state */
Brian Paula3f13702003-04-01 16:41:50 +00001568 if (!alloc_shared_state( ctx )) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001569 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001570 }
1571 }
Brian Paul9560f052000-01-31 23:11:39 +00001572 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001573 ctx->Shared->RefCount++;
Brian Paul9560f052000-01-31 23:11:39 +00001574 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +00001575
Keith Whitwell6dc85572003-07-17 13:43:59 +00001576 if (!init_attrib_groups( ctx )) {
jtgafb833d1999-08-19 00:55:39 +00001577 free_shared_state(ctx, ctx->Shared);
Brian Paul4d053dd2000-01-14 04:45:47 +00001578 return GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001579 }
jtgafb833d1999-08-19 00:55:39 +00001580
Brian Paulf44898c2003-07-18 15:44:57 +00001581 /* libGL ABI coordination */
1582 add_newer_entrypoints();
Keith Whitwell6dc85572003-07-17 13:43:59 +00001583
Brian Paul21f69782004-11-27 05:05:32 +00001584 /* setup the API dispatch tables */
1585 ctx->Exec = alloc_dispatch_table();
1586 ctx->Save = alloc_dispatch_table();
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001587 if (!ctx->Exec || !ctx->Save) {
1588 free_shared_state(ctx, ctx->Shared);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001589 if (ctx->Exec)
Brian Paul21f69782004-11-27 05:05:32 +00001590 _mesa_free(ctx->Exec);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001591 }
Brian Paul21f69782004-11-27 05:05:32 +00001592 _mesa_init_exec_table(ctx->Exec);
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001593 ctx->CurrentDispatch = ctx->Exec;
Keith Whitwell6dc85572003-07-17 13:43:59 +00001594#if _HAVE_FULL_GL
Brian Paul21f69782004-11-27 05:05:32 +00001595 _mesa_init_dlist_table(ctx->Save);
Keith Whitwellae0eaf92003-11-24 15:23:18 +00001596 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001597 /* Neutral tnl module stuff */
Keith Whitwell6dc85572003-07-17 13:43:59 +00001598 _mesa_init_exec_vtxfmt( ctx );
Gareth Hughesd8aa0262001-03-11 18:49:11 +00001599 ctx->TnlModule.Current = NULL;
1600 ctx->TnlModule.SwapCount = 0;
Keith Whitwell6dc85572003-07-17 13:43:59 +00001601#endif
Brian Paulb6bcae52001-01-23 23:39:36 +00001602
Brian Paul4d053dd2000-01-14 04:45:47 +00001603 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001604}
1605
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001606
Brian Paulde4f4602003-07-03 02:15:06 +00001607/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001608 * Allocate and initialize a GLcontext structure.
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001609 * Note that the driver needs to pass in its dd_function_table here since
1610 * we need to at least call driverFunctions->NewTextureObject to initialize
1611 * the rendering context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001612 *
1613 * \param visual a GLvisual pointer (we copy the struct contents)
1614 * \param share_list another context to share display lists with or NULL
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001615 * \param driverFunctions points to the dd_function_table into which the
1616 * driver has plugged in all its special functions.
1617 * \param driverCtx points to the device driver's private context state
Keith Whitwell6dc85572003-07-17 13:43:59 +00001618 *
1619 * \return pointer to a new __GLcontextRec or NULL if error.
Brian Paul4d053dd2000-01-14 04:45:47 +00001620 */
Brian Paul178a1c52000-04-22 01:05:00 +00001621GLcontext *
Brian Paulbe3602d2001-02-28 00:27:48 +00001622_mesa_create_context( const GLvisual *visual,
Brian Paulb1394fa2000-09-26 20:53:53 +00001623 GLcontext *share_list,
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001624 const struct dd_function_table *driverFunctions,
1625 void *driverContext )
Brian Paul3c634522002-10-24 23:57:19 +00001626
Brian Paul4d053dd2000-01-14 04:45:47 +00001627{
Brian Paul4753d602002-06-15 02:38:15 +00001628 GLcontext *ctx;
1629
1630 ASSERT(visual);
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001631 ASSERT(driverContext);
Brian Paul4753d602002-06-15 02:38:15 +00001632
Brian Paul3c634522002-10-24 23:57:19 +00001633 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
Brian Paul4753d602002-06-15 02:38:15 +00001634 if (!ctx)
Brian Paul4d053dd2000-01-14 04:45:47 +00001635 return NULL;
Brian Paul4753d602002-06-15 02:38:15 +00001636
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001637 if (_mesa_initialize_context(ctx, visual, share_list,
1638 driverFunctions, driverContext)) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001639 return ctx;
1640 }
1641 else {
Brian Paul3c634522002-10-24 23:57:19 +00001642 _mesa_free(ctx);
Brian Paul4d053dd2000-01-14 04:45:47 +00001643 return NULL;
1644 }
1645}
1646
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001647
Keith Whitwell6dc85572003-07-17 13:43:59 +00001648/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001649 * Free the data associated with the given context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001650 *
1651 * But doesn't free the GLcontext struct itself.
1652 *
1653 * \sa _mesa_initialize_context() and init_attrib_groups().
Brian Paul4d053dd2000-01-14 04:45:47 +00001654 */
Brian Paul178a1c52000-04-22 01:05:00 +00001655void
Brian Paulb1394fa2000-09-26 20:53:53 +00001656_mesa_free_context_data( GLcontext *ctx )
Brian Paul4d053dd2000-01-14 04:45:47 +00001657{
Brian Paul4d053dd2000-01-14 04:45:47 +00001658 /* if we're destroying the current context, unbind it first */
Brian Paulb1394fa2000-09-26 20:53:53 +00001659 if (ctx == _mesa_get_current_context()) {
1660 _mesa_make_current(NULL, NULL);
Brian Paul4d053dd2000-01-14 04:45:47 +00001661 }
1662
Keith Whitwell6dc85572003-07-17 13:43:59 +00001663 _mesa_free_lighting_data( ctx );
1664 _mesa_free_eval_data( ctx );
1665 _mesa_free_texture_data( ctx );
1666 _mesa_free_matrix_data( ctx );
1667 _mesa_free_viewport_data( ctx );
Brian Paul05944c02003-07-22 03:51:46 +00001668 _mesa_free_colortables_data( ctx );
Brian Paul21841f02004-08-14 14:28:11 +00001669 _mesa_free_program_data(ctx);
1670 _mesa_free_occlude_data(ctx);
1671
1672#if FEATURE_ARB_vertex_buffer_object
1673 _mesa_delete_buffer_object(ctx, ctx->Array.NullBufferObj);
Brian Paul8dfc5b92002-10-16 17:57:51 +00001674#endif
Brian Paulfd284452001-07-19 15:54:34 +00001675
Brian Paul65a66f52004-11-27 22:47:59 +00001676 /* free dispatch tables */
1677 _mesa_free(ctx->Exec);
1678 _mesa_free(ctx->Save);
1679
Brian Paul30f51ae2001-12-18 04:06:44 +00001680 /* Shared context state (display lists, textures, etc) */
Brian Paul9560f052000-01-31 23:11:39 +00001681 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d053dd2000-01-14 04:45:47 +00001682 ctx->Shared->RefCount--;
Brian Paul9560f052000-01-31 23:11:39 +00001683 assert(ctx->Shared->RefCount >= 0);
1684 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1685 if (ctx->Shared->RefCount == 0) {
Brian Paul4d053dd2000-01-14 04:45:47 +00001686 /* free shared state */
1687 free_shared_state( ctx, ctx->Shared );
1688 }
1689
Brian Paul702ca202003-07-18 15:22:16 +00001690 if (ctx->Extensions.String)
1691 FREE((void *) ctx->Extensions.String);
Brian Paul4d053dd2000-01-14 04:45:47 +00001692}
1693
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001694
Keith Whitwell6dc85572003-07-17 13:43:59 +00001695/**
Brian Paul4d053dd2000-01-14 04:45:47 +00001696 * Destroy a GLcontext structure.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001697 *
1698 * \param ctx GL context.
1699 *
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001700 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
jtgafb833d1999-08-19 00:55:39 +00001701 */
Brian Paul178a1c52000-04-22 01:05:00 +00001702void
Brian Paulb1394fa2000-09-26 20:53:53 +00001703_mesa_destroy_context( GLcontext *ctx )
jtgafb833d1999-08-19 00:55:39 +00001704{
1705 if (ctx) {
Brian Paulb1394fa2000-09-26 20:53:53 +00001706 _mesa_free_context_data(ctx);
Brian Paulbd5cdaf1999-10-13 18:42:49 +00001707 FREE( (void *) ctx );
jtgafb833d1999-08-19 00:55:39 +00001708 }
1709}
1710
Brian Pauld3fd7ba2004-01-20 02:49:27 +00001711
Keith Whitwell6dc85572003-07-17 13:43:59 +00001712#if _HAVE_FULL_GL
1713/**
jtgafb833d1999-08-19 00:55:39 +00001714 * Copy attribute groups from one context to another.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001715 *
1716 * \param src source context
1717 * \param dst destination context
1718 * \param mask bitwise OR of GL_*_BIT flags
1719 *
1720 * According to the bits specified in \p mask, copies the corresponding
Jose Fonseca375457b2004-09-09 22:23:24 +00001721 * attributes from \p src into \p dst. For many of the attributes a simple \c
Keith Whitwell6dc85572003-07-17 13:43:59 +00001722 * memcpy is not enough due to the existence of internal pointers in their data
1723 * structures.
jtgafb833d1999-08-19 00:55:39 +00001724 */
Brian Paul178a1c52000-04-22 01:05:00 +00001725void
Brian Paulb1394fa2000-09-26 20:53:53 +00001726_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
jtgafb833d1999-08-19 00:55:39 +00001727{
1728 if (mask & GL_ACCUM_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001729 /* OK to memcpy */
1730 dst->Accum = src->Accum;
jtgafb833d1999-08-19 00:55:39 +00001731 }
1732 if (mask & GL_COLOR_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001733 /* OK to memcpy */
1734 dst->Color = src->Color;
jtgafb833d1999-08-19 00:55:39 +00001735 }
1736 if (mask & GL_CURRENT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001737 /* OK to memcpy */
1738 dst->Current = src->Current;
jtgafb833d1999-08-19 00:55:39 +00001739 }
1740 if (mask & GL_DEPTH_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001741 /* OK to memcpy */
1742 dst->Depth = src->Depth;
jtgafb833d1999-08-19 00:55:39 +00001743 }
1744 if (mask & GL_ENABLE_BIT) {
1745 /* no op */
1746 }
1747 if (mask & GL_EVAL_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001748 /* OK to memcpy */
1749 dst->Eval = src->Eval;
jtgafb833d1999-08-19 00:55:39 +00001750 }
1751 if (mask & GL_FOG_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001752 /* OK to memcpy */
1753 dst->Fog = src->Fog;
jtgafb833d1999-08-19 00:55:39 +00001754 }
1755 if (mask & GL_HINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001756 /* OK to memcpy */
1757 dst->Hint = src->Hint;
jtgafb833d1999-08-19 00:55:39 +00001758 }
1759 if (mask & GL_LIGHTING_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001760 GLuint i;
1761 /* begin with memcpy */
1762 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light) );
1763 /* fixup linked lists to prevent pointer insanity */
1764 make_empty_list( &(dst->Light.EnabledList) );
1765 for (i = 0; i < MAX_LIGHTS; i++) {
1766 if (dst->Light.Light[i].Enabled) {
1767 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1768 }
1769 }
jtgafb833d1999-08-19 00:55:39 +00001770 }
1771 if (mask & GL_LINE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001772 /* OK to memcpy */
1773 dst->Line = src->Line;
jtgafb833d1999-08-19 00:55:39 +00001774 }
1775 if (mask & GL_LIST_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001776 /* OK to memcpy */
1777 dst->List = src->List;
jtgafb833d1999-08-19 00:55:39 +00001778 }
1779 if (mask & GL_PIXEL_MODE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001780 /* OK to memcpy */
1781 dst->Pixel = src->Pixel;
jtgafb833d1999-08-19 00:55:39 +00001782 }
1783 if (mask & GL_POINT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001784 /* OK to memcpy */
1785 dst->Point = src->Point;
jtgafb833d1999-08-19 00:55:39 +00001786 }
1787 if (mask & GL_POLYGON_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001788 /* OK to memcpy */
1789 dst->Polygon = src->Polygon;
jtgafb833d1999-08-19 00:55:39 +00001790 }
1791 if (mask & GL_POLYGON_STIPPLE_BIT) {
1792 /* Use loop instead of MEMCPY due to problem with Portland Group's
1793 * C compiler. Reported by John Stone.
1794 */
Brian Paul85d81602002-06-17 23:36:31 +00001795 GLuint i;
1796 for (i = 0; i < 32; i++) {
jtgafb833d1999-08-19 00:55:39 +00001797 dst->PolygonStipple[i] = src->PolygonStipple[i];
1798 }
1799 }
1800 if (mask & GL_SCISSOR_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001801 /* OK to memcpy */
1802 dst->Scissor = src->Scissor;
jtgafb833d1999-08-19 00:55:39 +00001803 }
1804 if (mask & GL_STENCIL_BUFFER_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001805 /* OK to memcpy */
1806 dst->Stencil = src->Stencil;
jtgafb833d1999-08-19 00:55:39 +00001807 }
1808 if (mask & GL_TEXTURE_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001809 /* Cannot memcpy because of pointers */
1810 _mesa_copy_texture_state(src, dst);
jtgafb833d1999-08-19 00:55:39 +00001811 }
1812 if (mask & GL_TRANSFORM_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001813 /* OK to memcpy */
1814 dst->Transform = src->Transform;
jtgafb833d1999-08-19 00:55:39 +00001815 }
1816 if (mask & GL_VIEWPORT_BIT) {
Brian Paul85d81602002-06-17 23:36:31 +00001817 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1818 dst->Viewport.X = src->Viewport.X;
1819 dst->Viewport.Y = src->Viewport.Y;
1820 dst->Viewport.Width = src->Viewport.Width;
1821 dst->Viewport.Height = src->Viewport.Height;
1822 dst->Viewport.Near = src->Viewport.Near;
1823 dst->Viewport.Far = src->Viewport.Far;
1824 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
jtgafb833d1999-08-19 00:55:39 +00001825 }
Brian Paul85d81602002-06-17 23:36:31 +00001826
Keith Whitwella96308c2000-10-30 13:31:59 +00001827 /* XXX FIXME: Call callbacks?
1828 */
1829 dst->NewState = _NEW_ALL;
jtgafb833d1999-08-19 00:55:39 +00001830}
Keith Whitwell23caf202000-11-16 21:05:34 +00001831#endif
Keith Whitwell23caf202000-11-16 21:05:34 +00001832
1833
Brian Paulb1d53d92003-06-11 18:48:19 +00001834/**
1835 * Check if the given context can render into the given framebuffer
1836 * by checking visual attributes.
1837 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1838 */
1839static GLboolean
1840check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1841{
1842 const GLvisual *ctxvis = &ctx->Visual;
1843 const GLvisual *bufvis = &buffer->Visual;
1844
1845 if (ctxvis == bufvis)
1846 return GL_TRUE;
1847
1848 if (ctxvis->rgbMode != bufvis->rgbMode)
1849 return GL_FALSE;
1850 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1851 return GL_FALSE;
1852 if (ctxvis->stereoMode && !bufvis->stereoMode)
1853 return GL_FALSE;
1854 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1855 return GL_FALSE;
1856 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1857 return GL_FALSE;
1858 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1859 return GL_FALSE;
1860 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1861 return GL_FALSE;
1862 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1863 return GL_FALSE;
1864 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1865 return GL_FALSE;
1866 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1867 return GL_FALSE;
1868 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1869 return GL_FALSE;
1870
1871 return GL_TRUE;
1872}
1873
1874
Keith Whitwell6dc85572003-07-17 13:43:59 +00001875/**
Brian Paul9a33a112002-06-13 04:28:29 +00001876 * Set the current context, binding the given frame buffer to the context.
Keith Whitwell6dc85572003-07-17 13:43:59 +00001877 *
1878 * \param newCtx new GL context.
1879 * \param buffer framebuffer.
1880 *
1881 * Calls _mesa_make_current2() with \p buffer as read and write framebuffer.
Brian Paul9a33a112002-06-13 04:28:29 +00001882 */
1883void
1884_mesa_make_current( GLcontext *newCtx, GLframebuffer *buffer )
1885{
1886 _mesa_make_current2( newCtx, buffer, buffer );
1887}
1888
Keith Whitwell6dc85572003-07-17 13:43:59 +00001889/**
1890 * Bind the given context to the given draw-buffer and read-buffer and
1891 * make it the current context for this thread.
1892 *
1893 * \param newCtx new GL context. If NULL then there will be no current GL
1894 * context.
1895 * \param drawBuffer draw framebuffer.
1896 * \param readBuffer read framebuffer.
1897 *
1898 * Check that the context's and framebuffer's visuals are compatible, returning
1899 * immediately otherwise. Sets the glapi current context via
1900 * _glapi_set_context(). If \p newCtx is not NULL, associates \p drawBuffer and
1901 * \p readBuffer with it and calls dd_function_table::ResizeBuffers if the buffers size has changed.
1902 * Calls dd_function_table::MakeCurrent callback if defined.
1903 *
1904 * When a context is bound by the first time and the \c MESA_INFO environment
1905 * variable is set it calls print_info() as an aid for remote user
1906 * troubleshooting.
Brian Paul00037781999-12-17 14:52:35 +00001907 */
Brian Paulb1394fa2000-09-26 20:53:53 +00001908void
1909_mesa_make_current2( GLcontext *newCtx, GLframebuffer *drawBuffer,
1910 GLframebuffer *readBuffer )
Brian Paul00037781999-12-17 14:52:35 +00001911{
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001912 if (MESA_VERBOSE)
Brian Paul4753d602002-06-15 02:38:15 +00001913 _mesa_debug(newCtx, "_mesa_make_current2()\n");
Brian Paul00037781999-12-17 14:52:35 +00001914
Brian Paulbe3602d2001-02-28 00:27:48 +00001915 /* Check that the context's and framebuffer's visuals are compatible.
Brian Paulbe3602d2001-02-28 00:27:48 +00001916 */
Brian Paulb1d53d92003-06-11 18:48:19 +00001917 if (newCtx && drawBuffer && newCtx->DrawBuffer != drawBuffer) {
1918 if (!check_compatible(newCtx, drawBuffer))
1919 return;
1920 }
1921 if (newCtx && readBuffer && newCtx->ReadBuffer != readBuffer) {
1922 if (!check_compatible(newCtx, readBuffer))
1923 return;
Brian Paulbe3602d2001-02-28 00:27:48 +00001924 }
1925
Brian Paul00037781999-12-17 14:52:35 +00001926 /* We call this function periodically (just here for now) in
1927 * order to detect when multithreading has begun.
1928 */
1929 _glapi_check_multithread();
1930
Brian Paulf9b97d92000-01-28 20:17:42 +00001931 _glapi_set_context((void *) newCtx);
Brian Paulb1394fa2000-09-26 20:53:53 +00001932 ASSERT(_mesa_get_current_context() == newCtx);
Keith Whitwell23caf202000-11-16 21:05:34 +00001933
1934
1935 if (!newCtx) {
Brian Paul00037781999-12-17 14:52:35 +00001936 _glapi_set_dispatch(NULL); /* none current */
1937 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001938 else {
1939 _glapi_set_dispatch(newCtx->CurrentDispatch);
Brian Paul00037781999-12-17 14:52:35 +00001940
Keith Whitwell23caf202000-11-16 21:05:34 +00001941 if (drawBuffer && readBuffer) {
1942 /* TODO: check if newCtx and buffer's visual match??? */
1943 newCtx->DrawBuffer = drawBuffer;
1944 newCtx->ReadBuffer = readBuffer;
1945 newCtx->NewState |= _NEW_BUFFERS;
Brian Paul10d7f542002-06-17 23:38:14 +00001946
Keith Whitwell6dc85572003-07-17 13:43:59 +00001947#if _HAVE_FULL_GL
Brian Paul65a66f52004-11-27 22:47:59 +00001948 if (!drawBuffer->Initialized) {
Brian Paul10d7f542002-06-17 23:38:14 +00001949 /* get initial window size */
1950 GLuint bufWidth, bufHeight;
Brian Paul65a66f52004-11-27 22:47:59 +00001951 /* ask device driver for size of the buffer */
1952 (*newCtx->Driver.GetBufferSize)(drawBuffer, &bufWidth, &bufHeight);
1953 /* set initial buffer size */
1954 drawBuffer->Width = bufWidth;
1955 drawBuffer->Height = bufHeight;
1956 newCtx->Driver.ResizeBuffers( drawBuffer );
1957 drawBuffer->Initialized = GL_TRUE;
Brian Paul10d7f542002-06-17 23:38:14 +00001958 }
1959
Brian Paul65a66f52004-11-27 22:47:59 +00001960 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
Brian Paul10d7f542002-06-17 23:38:14 +00001961 /* get initial window size */
1962 GLuint bufWidth, bufHeight;
Brian Paul65a66f52004-11-27 22:47:59 +00001963 /* ask device driver for size of the buffer */
1964 (*newCtx->Driver.GetBufferSize)(readBuffer, &bufWidth, &bufHeight);
1965 /* set initial buffer size */
1966 readBuffer->Width = bufWidth;
1967 readBuffer->Height = bufHeight;
1968 newCtx->Driver.ResizeBuffers( readBuffer );
1969 readBuffer->Initialized = GL_TRUE;
Brian Paul10d7f542002-06-17 23:38:14 +00001970 }
Keith Whitwell6dc85572003-07-17 13:43:59 +00001971#endif
Brian Paul65a66f52004-11-27 22:47:59 +00001972 if (newCtx->FirstTimeCurrent) {
1973 /* set initial viewport and scissor size now */
1974 _mesa_set_viewport(newCtx, 0, 0, drawBuffer->Width, drawBuffer->Height);
1975 newCtx->Scissor.Width = drawBuffer->Width;
1976 newCtx->Scissor.Height = drawBuffer->Height;
1977 }
Brian Paul00037781999-12-17 14:52:35 +00001978 }
Keith Whitwell23caf202000-11-16 21:05:34 +00001979
Keith Whitwell6dc85572003-07-17 13:43:59 +00001980 /* Alert the driver - usually passed on to the sw t&l module,
1981 * but also used to detect threaded cases in the radeon codegen
1982 * hw t&l module.
1983 */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001984 if (newCtx->Driver.MakeCurrent)
Keith Whitwell23caf202000-11-16 21:05:34 +00001985 newCtx->Driver.MakeCurrent( newCtx, drawBuffer, readBuffer );
1986
1987 /* We can use this to help debug user's problems. Tell them to set
1988 * the MESA_INFO env variable before running their app. Then the
1989 * first time each context is made current we'll print some useful
1990 * information.
1991 */
1992 if (newCtx->FirstTimeCurrent) {
Brian Paul3c634522002-10-24 23:57:19 +00001993 if (_mesa_getenv("MESA_INFO")) {
Keith Whitwell6dc85572003-07-17 13:43:59 +00001994 _mesa_print_info();
Keith Whitwell23caf202000-11-16 21:05:34 +00001995 }
1996 newCtx->FirstTimeCurrent = GL_FALSE;
1997 }
Brian Paul00037781999-12-17 14:52:35 +00001998 }
1999}
2000
Keith Whitwell6dc85572003-07-17 13:43:59 +00002001/**
2002 * Get current context for the calling thread.
2003 *
2004 * \return pointer to the current GL context.
2005 *
2006 * Calls _glapi_get_context(). This isn't the fastest way to get the current
2007 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in context.h.
Brian Paul00037781999-12-17 14:52:35 +00002008 */
Brian Paulb1394fa2000-09-26 20:53:53 +00002009GLcontext *
2010_mesa_get_current_context( void )
Brian Paul00037781999-12-17 14:52:35 +00002011{
Brian Paulf9b97d92000-01-28 20:17:42 +00002012 return (GLcontext *) _glapi_get_context();
Brian Paul00037781999-12-17 14:52:35 +00002013}
2014
Keith Whitwell6dc85572003-07-17 13:43:59 +00002015/**
2016 * Get context's current API dispatch table.
2017 *
2018 * It'll either be the immediate-mode execute dispatcher or the display list
2019 * compile dispatcher.
2020 *
2021 * \param ctx GL context.
2022 *
2023 * \return pointer to dispatch_table.
2024 *
2025 * Simply returns __GLcontextRec::CurrentDispatch.
Brian Paulfbd8f211999-11-11 01:22:25 +00002026 */
2027struct _glapi_table *
2028_mesa_get_dispatch(GLcontext *ctx)
2029{
2030 return ctx->CurrentDispatch;
2031}
2032
Keith Whitwell6dc85572003-07-17 13:43:59 +00002033/*@}*/
Brian Paulfbd8f211999-11-11 01:22:25 +00002034
2035
jtgafb833d1999-08-19 00:55:39 +00002036/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00002037/** \name Miscellaneous functions */
jtgafb833d1999-08-19 00:55:39 +00002038/**********************************************************************/
Keith Whitwell6dc85572003-07-17 13:43:59 +00002039/*@{*/
jtgafb833d1999-08-19 00:55:39 +00002040
Keith Whitwell6dc85572003-07-17 13:43:59 +00002041/**
2042 * Record an error.
2043 *
2044 * \param ctx GL context.
2045 * \param error error code.
2046 *
2047 * Records the given error code and call the driver's dd_function_table::Error
2048 * function if defined.
2049 *
2050 * \sa
Brian Paul4e9676f2002-06-29 19:48:15 +00002051 * This is called via _mesa_error().
jtgafb833d1999-08-19 00:55:39 +00002052 */
Brian Paulb1394fa2000-09-26 20:53:53 +00002053void
Brian Paul4e9676f2002-06-29 19:48:15 +00002054_mesa_record_error( GLcontext *ctx, GLenum error )
jtgafb833d1999-08-19 00:55:39 +00002055{
Brian Paul18a285a2002-03-16 00:53:15 +00002056 if (!ctx)
2057 return;
2058
Brian Paul7eb06032000-07-14 04:13:40 +00002059 if (ctx->ErrorValue == GL_NO_ERROR) {
jtgafb833d1999-08-19 00:55:39 +00002060 ctx->ErrorValue = error;
2061 }
2062
2063 /* Call device driver's error handler, if any. This is used on the Mac. */
2064 if (ctx->Driver.Error) {
2065 (*ctx->Driver.Error)( ctx );
2066 }
2067}
2068
Keith Whitwell6dc85572003-07-17 13:43:59 +00002069/**
2070 * Execute glFinish().
2071 *
2072 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2073 * dd_function_table::Finish driver callback, if not NULL.
2074 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002075void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00002076_mesa_Finish( void )
jtgafb833d1999-08-19 00:55:39 +00002077{
Brian Paulfa9df402000-02-02 19:16:46 +00002078 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002079 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00002080 if (ctx->Driver.Finish) {
2081 (*ctx->Driver.Finish)( ctx );
jtgafb833d1999-08-19 00:55:39 +00002082 }
2083}
2084
Keith Whitwell6dc85572003-07-17 13:43:59 +00002085/**
2086 * Execute glFlush().
2087 *
2088 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
2089 * dd_function_table::Flush driver callback, if not NULL.
2090 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00002091void GLAPIENTRY
Brian Paulfa9df402000-02-02 19:16:46 +00002092_mesa_Flush( void )
jtgafb833d1999-08-19 00:55:39 +00002093{
Brian Paulfa9df402000-02-02 19:16:46 +00002094 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +00002095 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paulfa9df402000-02-02 19:16:46 +00002096 if (ctx->Driver.Flush) {
2097 (*ctx->Driver.Flush)( ctx );
jtgafb833d1999-08-19 00:55:39 +00002098 }
jtgafb833d1999-08-19 00:55:39 +00002099}
Brian Paul48c6a6e2000-09-08 21:28:04 +00002100
2101
Keith Whitwell6dc85572003-07-17 13:43:59 +00002102/*@}*/