glsl/mesa: split gl_shader in two
[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 shader->Geom.VerticesOut = -1;
96 shader->Geom.InputType = GL_TRIANGLES;
97 shader->Geom.OutputType = GL_TRIANGLE_STRIP;
98 }
99
100 /**
101 * Allocate a new gl_shader object, initialize it.
102 */
103 struct gl_shader *
104 _mesa_new_shader(struct gl_context *ctx, GLuint name, gl_shader_stage stage)
105 {
106 struct gl_shader *shader;
107 shader = rzalloc(NULL, struct gl_shader);
108 if (shader) {
109 shader->Stage = stage;
110 shader->Name = name;
111 _mesa_init_shader(ctx, shader);
112 }
113 return shader;
114 }
115
116
117 /**
118 * Allocate a new gl_linked_shader object.
119 * Called via ctx->Driver.NewShader()
120 */
121 struct gl_linked_shader *
122 _mesa_new_linked_shader(gl_shader_stage stage)
123 {
124 struct gl_linked_shader *shader;
125 shader = rzalloc(NULL, struct gl_linked_shader);
126 if (shader) {
127 shader->Stage = stage;
128 }
129 return shader;
130 }
131
132
133 /**
134 * Delete a shader object.
135 */
136 void
137 _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh)
138 {
139 free((void *)sh->Source);
140 free(sh->Label);
141 ralloc_free(sh);
142 }
143
144
145 /**
146 * Delete a shader object.
147 */
148 void
149 _mesa_delete_linked_shader(struct gl_context *ctx,
150 struct gl_linked_shader *sh)
151 {
152 _mesa_reference_program(ctx, &sh->Program, NULL);
153 ralloc_free(sh);
154 }
155
156
157 /**
158 * Lookup a GLSL shader object.
159 */
160 struct gl_shader *
161 _mesa_lookup_shader(struct gl_context *ctx, GLuint name)
162 {
163 if (name) {
164 struct gl_shader *sh = (struct gl_shader *)
165 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
166 /* Note that both gl_shader and gl_shader_program objects are kept
167 * in the same hash table. Check the object's type to be sure it's
168 * what we're expecting.
169 */
170 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
171 return NULL;
172 }
173 return sh;
174 }
175 return NULL;
176 }
177
178
179 /**
180 * As above, but record an error if shader is not found.
181 */
182 struct gl_shader *
183 _mesa_lookup_shader_err(struct gl_context *ctx, GLuint name, const char *caller)
184 {
185 if (!name) {
186 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
187 return NULL;
188 }
189 else {
190 struct gl_shader *sh = (struct gl_shader *)
191 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
192 if (!sh) {
193 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
194 return NULL;
195 }
196 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
197 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
198 return NULL;
199 }
200 return sh;
201 }
202 }
203
204
205
206 /**********************************************************************/
207 /*** Shader Program object functions ***/
208 /**********************************************************************/
209
210
211 /**
212 * Set ptr to point to shProg.
213 * If ptr is pointing to another object, decrement its refcount (and delete
214 * if refcount hits zero).
215 * Then set ptr to point to shProg, incrementing its refcount.
216 */
217 void
218 _mesa_reference_shader_program_(struct gl_context *ctx,
219 struct gl_shader_program **ptr,
220 struct gl_shader_program *shProg)
221 {
222 assert(ptr);
223 if (*ptr == shProg) {
224 /* no-op */
225 return;
226 }
227 if (*ptr) {
228 /* Unreference the old shader program */
229 GLboolean deleteFlag = GL_FALSE;
230 struct gl_shader_program *old = *ptr;
231
232 assert(old->RefCount > 0);
233 old->RefCount--;
234 deleteFlag = (old->RefCount == 0);
235
236 if (deleteFlag) {
237 if (old->Name != 0)
238 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
239 _mesa_delete_shader_program(ctx, old);
240 }
241
242 *ptr = NULL;
243 }
244 assert(!*ptr);
245
246 if (shProg) {
247 shProg->RefCount++;
248 *ptr = shProg;
249 }
250 }
251
252 static void
253 init_shader_program(struct gl_shader_program *prog)
254 {
255 prog->Type = GL_SHADER_PROGRAM_MESA;
256 prog->RefCount = 1;
257
258 prog->AttributeBindings = string_to_uint_map_ctor();
259 prog->FragDataBindings = string_to_uint_map_ctor();
260 prog->FragDataIndexBindings = string_to_uint_map_ctor();
261
262 prog->Geom.UsesEndPrimitive = false;
263 prog->Geom.UsesStreams = false;
264
265 prog->TransformFeedback.BufferMode = GL_INTERLEAVED_ATTRIBS;
266
267 exec_list_make_empty(&prog->EmptyUniformLocations);
268
269 prog->InfoLog = ralloc_strdup(prog, "");
270 }
271
272 /**
273 * Allocate a new gl_shader_program object, initialize it.
274 */
275 struct gl_shader_program *
276 _mesa_new_shader_program(GLuint name)
277 {
278 struct gl_shader_program *shProg;
279 shProg = rzalloc(NULL, struct gl_shader_program);
280 if (shProg) {
281 shProg->Name = name;
282 init_shader_program(shProg);
283 }
284 return shProg;
285 }
286
287
288 /**
289 * Clear (free) the shader program state that gets produced by linking.
290 */
291 void
292 _mesa_clear_shader_program_data(struct gl_shader_program *shProg)
293 {
294 unsigned i;
295
296 if (shProg->UniformStorage) {
297 for (i = 0; i < shProg->NumUniformStorage; ++i)
298 _mesa_uniform_detach_all_driver_storage(&shProg->UniformStorage[i]);
299 ralloc_free(shProg->UniformStorage);
300 shProg->NumUniformStorage = 0;
301 shProg->UniformStorage = NULL;
302 }
303
304 if (shProg->UniformRemapTable) {
305 ralloc_free(shProg->UniformRemapTable);
306 shProg->NumUniformRemapTable = 0;
307 shProg->UniformRemapTable = NULL;
308 }
309
310 if (shProg->UniformHash) {
311 string_to_uint_map_dtor(shProg->UniformHash);
312 shProg->UniformHash = NULL;
313 }
314
315 assert(shProg->InfoLog != NULL);
316 ralloc_free(shProg->InfoLog);
317 shProg->InfoLog = ralloc_strdup(shProg, "");
318
319 ralloc_free(shProg->UniformBlocks);
320 shProg->UniformBlocks = NULL;
321 shProg->NumUniformBlocks = 0;
322
323 ralloc_free(shProg->ShaderStorageBlocks);
324 shProg->ShaderStorageBlocks = NULL;
325 shProg->NumShaderStorageBlocks = 0;
326
327 ralloc_free(shProg->AtomicBuffers);
328 shProg->AtomicBuffers = NULL;
329 shProg->NumAtomicBuffers = 0;
330
331 if (shProg->ProgramResourceList) {
332 ralloc_free(shProg->ProgramResourceList);
333 shProg->ProgramResourceList = NULL;
334 shProg->NumProgramResourceList = 0;
335 }
336 }
337
338
339 /**
340 * Free all the data that hangs off a shader program object, but not the
341 * object itself.
342 */
343 void
344 _mesa_free_shader_program_data(struct gl_context *ctx,
345 struct gl_shader_program *shProg)
346 {
347 GLuint i;
348 gl_shader_stage sh;
349
350 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
351
352 _mesa_clear_shader_program_data(shProg);
353
354 if (shProg->AttributeBindings) {
355 string_to_uint_map_dtor(shProg->AttributeBindings);
356 shProg->AttributeBindings = NULL;
357 }
358
359 if (shProg->FragDataBindings) {
360 string_to_uint_map_dtor(shProg->FragDataBindings);
361 shProg->FragDataBindings = NULL;
362 }
363
364 if (shProg->FragDataIndexBindings) {
365 string_to_uint_map_dtor(shProg->FragDataIndexBindings);
366 shProg->FragDataIndexBindings = NULL;
367 }
368
369 /* detach shaders */
370 for (i = 0; i < shProg->NumShaders; i++) {
371 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
372 }
373 shProg->NumShaders = 0;
374
375 free(shProg->Shaders);
376 shProg->Shaders = NULL;
377
378 /* Transform feedback varying vars */
379 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
380 free(shProg->TransformFeedback.VaryingNames[i]);
381 }
382 free(shProg->TransformFeedback.VaryingNames);
383 shProg->TransformFeedback.VaryingNames = NULL;
384 shProg->TransformFeedback.NumVarying = 0;
385
386
387 for (sh = 0; sh < MESA_SHADER_STAGES; sh++) {
388 if (shProg->_LinkedShaders[sh] != NULL) {
389 _mesa_delete_linked_shader(ctx, shProg->_LinkedShaders[sh]);
390 shProg->_LinkedShaders[sh] = NULL;
391 }
392 }
393
394 free(shProg->Label);
395 shProg->Label = NULL;
396 }
397
398
399 /**
400 * Free/delete a shader program object.
401 */
402 void
403 _mesa_delete_shader_program(struct gl_context *ctx,
404 struct gl_shader_program *shProg)
405 {
406 _mesa_free_shader_program_data(ctx, shProg);
407
408 ralloc_free(shProg);
409 }
410
411
412 /**
413 * Lookup a GLSL program object.
414 */
415 struct gl_shader_program *
416 _mesa_lookup_shader_program(struct gl_context *ctx, GLuint name)
417 {
418 struct gl_shader_program *shProg;
419 if (name) {
420 shProg = (struct gl_shader_program *)
421 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
422 /* Note that both gl_shader and gl_shader_program objects are kept
423 * in the same hash table. Check the object's type to be sure it's
424 * what we're expecting.
425 */
426 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
427 return NULL;
428 }
429 return shProg;
430 }
431 return NULL;
432 }
433
434
435 /**
436 * As above, but record an error if program is not found.
437 */
438 struct gl_shader_program *
439 _mesa_lookup_shader_program_err(struct gl_context *ctx, GLuint name,
440 const char *caller)
441 {
442 if (!name) {
443 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
444 return NULL;
445 }
446 else {
447 struct gl_shader_program *shProg = (struct gl_shader_program *)
448 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
449 if (!shProg) {
450 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
451 return NULL;
452 }
453 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
454 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
455 return NULL;
456 }
457 return shProg;
458 }
459 }
460
461
462 void
463 _mesa_init_shader_object_functions(struct dd_function_table *driver)
464 {
465 driver->NewShader = _mesa_new_linked_shader;
466 driver->LinkShader = _mesa_ir_link_shader;
467 }