Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  5.  * Copyright (C) 2008  VMware, Inc.  All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included
  15.  * in all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23.  * OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26. /**
  27.  * \file context.c
  28.  * Mesa context/visual/framebuffer management functions.
  29.  * \author Brian Paul
  30.  */
  31.  
  32. /**
  33.  * \mainpage Mesa Main Module
  34.  *
  35.  * \section MainIntroduction Introduction
  36.  *
  37.  * The Mesa Main module consists of all the files in the main/ directory.
  38.  * Among the features of this module are:
  39.  * <UL>
  40.  * <LI> Structures to represent most GL state </LI>
  41.  * <LI> State set/get functions </LI>
  42.  * <LI> Display lists </LI>
  43.  * <LI> Texture unit, object and image handling </LI>
  44.  * <LI> Matrix and attribute stacks </LI>
  45.  * </UL>
  46.  *
  47.  * Other modules are responsible for API dispatch, vertex transformation,
  48.  * point/line/triangle setup, rasterization, vertex array caching,
  49.  * vertex/fragment programs/shaders, etc.
  50.  *
  51.  *
  52.  * \section AboutDoxygen About Doxygen
  53.  *
  54.  * If you're viewing this information as Doxygen-generated HTML you'll
  55.  * see the documentation index at the top of this page.
  56.  *
  57.  * The first line lists the Mesa source code modules.
  58.  * The second line lists the indexes available for viewing the documentation
  59.  * for each module.
  60.  *
  61.  * Selecting the <b>Main page</b> link will display a summary of the module
  62.  * (this page).
  63.  *
  64.  * Selecting <b>Data Structures</b> will list all C structures.
  65.  *
  66.  * Selecting the <b>File List</b> link will list all the source files in
  67.  * the module.
  68.  * Selecting a filename will show a list of all functions defined in that file.
  69.  *
  70.  * Selecting the <b>Data Fields</b> link will display a list of all
  71.  * documented structure members.
  72.  *
  73.  * Selecting the <b>Globals</b> link will display a list
  74.  * of all functions, structures, global variables and macros in the module.
  75.  *
  76.  */
  77.  
  78.  
  79. #include "glheader.h"
  80. #include "imports.h"
  81. #include "accum.h"
  82. #include "api_exec.h"
  83. #include "api_loopback.h"
  84. #include "arrayobj.h"
  85. #include "attrib.h"
  86. #include "blend.h"
  87. #include "buffers.h"
  88. #include "bufferobj.h"
  89. #include "context.h"
  90. #include "cpuinfo.h"
  91. #include "debug.h"
  92. #include "depth.h"
  93. #include "dlist.h"
  94. #include "eval.h"
  95. #include "extensions.h"
  96. #include "fbobject.h"
  97. #include "feedback.h"
  98. #include "fog.h"
  99. #include "formats.h"
  100. #include "framebuffer.h"
  101. #include "hint.h"
  102. #include "hash.h"
  103. #include "light.h"
  104. #include "lines.h"
  105. #include "macros.h"
  106. #include "matrix.h"
  107. #include "multisample.h"
  108. #include "pixel.h"
  109. #include "pixelstore.h"
  110. #include "points.h"
  111. #include "polygon.h"
  112. #include "queryobj.h"
  113. #include "syncobj.h"
  114. #include "rastpos.h"
  115. #include "remap.h"
  116. #include "scissor.h"
  117. #include "shared.h"
  118. #include "shaderobj.h"
  119. #include "simple_list.h"
  120. #include "state.h"
  121. #include "stencil.h"
  122. #include "texcompress_s3tc.h"
  123. #include "texstate.h"
  124. #include "transformfeedback.h"
  125. #include "mtypes.h"
  126. #include "varray.h"
  127. #include "version.h"
  128. #include "viewport.h"
  129. #include "vtxfmt.h"
  130. #include "program/program.h"
  131. #include "program/prog_print.h"
  132. #include "math/m_matrix.h"
  133. #include "main/dispatch.h" /* for _gloffset_COUNT */
  134.  
  135. #ifdef USE_SPARC_ASM
  136. #include "sparc/sparc.h"
  137. #endif
  138.  
  139. #include "glsl_parser_extras.h"
  140. #include <stdbool.h>
  141.  
  142.  
  143. #ifndef MESA_VERBOSE
  144. int MESA_VERBOSE = (VERBOSE_API|VERBOSE_DRIVER|VERBOSE_STATE|VERBOSE_DRAW);
  145. #endif
  146.  
  147. #ifndef MESA_DEBUG_FLAGS
  148. int MESA_DEBUG_FLAGS = 0;
  149. #endif
  150.  
  151.  
  152. /* ubyte -> float conversion */
  153. GLfloat _mesa_ubyte_to_float_color_tab[256];
  154.  
  155.  
  156.  
  157. /**
  158.  * Swap buffers notification callback.
  159.  *
  160.  * \param ctx GL context.
  161.  *
  162.  * Called by window system just before swapping buffers.
  163.  * We have to finish any pending rendering.
  164.  */
  165. void
  166. _mesa_notifySwapBuffers(struct gl_context *ctx)
  167. {
  168.    if (MESA_VERBOSE & VERBOSE_SWAPBUFFERS)
  169.       _mesa_debug(ctx, "SwapBuffers\n");
  170.    FLUSH_CURRENT( ctx, 0 );
  171.    if (ctx->Driver.Flush) {
  172.       ctx->Driver.Flush(ctx);
  173.    }
  174. }
  175.  
  176.  
  177. /**********************************************************************/
  178. /** \name GL Visual allocation/destruction                            */
  179. /**********************************************************************/
  180. /*@{*/
  181.  
  182. /**
  183.  * Allocates a struct gl_config structure and initializes it via
  184.  * _mesa_initialize_visual().
  185.  *
  186.  * \param dbFlag double buffering
  187.  * \param stereoFlag stereo buffer
  188.  * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
  189.  * is acceptable but the actual depth type will be GLushort or GLuint as
  190.  * needed.
  191.  * \param stencilBits requested minimum bits per stencil buffer value
  192.  * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number
  193.  * of bits per color component in accum buffer.
  194.  * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
  195.  * \param redBits number of bits per color component in frame buffer for RGB(A)
  196.  * mode.  We always use 8 in core Mesa though.
  197.  * \param greenBits same as above.
  198.  * \param blueBits same as above.
  199.  * \param alphaBits same as above.
  200.  * \param numSamples not really used.
  201.  *
  202.  * \return pointer to new struct gl_config or NULL if requested parameters
  203.  * can't be met.
  204.  *
  205.  * \note Need to add params for level and numAuxBuffers (at least)
  206.  */
  207. struct gl_config *
  208. _mesa_create_visual( GLboolean dbFlag,
  209.                      GLboolean stereoFlag,
  210.                      GLint redBits,
  211.                      GLint greenBits,
  212.                      GLint blueBits,
  213.                      GLint alphaBits,
  214.                      GLint depthBits,
  215.                      GLint stencilBits,
  216.                      GLint accumRedBits,
  217.                      GLint accumGreenBits,
  218.                      GLint accumBlueBits,
  219.                      GLint accumAlphaBits,
  220.                      GLint numSamples )
  221. {
  222.    struct gl_config *vis = CALLOC_STRUCT(gl_config);
  223.    if (vis) {
  224.       if (!_mesa_initialize_visual(vis, dbFlag, stereoFlag,
  225.                                    redBits, greenBits, blueBits, alphaBits,
  226.                                    depthBits, stencilBits,
  227.                                    accumRedBits, accumGreenBits,
  228.                                    accumBlueBits, accumAlphaBits,
  229.                                    numSamples)) {
  230.          free(vis);
  231.          return NULL;
  232.       }
  233.    }
  234.    return vis;
  235. }
  236.  
  237.  
  238. /**
  239.  * Makes some sanity checks and fills in the fields of the struct
  240.  * gl_config object with the given parameters.  If the caller needs to
  241.  * set additional fields, he should just probably init the whole
  242.  * gl_config object himself.
  243.  *
  244.  * \return GL_TRUE on success, or GL_FALSE on failure.
  245.  *
  246.  * \sa _mesa_create_visual() above for the parameter description.
  247.  */
  248. GLboolean
  249. _mesa_initialize_visual( struct gl_config *vis,
  250.                          GLboolean dbFlag,
  251.                          GLboolean stereoFlag,
  252.                          GLint redBits,
  253.                          GLint greenBits,
  254.                          GLint blueBits,
  255.                          GLint alphaBits,
  256.                          GLint depthBits,
  257.                          GLint stencilBits,
  258.                          GLint accumRedBits,
  259.                          GLint accumGreenBits,
  260.                          GLint accumBlueBits,
  261.                          GLint accumAlphaBits,
  262.                          GLint numSamples )
  263. {
  264.    assert(vis);
  265.  
  266.    if (depthBits < 0 || depthBits > 32) {
  267.       return GL_FALSE;
  268.    }
  269.    if (stencilBits < 0 || stencilBits > 8) {
  270.       return GL_FALSE;
  271.    }
  272.    assert(accumRedBits >= 0);
  273.    assert(accumGreenBits >= 0);
  274.    assert(accumBlueBits >= 0);
  275.    assert(accumAlphaBits >= 0);
  276.  
  277.    vis->rgbMode          = GL_TRUE;
  278.    vis->doubleBufferMode = dbFlag;
  279.    vis->stereoMode       = stereoFlag;
  280.  
  281.    vis->redBits          = redBits;
  282.    vis->greenBits        = greenBits;
  283.    vis->blueBits         = blueBits;
  284.    vis->alphaBits        = alphaBits;
  285.    vis->rgbBits          = redBits + greenBits + blueBits;
  286.  
  287.    vis->indexBits      = 0;
  288.    vis->depthBits      = depthBits;
  289.    vis->stencilBits    = stencilBits;
  290.  
  291.    vis->accumRedBits   = accumRedBits;
  292.    vis->accumGreenBits = accumGreenBits;
  293.    vis->accumBlueBits  = accumBlueBits;
  294.    vis->accumAlphaBits = accumAlphaBits;
  295.  
  296.    vis->haveAccumBuffer   = accumRedBits > 0;
  297.    vis->haveDepthBuffer   = depthBits > 0;
  298.    vis->haveStencilBuffer = stencilBits > 0;
  299.  
  300.    vis->numAuxBuffers = 0;
  301.    vis->level = 0;
  302.    vis->sampleBuffers = numSamples > 0 ? 1 : 0;
  303.    vis->samples = numSamples;
  304.  
  305.    return GL_TRUE;
  306. }
  307.  
  308.  
  309. /**
  310.  * Destroy a visual and free its memory.
  311.  *
  312.  * \param vis visual.
  313.  *
  314.  * Frees the visual structure.
  315.  */
  316. void
  317. _mesa_destroy_visual( struct gl_config *vis )
  318. {
  319.    free(vis);
  320. }
  321.  
  322. /*@}*/
  323.  
  324.  
  325. /**********************************************************************/
  326. /** \name Context allocation, initialization, destroying
  327.  *
  328.  * The purpose of the most initialization functions here is to provide the
  329.  * default state values according to the OpenGL specification.
  330.  */
  331. /**********************************************************************/
  332. /*@{*/
  333.  
  334.  
  335. /**
  336.  * This is lame.  gdb only seems to recognize enum types that are
  337.  * actually used somewhere.  We want to be able to print/use enum
  338.  * values such as TEXTURE_2D_INDEX in gdb.  But we don't actually use
  339.  * the gl_texture_index type anywhere.  Thus, this lame function.
  340.  */
  341. static void
  342. dummy_enum_func(void)
  343. {
  344.    gl_buffer_index bi = BUFFER_FRONT_LEFT;
  345.    gl_face_index fi = FACE_POS_X;
  346.    gl_frag_result fr = FRAG_RESULT_DEPTH;
  347.    gl_texture_index ti = TEXTURE_2D_ARRAY_INDEX;
  348.    gl_vert_attrib va = VERT_ATTRIB_POS;
  349.    gl_varying_slot vs = VARYING_SLOT_POS;
  350.  
  351.    (void) bi;
  352.    (void) fi;
  353.    (void) fr;
  354.    (void) ti;
  355.    (void) va;
  356.    (void) vs;
  357. }
  358.  
  359.  
  360. /**
  361.  * One-time initialization mutex lock.
  362.  *
  363.  * \sa Used by one_time_init().
  364.  */
  365. _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
  366.  
  367.  
  368.  
  369. /**
  370.  * Calls all the various one-time-init functions in Mesa.
  371.  *
  372.  * While holding a global mutex lock, calls several initialization functions,
  373.  * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
  374.  * defined.
  375.  *
  376.  * \sa _math_init().
  377.  */
  378. static void
  379. one_time_init( struct gl_context *ctx )
  380. {
  381.    static GLbitfield api_init_mask = 0x0;
  382.  
  383.    _glthread_LOCK_MUTEX(OneTimeLock);
  384.  
  385.    /* truly one-time init */
  386.    if (!api_init_mask) {
  387.       GLuint i;
  388.  
  389.       /* do some implementation tests */
  390.       assert( sizeof(GLbyte) == 1 );
  391.       assert( sizeof(GLubyte) == 1 );
  392.       assert( sizeof(GLshort) == 2 );
  393.       assert( sizeof(GLushort) == 2 );
  394.       assert( sizeof(GLint) == 4 );
  395.       assert( sizeof(GLuint) == 4 );
  396.  
  397.       _mesa_get_cpu_features();
  398.  
  399.       for (i = 0; i < 256; i++) {
  400.          _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
  401.       }
  402.  
  403. #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
  404.       if (MESA_VERBOSE != 0) {
  405.          _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
  406.                      PACKAGE_VERSION, __DATE__, __TIME__);
  407.       }
  408. #endif
  409.  
  410. #ifdef DEBUG
  411.       _mesa_test_formats();
  412. #endif
  413.    }
  414.  
  415.    /* per-API one-time init */
  416.    if (!(api_init_mask & (1 << ctx->API))) {
  417.       _mesa_init_get_hash(ctx);
  418.  
  419.       _mesa_init_remap_table();
  420.    }
  421.  
  422.    api_init_mask |= 1 << ctx->API;
  423.  
  424.    _glthread_UNLOCK_MUTEX(OneTimeLock);
  425.  
  426.    /* Hopefully atexit() is widely available.  If not, we may need some
  427.     * #ifdef tests here.
  428.     */
  429.    atexit(_mesa_destroy_shader_compiler);
  430.  
  431.    dummy_enum_func();
  432. }
  433.  
  434.  
  435. /**
  436.  * Initialize fields of gl_current_attrib (aka ctx->Current.*)
  437.  */
  438. static void
  439. _mesa_init_current(struct gl_context *ctx)
  440. {
  441.    GLuint i;
  442.  
  443.    /* Init all to (0,0,0,1) */
  444.    for (i = 0; i < Elements(ctx->Current.Attrib); i++) {
  445.       ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
  446.    }
  447.  
  448.    /* redo special cases: */
  449.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 0.0 );
  450.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
  451.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
  452.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
  453.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
  454.    ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
  455. }
  456.  
  457.  
  458. /**
  459.  * Init vertex/fragment/geometry program limits.
  460.  * Important: drivers should override these with actual limits.
  461.  */
  462. static void
  463. init_program_limits(struct gl_context *ctx, GLenum type,
  464.                     struct gl_program_constants *prog)
  465. {
  466.    prog->MaxInstructions = MAX_PROGRAM_INSTRUCTIONS;
  467.    prog->MaxAluInstructions = MAX_PROGRAM_INSTRUCTIONS;
  468.    prog->MaxTexInstructions = MAX_PROGRAM_INSTRUCTIONS;
  469.    prog->MaxTexIndirections = MAX_PROGRAM_INSTRUCTIONS;
  470.    prog->MaxTemps = MAX_PROGRAM_TEMPS;
  471.    prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS;
  472.    prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
  473.    prog->MaxAddressOffset = MAX_PROGRAM_LOCAL_PARAMS;
  474.  
  475.    switch (type) {
  476.    case GL_VERTEX_PROGRAM_ARB:
  477.       prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
  478.       prog->MaxAttribs = MAX_VERTEX_GENERIC_ATTRIBS;
  479.       prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
  480.       prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
  481.       break;
  482.    case GL_FRAGMENT_PROGRAM_ARB:
  483.       prog->MaxParameters = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
  484.       prog->MaxAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
  485.       prog->MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
  486.       prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
  487.       break;
  488.    case MESA_GEOMETRY_PROGRAM:
  489.       prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
  490.       prog->MaxAttribs = MAX_VERTEX_GENERIC_ATTRIBS;
  491.       prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
  492.       prog->MaxUniformComponents = MAX_GEOMETRY_UNIFORM_COMPONENTS;
  493.       break;
  494.    default:
  495.       assert(0 && "Bad program type in init_program_limits()");
  496.    }
  497.  
  498.    /* Set the native limits to zero.  This implies that there is no native
  499.     * support for shaders.  Let the drivers fill in the actual values.
  500.     */
  501.    prog->MaxNativeInstructions = 0;
  502.    prog->MaxNativeAluInstructions = 0;
  503.    prog->MaxNativeTexInstructions = 0;
  504.    prog->MaxNativeTexIndirections = 0;
  505.    prog->MaxNativeAttribs = 0;
  506.    prog->MaxNativeTemps = 0;
  507.    prog->MaxNativeAddressRegs = 0;
  508.    prog->MaxNativeParameters = 0;
  509.  
  510.    /* Set GLSL datatype range/precision info assuming IEEE float values.
  511.     * Drivers should override these defaults as needed.
  512.     */
  513.    prog->MediumFloat.RangeMin = 127;
  514.    prog->MediumFloat.RangeMax = 127;
  515.    prog->MediumFloat.Precision = 23;
  516.    prog->LowFloat = prog->HighFloat = prog->MediumFloat;
  517.  
  518.    /* Assume ints are stored as floats for now, since this is the least-common
  519.     * denominator.  The OpenGL ES spec implies (page 132) that the precision
  520.     * of integer types should be 0.  Practically speaking, IEEE
  521.     * single-precision floating point values can only store integers in the
  522.     * range [-0x01000000, 0x01000000] without loss of precision.
  523.     */
  524.    prog->MediumInt.RangeMin = 24;
  525.    prog->MediumInt.RangeMax = 24;
  526.    prog->MediumInt.Precision = 0;
  527.    prog->LowInt = prog->HighInt = prog->MediumInt;
  528.  
  529.    prog->MaxUniformBlocks = 12;
  530.    prog->MaxCombinedUniformComponents = (prog->MaxUniformComponents +
  531.                                          ctx->Const.MaxUniformBlockSize / 4 *
  532.                                          prog->MaxUniformBlocks);
  533. }
  534.  
  535.  
  536. /**
  537.  * Initialize fields of gl_constants (aka ctx->Const.*).
  538.  * Use defaults from config.h.  The device drivers will often override
  539.  * some of these values (such as number of texture units).
  540.  */
  541. static void
  542. _mesa_init_constants(struct gl_context *ctx)
  543. {
  544.    assert(ctx);
  545.  
  546.    /* Constants, may be overriden (usually only reduced) by device drivers */
  547.    ctx->Const.MaxTextureMbytes = MAX_TEXTURE_MBYTES;
  548.    ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
  549.    ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
  550.    ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
  551.    ctx->Const.MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
  552.    ctx->Const.MaxArrayTextureLayers = MAX_ARRAY_TEXTURE_LAYERS;
  553.    ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
  554.    ctx->Const.FragmentProgram.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
  555.    ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureCoordUnits,
  556.                                      ctx->Const.FragmentProgram.MaxTextureImageUnits);
  557.    ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
  558.    ctx->Const.MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
  559.    ctx->Const.MaxTextureBufferSize = 65536;
  560.    ctx->Const.TextureBufferOffsetAlignment = 1;
  561.    ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
  562.    ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
  563.    ctx->Const.MinPointSize = MIN_POINT_SIZE;
  564.    ctx->Const.MaxPointSize = MAX_POINT_SIZE;
  565.    ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
  566.    ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
  567.    ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
  568.    ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
  569.    ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
  570.    ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
  571.    ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
  572.    ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
  573.    ctx->Const.MaxClipPlanes = 6;
  574.    ctx->Const.MaxLights = MAX_LIGHTS;
  575.    ctx->Const.MaxShininess = 128.0;
  576.    ctx->Const.MaxSpotExponent = 128.0;
  577.    ctx->Const.MaxViewportWidth = MAX_VIEWPORT_WIDTH;
  578.    ctx->Const.MaxViewportHeight = MAX_VIEWPORT_HEIGHT;
  579.  
  580.    /** GL_ARB_uniform_buffer_object */
  581.    ctx->Const.MaxCombinedUniformBlocks = 36;
  582.    ctx->Const.MaxUniformBufferBindings = 36;
  583.    ctx->Const.MaxUniformBlockSize = 16384;
  584.    ctx->Const.UniformBufferOffsetAlignment = 1;
  585.  
  586.    init_program_limits(ctx, GL_VERTEX_PROGRAM_ARB, &ctx->Const.VertexProgram);
  587.    init_program_limits(ctx, GL_FRAGMENT_PROGRAM_ARB, &ctx->Const.FragmentProgram);
  588.    init_program_limits(ctx, MESA_GEOMETRY_PROGRAM, &ctx->Const.GeometryProgram);
  589.  
  590.    ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
  591.    ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
  592.  
  593.    /* CheckArrayBounds is overriden by drivers/x11 for X server */
  594.    ctx->Const.CheckArrayBounds = GL_FALSE;
  595.  
  596.    /* GL_ARB_draw_buffers */
  597.    ctx->Const.MaxDrawBuffers = MAX_DRAW_BUFFERS;
  598.  
  599.    ctx->Const.MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
  600.    ctx->Const.MaxRenderbufferSize = MAX_RENDERBUFFER_SIZE;
  601.  
  602.    ctx->Const.VertexProgram.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
  603.    ctx->Const.MaxCombinedTextureImageUnits = MAX_COMBINED_TEXTURE_IMAGE_UNITS;
  604.    ctx->Const.MaxVarying = 16; /* old limit not to break tnl and swrast */
  605.    ctx->Const.GeometryProgram.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
  606.    ctx->Const.MaxGeometryOutputVertices = MAX_GEOMETRY_OUTPUT_VERTICES;
  607.    ctx->Const.MaxGeometryTotalOutputComponents = MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS;
  608.  
  609.    /* Shading language version */
  610.    if (_mesa_is_desktop_gl(ctx)) {
  611.       ctx->Const.GLSLVersion = 120;
  612.       _mesa_override_glsl_version(ctx);
  613.    }
  614.    else if (ctx->API == API_OPENGLES2) {
  615.       ctx->Const.GLSLVersion = 100;
  616.    }
  617.    else if (ctx->API == API_OPENGLES) {
  618.       ctx->Const.GLSLVersion = 0; /* GLSL not supported */
  619.    }
  620.  
  621.    /* GL_ARB_framebuffer_object */
  622.    ctx->Const.MaxSamples = 0;
  623.  
  624.    /* GL_ARB_sync */
  625.    ctx->Const.MaxServerWaitTimeout = 0x1fff7fffffffULL;
  626.  
  627.    /* GL_ATI_envmap_bumpmap */
  628.    ctx->Const.SupportedBumpUnits = SUPPORTED_ATI_BUMP_UNITS;
  629.  
  630.    /* GL_EXT_provoking_vertex */
  631.    ctx->Const.QuadsFollowProvokingVertexConvention = GL_TRUE;
  632.  
  633.    /* GL_EXT_transform_feedback */
  634.    ctx->Const.MaxTransformFeedbackBuffers = MAX_FEEDBACK_BUFFERS;
  635.    ctx->Const.MaxTransformFeedbackSeparateComponents = 4 * MAX_FEEDBACK_ATTRIBS;
  636.    ctx->Const.MaxTransformFeedbackInterleavedComponents = 4 * MAX_FEEDBACK_ATTRIBS;
  637.    ctx->Const.MaxVertexStreams = 1;
  638.  
  639.    /* GL 3.2  */
  640.    ctx->Const.ProfileMask = ctx->API == API_OPENGL_CORE
  641.                           ? GL_CONTEXT_CORE_PROFILE_BIT
  642.                           : GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
  643.  
  644.    /** GL_EXT_gpu_shader4 */
  645.    ctx->Const.MinProgramTexelOffset = -8;
  646.    ctx->Const.MaxProgramTexelOffset = 7;
  647.  
  648.    /* GL_ARB_robustness */
  649.    ctx->Const.ResetStrategy = GL_NO_RESET_NOTIFICATION_ARB;
  650.  
  651.    /* PrimitiveRestart */
  652.    ctx->Const.PrimitiveRestartInSoftware = GL_FALSE;
  653.  
  654.    /* ES 3.0 or ARB_ES3_compatibility */
  655.    ctx->Const.MaxElementIndex = 0xffffffffu;
  656.  
  657.    /* GL_ARB_texture_multisample */
  658.    ctx->Const.MaxColorTextureSamples = 1;
  659.    ctx->Const.MaxDepthTextureSamples = 1;
  660.    ctx->Const.MaxIntegerSamples = 1;
  661. }
  662.  
  663.  
  664. /**
  665.  * Do some sanity checks on the limits/constants for the given context.
  666.  * Only called the first time a context is bound.
  667.  */
  668. static void
  669. check_context_limits(struct gl_context *ctx)
  670. {
  671.    /* check that we don't exceed the size of various bitfields */
  672.    assert(VARYING_SLOT_MAX <=
  673.           (8 * sizeof(ctx->VertexProgram._Current->Base.OutputsWritten)));
  674.    assert(VARYING_SLOT_MAX <=
  675.           (8 * sizeof(ctx->FragmentProgram._Current->Base.InputsRead)));
  676.  
  677.    /* shader-related checks */
  678.    assert(ctx->Const.FragmentProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
  679.    assert(ctx->Const.VertexProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
  680.  
  681.    /* Texture unit checks */
  682.    assert(ctx->Const.FragmentProgram.MaxTextureImageUnits > 0);
  683.    assert(ctx->Const.FragmentProgram.MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS);
  684.    assert(ctx->Const.MaxTextureCoordUnits > 0);
  685.    assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS);
  686.    assert(ctx->Const.MaxTextureUnits > 0);
  687.    assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS);
  688.    assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS);
  689.    assert(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.FragmentProgram.MaxTextureImageUnits,
  690.                                              ctx->Const.MaxTextureCoordUnits));
  691.    assert(ctx->Const.MaxCombinedTextureImageUnits > 0);
  692.    assert(ctx->Const.MaxCombinedTextureImageUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
  693.    assert(ctx->Const.MaxTextureCoordUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
  694.    /* number of coord units cannot be greater than number of image units */
  695.    assert(ctx->Const.MaxTextureCoordUnits <= ctx->Const.FragmentProgram.MaxTextureImageUnits);
  696.  
  697.  
  698.    /* Texture size checks */
  699.    assert(ctx->Const.MaxTextureLevels <= MAX_TEXTURE_LEVELS);
  700.    assert(ctx->Const.Max3DTextureLevels <= MAX_3D_TEXTURE_LEVELS);
  701.    assert(ctx->Const.MaxCubeTextureLevels <= MAX_CUBE_TEXTURE_LEVELS);
  702.    assert(ctx->Const.MaxTextureRectSize <= MAX_TEXTURE_RECT_SIZE);
  703.  
  704.    /* Texture level checks */
  705.    assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
  706.    assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
  707.  
  708.    /* Max texture size should be <= max viewport size (render to texture) */
  709.    assert((1U << (ctx->Const.MaxTextureLevels - 1))
  710.           <= ctx->Const.MaxViewportWidth);
  711.    assert((1U << (ctx->Const.MaxTextureLevels - 1))
  712.           <= ctx->Const.MaxViewportHeight);
  713.  
  714.    assert(ctx->Const.MaxDrawBuffers <= MAX_DRAW_BUFFERS);
  715.  
  716.    /* if this fails, add more enum values to gl_buffer_index */
  717.    assert(BUFFER_COLOR0 + MAX_DRAW_BUFFERS <= BUFFER_COUNT);
  718.  
  719.    /* XXX probably add more tests */
  720. }
  721.  
  722.  
  723. /**
  724.  * Initialize the attribute groups in a GL context.
  725.  *
  726.  * \param ctx GL context.
  727.  *
  728.  * Initializes all the attributes, calling the respective <tt>init*</tt>
  729.  * functions for the more complex data structures.
  730.  */
  731. static GLboolean
  732. init_attrib_groups(struct gl_context *ctx)
  733. {
  734.    assert(ctx);
  735.  
  736.    /* Constants */
  737.    _mesa_init_constants( ctx );
  738.  
  739.    /* Extensions */
  740.    _mesa_init_extensions( ctx );
  741.  
  742.    /* Attribute Groups */
  743.    _mesa_init_accum( ctx );
  744.    _mesa_init_attrib( ctx );
  745.    _mesa_init_buffer_objects( ctx );
  746.    _mesa_init_color( ctx );
  747.    _mesa_init_current( ctx );
  748.    _mesa_init_depth( ctx );
  749.    _mesa_init_debug( ctx );
  750.    _mesa_init_display_list( ctx );
  751.    _mesa_init_errors( ctx );
  752.    _mesa_init_eval( ctx );
  753.    _mesa_init_fbobjects( ctx );
  754.    _mesa_init_feedback( ctx );
  755.    _mesa_init_fog( ctx );
  756.    _mesa_init_hint( ctx );
  757.    _mesa_init_line( ctx );
  758.    _mesa_init_lighting( ctx );
  759.    _mesa_init_matrix( ctx );
  760.    _mesa_init_multisample( ctx );
  761.    _mesa_init_pixel( ctx );
  762.    _mesa_init_pixelstore( ctx );
  763.    _mesa_init_point( ctx );
  764.    _mesa_init_polygon( ctx );
  765.    _mesa_init_program( ctx );
  766.    _mesa_init_queryobj( ctx );
  767.    _mesa_init_sync( ctx );
  768.    _mesa_init_rastpos( ctx );
  769.    _mesa_init_scissor( ctx );
  770.    _mesa_init_shader_state( ctx );
  771.    _mesa_init_stencil( ctx );
  772.    _mesa_init_transform( ctx );
  773.    _mesa_init_transform_feedback( ctx );
  774.    _mesa_init_varray( ctx );
  775.    _mesa_init_viewport( ctx );
  776.  
  777.    if (!_mesa_init_texture( ctx ))
  778.       return GL_FALSE;
  779.  
  780.    _mesa_init_texture_s3tc( ctx );
  781.  
  782.    /* Miscellaneous */
  783.    ctx->NewState = _NEW_ALL;
  784.    ctx->NewDriverState = ~0;
  785.    ctx->ErrorValue = GL_NO_ERROR;
  786.    ctx->ResetStatus = GL_NO_ERROR;
  787.    ctx->varying_vp_inputs = VERT_BIT_ALL;
  788.  
  789.    return GL_TRUE;
  790. }
  791.  
  792.  
  793. /**
  794.  * Update default objects in a GL context with respect to shared state.
  795.  *
  796.  * \param ctx GL context.
  797.  *
  798.  * Removes references to old default objects, (texture objects, program
  799.  * objects, etc.) and changes to reference those from the current shared
  800.  * state.
  801.  */
  802. static GLboolean
  803. update_default_objects(struct gl_context *ctx)
  804. {
  805.    assert(ctx);
  806.  
  807.    _mesa_update_default_objects_program(ctx);
  808.    _mesa_update_default_objects_texture(ctx);
  809.    _mesa_update_default_objects_buffer_objects(ctx);
  810.  
  811.    return GL_TRUE;
  812. }
  813.  
  814.  
  815. /**
  816.  * This is the default function we plug into all dispatch table slots
  817.  * This helps prevents a segfault when someone calls a GL function without
  818.  * first checking if the extension's supported.
  819.  */
  820. int
  821. _mesa_generic_nop(void)
  822. {
  823.    GET_CURRENT_CONTEXT(ctx);
  824.    _mesa_error(ctx, GL_INVALID_OPERATION,
  825.                "unsupported function called "
  826.                "(unsupported extension or deprecated function?)");
  827.    return 0;
  828. }
  829.  
  830.  
  831. /**
  832.  * Allocate and initialize a new dispatch table.
  833.  */
  834. struct _glapi_table *
  835. _mesa_alloc_dispatch_table()
  836. {
  837.    /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
  838.     * In practice, this'll be the same for stand-alone Mesa.  But for DRI
  839.     * Mesa we do this to accomodate different versions of libGL and various
  840.     * DRI drivers.
  841.     */
  842.    GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
  843.    struct _glapi_table *table;
  844.  
  845.    table = malloc(numEntries * sizeof(_glapi_proc));
  846.    if (table) {
  847.       _glapi_proc *entry = (_glapi_proc *) table;
  848.       GLint i;
  849.       for (i = 0; i < numEntries; i++) {
  850.          entry[i] = (_glapi_proc) _mesa_generic_nop;
  851.       }
  852.    }
  853.    return table;
  854. }
  855.  
  856. /**
  857.  * Creates a minimal dispatch table for use within glBegin()/glEnd().
  858.  *
  859.  * This ensures that we generate GL_INVALID_OPERATION errors from most
  860.  * functions, since the set of functions that are valid within Begin/End is
  861.  * very small.
  862.  *
  863.  * From the GL 1.0 specification section 2.6.3, "GL Commands within
  864.  * Begin/End"
  865.  *
  866.  *     "The only GL commands that are allowed within any Begin/End pairs are
  867.  *      the commands for specifying vertex coordinates, vertex color, normal
  868.  *      coordinates, and texture coordinates (Vertex, Color, Index, Normal,
  869.  *      TexCoord), EvalCoord and EvalPoint commands (see section 5.1),
  870.  *      commands for specifying lighting material parameters (Material
  871.  *      commands see section 2.12.2), display list invocation commands
  872.  *      (CallList and CallLists see section 5.4), and the EdgeFlag
  873.  *      command. Executing Begin after Begin has already been executed but
  874.  *      before an End is issued generates the INVALID OPERATION error, as does
  875.  *      executing End without a previous corresponding Begin. Executing any
  876.  *      other GL command within Begin/End results in the error INVALID
  877.  *      OPERATION."
  878.  *
  879.  * The table entries for specifying vertex attributes are set up by
  880.  * install_vtxfmt() and _mesa_loopback_init_api_table(), and End() and dlists
  881.  * are set by install_vtxfmt() as well.
  882.  */
  883. static struct _glapi_table *
  884. create_beginend_table(const struct gl_context *ctx)
  885. {
  886.    struct _glapi_table *table;
  887.  
  888.    table = _mesa_alloc_dispatch_table();
  889.    if (!table)
  890.       return NULL;
  891.  
  892.    /* Fill in functions which return a value, since they should return some
  893.     * specific value even if they emit a GL_INVALID_OPERATION error from them
  894.     * being called within glBegin()/glEnd().
  895.     */
  896. #define COPY_DISPATCH(func) SET_##func(table, GET_##func(ctx->Exec))
  897.  
  898.    COPY_DISPATCH(GenLists);
  899.    COPY_DISPATCH(IsProgram);
  900.    COPY_DISPATCH(IsVertexArray);
  901.    COPY_DISPATCH(IsBuffer);
  902.    COPY_DISPATCH(IsEnabled);
  903.    COPY_DISPATCH(IsEnabledi);
  904.    COPY_DISPATCH(IsRenderbuffer);
  905.    COPY_DISPATCH(IsFramebuffer);
  906.    COPY_DISPATCH(CheckFramebufferStatus);
  907.    COPY_DISPATCH(RenderMode);
  908.    COPY_DISPATCH(GetString);
  909.    COPY_DISPATCH(GetStringi);
  910.    COPY_DISPATCH(GetPointerv);
  911.    COPY_DISPATCH(IsQuery);
  912.    COPY_DISPATCH(IsSampler);
  913.    COPY_DISPATCH(IsSync);
  914.    COPY_DISPATCH(IsTexture);
  915.    COPY_DISPATCH(IsTransformFeedback);
  916.    COPY_DISPATCH(DeleteQueries);
  917.    COPY_DISPATCH(AreTexturesResident);
  918.    COPY_DISPATCH(FenceSync);
  919.    COPY_DISPATCH(ClientWaitSync);
  920.    COPY_DISPATCH(MapBuffer);
  921.    COPY_DISPATCH(UnmapBuffer);
  922.    COPY_DISPATCH(MapBufferRange);
  923.    COPY_DISPATCH(ObjectPurgeableAPPLE);
  924.    COPY_DISPATCH(ObjectUnpurgeableAPPLE);
  925.  
  926.    _mesa_loopback_init_api_table(ctx, table);
  927.  
  928.    return table;
  929. }
  930.  
  931. void
  932. _mesa_initialize_dispatch_tables(struct gl_context *ctx)
  933. {
  934.    /* Do the code-generated setup of the exec table in api_exec.c. */
  935.    _mesa_initialize_exec_table(ctx);
  936.  
  937.    if (ctx->Save)
  938.       _mesa_initialize_save_table(ctx);
  939. }
  940.  
  941. /**
  942.  * Initialize a struct gl_context struct (rendering context).
  943.  *
  944.  * This includes allocating all the other structs and arrays which hang off of
  945.  * the context by pointers.
  946.  * Note that the driver needs to pass in its dd_function_table here since
  947.  * we need to at least call driverFunctions->NewTextureObject to create the
  948.  * default texture objects.
  949.  *
  950.  * Called by _mesa_create_context().
  951.  *
  952.  * Performs the imports and exports callback tables initialization, and
  953.  * miscellaneous one-time initializations. If no shared context is supplied one
  954.  * is allocated, and increase its reference count.  Setups the GL API dispatch
  955.  * tables.  Initialize the TNL module. Sets the maximum Z buffer depth.
  956.  * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
  957.  * for debug flags.
  958.  *
  959.  * \param ctx the context to initialize
  960.  * \param api the GL API type to create the context for
  961.  * \param visual describes the visual attributes for this context
  962.  * \param share_list points to context to share textures, display lists,
  963.  *        etc with, or NULL
  964.  * \param driverFunctions table of device driver functions for this context
  965.  *        to use
  966.  */
  967. GLboolean
  968. _mesa_initialize_context(struct gl_context *ctx,
  969.                          gl_api api,
  970.                          const struct gl_config *visual,
  971.                          struct gl_context *share_list,
  972.                          const struct dd_function_table *driverFunctions)
  973. {
  974.    struct gl_shared_state *shared;
  975.    int i;
  976.  
  977.    assert(driverFunctions->NewTextureObject);
  978.    assert(driverFunctions->FreeTextureImageBuffer);
  979.  
  980.    ctx->API = api;
  981.    ctx->Visual = *visual;
  982.    ctx->DrawBuffer = NULL;
  983.    ctx->ReadBuffer = NULL;
  984.    ctx->WinSysDrawBuffer = NULL;
  985.    ctx->WinSysReadBuffer = NULL;
  986.  
  987.    if (_mesa_is_desktop_gl(ctx)) {
  988.       _mesa_override_gl_version(ctx);
  989.    }
  990.  
  991.    /* misc one-time initializations */
  992.    one_time_init(ctx);
  993.  
  994.    /* Plug in driver functions and context pointer here.
  995.     * This is important because when we call alloc_shared_state() below
  996.     * we'll call ctx->Driver.NewTextureObject() to create the default
  997.     * textures.
  998.     */
  999.    ctx->Driver = *driverFunctions;
  1000.  
  1001.    if (share_list) {
  1002.       /* share state with another context */
  1003.       shared = share_list->Shared;
  1004.    }
  1005.    else {
  1006.       /* allocate new, unshared state */
  1007.       shared = _mesa_alloc_shared_state(ctx);
  1008.       if (!shared)
  1009.          return GL_FALSE;
  1010.    }
  1011.  
  1012.    _mesa_reference_shared_state(ctx, &ctx->Shared, shared);
  1013.  
  1014.    if (!init_attrib_groups( ctx ))
  1015.       goto fail;
  1016.  
  1017.    /* setup the API dispatch tables with all nop functions */
  1018.    ctx->OutsideBeginEnd = _mesa_alloc_dispatch_table();
  1019.    if (!ctx->OutsideBeginEnd)
  1020.       goto fail;
  1021.    ctx->Exec = ctx->OutsideBeginEnd;
  1022.    ctx->CurrentDispatch = ctx->OutsideBeginEnd;
  1023.  
  1024.    ctx->FragmentProgram._MaintainTexEnvProgram
  1025.       = (_mesa_getenv("MESA_TEX_PROG") != NULL);
  1026.  
  1027.    ctx->VertexProgram._MaintainTnlProgram
  1028.       = (_mesa_getenv("MESA_TNL_PROG") != NULL);
  1029.    if (ctx->VertexProgram._MaintainTnlProgram) {
  1030.       /* this is required... */
  1031.       ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
  1032.    }
  1033.  
  1034.    /* Mesa core handles all the formats that mesa core knows about.
  1035.     * Drivers will want to override this list with just the formats
  1036.     * they can handle, and confirm that appropriate fallbacks exist in
  1037.     * _mesa_choose_tex_format().
  1038.     */
  1039.    memset(&ctx->TextureFormatSupported, GL_TRUE,
  1040.           sizeof(ctx->TextureFormatSupported));
  1041.  
  1042.    switch (ctx->API) {
  1043.    case API_OPENGL_COMPAT:
  1044.       ctx->BeginEnd = create_beginend_table(ctx);
  1045.       ctx->Save = _mesa_alloc_dispatch_table();
  1046.       if (!ctx->BeginEnd || !ctx->Save)
  1047.          goto fail;
  1048.  
  1049.       /* fall-through */
  1050.    case API_OPENGL_CORE:
  1051.       break;
  1052.    case API_OPENGLES:
  1053.       /**
  1054.        * GL_OES_texture_cube_map says
  1055.        * "Initially all texture generation modes are set to REFLECTION_MAP_OES"
  1056.        */
  1057.       for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
  1058.          struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
  1059.          texUnit->GenS.Mode = GL_REFLECTION_MAP_NV;
  1060.          texUnit->GenT.Mode = GL_REFLECTION_MAP_NV;
  1061.          texUnit->GenR.Mode = GL_REFLECTION_MAP_NV;
  1062.          texUnit->GenS._ModeBit = TEXGEN_REFLECTION_MAP_NV;
  1063.          texUnit->GenT._ModeBit = TEXGEN_REFLECTION_MAP_NV;
  1064.          texUnit->GenR._ModeBit = TEXGEN_REFLECTION_MAP_NV;
  1065.       }
  1066.       break;
  1067.    case API_OPENGLES2:
  1068.       ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
  1069.       ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
  1070.       break;
  1071.    }
  1072.  
  1073.    ctx->FirstTimeCurrent = GL_TRUE;
  1074.  
  1075.    return GL_TRUE;
  1076.  
  1077. fail:
  1078.    _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
  1079.    free(ctx->BeginEnd);
  1080.    free(ctx->Exec);
  1081.    free(ctx->Save);
  1082.    return GL_FALSE;
  1083. }
  1084.  
  1085.  
  1086. /**
  1087.  * Allocate and initialize a struct gl_context structure.
  1088.  * Note that the driver needs to pass in its dd_function_table here since
  1089.  * we need to at least call driverFunctions->NewTextureObject to initialize
  1090.  * the rendering context.
  1091.  *
  1092.  * \param api the GL API type to create the context for
  1093.  * \param visual a struct gl_config pointer (we copy the struct contents)
  1094.  * \param share_list another context to share display lists with or NULL
  1095.  * \param driverFunctions points to the dd_function_table into which the
  1096.  *        driver has plugged in all its special functions.
  1097.  *
  1098.  * \return pointer to a new __struct gl_contextRec or NULL if error.
  1099.  */
  1100. struct gl_context *
  1101. _mesa_create_context(gl_api api,
  1102.                      const struct gl_config *visual,
  1103.                      struct gl_context *share_list,
  1104.                      const struct dd_function_table *driverFunctions)
  1105. {
  1106.    struct gl_context *ctx;
  1107.  
  1108.    ASSERT(visual);
  1109.  
  1110.    ctx = calloc(1, sizeof(struct gl_context));
  1111.    if (!ctx)
  1112.       return NULL;
  1113.  
  1114.    if (_mesa_initialize_context(ctx, api, visual, share_list,
  1115.                                 driverFunctions)) {
  1116.       return ctx;
  1117.    }
  1118.    else {
  1119.       free(ctx);
  1120.       return NULL;
  1121.    }
  1122. }
  1123.  
  1124.  
  1125. /**
  1126.  * Free the data associated with the given context.
  1127.  *
  1128.  * But doesn't free the struct gl_context struct itself.
  1129.  *
  1130.  * \sa _mesa_initialize_context() and init_attrib_groups().
  1131.  */
  1132. void
  1133. _mesa_free_context_data( struct gl_context *ctx )
  1134. {
  1135.    if (!_mesa_get_current_context()){
  1136.       /* No current context, but we may need one in order to delete
  1137.        * texture objs, etc.  So temporarily bind the context now.
  1138.        */
  1139.       _mesa_make_current(ctx, NULL, NULL);
  1140.    }
  1141.  
  1142.    /* unreference WinSysDraw/Read buffers */
  1143.    _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
  1144.    _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
  1145.    _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
  1146.    _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
  1147.  
  1148.    _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
  1149.    _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
  1150.    _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram, NULL);
  1151.  
  1152.    _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
  1153.    _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
  1154.    _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, NULL);
  1155.  
  1156.    _mesa_reference_array_object(ctx, &ctx->Array.ArrayObj, NULL);
  1157.    _mesa_reference_array_object(ctx, &ctx->Array.DefaultArrayObj, NULL);
  1158.  
  1159.    _mesa_free_attrib_data(ctx);
  1160.    _mesa_free_buffer_objects(ctx);
  1161.    _mesa_free_lighting_data( ctx );
  1162.    _mesa_free_eval_data( ctx );
  1163.    _mesa_free_texture_data( ctx );
  1164.    _mesa_free_matrix_data( ctx );
  1165.    _mesa_free_viewport_data( ctx );
  1166.    _mesa_free_program_data(ctx);
  1167.    _mesa_free_shader_state(ctx);
  1168.    _mesa_free_queryobj_data(ctx);
  1169.    _mesa_free_sync_data(ctx);
  1170.    _mesa_free_varray_data(ctx);
  1171.    _mesa_free_transform_feedback(ctx);
  1172.  
  1173.    _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL);
  1174.    _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL);
  1175.    _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL);
  1176.    _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
  1177.  
  1178.    /* free dispatch tables */
  1179.    free(ctx->BeginEnd);
  1180.    free(ctx->Exec);
  1181.    free(ctx->Save);
  1182.  
  1183.    /* Shared context state (display lists, textures, etc) */
  1184.    _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
  1185.  
  1186.    /* needs to be after freeing shared state */
  1187.    _mesa_free_display_list_data(ctx);
  1188.  
  1189.    _mesa_free_errors_data(ctx);
  1190.  
  1191.    free((void *)ctx->Extensions.String);
  1192.  
  1193.    free(ctx->VersionString);
  1194.  
  1195.    /* unbind the context if it's currently bound */
  1196.    if (ctx == _mesa_get_current_context()) {
  1197.       _mesa_make_current(NULL, NULL, NULL);
  1198.    }
  1199. }
  1200.  
  1201.  
  1202. /**
  1203.  * Destroy a struct gl_context structure.
  1204.  *
  1205.  * \param ctx GL context.
  1206.  *
  1207.  * Calls _mesa_free_context_data() and frees the gl_context object itself.
  1208.  */
  1209. void
  1210. _mesa_destroy_context( struct gl_context *ctx )
  1211. {
  1212.    if (ctx) {
  1213.       _mesa_free_context_data(ctx);
  1214.       free( (void *) ctx );
  1215.    }
  1216. }
  1217.  
  1218.  
  1219. /**
  1220.  * Copy attribute groups from one context to another.
  1221.  *
  1222.  * \param src source context
  1223.  * \param dst destination context
  1224.  * \param mask bitwise OR of GL_*_BIT flags
  1225.  *
  1226.  * According to the bits specified in \p mask, copies the corresponding
  1227.  * attributes from \p src into \p dst.  For many of the attributes a simple \c
  1228.  * memcpy is not enough due to the existence of internal pointers in their data
  1229.  * structures.
  1230.  */
  1231. void
  1232. _mesa_copy_context( const struct gl_context *src, struct gl_context *dst,
  1233.                     GLuint mask )
  1234. {
  1235.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1236.       /* OK to memcpy */
  1237.       dst->Accum = src->Accum;
  1238.    }
  1239.    if (mask & GL_COLOR_BUFFER_BIT) {
  1240.       /* OK to memcpy */
  1241.       dst->Color = src->Color;
  1242.    }
  1243.    if (mask & GL_CURRENT_BIT) {
  1244.       /* OK to memcpy */
  1245.       dst->Current = src->Current;
  1246.    }
  1247.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1248.       /* OK to memcpy */
  1249.       dst->Depth = src->Depth;
  1250.    }
  1251.    if (mask & GL_ENABLE_BIT) {
  1252.       /* no op */
  1253.    }
  1254.    if (mask & GL_EVAL_BIT) {
  1255.       /* OK to memcpy */
  1256.       dst->Eval = src->Eval;
  1257.    }
  1258.    if (mask & GL_FOG_BIT) {
  1259.       /* OK to memcpy */
  1260.       dst->Fog = src->Fog;
  1261.    }
  1262.    if (mask & GL_HINT_BIT) {
  1263.       /* OK to memcpy */
  1264.       dst->Hint = src->Hint;
  1265.    }
  1266.    if (mask & GL_LIGHTING_BIT) {
  1267.       GLuint i;
  1268.       /* begin with memcpy */
  1269.       dst->Light = src->Light;
  1270.       /* fixup linked lists to prevent pointer insanity */
  1271.       make_empty_list( &(dst->Light.EnabledList) );
  1272.       for (i = 0; i < MAX_LIGHTS; i++) {
  1273.          if (dst->Light.Light[i].Enabled) {
  1274.             insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
  1275.          }
  1276.       }
  1277.    }
  1278.    if (mask & GL_LINE_BIT) {
  1279.       /* OK to memcpy */
  1280.       dst->Line = src->Line;
  1281.    }
  1282.    if (mask & GL_LIST_BIT) {
  1283.       /* OK to memcpy */
  1284.       dst->List = src->List;
  1285.    }
  1286.    if (mask & GL_PIXEL_MODE_BIT) {
  1287.       /* OK to memcpy */
  1288.       dst->Pixel = src->Pixel;
  1289.    }
  1290.    if (mask & GL_POINT_BIT) {
  1291.       /* OK to memcpy */
  1292.       dst->Point = src->Point;
  1293.    }
  1294.    if (mask & GL_POLYGON_BIT) {
  1295.       /* OK to memcpy */
  1296.       dst->Polygon = src->Polygon;
  1297.    }
  1298.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1299.       /* Use loop instead of memcpy due to problem with Portland Group's
  1300.        * C compiler.  Reported by John Stone.
  1301.        */
  1302.       GLuint i;
  1303.       for (i = 0; i < 32; i++) {
  1304.          dst->PolygonStipple[i] = src->PolygonStipple[i];
  1305.       }
  1306.    }
  1307.    if (mask & GL_SCISSOR_BIT) {
  1308.       /* OK to memcpy */
  1309.       dst->Scissor = src->Scissor;
  1310.    }
  1311.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1312.       /* OK to memcpy */
  1313.       dst->Stencil = src->Stencil;
  1314.    }
  1315.    if (mask & GL_TEXTURE_BIT) {
  1316.       /* Cannot memcpy because of pointers */
  1317.       _mesa_copy_texture_state(src, dst);
  1318.    }
  1319.    if (mask & GL_TRANSFORM_BIT) {
  1320.       /* OK to memcpy */
  1321.       dst->Transform = src->Transform;
  1322.    }
  1323.    if (mask & GL_VIEWPORT_BIT) {
  1324.       /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
  1325.       dst->Viewport.X = src->Viewport.X;
  1326.       dst->Viewport.Y = src->Viewport.Y;
  1327.       dst->Viewport.Width = src->Viewport.Width;
  1328.       dst->Viewport.Height = src->Viewport.Height;
  1329.       dst->Viewport.Near = src->Viewport.Near;
  1330.       dst->Viewport.Far = src->Viewport.Far;
  1331.       _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
  1332.    }
  1333.  
  1334.    /* XXX FIXME:  Call callbacks?
  1335.     */
  1336.    dst->NewState = _NEW_ALL;
  1337.    dst->NewDriverState = ~0;
  1338. }
  1339.  
  1340.  
  1341. /**
  1342.  * Check if the given context can render into the given framebuffer
  1343.  * by checking visual attributes.
  1344.  *
  1345.  * Most of these tests could go away because Mesa is now pretty flexible
  1346.  * in terms of mixing rendering contexts with framebuffers.  As long
  1347.  * as RGB vs. CI mode agree, we're probably good.
  1348.  *
  1349.  * \return GL_TRUE if compatible, GL_FALSE otherwise.
  1350.  */
  1351. static GLboolean
  1352. check_compatible(const struct gl_context *ctx,
  1353.                  const struct gl_framebuffer *buffer)
  1354. {
  1355.    const struct gl_config *ctxvis = &ctx->Visual;
  1356.    const struct gl_config *bufvis = &buffer->Visual;
  1357.  
  1358.    if (buffer == _mesa_get_incomplete_framebuffer())
  1359.       return GL_TRUE;
  1360.  
  1361. #if 0
  1362.    /* disabling this fixes the fgl_glxgears pbuffer demo */
  1363.    if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
  1364.       return GL_FALSE;
  1365. #endif
  1366.    if (ctxvis->stereoMode && !bufvis->stereoMode)
  1367.       return GL_FALSE;
  1368.    if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
  1369.       return GL_FALSE;
  1370.    if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
  1371.       return GL_FALSE;
  1372.    if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
  1373.       return GL_FALSE;
  1374.    if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
  1375.       return GL_FALSE;
  1376.    if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
  1377.       return GL_FALSE;
  1378.    if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
  1379.       return GL_FALSE;
  1380. #if 0
  1381.    /* disabled (see bug 11161) */
  1382.    if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
  1383.       return GL_FALSE;
  1384. #endif
  1385.    if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
  1386.       return GL_FALSE;
  1387.  
  1388.    return GL_TRUE;
  1389. }
  1390.  
  1391.  
  1392. /**
  1393.  * Check if the viewport/scissor size has not yet been initialized.
  1394.  * Initialize the size if the given width and height are non-zero.
  1395.  */
  1396. void
  1397. _mesa_check_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
  1398. {
  1399.    if (!ctx->ViewportInitialized && width > 0 && height > 0) {
  1400.       /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
  1401.        * potential infinite recursion.
  1402.        */
  1403.       ctx->ViewportInitialized = GL_TRUE;
  1404.       _mesa_set_viewport(ctx, 0, 0, width, height);
  1405.       _mesa_set_scissor(ctx, 0, 0, width, height);
  1406.    }
  1407. }
  1408.  
  1409.  
  1410. /**
  1411.  * Bind the given context to the given drawBuffer and readBuffer and
  1412.  * make it the current context for the calling thread.
  1413.  * We'll render into the drawBuffer and read pixels from the
  1414.  * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
  1415.  *
  1416.  * We check that the context's and framebuffer's visuals are compatible
  1417.  * and return immediately if they're not.
  1418.  *
  1419.  * \param newCtx  the new GL context. If NULL then there will be no current GL
  1420.  *                context.
  1421.  * \param drawBuffer  the drawing framebuffer
  1422.  * \param readBuffer  the reading framebuffer
  1423.  */
  1424. GLboolean
  1425. _mesa_make_current( struct gl_context *newCtx,
  1426.                     struct gl_framebuffer *drawBuffer,
  1427.                     struct gl_framebuffer *readBuffer )
  1428. {
  1429.    GET_CURRENT_CONTEXT(curCtx);
  1430.  
  1431.    if (MESA_VERBOSE & VERBOSE_API)
  1432.       _mesa_debug(newCtx, "_mesa_make_current()\n");
  1433.  
  1434.    /* Check that the context's and framebuffer's visuals are compatible.
  1435.     */
  1436.    if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
  1437.       if (!check_compatible(newCtx, drawBuffer)) {
  1438.          _mesa_warning(newCtx,
  1439.               "MakeCurrent: incompatible visuals for context and drawbuffer");
  1440.          return GL_FALSE;
  1441.       }
  1442.    }
  1443.    if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
  1444.       if (!check_compatible(newCtx, readBuffer)) {
  1445.          _mesa_warning(newCtx,
  1446.               "MakeCurrent: incompatible visuals for context and readbuffer");
  1447.          return GL_FALSE;
  1448.       }
  1449.    }
  1450.  
  1451.    if (curCtx &&
  1452.       (curCtx->WinSysDrawBuffer || curCtx->WinSysReadBuffer) &&
  1453.        /* make sure this context is valid for flushing */
  1454.       curCtx != newCtx)
  1455.       _mesa_flush(curCtx);
  1456.  
  1457.    /* We used to call _glapi_check_multithread() here.  Now do it in drivers */
  1458.    _glapi_set_context((void *) newCtx);
  1459.    ASSERT(_mesa_get_current_context() == newCtx);
  1460.  
  1461.    if (!newCtx) {
  1462.       _glapi_set_dispatch(NULL);  /* none current */
  1463.    }
  1464.    else {
  1465.       _glapi_set_dispatch(newCtx->CurrentDispatch);
  1466.  
  1467.       if (drawBuffer && readBuffer) {
  1468.          ASSERT(_mesa_is_winsys_fbo(drawBuffer));
  1469.          ASSERT(_mesa_is_winsys_fbo(readBuffer));
  1470.          _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
  1471.          _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
  1472.  
  1473.          /*
  1474.           * Only set the context's Draw/ReadBuffer fields if they're NULL
  1475.           * or not bound to a user-created FBO.
  1476.           */
  1477.          if (!newCtx->DrawBuffer || _mesa_is_winsys_fbo(newCtx->DrawBuffer)) {
  1478.             _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
  1479.             /* Update the FBO's list of drawbuffers/renderbuffers.
  1480.              * For winsys FBOs this comes from the GL state (which may have
  1481.              * changed since the last time this FBO was bound).
  1482.              */
  1483.             _mesa_update_draw_buffers(newCtx);
  1484.          }
  1485.          if (!newCtx->ReadBuffer || _mesa_is_winsys_fbo(newCtx->ReadBuffer)) {
  1486.             _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
  1487.          }
  1488.  
  1489.          /* XXX only set this flag if we're really changing the draw/read
  1490.           * framebuffer bindings.
  1491.           */
  1492.          newCtx->NewState |= _NEW_BUFFERS;
  1493.  
  1494.          if (drawBuffer) {
  1495.             _mesa_check_init_viewport(newCtx,
  1496.                                       drawBuffer->Width, drawBuffer->Height);
  1497.          }
  1498.       }
  1499.  
  1500.       if (newCtx->FirstTimeCurrent) {
  1501.          assert(newCtx->Version > 0);
  1502.  
  1503.          newCtx->Extensions.String = _mesa_make_extension_string(newCtx);
  1504.  
  1505.          check_context_limits(newCtx);
  1506.  
  1507.          /* We can use this to help debug user's problems.  Tell them to set
  1508.           * the MESA_INFO env variable before running their app.  Then the
  1509.           * first time each context is made current we'll print some useful
  1510.           * information.
  1511.           */
  1512.          if (_mesa_getenv("MESA_INFO")) {
  1513.             _mesa_print_info();
  1514.          }
  1515.  
  1516.          newCtx->FirstTimeCurrent = GL_FALSE;
  1517.       }
  1518.    }
  1519.  
  1520.    return GL_TRUE;
  1521. }
  1522.  
  1523.  
  1524. /**
  1525.  * Make context 'ctx' share the display lists, textures and programs
  1526.  * that are associated with 'ctxToShare'.
  1527.  * Any display lists, textures or programs associated with 'ctx' will
  1528.  * be deleted if nobody else is sharing them.
  1529.  */
  1530. GLboolean
  1531. _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare)
  1532. {
  1533.    if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
  1534.       struct gl_shared_state *oldShared = NULL;
  1535.  
  1536.       /* save ref to old state to prevent it from being deleted immediately */
  1537.       _mesa_reference_shared_state(ctx, &oldShared, ctx->Shared);
  1538.  
  1539.       /* update ctx's Shared pointer */
  1540.       _mesa_reference_shared_state(ctx, &ctx->Shared, ctxToShare->Shared);
  1541.  
  1542.       update_default_objects(ctx);
  1543.  
  1544.       /* release the old shared state */
  1545.       _mesa_reference_shared_state(ctx, &oldShared, NULL);
  1546.  
  1547.       return GL_TRUE;
  1548.    }
  1549.    else {
  1550.       return GL_FALSE;
  1551.    }
  1552. }
  1553.  
  1554.  
  1555.  
  1556. /**
  1557.  * \return pointer to the current GL context for this thread.
  1558.  *
  1559.  * Calls _glapi_get_context(). This isn't the fastest way to get the current
  1560.  * context.  If you need speed, see the #GET_CURRENT_CONTEXT macro in
  1561.  * context.h.
  1562.  */
  1563. struct gl_context *
  1564. _mesa_get_current_context( void )
  1565. {
  1566.    return (struct gl_context *) _glapi_get_context();
  1567. }
  1568.  
  1569.  
  1570. /**
  1571.  * Get context's current API dispatch table.
  1572.  *
  1573.  * It'll either be the immediate-mode execute dispatcher or the display list
  1574.  * compile dispatcher.
  1575.  *
  1576.  * \param ctx GL context.
  1577.  *
  1578.  * \return pointer to dispatch_table.
  1579.  *
  1580.  * Simply returns __struct gl_contextRec::CurrentDispatch.
  1581.  */
  1582. struct _glapi_table *
  1583. _mesa_get_dispatch(struct gl_context *ctx)
  1584. {
  1585.    return ctx->CurrentDispatch;
  1586. }
  1587.  
  1588. /*@}*/
  1589.  
  1590.  
  1591. /**********************************************************************/
  1592. /** \name Miscellaneous functions                                     */
  1593. /**********************************************************************/
  1594. /*@{*/
  1595.  
  1596. /**
  1597.  * Record an error.
  1598.  *
  1599.  * \param ctx GL context.
  1600.  * \param error error code.
  1601.  *
  1602.  * Records the given error code and call the driver's dd_function_table::Error
  1603.  * function if defined.
  1604.  *
  1605.  * \sa
  1606.  * This is called via _mesa_error().
  1607.  */
  1608. void
  1609. _mesa_record_error(struct gl_context *ctx, GLenum error)
  1610. {
  1611.    if (!ctx)
  1612.       return;
  1613.  
  1614.    if (ctx->ErrorValue == GL_NO_ERROR) {
  1615.       ctx->ErrorValue = error;
  1616.    }
  1617. }
  1618.  
  1619.  
  1620. /**
  1621.  * Flush commands and wait for completion.
  1622.  */
  1623. void
  1624. _mesa_finish(struct gl_context *ctx)
  1625. {
  1626.    FLUSH_VERTICES( ctx, 0 );
  1627.    FLUSH_CURRENT( ctx, 0 );
  1628.    if (ctx->Driver.Finish) {
  1629.       ctx->Driver.Finish(ctx);
  1630.    }
  1631. }
  1632.  
  1633.  
  1634. /**
  1635.  * Flush commands.
  1636.  */
  1637. void
  1638. _mesa_flush(struct gl_context *ctx)
  1639. {
  1640.    FLUSH_VERTICES( ctx, 0 );
  1641.    FLUSH_CURRENT( ctx, 0 );
  1642.    if (ctx->Driver.Flush) {
  1643.       ctx->Driver.Flush(ctx);
  1644.    }
  1645. }
  1646.  
  1647.  
  1648.  
  1649. /**
  1650.  * Execute glFinish().
  1651.  *
  1652.  * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
  1653.  * dd_function_table::Finish driver callback, if not NULL.
  1654.  */
  1655. void GLAPIENTRY
  1656. _mesa_Finish(void)
  1657. {
  1658.    GET_CURRENT_CONTEXT(ctx);
  1659.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  1660.    _mesa_finish(ctx);
  1661. }
  1662.  
  1663.  
  1664. /**
  1665.  * Execute glFlush().
  1666.  *
  1667.  * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
  1668.  * dd_function_table::Flush driver callback, if not NULL.
  1669.  */
  1670. void GLAPIENTRY
  1671. _mesa_Flush(void)
  1672. {
  1673.    GET_CURRENT_CONTEXT(ctx);
  1674.    ASSERT_OUTSIDE_BEGIN_END(ctx);
  1675.    _mesa_flush(ctx);
  1676. }
  1677.  
  1678.  
  1679. /*
  1680.  * ARB_blend_func_extended - ERRORS section
  1681.  * "The error INVALID_OPERATION is generated by Begin or any procedure that
  1682.  *  implicitly calls Begin if any draw buffer has a blend function requiring the
  1683.  *  second color input (SRC1_COLOR, ONE_MINUS_SRC1_COLOR, SRC1_ALPHA or
  1684.  *  ONE_MINUS_SRC1_ALPHA), and a framebuffer is bound that has more than
  1685.  *  the value of MAX_DUAL_SOURCE_DRAW_BUFFERS-1 active color attachements."
  1686.  */
  1687. static GLboolean
  1688. _mesa_check_blend_func_error(struct gl_context *ctx)
  1689. {
  1690.    GLuint i;
  1691.    for (i = ctx->Const.MaxDualSourceDrawBuffers;
  1692.         i < ctx->DrawBuffer->_NumColorDrawBuffers;
  1693.         i++) {
  1694.       if (ctx->Color.Blend[i]._UsesDualSrc) {
  1695.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1696.                      "dual source blend on illegal attachment");
  1697.          return GL_FALSE;
  1698.       }
  1699.    }
  1700.    return GL_TRUE;
  1701. }
  1702.  
  1703. /**
  1704.  * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
  1705.  * is called to see if it's valid to render.  This involves checking that
  1706.  * the current shader is valid and the framebuffer is complete.
  1707.  * If an error is detected it'll be recorded here.
  1708.  * \return GL_TRUE if OK to render, GL_FALSE if not
  1709.  */
  1710. GLboolean
  1711. _mesa_valid_to_render(struct gl_context *ctx, const char *where)
  1712. {
  1713.    bool vert_from_glsl_shader = false;
  1714.    bool geom_from_glsl_shader = false;
  1715.    bool frag_from_glsl_shader = false;
  1716.  
  1717.    /* This depends on having up to date derived state (shaders) */
  1718.    if (ctx->NewState)
  1719.       _mesa_update_state(ctx);
  1720.  
  1721.    if (ctx->Shader.CurrentVertexProgram) {
  1722.       vert_from_glsl_shader = true;
  1723.  
  1724.       if (!ctx->Shader.CurrentVertexProgram->LinkStatus) {
  1725.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1726.                      "%s(shader not linked)", where);
  1727.          return GL_FALSE;
  1728.       }
  1729. #if 0 /* not normally enabled */
  1730.       {
  1731.          char errMsg[100];
  1732.          if (!_mesa_validate_shader_program(ctx,
  1733.                                             ctx->Shader.CurrentVertexProgram,
  1734.                                             errMsg)) {
  1735.             _mesa_warning(ctx, "Shader program %u is invalid: %s",
  1736.                           ctx->Shader.CurrentVertexProgram->Name, errMsg);
  1737.          }
  1738.       }
  1739. #endif
  1740.    }
  1741.  
  1742.    if (ctx->Shader.CurrentGeometryProgram) {
  1743.       geom_from_glsl_shader = true;
  1744.  
  1745.       if (!ctx->Shader.CurrentGeometryProgram->LinkStatus) {
  1746.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1747.                      "%s(shader not linked)", where);
  1748.          return GL_FALSE;
  1749.       }
  1750. #if 0 /* not normally enabled */
  1751.       {
  1752.          char errMsg[100];
  1753.          if (!_mesa_validate_shader_program(ctx,
  1754.                                             ctx->Shader.CurrentGeometryProgram,
  1755.                                             errMsg)) {
  1756.             _mesa_warning(ctx, "Shader program %u is invalid: %s",
  1757.                           ctx->Shader.CurrentGeometryProgram->Name, errMsg);
  1758.          }
  1759.       }
  1760. #endif
  1761.    }
  1762.  
  1763.    if (ctx->Shader.CurrentFragmentProgram) {
  1764.       frag_from_glsl_shader = true;
  1765.  
  1766.       if (!ctx->Shader.CurrentFragmentProgram->LinkStatus) {
  1767.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1768.                      "%s(shader not linked)", where);
  1769.          return GL_FALSE;
  1770.       }
  1771. #if 0 /* not normally enabled */
  1772.       {
  1773.          char errMsg[100];
  1774.          if (!_mesa_validate_shader_program(ctx,
  1775.                                             ctx->Shader.CurrentFragmentProgram,
  1776.                                             errMsg)) {
  1777.             _mesa_warning(ctx, "Shader program %u is invalid: %s",
  1778.                           ctx->Shader.CurrentFragmentProgram->Name, errMsg);
  1779.          }
  1780.       }
  1781. #endif
  1782.    }
  1783.  
  1784.    /* Any shader stages that are not supplied by the GLSL shader and have
  1785.     * assembly shaders enabled must now be validated.
  1786.     */
  1787.    if (!vert_from_glsl_shader
  1788.        && ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) {
  1789.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1790.                   "%s(vertex program not valid)", where);
  1791.       return GL_FALSE;
  1792.    }
  1793.  
  1794.    /* FINISHME: If GL_NV_geometry_program4 is ever supported, the current
  1795.     * FINISHME: geometry program should validated here.
  1796.     */
  1797.    (void) geom_from_glsl_shader;
  1798.  
  1799.    if (!frag_from_glsl_shader) {
  1800.       if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
  1801.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1802.                      "%s(fragment program not valid)", where);
  1803.          return GL_FALSE;
  1804.       }
  1805.  
  1806.       /* If drawing to integer-valued color buffers, there must be an
  1807.        * active fragment shader (GL_EXT_texture_integer).
  1808.        */
  1809.       if (ctx->DrawBuffer && ctx->DrawBuffer->_IntegerColor) {
  1810.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1811.                      "%s(integer format but no fragment shader)", where);
  1812.          return GL_FALSE;
  1813.       }
  1814.    }
  1815.  
  1816.    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  1817.       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
  1818.                   "%s(incomplete framebuffer)", where);
  1819.       return GL_FALSE;
  1820.    }
  1821.  
  1822.    if (_mesa_check_blend_func_error(ctx) == GL_FALSE) {
  1823.       return GL_FALSE;
  1824.    }
  1825.  
  1826. #ifdef DEBUG
  1827.    if (ctx->Shader.Flags & GLSL_LOG) {
  1828.       struct gl_shader_program *shProg[MESA_SHADER_TYPES];
  1829.       gl_shader_type i;
  1830.  
  1831.       shProg[MESA_SHADER_VERTEX] = ctx->Shader.CurrentVertexProgram;
  1832.       shProg[MESA_SHADER_GEOMETRY] = ctx->Shader.CurrentGeometryProgram;
  1833.       shProg[MESA_SHADER_FRAGMENT] = ctx->Shader.CurrentFragmentProgram;
  1834.  
  1835.       for (i = 0; i < MESA_SHADER_TYPES; i++) {
  1836.          if (shProg[i] == NULL || shProg[i]->_Used
  1837.              || shProg[i]->_LinkedShaders[i] == NULL)
  1838.             continue;
  1839.  
  1840.          /* This is the first time this shader is being used.
  1841.           * Append shader's constants/uniforms to log file.
  1842.           *
  1843.           * Only log data for the program target that matches the shader
  1844.           * target.  It's possible to have a program bound to the vertex
  1845.           * shader target that also supplied a fragment shader.  If that
  1846.           * program isn't also bound to the fragment shader target we don't
  1847.           * want to log its fragment data.
  1848.           */
  1849.          _mesa_append_uniforms_to_file(shProg[i]->_LinkedShaders[i]);
  1850.       }
  1851.  
  1852.       for (i = 0; i < MESA_SHADER_TYPES; i++) {
  1853.          if (shProg[i] != NULL)
  1854.             shProg[i]->_Used = GL_TRUE;
  1855.       }
  1856.    }
  1857. #endif
  1858.  
  1859.    return GL_TRUE;
  1860. }
  1861.  
  1862.  
  1863. /*@}*/
  1864.