mesa/sso: Add gl_pipeline_object::InfoLog support
[mesa.git] / src / mesa / main / pipelineobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright © 2013 Gregory Hainaut <gregory.hainaut@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 /**
27 * \file pipelineobj.c
28 * \author Hainaut Gregory <gregory.hainaut@gmail.com>
29 *
30 * Implementation of pipeline object related API functions. Based on
31 * GL_ARB_separate_shader_objects extension.
32 */
33
34 #include "main/glheader.h"
35 #include "main/context.h"
36 #include "main/dispatch.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/transformfeedback.h"
44 #include "main/uniforms.h"
45 #include "program/program.h"
46 #include "program/prog_parameter.h"
47 #include "ralloc.h"
48 #include <stdbool.h>
49 #include "../glsl/glsl_parser_extras.h"
50 #include "../glsl/ir_uniform.h"
51
52 /**
53 * Delete a pipeline object.
54 */
55 void
56 _mesa_delete_pipeline_object(struct gl_context *ctx,
57 struct gl_pipeline_object *obj)
58 {
59 unsigned i;
60
61 _mesa_reference_shader_program(ctx, &obj->_CurrentFragmentProgram, NULL);
62
63 for (i = 0; i < MESA_SHADER_STAGES; i++)
64 _mesa_reference_shader_program(ctx, &obj->CurrentProgram[i], NULL);
65
66 _mesa_reference_shader_program(ctx, &obj->ActiveProgram, NULL);
67 mtx_destroy(&obj->Mutex);
68 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 mtx_init(&obj->Mutex, mtx_plain);
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(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_HashDeleteAll(ctx->Pipeline.Objects, delete_pipelineobj_cb, ctx);
124 _mesa_DeleteHashTable(ctx->Pipeline.Objects);
125
126 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
127 _mesa_delete_pipeline_object(ctx, ctx->Pipeline.Default);
128
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 static inline struct gl_pipeline_object *
140 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_HashLookup(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_HashInsert(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_HashRemove(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 GLboolean deleteFlag = GL_FALSE;
187 struct gl_pipeline_object *oldObj = *ptr;
188
189 mtx_lock(&oldObj->Mutex);
190 ASSERT(oldObj->RefCount > 0);
191 oldObj->RefCount--;
192 deleteFlag = (oldObj->RefCount == 0);
193 mtx_unlock(&oldObj->Mutex);
194
195 if (deleteFlag) {
196 _mesa_delete_pipeline_object(ctx, oldObj);
197 }
198
199 *ptr = NULL;
200 }
201 ASSERT(!*ptr);
202
203 if (obj) {
204 /* reference new pipeline object */
205 mtx_lock(&obj->Mutex);
206 if (obj->RefCount == 0) {
207 /* this pipeline's being deleted (look just above) */
208 /* Not sure this can ever really happen. Warn if it does. */
209 _mesa_problem(NULL, "referencing deleted pipeline object");
210 *ptr = NULL;
211 }
212 else {
213 obj->RefCount++;
214 *ptr = obj;
215 }
216 mtx_unlock(&obj->Mutex);
217 }
218 }
219
220 /**
221 * Bound program to severals stages of the pipeline
222 */
223 void GLAPIENTRY
224 _mesa_UseProgramStages(GLuint pipeline, GLbitfield stages, GLuint program)
225 {
226 GET_CURRENT_CONTEXT(ctx);
227
228 struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
229 struct gl_shader_program *shProg = NULL;
230
231 if (!pipe) {
232 _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgramStages(pipeline)");
233 return;
234 }
235
236 /* Object is created by any Pipeline call but glGenProgramPipelines,
237 * glIsProgramPipeline and GetProgramPipelineInfoLog
238 */
239 pipe->EverBound = GL_TRUE;
240
241 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec says:
242 *
243 * "If stages is not the special value ALL_SHADER_BITS, and has a bit
244 * set that is not recognized, the error INVALID_VALUE is generated."
245 *
246 * NOT YET SUPPORTED:
247 * GL_TESS_CONTROL_SHADER_BIT
248 * GL_TESS_EVALUATION_SHADER_BIT
249 */
250 GLbitfield any_valid_stages = GL_VERTEX_SHADER_BIT | GL_FRAGMENT_SHADER_BIT;
251 if (_mesa_has_geometry_shaders(ctx))
252 any_valid_stages |= GL_GEOMETRY_SHADER_BIT;
253
254 if (stages != GL_ALL_SHADER_BITS && (stages & ~any_valid_stages) != 0) {
255 _mesa_error(ctx, GL_INVALID_VALUE, "glUseProgramStages(Stages)");
256 return;
257 }
258
259 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
260 * spec says:
261 *
262 * "The error INVALID_OPERATION is generated:
263 *
264 * ...
265 *
266 * - by UseProgramStages if the program pipeline object it refers
267 * to is current and the current transform feedback object is
268 * active and not paused;
269 */
270 if (ctx->_Shader == pipe) {
271 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
272 _mesa_error(ctx, GL_INVALID_OPERATION,
273 "glUseProgramStages(transform feedback active)");
274 return;
275 }
276 }
277
278 if (program) {
279 shProg = _mesa_lookup_shader_program_err(ctx, program,
280 "glUseProgramStages");
281 if (shProg == NULL)
282 return;
283
284 /* Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
285 * says:
286 *
287 * "If the program object named by program was linked without the
288 * PROGRAM_SEPARABLE parameter set, or was not linked successfully,
289 * the error INVALID_OPERATION is generated and the corresponding
290 * shader stages in the pipeline program pipeline object are not
291 * modified."
292 */
293 if (!shProg->LinkStatus) {
294 _mesa_error(ctx, GL_INVALID_OPERATION,
295 "glUseProgramStages(program not linked)");
296 return;
297 }
298
299 if (!shProg->SeparateShader) {
300 _mesa_error(ctx, GL_INVALID_OPERATION,
301 "glUseProgramStages(program wasn't linked with the "
302 "PROGRAM_SEPARABLE flag)");
303 return;
304 }
305 }
306
307 /* Enable individual stages from the program as requested by the
308 * application. If there is no shader for a requested stage in the
309 * program, _mesa_use_shader_program will enable fixed-function processing
310 * as dictated by the spec.
311 *
312 * Section 2.11.4 (Program Pipeline Objects) of the OpenGL 4.1 spec
313 * says:
314 *
315 * "If UseProgramStages is called with program set to zero or with a
316 * program object that contains no executable code for the given
317 * stages, it is as if the pipeline object has no programmable stage
318 * configured for the indicated shader stages."
319 */
320 if ((stages & GL_VERTEX_SHADER_BIT) != 0)
321 _mesa_use_shader_program(ctx, GL_VERTEX_SHADER, shProg, pipe);
322
323 if ((stages & GL_FRAGMENT_SHADER_BIT) != 0)
324 _mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER, shProg, pipe);
325
326 if ((stages & GL_GEOMETRY_SHADER_BIT) != 0)
327 _mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER, shProg, pipe);
328 }
329
330 /**
331 * Use the named shader program for subsequent glUniform calls (if pipeline
332 * bound)
333 */
334 void GLAPIENTRY
335 _mesa_ActiveShaderProgram(GLuint pipeline, GLuint program)
336 {
337 GET_CURRENT_CONTEXT(ctx);
338 struct gl_shader_program *shProg = NULL;
339 struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
340
341 if (program != 0) {
342 shProg = _mesa_lookup_shader_program_err(ctx, program,
343 "glActiveShaderProgram(program)");
344 if (shProg == NULL)
345 return;
346 }
347
348 if (!pipe) {
349 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveShaderProgram(pipeline)");
350 return;
351 }
352
353 /* Object is created by any Pipeline call but glGenProgramPipelines,
354 * glIsProgramPipeline and GetProgramPipelineInfoLog
355 */
356 pipe->EverBound = GL_TRUE;
357
358 if ((shProg != NULL) && !shProg->LinkStatus) {
359 _mesa_error(ctx, GL_INVALID_OPERATION,
360 "glActiveShaderProgram(program %u not linked)", shProg->Name);
361 return;
362 }
363
364 _mesa_reference_shader_program(ctx, &pipe->ActiveProgram, shProg);
365 }
366
367 /**
368 * Make program of the pipeline current
369 */
370 void GLAPIENTRY
371 _mesa_BindProgramPipeline(GLuint pipeline)
372 {
373 GET_CURRENT_CONTEXT(ctx);
374 struct gl_pipeline_object *newObj = NULL;
375
376 /* Rebinding the same pipeline object: no change.
377 */
378 if (ctx->_Shader->Name == pipeline)
379 return;
380
381 /* Section 2.17.2 (Transform Feedback Primitive Capture) of the OpenGL 4.1
382 * spec says:
383 *
384 * "The error INVALID_OPERATION is generated:
385 *
386 * ...
387 *
388 * - by BindProgramPipeline if the current transform feedback
389 * object is active and not paused;
390 */
391 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
392 _mesa_error(ctx, GL_INVALID_OPERATION,
393 "glBindProgramPipeline(transform feedback active)");
394 return;
395 }
396
397 /* Get pointer to new pipeline object (newObj)
398 */
399 if (pipeline) {
400 /* non-default pipeline object */
401 newObj = lookup_pipeline_object(ctx, pipeline);
402 if (!newObj) {
403 _mesa_error(ctx, GL_INVALID_OPERATION,
404 "glBindProgramPipeline(non-gen name)");
405 return;
406 }
407
408 /* Object is created by any Pipeline call but glGenProgramPipelines,
409 * glIsProgramPipeline and GetProgramPipelineInfoLog
410 */
411 newObj->EverBound = GL_TRUE;
412 }
413
414 /* First bind the Pipeline to pipeline binding point */
415 _mesa_reference_pipeline_object(ctx, &ctx->Pipeline.Current, newObj);
416
417 /* Section 2.11.3 (Program Objects) of the OpenGL 4.1 spec says:
418 *
419 * "If there is a current program object established by UseProgram,
420 * that program is considered current for all stages. Otherwise, if
421 * there is a bound program pipeline object (see section 2.11.4), the
422 * program bound to the appropriate stage of the pipeline object is
423 * considered current."
424 */
425 if (&ctx->Shader != ctx->_Shader) {
426 if (pipeline) {
427 /* Bound the pipeline to the current program and
428 * restore the pipeline state
429 */
430 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, newObj);
431 } else {
432 /* Unbind the pipeline */
433 _mesa_reference_pipeline_object(ctx, &ctx->_Shader,
434 ctx->Pipeline.Default);
435 }
436
437 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
438
439 if (ctx->Driver.UseProgram)
440 ctx->Driver.UseProgram(ctx, NULL);
441 }
442 }
443
444 /**
445 * Delete a set of pipeline objects.
446 *
447 * \param n Number of pipeline objects to delete.
448 * \param ids pipeline of \c n pipeline object IDs.
449 */
450 void GLAPIENTRY
451 _mesa_DeleteProgramPipelines(GLsizei n, const GLuint *pipelines)
452 {
453 GET_CURRENT_CONTEXT(ctx);
454 GLsizei i;
455
456 if (n < 0) {
457 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgramPipelines(n<0)");
458 return;
459 }
460
461 for (i = 0; i < n; i++) {
462 struct gl_pipeline_object *obj =
463 lookup_pipeline_object(ctx, pipelines[i]);
464
465 if (obj) {
466 ASSERT(obj->Name == pipelines[i]);
467
468 /* If the pipeline object is currently bound, the spec says "If an
469 * object that is currently bound is deleted, the binding for that
470 * object reverts to zero and no program pipeline object becomes
471 * current."
472 */
473 if (obj == ctx->Pipeline.Current) {
474 _mesa_BindProgramPipeline(0);
475 }
476
477 /* The ID is immediately freed for re-use */
478 remove_pipeline_object(ctx, obj);
479
480 /* Unreference the pipeline object.
481 * If refcount hits zero, the object will be deleted.
482 */
483 _mesa_reference_pipeline_object(ctx, &obj, NULL);
484 }
485 }
486 }
487
488 /**
489 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
490 * \param n Number of IDs to generate.
491 * \param pipelines pipeline of \c n locations to store the IDs.
492 */
493 void GLAPIENTRY
494 _mesa_GenProgramPipelines(GLsizei n, GLuint *pipelines)
495 {
496 GET_CURRENT_CONTEXT(ctx);
497
498 GLuint first;
499 GLint i;
500
501 if (n < 0) {
502 _mesa_error(ctx, GL_INVALID_VALUE, "glGenProgramPipelines(n<0)");
503 return;
504 }
505
506 if (!pipelines) {
507 return;
508 }
509
510 first = _mesa_HashFindFreeKeyBlock(ctx->Pipeline.Objects, n);
511
512 for (i = 0; i < n; i++) {
513 struct gl_pipeline_object *obj;
514 GLuint name = first + i;
515
516 obj = _mesa_new_pipeline_object(ctx, name);
517 if (!obj) {
518 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenProgramPipelines");
519 return;
520 }
521
522 save_pipeline_object(ctx, obj);
523 pipelines[i] = first + i;
524 }
525
526 }
527
528 /**
529 * Determine if ID is the name of an pipeline object.
530 *
531 * \param id ID of the potential pipeline object.
532 * \return \c GL_TRUE if \c id is the name of a pipeline object,
533 * \c GL_FALSE otherwise.
534 */
535 GLboolean GLAPIENTRY
536 _mesa_IsProgramPipeline(GLuint pipeline)
537 {
538 GET_CURRENT_CONTEXT(ctx);
539
540 struct gl_pipeline_object *obj = lookup_pipeline_object(ctx, pipeline);
541 if (obj == NULL)
542 return GL_FALSE;
543
544 return obj->EverBound;
545 }
546
547 /**
548 * glGetProgramPipelineiv() - get pipeline shader state.
549 */
550 void GLAPIENTRY
551 _mesa_GetProgramPipelineiv(GLuint pipeline, GLenum pname, GLint *params)
552 {
553 GET_CURRENT_CONTEXT(ctx);
554 struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
555
556 /* Are geometry shaders available in this context?
557 */
558 const bool has_gs = _mesa_has_geometry_shaders(ctx);
559
560 if (!pipe) {
561 _mesa_error(ctx, GL_INVALID_OPERATION,
562 "glGetProgramPipelineiv(pipeline)");
563 return;
564 }
565
566 /* Object is created by any Pipeline call but glGenProgramPipelines,
567 * glIsProgramPipeline and GetProgramPipelineInfoLog
568 */
569 pipe->EverBound = GL_TRUE;
570
571 switch (pname) {
572 case GL_ACTIVE_PROGRAM:
573 *params = pipe->ActiveProgram ? pipe->ActiveProgram->Name : 0;
574 return;
575 case GL_INFO_LOG_LENGTH:
576 *params = pipe->InfoLog ? strlen(pipe->InfoLog) + 1 : 0;
577 return;
578 case GL_VALIDATE_STATUS:
579 /* FINISHME: Implement validation status.
580 */
581 *params = 0;
582 return;
583 case GL_VERTEX_SHADER:
584 *params = pipe->CurrentProgram[MESA_SHADER_VERTEX]
585 ? pipe->CurrentProgram[MESA_SHADER_VERTEX]->Name : 0;
586 return;
587 case GL_TESS_EVALUATION_SHADER:
588 /* NOT YET SUPPORTED */
589 break;
590 case GL_TESS_CONTROL_SHADER:
591 /* NOT YET SUPPORTED */
592 break;
593 case GL_GEOMETRY_SHADER:
594 if (!has_gs)
595 break;
596 *params = pipe->CurrentProgram[MESA_SHADER_GEOMETRY]
597 ? pipe->CurrentProgram[MESA_SHADER_GEOMETRY]->Name : 0;
598 return;
599 case GL_FRAGMENT_SHADER:
600 *params = pipe->CurrentProgram[MESA_SHADER_FRAGMENT]
601 ? pipe->CurrentProgram[MESA_SHADER_FRAGMENT]->Name : 0;
602 return;
603 default:
604 break;
605 }
606
607 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramPipelineiv(pname=%s)",
608 _mesa_lookup_enum_by_nr(pname));
609 }
610
611 /**
612 * Check compatibility of pipeline's program
613 */
614 void GLAPIENTRY
615 _mesa_ValidateProgramPipeline(GLuint pipeline)
616 {
617 }
618
619 void GLAPIENTRY
620 _mesa_GetProgramPipelineInfoLog(GLuint pipeline, GLsizei bufSize,
621 GLsizei *length, GLchar *infoLog)
622 {
623 GET_CURRENT_CONTEXT(ctx);
624
625 struct gl_pipeline_object *pipe = lookup_pipeline_object(ctx, pipeline);
626
627 if (!pipe) {
628 _mesa_error(ctx, GL_INVALID_VALUE,
629 "glGetProgramPipelineInfoLog(pipeline)");
630 return;
631 }
632
633 if (bufSize < 0) {
634 _mesa_error(ctx, GL_INVALID_VALUE,
635 "glGetProgramPipelineInfoLog(bufSize)");
636 return;
637 }
638
639 if (pipe->InfoLog)
640 _mesa_copy_string(infoLog, bufSize, length, pipe->InfoLog);
641 else
642 *length = 0;
643 }