mesa/sso: Implement _mesa_IsProgramPipeline
[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 unsinged 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 _glthread_DESTROY_MUTEX(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 _glthread_INIT_MUTEX(obj->Mutex);
81 obj->RefCount = 1;
82 obj->Flags = _mesa_get_shader_flags();
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
99
100 /**
101 * Callback for deleting a pipeline object. Called by _mesa_HashDeleteAll().
102 */
103 static void
104 delete_pipelineobj_cb(GLuint id, void *data, void *userData)
105 {
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);
109 }
110
111
112 /**
113 * Free pipeline state for given context.
114 */
115 void
116 _mesa_free_pipeline_data(struct gl_context *ctx)
117 {
118 _mesa_HashDeleteAll(ctx->Pipeline.Objects, delete_pipelineobj_cb, ctx);
119 _mesa_DeleteHashTable(ctx->Pipeline.Objects);
120 }
121
122 /**
123 * Look up the pipeline object for the given ID.
124 *
125 * \returns
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
128 * non-existent.
129 */
130 static inline struct gl_pipeline_object *
131 lookup_pipeline_object(struct gl_context *ctx, GLuint id)
132 {
133 if (id == 0)
134 return NULL;
135 else
136 return (struct gl_pipeline_object *)
137 _mesa_HashLookup(ctx->Pipeline.Objects, id);
138 }
139
140 /**
141 * Add the given pipeline object to the pipeline object pool.
142 */
143 static void
144 save_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
145 {
146 if (obj->Name > 0) {
147 _mesa_HashInsert(ctx->Pipeline.Objects, obj->Name, obj);
148 }
149 }
150
151 /**
152 * Remove the given pipeline object from the pipeline object pool.
153 * Do not deallocate the pipeline object though.
154 */
155 static void
156 remove_pipeline_object(struct gl_context *ctx, struct gl_pipeline_object *obj)
157 {
158 if (obj->Name > 0) {
159 _mesa_HashRemove(ctx->Pipeline.Objects, obj->Name);
160 }
161 }
162
163 /**
164 * Set ptr to obj w/ reference counting.
165 * Note: this should only be called from the _mesa_reference_pipeline_object()
166 * inline function.
167 */
168 void
169 _mesa_reference_pipeline_object_(struct gl_context *ctx,
170 struct gl_pipeline_object **ptr,
171 struct gl_pipeline_object *obj)
172 {
173 assert(*ptr != obj);
174
175 if (*ptr) {
176 /* Unreference the old pipeline object */
177 GLboolean deleteFlag = GL_FALSE;
178 struct gl_pipeline_object *oldObj = *ptr;
179
180 _glthread_LOCK_MUTEX(oldObj->Mutex);
181 ASSERT(oldObj->RefCount > 0);
182 oldObj->RefCount--;
183 deleteFlag = (oldObj->RefCount == 0);
184 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
185
186 if (deleteFlag) {
187 _mesa_delete_pipeline_object(ctx, oldObj);
188 }
189
190 *ptr = NULL;
191 }
192 ASSERT(!*ptr);
193
194 if (obj) {
195 /* reference new pipeline object */
196 _glthread_LOCK_MUTEX(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");
201 *ptr = NULL;
202 }
203 else {
204 obj->RefCount++;
205 *ptr = obj;
206 }
207 _glthread_UNLOCK_MUTEX(obj->Mutex);
208 }
209 }
210
211 /**
212 * Bound program to severals stages of the pipeline
213 */
214 void GLAPIENTRY
215 _mesa_UseProgramStages(GLuint pipeline, GLbitfield stages, GLuint program)
216 {
217 }
218
219 /**
220 * Use the named shader program for subsequent glUniform calls (if pipeline
221 * bound)
222 */
223 void GLAPIENTRY
224 _mesa_ActiveShaderProgram(GLuint pipeline, GLuint program)
225 {
226 }
227
228 /**
229 * Make program of the pipeline current
230 */
231 void GLAPIENTRY
232 _mesa_BindProgramPipeline(GLuint pipeline)
233 {
234 }
235
236 /**
237 * Delete a set of pipeline objects.
238 *
239 * \param n Number of pipeline objects to delete.
240 * \param ids pipeline of \c n pipeline object IDs.
241 */
242 void GLAPIENTRY
243 _mesa_DeleteProgramPipelines(GLsizei n, const GLuint *pipelines)
244 {
245 GET_CURRENT_CONTEXT(ctx);
246 GLsizei i;
247
248 if (n < 0) {
249 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgramPipelines(n<0)");
250 return;
251 }
252
253 for (i = 0; i < n; i++) {
254 struct gl_pipeline_object *obj =
255 lookup_pipeline_object(ctx, pipelines[i]);
256
257 if (obj) {
258 ASSERT(obj->Name == pipelines[i]);
259
260 /* If the pipeline object is currently bound, the spec says "If an
261 * object that is currently bound is deleted, the binding for that
262 * object reverts to zero and no program pipeline object becomes
263 * current."
264 */
265 if (obj == ctx->Pipeline.Current) {
266 _mesa_BindProgramPipeline(0);
267 }
268
269 /* The ID is immediately freed for re-use */
270 remove_pipeline_object(ctx, obj);
271
272 /* Unreference the pipeline object.
273 * If refcount hits zero, the object will be deleted.
274 */
275 _mesa_reference_pipeline_object(ctx, &obj, NULL);
276 }
277 }
278 }
279
280 /**
281 * Generate a set of unique pipeline object IDs and store them in \c pipelines.
282 * \param n Number of IDs to generate.
283 * \param pipelines pipeline of \c n locations to store the IDs.
284 */
285 void GLAPIENTRY
286 _mesa_GenProgramPipelines(GLsizei n, GLuint *pipelines)
287 {
288 GET_CURRENT_CONTEXT(ctx);
289
290 GLuint first;
291 GLint i;
292
293 if (n < 0) {
294 _mesa_error(ctx, GL_INVALID_VALUE, "glGenProgramPipelines(n<0)");
295 return;
296 }
297
298 if (!pipelines) {
299 return;
300 }
301
302 first = _mesa_HashFindFreeKeyBlock(ctx->Pipeline.Objects, n);
303
304 for (i = 0; i < n; i++) {
305 struct gl_pipeline_object *obj;
306 GLuint name = first + i;
307
308 obj = _mesa_new_pipeline_object(ctx, name);
309 if (!obj) {
310 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenProgramPipelines");
311 return;
312 }
313
314 save_pipeline_object(ctx, obj);
315 pipelines[i] = first + i;
316 }
317
318 }
319
320 /**
321 * Determine if ID is the name of an pipeline object.
322 *
323 * \param id ID of the potential pipeline object.
324 * \return \c GL_TRUE if \c id is the name of a pipeline object,
325 * \c GL_FALSE otherwise.
326 */
327 GLboolean GLAPIENTRY
328 _mesa_IsProgramPipeline(GLuint pipeline)
329 {
330 GET_CURRENT_CONTEXT(ctx);
331
332 struct gl_pipeline_object *obj = lookup_pipeline_object(ctx, pipeline);
333 if (obj == NULL)
334 return GL_FALSE;
335
336 return obj->EverBound;
337 }
338
339 /**
340 * glGetProgramPipelineiv() - get pipeline shader state.
341 */
342 void GLAPIENTRY
343 _mesa_GetProgramPipelineiv(GLuint pipeline, GLenum pname, GLint *params)
344 {
345 }
346
347 /**
348 * Check compatibility of pipeline's program
349 */
350 void GLAPIENTRY
351 _mesa_ValidateProgramPipeline(GLuint pipeline)
352 {
353 }
354
355 void GLAPIENTRY
356 _mesa_GetProgramPipelineInfoLog(GLuint pipeline, GLsizei bufSize,
357 GLsizei *length, GLchar *infoLog)
358 {
359 }