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