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 ¤t_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 /**
240 * Validate uniform resources used by a program versus the implementation limits
241 */
242 void
link_util_check_uniform_resources(const struct gl_constants * consts,struct gl_shader_program * prog)243 link_util_check_uniform_resources(const struct gl_constants *consts,
244 struct gl_shader_program *prog)
245 {
246 unsigned total_uniform_blocks = 0;
247 unsigned total_shader_storage_blocks = 0;
248
249 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
250 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
251
252 if (sh == NULL)
253 continue;
254
255 if (sh->num_uniform_components >
256 consts->Program[i].MaxUniformComponents) {
257 if (consts->GLSLSkipStrictMaxUniformLimitCheck) {
258 linker_warning(prog, "Too many %s shader default uniform block "
259 "components, but the driver will try to optimize "
260 "them out; this is non-portable out-of-spec "
261 "behavior\n",
262 _mesa_shader_stage_to_string(i));
263 } else {
264 linker_error(prog, "Too many %s shader default uniform block "
265 "components\n",
266 _mesa_shader_stage_to_string(i));
267 }
268 }
269
270 if (sh->num_combined_uniform_components >
271 consts->Program[i].MaxCombinedUniformComponents) {
272 if (consts->GLSLSkipStrictMaxUniformLimitCheck) {
273 linker_warning(prog, "Too many %s shader uniform components, "
274 "but the driver will try to optimize them out; "
275 "this is non-portable out-of-spec behavior\n",
276 _mesa_shader_stage_to_string(i));
277 } else {
278 linker_error(prog, "Too many %s shader uniform components\n",
279 _mesa_shader_stage_to_string(i));
280 }
281 }
282
283 total_shader_storage_blocks += sh->Program->info.num_ssbos;
284 total_uniform_blocks += sh->Program->info.num_ubos;
285 }
286
287 if (total_uniform_blocks > consts->MaxCombinedUniformBlocks) {
288 linker_error(prog, "Too many combined uniform blocks (%d/%d)\n",
289 total_uniform_blocks, consts->MaxCombinedUniformBlocks);
290 }
291
292 if (total_shader_storage_blocks > consts->MaxCombinedShaderStorageBlocks) {
293 linker_error(prog, "Too many combined shader storage blocks (%d/%d)\n",
294 total_shader_storage_blocks,
295 consts->MaxCombinedShaderStorageBlocks);
296 }
297
298 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
299 if (prog->data->UniformBlocks[i].UniformBufferSize >
300 consts->MaxUniformBlockSize) {
301 linker_error(prog, "Uniform block %s too big (%d/%d)\n",
302 prog->data->UniformBlocks[i].name.string,
303 prog->data->UniformBlocks[i].UniformBufferSize,
304 consts->MaxUniformBlockSize);
305 }
306 }
307
308 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
309 if (prog->data->ShaderStorageBlocks[i].UniformBufferSize >
310 consts->MaxShaderStorageBlockSize) {
311 linker_error(prog, "Shader storage block %s too big (%d/%d)\n",
312 prog->data->ShaderStorageBlocks[i].name.string,
313 prog->data->ShaderStorageBlocks[i].UniformBufferSize,
314 consts->MaxShaderStorageBlockSize);
315 }
316 }
317 }
318
319 void
link_util_calculate_subroutine_compat(struct gl_shader_program * prog)320 link_util_calculate_subroutine_compat(struct gl_shader_program *prog)
321 {
322 unsigned mask = prog->data->linked_stages;
323 while (mask) {
324 const int i = u_bit_scan(&mask);
325 struct gl_program *p = prog->_LinkedShaders[i]->Program;
326
327 for (unsigned j = 0; j < p->sh.NumSubroutineUniformRemapTable; j++) {
328 if (p->sh.SubroutineUniformRemapTable[j] == INACTIVE_UNIFORM_EXPLICIT_LOCATION)
329 continue;
330
331 struct gl_uniform_storage *uni = p->sh.SubroutineUniformRemapTable[j];
332
333 if (!uni)
334 continue;
335
336 int count = 0;
337 if (p->sh.NumSubroutineFunctions == 0) {
338 linker_error(prog, "subroutine uniform %s defined but no valid functions found\n", uni->type->name);
339 continue;
340 }
341 for (unsigned f = 0; f < p->sh.NumSubroutineFunctions; f++) {
342 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[f];
343 for (int k = 0; k < fn->num_compat_types; k++) {
344 if (fn->types[k] == uni->type) {
345 count++;
346 break;
347 }
348 }
349 }
350 uni->num_compatible_subroutines = count;
351 }
352 }
353 }
354
355 /**
356 * Recursive part of the public mark_array_elements_referenced function.
357 *
358 * The recursion occurs when an entire array-of- is accessed. See the
359 * implementation for more details.
360 *
361 * \param dr List of array_deref_range elements to be
362 * processed.
363 * \param count Number of array_deref_range elements to be
364 * processed.
365 * \param scale Current offset scale.
366 * \param linearized_index Current accumulated linearized array index.
367 */
368 void
_mark_array_elements_referenced(const struct array_deref_range * dr,unsigned count,unsigned scale,unsigned linearized_index,BITSET_WORD * bits)369 _mark_array_elements_referenced(const struct array_deref_range *dr,
370 unsigned count, unsigned scale,
371 unsigned linearized_index,
372 BITSET_WORD *bits)
373 {
374 /* Walk through the list of array dereferences in least- to
375 * most-significant order. Along the way, accumulate the current
376 * linearized offset and the scale factor for each array-of-.
377 */
378 for (unsigned i = 0; i < count; i++) {
379 if (dr[i].index < dr[i].size) {
380 linearized_index += dr[i].index * scale;
381 scale *= dr[i].size;
382 } else {
383 /* For each element in the current array, update the count and
384 * offset, then recurse to process the remaining arrays.
385 *
386 * There is some inefficency here if the last eBITSET_WORD *bitslement in the
387 * array_deref_range list specifies the entire array. In that case,
388 * the loop will make recursive calls with count == 0. In the call,
389 * all that will happen is the bit will be set.
390 */
391 for (unsigned j = 0; j < dr[i].size; j++) {
392 _mark_array_elements_referenced(&dr[i + 1],
393 count - (i + 1),
394 scale * dr[i].size,
395 linearized_index + (j * scale),
396 bits);
397 }
398
399 return;
400 }
401 }
402
403 BITSET_SET(bits, linearized_index);
404 }
405
406 /**
407 * Mark a set of array elements as accessed.
408 *
409 * If every \c array_deref_range is for a single index, only a single
410 * element will be marked. If any \c array_deref_range is for an entire
411 * array-of-, then multiple elements will be marked.
412 *
413 * Items in the \c array_deref_range list appear in least- to
414 * most-significant order. This is the \b opposite order the indices
415 * appear in the GLSL shader text. An array access like
416 *
417 * x = y[1][i][3];
418 *
419 * would appear as
420 *
421 * { { 3, n }, { m, m }, { 1, p } }
422 *
423 * where n, m, and p are the sizes of the arrays-of-arrays.
424 *
425 * The set of marked array elements can later be queried by
426 * \c ::is_linearized_index_referenced.
427 *
428 * \param dr List of array_deref_range elements to be processed.
429 * \param count Number of array_deref_range elements to be processed.
430 */
431 void
link_util_mark_array_elements_referenced(const struct array_deref_range * dr,unsigned count,unsigned array_depth,BITSET_WORD * bits)432 link_util_mark_array_elements_referenced(const struct array_deref_range *dr,
433 unsigned count, unsigned array_depth,
434 BITSET_WORD *bits)
435 {
436 if (count != array_depth)
437 return;
438
439 _mark_array_elements_referenced(dr, count, 1, 0, bits);
440 }
441