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
);
65 _mesa_reference_shader_program(ctx
, &obj
->ReferencedPrograms
[i
], NULL
);
68 _mesa_reference_shader_program(ctx
, &obj
->ActiveProgram
, NULL
);
69 mtx_destroy(&obj
->Mutex
);
75 * Allocate and initialize a new pipeline object.
77 static struct gl_pipeline_object
*
78 _mesa_new_pipeline_object(struct gl_context
*ctx
, GLuint name
)
80 struct gl_pipeline_object
*obj
= rzalloc(NULL
, struct gl_pipeline_object
);
83 mtx_init(&obj
->Mutex
, mtx_plain
);
85 obj
->Flags
= _mesa_get_shader_flags();
93 * Initialize pipeline object state for given context.
96 _mesa_init_pipeline(struct gl_context
*ctx
)
98 ctx
->Pipeline
.Objects
= _mesa_NewHashTable();
100 ctx
->Pipeline
.Current
= NULL
;
102 /* Install a default Pipeline */
103 ctx
->Pipeline
.Default
= _mesa_new_pipeline_object(ctx
, 0);
104 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
109 * Callback for deleting a pipeline object. Called by _mesa_HashDeleteAll().
112 delete_pipelineobj_cb(UNUSED GLuint id
, void *data
, void *userData
)
114 struct gl_pipeline_object
*obj
= (struct gl_pipeline_object
*) data
;
115 struct gl_context
*ctx
= (struct gl_context
*) userData
;
116 _mesa_delete_pipeline_object(ctx
, obj
);
121 * Free pipeline state for given context.
124 _mesa_free_pipeline_data(struct gl_context
*ctx
)
126 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
128 _mesa_HashDeleteAll(ctx
->Pipeline
.Objects
, delete_pipelineobj_cb
, ctx
);
129 _mesa_DeleteHashTable(ctx
->Pipeline
.Objects
);
131 _mesa_delete_pipeline_object(ctx
, ctx
->Pipeline
.Default
);
135 * Look up the pipeline object for the given ID.
138 * Either a pointer to the pipeline object with the specified ID or \c NULL for
139 * a non-existent ID. The spec defines ID 0 as being technically
142 struct gl_pipeline_object
*
143 _mesa_lookup_pipeline_object(struct gl_context
*ctx
, GLuint id
)
148 return (struct gl_pipeline_object
*)
149 _mesa_HashLookup(ctx
->Pipeline
.Objects
, id
);
153 * Add the given pipeline object to the pipeline object pool.
156 save_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
159 _mesa_HashInsert(ctx
->Pipeline
.Objects
, obj
->Name
, obj
);
164 * Remove the given pipeline object from the pipeline object pool.
165 * Do not deallocate the pipeline object though.
168 remove_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
171 _mesa_HashRemove(ctx
->Pipeline
.Objects
, obj
->Name
);
176 * Set ptr to obj w/ reference counting.
177 * Note: this should only be called from the _mesa_reference_pipeline_object()
181 _mesa_reference_pipeline_object_(struct gl_context
*ctx
,
182 struct gl_pipeline_object
**ptr
,
183 struct gl_pipeline_object
*obj
)
188 /* Unreference the old pipeline object */
189 GLboolean deleteFlag
= GL_FALSE
;
190 struct gl_pipeline_object
*oldObj
= *ptr
;
192 mtx_lock(&oldObj
->Mutex
);
193 assert(oldObj
->RefCount
> 0);
195 deleteFlag
= (oldObj
->RefCount
== 0);
196 mtx_unlock(&oldObj
->Mutex
);
199 _mesa_delete_pipeline_object(ctx
, oldObj
);
207 /* reference new pipeline object */
208 mtx_lock(&obj
->Mutex
);
209 if (obj
->RefCount
== 0) {
210 /* this pipeline's being deleted (look just above) */
211 /* Not sure this can ever really happen. Warn if it does. */
212 _mesa_problem(NULL
, "referencing deleted pipeline object");
219 mtx_unlock(&obj
->Mutex
);
224 use_program_stage(struct gl_context
*ctx
, GLenum type
,
225 struct gl_shader_program
*shProg
,
226 struct gl_pipeline_object
*pipe
) {
227 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
228 struct gl_program
*prog
= NULL
;
229 if (shProg
&& shProg
->_LinkedShaders
[stage
])
230 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
232 _mesa_use_program(ctx
, stage
, shProg
, prog
, pipe
);
236 * Bound program to severals stages of the pipeline
239 _mesa_UseProgramStages(GLuint pipeline
, GLbitfield stages
, GLuint program
)
241 GET_CURRENT_CONTEXT(ctx
);
243 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
244 struct gl_shader_program
*shProg
= NULL
;
245 GLbitfield any_valid_stages
;
247 if (MESA_VERBOSE
& VERBOSE_API
)
248 _mesa_debug(ctx
, "glUseProgramStages(%u, 0x%x, %u)\n",
249 pipeline
, stages
, program
);
252 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUseProgramStages(pipeline)");
256 /* Object is created by any Pipeline call but glGenProgramPipelines,
257 * glIsProgramPipeline and GetProgramPipelineInfoLog
259 pipe
->EverBound
= GL_TRUE
;
261 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
263 * "If stages is not the special value ALL_SHADER_BITS, and has a bit
264 * set that is not recognized, the error INVALID_VALUE is generated."
266 any_valid_stages
= GL_VERTEX_SHADER_BIT
| GL_FRAGMENT_SHADER_BIT
;
267 if (_mesa_has_geometry_shaders(ctx
))
268 any_valid_stages
|= GL_GEOMETRY_SHADER_BIT
;
269 if (_mesa_has_tessellation(ctx
))
270 any_valid_stages
|= GL_TESS_CONTROL_SHADER_BIT
|
271 GL_TESS_EVALUATION_SHADER_BIT
;
272 if (_mesa_has_compute_shaders(ctx
))
273 any_valid_stages
|= GL_COMPUTE_SHADER_BIT
;
275 if (stages
!= GL_ALL_SHADER_BITS
&& (stages
& ~any_valid_stages
) != 0) {
276 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUseProgramStages(Stages)");
280 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
283 * "The error INVALID_OPERATION is generated:
287 * - by UseProgramStages if the program pipeline object it refers
288 * to is current and the current transform feedback object is
289 * active and not paused;
291 if (ctx
->_Shader
== pipe
) {
292 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
294 "glUseProgramStages(transform feedback active)");
300 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
301 "glUseProgramStages");
305 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
308 * "If the program object named by program was linked without the
309 * PROGRAM_SEPARABLE parameter set, or was not linked successfully,
310 * the error INVALID_OPERATION is generated and the corresponding
311 * shader stages in the pipeline program pipeline object are not
314 if (!shProg
->data
->LinkStatus
) {
315 _mesa_error(ctx
, GL_INVALID_OPERATION
,
316 "glUseProgramStages(program not linked)");
320 if (!shProg
->SeparateShader
) {
321 _mesa_error(ctx
, GL_INVALID_OPERATION
,
322 "glUseProgramStages(program wasn't linked with the "
323 "PROGRAM_SEPARABLE flag)");
328 /* Enable individual stages from the program as requested by the
329 * application. If there is no shader for a requested stage in the
330 * program, _mesa_use_shader_program will enable fixed-function processing
331 * as dictated by the spec.
333 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
336 * "If UseProgramStages is called with program set to zero or with a
337 * program object that contains no executable code for the given
338 * stages, it is as if the pipeline object has no programmable stage
339 * configured for the indicated shader stages."
341 if ((stages
& GL_VERTEX_SHADER_BIT
) != 0)
342 use_program_stage(ctx
, GL_VERTEX_SHADER
, shProg
, pipe
);
344 if ((stages
& GL_FRAGMENT_SHADER_BIT
) != 0)
345 use_program_stage(ctx
, GL_FRAGMENT_SHADER
, shProg
, pipe
);
347 if ((stages
& GL_GEOMETRY_SHADER_BIT
) != 0)
348 use_program_stage(ctx
, GL_GEOMETRY_SHADER
, shProg
, pipe
);
350 if ((stages
& GL_TESS_CONTROL_SHADER_BIT
) != 0)
351 use_program_stage(ctx
, GL_TESS_CONTROL_SHADER
, shProg
, pipe
);
353 if ((stages
& GL_TESS_EVALUATION_SHADER_BIT
) != 0)
354 use_program_stage(ctx
, GL_TESS_EVALUATION_SHADER
, shProg
, pipe
);
356 if ((stages
& GL_COMPUTE_SHADER_BIT
) != 0)
357 use_program_stage(ctx
, GL_COMPUTE_SHADER
, shProg
, pipe
);
359 pipe
->Validated
= false;
363 * Use the named shader program for subsequent glUniform calls (if pipeline
367 _mesa_ActiveShaderProgram(GLuint pipeline
, GLuint program
)
369 GET_CURRENT_CONTEXT(ctx
);
370 struct gl_shader_program
*shProg
= NULL
;
371 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
373 if (MESA_VERBOSE
& VERBOSE_API
)
374 _mesa_debug(ctx
, "glActiveShaderProgram(%u, %u)\n", pipeline
, program
);
377 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
378 "glActiveShaderProgram(program)");
384 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveShaderProgram(pipeline)");
388 /* Object is created by any Pipeline call but glGenProgramPipelines,
389 * glIsProgramPipeline and GetProgramPipelineInfoLog
391 pipe
->EverBound
= GL_TRUE
;
393 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
394 _mesa_error(ctx
, GL_INVALID_OPERATION
,
395 "glActiveShaderProgram(program %u not linked)", shProg
->Name
);
399 _mesa_reference_shader_program(ctx
, &pipe
->ActiveProgram
, shProg
);
403 * Make program of the pipeline current
406 _mesa_BindProgramPipeline(GLuint pipeline
)
408 GET_CURRENT_CONTEXT(ctx
);
409 struct gl_pipeline_object
*newObj
= NULL
;
411 if (MESA_VERBOSE
& VERBOSE_API
)
412 _mesa_debug(ctx
, "glBindProgramPipeline(%u)\n", pipeline
);
414 /* Rebinding the same pipeline object: no change.
416 if (ctx
->_Shader
->Name
== pipeline
)
419 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
422 * "The error INVALID_OPERATION is generated:
426 * - by BindProgramPipeline if the current transform feedback
427 * object is active and not paused;
429 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
430 _mesa_error(ctx
, GL_INVALID_OPERATION
,
431 "glBindProgramPipeline(transform feedback active)");
435 /* Get pointer to new pipeline object (newObj)
438 /* non-default pipeline object */
439 newObj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
441 _mesa_error(ctx
, GL_INVALID_OPERATION
,
442 "glBindProgramPipeline(non-gen name)");
446 /* Object is created by any Pipeline call but glGenProgramPipelines,
447 * glIsProgramPipeline and GetProgramPipelineInfoLog
449 newObj
->EverBound
= GL_TRUE
;
452 _mesa_bind_pipeline(ctx
, newObj
);
456 _mesa_bind_pipeline(struct gl_context
*ctx
,
457 struct gl_pipeline_object
*pipe
)
460 /* First bind the Pipeline to pipeline binding point */
461 _mesa_reference_pipeline_object(ctx
, &ctx
->Pipeline
.Current
, pipe
);
463 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
465 * "If there is a current program object established by UseProgram,
466 * that program is considered current for all stages. Otherwise, if
467 * there is a bound program pipeline object (see section 2.11.4), the
468 * program bound to the appropriate stage of the pipeline object is
469 * considered current."
471 if (&ctx
->Shader
!= ctx
->_Shader
) {
473 /* Bound the pipeline to the current program and
474 * restore the pipeline state
476 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, pipe
);
478 /* Unbind the pipeline */
479 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
480 ctx
->Pipeline
.Default
);
483 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
485 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
486 struct gl_program
*prog
= ctx
->_Shader
->CurrentProgram
[i
];
488 _mesa_program_init_subroutine_defaults(ctx
, prog
);
495 * Delete a set of pipeline objects.
497 * \param n Number of pipeline objects to delete.
498 * \param ids pipeline of \c n pipeline object IDs.
501 _mesa_DeleteProgramPipelines(GLsizei n
, const GLuint
*pipelines
)
503 GET_CURRENT_CONTEXT(ctx
);
506 if (MESA_VERBOSE
& VERBOSE_API
)
507 _mesa_debug(ctx
, "glDeleteProgramPipelines(%d, %p)\n", n
, pipelines
);
510 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteProgramPipelines(n<0)");
514 for (i
= 0; i
< n
; i
++) {
515 struct gl_pipeline_object
*obj
=
516 _mesa_lookup_pipeline_object(ctx
, pipelines
[i
]);
519 assert(obj
->Name
== pipelines
[i
]);
521 /* If the pipeline object is currently bound, the spec says "If an
522 * object that is currently bound is deleted, the binding for that
523 * object reverts to zero and no program pipeline object becomes
526 if (obj
== ctx
->Pipeline
.Current
) {
527 _mesa_BindProgramPipeline(0);
530 /* The ID is immediately freed for re-use */
531 remove_pipeline_object(ctx
, obj
);
533 /* Unreference the pipeline object.
534 * If refcount hits zero, the object will be deleted.
536 _mesa_reference_pipeline_object(ctx
, &obj
, NULL
);
542 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
543 * \param n Number of IDs to generate.
544 * \param pipelines pipeline of \c n locations to store the IDs.
547 create_program_pipelines(struct gl_context
*ctx
, GLsizei n
, GLuint
*pipelines
,
554 func
= dsa
? "glCreateProgramPipelines" : "glGenProgramPipelines";
557 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s (n < 0)", func
);
565 first
= _mesa_HashFindFreeKeyBlock(ctx
->Pipeline
.Objects
, n
);
567 for (i
= 0; i
< n
; i
++) {
568 struct gl_pipeline_object
*obj
;
569 GLuint name
= first
+ i
;
571 obj
= _mesa_new_pipeline_object(ctx
, name
);
573 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
578 /* make dsa-allocated objects behave like program objects */
579 obj
->EverBound
= GL_TRUE
;
582 save_pipeline_object(ctx
, obj
);
583 pipelines
[i
] = first
+ i
;
589 _mesa_GenProgramPipelines(GLsizei n
, GLuint
*pipelines
)
591 GET_CURRENT_CONTEXT(ctx
);
593 if (MESA_VERBOSE
& VERBOSE_API
)
594 _mesa_debug(ctx
, "glGenProgramPipelines(%d, %p)\n", n
, pipelines
);
596 create_program_pipelines(ctx
, n
, pipelines
, false);
600 _mesa_CreateProgramPipelines(GLsizei n
, GLuint
*pipelines
)
602 GET_CURRENT_CONTEXT(ctx
);
604 if (MESA_VERBOSE
& VERBOSE_API
)
605 _mesa_debug(ctx
, "glCreateProgramPipelines(%d, %p)\n", n
, pipelines
);
607 create_program_pipelines(ctx
, n
, pipelines
, true);
611 * Determine if ID is the name of an pipeline object.
613 * \param id ID of the potential pipeline object.
614 * \return \c GL_TRUE if \c id is the name of a pipeline object,
615 * \c GL_FALSE otherwise.
618 _mesa_IsProgramPipeline(GLuint pipeline
)
620 GET_CURRENT_CONTEXT(ctx
);
622 if (MESA_VERBOSE
& VERBOSE_API
)
623 _mesa_debug(ctx
, "glIsProgramPipeline(%u)\n", pipeline
);
625 struct gl_pipeline_object
*obj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
629 return obj
->EverBound
;
633 * glGetProgramPipelineiv() - get pipeline shader state.
636 _mesa_GetProgramPipelineiv(GLuint pipeline
, GLenum pname
, GLint
*params
)
638 GET_CURRENT_CONTEXT(ctx
);
639 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
641 if (MESA_VERBOSE
& VERBOSE_API
)
642 _mesa_debug(ctx
, "glGetProgramPipelineiv(%u, %d, %p)\n",
643 pipeline
, pname
, params
);
645 /* Are geometry shaders available in this context?
647 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
648 const bool has_tess
= _mesa_has_tessellation(ctx
);
651 _mesa_error(ctx
, GL_INVALID_OPERATION
,
652 "glGetProgramPipelineiv(pipeline)");
656 /* Object is created by any Pipeline call but glGenProgramPipelines,
657 * glIsProgramPipeline and GetProgramPipelineInfoLog
659 pipe
->EverBound
= GL_TRUE
;
662 case GL_ACTIVE_PROGRAM
:
663 *params
= pipe
->ActiveProgram
? pipe
->ActiveProgram
->Name
: 0;
665 case GL_INFO_LOG_LENGTH
:
666 *params
= (pipe
->InfoLog
&& pipe
->InfoLog
[0] != '\0') ?
667 strlen(pipe
->InfoLog
) + 1 : 0;
669 case GL_VALIDATE_STATUS
:
670 *params
= pipe
->Validated
;
672 case GL_VERTEX_SHADER
:
673 *params
= pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
674 ? pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]->Id
: 0;
676 case GL_TESS_EVALUATION_SHADER
:
679 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]
680 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]->Id
: 0;
682 case GL_TESS_CONTROL_SHADER
:
685 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]
686 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]->Id
: 0;
688 case GL_GEOMETRY_SHADER
:
691 *params
= pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]
692 ? pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Id
: 0;
694 case GL_FRAGMENT_SHADER
:
695 *params
= pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]
696 ? pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]->Id
: 0;
698 case GL_COMPUTE_SHADER
:
699 if (!_mesa_has_compute_shaders(ctx
))
701 *params
= pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]
702 ? pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]->Id
: 0;
708 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramPipelineiv(pname=%s)",
709 _mesa_enum_to_string(pname
));
713 * Determines whether every stage in a linked program is active in the
714 * specified pipeline.
717 program_stages_all_active(struct gl_pipeline_object
*pipe
,
718 const struct gl_program
*prog
)
725 unsigned mask
= prog
->sh
.data
->linked_stages
;
727 const int i
= u_bit_scan(&mask
);
728 if (pipe
->CurrentProgram
[i
]) {
729 if (prog
->Id
!= pipe
->CurrentProgram
[i
]->Id
) {
738 pipe
->InfoLog
= ralloc_asprintf(pipe
,
739 "Program %d is not active for all "
740 "shaders that was linked",
748 program_stages_interleaved_illegally(const struct gl_pipeline_object
*pipe
)
750 unsigned prev_linked_stages
= 0;
752 /* Look for programs bound to stages: A -> B -> A, with any intervening
753 * sequence of unrelated programs or empty stages.
755 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
756 struct gl_program
*cur
= pipe
->CurrentProgram
[i
];
758 /* Empty stages anywhere in the pipe are OK. Also we can be confident
759 * that if the linked_stages mask matches we are looking at the same
760 * linked program because a previous validation call to
761 * program_stages_all_active() will have already failed if two different
762 * programs with the sames stages linked are not active for all linked
765 if (!cur
|| cur
->sh
.data
->linked_stages
== prev_linked_stages
)
768 if (prev_linked_stages
) {
769 /* We've seen an A -> B transition; look at the rest of the pipe
770 * to see if we ever see A again.
772 if (prev_linked_stages
>> (i
+ 1))
776 prev_linked_stages
= cur
->sh
.data
->linked_stages
;
783 _mesa_validate_program_pipeline(struct gl_context
* ctx
,
784 struct gl_pipeline_object
*pipe
)
787 bool program_empty
= true;
789 pipe
->Validated
= GL_FALSE
;
791 /* Release and reset the info log.
793 if (pipe
->InfoLog
!= NULL
)
794 ralloc_free(pipe
->InfoLog
);
796 pipe
->InfoLog
= NULL
;
798 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
799 * OpenGL 4.1 spec says:
801 * "[INVALID_OPERATION] is generated by any command that transfers
802 * vertices to the GL if:
804 * - A program object is active for at least one, but not all of
805 * the shader stages that were present when the program was
808 * For each possible program stage, verify that the program bound to that
809 * stage has all of its stages active. In other words, if the program
810 * bound to the vertex stage also has a fragment shader, the fragment
811 * shader must also be bound to the fragment stage.
813 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
814 if (!program_stages_all_active(pipe
, pipe
->CurrentProgram
[i
])) {
819 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
820 * OpenGL 4.1 spec says:
822 * "[INVALID_OPERATION] is generated by any command that transfers
823 * vertices to the GL if:
827 * - One program object is active for at least two shader stages
828 * and a second program is active for a shader stage between two
829 * stages for which the first program was active."
831 if (program_stages_interleaved_illegally(pipe
)) {
834 "Program is active for multiple shader stages with an "
835 "intervening stage provided by another program");
839 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
840 * OpenGL 4.1 spec says:
842 * "[INVALID_OPERATION] is generated by any command that transfers
843 * vertices to the GL if:
847 * - There is an active program for tessellation control,
848 * tessellation evaluation, or geometry stages with corresponding
849 * executable shader, but there is no active program with
850 * executable vertex shader."
852 if (!pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
853 && (pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
] ||
854 pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
] ||
855 pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
])) {
856 pipe
->InfoLog
= ralloc_strdup(pipe
, "Program lacks a vertex shader");
860 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
861 * OpenGL 4.1 spec says:
863 * "[INVALID_OPERATION] is generated by any command that transfers
864 * vertices to the GL if:
868 * - There is no current program object specified by UseProgram,
869 * there is a current program pipeline object, and the current
870 * program for any shader stage has been relinked since being
871 * applied to the pipeline object via UseProgramStages with the
872 * PROGRAM_SEPARABLE parameter set to FALSE.
874 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
875 if (pipe
->CurrentProgram
[i
] &&
876 !pipe
->CurrentProgram
[i
]->info
.separate_shader
) {
877 pipe
->InfoLog
= ralloc_asprintf(pipe
,
878 "Program %d was relinked without "
879 "PROGRAM_SEPARABLE state",
880 pipe
->CurrentProgram
[i
]->Id
);
885 /* Section 11.1.3.11 (Validation) of the OpenGL 4.5 spec says:
887 * "An INVALID_OPERATION error is generated by any command that trans-
888 * fers vertices to the GL or launches compute work if the current set
889 * of active program objects cannot be executed, for reasons including:
893 * - There is no current program object specified by UseProgram,
894 * there is a current program pipeline object, and that object is
895 * empty (no executable code is installed for any stage).
897 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
898 if (pipe
->CurrentProgram
[i
]) {
899 program_empty
= false;
908 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
909 * OpenGL 4.1 spec says:
911 * "[INVALID_OPERATION] is generated by any command that transfers
912 * vertices to the GL if:
916 * - Any two active samplers in the current program object are of
917 * different types, but refer to the same texture image unit.
919 * - The number of active samplers in the program exceeds the
920 * maximum number of texture image units allowed."
922 if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe
))
925 /* Validate inputs against outputs, this cannot be done during linking
926 * since programs have been linked separately from each other.
928 * Section 11.1.3.11 (Validation) of the OpenGL 4.5 Core Profile spec says:
930 * "Separable program objects may have validation failures that cannot be
931 * detected without the complete program pipeline. Mismatched interfaces,
932 * improper usage of program objects together, and the same
933 * state-dependent failures can result in validation errors for such
936 * OpenGL ES 3.1 specification has the same text.
938 * Section 11.1.3.11 (Validation) of the OpenGL ES spec also says:
940 * An INVALID_OPERATION error is generated by any command that transfers
941 * vertices to the GL or launches compute work if the current set of
942 * active program objects cannot be executed, for reasons including:
944 * * The current program pipeline object contains a shader interface
945 * that doesn't have an exact match (see section 7.4.1)
947 * Based on this, only perform the most-strict checking on ES or when the
948 * application has created a debug context.
950 if ((_mesa_is_gles(ctx
) || (ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
)) &&
951 !_mesa_validate_pipeline_io(pipe
)) {
952 if (_mesa_is_gles(ctx
))
955 static GLuint msg_id
= 0;
957 _mesa_gl_debug(ctx
, &msg_id
,
958 MESA_DEBUG_SOURCE_API
,
959 MESA_DEBUG_TYPE_PORTABILITY
,
960 MESA_DEBUG_SEVERITY_MEDIUM
,
961 "glValidateProgramPipeline: pipeline %u does not meet "
962 "strict OpenGL ES 3.1 requirements and may not be "
963 "portable across desktop hardware\n",
967 pipe
->Validated
= GL_TRUE
;
972 * Check compatibility of pipeline's program
975 _mesa_ValidateProgramPipeline(GLuint pipeline
)
977 GET_CURRENT_CONTEXT(ctx
);
979 if (MESA_VERBOSE
& VERBOSE_API
)
980 _mesa_debug(ctx
, "glValidateProgramPipeline(%u)\n", pipeline
);
982 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
985 _mesa_error(ctx
, GL_INVALID_OPERATION
,
986 "glValidateProgramPipeline(pipeline)");
990 _mesa_validate_program_pipeline(ctx
, pipe
);
994 _mesa_GetProgramPipelineInfoLog(GLuint pipeline
, GLsizei bufSize
,
995 GLsizei
*length
, GLchar
*infoLog
)
997 GET_CURRENT_CONTEXT(ctx
);
999 if (MESA_VERBOSE
& VERBOSE_API
)
1000 _mesa_debug(ctx
, "glGetProgramPipelineInfoLog(%u, %d, %p, %p)\n",
1001 pipeline
, bufSize
, length
, infoLog
);
1003 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
1006 _mesa_error(ctx
, GL_INVALID_VALUE
,
1007 "glGetProgramPipelineInfoLog(pipeline)");
1012 _mesa_error(ctx
, GL_INVALID_VALUE
,
1013 "glGetProgramPipelineInfoLog(bufSize)");
1017 _mesa_copy_string(infoLog
, bufSize
, length
, pipe
->InfoLog
);