radeon/r200/r300: cleanup some of the renderbuffer code
[mesa.git] / src / mesa / main / context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file context.c
28 * Mesa context/visual/framebuffer management functions.
29 * \author Brian Paul
30 */
31
32 /**
33 * \mainpage Mesa Main Module
34 *
35 * \section MainIntroduction Introduction
36 *
37 * 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>
46 *
47 * Other modules are responsible for API dispatch, vertex transformation,
48 * point/line/triangle setup, rasterization, vertex array caching,
49 * vertex/fragment programs/shaders, etc.
50 *
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 *
64 * Selecting <b>Data Structures</b> will list all C structures.
65 *
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 *
70 * Selecting the <b>Data Fields</b> link will display a list of all
71 * documented structure members.
72 *
73 * Selecting the <b>Globals</b> link will display a list
74 * of all functions, structures, global variables and macros in the module.
75 *
76 */
77
78
79 #include "glheader.h"
80 #include "imports.h"
81 #if FEATURE_accum
82 #include "accum.h"
83 #endif
84 #include "api_exec.h"
85 #include "arrayobj.h"
86 #if FEATURE_attrib_stack
87 #include "attrib.h"
88 #endif
89 #include "blend.h"
90 #include "buffers.h"
91 #include "bufferobj.h"
92 #if FEATURE_colortable
93 #include "colortab.h"
94 #endif
95 #include "context.h"
96 #include "debug.h"
97 #include "depth.h"
98 #if FEATURE_dlist
99 #include "dlist.h"
100 #endif
101 #if FEATURE_evaluators
102 #include "eval.h"
103 #endif
104 #include "enums.h"
105 #include "extensions.h"
106 #include "fbobject.h"
107 #if FEATURE_feedback
108 #include "feedback.h"
109 #endif
110 #include "fog.h"
111 #include "framebuffer.h"
112 #include "get.h"
113 #if FEATURE_histogram
114 #include "histogram.h"
115 #endif
116 #include "hint.h"
117 #include "hash.h"
118 #include "light.h"
119 #include "lines.h"
120 #include "macros.h"
121 #include "matrix.h"
122 #include "multisample.h"
123 #include "pixel.h"
124 #include "pixelstore.h"
125 #include "points.h"
126 #include "polygon.h"
127 #if FEATURE_ARB_occlusion_query
128 #include "queryobj.h"
129 #endif
130 #if FEATURE_drawpix
131 #include "rastpos.h"
132 #endif
133 #include "scissor.h"
134 #include "simple_list.h"
135 #include "state.h"
136 #include "stencil.h"
137 #include "texcompress.h"
138 #include "teximage.h"
139 #include "texobj.h"
140 #include "texstate.h"
141 #include "mtypes.h"
142 #include "varray.h"
143 #include "version.h"
144 #include "vtxfmt.h"
145 #include "glapi/glthread.h"
146 #include "glapi/glapioffsets.h"
147 #include "glapi/glapitable.h"
148 #include "shader/program.h"
149 #include "shader/shader_api.h"
150 #if FEATURE_ATI_fragment_shader
151 #include "shader/atifragshader.h"
152 #endif
153 #if _HAVE_FULL_GL
154 #include "math/m_matrix.h"
155 #endif
156
157 #ifdef USE_SPARC_ASM
158 #include "sparc/sparc.h"
159 #endif
160
161 #ifndef MESA_VERBOSE
162 int MESA_VERBOSE = 0;
163 #endif
164
165 #ifndef MESA_DEBUG_FLAGS
166 int MESA_DEBUG_FLAGS = 0;
167 #endif
168
169
170 /* ubyte -> float conversion */
171 GLfloat _mesa_ubyte_to_float_color_tab[256];
172
173
174
175 /**
176 * Swap buffers notification callback.
177 *
178 * \param gc GL context.
179 *
180 * Called by window system just before swapping buffers.
181 * We have to finish any pending rendering.
182 */
183 void
184 _mesa_notifySwapBuffers(__GLcontext *ctx)
185 {
186 if (ctx->Driver.Flush) {
187 ctx->Driver.Flush(ctx);
188 }
189 }
190
191
192 /**********************************************************************/
193 /** \name GL Visual allocation/destruction */
194 /**********************************************************************/
195 /*@{*/
196
197 /**
198 * Allocates a GLvisual structure and initializes it via
199 * _mesa_initialize_visual().
200 *
201 * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode.
202 * \param dbFlag double buffering
203 * \param stereoFlag stereo buffer
204 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
205 * is acceptable but the actual depth type will be GLushort or GLuint as
206 * needed.
207 * \param stencilBits requested minimum bits per stencil buffer value
208 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer.
209 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
210 * \param redBits number of bits per color component in frame buffer for RGB(A)
211 * mode. We always use 8 in core Mesa though.
212 * \param greenBits same as above.
213 * \param blueBits same as above.
214 * \param alphaBits same as above.
215 * \param numSamples not really used.
216 *
217 * \return pointer to new GLvisual or NULL if requested parameters can't be
218 * met.
219 *
220 * \note Need to add params for level and numAuxBuffers (at least)
221 */
222 GLvisual *
223 _mesa_create_visual( GLboolean rgbFlag,
224 GLboolean dbFlag,
225 GLboolean stereoFlag,
226 GLint redBits,
227 GLint greenBits,
228 GLint blueBits,
229 GLint alphaBits,
230 GLint indexBits,
231 GLint depthBits,
232 GLint stencilBits,
233 GLint accumRedBits,
234 GLint accumGreenBits,
235 GLint accumBlueBits,
236 GLint accumAlphaBits,
237 GLint numSamples )
238 {
239 GLvisual *vis = (GLvisual *) _mesa_calloc(sizeof(GLvisual));
240 if (vis) {
241 if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag,
242 redBits, greenBits, blueBits, alphaBits,
243 indexBits, depthBits, stencilBits,
244 accumRedBits, accumGreenBits,
245 accumBlueBits, accumAlphaBits,
246 numSamples)) {
247 _mesa_free(vis);
248 return NULL;
249 }
250 }
251 return vis;
252 }
253
254 /**
255 * Makes some sanity checks and fills in the fields of the
256 * GLvisual object with the given parameters. If the caller needs
257 * to set additional fields, he should just probably init the whole GLvisual
258 * object himself.
259 * \return GL_TRUE on success, or GL_FALSE on failure.
260 *
261 * \sa _mesa_create_visual() above for the parameter description.
262 */
263 GLboolean
264 _mesa_initialize_visual( GLvisual *vis,
265 GLboolean rgbFlag,
266 GLboolean dbFlag,
267 GLboolean stereoFlag,
268 GLint redBits,
269 GLint greenBits,
270 GLint blueBits,
271 GLint alphaBits,
272 GLint indexBits,
273 GLint depthBits,
274 GLint stencilBits,
275 GLint accumRedBits,
276 GLint accumGreenBits,
277 GLint accumBlueBits,
278 GLint accumAlphaBits,
279 GLint numSamples )
280 {
281 assert(vis);
282
283 if (depthBits < 0 || depthBits > 32) {
284 return GL_FALSE;
285 }
286 if (stencilBits < 0 || stencilBits > STENCIL_BITS) {
287 return GL_FALSE;
288 }
289 assert(accumRedBits >= 0);
290 assert(accumGreenBits >= 0);
291 assert(accumBlueBits >= 0);
292 assert(accumAlphaBits >= 0);
293
294 vis->rgbMode = rgbFlag;
295 vis->doubleBufferMode = dbFlag;
296 vis->stereoMode = stereoFlag;
297
298 vis->redBits = redBits;
299 vis->greenBits = greenBits;
300 vis->blueBits = blueBits;
301 vis->alphaBits = alphaBits;
302 vis->rgbBits = redBits + greenBits + blueBits;
303
304 vis->indexBits = indexBits;
305 vis->depthBits = depthBits;
306 vis->stencilBits = stencilBits;
307
308 vis->accumRedBits = accumRedBits;
309 vis->accumGreenBits = accumGreenBits;
310 vis->accumBlueBits = accumBlueBits;
311 vis->accumAlphaBits = accumAlphaBits;
312
313 vis->haveAccumBuffer = accumRedBits > 0;
314 vis->haveDepthBuffer = depthBits > 0;
315 vis->haveStencilBuffer = stencilBits > 0;
316
317 vis->numAuxBuffers = 0;
318 vis->level = 0;
319 vis->pixmapMode = 0;
320 vis->sampleBuffers = numSamples > 0 ? 1 : 0;
321 vis->samples = numSamples;
322
323 return GL_TRUE;
324 }
325
326
327 /**
328 * Destroy a visual and free its memory.
329 *
330 * \param vis visual.
331 *
332 * Frees the visual structure.
333 */
334 void
335 _mesa_destroy_visual( GLvisual *vis )
336 {
337 _mesa_free(vis);
338 }
339
340 /*@}*/
341
342
343 /**********************************************************************/
344 /** \name Context allocation, initialization, destroying
345 *
346 * The purpose of the most initialization functions here is to provide the
347 * default state values according to the OpenGL specification.
348 */
349 /**********************************************************************/
350 /*@{*/
351
352 /**
353 * One-time initialization mutex lock.
354 *
355 * \sa Used by one_time_init().
356 */
357 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
358
359 /**
360 * Calls all the various one-time-init functions in Mesa.
361 *
362 * While holding a global mutex lock, calls several initialization functions,
363 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
364 * defined.
365 *
366 * \sa _math_init().
367 */
368 static void
369 one_time_init( GLcontext *ctx )
370 {
371 static GLboolean alreadyCalled = GL_FALSE;
372 (void) ctx;
373 _glthread_LOCK_MUTEX(OneTimeLock);
374 if (!alreadyCalled) {
375 GLuint i;
376
377 /* do some implementation tests */
378 assert( sizeof(GLbyte) == 1 );
379 assert( sizeof(GLubyte) == 1 );
380 assert( sizeof(GLshort) == 2 );
381 assert( sizeof(GLushort) == 2 );
382 assert( sizeof(GLint) == 4 );
383 assert( sizeof(GLuint) == 4 );
384
385 _mesa_init_sqrt_table();
386
387 for (i = 0; i < 256; i++) {
388 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
389 }
390
391 #ifdef USE_SPARC_ASM
392 _mesa_init_sparc_glapi_relocs();
393 #endif
394 if (_mesa_getenv("MESA_DEBUG")) {
395 _glapi_noop_enable_warnings(GL_TRUE);
396 _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
397 }
398 else {
399 _glapi_noop_enable_warnings(GL_FALSE);
400 }
401
402 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
403 _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
404 MESA_VERSION_STRING, __DATE__, __TIME__);
405 #endif
406
407 alreadyCalled = GL_TRUE;
408 }
409 _glthread_UNLOCK_MUTEX(OneTimeLock);
410 }
411
412
413 /**
414 * Allocate and initialize a shared context state structure.
415 * Initializes the display list, texture objects and vertex programs hash
416 * tables, allocates the texture objects. If it runs out of memory, frees
417 * everything already allocated before returning NULL.
418 *
419 * \return pointer to a gl_shared_state structure on success, or NULL on
420 * failure.
421 */
422 static GLboolean
423 alloc_shared_state( GLcontext *ctx )
424 {
425 struct gl_shared_state *ss = CALLOC_STRUCT(gl_shared_state);
426 if (!ss)
427 return GL_FALSE;
428
429 ctx->Shared = ss;
430
431 _glthread_INIT_MUTEX(ss->Mutex);
432
433 ss->DisplayList = _mesa_NewHashTable();
434 ss->TexObjects = _mesa_NewHashTable();
435 ss->Programs = _mesa_NewHashTable();
436
437 #if FEATURE_ARB_vertex_program
438 ss->DefaultVertexProgram = (struct gl_vertex_program *)
439 ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
440 if (!ss->DefaultVertexProgram)
441 goto cleanup;
442 #endif
443 #if FEATURE_ARB_fragment_program
444 ss->DefaultFragmentProgram = (struct gl_fragment_program *)
445 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
446 if (!ss->DefaultFragmentProgram)
447 goto cleanup;
448 #endif
449 #if FEATURE_ATI_fragment_shader
450 ss->ATIShaders = _mesa_NewHashTable();
451 ss->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
452 if (!ss->DefaultFragmentShader)
453 goto cleanup;
454 #endif
455
456 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
457 ss->BufferObjects = _mesa_NewHashTable();
458 #endif
459
460 ss->ArrayObjects = _mesa_NewHashTable();
461
462 #if FEATURE_ARB_shader_objects
463 ss->ShaderObjects = _mesa_NewHashTable();
464 #endif
465
466 ss->Default1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
467 if (!ss->Default1D)
468 goto cleanup;
469
470 ss->Default2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
471 if (!ss->Default2D)
472 goto cleanup;
473
474 ss->Default3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
475 if (!ss->Default3D)
476 goto cleanup;
477
478 ss->DefaultCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
479 if (!ss->DefaultCubeMap)
480 goto cleanup;
481
482 ss->DefaultRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
483 if (!ss->DefaultRect)
484 goto cleanup;
485
486 ss->Default1DArray = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D_ARRAY_EXT);
487 if (!ss->Default1DArray)
488 goto cleanup;
489
490 ss->Default2DArray = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D_ARRAY_EXT);
491 if (!ss->Default2DArray)
492 goto cleanup;
493
494 /* sanity check */
495 assert(ss->Default1D->RefCount == 1);
496
497 _glthread_INIT_MUTEX(ss->TexMutex);
498 ss->TextureStateStamp = 0;
499
500 #if FEATURE_EXT_framebuffer_object
501 ss->FrameBuffers = _mesa_NewHashTable();
502 if (!ss->FrameBuffers)
503 goto cleanup;
504 ss->RenderBuffers = _mesa_NewHashTable();
505 if (!ss->RenderBuffers)
506 goto cleanup;
507 #endif
508
509 return GL_TRUE;
510
511 cleanup:
512 /* Ran out of memory at some point. Free everything and return NULL */
513 if (ss->DisplayList)
514 _mesa_DeleteHashTable(ss->DisplayList);
515 if (ss->TexObjects)
516 _mesa_DeleteHashTable(ss->TexObjects);
517 if (ss->Programs)
518 _mesa_DeleteHashTable(ss->Programs);
519 #if FEATURE_ARB_vertex_program
520 _mesa_reference_vertprog(ctx, &ss->DefaultVertexProgram, NULL);
521 #endif
522 #if FEATURE_ARB_fragment_program
523 _mesa_reference_fragprog(ctx, &ss->DefaultFragmentProgram, NULL);
524 #endif
525 #if FEATURE_ATI_fragment_shader
526 if (ss->DefaultFragmentShader)
527 _mesa_delete_ati_fragment_shader(ctx, ss->DefaultFragmentShader);
528 #endif
529 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
530 if (ss->BufferObjects)
531 _mesa_DeleteHashTable(ss->BufferObjects);
532 #endif
533
534 if (ss->ArrayObjects)
535 _mesa_DeleteHashTable (ss->ArrayObjects);
536
537 #if FEATURE_ARB_shader_objects
538 if (ss->ShaderObjects)
539 _mesa_DeleteHashTable (ss->ShaderObjects);
540 #endif
541
542 #if FEATURE_EXT_framebuffer_object
543 if (ss->FrameBuffers)
544 _mesa_DeleteHashTable(ss->FrameBuffers);
545 if (ss->RenderBuffers)
546 _mesa_DeleteHashTable(ss->RenderBuffers);
547 #endif
548
549 if (ss->Default1D)
550 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1D);
551 if (ss->Default2D)
552 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2D);
553 if (ss->Default3D)
554 (*ctx->Driver.DeleteTexture)(ctx, ss->Default3D);
555 if (ss->DefaultCubeMap)
556 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultCubeMap);
557 if (ss->DefaultRect)
558 (*ctx->Driver.DeleteTexture)(ctx, ss->DefaultRect);
559 if (ss->Default1DArray)
560 (*ctx->Driver.DeleteTexture)(ctx, ss->Default1DArray);
561 if (ss->Default2DArray)
562 (*ctx->Driver.DeleteTexture)(ctx, ss->Default2DArray);
563
564 _mesa_free(ss);
565
566 return GL_FALSE;
567 }
568
569
570 /**
571 * Callback for deleting a display list. Called by _mesa_HashDeleteAll().
572 */
573 static void
574 delete_displaylist_cb(GLuint id, void *data, void *userData)
575 {
576 #if FEATURE_dlist
577 struct gl_display_list *list = (struct gl_display_list *) data;
578 GLcontext *ctx = (GLcontext *) userData;
579 _mesa_delete_list(ctx, list);
580 #endif
581 }
582
583 /**
584 * Callback for deleting a texture object. Called by _mesa_HashDeleteAll().
585 */
586 static void
587 delete_texture_cb(GLuint id, void *data, void *userData)
588 {
589 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
590 GLcontext *ctx = (GLcontext *) userData;
591 ctx->Driver.DeleteTexture(ctx, texObj);
592 }
593
594 /**
595 * Callback for deleting a program object. Called by _mesa_HashDeleteAll().
596 */
597 static void
598 delete_program_cb(GLuint id, void *data, void *userData)
599 {
600 struct gl_program *prog = (struct gl_program *) data;
601 GLcontext *ctx = (GLcontext *) userData;
602 ASSERT(prog->RefCount == 1); /* should only be referenced by hash table */
603 prog->RefCount = 0; /* now going away */
604 ctx->Driver.DeleteProgram(ctx, prog);
605 }
606
607 #if FEATURE_ATI_fragment_shader
608 /**
609 * Callback for deleting an ATI fragment shader object.
610 * Called by _mesa_HashDeleteAll().
611 */
612 static void
613 delete_fragshader_cb(GLuint id, void *data, void *userData)
614 {
615 struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
616 GLcontext *ctx = (GLcontext *) userData;
617 _mesa_delete_ati_fragment_shader(ctx, shader);
618 }
619 #endif
620
621 /**
622 * Callback for deleting a buffer object. Called by _mesa_HashDeleteAll().
623 */
624 static void
625 delete_bufferobj_cb(GLuint id, void *data, void *userData)
626 {
627 struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
628 GLcontext *ctx = (GLcontext *) userData;
629 ctx->Driver.DeleteBuffer(ctx, bufObj);
630 }
631
632 /**
633 * Callback for deleting an array object. Called by _mesa_HashDeleteAll().
634 */
635 static void
636 delete_arrayobj_cb(GLuint id, void *data, void *userData)
637 {
638 struct gl_array_object *arrayObj = (struct gl_array_object *) data;
639 GLcontext *ctx = (GLcontext *) userData;
640 _mesa_delete_array_object(ctx, arrayObj);
641 }
642
643 /**
644 * Callback for freeing shader program data. Call it before delete_shader_cb
645 * to avoid memory access error.
646 */
647 static void
648 free_shader_program_data_cb(GLuint id, void *data, void *userData)
649 {
650 GLcontext *ctx = (GLcontext *) userData;
651 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
652
653 if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
654 _mesa_free_shader_program_data(ctx, shProg);
655 }
656 }
657
658 /**
659 * Callback for deleting shader and shader programs objects.
660 * Called by _mesa_HashDeleteAll().
661 */
662 static void
663 delete_shader_cb(GLuint id, void *data, void *userData)
664 {
665 GLcontext *ctx = (GLcontext *) userData;
666 struct gl_shader *sh = (struct gl_shader *) data;
667 if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
668 _mesa_free_shader(ctx, sh);
669 }
670 else {
671 struct gl_shader_program *shProg = (struct gl_shader_program *) data;
672 ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
673 _mesa_free_shader_program(ctx, shProg);
674 }
675 }
676
677 /**
678 * Callback for deleting a framebuffer object. Called by _mesa_HashDeleteAll()
679 */
680 static void
681 delete_framebuffer_cb(GLuint id, void *data, void *userData)
682 {
683 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
684 /* The fact that the framebuffer is in the hashtable means its refcount
685 * is one, but we're removing from the hashtable now. So clear refcount.
686 */
687 /*assert(fb->RefCount == 1);*/
688 fb->RefCount = 0;
689
690 /* NOTE: Delete should always be defined but there are two reports
691 * of it being NULL (bugs 13507, 14293). Work-around for now.
692 */
693 if (fb->Delete)
694 fb->Delete(fb);
695 }
696
697 /**
698 * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
699 */
700 static void
701 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
702 {
703 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
704 rb->RefCount = 0; /* see comment for FBOs above */
705 if (rb->Delete)
706 rb->Delete(rb);
707 }
708
709
710 /**
711 * Deallocate a shared state object and all children structures.
712 *
713 * \param ctx GL context.
714 * \param ss shared state pointer.
715 *
716 * Frees the display lists, the texture objects (calling the driver texture
717 * deletion callback to free its private data) and the vertex programs, as well
718 * as their hash tables.
719 *
720 * \sa alloc_shared_state().
721 */
722 static void
723 free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
724 {
725 /*
726 * Free display lists
727 */
728 _mesa_HashDeleteAll(ss->DisplayList, delete_displaylist_cb, ctx);
729 _mesa_DeleteHashTable(ss->DisplayList);
730
731 #if FEATURE_ARB_shader_objects
732 _mesa_HashWalk(ss->ShaderObjects, free_shader_program_data_cb, ctx);
733 _mesa_HashDeleteAll(ss->ShaderObjects, delete_shader_cb, ctx);
734 _mesa_DeleteHashTable(ss->ShaderObjects);
735 #endif
736
737 _mesa_HashDeleteAll(ss->Programs, delete_program_cb, ctx);
738 _mesa_DeleteHashTable(ss->Programs);
739
740 #if FEATURE_ARB_vertex_program
741 _mesa_reference_vertprog(ctx, &ss->DefaultVertexProgram, NULL);
742 #endif
743 #if FEATURE_ARB_fragment_program
744 _mesa_reference_fragprog(ctx, &ss->DefaultFragmentProgram, NULL);
745 #endif
746
747 #if FEATURE_ATI_fragment_shader
748 _mesa_HashDeleteAll(ss->ATIShaders, delete_fragshader_cb, ctx);
749 _mesa_DeleteHashTable(ss->ATIShaders);
750 _mesa_delete_ati_fragment_shader(ctx, ss->DefaultFragmentShader);
751 #endif
752
753 #if FEATURE_ARB_vertex_buffer_object || FEATURE_ARB_pixel_buffer_object
754 _mesa_HashDeleteAll(ss->BufferObjects, delete_bufferobj_cb, ctx);
755 _mesa_DeleteHashTable(ss->BufferObjects);
756 #endif
757
758 _mesa_HashDeleteAll(ss->ArrayObjects, delete_arrayobj_cb, ctx);
759 _mesa_DeleteHashTable(ss->ArrayObjects);
760
761 #if FEATURE_EXT_framebuffer_object
762 _mesa_HashDeleteAll(ss->FrameBuffers, delete_framebuffer_cb, ctx);
763 _mesa_DeleteHashTable(ss->FrameBuffers);
764 _mesa_HashDeleteAll(ss->RenderBuffers, delete_renderbuffer_cb, ctx);
765 _mesa_DeleteHashTable(ss->RenderBuffers);
766 #endif
767
768 /*
769 * Free texture objects (after FBOs since some textures might have
770 * been bound to FBOs).
771 */
772 ASSERT(ctx->Driver.DeleteTexture);
773 /* the default textures */
774 ctx->Driver.DeleteTexture(ctx, ss->Default1D);
775 ctx->Driver.DeleteTexture(ctx, ss->Default2D);
776 ctx->Driver.DeleteTexture(ctx, ss->Default3D);
777 ctx->Driver.DeleteTexture(ctx, ss->DefaultCubeMap);
778 ctx->Driver.DeleteTexture(ctx, ss->DefaultRect);
779 ctx->Driver.DeleteTexture(ctx, ss->Default1DArray);
780 ctx->Driver.DeleteTexture(ctx, ss->Default2DArray);
781 /* all other textures */
782 _mesa_HashDeleteAll(ss->TexObjects, delete_texture_cb, ctx);
783 _mesa_DeleteHashTable(ss->TexObjects);
784
785 _glthread_DESTROY_MUTEX(ss->Mutex);
786
787 _mesa_free(ss);
788 }
789
790
791 /**
792 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
793 */
794 static void
795 _mesa_init_current(GLcontext *ctx)
796 {
797 GLuint i;
798
799 /* Init all to (0,0,0,1) */
800 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
801 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
802 }
803
804 /* redo special cases: */
805 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 0.0 );
806 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
807 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
808 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
809 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
810 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
811 }
812
813
814 /**
815 * Init vertex/fragment program limits.
816 * Important: drivers should override these with actual limits.
817 */
818 static void
819 init_program_limits(GLenum type, struct gl_program_constants *prog)
820 {
821 prog->MaxInstructions = MAX_PROGRAM_INSTRUCTIONS;
822 prog->MaxAluInstructions = MAX_PROGRAM_INSTRUCTIONS;
823 prog->MaxTexInstructions = MAX_PROGRAM_INSTRUCTIONS;
824 prog->MaxTexIndirections = MAX_PROGRAM_INSTRUCTIONS;
825 prog->MaxTemps = MAX_PROGRAM_TEMPS;
826 prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS;
827 prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
828 prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
829
830 if (type == GL_VERTEX_PROGRAM_ARB) {
831 prog->MaxParameters = MAX_NV_VERTEX_PROGRAM_PARAMS;
832 prog->MaxAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
833 prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
834 }
835 else {
836 prog->MaxParameters = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
837 prog->MaxAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
838 prog->MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
839 }
840
841 /* copy the above limits to init native limits */
842 prog->MaxNativeInstructions = prog->MaxInstructions;
843 prog->MaxNativeAluInstructions = prog->MaxAluInstructions;
844 prog->MaxNativeTexInstructions = prog->MaxTexInstructions;
845 prog->MaxNativeTexIndirections = prog->MaxTexIndirections;
846 prog->MaxNativeAttribs = prog->MaxAttribs;
847 prog->MaxNativeTemps = prog->MaxTemps;
848 prog->MaxNativeAddressRegs = prog->MaxAddressRegs;
849 prog->MaxNativeParameters = prog->MaxParameters;
850 }
851
852
853 /**
854 * Initialize fields of gl_constants (aka ctx->Const.*).
855 * Use defaults from config.h. The device drivers will often override
856 * some of these values (such as number of texture units).
857 */
858 static void
859 _mesa_init_constants(GLcontext *ctx)
860 {
861 assert(ctx);
862
863 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
864 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
865
866 /* Constants, may be overriden (usually only reduced) by device drivers */
867 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
868 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
869 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
870 ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
871 ctx->Const.MaxArrayTextureLayers = MAX_ARRAY_TEXTURE_LAYERS;
872 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
873 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
874 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureCoordUnits,
875 ctx->Const.MaxTextureImageUnits);
876 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
877 ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
878 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
879 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
880 ctx->Const.MinPointSize = MIN_POINT_SIZE;
881 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
882 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
883 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
884 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
885 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
886 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
887 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
888 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
889 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
890 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
891 ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH;
892 ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT;
893 ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
894 ctx->Const.MaxLights = MAX_LIGHTS;
895 ctx->Const.MaxShininess = 128.0;
896 ctx->Const.MaxSpotExponent = 128.0;
897 ctx->Const.MaxViewportWidth = MAX_WIDTH;
898 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
899 #if FEATURE_ARB_vertex_program
900 init_program_limits(GL_VERTEX_PROGRAM_ARB, &ctx->Const.VertexProgram);
901 #endif
902 #if FEATURE_ARB_fragment_program
903 init_program_limits(GL_FRAGMENT_PROGRAM_ARB, &ctx->Const.FragmentProgram);
904 #endif
905 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
906 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
907
908 /* CheckArrayBounds is overriden by drivers/x11 for X server */
909 ctx->Const.CheckArrayBounds = GL_FALSE;
910
911 /* GL_ARB_draw_buffers */
912 ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
913
914 /* GL_OES_read_format */
915 ctx->Const.ColorReadFormat = GL_RGBA;
916 ctx->Const.ColorReadType = GL_UNSIGNED_BYTE;
917
918 #if FEATURE_EXT_framebuffer_object
919 ctx->Const.MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
920 ctx->Const.MaxRenderbufferSize = MAX_WIDTH;
921 #endif
922
923 #if FEATURE_ARB_vertex_shader
924 ctx->Const.MaxVertexTextureImageUnits = MAX_VERTEX_TEXTURE_IMAGE_UNITS;
925 ctx->Const.MaxVarying = MAX_VARYING;
926 #endif
927
928 /* GL_ARB_framebuffer_object */
929 ctx->Const.MaxSamples = 0;
930
931 /* sanity checks */
932 ASSERT(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.MaxTextureImageUnits,
933 ctx->Const.MaxTextureCoordUnits));
934 ASSERT(ctx->Const.FragmentProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
935 ASSERT(ctx->Const.VertexProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
936
937 ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
938 ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
939 ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS <= VERT_ATTRIB_MAX);
940 ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS <= VERT_RESULT_MAX);
941 }
942
943
944 /**
945 * Do some sanity checks on the limits/constants for the given context.
946 * Only called the first time a context is bound.
947 */
948 static void
949 check_context_limits(GLcontext *ctx)
950 {
951 /* Many context limits/constants are limited by the size of
952 * internal arrays.
953 */
954 assert(ctx->Const.MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS);
955 assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS);
956 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS);
957 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS);
958
959 /* number of coord units cannot be greater than number of image units */
960 assert(ctx->Const.MaxTextureCoordUnits <= ctx->Const.MaxTextureImageUnits);
961
962 assert(ctx->Const.MaxViewportWidth <= MAX_WIDTH);
963 assert(ctx->Const.MaxViewportHeight <= MAX_WIDTH);
964
965 /* make sure largest texture image is <= MAX_WIDTH in size */
966 assert((1 << (ctx->Const.MaxTextureLevels -1 )) <= MAX_WIDTH);
967 assert((1 << (ctx->Const.MaxCubeTextureLevels -1 )) <= MAX_WIDTH);
968 assert((1 << (ctx->Const.Max3DTextureLevels -1 )) <= MAX_WIDTH);
969
970 assert(ctx->Const.MaxDrawBuffers <= MAX_DRAW_BUFFERS);
971
972 /* XXX probably add more tests */
973 }
974
975
976 /**
977 * Initialize the attribute groups in a GL context.
978 *
979 * \param ctx GL context.
980 *
981 * Initializes all the attributes, calling the respective <tt>init*</tt>
982 * functions for the more complex data structures.
983 */
984 static GLboolean
985 init_attrib_groups(GLcontext *ctx)
986 {
987 assert(ctx);
988
989 /* Constants */
990 _mesa_init_constants( ctx );
991
992 /* Extensions */
993 _mesa_init_extensions( ctx );
994
995 /* Attribute Groups */
996 #if FEATURE_accum
997 _mesa_init_accum( ctx );
998 #endif
999 #if FEATURE_attrib_stack
1000 _mesa_init_attrib( ctx );
1001 #endif
1002 _mesa_init_buffer_objects( ctx );
1003 _mesa_init_color( ctx );
1004 #if FEATURE_colortable
1005 _mesa_init_colortables( ctx );
1006 #endif
1007 _mesa_init_current( ctx );
1008 _mesa_init_depth( ctx );
1009 _mesa_init_debug( ctx );
1010 #if FEATURE_dlist
1011 _mesa_init_display_list( ctx );
1012 #endif
1013 #if FEATURE_evaluators
1014 _mesa_init_eval( ctx );
1015 #endif
1016 _mesa_init_fbobjects( ctx );
1017 #if FEATURE_feedback
1018 _mesa_init_feedback( ctx );
1019 #else
1020 ctx->RenderMode = GL_RENDER;
1021 #endif
1022 _mesa_init_fog( ctx );
1023 #if FEATURE_histogram
1024 _mesa_init_histogram( ctx );
1025 #endif
1026 _mesa_init_hint( ctx );
1027 _mesa_init_line( ctx );
1028 _mesa_init_lighting( ctx );
1029 _mesa_init_matrix( ctx );
1030 _mesa_init_multisample( ctx );
1031 _mesa_init_pixel( ctx );
1032 _mesa_init_pixelstore( ctx );
1033 _mesa_init_point( ctx );
1034 _mesa_init_polygon( ctx );
1035 _mesa_init_program( ctx );
1036 #if FEATURE_ARB_occlusion_query
1037 _mesa_init_query( ctx );
1038 #endif
1039 #if FEATURE_drawpix
1040 _mesa_init_rastpos( ctx );
1041 #endif
1042 _mesa_init_scissor( ctx );
1043 _mesa_init_shader_state( ctx );
1044 _mesa_init_stencil( ctx );
1045 _mesa_init_transform( ctx );
1046 _mesa_init_varray( ctx );
1047 _mesa_init_viewport( ctx );
1048
1049 if (!_mesa_init_texture( ctx ))
1050 return GL_FALSE;
1051
1052 #if FEATURE_texture_s3tc
1053 _mesa_init_texture_s3tc( ctx );
1054 #endif
1055 #if FEATURE_texture_fxt1
1056 _mesa_init_texture_fxt1( ctx );
1057 #endif
1058
1059 /* Miscellaneous */
1060 ctx->NewState = _NEW_ALL;
1061 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
1062 ctx->varying_vp_inputs = ~0;
1063
1064 return GL_TRUE;
1065 }
1066
1067
1068 /**
1069 * Update default objects in a GL context with respect to shared state.
1070 *
1071 * \param ctx GL context.
1072 *
1073 * Removes references to old default objects, (texture objects, program
1074 * objects, etc.) and changes to reference those from the current shared
1075 * state.
1076 */
1077 static GLboolean
1078 update_default_objects(GLcontext *ctx)
1079 {
1080 assert(ctx);
1081
1082 _mesa_update_default_objects_program(ctx);
1083 _mesa_update_default_objects_texture(ctx);
1084 _mesa_update_default_objects_buffer_objects(ctx);
1085
1086 return GL_TRUE;
1087 }
1088
1089
1090 /**
1091 * This is the default function we plug into all dispatch table slots
1092 * This helps prevents a segfault when someone calls a GL function without
1093 * first checking if the extension's supported.
1094 */
1095 static int
1096 generic_nop(void)
1097 {
1098 _mesa_warning(NULL, "User called no-op dispatch function (an unsupported extension function?)");
1099 return 0;
1100 }
1101
1102
1103 /**
1104 * Allocate and initialize a new dispatch table.
1105 */
1106 static struct _glapi_table *
1107 alloc_dispatch_table(void)
1108 {
1109 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1110 * In practice, this'll be the same for stand-alone Mesa. But for DRI
1111 * Mesa we do this to accomodate different versions of libGL and various
1112 * DRI drivers.
1113 */
1114 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(),
1115 sizeof(struct _glapi_table) / sizeof(_glapi_proc));
1116 struct _glapi_table *table =
1117 (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc));
1118 if (table) {
1119 _glapi_proc *entry = (_glapi_proc *) table;
1120 GLint i;
1121 for (i = 0; i < numEntries; i++) {
1122 entry[i] = (_glapi_proc) generic_nop;
1123 }
1124 }
1125 return table;
1126 }
1127
1128
1129 /**
1130 * Initialize a GLcontext struct (rendering context).
1131 *
1132 * This includes allocating all the other structs and arrays which hang off of
1133 * the context by pointers.
1134 * Note that the driver needs to pass in its dd_function_table here since
1135 * we need to at least call driverFunctions->NewTextureObject to create the
1136 * default texture objects.
1137 *
1138 * Called by _mesa_create_context().
1139 *
1140 * Performs the imports and exports callback tables initialization, and
1141 * miscellaneous one-time initializations. If no shared context is supplied one
1142 * is allocated, and increase its reference count. Setups the GL API dispatch
1143 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
1144 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1145 * for debug flags.
1146 *
1147 * \param ctx the context to initialize
1148 * \param visual describes the visual attributes for this context
1149 * \param share_list points to context to share textures, display lists,
1150 * etc with, or NULL
1151 * \param driverFunctions table of device driver functions for this context
1152 * to use
1153 * \param driverContext pointer to driver-specific context data
1154 */
1155 GLboolean
1156 _mesa_initialize_context(GLcontext *ctx,
1157 const GLvisual *visual,
1158 GLcontext *share_list,
1159 const struct dd_function_table *driverFunctions,
1160 void *driverContext)
1161 {
1162 /*ASSERT(driverContext);*/
1163 assert(driverFunctions->NewTextureObject);
1164 assert(driverFunctions->FreeTexImageData);
1165
1166 /* misc one-time initializations */
1167 one_time_init(ctx);
1168
1169 ctx->Visual = *visual;
1170 ctx->DrawBuffer = NULL;
1171 ctx->ReadBuffer = NULL;
1172 ctx->WinSysDrawBuffer = NULL;
1173 ctx->WinSysReadBuffer = NULL;
1174
1175 /* Plug in driver functions and context pointer here.
1176 * This is important because when we call alloc_shared_state() below
1177 * we'll call ctx->Driver.NewTextureObject() to create the default
1178 * textures.
1179 */
1180 ctx->Driver = *driverFunctions;
1181 ctx->DriverCtx = driverContext;
1182
1183 if (share_list) {
1184 /* share state with another context */
1185 ctx->Shared = share_list->Shared;
1186 }
1187 else {
1188 /* allocate new, unshared state */
1189 if (!alloc_shared_state( ctx )) {
1190 return GL_FALSE;
1191 }
1192 }
1193 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1194 ctx->Shared->RefCount++;
1195 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1196
1197 if (!init_attrib_groups( ctx )) {
1198 free_shared_state(ctx, ctx->Shared);
1199 return GL_FALSE;
1200 }
1201
1202 /* setup the API dispatch tables */
1203 ctx->Exec = alloc_dispatch_table();
1204 ctx->Save = alloc_dispatch_table();
1205 if (!ctx->Exec || !ctx->Save) {
1206 free_shared_state(ctx, ctx->Shared);
1207 if (ctx->Exec)
1208 _mesa_free(ctx->Exec);
1209 }
1210 #if FEATURE_dispatch
1211 _mesa_init_exec_table(ctx->Exec);
1212 #endif
1213 ctx->CurrentDispatch = ctx->Exec;
1214 #if FEATURE_dlist
1215 _mesa_init_dlist_table(ctx->Save);
1216 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
1217 #endif
1218 /* Neutral tnl module stuff */
1219 _mesa_init_exec_vtxfmt( ctx );
1220 ctx->TnlModule.Current = NULL;
1221 ctx->TnlModule.SwapCount = 0;
1222
1223 ctx->FragmentProgram._MaintainTexEnvProgram
1224 = (_mesa_getenv("MESA_TEX_PROG") != NULL);
1225
1226 ctx->VertexProgram._MaintainTnlProgram
1227 = (_mesa_getenv("MESA_TNL_PROG") != NULL);
1228 if (ctx->VertexProgram._MaintainTnlProgram) {
1229 /* this is required... */
1230 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
1231 }
1232
1233 #ifdef FEATURE_extra_context_init
1234 _mesa_initialize_context_extra(ctx);
1235 #endif
1236
1237 ctx->FirstTimeCurrent = GL_TRUE;
1238
1239 return GL_TRUE;
1240 }
1241
1242
1243 /**
1244 * Allocate and initialize a GLcontext structure.
1245 * Note that the driver needs to pass in its dd_function_table here since
1246 * we need to at least call driverFunctions->NewTextureObject to initialize
1247 * the rendering context.
1248 *
1249 * \param visual a GLvisual pointer (we copy the struct contents)
1250 * \param share_list another context to share display lists with or NULL
1251 * \param driverFunctions points to the dd_function_table into which the
1252 * driver has plugged in all its special functions.
1253 * \param driverCtx points to the device driver's private context state
1254 *
1255 * \return pointer to a new __GLcontextRec or NULL if error.
1256 */
1257 GLcontext *
1258 _mesa_create_context(const GLvisual *visual,
1259 GLcontext *share_list,
1260 const struct dd_function_table *driverFunctions,
1261 void *driverContext)
1262 {
1263 GLcontext *ctx;
1264
1265 ASSERT(visual);
1266 /*ASSERT(driverContext);*/
1267
1268 ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext));
1269 if (!ctx)
1270 return NULL;
1271
1272 if (_mesa_initialize_context(ctx, visual, share_list,
1273 driverFunctions, driverContext)) {
1274 return ctx;
1275 }
1276 else {
1277 _mesa_free(ctx);
1278 return NULL;
1279 }
1280 }
1281
1282
1283 /**
1284 * Free the data associated with the given context.
1285 *
1286 * But doesn't free the GLcontext struct itself.
1287 *
1288 * \sa _mesa_initialize_context() and init_attrib_groups().
1289 */
1290 void
1291 _mesa_free_context_data( GLcontext *ctx )
1292 {
1293 if (!_mesa_get_current_context()){
1294 /* No current context, but we may need one in order to delete
1295 * texture objs, etc. So temporarily bind the context now.
1296 */
1297 _mesa_make_current(ctx, NULL, NULL);
1298 }
1299
1300 /* unreference WinSysDraw/Read buffers */
1301 _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
1302 _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
1303 _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
1304 _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
1305
1306 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
1307 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
1308 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram, NULL);
1309
1310 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
1311 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
1312 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, NULL);
1313
1314 #if FEATURE_attrib_stack
1315 _mesa_free_attrib_data(ctx);
1316 #endif
1317 _mesa_free_lighting_data( ctx );
1318 #if FEATURE_evaluators
1319 _mesa_free_eval_data( ctx );
1320 #endif
1321 _mesa_free_texture_data( ctx );
1322 _mesa_free_matrix_data( ctx );
1323 _mesa_free_viewport_data( ctx );
1324 #if FEATURE_colortable
1325 _mesa_free_colortables_data( ctx );
1326 #endif
1327 _mesa_free_program_data(ctx);
1328 _mesa_free_shader_state(ctx);
1329 #if FEATURE_ARB_occlusion_query
1330 _mesa_free_query_data(ctx);
1331 #endif
1332
1333 #if FEATURE_ARB_vertex_buffer_object
1334 _mesa_delete_buffer_object(ctx, ctx->Array.NullBufferObj);
1335 #endif
1336 _mesa_delete_array_object(ctx, ctx->Array.DefaultArrayObj);
1337
1338 /* free dispatch tables */
1339 _mesa_free(ctx->Exec);
1340 _mesa_free(ctx->Save);
1341
1342 /* Shared context state (display lists, textures, etc) */
1343 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1344 ctx->Shared->RefCount--;
1345 assert(ctx->Shared->RefCount >= 0);
1346 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1347 if (ctx->Shared->RefCount == 0) {
1348 /* free shared state */
1349 free_shared_state( ctx, ctx->Shared );
1350 }
1351
1352 if (ctx->Extensions.String)
1353 _mesa_free((void *) ctx->Extensions.String);
1354
1355 /* unbind the context if it's currently bound */
1356 if (ctx == _mesa_get_current_context()) {
1357 _mesa_make_current(NULL, NULL, NULL);
1358 }
1359 }
1360
1361
1362 /**
1363 * Destroy a GLcontext structure.
1364 *
1365 * \param ctx GL context.
1366 *
1367 * Calls _mesa_free_context_data() and frees the GLcontext structure itself.
1368 */
1369 void
1370 _mesa_destroy_context( GLcontext *ctx )
1371 {
1372 if (ctx) {
1373 _mesa_free_context_data(ctx);
1374 _mesa_free( (void *) ctx );
1375 }
1376 }
1377
1378
1379 #if _HAVE_FULL_GL
1380 /**
1381 * Copy attribute groups from one context to another.
1382 *
1383 * \param src source context
1384 * \param dst destination context
1385 * \param mask bitwise OR of GL_*_BIT flags
1386 *
1387 * According to the bits specified in \p mask, copies the corresponding
1388 * attributes from \p src into \p dst. For many of the attributes a simple \c
1389 * memcpy is not enough due to the existence of internal pointers in their data
1390 * structures.
1391 */
1392 void
1393 _mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
1394 {
1395 if (mask & GL_ACCUM_BUFFER_BIT) {
1396 /* OK to memcpy */
1397 dst->Accum = src->Accum;
1398 }
1399 if (mask & GL_COLOR_BUFFER_BIT) {
1400 /* OK to memcpy */
1401 dst->Color = src->Color;
1402 }
1403 if (mask & GL_CURRENT_BIT) {
1404 /* OK to memcpy */
1405 dst->Current = src->Current;
1406 }
1407 if (mask & GL_DEPTH_BUFFER_BIT) {
1408 /* OK to memcpy */
1409 dst->Depth = src->Depth;
1410 }
1411 if (mask & GL_ENABLE_BIT) {
1412 /* no op */
1413 }
1414 if (mask & GL_EVAL_BIT) {
1415 /* OK to memcpy */
1416 dst->Eval = src->Eval;
1417 }
1418 if (mask & GL_FOG_BIT) {
1419 /* OK to memcpy */
1420 dst->Fog = src->Fog;
1421 }
1422 if (mask & GL_HINT_BIT) {
1423 /* OK to memcpy */
1424 dst->Hint = src->Hint;
1425 }
1426 if (mask & GL_LIGHTING_BIT) {
1427 GLuint i;
1428 /* begin with memcpy */
1429 dst->Light = src->Light;
1430 /* fixup linked lists to prevent pointer insanity */
1431 make_empty_list( &(dst->Light.EnabledList) );
1432 for (i = 0; i < MAX_LIGHTS; i++) {
1433 if (dst->Light.Light[i].Enabled) {
1434 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1435 }
1436 }
1437 }
1438 if (mask & GL_LINE_BIT) {
1439 /* OK to memcpy */
1440 dst->Line = src->Line;
1441 }
1442 if (mask & GL_LIST_BIT) {
1443 /* OK to memcpy */
1444 dst->List = src->List;
1445 }
1446 if (mask & GL_PIXEL_MODE_BIT) {
1447 /* OK to memcpy */
1448 dst->Pixel = src->Pixel;
1449 }
1450 if (mask & GL_POINT_BIT) {
1451 /* OK to memcpy */
1452 dst->Point = src->Point;
1453 }
1454 if (mask & GL_POLYGON_BIT) {
1455 /* OK to memcpy */
1456 dst->Polygon = src->Polygon;
1457 }
1458 if (mask & GL_POLYGON_STIPPLE_BIT) {
1459 /* Use loop instead of MEMCPY due to problem with Portland Group's
1460 * C compiler. Reported by John Stone.
1461 */
1462 GLuint i;
1463 for (i = 0; i < 32; i++) {
1464 dst->PolygonStipple[i] = src->PolygonStipple[i];
1465 }
1466 }
1467 if (mask & GL_SCISSOR_BIT) {
1468 /* OK to memcpy */
1469 dst->Scissor = src->Scissor;
1470 }
1471 if (mask & GL_STENCIL_BUFFER_BIT) {
1472 /* OK to memcpy */
1473 dst->Stencil = src->Stencil;
1474 }
1475 if (mask & GL_TEXTURE_BIT) {
1476 /* Cannot memcpy because of pointers */
1477 _mesa_copy_texture_state(src, dst);
1478 }
1479 if (mask & GL_TRANSFORM_BIT) {
1480 /* OK to memcpy */
1481 dst->Transform = src->Transform;
1482 }
1483 if (mask & GL_VIEWPORT_BIT) {
1484 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1485 dst->Viewport.X = src->Viewport.X;
1486 dst->Viewport.Y = src->Viewport.Y;
1487 dst->Viewport.Width = src->Viewport.Width;
1488 dst->Viewport.Height = src->Viewport.Height;
1489 dst->Viewport.Near = src->Viewport.Near;
1490 dst->Viewport.Far = src->Viewport.Far;
1491 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
1492 }
1493
1494 /* XXX FIXME: Call callbacks?
1495 */
1496 dst->NewState = _NEW_ALL;
1497 }
1498 #endif
1499
1500
1501 /**
1502 * Check if the given context can render into the given framebuffer
1503 * by checking visual attributes.
1504 *
1505 * Most of these tests could go away because Mesa is now pretty flexible
1506 * in terms of mixing rendering contexts with framebuffers. As long
1507 * as RGB vs. CI mode agree, we're probably good.
1508 *
1509 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1510 */
1511 static GLboolean
1512 check_compatible(const GLcontext *ctx, const GLframebuffer *buffer)
1513 {
1514 const GLvisual *ctxvis = &ctx->Visual;
1515 const GLvisual *bufvis = &buffer->Visual;
1516
1517 if (ctxvis == bufvis)
1518 return GL_TRUE;
1519
1520 if (ctxvis->rgbMode != bufvis->rgbMode)
1521 return GL_FALSE;
1522 #if 0
1523 /* disabling this fixes the fgl_glxgears pbuffer demo */
1524 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1525 return GL_FALSE;
1526 #endif
1527 if (ctxvis->stereoMode && !bufvis->stereoMode)
1528 return GL_FALSE;
1529 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1530 return GL_FALSE;
1531 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1532 return GL_FALSE;
1533 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1534 return GL_FALSE;
1535 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1536 return GL_FALSE;
1537 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1538 return GL_FALSE;
1539 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1540 return GL_FALSE;
1541 #if 0
1542 /* disabled (see bug 11161) */
1543 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1544 return GL_FALSE;
1545 #endif
1546 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1547 return GL_FALSE;
1548
1549 return GL_TRUE;
1550 }
1551
1552
1553 /**
1554 * Do one-time initialization for the given framebuffer. Specifically,
1555 * ask the driver for the window's current size and update the framebuffer
1556 * object to match.
1557 * Really, the device driver should totally take care of this.
1558 */
1559 static void
1560 initialize_framebuffer_size(GLcontext *ctx, GLframebuffer *fb)
1561 {
1562 GLuint width, height;
1563 if (ctx->Driver.GetBufferSize) {
1564 ctx->Driver.GetBufferSize(fb, &width, &height);
1565 if (ctx->Driver.ResizeBuffers)
1566 ctx->Driver.ResizeBuffers(ctx, fb, width, height);
1567 fb->Initialized = GL_TRUE;
1568 }
1569 }
1570
1571
1572 /**
1573 * Bind the given context to the given drawBuffer and readBuffer and
1574 * make it the current context for the calling thread.
1575 * We'll render into the drawBuffer and read pixels from the
1576 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1577 *
1578 * We check that the context's and framebuffer's visuals are compatible
1579 * and return immediately if they're not.
1580 *
1581 * \param newCtx the new GL context. If NULL then there will be no current GL
1582 * context.
1583 * \param drawBuffer the drawing framebuffer
1584 * \param readBuffer the reading framebuffer
1585 */
1586 void
1587 _mesa_make_current( GLcontext *newCtx, GLframebuffer *drawBuffer,
1588 GLframebuffer *readBuffer )
1589 {
1590 if (MESA_VERBOSE & VERBOSE_API)
1591 _mesa_debug(newCtx, "_mesa_make_current()\n");
1592
1593 /* Check that the context's and framebuffer's visuals are compatible.
1594 */
1595 if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
1596 if (!check_compatible(newCtx, drawBuffer)) {
1597 _mesa_warning(newCtx,
1598 "MakeCurrent: incompatible visuals for context and drawbuffer");
1599 return;
1600 }
1601 }
1602 if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
1603 if (!check_compatible(newCtx, readBuffer)) {
1604 _mesa_warning(newCtx,
1605 "MakeCurrent: incompatible visuals for context and readbuffer");
1606 return;
1607 }
1608 }
1609
1610 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1611 _glapi_set_context((void *) newCtx);
1612 ASSERT(_mesa_get_current_context() == newCtx);
1613
1614 if (!newCtx) {
1615 _glapi_set_dispatch(NULL); /* none current */
1616 }
1617 else {
1618 _glapi_set_dispatch(newCtx->CurrentDispatch);
1619
1620 if (drawBuffer && readBuffer) {
1621 /* TODO: check if newCtx and buffer's visual match??? */
1622
1623 ASSERT(drawBuffer->Name == 0);
1624 ASSERT(readBuffer->Name == 0);
1625 _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
1626 _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
1627
1628 /*
1629 * Only set the context's Draw/ReadBuffer fields if they're NULL
1630 * or not bound to a user-created FBO.
1631 */
1632 if (!newCtx->DrawBuffer || newCtx->DrawBuffer->Name == 0) {
1633 /* KW: merge conflict here, revisit.
1634 */
1635 /* fix up the fb fields - these will end up wrong otherwise
1636 * if the DRIdrawable changes, and everything relies on them.
1637 * This is a bit messy (same as needed in _mesa_BindFramebufferEXT)
1638 */
1639 unsigned int i;
1640 GLenum buffers[MAX_DRAW_BUFFERS];
1641
1642 _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
1643
1644 for(i = 0; i < newCtx->Const.MaxDrawBuffers; i++) {
1645 buffers[i] = newCtx->Color.DrawBuffer[i];
1646 }
1647
1648 _mesa_drawbuffers(newCtx, newCtx->Const.MaxDrawBuffers, buffers, NULL);
1649 }
1650 if (!newCtx->ReadBuffer || newCtx->ReadBuffer->Name == 0) {
1651 _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
1652 }
1653
1654 /* XXX only set this flag if we're really changing the draw/read
1655 * framebuffer bindings.
1656 */
1657 newCtx->NewState |= _NEW_BUFFERS;
1658
1659 #if 1
1660 /* We want to get rid of these lines: */
1661
1662 #if _HAVE_FULL_GL
1663 if (!drawBuffer->Initialized) {
1664 initialize_framebuffer_size(newCtx, drawBuffer);
1665 }
1666 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
1667 initialize_framebuffer_size(newCtx, readBuffer);
1668 }
1669
1670 _mesa_resizebuffers(newCtx);
1671 #endif
1672
1673 #else
1674 /* We want the drawBuffer and readBuffer to be initialized by
1675 * the driver.
1676 * This generally means the Width and Height match the actual
1677 * window size and the renderbuffers (both hardware and software
1678 * based) are allocated to match. The later can generally be
1679 * done with a call to _mesa_resize_framebuffer().
1680 *
1681 * It's theoretically possible for a buffer to have zero width
1682 * or height, but for now, assert check that the driver did what's
1683 * expected of it.
1684 */
1685 ASSERT(drawBuffer->Width > 0);
1686 ASSERT(drawBuffer->Height > 0);
1687 #endif
1688
1689 if (newCtx->FirstTimeCurrent) {
1690 /* set initial viewport and scissor size now */
1691 _mesa_set_viewport(newCtx, 0, 0,
1692 drawBuffer->Width, drawBuffer->Height);
1693 _mesa_set_scissor(newCtx, 0, 0,
1694 drawBuffer->Width, drawBuffer->Height );
1695 check_context_limits(newCtx);
1696 }
1697 }
1698
1699 /* We can use this to help debug user's problems. Tell them to set
1700 * the MESA_INFO env variable before running their app. Then the
1701 * first time each context is made current we'll print some useful
1702 * information.
1703 */
1704 if (newCtx->FirstTimeCurrent) {
1705 if (_mesa_getenv("MESA_INFO")) {
1706 _mesa_print_info();
1707 }
1708 newCtx->FirstTimeCurrent = GL_FALSE;
1709 }
1710 }
1711 }
1712
1713
1714 /**
1715 * Make context 'ctx' share the display lists, textures and programs
1716 * that are associated with 'ctxToShare'.
1717 * Any display lists, textures or programs associated with 'ctx' will
1718 * be deleted if nobody else is sharing them.
1719 */
1720 GLboolean
1721 _mesa_share_state(GLcontext *ctx, GLcontext *ctxToShare)
1722 {
1723 if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1724 struct gl_shared_state *oldSharedState = ctx->Shared;
1725
1726 ctx->Shared = ctxToShare->Shared;
1727 ctx->Shared->RefCount++;
1728
1729 update_default_objects(ctx);
1730
1731 oldSharedState->RefCount--;
1732 if (oldSharedState->RefCount == 0) {
1733 free_shared_state(ctx, oldSharedState);
1734 }
1735
1736 return GL_TRUE;
1737 }
1738 else {
1739 return GL_FALSE;
1740 }
1741 }
1742
1743
1744
1745 /**
1746 * \return pointer to the current GL context for this thread.
1747 *
1748 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1749 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1750 * context.h.
1751 */
1752 GLcontext *
1753 _mesa_get_current_context( void )
1754 {
1755 return (GLcontext *) _glapi_get_context();
1756 }
1757
1758
1759 /**
1760 * Get context's current API dispatch table.
1761 *
1762 * It'll either be the immediate-mode execute dispatcher or the display list
1763 * compile dispatcher.
1764 *
1765 * \param ctx GL context.
1766 *
1767 * \return pointer to dispatch_table.
1768 *
1769 * Simply returns __GLcontextRec::CurrentDispatch.
1770 */
1771 struct _glapi_table *
1772 _mesa_get_dispatch(GLcontext *ctx)
1773 {
1774 return ctx->CurrentDispatch;
1775 }
1776
1777 /*@}*/
1778
1779
1780 /**********************************************************************/
1781 /** \name Miscellaneous functions */
1782 /**********************************************************************/
1783 /*@{*/
1784
1785 /**
1786 * Record an error.
1787 *
1788 * \param ctx GL context.
1789 * \param error error code.
1790 *
1791 * Records the given error code and call the driver's dd_function_table::Error
1792 * function if defined.
1793 *
1794 * \sa
1795 * This is called via _mesa_error().
1796 */
1797 void
1798 _mesa_record_error(GLcontext *ctx, GLenum error)
1799 {
1800 if (!ctx)
1801 return;
1802
1803 if (ctx->ErrorValue == GL_NO_ERROR) {
1804 ctx->ErrorValue = error;
1805 }
1806
1807 /* Call device driver's error handler, if any. This is used on the Mac. */
1808 if (ctx->Driver.Error) {
1809 ctx->Driver.Error(ctx);
1810 }
1811 }
1812
1813
1814 /**
1815 * Execute glFinish().
1816 *
1817 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1818 * dd_function_table::Finish driver callback, if not NULL.
1819 */
1820 void GLAPIENTRY
1821 _mesa_Finish(void)
1822 {
1823 GET_CURRENT_CONTEXT(ctx);
1824 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1825 if (ctx->Driver.Finish) {
1826 ctx->Driver.Finish(ctx);
1827 }
1828 }
1829
1830
1831 /**
1832 * Execute glFlush().
1833 *
1834 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1835 * dd_function_table::Flush driver callback, if not NULL.
1836 */
1837 void GLAPIENTRY
1838 _mesa_Flush(void)
1839 {
1840 GET_CURRENT_CONTEXT(ctx);
1841 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1842 if (ctx->Driver.Flush) {
1843 ctx->Driver.Flush(ctx);
1844 }
1845 }
1846
1847
1848 /*@}*/