context.c revision 4a49301e
17117f1b4Smrg/* 27117f1b4Smrg * Mesa 3-D graphics library 3c1f859d4Smrg * Version: 7.3 47117f1b4Smrg * 57117f1b4Smrg * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 6c1f859d4Smrg * Copyright (C) 2008 VMware, Inc. All Rights Reserved. 77117f1b4Smrg * 87117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a 97117f1b4Smrg * copy of this software and associated documentation files (the "Software"), 107117f1b4Smrg * to deal in the Software without restriction, including without limitation 117117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 127117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the 137117f1b4Smrg * Software is furnished to do so, subject to the following conditions: 147117f1b4Smrg * 157117f1b4Smrg * The above copyright notice and this permission notice shall be included 167117f1b4Smrg * in all copies or substantial portions of the Software. 177117f1b4Smrg * 187117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 197117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 207117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 217117f1b4Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 227117f1b4Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 237117f1b4Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 247117f1b4Smrg */ 257117f1b4Smrg 26c1f859d4Smrg/** 27c1f859d4Smrg * \file context.c 28c1f859d4Smrg * Mesa context/visual/framebuffer management functions. 29c1f859d4Smrg * \author Brian Paul 30c1f859d4Smrg */ 317117f1b4Smrg 327117f1b4Smrg/** 337117f1b4Smrg * \mainpage Mesa Main Module 347117f1b4Smrg * 357117f1b4Smrg * \section MainIntroduction Introduction 367117f1b4Smrg * 377117f1b4Smrg * The Mesa Main module consists of all the files in the main/ directory. 387117f1b4Smrg * Among the features of this module are: 397117f1b4Smrg * <UL> 407117f1b4Smrg * <LI> Structures to represent most GL state </LI> 417117f1b4Smrg * <LI> State set/get functions </LI> 427117f1b4Smrg * <LI> Display lists </LI> 437117f1b4Smrg * <LI> Texture unit, object and image handling </LI> 447117f1b4Smrg * <LI> Matrix and attribute stacks </LI> 457117f1b4Smrg * </UL> 467117f1b4Smrg * 477117f1b4Smrg * Other modules are responsible for API dispatch, vertex transformation, 487117f1b4Smrg * point/line/triangle setup, rasterization, vertex array caching, 497117f1b4Smrg * vertex/fragment programs/shaders, etc. 507117f1b4Smrg * 517117f1b4Smrg * 527117f1b4Smrg * \section AboutDoxygen About Doxygen 537117f1b4Smrg * 547117f1b4Smrg * If you're viewing this information as Doxygen-generated HTML you'll 557117f1b4Smrg * see the documentation index at the top of this page. 567117f1b4Smrg * 577117f1b4Smrg * The first line lists the Mesa source code modules. 587117f1b4Smrg * The second line lists the indexes available for viewing the documentation 597117f1b4Smrg * for each module. 607117f1b4Smrg * 617117f1b4Smrg * Selecting the <b>Main page</b> link will display a summary of the module 627117f1b4Smrg * (this page). 637117f1b4Smrg * 647117f1b4Smrg * Selecting <b>Data Structures</b> will list all C structures. 657117f1b4Smrg * 667117f1b4Smrg * Selecting the <b>File List</b> link will list all the source files in 677117f1b4Smrg * the module. 687117f1b4Smrg * Selecting a filename will show a list of all functions defined in that file. 697117f1b4Smrg * 707117f1b4Smrg * Selecting the <b>Data Fields</b> link will display a list of all 717117f1b4Smrg * documented structure members. 727117f1b4Smrg * 737117f1b4Smrg * Selecting the <b>Globals</b> link will display a list 747117f1b4Smrg * of all functions, structures, global variables and macros in the module. 757117f1b4Smrg * 767117f1b4Smrg */ 777117f1b4Smrg 787117f1b4Smrg 797117f1b4Smrg#include "glheader.h" 804a49301eSmrg#include "mfeatures.h" 817117f1b4Smrg#include "imports.h" 827117f1b4Smrg#include "accum.h" 83c1f859d4Smrg#include "api_exec.h" 847117f1b4Smrg#include "arrayobj.h" 857117f1b4Smrg#include "attrib.h" 867117f1b4Smrg#include "blend.h" 877117f1b4Smrg#include "buffers.h" 887117f1b4Smrg#include "bufferobj.h" 897117f1b4Smrg#include "colortab.h" 907117f1b4Smrg#include "context.h" 914a49301eSmrg#include "cpuinfo.h" 927117f1b4Smrg#include "debug.h" 937117f1b4Smrg#include "depth.h" 947117f1b4Smrg#include "dlist.h" 957117f1b4Smrg#include "eval.h" 967117f1b4Smrg#include "enums.h" 977117f1b4Smrg#include "extensions.h" 987117f1b4Smrg#include "fbobject.h" 997117f1b4Smrg#include "feedback.h" 1007117f1b4Smrg#include "fog.h" 1017117f1b4Smrg#include "framebuffer.h" 1027117f1b4Smrg#include "get.h" 1037117f1b4Smrg#include "histogram.h" 1047117f1b4Smrg#include "hint.h" 1057117f1b4Smrg#include "hash.h" 1067117f1b4Smrg#include "light.h" 1077117f1b4Smrg#include "lines.h" 1087117f1b4Smrg#include "macros.h" 1097117f1b4Smrg#include "matrix.h" 110c1f859d4Smrg#include "multisample.h" 1117117f1b4Smrg#include "pixel.h" 112c1f859d4Smrg#include "pixelstore.h" 1137117f1b4Smrg#include "points.h" 1147117f1b4Smrg#include "polygon.h" 1157117f1b4Smrg#include "queryobj.h" 1164a49301eSmrg#if FEATURE_ARB_sync 1174a49301eSmrg#include "syncobj.h" 118c1f859d4Smrg#endif 1197117f1b4Smrg#include "rastpos.h" 1204a49301eSmrg#include "remap.h" 121c1f859d4Smrg#include "scissor.h" 1224a49301eSmrg#include "shared.h" 1237117f1b4Smrg#include "simple_list.h" 1247117f1b4Smrg#include "state.h" 1257117f1b4Smrg#include "stencil.h" 1264a49301eSmrg#include "texcompress_s3tc.h" 1277117f1b4Smrg#include "teximage.h" 1287117f1b4Smrg#include "texobj.h" 1297117f1b4Smrg#include "texstate.h" 1307117f1b4Smrg#include "mtypes.h" 1317117f1b4Smrg#include "varray.h" 1327117f1b4Smrg#include "version.h" 1334a49301eSmrg#include "viewport.h" 1347117f1b4Smrg#include "vtxfmt.h" 135c1f859d4Smrg#include "glapi/glthread.h" 136c1f859d4Smrg#include "glapi/glapitable.h" 137c1f859d4Smrg#include "shader/program.h" 1384a49301eSmrg#include "shader/prog_print.h" 139c1f859d4Smrg#include "shader/shader_api.h" 140c1f859d4Smrg#if FEATURE_ATI_fragment_shader 141c1f859d4Smrg#include "shader/atifragshader.h" 142c1f859d4Smrg#endif 1437117f1b4Smrg#if _HAVE_FULL_GL 1447117f1b4Smrg#include "math/m_matrix.h" 1457117f1b4Smrg#endif 1467117f1b4Smrg 1477117f1b4Smrg#ifdef USE_SPARC_ASM 1487117f1b4Smrg#include "sparc/sparc.h" 1497117f1b4Smrg#endif 1507117f1b4Smrg 1517117f1b4Smrg#ifndef MESA_VERBOSE 1527117f1b4Smrgint MESA_VERBOSE = 0; 1537117f1b4Smrg#endif 1547117f1b4Smrg 1557117f1b4Smrg#ifndef MESA_DEBUG_FLAGS 1567117f1b4Smrgint MESA_DEBUG_FLAGS = 0; 1577117f1b4Smrg#endif 1587117f1b4Smrg 1597117f1b4Smrg 1607117f1b4Smrg/* ubyte -> float conversion */ 1617117f1b4SmrgGLfloat _mesa_ubyte_to_float_color_tab[256]; 1627117f1b4Smrg 1637117f1b4Smrg 1647117f1b4Smrg 1657117f1b4Smrg/** 1667117f1b4Smrg * Swap buffers notification callback. 1677117f1b4Smrg * 1684a49301eSmrg * \param ctx GL context. 1697117f1b4Smrg * 1707117f1b4Smrg * Called by window system just before swapping buffers. 1717117f1b4Smrg * We have to finish any pending rendering. 1727117f1b4Smrg */ 1737117f1b4Smrgvoid 1744a49301eSmrg_mesa_notifySwapBuffers(__GLcontext *ctx) 1757117f1b4Smrg{ 1764a49301eSmrg if (MESA_VERBOSE & VERBOSE_SWAPBUFFERS) 1774a49301eSmrg _mesa_debug(ctx, "SwapBuffers\n"); 1784a49301eSmrg FLUSH_CURRENT( ctx, 0 ); 1794a49301eSmrg if (ctx->Driver.Flush) { 1804a49301eSmrg ctx->Driver.Flush(ctx); 1814a49301eSmrg } 1827117f1b4Smrg} 1837117f1b4Smrg 1847117f1b4Smrg 1857117f1b4Smrg/**********************************************************************/ 1867117f1b4Smrg/** \name GL Visual allocation/destruction */ 1877117f1b4Smrg/**********************************************************************/ 1887117f1b4Smrg/*@{*/ 1897117f1b4Smrg 1907117f1b4Smrg/** 1917117f1b4Smrg * Allocates a GLvisual structure and initializes it via 1927117f1b4Smrg * _mesa_initialize_visual(). 1937117f1b4Smrg * 1947117f1b4Smrg * \param rgbFlag GL_TRUE for RGB(A) mode, GL_FALSE for Color Index mode. 1957117f1b4Smrg * \param dbFlag double buffering 1967117f1b4Smrg * \param stereoFlag stereo buffer 1977117f1b4Smrg * \param depthBits requested bits per depth buffer value. Any value in [0, 32] 1987117f1b4Smrg * is acceptable but the actual depth type will be GLushort or GLuint as 1997117f1b4Smrg * needed. 2007117f1b4Smrg * \param stencilBits requested minimum bits per stencil buffer value 2017117f1b4Smrg * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number of bits per color component in accum buffer. 2027117f1b4Smrg * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE 2037117f1b4Smrg * \param redBits number of bits per color component in frame buffer for RGB(A) 2047117f1b4Smrg * mode. We always use 8 in core Mesa though. 2057117f1b4Smrg * \param greenBits same as above. 2067117f1b4Smrg * \param blueBits same as above. 2077117f1b4Smrg * \param alphaBits same as above. 2087117f1b4Smrg * \param numSamples not really used. 2097117f1b4Smrg * 2107117f1b4Smrg * \return pointer to new GLvisual or NULL if requested parameters can't be 2117117f1b4Smrg * met. 2127117f1b4Smrg * 2137117f1b4Smrg * \note Need to add params for level and numAuxBuffers (at least) 2147117f1b4Smrg */ 2157117f1b4SmrgGLvisual * 2167117f1b4Smrg_mesa_create_visual( GLboolean rgbFlag, 2177117f1b4Smrg GLboolean dbFlag, 2187117f1b4Smrg GLboolean stereoFlag, 2197117f1b4Smrg GLint redBits, 2207117f1b4Smrg GLint greenBits, 2217117f1b4Smrg GLint blueBits, 2227117f1b4Smrg GLint alphaBits, 2237117f1b4Smrg GLint indexBits, 2247117f1b4Smrg GLint depthBits, 2257117f1b4Smrg GLint stencilBits, 2267117f1b4Smrg GLint accumRedBits, 2277117f1b4Smrg GLint accumGreenBits, 2287117f1b4Smrg GLint accumBlueBits, 2297117f1b4Smrg GLint accumAlphaBits, 2307117f1b4Smrg GLint numSamples ) 2317117f1b4Smrg{ 2327117f1b4Smrg GLvisual *vis = (GLvisual *) _mesa_calloc(sizeof(GLvisual)); 2337117f1b4Smrg if (vis) { 2347117f1b4Smrg if (!_mesa_initialize_visual(vis, rgbFlag, dbFlag, stereoFlag, 2357117f1b4Smrg redBits, greenBits, blueBits, alphaBits, 2367117f1b4Smrg indexBits, depthBits, stencilBits, 2377117f1b4Smrg accumRedBits, accumGreenBits, 2387117f1b4Smrg accumBlueBits, accumAlphaBits, 2397117f1b4Smrg numSamples)) { 2407117f1b4Smrg _mesa_free(vis); 2417117f1b4Smrg return NULL; 2427117f1b4Smrg } 2437117f1b4Smrg } 2447117f1b4Smrg return vis; 2457117f1b4Smrg} 2467117f1b4Smrg 2477117f1b4Smrg/** 2487117f1b4Smrg * Makes some sanity checks and fills in the fields of the 2497117f1b4Smrg * GLvisual object with the given parameters. If the caller needs 2507117f1b4Smrg * to set additional fields, he should just probably init the whole GLvisual 2517117f1b4Smrg * object himself. 2527117f1b4Smrg * \return GL_TRUE on success, or GL_FALSE on failure. 2537117f1b4Smrg * 2547117f1b4Smrg * \sa _mesa_create_visual() above for the parameter description. 2557117f1b4Smrg */ 2567117f1b4SmrgGLboolean 2577117f1b4Smrg_mesa_initialize_visual( GLvisual *vis, 2587117f1b4Smrg GLboolean rgbFlag, 2597117f1b4Smrg GLboolean dbFlag, 2607117f1b4Smrg GLboolean stereoFlag, 2617117f1b4Smrg GLint redBits, 2627117f1b4Smrg GLint greenBits, 2637117f1b4Smrg GLint blueBits, 2647117f1b4Smrg GLint alphaBits, 2657117f1b4Smrg GLint indexBits, 2667117f1b4Smrg GLint depthBits, 2677117f1b4Smrg GLint stencilBits, 2687117f1b4Smrg GLint accumRedBits, 2697117f1b4Smrg GLint accumGreenBits, 2707117f1b4Smrg GLint accumBlueBits, 2717117f1b4Smrg GLint accumAlphaBits, 2727117f1b4Smrg GLint numSamples ) 2737117f1b4Smrg{ 2747117f1b4Smrg assert(vis); 2757117f1b4Smrg 2767117f1b4Smrg if (depthBits < 0 || depthBits > 32) { 2777117f1b4Smrg return GL_FALSE; 2787117f1b4Smrg } 2797117f1b4Smrg if (stencilBits < 0 || stencilBits > STENCIL_BITS) { 2807117f1b4Smrg return GL_FALSE; 2817117f1b4Smrg } 2827117f1b4Smrg assert(accumRedBits >= 0); 2837117f1b4Smrg assert(accumGreenBits >= 0); 2847117f1b4Smrg assert(accumBlueBits >= 0); 2857117f1b4Smrg assert(accumAlphaBits >= 0); 2867117f1b4Smrg 2877117f1b4Smrg vis->rgbMode = rgbFlag; 2887117f1b4Smrg vis->doubleBufferMode = dbFlag; 2897117f1b4Smrg vis->stereoMode = stereoFlag; 2907117f1b4Smrg 2917117f1b4Smrg vis->redBits = redBits; 2927117f1b4Smrg vis->greenBits = greenBits; 2937117f1b4Smrg vis->blueBits = blueBits; 2947117f1b4Smrg vis->alphaBits = alphaBits; 2957117f1b4Smrg vis->rgbBits = redBits + greenBits + blueBits; 2967117f1b4Smrg 2977117f1b4Smrg vis->indexBits = indexBits; 2987117f1b4Smrg vis->depthBits = depthBits; 2997117f1b4Smrg vis->stencilBits = stencilBits; 3007117f1b4Smrg 3017117f1b4Smrg vis->accumRedBits = accumRedBits; 3027117f1b4Smrg vis->accumGreenBits = accumGreenBits; 3037117f1b4Smrg vis->accumBlueBits = accumBlueBits; 3047117f1b4Smrg vis->accumAlphaBits = accumAlphaBits; 3057117f1b4Smrg 3067117f1b4Smrg vis->haveAccumBuffer = accumRedBits > 0; 3077117f1b4Smrg vis->haveDepthBuffer = depthBits > 0; 3087117f1b4Smrg vis->haveStencilBuffer = stencilBits > 0; 3097117f1b4Smrg 3107117f1b4Smrg vis->numAuxBuffers = 0; 3117117f1b4Smrg vis->level = 0; 3127117f1b4Smrg vis->pixmapMode = 0; 3137117f1b4Smrg vis->sampleBuffers = numSamples > 0 ? 1 : 0; 3147117f1b4Smrg vis->samples = numSamples; 3157117f1b4Smrg 3167117f1b4Smrg return GL_TRUE; 3177117f1b4Smrg} 3187117f1b4Smrg 3197117f1b4Smrg 3207117f1b4Smrg/** 3217117f1b4Smrg * Destroy a visual and free its memory. 3227117f1b4Smrg * 3237117f1b4Smrg * \param vis visual. 3247117f1b4Smrg * 3257117f1b4Smrg * Frees the visual structure. 3267117f1b4Smrg */ 3277117f1b4Smrgvoid 3287117f1b4Smrg_mesa_destroy_visual( GLvisual *vis ) 3297117f1b4Smrg{ 3307117f1b4Smrg _mesa_free(vis); 3317117f1b4Smrg} 3327117f1b4Smrg 3337117f1b4Smrg/*@}*/ 3347117f1b4Smrg 3357117f1b4Smrg 3367117f1b4Smrg/**********************************************************************/ 3377117f1b4Smrg/** \name Context allocation, initialization, destroying 3387117f1b4Smrg * 3397117f1b4Smrg * The purpose of the most initialization functions here is to provide the 3407117f1b4Smrg * default state values according to the OpenGL specification. 3417117f1b4Smrg */ 3427117f1b4Smrg/**********************************************************************/ 3437117f1b4Smrg/*@{*/ 3447117f1b4Smrg 3454a49301eSmrg 3464a49301eSmrg/** 3474a49301eSmrg * This is lame. gdb only seems to recognize enum types that are 3484a49301eSmrg * actually used somewhere. We want to be able to print/use enum 3494a49301eSmrg * values such as TEXTURE_2D_INDEX in gdb. But we don't actually use 3504a49301eSmrg * the gl_texture_index type anywhere. Thus, this lame function. 3514a49301eSmrg */ 3524a49301eSmrgstatic void 3534a49301eSmrgdummy_enum_func(void) 3544a49301eSmrg{ 3554a49301eSmrg gl_buffer_index bi; 3564a49301eSmrg gl_colortable_index ci; 3574a49301eSmrg gl_face_index fi; 3584a49301eSmrg gl_frag_attrib fa; 3594a49301eSmrg gl_frag_result fr; 3604a49301eSmrg gl_texture_index ti; 3614a49301eSmrg gl_vert_attrib va; 3624a49301eSmrg gl_vert_result vr; 3634a49301eSmrg 3644a49301eSmrg (void) bi; 3654a49301eSmrg (void) ci; 3664a49301eSmrg (void) fi; 3674a49301eSmrg (void) fa; 3684a49301eSmrg (void) fr; 3694a49301eSmrg (void) ti; 3704a49301eSmrg (void) va; 3714a49301eSmrg (void) vr; 3724a49301eSmrg} 3734a49301eSmrg 3744a49301eSmrg 3757117f1b4Smrg/** 3767117f1b4Smrg * One-time initialization mutex lock. 3777117f1b4Smrg * 3787117f1b4Smrg * \sa Used by one_time_init(). 3797117f1b4Smrg */ 3807117f1b4Smrg_glthread_DECLARE_STATIC_MUTEX(OneTimeLock); 3817117f1b4Smrg 3827117f1b4Smrg/** 3837117f1b4Smrg * Calls all the various one-time-init functions in Mesa. 3847117f1b4Smrg * 3857117f1b4Smrg * While holding a global mutex lock, calls several initialization functions, 3867117f1b4Smrg * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is 3877117f1b4Smrg * defined. 3887117f1b4Smrg * 3897117f1b4Smrg * \sa _math_init(). 3907117f1b4Smrg */ 3917117f1b4Smrgstatic void 3927117f1b4Smrgone_time_init( GLcontext *ctx ) 3937117f1b4Smrg{ 3947117f1b4Smrg static GLboolean alreadyCalled = GL_FALSE; 3957117f1b4Smrg (void) ctx; 3967117f1b4Smrg _glthread_LOCK_MUTEX(OneTimeLock); 3977117f1b4Smrg if (!alreadyCalled) { 3987117f1b4Smrg GLuint i; 3997117f1b4Smrg 4007117f1b4Smrg /* do some implementation tests */ 4017117f1b4Smrg assert( sizeof(GLbyte) == 1 ); 4027117f1b4Smrg assert( sizeof(GLubyte) == 1 ); 4037117f1b4Smrg assert( sizeof(GLshort) == 2 ); 4047117f1b4Smrg assert( sizeof(GLushort) == 2 ); 4057117f1b4Smrg assert( sizeof(GLint) == 4 ); 4067117f1b4Smrg assert( sizeof(GLuint) == 4 ); 4077117f1b4Smrg 4084a49301eSmrg _mesa_get_cpu_features(); 4097117f1b4Smrg 4104a49301eSmrg _mesa_init_remap_table(); 4114a49301eSmrg 4124a49301eSmrg _mesa_init_sqrt_table(); 4137117f1b4Smrg 4147117f1b4Smrg for (i = 0; i < 256; i++) { 4157117f1b4Smrg _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F; 4167117f1b4Smrg } 4177117f1b4Smrg 4187117f1b4Smrg if (_mesa_getenv("MESA_DEBUG")) { 4197117f1b4Smrg _glapi_noop_enable_warnings(GL_TRUE); 4207117f1b4Smrg _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning ); 4217117f1b4Smrg } 4227117f1b4Smrg else { 4237117f1b4Smrg _glapi_noop_enable_warnings(GL_FALSE); 4247117f1b4Smrg } 4257117f1b4Smrg 4267117f1b4Smrg#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__) 4277117f1b4Smrg _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n", 4287117f1b4Smrg MESA_VERSION_STRING, __DATE__, __TIME__); 4297117f1b4Smrg#endif 4307117f1b4Smrg 4317117f1b4Smrg alreadyCalled = GL_TRUE; 4327117f1b4Smrg } 4337117f1b4Smrg _glthread_UNLOCK_MUTEX(OneTimeLock); 4347117f1b4Smrg 4354a49301eSmrg dummy_enum_func(); 4367117f1b4Smrg} 4377117f1b4Smrg 4387117f1b4Smrg 4397117f1b4Smrg/** 4407117f1b4Smrg * Initialize fields of gl_current_attrib (aka ctx->Current.*) 4417117f1b4Smrg */ 4427117f1b4Smrgstatic void 4437117f1b4Smrg_mesa_init_current(GLcontext *ctx) 4447117f1b4Smrg{ 4457117f1b4Smrg GLuint i; 4467117f1b4Smrg 4477117f1b4Smrg /* Init all to (0,0,0,1) */ 4484a49301eSmrg for (i = 0; i < Elements(ctx->Current.Attrib); i++) { 4497117f1b4Smrg ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 ); 4507117f1b4Smrg } 4517117f1b4Smrg 4527117f1b4Smrg /* redo special cases: */ 4537117f1b4Smrg ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 0.0 ); 4547117f1b4Smrg ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 ); 4557117f1b4Smrg ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 ); 4567117f1b4Smrg ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 ); 4577117f1b4Smrg ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 ); 4587117f1b4Smrg ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 ); 4597117f1b4Smrg} 4607117f1b4Smrg 4617117f1b4Smrg 4627117f1b4Smrg/** 4634a49301eSmrg * Init vertex/fragment program limits. 4644a49301eSmrg * Important: drivers should override these with actual limits. 4657117f1b4Smrg */ 4667117f1b4Smrgstatic void 4674a49301eSmrginit_program_limits(GLenum type, struct gl_program_constants *prog) 4687117f1b4Smrg{ 4694a49301eSmrg prog->MaxInstructions = MAX_PROGRAM_INSTRUCTIONS; 4704a49301eSmrg prog->MaxAluInstructions = MAX_PROGRAM_INSTRUCTIONS; 4714a49301eSmrg prog->MaxTexInstructions = MAX_PROGRAM_INSTRUCTIONS; 4724a49301eSmrg prog->MaxTexIndirections = MAX_PROGRAM_INSTRUCTIONS; 4734a49301eSmrg prog->MaxTemps = MAX_PROGRAM_TEMPS; 4744a49301eSmrg prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS; 4754a49301eSmrg prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS; 4764a49301eSmrg prog->MaxUniformComponents = 4 * MAX_UNIFORMS; 4774a49301eSmrg 4784a49301eSmrg if (type == GL_VERTEX_PROGRAM_ARB) { 4794a49301eSmrg prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS; 4804a49301eSmrg prog->MaxAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS; 4814a49301eSmrg prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS; 4824a49301eSmrg } 4834a49301eSmrg else { 4844a49301eSmrg prog->MaxParameters = MAX_NV_FRAGMENT_PROGRAM_PARAMS; 4854a49301eSmrg prog->MaxAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS; 4864a49301eSmrg prog->MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS; 4874a49301eSmrg } 4884a49301eSmrg 4894a49301eSmrg /* Set the native limits to zero. This implies that there is no native 4904a49301eSmrg * support for shaders. Let the drivers fill in the actual values. 4914a49301eSmrg */ 4924a49301eSmrg prog->MaxNativeInstructions = 0; 4934a49301eSmrg prog->MaxNativeAluInstructions = 0; 4944a49301eSmrg prog->MaxNativeTexInstructions = 0; 4954a49301eSmrg prog->MaxNativeTexIndirections = 0; 4964a49301eSmrg prog->MaxNativeAttribs = 0; 4974a49301eSmrg prog->MaxNativeTemps = 0; 4984a49301eSmrg prog->MaxNativeAddressRegs = 0; 4994a49301eSmrg prog->MaxNativeParameters = 0; 5007117f1b4Smrg} 5017117f1b4Smrg 5027117f1b4Smrg 5037117f1b4Smrg/** 5047117f1b4Smrg * Initialize fields of gl_constants (aka ctx->Const.*). 5057117f1b4Smrg * Use defaults from config.h. The device drivers will often override 5067117f1b4Smrg * some of these values (such as number of texture units). 5077117f1b4Smrg */ 5087117f1b4Smrgstatic void 5097117f1b4Smrg_mesa_init_constants(GLcontext *ctx) 5107117f1b4Smrg{ 5117117f1b4Smrg assert(ctx); 5127117f1b4Smrg 5137117f1b4Smrg assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS); 5147117f1b4Smrg assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS); 5157117f1b4Smrg 5164a49301eSmrg /* Max texture size should be <= max viewport size (render to texture) */ 5174a49301eSmrg assert((1 << (MAX_TEXTURE_LEVELS - 1)) <= MAX_WIDTH); 5184a49301eSmrg 5197117f1b4Smrg /* Constants, may be overriden (usually only reduced) by device drivers */ 5207117f1b4Smrg ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS; 5217117f1b4Smrg ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS; 5227117f1b4Smrg ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS; 5237117f1b4Smrg ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE; 524c1f859d4Smrg ctx->Const.MaxArrayTextureLayers = MAX_ARRAY_TEXTURE_LAYERS; 5257117f1b4Smrg ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS; 5267117f1b4Smrg ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS; 5277117f1b4Smrg ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureCoordUnits, 5287117f1b4Smrg ctx->Const.MaxTextureImageUnits); 5297117f1b4Smrg ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY; 5307117f1b4Smrg ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS; 5317117f1b4Smrg ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE; 5327117f1b4Smrg ctx->Const.SubPixelBits = SUB_PIXEL_BITS; 5337117f1b4Smrg ctx->Const.MinPointSize = MIN_POINT_SIZE; 5347117f1b4Smrg ctx->Const.MaxPointSize = MAX_POINT_SIZE; 5357117f1b4Smrg ctx->Const.MinPointSizeAA = MIN_POINT_SIZE; 5367117f1b4Smrg ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE; 5377117f1b4Smrg ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY; 5387117f1b4Smrg ctx->Const.MinLineWidth = MIN_LINE_WIDTH; 5397117f1b4Smrg ctx->Const.MaxLineWidth = MAX_LINE_WIDTH; 5407117f1b4Smrg ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH; 5417117f1b4Smrg ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH; 5427117f1b4Smrg ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY; 5437117f1b4Smrg ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE; 5447117f1b4Smrg ctx->Const.MaxConvolutionWidth = MAX_CONVOLUTION_WIDTH; 5457117f1b4Smrg ctx->Const.MaxConvolutionHeight = MAX_CONVOLUTION_HEIGHT; 5467117f1b4Smrg ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES; 5477117f1b4Smrg ctx->Const.MaxLights = MAX_LIGHTS; 5487117f1b4Smrg ctx->Const.MaxShininess = 128.0; 5497117f1b4Smrg ctx->Const.MaxSpotExponent = 128.0; 5507117f1b4Smrg ctx->Const.MaxViewportWidth = MAX_WIDTH; 5517117f1b4Smrg ctx->Const.MaxViewportHeight = MAX_HEIGHT; 5527117f1b4Smrg#if FEATURE_ARB_vertex_program 5534a49301eSmrg init_program_limits(GL_VERTEX_PROGRAM_ARB, &ctx->Const.VertexProgram); 5547117f1b4Smrg#endif 5557117f1b4Smrg#if FEATURE_ARB_fragment_program 5564a49301eSmrg init_program_limits(GL_FRAGMENT_PROGRAM_ARB, &ctx->Const.FragmentProgram); 5577117f1b4Smrg#endif 5587117f1b4Smrg ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES; 5597117f1b4Smrg ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH; 5607117f1b4Smrg 5617117f1b4Smrg /* CheckArrayBounds is overriden by drivers/x11 for X server */ 5627117f1b4Smrg ctx->Const.CheckArrayBounds = GL_FALSE; 5637117f1b4Smrg 5647117f1b4Smrg /* GL_ARB_draw_buffers */ 5657117f1b4Smrg ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS; 5667117f1b4Smrg 5677117f1b4Smrg#if FEATURE_EXT_framebuffer_object 5687117f1b4Smrg ctx->Const.MaxColorAttachments = MAX_COLOR_ATTACHMENTS; 5697117f1b4Smrg ctx->Const.MaxRenderbufferSize = MAX_WIDTH; 5707117f1b4Smrg#endif 5717117f1b4Smrg 5727117f1b4Smrg#if FEATURE_ARB_vertex_shader 5737117f1b4Smrg ctx->Const.MaxVertexTextureImageUnits = MAX_VERTEX_TEXTURE_IMAGE_UNITS; 5747117f1b4Smrg ctx->Const.MaxVarying = MAX_VARYING; 5757117f1b4Smrg#endif 5767117f1b4Smrg 5774a49301eSmrg /* GL_ARB_framebuffer_object */ 5784a49301eSmrg ctx->Const.MaxSamples = 0; 5794a49301eSmrg 5804a49301eSmrg /* GL_ARB_sync */ 5814a49301eSmrg ctx->Const.MaxServerWaitTimeout = (GLuint64) ~0; 5824a49301eSmrg 5834a49301eSmrg /* GL_ATI_envmap_bumpmap */ 5844a49301eSmrg ctx->Const.SupportedBumpUnits = SUPPORTED_ATI_BUMP_UNITS; 5854a49301eSmrg 5864a49301eSmrg /* GL_EXT_provoking_vertex */ 5874a49301eSmrg ctx->Const.QuadsFollowProvokingVertexConvention = GL_TRUE; 5884a49301eSmrg 5897117f1b4Smrg /* sanity checks */ 5907117f1b4Smrg ASSERT(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.MaxTextureImageUnits, 5917117f1b4Smrg ctx->Const.MaxTextureCoordUnits)); 5927117f1b4Smrg ASSERT(ctx->Const.FragmentProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS); 5937117f1b4Smrg ASSERT(ctx->Const.VertexProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS); 5947117f1b4Smrg 5957117f1b4Smrg ASSERT(MAX_NV_FRAGMENT_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS); 5967117f1b4Smrg ASSERT(MAX_NV_VERTEX_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS); 5977117f1b4Smrg ASSERT(MAX_NV_VERTEX_PROGRAM_INPUTS <= VERT_ATTRIB_MAX); 5987117f1b4Smrg ASSERT(MAX_NV_VERTEX_PROGRAM_OUTPUTS <= VERT_RESULT_MAX); 5994a49301eSmrg 6004a49301eSmrg /* check that we don't exceed the size of various bitfields */ 6014a49301eSmrg ASSERT(VERT_RESULT_MAX <= 6024a49301eSmrg (8 * sizeof(ctx->VertexProgram._Current->Base.OutputsWritten))); 6034a49301eSmrg ASSERT(FRAG_ATTRIB_MAX <= 6044a49301eSmrg (8 * sizeof(ctx->FragmentProgram._Current->Base.InputsRead))); 6057117f1b4Smrg} 6067117f1b4Smrg 6077117f1b4Smrg 6087117f1b4Smrg/** 6097117f1b4Smrg * Do some sanity checks on the limits/constants for the given context. 6107117f1b4Smrg * Only called the first time a context is bound. 6117117f1b4Smrg */ 6127117f1b4Smrgstatic void 6137117f1b4Smrgcheck_context_limits(GLcontext *ctx) 6147117f1b4Smrg{ 6157117f1b4Smrg /* Many context limits/constants are limited by the size of 6167117f1b4Smrg * internal arrays. 6177117f1b4Smrg */ 6187117f1b4Smrg assert(ctx->Const.MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS); 6197117f1b4Smrg assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS); 6207117f1b4Smrg assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS); 6217117f1b4Smrg assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS); 6227117f1b4Smrg 623c1f859d4Smrg /* number of coord units cannot be greater than number of image units */ 624c1f859d4Smrg assert(ctx->Const.MaxTextureCoordUnits <= ctx->Const.MaxTextureImageUnits); 625c1f859d4Smrg 6264a49301eSmrg assert(ctx->Const.MaxTextureLevels <= MAX_TEXTURE_LEVELS); 6274a49301eSmrg assert(ctx->Const.Max3DTextureLevels <= MAX_3D_TEXTURE_LEVELS); 6284a49301eSmrg assert(ctx->Const.MaxCubeTextureLevels <= MAX_CUBE_TEXTURE_LEVELS); 6294a49301eSmrg assert(ctx->Const.MaxTextureRectSize <= MAX_TEXTURE_RECT_SIZE); 6307117f1b4Smrg 6317117f1b4Smrg /* make sure largest texture image is <= MAX_WIDTH in size */ 6324a49301eSmrg assert((1 << (ctx->Const.MaxTextureLevels - 1)) <= MAX_WIDTH); 6334a49301eSmrg assert((1 << (ctx->Const.MaxCubeTextureLevels - 1)) <= MAX_WIDTH); 6344a49301eSmrg assert((1 << (ctx->Const.Max3DTextureLevels - 1)) <= MAX_WIDTH); 6354a49301eSmrg 6364a49301eSmrg assert(ctx->Const.MaxViewportWidth <= MAX_WIDTH); 6374a49301eSmrg assert(ctx->Const.MaxViewportHeight <= MAX_WIDTH); 6387117f1b4Smrg 6397117f1b4Smrg assert(ctx->Const.MaxDrawBuffers <= MAX_DRAW_BUFFERS); 6407117f1b4Smrg 6417117f1b4Smrg /* XXX probably add more tests */ 6427117f1b4Smrg} 6437117f1b4Smrg 6447117f1b4Smrg 6457117f1b4Smrg/** 6467117f1b4Smrg * Initialize the attribute groups in a GL context. 6477117f1b4Smrg * 6487117f1b4Smrg * \param ctx GL context. 6497117f1b4Smrg * 6507117f1b4Smrg * Initializes all the attributes, calling the respective <tt>init*</tt> 6517117f1b4Smrg * functions for the more complex data structures. 6527117f1b4Smrg */ 6537117f1b4Smrgstatic GLboolean 6547117f1b4Smrginit_attrib_groups(GLcontext *ctx) 6557117f1b4Smrg{ 6567117f1b4Smrg assert(ctx); 6577117f1b4Smrg 6587117f1b4Smrg /* Constants */ 6597117f1b4Smrg _mesa_init_constants( ctx ); 6607117f1b4Smrg 6617117f1b4Smrg /* Extensions */ 6627117f1b4Smrg _mesa_init_extensions( ctx ); 6637117f1b4Smrg 6647117f1b4Smrg /* Attribute Groups */ 6657117f1b4Smrg _mesa_init_accum( ctx ); 6667117f1b4Smrg _mesa_init_attrib( ctx ); 6677117f1b4Smrg _mesa_init_buffer_objects( ctx ); 6687117f1b4Smrg _mesa_init_color( ctx ); 6697117f1b4Smrg _mesa_init_colortables( ctx ); 6707117f1b4Smrg _mesa_init_current( ctx ); 6717117f1b4Smrg _mesa_init_depth( ctx ); 6727117f1b4Smrg _mesa_init_debug( ctx ); 6737117f1b4Smrg _mesa_init_display_list( ctx ); 6747117f1b4Smrg _mesa_init_eval( ctx ); 675c1f859d4Smrg _mesa_init_fbobjects( ctx ); 6767117f1b4Smrg _mesa_init_feedback( ctx ); 6777117f1b4Smrg _mesa_init_fog( ctx ); 6787117f1b4Smrg _mesa_init_histogram( ctx ); 6797117f1b4Smrg _mesa_init_hint( ctx ); 6807117f1b4Smrg _mesa_init_line( ctx ); 6817117f1b4Smrg _mesa_init_lighting( ctx ); 6827117f1b4Smrg _mesa_init_matrix( ctx ); 6837117f1b4Smrg _mesa_init_multisample( ctx ); 6847117f1b4Smrg _mesa_init_pixel( ctx ); 685c1f859d4Smrg _mesa_init_pixelstore( ctx ); 6867117f1b4Smrg _mesa_init_point( ctx ); 6877117f1b4Smrg _mesa_init_polygon( ctx ); 6887117f1b4Smrg _mesa_init_program( ctx ); 6894a49301eSmrg _mesa_init_queryobj( ctx ); 6904a49301eSmrg#if FEATURE_ARB_sync 6914a49301eSmrg _mesa_init_sync( ctx ); 692c1f859d4Smrg#endif 6937117f1b4Smrg _mesa_init_rastpos( ctx ); 6947117f1b4Smrg _mesa_init_scissor( ctx ); 6957117f1b4Smrg _mesa_init_shader_state( ctx ); 6967117f1b4Smrg _mesa_init_stencil( ctx ); 6977117f1b4Smrg _mesa_init_transform( ctx ); 6987117f1b4Smrg _mesa_init_varray( ctx ); 6997117f1b4Smrg _mesa_init_viewport( ctx ); 7007117f1b4Smrg 7017117f1b4Smrg if (!_mesa_init_texture( ctx )) 7027117f1b4Smrg return GL_FALSE; 7037117f1b4Smrg 7047117f1b4Smrg _mesa_init_texture_s3tc( ctx ); 7057117f1b4Smrg 7067117f1b4Smrg /* Miscellaneous */ 7077117f1b4Smrg ctx->NewState = _NEW_ALL; 7087117f1b4Smrg ctx->ErrorValue = (GLenum) GL_NO_ERROR; 7094a49301eSmrg ctx->varying_vp_inputs = ~0; 7107117f1b4Smrg 7117117f1b4Smrg return GL_TRUE; 7127117f1b4Smrg} 7137117f1b4Smrg 7147117f1b4Smrg 715c1f859d4Smrg/** 716c1f859d4Smrg * Update default objects in a GL context with respect to shared state. 717c1f859d4Smrg * 718c1f859d4Smrg * \param ctx GL context. 719c1f859d4Smrg * 720c1f859d4Smrg * Removes references to old default objects, (texture objects, program 721c1f859d4Smrg * objects, etc.) and changes to reference those from the current shared 722c1f859d4Smrg * state. 723c1f859d4Smrg */ 724c1f859d4Smrgstatic GLboolean 725c1f859d4Smrgupdate_default_objects(GLcontext *ctx) 726c1f859d4Smrg{ 727c1f859d4Smrg assert(ctx); 728c1f859d4Smrg 729c1f859d4Smrg _mesa_update_default_objects_program(ctx); 730c1f859d4Smrg _mesa_update_default_objects_texture(ctx); 731c1f859d4Smrg _mesa_update_default_objects_buffer_objects(ctx); 732c1f859d4Smrg 733c1f859d4Smrg return GL_TRUE; 734c1f859d4Smrg} 735c1f859d4Smrg 736c1f859d4Smrg 7377117f1b4Smrg/** 7387117f1b4Smrg * This is the default function we plug into all dispatch table slots 7397117f1b4Smrg * This helps prevents a segfault when someone calls a GL function without 7407117f1b4Smrg * first checking if the extension's supported. 7417117f1b4Smrg */ 7427117f1b4Smrgstatic int 7437117f1b4Smrggeneric_nop(void) 7447117f1b4Smrg{ 745c1f859d4Smrg _mesa_warning(NULL, "User called no-op dispatch function (an unsupported extension function?)"); 7467117f1b4Smrg return 0; 7477117f1b4Smrg} 7487117f1b4Smrg 7497117f1b4Smrg 7507117f1b4Smrg/** 7517117f1b4Smrg * Allocate and initialize a new dispatch table. 7527117f1b4Smrg */ 7537117f1b4Smrgstatic struct _glapi_table * 7547117f1b4Smrgalloc_dispatch_table(void) 7557117f1b4Smrg{ 7567117f1b4Smrg /* Find the larger of Mesa's dispatch table and libGL's dispatch table. 7577117f1b4Smrg * In practice, this'll be the same for stand-alone Mesa. But for DRI 7587117f1b4Smrg * Mesa we do this to accomodate different versions of libGL and various 7597117f1b4Smrg * DRI drivers. 7607117f1b4Smrg */ 7617117f1b4Smrg GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), 7627117f1b4Smrg sizeof(struct _glapi_table) / sizeof(_glapi_proc)); 7637117f1b4Smrg struct _glapi_table *table = 7647117f1b4Smrg (struct _glapi_table *) _mesa_malloc(numEntries * sizeof(_glapi_proc)); 7657117f1b4Smrg if (table) { 7667117f1b4Smrg _glapi_proc *entry = (_glapi_proc *) table; 7677117f1b4Smrg GLint i; 7687117f1b4Smrg for (i = 0; i < numEntries; i++) { 7697117f1b4Smrg entry[i] = (_glapi_proc) generic_nop; 7707117f1b4Smrg } 7717117f1b4Smrg } 7727117f1b4Smrg return table; 7737117f1b4Smrg} 7747117f1b4Smrg 7757117f1b4Smrg 7767117f1b4Smrg/** 7777117f1b4Smrg * Initialize a GLcontext struct (rendering context). 7787117f1b4Smrg * 7797117f1b4Smrg * This includes allocating all the other structs and arrays which hang off of 7807117f1b4Smrg * the context by pointers. 7817117f1b4Smrg * Note that the driver needs to pass in its dd_function_table here since 7827117f1b4Smrg * we need to at least call driverFunctions->NewTextureObject to create the 7837117f1b4Smrg * default texture objects. 7847117f1b4Smrg * 7857117f1b4Smrg * Called by _mesa_create_context(). 7867117f1b4Smrg * 7877117f1b4Smrg * Performs the imports and exports callback tables initialization, and 7887117f1b4Smrg * miscellaneous one-time initializations. If no shared context is supplied one 7897117f1b4Smrg * is allocated, and increase its reference count. Setups the GL API dispatch 7907117f1b4Smrg * tables. Initialize the TNL module. Sets the maximum Z buffer depth. 7917117f1b4Smrg * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables 7927117f1b4Smrg * for debug flags. 7937117f1b4Smrg * 7947117f1b4Smrg * \param ctx the context to initialize 7957117f1b4Smrg * \param visual describes the visual attributes for this context 7967117f1b4Smrg * \param share_list points to context to share textures, display lists, 7977117f1b4Smrg * etc with, or NULL 7987117f1b4Smrg * \param driverFunctions table of device driver functions for this context 7997117f1b4Smrg * to use 8007117f1b4Smrg * \param driverContext pointer to driver-specific context data 8017117f1b4Smrg */ 8027117f1b4SmrgGLboolean 8037117f1b4Smrg_mesa_initialize_context(GLcontext *ctx, 8047117f1b4Smrg const GLvisual *visual, 8057117f1b4Smrg GLcontext *share_list, 8067117f1b4Smrg const struct dd_function_table *driverFunctions, 8077117f1b4Smrg void *driverContext) 8087117f1b4Smrg{ 8094a49301eSmrg struct gl_shared_state *shared; 8104a49301eSmrg 8114a49301eSmrg /*ASSERT(driverContext);*/ 8127117f1b4Smrg assert(driverFunctions->NewTextureObject); 8137117f1b4Smrg assert(driverFunctions->FreeTexImageData); 8147117f1b4Smrg 8157117f1b4Smrg /* misc one-time initializations */ 8167117f1b4Smrg one_time_init(ctx); 8177117f1b4Smrg 8187117f1b4Smrg ctx->Visual = *visual; 8197117f1b4Smrg ctx->DrawBuffer = NULL; 8207117f1b4Smrg ctx->ReadBuffer = NULL; 8217117f1b4Smrg ctx->WinSysDrawBuffer = NULL; 8227117f1b4Smrg ctx->WinSysReadBuffer = NULL; 8237117f1b4Smrg 8247117f1b4Smrg /* Plug in driver functions and context pointer here. 8257117f1b4Smrg * This is important because when we call alloc_shared_state() below 8267117f1b4Smrg * we'll call ctx->Driver.NewTextureObject() to create the default 8277117f1b4Smrg * textures. 8287117f1b4Smrg */ 8297117f1b4Smrg ctx->Driver = *driverFunctions; 8307117f1b4Smrg ctx->DriverCtx = driverContext; 8317117f1b4Smrg 8327117f1b4Smrg if (share_list) { 8337117f1b4Smrg /* share state with another context */ 8344a49301eSmrg shared = share_list->Shared; 8357117f1b4Smrg } 8367117f1b4Smrg else { 8377117f1b4Smrg /* allocate new, unshared state */ 8384a49301eSmrg shared = _mesa_alloc_shared_state(ctx); 8394a49301eSmrg if (!shared) 8407117f1b4Smrg return GL_FALSE; 8417117f1b4Smrg } 8424a49301eSmrg 8434a49301eSmrg _glthread_LOCK_MUTEX(shared->Mutex); 8444a49301eSmrg ctx->Shared = shared; 8454a49301eSmrg shared->RefCount++; 8464a49301eSmrg _glthread_UNLOCK_MUTEX(shared->Mutex); 8477117f1b4Smrg 8487117f1b4Smrg if (!init_attrib_groups( ctx )) { 8494a49301eSmrg _mesa_release_shared_state(ctx, ctx->Shared); 8507117f1b4Smrg return GL_FALSE; 8517117f1b4Smrg } 8527117f1b4Smrg 8537117f1b4Smrg /* setup the API dispatch tables */ 8547117f1b4Smrg ctx->Exec = alloc_dispatch_table(); 8557117f1b4Smrg ctx->Save = alloc_dispatch_table(); 8567117f1b4Smrg if (!ctx->Exec || !ctx->Save) { 8574a49301eSmrg _mesa_release_shared_state(ctx, ctx->Shared); 8587117f1b4Smrg if (ctx->Exec) 8597117f1b4Smrg _mesa_free(ctx->Exec); 8604a49301eSmrg return GL_FALSE; 8617117f1b4Smrg } 862c1f859d4Smrg#if FEATURE_dispatch 8637117f1b4Smrg _mesa_init_exec_table(ctx->Exec); 864c1f859d4Smrg#endif 8657117f1b4Smrg ctx->CurrentDispatch = ctx->Exec; 8664a49301eSmrg 867c1f859d4Smrg#if FEATURE_dlist 8684a49301eSmrg _mesa_init_save_table(ctx->Save); 8697117f1b4Smrg _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt ); 870c1f859d4Smrg#endif 8714a49301eSmrg 8727117f1b4Smrg /* Neutral tnl module stuff */ 8737117f1b4Smrg _mesa_init_exec_vtxfmt( ctx ); 8747117f1b4Smrg ctx->TnlModule.Current = NULL; 8757117f1b4Smrg ctx->TnlModule.SwapCount = 0; 8767117f1b4Smrg 8777117f1b4Smrg ctx->FragmentProgram._MaintainTexEnvProgram 8787117f1b4Smrg = (_mesa_getenv("MESA_TEX_PROG") != NULL); 8797117f1b4Smrg 8807117f1b4Smrg ctx->VertexProgram._MaintainTnlProgram 8817117f1b4Smrg = (_mesa_getenv("MESA_TNL_PROG") != NULL); 8827117f1b4Smrg if (ctx->VertexProgram._MaintainTnlProgram) { 8837117f1b4Smrg /* this is required... */ 8847117f1b4Smrg ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE; 8857117f1b4Smrg } 8867117f1b4Smrg 887c1f859d4Smrg#ifdef FEATURE_extra_context_init 888c1f859d4Smrg _mesa_initialize_context_extra(ctx); 889c1f859d4Smrg#endif 890c1f859d4Smrg 8917117f1b4Smrg ctx->FirstTimeCurrent = GL_TRUE; 8927117f1b4Smrg 8937117f1b4Smrg return GL_TRUE; 8947117f1b4Smrg} 8957117f1b4Smrg 8967117f1b4Smrg 8977117f1b4Smrg/** 8987117f1b4Smrg * Allocate and initialize a GLcontext structure. 8997117f1b4Smrg * Note that the driver needs to pass in its dd_function_table here since 9007117f1b4Smrg * we need to at least call driverFunctions->NewTextureObject to initialize 9017117f1b4Smrg * the rendering context. 9027117f1b4Smrg * 9037117f1b4Smrg * \param visual a GLvisual pointer (we copy the struct contents) 9047117f1b4Smrg * \param share_list another context to share display lists with or NULL 9057117f1b4Smrg * \param driverFunctions points to the dd_function_table into which the 9067117f1b4Smrg * driver has plugged in all its special functions. 9074a49301eSmrg * \param driverContext points to the device driver's private context state 9087117f1b4Smrg * 9097117f1b4Smrg * \return pointer to a new __GLcontextRec or NULL if error. 9107117f1b4Smrg */ 9117117f1b4SmrgGLcontext * 9127117f1b4Smrg_mesa_create_context(const GLvisual *visual, 9137117f1b4Smrg GLcontext *share_list, 9147117f1b4Smrg const struct dd_function_table *driverFunctions, 9157117f1b4Smrg void *driverContext) 9167117f1b4Smrg{ 9177117f1b4Smrg GLcontext *ctx; 9187117f1b4Smrg 9197117f1b4Smrg ASSERT(visual); 9204a49301eSmrg /*ASSERT(driverContext);*/ 9217117f1b4Smrg 9227117f1b4Smrg ctx = (GLcontext *) _mesa_calloc(sizeof(GLcontext)); 9237117f1b4Smrg if (!ctx) 9247117f1b4Smrg return NULL; 9257117f1b4Smrg 9267117f1b4Smrg if (_mesa_initialize_context(ctx, visual, share_list, 9277117f1b4Smrg driverFunctions, driverContext)) { 9287117f1b4Smrg return ctx; 9297117f1b4Smrg } 9307117f1b4Smrg else { 9317117f1b4Smrg _mesa_free(ctx); 9327117f1b4Smrg return NULL; 9337117f1b4Smrg } 9347117f1b4Smrg} 9357117f1b4Smrg 9367117f1b4Smrg 9377117f1b4Smrg/** 9387117f1b4Smrg * Free the data associated with the given context. 9397117f1b4Smrg * 9407117f1b4Smrg * But doesn't free the GLcontext struct itself. 9417117f1b4Smrg * 9427117f1b4Smrg * \sa _mesa_initialize_context() and init_attrib_groups(). 9437117f1b4Smrg */ 9447117f1b4Smrgvoid 9457117f1b4Smrg_mesa_free_context_data( GLcontext *ctx ) 9467117f1b4Smrg{ 9477117f1b4Smrg if (!_mesa_get_current_context()){ 9487117f1b4Smrg /* No current context, but we may need one in order to delete 9497117f1b4Smrg * texture objs, etc. So temporarily bind the context now. 9507117f1b4Smrg */ 9517117f1b4Smrg _mesa_make_current(ctx, NULL, NULL); 9527117f1b4Smrg } 9537117f1b4Smrg 9547117f1b4Smrg /* unreference WinSysDraw/Read buffers */ 9554a49301eSmrg _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL); 9564a49301eSmrg _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL); 9574a49301eSmrg _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL); 9584a49301eSmrg _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL); 9597117f1b4Smrg 960c1f859d4Smrg _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL); 961c1f859d4Smrg _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL); 962c1f859d4Smrg _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram, NULL); 963c1f859d4Smrg 964c1f859d4Smrg _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL); 965c1f859d4Smrg _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL); 966c1f859d4Smrg _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, NULL); 967c1f859d4Smrg 9687117f1b4Smrg _mesa_free_attrib_data(ctx); 9694a49301eSmrg _mesa_free_buffer_objects(ctx); 9707117f1b4Smrg _mesa_free_lighting_data( ctx ); 9717117f1b4Smrg _mesa_free_eval_data( ctx ); 9727117f1b4Smrg _mesa_free_texture_data( ctx ); 9737117f1b4Smrg _mesa_free_matrix_data( ctx ); 9747117f1b4Smrg _mesa_free_viewport_data( ctx ); 9757117f1b4Smrg _mesa_free_colortables_data( ctx ); 9767117f1b4Smrg _mesa_free_program_data(ctx); 9777117f1b4Smrg _mesa_free_shader_state(ctx); 9784a49301eSmrg _mesa_free_queryobj_data(ctx); 9794a49301eSmrg#if FEATURE_ARB_sync 9804a49301eSmrg _mesa_free_sync_data(ctx); 9814a49301eSmrg#endif 9824a49301eSmrg _mesa_free_varray_data(ctx); 9834a49301eSmrg 9844a49301eSmrg _mesa_delete_array_object(ctx, ctx->Array.DefaultArrayObj); 9854a49301eSmrg 9864a49301eSmrg#if FEATURE_ARB_pixel_buffer_object 9874a49301eSmrg _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL); 9884a49301eSmrg _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL); 9894a49301eSmrg _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL); 990c1f859d4Smrg#endif 9917117f1b4Smrg 9927117f1b4Smrg#if FEATURE_ARB_vertex_buffer_object 9934a49301eSmrg _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL); 9944a49301eSmrg _mesa_reference_buffer_object(ctx, &ctx->Array.ElementArrayBufferObj, NULL); 9957117f1b4Smrg#endif 9967117f1b4Smrg 9977117f1b4Smrg /* free dispatch tables */ 9987117f1b4Smrg _mesa_free(ctx->Exec); 9997117f1b4Smrg _mesa_free(ctx->Save); 10007117f1b4Smrg 10017117f1b4Smrg /* Shared context state (display lists, textures, etc) */ 10024a49301eSmrg _mesa_release_shared_state( ctx, ctx->Shared ); 10034a49301eSmrg 10044a49301eSmrg /* needs to be after freeing shared state */ 10054a49301eSmrg _mesa_free_display_list_data(ctx); 10067117f1b4Smrg 10077117f1b4Smrg if (ctx->Extensions.String) 10087117f1b4Smrg _mesa_free((void *) ctx->Extensions.String); 10097117f1b4Smrg 10107117f1b4Smrg /* unbind the context if it's currently bound */ 10117117f1b4Smrg if (ctx == _mesa_get_current_context()) { 10127117f1b4Smrg _mesa_make_current(NULL, NULL, NULL); 10137117f1b4Smrg } 10147117f1b4Smrg} 10157117f1b4Smrg 10167117f1b4Smrg 10177117f1b4Smrg/** 10187117f1b4Smrg * Destroy a GLcontext structure. 10197117f1b4Smrg * 10207117f1b4Smrg * \param ctx GL context. 10217117f1b4Smrg * 10227117f1b4Smrg * Calls _mesa_free_context_data() and frees the GLcontext structure itself. 10237117f1b4Smrg */ 10247117f1b4Smrgvoid 10257117f1b4Smrg_mesa_destroy_context( GLcontext *ctx ) 10267117f1b4Smrg{ 10277117f1b4Smrg if (ctx) { 10287117f1b4Smrg _mesa_free_context_data(ctx); 10297117f1b4Smrg _mesa_free( (void *) ctx ); 10307117f1b4Smrg } 10317117f1b4Smrg} 10327117f1b4Smrg 10337117f1b4Smrg 10347117f1b4Smrg#if _HAVE_FULL_GL 10357117f1b4Smrg/** 10367117f1b4Smrg * Copy attribute groups from one context to another. 10377117f1b4Smrg * 10387117f1b4Smrg * \param src source context 10397117f1b4Smrg * \param dst destination context 10407117f1b4Smrg * \param mask bitwise OR of GL_*_BIT flags 10417117f1b4Smrg * 10427117f1b4Smrg * According to the bits specified in \p mask, copies the corresponding 10437117f1b4Smrg * attributes from \p src into \p dst. For many of the attributes a simple \c 10447117f1b4Smrg * memcpy is not enough due to the existence of internal pointers in their data 10457117f1b4Smrg * structures. 10467117f1b4Smrg */ 10477117f1b4Smrgvoid 10487117f1b4Smrg_mesa_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask ) 10497117f1b4Smrg{ 10507117f1b4Smrg if (mask & GL_ACCUM_BUFFER_BIT) { 10517117f1b4Smrg /* OK to memcpy */ 10527117f1b4Smrg dst->Accum = src->Accum; 10537117f1b4Smrg } 10547117f1b4Smrg if (mask & GL_COLOR_BUFFER_BIT) { 10557117f1b4Smrg /* OK to memcpy */ 10567117f1b4Smrg dst->Color = src->Color; 10577117f1b4Smrg } 10587117f1b4Smrg if (mask & GL_CURRENT_BIT) { 10597117f1b4Smrg /* OK to memcpy */ 10607117f1b4Smrg dst->Current = src->Current; 10617117f1b4Smrg } 10627117f1b4Smrg if (mask & GL_DEPTH_BUFFER_BIT) { 10637117f1b4Smrg /* OK to memcpy */ 10647117f1b4Smrg dst->Depth = src->Depth; 10657117f1b4Smrg } 10667117f1b4Smrg if (mask & GL_ENABLE_BIT) { 10677117f1b4Smrg /* no op */ 10687117f1b4Smrg } 10697117f1b4Smrg if (mask & GL_EVAL_BIT) { 10707117f1b4Smrg /* OK to memcpy */ 10717117f1b4Smrg dst->Eval = src->Eval; 10727117f1b4Smrg } 10737117f1b4Smrg if (mask & GL_FOG_BIT) { 10747117f1b4Smrg /* OK to memcpy */ 10757117f1b4Smrg dst->Fog = src->Fog; 10767117f1b4Smrg } 10777117f1b4Smrg if (mask & GL_HINT_BIT) { 10787117f1b4Smrg /* OK to memcpy */ 10797117f1b4Smrg dst->Hint = src->Hint; 10807117f1b4Smrg } 10817117f1b4Smrg if (mask & GL_LIGHTING_BIT) { 10827117f1b4Smrg GLuint i; 10837117f1b4Smrg /* begin with memcpy */ 10847117f1b4Smrg dst->Light = src->Light; 10857117f1b4Smrg /* fixup linked lists to prevent pointer insanity */ 10867117f1b4Smrg make_empty_list( &(dst->Light.EnabledList) ); 10877117f1b4Smrg for (i = 0; i < MAX_LIGHTS; i++) { 10887117f1b4Smrg if (dst->Light.Light[i].Enabled) { 10897117f1b4Smrg insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i])); 10907117f1b4Smrg } 10917117f1b4Smrg } 10927117f1b4Smrg } 10937117f1b4Smrg if (mask & GL_LINE_BIT) { 10947117f1b4Smrg /* OK to memcpy */ 10957117f1b4Smrg dst->Line = src->Line; 10967117f1b4Smrg } 10977117f1b4Smrg if (mask & GL_LIST_BIT) { 10987117f1b4Smrg /* OK to memcpy */ 10997117f1b4Smrg dst->List = src->List; 11007117f1b4Smrg } 11017117f1b4Smrg if (mask & GL_PIXEL_MODE_BIT) { 11027117f1b4Smrg /* OK to memcpy */ 11037117f1b4Smrg dst->Pixel = src->Pixel; 11047117f1b4Smrg } 11057117f1b4Smrg if (mask & GL_POINT_BIT) { 11067117f1b4Smrg /* OK to memcpy */ 11077117f1b4Smrg dst->Point = src->Point; 11087117f1b4Smrg } 11097117f1b4Smrg if (mask & GL_POLYGON_BIT) { 11107117f1b4Smrg /* OK to memcpy */ 11117117f1b4Smrg dst->Polygon = src->Polygon; 11127117f1b4Smrg } 11137117f1b4Smrg if (mask & GL_POLYGON_STIPPLE_BIT) { 11147117f1b4Smrg /* Use loop instead of MEMCPY due to problem with Portland Group's 11157117f1b4Smrg * C compiler. Reported by John Stone. 11167117f1b4Smrg */ 11177117f1b4Smrg GLuint i; 11187117f1b4Smrg for (i = 0; i < 32; i++) { 11197117f1b4Smrg dst->PolygonStipple[i] = src->PolygonStipple[i]; 11207117f1b4Smrg } 11217117f1b4Smrg } 11227117f1b4Smrg if (mask & GL_SCISSOR_BIT) { 11237117f1b4Smrg /* OK to memcpy */ 11247117f1b4Smrg dst->Scissor = src->Scissor; 11257117f1b4Smrg } 11267117f1b4Smrg if (mask & GL_STENCIL_BUFFER_BIT) { 11277117f1b4Smrg /* OK to memcpy */ 11287117f1b4Smrg dst->Stencil = src->Stencil; 11297117f1b4Smrg } 11307117f1b4Smrg if (mask & GL_TEXTURE_BIT) { 11317117f1b4Smrg /* Cannot memcpy because of pointers */ 11327117f1b4Smrg _mesa_copy_texture_state(src, dst); 11337117f1b4Smrg } 11347117f1b4Smrg if (mask & GL_TRANSFORM_BIT) { 11357117f1b4Smrg /* OK to memcpy */ 11367117f1b4Smrg dst->Transform = src->Transform; 11377117f1b4Smrg } 11387117f1b4Smrg if (mask & GL_VIEWPORT_BIT) { 11397117f1b4Smrg /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */ 11407117f1b4Smrg dst->Viewport.X = src->Viewport.X; 11417117f1b4Smrg dst->Viewport.Y = src->Viewport.Y; 11427117f1b4Smrg dst->Viewport.Width = src->Viewport.Width; 11437117f1b4Smrg dst->Viewport.Height = src->Viewport.Height; 11447117f1b4Smrg dst->Viewport.Near = src->Viewport.Near; 11457117f1b4Smrg dst->Viewport.Far = src->Viewport.Far; 11467117f1b4Smrg _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap); 11477117f1b4Smrg } 11487117f1b4Smrg 11497117f1b4Smrg /* XXX FIXME: Call callbacks? 11507117f1b4Smrg */ 11517117f1b4Smrg dst->NewState = _NEW_ALL; 11527117f1b4Smrg} 11537117f1b4Smrg#endif 11547117f1b4Smrg 11557117f1b4Smrg 11567117f1b4Smrg/** 11577117f1b4Smrg * Check if the given context can render into the given framebuffer 11587117f1b4Smrg * by checking visual attributes. 11597117f1b4Smrg * 1160c1f859d4Smrg * Most of these tests could go away because Mesa is now pretty flexible 1161c1f859d4Smrg * in terms of mixing rendering contexts with framebuffers. As long 1162c1f859d4Smrg * as RGB vs. CI mode agree, we're probably good. 11637117f1b4Smrg * 11647117f1b4Smrg * \return GL_TRUE if compatible, GL_FALSE otherwise. 11657117f1b4Smrg */ 11667117f1b4Smrgstatic GLboolean 11677117f1b4Smrgcheck_compatible(const GLcontext *ctx, const GLframebuffer *buffer) 11687117f1b4Smrg{ 11697117f1b4Smrg const GLvisual *ctxvis = &ctx->Visual; 11707117f1b4Smrg const GLvisual *bufvis = &buffer->Visual; 11717117f1b4Smrg 11727117f1b4Smrg if (ctxvis == bufvis) 11737117f1b4Smrg return GL_TRUE; 11747117f1b4Smrg 11757117f1b4Smrg if (ctxvis->rgbMode != bufvis->rgbMode) 11767117f1b4Smrg return GL_FALSE; 11777117f1b4Smrg#if 0 11787117f1b4Smrg /* disabling this fixes the fgl_glxgears pbuffer demo */ 11797117f1b4Smrg if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode) 11807117f1b4Smrg return GL_FALSE; 11817117f1b4Smrg#endif 11827117f1b4Smrg if (ctxvis->stereoMode && !bufvis->stereoMode) 11837117f1b4Smrg return GL_FALSE; 11847117f1b4Smrg if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer) 11857117f1b4Smrg return GL_FALSE; 11867117f1b4Smrg if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer) 11877117f1b4Smrg return GL_FALSE; 11887117f1b4Smrg if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer) 11897117f1b4Smrg return GL_FALSE; 11907117f1b4Smrg if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask) 11917117f1b4Smrg return GL_FALSE; 11927117f1b4Smrg if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask) 11937117f1b4Smrg return GL_FALSE; 11947117f1b4Smrg if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask) 11957117f1b4Smrg return GL_FALSE; 11967117f1b4Smrg#if 0 11977117f1b4Smrg /* disabled (see bug 11161) */ 11987117f1b4Smrg if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits) 11997117f1b4Smrg return GL_FALSE; 12007117f1b4Smrg#endif 12017117f1b4Smrg if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits) 12027117f1b4Smrg return GL_FALSE; 12037117f1b4Smrg 12047117f1b4Smrg return GL_TRUE; 12057117f1b4Smrg} 12067117f1b4Smrg 12077117f1b4Smrg 12087117f1b4Smrg/** 12097117f1b4Smrg * Do one-time initialization for the given framebuffer. Specifically, 12107117f1b4Smrg * ask the driver for the window's current size and update the framebuffer 12117117f1b4Smrg * object to match. 12127117f1b4Smrg * Really, the device driver should totally take care of this. 12137117f1b4Smrg */ 12147117f1b4Smrgstatic void 12157117f1b4Smrginitialize_framebuffer_size(GLcontext *ctx, GLframebuffer *fb) 12167117f1b4Smrg{ 12177117f1b4Smrg GLuint width, height; 12187117f1b4Smrg if (ctx->Driver.GetBufferSize) { 12197117f1b4Smrg ctx->Driver.GetBufferSize(fb, &width, &height); 12207117f1b4Smrg if (ctx->Driver.ResizeBuffers) 12217117f1b4Smrg ctx->Driver.ResizeBuffers(ctx, fb, width, height); 12227117f1b4Smrg fb->Initialized = GL_TRUE; 12237117f1b4Smrg } 12247117f1b4Smrg} 12257117f1b4Smrg 12267117f1b4Smrg 1227c7037ccdSmrg/** 1228c7037ccdSmrg * Check if the viewport/scissor size has not yet been initialized. 1229c7037ccdSmrg * Initialize the size if the given width and height are non-zero. 1230c7037ccdSmrg */ 1231c7037ccdSmrgvoid 1232c7037ccdSmrg_mesa_check_init_viewport(GLcontext *ctx, GLuint width, GLuint height) 1233c7037ccdSmrg{ 1234c7037ccdSmrg if (!ctx->ViewportInitialized && width > 0 && height > 0) { 1235c7037ccdSmrg /* Note: set flag here, before calling _mesa_set_viewport(), to prevent 1236c7037ccdSmrg * potential infinite recursion. 1237c7037ccdSmrg */ 1238c7037ccdSmrg ctx->ViewportInitialized = GL_TRUE; 1239c7037ccdSmrg _mesa_set_viewport(ctx, 0, 0, width, height); 1240c7037ccdSmrg _mesa_set_scissor(ctx, 0, 0, width, height); 1241c7037ccdSmrg } 1242c7037ccdSmrg} 1243c7037ccdSmrg 1244c7037ccdSmrg 12457117f1b4Smrg/** 12467117f1b4Smrg * Bind the given context to the given drawBuffer and readBuffer and 12477117f1b4Smrg * make it the current context for the calling thread. 12487117f1b4Smrg * We'll render into the drawBuffer and read pixels from the 12497117f1b4Smrg * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc). 12507117f1b4Smrg * 12517117f1b4Smrg * We check that the context's and framebuffer's visuals are compatible 12527117f1b4Smrg * and return immediately if they're not. 12537117f1b4Smrg * 12547117f1b4Smrg * \param newCtx the new GL context. If NULL then there will be no current GL 12557117f1b4Smrg * context. 12567117f1b4Smrg * \param drawBuffer the drawing framebuffer 12577117f1b4Smrg * \param readBuffer the reading framebuffer 12587117f1b4Smrg */ 12594a49301eSmrgGLboolean 12607117f1b4Smrg_mesa_make_current( GLcontext *newCtx, GLframebuffer *drawBuffer, 12617117f1b4Smrg GLframebuffer *readBuffer ) 12627117f1b4Smrg{ 12637117f1b4Smrg if (MESA_VERBOSE & VERBOSE_API) 12647117f1b4Smrg _mesa_debug(newCtx, "_mesa_make_current()\n"); 12657117f1b4Smrg 12667117f1b4Smrg /* Check that the context's and framebuffer's visuals are compatible. 12677117f1b4Smrg */ 12687117f1b4Smrg if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) { 12697117f1b4Smrg if (!check_compatible(newCtx, drawBuffer)) { 12707117f1b4Smrg _mesa_warning(newCtx, 12717117f1b4Smrg "MakeCurrent: incompatible visuals for context and drawbuffer"); 12724a49301eSmrg return GL_FALSE; 12737117f1b4Smrg } 12747117f1b4Smrg } 12757117f1b4Smrg if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) { 12767117f1b4Smrg if (!check_compatible(newCtx, readBuffer)) { 12777117f1b4Smrg _mesa_warning(newCtx, 12787117f1b4Smrg "MakeCurrent: incompatible visuals for context and readbuffer"); 12794a49301eSmrg return GL_FALSE; 12807117f1b4Smrg } 12817117f1b4Smrg } 12827117f1b4Smrg 12837117f1b4Smrg /* We used to call _glapi_check_multithread() here. Now do it in drivers */ 12847117f1b4Smrg _glapi_set_context((void *) newCtx); 12857117f1b4Smrg ASSERT(_mesa_get_current_context() == newCtx); 12867117f1b4Smrg 12877117f1b4Smrg if (!newCtx) { 12887117f1b4Smrg _glapi_set_dispatch(NULL); /* none current */ 12897117f1b4Smrg } 12907117f1b4Smrg else { 12917117f1b4Smrg _glapi_set_dispatch(newCtx->CurrentDispatch); 12927117f1b4Smrg 12937117f1b4Smrg if (drawBuffer && readBuffer) { 12947117f1b4Smrg /* TODO: check if newCtx and buffer's visual match??? */ 12957117f1b4Smrg 12967117f1b4Smrg ASSERT(drawBuffer->Name == 0); 12977117f1b4Smrg ASSERT(readBuffer->Name == 0); 12987117f1b4Smrg _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer); 12997117f1b4Smrg _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer); 13007117f1b4Smrg 13017117f1b4Smrg /* 13027117f1b4Smrg * Only set the context's Draw/ReadBuffer fields if they're NULL 13037117f1b4Smrg * or not bound to a user-created FBO. 13047117f1b4Smrg */ 13057117f1b4Smrg if (!newCtx->DrawBuffer || newCtx->DrawBuffer->Name == 0) { 13064a49301eSmrg /* KW: merge conflict here, revisit. 13074a49301eSmrg */ 13084a49301eSmrg /* fix up the fb fields - these will end up wrong otherwise 13094a49301eSmrg * if the DRIdrawable changes, and everything relies on them. 13104a49301eSmrg * This is a bit messy (same as needed in _mesa_BindFramebufferEXT) 13114a49301eSmrg */ 13124a49301eSmrg unsigned int i; 13134a49301eSmrg GLenum buffers[MAX_DRAW_BUFFERS]; 13144a49301eSmrg 13157117f1b4Smrg _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer); 13164a49301eSmrg 13174a49301eSmrg for(i = 0; i < newCtx->Const.MaxDrawBuffers; i++) { 13184a49301eSmrg buffers[i] = newCtx->Color.DrawBuffer[i]; 13194a49301eSmrg } 13204a49301eSmrg 13214a49301eSmrg _mesa_drawbuffers(newCtx, newCtx->Const.MaxDrawBuffers, buffers, NULL); 13227117f1b4Smrg } 13237117f1b4Smrg if (!newCtx->ReadBuffer || newCtx->ReadBuffer->Name == 0) { 13247117f1b4Smrg _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer); 13257117f1b4Smrg } 13267117f1b4Smrg 1327c1f859d4Smrg /* XXX only set this flag if we're really changing the draw/read 1328c1f859d4Smrg * framebuffer bindings. 1329c1f859d4Smrg */ 13307117f1b4Smrg newCtx->NewState |= _NEW_BUFFERS; 13317117f1b4Smrg 13327117f1b4Smrg#if 1 13337117f1b4Smrg /* We want to get rid of these lines: */ 13347117f1b4Smrg 13357117f1b4Smrg#if _HAVE_FULL_GL 13367117f1b4Smrg if (!drawBuffer->Initialized) { 13377117f1b4Smrg initialize_framebuffer_size(newCtx, drawBuffer); 13387117f1b4Smrg } 13397117f1b4Smrg if (readBuffer != drawBuffer && !readBuffer->Initialized) { 13407117f1b4Smrg initialize_framebuffer_size(newCtx, readBuffer); 13417117f1b4Smrg } 13427117f1b4Smrg 13437117f1b4Smrg _mesa_resizebuffers(newCtx); 13447117f1b4Smrg#endif 13457117f1b4Smrg 13467117f1b4Smrg#else 13477117f1b4Smrg /* We want the drawBuffer and readBuffer to be initialized by 13487117f1b4Smrg * the driver. 13497117f1b4Smrg * This generally means the Width and Height match the actual 13507117f1b4Smrg * window size and the renderbuffers (both hardware and software 13517117f1b4Smrg * based) are allocated to match. The later can generally be 13527117f1b4Smrg * done with a call to _mesa_resize_framebuffer(). 13537117f1b4Smrg * 13547117f1b4Smrg * It's theoretically possible for a buffer to have zero width 13557117f1b4Smrg * or height, but for now, assert check that the driver did what's 13567117f1b4Smrg * expected of it. 13577117f1b4Smrg */ 13587117f1b4Smrg ASSERT(drawBuffer->Width > 0); 13597117f1b4Smrg ASSERT(drawBuffer->Height > 0); 13607117f1b4Smrg#endif 13617117f1b4Smrg 1362c7037ccdSmrg if (drawBuffer) { 1363c7037ccdSmrg _mesa_check_init_viewport(newCtx, 1364c7037ccdSmrg drawBuffer->Width, drawBuffer->Height); 13657117f1b4Smrg } 13667117f1b4Smrg } 13677117f1b4Smrg 13687117f1b4Smrg if (newCtx->FirstTimeCurrent) { 1369c7037ccdSmrg check_context_limits(newCtx); 1370c7037ccdSmrg 1371c7037ccdSmrg /* We can use this to help debug user's problems. Tell them to set 1372c7037ccdSmrg * the MESA_INFO env variable before running their app. Then the 1373c7037ccdSmrg * first time each context is made current we'll print some useful 1374c7037ccdSmrg * information. 1375c7037ccdSmrg */ 13767117f1b4Smrg if (_mesa_getenv("MESA_INFO")) { 13777117f1b4Smrg _mesa_print_info(); 13787117f1b4Smrg } 1379c7037ccdSmrg 13807117f1b4Smrg newCtx->FirstTimeCurrent = GL_FALSE; 13817117f1b4Smrg } 13827117f1b4Smrg } 13834a49301eSmrg 13844a49301eSmrg return GL_TRUE; 13857117f1b4Smrg} 13867117f1b4Smrg 13877117f1b4Smrg 13887117f1b4Smrg/** 13897117f1b4Smrg * Make context 'ctx' share the display lists, textures and programs 13907117f1b4Smrg * that are associated with 'ctxToShare'. 13917117f1b4Smrg * Any display lists, textures or programs associated with 'ctx' will 13927117f1b4Smrg * be deleted if nobody else is sharing them. 13937117f1b4Smrg */ 13947117f1b4SmrgGLboolean 13957117f1b4Smrg_mesa_share_state(GLcontext *ctx, GLcontext *ctxToShare) 13967117f1b4Smrg{ 13977117f1b4Smrg if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) { 1398c1f859d4Smrg struct gl_shared_state *oldSharedState = ctx->Shared; 1399c1f859d4Smrg 14007117f1b4Smrg ctx->Shared = ctxToShare->Shared; 14014a49301eSmrg 14024a49301eSmrg _glthread_LOCK_MUTEX(ctx->Shared->Mutex); 14037117f1b4Smrg ctx->Shared->RefCount++; 14044a49301eSmrg _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); 1405c1f859d4Smrg 1406c1f859d4Smrg update_default_objects(ctx); 1407c1f859d4Smrg 14084a49301eSmrg _mesa_release_shared_state(ctx, oldSharedState); 1409c1f859d4Smrg 14107117f1b4Smrg return GL_TRUE; 14117117f1b4Smrg } 14127117f1b4Smrg else { 14137117f1b4Smrg return GL_FALSE; 14147117f1b4Smrg } 14157117f1b4Smrg} 14167117f1b4Smrg 14177117f1b4Smrg 14187117f1b4Smrg 14197117f1b4Smrg/** 14207117f1b4Smrg * \return pointer to the current GL context for this thread. 14217117f1b4Smrg * 14227117f1b4Smrg * Calls _glapi_get_context(). This isn't the fastest way to get the current 14237117f1b4Smrg * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in 14247117f1b4Smrg * context.h. 14257117f1b4Smrg */ 14267117f1b4SmrgGLcontext * 14277117f1b4Smrg_mesa_get_current_context( void ) 14287117f1b4Smrg{ 14297117f1b4Smrg return (GLcontext *) _glapi_get_context(); 14307117f1b4Smrg} 14317117f1b4Smrg 14327117f1b4Smrg 14337117f1b4Smrg/** 14347117f1b4Smrg * Get context's current API dispatch table. 14357117f1b4Smrg * 14367117f1b4Smrg * It'll either be the immediate-mode execute dispatcher or the display list 14377117f1b4Smrg * compile dispatcher. 14387117f1b4Smrg * 14397117f1b4Smrg * \param ctx GL context. 14407117f1b4Smrg * 14417117f1b4Smrg * \return pointer to dispatch_table. 14427117f1b4Smrg * 14437117f1b4Smrg * Simply returns __GLcontextRec::CurrentDispatch. 14447117f1b4Smrg */ 14457117f1b4Smrgstruct _glapi_table * 14467117f1b4Smrg_mesa_get_dispatch(GLcontext *ctx) 14477117f1b4Smrg{ 14487117f1b4Smrg return ctx->CurrentDispatch; 14497117f1b4Smrg} 14507117f1b4Smrg 14517117f1b4Smrg/*@}*/ 14527117f1b4Smrg 14537117f1b4Smrg 14547117f1b4Smrg/**********************************************************************/ 14557117f1b4Smrg/** \name Miscellaneous functions */ 14567117f1b4Smrg/**********************************************************************/ 14577117f1b4Smrg/*@{*/ 14587117f1b4Smrg 14597117f1b4Smrg/** 14607117f1b4Smrg * Record an error. 14617117f1b4Smrg * 14627117f1b4Smrg * \param ctx GL context. 14637117f1b4Smrg * \param error error code. 14647117f1b4Smrg * 14657117f1b4Smrg * Records the given error code and call the driver's dd_function_table::Error 14667117f1b4Smrg * function if defined. 14677117f1b4Smrg * 14687117f1b4Smrg * \sa 14697117f1b4Smrg * This is called via _mesa_error(). 14707117f1b4Smrg */ 14717117f1b4Smrgvoid 14727117f1b4Smrg_mesa_record_error(GLcontext *ctx, GLenum error) 14737117f1b4Smrg{ 14747117f1b4Smrg if (!ctx) 14757117f1b4Smrg return; 14767117f1b4Smrg 14777117f1b4Smrg if (ctx->ErrorValue == GL_NO_ERROR) { 14787117f1b4Smrg ctx->ErrorValue = error; 14797117f1b4Smrg } 14807117f1b4Smrg 14817117f1b4Smrg /* Call device driver's error handler, if any. This is used on the Mac. */ 14827117f1b4Smrg if (ctx->Driver.Error) { 14837117f1b4Smrg ctx->Driver.Error(ctx); 14847117f1b4Smrg } 14857117f1b4Smrg} 14867117f1b4Smrg 14877117f1b4Smrg 14884a49301eSmrg/** 14894a49301eSmrg * Flush commands and wait for completion. 14904a49301eSmrg */ 14914a49301eSmrgvoid 14924a49301eSmrg_mesa_finish(GLcontext *ctx) 14934a49301eSmrg{ 14944a49301eSmrg FLUSH_CURRENT( ctx, 0 ); 14954a49301eSmrg if (ctx->Driver.Finish) { 14964a49301eSmrg ctx->Driver.Finish(ctx); 14974a49301eSmrg } 14984a49301eSmrg} 14994a49301eSmrg 15004a49301eSmrg 15014a49301eSmrg/** 15024a49301eSmrg * Flush commands. 15034a49301eSmrg */ 15044a49301eSmrgvoid 15054a49301eSmrg_mesa_flush(GLcontext *ctx) 15064a49301eSmrg{ 15074a49301eSmrg FLUSH_CURRENT( ctx, 0 ); 15084a49301eSmrg if (ctx->Driver.Flush) { 15094a49301eSmrg ctx->Driver.Flush(ctx); 15104a49301eSmrg } 15114a49301eSmrg} 15124a49301eSmrg 15134a49301eSmrg 15144a49301eSmrg 15157117f1b4Smrg/** 15167117f1b4Smrg * Execute glFinish(). 15177117f1b4Smrg * 15187117f1b4Smrg * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the 15197117f1b4Smrg * dd_function_table::Finish driver callback, if not NULL. 15207117f1b4Smrg */ 15217117f1b4Smrgvoid GLAPIENTRY 15227117f1b4Smrg_mesa_Finish(void) 15237117f1b4Smrg{ 15247117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 15257117f1b4Smrg ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); 15264a49301eSmrg _mesa_finish(ctx); 15277117f1b4Smrg} 15287117f1b4Smrg 15297117f1b4Smrg 15307117f1b4Smrg/** 15317117f1b4Smrg * Execute glFlush(). 15327117f1b4Smrg * 15337117f1b4Smrg * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the 15347117f1b4Smrg * dd_function_table::Flush driver callback, if not NULL. 15357117f1b4Smrg */ 15367117f1b4Smrgvoid GLAPIENTRY 15377117f1b4Smrg_mesa_Flush(void) 15387117f1b4Smrg{ 15397117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 15407117f1b4Smrg ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); 15414a49301eSmrg _mesa_flush(ctx); 15424a49301eSmrg} 15434a49301eSmrg 15444a49301eSmrg 15454a49301eSmrg/** 15464a49301eSmrg * Set mvp_with_dp4 flag. If a driver has a preference for DP4 over 15474a49301eSmrg * MUL/MAD, or vice versa, call this function to register that. 15484a49301eSmrg * Otherwise we default to MUL/MAD. 15494a49301eSmrg */ 15504a49301eSmrgvoid 15514a49301eSmrg_mesa_set_mvp_with_dp4( GLcontext *ctx, 15524a49301eSmrg GLboolean flag ) 15534a49301eSmrg{ 15544a49301eSmrg ctx->mvp_with_dp4 = flag; 15554a49301eSmrg} 15564a49301eSmrg 15574a49301eSmrg 15584a49301eSmrg 15594a49301eSmrg/** 15604a49301eSmrg * Prior to drawing anything with glBegin, glDrawArrays, etc. this function 15614a49301eSmrg * is called to see if it's valid to render. This involves checking that 15624a49301eSmrg * the current shader is valid and the framebuffer is complete. 15634a49301eSmrg * If an error is detected it'll be recorded here. 15644a49301eSmrg * \return GL_TRUE if OK to render, GL_FALSE if not 15654a49301eSmrg */ 15664a49301eSmrgGLboolean 15674a49301eSmrg_mesa_valid_to_render(GLcontext *ctx, const char *where) 15684a49301eSmrg{ 15694a49301eSmrg /* This depends on having up to date derived state (shaders) */ 15704a49301eSmrg if (ctx->NewState) 15714a49301eSmrg _mesa_update_state(ctx); 15724a49301eSmrg 15734a49301eSmrg if (ctx->Shader.CurrentProgram) { 15744a49301eSmrg /* using shaders */ 15754a49301eSmrg if (!ctx->Shader.CurrentProgram->LinkStatus) { 15764a49301eSmrg _mesa_error(ctx, GL_INVALID_OPERATION, 15774a49301eSmrg "%s(shader not linked), where"); 15784a49301eSmrg return GL_FALSE; 15794a49301eSmrg } 15804a49301eSmrg#if 0 /* not normally enabled */ 15814a49301eSmrg { 15824a49301eSmrg char errMsg[100]; 15834a49301eSmrg if (!_mesa_validate_shader_program(ctx, ctx->Shader.CurrentProgram, 15844a49301eSmrg errMsg)) { 15854a49301eSmrg _mesa_warning(ctx, "Shader program %u is invalid: %s", 15864a49301eSmrg ctx->Shader.CurrentProgram->Name, errMsg); 15874a49301eSmrg } 15884a49301eSmrg } 15894a49301eSmrg#endif 15904a49301eSmrg } 15914a49301eSmrg else { 15924a49301eSmrg if (ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) { 15934a49301eSmrg _mesa_error(ctx, GL_INVALID_OPERATION, 15944a49301eSmrg "%s(vertex program not valid)", where); 15954a49301eSmrg return GL_FALSE; 15964a49301eSmrg } 15974a49301eSmrg if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) { 15984a49301eSmrg _mesa_error(ctx, GL_INVALID_OPERATION, 15994a49301eSmrg "%s(fragment program not valid)", where); 16004a49301eSmrg return GL_FALSE; 16014a49301eSmrg } 16027117f1b4Smrg } 16034a49301eSmrg 16044a49301eSmrg if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { 16054a49301eSmrg _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT, 16064a49301eSmrg "%s(incomplete framebuffer)", where); 16074a49301eSmrg return GL_FALSE; 16084a49301eSmrg } 16094a49301eSmrg 16104a49301eSmrg#ifdef DEBUG 16114a49301eSmrg if (ctx->Shader.Flags & GLSL_LOG) { 16124a49301eSmrg struct gl_shader_program *shProg = ctx->Shader.CurrentProgram; 16134a49301eSmrg if (shProg) { 16144a49301eSmrg if (!shProg->_Used) { 16154a49301eSmrg /* This is the first time this shader is being used. 16164a49301eSmrg * Append shader's constants/uniforms to log file. 16174a49301eSmrg */ 16184a49301eSmrg GLuint i; 16194a49301eSmrg for (i = 0; i < shProg->NumShaders; i++) { 16204a49301eSmrg struct gl_shader *sh = shProg->Shaders[i]; 16214a49301eSmrg if (sh->Type == GL_VERTEX_SHADER) { 16224a49301eSmrg _mesa_append_uniforms_to_file(sh, 16234a49301eSmrg &shProg->VertexProgram->Base); 16244a49301eSmrg } 16254a49301eSmrg else if (sh->Type == GL_FRAGMENT_SHADER) { 16264a49301eSmrg _mesa_append_uniforms_to_file(sh, 16274a49301eSmrg &shProg->FragmentProgram->Base); 16284a49301eSmrg } 16294a49301eSmrg } 16304a49301eSmrg shProg->_Used = GL_TRUE; 16314a49301eSmrg } 16324a49301eSmrg } 16334a49301eSmrg } 16344a49301eSmrg#endif 16354a49301eSmrg 16364a49301eSmrg return GL_TRUE; 16377117f1b4Smrg} 16387117f1b4Smrg 16397117f1b4Smrg 16407117f1b4Smrg/*@}*/ 1641