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/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/state.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"
51 #include "util/bitscan.h"
54 * Delete a pipeline object.
57 _mesa_delete_pipeline_object(struct gl_context
*ctx
,
58 struct gl_pipeline_object
*obj
)
62 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
63 _mesa_reference_program(ctx
, &obj
->CurrentProgram
[i
], NULL
);
64 _mesa_reference_shader_program(ctx
, &obj
->ReferencedPrograms
[i
], NULL
);
67 _mesa_reference_shader_program(ctx
, &obj
->ActiveProgram
, NULL
);
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
);
82 obj
->Flags
= _mesa_get_shader_flags();
90 * Initialize pipeline object state for given context.
93 _mesa_init_pipeline(struct gl_context
*ctx
)
95 ctx
->Pipeline
.Objects
= _mesa_NewHashTable();
97 ctx
->Pipeline
.Current
= NULL
;
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
);
106 * Callback for deleting a pipeline object. Called by _mesa_HashDeleteAll().
109 delete_pipelineobj_cb(UNUSED GLuint id
, void *data
, void *userData
)
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
);
118 * Free pipeline state for given context.
121 _mesa_free_pipeline_data(struct gl_context
*ctx
)
123 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
125 _mesa_HashDeleteAll(ctx
->Pipeline
.Objects
, delete_pipelineobj_cb
, ctx
);
126 _mesa_DeleteHashTable(ctx
->Pipeline
.Objects
);
128 _mesa_delete_pipeline_object(ctx
, ctx
->Pipeline
.Default
);
132 * Look up the pipeline object for the given ID.
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
139 struct gl_pipeline_object
*
140 _mesa_lookup_pipeline_object(struct gl_context
*ctx
, GLuint id
)
145 return (struct gl_pipeline_object
*)
146 _mesa_HashLookupLocked(ctx
->Pipeline
.Objects
, id
);
150 * Add the given pipeline object to the pipeline object pool.
153 save_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
156 _mesa_HashInsertLocked(ctx
->Pipeline
.Objects
, obj
->Name
, obj
);
161 * Remove the given pipeline object from the pipeline object pool.
162 * Do not deallocate the pipeline object though.
165 remove_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
168 _mesa_HashRemoveLocked(ctx
->Pipeline
.Objects
, obj
->Name
);
173 * Set ptr to obj w/ reference counting.
174 * Note: this should only be called from the _mesa_reference_pipeline_object()
178 _mesa_reference_pipeline_object_(struct gl_context
*ctx
,
179 struct gl_pipeline_object
**ptr
,
180 struct gl_pipeline_object
*obj
)
185 /* Unreference the old pipeline object */
186 struct gl_pipeline_object
*oldObj
= *ptr
;
188 assert(oldObj
->RefCount
> 0);
191 if (oldObj
->RefCount
== 0) {
192 _mesa_delete_pipeline_object(ctx
, oldObj
);
200 /* reference new pipeline object */
201 assert(obj
->RefCount
> 0);
209 use_program_stage(struct gl_context
*ctx
, GLenum type
,
210 struct gl_shader_program
*shProg
,
211 struct gl_pipeline_object
*pipe
) {
212 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
213 struct gl_program
*prog
= NULL
;
214 if (shProg
&& shProg
->_LinkedShaders
[stage
])
215 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
217 _mesa_use_program(ctx
, stage
, shProg
, prog
, pipe
);
221 use_program_stages(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
222 GLbitfield stages
, struct gl_pipeline_object
*pipe
) {
224 /* Enable individual stages from the program as requested by the
225 * application. If there is no shader for a requested stage in the
226 * program, _mesa_use_shader_program will enable fixed-function processing
227 * as dictated by the spec.
229 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
232 * "If UseProgramStages is called with program set to zero or with a
233 * program object that contains no executable code for the given
234 * stages, it is as if the pipeline object has no programmable stage
235 * configured for the indicated shader stages."
237 if ((stages
& GL_VERTEX_SHADER_BIT
) != 0)
238 use_program_stage(ctx
, GL_VERTEX_SHADER
, shProg
, pipe
);
240 if ((stages
& GL_FRAGMENT_SHADER_BIT
) != 0)
241 use_program_stage(ctx
, GL_FRAGMENT_SHADER
, shProg
, pipe
);
243 if ((stages
& GL_GEOMETRY_SHADER_BIT
) != 0)
244 use_program_stage(ctx
, GL_GEOMETRY_SHADER
, shProg
, pipe
);
246 if ((stages
& GL_TESS_CONTROL_SHADER_BIT
) != 0)
247 use_program_stage(ctx
, GL_TESS_CONTROL_SHADER
, shProg
, pipe
);
249 if ((stages
& GL_TESS_EVALUATION_SHADER_BIT
) != 0)
250 use_program_stage(ctx
, GL_TESS_EVALUATION_SHADER
, shProg
, pipe
);
252 if ((stages
& GL_COMPUTE_SHADER_BIT
) != 0)
253 use_program_stage(ctx
, GL_COMPUTE_SHADER
, shProg
, pipe
);
255 pipe
->Validated
= false;
259 _mesa_UseProgramStages_no_error(GLuint pipeline
, GLbitfield stages
,
262 GET_CURRENT_CONTEXT(ctx
);
264 struct gl_pipeline_object
*pipe
=
265 _mesa_lookup_pipeline_object(ctx
, pipeline
);
266 struct gl_shader_program
*shProg
= NULL
;
269 shProg
= _mesa_lookup_shader_program(ctx
, prog
);
271 /* Object is created by any Pipeline call but glGenProgramPipelines,
272 * glIsProgramPipeline and GetProgramPipelineInfoLog
274 pipe
->EverBound
= GL_TRUE
;
276 use_program_stages(ctx
, shProg
, stages
, pipe
);
280 * Bound program to severals stages of the pipeline
283 _mesa_UseProgramStages(GLuint pipeline
, GLbitfield stages
, GLuint program
)
285 GET_CURRENT_CONTEXT(ctx
);
287 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
288 struct gl_shader_program
*shProg
= NULL
;
289 GLbitfield any_valid_stages
;
291 if (MESA_VERBOSE
& VERBOSE_API
)
292 _mesa_debug(ctx
, "glUseProgramStages(%u, 0x%x, %u)\n",
293 pipeline
, stages
, program
);
296 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUseProgramStages(pipeline)");
300 /* Object is created by any Pipeline call but glGenProgramPipelines,
301 * glIsProgramPipeline and GetProgramPipelineInfoLog
303 pipe
->EverBound
= GL_TRUE
;
305 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
307 * "If stages is not the special value ALL_SHADER_BITS, and has a bit
308 * set that is not recognized, the error INVALID_VALUE is generated."
310 any_valid_stages
= GL_VERTEX_SHADER_BIT
| GL_FRAGMENT_SHADER_BIT
;
311 if (_mesa_has_geometry_shaders(ctx
))
312 any_valid_stages
|= GL_GEOMETRY_SHADER_BIT
;
313 if (_mesa_has_tessellation(ctx
))
314 any_valid_stages
|= GL_TESS_CONTROL_SHADER_BIT
|
315 GL_TESS_EVALUATION_SHADER_BIT
;
316 if (_mesa_has_compute_shaders(ctx
))
317 any_valid_stages
|= GL_COMPUTE_SHADER_BIT
;
319 if (stages
!= GL_ALL_SHADER_BITS
&& (stages
& ~any_valid_stages
) != 0) {
320 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUseProgramStages(Stages)");
324 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
327 * "The error INVALID_OPERATION is generated:
331 * - by UseProgramStages if the program pipeline object it refers
332 * to is current and the current transform feedback object is
333 * active and not paused;
335 if (ctx
->_Shader
== pipe
) {
336 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
337 _mesa_error(ctx
, GL_INVALID_OPERATION
,
338 "glUseProgramStages(transform feedback active)");
344 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
345 "glUseProgramStages");
349 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
352 * "If the program object named by program was linked without the
353 * PROGRAM_SEPARABLE parameter set, or was not linked successfully,
354 * the error INVALID_OPERATION is generated and the corresponding
355 * shader stages in the pipeline program pipeline object are not
358 if (!shProg
->data
->LinkStatus
) {
359 _mesa_error(ctx
, GL_INVALID_OPERATION
,
360 "glUseProgramStages(program not linked)");
364 if (!shProg
->SeparateShader
) {
365 _mesa_error(ctx
, GL_INVALID_OPERATION
,
366 "glUseProgramStages(program wasn't linked with the "
367 "PROGRAM_SEPARABLE flag)");
372 use_program_stages(ctx
, shProg
, stages
, pipe
);
375 static ALWAYS_INLINE
void
376 active_shader_program(struct gl_context
*ctx
, GLuint pipeline
, GLuint program
,
379 struct gl_shader_program
*shProg
= NULL
;
380 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
384 shProg
= _mesa_lookup_shader_program(ctx
, program
);
386 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
387 "glActiveShaderProgram(program)");
393 if (!no_error
&& !pipe
) {
394 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveShaderProgram(pipeline)");
398 /* Object is created by any Pipeline call but glGenProgramPipelines,
399 * glIsProgramPipeline and GetProgramPipelineInfoLog
401 pipe
->EverBound
= GL_TRUE
;
403 if (!no_error
&& shProg
!= NULL
&& !shProg
->data
->LinkStatus
) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "glActiveShaderProgram(program %u not linked)", shProg
->Name
);
409 _mesa_reference_shader_program(ctx
, &pipe
->ActiveProgram
, shProg
);
413 _mesa_ActiveShaderProgram_no_error(GLuint pipeline
, GLuint program
)
415 GET_CURRENT_CONTEXT(ctx
);
416 active_shader_program(ctx
, pipeline
, program
, true);
420 * Use the named shader program for subsequent glUniform calls (if pipeline
424 _mesa_ActiveShaderProgram(GLuint pipeline
, GLuint program
)
426 GET_CURRENT_CONTEXT(ctx
);
428 if (MESA_VERBOSE
& VERBOSE_API
)
429 _mesa_debug(ctx
, "glActiveShaderProgram(%u, %u)\n", pipeline
, program
);
431 active_shader_program(ctx
, pipeline
, program
, false);
434 static ALWAYS_INLINE
void
435 bind_program_pipeline(struct gl_context
*ctx
, GLuint pipeline
, bool no_error
)
437 struct gl_pipeline_object
*newObj
= NULL
;
439 if (MESA_VERBOSE
& VERBOSE_API
)
440 _mesa_debug(ctx
, "glBindProgramPipeline(%u)\n", pipeline
);
442 /* Rebinding the same pipeline object: no change.
444 if (ctx
->_Shader
->Name
== pipeline
)
447 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
450 * "The error INVALID_OPERATION is generated:
454 * - by BindProgramPipeline if the current transform feedback
455 * object is active and not paused;
457 if (!no_error
&& _mesa_is_xfb_active_and_unpaused(ctx
)) {
458 _mesa_error(ctx
, GL_INVALID_OPERATION
,
459 "glBindProgramPipeline(transform feedback active)");
463 /* Get pointer to new pipeline object (newObj)
466 /* non-default pipeline object */
467 newObj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
468 if (!no_error
&& !newObj
) {
469 _mesa_error(ctx
, GL_INVALID_OPERATION
,
470 "glBindProgramPipeline(non-gen name)");
474 /* Object is created by any Pipeline call but glGenProgramPipelines,
475 * glIsProgramPipeline and GetProgramPipelineInfoLog
477 newObj
->EverBound
= GL_TRUE
;
480 _mesa_bind_pipeline(ctx
, newObj
);
484 _mesa_BindProgramPipeline_no_error(GLuint pipeline
)
486 GET_CURRENT_CONTEXT(ctx
);
487 bind_program_pipeline(ctx
, pipeline
, true);
491 * Make program of the pipeline current
494 _mesa_BindProgramPipeline(GLuint pipeline
)
496 GET_CURRENT_CONTEXT(ctx
);
497 bind_program_pipeline(ctx
, pipeline
, false);
501 _mesa_bind_pipeline(struct gl_context
*ctx
,
502 struct gl_pipeline_object
*pipe
)
505 /* First bind the Pipeline to pipeline binding point */
506 _mesa_reference_pipeline_object(ctx
, &ctx
->Pipeline
.Current
, pipe
);
508 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
510 * "If there is a current program object established by UseProgram,
511 * that program is considered current for all stages. Otherwise, if
512 * there is a bound program pipeline object (see section 2.11.4), the
513 * program bound to the appropriate stage of the pipeline object is
514 * considered current."
516 if (&ctx
->Shader
!= ctx
->_Shader
) {
517 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
520 /* Bound the pipeline to the current program and
521 * restore the pipeline state
523 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, pipe
);
525 /* Unbind the pipeline */
526 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
527 ctx
->Pipeline
.Default
);
530 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
531 struct gl_program
*prog
= ctx
->_Shader
->CurrentProgram
[i
];
533 _mesa_program_init_subroutine_defaults(ctx
, prog
);
537 _mesa_update_vertex_processing_mode(ctx
);
538 _mesa_update_allow_draw_out_of_order(ctx
);
543 * Delete a set of pipeline objects.
545 * \param n Number of pipeline objects to delete.
546 * \param ids pipeline of \c n pipeline object IDs.
549 _mesa_DeleteProgramPipelines(GLsizei n
, const GLuint
*pipelines
)
551 GET_CURRENT_CONTEXT(ctx
);
554 if (MESA_VERBOSE
& VERBOSE_API
)
555 _mesa_debug(ctx
, "glDeleteProgramPipelines(%d, %p)\n", n
, pipelines
);
558 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteProgramPipelines(n<0)");
562 for (i
= 0; i
< n
; i
++) {
563 struct gl_pipeline_object
*obj
=
564 _mesa_lookup_pipeline_object(ctx
, pipelines
[i
]);
567 assert(obj
->Name
== pipelines
[i
]);
569 /* If the pipeline object is currently bound, the spec says "If an
570 * object that is currently bound is deleted, the binding for that
571 * object reverts to zero and no program pipeline object becomes
574 if (obj
== ctx
->Pipeline
.Current
) {
575 _mesa_BindProgramPipeline(0);
578 /* The ID is immediately freed for re-use */
579 remove_pipeline_object(ctx
, obj
);
581 /* Unreference the pipeline object.
582 * If refcount hits zero, the object will be deleted.
584 _mesa_reference_pipeline_object(ctx
, &obj
, NULL
);
590 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
591 * \param n Number of IDs to generate.
592 * \param pipelines pipeline of \c n locations to store the IDs.
595 create_program_pipelines(struct gl_context
*ctx
, GLsizei n
, GLuint
*pipelines
,
598 const char *func
= dsa
? "glCreateProgramPipelines" : "glGenProgramPipelines";
605 first
= _mesa_HashFindFreeKeyBlock(ctx
->Pipeline
.Objects
, n
);
607 for (i
= 0; i
< n
; i
++) {
608 struct gl_pipeline_object
*obj
;
609 GLuint name
= first
+ i
;
611 obj
= _mesa_new_pipeline_object(ctx
, name
);
613 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
618 /* make dsa-allocated objects behave like program objects */
619 obj
->EverBound
= GL_TRUE
;
622 save_pipeline_object(ctx
, obj
);
623 pipelines
[i
] = first
+ i
;
628 create_program_pipelines_err(struct gl_context
*ctx
, GLsizei n
,
629 GLuint
*pipelines
, bool dsa
)
631 const char *func
= dsa
? "glCreateProgramPipelines" : "glGenProgramPipelines";
634 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s (n < 0)", func
);
638 create_program_pipelines(ctx
, n
, pipelines
, dsa
);
642 _mesa_GenProgramPipelines_no_error(GLsizei n
, GLuint
*pipelines
)
644 GET_CURRENT_CONTEXT(ctx
);
645 create_program_pipelines(ctx
, n
, pipelines
, false);
649 _mesa_GenProgramPipelines(GLsizei n
, GLuint
*pipelines
)
651 GET_CURRENT_CONTEXT(ctx
);
653 if (MESA_VERBOSE
& VERBOSE_API
)
654 _mesa_debug(ctx
, "glGenProgramPipelines(%d, %p)\n", n
, pipelines
);
656 create_program_pipelines_err(ctx
, n
, pipelines
, false);
660 _mesa_CreateProgramPipelines_no_error(GLsizei n
, GLuint
*pipelines
)
662 GET_CURRENT_CONTEXT(ctx
);
663 create_program_pipelines(ctx
, n
, pipelines
, true);
667 _mesa_CreateProgramPipelines(GLsizei n
, GLuint
*pipelines
)
669 GET_CURRENT_CONTEXT(ctx
);
671 if (MESA_VERBOSE
& VERBOSE_API
)
672 _mesa_debug(ctx
, "glCreateProgramPipelines(%d, %p)\n", n
, pipelines
);
674 create_program_pipelines_err(ctx
, n
, pipelines
, true);
678 * Determine if ID is the name of an pipeline object.
680 * \param id ID of the potential pipeline object.
681 * \return \c GL_TRUE if \c id is the name of a pipeline object,
682 * \c GL_FALSE otherwise.
685 _mesa_IsProgramPipeline(GLuint pipeline
)
687 GET_CURRENT_CONTEXT(ctx
);
689 if (MESA_VERBOSE
& VERBOSE_API
)
690 _mesa_debug(ctx
, "glIsProgramPipeline(%u)\n", pipeline
);
692 struct gl_pipeline_object
*obj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
696 return obj
->EverBound
;
700 * glGetProgramPipelineiv() - get pipeline shader state.
703 _mesa_GetProgramPipelineiv(GLuint pipeline
, GLenum pname
, GLint
*params
)
705 GET_CURRENT_CONTEXT(ctx
);
706 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
708 if (MESA_VERBOSE
& VERBOSE_API
)
709 _mesa_debug(ctx
, "glGetProgramPipelineiv(%u, %d, %p)\n",
710 pipeline
, pname
, params
);
712 /* Are geometry shaders available in this context?
714 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
715 const bool has_tess
= _mesa_has_tessellation(ctx
);
718 _mesa_error(ctx
, GL_INVALID_OPERATION
,
719 "glGetProgramPipelineiv(pipeline)");
723 /* Object is created by any Pipeline call but glGenProgramPipelines,
724 * glIsProgramPipeline and GetProgramPipelineInfoLog
726 pipe
->EverBound
= GL_TRUE
;
729 case GL_ACTIVE_PROGRAM
:
730 *params
= pipe
->ActiveProgram
? pipe
->ActiveProgram
->Name
: 0;
732 case GL_INFO_LOG_LENGTH
:
733 *params
= (pipe
->InfoLog
&& pipe
->InfoLog
[0] != '\0') ?
734 strlen(pipe
->InfoLog
) + 1 : 0;
736 case GL_VALIDATE_STATUS
:
737 *params
= pipe
->Validated
;
739 case GL_VERTEX_SHADER
:
740 *params
= pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
741 ? pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]->Id
: 0;
743 case GL_TESS_EVALUATION_SHADER
:
746 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]
747 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]->Id
: 0;
749 case GL_TESS_CONTROL_SHADER
:
752 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]
753 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]->Id
: 0;
755 case GL_GEOMETRY_SHADER
:
758 *params
= pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]
759 ? pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Id
: 0;
761 case GL_FRAGMENT_SHADER
:
762 *params
= pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]
763 ? pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]->Id
: 0;
765 case GL_COMPUTE_SHADER
:
766 if (!_mesa_has_compute_shaders(ctx
))
768 *params
= pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]
769 ? pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]->Id
: 0;
775 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramPipelineiv(pname=%s)",
776 _mesa_enum_to_string(pname
));
780 * Determines whether every stage in a linked program is active in the
781 * specified pipeline.
784 program_stages_all_active(struct gl_pipeline_object
*pipe
,
785 const struct gl_program
*prog
)
792 unsigned mask
= prog
->sh
.data
->linked_stages
;
794 const int i
= u_bit_scan(&mask
);
795 if (pipe
->CurrentProgram
[i
]) {
796 if (prog
->Id
!= pipe
->CurrentProgram
[i
]->Id
) {
805 pipe
->InfoLog
= ralloc_asprintf(pipe
,
806 "Program %d is not active for all "
807 "shaders that was linked",
815 program_stages_interleaved_illegally(const struct gl_pipeline_object
*pipe
)
817 unsigned prev_linked_stages
= 0;
819 /* Look for programs bound to stages: A -> B -> A, with any intervening
820 * sequence of unrelated programs or empty stages.
822 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
823 struct gl_program
*cur
= pipe
->CurrentProgram
[i
];
825 /* Empty stages anywhere in the pipe are OK. Also we can be confident
826 * that if the linked_stages mask matches we are looking at the same
827 * linked program because a previous validation call to
828 * program_stages_all_active() will have already failed if two different
829 * programs with the sames stages linked are not active for all linked
832 if (!cur
|| cur
->sh
.data
->linked_stages
== prev_linked_stages
)
835 if (prev_linked_stages
) {
836 /* We've seen an A -> B transition; look at the rest of the pipe
837 * to see if we ever see A again.
839 if (prev_linked_stages
>> (i
+ 1))
843 prev_linked_stages
= cur
->sh
.data
->linked_stages
;
850 _mesa_validate_program_pipeline(struct gl_context
* ctx
,
851 struct gl_pipeline_object
*pipe
)
854 bool program_empty
= true;
856 pipe
->Validated
= GL_FALSE
;
858 /* Release and reset the info log.
860 if (pipe
->InfoLog
!= NULL
)
861 ralloc_free(pipe
->InfoLog
);
863 pipe
->InfoLog
= NULL
;
865 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
866 * OpenGL 4.1 spec says:
868 * "[INVALID_OPERATION] is generated by any command that transfers
869 * vertices to the GL if:
871 * - A program object is active for at least one, but not all of
872 * the shader stages that were present when the program was
875 * For each possible program stage, verify that the program bound to that
876 * stage has all of its stages active. In other words, if the program
877 * bound to the vertex stage also has a fragment shader, the fragment
878 * shader must also be bound to the fragment stage.
880 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
881 if (!program_stages_all_active(pipe
, pipe
->CurrentProgram
[i
])) {
886 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
887 * OpenGL 4.1 spec says:
889 * "[INVALID_OPERATION] is generated by any command that transfers
890 * vertices to the GL if:
894 * - One program object is active for at least two shader stages
895 * and a second program is active for a shader stage between two
896 * stages for which the first program was active."
898 if (program_stages_interleaved_illegally(pipe
)) {
901 "Program is active for multiple shader stages with an "
902 "intervening stage provided by another program");
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 * - There is an active program for tessellation control,
915 * tessellation evaluation, or geometry stages with corresponding
916 * executable shader, but there is no active program with
917 * executable vertex shader."
919 if (!pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
920 && (pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
] ||
921 pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
] ||
922 pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
])) {
923 pipe
->InfoLog
= ralloc_strdup(pipe
, "Program lacks a vertex shader");
927 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
928 * OpenGL 4.1 spec says:
930 * "[INVALID_OPERATION] is generated by any command that transfers
931 * vertices to the GL if:
935 * - There is no current program object specified by UseProgram,
936 * there is a current program pipeline object, and the current
937 * program for any shader stage has been relinked since being
938 * applied to the pipeline object via UseProgramStages with the
939 * PROGRAM_SEPARABLE parameter set to FALSE.
941 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
942 if (pipe
->CurrentProgram
[i
] &&
943 !pipe
->CurrentProgram
[i
]->info
.separate_shader
) {
944 pipe
->InfoLog
= ralloc_asprintf(pipe
,
945 "Program %d was relinked without "
946 "PROGRAM_SEPARABLE state",
947 pipe
->CurrentProgram
[i
]->Id
);
952 /* Section 11.1.3.11 (Validation) of the OpenGL 4.5 spec says:
954 * "An INVALID_OPERATION error is generated by any command that trans-
955 * fers vertices to the GL or launches compute work if the current set
956 * of active program objects cannot be executed, for reasons including:
960 * - There is no current program object specified by UseProgram,
961 * there is a current program pipeline object, and that object is
962 * empty (no executable code is installed for any stage).
964 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
965 if (pipe
->CurrentProgram
[i
]) {
966 program_empty
= false;
975 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
976 * OpenGL 4.1 spec says:
978 * "[INVALID_OPERATION] is generated by any command that transfers
979 * vertices to the GL if:
983 * - Any two active samplers in the current program object are of
984 * different types, but refer to the same texture image unit.
986 * - The number of active samplers in the program exceeds the
987 * maximum number of texture image units allowed."
989 if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe
))
992 /* Validate inputs against outputs, this cannot be done during linking
993 * since programs have been linked separately from each other.
995 * Section 11.1.3.11 (Validation) of the OpenGL 4.5 Core Profile spec says:
997 * "Separable program objects may have validation failures that cannot be
998 * detected without the complete program pipeline. Mismatched interfaces,
999 * improper usage of program objects together, and the same
1000 * state-dependent failures can result in validation errors for such
1003 * OpenGL ES 3.1 specification has the same text.
1005 * Section 11.1.3.11 (Validation) of the OpenGL ES spec also says:
1007 * An INVALID_OPERATION error is generated by any command that transfers
1008 * vertices to the GL or launches compute work if the current set of
1009 * active program objects cannot be executed, for reasons including:
1011 * * The current program pipeline object contains a shader interface
1012 * that doesn't have an exact match (see section 7.4.1)
1014 * Based on this, only perform the most-strict checking on ES or when the
1015 * application has created a debug context.
1017 if ((_mesa_is_gles(ctx
) || (ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
)) &&
1018 !_mesa_validate_pipeline_io(pipe
)) {
1019 if (_mesa_is_gles(ctx
))
1022 static GLuint msg_id
= 0;
1024 _mesa_gl_debugf(ctx
, &msg_id
,
1025 MESA_DEBUG_SOURCE_API
,
1026 MESA_DEBUG_TYPE_PORTABILITY
,
1027 MESA_DEBUG_SEVERITY_MEDIUM
,
1028 "glValidateProgramPipeline: pipeline %u does not meet "
1029 "strict OpenGL ES 3.1 requirements and may not be "
1030 "portable across desktop hardware\n",
1034 pipe
->Validated
= GL_TRUE
;
1039 * Check compatibility of pipeline's program
1042 _mesa_ValidateProgramPipeline(GLuint pipeline
)
1044 GET_CURRENT_CONTEXT(ctx
);
1046 if (MESA_VERBOSE
& VERBOSE_API
)
1047 _mesa_debug(ctx
, "glValidateProgramPipeline(%u)\n", pipeline
);
1049 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
1052 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1053 "glValidateProgramPipeline(pipeline)");
1057 _mesa_validate_program_pipeline(ctx
, pipe
);
1061 _mesa_GetProgramPipelineInfoLog(GLuint pipeline
, GLsizei bufSize
,
1062 GLsizei
*length
, GLchar
*infoLog
)
1064 GET_CURRENT_CONTEXT(ctx
);
1066 if (MESA_VERBOSE
& VERBOSE_API
)
1067 _mesa_debug(ctx
, "glGetProgramPipelineInfoLog(%u, %d, %p, %p)\n",
1068 pipeline
, bufSize
, length
, infoLog
);
1070 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
1073 _mesa_error(ctx
, GL_INVALID_VALUE
,
1074 "glGetProgramPipelineInfoLog(pipeline)");
1079 _mesa_error(ctx
, GL_INVALID_VALUE
,
1080 "glGetProgramPipelineInfoLog(bufSize)");
1084 _mesa_copy_string(infoLog
, bufSize
, length
, pipe
->InfoLog
);