2 * Mesa 3-D graphics library
4 * Copyright © 2013 Gregory Hainaut <gregory.hainaut@gmail.com>
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:
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
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
28 * \author Hainaut Gregory <gregory.hainaut@gmail.com>
30 * Implementation of pipeline object related API functions. Based on
31 * GL_ARB_separate_shader_objects extension.
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"
49 #include "../glsl/glsl_parser_extras.h"
50 #include "../glsl/ir_uniform.h"
53 * Delete a pipeline object.
56 _mesa_delete_pipeline_object(struct gl_context
*ctx
,
57 struct gl_pipeline_object
*obj
)
61 _mesa_reference_shader_program(ctx
, &obj
->_CurrentFragmentProgram
, NULL
);
63 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
64 _mesa_reference_shader_program(ctx
, &obj
->CurrentProgram
[i
], NULL
);
66 _mesa_reference_shader_program(ctx
, &obj
->ActiveProgram
, NULL
);
67 mtx_destroy(&obj
->Mutex
);
72 * Allocate and initialize a new pipeline object.
74 static struct gl_pipeline_object
*
75 _mesa_new_pipeline_object(struct gl_context
*ctx
, GLuint name
)
77 struct gl_pipeline_object
*obj
= rzalloc(NULL
, struct gl_pipeline_object
);
80 mtx_init(&obj
->Mutex
, mtx_plain
);
82 obj
->Flags
= _mesa_get_shader_flags();
89 * Initialize pipeline object state for given context.
92 _mesa_init_pipeline(struct gl_context
*ctx
)
94 ctx
->Pipeline
.Objects
= _mesa_NewHashTable();
96 ctx
->Pipeline
.Current
= NULL
;
101 * Callback for deleting a pipeline object. Called by _mesa_HashDeleteAll().
104 delete_pipelineobj_cb(GLuint id
, void *data
, void *userData
)
106 struct gl_pipeline_object
*obj
= (struct gl_pipeline_object
*) data
;
107 struct gl_context
*ctx
= (struct gl_context
*) userData
;
108 _mesa_delete_pipeline_object(ctx
, obj
);
113 * Free pipeline state for given context.
116 _mesa_free_pipeline_data(struct gl_context
*ctx
)
118 _mesa_HashDeleteAll(ctx
->Pipeline
.Objects
, delete_pipelineobj_cb
, ctx
);
119 _mesa_DeleteHashTable(ctx
->Pipeline
.Objects
);
123 * Look up the pipeline object for the given ID.
126 * Either a pointer to the pipeline object with the specified ID or \c NULL for
127 * a non-existent ID. The spec defines ID 0 as being technically
130 static inline struct gl_pipeline_object
*
131 lookup_pipeline_object(struct gl_context
*ctx
, GLuint id
)
136 return (struct gl_pipeline_object
*)
137 _mesa_HashLookup(ctx
->Pipeline
.Objects
, id
);
141 * Add the given pipeline object to the pipeline object pool.
144 save_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
147 _mesa_HashInsert(ctx
->Pipeline
.Objects
, obj
->Name
, obj
);
152 * Remove the given pipeline object from the pipeline object pool.
153 * Do not deallocate the pipeline object though.
156 remove_pipeline_object(struct gl_context
*ctx
, struct gl_pipeline_object
*obj
)
159 _mesa_HashRemove(ctx
->Pipeline
.Objects
, obj
->Name
);
164 * Set ptr to obj w/ reference counting.
165 * Note: this should only be called from the _mesa_reference_pipeline_object()
169 _mesa_reference_pipeline_object_(struct gl_context
*ctx
,
170 struct gl_pipeline_object
**ptr
,
171 struct gl_pipeline_object
*obj
)
176 /* Unreference the old pipeline object */
177 GLboolean deleteFlag
= GL_FALSE
;
178 struct gl_pipeline_object
*oldObj
= *ptr
;
180 mtx_lock(&oldObj
->Mutex
);
181 ASSERT(oldObj
->RefCount
> 0);
183 deleteFlag
= (oldObj
->RefCount
== 0);
184 mtx_unlock(&oldObj
->Mutex
);
187 _mesa_delete_pipeline_object(ctx
, oldObj
);
195 /* reference new pipeline object */
196 mtx_lock(&obj
->Mutex
);
197 if (obj
->RefCount
== 0) {
198 /* this pipeline's being deleted (look just above) */
199 /* Not sure this can ever really happen. Warn if it does. */
200 _mesa_problem(NULL
, "referencing deleted pipeline object");
207 mtx_unlock(&obj
->Mutex
);
212 * Bound program to severals stages of the pipeline
215 _mesa_UseProgramStages(GLuint pipeline
, GLbitfield stages
, GLuint program
)
220 * Use the named shader program for subsequent glUniform calls (if pipeline
224 _mesa_ActiveShaderProgram(GLuint pipeline
, GLuint program
)
226 GET_CURRENT_CONTEXT(ctx
);
227 struct gl_shader_program
*shProg
= NULL
;
228 struct gl_pipeline_object
*pipe
= lookup_pipeline_object(ctx
, pipeline
);
231 shProg
= _mesa_lookup_shader_program_err(ctx
, program
,
232 "glActiveShaderProgram(program)");
238 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveShaderProgram(pipeline)");
242 /* Object is created by any Pipeline call but glGenProgramPipelines,
243 * glIsProgramPipeline and GetProgramPipelineInfoLog
245 pipe
->EverBound
= GL_TRUE
;
247 if ((shProg
!= NULL
) && !shProg
->LinkStatus
) {
248 _mesa_error(ctx
, GL_INVALID_OPERATION
,
249 "glActiveShaderProgram(program %u not linked)", shProg
->Name
);
253 _mesa_reference_shader_program(ctx
, &pipe
->ActiveProgram
, shProg
);
257 * Make program of the pipeline current
260 _mesa_BindProgramPipeline(GLuint pipeline
)
265 * Delete a set of pipeline objects.
267 * \param n Number of pipeline objects to delete.
268 * \param ids pipeline of \c n pipeline object IDs.
271 _mesa_DeleteProgramPipelines(GLsizei n
, const GLuint
*pipelines
)
273 GET_CURRENT_CONTEXT(ctx
);
277 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteProgramPipelines(n<0)");
281 for (i
= 0; i
< n
; i
++) {
282 struct gl_pipeline_object
*obj
=
283 lookup_pipeline_object(ctx
, pipelines
[i
]);
286 ASSERT(obj
->Name
== pipelines
[i
]);
288 /* If the pipeline object is currently bound, the spec says "If an
289 * object that is currently bound is deleted, the binding for that
290 * object reverts to zero and no program pipeline object becomes
293 if (obj
== ctx
->Pipeline
.Current
) {
294 _mesa_BindProgramPipeline(0);
297 /* The ID is immediately freed for re-use */
298 remove_pipeline_object(ctx
, obj
);
300 /* Unreference the pipeline object.
301 * If refcount hits zero, the object will be deleted.
303 _mesa_reference_pipeline_object(ctx
, &obj
, NULL
);
309 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
310 * \param n Number of IDs to generate.
311 * \param pipelines pipeline of \c n locations to store the IDs.
314 _mesa_GenProgramPipelines(GLsizei n
, GLuint
*pipelines
)
316 GET_CURRENT_CONTEXT(ctx
);
322 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenProgramPipelines(n<0)");
330 first
= _mesa_HashFindFreeKeyBlock(ctx
->Pipeline
.Objects
, n
);
332 for (i
= 0; i
< n
; i
++) {
333 struct gl_pipeline_object
*obj
;
334 GLuint name
= first
+ i
;
336 obj
= _mesa_new_pipeline_object(ctx
, name
);
338 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenProgramPipelines");
342 save_pipeline_object(ctx
, obj
);
343 pipelines
[i
] = first
+ i
;
349 * Determine if ID is the name of an pipeline object.
351 * \param id ID of the potential pipeline object.
352 * \return \c GL_TRUE if \c id is the name of a pipeline object,
353 * \c GL_FALSE otherwise.
356 _mesa_IsProgramPipeline(GLuint pipeline
)
358 GET_CURRENT_CONTEXT(ctx
);
360 struct gl_pipeline_object
*obj
= lookup_pipeline_object(ctx
, pipeline
);
364 return obj
->EverBound
;
368 * glGetProgramPipelineiv() - get pipeline shader state.
371 _mesa_GetProgramPipelineiv(GLuint pipeline
, GLenum pname
, GLint
*params
)
373 GET_CURRENT_CONTEXT(ctx
);
374 struct gl_pipeline_object
*pipe
= lookup_pipeline_object(ctx
, pipeline
);
376 /* Are geometry shaders available in this context?
378 const bool has_gs
= _mesa_has_geometry_shaders(ctx
);
381 _mesa_error(ctx
, GL_INVALID_OPERATION
,
382 "glGetProgramPipelineiv(pipeline)");
386 /* Object is created by any Pipeline call but glGenProgramPipelines,
387 * glIsProgramPipeline and GetProgramPipelineInfoLog
389 pipe
->EverBound
= GL_TRUE
;
392 case GL_ACTIVE_PROGRAM
:
393 *params
= pipe
->ActiveProgram
? pipe
->ActiveProgram
->Name
: 0;
395 case GL_INFO_LOG_LENGTH
:
396 /* FINISHME: Implement the info log.
400 case GL_VALIDATE_STATUS
:
401 /* FINISHME: Implement validation status.
405 case GL_VERTEX_SHADER
:
406 *params
= pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]
407 ? pipe
->CurrentProgram
[MESA_SHADER_VERTEX
]->Name
: 0;
409 case GL_TESS_EVALUATION_SHADER
:
410 /* NOT YET SUPPORTED */
412 case GL_TESS_CONTROL_SHADER
:
413 /* NOT YET SUPPORTED */
415 case GL_GEOMETRY_SHADER
:
418 *params
= pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]
419 ? pipe
->CurrentProgram
[MESA_SHADER_GEOMETRY
]->Name
: 0;
421 case GL_FRAGMENT_SHADER
:
422 *params
= pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]
423 ? pipe
->CurrentProgram
[MESA_SHADER_FRAGMENT
]->Name
: 0;
429 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetProgramPipelineiv(pname=%s)",
430 _mesa_lookup_enum_by_nr(pname
));
434 * Check compatibility of pipeline's program
437 _mesa_ValidateProgramPipeline(GLuint pipeline
)
442 _mesa_GetProgramPipelineInfoLog(GLuint pipeline
, GLsizei bufSize
,
443 GLsizei
*length
, GLchar
*infoLog
)