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.
35 #include "main/glheader.h"
36 #include "main/context.h"
37 #include "main/dispatch.h"
38 #include "main/enums.h"
39 #include "main/hash.h"
40 #include "main/mtypes.h"
41 #include "main/pipelineobj.h"
42 #include "main/shaderapi.h"
43 #include "main/shaderobj.h"
44 #include "main/transformfeedback.h"
45 #include "main/uniforms.h"
46 #include "compiler/glsl/glsl_parser_extras.h"
47 #include "compiler/glsl/ir_uniform.h"
48 #include "program/program.h"
49 #include "program/prog_parameter.h"
50 #include "util/ralloc.h"
53 * Delete a pipeline object.
56 _mesa_delete_pipeline_object(struct gl_context
*ctx
,
57 struct gl_pipeline_object
*obj
)
61 _mesa_reference_program(ctx
, &obj
->_CurrentFragmentProgram
, NULL
);
63 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
64 _mesa_reference_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(UNUSED 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 use_program_stage(struct gl_context
*ctx
, GLenum type
,
223 struct gl_shader_program
*shProg
,
224 struct gl_pipeline_object
*pipe
) {
225 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
226 struct gl_program
*prog
= NULL
;
227 if (shProg
&& shProg
->_LinkedShaders
[stage
])
228 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
230 _mesa_use_program(ctx
, stage
, prog
, pipe
);
234 * Bound program to severals stages of the pipeline
237 _mesa_UseProgramStages(GLuint pipeline
, GLbitfield stages
, GLuint program
)
239 GET_CURRENT_CONTEXT(ctx
);
241 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
242 struct gl_shader_program
*shProg
= NULL
;
243 GLbitfield any_valid_stages
;
245 if (MESA_VERBOSE
& VERBOSE_API
)
246 _mesa_debug(ctx
, "glUseProgramStages(%u, 0x%x, %u)\n",
247 pipeline
, stages
, program
);
250 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUseProgramStages(pipeline)");
254 /* Object is created by any Pipeline call but glGenProgramPipelines,
255 * glIsProgramPipeline and GetProgramPipelineInfoLog
257 pipe
->EverBound
= GL_TRUE
;
259 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
261 * "If stages is not the special value ALL_SHADER_BITS, and has a bit
262 * set that is not recognized, the error INVALID_VALUE is generated."
264 any_valid_stages
= GL_VERTEX_SHADER_BIT
| GL_FRAGMENT_SHADER_BIT
;
265 if (_mesa_has_geometry_shaders(ctx
))
266 any_valid_stages
|= GL_GEOMETRY_SHADER_BIT
;
267 if (_mesa_has_tessellation(ctx
))
268 any_valid_stages
|= GL_TESS_CONTROL_SHADER_BIT
|
269 GL_TESS_EVALUATION_SHADER_BIT
;
270 if (_mesa_has_compute_shaders(ctx
))
271 any_valid_stages
|= GL_COMPUTE_SHADER_BIT
;
273 if (stages
!= GL_ALL_SHADER_BITS
&& (stages
& ~any_valid_stages
) != 0) {
274 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUseProgramStages(Stages)");
278 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
281 * "The error INVALID_OPERATION is generated:
285 * - by UseProgramStages if the program pipeline object it refers
286 * to is current and the current transform feedback object is
287 * active and not paused;
289 if (ctx
->_Shader
== pipe
) {
290 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
291 _mesa_error(ctx
, GL_INVALID_OPERATION
,
292 "glUseProgramStages(transform feedback active)");
298 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
299 "glUseProgramStages");
303 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
306 * "If the program object named by program was linked without the
307 * PROGRAM_SEPARABLE parameter set, or was not linked successfully,
308 * the error INVALID_OPERATION is generated and the corresponding
309 * shader stages in the pipeline program pipeline object are not
312 if (!shProg
->data
->LinkStatus
) {
313 _mesa_error(ctx
, GL_INVALID_OPERATION
,
314 "glUseProgramStages(program not linked)");
318 if (!shProg
->SeparateShader
) {
319 _mesa_error(ctx
, GL_INVALID_OPERATION
,
320 "glUseProgramStages(program wasn't linked with the "
321 "PROGRAM_SEPARABLE flag)");
326 /* Enable individual stages from the program as requested by the
327 * application. If there is no shader for a requested stage in the
328 * program, _mesa_use_shader_program will enable fixed-function processing
329 * as dictated by the spec.
331 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
334 * "If UseProgramStages is called with program set to zero or with a
335 * program object that contains no executable code for the given
336 * stages, it is as if the pipeline object has no programmable stage
337 * configured for the indicated shader stages."
339 if ((stages
& GL_VERTEX_SHADER_BIT
) != 0)
340 use_program_stage(ctx
, GL_VERTEX_SHADER
, shProg
, pipe
);
342 if ((stages
& GL_FRAGMENT_SHADER_BIT
) != 0)
343 use_program_stage(ctx
, GL_FRAGMENT_SHADER
, shProg
, pipe
);
345 if ((stages
& GL_GEOMETRY_SHADER_BIT
) != 0)
346 use_program_stage(ctx
, GL_GEOMETRY_SHADER
, shProg
, pipe
);
348 if ((stages
& GL_TESS_CONTROL_SHADER_BIT
) != 0)
349 use_program_stage(ctx
, GL_TESS_CONTROL_SHADER
, shProg
, pipe
);
351 if ((stages
& GL_TESS_EVALUATION_SHADER_BIT
) != 0)
352 use_program_stage(ctx
, GL_TESS_EVALUATION_SHADER
, shProg
, pipe
);
354 if ((stages
& GL_COMPUTE_SHADER_BIT
) != 0)
355 use_program_stage(ctx
, GL_COMPUTE_SHADER
, shProg
, pipe
);
357 pipe
->Validated
= false;
361 * Use the named shader program for subsequent glUniform calls (if pipeline
365 _mesa_ActiveShaderProgram(GLuint pipeline
, GLuint program
)
367 GET_CURRENT_CONTEXT(ctx
);
368 struct gl_shader_program
*shProg
= NULL
;
369 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
371 if (MESA_VERBOSE
& VERBOSE_API
)
372 _mesa_debug(ctx
, "glActiveShaderProgram(%u, %u)\n", pipeline
, program
);
375 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
376 "glActiveShaderProgram(program)");
382 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveShaderProgram(pipeline)");
386 /* Object is created by any Pipeline call but glGenProgramPipelines,
387 * glIsProgramPipeline and GetProgramPipelineInfoLog
389 pipe
->EverBound
= GL_TRUE
;
391 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
392 _mesa_error(ctx
, GL_INVALID_OPERATION
,
393 "glActiveShaderProgram(program %u not linked)", shProg
->Name
);
397 _mesa_reference_shader_program(ctx
, &pipe
->ActiveProgram
, shProg
);
401 * Make program of the pipeline current
404 _mesa_BindProgramPipeline(GLuint pipeline
)
406 GET_CURRENT_CONTEXT(ctx
);
407 struct gl_pipeline_object
*newObj
= NULL
;
409 if (MESA_VERBOSE
& VERBOSE_API
)
410 _mesa_debug(ctx
, "glBindProgramPipeline(%u)\n", pipeline
);
412 /* Rebinding the same pipeline object: no change.
414 if (ctx
->_Shader
->Name
== pipeline
)
417 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
420 * "The error INVALID_OPERATION is generated:
424 * - by BindProgramPipeline if the current transform feedback
425 * object is active and not paused;
427 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
428 _mesa_error(ctx
, GL_INVALID_OPERATION
,
429 "glBindProgramPipeline(transform feedback active)");
433 /* Get pointer to new pipeline object (newObj)
436 /* non-default pipeline object */
437 newObj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
440 "glBindProgramPipeline(non-gen name)");
444 /* Object is created by any Pipeline call but glGenProgramPipelines,
445 * glIsProgramPipeline and GetProgramPipelineInfoLog
447 newObj
->EverBound
= GL_TRUE
;
450 _mesa_bind_pipeline(ctx
, newObj
);
454 _mesa_bind_pipeline(struct gl_context
*ctx
,
455 struct gl_pipeline_object
*pipe
)
458 /* First bind the Pipeline to pipeline binding point */
459 _mesa_reference_pipeline_object(ctx
, &ctx
->Pipeline
.Current
, pipe
);
461 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
463 * "If there is a current program object established by UseProgram,
464 * that program is considered current for all stages. Otherwise, if
465 * there is a bound program pipeline object (see section 2.11.4), the
466 * program bound to the appropriate stage of the pipeline object is
467 * considered current."
469 if (&ctx
->Shader
!= ctx
->_Shader
) {
471 /* Bound the pipeline to the current program and
472 * restore the pipeline state
474 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, pipe
);
476 /* Unbind the pipeline */
477 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
478 ctx
->Pipeline
.Default
);
481 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
483 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
484 struct gl_program
*prog
= ctx
->_Shader
->CurrentProgram
[i
];
486 _mesa_program_init_subroutine_defaults(ctx
, prog
);
493 * Delete a set of pipeline objects.
495 * \param n Number of pipeline objects to delete.
496 * \param ids pipeline of \c n pipeline object IDs.
499 _mesa_DeleteProgramPipelines(GLsizei n
, const GLuint
*pipelines
)
501 GET_CURRENT_CONTEXT(ctx
);
504 if (MESA_VERBOSE
& VERBOSE_API
)
505 _mesa_debug(ctx
, "glDeleteProgramPipelines(%d, %p)\n", n
, pipelines
);
508 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteProgramPipelines(n<0)");
512 for (i
= 0; i
< n
; i
++) {
513 struct gl_pipeline_object
*obj
=
514 _mesa_lookup_pipeline_object(ctx
, pipelines
[i
]);
517 assert(obj
->Name
== pipelines
[i
]);
519 /* If the pipeline object is currently bound, the spec says "If an
520 * object that is currently bound is deleted, the binding for that
521 * object reverts to zero and no program pipeline object becomes
524 if (obj
== ctx
->Pipeline
.Current
) {
525 _mesa_BindProgramPipeline(0);
528 /* The ID is immediately freed for re-use */
529 remove_pipeline_object(ctx
, obj
);
531 /* Unreference the pipeline object.
532 * If refcount hits zero, the object will be deleted.
534 _mesa_reference_pipeline_object(ctx
, &obj
, NULL
);
540 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
541 * \param n Number of IDs to generate.
542 * \param pipelines pipeline of \c n locations to store the IDs.
545 create_program_pipelines(struct gl_context
*ctx
, GLsizei n
, GLuint
*pipelines
,
552 func
= dsa
? "glCreateProgramPipelines" : "glGenProgramPipelines";
555 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s (n < 0)", func
);
563 first
= _mesa_HashFindFreeKeyBlock(ctx
->Pipeline
.Objects
, n
);
565 for (i
= 0; i
< n
; i
++) {
566 struct gl_pipeline_object
*obj
;
567 GLuint name
= first
+ i
;
569 obj
= _mesa_new_pipeline_object(ctx
, name
);
571 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
576 /* make dsa-allocated objects behave like program objects */
577 obj
->EverBound
= GL_TRUE
;
580 save_pipeline_object(ctx
, obj
);
581 pipelines
[i
] = first
+ i
;
587 _mesa_GenProgramPipelines(GLsizei n
, GLuint
*pipelines
)
589 GET_CURRENT_CONTEXT(ctx
);
591 if (MESA_VERBOSE
& VERBOSE_API
)
592 _mesa_debug(ctx
, "glGenProgramPipelines(%d, %p)\n", n
, pipelines
);
594 create_program_pipelines(ctx
, n
, pipelines
, false);
598 _mesa_CreateProgramPipelines(GLsizei n
, GLuint
*pipelines
)
600 GET_CURRENT_CONTEXT(ctx
);
602 if (MESA_VERBOSE
& VERBOSE_API
)
603 _mesa_debug(ctx
, "glCreateProgramPipelines(%d, %p)\n", n
, pipelines
);
605 create_program_pipelines(ctx
, n
, pipelines
, true);
609 * Determine if ID is the name of an pipeline object.
611 * \param id ID of the potential pipeline object.
612 * \return \c GL_TRUE if \c id is the name of a pipeline object,
613 * \c GL_FALSE otherwise.
616 _mesa_IsProgramPipeline(GLuint pipeline
)
618 GET_CURRENT_CONTEXT(ctx
);
620 if (MESA_VERBOSE
& VERBOSE_API
)
621 _mesa_debug(ctx
, "glIsProgramPipeline(%u)\n", pipeline
);
623 struct gl_pipeline_object
*obj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
627 return obj
->EverBound
;
631 * glGetProgramPipelineiv() - get pipeline shader state.
634 _mesa_GetProgramPipelineiv(GLuint pipeline
, GLenum pname
, GLint
*params
)
636 GET_CURRENT_CONTEXT(ctx
);
637 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
639 if (MESA_VERBOSE
& VERBOSE_API
)
640 _mesa_debug(ctx
, "glGetProgramPipelineiv(%u, %d, %p)\n",
641 pipeline
, pname
, params
);
643 /* Are geometry shaders available in this context?
645 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
646 const bool has_tess
= _mesa_has_tessellation(ctx
);
649 _mesa_error(ctx
, GL_INVALID_OPERATION
,
650 "glGetProgramPipelineiv(pipeline)");
654 /* Object is created by any Pipeline call but glGenProgramPipelines,
655 * glIsProgramPipeline and GetProgramPipelineInfoLog
657 pipe
->EverBound
= GL_TRUE
;
660 case GL_ACTIVE_PROGRAM
:
661 *params
= pipe
->ActiveProgram
? pipe
->ActiveProgram
->Name
: 0;
663 case GL_INFO_LOG_LENGTH
:
664 *params
= (pipe
->InfoLog
&& pipe
->InfoLog
[0] != '\0') ?
665 strlen(pipe
->InfoLog
) + 1 : 0;
667 case GL_VALIDATE_STATUS
:
668 *params
= pipe
->Validated
;
670 case GL_VERTEX_SHADER
:
671 *params
= pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
672 ? pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]->Id
: 0;
674 case GL_TESS_EVALUATION_SHADER
:
677 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]
678 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]->Id
: 0;
680 case GL_TESS_CONTROL_SHADER
:
683 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]
684 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]->Id
: 0;
686 case GL_GEOMETRY_SHADER
:
689 *params
= pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]
690 ? pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Id
: 0;
692 case GL_FRAGMENT_SHADER
:
693 *params
= pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]
694 ? pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]->Id
: 0;
696 case GL_COMPUTE_SHADER
:
697 if (!_mesa_has_compute_shaders(ctx
))
699 *params
= pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]
700 ? pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]->Id
: 0;
706 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramPipelineiv(pname=%s)",
707 _mesa_enum_to_string(pname
));
711 * Determines whether every stage in a linked program is active in the
712 * specified pipeline.
715 program_stages_all_active(struct gl_pipeline_object
*pipe
,
716 const struct gl_program
*prog
)
723 unsigned mask
= prog
->sh
.data
->linked_stages
;
725 const int i
= u_bit_scan(&mask
);
726 if (pipe
->CurrentProgram
[i
]) {
727 if (prog
->Id
!= pipe
->CurrentProgram
[i
]->Id
) {
736 pipe
->InfoLog
= ralloc_asprintf(pipe
,
737 "Program %d is not active for all "
738 "shaders that was linked",
746 program_stages_interleaved_illegally(const struct gl_pipeline_object
*pipe
)
748 unsigned prev_linked_stages
= 0;
750 /* Look for programs bound to stages: A -> B -> A, with any intervening
751 * sequence of unrelated programs or empty stages.
753 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
754 struct gl_program
*cur
= pipe
->CurrentProgram
[i
];
756 /* Empty stages anywhere in the pipe are OK. Also we can be confident
757 * that if the linked_stages mask matches we are looking at the same
758 * linked program because a previous validation call to
759 * program_stages_all_active() will have already failed if two different
760 * programs with the sames stages linked are not active for all linked
763 if (!cur
|| cur
->sh
.data
->linked_stages
== prev_linked_stages
)
766 if (prev_linked_stages
) {
767 /* We've seen an A -> B transition; look at the rest of the pipe
768 * to see if we ever see A again.
770 if (prev_linked_stages
>> (i
+ 1))
774 prev_linked_stages
= cur
->sh
.data
->linked_stages
;
781 _mesa_validate_program_pipeline(struct gl_context
* ctx
,
782 struct gl_pipeline_object
*pipe
)
785 bool program_empty
= true;
787 pipe
->Validated
= GL_FALSE
;
789 /* Release and reset the info log.
791 if (pipe
->InfoLog
!= NULL
)
792 ralloc_free(pipe
->InfoLog
);
794 pipe
->InfoLog
= NULL
;
796 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
797 * OpenGL 4.1 spec says:
799 * "[INVALID_OPERATION] is generated by any command that transfers
800 * vertices to the GL if:
802 * - A program object is active for at least one, but not all of
803 * the shader stages that were present when the program was
806 * For each possible program stage, verify that the program bound to that
807 * stage has all of its stages active. In other words, if the program
808 * bound to the vertex stage also has a fragment shader, the fragment
809 * shader must also be bound to the fragment stage.
811 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
812 if (!program_stages_all_active(pipe
, pipe
->CurrentProgram
[i
])) {
817 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
818 * OpenGL 4.1 spec says:
820 * "[INVALID_OPERATION] is generated by any command that transfers
821 * vertices to the GL if:
825 * - One program object is active for at least two shader stages
826 * and a second program is active for a shader stage between two
827 * stages for which the first program was active."
829 if (program_stages_interleaved_illegally(pipe
)) {
832 "Program is active for multiple shader stages with an "
833 "intervening stage provided by another program");
837 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
838 * OpenGL 4.1 spec says:
840 * "[INVALID_OPERATION] is generated by any command that transfers
841 * vertices to the GL if:
845 * - There is an active program for tessellation control,
846 * tessellation evaluation, or geometry stages with corresponding
847 * executable shader, but there is no active program with
848 * executable vertex shader."
850 if (!pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
851 && (pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
] ||
852 pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
] ||
853 pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
])) {
854 pipe
->InfoLog
= ralloc_strdup(pipe
, "Program lacks a vertex shader");
858 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
859 * OpenGL 4.1 spec says:
861 * "[INVALID_OPERATION] is generated by any command that transfers
862 * vertices to the GL if:
866 * - There is no current program object specified by UseProgram,
867 * there is a current program pipeline object, and the current
868 * program for any shader stage has been relinked since being
869 * applied to the pipeline object via UseProgramStages with the
870 * PROGRAM_SEPARABLE parameter set to FALSE.
872 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
873 if (pipe
->CurrentProgram
[i
] &&
874 !pipe
->CurrentProgram
[i
]->info
.separate_shader
) {
875 pipe
->InfoLog
= ralloc_asprintf(pipe
,
876 "Program %d was relinked without "
877 "PROGRAM_SEPARABLE state",
878 pipe
->CurrentProgram
[i
]->Id
);
883 /* Section 11.1.3.11 (Validation) of the OpenGL 4.5 spec says:
885 * "An INVALID_OPERATION error is generated by any command that trans-
886 * fers vertices to the GL or launches compute work if the current set
887 * of active program objects cannot be executed, for reasons including:
891 * - There is no current program object specified by UseProgram,
892 * there is a current program pipeline object, and that object is
893 * empty (no executable code is installed for any stage).
895 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
896 if (pipe
->CurrentProgram
[i
]) {
897 program_empty
= false;
906 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
907 * OpenGL 4.1 spec says:
909 * "[INVALID_OPERATION] is generated by any command that transfers
910 * vertices to the GL if:
914 * - Any two active samplers in the current program object are of
915 * different types, but refer to the same texture image unit.
917 * - The number of active samplers in the program exceeds the
918 * maximum number of texture image units allowed."
920 if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe
))
923 /* Validate inputs against outputs, this cannot be done during linking
924 * since programs have been linked separately from each other.
926 * Section 11.1.3.11 (Validation) of the OpenGL 4.5 Core Profile spec says:
928 * "Separable program objects may have validation failures that cannot be
929 * detected without the complete program pipeline. Mismatched interfaces,
930 * improper usage of program objects together, and the same
931 * state-dependent failures can result in validation errors for such
934 * OpenGL ES 3.1 specification has the same text.
936 * Section 11.1.3.11 (Validation) of the OpenGL ES spec also says:
938 * An INVALID_OPERATION error is generated by any command that transfers
939 * vertices to the GL or launches compute work if the current set of
940 * active program objects cannot be executed, for reasons including:
942 * * The current program pipeline object contains a shader interface
943 * that doesn't have an exact match (see section 7.4.1)
945 * Based on this, only perform the most-strict checking on ES or when the
946 * application has created a debug context.
948 if ((_mesa_is_gles(ctx
) || (ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
)) &&
949 !_mesa_validate_pipeline_io(pipe
)) {
950 if (_mesa_is_gles(ctx
))
953 static GLuint msg_id
= 0;
955 _mesa_gl_debug(ctx
, &msg_id
,
956 MESA_DEBUG_SOURCE_API
,
957 MESA_DEBUG_TYPE_PORTABILITY
,
958 MESA_DEBUG_SEVERITY_MEDIUM
,
959 "glValidateProgramPipeline: pipeline %u does not meet "
960 "strict OpenGL ES 3.1 requirements and may not be "
961 "portable across desktop hardware\n",
965 pipe
->Validated
= GL_TRUE
;
970 * Check compatibility of pipeline's program
973 _mesa_ValidateProgramPipeline(GLuint pipeline
)
975 GET_CURRENT_CONTEXT(ctx
);
977 if (MESA_VERBOSE
& VERBOSE_API
)
978 _mesa_debug(ctx
, "glValidateProgramPipeline(%u)\n", pipeline
);
980 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
983 _mesa_error(ctx
, GL_INVALID_OPERATION
,
984 "glValidateProgramPipeline(pipeline)");
988 _mesa_validate_program_pipeline(ctx
, pipe
);
992 _mesa_GetProgramPipelineInfoLog(GLuint pipeline
, GLsizei bufSize
,
993 GLsizei
*length
, GLchar
*infoLog
)
995 GET_CURRENT_CONTEXT(ctx
);
997 if (MESA_VERBOSE
& VERBOSE_API
)
998 _mesa_debug(ctx
, "glGetProgramPipelineInfoLog(%u, %d, %p, %p)\n",
999 pipeline
, bufSize
, length
, infoLog
);
1001 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
1004 _mesa_error(ctx
, GL_INVALID_VALUE
,
1005 "glGetProgramPipelineInfoLog(pipeline)");
1010 _mesa_error(ctx
, GL_INVALID_VALUE
,
1011 "glGetProgramPipelineInfoLog(bufSize)");
1015 _mesa_copy_string(infoLog
, bufSize
, length
, pipe
->InfoLog
);