Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2004-2008  Brian Paul   All Rights Reserved.
  5.  * Copyright (C) 2009-2010  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 shaderapi.c
  28.  * \author Brian Paul
  29.  *
  30.  * Implementation of GLSL-related API functions.
  31.  * The glUniform* functions are in uniforms.c
  32.  *
  33.  *
  34.  * XXX things to do:
  35.  * 1. Check that the right error code is generated for all _mesa_error() calls.
  36.  * 2. Insert FLUSH_VERTICES calls in various places
  37.  */
  38.  
  39.  
  40. #include "main/glheader.h"
  41. #include "main/context.h"
  42. #include "main/dispatch.h"
  43. #include "main/enums.h"
  44. #include "main/hash.h"
  45. #include "main/mtypes.h"
  46. #include "main/pipelineobj.h"
  47. #include "main/shaderapi.h"
  48. #include "main/shaderobj.h"
  49. #include "main/transformfeedback.h"
  50. #include "main/uniforms.h"
  51. #include "program/program.h"
  52. #include "program/prog_print.h"
  53. #include "program/prog_parameter.h"
  54. #include "util/ralloc.h"
  55. #include "util/hash_table.h"
  56. #include <stdbool.h>
  57. #include "../glsl/glsl_parser_extras.h"
  58. #include "../glsl/ir.h"
  59. #include "../glsl/ir_uniform.h"
  60. #include "../glsl/program.h"
  61.  
  62. /** Define this to enable shader substitution (see below) */
  63. #define SHADER_SUBST 0
  64.  
  65.  
  66. /**
  67.  * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
  68.  */
  69. GLbitfield
  70. _mesa_get_shader_flags(void)
  71. {
  72.    GLbitfield flags = 0x0;
  73.    const char *env = getenv("MESA_GLSL");
  74.  
  75.    if (env) {
  76.       if (strstr(env, "dump_on_error"))
  77.          flags |= GLSL_DUMP_ON_ERROR;
  78.       else if (strstr(env, "dump"))
  79.          flags |= GLSL_DUMP;
  80.       if (strstr(env, "log"))
  81.          flags |= GLSL_LOG;
  82.       if (strstr(env, "nopvert"))
  83.          flags |= GLSL_NOP_VERT;
  84.       if (strstr(env, "nopfrag"))
  85.          flags |= GLSL_NOP_FRAG;
  86.       if (strstr(env, "nopt"))
  87.          flags |= GLSL_NO_OPT;
  88.       else if (strstr(env, "opt"))
  89.          flags |= GLSL_OPT;
  90.       if (strstr(env, "uniform"))
  91.          flags |= GLSL_UNIFORMS;
  92.       if (strstr(env, "useprog"))
  93.          flags |= GLSL_USE_PROG;
  94.       if (strstr(env, "errors"))
  95.          flags |= GLSL_REPORT_ERRORS;
  96.    }
  97.  
  98.    return flags;
  99. }
  100.  
  101.  
  102. /**
  103.  * Initialize context's shader state.
  104.  */
  105. void
  106. _mesa_init_shader_state(struct gl_context *ctx)
  107. {
  108.    /* Device drivers may override these to control what kind of instructions
  109.     * are generated by the GLSL compiler.
  110.     */
  111.    struct gl_shader_compiler_options options;
  112.    gl_shader_stage sh;
  113.  
  114.    memset(&options, 0, sizeof(options));
  115.    options.MaxUnrollIterations = 32;
  116.    options.MaxIfDepth = UINT_MAX;
  117.  
  118.    for (sh = 0; sh < MESA_SHADER_STAGES; ++sh)
  119.       memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
  120.  
  121.    ctx->Shader.Flags = _mesa_get_shader_flags();
  122.  
  123.    if (ctx->Shader.Flags != 0)
  124.       ctx->Const.GenerateTemporaryNames = true;
  125.  
  126.    /* Extended for ARB_separate_shader_objects */
  127.    ctx->Shader.RefCount = 1;
  128.    mtx_init(&ctx->Shader.Mutex, mtx_plain);
  129. }
  130.  
  131.  
  132. /**
  133.  * Free the per-context shader-related state.
  134.  */
  135. void
  136. _mesa_free_shader_state(struct gl_context *ctx)
  137. {
  138.    int i;
  139.    for (i = 0; i < MESA_SHADER_STAGES; i++) {
  140.       _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram[i],
  141.                                      NULL);
  142.    }
  143.    _mesa_reference_shader_program(ctx, &ctx->Shader._CurrentFragmentProgram,
  144.                                   NULL);
  145.    _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, NULL);
  146.  
  147.    /* Extended for ARB_separate_shader_objects */
  148.    _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
  149.  
  150.    assert(ctx->Shader.RefCount == 1);
  151.    mtx_destroy(&ctx->Shader.Mutex);
  152. }
  153.  
  154.  
  155. /**
  156.  * Copy string from <src> to <dst>, up to maxLength characters, returning
  157.  * length of <dst> in <length>.
  158.  * \param src  the strings source
  159.  * \param maxLength  max chars to copy
  160.  * \param length  returns number of chars copied
  161.  * \param dst  the string destination
  162.  */
  163. void
  164. _mesa_copy_string(GLchar *dst, GLsizei maxLength,
  165.                   GLsizei *length, const GLchar *src)
  166. {
  167.    GLsizei len;
  168.    for (len = 0; len < maxLength - 1 && src && src[len]; len++)
  169.       dst[len] = src[len];
  170.    if (maxLength > 0)
  171.       dst[len] = 0;
  172.    if (length)
  173.       *length = len;
  174. }
  175.  
  176.  
  177.  
  178. /**
  179.  * Confirm that the a shader type is valid and supported by the implementation
  180.  *
  181.  * \param ctx   Current GL context
  182.  * \param type  Shader target
  183.  *
  184.  */
  185. bool
  186. _mesa_validate_shader_target(const struct gl_context *ctx, GLenum type)
  187. {
  188.    /* Note: when building built-in GLSL functions, this function may be
  189.     * invoked with ctx == NULL.  In that case, we can only validate that it's
  190.     * a shader target we recognize, not that it's supported in the current
  191.     * context.  But that's fine--we don't need any further validation than
  192.     * that when building built-in GLSL functions.
  193.     */
  194.  
  195.    switch (type) {
  196.    case GL_FRAGMENT_SHADER:
  197.       return ctx == NULL || ctx->Extensions.ARB_fragment_shader;
  198.    case GL_VERTEX_SHADER:
  199.       return ctx == NULL || ctx->Extensions.ARB_vertex_shader;
  200.    case GL_GEOMETRY_SHADER_ARB:
  201.       return ctx == NULL || _mesa_has_geometry_shaders(ctx);
  202.    case GL_COMPUTE_SHADER:
  203.       return ctx == NULL || ctx->Extensions.ARB_compute_shader;
  204.    default:
  205.       return false;
  206.    }
  207. }
  208.  
  209.  
  210. static GLboolean
  211. is_program(struct gl_context *ctx, GLuint name)
  212. {
  213.    struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
  214.    return shProg ? GL_TRUE : GL_FALSE;
  215. }
  216.  
  217.  
  218. static GLboolean
  219. is_shader(struct gl_context *ctx, GLuint name)
  220. {
  221.    struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
  222.    return shader ? GL_TRUE : GL_FALSE;
  223. }
  224.  
  225.  
  226. /**
  227.  * Attach shader to a shader program.
  228.  */
  229. static void
  230. attach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
  231. {
  232.    struct gl_shader_program *shProg;
  233.    struct gl_shader *sh;
  234.    GLuint i, n;
  235.  
  236.    const bool same_type_disallowed = _mesa_is_gles(ctx);
  237.  
  238.    shProg = _mesa_lookup_shader_program_err(ctx, program, "glAttachShader");
  239.    if (!shProg)
  240.       return;
  241.  
  242.    sh = _mesa_lookup_shader_err(ctx, shader, "glAttachShader");
  243.    if (!sh) {
  244.       return;
  245.    }
  246.  
  247.    n = shProg->NumShaders;
  248.    for (i = 0; i < n; i++) {
  249.       if (shProg->Shaders[i] == sh) {
  250.          /* The shader is already attched to this program.  The
  251.           * GL_ARB_shader_objects spec says:
  252.           *
  253.           *     "The error INVALID_OPERATION is generated by AttachObjectARB
  254.           *     if <obj> is already attached to <containerObj>."
  255.           */
  256.          _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
  257.          return;
  258.       } else if (same_type_disallowed &&
  259.                  shProg->Shaders[i]->Type == sh->Type) {
  260.         /* Shader with the same type is already attached to this program,
  261.          * OpenGL ES 2.0 and 3.0 specs say:
  262.          *
  263.          *      "Multiple shader objects of the same type may not be attached
  264.          *      to a single program object. [...] The error INVALID_OPERATION
  265.          *      is generated if [...] another shader object of the same type
  266.          *      as shader is already attached to program."
  267.          */
  268.          _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
  269.          return;
  270.       }
  271.    }
  272.  
  273.    /* grow list */
  274.    shProg->Shaders = realloc(shProg->Shaders,
  275.                              (n + 1) * sizeof(struct gl_shader *));
  276.    if (!shProg->Shaders) {
  277.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
  278.       return;
  279.    }
  280.  
  281.    /* append */
  282.    shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
  283.    _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
  284.    shProg->NumShaders++;
  285. }
  286.  
  287.  
  288. static GLuint
  289. create_shader(struct gl_context *ctx, GLenum type)
  290. {
  291.    struct gl_shader *sh;
  292.    GLuint name;
  293.  
  294.    if (!_mesa_validate_shader_target(ctx, type)) {
  295.       _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
  296.       return 0;
  297.    }
  298.  
  299.    name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
  300.    sh = ctx->Driver.NewShader(ctx, name, type);
  301.    _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
  302.  
  303.    return name;
  304. }
  305.  
  306.  
  307. static GLuint
  308. create_shader_program(struct gl_context *ctx)
  309. {
  310.    GLuint name;
  311.    struct gl_shader_program *shProg;
  312.  
  313.    name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
  314.  
  315.    shProg = ctx->Driver.NewShaderProgram(name);
  316.  
  317.    _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
  318.  
  319.    assert(shProg->RefCount == 1);
  320.  
  321.    return name;
  322. }
  323.  
  324.  
  325. /**
  326.  * Delete a shader program.  Actually, just decrement the program's
  327.  * reference count and mark it as DeletePending.
  328.  * Used to implement glDeleteProgram() and glDeleteObjectARB().
  329.  */
  330. static void
  331. delete_shader_program(struct gl_context *ctx, GLuint name)
  332. {
  333.    /*
  334.     * NOTE: deleting shaders/programs works a bit differently than
  335.     * texture objects (and buffer objects, etc).  Shader/program
  336.     * handles/IDs exist in the hash table until the object is really
  337.     * deleted (refcount==0).  With texture objects, the handle/ID is
  338.     * removed from the hash table in glDeleteTextures() while the tex
  339.     * object itself might linger until its refcount goes to zero.
  340.     */
  341.    struct gl_shader_program *shProg;
  342.  
  343.    shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
  344.    if (!shProg)
  345.       return;
  346.  
  347.    if (!shProg->DeletePending) {
  348.       shProg->DeletePending = GL_TRUE;
  349.  
  350.       /* effectively, decr shProg's refcount */
  351.       _mesa_reference_shader_program(ctx, &shProg, NULL);
  352.    }
  353. }
  354.  
  355.  
  356. static void
  357. delete_shader(struct gl_context *ctx, GLuint shader)
  358. {
  359.    struct gl_shader *sh;
  360.  
  361.    sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
  362.    if (!sh)
  363.       return;
  364.  
  365.    if (!sh->DeletePending) {
  366.       sh->DeletePending = GL_TRUE;
  367.  
  368.       /* effectively, decr sh's refcount */
  369.       _mesa_reference_shader(ctx, &sh, NULL);
  370.    }
  371. }
  372.  
  373.  
  374. static void
  375. detach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
  376. {
  377.    struct gl_shader_program *shProg;
  378.    GLuint n;
  379.    GLuint i, j;
  380.  
  381.    shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
  382.    if (!shProg)
  383.       return;
  384.  
  385.    n = shProg->NumShaders;
  386.  
  387.    for (i = 0; i < n; i++) {
  388.       if (shProg->Shaders[i]->Name == shader) {
  389.          /* found it */
  390.          struct gl_shader **newList;
  391.  
  392.          /* release */
  393.          _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
  394.  
  395.          /* alloc new, smaller array */
  396.          newList = malloc((n - 1) * sizeof(struct gl_shader *));
  397.          if (!newList) {
  398.             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
  399.             return;
  400.          }
  401.          /* Copy old list entries to new list, skipping removed entry at [i] */
  402.          for (j = 0; j < i; j++) {
  403.             newList[j] = shProg->Shaders[j];
  404.          }
  405.          while (++i < n) {
  406.             newList[j++] = shProg->Shaders[i];
  407.          }
  408.  
  409.          /* Free old list and install new one */
  410.          free(shProg->Shaders);
  411.          shProg->Shaders = newList;
  412.          shProg->NumShaders = n - 1;
  413.  
  414. #ifdef DEBUG
  415.          /* sanity check - make sure the new list's entries are sensible */
  416.          for (j = 0; j < shProg->NumShaders; j++) {
  417.             assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
  418.                    shProg->Shaders[j]->Type == GL_GEOMETRY_SHADER ||
  419.                    shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
  420.             assert(shProg->Shaders[j]->RefCount > 0);
  421.          }
  422. #endif
  423.  
  424.          return;
  425.       }
  426.    }
  427.  
  428.    /* not found */
  429.    {
  430.       GLenum err;
  431.       if (is_shader(ctx, shader) || is_program(ctx, shader))
  432.          err = GL_INVALID_OPERATION;
  433.       else
  434.          err = GL_INVALID_VALUE;
  435.       _mesa_error(ctx, err, "glDetachShader(shader)");
  436.       return;
  437.    }
  438. }
  439.  
  440.  
  441. /**
  442.  * Return list of shaders attached to shader program.
  443.  */
  444. static void
  445. get_attached_shaders(struct gl_context *ctx, GLuint program, GLsizei maxCount,
  446.                      GLsizei *count, GLuint *obj)
  447. {
  448.    struct gl_shader_program *shProg;
  449.  
  450.    if (maxCount < 0) {
  451.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttachedShaders(maxCount < 0)");
  452.       return;
  453.    }
  454.  
  455.    shProg =
  456.       _mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
  457.  
  458.    if (shProg) {
  459.       GLuint i;
  460.       for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
  461.          obj[i] = shProg->Shaders[i]->Name;
  462.       }
  463.       if (count)
  464.          *count = i;
  465.    }
  466. }
  467.  
  468.  
  469. /**
  470.  * glGetHandleARB() - return ID/name of currently bound shader program.
  471.  */
  472. static GLuint
  473. get_handle(struct gl_context *ctx, GLenum pname)
  474. {
  475.    if (pname == GL_PROGRAM_OBJECT_ARB) {
  476.       if (ctx->_Shader->ActiveProgram)
  477.          return ctx->_Shader->ActiveProgram->Name;
  478.       else
  479.          return 0;
  480.    }
  481.    else {
  482.       _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
  483.       return 0;
  484.    }
  485. }
  486.  
  487.  
  488. /**
  489.  * Check if a geometry shader query is valid at this time.  If not, report an
  490.  * error and return false.
  491.  *
  492.  * From GL 3.2 section 6.1.16 (Shader and Program Queries):
  493.  *
  494.  *     "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
  495.  *     are queried for a program which has not been linked successfully, or
  496.  *     which does not contain objects to form a geometry shader, then an
  497.  *     INVALID_OPERATION error is generated."
  498.  */
  499. static bool
  500. check_gs_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
  501. {
  502.    if (shProg->LinkStatus &&
  503.        shProg->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
  504.       return true;
  505.    }
  506.  
  507.    _mesa_error(ctx, GL_INVALID_OPERATION,
  508.                "glGetProgramv(linked geometry shader required)");
  509.    return false;
  510. }
  511.  
  512.  
  513. /**
  514.  * glGetProgramiv() - get shader program state.
  515.  * Note that this is for GLSL shader programs, not ARB vertex/fragment
  516.  * programs (see glGetProgramivARB).
  517.  */
  518. static void
  519. get_programiv(struct gl_context *ctx, GLuint program, GLenum pname,
  520.               GLint *params)
  521. {
  522.    struct gl_shader_program *shProg
  523.       = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramiv(program)");
  524.  
  525.    /* Is transform feedback available in this context?
  526.     */
  527.    const bool has_xfb =
  528.       (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.EXT_transform_feedback)
  529.       || ctx->API == API_OPENGL_CORE
  530.       || _mesa_is_gles3(ctx);
  531.  
  532.    /* True if geometry shaders (of the form that was adopted into GLSL 1.50
  533.     * and GL 3.2) are available in this context
  534.     */
  535.    const bool has_core_gs = _mesa_is_desktop_gl(ctx) && ctx->Version >= 32;
  536.  
  537.    /* Are uniform buffer objects available in this context?
  538.     */
  539.    const bool has_ubo =
  540.       (ctx->API == API_OPENGL_COMPAT &&
  541.        ctx->Extensions.ARB_uniform_buffer_object)
  542.       || ctx->API == API_OPENGL_CORE
  543.       || _mesa_is_gles3(ctx);
  544.  
  545.    if (!shProg) {
  546.       return;
  547.    }
  548.  
  549.    switch (pname) {
  550.    case GL_DELETE_STATUS:
  551.       *params = shProg->DeletePending;
  552.       return;
  553.    case GL_LINK_STATUS:
  554.       *params = shProg->LinkStatus;
  555.       return;
  556.    case GL_VALIDATE_STATUS:
  557.       *params = shProg->Validated;
  558.       return;
  559.    case GL_INFO_LOG_LENGTH:
  560.       *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
  561.       return;
  562.    case GL_ATTACHED_SHADERS:
  563.       *params = shProg->NumShaders;
  564.       return;
  565.    case GL_ACTIVE_ATTRIBUTES:
  566.       *params = _mesa_count_active_attribs(shProg);
  567.       return;
  568.    case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
  569.       *params = _mesa_longest_attribute_name_length(shProg);
  570.       return;
  571.    case GL_ACTIVE_UNIFORMS:
  572.       *params = shProg->NumUserUniformStorage - shProg->NumHiddenUniforms;
  573.       return;
  574.    case GL_ACTIVE_UNIFORM_MAX_LENGTH: {
  575.       unsigned i;
  576.       GLint max_len = 0;
  577.       const unsigned num_uniforms =
  578.          shProg->NumUserUniformStorage - shProg->NumHiddenUniforms;
  579.  
  580.       for (i = 0; i < num_uniforms; i++) {
  581.          /* Add one for the terminating NUL character for a non-array, and
  582.           * 4 for the "[0]" and the NUL for an array.
  583.           */
  584.          const GLint len = strlen(shProg->UniformStorage[i].name) + 1 +
  585.              ((shProg->UniformStorage[i].array_elements != 0) ? 3 : 0);
  586.  
  587.          if (len > max_len)
  588.             max_len = len;
  589.       }
  590.  
  591.       *params = max_len;
  592.       return;
  593.    }
  594.    case GL_TRANSFORM_FEEDBACK_VARYINGS:
  595.       if (!has_xfb)
  596.          break;
  597.       *params = shProg->TransformFeedback.NumVarying;
  598.       return;
  599.    case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH: {
  600.       unsigned i;
  601.       GLint max_len = 0;
  602.       if (!has_xfb)
  603.          break;
  604.  
  605.       for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
  606.          /* Add one for the terminating NUL character.
  607.           */
  608.          const GLint len =
  609.             strlen(shProg->TransformFeedback.VaryingNames[i]) + 1;
  610.  
  611.          if (len > max_len)
  612.             max_len = len;
  613.       }
  614.  
  615.       *params = max_len;
  616.       return;
  617.    }
  618.    case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
  619.       if (!has_xfb)
  620.          break;
  621.       *params = shProg->TransformFeedback.BufferMode;
  622.       return;
  623.    case GL_GEOMETRY_VERTICES_OUT:
  624.       if (!has_core_gs)
  625.          break;
  626.       if (check_gs_query(ctx, shProg))
  627.          *params = shProg->Geom.VerticesOut;
  628.       return;
  629.    case GL_GEOMETRY_SHADER_INVOCATIONS:
  630.       if (!has_core_gs || !ctx->Extensions.ARB_gpu_shader5)
  631.          break;
  632.       if (check_gs_query(ctx, shProg))
  633.          *params = shProg->Geom.Invocations;
  634.       return;
  635.    case GL_GEOMETRY_INPUT_TYPE:
  636.       if (!has_core_gs)
  637.          break;
  638.       if (check_gs_query(ctx, shProg))
  639.          *params = shProg->Geom.InputType;
  640.       return;
  641.    case GL_GEOMETRY_OUTPUT_TYPE:
  642.       if (!has_core_gs)
  643.          break;
  644.       if (check_gs_query(ctx, shProg))
  645.          *params = shProg->Geom.OutputType;
  646.       return;
  647.    case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH: {
  648.       unsigned i;
  649.       GLint max_len = 0;
  650.  
  651.       if (!has_ubo)
  652.          break;
  653.  
  654.       for (i = 0; i < shProg->NumUniformBlocks; i++) {
  655.          /* Add one for the terminating NUL character.
  656.           */
  657.          const GLint len = strlen(shProg->UniformBlocks[i].Name) + 1;
  658.  
  659.          if (len > max_len)
  660.             max_len = len;
  661.       }
  662.  
  663.       *params = max_len;
  664.       return;
  665.    }
  666.    case GL_ACTIVE_UNIFORM_BLOCKS:
  667.       if (!has_ubo)
  668.          break;
  669.  
  670.       *params = shProg->NumUniformBlocks;
  671.       return;
  672.    case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
  673.       /* This enum isn't part of the OES extension for OpenGL ES 2.0.  It is
  674.        * only available with desktop OpenGL 3.0+ with the
  675.        * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
  676.        *
  677.        * On desktop, we ignore the 3.0+ requirement because it is silly.
  678.        */
  679.       if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
  680.          break;
  681.  
  682.       *params = shProg->BinaryRetreivableHint;
  683.       return;
  684.    case GL_PROGRAM_BINARY_LENGTH:
  685.       *params = 0;
  686.       return;
  687.    case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
  688.       if (!ctx->Extensions.ARB_shader_atomic_counters)
  689.          break;
  690.  
  691.       *params = shProg->NumAtomicBuffers;
  692.       return;
  693.    case GL_COMPUTE_WORK_GROUP_SIZE: {
  694.       int i;
  695.       if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_compute_shader)
  696.          break;
  697.       if (!shProg->LinkStatus) {
  698.          _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(program not "
  699.                      "linked)");
  700.          return;
  701.       }
  702.       if (shProg->_LinkedShaders[MESA_SHADER_COMPUTE] == NULL) {
  703.          _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(no compute "
  704.                      "shaders)");
  705.          return;
  706.       }
  707.       for (i = 0; i < 3; i++)
  708.          params[i] = shProg->Comp.LocalSize[i];
  709.       return;
  710.    }
  711.    case GL_PROGRAM_SEPARABLE:
  712.       *params = shProg->SeparateShader;
  713.       return;
  714.    default:
  715.       break;
  716.    }
  717.  
  718.    _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname=%s)",
  719.                _mesa_lookup_enum_by_nr(pname));
  720. }
  721.  
  722.  
  723. /**
  724.  * glGetShaderiv() - get GLSL shader state
  725.  */
  726. static void
  727. get_shaderiv(struct gl_context *ctx, GLuint name, GLenum pname, GLint *params)
  728. {
  729.    struct gl_shader *shader =
  730.       _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
  731.  
  732.    if (!shader) {
  733.       return;
  734.    }
  735.  
  736.    switch (pname) {
  737.    case GL_SHADER_TYPE:
  738.       *params = shader->Type;
  739.       break;
  740.    case GL_DELETE_STATUS:
  741.       *params = shader->DeletePending;
  742.       break;
  743.    case GL_COMPILE_STATUS:
  744.       *params = shader->CompileStatus;
  745.       break;
  746.    case GL_INFO_LOG_LENGTH:
  747.       *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
  748.       break;
  749.    case GL_SHADER_SOURCE_LENGTH:
  750.       *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
  751.       break;
  752.    default:
  753.       _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
  754.       return;
  755.    }
  756. }
  757.  
  758.  
  759. static void
  760. get_program_info_log(struct gl_context *ctx, GLuint program, GLsizei bufSize,
  761.                      GLsizei *length, GLchar *infoLog)
  762. {
  763.    struct gl_shader_program *shProg;
  764.  
  765.    /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
  766.     * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
  767.     *
  768.     *     "If a negative number is provided where an argument of type sizei or
  769.     *     sizeiptr is specified, an INVALID_VALUE error is generated."
  770.     */
  771.    if (bufSize < 0) {
  772.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(bufSize < 0)");
  773.       return;
  774.    }
  775.  
  776.    shProg = _mesa_lookup_shader_program_err(ctx, program,
  777.                                             "glGetProgramInfoLog(program)");
  778.    if (!shProg) {
  779.       return;
  780.    }
  781.  
  782.    _mesa_copy_string(infoLog, bufSize, length, shProg->InfoLog);
  783. }
  784.  
  785.  
  786. static void
  787. get_shader_info_log(struct gl_context *ctx, GLuint shader, GLsizei bufSize,
  788.                     GLsizei *length, GLchar *infoLog)
  789. {
  790.    struct gl_shader *sh;
  791.  
  792.    /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
  793.     * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
  794.     *
  795.     *     "If a negative number is provided where an argument of type sizei or
  796.     *     sizeiptr is specified, an INVALID_VALUE error is generated."
  797.     */
  798.    if (bufSize < 0) {
  799.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(bufSize < 0)");
  800.       return;
  801.    }
  802.  
  803.    sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderInfoLog(shader)");
  804.    if (!sh) {
  805.       return;
  806.    }
  807.  
  808.    _mesa_copy_string(infoLog, bufSize, length, sh->InfoLog);
  809. }
  810.  
  811.  
  812. /**
  813.  * Return shader source code.
  814.  */
  815. static void
  816. get_shader_source(struct gl_context *ctx, GLuint shader, GLsizei maxLength,
  817.                   GLsizei *length, GLchar *sourceOut)
  818. {
  819.    struct gl_shader *sh;
  820.  
  821.    if (maxLength < 0) {
  822.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(bufSize < 0)");
  823.       return;
  824.    }
  825.  
  826.    sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
  827.    if (!sh) {
  828.       return;
  829.    }
  830.    _mesa_copy_string(sourceOut, maxLength, length, sh->Source);
  831. }
  832.  
  833.  
  834. /**
  835.  * Set/replace shader source code.  A helper function used by
  836.  * glShaderSource[ARB].
  837.  */
  838. static void
  839. shader_source(struct gl_context *ctx, GLuint shader, const GLchar *source)
  840. {
  841.    struct gl_shader *sh;
  842.  
  843.    sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource");
  844.    if (!sh)
  845.       return;
  846.  
  847.    /* free old shader source string and install new one */
  848.    free((void *)sh->Source);
  849.    sh->Source = source;
  850.    sh->CompileStatus = GL_FALSE;
  851. #ifdef DEBUG
  852.    sh->SourceChecksum = _mesa_str_checksum(sh->Source);
  853. #endif
  854. }
  855.  
  856.  
  857. /**
  858.  * Compile a shader.
  859.  */
  860. static void
  861. compile_shader(struct gl_context *ctx, GLuint shaderObj)
  862. {
  863.    struct gl_shader *sh;
  864.  
  865.    sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
  866.    if (!sh)
  867.       return;
  868.  
  869.    if (!sh->Source) {
  870.       /* If the user called glCompileShader without first calling
  871.        * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
  872.        */
  873.       sh->CompileStatus = GL_FALSE;
  874.    } else {
  875.       if (ctx->_Shader->Flags & GLSL_DUMP) {
  876.          _mesa_log("GLSL source for %s shader %d:\n",
  877.                  _mesa_shader_stage_to_string(sh->Stage), sh->Name);
  878.          _mesa_log("%s\n", sh->Source);
  879.       }
  880.  
  881.       /* this call will set the shader->CompileStatus field to indicate if
  882.        * compilation was successful.
  883.        */
  884.       _mesa_glsl_compile_shader(ctx, sh, false, false);
  885.  
  886.       if (ctx->_Shader->Flags & GLSL_LOG) {
  887.          _mesa_write_shader_to_file(sh);
  888.       }
  889.  
  890.       if (ctx->_Shader->Flags & GLSL_DUMP) {
  891.          if (sh->CompileStatus) {
  892.             _mesa_log("GLSL IR for shader %d:\n", sh->Name);
  893.             _mesa_print_ir(_mesa_get_log_file(), sh->ir, NULL);
  894.             _mesa_log("\n\n");
  895.          } else {
  896.             _mesa_log("GLSL shader %d failed to compile.\n", sh->Name);
  897.          }
  898.          if (sh->InfoLog && sh->InfoLog[0] != 0) {
  899.             _mesa_log("GLSL shader %d info log:\n", sh->Name);
  900.             _mesa_log("%s\n", sh->InfoLog);
  901.          }
  902.       }
  903.    }
  904.  
  905.    if (!sh->CompileStatus) {
  906.       if (ctx->_Shader->Flags & GLSL_DUMP_ON_ERROR) {
  907.          _mesa_log("GLSL source for %s shader %d:\n",
  908.                  _mesa_shader_stage_to_string(sh->Stage), sh->Name);
  909.          _mesa_log("%s\n", sh->Source);
  910.          _mesa_log("Info Log:\n%s\n", sh->InfoLog);
  911.       }
  912.  
  913.       if (ctx->_Shader->Flags & GLSL_REPORT_ERRORS) {
  914.          _mesa_debug(ctx, "Error compiling shader %u:\n%s\n",
  915.                      sh->Name, sh->InfoLog);
  916.       }
  917.    }
  918. }
  919.  
  920.  
  921. /**
  922.  * Link a program's shaders.
  923.  */
  924. static void
  925. link_program(struct gl_context *ctx, GLuint program)
  926. {
  927.    struct gl_shader_program *shProg;
  928.  
  929.    shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram");
  930.    if (!shProg)
  931.       return;
  932.  
  933.    /* From the ARB_transform_feedback2 specification:
  934.     * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
  935.     *  the name of a program being used by one or more transform feedback
  936.     *  objects, even if the objects are not currently bound or are paused."
  937.     */
  938.    if (_mesa_transform_feedback_is_using_program(ctx, shProg)) {
  939.       _mesa_error(ctx, GL_INVALID_OPERATION,
  940.                   "glLinkProgram(transform feedback is using the program)");
  941.       return;
  942.    }
  943.  
  944.    FLUSH_VERTICES(ctx, _NEW_PROGRAM);
  945.  
  946.    _mesa_glsl_link_shader(ctx, shProg);
  947.  
  948.    if (shProg->LinkStatus == GL_FALSE &&
  949.        (ctx->_Shader->Flags & GLSL_REPORT_ERRORS)) {
  950.       _mesa_debug(ctx, "Error linking program %u:\n%s\n",
  951.                   shProg->Name, shProg->InfoLog);
  952.    }
  953.  
  954.    /* debug code */
  955.    if (0) {
  956.       GLuint i;
  957.  
  958.       printf("Link %u shaders in program %u: %s\n",
  959.                    shProg->NumShaders, shProg->Name,
  960.                    shProg->LinkStatus ? "Success" : "Failed");
  961.  
  962.       for (i = 0; i < shProg->NumShaders; i++) {
  963.          printf(" shader %u, type 0x%x\n",
  964.                       shProg->Shaders[i]->Name,
  965.                       shProg->Shaders[i]->Type);
  966.       }
  967.    }
  968. }
  969.  
  970.  
  971. /**
  972.  * Print basic shader info (for debug).
  973.  */
  974. static void
  975. print_shader_info(const struct gl_shader_program *shProg)
  976. {
  977.    GLuint i;
  978.  
  979.    printf("Mesa: glUseProgram(%u)\n", shProg->Name);
  980.    for (i = 0; i < shProg->NumShaders; i++) {
  981.       printf("  %s shader %u, checksum %u\n",
  982.              _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
  983.              shProg->Shaders[i]->Name,
  984.              shProg->Shaders[i]->SourceChecksum);
  985.    }
  986.    if (shProg->_LinkedShaders[MESA_SHADER_VERTEX])
  987.       printf("  vert prog %u\n",
  988.              shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program->Id);
  989.    if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT])
  990.       printf("  frag prog %u\n",
  991.              shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->Id);
  992.    if (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY])
  993.       printf("  geom prog %u\n",
  994.              shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program->Id);
  995. }
  996.  
  997.  
  998. /**
  999.  * Use the named shader program for subsequent glUniform calls
  1000.  */
  1001. void
  1002. _mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
  1003.                      const char *caller)
  1004. {
  1005.    if ((shProg != NULL) && !shProg->LinkStatus) {
  1006.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1007.                   "%s(program %u not linked)", caller, shProg->Name);
  1008.       return;
  1009.    }
  1010.  
  1011.    if (ctx->Shader.ActiveProgram != shProg) {
  1012.       _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
  1013.    }
  1014. }
  1015.  
  1016.  
  1017. static void
  1018. use_shader_program(struct gl_context *ctx, gl_shader_stage stage,
  1019.                    struct gl_shader_program *shProg,
  1020.                    struct gl_pipeline_object *shTarget)
  1021. {
  1022.    struct gl_shader_program **target;
  1023.  
  1024.    target = &shTarget->CurrentProgram[stage];
  1025.    if ((shProg != NULL) && (shProg->_LinkedShaders[stage] == NULL))
  1026.       shProg = NULL;
  1027.  
  1028.    if (*target != shProg) {
  1029.       /* Program is current, flush it */
  1030.       if (shTarget == ctx->_Shader) {
  1031.          FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
  1032.       }
  1033.  
  1034.       /* If the shader is also bound as the current rendering shader, unbind
  1035.        * it from that binding point as well.  This ensures that the correct
  1036.        * semantics of glDeleteProgram are maintained.
  1037.        */
  1038.       switch (stage) {
  1039.       case MESA_SHADER_VERTEX:
  1040.          /* Empty for now. */
  1041.          break;
  1042.       case MESA_SHADER_GEOMETRY:
  1043.          /* Empty for now. */
  1044.          break;
  1045.       case MESA_SHADER_COMPUTE:
  1046.          /* Empty for now. */
  1047.          break;
  1048.       case MESA_SHADER_FRAGMENT:
  1049.          if (*target == ctx->_Shader->_CurrentFragmentProgram) {
  1050.             _mesa_reference_shader_program(ctx,
  1051.                                            &ctx->_Shader->_CurrentFragmentProgram,
  1052.                                            NULL);
  1053.          }
  1054.          break;
  1055.       }
  1056.  
  1057.       _mesa_reference_shader_program(ctx, target, shProg);
  1058.       return;
  1059.    }
  1060. }
  1061.  
  1062.  
  1063. /**
  1064.  * Use the named shader program for subsequent rendering.
  1065.  */
  1066. void
  1067. _mesa_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
  1068. {
  1069.    int i;
  1070.    for (i = 0; i < MESA_SHADER_STAGES; i++)
  1071.       use_shader_program(ctx, i, shProg, &ctx->Shader);
  1072.    _mesa_active_program(ctx, shProg, "glUseProgram");
  1073.  
  1074.    if (ctx->Driver.UseProgram)
  1075.       ctx->Driver.UseProgram(ctx, shProg);
  1076. }
  1077.  
  1078.  
  1079. /**
  1080.  * Do validation of the given shader program.
  1081.  * \param errMsg  returns error message if validation fails.
  1082.  * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
  1083.  */
  1084. static GLboolean
  1085. validate_shader_program(const struct gl_shader_program *shProg,
  1086.                         char *errMsg)
  1087. {
  1088.    if (!shProg->LinkStatus) {
  1089.       return GL_FALSE;
  1090.    }
  1091.  
  1092.    /* From the GL spec, a program is invalid if any of these are true:
  1093.  
  1094.      any two active samplers in the current program object are of
  1095.      different types, but refer to the same texture image unit,
  1096.  
  1097.      any active sampler in the current program object refers to a texture
  1098.      image unit where fixed-function fragment processing accesses a
  1099.      texture target that does not match the sampler type, or
  1100.  
  1101.      the sum of the number of active samplers in the program and the
  1102.      number of texture image units enabled for fixed-function fragment
  1103.      processing exceeds the combined limit on the total number of texture
  1104.      image units allowed.
  1105.    */
  1106.  
  1107.    /*
  1108.     * Check: any two active samplers in the current program object are of
  1109.     * different types, but refer to the same texture image unit,
  1110.     */
  1111.    if (!_mesa_sampler_uniforms_are_valid(shProg, errMsg, 100))
  1112.       return GL_FALSE;
  1113.  
  1114.    return GL_TRUE;
  1115. }
  1116.  
  1117.  
  1118. /**
  1119.  * Called via glValidateProgram()
  1120.  */
  1121. static void
  1122. validate_program(struct gl_context *ctx, GLuint program)
  1123. {
  1124.    struct gl_shader_program *shProg;
  1125.    char errMsg[100] = "";
  1126.  
  1127.    shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
  1128.    if (!shProg) {
  1129.       return;
  1130.    }
  1131.  
  1132.    shProg->Validated = validate_shader_program(shProg, errMsg);
  1133.    if (!shProg->Validated) {
  1134.       /* update info log */
  1135.       if (shProg->InfoLog) {
  1136.          ralloc_free(shProg->InfoLog);
  1137.       }
  1138.       shProg->InfoLog = ralloc_strdup(shProg, errMsg);
  1139.    }
  1140. }
  1141.  
  1142.  
  1143.  
  1144. void GLAPIENTRY
  1145. _mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
  1146. {
  1147.    GET_CURRENT_CONTEXT(ctx);
  1148.    attach_shader(ctx, program, shader);
  1149. }
  1150.  
  1151.  
  1152. void GLAPIENTRY
  1153. _mesa_AttachShader(GLuint program, GLuint shader)
  1154. {
  1155.    GET_CURRENT_CONTEXT(ctx);
  1156.    attach_shader(ctx, program, shader);
  1157. }
  1158.  
  1159.  
  1160. void GLAPIENTRY
  1161. _mesa_CompileShader(GLhandleARB shaderObj)
  1162. {
  1163.    GET_CURRENT_CONTEXT(ctx);
  1164.    if (MESA_VERBOSE & VERBOSE_API)
  1165.       _mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
  1166.    compile_shader(ctx, shaderObj);
  1167. }
  1168.  
  1169.  
  1170. GLuint GLAPIENTRY
  1171. _mesa_CreateShader(GLenum type)
  1172. {
  1173.    GET_CURRENT_CONTEXT(ctx);
  1174.    if (MESA_VERBOSE & VERBOSE_API)
  1175.       _mesa_debug(ctx, "glCreateShader %s\n", _mesa_lookup_enum_by_nr(type));
  1176.    return create_shader(ctx, type);
  1177. }
  1178.  
  1179.  
  1180. GLhandleARB GLAPIENTRY
  1181. _mesa_CreateShaderObjectARB(GLenum type)
  1182. {
  1183.    GET_CURRENT_CONTEXT(ctx);
  1184.    return create_shader(ctx, type);
  1185. }
  1186.  
  1187.  
  1188. GLuint GLAPIENTRY
  1189. _mesa_CreateProgram(void)
  1190. {
  1191.    GET_CURRENT_CONTEXT(ctx);
  1192.    if (MESA_VERBOSE & VERBOSE_API)
  1193.       _mesa_debug(ctx, "glCreateProgram\n");
  1194.    return create_shader_program(ctx);
  1195. }
  1196.  
  1197.  
  1198. GLhandleARB GLAPIENTRY
  1199. _mesa_CreateProgramObjectARB(void)
  1200. {
  1201.    GET_CURRENT_CONTEXT(ctx);
  1202.    return create_shader_program(ctx);
  1203. }
  1204.  
  1205.  
  1206. void GLAPIENTRY
  1207. _mesa_DeleteObjectARB(GLhandleARB obj)
  1208. {
  1209.    if (MESA_VERBOSE & VERBOSE_API) {
  1210.       GET_CURRENT_CONTEXT(ctx);
  1211.       _mesa_debug(ctx, "glDeleteObjectARB(%u)\n", obj);
  1212.    }
  1213.  
  1214.    if (obj) {
  1215.       GET_CURRENT_CONTEXT(ctx);
  1216.       FLUSH_VERTICES(ctx, 0);
  1217.       if (is_program(ctx, obj)) {
  1218.          delete_shader_program(ctx, obj);
  1219.       }
  1220.       else if (is_shader(ctx, obj)) {
  1221.          delete_shader(ctx, obj);
  1222.       }
  1223.       else {
  1224.          /* error? */
  1225.       }
  1226.    }
  1227. }
  1228.  
  1229.  
  1230. void GLAPIENTRY
  1231. _mesa_DeleteProgram(GLuint name)
  1232. {
  1233.    if (name) {
  1234.       GET_CURRENT_CONTEXT(ctx);
  1235.       FLUSH_VERTICES(ctx, 0);
  1236.       delete_shader_program(ctx, name);
  1237.    }
  1238. }
  1239.  
  1240.  
  1241. void GLAPIENTRY
  1242. _mesa_DeleteShader(GLuint name)
  1243. {
  1244.    if (name) {
  1245.       GET_CURRENT_CONTEXT(ctx);
  1246.       FLUSH_VERTICES(ctx, 0);
  1247.       delete_shader(ctx, name);
  1248.    }
  1249. }
  1250.  
  1251.  
  1252. void GLAPIENTRY
  1253. _mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
  1254. {
  1255.    GET_CURRENT_CONTEXT(ctx);
  1256.    detach_shader(ctx, program, shader);
  1257. }
  1258.  
  1259.  
  1260. void GLAPIENTRY
  1261. _mesa_DetachShader(GLuint program, GLuint shader)
  1262. {
  1263.    GET_CURRENT_CONTEXT(ctx);
  1264.    detach_shader(ctx, program, shader);
  1265. }
  1266.  
  1267.  
  1268. void GLAPIENTRY
  1269. _mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
  1270.                             GLsizei * count, GLhandleARB * obj)
  1271. {
  1272.    GET_CURRENT_CONTEXT(ctx);
  1273.    get_attached_shaders(ctx, container, maxCount, count, obj);
  1274. }
  1275.  
  1276.  
  1277. void GLAPIENTRY
  1278. _mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
  1279.                          GLsizei *count, GLuint *obj)
  1280. {
  1281.    GET_CURRENT_CONTEXT(ctx);
  1282.    get_attached_shaders(ctx, program, maxCount, count, obj);
  1283. }
  1284.  
  1285.  
  1286. void GLAPIENTRY
  1287. _mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
  1288.                     GLcharARB * infoLog)
  1289. {
  1290.    GET_CURRENT_CONTEXT(ctx);
  1291.    if (is_program(ctx, object)) {
  1292.       get_program_info_log(ctx, object, maxLength, length, infoLog);
  1293.    }
  1294.    else if (is_shader(ctx, object)) {
  1295.       get_shader_info_log(ctx, object, maxLength, length, infoLog);
  1296.    }
  1297.    else {
  1298.       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
  1299.    }
  1300. }
  1301.  
  1302.  
  1303. void GLAPIENTRY
  1304. _mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
  1305. {
  1306.    GET_CURRENT_CONTEXT(ctx);
  1307.    /* Implement in terms of GetProgramiv, GetShaderiv */
  1308.    if (is_program(ctx, object)) {
  1309.       if (pname == GL_OBJECT_TYPE_ARB) {
  1310.          *params = GL_PROGRAM_OBJECT_ARB;
  1311.       }
  1312.       else {
  1313.          get_programiv(ctx, object, pname, params);
  1314.       }
  1315.    }
  1316.    else if (is_shader(ctx, object)) {
  1317.       if (pname == GL_OBJECT_TYPE_ARB) {
  1318.          *params = GL_SHADER_OBJECT_ARB;
  1319.       }
  1320.       else {
  1321.          get_shaderiv(ctx, object, pname, params);
  1322.       }
  1323.    }
  1324.    else {
  1325.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
  1326.    }
  1327. }
  1328.  
  1329.  
  1330. void GLAPIENTRY
  1331. _mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
  1332.                               GLfloat *params)
  1333. {
  1334.    GLint iparams[1];  /* XXX is one element enough? */
  1335.    _mesa_GetObjectParameterivARB(object, pname, iparams);
  1336.    params[0] = (GLfloat) iparams[0];
  1337. }
  1338.  
  1339.  
  1340. void GLAPIENTRY
  1341. _mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
  1342. {
  1343.    GET_CURRENT_CONTEXT(ctx);
  1344.    get_programiv(ctx, program, pname, params);
  1345. }
  1346.  
  1347.  
  1348. void GLAPIENTRY
  1349. _mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
  1350. {
  1351.    GET_CURRENT_CONTEXT(ctx);
  1352.    get_shaderiv(ctx, shader, pname, params);
  1353. }
  1354.  
  1355.  
  1356. void GLAPIENTRY
  1357. _mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
  1358.                         GLsizei *length, GLchar *infoLog)
  1359. {
  1360.    GET_CURRENT_CONTEXT(ctx);
  1361.    get_program_info_log(ctx, program, bufSize, length, infoLog);
  1362. }
  1363.  
  1364.  
  1365. void GLAPIENTRY
  1366. _mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
  1367.                        GLsizei *length, GLchar *infoLog)
  1368. {
  1369.    GET_CURRENT_CONTEXT(ctx);
  1370.    get_shader_info_log(ctx, shader, bufSize, length, infoLog);
  1371. }
  1372.  
  1373.  
  1374. void GLAPIENTRY
  1375. _mesa_GetShaderSource(GLhandleARB shader, GLsizei maxLength,
  1376.                          GLsizei *length, GLcharARB *sourceOut)
  1377. {
  1378.    GET_CURRENT_CONTEXT(ctx);
  1379.    get_shader_source(ctx, shader, maxLength, length, sourceOut);
  1380. }
  1381.  
  1382.  
  1383. GLhandleARB GLAPIENTRY
  1384. _mesa_GetHandleARB(GLenum pname)
  1385. {
  1386.    GET_CURRENT_CONTEXT(ctx);
  1387.    return get_handle(ctx, pname);
  1388. }
  1389.  
  1390.  
  1391. GLboolean GLAPIENTRY
  1392. _mesa_IsProgram(GLuint name)
  1393. {
  1394.    GET_CURRENT_CONTEXT(ctx);
  1395.    return is_program(ctx, name);
  1396. }
  1397.  
  1398.  
  1399. GLboolean GLAPIENTRY
  1400. _mesa_IsShader(GLuint name)
  1401. {
  1402.    GET_CURRENT_CONTEXT(ctx);
  1403.    return is_shader(ctx, name);
  1404. }
  1405.  
  1406.  
  1407. void GLAPIENTRY
  1408. _mesa_LinkProgram(GLhandleARB programObj)
  1409. {
  1410.    GET_CURRENT_CONTEXT(ctx);
  1411.    link_program(ctx, programObj);
  1412. }
  1413.  
  1414.  
  1415.  
  1416. /**
  1417.  * Read shader source code from a file.
  1418.  * Useful for debugging to override an app's shader.
  1419.  */
  1420. static GLcharARB *
  1421. read_shader(const char *fname)
  1422. {
  1423.    int shader_size = 0;
  1424.    FILE *f = fopen(fname, "r");
  1425.    GLcharARB *buffer, *shader;
  1426.    int len;
  1427.  
  1428.    if (!f) {
  1429.       return NULL;
  1430.    }
  1431.  
  1432.    /* allocate enough room for the entire shader */
  1433.    fseek(f, 0, SEEK_END);
  1434.    shader_size = ftell(f);
  1435.    rewind(f);
  1436.    assert(shader_size);
  1437.  
  1438.    /* add one for terminating zero */
  1439.    shader_size++;
  1440.  
  1441.    buffer = malloc(shader_size);
  1442.    assert(buffer);
  1443.  
  1444.    len = fread(buffer, 1, shader_size, f);
  1445.    buffer[len] = 0;
  1446.  
  1447.    fclose(f);
  1448.  
  1449.    shader = strdup(buffer);
  1450.    free(buffer);
  1451.  
  1452.    return shader;
  1453. }
  1454.  
  1455.  
  1456. /**
  1457.  * Called via glShaderSource() and glShaderSourceARB() API functions.
  1458.  * Basically, concatenate the source code strings into one long string
  1459.  * and pass it to _mesa_shader_source().
  1460.  */
  1461. void GLAPIENTRY
  1462. _mesa_ShaderSource(GLhandleARB shaderObj, GLsizei count,
  1463.                       const GLcharARB * const * string, const GLint * length)
  1464. {
  1465.    GET_CURRENT_CONTEXT(ctx);
  1466.    GLint *offsets;
  1467.    GLsizei i, totalLength;
  1468.    GLcharARB *source;
  1469.    GLuint checksum;
  1470.  
  1471.    if (!shaderObj || string == NULL) {
  1472.       _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
  1473.       return;
  1474.    }
  1475.  
  1476.    /*
  1477.     * This array holds offsets of where the appropriate string ends, thus the
  1478.     * last element will be set to the total length of the source code.
  1479.     */
  1480.    offsets = malloc(count * sizeof(GLint));
  1481.    if (offsets == NULL) {
  1482.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
  1483.       return;
  1484.    }
  1485.  
  1486.    for (i = 0; i < count; i++) {
  1487.       if (string[i] == NULL) {
  1488.          free((GLvoid *) offsets);
  1489.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1490.                      "glShaderSourceARB(null string)");
  1491.          return;
  1492.       }
  1493.       if (length == NULL || length[i] < 0)
  1494.          offsets[i] = strlen(string[i]);
  1495.       else
  1496.          offsets[i] = length[i];
  1497.       /* accumulate string lengths */
  1498.       if (i > 0)
  1499.          offsets[i] += offsets[i - 1];
  1500.    }
  1501.  
  1502.    /* Total length of source string is sum off all strings plus two.
  1503.     * One extra byte for terminating zero, another extra byte to silence
  1504.     * valgrind warnings in the parser/grammer code.
  1505.     */
  1506.    totalLength = offsets[count - 1] + 2;
  1507.    source = malloc(totalLength * sizeof(GLcharARB));
  1508.    if (source == NULL) {
  1509.       free((GLvoid *) offsets);
  1510.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
  1511.       return;
  1512.    }
  1513.  
  1514.    for (i = 0; i < count; i++) {
  1515.       GLint start = (i > 0) ? offsets[i - 1] : 0;
  1516.       memcpy(source + start, string[i],
  1517.              (offsets[i] - start) * sizeof(GLcharARB));
  1518.    }
  1519.    source[totalLength - 1] = '\0';
  1520.    source[totalLength - 2] = '\0';
  1521.  
  1522.    if (SHADER_SUBST) {
  1523.       /* Compute the shader's source code checksum then try to open a file
  1524.        * named newshader_<CHECKSUM>.  If it exists, use it in place of the
  1525.        * original shader source code.  For debugging.
  1526.        */
  1527.       char filename[100];
  1528.       GLcharARB *newSource;
  1529.  
  1530.       checksum = _mesa_str_checksum(source);
  1531.  
  1532.       _mesa_snprintf(filename, sizeof(filename), "newshader_%d", checksum);
  1533.  
  1534.       newSource = read_shader(filename);
  1535.       if (newSource) {
  1536.          fprintf(stderr, "Mesa: Replacing shader %u chksum=%d with %s\n",
  1537.                        shaderObj, checksum, filename);
  1538.          free(source);
  1539.          source = newSource;
  1540.       }
  1541.    }
  1542.  
  1543.    shader_source(ctx, shaderObj, source);
  1544.  
  1545.    if (SHADER_SUBST) {
  1546.       struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
  1547.       if (sh)
  1548.          sh->SourceChecksum = checksum; /* save original checksum */
  1549.    }
  1550.  
  1551.    free(offsets);
  1552. }
  1553.  
  1554.  
  1555. void GLAPIENTRY
  1556. _mesa_UseProgram(GLhandleARB program)
  1557. {
  1558.    GET_CURRENT_CONTEXT(ctx);
  1559.    struct gl_shader_program *shProg;
  1560.  
  1561.    if (_mesa_is_xfb_active_and_unpaused(ctx)) {
  1562.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1563.                   "glUseProgram(transform feedback active)");
  1564.       return;
  1565.    }
  1566.  
  1567.    if (program) {
  1568.       shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
  1569.       if (!shProg) {
  1570.          return;
  1571.       }
  1572.       if (!shProg->LinkStatus) {
  1573.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1574.                      "glUseProgram(program %u not linked)", program);
  1575.          return;
  1576.       }
  1577.  
  1578.       /* debug code */
  1579.       if (ctx->_Shader->Flags & GLSL_USE_PROG) {
  1580.          print_shader_info(shProg);
  1581.       }
  1582.    }
  1583.    else {
  1584.       shProg = NULL;
  1585.    }
  1586.  
  1587.    /* The ARB_separate_shader_object spec says:
  1588.     *
  1589.     *     "The executable code for an individual shader stage is taken from
  1590.     *     the current program for that stage.  If there is a current program
  1591.     *     object established by UseProgram, that program is considered current
  1592.     *     for all stages.  Otherwise, if there is a bound program pipeline
  1593.     *     object (section 2.14.PPO), the program bound to the appropriate
  1594.     *     stage of the pipeline object is considered current."
  1595.     */
  1596.    if (program) {
  1597.       /* Attach shader state to the binding point */
  1598.       _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
  1599.       /* Update the program */
  1600.       _mesa_use_program(ctx, shProg);
  1601.    } else {
  1602.       /* Must be done first: detach the progam */
  1603.       _mesa_use_program(ctx, shProg);
  1604.       /* Unattach shader_state binding point */
  1605.       _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
  1606.       /* If a pipeline was bound, rebind it */
  1607.       if (ctx->Pipeline.Current) {
  1608.          _mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
  1609.       }
  1610.    }
  1611. }
  1612.  
  1613.  
  1614. void GLAPIENTRY
  1615. _mesa_ValidateProgram(GLhandleARB program)
  1616. {
  1617.    GET_CURRENT_CONTEXT(ctx);
  1618.    validate_program(ctx, program);
  1619. }
  1620.  
  1621.  
  1622. /**
  1623.  * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
  1624.  */
  1625. void GLAPIENTRY
  1626. _mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
  1627.                                GLint* range, GLint* precision)
  1628. {
  1629.    const struct gl_program_constants *limits;
  1630.    const struct gl_precision *p;
  1631.    GET_CURRENT_CONTEXT(ctx);
  1632.  
  1633.    switch (shadertype) {
  1634.    case GL_VERTEX_SHADER:
  1635.       limits = &ctx->Const.Program[MESA_SHADER_VERTEX];
  1636.       break;
  1637.    case GL_FRAGMENT_SHADER:
  1638.       limits = &ctx->Const.Program[MESA_SHADER_FRAGMENT];
  1639.       break;
  1640.    default:
  1641.       _mesa_error(ctx, GL_INVALID_ENUM,
  1642.                   "glGetShaderPrecisionFormat(shadertype)");
  1643.       return;
  1644.    }
  1645.  
  1646.    switch (precisiontype) {
  1647.    case GL_LOW_FLOAT:
  1648.       p = &limits->LowFloat;
  1649.       break;
  1650.    case GL_MEDIUM_FLOAT:
  1651.       p = &limits->MediumFloat;
  1652.       break;
  1653.    case GL_HIGH_FLOAT:
  1654.       p = &limits->HighFloat;
  1655.       break;
  1656.    case GL_LOW_INT:
  1657.       p = &limits->LowInt;
  1658.       break;
  1659.    case GL_MEDIUM_INT:
  1660.       p = &limits->MediumInt;
  1661.       break;
  1662.    case GL_HIGH_INT:
  1663.       p = &limits->HighInt;
  1664.       break;
  1665.    default:
  1666.       _mesa_error(ctx, GL_INVALID_ENUM,
  1667.                   "glGetShaderPrecisionFormat(precisiontype)");
  1668.       return;
  1669.    }
  1670.  
  1671.    range[0] = p->RangeMin;
  1672.    range[1] = p->RangeMax;
  1673.    precision[0] = p->Precision;
  1674. }
  1675.  
  1676.  
  1677. /**
  1678.  * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
  1679.  */
  1680. void GLAPIENTRY
  1681. _mesa_ReleaseShaderCompiler(void)
  1682. {
  1683.    _mesa_destroy_shader_compiler_caches();
  1684. }
  1685.  
  1686.  
  1687. /**
  1688.  * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
  1689.  */
  1690. void GLAPIENTRY
  1691. _mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
  1692.                    const void* binary, GLint length)
  1693. {
  1694.    GET_CURRENT_CONTEXT(ctx);
  1695.    (void) n;
  1696.    (void) shaders;
  1697.    (void) binaryformat;
  1698.    (void) binary;
  1699.    (void) length;
  1700.    _mesa_error(ctx, GL_INVALID_OPERATION, "glShaderBinary");
  1701. }
  1702.  
  1703.  
  1704. void GLAPIENTRY
  1705. _mesa_GetProgramBinary(GLuint program, GLsizei bufSize, GLsizei *length,
  1706.                        GLenum *binaryFormat, GLvoid *binary)
  1707. {
  1708.    struct gl_shader_program *shProg;
  1709.    GLsizei length_dummy;
  1710.    GET_CURRENT_CONTEXT(ctx);
  1711.  
  1712.    if (bufSize < 0){
  1713.       _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramBinary(bufSize < 0)");
  1714.       return;
  1715.    }
  1716.  
  1717.    shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramBinary");
  1718.    if (!shProg)
  1719.       return;
  1720.  
  1721.    /* The ARB_get_program_binary spec says:
  1722.     *
  1723.     *     "If <length> is NULL, then no length is returned."
  1724.     *
  1725.     * Ensure that length always points to valid storage to avoid multiple NULL
  1726.     * pointer checks below.
  1727.     */
  1728.    if (length == NULL)
  1729.       length = &length_dummy;
  1730.  
  1731.  
  1732.    /* The ARB_get_program_binary spec says:
  1733.     *
  1734.     *     "When a program object's LINK_STATUS is FALSE, its program binary
  1735.     *     length is zero, and a call to GetProgramBinary will generate an
  1736.     *     INVALID_OPERATION error.
  1737.     */
  1738.    if (!shProg->LinkStatus) {
  1739.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1740.                   "glGetProgramBinary(program %u not linked)",
  1741.                   shProg->Name);
  1742.       *length = 0;
  1743.       return;
  1744.    }
  1745.  
  1746.    *length = 0;
  1747.    _mesa_error(ctx, GL_INVALID_OPERATION,
  1748.                "glGetProgramBinary(driver supports zero binary formats)");
  1749.  
  1750.    (void) binaryFormat;
  1751.    (void) binary;
  1752. }
  1753.  
  1754. void GLAPIENTRY
  1755. _mesa_ProgramBinary(GLuint program, GLenum binaryFormat,
  1756.                     const GLvoid *binary, GLsizei length)
  1757. {
  1758.    struct gl_shader_program *shProg;
  1759.    GET_CURRENT_CONTEXT(ctx);
  1760.  
  1761.    shProg = _mesa_lookup_shader_program_err(ctx, program, "glProgramBinary");
  1762.    if (!shProg)
  1763.       return;
  1764.  
  1765.    (void) binaryFormat;
  1766.    (void) binary;
  1767.  
  1768.    /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
  1769.     *
  1770.     *     "If a negative number is provided where an argument of type sizei or
  1771.     *     sizeiptr is specified, an INVALID_VALUE error is generated."
  1772.     */
  1773.    if (length < 0) {
  1774.       _mesa_error(ctx, GL_INVALID_VALUE, "glProgramBinary(length < 0)");
  1775.       return;
  1776.    }
  1777.  
  1778.    /* The ARB_get_program_binary spec says:
  1779.     *
  1780.     *     "<binaryFormat> and <binary> must be those returned by a previous
  1781.     *     call to GetProgramBinary, and <length> must be the length of the
  1782.     *     program binary as returned by GetProgramBinary or GetProgramiv with
  1783.     *     <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
  1784.     *     setting the LINK_STATUS of <program> to FALSE, if these conditions
  1785.     *     are not met."
  1786.     *
  1787.     * Since any value of binaryFormat passed "is not one of those specified as
  1788.     * allowable for [this] command, an INVALID_ENUM error is generated."
  1789.     */
  1790.    shProg->LinkStatus = GL_FALSE;
  1791.    _mesa_error(ctx, GL_INVALID_ENUM, "glProgramBinary");
  1792. }
  1793.  
  1794.  
  1795. void GLAPIENTRY
  1796. _mesa_ProgramParameteri(GLuint program, GLenum pname, GLint value)
  1797. {
  1798.    struct gl_shader_program *shProg;
  1799.    GET_CURRENT_CONTEXT(ctx);
  1800.  
  1801.    shProg = _mesa_lookup_shader_program_err(ctx, program,
  1802.                                             "glProgramParameteri");
  1803.    if (!shProg)
  1804.       return;
  1805.  
  1806.    switch (pname) {
  1807.    case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
  1808.       /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
  1809.        * is part of OpenGL ES 3.0.  For the ES2 case, this function shouldn't
  1810.        * even be in the dispatch table, so we shouldn't need to expclicitly
  1811.        * check here.
  1812.        *
  1813.        * On desktop, we ignore the 3.0+ requirement because it is silly.
  1814.        */
  1815.  
  1816.       /* The ARB_get_program_binary extension spec says:
  1817.        *
  1818.        *     "An INVALID_VALUE error is generated if the <value> argument to
  1819.        *     ProgramParameteri is not TRUE or FALSE."
  1820.        */
  1821.       if (value != GL_TRUE && value != GL_FALSE) {
  1822.          goto invalid_value;
  1823.       }
  1824.  
  1825.       /* No need to notify the driver.  Any changes will actually take effect
  1826.        * the next time the shader is linked.
  1827.        *
  1828.        * The ARB_get_program_binary extension spec says:
  1829.        *
  1830.        *     "To indicate that a program binary is likely to be retrieved,
  1831.        *     ProgramParameteri should be called with <pname>
  1832.        *     PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
  1833.        *     will not be in effect until the next time LinkProgram or
  1834.        *     ProgramBinary has been called successfully."
  1835.        *
  1836.        * The resloution of issue 9 in the extension spec also says:
  1837.        *
  1838.        *     "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
  1839.        *     to indicate to the GL implementation that this program will
  1840.        *     likely be saved with GetProgramBinary at some point. This will
  1841.        *     give the GL implementation the opportunity to track any state
  1842.        *     changes made to the program before being saved such that when it
  1843.        *     is loaded again a recompile can be avoided."
  1844.        */
  1845.       shProg->BinaryRetreivableHint = value;
  1846.       return;
  1847.  
  1848.    case GL_PROGRAM_SEPARABLE:
  1849.       /* Spec imply that the behavior is the same as ARB_get_program_binary
  1850.        * Chapter 7.3 Program Objects
  1851.        */
  1852.       if (value != GL_TRUE && value != GL_FALSE) {
  1853.          goto invalid_value;
  1854.       }
  1855.       shProg->SeparateShader = value;
  1856.       return;
  1857.  
  1858.    default:
  1859.       _mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteri(pname=%s)",
  1860.                   _mesa_lookup_enum_by_nr(pname));
  1861.       return;
  1862.    }
  1863.  
  1864. invalid_value:
  1865.    _mesa_error(ctx, GL_INVALID_VALUE,
  1866.                "glProgramParameteri(pname=%s, value=%d): "
  1867.                "value must be 0 or 1.",
  1868.                _mesa_lookup_enum_by_nr(pname),
  1869.                value);
  1870. }
  1871.  
  1872.  
  1873. void
  1874. _mesa_use_shader_program(struct gl_context *ctx, GLenum type,
  1875.                          struct gl_shader_program *shProg,
  1876.                          struct gl_pipeline_object *shTarget)
  1877. {
  1878.    gl_shader_stage stage = _mesa_shader_enum_to_shader_stage(type);
  1879.    use_shader_program(ctx, stage, shProg, shTarget);
  1880.  
  1881.    if (ctx->Driver.UseProgram)
  1882.       ctx->Driver.UseProgram(ctx, shProg);
  1883. }
  1884.  
  1885.  
  1886. static GLuint
  1887. _mesa_create_shader_program(struct gl_context* ctx, GLboolean separate,
  1888.                             GLenum type, GLsizei count, const GLchar* const *strings)
  1889. {
  1890.    const GLuint shader = create_shader(ctx, type);
  1891.    GLuint program = 0;
  1892.  
  1893.    if (shader) {
  1894.       _mesa_ShaderSource(shader, count, strings, NULL);
  1895.  
  1896.       compile_shader(ctx, shader);
  1897.  
  1898.       program = create_shader_program(ctx);
  1899.       if (program) {
  1900.          struct gl_shader_program *shProg;
  1901.          struct gl_shader *sh;
  1902.          GLint compiled = GL_FALSE;
  1903.  
  1904.          shProg = _mesa_lookup_shader_program(ctx, program);
  1905.          sh = _mesa_lookup_shader(ctx, shader);
  1906.  
  1907.          shProg->SeparateShader = separate;
  1908.  
  1909.          get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
  1910.          if (compiled) {
  1911.             attach_shader(ctx, program, shader);
  1912.             link_program(ctx, program);
  1913.             detach_shader(ctx, program, shader);
  1914.  
  1915. #if 0
  1916.             /* Possibly... */
  1917.             if (active-user-defined-varyings-in-linked-program) {
  1918.                append-error-to-info-log;
  1919.                shProg->LinkStatus = GL_FALSE;
  1920.             }
  1921. #endif
  1922.          }
  1923.  
  1924.          ralloc_strcat(&shProg->InfoLog, sh->InfoLog);
  1925.       }
  1926.  
  1927.       delete_shader(ctx, shader);
  1928.    }
  1929.  
  1930.    return program;
  1931. }
  1932.  
  1933.  
  1934. /**
  1935.  * Copy program-specific data generated by linking from the gl_shader_program
  1936.  * object to a specific gl_program object.
  1937.  */
  1938. void
  1939. _mesa_copy_linked_program_data(gl_shader_stage type,
  1940.                                const struct gl_shader_program *src,
  1941.                                struct gl_program *dst)
  1942. {
  1943.    switch (type) {
  1944.    case MESA_SHADER_VERTEX:
  1945.       dst->UsesClipDistanceOut = src->Vert.UsesClipDistance;
  1946.       break;
  1947.    case MESA_SHADER_GEOMETRY: {
  1948.       struct gl_geometry_program *dst_gp = (struct gl_geometry_program *) dst;
  1949.       dst_gp->VerticesIn = src->Geom.VerticesIn;
  1950.       dst_gp->VerticesOut = src->Geom.VerticesOut;
  1951.       dst_gp->Invocations = src->Geom.Invocations;
  1952.       dst_gp->InputType = src->Geom.InputType;
  1953.       dst_gp->OutputType = src->Geom.OutputType;
  1954.       dst->UsesClipDistanceOut = src->Geom.UsesClipDistance;
  1955.       dst_gp->UsesEndPrimitive = src->Geom.UsesEndPrimitive;
  1956.       dst_gp->UsesStreams = src->Geom.UsesStreams;
  1957.    }
  1958.       break;
  1959.    case MESA_SHADER_FRAGMENT: {
  1960.       struct gl_fragment_program *dst_fp = (struct gl_fragment_program *) dst;
  1961.       dst_fp->FragDepthLayout = src->FragDepthLayout;
  1962.    }
  1963.       break;
  1964.    case MESA_SHADER_COMPUTE: {
  1965.       struct gl_compute_program *dst_cp = (struct gl_compute_program *) dst;
  1966.       int i;
  1967.       for (i = 0; i < 3; i++)
  1968.          dst_cp->LocalSize[i] = src->Comp.LocalSize[i];
  1969.    }
  1970.       break;
  1971.    default:
  1972.       break;
  1973.    }
  1974. }
  1975.  
  1976. /**
  1977.  * ARB_separate_shader_objects: Compile & Link Program
  1978.  */
  1979. GLuint GLAPIENTRY
  1980. _mesa_CreateShaderProgramv(GLenum type, GLsizei count,
  1981.                            const GLchar* const *strings)
  1982. {
  1983.    GET_CURRENT_CONTEXT(ctx);
  1984.  
  1985.    return _mesa_create_shader_program(ctx, GL_TRUE, type, count, strings);
  1986. }
  1987.