Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright © 2013 Gregory Hainaut <gregory.hainaut@gmail.com>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice (including the next
  14.  * paragraph) shall be included in all copies or substantial portions of the
  15.  * Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * 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 OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23.  * IN THE SOFTWARE.
  24.  */
  25.  
  26. /**
  27.  * \file pipelineobj.c
  28.  * \author Hainaut Gregory <gregory.hainaut@gmail.com>
  29.  *
  30.  * Implementation of pipeline object related API functions. Based on
  31.  * GL_ARB_separate_shader_objects extension.
  32.  */
  33.  
  34. #include "main/glheader.h"
  35. #include "main/context.h"
  36. #include "main/dispatch.h"
  37. #include "main/enums.h"
  38. #include "main/hash.h"
  39. #include "main/mtypes.h"
  40. #include "main/pipelineobj.h"
  41. #include "main/shaderapi.h"
  42. #include "main/shaderobj.h"
  43. #include "main/transformfeedback.h"
  44. #include "main/uniforms.h"
  45. #include "program/program.h"
  46. #include "program/prog_parameter.h"
  47. #include "util/ralloc.h"
  48. #include <stdbool.h>
  49. #include "../glsl/glsl_parser_extras.h"
  50. #include "../glsl/ir_uniform.h"
  51.  
  52. /**
  53.  * Delete a pipeline object.
  54.  */
  55. void
  56. _mesa_delete_pipeline_object(struct gl_context *ctx,
  57.                              struct gl_pipeline_object *obj)
  58. {
  59.    unsigned i;
  60.  
  61.    _mesa_reference_shader_program(ctx, &obj->_CurrentFragmentProgram, NULL);
  62.  
  63.    for (i = 0; i < MESA_SHADER_STAGES; i++)
  64.       _mesa_reference_shader_program(ctx, &obj->CurrentProgram[i], NULL);
  65.  
  66.    _mesa_reference_shader_program(ctx, &obj->ActiveProgram, NULL);
  67.    mtx_destroy(&obj->Mutex);
  68.    ralloc_free(obj);
  69. }
  70.  
  71. /**
  72.  * Allocate and initialize a new pipeline object.
  73.  */
  74. static struct gl_pipeline_object *
  75. _mesa_new_pipeline_object(struct gl_context *ctx, GLuint name)
  76. {
  77.    struct gl_pipeline_object *obj = rzalloc(NULL, struct gl_pipeline_object);
  78.    if (obj) {
  79.       obj->Name = name;
  80.       mtx_init(&obj->Mutex, mtx_plain);
  81.       obj->RefCount = 1;
  82.       obj->Flags = _mesa_get_shader_flags();
  83.       obj->InfoLog = NULL;
  84.    }
  85.  
  86.    return obj;
  87. }
  88.  
  89. /**
  90.  * Initialize pipeline object state for given context.
  91.  */
  92. void
  93. _mesa_init_pipeline(struct gl_context *ctx)
  94. {
  95.    ctx->Pipeline.Objects = _mesa_NewHashTable();
  96.  
  97.    ctx->Pipeline.Current = NULL;
  98.  
  99.    /* Install a default Pipeline */
  100.    ctx->Pipeline.Default = _mesa_new_pipeline_object(ctx, 0);
  101.    _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
  102. }
  103.  
  104.  
  105. /**
  106.  * Callback for deleting a pipeline object.  Called by _mesa_HashDeleteAll().
  107.  */
  108. static void
  109. delete_pipelineobj_cb(GLuint id, void *data, void *userData)
  110. {
  111.    struct gl_pipeline_object *obj = (struct gl_pipeline_object *) data;
  112.    struct gl_context *ctx = (struct gl_context *) userData;
  113.    _mesa_delete_pipeline_object(ctx, obj);
  114. }
  115.  
  116.  
  117. /**
  118.  * Free pipeline state for given context.
  119.  */
  120. void
  121. _mesa_free_pipeline_data(struct gl_context *ctx)
  122. {
  123.    _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
  124.  
  125.    _mesa_HashDeleteAll(ctx->Pipeline.Objects, delete_pipelineobj_cb, ctx);
  126.    _mesa_DeleteHashTable(ctx->Pipeline.Objects);
  127.  
  128.    _mesa_delete_pipeline_object(ctx, ctx->Pipeline.Default);
  129. }
  130.  
  131. /**
  132.  * Look up the pipeline object for the given ID.
  133.  *
  134.  * \returns
  135.  * Either a pointer to the pipeline object with the specified ID or \c NULL for
  136.  * a non-existent ID.  The spec defines ID 0 as being technically
  137.  * non-existent.
  138.  */
  139. static inline struct gl_pipeline_object *
  140. lookup_pipeline_object(struct gl_context *ctx, GLuint id)
  141. {
  142.    if (id == 0)
  143.       return NULL;
  144.    else
  145.       return (struct gl_pipeline_object *)
  146.          _mesa_HashLookup(ctx->Pipeline.Objects, id);
  147. }
  148.  
  149. /**
  150.  * Add the given pipeline object to the pipeline object pool.
  151.  */
  152. static void
  153. save_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
  154. {
  155.    if (obj->Name > 0) {
  156.       _mesa_HashInsert(ctx->Pipeline.Objects, obj->Name, obj);
  157.    }
  158. }
  159.  
  160. /**
  161.  * Remove the given pipeline object from the pipeline object pool.
  162.  * Do not deallocate the pipeline object though.
  163.  */
  164. static void
  165. remove_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
  166. {
  167.    if (obj->Name > 0) {
  168.       _mesa_HashRemove(ctx->Pipeline.Objects, obj->Name);
  169.    }
  170. }
  171.  
  172. /**
  173.  * Set ptr to obj w/ reference counting.
  174.  * Note: this should only be called from the _mesa_reference_pipeline_object()
  175.  * inline function.
  176.  */
  177. void
  178. _mesa_reference_pipeline_object_(struct gl_context *ctx,
  179.                                  struct gl_pipeline_object **ptr,
  180.                                  struct gl_pipeline_object *obj)
  181. {
  182.    assert(*ptr != obj);
  183.  
  184.    if (*ptr) {
  185.       /* Unreference the old pipeline object */
  186.       GLboolean deleteFlag = GL_FALSE;
  187.       struct gl_pipeline_object *oldObj = *ptr;
  188.  
  189.       mtx_lock(&oldObj->Mutex);
  190.       assert(oldObj->RefCount > 0);
  191.       oldObj->RefCount--;
  192.       deleteFlag = (oldObj->RefCount == 0);
  193.       mtx_unlock(&oldObj->Mutex);
  194.  
  195.       if (deleteFlag) {
  196.          _mesa_delete_pipeline_object(ctx, oldObj);
  197.       }
  198.  
  199.       *ptr = NULL;
  200.    }
  201.    assert(!*ptr);
  202.  
  203.    if (obj) {
  204.       /* reference new pipeline object */
  205.       mtx_lock(&obj->Mutex);
  206.       if (obj->RefCount == 0) {
  207.          /* this pipeline's being deleted (look just above) */
  208.          /* Not sure this can ever really happen.  Warn if it does. */
  209.          _mesa_problem(NULL, "referencing deleted pipeline object");
  210.          *ptr = NULL;
  211.       }
  212.       else {
  213.          obj->RefCount++;
  214.          *ptr = obj;
  215.       }
  216.       mtx_unlock(&obj->Mutex);
  217.    }
  218. }
  219.  
  220. /**
  221.  * Bound program to severals stages of the pipeline
  222.  */
  223. void GLAPIENTRY
  224. _mesa_UseProgramStages(GLuint pipeline, GLbitfield stages, GLuint program)
  225. {
  226.    GET_CURRENT_CONTEXT(ctx);
  227.  
  228.    struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
  229.    struct gl_shader_program *shProg = NULL;
  230.    GLbitfield any_valid_stages;
  231.  
  232.    if (!pipe) {
  233.       _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgramStages(pipeline)");
  234.       return;
  235.    }
  236.  
  237.    /* Object is created by any Pipeline call but glGenProgramPipelines,
  238.     * glIsProgramPipeline and GetProgramPipelineInfoLog
  239.     */
  240.    pipe->EverBound = GL_TRUE;
  241.  
  242.    /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
  243.     *
  244.     *     "If stages is not the special value ALL_SHADER_BITS, and has a bit
  245.     *     set that is not recognized, the error INVALID_VALUE is generated."
  246.     *
  247.     * NOT YET SUPPORTED:
  248.     * GL_TESS_CONTROL_SHADER_BIT
  249.     * GL_TESS_EVALUATION_SHADER_BIT
  250.     */
  251.    any_valid_stages = GL_VERTEX_SHADER_BIT | GL_FRAGMENT_SHADER_BIT;
  252.    if (_mesa_has_geometry_shaders(ctx))
  253.       any_valid_stages |= GL_GEOMETRY_SHADER_BIT;
  254.  
  255.    if (stages != GL_ALL_SHADER_BITS && (stages & ~any_valid_stages) != 0) {
  256.       _mesa_error(ctx, GL_INVALID_VALUE, "glUseProgramStages(Stages)");
  257.       return;
  258.    }
  259.  
  260.    /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
  261.     * spec says:
  262.     *
  263.     *     "The error INVALID_OPERATION is generated:
  264.     *
  265.     *      ...
  266.     *
  267.     *         - by UseProgramStages if the program pipeline object it refers
  268.     *           to is current and the current transform feedback object is
  269.     *           active and not paused;
  270.     */
  271.    if (ctx->_Shader == pipe) {
  272.       if (_mesa_is_xfb_active_and_unpaused(ctx)) {
  273.          _mesa_error(ctx, GL_INVALID_OPERATION,
  274.                "glUseProgramStages(transform feedback active)");
  275.          return;
  276.       }
  277.    }
  278.  
  279.    if (program) {
  280.       shProg = _mesa_lookup_shader_program_err(ctx, program,
  281.                                                "glUseProgramStages");
  282.       if (shProg == NULL)
  283.          return;
  284.  
  285.       /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
  286.        * says:
  287.        *
  288.        *     "If the program object named by program was linked without the
  289.        *     PROGRAM_SEPARABLE parameter set, or was not linked successfully,
  290.        *     the error INVALID_OPERATION is generated and the corresponding
  291.        *     shader stages in the pipeline program pipeline object are not
  292.        *     modified."
  293.        */
  294.       if (!shProg->LinkStatus) {
  295.          _mesa_error(ctx, GL_INVALID_OPERATION,
  296.                      "glUseProgramStages(program not linked)");
  297.          return;
  298.       }
  299.  
  300.       if (!shProg->SeparateShader) {
  301.          _mesa_error(ctx, GL_INVALID_OPERATION,
  302.                      "glUseProgramStages(program wasn't linked with the "
  303.                      "PROGRAM_SEPARABLE flag)");
  304.          return;
  305.       }
  306.    }
  307.  
  308.    /* Enable individual stages from the program as requested by the
  309.     * application.  If there is no shader for a requested stage in the
  310.     * program, _mesa_use_shader_program will enable fixed-function processing
  311.     * as dictated by the spec.
  312.     *
  313.     * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
  314.     * says:
  315.     *
  316.     *     "If UseProgramStages is called with program set to zero or with a
  317.     *     program object that contains no executable code for the given
  318.     *     stages, it is as if the pipeline object has no programmable stage
  319.     *     configured for the indicated shader stages."
  320.     */
  321.    if ((stages & GL_VERTEX_SHADER_BIT) != 0)
  322.       _mesa_use_shader_program(ctx, GL_VERTEX_SHADER, shProg, pipe);
  323.  
  324.    if ((stages & GL_FRAGMENT_SHADER_BIT) != 0)
  325.       _mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER, shProg, pipe);
  326.  
  327.    if ((stages & GL_GEOMETRY_SHADER_BIT) != 0)
  328.       _mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER, shProg, pipe);
  329. }
  330.  
  331. /**
  332.  * Use the named shader program for subsequent glUniform calls (if pipeline
  333.  * bound)
  334.  */
  335. void GLAPIENTRY
  336. _mesa_ActiveShaderProgram(GLuint pipeline, GLuint program)
  337. {
  338.    GET_CURRENT_CONTEXT(ctx);
  339.    struct gl_shader_program *shProg = NULL;
  340.    struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
  341.  
  342.    if (program != 0) {
  343.       shProg = _mesa_lookup_shader_program_err(ctx, program,
  344.                                                "glActiveShaderProgram(program)");
  345.       if (shProg == NULL)
  346.          return;
  347.    }
  348.  
  349.    if (!pipe) {
  350.       _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveShaderProgram(pipeline)");
  351.       return;
  352.    }
  353.  
  354.    /* Object is created by any Pipeline call but glGenProgramPipelines,
  355.     * glIsProgramPipeline and GetProgramPipelineInfoLog
  356.     */
  357.    pipe->EverBound = GL_TRUE;
  358.  
  359.    if ((shProg != NULL) && !shProg->LinkStatus) {
  360.       _mesa_error(ctx, GL_INVALID_OPERATION,
  361.             "glActiveShaderProgram(program %u not linked)", shProg->Name);
  362.       return;
  363.    }
  364.  
  365.    _mesa_reference_shader_program(ctx, &pipe->ActiveProgram, shProg);
  366. }
  367.  
  368. /**
  369.  * Make program of the pipeline current
  370.  */
  371. void GLAPIENTRY
  372. _mesa_BindProgramPipeline(GLuint pipeline)
  373. {
  374.    GET_CURRENT_CONTEXT(ctx);
  375.    struct gl_pipeline_object *newObj = NULL;
  376.  
  377.    /* Rebinding the same pipeline object: no change.
  378.     */
  379.    if (ctx->_Shader->Name == pipeline)
  380.       return;
  381.  
  382.    /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
  383.     * spec says:
  384.     *
  385.     *     "The error INVALID_OPERATION is generated:
  386.     *
  387.     *      ...
  388.     *
  389.     *         - by BindProgramPipeline if the current transform feedback
  390.     *           object is active and not paused;
  391.     */
  392.    if (_mesa_is_xfb_active_and_unpaused(ctx)) {
  393.       _mesa_error(ctx, GL_INVALID_OPERATION,
  394.             "glBindProgramPipeline(transform feedback active)");
  395.       return;
  396.    }
  397.  
  398.    /* Get pointer to new pipeline object (newObj)
  399.     */
  400.    if (pipeline) {
  401.       /* non-default pipeline object */
  402.       newObj = lookup_pipeline_object(ctx, pipeline);
  403.       if (!newObj) {
  404.          _mesa_error(ctx, GL_INVALID_OPERATION,
  405.                      "glBindProgramPipeline(non-gen name)");
  406.          return;
  407.       }
  408.  
  409.       /* Object is created by any Pipeline call but glGenProgramPipelines,
  410.        * glIsProgramPipeline and GetProgramPipelineInfoLog
  411.        */
  412.       newObj->EverBound = GL_TRUE;
  413.    }
  414.  
  415.    _mesa_bind_pipeline(ctx, newObj);
  416. }
  417.  
  418. void
  419. _mesa_bind_pipeline(struct gl_context *ctx,
  420.                     struct gl_pipeline_object *pipe)
  421. {
  422.    /* First bind the Pipeline to pipeline binding point */
  423.    _mesa_reference_pipeline_object(ctx, &ctx->Pipeline.Current, pipe);
  424.  
  425.    /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
  426.     *
  427.     *     "If there is a current program object established by UseProgram,
  428.     *     that program is considered current for all stages. Otherwise, if
  429.     *     there is a bound program pipeline object (see section 2.11.4), the
  430.     *     program bound to the appropriate stage of the pipeline object is
  431.     *     considered current."
  432.     */
  433.    if (&ctx->Shader != ctx->_Shader) {
  434.       if (pipe != NULL) {
  435.          /* Bound the pipeline to the current program and
  436.           * restore the pipeline state
  437.           */
  438.          _mesa_reference_pipeline_object(ctx, &ctx->_Shader, pipe);
  439.       } else {
  440.          /* Unbind the pipeline */
  441.          _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
  442.                                          ctx->Pipeline.Default);
  443.       }
  444.  
  445.       FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
  446.  
  447.       if (ctx->Driver.UseProgram)
  448.          ctx->Driver.UseProgram(ctx, NULL);
  449.    }
  450. }
  451.  
  452. /**
  453.  * Delete a set of pipeline objects.
  454.  *
  455.  * \param n      Number of pipeline objects to delete.
  456.  * \param ids    pipeline of \c n pipeline object IDs.
  457.  */
  458. void GLAPIENTRY
  459. _mesa_DeleteProgramPipelines(GLsizei n, const GLuint *pipelines)
  460. {
  461.    GET_CURRENT_CONTEXT(ctx);
  462.    GLsizei i;
  463.  
  464.    if (n < 0) {
  465.       _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgramPipelines(n<0)");
  466.       return;
  467.    }
  468.  
  469.    for (i = 0; i < n; i++) {
  470.       struct gl_pipeline_object *obj =
  471.          lookup_pipeline_object(ctx, pipelines[i]);
  472.  
  473.       if (obj) {
  474.          assert(obj->Name == pipelines[i]);
  475.  
  476.          /* If the pipeline object is currently bound, the spec says "If an
  477.           * object that is currently bound is deleted, the binding for that
  478.           * object reverts to zero and no program pipeline object becomes
  479.           * current."
  480.           */
  481.          if (obj == ctx->Pipeline.Current) {
  482.             _mesa_BindProgramPipeline(0);
  483.          }
  484.  
  485.          /* The ID is immediately freed for re-use */
  486.          remove_pipeline_object(ctx, obj);
  487.  
  488.          /* Unreference the pipeline object.
  489.           * If refcount hits zero, the object will be deleted.
  490.           */
  491.          _mesa_reference_pipeline_object(ctx, &obj, NULL);
  492.       }
  493.    }
  494. }
  495.  
  496. /**
  497.  * Generate a set of unique pipeline object IDs and store them in \c pipelines.
  498.  * \param n       Number of IDs to generate.
  499.  * \param pipelines  pipeline of \c n locations to store the IDs.
  500.  */
  501. static void
  502. create_program_pipelines(struct gl_context *ctx, GLsizei n, GLuint *pipelines,
  503.                          bool dsa)
  504. {
  505.    const char *func;
  506.    GLuint first;
  507.    GLint i;
  508.  
  509.    func = dsa ? "glCreateProgramPipelines" : "glGenProgramPipelines";
  510.  
  511.    if (n < 0) {
  512.       _mesa_error(ctx, GL_INVALID_VALUE, "%s (n < 0)", func);
  513.       return;
  514.    }
  515.  
  516.    if (!pipelines) {
  517.       return;
  518.    }
  519.  
  520.    first = _mesa_HashFindFreeKeyBlock(ctx->Pipeline.Objects, n);
  521.  
  522.    for (i = 0; i < n; i++) {
  523.       struct gl_pipeline_object *obj;
  524.       GLuint name = first + i;
  525.  
  526.       obj = _mesa_new_pipeline_object(ctx, name);
  527.       if (!obj) {
  528.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
  529.          return;
  530.       }
  531.  
  532.       if (dsa) {
  533.          /* make dsa-allocated objects behave like program objects */
  534.          obj->EverBound = GL_TRUE;
  535.       }
  536.  
  537.       save_pipeline_object(ctx, obj);
  538.       pipelines[i] = first + i;
  539.    }
  540.  
  541. }
  542.  
  543. void GLAPIENTRY
  544. _mesa_GenProgramPipelines(GLsizei n, GLuint *pipelines)
  545. {
  546.    GET_CURRENT_CONTEXT(ctx);
  547.  
  548.    create_program_pipelines(ctx, n, pipelines, false);
  549. }
  550.  
  551. void GLAPIENTRY
  552. _mesa_CreateProgramPipelines(GLsizei n, GLuint *pipelines)
  553. {
  554.    GET_CURRENT_CONTEXT(ctx);
  555.  
  556.    if (!ctx->Extensions.ARB_direct_state_access) {
  557.       _mesa_error(ctx, GL_INVALID_OPERATION, "glCreateProgramPipelines("
  558.                   "GL_ARB_direct_state_access is not supported)");
  559.       return;
  560.    }
  561.  
  562.    create_program_pipelines(ctx, n, pipelines, true);
  563. }
  564.  
  565. /**
  566.  * Determine if ID is the name of an pipeline object.
  567.  *
  568.  * \param id  ID of the potential pipeline object.
  569.  * \return  \c GL_TRUE if \c id is the name of a pipeline object,
  570.  *          \c GL_FALSE otherwise.
  571.  */
  572. GLboolean GLAPIENTRY
  573. _mesa_IsProgramPipeline(GLuint pipeline)
  574. {
  575.    GET_CURRENT_CONTEXT(ctx);
  576.  
  577.    struct gl_pipeline_object *obj = lookup_pipeline_object(ctx, pipeline);
  578.    if (obj == NULL)
  579.       return GL_FALSE;
  580.  
  581.    return obj->EverBound;
  582. }
  583.  
  584. /**
  585.  * glGetProgramPipelineiv() - get pipeline shader state.
  586.  */
  587. void GLAPIENTRY
  588. _mesa_GetProgramPipelineiv(GLuint pipeline, GLenum pname, GLint *params)
  589. {
  590.    GET_CURRENT_CONTEXT(ctx);
  591.    struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
  592.  
  593.    /* Are geometry shaders available in this context?
  594.     */
  595.    const bool has_gs = _mesa_has_geometry_shaders(ctx);
  596.  
  597.    if (!pipe) {
  598.       _mesa_error(ctx, GL_INVALID_OPERATION,
  599.                   "glGetProgramPipelineiv(pipeline)");
  600.       return;
  601.    }
  602.  
  603.    /* Object is created by any Pipeline call but glGenProgramPipelines,
  604.     * glIsProgramPipeline and GetProgramPipelineInfoLog
  605.     */
  606.    pipe->EverBound = GL_TRUE;
  607.  
  608.    switch (pname) {
  609.    case GL_ACTIVE_PROGRAM:
  610.       *params = pipe->ActiveProgram ? pipe->ActiveProgram->Name : 0;
  611.       return;
  612.    case GL_INFO_LOG_LENGTH:
  613.       *params = pipe->InfoLog ? strlen(pipe->InfoLog) + 1 : 0;
  614.       return;
  615.    case GL_VALIDATE_STATUS:
  616.       *params = pipe->Validated;
  617.       return;
  618.    case GL_VERTEX_SHADER:
  619.       *params = pipe->CurrentProgram[MESA_SHADER_VERTEX]
  620.          ? pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name : 0;
  621.       return;
  622.    case GL_TESS_EVALUATION_SHADER:
  623.       /* NOT YET SUPPORTED */
  624.       break;
  625.    case GL_TESS_CONTROL_SHADER:
  626.       /* NOT YET SUPPORTED */
  627.       break;
  628.    case GL_GEOMETRY_SHADER:
  629.       if (!has_gs)
  630.          break;
  631.       *params = pipe->CurrentProgram[MESA_SHADER_GEOMETRY]
  632.          ? pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name : 0;
  633.       return;
  634.    case GL_FRAGMENT_SHADER:
  635.       *params = pipe->CurrentProgram[MESA_SHADER_FRAGMENT]
  636.          ? pipe->CurrentProgram[MESA_SHADER_FRAGMENT]->Name : 0;
  637.       return;
  638.    default:
  639.       break;
  640.    }
  641.  
  642.    _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramPipelineiv(pname=%s)",
  643.                _mesa_lookup_enum_by_nr(pname));
  644. }
  645.  
  646. /**
  647.  * Determines whether every stage in a linked program is active in the
  648.  * specified pipeline.
  649.  */
  650. static bool
  651. program_stages_all_active(struct gl_pipeline_object *pipe,
  652.                           const struct gl_shader_program *prog)
  653. {
  654.    unsigned i;
  655.    bool status = true;
  656.  
  657.    if (!prog)
  658.       return true;
  659.  
  660.    for (i = 0; i < MESA_SHADER_STAGES; i++) {
  661.       if (prog->_LinkedShaders[i]) {
  662.          if (pipe->CurrentProgram[i]) {
  663.             if (prog->Name != pipe->CurrentProgram[i]->Name) {
  664.                status = false;
  665.             }
  666.          } else {
  667.             status = false;
  668.          }
  669.       }
  670.    }
  671.  
  672.    if (!status) {
  673.       pipe->InfoLog = ralloc_asprintf(pipe,
  674.                                       "Program %d is not active for all "
  675.                                       "shaders that was linked",
  676.                                       prog->Name);
  677.    }
  678.  
  679.    return status;
  680. }
  681.  
  682. extern GLboolean
  683. _mesa_validate_program_pipeline(struct gl_context* ctx,
  684.                                 struct gl_pipeline_object *pipe,
  685.                                 GLboolean IsBound)
  686. {
  687.    unsigned i;
  688.  
  689.    pipe->Validated = GL_FALSE;
  690.  
  691.    /* Release and reset the info log.
  692.     */
  693.    if (pipe->InfoLog != NULL)
  694.       ralloc_free(pipe->InfoLog);
  695.  
  696.    pipe->InfoLog = NULL;
  697.  
  698.    /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
  699.     * OpenGL 4.1 spec says:
  700.     *
  701.     *     "[INVALID_OPERATION] is generated by any command that transfers
  702.     *     vertices to the GL if:
  703.     *
  704.     *         - A program object is active for at least one, but not all of
  705.     *           the shader stages that were present when the program was
  706.     *           linked."
  707.     *
  708.     * For each possible program stage, verify that the program bound to that
  709.     * stage has all of its stages active.  In other words, if the program
  710.     * bound to the vertex stage also has a fragment shader, the fragment
  711.     * shader must also be bound to the fragment stage.
  712.     */
  713.    for (i = 0; i < MESA_SHADER_STAGES; i++) {
  714.       if (!program_stages_all_active(pipe, pipe->CurrentProgram[i])) {
  715.          goto err;
  716.       }
  717.    }
  718.  
  719.    /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
  720.     * OpenGL 4.1 spec says:
  721.     *
  722.     *     "[INVALID_OPERATION] is generated by any command that transfers
  723.     *     vertices to the GL if:
  724.     *
  725.     *         ...
  726.     *
  727.     *         - One program object is active for at least two shader stages
  728.     *           and a second program is active for a shader stage between two
  729.     *           stages for which the first program was active."
  730.     *
  731.     * Without Tesselation, the only case where this can occur is the geometry
  732.     * shader between the fragment shader and vertex shader.
  733.     */
  734.    if (pipe->CurrentProgram[MESA_SHADER_GEOMETRY]
  735.        && pipe->CurrentProgram[MESA_SHADER_FRAGMENT]
  736.        && pipe->CurrentProgram[MESA_SHADER_VERTEX]) {
  737.       if (pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name == pipe->CurrentProgram[MESA_SHADER_FRAGMENT]->Name &&
  738.           pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name != pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name) {
  739.          pipe->InfoLog =
  740.             ralloc_asprintf(pipe,
  741.                             "Program %d is active for geometry stage between "
  742.                             "two stages for which another program %d is "
  743.                             "active",
  744.                             pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name,
  745.                             pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name);
  746.          goto err;
  747.       }
  748.    }
  749.  
  750.    /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
  751.     * OpenGL 4.1 spec says:
  752.     *
  753.     *     "[INVALID_OPERATION] is generated by any command that transfers
  754.     *     vertices to the GL if:
  755.     *
  756.     *         ...
  757.     *
  758.     *         - There is an active program for tessellation control,
  759.     *           tessellation evaluation, or geometry stages with corresponding
  760.     *           executable shader, but there is no active program with
  761.     *           executable vertex shader."
  762.     */
  763.    if (!pipe->CurrentProgram[MESA_SHADER_VERTEX]
  764.        && pipe->CurrentProgram[MESA_SHADER_GEOMETRY]) {
  765.       pipe->InfoLog = ralloc_strdup(pipe, "Program lacks a vertex shader");
  766.       goto err;
  767.    }
  768.  
  769.    /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
  770.     * OpenGL 4.1 spec says:
  771.     *
  772.     *     "[INVALID_OPERATION] is generated by any command that transfers
  773.     *     vertices to the GL if:
  774.     *
  775.     *         ...
  776.     *
  777.     *         - There is no current program object specified by UseProgram,
  778.     *           there is a current program pipeline object, and the current
  779.     *           program for any shader stage has been relinked since being
  780.     *           applied to the pipeline object via UseProgramStages with the
  781.     *           PROGRAM_SEPARABLE parameter set to FALSE.
  782.     */
  783.    for (i = 0; i < MESA_SHADER_STAGES; i++) {
  784.       if (pipe->CurrentProgram[i] && !pipe->CurrentProgram[i]->SeparateShader) {
  785.          pipe->InfoLog = ralloc_asprintf(pipe,
  786.                                          "Program %d was relinked without "
  787.                                          "PROGRAM_SEPARABLE state",
  788.                                          pipe->CurrentProgram[i]->Name);
  789.          goto err;
  790.       }
  791.    }
  792.  
  793.    /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
  794.     * OpenGL 4.1 spec says:
  795.     *
  796.     *     "[INVALID_OPERATION] is generated by any command that transfers
  797.     *     vertices to the GL if:
  798.     *
  799.     *         ...
  800.     *
  801.     *         - Any two active samplers in the current program object are of
  802.     *           different types, but refer to the same texture image unit.
  803.     *
  804.     *         - The number of active samplers in the program exceeds the
  805.     *           maximum number of texture image units allowed."
  806.     */
  807.    if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe))
  808.       goto err;
  809.  
  810.    pipe->Validated = GL_TRUE;
  811.    return GL_TRUE;
  812.  
  813. err:
  814.    if (IsBound)
  815.       _mesa_error(ctx, GL_INVALID_OPERATION,
  816.                   "glValidateProgramPipeline failed to validate the pipeline");
  817.  
  818.    return GL_FALSE;
  819. }
  820.  
  821. /**
  822.  * Check compatibility of pipeline's program
  823.  */
  824. void GLAPIENTRY
  825. _mesa_ValidateProgramPipeline(GLuint pipeline)
  826. {
  827.    GET_CURRENT_CONTEXT(ctx);
  828.  
  829.    struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
  830.  
  831.    if (!pipe) {
  832.       _mesa_error(ctx, GL_INVALID_OPERATION,
  833.                   "glValidateProgramPipeline(pipeline)");
  834.       return;
  835.    }
  836.  
  837.    _mesa_validate_program_pipeline(ctx, pipe,
  838.                                    (ctx->_Shader->Name == pipe->Name));
  839. }
  840.  
  841. void GLAPIENTRY
  842. _mesa_GetProgramPipelineInfoLog(GLuint pipeline, GLsizei bufSize,
  843.                                 GLsizei *length, GLchar *infoLog)
  844. {
  845.    GET_CURRENT_CONTEXT(ctx);
  846.  
  847.    struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
  848.  
  849.    if (!pipe) {
  850.       _mesa_error(ctx, GL_INVALID_VALUE,
  851.                   "glGetProgramPipelineInfoLog(pipeline)");
  852.       return;
  853.    }
  854.  
  855.    if (bufSize < 0) {
  856.       _mesa_error(ctx, GL_INVALID_VALUE,
  857.                   "glGetProgramPipelineInfoLog(bufSize)");
  858.       return;
  859.    }
  860.  
  861.    if (pipe->InfoLog)
  862.       _mesa_copy_string(infoLog, bufSize, length, pipe->InfoLog);
  863.    else
  864.       *length = 0;
  865. }
  866.