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