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