mesa/glsl: stop using GL shader type internally
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file shaderobj.c
28 * \author Brian Paul
29 *
30 */
31
32
33 #include "main/glheader.h"
34 #include "main/context.h"
35 #include "main/hash.h"
36 #include "main/mtypes.h"
37 #include "main/shaderapi.h"
38 #include "main/shaderobj.h"
39 #include "main/uniforms.h"
40 #include "program/program.h"
41 #include "program/prog_parameter.h"
42 #include "program/hash_table.h"
43 #include "util/ralloc.h"
44
45 /**********************************************************************/
46 /*** Shader object functions ***/
47 /**********************************************************************/
48
49
50 /**
51 * Set ptr to point to sh.
52 * If ptr is pointing to another shader, decrement its refcount (and delete
53 * if refcount hits zero).
54 * Then set ptr to point to sh, incrementing its refcount.
55 */
56 void
57 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
58 struct gl_shader *sh)
59 {
60 assert(ptr);
61 if (*ptr == sh) {
62 /* no-op */
63 return;
64 }
65 if (*ptr) {
66 /* Unreference the old shader */
67 GLboolean deleteFlag = GL_FALSE;
68 struct gl_shader *old = *ptr;
69
70 assert(old->RefCount > 0);
71 old->RefCount--;
72 deleteFlag = (old->RefCount == 0);
73
74 if (deleteFlag) {
75 if (old->Name != 0)
76 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
77 _mesa_delete_shader(ctx, old);
78 }
79
80 *ptr = NULL;
81 }
82 assert(!*ptr);
83
84 if (sh) {
85 /* reference new */
86 sh->RefCount++;
87 *ptr = sh;
88 }
89 }
90
91 void
92 _mesa_init_shader(struct gl_context *ctx, struct gl_shader *shader)
93 {
94 shader->RefCount = 1;
95 }
96
97 /**
98 * Allocate a new gl_shader object, initialize it.
99 * Called via ctx->Driver.NewShader()
100 */
101 struct gl_shader *
102 _mesa_new_shader(struct gl_context *ctx, GLuint name, gl_shader_stage stage)
103 {
104 struct gl_shader *shader;
105 shader = rzalloc(NULL, struct gl_shader);
106 if (shader) {
107 shader->Stage = stage;
108 shader->Name = name;
109 _mesa_init_shader(ctx, shader);
110 }
111 return shader;
112 }
113
114
115 /**
116 * Delete a shader object.
117 */
118 void
119 _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
120 {
121 free((void *)sh->Source);
122 free(sh->Label);
123 _mesa_reference_program(ctx, &sh->Program, NULL);
124 ralloc_free(sh);
125 }
126
127
128 /**
129 * Lookup a GLSL shader object.
130 */
131 struct gl_shader *
132 _mesa_lookup_shader(struct gl_context *ctx, GLuint name)
133 {
134 if (name) {
135 struct gl_shader *sh = (struct gl_shader *)
136 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
137 /* Note that both gl_shader and gl_shader_program objects are kept
138 * in the same hash table. Check the object's type to be sure it's
139 * what we're expecting.
140 */
141 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
142 return NULL;
143 }
144 return sh;
145 }
146 return NULL;
147 }
148
149
150 /**
151 * As above, but record an error if shader is not found.
152 */
153 struct gl_shader *
154 _mesa_lookup_shader_err(struct gl_context *ctx, GLuint name, const char *caller)
155 {
156 if (!name) {
157 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
158 return NULL;
159 }
160 else {
161 struct gl_shader *sh = (struct gl_shader *)
162 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
163 if (!sh) {
164 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
165 return NULL;
166 }
167 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
168 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
169 return NULL;
170 }
171 return sh;
172 }
173 }
174
175
176
177 /**********************************************************************/
178 /*** Shader Program object functions ***/
179 /**********************************************************************/
180
181
182 /**
183 * Set ptr to point to shProg.
184 * If ptr is pointing to another object, decrement its refcount (and delete
185 * if refcount hits zero).
186 * Then set ptr to point to shProg, incrementing its refcount.
187 */
188 void
189 _mesa_reference_shader_program_(struct gl_context *ctx,
190 struct gl_shader_program **ptr,
191 struct gl_shader_program *shProg)
192 {
193 assert(ptr);
194 if (*ptr == shProg) {
195 /* no-op */
196 return;
197 }
198 if (*ptr) {
199 /* Unreference the old shader program */
200 GLboolean deleteFlag = GL_FALSE;
201 struct gl_shader_program *old = *ptr;
202
203 assert(old->RefCount > 0);
204 old->RefCount--;
205 deleteFlag = (old->RefCount == 0);
206
207 if (deleteFlag) {
208 if (old->Name != 0)
209 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
210 _mesa_delete_shader_program(ctx, old);
211 }
212
213 *ptr = NULL;
214 }
215 assert(!*ptr);
216
217 if (shProg) {
218 shProg->RefCount++;
219 *ptr = shProg;
220 }
221 }
222
223 static void
224 init_shader_program(struct gl_shader_program *prog)
225 {
226 prog->Type = GL_SHADER_PROGRAM_MESA;
227 prog->RefCount = 1;
228
229 prog->AttributeBindings = string_to_uint_map_ctor();
230 prog->FragDataBindings = string_to_uint_map_ctor();
231 prog->FragDataIndexBindings = string_to_uint_map_ctor();
232
233 prog->Geom.VerticesOut = -1;
234 prog->Geom.InputType = GL_TRIANGLES;
235 prog->Geom.OutputType = GL_TRIANGLE_STRIP;
236 prog->Geom.UsesEndPrimitive = false;
237 prog->Geom.UsesStreams = false;
238
239 prog->TransformFeedback.BufferMode = GL_INTERLEAVED_ATTRIBS;
240
241 exec_list_make_empty(&prog->EmptyUniformLocations);
242
243 prog->InfoLog = ralloc_strdup(prog, "");
244 }
245
246 /**
247 * Allocate a new gl_shader_program object, initialize it.
248 */
249 struct gl_shader_program *
250 _mesa_new_shader_program(GLuint name)
251 {
252 struct gl_shader_program *shProg;
253 shProg = rzalloc(NULL, struct gl_shader_program);
254 if (shProg) {
255 shProg->Name = name;
256 init_shader_program(shProg);
257 }
258 return shProg;
259 }
260
261
262 /**
263 * Clear (free) the shader program state that gets produced by linking.
264 */
265 void
266 _mesa_clear_shader_program_data(struct gl_shader_program *shProg)
267 {
268 unsigned i;
269
270 if (shProg->UniformStorage) {
271 for (i = 0; i < shProg->NumUniformStorage; ++i)
272 _mesa_uniform_detach_all_driver_storage(&shProg->UniformStorage[i]);
273 ralloc_free(shProg->UniformStorage);
274 shProg->NumUniformStorage = 0;
275 shProg->UniformStorage = NULL;
276 }
277
278 if (shProg->UniformRemapTable) {
279 ralloc_free(shProg->UniformRemapTable);
280 shProg->NumUniformRemapTable = 0;
281 shProg->UniformRemapTable = NULL;
282 }
283
284 if (shProg->UniformHash) {
285 string_to_uint_map_dtor(shProg->UniformHash);
286 shProg->UniformHash = NULL;
287 }
288
289 assert(shProg->InfoLog != NULL);
290 ralloc_free(shProg->InfoLog);
291 shProg->InfoLog = ralloc_strdup(shProg, "");
292
293 ralloc_free(shProg->UniformBlocks);
294 shProg->UniformBlocks = NULL;
295 shProg->NumUniformBlocks = 0;
296
297 ralloc_free(shProg->ShaderStorageBlocks);
298 shProg->ShaderStorageBlocks = NULL;
299 shProg->NumShaderStorageBlocks = 0;
300
301 ralloc_free(shProg->AtomicBuffers);
302 shProg->AtomicBuffers = NULL;
303 shProg->NumAtomicBuffers = 0;
304
305 if (shProg->ProgramResourceList) {
306 ralloc_free(shProg->ProgramResourceList);
307 shProg->ProgramResourceList = NULL;
308 shProg->NumProgramResourceList = 0;
309 }
310 }
311
312
313 /**
314 * Free all the data that hangs off a shader program object, but not the
315 * object itself.
316 */
317 void
318 _mesa_free_shader_program_data(struct gl_context *ctx,
319 struct gl_shader_program *shProg)
320 {
321 GLuint i;
322 gl_shader_stage sh;
323
324 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
325
326 _mesa_clear_shader_program_data(shProg);
327
328 if (shProg->AttributeBindings) {
329 string_to_uint_map_dtor(shProg->AttributeBindings);
330 shProg->AttributeBindings = NULL;
331 }
332
333 if (shProg->FragDataBindings) {
334 string_to_uint_map_dtor(shProg->FragDataBindings);
335 shProg->FragDataBindings = NULL;
336 }
337
338 if (shProg->FragDataIndexBindings) {
339 string_to_uint_map_dtor(shProg->FragDataIndexBindings);
340 shProg->FragDataIndexBindings = NULL;
341 }
342
343 /* detach shaders */
344 for (i = 0; i < shProg->NumShaders; i++) {
345 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
346 }
347 shProg->NumShaders = 0;
348
349 free(shProg->Shaders);
350 shProg->Shaders = NULL;
351
352 /* Transform feedback varying vars */
353 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
354 free(shProg->TransformFeedback.VaryingNames[i]);
355 }
356 free(shProg->TransformFeedback.VaryingNames);
357 shProg->TransformFeedback.VaryingNames = NULL;
358 shProg->TransformFeedback.NumVarying = 0;
359
360
361 for (sh = 0; sh < MESA_SHADER_STAGES; sh++) {
362 if (shProg->_LinkedShaders[sh] != NULL) {
363 _mesa_delete_shader(ctx, shProg->_LinkedShaders[sh]);
364 shProg->_LinkedShaders[sh] = NULL;
365 }
366 }
367
368 free(shProg->Label);
369 shProg->Label = NULL;
370 }
371
372
373 /**
374 * Free/delete a shader program object.
375 */
376 void
377 _mesa_delete_shader_program(struct gl_context *ctx,
378 struct gl_shader_program *shProg)
379 {
380 _mesa_free_shader_program_data(ctx, shProg);
381
382 ralloc_free(shProg);
383 }
384
385
386 /**
387 * Lookup a GLSL program object.
388 */
389 struct gl_shader_program *
390 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
391 {
392 struct gl_shader_program *shProg;
393 if (name) {
394 shProg = (struct gl_shader_program *)
395 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
396 /* Note that both gl_shader and gl_shader_program objects are kept
397 * in the same hash table. Check the object's type to be sure it's
398 * what we're expecting.
399 */
400 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
401 return NULL;
402 }
403 return shProg;
404 }
405 return NULL;
406 }
407
408
409 /**
410 * As above, but record an error if program is not found.
411 */
412 struct gl_shader_program *
413 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
414 const char *caller)
415 {
416 if (!name) {
417 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
418 return NULL;
419 }
420 else {
421 struct gl_shader_program *shProg = (struct gl_shader_program *)
422 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
423 if (!shProg) {
424 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
425 return NULL;
426 }
427 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
428 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
429 return NULL;
430 }
431 return shProg;
432 }
433 }
434
435
436 void
437 _mesa_init_shader_object_functions(struct dd_function_table *driver)
438 {
439 driver->NewShader = _mesa_new_shader;
440 driver->LinkShader = _mesa_ir_link_shader;
441 }