mesa: allow tess stages in 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 "main/glheader.h"
35 #include "main/context.h"
36 #include "main/dispatch.h"
37 #include "main/enums.h"
38 #include "main/hash.h"
39 #include "main/mtypes.h"
40 #include "main/pipelineobj.h"
41 #include "main/shaderapi.h"
42 #include "main/shaderobj.h"
43 #include "main/transformfeedback.h"
44 #include "main/uniforms.h"
45 #include "program/program.h"
46 #include "program/prog_parameter.h"
47 #include "util/ralloc.h"
48 #include <stdbool.h>
49 #include "../glsl/glsl_parser_extras.h"
50 #include "../glsl/ir_uniform.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_shader_program(ctx, &obj->_CurrentFragmentProgram, NULL);
62
63 for (i = 0; i < MESA_SHADER_STAGES; i++)
64 _mesa_reference_shader_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(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 /**
222 * Bound program to severals stages of the pipeline
223 */
224 void GLAPIENTRY
225 _mesa_UseProgramStages(GLuint pipeline, GLbitfield stages, GLuint program)
226 {
227 GET_CURRENT_CONTEXT(ctx);
228
229 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
230 struct gl_shader_program *shProg = NULL;
231 GLbitfield any_valid_stages;
232
233 if (!pipe) {
234 _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgramStages(pipeline)");
235 return;
236 }
237
238 /* Object is created by any Pipeline call but glGenProgramPipelines,
239 * glIsProgramPipeline and GetProgramPipelineInfoLog
240 */
241 pipe->EverBound = GL_TRUE;
242
243 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
244 *
245 * "If stages is not the special value ALL_SHADER_BITS, and has a bit
246 * set that is not recognized, the error INVALID_VALUE is generated."
247 */
248 any_valid_stages = GL_VERTEX_SHADER_BIT | GL_FRAGMENT_SHADER_BIT;
249 if (_mesa_has_geometry_shaders(ctx))
250 any_valid_stages |= GL_GEOMETRY_SHADER_BIT;
251 if (_mesa_has_tessellation(ctx))
252 any_valid_stages |= GL_TESS_CONTROL_SHADER_BIT |
253 GL_TESS_EVALUATION_SHADER_BIT;
254
255 if (stages != GL_ALL_SHADER_BITS && (stages & ~any_valid_stages) != 0) {
256 _mesa_error(ctx, GL_INVALID_VALUE, "glUseProgramStages(Stages)");
257 return;
258 }
259
260 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
261 * spec says:
262 *
263 * "The error INVALID_OPERATION is generated:
264 *
265 * ...
266 *
267 * - by UseProgramStages if the program pipeline object it refers
268 * to is current and the current transform feedback object is
269 * active and not paused;
270 */
271 if (ctx->_Shader == pipe) {
272 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
273 _mesa_error(ctx, GL_INVALID_OPERATION,
274 "glUseProgramStages(transform feedback active)");
275 return;
276 }
277 }
278
279 if (program) {
280 shProg = _mesa_lookup_shader_program_err(ctx, program,
281 "glUseProgramStages");
282 if (shProg == NULL)
283 return;
284
285 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
286 * says:
287 *
288 * "If the program object named by program was linked without the
289 * PROGRAM_SEPARABLE parameter set, or was not linked successfully,
290 * the error INVALID_OPERATION is generated and the corresponding
291 * shader stages in the pipeline program pipeline object are not
292 * modified."
293 */
294 if (!shProg->LinkStatus) {
295 _mesa_error(ctx, GL_INVALID_OPERATION,
296 "glUseProgramStages(program not linked)");
297 return;
298 }
299
300 if (!shProg->SeparateShader) {
301 _mesa_error(ctx, GL_INVALID_OPERATION,
302 "glUseProgramStages(program wasn't linked with the "
303 "PROGRAM_SEPARABLE flag)");
304 return;
305 }
306 }
307
308 /* Enable individual stages from the program as requested by the
309 * application. If there is no shader for a requested stage in the
310 * program, _mesa_use_shader_program will enable fixed-function processing
311 * as dictated by the spec.
312 *
313 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
314 * says:
315 *
316 * "If UseProgramStages is called with program set to zero or with a
317 * program object that contains no executable code for the given
318 * stages, it is as if the pipeline object has no programmable stage
319 * configured for the indicated shader stages."
320 */
321 if ((stages & GL_VERTEX_SHADER_BIT) != 0)
322 _mesa_use_shader_program(ctx, GL_VERTEX_SHADER, shProg, pipe);
323
324 if ((stages & GL_FRAGMENT_SHADER_BIT) != 0)
325 _mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER, shProg, pipe);
326
327 if ((stages & GL_GEOMETRY_SHADER_BIT) != 0)
328 _mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER, shProg, pipe);
329
330 if ((stages & GL_TESS_CONTROL_SHADER_BIT) != 0)
331 _mesa_use_shader_program(ctx, GL_TESS_CONTROL_SHADER, shProg, pipe);
332
333 if ((stages & GL_TESS_EVALUATION_SHADER_BIT) != 0)
334 _mesa_use_shader_program(ctx, GL_TESS_EVALUATION_SHADER, shProg, pipe);
335 }
336
337 /**
338 * Use the named shader program for subsequent glUniform calls (if pipeline
339 * bound)
340 */
341 void GLAPIENTRY
342 _mesa_ActiveShaderProgram(GLuint pipeline, GLuint program)
343 {
344 GET_CURRENT_CONTEXT(ctx);
345 struct gl_shader_program *shProg = NULL;
346 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
347
348 if (program != 0) {
349 shProg = _mesa_lookup_shader_program_err(ctx, program,
350 "glActiveShaderProgram(program)");
351 if (shProg == NULL)
352 return;
353 }
354
355 if (!pipe) {
356 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveShaderProgram(pipeline)");
357 return;
358 }
359
360 /* Object is created by any Pipeline call but glGenProgramPipelines,
361 * glIsProgramPipeline and GetProgramPipelineInfoLog
362 */
363 pipe->EverBound = GL_TRUE;
364
365 if ((shProg != NULL) && !shProg->LinkStatus) {
366 _mesa_error(ctx, GL_INVALID_OPERATION,
367 "glActiveShaderProgram(program %u not linked)", shProg->Name);
368 return;
369 }
370
371 _mesa_reference_shader_program(ctx, &pipe->ActiveProgram, shProg);
372 }
373
374 /**
375 * Make program of the pipeline current
376 */
377 void GLAPIENTRY
378 _mesa_BindProgramPipeline(GLuint pipeline)
379 {
380 GET_CURRENT_CONTEXT(ctx);
381 struct gl_pipeline_object *newObj = NULL;
382
383 /* Rebinding the same pipeline object: no change.
384 */
385 if (ctx->_Shader->Name == pipeline)
386 return;
387
388 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
389 * spec says:
390 *
391 * "The error INVALID_OPERATION is generated:
392 *
393 * ...
394 *
395 * - by BindProgramPipeline if the current transform feedback
396 * object is active and not paused;
397 */
398 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
399 _mesa_error(ctx, GL_INVALID_OPERATION,
400 "glBindProgramPipeline(transform feedback active)");
401 return;
402 }
403
404 /* Get pointer to new pipeline object (newObj)
405 */
406 if (pipeline) {
407 /* non-default pipeline object */
408 newObj = _mesa_lookup_pipeline_object(ctx, pipeline);
409 if (!newObj) {
410 _mesa_error(ctx, GL_INVALID_OPERATION,
411 "glBindProgramPipeline(non-gen name)");
412 return;
413 }
414
415 /* Object is created by any Pipeline call but glGenProgramPipelines,
416 * glIsProgramPipeline and GetProgramPipelineInfoLog
417 */
418 newObj->EverBound = GL_TRUE;
419 }
420
421 _mesa_bind_pipeline(ctx, newObj);
422 }
423
424 void
425 _mesa_bind_pipeline(struct gl_context *ctx,
426 struct gl_pipeline_object *pipe)
427 {
428 /* First bind the Pipeline to pipeline binding point */
429 _mesa_reference_pipeline_object(ctx, &ctx->Pipeline.Current, pipe);
430
431 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
432 *
433 * "If there is a current program object established by UseProgram,
434 * that program is considered current for all stages. Otherwise, if
435 * there is a bound program pipeline object (see section 2.11.4), the
436 * program bound to the appropriate stage of the pipeline object is
437 * considered current."
438 */
439 if (&ctx->Shader != ctx->_Shader) {
440 if (pipe != NULL) {
441 /* Bound the pipeline to the current program and
442 * restore the pipeline state
443 */
444 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, pipe);
445 } else {
446 /* Unbind the pipeline */
447 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
448 ctx->Pipeline.Default);
449 }
450
451 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
452
453 if (ctx->Driver.UseProgram)
454 ctx->Driver.UseProgram(ctx, NULL);
455 }
456 }
457
458 /**
459 * Delete a set of pipeline objects.
460 *
461 * \param n Number of pipeline objects to delete.
462 * \param ids pipeline of \c n pipeline object IDs.
463 */
464 void GLAPIENTRY
465 _mesa_DeleteProgramPipelines(GLsizei n, const GLuint *pipelines)
466 {
467 GET_CURRENT_CONTEXT(ctx);
468 GLsizei i;
469
470 if (n < 0) {
471 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgramPipelines(n<0)");
472 return;
473 }
474
475 for (i = 0; i < n; i++) {
476 struct gl_pipeline_object *obj =
477 _mesa_lookup_pipeline_object(ctx, pipelines[i]);
478
479 if (obj) {
480 assert(obj->Name == pipelines[i]);
481
482 /* If the pipeline object is currently bound, the spec says "If an
483 * object that is currently bound is deleted, the binding for that
484 * object reverts to zero and no program pipeline object becomes
485 * current."
486 */
487 if (obj == ctx->Pipeline.Current) {
488 _mesa_BindProgramPipeline(0);
489 }
490
491 /* The ID is immediately freed for re-use */
492 remove_pipeline_object(ctx, obj);
493
494 /* Unreference the pipeline object.
495 * If refcount hits zero, the object will be deleted.
496 */
497 _mesa_reference_pipeline_object(ctx, &obj, NULL);
498 }
499 }
500 }
501
502 /**
503 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
504 * \param n Number of IDs to generate.
505 * \param pipelines pipeline of \c n locations to store the IDs.
506 */
507 static void
508 create_program_pipelines(struct gl_context *ctx, GLsizei n, GLuint *pipelines,
509 bool dsa)
510 {
511 const char *func;
512 GLuint first;
513 GLint i;
514
515 func = dsa ? "glCreateProgramPipelines" : "glGenProgramPipelines";
516
517 if (n < 0) {
518 _mesa_error(ctx, GL_INVALID_VALUE, "%s (n < 0)", func);
519 return;
520 }
521
522 if (!pipelines) {
523 return;
524 }
525
526 first = _mesa_HashFindFreeKeyBlock(ctx->Pipeline.Objects, n);
527
528 for (i = 0; i < n; i++) {
529 struct gl_pipeline_object *obj;
530 GLuint name = first + i;
531
532 obj = _mesa_new_pipeline_object(ctx, name);
533 if (!obj) {
534 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
535 return;
536 }
537
538 if (dsa) {
539 /* make dsa-allocated objects behave like program objects */
540 obj->EverBound = GL_TRUE;
541 }
542
543 save_pipeline_object(ctx, obj);
544 pipelines[i] = first + i;
545 }
546
547 }
548
549 void GLAPIENTRY
550 _mesa_GenProgramPipelines(GLsizei n, GLuint *pipelines)
551 {
552 GET_CURRENT_CONTEXT(ctx);
553
554 create_program_pipelines(ctx, n, pipelines, false);
555 }
556
557 void GLAPIENTRY
558 _mesa_CreateProgramPipelines(GLsizei n, GLuint *pipelines)
559 {
560 GET_CURRENT_CONTEXT(ctx);
561
562 create_program_pipelines(ctx, n, pipelines, true);
563 }
564
565 /**
566 * Determine if ID is the name of an pipeline object.
567 *
568 * \param id ID of the potential pipeline object.
569 * \return \c GL_TRUE if \c id is the name of a pipeline object,
570 * \c GL_FALSE otherwise.
571 */
572 GLboolean GLAPIENTRY
573 _mesa_IsProgramPipeline(GLuint pipeline)
574 {
575 GET_CURRENT_CONTEXT(ctx);
576
577 struct gl_pipeline_object *obj = _mesa_lookup_pipeline_object(ctx, pipeline);
578 if (obj == NULL)
579 return GL_FALSE;
580
581 return obj->EverBound;
582 }
583
584 /**
585 * glGetProgramPipelineiv() - get pipeline shader state.
586 */
587 void GLAPIENTRY
588 _mesa_GetProgramPipelineiv(GLuint pipeline, GLenum pname, GLint *params)
589 {
590 GET_CURRENT_CONTEXT(ctx);
591 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
592
593 /* Are geometry shaders available in this context?
594 */
595 const bool has_gs = _mesa_has_geometry_shaders(ctx);
596 const bool has_tess = _mesa_has_tessellation(ctx);;
597
598 if (!pipe) {
599 _mesa_error(ctx, GL_INVALID_OPERATION,
600 "glGetProgramPipelineiv(pipeline)");
601 return;
602 }
603
604 /* Object is created by any Pipeline call but glGenProgramPipelines,
605 * glIsProgramPipeline and GetProgramPipelineInfoLog
606 */
607 pipe->EverBound = GL_TRUE;
608
609 switch (pname) {
610 case GL_ACTIVE_PROGRAM:
611 *params = pipe->ActiveProgram ? pipe->ActiveProgram->Name : 0;
612 return;
613 case GL_INFO_LOG_LENGTH:
614 *params = pipe->InfoLog ? strlen(pipe->InfoLog) + 1 : 0;
615 return;
616 case GL_VALIDATE_STATUS:
617 *params = pipe->Validated;
618 return;
619 case GL_VERTEX_SHADER:
620 *params = pipe->CurrentProgram[MESA_SHADER_VERTEX]
621 ? pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name : 0;
622 return;
623 case GL_TESS_EVALUATION_SHADER:
624 if (!has_tess)
625 break;
626 *params = pipe->CurrentProgram[MESA_SHADER_TESS_EVAL]
627 ? pipe->CurrentProgram[MESA_SHADER_TESS_EVAL]->Name : 0;
628 return;
629 case GL_TESS_CONTROL_SHADER:
630 if (!has_tess)
631 break;
632 *params = pipe->CurrentProgram[MESA_SHADER_TESS_CTRL]
633 ? pipe->CurrentProgram[MESA_SHADER_TESS_CTRL]->Name : 0;
634 return;
635 case GL_GEOMETRY_SHADER:
636 if (!has_gs)
637 break;
638 *params = pipe->CurrentProgram[MESA_SHADER_GEOMETRY]
639 ? pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name : 0;
640 return;
641 case GL_FRAGMENT_SHADER:
642 *params = pipe->CurrentProgram[MESA_SHADER_FRAGMENT]
643 ? pipe->CurrentProgram[MESA_SHADER_FRAGMENT]->Name : 0;
644 return;
645 default:
646 break;
647 }
648
649 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramPipelineiv(pname=%s)",
650 _mesa_enum_to_string(pname));
651 }
652
653 /**
654 * Determines whether every stage in a linked program is active in the
655 * specified pipeline.
656 */
657 static bool
658 program_stages_all_active(struct gl_pipeline_object *pipe,
659 const struct gl_shader_program *prog)
660 {
661 unsigned i;
662 bool status = true;
663
664 if (!prog)
665 return true;
666
667 for (i = 0; i < MESA_SHADER_STAGES; i++) {
668 if (prog->_LinkedShaders[i]) {
669 if (pipe->CurrentProgram[i]) {
670 if (prog->Name != pipe->CurrentProgram[i]->Name) {
671 status = false;
672 }
673 } else {
674 status = false;
675 }
676 }
677 }
678
679 if (!status) {
680 pipe->InfoLog = ralloc_asprintf(pipe,
681 "Program %d is not active for all "
682 "shaders that was linked",
683 prog->Name);
684 }
685
686 return status;
687 }
688
689 static bool
690 program_stages_interleaved_illegally(const struct gl_pipeline_object *pipe)
691 {
692 struct gl_shader_program *prev = NULL;
693 unsigned i, j;
694
695 /* Look for programs bound to stages: A -> B -> A, with any intervening
696 * sequence of unrelated programs or empty stages.
697 */
698 for (i = 0; i < MESA_SHADER_STAGES; i++) {
699 struct gl_shader_program *cur = pipe->CurrentProgram[i];
700
701 /* Empty stages anywhere in the pipe are OK */
702 if (!cur || cur == prev)
703 continue;
704
705 if (prev) {
706 /* We've seen an A -> B transition; look at the rest of the pipe
707 * to see if we ever see A again.
708 */
709 for (j = i + 1; j < MESA_SHADER_STAGES; j++) {
710 if (pipe->CurrentProgram[j] == prev)
711 return true;
712 }
713 }
714
715 prev = cur;
716 }
717
718 return false;
719 }
720
721 extern GLboolean
722 _mesa_validate_program_pipeline(struct gl_context* ctx,
723 struct gl_pipeline_object *pipe,
724 GLboolean IsBound)
725 {
726 unsigned i;
727
728 pipe->Validated = GL_FALSE;
729
730 /* Release and reset the info log.
731 */
732 if (pipe->InfoLog != NULL)
733 ralloc_free(pipe->InfoLog);
734
735 pipe->InfoLog = NULL;
736
737 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
738 * OpenGL 4.1 spec says:
739 *
740 * "[INVALID_OPERATION] is generated by any command that transfers
741 * vertices to the GL if:
742 *
743 * - A program object is active for at least one, but not all of
744 * the shader stages that were present when the program was
745 * linked."
746 *
747 * For each possible program stage, verify that the program bound to that
748 * stage has all of its stages active. In other words, if the program
749 * bound to the vertex stage also has a fragment shader, the fragment
750 * shader must also be bound to the fragment stage.
751 */
752 for (i = 0; i < MESA_SHADER_STAGES; i++) {
753 if (!program_stages_all_active(pipe, pipe->CurrentProgram[i])) {
754 goto err;
755 }
756 }
757
758 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
759 * OpenGL 4.1 spec says:
760 *
761 * "[INVALID_OPERATION] is generated by any command that transfers
762 * vertices to the GL if:
763 *
764 * ...
765 *
766 * - One program object is active for at least two shader stages
767 * and a second program is active for a shader stage between two
768 * stages for which the first program was active."
769 */
770 if (program_stages_interleaved_illegally(pipe)) {
771 pipe->InfoLog =
772 ralloc_strdup(pipe,
773 "Program is active for multiple shader stages with an "
774 "intervening stage provided by another program");
775 goto err;
776 }
777
778 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
779 * OpenGL 4.1 spec says:
780 *
781 * "[INVALID_OPERATION] is generated by any command that transfers
782 * vertices to the GL if:
783 *
784 * ...
785 *
786 * - There is an active program for tessellation control,
787 * tessellation evaluation, or geometry stages with corresponding
788 * executable shader, but there is no active program with
789 * executable vertex shader."
790 */
791 if (!pipe->CurrentProgram[MESA_SHADER_VERTEX]
792 && pipe->CurrentProgram[MESA_SHADER_GEOMETRY]) {
793 pipe->InfoLog = ralloc_strdup(pipe, "Program lacks a vertex shader");
794 goto err;
795 }
796
797 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
798 * OpenGL 4.1 spec says:
799 *
800 * "[INVALID_OPERATION] is generated by any command that transfers
801 * vertices to the GL if:
802 *
803 * ...
804 *
805 * - There is no current program object specified by UseProgram,
806 * there is a current program pipeline object, and the current
807 * program for any shader stage has been relinked since being
808 * applied to the pipeline object via UseProgramStages with the
809 * PROGRAM_SEPARABLE parameter set to FALSE.
810 */
811 for (i = 0; i < MESA_SHADER_STAGES; i++) {
812 if (pipe->CurrentProgram[i] && !pipe->CurrentProgram[i]->SeparateShader) {
813 pipe->InfoLog = ralloc_asprintf(pipe,
814 "Program %d was relinked without "
815 "PROGRAM_SEPARABLE state",
816 pipe->CurrentProgram[i]->Name);
817 goto err;
818 }
819 }
820
821 /* Section 2.11.11 (Shader Execution), subheading "Validation," of the
822 * OpenGL 4.1 spec says:
823 *
824 * "[INVALID_OPERATION] is generated by any command that transfers
825 * vertices to the GL if:
826 *
827 * ...
828 *
829 * - Any two active samplers in the current program object are of
830 * different types, but refer to the same texture image unit.
831 *
832 * - The number of active samplers in the program exceeds the
833 * maximum number of texture image units allowed."
834 */
835 if (!_mesa_sampler_uniforms_pipeline_are_valid(pipe))
836 goto err;
837
838 pipe->Validated = GL_TRUE;
839 return GL_TRUE;
840
841 err:
842 if (IsBound)
843 _mesa_error(ctx, GL_INVALID_OPERATION,
844 "glValidateProgramPipeline failed to validate the pipeline");
845
846 return GL_FALSE;
847 }
848
849 /**
850 * Check compatibility of pipeline's program
851 */
852 void GLAPIENTRY
853 _mesa_ValidateProgramPipeline(GLuint pipeline)
854 {
855 GET_CURRENT_CONTEXT(ctx);
856
857 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
858
859 if (!pipe) {
860 _mesa_error(ctx, GL_INVALID_OPERATION,
861 "glValidateProgramPipeline(pipeline)");
862 return;
863 }
864
865 _mesa_validate_program_pipeline(ctx, pipe,
866 (ctx->_Shader->Name == pipe->Name));
867 }
868
869 void GLAPIENTRY
870 _mesa_GetProgramPipelineInfoLog(GLuint pipeline, GLsizei bufSize,
871 GLsizei *length, GLchar *infoLog)
872 {
873 GET_CURRENT_CONTEXT(ctx);
874
875 struct gl_pipeline_object *pipe = _mesa_lookup_pipeline_object(ctx, pipeline);
876
877 if (!pipe) {
878 _mesa_error(ctx, GL_INVALID_VALUE,
879 "glGetProgramPipelineInfoLog(pipeline)");
880 return;
881 }
882
883 if (bufSize < 0) {
884 _mesa_error(ctx, GL_INVALID_VALUE,
885 "glGetProgramPipelineInfoLog(bufSize)");
886 return;
887 }
888
889 if (pipe->InfoLog)
890 _mesa_copy_string(infoLog, bufSize, length, pipe->InfoLog);
891 else
892 *length = 0;
893 }