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