context.c revision eabf4f72
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 "context.h"
904a49301eSmrg#include "cpuinfo.h"
917117f1b4Smrg#include "debug.h"
927117f1b4Smrg#include "depth.h"
937117f1b4Smrg#include "dlist.h"
947117f1b4Smrg#include "eval.h"
957117f1b4Smrg#include "extensions.h"
967117f1b4Smrg#include "fbobject.h"
977117f1b4Smrg#include "feedback.h"
987117f1b4Smrg#include "fog.h"
993464ebd5Sriastradh#include "formats.h"
1007117f1b4Smrg#include "framebuffer.h"
1017117f1b4Smrg#include "hint.h"
1027117f1b4Smrg#include "hash.h"
1037117f1b4Smrg#include "light.h"
1047117f1b4Smrg#include "lines.h"
1057117f1b4Smrg#include "macros.h"
1067117f1b4Smrg#include "matrix.h"
107c1f859d4Smrg#include "multisample.h"
1087117f1b4Smrg#include "pixel.h"
109c1f859d4Smrg#include "pixelstore.h"
1107117f1b4Smrg#include "points.h"
1117117f1b4Smrg#include "polygon.h"
1127117f1b4Smrg#include "queryobj.h"
1134a49301eSmrg#include "syncobj.h"
1147117f1b4Smrg#include "rastpos.h"
1154a49301eSmrg#include "remap.h"
116c1f859d4Smrg#include "scissor.h"
1174a49301eSmrg#include "shared.h"
1183464ebd5Sriastradh#include "shaderobj.h"
1197117f1b4Smrg#include "simple_list.h"
1207117f1b4Smrg#include "state.h"
1217117f1b4Smrg#include "stencil.h"
1224a49301eSmrg#include "texcompress_s3tc.h"
1237117f1b4Smrg#include "texstate.h"
1243464ebd5Sriastradh#include "transformfeedback.h"
1257117f1b4Smrg#include "mtypes.h"
1267117f1b4Smrg#include "varray.h"
1277117f1b4Smrg#include "version.h"
1284a49301eSmrg#include "viewport.h"
1297117f1b4Smrg#include "vtxfmt.h"
1303464ebd5Sriastradh#include "program/program.h"
1313464ebd5Sriastradh#include "program/prog_print.h"
1327117f1b4Smrg#if _HAVE_FULL_GL
1337117f1b4Smrg#include "math/m_matrix.h"
1347117f1b4Smrg#endif
1353464ebd5Sriastradh#include "main/dispatch.h" /* for _gloffset_COUNT */
1367117f1b4Smrg
1377117f1b4Smrg#ifdef USE_SPARC_ASM
1387117f1b4Smrg#include "sparc/sparc.h"
1397117f1b4Smrg#endif
1407117f1b4Smrg
1413464ebd5Sriastradh#include "glsl_parser_extras.h"
1423464ebd5Sriastradh#include <stdbool.h>
1433464ebd5Sriastradh
1443464ebd5Sriastradh
1457117f1b4Smrg#ifndef MESA_VERBOSE
1467117f1b4Smrgint MESA_VERBOSE = 0;
1477117f1b4Smrg#endif
1487117f1b4Smrg
1497117f1b4Smrg#ifndef MESA_DEBUG_FLAGS
1507117f1b4Smrgint MESA_DEBUG_FLAGS = 0;
1517117f1b4Smrg#endif
1527117f1b4Smrg
1537117f1b4Smrg
1547117f1b4Smrg/* ubyte -> float conversion */
1557117f1b4SmrgGLfloat _mesa_ubyte_to_float_color_tab[256];
1567117f1b4Smrg
1577117f1b4Smrg
1587117f1b4Smrg
1597117f1b4Smrg/**
1607117f1b4Smrg * Swap buffers notification callback.
1617117f1b4Smrg *
1624a49301eSmrg * \param ctx GL context.
1637117f1b4Smrg *
1647117f1b4Smrg * Called by window system just before swapping buffers.
1657117f1b4Smrg * We have to finish any pending rendering.
1667117f1b4Smrg */
1677117f1b4Smrgvoid
1683464ebd5Sriastradh_mesa_notifySwapBuffers(struct gl_context *ctx)
1697117f1b4Smrg{
1704a49301eSmrg   if (MESA_VERBOSE & VERBOSE_SWAPBUFFERS)
1714a49301eSmrg      _mesa_debug(ctx, "SwapBuffers\n");
1724a49301eSmrg   FLUSH_CURRENT( ctx, 0 );
1734a49301eSmrg   if (ctx->Driver.Flush) {
1744a49301eSmrg      ctx->Driver.Flush(ctx);
1754a49301eSmrg   }
1767117f1b4Smrg}
1777117f1b4Smrg
1787117f1b4Smrg
1797117f1b4Smrg/**********************************************************************/
1807117f1b4Smrg/** \name GL Visual allocation/destruction                            */
1817117f1b4Smrg/**********************************************************************/
1827117f1b4Smrg/*@{*/
1837117f1b4Smrg
1847117f1b4Smrg/**
1853464ebd5Sriastradh * Allocates a struct gl_config structure and initializes it via
1867117f1b4Smrg * _mesa_initialize_visual().
1877117f1b4Smrg *
1887117f1b4Smrg * \param dbFlag double buffering
1897117f1b4Smrg * \param stereoFlag stereo buffer
1907117f1b4Smrg * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
1917117f1b4Smrg * is acceptable but the actual depth type will be GLushort or GLuint as
1927117f1b4Smrg * needed.
1937117f1b4Smrg * \param stencilBits requested minimum bits per stencil buffer value
1943464ebd5Sriastradh * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number
1953464ebd5Sriastradh * of bits per color component in accum buffer.
1967117f1b4Smrg * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
1977117f1b4Smrg * \param redBits number of bits per color component in frame buffer for RGB(A)
1987117f1b4Smrg * mode.  We always use 8 in core Mesa though.
1997117f1b4Smrg * \param greenBits same as above.
2007117f1b4Smrg * \param blueBits same as above.
2017117f1b4Smrg * \param alphaBits same as above.
2027117f1b4Smrg * \param numSamples not really used.
2037117f1b4Smrg *
2043464ebd5Sriastradh * \return pointer to new struct gl_config or NULL if requested parameters
2053464ebd5Sriastradh * can't be met.
2067117f1b4Smrg *
2077117f1b4Smrg * \note Need to add params for level and numAuxBuffers (at least)
2087117f1b4Smrg */
2093464ebd5Sriastradhstruct gl_config *
210cdc920a0Smrg_mesa_create_visual( GLboolean dbFlag,
2117117f1b4Smrg                     GLboolean stereoFlag,
2127117f1b4Smrg                     GLint redBits,
2137117f1b4Smrg                     GLint greenBits,
2147117f1b4Smrg                     GLint blueBits,
2157117f1b4Smrg                     GLint alphaBits,
2167117f1b4Smrg                     GLint depthBits,
2177117f1b4Smrg                     GLint stencilBits,
2187117f1b4Smrg                     GLint accumRedBits,
2197117f1b4Smrg                     GLint accumGreenBits,
2207117f1b4Smrg                     GLint accumBlueBits,
2217117f1b4Smrg                     GLint accumAlphaBits,
2227117f1b4Smrg                     GLint numSamples )
2237117f1b4Smrg{
2243464ebd5Sriastradh   struct gl_config *vis = CALLOC_STRUCT(gl_config);
2257117f1b4Smrg   if (vis) {
226cdc920a0Smrg      if (!_mesa_initialize_visual(vis, dbFlag, stereoFlag,
2277117f1b4Smrg                                   redBits, greenBits, blueBits, alphaBits,
228cdc920a0Smrg                                   depthBits, stencilBits,
2297117f1b4Smrg                                   accumRedBits, accumGreenBits,
2307117f1b4Smrg                                   accumBlueBits, accumAlphaBits,
2317117f1b4Smrg                                   numSamples)) {
232cdc920a0Smrg         free(vis);
2337117f1b4Smrg         return NULL;
2347117f1b4Smrg      }
2357117f1b4Smrg   }
2367117f1b4Smrg   return vis;
2377117f1b4Smrg}
2387117f1b4Smrg
2393464ebd5Sriastradh
2407117f1b4Smrg/**
2413464ebd5Sriastradh * Makes some sanity checks and fills in the fields of the struct
2423464ebd5Sriastradh * gl_config object with the given parameters.  If the caller needs to
2433464ebd5Sriastradh * set additional fields, he should just probably init the whole
2443464ebd5Sriastradh * gl_config object himself.
2453464ebd5Sriastradh *
2467117f1b4Smrg * \return GL_TRUE on success, or GL_FALSE on failure.
2477117f1b4Smrg *
2487117f1b4Smrg * \sa _mesa_create_visual() above for the parameter description.
2497117f1b4Smrg */
2507117f1b4SmrgGLboolean
2513464ebd5Sriastradh_mesa_initialize_visual( struct gl_config *vis,
2527117f1b4Smrg                         GLboolean dbFlag,
2537117f1b4Smrg                         GLboolean stereoFlag,
2547117f1b4Smrg                         GLint redBits,
2557117f1b4Smrg                         GLint greenBits,
2567117f1b4Smrg                         GLint blueBits,
2577117f1b4Smrg                         GLint alphaBits,
2587117f1b4Smrg                         GLint depthBits,
2597117f1b4Smrg                         GLint stencilBits,
2607117f1b4Smrg                         GLint accumRedBits,
2617117f1b4Smrg                         GLint accumGreenBits,
2627117f1b4Smrg                         GLint accumBlueBits,
2637117f1b4Smrg                         GLint accumAlphaBits,
2647117f1b4Smrg                         GLint numSamples )
2657117f1b4Smrg{
2667117f1b4Smrg   assert(vis);
2677117f1b4Smrg
2687117f1b4Smrg   if (depthBits < 0 || depthBits > 32) {
2697117f1b4Smrg      return GL_FALSE;
2707117f1b4Smrg   }
2717117f1b4Smrg   if (stencilBits < 0 || stencilBits > STENCIL_BITS) {
2727117f1b4Smrg      return GL_FALSE;
2737117f1b4Smrg   }
2747117f1b4Smrg   assert(accumRedBits >= 0);
2757117f1b4Smrg   assert(accumGreenBits >= 0);
2767117f1b4Smrg   assert(accumBlueBits >= 0);
2777117f1b4Smrg   assert(accumAlphaBits >= 0);
2787117f1b4Smrg
279cdc920a0Smrg   vis->rgbMode          = GL_TRUE;
2807117f1b4Smrg   vis->doubleBufferMode = dbFlag;
2817117f1b4Smrg   vis->stereoMode       = stereoFlag;
2827117f1b4Smrg
2837117f1b4Smrg   vis->redBits          = redBits;
2847117f1b4Smrg   vis->greenBits        = greenBits;
2857117f1b4Smrg   vis->blueBits         = blueBits;
2867117f1b4Smrg   vis->alphaBits        = alphaBits;
2877117f1b4Smrg   vis->rgbBits          = redBits + greenBits + blueBits;
2887117f1b4Smrg
289cdc920a0Smrg   vis->indexBits      = 0;
2907117f1b4Smrg   vis->depthBits      = depthBits;
2917117f1b4Smrg   vis->stencilBits    = stencilBits;
2927117f1b4Smrg
2937117f1b4Smrg   vis->accumRedBits   = accumRedBits;
2947117f1b4Smrg   vis->accumGreenBits = accumGreenBits;
2957117f1b4Smrg   vis->accumBlueBits  = accumBlueBits;
2967117f1b4Smrg   vis->accumAlphaBits = accumAlphaBits;
2977117f1b4Smrg
2987117f1b4Smrg   vis->haveAccumBuffer   = accumRedBits > 0;
2997117f1b4Smrg   vis->haveDepthBuffer   = depthBits > 0;
3007117f1b4Smrg   vis->haveStencilBuffer = stencilBits > 0;
3017117f1b4Smrg
3027117f1b4Smrg   vis->numAuxBuffers = 0;
3037117f1b4Smrg   vis->level = 0;
3047117f1b4Smrg   vis->sampleBuffers = numSamples > 0 ? 1 : 0;
3057117f1b4Smrg   vis->samples = numSamples;
3067117f1b4Smrg
3077117f1b4Smrg   return GL_TRUE;
3087117f1b4Smrg}
3097117f1b4Smrg
3107117f1b4Smrg
3117117f1b4Smrg/**
3127117f1b4Smrg * Destroy a visual and free its memory.
3137117f1b4Smrg *
3147117f1b4Smrg * \param vis visual.
3157117f1b4Smrg *
3167117f1b4Smrg * Frees the visual structure.
3177117f1b4Smrg */
3187117f1b4Smrgvoid
3193464ebd5Sriastradh_mesa_destroy_visual( struct gl_config *vis )
3207117f1b4Smrg{
321cdc920a0Smrg   free(vis);
3227117f1b4Smrg}
3237117f1b4Smrg
3247117f1b4Smrg/*@}*/
3257117f1b4Smrg
3267117f1b4Smrg
3277117f1b4Smrg/**********************************************************************/
3287117f1b4Smrg/** \name Context allocation, initialization, destroying
3297117f1b4Smrg *
3307117f1b4Smrg * The purpose of the most initialization functions here is to provide the
3317117f1b4Smrg * default state values according to the OpenGL specification.
3327117f1b4Smrg */
3337117f1b4Smrg/**********************************************************************/
3347117f1b4Smrg/*@{*/
3357117f1b4Smrg
3364a49301eSmrg
3374a49301eSmrg/**
3384a49301eSmrg * This is lame.  gdb only seems to recognize enum types that are
3394a49301eSmrg * actually used somewhere.  We want to be able to print/use enum
3404a49301eSmrg * values such as TEXTURE_2D_INDEX in gdb.  But we don't actually use
3414a49301eSmrg * the gl_texture_index type anywhere.  Thus, this lame function.
3424a49301eSmrg */
3434a49301eSmrgstatic void
3444a49301eSmrgdummy_enum_func(void)
3454a49301eSmrg{
3463464ebd5Sriastradh   gl_buffer_index bi = BUFFER_FRONT_LEFT;
3473464ebd5Sriastradh   gl_face_index fi = FACE_POS_X;
3483464ebd5Sriastradh   gl_frag_attrib fa = FRAG_ATTRIB_WPOS;
3493464ebd5Sriastradh   gl_frag_result fr = FRAG_RESULT_DEPTH;
3503464ebd5Sriastradh   gl_texture_index ti = TEXTURE_2D_ARRAY_INDEX;
3513464ebd5Sriastradh   gl_vert_attrib va = VERT_ATTRIB_POS;
3523464ebd5Sriastradh   gl_vert_result vr = VERT_RESULT_HPOS;
3533464ebd5Sriastradh   gl_geom_attrib ga = GEOM_ATTRIB_POSITION;
3543464ebd5Sriastradh   gl_geom_result gr = GEOM_RESULT_POS;
3554a49301eSmrg
3564a49301eSmrg   (void) bi;
3574a49301eSmrg   (void) fi;
3584a49301eSmrg   (void) fa;
3594a49301eSmrg   (void) fr;
3604a49301eSmrg   (void) ti;
3614a49301eSmrg   (void) va;
3624a49301eSmrg   (void) vr;
3633464ebd5Sriastradh   (void) ga;
3643464ebd5Sriastradh   (void) gr;
3654a49301eSmrg}
3664a49301eSmrg
3674a49301eSmrg
3687117f1b4Smrg/**
3697117f1b4Smrg * One-time initialization mutex lock.
3707117f1b4Smrg *
3717117f1b4Smrg * \sa Used by one_time_init().
3727117f1b4Smrg */
3737117f1b4Smrg_glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
3747117f1b4Smrg
3753464ebd5Sriastradh
3763464ebd5Sriastradh
3777117f1b4Smrg/**
3787117f1b4Smrg * Calls all the various one-time-init functions in Mesa.
3797117f1b4Smrg *
3807117f1b4Smrg * While holding a global mutex lock, calls several initialization functions,
3817117f1b4Smrg * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
3827117f1b4Smrg * defined.
3837117f1b4Smrg *
3847117f1b4Smrg * \sa _math_init().
3857117f1b4Smrg */
386eabf4f72Sriastradhstatic GLbitfield api_init_mask = 0x0;
3877117f1b4Smrgstatic void
3883464ebd5Sriastradhone_time_init( struct gl_context *ctx )
3897117f1b4Smrg{
3903464ebd5Sriastradh
3917117f1b4Smrg   _glthread_LOCK_MUTEX(OneTimeLock);
3923464ebd5Sriastradh
3933464ebd5Sriastradh   /* truly one-time init */
3943464ebd5Sriastradh   if (!api_init_mask) {
3957117f1b4Smrg      GLuint i;
3967117f1b4Smrg
3977117f1b4Smrg      /* do some implementation tests */
3987117f1b4Smrg      assert( sizeof(GLbyte) == 1 );
3997117f1b4Smrg      assert( sizeof(GLubyte) == 1 );
4007117f1b4Smrg      assert( sizeof(GLshort) == 2 );
4017117f1b4Smrg      assert( sizeof(GLushort) == 2 );
4027117f1b4Smrg      assert( sizeof(GLint) == 4 );
4037117f1b4Smrg      assert( sizeof(GLuint) == 4 );
4047117f1b4Smrg
4054a49301eSmrg      _mesa_get_cpu_features();
4067117f1b4Smrg
4074a49301eSmrg      _mesa_init_sqrt_table();
4087117f1b4Smrg
4093464ebd5Sriastradh      /* context dependence is never a one-time thing... */
4103464ebd5Sriastradh      _mesa_init_get_hash(ctx);
4113464ebd5Sriastradh
4127117f1b4Smrg      for (i = 0; i < 256; i++) {
4137117f1b4Smrg         _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
4147117f1b4Smrg      }
4157117f1b4Smrg
4167117f1b4Smrg#if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
4173464ebd5Sriastradh      if (MESA_VERBOSE != 0) {
4183464ebd5Sriastradh	 _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
4193464ebd5Sriastradh		     MESA_VERSION_STRING, __DATE__, __TIME__);
4203464ebd5Sriastradh      }
4217117f1b4Smrg#endif
4227117f1b4Smrg
4233464ebd5Sriastradh#ifdef DEBUG
4243464ebd5Sriastradh      _mesa_test_formats();
4253464ebd5Sriastradh#endif
4267117f1b4Smrg   }
4273464ebd5Sriastradh
4283464ebd5Sriastradh   /* per-API one-time init */
4293464ebd5Sriastradh   if (!(api_init_mask & (1 << ctx->API))) {
4303464ebd5Sriastradh      /*
4313464ebd5Sriastradh       * This is fine as ES does not use the remap table, but it may not be
4323464ebd5Sriastradh       * future-proof.  We cannot always initialize the remap table because
4333464ebd5Sriastradh       * when an app is linked to libGLES*, there are not enough dynamic
4343464ebd5Sriastradh       * entries.
4353464ebd5Sriastradh       */
4363464ebd5Sriastradh      if (ctx->API == API_OPENGL)
4373464ebd5Sriastradh         _mesa_init_remap_table();
4383464ebd5Sriastradh   }
4393464ebd5Sriastradh
4403464ebd5Sriastradh   api_init_mask |= 1 << ctx->API;
4413464ebd5Sriastradh
4427117f1b4Smrg   _glthread_UNLOCK_MUTEX(OneTimeLock);
4437117f1b4Smrg
4444a49301eSmrg   dummy_enum_func();
4457117f1b4Smrg}
4467117f1b4Smrg
447eabf4f72Sriastradhstatic void __attribute__((__destructor__))
448eabf4f72Sriastradhone_time_fini(void)
449eabf4f72Sriastradh{
450eabf4f72Sriastradh  if (api_init_mask)
451eabf4f72Sriastradh    _mesa_destroy_shader_compiler();
452eabf4f72Sriastradh}
453eabf4f72Sriastradh
4547117f1b4Smrg
4557117f1b4Smrg/**
4567117f1b4Smrg * Initialize fields of gl_current_attrib (aka ctx->Current.*)
4577117f1b4Smrg */
4587117f1b4Smrgstatic void
4593464ebd5Sriastradh_mesa_init_current(struct gl_context *ctx)
4607117f1b4Smrg{
4617117f1b4Smrg   GLuint i;
4627117f1b4Smrg
4637117f1b4Smrg   /* Init all to (0,0,0,1) */
4644a49301eSmrg   for (i = 0; i < Elements(ctx->Current.Attrib); i++) {
4657117f1b4Smrg      ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
4667117f1b4Smrg   }
4677117f1b4Smrg
4687117f1b4Smrg   /* redo special cases: */
4697117f1b4Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 0.0 );
4707117f1b4Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
4717117f1b4Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
4727117f1b4Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
4737117f1b4Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
4747117f1b4Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
4757117f1b4Smrg}
4767117f1b4Smrg
4777117f1b4Smrg
4787117f1b4Smrg/**
4793464ebd5Sriastradh * Init vertex/fragment/geometry program limits.
4804a49301eSmrg * Important: drivers should override these with actual limits.
4817117f1b4Smrg */
4827117f1b4Smrgstatic void
4834a49301eSmrginit_program_limits(GLenum type, struct gl_program_constants *prog)
4847117f1b4Smrg{
4854a49301eSmrg   prog->MaxInstructions = MAX_PROGRAM_INSTRUCTIONS;
4864a49301eSmrg   prog->MaxAluInstructions = MAX_PROGRAM_INSTRUCTIONS;
4874a49301eSmrg   prog->MaxTexInstructions = MAX_PROGRAM_INSTRUCTIONS;
4884a49301eSmrg   prog->MaxTexIndirections = MAX_PROGRAM_INSTRUCTIONS;
4894a49301eSmrg   prog->MaxTemps = MAX_PROGRAM_TEMPS;
4904a49301eSmrg   prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS;
4914a49301eSmrg   prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
4923464ebd5Sriastradh   prog->MaxAddressOffset = MAX_PROGRAM_LOCAL_PARAMS;
4934a49301eSmrg
4943464ebd5Sriastradh   switch (type) {
4953464ebd5Sriastradh   case GL_VERTEX_PROGRAM_ARB:
4964a49301eSmrg      prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
4974a49301eSmrg      prog->MaxAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
4984a49301eSmrg      prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
4993464ebd5Sriastradh      prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
5003464ebd5Sriastradh      break;
5013464ebd5Sriastradh   case GL_FRAGMENT_PROGRAM_ARB:
5024a49301eSmrg      prog->MaxParameters = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
5034a49301eSmrg      prog->MaxAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
5044a49301eSmrg      prog->MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
5053464ebd5Sriastradh      prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
5063464ebd5Sriastradh      break;
5073464ebd5Sriastradh   case MESA_GEOMETRY_PROGRAM:
5083464ebd5Sriastradh      prog->MaxParameters = MAX_NV_VERTEX_PROGRAM_PARAMS;
5093464ebd5Sriastradh      prog->MaxAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
5103464ebd5Sriastradh      prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
5113464ebd5Sriastradh      prog->MaxUniformComponents = MAX_GEOMETRY_UNIFORM_COMPONENTS;
5123464ebd5Sriastradh      break;
5133464ebd5Sriastradh   default:
5143464ebd5Sriastradh      assert(0 && "Bad program type in init_program_limits()");
5154a49301eSmrg   }
5164a49301eSmrg
5174a49301eSmrg   /* Set the native limits to zero.  This implies that there is no native
5184a49301eSmrg    * support for shaders.  Let the drivers fill in the actual values.
5194a49301eSmrg    */
5204a49301eSmrg   prog->MaxNativeInstructions = 0;
5214a49301eSmrg   prog->MaxNativeAluInstructions = 0;
5224a49301eSmrg   prog->MaxNativeTexInstructions = 0;
5234a49301eSmrg   prog->MaxNativeTexIndirections = 0;
5244a49301eSmrg   prog->MaxNativeAttribs = 0;
5254a49301eSmrg   prog->MaxNativeTemps = 0;
5264a49301eSmrg   prog->MaxNativeAddressRegs = 0;
5274a49301eSmrg   prog->MaxNativeParameters = 0;
5283464ebd5Sriastradh
5293464ebd5Sriastradh   /* Set GLSL datatype range/precision info assuming IEEE float values.
5303464ebd5Sriastradh    * Drivers should override these defaults as needed.
5313464ebd5Sriastradh    */
5323464ebd5Sriastradh   prog->MediumFloat.RangeMin = 127;
5333464ebd5Sriastradh   prog->MediumFloat.RangeMax = 127;
5343464ebd5Sriastradh   prog->MediumFloat.Precision = 23;
5353464ebd5Sriastradh   prog->LowFloat = prog->HighFloat = prog->MediumFloat;
5363464ebd5Sriastradh
5373464ebd5Sriastradh   /* Assume ints are stored as floats for now, since this is the least-common
5383464ebd5Sriastradh    * denominator.  The OpenGL ES spec implies (page 132) that the precision
5393464ebd5Sriastradh    * of integer types should be 0.  Practically speaking, IEEE
5403464ebd5Sriastradh    * single-precision floating point values can only store integers in the
5413464ebd5Sriastradh    * range [-0x01000000, 0x01000000] without loss of precision.
5423464ebd5Sriastradh    */
5433464ebd5Sriastradh   prog->MediumInt.RangeMin = 24;
5443464ebd5Sriastradh   prog->MediumInt.RangeMax = 24;
5453464ebd5Sriastradh   prog->MediumInt.Precision = 0;
5463464ebd5Sriastradh   prog->LowInt = prog->HighInt = prog->MediumInt;
5477117f1b4Smrg}
5487117f1b4Smrg
5497117f1b4Smrg
5507117f1b4Smrg/**
5517117f1b4Smrg * Initialize fields of gl_constants (aka ctx->Const.*).
5527117f1b4Smrg * Use defaults from config.h.  The device drivers will often override
5537117f1b4Smrg * some of these values (such as number of texture units).
5547117f1b4Smrg */
5557117f1b4Smrgstatic void
5563464ebd5Sriastradh_mesa_init_constants(struct gl_context *ctx)
5577117f1b4Smrg{
5587117f1b4Smrg   assert(ctx);
5597117f1b4Smrg
5607117f1b4Smrg   /* Constants, may be overriden (usually only reduced) by device drivers */
5613464ebd5Sriastradh   ctx->Const.MaxTextureMbytes = MAX_TEXTURE_MBYTES;
5627117f1b4Smrg   ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
5637117f1b4Smrg   ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
5647117f1b4Smrg   ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
5657117f1b4Smrg   ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
566c1f859d4Smrg   ctx->Const.MaxArrayTextureLayers = MAX_ARRAY_TEXTURE_LAYERS;
5677117f1b4Smrg   ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
5687117f1b4Smrg   ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
5697117f1b4Smrg   ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureCoordUnits,
5707117f1b4Smrg                                     ctx->Const.MaxTextureImageUnits);
5717117f1b4Smrg   ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
5727117f1b4Smrg   ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
5733464ebd5Sriastradh   ctx->Const.MaxTextureBufferSize = 65536;
5747117f1b4Smrg   ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
5757117f1b4Smrg   ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
5767117f1b4Smrg   ctx->Const.MinPointSize = MIN_POINT_SIZE;
5777117f1b4Smrg   ctx->Const.MaxPointSize = MAX_POINT_SIZE;
5787117f1b4Smrg   ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
5797117f1b4Smrg   ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
5807117f1b4Smrg   ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
5817117f1b4Smrg   ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
5827117f1b4Smrg   ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
5837117f1b4Smrg   ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
5847117f1b4Smrg   ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
5857117f1b4Smrg   ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
5867117f1b4Smrg   ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
5877117f1b4Smrg   ctx->Const.MaxClipPlanes = MAX_CLIP_PLANES;
5887117f1b4Smrg   ctx->Const.MaxLights = MAX_LIGHTS;
5897117f1b4Smrg   ctx->Const.MaxShininess = 128.0;
5907117f1b4Smrg   ctx->Const.MaxSpotExponent = 128.0;
5917117f1b4Smrg   ctx->Const.MaxViewportWidth = MAX_WIDTH;
5927117f1b4Smrg   ctx->Const.MaxViewportHeight = MAX_HEIGHT;
5937117f1b4Smrg#if FEATURE_ARB_vertex_program
5944a49301eSmrg   init_program_limits(GL_VERTEX_PROGRAM_ARB, &ctx->Const.VertexProgram);
5957117f1b4Smrg#endif
5967117f1b4Smrg#if FEATURE_ARB_fragment_program
5974a49301eSmrg   init_program_limits(GL_FRAGMENT_PROGRAM_ARB, &ctx->Const.FragmentProgram);
5983464ebd5Sriastradh#endif
5993464ebd5Sriastradh#if FEATURE_ARB_geometry_shader4
6003464ebd5Sriastradh   init_program_limits(MESA_GEOMETRY_PROGRAM, &ctx->Const.GeometryProgram);
6017117f1b4Smrg#endif
6027117f1b4Smrg   ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
6037117f1b4Smrg   ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
6047117f1b4Smrg
6057117f1b4Smrg   /* CheckArrayBounds is overriden by drivers/x11 for X server */
6067117f1b4Smrg   ctx->Const.CheckArrayBounds = GL_FALSE;
6077117f1b4Smrg
6087117f1b4Smrg   /* GL_ARB_draw_buffers */
6097117f1b4Smrg   ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
6107117f1b4Smrg
6117117f1b4Smrg#if FEATURE_EXT_framebuffer_object
6127117f1b4Smrg   ctx->Const.MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
6137117f1b4Smrg   ctx->Const.MaxRenderbufferSize = MAX_WIDTH;
6147117f1b4Smrg#endif
6157117f1b4Smrg
6167117f1b4Smrg#if FEATURE_ARB_vertex_shader
6177117f1b4Smrg   ctx->Const.MaxVertexTextureImageUnits = MAX_VERTEX_TEXTURE_IMAGE_UNITS;
618cdc920a0Smrg   ctx->Const.MaxCombinedTextureImageUnits = MAX_COMBINED_TEXTURE_IMAGE_UNITS;
6197117f1b4Smrg   ctx->Const.MaxVarying = MAX_VARYING;
6207117f1b4Smrg#endif
6213464ebd5Sriastradh#if FEATURE_ARB_geometry_shader4
6223464ebd5Sriastradh   ctx->Const.MaxGeometryTextureImageUnits = MAX_GEOMETRY_TEXTURE_IMAGE_UNITS;
6233464ebd5Sriastradh   ctx->Const.MaxVertexVaryingComponents = MAX_VERTEX_VARYING_COMPONENTS;
6243464ebd5Sriastradh   ctx->Const.MaxGeometryVaryingComponents = MAX_GEOMETRY_VARYING_COMPONENTS;
6253464ebd5Sriastradh   ctx->Const.MaxGeometryOutputVertices = MAX_GEOMETRY_OUTPUT_VERTICES;
6263464ebd5Sriastradh   ctx->Const.MaxGeometryTotalOutputComponents = MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS;
6273464ebd5Sriastradh#endif
6283464ebd5Sriastradh
6293464ebd5Sriastradh   /* Shading language version */
6303464ebd5Sriastradh   if (ctx->API == API_OPENGL) {
6313464ebd5Sriastradh      ctx->Const.GLSLVersion = 120;
6323464ebd5Sriastradh   }
6333464ebd5Sriastradh   else if (ctx->API == API_OPENGLES2) {
6343464ebd5Sriastradh      ctx->Const.GLSLVersion = 100;
6353464ebd5Sriastradh   }
6363464ebd5Sriastradh   else if (ctx->API == API_OPENGLES) {
6373464ebd5Sriastradh      ctx->Const.GLSLVersion = 0; /* GLSL not supported */
6383464ebd5Sriastradh   }
6397117f1b4Smrg
6404a49301eSmrg   /* GL_ARB_framebuffer_object */
6414a49301eSmrg   ctx->Const.MaxSamples = 0;
6424a49301eSmrg
6434a49301eSmrg   /* GL_ARB_sync */
6444a49301eSmrg   ctx->Const.MaxServerWaitTimeout = (GLuint64) ~0;
6454a49301eSmrg
6464a49301eSmrg   /* GL_ATI_envmap_bumpmap */
6474a49301eSmrg   ctx->Const.SupportedBumpUnits = SUPPORTED_ATI_BUMP_UNITS;
6484a49301eSmrg
6494a49301eSmrg   /* GL_EXT_provoking_vertex */
6504a49301eSmrg   ctx->Const.QuadsFollowProvokingVertexConvention = GL_TRUE;
6513464ebd5Sriastradh
6523464ebd5Sriastradh   /* GL_EXT_transform_feedback */
6533464ebd5Sriastradh   ctx->Const.MaxTransformFeedbackSeparateAttribs = MAX_FEEDBACK_ATTRIBS;
6543464ebd5Sriastradh   ctx->Const.MaxTransformFeedbackSeparateComponents = 4 * MAX_FEEDBACK_ATTRIBS;
6553464ebd5Sriastradh   ctx->Const.MaxTransformFeedbackInterleavedComponents = 4 * MAX_FEEDBACK_ATTRIBS;
6563464ebd5Sriastradh
6573464ebd5Sriastradh   /* GL 3.2: hard-coded for now: */
6583464ebd5Sriastradh   ctx->Const.ProfileMask = GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
6593464ebd5Sriastradh
6603464ebd5Sriastradh   /** GL_EXT_gpu_shader4 */
6613464ebd5Sriastradh   ctx->Const.MinProgramTexelOffset = -8;
6623464ebd5Sriastradh   ctx->Const.MaxProgramTexelOffset = 7;
6633464ebd5Sriastradh
6643464ebd5Sriastradh   /* GL_ARB_robustness */
6653464ebd5Sriastradh   ctx->Const.ResetStrategy = GL_NO_RESET_NOTIFICATION_ARB;
6667117f1b4Smrg}
6677117f1b4Smrg
6687117f1b4Smrg
6697117f1b4Smrg/**
6707117f1b4Smrg * Do some sanity checks on the limits/constants for the given context.
6717117f1b4Smrg * Only called the first time a context is bound.
6727117f1b4Smrg */
6737117f1b4Smrgstatic void
6743464ebd5Sriastradhcheck_context_limits(struct gl_context *ctx)
6757117f1b4Smrg{
676cdc920a0Smrg   /* check that we don't exceed the size of various bitfields */
677cdc920a0Smrg   assert(VERT_RESULT_MAX <=
678cdc920a0Smrg	  (8 * sizeof(ctx->VertexProgram._Current->Base.OutputsWritten)));
679cdc920a0Smrg   assert(FRAG_ATTRIB_MAX <=
680cdc920a0Smrg	  (8 * sizeof(ctx->FragmentProgram._Current->Base.InputsRead)));
681cdc920a0Smrg
682cdc920a0Smrg   assert(MAX_COMBINED_TEXTURE_IMAGE_UNITS <= 8 * sizeof(GLbitfield));
683cdc920a0Smrg
684cdc920a0Smrg   /* shader-related checks */
685cdc920a0Smrg   assert(ctx->Const.FragmentProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
686cdc920a0Smrg   assert(ctx->Const.VertexProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
687cdc920a0Smrg
688cdc920a0Smrg   assert(MAX_NV_FRAGMENT_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
689cdc920a0Smrg   assert(MAX_NV_VERTEX_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
690cdc920a0Smrg   assert(MAX_NV_VERTEX_PROGRAM_INPUTS <= VERT_ATTRIB_MAX);
691cdc920a0Smrg   assert(MAX_NV_VERTEX_PROGRAM_OUTPUTS <= VERT_RESULT_MAX);
692cdc920a0Smrg
693cdc920a0Smrg   /* Texture unit checks */
694cdc920a0Smrg   assert(ctx->Const.MaxTextureImageUnits > 0);
6957117f1b4Smrg   assert(ctx->Const.MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS);
696cdc920a0Smrg   assert(ctx->Const.MaxTextureCoordUnits > 0);
6977117f1b4Smrg   assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS);
698cdc920a0Smrg   assert(ctx->Const.MaxTextureUnits > 0);
6997117f1b4Smrg   assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS);
7007117f1b4Smrg   assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS);
701cdc920a0Smrg   assert(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.MaxTextureImageUnits,
702cdc920a0Smrg                                             ctx->Const.MaxTextureCoordUnits));
703cdc920a0Smrg   assert(ctx->Const.MaxCombinedTextureImageUnits > 0);
704cdc920a0Smrg   assert(ctx->Const.MaxCombinedTextureImageUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
705cdc920a0Smrg   assert(ctx->Const.MaxTextureCoordUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
706c1f859d4Smrg   /* number of coord units cannot be greater than number of image units */
707c1f859d4Smrg   assert(ctx->Const.MaxTextureCoordUnits <= ctx->Const.MaxTextureImageUnits);
708c1f859d4Smrg
709cdc920a0Smrg
710cdc920a0Smrg   /* Texture size checks */
7114a49301eSmrg   assert(ctx->Const.MaxTextureLevels <= MAX_TEXTURE_LEVELS);
7124a49301eSmrg   assert(ctx->Const.Max3DTextureLevels <= MAX_3D_TEXTURE_LEVELS);
7134a49301eSmrg   assert(ctx->Const.MaxCubeTextureLevels <= MAX_CUBE_TEXTURE_LEVELS);
7144a49301eSmrg   assert(ctx->Const.MaxTextureRectSize <= MAX_TEXTURE_RECT_SIZE);
7157117f1b4Smrg
7167117f1b4Smrg   /* make sure largest texture image is <= MAX_WIDTH in size */
7174a49301eSmrg   assert((1 << (ctx->Const.MaxTextureLevels - 1)) <= MAX_WIDTH);
7184a49301eSmrg   assert((1 << (ctx->Const.MaxCubeTextureLevels - 1)) <= MAX_WIDTH);
7194a49301eSmrg   assert((1 << (ctx->Const.Max3DTextureLevels - 1)) <= MAX_WIDTH);
7204a49301eSmrg
721cdc920a0Smrg   /* Texture level checks */
722cdc920a0Smrg   assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
723cdc920a0Smrg   assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
724cdc920a0Smrg
725cdc920a0Smrg   /* Max texture size should be <= max viewport size (render to texture) */
726cdc920a0Smrg   assert((1 << (MAX_TEXTURE_LEVELS - 1)) <= MAX_WIDTH);
727cdc920a0Smrg
7284a49301eSmrg   assert(ctx->Const.MaxViewportWidth <= MAX_WIDTH);
7294a49301eSmrg   assert(ctx->Const.MaxViewportHeight <= MAX_WIDTH);
7307117f1b4Smrg
7317117f1b4Smrg   assert(ctx->Const.MaxDrawBuffers <= MAX_DRAW_BUFFERS);
7327117f1b4Smrg
7333464ebd5Sriastradh   /* if this fails, add more enum values to gl_buffer_index */
7343464ebd5Sriastradh   assert(BUFFER_COLOR0 + MAX_DRAW_BUFFERS <= BUFFER_COUNT);
7353464ebd5Sriastradh
7367117f1b4Smrg   /* XXX probably add more tests */
7377117f1b4Smrg}
7387117f1b4Smrg
7397117f1b4Smrg
7407117f1b4Smrg/**
7417117f1b4Smrg * Initialize the attribute groups in a GL context.
7427117f1b4Smrg *
7437117f1b4Smrg * \param ctx GL context.
7447117f1b4Smrg *
7457117f1b4Smrg * Initializes all the attributes, calling the respective <tt>init*</tt>
7467117f1b4Smrg * functions for the more complex data structures.
7477117f1b4Smrg */
7487117f1b4Smrgstatic GLboolean
7493464ebd5Sriastradhinit_attrib_groups(struct gl_context *ctx)
7507117f1b4Smrg{
7517117f1b4Smrg   assert(ctx);
7527117f1b4Smrg
7537117f1b4Smrg   /* Constants */
7547117f1b4Smrg   _mesa_init_constants( ctx );
7557117f1b4Smrg
7567117f1b4Smrg   /* Extensions */
7577117f1b4Smrg   _mesa_init_extensions( ctx );
7587117f1b4Smrg
7597117f1b4Smrg   /* Attribute Groups */
7607117f1b4Smrg   _mesa_init_accum( ctx );
7617117f1b4Smrg   _mesa_init_attrib( ctx );
7627117f1b4Smrg   _mesa_init_buffer_objects( ctx );
7637117f1b4Smrg   _mesa_init_color( ctx );
7647117f1b4Smrg   _mesa_init_current( ctx );
7657117f1b4Smrg   _mesa_init_depth( ctx );
7667117f1b4Smrg   _mesa_init_debug( ctx );
7677117f1b4Smrg   _mesa_init_display_list( ctx );
7687117f1b4Smrg   _mesa_init_eval( ctx );
769c1f859d4Smrg   _mesa_init_fbobjects( ctx );
7707117f1b4Smrg   _mesa_init_feedback( ctx );
7717117f1b4Smrg   _mesa_init_fog( ctx );
7727117f1b4Smrg   _mesa_init_hint( ctx );
7737117f1b4Smrg   _mesa_init_line( ctx );
7747117f1b4Smrg   _mesa_init_lighting( ctx );
7757117f1b4Smrg   _mesa_init_matrix( ctx );
7767117f1b4Smrg   _mesa_init_multisample( ctx );
7777117f1b4Smrg   _mesa_init_pixel( ctx );
778c1f859d4Smrg   _mesa_init_pixelstore( ctx );
7797117f1b4Smrg   _mesa_init_point( ctx );
7807117f1b4Smrg   _mesa_init_polygon( ctx );
7817117f1b4Smrg   _mesa_init_program( ctx );
7824a49301eSmrg   _mesa_init_queryobj( ctx );
7834a49301eSmrg   _mesa_init_sync( ctx );
7847117f1b4Smrg   _mesa_init_rastpos( ctx );
7857117f1b4Smrg   _mesa_init_scissor( ctx );
7867117f1b4Smrg   _mesa_init_shader_state( ctx );
7877117f1b4Smrg   _mesa_init_stencil( ctx );
7887117f1b4Smrg   _mesa_init_transform( ctx );
7893464ebd5Sriastradh   _mesa_init_transform_feedback( ctx );
7907117f1b4Smrg   _mesa_init_varray( ctx );
7917117f1b4Smrg   _mesa_init_viewport( ctx );
7927117f1b4Smrg
7937117f1b4Smrg   if (!_mesa_init_texture( ctx ))
7947117f1b4Smrg      return GL_FALSE;
7957117f1b4Smrg
7967117f1b4Smrg   _mesa_init_texture_s3tc( ctx );
7977117f1b4Smrg
7987117f1b4Smrg   /* Miscellaneous */
7997117f1b4Smrg   ctx->NewState = _NEW_ALL;
8007117f1b4Smrg   ctx->ErrorValue = (GLenum) GL_NO_ERROR;
8013464ebd5Sriastradh   ctx->ResetStatus = (GLenum) GL_NO_ERROR;
8024a49301eSmrg   ctx->varying_vp_inputs = ~0;
8037117f1b4Smrg
8047117f1b4Smrg   return GL_TRUE;
8057117f1b4Smrg}
8067117f1b4Smrg
8077117f1b4Smrg
808c1f859d4Smrg/**
809c1f859d4Smrg * Update default objects in a GL context with respect to shared state.
810c1f859d4Smrg *
811c1f859d4Smrg * \param ctx GL context.
812c1f859d4Smrg *
813c1f859d4Smrg * Removes references to old default objects, (texture objects, program
814c1f859d4Smrg * objects, etc.) and changes to reference those from the current shared
815c1f859d4Smrg * state.
816c1f859d4Smrg */
817c1f859d4Smrgstatic GLboolean
8183464ebd5Sriastradhupdate_default_objects(struct gl_context *ctx)
819c1f859d4Smrg{
820c1f859d4Smrg   assert(ctx);
821c1f859d4Smrg
822c1f859d4Smrg   _mesa_update_default_objects_program(ctx);
823c1f859d4Smrg   _mesa_update_default_objects_texture(ctx);
824c1f859d4Smrg   _mesa_update_default_objects_buffer_objects(ctx);
825c1f859d4Smrg
826c1f859d4Smrg   return GL_TRUE;
827c1f859d4Smrg}
828c1f859d4Smrg
829c1f859d4Smrg
8307117f1b4Smrg/**
8317117f1b4Smrg * This is the default function we plug into all dispatch table slots
8327117f1b4Smrg * This helps prevents a segfault when someone calls a GL function without
8337117f1b4Smrg * first checking if the extension's supported.
8347117f1b4Smrg */
8357117f1b4Smrgstatic int
8367117f1b4Smrggeneric_nop(void)
8377117f1b4Smrg{
838c1f859d4Smrg   _mesa_warning(NULL, "User called no-op dispatch function (an unsupported extension function?)");
8397117f1b4Smrg   return 0;
8407117f1b4Smrg}
8417117f1b4Smrg
8427117f1b4Smrg
8437117f1b4Smrg/**
8447117f1b4Smrg * Allocate and initialize a new dispatch table.
8457117f1b4Smrg */
8463464ebd5Sriastradhstruct _glapi_table *
8473464ebd5Sriastradh_mesa_alloc_dispatch_table(int size)
8487117f1b4Smrg{
8497117f1b4Smrg   /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
8507117f1b4Smrg    * In practice, this'll be the same for stand-alone Mesa.  But for DRI
8517117f1b4Smrg    * Mesa we do this to accomodate different versions of libGL and various
8527117f1b4Smrg    * DRI drivers.
8537117f1b4Smrg    */
8543464ebd5Sriastradh   GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
8553464ebd5Sriastradh   struct _glapi_table *table;
8563464ebd5Sriastradh
8573464ebd5Sriastradh   /* should never happen, but just in case */
8583464ebd5Sriastradh   numEntries = MAX2(numEntries, size);
8593464ebd5Sriastradh
8603464ebd5Sriastradh   table = (struct _glapi_table *) malloc(numEntries * sizeof(_glapi_proc));
8617117f1b4Smrg   if (table) {
8627117f1b4Smrg      _glapi_proc *entry = (_glapi_proc *) table;
8637117f1b4Smrg      GLint i;
8647117f1b4Smrg      for (i = 0; i < numEntries; i++) {
8657117f1b4Smrg         entry[i] = (_glapi_proc) generic_nop;
8667117f1b4Smrg      }
8677117f1b4Smrg   }
8687117f1b4Smrg   return table;
8697117f1b4Smrg}
8707117f1b4Smrg
8717117f1b4Smrg
8727117f1b4Smrg/**
8733464ebd5Sriastradh * Initialize a struct gl_context struct (rendering context).
8747117f1b4Smrg *
8757117f1b4Smrg * This includes allocating all the other structs and arrays which hang off of
8767117f1b4Smrg * the context by pointers.
8777117f1b4Smrg * Note that the driver needs to pass in its dd_function_table here since
8787117f1b4Smrg * we need to at least call driverFunctions->NewTextureObject to create the
8797117f1b4Smrg * default texture objects.
8807117f1b4Smrg *
8817117f1b4Smrg * Called by _mesa_create_context().
8827117f1b4Smrg *
8837117f1b4Smrg * Performs the imports and exports callback tables initialization, and
8847117f1b4Smrg * miscellaneous one-time initializations. If no shared context is supplied one
8857117f1b4Smrg * is allocated, and increase its reference count.  Setups the GL API dispatch
8867117f1b4Smrg * tables.  Initialize the TNL module. Sets the maximum Z buffer depth.
8877117f1b4Smrg * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
8887117f1b4Smrg * for debug flags.
8897117f1b4Smrg *
8907117f1b4Smrg * \param ctx the context to initialize
8913464ebd5Sriastradh * \param api the GL API type to create the context for
8927117f1b4Smrg * \param visual describes the visual attributes for this context
8937117f1b4Smrg * \param share_list points to context to share textures, display lists,
8947117f1b4Smrg *        etc with, or NULL
8957117f1b4Smrg * \param driverFunctions table of device driver functions for this context
8967117f1b4Smrg *        to use
8977117f1b4Smrg * \param driverContext pointer to driver-specific context data
8987117f1b4Smrg */
8997117f1b4SmrgGLboolean
9003464ebd5Sriastradh_mesa_initialize_context(struct gl_context *ctx,
9013464ebd5Sriastradh                         gl_api api,
9023464ebd5Sriastradh                         const struct gl_config *visual,
9033464ebd5Sriastradh                         struct gl_context *share_list,
9047117f1b4Smrg                         const struct dd_function_table *driverFunctions,
9057117f1b4Smrg                         void *driverContext)
9067117f1b4Smrg{
9074a49301eSmrg   struct gl_shared_state *shared;
9083464ebd5Sriastradh   int i;
9094a49301eSmrg
9104a49301eSmrg   /*ASSERT(driverContext);*/
9117117f1b4Smrg   assert(driverFunctions->NewTextureObject);
9127117f1b4Smrg   assert(driverFunctions->FreeTexImageData);
9137117f1b4Smrg
9143464ebd5Sriastradh   ctx->API = api;
9157117f1b4Smrg   ctx->Visual = *visual;
9167117f1b4Smrg   ctx->DrawBuffer = NULL;
9177117f1b4Smrg   ctx->ReadBuffer = NULL;
9187117f1b4Smrg   ctx->WinSysDrawBuffer = NULL;
9197117f1b4Smrg   ctx->WinSysReadBuffer = NULL;
9207117f1b4Smrg
9213464ebd5Sriastradh   /* misc one-time initializations */
9223464ebd5Sriastradh   one_time_init(ctx);
9233464ebd5Sriastradh
9247117f1b4Smrg   /* Plug in driver functions and context pointer here.
9257117f1b4Smrg    * This is important because when we call alloc_shared_state() below
9267117f1b4Smrg    * we'll call ctx->Driver.NewTextureObject() to create the default
9277117f1b4Smrg    * textures.
9287117f1b4Smrg    */
9297117f1b4Smrg   ctx->Driver = *driverFunctions;
9307117f1b4Smrg   ctx->DriverCtx = driverContext;
9317117f1b4Smrg
9327117f1b4Smrg   if (share_list) {
9337117f1b4Smrg      /* share state with another context */
9344a49301eSmrg      shared = share_list->Shared;
9357117f1b4Smrg   }
9367117f1b4Smrg   else {
9377117f1b4Smrg      /* allocate new, unshared state */
9384a49301eSmrg      shared = _mesa_alloc_shared_state(ctx);
9394a49301eSmrg      if (!shared)
9407117f1b4Smrg         return GL_FALSE;
9417117f1b4Smrg   }
9424a49301eSmrg
9434a49301eSmrg   _glthread_LOCK_MUTEX(shared->Mutex);
9444a49301eSmrg   ctx->Shared = shared;
9454a49301eSmrg   shared->RefCount++;
9464a49301eSmrg   _glthread_UNLOCK_MUTEX(shared->Mutex);
9477117f1b4Smrg
9487117f1b4Smrg   if (!init_attrib_groups( ctx )) {
9494a49301eSmrg      _mesa_release_shared_state(ctx, ctx->Shared);
9507117f1b4Smrg      return GL_FALSE;
9517117f1b4Smrg   }
9527117f1b4Smrg
9533464ebd5Sriastradh#if FEATURE_dispatch
9547117f1b4Smrg   /* setup the API dispatch tables */
9553464ebd5Sriastradh   switch (ctx->API) {
9563464ebd5Sriastradh#if FEATURE_GL
9573464ebd5Sriastradh   case API_OPENGL:
9583464ebd5Sriastradh      ctx->Exec = _mesa_create_exec_table();
9593464ebd5Sriastradh      break;
9603464ebd5Sriastradh#endif
9613464ebd5Sriastradh#if FEATURE_ES1
9623464ebd5Sriastradh   case API_OPENGLES:
9633464ebd5Sriastradh      ctx->Exec = _mesa_create_exec_table_es1();
9643464ebd5Sriastradh      break;
9653464ebd5Sriastradh#endif
9663464ebd5Sriastradh#if FEATURE_ES2
9673464ebd5Sriastradh   case API_OPENGLES2:
9683464ebd5Sriastradh      ctx->Exec = _mesa_create_exec_table_es2();
9693464ebd5Sriastradh      break;
9703464ebd5Sriastradh#endif
9713464ebd5Sriastradh   default:
9723464ebd5Sriastradh      _mesa_problem(ctx, "unknown or unsupported API");
9733464ebd5Sriastradh      break;
9743464ebd5Sriastradh   }
9753464ebd5Sriastradh
9763464ebd5Sriastradh   if (!ctx->Exec) {
9774a49301eSmrg      _mesa_release_shared_state(ctx, ctx->Shared);
9784a49301eSmrg      return GL_FALSE;
9797117f1b4Smrg   }
980c1f859d4Smrg#endif
9817117f1b4Smrg   ctx->CurrentDispatch = ctx->Exec;
9824a49301eSmrg
9837117f1b4Smrg   ctx->FragmentProgram._MaintainTexEnvProgram
9847117f1b4Smrg      = (_mesa_getenv("MESA_TEX_PROG") != NULL);
9857117f1b4Smrg
9867117f1b4Smrg   ctx->VertexProgram._MaintainTnlProgram
9877117f1b4Smrg      = (_mesa_getenv("MESA_TNL_PROG") != NULL);
9887117f1b4Smrg   if (ctx->VertexProgram._MaintainTnlProgram) {
9897117f1b4Smrg      /* this is required... */
9907117f1b4Smrg      ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
9917117f1b4Smrg   }
9927117f1b4Smrg
9933464ebd5Sriastradh   /* Mesa core handles all the formats that mesa core knows about.
9943464ebd5Sriastradh    * Drivers will want to override this list with just the formats
9953464ebd5Sriastradh    * they can handle, and confirm that appropriate fallbacks exist in
9963464ebd5Sriastradh    * _mesa_choose_tex_format().
9973464ebd5Sriastradh    */
9983464ebd5Sriastradh   memset(&ctx->TextureFormatSupported, GL_TRUE,
9993464ebd5Sriastradh	  sizeof(ctx->TextureFormatSupported));
10003464ebd5Sriastradh
10013464ebd5Sriastradh   switch (ctx->API) {
10023464ebd5Sriastradh   case API_OPENGL:
10033464ebd5Sriastradh#if FEATURE_dlist
10043464ebd5Sriastradh      ctx->Save = _mesa_create_save_table();
10053464ebd5Sriastradh      if (!ctx->Save) {
10063464ebd5Sriastradh	 _mesa_release_shared_state(ctx, ctx->Shared);
10073464ebd5Sriastradh	 free(ctx->Exec);
10083464ebd5Sriastradh	 return GL_FALSE;
10093464ebd5Sriastradh      }
10103464ebd5Sriastradh
10113464ebd5Sriastradh      _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
1012c1f859d4Smrg#endif
10133464ebd5Sriastradh      break;
10143464ebd5Sriastradh   case API_OPENGLES:
10153464ebd5Sriastradh      /**
10163464ebd5Sriastradh       * GL_OES_texture_cube_map says
10173464ebd5Sriastradh       * "Initially all texture generation modes are set to REFLECTION_MAP_OES"
10183464ebd5Sriastradh       */
10193464ebd5Sriastradh      for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
10203464ebd5Sriastradh	 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
10213464ebd5Sriastradh	 texUnit->GenS.Mode = GL_REFLECTION_MAP_NV;
10223464ebd5Sriastradh	 texUnit->GenT.Mode = GL_REFLECTION_MAP_NV;
10233464ebd5Sriastradh	 texUnit->GenR.Mode = GL_REFLECTION_MAP_NV;
10243464ebd5Sriastradh	 texUnit->GenS._ModeBit = TEXGEN_REFLECTION_MAP_NV;
10253464ebd5Sriastradh	 texUnit->GenT._ModeBit = TEXGEN_REFLECTION_MAP_NV;
10263464ebd5Sriastradh	 texUnit->GenR._ModeBit = TEXGEN_REFLECTION_MAP_NV;
10273464ebd5Sriastradh      }
10283464ebd5Sriastradh      break;
10293464ebd5Sriastradh   case API_OPENGLES2:
10303464ebd5Sriastradh      ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
10313464ebd5Sriastradh      ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
10323464ebd5Sriastradh      ctx->Point.PointSprite = GL_TRUE;  /* always on for ES 2.x */
10333464ebd5Sriastradh      break;
10343464ebd5Sriastradh   }
1035c1f859d4Smrg
10367117f1b4Smrg   ctx->FirstTimeCurrent = GL_TRUE;
10377117f1b4Smrg
10387117f1b4Smrg   return GL_TRUE;
10397117f1b4Smrg}
10407117f1b4Smrg
10417117f1b4Smrg
10427117f1b4Smrg/**
10433464ebd5Sriastradh * Allocate and initialize a struct gl_context structure.
10447117f1b4Smrg * Note that the driver needs to pass in its dd_function_table here since
10457117f1b4Smrg * we need to at least call driverFunctions->NewTextureObject to initialize
10467117f1b4Smrg * the rendering context.
10477117f1b4Smrg *
10483464ebd5Sriastradh * \param api the GL API type to create the context for
10493464ebd5Sriastradh * \param visual a struct gl_config pointer (we copy the struct contents)
10507117f1b4Smrg * \param share_list another context to share display lists with or NULL
10517117f1b4Smrg * \param driverFunctions points to the dd_function_table into which the
10527117f1b4Smrg *        driver has plugged in all its special functions.
10534a49301eSmrg * \param driverContext points to the device driver's private context state
10547117f1b4Smrg *
10553464ebd5Sriastradh * \return pointer to a new __struct gl_contextRec or NULL if error.
10567117f1b4Smrg */
10573464ebd5Sriastradhstruct gl_context *
10583464ebd5Sriastradh_mesa_create_context(gl_api api,
10593464ebd5Sriastradh                     const struct gl_config *visual,
10603464ebd5Sriastradh                     struct gl_context *share_list,
10617117f1b4Smrg                     const struct dd_function_table *driverFunctions,
10627117f1b4Smrg                     void *driverContext)
10637117f1b4Smrg{
10643464ebd5Sriastradh   struct gl_context *ctx;
10657117f1b4Smrg
10667117f1b4Smrg   ASSERT(visual);
10674a49301eSmrg   /*ASSERT(driverContext);*/
10687117f1b4Smrg
10693464ebd5Sriastradh   ctx = (struct gl_context *) calloc(1, sizeof(struct gl_context));
10707117f1b4Smrg   if (!ctx)
10717117f1b4Smrg      return NULL;
10727117f1b4Smrg
10733464ebd5Sriastradh   if (_mesa_initialize_context(ctx, api, visual, share_list,
10747117f1b4Smrg                                driverFunctions, driverContext)) {
10757117f1b4Smrg      return ctx;
10767117f1b4Smrg   }
10777117f1b4Smrg   else {
1078cdc920a0Smrg      free(ctx);
10797117f1b4Smrg      return NULL;
10807117f1b4Smrg   }
10817117f1b4Smrg}
10827117f1b4Smrg
10837117f1b4Smrg
10847117f1b4Smrg/**
10857117f1b4Smrg * Free the data associated with the given context.
10867117f1b4Smrg *
10873464ebd5Sriastradh * But doesn't free the struct gl_context struct itself.
10887117f1b4Smrg *
10897117f1b4Smrg * \sa _mesa_initialize_context() and init_attrib_groups().
10907117f1b4Smrg */
10917117f1b4Smrgvoid
10923464ebd5Sriastradh_mesa_free_context_data( struct gl_context *ctx )
10937117f1b4Smrg{
10947117f1b4Smrg   if (!_mesa_get_current_context()){
10957117f1b4Smrg      /* No current context, but we may need one in order to delete
10967117f1b4Smrg       * texture objs, etc.  So temporarily bind the context now.
10977117f1b4Smrg       */
10987117f1b4Smrg      _mesa_make_current(ctx, NULL, NULL);
10997117f1b4Smrg   }
11007117f1b4Smrg
11017117f1b4Smrg   /* unreference WinSysDraw/Read buffers */
11024a49301eSmrg   _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
11034a49301eSmrg   _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
11044a49301eSmrg   _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
11054a49301eSmrg   _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
11067117f1b4Smrg
1107c1f859d4Smrg   _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
1108c1f859d4Smrg   _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
1109c1f859d4Smrg   _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram, NULL);
1110c1f859d4Smrg
1111c1f859d4Smrg   _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
1112c1f859d4Smrg   _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
1113c1f859d4Smrg   _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, NULL);
1114c1f859d4Smrg
11157117f1b4Smrg   _mesa_free_attrib_data(ctx);
11164a49301eSmrg   _mesa_free_buffer_objects(ctx);
11177117f1b4Smrg   _mesa_free_lighting_data( ctx );
11187117f1b4Smrg   _mesa_free_eval_data( ctx );
11197117f1b4Smrg   _mesa_free_texture_data( ctx );
11207117f1b4Smrg   _mesa_free_matrix_data( ctx );
11217117f1b4Smrg   _mesa_free_viewport_data( ctx );
11227117f1b4Smrg   _mesa_free_program_data(ctx);
11237117f1b4Smrg   _mesa_free_shader_state(ctx);
11244a49301eSmrg   _mesa_free_queryobj_data(ctx);
11254a49301eSmrg   _mesa_free_sync_data(ctx);
11264a49301eSmrg   _mesa_free_varray_data(ctx);
11273464ebd5Sriastradh   _mesa_free_transform_feedback(ctx);
11284a49301eSmrg
11294a49301eSmrg   _mesa_delete_array_object(ctx, ctx->Array.DefaultArrayObj);
11304a49301eSmrg
11314a49301eSmrg#if FEATURE_ARB_pixel_buffer_object
11324a49301eSmrg   _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL);
11334a49301eSmrg   _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL);
11344a49301eSmrg   _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL);
1135c1f859d4Smrg#endif
11367117f1b4Smrg
11377117f1b4Smrg#if FEATURE_ARB_vertex_buffer_object
11384a49301eSmrg   _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
11394a49301eSmrg   _mesa_reference_buffer_object(ctx, &ctx->Array.ElementArrayBufferObj, NULL);
11407117f1b4Smrg#endif
11417117f1b4Smrg
11427117f1b4Smrg   /* free dispatch tables */
1143cdc920a0Smrg   free(ctx->Exec);
1144cdc920a0Smrg   free(ctx->Save);
11457117f1b4Smrg
11467117f1b4Smrg   /* Shared context state (display lists, textures, etc) */
11474a49301eSmrg   _mesa_release_shared_state( ctx, ctx->Shared );
11484a49301eSmrg
11494a49301eSmrg   /* needs to be after freeing shared state */
11504a49301eSmrg   _mesa_free_display_list_data(ctx);
11517117f1b4Smrg
11527117f1b4Smrg   if (ctx->Extensions.String)
1153cdc920a0Smrg      free((void *) ctx->Extensions.String);
1154cdc920a0Smrg
1155cdc920a0Smrg   if (ctx->VersionString)
1156cdc920a0Smrg      free(ctx->VersionString);
11577117f1b4Smrg
11587117f1b4Smrg   /* unbind the context if it's currently bound */
11597117f1b4Smrg   if (ctx == _mesa_get_current_context()) {
11607117f1b4Smrg      _mesa_make_current(NULL, NULL, NULL);
11617117f1b4Smrg   }
11627117f1b4Smrg}
11637117f1b4Smrg
11647117f1b4Smrg
11657117f1b4Smrg/**
11663464ebd5Sriastradh * Destroy a struct gl_context structure.
11677117f1b4Smrg *
11687117f1b4Smrg * \param ctx GL context.
11697117f1b4Smrg *
11703464ebd5Sriastradh * Calls _mesa_free_context_data() and frees the gl_context object itself.
11717117f1b4Smrg */
11727117f1b4Smrgvoid
11733464ebd5Sriastradh_mesa_destroy_context( struct gl_context *ctx )
11747117f1b4Smrg{
11757117f1b4Smrg   if (ctx) {
11767117f1b4Smrg      _mesa_free_context_data(ctx);
1177cdc920a0Smrg      free( (void *) ctx );
11787117f1b4Smrg   }
11797117f1b4Smrg}
11807117f1b4Smrg
11817117f1b4Smrg
11827117f1b4Smrg#if _HAVE_FULL_GL
11837117f1b4Smrg/**
11847117f1b4Smrg * Copy attribute groups from one context to another.
11857117f1b4Smrg *
11867117f1b4Smrg * \param src source context
11877117f1b4Smrg * \param dst destination context
11887117f1b4Smrg * \param mask bitwise OR of GL_*_BIT flags
11897117f1b4Smrg *
11907117f1b4Smrg * According to the bits specified in \p mask, copies the corresponding
11917117f1b4Smrg * attributes from \p src into \p dst.  For many of the attributes a simple \c
11927117f1b4Smrg * memcpy is not enough due to the existence of internal pointers in their data
11937117f1b4Smrg * structures.
11947117f1b4Smrg */
11957117f1b4Smrgvoid
11963464ebd5Sriastradh_mesa_copy_context( const struct gl_context *src, struct gl_context *dst,
11973464ebd5Sriastradh                    GLuint mask )
11987117f1b4Smrg{
11997117f1b4Smrg   if (mask & GL_ACCUM_BUFFER_BIT) {
12007117f1b4Smrg      /* OK to memcpy */
12017117f1b4Smrg      dst->Accum = src->Accum;
12027117f1b4Smrg   }
12037117f1b4Smrg   if (mask & GL_COLOR_BUFFER_BIT) {
12047117f1b4Smrg      /* OK to memcpy */
12057117f1b4Smrg      dst->Color = src->Color;
12067117f1b4Smrg   }
12077117f1b4Smrg   if (mask & GL_CURRENT_BIT) {
12087117f1b4Smrg      /* OK to memcpy */
12097117f1b4Smrg      dst->Current = src->Current;
12107117f1b4Smrg   }
12117117f1b4Smrg   if (mask & GL_DEPTH_BUFFER_BIT) {
12127117f1b4Smrg      /* OK to memcpy */
12137117f1b4Smrg      dst->Depth = src->Depth;
12147117f1b4Smrg   }
12157117f1b4Smrg   if (mask & GL_ENABLE_BIT) {
12167117f1b4Smrg      /* no op */
12177117f1b4Smrg   }
12187117f1b4Smrg   if (mask & GL_EVAL_BIT) {
12197117f1b4Smrg      /* OK to memcpy */
12207117f1b4Smrg      dst->Eval = src->Eval;
12217117f1b4Smrg   }
12227117f1b4Smrg   if (mask & GL_FOG_BIT) {
12237117f1b4Smrg      /* OK to memcpy */
12247117f1b4Smrg      dst->Fog = src->Fog;
12257117f1b4Smrg   }
12267117f1b4Smrg   if (mask & GL_HINT_BIT) {
12277117f1b4Smrg      /* OK to memcpy */
12287117f1b4Smrg      dst->Hint = src->Hint;
12297117f1b4Smrg   }
12307117f1b4Smrg   if (mask & GL_LIGHTING_BIT) {
12317117f1b4Smrg      GLuint i;
12327117f1b4Smrg      /* begin with memcpy */
12337117f1b4Smrg      dst->Light = src->Light;
12347117f1b4Smrg      /* fixup linked lists to prevent pointer insanity */
12357117f1b4Smrg      make_empty_list( &(dst->Light.EnabledList) );
12367117f1b4Smrg      for (i = 0; i < MAX_LIGHTS; i++) {
12377117f1b4Smrg         if (dst->Light.Light[i].Enabled) {
12387117f1b4Smrg            insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
12397117f1b4Smrg         }
12407117f1b4Smrg      }
12417117f1b4Smrg   }
12427117f1b4Smrg   if (mask & GL_LINE_BIT) {
12437117f1b4Smrg      /* OK to memcpy */
12447117f1b4Smrg      dst->Line = src->Line;
12457117f1b4Smrg   }
12467117f1b4Smrg   if (mask & GL_LIST_BIT) {
12477117f1b4Smrg      /* OK to memcpy */
12487117f1b4Smrg      dst->List = src->List;
12497117f1b4Smrg   }
12507117f1b4Smrg   if (mask & GL_PIXEL_MODE_BIT) {
12517117f1b4Smrg      /* OK to memcpy */
12527117f1b4Smrg      dst->Pixel = src->Pixel;
12537117f1b4Smrg   }
12547117f1b4Smrg   if (mask & GL_POINT_BIT) {
12557117f1b4Smrg      /* OK to memcpy */
12567117f1b4Smrg      dst->Point = src->Point;
12577117f1b4Smrg   }
12587117f1b4Smrg   if (mask & GL_POLYGON_BIT) {
12597117f1b4Smrg      /* OK to memcpy */
12607117f1b4Smrg      dst->Polygon = src->Polygon;
12617117f1b4Smrg   }
12627117f1b4Smrg   if (mask & GL_POLYGON_STIPPLE_BIT) {
1263cdc920a0Smrg      /* Use loop instead of memcpy due to problem with Portland Group's
12647117f1b4Smrg       * C compiler.  Reported by John Stone.
12657117f1b4Smrg       */
12667117f1b4Smrg      GLuint i;
12677117f1b4Smrg      for (i = 0; i < 32; i++) {
12687117f1b4Smrg         dst->PolygonStipple[i] = src->PolygonStipple[i];
12697117f1b4Smrg      }
12707117f1b4Smrg   }
12717117f1b4Smrg   if (mask & GL_SCISSOR_BIT) {
12727117f1b4Smrg      /* OK to memcpy */
12737117f1b4Smrg      dst->Scissor = src->Scissor;
12747117f1b4Smrg   }
12757117f1b4Smrg   if (mask & GL_STENCIL_BUFFER_BIT) {
12767117f1b4Smrg      /* OK to memcpy */
12777117f1b4Smrg      dst->Stencil = src->Stencil;
12787117f1b4Smrg   }
12797117f1b4Smrg   if (mask & GL_TEXTURE_BIT) {
12807117f1b4Smrg      /* Cannot memcpy because of pointers */
12817117f1b4Smrg      _mesa_copy_texture_state(src, dst);
12827117f1b4Smrg   }
12837117f1b4Smrg   if (mask & GL_TRANSFORM_BIT) {
12847117f1b4Smrg      /* OK to memcpy */
12857117f1b4Smrg      dst->Transform = src->Transform;
12867117f1b4Smrg   }
12877117f1b4Smrg   if (mask & GL_VIEWPORT_BIT) {
12887117f1b4Smrg      /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
12897117f1b4Smrg      dst->Viewport.X = src->Viewport.X;
12907117f1b4Smrg      dst->Viewport.Y = src->Viewport.Y;
12917117f1b4Smrg      dst->Viewport.Width = src->Viewport.Width;
12927117f1b4Smrg      dst->Viewport.Height = src->Viewport.Height;
12937117f1b4Smrg      dst->Viewport.Near = src->Viewport.Near;
12947117f1b4Smrg      dst->Viewport.Far = src->Viewport.Far;
12957117f1b4Smrg      _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
12967117f1b4Smrg   }
12977117f1b4Smrg
12987117f1b4Smrg   /* XXX FIXME:  Call callbacks?
12997117f1b4Smrg    */
13007117f1b4Smrg   dst->NewState = _NEW_ALL;
13017117f1b4Smrg}
13027117f1b4Smrg#endif
13037117f1b4Smrg
13047117f1b4Smrg
13057117f1b4Smrg/**
13067117f1b4Smrg * Check if the given context can render into the given framebuffer
13077117f1b4Smrg * by checking visual attributes.
13087117f1b4Smrg *
1309c1f859d4Smrg * Most of these tests could go away because Mesa is now pretty flexible
1310c1f859d4Smrg * in terms of mixing rendering contexts with framebuffers.  As long
1311c1f859d4Smrg * as RGB vs. CI mode agree, we're probably good.
13127117f1b4Smrg *
13137117f1b4Smrg * \return GL_TRUE if compatible, GL_FALSE otherwise.
13147117f1b4Smrg */
13157117f1b4Smrgstatic GLboolean
13163464ebd5Sriastradhcheck_compatible(const struct gl_context *ctx,
13173464ebd5Sriastradh                 const struct gl_framebuffer *buffer)
13187117f1b4Smrg{
13193464ebd5Sriastradh   const struct gl_config *ctxvis = &ctx->Visual;
13203464ebd5Sriastradh   const struct gl_config *bufvis = &buffer->Visual;
13217117f1b4Smrg
13223464ebd5Sriastradh   if (buffer == _mesa_get_incomplete_framebuffer())
13237117f1b4Smrg      return GL_TRUE;
13247117f1b4Smrg
13257117f1b4Smrg#if 0
13267117f1b4Smrg   /* disabling this fixes the fgl_glxgears pbuffer demo */
13277117f1b4Smrg   if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
13287117f1b4Smrg      return GL_FALSE;
13297117f1b4Smrg#endif
13307117f1b4Smrg   if (ctxvis->stereoMode && !bufvis->stereoMode)
13317117f1b4Smrg      return GL_FALSE;
13327117f1b4Smrg   if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
13337117f1b4Smrg      return GL_FALSE;
13347117f1b4Smrg   if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
13357117f1b4Smrg      return GL_FALSE;
13367117f1b4Smrg   if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
13377117f1b4Smrg      return GL_FALSE;
13387117f1b4Smrg   if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
13397117f1b4Smrg      return GL_FALSE;
13407117f1b4Smrg   if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
13417117f1b4Smrg      return GL_FALSE;
13427117f1b4Smrg   if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
13437117f1b4Smrg      return GL_FALSE;
13447117f1b4Smrg#if 0
13457117f1b4Smrg   /* disabled (see bug 11161) */
13467117f1b4Smrg   if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
13477117f1b4Smrg      return GL_FALSE;
13487117f1b4Smrg#endif
13497117f1b4Smrg   if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
13507117f1b4Smrg      return GL_FALSE;
13517117f1b4Smrg
13527117f1b4Smrg   return GL_TRUE;
13537117f1b4Smrg}
13547117f1b4Smrg
13557117f1b4Smrg
13567117f1b4Smrg/**
13577117f1b4Smrg * Do one-time initialization for the given framebuffer.  Specifically,
13587117f1b4Smrg * ask the driver for the window's current size and update the framebuffer
13597117f1b4Smrg * object to match.
13607117f1b4Smrg * Really, the device driver should totally take care of this.
13617117f1b4Smrg */
13627117f1b4Smrgstatic void
13633464ebd5Sriastradhinitialize_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
13647117f1b4Smrg{
13657117f1b4Smrg   GLuint width, height;
13667117f1b4Smrg   if (ctx->Driver.GetBufferSize) {
13677117f1b4Smrg      ctx->Driver.GetBufferSize(fb, &width, &height);
13687117f1b4Smrg      if (ctx->Driver.ResizeBuffers)
13697117f1b4Smrg         ctx->Driver.ResizeBuffers(ctx, fb, width, height);
13707117f1b4Smrg      fb->Initialized = GL_TRUE;
13717117f1b4Smrg   }
13727117f1b4Smrg}
13737117f1b4Smrg
13747117f1b4Smrg
1375c7037ccdSmrg/**
1376c7037ccdSmrg * Check if the viewport/scissor size has not yet been initialized.
1377c7037ccdSmrg * Initialize the size if the given width and height are non-zero.
1378c7037ccdSmrg */
1379c7037ccdSmrgvoid
13803464ebd5Sriastradh_mesa_check_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
1381c7037ccdSmrg{
1382c7037ccdSmrg   if (!ctx->ViewportInitialized && width > 0 && height > 0) {
1383c7037ccdSmrg      /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
1384c7037ccdSmrg       * potential infinite recursion.
1385c7037ccdSmrg       */
1386c7037ccdSmrg      ctx->ViewportInitialized = GL_TRUE;
1387c7037ccdSmrg      _mesa_set_viewport(ctx, 0, 0, width, height);
1388c7037ccdSmrg      _mesa_set_scissor(ctx, 0, 0, width, height);
1389c7037ccdSmrg   }
1390c7037ccdSmrg}
1391c7037ccdSmrg
1392c7037ccdSmrg
13937117f1b4Smrg/**
13947117f1b4Smrg * Bind the given context to the given drawBuffer and readBuffer and
13957117f1b4Smrg * make it the current context for the calling thread.
13967117f1b4Smrg * We'll render into the drawBuffer and read pixels from the
13977117f1b4Smrg * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
13987117f1b4Smrg *
13997117f1b4Smrg * We check that the context's and framebuffer's visuals are compatible
14007117f1b4Smrg * and return immediately if they're not.
14017117f1b4Smrg *
14027117f1b4Smrg * \param newCtx  the new GL context. If NULL then there will be no current GL
14037117f1b4Smrg *                context.
14047117f1b4Smrg * \param drawBuffer  the drawing framebuffer
14057117f1b4Smrg * \param readBuffer  the reading framebuffer
14067117f1b4Smrg */
14074a49301eSmrgGLboolean
14083464ebd5Sriastradh_mesa_make_current( struct gl_context *newCtx,
14093464ebd5Sriastradh                    struct gl_framebuffer *drawBuffer,
14103464ebd5Sriastradh                    struct gl_framebuffer *readBuffer )
14117117f1b4Smrg{
14123464ebd5Sriastradh   GET_CURRENT_CONTEXT(curCtx);
14133464ebd5Sriastradh
14147117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
14157117f1b4Smrg      _mesa_debug(newCtx, "_mesa_make_current()\n");
14167117f1b4Smrg
14177117f1b4Smrg   /* Check that the context's and framebuffer's visuals are compatible.
14187117f1b4Smrg    */
14197117f1b4Smrg   if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
14207117f1b4Smrg      if (!check_compatible(newCtx, drawBuffer)) {
14217117f1b4Smrg         _mesa_warning(newCtx,
14227117f1b4Smrg              "MakeCurrent: incompatible visuals for context and drawbuffer");
14234a49301eSmrg         return GL_FALSE;
14247117f1b4Smrg      }
14257117f1b4Smrg   }
14267117f1b4Smrg   if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
14277117f1b4Smrg      if (!check_compatible(newCtx, readBuffer)) {
14287117f1b4Smrg         _mesa_warning(newCtx,
14297117f1b4Smrg              "MakeCurrent: incompatible visuals for context and readbuffer");
14304a49301eSmrg         return GL_FALSE;
14317117f1b4Smrg      }
14327117f1b4Smrg   }
14337117f1b4Smrg
14343464ebd5Sriastradh   if (curCtx &&
14353464ebd5Sriastradh      (curCtx->WinSysDrawBuffer || curCtx->WinSysReadBuffer) &&
14363464ebd5Sriastradh       /* make sure this context is valid for flushing */
14373464ebd5Sriastradh      curCtx != newCtx)
14383464ebd5Sriastradh      _mesa_flush(curCtx);
14393464ebd5Sriastradh
14407117f1b4Smrg   /* We used to call _glapi_check_multithread() here.  Now do it in drivers */
14417117f1b4Smrg   _glapi_set_context((void *) newCtx);
14427117f1b4Smrg   ASSERT(_mesa_get_current_context() == newCtx);
14437117f1b4Smrg
14447117f1b4Smrg   if (!newCtx) {
14457117f1b4Smrg      _glapi_set_dispatch(NULL);  /* none current */
14467117f1b4Smrg   }
14477117f1b4Smrg   else {
14487117f1b4Smrg      _glapi_set_dispatch(newCtx->CurrentDispatch);
14497117f1b4Smrg
14507117f1b4Smrg      if (drawBuffer && readBuffer) {
14517117f1b4Smrg         ASSERT(drawBuffer->Name == 0);
14527117f1b4Smrg         ASSERT(readBuffer->Name == 0);
14537117f1b4Smrg         _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
14547117f1b4Smrg         _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
14557117f1b4Smrg
14567117f1b4Smrg         /*
14577117f1b4Smrg          * Only set the context's Draw/ReadBuffer fields if they're NULL
14587117f1b4Smrg          * or not bound to a user-created FBO.
14597117f1b4Smrg          */
14607117f1b4Smrg         if (!newCtx->DrawBuffer || newCtx->DrawBuffer->Name == 0) {
14617117f1b4Smrg            _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
14623464ebd5Sriastradh            /* Update the FBO's list of drawbuffers/renderbuffers.
14633464ebd5Sriastradh             * For winsys FBOs this comes from the GL state (which may have
14643464ebd5Sriastradh             * changed since the last time this FBO was bound).
14653464ebd5Sriastradh             */
14663464ebd5Sriastradh            _mesa_update_draw_buffers(newCtx);
14677117f1b4Smrg         }
14687117f1b4Smrg         if (!newCtx->ReadBuffer || newCtx->ReadBuffer->Name == 0) {
14697117f1b4Smrg            _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
14707117f1b4Smrg         }
14717117f1b4Smrg
1472c1f859d4Smrg         /* XXX only set this flag if we're really changing the draw/read
1473c1f859d4Smrg          * framebuffer bindings.
1474c1f859d4Smrg          */
14757117f1b4Smrg	 newCtx->NewState |= _NEW_BUFFERS;
14767117f1b4Smrg
14777117f1b4Smrg#if 1
14787117f1b4Smrg         /* We want to get rid of these lines: */
14797117f1b4Smrg
14807117f1b4Smrg#if _HAVE_FULL_GL
14817117f1b4Smrg         if (!drawBuffer->Initialized) {
14827117f1b4Smrg            initialize_framebuffer_size(newCtx, drawBuffer);
14837117f1b4Smrg         }
14847117f1b4Smrg         if (readBuffer != drawBuffer && !readBuffer->Initialized) {
14857117f1b4Smrg            initialize_framebuffer_size(newCtx, readBuffer);
14867117f1b4Smrg         }
14877117f1b4Smrg
14887117f1b4Smrg	 _mesa_resizebuffers(newCtx);
14897117f1b4Smrg#endif
14907117f1b4Smrg
14917117f1b4Smrg#else
14927117f1b4Smrg         /* We want the drawBuffer and readBuffer to be initialized by
14937117f1b4Smrg          * the driver.
14947117f1b4Smrg          * This generally means the Width and Height match the actual
14957117f1b4Smrg          * window size and the renderbuffers (both hardware and software
14967117f1b4Smrg          * based) are allocated to match.  The later can generally be
14977117f1b4Smrg          * done with a call to _mesa_resize_framebuffer().
14987117f1b4Smrg          *
14997117f1b4Smrg          * It's theoretically possible for a buffer to have zero width
15007117f1b4Smrg          * or height, but for now, assert check that the driver did what's
15017117f1b4Smrg          * expected of it.
15027117f1b4Smrg          */
15037117f1b4Smrg         ASSERT(drawBuffer->Width > 0);
15047117f1b4Smrg         ASSERT(drawBuffer->Height > 0);
15057117f1b4Smrg#endif
15067117f1b4Smrg
1507c7037ccdSmrg         if (drawBuffer) {
1508c7037ccdSmrg            _mesa_check_init_viewport(newCtx,
1509c7037ccdSmrg                                      drawBuffer->Width, drawBuffer->Height);
15107117f1b4Smrg         }
15117117f1b4Smrg      }
15127117f1b4Smrg
15137117f1b4Smrg      if (newCtx->FirstTimeCurrent) {
1514cdc920a0Smrg         _mesa_compute_version(newCtx);
1515cdc920a0Smrg
15163464ebd5Sriastradh         newCtx->Extensions.String = _mesa_make_extension_string(newCtx);
15173464ebd5Sriastradh
1518c7037ccdSmrg         check_context_limits(newCtx);
1519c7037ccdSmrg
1520c7037ccdSmrg         /* We can use this to help debug user's problems.  Tell them to set
1521c7037ccdSmrg          * the MESA_INFO env variable before running their app.  Then the
1522c7037ccdSmrg          * first time each context is made current we'll print some useful
1523c7037ccdSmrg          * information.
1524c7037ccdSmrg          */
15257117f1b4Smrg	 if (_mesa_getenv("MESA_INFO")) {
15267117f1b4Smrg	    _mesa_print_info();
15277117f1b4Smrg	 }
1528c7037ccdSmrg
15297117f1b4Smrg	 newCtx->FirstTimeCurrent = GL_FALSE;
15307117f1b4Smrg      }
15317117f1b4Smrg   }
15324a49301eSmrg
15334a49301eSmrg   return GL_TRUE;
15347117f1b4Smrg}
15357117f1b4Smrg
15367117f1b4Smrg
15377117f1b4Smrg/**
15387117f1b4Smrg * Make context 'ctx' share the display lists, textures and programs
15397117f1b4Smrg * that are associated with 'ctxToShare'.
15407117f1b4Smrg * Any display lists, textures or programs associated with 'ctx' will
15417117f1b4Smrg * be deleted if nobody else is sharing them.
15427117f1b4Smrg */
15437117f1b4SmrgGLboolean
15443464ebd5Sriastradh_mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare)
15457117f1b4Smrg{
15467117f1b4Smrg   if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1547c1f859d4Smrg      struct gl_shared_state *oldSharedState = ctx->Shared;
1548c1f859d4Smrg
15497117f1b4Smrg      ctx->Shared = ctxToShare->Shared;
15504a49301eSmrg
15514a49301eSmrg      _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
15527117f1b4Smrg      ctx->Shared->RefCount++;
15534a49301eSmrg      _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1554c1f859d4Smrg
1555c1f859d4Smrg      update_default_objects(ctx);
1556c1f859d4Smrg
15574a49301eSmrg      _mesa_release_shared_state(ctx, oldSharedState);
1558c1f859d4Smrg
15597117f1b4Smrg      return GL_TRUE;
15607117f1b4Smrg   }
15617117f1b4Smrg   else {
15627117f1b4Smrg      return GL_FALSE;
15637117f1b4Smrg   }
15647117f1b4Smrg}
15657117f1b4Smrg
15667117f1b4Smrg
15677117f1b4Smrg
15687117f1b4Smrg/**
15697117f1b4Smrg * \return pointer to the current GL context for this thread.
15707117f1b4Smrg *
15717117f1b4Smrg * Calls _glapi_get_context(). This isn't the fastest way to get the current
15727117f1b4Smrg * context.  If you need speed, see the #GET_CURRENT_CONTEXT macro in
15737117f1b4Smrg * context.h.
15747117f1b4Smrg */
15753464ebd5Sriastradhstruct gl_context *
15767117f1b4Smrg_mesa_get_current_context( void )
15777117f1b4Smrg{
15783464ebd5Sriastradh   return (struct gl_context *) _glapi_get_context();
15797117f1b4Smrg}
15807117f1b4Smrg
15817117f1b4Smrg
15827117f1b4Smrg/**
15837117f1b4Smrg * Get context's current API dispatch table.
15847117f1b4Smrg *
15857117f1b4Smrg * It'll either be the immediate-mode execute dispatcher or the display list
15867117f1b4Smrg * compile dispatcher.
15877117f1b4Smrg *
15887117f1b4Smrg * \param ctx GL context.
15897117f1b4Smrg *
15907117f1b4Smrg * \return pointer to dispatch_table.
15917117f1b4Smrg *
15923464ebd5Sriastradh * Simply returns __struct gl_contextRec::CurrentDispatch.
15937117f1b4Smrg */
15947117f1b4Smrgstruct _glapi_table *
15953464ebd5Sriastradh_mesa_get_dispatch(struct gl_context *ctx)
15967117f1b4Smrg{
15977117f1b4Smrg   return ctx->CurrentDispatch;
15987117f1b4Smrg}
15997117f1b4Smrg
16007117f1b4Smrg/*@}*/
16017117f1b4Smrg
16027117f1b4Smrg
16037117f1b4Smrg/**********************************************************************/
16047117f1b4Smrg/** \name Miscellaneous functions                                     */
16057117f1b4Smrg/**********************************************************************/
16067117f1b4Smrg/*@{*/
16077117f1b4Smrg
16087117f1b4Smrg/**
16097117f1b4Smrg * Record an error.
16107117f1b4Smrg *
16117117f1b4Smrg * \param ctx GL context.
16127117f1b4Smrg * \param error error code.
16137117f1b4Smrg *
16147117f1b4Smrg * Records the given error code and call the driver's dd_function_table::Error
16157117f1b4Smrg * function if defined.
16167117f1b4Smrg *
16177117f1b4Smrg * \sa
16187117f1b4Smrg * This is called via _mesa_error().
16197117f1b4Smrg */
16207117f1b4Smrgvoid
16213464ebd5Sriastradh_mesa_record_error(struct gl_context *ctx, GLenum error)
16227117f1b4Smrg{
16237117f1b4Smrg   if (!ctx)
16247117f1b4Smrg      return;
16257117f1b4Smrg
16267117f1b4Smrg   if (ctx->ErrorValue == GL_NO_ERROR) {
16277117f1b4Smrg      ctx->ErrorValue = error;
16287117f1b4Smrg   }
16297117f1b4Smrg
16307117f1b4Smrg   /* Call device driver's error handler, if any.  This is used on the Mac. */
16317117f1b4Smrg   if (ctx->Driver.Error) {
16327117f1b4Smrg      ctx->Driver.Error(ctx);
16337117f1b4Smrg   }
16347117f1b4Smrg}
16357117f1b4Smrg
16367117f1b4Smrg
16374a49301eSmrg/**
16384a49301eSmrg * Flush commands and wait for completion.
16394a49301eSmrg */
16404a49301eSmrgvoid
16413464ebd5Sriastradh_mesa_finish(struct gl_context *ctx)
16424a49301eSmrg{
16434a49301eSmrg   FLUSH_CURRENT( ctx, 0 );
16444a49301eSmrg   if (ctx->Driver.Finish) {
16454a49301eSmrg      ctx->Driver.Finish(ctx);
16464a49301eSmrg   }
16474a49301eSmrg}
16484a49301eSmrg
16494a49301eSmrg
16504a49301eSmrg/**
16514a49301eSmrg * Flush commands.
16524a49301eSmrg */
16534a49301eSmrgvoid
16543464ebd5Sriastradh_mesa_flush(struct gl_context *ctx)
16554a49301eSmrg{
16564a49301eSmrg   FLUSH_CURRENT( ctx, 0 );
16574a49301eSmrg   if (ctx->Driver.Flush) {
16584a49301eSmrg      ctx->Driver.Flush(ctx);
16594a49301eSmrg   }
16604a49301eSmrg}
16614a49301eSmrg
16624a49301eSmrg
16634a49301eSmrg
16647117f1b4Smrg/**
16657117f1b4Smrg * Execute glFinish().
16667117f1b4Smrg *
16677117f1b4Smrg * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
16687117f1b4Smrg * dd_function_table::Finish driver callback, if not NULL.
16697117f1b4Smrg */
16707117f1b4Smrgvoid GLAPIENTRY
16717117f1b4Smrg_mesa_Finish(void)
16727117f1b4Smrg{
16737117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
16747117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
16754a49301eSmrg   _mesa_finish(ctx);
16767117f1b4Smrg}
16777117f1b4Smrg
16787117f1b4Smrg
16797117f1b4Smrg/**
16807117f1b4Smrg * Execute glFlush().
16817117f1b4Smrg *
16827117f1b4Smrg * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
16837117f1b4Smrg * dd_function_table::Flush driver callback, if not NULL.
16847117f1b4Smrg */
16857117f1b4Smrgvoid GLAPIENTRY
16867117f1b4Smrg_mesa_Flush(void)
16877117f1b4Smrg{
16887117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
16897117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
16904a49301eSmrg   _mesa_flush(ctx);
16914a49301eSmrg}
16924a49301eSmrg
16934a49301eSmrg
16944a49301eSmrg/**
16954a49301eSmrg * Set mvp_with_dp4 flag.  If a driver has a preference for DP4 over
16964a49301eSmrg * MUL/MAD, or vice versa, call this function to register that.
16974a49301eSmrg * Otherwise we default to MUL/MAD.
16984a49301eSmrg */
16994a49301eSmrgvoid
17003464ebd5Sriastradh_mesa_set_mvp_with_dp4( struct gl_context *ctx,
17014a49301eSmrg                        GLboolean flag )
17024a49301eSmrg{
17034a49301eSmrg   ctx->mvp_with_dp4 = flag;
17044a49301eSmrg}
17054a49301eSmrg
17064a49301eSmrg
17074a49301eSmrg
17084a49301eSmrg/**
17094a49301eSmrg * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
17104a49301eSmrg * is called to see if it's valid to render.  This involves checking that
17114a49301eSmrg * the current shader is valid and the framebuffer is complete.
17124a49301eSmrg * If an error is detected it'll be recorded here.
17134a49301eSmrg * \return GL_TRUE if OK to render, GL_FALSE if not
17144a49301eSmrg */
17154a49301eSmrgGLboolean
17163464ebd5Sriastradh_mesa_valid_to_render(struct gl_context *ctx, const char *where)
17174a49301eSmrg{
17183464ebd5Sriastradh   bool vert_from_glsl_shader = false;
17193464ebd5Sriastradh   bool geom_from_glsl_shader = false;
17203464ebd5Sriastradh   bool frag_from_glsl_shader = false;
17213464ebd5Sriastradh
17224a49301eSmrg   /* This depends on having up to date derived state (shaders) */
17234a49301eSmrg   if (ctx->NewState)
17244a49301eSmrg      _mesa_update_state(ctx);
17254a49301eSmrg
17263464ebd5Sriastradh   if (ctx->Shader.CurrentVertexProgram) {
17273464ebd5Sriastradh      vert_from_glsl_shader = true;
17283464ebd5Sriastradh
17293464ebd5Sriastradh      if (!ctx->Shader.CurrentVertexProgram->LinkStatus) {
17304a49301eSmrg         _mesa_error(ctx, GL_INVALID_OPERATION,
17313464ebd5Sriastradh                     "%s(shader not linked)", where);
17324a49301eSmrg         return GL_FALSE;
17334a49301eSmrg      }
17344a49301eSmrg#if 0 /* not normally enabled */
17354a49301eSmrg      {
17364a49301eSmrg         char errMsg[100];
17373464ebd5Sriastradh         if (!_mesa_validate_shader_program(ctx,
17383464ebd5Sriastradh					    ctx->Shader.CurrentVertexProgram,
17394a49301eSmrg                                            errMsg)) {
17404a49301eSmrg            _mesa_warning(ctx, "Shader program %u is invalid: %s",
17413464ebd5Sriastradh                          ctx->Shader.CurrentVertexProgram->Name, errMsg);
17424a49301eSmrg         }
17434a49301eSmrg      }
17444a49301eSmrg#endif
17454a49301eSmrg   }
17463464ebd5Sriastradh
17473464ebd5Sriastradh   if (ctx->Shader.CurrentGeometryProgram) {
17483464ebd5Sriastradh      geom_from_glsl_shader = true;
17493464ebd5Sriastradh
17503464ebd5Sriastradh      if (!ctx->Shader.CurrentGeometryProgram->LinkStatus) {
17514a49301eSmrg         _mesa_error(ctx, GL_INVALID_OPERATION,
17523464ebd5Sriastradh                     "%s(shader not linked)", where);
17534a49301eSmrg         return GL_FALSE;
17544a49301eSmrg      }
17553464ebd5Sriastradh#if 0 /* not normally enabled */
17563464ebd5Sriastradh      {
17573464ebd5Sriastradh         char errMsg[100];
17583464ebd5Sriastradh         if (!_mesa_validate_shader_program(ctx,
17593464ebd5Sriastradh					    ctx->Shader.CurrentGeometryProgram,
17603464ebd5Sriastradh                                            errMsg)) {
17613464ebd5Sriastradh            _mesa_warning(ctx, "Shader program %u is invalid: %s",
17623464ebd5Sriastradh                          ctx->Shader.CurrentGeometryProgram->Name, errMsg);
17633464ebd5Sriastradh         }
17643464ebd5Sriastradh      }
17653464ebd5Sriastradh#endif
17663464ebd5Sriastradh   }
17673464ebd5Sriastradh
17683464ebd5Sriastradh   if (ctx->Shader.CurrentFragmentProgram) {
17693464ebd5Sriastradh      frag_from_glsl_shader = true;
17703464ebd5Sriastradh
17713464ebd5Sriastradh      if (!ctx->Shader.CurrentFragmentProgram->LinkStatus) {
17723464ebd5Sriastradh         _mesa_error(ctx, GL_INVALID_OPERATION,
17733464ebd5Sriastradh                     "%s(shader not linked)", where);
17743464ebd5Sriastradh         return GL_FALSE;
17753464ebd5Sriastradh      }
17763464ebd5Sriastradh#if 0 /* not normally enabled */
17773464ebd5Sriastradh      {
17783464ebd5Sriastradh         char errMsg[100];
17793464ebd5Sriastradh         if (!_mesa_validate_shader_program(ctx,
17803464ebd5Sriastradh					    ctx->Shader.CurrentFragmentProgram,
17813464ebd5Sriastradh                                            errMsg)) {
17823464ebd5Sriastradh            _mesa_warning(ctx, "Shader program %u is invalid: %s",
17833464ebd5Sriastradh                          ctx->Shader.CurrentFragmentProgram->Name, errMsg);
17843464ebd5Sriastradh         }
17853464ebd5Sriastradh      }
17863464ebd5Sriastradh#endif
17873464ebd5Sriastradh   }
17883464ebd5Sriastradh
17893464ebd5Sriastradh   /* Any shader stages that are not supplied by the GLSL shader and have
17903464ebd5Sriastradh    * assembly shaders enabled must now be validated.
17913464ebd5Sriastradh    */
17923464ebd5Sriastradh   if (!vert_from_glsl_shader
17933464ebd5Sriastradh       && ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) {
17943464ebd5Sriastradh      _mesa_error(ctx, GL_INVALID_OPERATION,
17953464ebd5Sriastradh		  "%s(vertex program not valid)", where);
17963464ebd5Sriastradh      return GL_FALSE;
17973464ebd5Sriastradh   }
17983464ebd5Sriastradh
17993464ebd5Sriastradh   /* FINISHME: If GL_NV_geometry_program4 is ever supported, the current
18003464ebd5Sriastradh    * FINISHME: geometry program should validated here.
18013464ebd5Sriastradh    */
18023464ebd5Sriastradh   (void) geom_from_glsl_shader;
18033464ebd5Sriastradh
18043464ebd5Sriastradh   if (!frag_from_glsl_shader) {
18054a49301eSmrg      if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
18063464ebd5Sriastradh	 _mesa_error(ctx, GL_INVALID_OPERATION,
18073464ebd5Sriastradh		     "%s(fragment program not valid)", where);
18083464ebd5Sriastradh	 return GL_FALSE;
18093464ebd5Sriastradh      }
18103464ebd5Sriastradh
18113464ebd5Sriastradh      /* If drawing to integer-valued color buffers, there must be an
18123464ebd5Sriastradh       * active fragment shader (GL_EXT_texture_integer).
18133464ebd5Sriastradh       */
18143464ebd5Sriastradh      if (ctx->DrawBuffer && ctx->DrawBuffer->_IntegerColor) {
18154a49301eSmrg         _mesa_error(ctx, GL_INVALID_OPERATION,
18163464ebd5Sriastradh                     "%s(integer format but no fragment shader)", where);
18174a49301eSmrg         return GL_FALSE;
18184a49301eSmrg      }
18197117f1b4Smrg   }
18204a49301eSmrg
18214a49301eSmrg   if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
18224a49301eSmrg      _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
18234a49301eSmrg                  "%s(incomplete framebuffer)", where);
18244a49301eSmrg      return GL_FALSE;
18254a49301eSmrg   }
18264a49301eSmrg
18274a49301eSmrg#ifdef DEBUG
18284a49301eSmrg   if (ctx->Shader.Flags & GLSL_LOG) {
18293464ebd5Sriastradh      struct gl_shader_program *shProg[MESA_SHADER_TYPES];
18303464ebd5Sriastradh      gl_shader_type i;
18313464ebd5Sriastradh
18323464ebd5Sriastradh      shProg[MESA_SHADER_VERTEX] = ctx->Shader.CurrentVertexProgram;
18333464ebd5Sriastradh      shProg[MESA_SHADER_GEOMETRY] = ctx->Shader.CurrentGeometryProgram;
18343464ebd5Sriastradh      shProg[MESA_SHADER_FRAGMENT] = ctx->Shader.CurrentFragmentProgram;
18353464ebd5Sriastradh
18363464ebd5Sriastradh      for (i = 0; i < MESA_SHADER_TYPES; i++) {
18373464ebd5Sriastradh	 struct gl_shader *sh;
18383464ebd5Sriastradh
18393464ebd5Sriastradh	 if (shProg[i] == NULL || shProg[i]->_Used
18403464ebd5Sriastradh	     || shProg[i]->_LinkedShaders[i] == NULL)
18413464ebd5Sriastradh	    continue;
18423464ebd5Sriastradh
18433464ebd5Sriastradh	 /* This is the first time this shader is being used.
18443464ebd5Sriastradh	  * Append shader's constants/uniforms to log file.
18453464ebd5Sriastradh	  *
18463464ebd5Sriastradh	  * The logic is a little odd here.  We only want to log data for each
18473464ebd5Sriastradh	  * shader target that will actually be used, and we only want to log
18483464ebd5Sriastradh	  * it once.  It's possible to have a program bound to the vertex
18493464ebd5Sriastradh	  * shader target that also supplied a fragment shader.  If that
18503464ebd5Sriastradh	  * program isn't also bound to the fragment shader target we don't
18513464ebd5Sriastradh	  * want to log its fragment data.
18523464ebd5Sriastradh	  */
18533464ebd5Sriastradh	 sh = shProg[i]->_LinkedShaders[i];
18543464ebd5Sriastradh	 switch (sh->Type) {
18553464ebd5Sriastradh	 case GL_VERTEX_SHADER:
18563464ebd5Sriastradh	    _mesa_append_uniforms_to_file(sh, &shProg[i]->VertexProgram->Base);
18573464ebd5Sriastradh	    break;
18583464ebd5Sriastradh
18593464ebd5Sriastradh	 case GL_GEOMETRY_SHADER_ARB:
18603464ebd5Sriastradh	    _mesa_append_uniforms_to_file(sh,
18613464ebd5Sriastradh					  &shProg[i]->GeometryProgram->Base);
18623464ebd5Sriastradh	    break;
18633464ebd5Sriastradh
18643464ebd5Sriastradh	 case GL_FRAGMENT_SHADER:
18653464ebd5Sriastradh	    _mesa_append_uniforms_to_file(sh,
18663464ebd5Sriastradh					  &shProg[i]->FragmentProgram->Base);
18673464ebd5Sriastradh	    break;
18683464ebd5Sriastradh	 }
18693464ebd5Sriastradh      }
18703464ebd5Sriastradh
18713464ebd5Sriastradh      for (i = 0; i < MESA_SHADER_TYPES; i++) {
18723464ebd5Sriastradh	 if (shProg[i] != NULL)
18733464ebd5Sriastradh	    shProg[i]->_Used = GL_TRUE;
18744a49301eSmrg      }
18754a49301eSmrg   }
18764a49301eSmrg#endif
18774a49301eSmrg
18784a49301eSmrg   return GL_TRUE;
18797117f1b4Smrg}
18807117f1b4Smrg
18817117f1b4Smrg
18827117f1b4Smrg/*@}*/
1883