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