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