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