mesa: fix KHR_no_error SSO support
[mesa.git] / src / mesa / main / pipelineobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright © 2013 Gregory Hainaut <gregory.hainaut@gmail.com>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * IN THE SOFTWARE.
24 */
25
26 /**
27 * \file pipelineobj.c
28 * \author Hainaut Gregory <gregory.hainaut@gmail.com>
29 *
30 * Implementation of pipeline object related API functions. Based on
31 * GL_ARB_separate_shader_objects extension.
32 */
33
34 #include <stdbool.h>
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"
51
52 /**
53 * Delete a pipeline object.
54 */
55 void
56 _mesa_delete_pipeline_object(struct gl_context *ctx,
57 struct gl_pipeline_object *obj)
58 {
59 unsigned i;
60
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);
64 }
65
66 _mesa_reference_shader_program(ctx, &obj->ActiveProgram, NULL);
67 free(obj->Label);
68 ralloc_free(obj);
69 }
70
71 /**
72 * Allocate and initialize a new pipeline object.
73 */
74 static struct gl_pipeline_object *
75 _mesa_new_pipeline_object(struct gl_context *ctx, GLuint name)
76 {
77 struct gl_pipeline_object *obj = rzalloc(NULL, struct gl_pipeline_object);
78 if (obj) {
79 obj->Name = name;
80 obj->RefCount = 1;
81 obj->Flags = _mesa_get_shader_flags();
82 obj->InfoLog = NULL;
83 }
84
85 return obj;
86 }
87
88 /**
89 * Initialize pipeline object state for given context.
90 */
91 void
92 _mesa_init_pipeline(struct gl_context *ctx)
93 {
94 ctx->Pipeline.Objects = _mesa_NewHashTable();
95
96 ctx->Pipeline.Current = NULL;
97
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);
101 }
102
103
104 /**
105 * Callback for deleting a pipeline object. Called by _mesa_HashDeleteAll().
106 */
107 static void
108 delete_pipelineobj_cb(UNUSED GLuint id, void *data, void *userData)
109 {
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);
113 }
114
115
116 /**
117 * Free pipeline state for given context.
118 */
119 void
120 _mesa_free_pipeline_data(struct gl_context *ctx)
121 {
122 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
123
124 _mesa_HashDeleteAll(ctx->Pipeline.Objects, delete_pipelineobj_cb, ctx);
125 _mesa_DeleteHashTable(ctx->Pipeline.Objects);
126
127 _mesa_delete_pipeline_object(ctx, ctx->Pipeline.Default);
128 }
129
130 /**
131 * Look up the pipeline object for the given ID.
132 *
133 * \returns
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
136 * non-existent.
137 */
138 struct gl_pipeline_object *
139 _mesa_lookup_pipeline_object(struct gl_context *ctx, GLuint id)
140 {
141 if (id == 0)
142 return NULL;
143 else
144 return (struct gl_pipeline_object *)
145 _mesa_HashLookupLocked(ctx->Pipeline.Objects, id);
146 }
147
148 /**
149 * Add the given pipeline object to the pipeline object pool.
150 */
151 static void
152 save_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
153 {
154 if (obj->Name > 0) {
155 _mesa_HashInsertLocked(ctx->Pipeline.Objects, obj->Name, obj);
156 }
157 }
158
159 /**
160 * Remove the given pipeline object from the pipeline object pool.
161 * Do not deallocate the pipeline object though.
162 */
163 static void
164 remove_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
165 {
166 if (obj->Name > 0) {
167 _mesa_HashRemoveLocked(ctx->Pipeline.Objects, obj->Name);
168 }
169 }
170
171 /**
172 * Set ptr to obj w/ reference counting.
173 * Note: this should only be called from the _mesa_reference_pipeline_object()
174 * inline function.
175 */
176 void
177 _mesa_reference_pipeline_object_(struct gl_context *ctx,
178 struct gl_pipeline_object **ptr,
179 struct gl_pipeline_object *obj)
180 {
181 assert(*ptr != obj);
182
183 if (*ptr) {
184 /* Unreference the old pipeline object */
185 struct gl_pipeline_object *oldObj = *ptr;
186
187 assert(oldObj->RefCount > 0);
188 oldObj->RefCount--;
189
190 if (oldObj->RefCount == 0) {
191 _mesa_delete_pipeline_object(ctx, oldObj);
192 }
193
194 *ptr = NULL;
195 }
196 assert(!*ptr);
197
198 if (obj) {
199 /* reference new pipeline object */
200 assert(obj->RefCount > 0);
201
202 obj->RefCount++;
203 *ptr = obj;
204 }
205 }
206
207 static void
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;
215
216 _mesa_use_program(ctx, stage, shProg, prog, pipe);
217 }
218
219 static void
220 use_program_stages(struct gl_context *ctx, struct gl_shader_program *shProg,
221 GLbitfield stages, struct gl_pipeline_object *pipe) {
222
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.
227 *
228 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
229 * says:
230 *
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."
235 */
236 if ((stages & GL_VERTEX_SHADER_BIT) != 0)
237 use_program_stage(ctx, GL_VERTEX_SHADER, shProg, pipe);
238
239 if ((stages & GL_FRAGMENT_SHADER_BIT) != 0)
240 use_program_stage(ctx, GL_FRAGMENT_SHADER, shProg, pipe);
241
242 if ((stages & GL_GEOMETRY_SHADER_BIT) != 0)
243 use_program_stage(ctx, GL_GEOMETRY_SHADER, shProg, pipe);
244
245 if ((stages & GL_TESS_CONTROL_SHADER_BIT) != 0)
246 use_program_stage(ctx, GL_TESS_CONTROL_SHADER, shProg, pipe);
247
248 if ((stages & GL_TESS_EVALUATION_SHADER_BIT) != 0)
249 use_program_stage(ctx, GL_TESS_EVALUATION_SHADER, shProg, pipe);
250
251 if ((stages & GL_COMPUTE_SHADER_BIT) != 0)
252 use_program_stage(ctx, GL_COMPUTE_SHADER, shProg, pipe);
253
254 pipe->Validated = false;
255 }
256
257 void GLAPIENTRY
258 _mesa_UseProgramStages_no_error(GLuint pipeline, GLbitfield stages,
259 GLuint prog)
260 {
261 GET_CURRENT_CONTEXT(ctx);
262
263 struct gl_pipeline_object *pipe =
264 _mesa_lookup_pipeline_object(ctx, pipeline);
265 struct gl_shader_program *shProg = NULL;
266
267 if (prog)
268 shProg = _mesa_lookup_shader_program(ctx, prog);
269
270 /* Object is created by any Pipeline call but glGenProgramPipelines,
271 * glIsProgramPipeline and GetProgramPipelineInfoLog
272 */
273 pipe->EverBound = GL_TRUE;
274
275 use_program_stages(ctx, shProg, stages, pipe);
276 }
277
278 /**
279 * Bound program to severals stages of the pipeline
280 */
281 void GLAPIENTRY
282 _mesa_UseProgramStages(GLuint pipeline, GLbitfield stages, GLuint program)
283 {
284 GET_CURRENT_CONTEXT(ctx);
285
286 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
287 struct gl_shader_program *shProg = NULL;
288 GLbitfield any_valid_stages;
289
290 if (MESA_VERBOSE & VERBOSE_API)
291 _mesa_debug(ctx, "glUseProgramStages(%u, 0x%x, %u)\n",
292 pipeline, stages, program);
293
294 if (!pipe) {
295 _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgramStages(pipeline)");
296 return;
297 }
298
299 /* Object is created by any Pipeline call but glGenProgramPipelines,
300 * glIsProgramPipeline and GetProgramPipelineInfoLog
301 */
302 pipe->EverBound = GL_TRUE;
303
304 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
305 *
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."
308 */
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;
317
318 if (stages != GL_ALL_SHADER_BITS && (stages & ~any_valid_stages) != 0) {
319 _mesa_error(ctx, GL_INVALID_VALUE, "glUseProgramStages(Stages)");
320 return;
321 }
322
323 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
324 * spec says:
325 *
326 * "The error INVALID_OPERATION is generated:
327 *
328 * ...
329 *
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;
333 */
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)");
338 return;
339 }
340 }
341
342 if (program) {
343 shProg = _mesa_lookup_shader_program_err(ctx, program,
344 "glUseProgramStages");
345 if (shProg == NULL)
346 return;
347
348 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
349 * says:
350 *
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
355 * modified."
356 */
357 if (!shProg->data->LinkStatus) {
358 _mesa_error(ctx, GL_INVALID_OPERATION,
359 "glUseProgramStages(program not linked)");
360 return;
361 }
362
363 if (!shProg->SeparateShader) {
364 _mesa_error(ctx, GL_INVALID_OPERATION,
365 "glUseProgramStages(program wasn't linked with the "
366 "PROGRAM_SEPARABLE flag)");
367 return;
368 }
369 }
370
371 use_program_stages(ctx, shProg, stages, pipe);
372 }
373
374 void GLAPIENTRY
375 _mesa_ActiveShaderProgram_no_error(GLuint pipeline, GLuint program)
376 {
377 GET_CURRENT_CONTEXT(ctx);
378 struct gl_shader_program *shProg = NULL;
379 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
380
381 if (program)
382 shProg = _mesa_lookup_shader_program(ctx, program);
383
384 /* Object is created by any Pipeline call but glGenProgramPipelines,
385 * glIsProgramPipeline and GetProgramPipelineInfoLog
386 */
387 pipe->EverBound = GL_TRUE;
388
389 _mesa_reference_shader_program(ctx, &pipe->ActiveProgram, shProg);
390 }
391
392 /**
393 * Use the named shader program for subsequent glUniform calls (if pipeline
394 * bound)
395 */
396 void GLAPIENTRY
397 _mesa_ActiveShaderProgram(GLuint pipeline, GLuint program)
398 {
399 GET_CURRENT_CONTEXT(ctx);
400 struct gl_shader_program *shProg = NULL;
401 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
402
403 if (MESA_VERBOSE & VERBOSE_API)
404 _mesa_debug(ctx, "glActiveShaderProgram(%u, %u)\n", pipeline, program);
405
406 if (program != 0) {
407 shProg = _mesa_lookup_shader_program_err(ctx, program,
408 "glActiveShaderProgram(program)");
409 if (shProg == NULL)
410 return;
411 }
412
413 if (!pipe) {
414 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveShaderProgram(pipeline)");
415 return;
416 }
417
418 /* Object is created by any Pipeline call but glGenProgramPipelines,
419 * glIsProgramPipeline and GetProgramPipelineInfoLog
420 */
421 pipe->EverBound = GL_TRUE;
422
423 if ((shProg != NULL) && !shProg->data->LinkStatus) {
424 _mesa_error(ctx, GL_INVALID_OPERATION,
425 "glActiveShaderProgram(program %u not linked)", shProg->Name);
426 return;
427 }
428
429 _mesa_reference_shader_program(ctx, &pipe->ActiveProgram, shProg);
430 }
431
432 void GLAPIENTRY
433 _mesa_BindProgramPipeline_no_error(GLuint pipeline)
434 {
435 GET_CURRENT_CONTEXT(ctx);
436 struct gl_pipeline_object *newObj = NULL;
437
438 /* Rebinding the same pipeline object: no change.
439 */
440 if (ctx->_Shader->Name == pipeline)
441 return;
442
443 /* Get pointer to new pipeline object (newObj)
444 */
445 if (pipeline) {
446 /* non-default pipeline object */
447 newObj = _mesa_lookup_pipeline_object(ctx, pipeline);
448
449 /* Object is created by any Pipeline call but glGenProgramPipelines,
450 * glIsProgramPipeline and GetProgramPipelineInfoLog
451 */
452 newObj->EverBound = GL_TRUE;
453 }
454
455 _mesa_bind_pipeline(ctx, newObj);
456 }
457
458 /**
459 * Make program of the pipeline current
460 */
461 void GLAPIENTRY
462 _mesa_BindProgramPipeline(GLuint pipeline)
463 {
464 GET_CURRENT_CONTEXT(ctx);
465 struct gl_pipeline_object *newObj = NULL;
466
467 if (MESA_VERBOSE & VERBOSE_API)
468 _mesa_debug(ctx, "glBindProgramPipeline(%u)\n", pipeline);
469
470 /* Rebinding the same pipeline object: no change.
471 */
472 if (ctx->_Shader->Name == pipeline)
473 return;
474
475 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
476 * spec says:
477 *
478 * "The error INVALID_OPERATION is generated:
479 *
480 * ...
481 *
482 * - by BindProgramPipeline if the current transform feedback
483 * object is active and not paused;
484 */
485 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
486 _mesa_error(ctx, GL_INVALID_OPERATION,
487 "glBindProgramPipeline(transform feedback active)");
488 return;
489 }
490
491 /* Get pointer to new pipeline object (newObj)
492 */
493 if (pipeline) {
494 /* non-default pipeline object */
495 newObj = _mesa_lookup_pipeline_object(ctx, pipeline);
496 if (!newObj) {
497 _mesa_error(ctx, GL_INVALID_OPERATION,
498 "glBindProgramPipeline(non-gen name)");
499 return;
500 }
501
502 /* Object is created by any Pipeline call but glGenProgramPipelines,
503 * glIsProgramPipeline and GetProgramPipelineInfoLog
504 */
505 newObj->EverBound = GL_TRUE;
506 }
507
508 _mesa_bind_pipeline(ctx, newObj);
509 }
510
511 void
512 _mesa_bind_pipeline(struct gl_context *ctx,
513 struct gl_pipeline_object *pipe)
514 {
515 int i;
516 /* First bind the Pipeline to pipeline binding point */
517 _mesa_reference_pipeline_object(ctx, &ctx->Pipeline.Current, pipe);
518
519 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
520 *
521 * "If there is a current program object established by UseProgram,
522 * that program is considered current for all stages. Otherwise, if
523 * there is a bound program pipeline object (see section 2.11.4), the
524 * program bound to the appropriate stage of the pipeline object is
525 * considered current."
526 */
527 if (&ctx->Shader != ctx->_Shader) {
528 if (pipe != NULL) {
529 /* Bound the pipeline to the current program and
530 * restore the pipeline state
531 */
532 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, pipe);
533 } else {
534 /* Unbind the pipeline */
535 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
536 ctx->Pipeline.Default);
537 }
538
539 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
540
541 for (i = 0; i < MESA_SHADER_STAGES; i++) {
542 struct gl_program *prog = ctx->_Shader->CurrentProgram[i];
543 if (prog) {
544 _mesa_program_init_subroutine_defaults(ctx, prog);
545 }
546 }
547 }
548 }
549
550 /**
551 * Delete a set of pipeline objects.
552 *
553 * \param n Number of pipeline objects to delete.
554 * \param ids pipeline of \c n pipeline object IDs.
555 */
556 void GLAPIENTRY
557 _mesa_DeleteProgramPipelines(GLsizei n, const GLuint *pipelines)
558 {
559 GET_CURRENT_CONTEXT(ctx);
560 GLsizei i;
561
562 if (MESA_VERBOSE & VERBOSE_API)
563 _mesa_debug(ctx, "glDeleteProgramPipelines(%d, %p)\n", n, pipelines);
564
565 if (n < 0) {
566 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgramPipelines(n<0)");
567 return;
568 }
569
570 for (i = 0; i < n; i++) {
571 struct gl_pipeline_object *obj =
572 _mesa_lookup_pipeline_object(ctx, pipelines[i]);
573
574 if (obj) {
575 assert(obj->Name == pipelines[i]);
576
577 /* If the pipeline object is currently bound, the spec says "If an
578 * object that is currently bound is deleted, the binding for that
579 * object reverts to zero and no program pipeline object becomes
580 * current."
581 */
582 if (obj == ctx->Pipeline.Current) {
583 _mesa_BindProgramPipeline(0);
584 }
585
586 /* The ID is immediately freed for re-use */
587 remove_pipeline_object(ctx, obj);
588
589 /* Unreference the pipeline object.
590 * If refcount hits zero, the object will be deleted.
591 */
592 _mesa_reference_pipeline_object(ctx, &obj, NULL);
593 }
594 }
595 }
596
597 /**
598 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
599 * \param n Number of IDs to generate.
600 * \param pipelines pipeline of \c n locations to store the IDs.
601 */
602 static void
603 create_program_pipelines(struct gl_context *ctx, GLsizei n, GLuint *pipelines,
604 bool dsa)
605 {
606 const char *func;
607 GLuint first;
608 GLint i;
609
610 func = dsa ? "glCreateProgramPipelines" : "glGenProgramPipelines";
611
612 if (n < 0) {
613 _mesa_error(ctx, GL_INVALID_VALUE, "%s (n < 0)", func);
614 return;
615 }
616
617 if (!pipelines) {
618 return;
619 }
620
621 first = _mesa_HashFindFreeKeyBlock(ctx->Pipeline.Objects, n);
622
623 for (i = 0; i < n; i++) {
624 struct gl_pipeline_object *obj;
625 GLuint name = first + i;
626
627 obj = _mesa_new_pipeline_object(ctx, name);
628 if (!obj) {
629 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
630 return;
631 }
632
633 if (dsa) {
634 /* make dsa-allocated objects behave like program objects */
635 obj->EverBound = GL_TRUE;
636 }
637
638 save_pipeline_object(ctx, obj);
639 pipelines[i] = first + i;
640 }
641
642 }
643
644 void GLAPIENTRY
645 _mesa_GenProgramPipelines(GLsizei n, GLuint *pipelines)
646 {
647 GET_CURRENT_CONTEXT(ctx);
648
649 if (MESA_VERBOSE & VERBOSE_API)
650 _mesa_debug(ctx, "glGenProgramPipelines(%d, %p)\n", n, pipelines);
651
652 create_program_pipelines(ctx, n, pipelines, false);
653 }
654
655 void GLAPIENTRY
656 _mesa_CreateProgramPipelines(GLsizei n, GLuint *pipelines)
657 {
658 GET_CURRENT_CONTEXT(ctx);
659
660 if (MESA_VERBOSE & VERBOSE_API)
661 _mesa_debug(ctx, "glCreateProgramPipelines(%d, %p)\n", n, pipelines);
662
663 create_program_pipelines(ctx, n, pipelines, true);
664 }
665
666 /**
667 * Determine if ID is the name of an pipeline object.
668 *
669 * \param id ID of the potential pipeline object.
670 * \return \c GL_TRUE if \c id is the name of a pipeline object,
671 * \c GL_FALSE otherwise.
672 */
673 GLboolean GLAPIENTRY
674 _mesa_IsProgramPipeline(GLuint pipeline)
675 {
676 GET_CURRENT_CONTEXT(ctx);
677
678 if (MESA_VERBOSE & VERBOSE_API)
679 _mesa_debug(ctx, "glIsProgramPipeline(%u)\n", pipeline);
680
681 struct gl_pipeline_object *obj = _mesa_lookup_pipeline_object(ctx, pipeline);
682 if (obj == NULL)
683 return GL_FALSE;
684
685 return obj->EverBound;
686 }
687
688 /**
689 * glGetProgramPipelineiv() - get pipeline shader state.
690 */
691 void GLAPIENTRY
692 _mesa_GetProgramPipelineiv(GLuint pipeline, GLenum pname, GLint *params)
693 {
694 GET_CURRENT_CONTEXT(ctx);
695 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
696
697 if (MESA_VERBOSE & VERBOSE_API)
698 _mesa_debug(ctx, "glGetProgramPipelineiv(%u, %d, %p)\n",
699 pipeline, pname, params);
700
701 /* Are geometry shaders available in this context?
702 */
703 const bool has_gs = _mesa_has_geometry_shaders(ctx);
704 const bool has_tess = _mesa_has_tessellation(ctx);
705
706 if (!pipe) {
707 _mesa_error(ctx, GL_INVALID_OPERATION,
708 "glGetProgramPipelineiv(pipeline)");
709 return;
710 }
711
712 /* Object is created by any Pipeline call but glGenProgramPipelines,
713 * glIsProgramPipeline and GetProgramPipelineInfoLog
714 */
715 pipe->EverBound = GL_TRUE;
716
717 switch (pname) {
718 case GL_ACTIVE_PROGRAM:
719 *params = pipe->ActiveProgram ? pipe->ActiveProgram->Name : 0;
720 return;
721 case GL_INFO_LOG_LENGTH:
722 *params = (pipe->InfoLog && pipe->InfoLog[0] != '\0') ?
723 strlen(pipe->InfoLog) + 1 : 0;
724 return;
725 case GL_VALIDATE_STATUS:
726 *params = pipe->Validated;
727 return;
728 case GL_VERTEX_SHADER:
729 *params = pipe->CurrentProgram[MESA_SHADER_VERTEX]
730 ? pipe->CurrentProgram[MESA_SHADER_VERTEX]->Id : 0;
731 return;
732 case GL_TESS_EVALUATION_SHADER:
733 if (!has_tess)
734 break;
735 *params = pipe->CurrentProgram[MESA_SHADER_TESS_EVAL]
736 ? pipe->CurrentProgram[MESA_SHADER_TESS_EVAL]->Id : 0;
737 return;
738 case GL_TESS_CONTROL_SHADER:
739 if (!has_tess)
740 break;
741 *params = pipe->CurrentProgram[MESA_SHADER_TESS_CTRL]
742 ? pipe->CurrentProgram[MESA_SHADER_TESS_CTRL]->Id : 0;
743 return;
744 case GL_GEOMETRY_SHADER:
745 if (!has_gs)
746 break;
747 *params = pipe->CurrentProgram[MESA_SHADER_GEOMETRY]
748 ? pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Id : 0;
749 return;
750 case GL_FRAGMENT_SHADER:
751 *params = pipe->CurrentProgram[MESA_SHADER_FRAGMENT]
752 ? pipe->CurrentProgram[MESA_SHADER_FRAGMENT]->Id : 0;
753 return;
754 case GL_COMPUTE_SHADER:
755 if (!_mesa_has_compute_shaders(ctx))
756 break;
757 *params = pipe->CurrentProgram[MESA_SHADER_COMPUTE]
758 ? pipe->CurrentProgram[MESA_SHADER_COMPUTE]->Id : 0;
759 return;
760 default:
761 break;
762 }
763
764 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramPipelineiv(pname=%s)",
765 _mesa_enum_to_string(pname));
766 }
767
768 /**
769 * Determines whether every stage in a linked program is active in the
770 * specified pipeline.
771 */
772 static bool
773 program_stages_all_active(struct gl_pipeline_object *pipe,
774 const struct gl_program *prog)
775 {
776 bool status = true;
777
778 if (!prog)
779 return true;
780
781 unsigned mask = prog->sh.data->linked_stages;
782 while (mask) {
783 const int i = u_bit_scan(&mask);
784 if (pipe->CurrentProgram[i]) {
785 if (prog->Id != pipe->CurrentProgram[i]->Id) {
786 status = false;
787 }
788 } else {
789 status = false;
790 }
791 }
792
793 if (!status) {
794 pipe->InfoLog = ralloc_asprintf(pipe,
795 "Program %d is not active for all "
796 "shaders that was linked",
797 prog->Id);
798 }
799
800 return status;
801 }
802
803 static bool
804 program_stages_interleaved_illegally(const struct gl_pipeline_object *pipe)
805 {
806 unsigned prev_linked_stages = 0;
807
808 /* Look for programs bound to stages: A -> B -> A, with any intervening
809 * sequence of unrelated programs or empty stages.
810 */
811 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
812 struct gl_program *cur = pipe->CurrentProgram[i];
813
814 /* Empty stages anywhere in the pipe are OK. Also we can be confident
815 * that if the linked_stages mask matches we are looking at the same
816 * linked program because a previous validation call to
817 * program_stages_all_active() will have already failed if two different
818 * programs with the sames stages linked are not active for all linked
819 * stages.
820 */
821 if (!cur || cur->sh.data->linked_stages == prev_linked_stages)
822 continue;
823
824 if (prev_linked_stages) {
825 /* We've seen an A -> B transition; look at the rest of the pipe
826 * to see if we ever see A again.
827 */
828 if (prev_linked_stages >> (i + 1))
829 return true;
830 }
831
832 prev_linked_stages = cur->sh.data->linked_stages;
833 }
834
835 return false;
836 }
837
838 extern GLboolean
839 _mesa_validate_program_pipeline(struct gl_context* ctx,
840 struct gl_pipeline_object *pipe)
841 {
842 unsigned i;
843 bool program_empty = true;
844
845 pipe->Validated = GL_FALSE;
846
847 /* Release and reset the info log.
848 */
849 if (pipe->InfoLog != NULL)
850 ralloc_free(pipe->InfoLog);
851
852 pipe->InfoLog = NULL;
853
854 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
855 * OpenGL 4.1 spec says:
856 *
857 * "[INVALID_OPERATION] is generated by any command that transfers
858 * vertices to the GL if:
859 *
860 * - A program object is active for at least one, but not all of
861 * the shader stages that were present when the program was
862 * linked."
863 *
864 * For each possible program stage, verify that the program bound to that
865 * stage has all of its stages active. In other words, if the program
866 * bound to the vertex stage also has a fragment shader, the fragment
867 * shader must also be bound to the fragment stage.
868 */
869 for (i = 0; i < MESA_SHADER_STAGES; i++) {
870 if (!program_stages_all_active(pipe, pipe->CurrentProgram[i])) {
871 return GL_FALSE;
872 }
873 }
874
875 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
876 * OpenGL 4.1 spec says:
877 *
878 * "[INVALID_OPERATION] is generated by any command that transfers
879 * vertices to the GL if:
880 *
881 * ...
882 *
883 * - One program object is active for at least two shader stages
884 * and a second program is active for a shader stage between two
885 * stages for which the first program was active."
886 */
887 if (program_stages_interleaved_illegally(pipe)) {
888 pipe->InfoLog =
889 ralloc_strdup(pipe,
890 "Program is active for multiple shader stages with an "
891 "intervening stage provided by another program");
892 return GL_FALSE;
893 }
894
895 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
896 * OpenGL 4.1 spec says:
897 *
898 * "[INVALID_OPERATION] is generated by any command that transfers
899 * vertices to the GL if:
900 *
901 * ...
902 *
903 * - There is an active program for tessellation control,
904 * tessellation evaluation, or geometry stages with corresponding
905 * executable shader, but there is no active program with
906 * executable vertex shader."
907 */
908 if (!pipe->CurrentProgram[MESA_SHADER_VERTEX]
909 && (pipe->CurrentProgram[MESA_SHADER_GEOMETRY] ||
910 pipe->CurrentProgram[MESA_SHADER_TESS_CTRL] ||
911 pipe->CurrentProgram[MESA_SHADER_TESS_EVAL])) {
912 pipe->InfoLog = ralloc_strdup(pipe, "Program lacks a vertex shader");
913 return GL_FALSE;
914 }
915
916 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
917 * OpenGL 4.1 spec says:
918 *
919 * "[INVALID_OPERATION] is generated by any command that transfers
920 * vertices to the GL if:
921 *
922 * ...
923 *
924 * - There is no current program object specified by UseProgram,
925 * there is a current program pipeline object, and the current
926 * program for any shader stage has been relinked since being
927 * applied to the pipeline object via UseProgramStages with the
928 * PROGRAM_SEPARABLE parameter set to FALSE.
929 */
930 for (i = 0; i < MESA_SHADER_STAGES; i++) {
931 if (pipe->CurrentProgram[i] &&
932 !pipe->CurrentProgram[i]->info.separate_shader) {
933 pipe->InfoLog = ralloc_asprintf(pipe,
934 "Program %d was relinked without "
935 "PROGRAM_SEPARABLE state",
936 pipe->CurrentProgram[i]->Id);
937 return GL_FALSE;
938 }
939 }
940
941 /* Section 11.1.3.11 (Validation) of the OpenGL 4.5 spec says:
942 *
943 * "An INVALID_OPERATION error is generated by any command that trans-
944 * fers vertices to the GL or launches compute work if the current set
945 * of active program objects cannot be executed, for reasons including:
946 *
947 * ...
948 *
949 * - There is no current program object specified by UseProgram,
950 * there is a current program pipeline object, and that object is
951 * empty (no executable code is installed for any stage).
952 */
953 for (i = 0; i < MESA_SHADER_STAGES; i++) {
954 if (pipe->CurrentProgram[i]) {
955 program_empty = false;
956 break;
957 }
958 }
959
960 if (program_empty) {
961 return GL_FALSE;
962 }
963
964 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
965 * OpenGL 4.1 spec says:
966 *
967 * "[INVALID_OPERATION] is generated by any command that transfers
968 * vertices to the GL if:
969 *
970 * ...
971 *
972 * - Any two active samplers in the current program object are of
973 * different types, but refer to the same texture image unit.
974 *
975 * - The number of active samplers in the program exceeds the
976 * maximum number of texture image units allowed."
977 */
978 if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe))
979 return GL_FALSE;
980
981 /* Validate inputs against outputs, this cannot be done during linking
982 * since programs have been linked separately from each other.
983 *
984 * Section 11.1.3.11 (Validation) of the OpenGL 4.5 Core Profile spec says:
985 *
986 * "Separable program objects may have validation failures that cannot be
987 * detected without the complete program pipeline. Mismatched interfaces,
988 * improper usage of program objects together, and the same
989 * state-dependent failures can result in validation errors for such
990 * program objects."
991 *
992 * OpenGL ES 3.1 specification has the same text.
993 *
994 * Section 11.1.3.11 (Validation) of the OpenGL ES spec also says:
995 *
996 * An INVALID_OPERATION error is generated by any command that transfers
997 * vertices to the GL or launches compute work if the current set of
998 * active program objects cannot be executed, for reasons including:
999 *
1000 * * The current program pipeline object contains a shader interface
1001 * that doesn't have an exact match (see section 7.4.1)
1002 *
1003 * Based on this, only perform the most-strict checking on ES or when the
1004 * application has created a debug context.
1005 */
1006 if ((_mesa_is_gles(ctx) || (ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT)) &&
1007 !_mesa_validate_pipeline_io(pipe)) {
1008 if (_mesa_is_gles(ctx))
1009 return GL_FALSE;
1010
1011 static GLuint msg_id = 0;
1012
1013 _mesa_gl_debug(ctx, &msg_id,
1014 MESA_DEBUG_SOURCE_API,
1015 MESA_DEBUG_TYPE_PORTABILITY,
1016 MESA_DEBUG_SEVERITY_MEDIUM,
1017 "glValidateProgramPipeline: pipeline %u does not meet "
1018 "strict OpenGL ES 3.1 requirements and may not be "
1019 "portable across desktop hardware\n",
1020 pipe->Name);
1021 }
1022
1023 pipe->Validated = GL_TRUE;
1024 return GL_TRUE;
1025 }
1026
1027 /**
1028 * Check compatibility of pipeline's program
1029 */
1030 void GLAPIENTRY
1031 _mesa_ValidateProgramPipeline(GLuint pipeline)
1032 {
1033 GET_CURRENT_CONTEXT(ctx);
1034
1035 if (MESA_VERBOSE & VERBOSE_API)
1036 _mesa_debug(ctx, "glValidateProgramPipeline(%u)\n", pipeline);
1037
1038 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
1039
1040 if (!pipe) {
1041 _mesa_error(ctx, GL_INVALID_OPERATION,
1042 "glValidateProgramPipeline(pipeline)");
1043 return;
1044 }
1045
1046 _mesa_validate_program_pipeline(ctx, pipe);
1047 }
1048
1049 void GLAPIENTRY
1050 _mesa_GetProgramPipelineInfoLog(GLuint pipeline, GLsizei bufSize,
1051 GLsizei *length, GLchar *infoLog)
1052 {
1053 GET_CURRENT_CONTEXT(ctx);
1054
1055 if (MESA_VERBOSE & VERBOSE_API)
1056 _mesa_debug(ctx, "glGetProgramPipelineInfoLog(%u, %d, %p, %p)\n",
1057 pipeline, bufSize, length, infoLog);
1058
1059 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
1060
1061 if (!pipe) {
1062 _mesa_error(ctx, GL_INVALID_VALUE,
1063 "glGetProgramPipelineInfoLog(pipeline)");
1064 return;
1065 }
1066
1067 if (bufSize < 0) {
1068 _mesa_error(ctx, GL_INVALID_VALUE,
1069 "glGetProgramPipelineInfoLog(bufSize)");
1070 return;
1071 }
1072
1073 _mesa_copy_string(infoLog, bufSize, length, pipe->InfoLog);
1074 }