2 * Mesa 3-D graphics library
4 * Copyright © 2013 Gregory Hainaut <gregory.hainaut@gmail.com>
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:
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
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
28 * \author Hainaut Gregory <gregory.hainaut@gmail.com>
30 * Implementation of pipeline object related API functions. Based on
31 * GL_ARB_separate_shader_objects extension.
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"
49 #include "../glsl/glsl_parser_extras.h"
50 #include "../glsl/ir_uniform.h"
53 * Delete a pipeline object.
56 _mesa_delete_pipeline_object(struct gl_context *ctx,
57 struct gl_pipeline_object *obj)
61 _mesa_reference_shader_program(ctx, &obj->_CurrentFragmentProgram, NULL);
63 for (i = 0; i < MESA_SHADER_STAGES; i++)
64 _mesa_reference_shader_program(ctx, &obj->CurrentProgram[i], NULL);
66 _mesa_reference_shader_program(ctx, &obj->ActiveProgram, NULL);
67 mtx_destroy(&obj->Mutex);
73 * Allocate and initialize a new pipeline object.
75 static struct gl_pipeline_object *
76 _mesa_new_pipeline_object(struct gl_context *ctx, GLuint name)
78 struct gl_pipeline_object *obj = rzalloc(NULL, struct gl_pipeline_object);
81 mtx_init(&obj->Mutex, mtx_plain);
83 obj->Flags = _mesa_get_shader_flags();
91 * Initialize pipeline object state for given context.
94 _mesa_init_pipeline(struct gl_context *ctx)
96 ctx->Pipeline.Objects = _mesa_NewHashTable();
98 ctx->Pipeline.Current = NULL;
100 /* Install a default Pipeline */
101 ctx->Pipeline.Default = _mesa_new_pipeline_object(ctx, 0);
102 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
107 * Callback for deleting a pipeline object. Called by _mesa_HashDeleteAll().
110 delete_pipelineobj_cb(GLuint id, void *data, void *userData)
112 struct gl_pipeline_object *obj = (struct gl_pipeline_object *) data;
113 struct gl_context *ctx = (struct gl_context *) userData;
114 _mesa_delete_pipeline_object(ctx, obj);
119 * Free pipeline state for given context.
122 _mesa_free_pipeline_data(struct gl_context *ctx)
124 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
126 _mesa_HashDeleteAll(ctx->Pipeline.Objects, delete_pipelineobj_cb, ctx);
127 _mesa_DeleteHashTable(ctx->Pipeline.Objects);
129 _mesa_delete_pipeline_object(ctx, ctx->Pipeline.Default);
133 * Look up the pipeline object for the given ID.
136 * Either a pointer to the pipeline object with the specified ID or \c NULL for
137 * a non-existent ID. The spec defines ID 0 as being technically
140 struct gl_pipeline_object *
141 _mesa_lookup_pipeline_object(struct gl_context *ctx, GLuint id)
146 return (struct gl_pipeline_object *)
147 _mesa_HashLookup(ctx->Pipeline.Objects, id);
151 * Add the given pipeline object to the pipeline object pool.
154 save_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
157 _mesa_HashInsert(ctx->Pipeline.Objects, obj->Name, obj);
162 * Remove the given pipeline object from the pipeline object pool.
163 * Do not deallocate the pipeline object though.
166 remove_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
169 _mesa_HashRemove(ctx->Pipeline.Objects, obj->Name);
174 * Set ptr to obj w/ reference counting.
175 * Note: this should only be called from the _mesa_reference_pipeline_object()
179 _mesa_reference_pipeline_object_(struct gl_context *ctx,
180 struct gl_pipeline_object **ptr,
181 struct gl_pipeline_object *obj)
186 /* Unreference the old pipeline object */
187 GLboolean deleteFlag = GL_FALSE;
188 struct gl_pipeline_object *oldObj = *ptr;
190 mtx_lock(&oldObj->Mutex);
191 ASSERT(oldObj->RefCount > 0);
193 deleteFlag = (oldObj->RefCount == 0);
194 mtx_unlock(&oldObj->Mutex);
197 _mesa_delete_pipeline_object(ctx, oldObj);
205 /* reference new pipeline object */
206 mtx_lock(&obj->Mutex);
207 if (obj->RefCount == 0) {
208 /* this pipeline's being deleted (look just above) */
209 /* Not sure this can ever really happen. Warn if it does. */
210 _mesa_problem(NULL, "referencing deleted pipeline object");
217 mtx_unlock(&obj->Mutex);
222 * Bound program to severals stages of the pipeline
225 _mesa_UseProgramStages(GLuint pipeline, GLbitfield stages, GLuint program)
227 GET_CURRENT_CONTEXT(ctx);
229 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
230 struct gl_shader_program *shProg = NULL;
231 GLbitfield any_valid_stages;
234 _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgramStages(pipeline)");
238 /* Object is created by any Pipeline call but glGenProgramPipelines,
239 * glIsProgramPipeline and GetProgramPipelineInfoLog
241 pipe->EverBound = GL_TRUE;
243 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
245 * "If stages is not the special value ALL_SHADER_BITS, and has a bit
246 * set that is not recognized, the error INVALID_VALUE is generated."
249 * GL_TESS_CONTROL_SHADER_BIT
250 * GL_TESS_EVALUATION_SHADER_BIT
252 any_valid_stages = GL_VERTEX_SHADER_BIT | GL_FRAGMENT_SHADER_BIT;
253 if (_mesa_has_geometry_shaders(ctx))
254 any_valid_stages |= GL_GEOMETRY_SHADER_BIT;
256 if (stages != GL_ALL_SHADER_BITS && (stages & ~any_valid_stages) != 0) {
257 _mesa_error(ctx, GL_INVALID_VALUE, "glUseProgramStages(Stages)");
261 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
264 * "The error INVALID_OPERATION is generated:
268 * - by UseProgramStages if the program pipeline object it refers
269 * to is current and the current transform feedback object is
270 * active and not paused;
272 if (ctx->_Shader == pipe) {
273 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
274 _mesa_error(ctx, GL_INVALID_OPERATION,
275 "glUseProgramStages(transform feedback active)");
281 shProg = _mesa_lookup_shader_program_err(ctx, program,
282 "glUseProgramStages");
286 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
289 * "If the program object named by program was linked without the
290 * PROGRAM_SEPARABLE parameter set, or was not linked successfully,
291 * the error INVALID_OPERATION is generated and the corresponding
292 * shader stages in the pipeline program pipeline object are not
295 if (!shProg->LinkStatus) {
296 _mesa_error(ctx, GL_INVALID_OPERATION,
297 "glUseProgramStages(program not linked)");
301 if (!shProg->SeparateShader) {
302 _mesa_error(ctx, GL_INVALID_OPERATION,
303 "glUseProgramStages(program wasn't linked with the "
304 "PROGRAM_SEPARABLE flag)");
309 /* Enable individual stages from the program as requested by the
310 * application. If there is no shader for a requested stage in the
311 * program, _mesa_use_shader_program will enable fixed-function processing
312 * as dictated by the spec.
314 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
317 * "If UseProgramStages is called with program set to zero or with a
318 * program object that contains no executable code for the given
319 * stages, it is as if the pipeline object has no programmable stage
320 * configured for the indicated shader stages."
322 if ((stages & GL_VERTEX_SHADER_BIT) != 0)
323 _mesa_use_shader_program(ctx, GL_VERTEX_SHADER, shProg, pipe);
325 if ((stages & GL_FRAGMENT_SHADER_BIT) != 0)
326 _mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER, shProg, pipe);
328 if ((stages & GL_GEOMETRY_SHADER_BIT) != 0)
329 _mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER, shProg, pipe);
333 * Use the named shader program for subsequent glUniform calls (if pipeline
337 _mesa_ActiveShaderProgram(GLuint pipeline, GLuint program)
339 GET_CURRENT_CONTEXT(ctx);
340 struct gl_shader_program *shProg = NULL;
341 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
344 shProg = _mesa_lookup_shader_program_err(ctx, program,
345 "glActiveShaderProgram(program)");
351 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveShaderProgram(pipeline)");
355 /* Object is created by any Pipeline call but glGenProgramPipelines,
356 * glIsProgramPipeline and GetProgramPipelineInfoLog
358 pipe->EverBound = GL_TRUE;
360 if ((shProg != NULL) && !shProg->LinkStatus) {
361 _mesa_error(ctx, GL_INVALID_OPERATION,
362 "glActiveShaderProgram(program %u not linked)", shProg->Name);
366 _mesa_reference_shader_program(ctx, &pipe->ActiveProgram, shProg);
370 * Make program of the pipeline current
373 _mesa_BindProgramPipeline(GLuint pipeline)
375 GET_CURRENT_CONTEXT(ctx);
376 struct gl_pipeline_object *newObj = NULL;
378 /* Rebinding the same pipeline object: no change.
380 if (ctx->_Shader->Name == pipeline)
383 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
386 * "The error INVALID_OPERATION is generated:
390 * - by BindProgramPipeline if the current transform feedback
391 * object is active and not paused;
393 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
394 _mesa_error(ctx, GL_INVALID_OPERATION,
395 "glBindProgramPipeline(transform feedback active)");
399 /* Get pointer to new pipeline object (newObj)
402 /* non-default pipeline object */
403 newObj = _mesa_lookup_pipeline_object(ctx, pipeline);
405 _mesa_error(ctx, GL_INVALID_OPERATION,
406 "glBindProgramPipeline(non-gen name)");
410 /* Object is created by any Pipeline call but glGenProgramPipelines,
411 * glIsProgramPipeline and GetProgramPipelineInfoLog
413 newObj->EverBound = GL_TRUE;
416 _mesa_bind_pipeline(ctx, newObj);
420 _mesa_bind_pipeline(struct gl_context *ctx,
421 struct gl_pipeline_object *pipe)
423 /* First bind the Pipeline to pipeline binding point */
424 _mesa_reference_pipeline_object(ctx, &ctx->Pipeline.Current, pipe);
426 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
428 * "If there is a current program object established by UseProgram,
429 * that program is considered current for all stages. Otherwise, if
430 * there is a bound program pipeline object (see section 2.11.4), the
431 * program bound to the appropriate stage of the pipeline object is
432 * considered current."
434 if (&ctx->Shader != ctx->_Shader) {
436 /* Bound the pipeline to the current program and
437 * restore the pipeline state
439 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, pipe);
441 /* Unbind the pipeline */
442 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
443 ctx->Pipeline.Default);
446 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
448 if (ctx->Driver.UseProgram)
449 ctx->Driver.UseProgram(ctx, NULL);
454 * Delete a set of pipeline objects.
456 * \param n Number of pipeline objects to delete.
457 * \param ids pipeline of \c n pipeline object IDs.
460 _mesa_DeleteProgramPipelines(GLsizei n, const GLuint *pipelines)
462 GET_CURRENT_CONTEXT(ctx);
466 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgramPipelines(n<0)");
470 for (i = 0; i < n; i++) {
471 struct gl_pipeline_object *obj =
472 _mesa_lookup_pipeline_object(ctx, pipelines[i]);
475 ASSERT(obj->Name == pipelines[i]);
477 /* If the pipeline object is currently bound, the spec says "If an
478 * object that is currently bound is deleted, the binding for that
479 * object reverts to zero and no program pipeline object becomes
482 if (obj == ctx->Pipeline.Current) {
483 _mesa_BindProgramPipeline(0);
486 /* The ID is immediately freed for re-use */
487 remove_pipeline_object(ctx, obj);
489 /* Unreference the pipeline object.
490 * If refcount hits zero, the object will be deleted.
492 _mesa_reference_pipeline_object(ctx, &obj, NULL);
498 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
499 * \param n Number of IDs to generate.
500 * \param pipelines pipeline of \c n locations to store the IDs.
503 _mesa_GenProgramPipelines(GLsizei n, GLuint *pipelines)
505 GET_CURRENT_CONTEXT(ctx);
511 _mesa_error(ctx, GL_INVALID_VALUE, "glGenProgramPipelines(n<0)");
519 first = _mesa_HashFindFreeKeyBlock(ctx->Pipeline.Objects, n);
521 for (i = 0; i < n; i++) {
522 struct gl_pipeline_object *obj;
523 GLuint name = first + i;
525 obj = _mesa_new_pipeline_object(ctx, name);
527 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenProgramPipelines");
531 save_pipeline_object(ctx, obj);
532 pipelines[i] = first + i;
538 * Determine if ID is the name of an pipeline object.
540 * \param id ID of the potential pipeline object.
541 * \return \c GL_TRUE if \c id is the name of a pipeline object,
542 * \c GL_FALSE otherwise.
545 _mesa_IsProgramPipeline(GLuint pipeline)
547 GET_CURRENT_CONTEXT(ctx);
549 struct gl_pipeline_object *obj = _mesa_lookup_pipeline_object(ctx, pipeline);
553 return obj->EverBound;
557 * glGetProgramPipelineiv() - get pipeline shader state.
560 _mesa_GetProgramPipelineiv(GLuint pipeline, GLenum pname, GLint *params)
562 GET_CURRENT_CONTEXT(ctx);
563 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
565 /* Are geometry shaders available in this context?
567 const bool has_gs = _mesa_has_geometry_shaders(ctx);
570 _mesa_error(ctx, GL_INVALID_OPERATION,
571 "glGetProgramPipelineiv(pipeline)");
575 /* Object is created by any Pipeline call but glGenProgramPipelines,
576 * glIsProgramPipeline and GetProgramPipelineInfoLog
578 pipe->EverBound = GL_TRUE;
581 case GL_ACTIVE_PROGRAM:
582 *params = pipe->ActiveProgram ? pipe->ActiveProgram->Name : 0;
584 case GL_INFO_LOG_LENGTH:
585 *params = pipe->InfoLog ? strlen(pipe->InfoLog) + 1 : 0;
587 case GL_VALIDATE_STATUS:
588 *params = pipe->Validated;
590 case GL_VERTEX_SHADER:
591 *params = pipe->CurrentProgram[MESA_SHADER_VERTEX]
592 ? pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name : 0;
594 case GL_TESS_EVALUATION_SHADER:
595 /* NOT YET SUPPORTED */
597 case GL_TESS_CONTROL_SHADER:
598 /* NOT YET SUPPORTED */
600 case GL_GEOMETRY_SHADER:
603 *params = pipe->CurrentProgram[MESA_SHADER_GEOMETRY]
604 ? pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name : 0;
606 case GL_FRAGMENT_SHADER:
607 *params = pipe->CurrentProgram[MESA_SHADER_FRAGMENT]
608 ? pipe->CurrentProgram[MESA_SHADER_FRAGMENT]->Name : 0;
614 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramPipelineiv(pname=%s)",
615 _mesa_lookup_enum_by_nr(pname));
619 * Determines whether every stage in a linked program is active in the
620 * specified pipeline.
623 program_stages_all_active(struct gl_pipeline_object *pipe,
624 const struct gl_shader_program *prog)
632 for (i = 0; i < MESA_SHADER_STAGES; i++) {
633 if (prog->_LinkedShaders[i]) {
634 if (pipe->CurrentProgram[i]) {
635 if (prog->Name != pipe->CurrentProgram[i]->Name) {
645 pipe->InfoLog = ralloc_asprintf(pipe,
646 "Program %d is not active for all "
647 "shaders that was linked",
655 _mesa_validate_program_pipeline(struct gl_context* ctx,
656 struct gl_pipeline_object *pipe,
661 pipe->Validated = GL_FALSE;
663 /* Release and reset the info log.
665 if (pipe->InfoLog != NULL)
666 ralloc_free(pipe->InfoLog);
668 pipe->InfoLog = NULL;
670 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
671 * OpenGL 4.1 spec says:
673 * "[INVALID_OPERATION] is generated by any command that transfers
674 * vertices to the GL if:
676 * - A program object is active for at least one, but not all of
677 * the shader stages that were present when the program was
680 * For each possible program stage, verify that the program bound to that
681 * stage has all of its stages active. In other words, if the program
682 * bound to the vertex stage also has a fragment shader, the fragment
683 * shader must also be bound to the fragment stage.
685 for (i = 0; i < MESA_SHADER_STAGES; i++) {
686 if (!program_stages_all_active(pipe, pipe->CurrentProgram[i])) {
691 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
692 * OpenGL 4.1 spec says:
694 * "[INVALID_OPERATION] is generated by any command that transfers
695 * vertices to the GL if:
699 * - One program object is active for at least two shader stages
700 * and a second program is active for a shader stage between two
701 * stages for which the first program was active."
703 * Without Tesselation, the only case where this can occur is the geometry
704 * shader between the fragment shader and vertex shader.
706 if (pipe->CurrentProgram[MESA_SHADER_GEOMETRY]
707 && pipe->CurrentProgram[MESA_SHADER_FRAGMENT]
708 && pipe->CurrentProgram[MESA_SHADER_VERTEX]) {
709 if (pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name == pipe->CurrentProgram[MESA_SHADER_FRAGMENT]->Name &&
710 pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name != pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name) {
712 ralloc_asprintf(pipe,
713 "Program %d is active for geometry stage between "
714 "two stages for which another program %d is "
716 pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name,
717 pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name);
722 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
723 * OpenGL 4.1 spec says:
725 * "[INVALID_OPERATION] is generated by any command that transfers
726 * vertices to the GL if:
730 * - There is an active program for tessellation control,
731 * tessellation evaluation, or geometry stages with corresponding
732 * executable shader, but there is no active program with
733 * executable vertex shader."
735 if (!pipe->CurrentProgram[MESA_SHADER_VERTEX]
736 && pipe->CurrentProgram[MESA_SHADER_GEOMETRY]) {
737 pipe->InfoLog = ralloc_strdup(pipe, "Program lacks a vertex shader");
741 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
742 * OpenGL 4.1 spec says:
744 * "[INVALID_OPERATION] is generated by any command that transfers
745 * vertices to the GL if:
749 * - There is no current program object specified by UseProgram,
750 * there is a current program pipeline object, and the current
751 * program for any shader stage has been relinked since being
752 * applied to the pipeline object via UseProgramStages with the
753 * PROGRAM_SEPARABLE parameter set to FALSE.
755 for (i = 0; i < MESA_SHADER_STAGES; i++) {
756 if (pipe->CurrentProgram[i] && !pipe->CurrentProgram[i]->SeparateShader) {
757 pipe->InfoLog = ralloc_asprintf(pipe,
758 "Program %d was relinked without "
759 "PROGRAM_SEPARABLE state",
760 pipe->CurrentProgram[i]->Name);
765 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
766 * OpenGL 4.1 spec says:
768 * "[INVALID_OPERATION] is generated by any command that transfers
769 * vertices to the GL if:
773 * - Any two active samplers in the current program object are of
774 * different types, but refer to the same texture image unit.
776 * - The number of active samplers in the program exceeds the
777 * maximum number of texture image units allowed."
779 if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe))
782 pipe->Validated = GL_TRUE;
787 _mesa_error(ctx, GL_INVALID_OPERATION,
788 "glValidateProgramPipeline failed to validate the pipeline");
794 * Check compatibility of pipeline's program
797 _mesa_ValidateProgramPipeline(GLuint pipeline)
799 GET_CURRENT_CONTEXT(ctx);
801 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
804 _mesa_error(ctx, GL_INVALID_OPERATION,
805 "glValidateProgramPipeline(pipeline)");
809 _mesa_validate_program_pipeline(ctx, pipe,
810 (ctx->_Shader->Name == pipe->Name));
814 _mesa_GetProgramPipelineInfoLog(GLuint pipeline, GLsizei bufSize,
815 GLsizei *length, GLchar *infoLog)
817 GET_CURRENT_CONTEXT(ctx);
819 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
822 _mesa_error(ctx, GL_INVALID_VALUE,
823 "glGetProgramPipelineInfoLog(pipeline)");
828 _mesa_error(ctx, GL_INVALID_VALUE,
829 "glGetProgramPipelineInfoLog(bufSize)");
834 _mesa_copy_string(infoLog, bufSize, length, pipe->InfoLog);