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