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