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 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
62 _mesa_reference_program(ctx
, &obj
->CurrentProgram
[i
], NULL
);
63 _mesa_reference_shader_program(ctx
, &obj
->ReferencedPrograms
[i
], NULL
);
66 _mesa_reference_shader_program(ctx
, &obj
->ActiveProgram
, NULL
);
72 * Allocate and initialize a new pipeline object.
74 static struct gl_pipeline_object
*
75 _mesa_new_pipeline_object(struct gl_context
*ctx
, GLuint name
)
77 struct gl_pipeline_object
*obj
= rzalloc(NULL
, struct gl_pipeline_object
);
81 obj
->Flags
= _mesa_get_shader_flags();
89 * Initialize pipeline object state for given context.
92 _mesa_init_pipeline(struct gl_context
*ctx
)
94 ctx
->Pipeline
.Objects
= _mesa_NewHashTable();
96 ctx
->Pipeline
.Current
= NULL
;
98 /* Install a default Pipeline */
99 ctx
->Pipeline
.Default
= _mesa_new_pipeline_object(ctx
, 0);
100 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, ctx
->Pipeline
.Default
);
105 * Callback for deleting a pipeline object. Called by _mesa_HashDeleteAll().
108 delete_pipelineobj_cb(UNUSED GLuint id
, void *data
, void *userData
)
110 struct gl_pipeline_object
*obj
= (struct gl_pipeline_object
*) data
;
111 struct gl_context
*ctx
= (struct gl_context
*) userData
;
112 _mesa_delete_pipeline_object(ctx
, obj
);
117 * Free pipeline state for given context.
120 _mesa_free_pipeline_data(struct gl_context
*ctx
)
122 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, NULL
);
124 _mesa_HashDeleteAll(ctx
->Pipeline
.Objects
, delete_pipelineobj_cb
, ctx
);
125 _mesa_DeleteHashTable(ctx
->Pipeline
.Objects
);
127 _mesa_delete_pipeline_object(ctx
, ctx
->Pipeline
.Default
);
131 * Look up the pipeline object for the given ID.
134 * Either a pointer to the pipeline object with the specified ID or \c NULL for
135 * a non-existent ID. The spec defines ID 0 as being technically
138 struct gl_pipeline_object
*
139 _mesa_lookup_pipeline_object(struct gl_context
*ctx
, GLuint id
)
144 return (struct gl_pipeline_object
*)
145 _mesa_HashLookupLocked(ctx
->Pipeline
.Objects
, id
);
149 * Add the given pipeline object to the pipeline object pool.
152 save_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
155 _mesa_HashInsertLocked(ctx
->Pipeline
.Objects
, obj
->Name
, obj
);
160 * Remove the given pipeline object from the pipeline object pool.
161 * Do not deallocate the pipeline object though.
164 remove_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
167 _mesa_HashRemoveLocked(ctx
->Pipeline
.Objects
, obj
->Name
);
172 * Set ptr to obj w/ reference counting.
173 * Note: this should only be called from the _mesa_reference_pipeline_object()
177 _mesa_reference_pipeline_object_(struct gl_context
*ctx
,
178 struct gl_pipeline_object
**ptr
,
179 struct gl_pipeline_object
*obj
)
184 /* Unreference the old pipeline object */
185 struct gl_pipeline_object
*oldObj
= *ptr
;
187 assert(oldObj
->RefCount
> 0);
190 if (oldObj
->RefCount
== 0) {
191 _mesa_delete_pipeline_object(ctx
, oldObj
);
199 /* reference new pipeline object */
200 assert(obj
->RefCount
> 0);
208 use_program_stage(struct gl_context
*ctx
, GLenum type
,
209 struct gl_shader_program
*shProg
,
210 struct gl_pipeline_object
*pipe
) {
211 gl_shader_stage stage
= _mesa_shader_enum_to_shader_stage(type
);
212 struct gl_program
*prog
= NULL
;
213 if (shProg
&& shProg
->_LinkedShaders
[stage
])
214 prog
= shProg
->_LinkedShaders
[stage
]->Program
;
216 _mesa_use_program(ctx
, stage
, shProg
, prog
, pipe
);
220 use_program_stages(struct gl_context
*ctx
, struct gl_shader_program
*shProg
,
221 GLbitfield stages
, struct gl_pipeline_object
*pipe
) {
223 /* Enable individual stages from the program as requested by the
224 * application. If there is no shader for a requested stage in the
225 * program, _mesa_use_shader_program will enable fixed-function processing
226 * as dictated by the spec.
228 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
231 * "If UseProgramStages is called with program set to zero or with a
232 * program object that contains no executable code for the given
233 * stages, it is as if the pipeline object has no programmable stage
234 * configured for the indicated shader stages."
236 if ((stages
& GL_VERTEX_SHADER_BIT
) != 0)
237 use_program_stage(ctx
, GL_VERTEX_SHADER
, shProg
, pipe
);
239 if ((stages
& GL_FRAGMENT_SHADER_BIT
) != 0)
240 use_program_stage(ctx
, GL_FRAGMENT_SHADER
, shProg
, pipe
);
242 if ((stages
& GL_GEOMETRY_SHADER_BIT
) != 0)
243 use_program_stage(ctx
, GL_GEOMETRY_SHADER
, shProg
, pipe
);
245 if ((stages
& GL_TESS_CONTROL_SHADER_BIT
) != 0)
246 use_program_stage(ctx
, GL_TESS_CONTROL_SHADER
, shProg
, pipe
);
248 if ((stages
& GL_TESS_EVALUATION_SHADER_BIT
) != 0)
249 use_program_stage(ctx
, GL_TESS_EVALUATION_SHADER
, shProg
, pipe
);
251 if ((stages
& GL_COMPUTE_SHADER_BIT
) != 0)
252 use_program_stage(ctx
, GL_COMPUTE_SHADER
, shProg
, pipe
);
254 pipe
->Validated
= false;
258 _mesa_UseProgramStages_no_error(GLuint pipeline
, GLbitfield stages
,
261 GET_CURRENT_CONTEXT(ctx
);
263 struct gl_pipeline_object
*pipe
=
264 _mesa_lookup_pipeline_object(ctx
, pipeline
);
265 struct gl_shader_program
*shProg
= NULL
;
268 shProg
= _mesa_lookup_shader_program(ctx
, prog
);
270 /* Object is created by any Pipeline call but glGenProgramPipelines,
271 * glIsProgramPipeline and GetProgramPipelineInfoLog
273 pipe
->EverBound
= GL_TRUE
;
275 use_program_stages(ctx
, shProg
, stages
, pipe
);
279 * Bound program to severals stages of the pipeline
282 _mesa_UseProgramStages(GLuint pipeline
, GLbitfield stages
, GLuint program
)
284 GET_CURRENT_CONTEXT(ctx
);
286 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
287 struct gl_shader_program
*shProg
= NULL
;
288 GLbitfield any_valid_stages
;
290 if (MESA_VERBOSE
& VERBOSE_API
)
291 _mesa_debug(ctx
, "glUseProgramStages(%u, 0x%x, %u)\n",
292 pipeline
, stages
, program
);
295 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUseProgramStages(pipeline)");
299 /* Object is created by any Pipeline call but glGenProgramPipelines,
300 * glIsProgramPipeline and GetProgramPipelineInfoLog
302 pipe
->EverBound
= GL_TRUE
;
304 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
306 * "If stages is not the special value ALL_SHADER_BITS, and has a bit
307 * set that is not recognized, the error INVALID_VALUE is generated."
309 any_valid_stages
= GL_VERTEX_SHADER_BIT
| GL_FRAGMENT_SHADER_BIT
;
310 if (_mesa_has_geometry_shaders(ctx
))
311 any_valid_stages
|= GL_GEOMETRY_SHADER_BIT
;
312 if (_mesa_has_tessellation(ctx
))
313 any_valid_stages
|= GL_TESS_CONTROL_SHADER_BIT
|
314 GL_TESS_EVALUATION_SHADER_BIT
;
315 if (_mesa_has_compute_shaders(ctx
))
316 any_valid_stages
|= GL_COMPUTE_SHADER_BIT
;
318 if (stages
!= GL_ALL_SHADER_BITS
&& (stages
& ~any_valid_stages
) != 0) {
319 _mesa_error(ctx
, GL_INVALID_VALUE
, "glUseProgramStages(Stages)");
323 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
326 * "The error INVALID_OPERATION is generated:
330 * - by UseProgramStages if the program pipeline object it refers
331 * to is current and the current transform feedback object is
332 * active and not paused;
334 if (ctx
->_Shader
== pipe
) {
335 if (_mesa_is_xfb_active_and_unpaused(ctx
)) {
336 _mesa_error(ctx
, GL_INVALID_OPERATION
,
337 "glUseProgramStages(transform feedback active)");
343 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
344 "glUseProgramStages");
348 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
351 * "If the program object named by program was linked without the
352 * PROGRAM_SEPARABLE parameter set, or was not linked successfully,
353 * the error INVALID_OPERATION is generated and the corresponding
354 * shader stages in the pipeline program pipeline object are not
357 if (!shProg
->data
->LinkStatus
) {
358 _mesa_error(ctx
, GL_INVALID_OPERATION
,
359 "glUseProgramStages(program not linked)");
363 if (!shProg
->SeparateShader
) {
364 _mesa_error(ctx
, GL_INVALID_OPERATION
,
365 "glUseProgramStages(program wasn't linked with the "
366 "PROGRAM_SEPARABLE flag)");
371 use_program_stages(ctx
, shProg
, stages
, pipe
);
375 _mesa_ActiveShaderProgram_no_error(GLuint pipeline
, GLuint program
)
377 GET_CURRENT_CONTEXT(ctx
);
378 struct gl_shader_program
*shProg
= NULL
;
379 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
382 shProg
= _mesa_lookup_shader_program(ctx
, program
);
384 /* Object is created by any Pipeline call but glGenProgramPipelines,
385 * glIsProgramPipeline and GetProgramPipelineInfoLog
387 pipe
->EverBound
= GL_TRUE
;
389 _mesa_reference_shader_program(ctx
, &pipe
->ActiveProgram
, shProg
);
393 * Use the named shader program for subsequent glUniform calls (if pipeline
397 _mesa_ActiveShaderProgram(GLuint pipeline
, GLuint program
)
399 GET_CURRENT_CONTEXT(ctx
);
400 struct gl_shader_program
*shProg
= NULL
;
401 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
403 if (MESA_VERBOSE
& VERBOSE_API
)
404 _mesa_debug(ctx
, "glActiveShaderProgram(%u, %u)\n", pipeline
, program
);
407 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
408 "glActiveShaderProgram(program)");
414 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveShaderProgram(pipeline)");
418 /* Object is created by any Pipeline call but glGenProgramPipelines,
419 * glIsProgramPipeline and GetProgramPipelineInfoLog
421 pipe
->EverBound
= GL_TRUE
;
423 if ((shProg
!= NULL
) && !shProg
->data
->LinkStatus
) {
424 _mesa_error(ctx
, GL_INVALID_OPERATION
,
425 "glActiveShaderProgram(program %u not linked)", shProg
->Name
);
429 _mesa_reference_shader_program(ctx
, &pipe
->ActiveProgram
, shProg
);
432 static ALWAYS_INLINE
void
433 bind_program_pipeline(struct gl_context
*ctx
, GLuint pipeline
, bool no_error
)
435 struct gl_pipeline_object
*newObj
= NULL
;
437 if (MESA_VERBOSE
& VERBOSE_API
)
438 _mesa_debug(ctx
, "glBindProgramPipeline(%u)\n", pipeline
);
440 /* Rebinding the same pipeline object: no change.
442 if (ctx
->_Shader
->Name
== pipeline
)
445 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
448 * "The error INVALID_OPERATION is generated:
452 * - by BindProgramPipeline if the current transform feedback
453 * object is active and not paused;
455 if (!no_error
&& _mesa_is_xfb_active_and_unpaused(ctx
)) {
456 _mesa_error(ctx
, GL_INVALID_OPERATION
,
457 "glBindProgramPipeline(transform feedback active)");
461 /* Get pointer to new pipeline object (newObj)
464 /* non-default pipeline object */
465 newObj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
466 if (!no_error
&& !newObj
) {
467 _mesa_error(ctx
, GL_INVALID_OPERATION
,
468 "glBindProgramPipeline(non-gen name)");
472 /* Object is created by any Pipeline call but glGenProgramPipelines,
473 * glIsProgramPipeline and GetProgramPipelineInfoLog
475 newObj
->EverBound
= GL_TRUE
;
478 _mesa_bind_pipeline(ctx
, newObj
);
482 _mesa_BindProgramPipeline_no_error(GLuint pipeline
)
484 GET_CURRENT_CONTEXT(ctx
);
485 bind_program_pipeline(ctx
, pipeline
, true);
489 * Make program of the pipeline current
492 _mesa_BindProgramPipeline(GLuint pipeline
)
494 GET_CURRENT_CONTEXT(ctx
);
495 bind_program_pipeline(ctx
, pipeline
, false);
499 _mesa_bind_pipeline(struct gl_context
*ctx
,
500 struct gl_pipeline_object
*pipe
)
503 /* First bind the Pipeline to pipeline binding point */
504 _mesa_reference_pipeline_object(ctx
, &ctx
->Pipeline
.Current
, pipe
);
506 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
508 * "If there is a current program object established by UseProgram,
509 * that program is considered current for all stages. Otherwise, if
510 * there is a bound program pipeline object (see section 2.11.4), the
511 * program bound to the appropriate stage of the pipeline object is
512 * considered current."
514 if (&ctx
->Shader
!= ctx
->_Shader
) {
515 FLUSH_VERTICES(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
518 /* Bound the pipeline to the current program and
519 * restore the pipeline state
521 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
, pipe
);
523 /* Unbind the pipeline */
524 _mesa_reference_pipeline_object(ctx
, &ctx
->_Shader
,
525 ctx
->Pipeline
.Default
);
528 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
529 struct gl_program
*prog
= ctx
->_Shader
->CurrentProgram
[i
];
531 _mesa_program_init_subroutine_defaults(ctx
, prog
);
538 * Delete a set of pipeline objects.
540 * \param n Number of pipeline objects to delete.
541 * \param ids pipeline of \c n pipeline object IDs.
544 _mesa_DeleteProgramPipelines(GLsizei n
, const GLuint
*pipelines
)
546 GET_CURRENT_CONTEXT(ctx
);
549 if (MESA_VERBOSE
& VERBOSE_API
)
550 _mesa_debug(ctx
, "glDeleteProgramPipelines(%d, %p)\n", n
, pipelines
);
553 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteProgramPipelines(n<0)");
557 for (i
= 0; i
< n
; i
++) {
558 struct gl_pipeline_object
*obj
=
559 _mesa_lookup_pipeline_object(ctx
, pipelines
[i
]);
562 assert(obj
->Name
== pipelines
[i
]);
564 /* If the pipeline object is currently bound, the spec says "If an
565 * object that is currently bound is deleted, the binding for that
566 * object reverts to zero and no program pipeline object becomes
569 if (obj
== ctx
->Pipeline
.Current
) {
570 _mesa_BindProgramPipeline(0);
573 /* The ID is immediately freed for re-use */
574 remove_pipeline_object(ctx
, obj
);
576 /* Unreference the pipeline object.
577 * If refcount hits zero, the object will be deleted.
579 _mesa_reference_pipeline_object(ctx
, &obj
, NULL
);
585 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
586 * \param n Number of IDs to generate.
587 * \param pipelines pipeline of \c n locations to store the IDs.
590 create_program_pipelines(struct gl_context
*ctx
, GLsizei n
, GLuint
*pipelines
,
593 const char *func
= dsa
? "glCreateProgramPipelines" : "glGenProgramPipelines";
600 first
= _mesa_HashFindFreeKeyBlock(ctx
->Pipeline
.Objects
, n
);
602 for (i
= 0; i
< n
; i
++) {
603 struct gl_pipeline_object
*obj
;
604 GLuint name
= first
+ i
;
606 obj
= _mesa_new_pipeline_object(ctx
, name
);
608 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
613 /* make dsa-allocated objects behave like program objects */
614 obj
->EverBound
= GL_TRUE
;
617 save_pipeline_object(ctx
, obj
);
618 pipelines
[i
] = first
+ i
;
623 create_program_pipelines_err(struct gl_context
*ctx
, GLsizei n
,
624 GLuint
*pipelines
, bool dsa
)
626 const char *func
= dsa
? "glCreateProgramPipelines" : "glGenProgramPipelines";
629 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s (n < 0)", func
);
633 create_program_pipelines(ctx
, n
, pipelines
, dsa
);
637 _mesa_GenProgramPipelines_no_error(GLsizei n
, GLuint
*pipelines
)
639 GET_CURRENT_CONTEXT(ctx
);
640 create_program_pipelines(ctx
, n
, pipelines
, false);
644 _mesa_GenProgramPipelines(GLsizei n
, GLuint
*pipelines
)
646 GET_CURRENT_CONTEXT(ctx
);
648 if (MESA_VERBOSE
& VERBOSE_API
)
649 _mesa_debug(ctx
, "glGenProgramPipelines(%d, %p)\n", n
, pipelines
);
651 create_program_pipelines_err(ctx
, n
, pipelines
, false);
655 _mesa_CreateProgramPipelines_no_error(GLsizei n
, GLuint
*pipelines
)
657 GET_CURRENT_CONTEXT(ctx
);
658 create_program_pipelines(ctx
, n
, pipelines
, true);
662 _mesa_CreateProgramPipelines(GLsizei n
, GLuint
*pipelines
)
664 GET_CURRENT_CONTEXT(ctx
);
666 if (MESA_VERBOSE
& VERBOSE_API
)
667 _mesa_debug(ctx
, "glCreateProgramPipelines(%d, %p)\n", n
, pipelines
);
669 create_program_pipelines_err(ctx
, n
, pipelines
, true);
673 * Determine if ID is the name of an pipeline object.
675 * \param id ID of the potential pipeline object.
676 * \return \c GL_TRUE if \c id is the name of a pipeline object,
677 * \c GL_FALSE otherwise.
680 _mesa_IsProgramPipeline(GLuint pipeline
)
682 GET_CURRENT_CONTEXT(ctx
);
684 if (MESA_VERBOSE
& VERBOSE_API
)
685 _mesa_debug(ctx
, "glIsProgramPipeline(%u)\n", pipeline
);
687 struct gl_pipeline_object
*obj
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
691 return obj
->EverBound
;
695 * glGetProgramPipelineiv() - get pipeline shader state.
698 _mesa_GetProgramPipelineiv(GLuint pipeline
, GLenum pname
, GLint
*params
)
700 GET_CURRENT_CONTEXT(ctx
);
701 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
703 if (MESA_VERBOSE
& VERBOSE_API
)
704 _mesa_debug(ctx
, "glGetProgramPipelineiv(%u, %d, %p)\n",
705 pipeline
, pname
, params
);
707 /* Are geometry shaders available in this context?
709 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
710 const bool has_tess
= _mesa_has_tessellation(ctx
);
713 _mesa_error(ctx
, GL_INVALID_OPERATION
,
714 "glGetProgramPipelineiv(pipeline)");
718 /* Object is created by any Pipeline call but glGenProgramPipelines,
719 * glIsProgramPipeline and GetProgramPipelineInfoLog
721 pipe
->EverBound
= GL_TRUE
;
724 case GL_ACTIVE_PROGRAM
:
725 *params
= pipe
->ActiveProgram
? pipe
->ActiveProgram
->Name
: 0;
727 case GL_INFO_LOG_LENGTH
:
728 *params
= (pipe
->InfoLog
&& pipe
->InfoLog
[0] != '\0') ?
729 strlen(pipe
->InfoLog
) + 1 : 0;
731 case GL_VALIDATE_STATUS
:
732 *params
= pipe
->Validated
;
734 case GL_VERTEX_SHADER
:
735 *params
= pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
736 ? pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]->Id
: 0;
738 case GL_TESS_EVALUATION_SHADER
:
741 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]
742 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
]->Id
: 0;
744 case GL_TESS_CONTROL_SHADER
:
747 *params
= pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]
748 ? pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
]->Id
: 0;
750 case GL_GEOMETRY_SHADER
:
753 *params
= pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]
754 ? pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Id
: 0;
756 case GL_FRAGMENT_SHADER
:
757 *params
= pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]
758 ? pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]->Id
: 0;
760 case GL_COMPUTE_SHADER
:
761 if (!_mesa_has_compute_shaders(ctx
))
763 *params
= pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]
764 ? pipe
->CurrentProgram
[MESA_SHADER_COMPUTE
]->Id
: 0;
770 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramPipelineiv(pname=%s)",
771 _mesa_enum_to_string(pname
));
775 * Determines whether every stage in a linked program is active in the
776 * specified pipeline.
779 program_stages_all_active(struct gl_pipeline_object
*pipe
,
780 const struct gl_program
*prog
)
787 unsigned mask
= prog
->sh
.data
->linked_stages
;
789 const int i
= u_bit_scan(&mask
);
790 if (pipe
->CurrentProgram
[i
]) {
791 if (prog
->Id
!= pipe
->CurrentProgram
[i
]->Id
) {
800 pipe
->InfoLog
= ralloc_asprintf(pipe
,
801 "Program %d is not active for all "
802 "shaders that was linked",
810 program_stages_interleaved_illegally(const struct gl_pipeline_object
*pipe
)
812 unsigned prev_linked_stages
= 0;
814 /* Look for programs bound to stages: A -> B -> A, with any intervening
815 * sequence of unrelated programs or empty stages.
817 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
818 struct gl_program
*cur
= pipe
->CurrentProgram
[i
];
820 /* Empty stages anywhere in the pipe are OK. Also we can be confident
821 * that if the linked_stages mask matches we are looking at the same
822 * linked program because a previous validation call to
823 * program_stages_all_active() will have already failed if two different
824 * programs with the sames stages linked are not active for all linked
827 if (!cur
|| cur
->sh
.data
->linked_stages
== prev_linked_stages
)
830 if (prev_linked_stages
) {
831 /* We've seen an A -> B transition; look at the rest of the pipe
832 * to see if we ever see A again.
834 if (prev_linked_stages
>> (i
+ 1))
838 prev_linked_stages
= cur
->sh
.data
->linked_stages
;
845 _mesa_validate_program_pipeline(struct gl_context
* ctx
,
846 struct gl_pipeline_object
*pipe
)
849 bool program_empty
= true;
851 pipe
->Validated
= GL_FALSE
;
853 /* Release and reset the info log.
855 if (pipe
->InfoLog
!= NULL
)
856 ralloc_free(pipe
->InfoLog
);
858 pipe
->InfoLog
= NULL
;
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:
866 * - A program object is active for at least one, but not all of
867 * the shader stages that were present when the program was
870 * For each possible program stage, verify that the program bound to that
871 * stage has all of its stages active. In other words, if the program
872 * bound to the vertex stage also has a fragment shader, the fragment
873 * shader must also be bound to the fragment stage.
875 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
876 if (!program_stages_all_active(pipe
, pipe
->CurrentProgram
[i
])) {
881 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
882 * OpenGL 4.1 spec says:
884 * "[INVALID_OPERATION] is generated by any command that transfers
885 * vertices to the GL if:
889 * - One program object is active for at least two shader stages
890 * and a second program is active for a shader stage between two
891 * stages for which the first program was active."
893 if (program_stages_interleaved_illegally(pipe
)) {
896 "Program is active for multiple shader stages with an "
897 "intervening stage provided by another program");
901 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
902 * OpenGL 4.1 spec says:
904 * "[INVALID_OPERATION] is generated by any command that transfers
905 * vertices to the GL if:
909 * - There is an active program for tessellation control,
910 * tessellation evaluation, or geometry stages with corresponding
911 * executable shader, but there is no active program with
912 * executable vertex shader."
914 if (!pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
915 && (pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
] ||
916 pipe
->CurrentProgram
[MESA_SHADER_TESS_CTRL
] ||
917 pipe
->CurrentProgram
[MESA_SHADER_TESS_EVAL
])) {
918 pipe
->InfoLog
= ralloc_strdup(pipe
, "Program lacks a vertex shader");
922 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
923 * OpenGL 4.1 spec says:
925 * "[INVALID_OPERATION] is generated by any command that transfers
926 * vertices to the GL if:
930 * - There is no current program object specified by UseProgram,
931 * there is a current program pipeline object, and the current
932 * program for any shader stage has been relinked since being
933 * applied to the pipeline object via UseProgramStages with the
934 * PROGRAM_SEPARABLE parameter set to FALSE.
936 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
937 if (pipe
->CurrentProgram
[i
] &&
938 !pipe
->CurrentProgram
[i
]->info
.separate_shader
) {
939 pipe
->InfoLog
= ralloc_asprintf(pipe
,
940 "Program %d was relinked without "
941 "PROGRAM_SEPARABLE state",
942 pipe
->CurrentProgram
[i
]->Id
);
947 /* Section 11.1.3.11 (Validation) of the OpenGL 4.5 spec says:
949 * "An INVALID_OPERATION error is generated by any command that trans-
950 * fers vertices to the GL or launches compute work if the current set
951 * of active program objects cannot be executed, for reasons including:
955 * - There is no current program object specified by UseProgram,
956 * there is a current program pipeline object, and that object is
957 * empty (no executable code is installed for any stage).
959 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
960 if (pipe
->CurrentProgram
[i
]) {
961 program_empty
= false;
970 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
971 * OpenGL 4.1 spec says:
973 * "[INVALID_OPERATION] is generated by any command that transfers
974 * vertices to the GL if:
978 * - Any two active samplers in the current program object are of
979 * different types, but refer to the same texture image unit.
981 * - The number of active samplers in the program exceeds the
982 * maximum number of texture image units allowed."
984 if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe
))
987 /* Validate inputs against outputs, this cannot be done during linking
988 * since programs have been linked separately from each other.
990 * Section 11.1.3.11 (Validation) of the OpenGL 4.5 Core Profile spec says:
992 * "Separable program objects may have validation failures that cannot be
993 * detected without the complete program pipeline. Mismatched interfaces,
994 * improper usage of program objects together, and the same
995 * state-dependent failures can result in validation errors for such
998 * OpenGL ES 3.1 specification has the same text.
1000 * Section 11.1.3.11 (Validation) of the OpenGL ES spec also says:
1002 * An INVALID_OPERATION error is generated by any command that transfers
1003 * vertices to the GL or launches compute work if the current set of
1004 * active program objects cannot be executed, for reasons including:
1006 * * The current program pipeline object contains a shader interface
1007 * that doesn't have an exact match (see section 7.4.1)
1009 * Based on this, only perform the most-strict checking on ES or when the
1010 * application has created a debug context.
1012 if ((_mesa_is_gles(ctx
) || (ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
)) &&
1013 !_mesa_validate_pipeline_io(pipe
)) {
1014 if (_mesa_is_gles(ctx
))
1017 static GLuint msg_id
= 0;
1019 _mesa_gl_debug(ctx
, &msg_id
,
1020 MESA_DEBUG_SOURCE_API
,
1021 MESA_DEBUG_TYPE_PORTABILITY
,
1022 MESA_DEBUG_SEVERITY_MEDIUM
,
1023 "glValidateProgramPipeline: pipeline %u does not meet "
1024 "strict OpenGL ES 3.1 requirements and may not be "
1025 "portable across desktop hardware\n",
1029 pipe
->Validated
= GL_TRUE
;
1034 * Check compatibility of pipeline's program
1037 _mesa_ValidateProgramPipeline(GLuint pipeline
)
1039 GET_CURRENT_CONTEXT(ctx
);
1041 if (MESA_VERBOSE
& VERBOSE_API
)
1042 _mesa_debug(ctx
, "glValidateProgramPipeline(%u)\n", pipeline
);
1044 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
1047 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1048 "glValidateProgramPipeline(pipeline)");
1052 _mesa_validate_program_pipeline(ctx
, pipe
);
1056 _mesa_GetProgramPipelineInfoLog(GLuint pipeline
, GLsizei bufSize
,
1057 GLsizei
*length
, GLchar
*infoLog
)
1059 GET_CURRENT_CONTEXT(ctx
);
1061 if (MESA_VERBOSE
& VERBOSE_API
)
1062 _mesa_debug(ctx
, "glGetProgramPipelineInfoLog(%u, %d, %p, %p)\n",
1063 pipeline
, bufSize
, length
, infoLog
);
1065 struct gl_pipeline_object
*pipe
= _mesa_lookup_pipeline_object(ctx
, pipeline
);
1068 _mesa_error(ctx
, GL_INVALID_VALUE
,
1069 "glGetProgramPipelineInfoLog(pipeline)");
1074 _mesa_error(ctx
, GL_INVALID_VALUE
,
1075 "glGetProgramPipelineInfoLog(bufSize)");
1079 _mesa_copy_string(infoLog
, bufSize
, length
, pipe
->InfoLog
);