xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/linker_util.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2018 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 #include <ctype.h>
25 
26 #include "glsl_types.h"
27 #include "linker_util.h"
28 #include "util/bitscan.h"
29 #include "util/set.h"
30 #include "ir_uniform.h" /* for gl_uniform_storage */
31 #include "main/shader_types.h"
32 #include "main/consts_exts.h"
33 
34 /**
35  * Given a string identifying a program resource, break it into a base name
36  * and an optional array index in square brackets.
37  *
38  * If an array index is present, \c out_base_name_end is set to point to the
39  * "[" that precedes the array index, and the array index itself is returned
40  * as a long.
41  *
42  * If no array index is present (or if the array index is negative or
43  * mal-formed), \c out_base_name_end, is set to point to the null terminator
44  * at the end of the input string, and -1 is returned.
45  *
46  * Only the final array index is parsed; if the string contains other array
47  * indices (or structure field accesses), they are left in the base name.
48  *
49  * No attempt is made to check that the base name is properly formed;
50  * typically the caller will look up the base name in a hash table, so
51  * ill-formed base names simply turn into hash table lookup failures.
52  */
53 long
link_util_parse_program_resource_name(const GLchar * name,const size_t len,const GLchar ** out_base_name_end)54 link_util_parse_program_resource_name(const GLchar *name, const size_t len,
55                                       const GLchar **out_base_name_end)
56 {
57    /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
58     *
59     *     "When an integer array element or block instance number is part of
60     *     the name string, it will be specified in decimal form without a "+"
61     *     or "-" sign or any extra leading zeroes. Additionally, the name
62     *     string will not include white space anywhere in the string."
63     */
64 
65    *out_base_name_end = name + len;
66 
67    if (len == 0 || name[len-1] != ']')
68       return -1;
69 
70    /* Walk backwards over the string looking for a non-digit character.  This
71     * had better be the opening bracket for an array index.
72     *
73     * Initially, i specifies the location of the ']'.  Since the string may
74     * contain only the ']' charcater, walk backwards very carefully.
75     */
76    unsigned i;
77    for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
78       /* empty */ ;
79 
80    if ((i == 0) || name[i-1] != '[')
81       return -1;
82 
83    long array_index = strtol(&name[i], NULL, 10);
84    if (array_index < 0)
85       return -1;
86 
87    /* Check for leading zero */
88    if (name[i] == '0' && name[i+1] != ']')
89       return -1;
90 
91    *out_base_name_end = name + (i - 1);
92    return array_index;
93 }
94 
95 /* Utility methods shared between the GLSL IR and the NIR */
96 
97 /* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources:
98  *
99  *    "For an active shader storage block member declared as an array of an
100  *     aggregate type, an entry will be generated only for the first array
101  *     element, regardless of its type. Such block members are referred to as
102  *     top-level arrays. If the block member is an aggregate type, the
103  *     enumeration rules are then applied recursively."
104  */
105 bool
link_util_should_add_buffer_variable(struct gl_shader_program * prog,struct gl_uniform_storage * uniform,int top_level_array_base_offset,int top_level_array_size_in_bytes,int second_element_offset,int block_index)106 link_util_should_add_buffer_variable(struct gl_shader_program *prog,
107                                      struct gl_uniform_storage *uniform,
108                                      int top_level_array_base_offset,
109                                      int top_level_array_size_in_bytes,
110                                      int second_element_offset,
111                                      int block_index)
112 {
113    /* If the uniform is not a shader storage buffer or is not an array return
114     * true.
115     */
116    if (!uniform->is_shader_storage || top_level_array_size_in_bytes == 0)
117       return true;
118 
119    int after_top_level_array = top_level_array_base_offset +
120       top_level_array_size_in_bytes;
121 
122    /* Check for a new block, or that we are not dealing with array elements of
123     * a top member array other than the first element.
124     */
125    if (block_index != uniform->block_index ||
126        uniform->offset >= after_top_level_array ||
127        uniform->offset < second_element_offset) {
128       return true;
129    }
130 
131    return false;
132 }
133 
134 bool
link_util_add_program_resource(struct gl_shader_program * prog,struct set * resource_set,GLenum type,const void * data,uint8_t stages)135 link_util_add_program_resource(struct gl_shader_program *prog,
136                                struct set *resource_set,
137                                GLenum type, const void *data, uint8_t stages)
138 {
139    assert(data);
140 
141    /* If resource already exists, do not add it again. */
142    if (_mesa_set_search(resource_set, data))
143       return true;
144 
145    prog->data->ProgramResourceList =
146       reralloc(prog->data,
147                prog->data->ProgramResourceList,
148                gl_program_resource,
149                prog->data->NumProgramResourceList + 1);
150 
151    if (!prog->data->ProgramResourceList) {
152       linker_error(prog, "Out of memory during linking.\n");
153       return false;
154    }
155 
156    struct gl_program_resource *res =
157       &prog->data->ProgramResourceList[prog->data->NumProgramResourceList];
158 
159    res->Type = type;
160    res->Data = data;
161    res->StageReferences = stages;
162 
163    prog->data->NumProgramResourceList++;
164 
165    _mesa_set_add(resource_set, data);
166 
167    return true;
168 }
169 
170 /**
171  * Search through the list of empty blocks to find one that fits the current
172  * uniform.
173  */
174 int
link_util_find_empty_block(struct gl_shader_program * prog,struct gl_uniform_storage * uniform)175 link_util_find_empty_block(struct gl_shader_program *prog,
176                            struct gl_uniform_storage *uniform)
177 {
178    const unsigned entries = MAX2(1, uniform->array_elements);
179 
180    foreach_list_typed(struct empty_uniform_block, block, link,
181                       &prog->EmptyUniformLocations) {
182       /* Found a block with enough slots to fit the uniform */
183       if (block->slots == entries) {
184          unsigned start = block->start;
185          exec_node_remove(&block->link);
186          ralloc_free(block);
187 
188          return start;
189       /* Found a block with more slots than needed. It can still be used. */
190       } else if (block->slots > entries) {
191          unsigned start = block->start;
192          block->start += entries;
193          block->slots -= entries;
194 
195          return start;
196       }
197    }
198 
199    return -1;
200 }
201 
202 void
link_util_update_empty_uniform_locations(struct gl_shader_program * prog)203 link_util_update_empty_uniform_locations(struct gl_shader_program *prog)
204 {
205    struct empty_uniform_block *current_block = NULL;
206 
207    for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
208       /* We found empty space in UniformRemapTable. */
209       if (prog->UniformRemapTable[i] == NULL) {
210          /* We've found the beginning of a new continous block of empty slots */
211          if (!current_block || current_block->start + current_block->slots != i) {
212             current_block = rzalloc(prog, struct empty_uniform_block);
213             current_block->start = i;
214             exec_list_push_tail(&prog->EmptyUniformLocations,
215                                 &current_block->link);
216          }
217 
218          /* The current block continues, so we simply increment its slots */
219          current_block->slots++;
220       }
221    }
222 }
223 
224 void
link_util_check_subroutine_resources(struct gl_shader_program * prog)225 link_util_check_subroutine_resources(struct gl_shader_program *prog)
226 {
227    unsigned mask = prog->data->linked_stages;
228    while (mask) {
229       const int i = u_bit_scan(&mask);
230       struct gl_program *p = prog->_LinkedShaders[i]->Program;
231 
232       if (p->sh.NumSubroutineUniformRemapTable > MAX_SUBROUTINE_UNIFORM_LOCATIONS) {
233          linker_error(prog, "Too many %s shader subroutine uniforms\n",
234                       _mesa_shader_stage_to_string(i));
235       }
236    }
237 }
238 
239 #if defined(_MSC_VER) && DETECT_ARCH_AARCH64
240 // Work around https://developercommunity.visualstudio.com/t/Incorrect-ARM64-codegen-with-optimizatio/10564605
241 #pragma optimize("", off)
242 #endif
243 /**
244  * Validate uniform resources used by a program versus the implementation limits
245  */
246 void
link_util_check_uniform_resources(const struct gl_constants * consts,struct gl_shader_program * prog)247 link_util_check_uniform_resources(const struct gl_constants *consts,
248                                   struct gl_shader_program *prog)
249 {
250    unsigned total_uniform_blocks = 0;
251    unsigned total_shader_storage_blocks = 0;
252 
253    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
254       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
255 
256       if (sh == NULL)
257          continue;
258 
259       if (sh->num_uniform_components >
260           consts->Program[i].MaxUniformComponents) {
261          if (consts->GLSLSkipStrictMaxUniformLimitCheck) {
262             linker_warning(prog, "Too many %s shader default uniform block "
263                            "components, but the driver will try to optimize "
264                            "them out; this is non-portable out-of-spec "
265                            "behavior\n",
266                            _mesa_shader_stage_to_string(i));
267          } else {
268             linker_error(prog, "Too many %s shader default uniform block "
269                          "components\n",
270                          _mesa_shader_stage_to_string(i));
271          }
272       }
273 
274       if (sh->num_combined_uniform_components >
275           consts->Program[i].MaxCombinedUniformComponents) {
276          if (consts->GLSLSkipStrictMaxUniformLimitCheck) {
277             linker_warning(prog, "Too many %s shader uniform components, "
278                            "but the driver will try to optimize them out; "
279                            "this is non-portable out-of-spec behavior\n",
280                            _mesa_shader_stage_to_string(i));
281          } else {
282             linker_error(prog, "Too many %s shader uniform components\n",
283                          _mesa_shader_stage_to_string(i));
284          }
285       }
286 
287       total_shader_storage_blocks += sh->Program->info.num_ssbos;
288       total_uniform_blocks += sh->Program->info.num_ubos;
289    }
290 
291    if (total_uniform_blocks > consts->MaxCombinedUniformBlocks) {
292       linker_error(prog, "Too many combined uniform blocks (%d/%d)\n",
293                    total_uniform_blocks, consts->MaxCombinedUniformBlocks);
294    }
295 
296    if (total_shader_storage_blocks > consts->MaxCombinedShaderStorageBlocks) {
297       linker_error(prog, "Too many combined shader storage blocks (%d/%d)\n",
298                    total_shader_storage_blocks,
299                    consts->MaxCombinedShaderStorageBlocks);
300    }
301 
302    for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
303       if (prog->data->UniformBlocks[i].UniformBufferSize >
304           consts->MaxUniformBlockSize) {
305          linker_error(prog, "Uniform block %s too big (%d/%d)\n",
306                       prog->data->UniformBlocks[i].name.string,
307                       prog->data->UniformBlocks[i].UniformBufferSize,
308                       consts->MaxUniformBlockSize);
309       }
310    }
311 
312    for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
313       if (prog->data->ShaderStorageBlocks[i].UniformBufferSize >
314           consts->MaxShaderStorageBlockSize) {
315          linker_error(prog, "Shader storage block %s too big (%d/%d)\n",
316                       prog->data->ShaderStorageBlocks[i].name.string,
317                       prog->data->ShaderStorageBlocks[i].UniformBufferSize,
318                       consts->MaxShaderStorageBlockSize);
319       }
320    }
321 }
322 #if defined(_MSC_VER) && DETECT_ARCH_AARCH64
323 #pragma optimize("", on)
324 #endif
325 
326 void
link_util_calculate_subroutine_compat(struct gl_shader_program * prog)327 link_util_calculate_subroutine_compat(struct gl_shader_program *prog)
328 {
329    unsigned mask = prog->data->linked_stages;
330    while (mask) {
331       const int i = u_bit_scan(&mask);
332       struct gl_program *p = prog->_LinkedShaders[i]->Program;
333 
334       for (unsigned j = 0; j < p->sh.NumSubroutineUniformRemapTable; j++) {
335          if (p->sh.SubroutineUniformRemapTable[j] == INACTIVE_UNIFORM_EXPLICIT_LOCATION)
336             continue;
337 
338          struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[j];
339 
340          if (!uni)
341             continue;
342 
343          int count = 0;
344          if (p->sh.NumSubroutineFunctions == 0) {
345             linker_error(prog, "subroutine uniform %s defined but no valid functions found\n", glsl_get_type_name(uni->type));
346             continue;
347          }
348          for (unsigned f = 0; f < p->sh.NumSubroutineFunctions; f++) {
349             struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[f];
350             for (int k = 0; k < fn->num_compat_types; k++) {
351                if (fn->types[k] == uni->type) {
352                   count++;
353                   break;
354                }
355             }
356          }
357          uni->num_compatible_subroutines = count;
358       }
359    }
360 }
361 
362 /**
363  * Recursive part of the public mark_array_elements_referenced function.
364  *
365  * The recursion occurs when an entire array-of- is accessed.  See the
366  * implementation for more details.
367  *
368  * \param dr                List of array_deref_range elements to be
369  *                          processed.
370  * \param count             Number of array_deref_range elements to be
371  *                          processed.
372  * \param scale             Current offset scale.
373  * \param linearized_index  Current accumulated linearized array index.
374  */
375 void
_mark_array_elements_referenced(const struct array_deref_range * dr,unsigned count,unsigned scale,unsigned linearized_index,BITSET_WORD * bits)376 _mark_array_elements_referenced(const struct array_deref_range *dr,
377                                 unsigned count, unsigned scale,
378                                 unsigned linearized_index,
379                                 BITSET_WORD *bits)
380 {
381    /* Walk through the list of array dereferences in least- to
382     * most-significant order.  Along the way, accumulate the current
383     * linearized offset and the scale factor for each array-of-.
384     */
385    for (unsigned i = 0; i < count; i++) {
386       if (dr[i].index < dr[i].size) {
387          linearized_index += dr[i].index * scale;
388          scale *= dr[i].size;
389       } else {
390          /* For each element in the current array, update the count and
391           * offset, then recurse to process the remaining arrays.
392           *
393           * There is some inefficency here if the last eBITSET_WORD *bitslement in the
394           * array_deref_range list specifies the entire array.  In that case,
395           * the loop will make recursive calls with count == 0.  In the call,
396           * all that will happen is the bit will be set.
397           */
398          for (unsigned j = 0; j < dr[i].size; j++) {
399             _mark_array_elements_referenced(&dr[i + 1],
400                                             count - (i + 1),
401                                             scale * dr[i].size,
402                                             linearized_index + (j * scale),
403                                             bits);
404          }
405 
406          return;
407       }
408    }
409 
410    BITSET_SET(bits, linearized_index);
411 }
412 
413 /**
414  * Mark a set of array elements as accessed.
415  *
416  * If every \c array_deref_range is for a single index, only a single
417  * element will be marked.  If any \c array_deref_range is for an entire
418  * array-of-, then multiple elements will be marked.
419  *
420  * Items in the \c array_deref_range list appear in least- to
421  * most-significant order.  This is the \b opposite order the indices
422  * appear in the GLSL shader text.  An array access like
423  *
424  *     x = y[1][i][3];
425  *
426  * would appear as
427  *
428  *     { { 3, n }, { m, m }, { 1, p } }
429  *
430  * where n, m, and p are the sizes of the arrays-of-arrays.
431  *
432  * The set of marked array elements can later be queried by
433  * \c ::is_linearized_index_referenced.
434  *
435  * \param dr     List of array_deref_range elements to be processed.
436  * \param count  Number of array_deref_range elements to be processed.
437  */
438 void
link_util_mark_array_elements_referenced(const struct array_deref_range * dr,unsigned count,unsigned array_depth,BITSET_WORD * bits)439 link_util_mark_array_elements_referenced(const struct array_deref_range *dr,
440                                          unsigned count, unsigned array_depth,
441                                          BITSET_WORD *bits)
442 {
443    if (count != array_depth)
444       return;
445 
446    _mark_array_elements_referenced(dr, count, 1, 0, bits);
447 }
448 
449 const char *
interpolation_string(unsigned interpolation)450 interpolation_string(unsigned interpolation)
451 {
452    switch (interpolation) {
453    case INTERP_MODE_NONE:          return "no";
454    case INTERP_MODE_SMOOTH:        return "smooth";
455    case INTERP_MODE_FLAT:          return "flat";
456    case INTERP_MODE_NOPERSPECTIVE: return "noperspective";
457    }
458 
459    assert(!"Should not get here.");
460    return "";
461 }
462