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