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