mesa: Support EXT_framebuffer_blit targets in ES 3.0 as well.
[mesa.git] / src / mesa / main / shaderobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file shaderobj.c
27 * \author Brian Paul
28 *
29 */
30
31
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/hash.h"
35 #include "main/mfeatures.h"
36 #include "main/mtypes.h"
37 #include "main/shaderobj.h"
38 #include "main/uniforms.h"
39 #include "program/program.h"
40 #include "program/prog_parameter.h"
41 #include "program/hash_table.h"
42 #include "ralloc.h"
43
44 /**********************************************************************/
45 /*** Shader object functions ***/
46 /**********************************************************************/
47
48
49 /**
50 * Set ptr to point to sh.
51 * If ptr is pointing to another shader, decrement its refcount (and delete
52 * if refcount hits zero).
53 * Then set ptr to point to sh, incrementing its refcount.
54 */
55 void
56 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
57 struct gl_shader *sh)
58 {
59 assert(ptr);
60 if (*ptr == sh) {
61 /* no-op */
62 return;
63 }
64 if (*ptr) {
65 /* Unreference the old shader */
66 GLboolean deleteFlag = GL_FALSE;
67 struct gl_shader *old = *ptr;
68
69 ASSERT(old->RefCount > 0);
70 old->RefCount--;
71 /*printf("SHADER DECR %p (%d) to %d\n",
72 (void*) old, old->Name, old->RefCount);*/
73 deleteFlag = (old->RefCount == 0);
74
75 if (deleteFlag) {
76 if (old->Name != 0)
77 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
78 ctx->Driver.DeleteShader(ctx, old);
79 }
80
81 *ptr = NULL;
82 }
83 assert(!*ptr);
84
85 if (sh) {
86 /* reference new */
87 sh->RefCount++;
88 /*printf("SHADER INCR %p (%d) to %d\n",
89 (void*) sh, sh->Name, sh->RefCount);*/
90 *ptr = sh;
91 }
92 }
93
94 void
95 _mesa_init_shader(struct gl_context *ctx, struct gl_shader *shader)
96 {
97 shader->RefCount = 1;
98 }
99
100 /**
101 * Allocate a new gl_shader object, initialize it.
102 * Called via ctx->Driver.NewShader()
103 */
104 struct gl_shader *
105 _mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
106 {
107 struct gl_shader *shader;
108 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER ||
109 type == GL_GEOMETRY_SHADER_ARB);
110 shader = rzalloc(NULL, struct gl_shader);
111 if (shader) {
112 shader->Type = type;
113 shader->Name = name;
114 _mesa_init_shader(ctx, shader);
115 }
116 return shader;
117 }
118
119
120 /**
121 * Delete a shader object.
122 * Called via ctx->Driver.DeleteShader().
123 */
124 static void
125 _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
126 {
127 free((void *)sh->Source);
128 _mesa_reference_program(ctx, &sh->Program, NULL);
129 ralloc_free(sh);
130 }
131
132
133 /**
134 * Lookup a GLSL shader object.
135 */
136 struct gl_shader *
137 _mesa_lookup_shader(struct gl_context *ctx, GLuint name)
138 {
139 if (name) {
140 struct gl_shader *sh = (struct gl_shader *)
141 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
142 /* Note that both gl_shader and gl_shader_program objects are kept
143 * in the same hash table. Check the object's type to be sure it's
144 * what we're expecting.
145 */
146 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
147 return NULL;
148 }
149 return sh;
150 }
151 return NULL;
152 }
153
154
155 /**
156 * As above, but record an error if shader is not found.
157 */
158 struct gl_shader *
159 _mesa_lookup_shader_err(struct gl_context *ctx, GLuint name, const char *caller)
160 {
161 if (!name) {
162 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
163 return NULL;
164 }
165 else {
166 struct gl_shader *sh = (struct gl_shader *)
167 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
168 if (!sh) {
169 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
170 return NULL;
171 }
172 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
173 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
174 return NULL;
175 }
176 return sh;
177 }
178 }
179
180
181
182 /**********************************************************************/
183 /*** Shader Program object functions ***/
184 /**********************************************************************/
185
186
187 /**
188 * Set ptr to point to shProg.
189 * If ptr is pointing to another object, decrement its refcount (and delete
190 * if refcount hits zero).
191 * Then set ptr to point to shProg, incrementing its refcount.
192 */
193 void
194 _mesa_reference_shader_program(struct gl_context *ctx,
195 struct gl_shader_program **ptr,
196 struct gl_shader_program *shProg)
197 {
198 assert(ptr);
199 if (*ptr == shProg) {
200 /* no-op */
201 return;
202 }
203 if (*ptr) {
204 /* Unreference the old shader program */
205 GLboolean deleteFlag = GL_FALSE;
206 struct gl_shader_program *old = *ptr;
207
208 ASSERT(old->RefCount > 0);
209 old->RefCount--;
210 #if 0
211 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
212 (void *) old, old->Name, old->RefCount);
213 #endif
214 deleteFlag = (old->RefCount == 0);
215
216 if (deleteFlag) {
217 if (old->Name != 0)
218 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
219 ctx->Driver.DeleteShaderProgram(ctx, old);
220 }
221
222 *ptr = NULL;
223 }
224 assert(!*ptr);
225
226 if (shProg) {
227 shProg->RefCount++;
228 #if 0
229 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
230 (void *) shProg, shProg->Name, shProg->RefCount);
231 #endif
232 *ptr = shProg;
233 }
234 }
235
236 void
237 _mesa_init_shader_program(struct gl_context *ctx, struct gl_shader_program *prog)
238 {
239 prog->Type = GL_SHADER_PROGRAM_MESA;
240 prog->RefCount = 1;
241
242 prog->AttributeBindings = string_to_uint_map_ctor();
243 prog->FragDataBindings = string_to_uint_map_ctor();
244 prog->FragDataIndexBindings = string_to_uint_map_ctor();
245
246 prog->Geom.VerticesOut = 0;
247 prog->Geom.InputType = GL_TRIANGLES;
248 prog->Geom.OutputType = GL_TRIANGLE_STRIP;
249
250 prog->InfoLog = ralloc_strdup(prog, "");
251 }
252
253 /**
254 * Allocate a new gl_shader_program object, initialize it.
255 * Called via ctx->Driver.NewShaderProgram()
256 */
257 static struct gl_shader_program *
258 _mesa_new_shader_program(struct gl_context *ctx, GLuint name)
259 {
260 struct gl_shader_program *shProg;
261 shProg = rzalloc(NULL, struct gl_shader_program);
262 if (shProg) {
263 shProg->Name = name;
264 _mesa_init_shader_program(ctx, shProg);
265 }
266 return shProg;
267 }
268
269
270 /**
271 * Clear (free) the shader program state that gets produced by linking.
272 */
273 void
274 _mesa_clear_shader_program_data(struct gl_context *ctx,
275 struct gl_shader_program *shProg)
276 {
277 if (shProg->UniformStorage) {
278 unsigned i;
279 for (i = 0; i < shProg->NumUserUniformStorage; ++i)
280 _mesa_uniform_detach_all_driver_storage(&shProg->UniformStorage[i]);
281 ralloc_free(shProg->UniformStorage);
282 shProg->NumUserUniformStorage = 0;
283 shProg->UniformStorage = NULL;
284 }
285
286 if (shProg->UniformHash) {
287 string_to_uint_map_dtor(shProg->UniformHash);
288 shProg->UniformHash = NULL;
289 }
290
291 assert(shProg->InfoLog != NULL);
292 ralloc_free(shProg->InfoLog);
293 shProg->InfoLog = ralloc_strdup(shProg, "");
294 }
295
296
297 /**
298 * Free all the data that hangs off a shader program object, but not the
299 * object itself.
300 */
301 void
302 _mesa_free_shader_program_data(struct gl_context *ctx,
303 struct gl_shader_program *shProg)
304 {
305 GLuint i;
306 gl_shader_type sh;
307
308 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
309
310 _mesa_clear_shader_program_data(ctx, shProg);
311
312 if (shProg->AttributeBindings) {
313 string_to_uint_map_dtor(shProg->AttributeBindings);
314 shProg->AttributeBindings = NULL;
315 }
316
317 if (shProg->FragDataBindings) {
318 string_to_uint_map_dtor(shProg->FragDataBindings);
319 shProg->FragDataBindings = NULL;
320 }
321
322 if (shProg->FragDataIndexBindings) {
323 string_to_uint_map_dtor(shProg->FragDataIndexBindings);
324 shProg->FragDataIndexBindings = NULL;
325 }
326
327 /* detach shaders */
328 for (i = 0; i < shProg->NumShaders; i++) {
329 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
330 }
331 shProg->NumShaders = 0;
332
333 free(shProg->Shaders);
334 shProg->Shaders = NULL;
335
336 /* Transform feedback varying vars */
337 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
338 free(shProg->TransformFeedback.VaryingNames[i]);
339 }
340 free(shProg->TransformFeedback.VaryingNames);
341 shProg->TransformFeedback.VaryingNames = NULL;
342 shProg->TransformFeedback.NumVarying = 0;
343
344
345 for (sh = 0; sh < MESA_SHADER_TYPES; sh++) {
346 if (shProg->_LinkedShaders[sh] != NULL) {
347 ctx->Driver.DeleteShader(ctx, shProg->_LinkedShaders[sh]);
348 shProg->_LinkedShaders[sh] = NULL;
349 }
350 }
351 }
352
353
354 /**
355 * Free/delete a shader program object.
356 * Called via ctx->Driver.DeleteShaderProgram().
357 */
358 static void
359 _mesa_delete_shader_program(struct gl_context *ctx, struct gl_shader_program *shProg)
360 {
361 _mesa_free_shader_program_data(ctx, shProg);
362
363 ralloc_free(shProg);
364 }
365
366
367 /**
368 * Lookup a GLSL program object.
369 */
370 struct gl_shader_program *
371 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
372 {
373 struct gl_shader_program *shProg;
374 if (name) {
375 shProg = (struct gl_shader_program *)
376 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
377 /* Note that both gl_shader and gl_shader_program objects are kept
378 * in the same hash table. Check the object's type to be sure it's
379 * what we're expecting.
380 */
381 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
382 return NULL;
383 }
384 return shProg;
385 }
386 return NULL;
387 }
388
389
390 /**
391 * As above, but record an error if program is not found.
392 */
393 struct gl_shader_program *
394 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
395 const char *caller)
396 {
397 if (!name) {
398 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
399 return NULL;
400 }
401 else {
402 struct gl_shader_program *shProg = (struct gl_shader_program *)
403 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
404 if (!shProg) {
405 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
406 return NULL;
407 }
408 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
409 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
410 return NULL;
411 }
412 return shProg;
413 }
414 }
415
416
417 void
418 _mesa_init_shader_object_functions(struct dd_function_table *driver)
419 {
420 driver->NewShader = _mesa_new_shader;
421 driver->DeleteShader = _mesa_delete_shader;
422 driver->NewShaderProgram = _mesa_new_shader_program;
423 driver->DeleteShaderProgram = _mesa_delete_shader_program;
424 driver->LinkShader = _mesa_ir_link_shader;
425 }