glsl: Remove integer matrix support from ir_dereference_array::constant_expression_value
[mesa.git] / src / compiler / glsl / gl_nir_linker.c
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 "nir.h"
25 #include "gl_nir.h"
26 #include "gl_nir_linker.h"
27 #include "linker_util.h"
28 #include "main/mtypes.h"
29 #include "main/shaderobj.h"
30 #include "ir_uniform.h" /* for gl_uniform_storage */
31
32 /**
33 * This file included general link methods, using NIR, instead of IR as
34 * the counter-part glsl/linker.cpp
35 */
36
37 /**
38 * Built-in / reserved GL variables names start with "gl_"
39 */
40 static inline bool
41 is_gl_identifier(const char *s)
42 {
43 return s && s[0] == 'g' && s[1] == 'l' && s[2] == '_';
44 }
45
46 static bool
47 inout_has_same_location(const nir_variable *var, unsigned stage)
48 {
49 if (!var->data.patch &&
50 ((var->data.mode == nir_var_shader_out &&
51 stage == MESA_SHADER_TESS_CTRL) ||
52 (var->data.mode == nir_var_shader_in &&
53 (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL ||
54 stage == MESA_SHADER_GEOMETRY))))
55 return true;
56 else
57 return false;
58 }
59
60 /**
61 * Create gl_shader_variable from nir_variable.
62 */
63 static struct gl_shader_variable *
64 create_shader_variable(struct gl_shader_program *shProg,
65 const nir_variable *in,
66 const char *name, const struct glsl_type *type,
67 const struct glsl_type *interface_type,
68 bool use_implicit_location, int location,
69 const struct glsl_type *outermost_struct_type)
70 {
71 /* Allocate zero-initialized memory to ensure that bitfield padding
72 * is zero.
73 */
74 struct gl_shader_variable *out = rzalloc(shProg,
75 struct gl_shader_variable);
76 if (!out)
77 return NULL;
78
79 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
80 * expect to see gl_VertexID in the program resource list. Pretend.
81 */
82 if (in->data.mode == nir_var_system_value &&
83 in->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
84 out->name = ralloc_strdup(shProg, "gl_VertexID");
85 } else if ((in->data.mode == nir_var_shader_out &&
86 in->data.location == VARYING_SLOT_TESS_LEVEL_OUTER) ||
87 (in->data.mode == nir_var_system_value &&
88 in->data.location == SYSTEM_VALUE_TESS_LEVEL_OUTER)) {
89 out->name = ralloc_strdup(shProg, "gl_TessLevelOuter");
90 type = glsl_array_type(glsl_float_type(), 4, 0);
91 } else if ((in->data.mode == nir_var_shader_out &&
92 in->data.location == VARYING_SLOT_TESS_LEVEL_INNER) ||
93 (in->data.mode == nir_var_system_value &&
94 in->data.location == SYSTEM_VALUE_TESS_LEVEL_INNER)) {
95 out->name = ralloc_strdup(shProg, "gl_TessLevelInner");
96 type = glsl_array_type(glsl_float_type(), 2, 0);
97 } else {
98 out->name = ralloc_strdup(shProg, name);
99 }
100
101 if (!out->name)
102 return NULL;
103
104 /* The ARB_program_interface_query spec says:
105 *
106 * "Not all active variables are assigned valid locations; the
107 * following variables will have an effective location of -1:
108 *
109 * * uniforms declared as atomic counters;
110 *
111 * * members of a uniform block;
112 *
113 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
114 *
115 * * inputs or outputs not declared with a "location" layout
116 * qualifier, except for vertex shader inputs and fragment shader
117 * outputs."
118 */
119 if (glsl_get_base_type(in->type) == GLSL_TYPE_ATOMIC_UINT ||
120 is_gl_identifier(in->name) ||
121 !(in->data.explicit_location || use_implicit_location)) {
122 out->location = -1;
123 } else {
124 out->location = location;
125 }
126
127 out->type = type;
128 out->outermost_struct_type = outermost_struct_type;
129 out->interface_type = interface_type;
130 out->component = in->data.location_frac;
131 out->index = in->data.index;
132 out->patch = in->data.patch;
133 out->mode = in->data.mode;
134 out->interpolation = in->data.interpolation;
135 out->precision = in->data.precision;
136 out->explicit_location = in->data.explicit_location;
137
138 return out;
139 }
140
141 static bool
142 add_shader_variable(const struct gl_context *ctx,
143 struct gl_shader_program *shProg,
144 struct set *resource_set,
145 unsigned stage_mask,
146 GLenum programInterface, nir_variable *var,
147 const char *name, const struct glsl_type *type,
148 bool use_implicit_location, int location,
149 bool inouts_share_location,
150 const struct glsl_type *outermost_struct_type)
151 {
152 const struct glsl_type *interface_type = var->interface_type;
153
154 if (outermost_struct_type == NULL) {
155 if (var->data.from_named_ifc_block) {
156 const char *interface_name = glsl_get_type_name(interface_type);
157
158 if (glsl_type_is_array(interface_type)) {
159 /* Issue #16 of the ARB_program_interface_query spec says:
160 *
161 * "* If a variable is a member of an interface block without an
162 * instance name, it is enumerated using just the variable name.
163 *
164 * * If a variable is a member of an interface block with an
165 * instance name, it is enumerated as "BlockName.Member", where
166 * "BlockName" is the name of the interface block (not the
167 * instance name) and "Member" is the name of the variable."
168 *
169 * In particular, it indicates that it should be "BlockName",
170 * not "BlockName[array length]". The conformance suite and
171 * dEQP both require this behavior.
172 *
173 * Here, we unwrap the extra array level added by named interface
174 * block array lowering so we have the correct variable type. We
175 * also unwrap the interface type when constructing the name.
176 *
177 * We leave interface_type the same so that ES 3.x SSO pipeline
178 * validation can enforce the rules requiring array length to
179 * match on interface blocks.
180 */
181 type = glsl_get_array_element(type);
182
183 interface_name =
184 glsl_get_type_name(glsl_get_array_element(interface_type));
185 }
186
187 name = ralloc_asprintf(shProg, "%s.%s", interface_name, name);
188 }
189 }
190
191 switch (glsl_get_base_type(type)) {
192 case GLSL_TYPE_STRUCT: {
193 /* The ARB_program_interface_query spec says:
194 *
195 * "For an active variable declared as a structure, a separate entry
196 * will be generated for each active structure member. The name of
197 * each entry is formed by concatenating the name of the structure,
198 * the "." character, and the name of the structure member. If a
199 * structure member to enumerate is itself a structure or array,
200 * these enumeration rules are applied recursively."
201 */
202 if (outermost_struct_type == NULL)
203 outermost_struct_type = type;
204
205 unsigned field_location = location;
206 for (unsigned i = 0; i < glsl_get_length(type); i++) {
207 const struct glsl_type *field_type = glsl_get_struct_field(type, i);
208 const struct glsl_struct_field *field =
209 glsl_get_struct_field_data(type, i);
210
211 char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name);
212 if (!add_shader_variable(ctx, shProg, resource_set,
213 stage_mask, programInterface,
214 var, field_name, field_type,
215 use_implicit_location, field_location,
216 false, outermost_struct_type))
217 return false;
218
219 field_location += glsl_count_attribute_slots(field_type, false);
220 }
221 return true;
222 }
223
224 case GLSL_TYPE_ARRAY: {
225 /* The ARB_program_interface_query spec says:
226 *
227 * "For an active variable declared as an array of basic types, a
228 * single entry will be generated, with its name string formed by
229 * concatenating the name of the array and the string "[0]"."
230 *
231 * "For an active variable declared as an array of an aggregate data
232 * type (structures or arrays), a separate entry will be generated
233 * for each active array element, unless noted immediately below.
234 * The name of each entry is formed by concatenating the name of
235 * the array, the "[" character, an integer identifying the element
236 * number, and the "]" character. These enumeration rules are
237 * applied recursively, treating each enumerated array element as a
238 * separate active variable."
239 */
240 const struct glsl_type *array_type = glsl_get_array_element(type);
241 if (glsl_get_base_type(array_type) == GLSL_TYPE_STRUCT ||
242 glsl_get_base_type(array_type) == GLSL_TYPE_ARRAY) {
243 unsigned elem_location = location;
244 unsigned stride = inouts_share_location ? 0 :
245 glsl_count_attribute_slots(array_type, false);
246 for (unsigned i = 0; i < glsl_get_length(type); i++) {
247 char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i);
248 if (!add_shader_variable(ctx, shProg, resource_set,
249 stage_mask, programInterface,
250 var, elem, array_type,
251 use_implicit_location, elem_location,
252 false, outermost_struct_type))
253 return false;
254 elem_location += stride;
255 }
256 return true;
257 }
258 /* fallthrough */
259 }
260
261 default: {
262 /* The ARB_program_interface_query spec says:
263 *
264 * "For an active variable declared as a single instance of a basic
265 * type, a single entry will be generated, using the variable name
266 * from the shader source."
267 */
268 struct gl_shader_variable *sha_v =
269 create_shader_variable(shProg, var, name, type, interface_type,
270 use_implicit_location, location,
271 outermost_struct_type);
272 if (!sha_v)
273 return false;
274
275 return link_util_add_program_resource(shProg, resource_set,
276 programInterface, sha_v, stage_mask);
277 }
278 }
279 }
280
281 static bool
282 add_vars_from_list(const struct gl_context *ctx,
283 struct gl_shader_program *prog, struct set *resource_set,
284 const struct exec_list *var_list, unsigned stage,
285 GLenum programInterface)
286 {
287 nir_foreach_variable(var, var_list) {
288 if (var->data.how_declared == nir_var_hidden)
289 continue;
290
291 int loc_bias = 0;
292 switch(var->data.mode) {
293 case nir_var_system_value:
294 case nir_var_shader_in:
295 if (programInterface != GL_PROGRAM_INPUT)
296 continue;
297 loc_bias = (stage == MESA_SHADER_VERTEX) ? VERT_ATTRIB_GENERIC0
298 : VARYING_SLOT_VAR0;
299 break;
300 case nir_var_shader_out:
301 if (programInterface != GL_PROGRAM_OUTPUT)
302 continue;
303 loc_bias = (stage == MESA_SHADER_FRAGMENT) ? FRAG_RESULT_DATA0
304 : VARYING_SLOT_VAR0;
305 break;
306 default:
307 continue;
308 }
309
310 if (var->data.patch)
311 loc_bias = VARYING_SLOT_PATCH0;
312
313 if (prog->data->spirv) {
314 struct gl_shader_variable *sh_var =
315 rzalloc(prog, struct gl_shader_variable);
316
317 /* In the ARB_gl_spirv spec, names are considered optional debug info, so
318 * the linker needs to work without them. Returning them is optional.
319 * For simplicity, we ignore names.
320 */
321 sh_var->name = NULL;
322 sh_var->type = var->type;
323 sh_var->location = var->data.location - loc_bias;
324 sh_var->index = var->data.index;
325
326 if (!link_util_add_program_resource(prog, resource_set,
327 programInterface,
328 sh_var, 1 << stage)) {
329 return false;
330 }
331 } else {
332 /* Skip packed varyings, packed varyings are handled separately
333 * by add_packed_varyings in the GLSL IR
334 * build_program_resource_list() call.
335 * TODO: handle packed varyings here instead. We likely want a NIR
336 * based packing pass first.
337 */
338 if (strncmp(var->name, "packed:", 7) == 0)
339 continue;
340
341 const bool vs_input_or_fs_output =
342 (stage == MESA_SHADER_VERTEX &&
343 var->data.mode == nir_var_shader_in) ||
344 (stage == MESA_SHADER_FRAGMENT &&
345 var->data.mode == nir_var_shader_out);
346
347 if (!add_shader_variable(ctx, prog, resource_set,
348 1 << stage, programInterface,
349 var, var->name, var->type,
350 vs_input_or_fs_output,
351 var->data.location - loc_bias,
352 inout_has_same_location(var, stage),
353 NULL))
354 return false;
355 }
356 }
357
358 return true;
359 }
360
361 static bool
362 add_interface_variables(const struct gl_context *ctx,
363 struct gl_shader_program *prog,
364 struct set *resource_set,
365 unsigned stage, GLenum programInterface)
366 {
367 struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
368 if (!sh)
369 return true;
370
371 nir_shader *nir = sh->Program->nir;
372 assert(nir);
373
374 switch (programInterface) {
375 case GL_PROGRAM_INPUT: {
376 bool result = add_vars_from_list(ctx, prog, resource_set,
377 &nir->inputs, stage, programInterface);
378 result &= add_vars_from_list(ctx, prog, resource_set, &nir->system_values,
379 stage, programInterface);
380 return result;
381 }
382 case GL_PROGRAM_OUTPUT:
383 return add_vars_from_list(ctx, prog, resource_set, &nir->outputs, stage,
384 programInterface);
385 default:
386 assert("!Should not get here");
387 break;
388 }
389
390 return false;
391 }
392
393 /* TODO: as we keep adding features, this method is becoming more and more
394 * similar to its GLSL counterpart at linker.cpp. Eventually it would be good
395 * to check if they could be refactored, and reduce code duplication somehow
396 */
397 void
398 nir_build_program_resource_list(struct gl_context *ctx,
399 struct gl_shader_program *prog,
400 bool rebuild_resourse_list)
401 {
402 /* Rebuild resource list. */
403 if (prog->data->ProgramResourceList && rebuild_resourse_list) {
404 ralloc_free(prog->data->ProgramResourceList);
405 prog->data->ProgramResourceList = NULL;
406 prog->data->NumProgramResourceList = 0;
407 }
408
409 int input_stage = MESA_SHADER_STAGES, output_stage = 0;
410
411 /* Determine first input and final output stage. These are used to
412 * detect which variables should be enumerated in the resource list
413 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
414 */
415 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
416 if (!prog->_LinkedShaders[i])
417 continue;
418 if (input_stage == MESA_SHADER_STAGES)
419 input_stage = i;
420 output_stage = i;
421 }
422
423 /* Empty shader, no resources. */
424 if (input_stage == MESA_SHADER_STAGES && output_stage == 0)
425 return;
426
427 struct set *resource_set = _mesa_pointer_set_create(NULL);
428
429 /* Add inputs and outputs to the resource list. */
430 if (!add_interface_variables(ctx, prog, resource_set, input_stage,
431 GL_PROGRAM_INPUT)) {
432 return;
433 }
434
435 if (!add_interface_variables(ctx, prog, resource_set, output_stage,
436 GL_PROGRAM_OUTPUT)) {
437 return;
438 }
439
440 /* Add transform feedback varyings and buffers. */
441 if (prog->last_vert_prog) {
442 struct gl_transform_feedback_info *linked_xfb =
443 prog->last_vert_prog->sh.LinkedTransformFeedback;
444
445 /* Add varyings. */
446 if (linked_xfb->NumVarying > 0) {
447 for (int i = 0; i < linked_xfb->NumVarying; i++) {
448 if (!link_util_add_program_resource(prog, resource_set,
449 GL_TRANSFORM_FEEDBACK_VARYING,
450 &linked_xfb->Varyings[i], 0))
451 return;
452 }
453 }
454
455 /* Add buffers. */
456 for (unsigned i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
457 if ((linked_xfb->ActiveBuffers >> i) & 1) {
458 linked_xfb->Buffers[i].Binding = i;
459 if (!link_util_add_program_resource(prog, resource_set,
460 GL_TRANSFORM_FEEDBACK_BUFFER,
461 &linked_xfb->Buffers[i], 0))
462 return;
463 }
464 }
465 }
466
467 /* Add uniforms
468 *
469 * Here, it is expected that nir_link_uniforms() has already been
470 * called, so that UniformStorage table is already available.
471 */
472 int top_level_array_base_offset = -1;
473 int top_level_array_size_in_bytes = -1;
474 int second_element_offset = -1;
475 int block_index = -1;
476 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
477 struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
478
479 if (uniform->hidden) {
480 for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) {
481 if (!uniform->opaque[j].active ||
482 glsl_get_base_type(uniform->type) != GLSL_TYPE_SUBROUTINE)
483 continue;
484
485 GLenum type =
486 _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j);
487 /* add shader subroutines */
488 if (!link_util_add_program_resource(prog, resource_set,
489 type, uniform, 0))
490 return;
491 }
492
493 continue;
494 }
495
496 if (!link_util_should_add_buffer_variable(prog, uniform,
497 top_level_array_base_offset,
498 top_level_array_size_in_bytes,
499 second_element_offset, block_index))
500 continue;
501
502
503 if (prog->data->UniformStorage[i].offset >= second_element_offset) {
504 top_level_array_base_offset =
505 prog->data->UniformStorage[i].offset;
506
507 top_level_array_size_in_bytes =
508 prog->data->UniformStorage[i].top_level_array_size *
509 prog->data->UniformStorage[i].top_level_array_stride;
510
511 /* Set or reset the second element offset. For non arrays this
512 * will be set to -1.
513 */
514 second_element_offset = top_level_array_size_in_bytes ?
515 top_level_array_base_offset +
516 prog->data->UniformStorage[i].top_level_array_stride : -1;
517 }
518 block_index = uniform->block_index;
519
520
521 GLenum interface = uniform->is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM;
522 if (!link_util_add_program_resource(prog, resource_set, interface, uniform,
523 uniform->active_shader_mask)) {
524 return;
525 }
526 }
527
528
529 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
530 if (!link_util_add_program_resource(prog, resource_set, GL_UNIFORM_BLOCK,
531 &prog->data->UniformBlocks[i],
532 prog->data->UniformBlocks[i].stageref))
533 return;
534 }
535
536 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
537 if (!link_util_add_program_resource(prog, resource_set, GL_SHADER_STORAGE_BLOCK,
538 &prog->data->ShaderStorageBlocks[i],
539 prog->data->ShaderStorageBlocks[i].stageref))
540 return;
541 }
542
543 /* Add atomic counter buffers. */
544 for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
545 if (!link_util_add_program_resource(prog, resource_set, GL_ATOMIC_COUNTER_BUFFER,
546 &prog->data->AtomicBuffers[i], 0))
547 return;
548 }
549
550 unsigned mask = prog->data->linked_stages;
551 while (mask) {
552 const int i = u_bit_scan(&mask);
553 struct gl_program *p = prog->_LinkedShaders[i]->Program;
554
555 GLuint type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i);
556 for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
557 if (!link_util_add_program_resource(prog, resource_set,
558 type,
559 &p->sh.SubroutineFunctions[j],
560 0))
561 return;
562 }
563 }
564
565 _mesa_set_destroy(resource_set, NULL);
566 }
567
568 bool
569 gl_nir_link_spirv(struct gl_context *ctx, struct gl_shader_program *prog,
570 const struct gl_nir_linker_options *options)
571 {
572 if (!gl_nir_link_uniform_blocks(ctx, prog))
573 return false;
574
575 if (!gl_nir_link_uniforms(ctx, prog, options->fill_parameters))
576 return false;
577
578 gl_nir_link_assign_atomic_counter_resources(ctx, prog);
579 gl_nir_link_assign_xfb_resources(ctx, prog);
580
581 return true;
582 }
583
584 /**
585 * Validate shader image resources.
586 */
587 static void
588 check_image_resources(struct gl_context *ctx, struct gl_shader_program *prog)
589 {
590 unsigned total_image_units = 0;
591 unsigned fragment_outputs = 0;
592 unsigned total_shader_storage_blocks = 0;
593
594 if (!ctx->Extensions.ARB_shader_image_load_store)
595 return;
596
597 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
598 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
599 if (!sh)
600 continue;
601
602 total_image_units += sh->Program->info.num_images;
603 total_shader_storage_blocks += sh->Program->info.num_ssbos;
604 }
605
606 if (total_image_units > ctx->Const.MaxCombinedImageUniforms)
607 linker_error(prog, "Too many combined image uniforms\n");
608
609 struct gl_linked_shader *frag_sh =
610 prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
611 if (frag_sh) {
612 uint64_t frag_outputs_written = frag_sh->Program->info.outputs_written;
613 fragment_outputs = util_bitcount64(frag_outputs_written);
614 }
615
616 if (total_image_units + fragment_outputs + total_shader_storage_blocks >
617 ctx->Const.MaxCombinedShaderOutputResources)
618 linker_error(prog, "Too many combined image uniforms, shader storage "
619 " buffers and fragment outputs\n");
620 }
621
622 bool
623 gl_nir_link_glsl(struct gl_context *ctx, struct gl_shader_program *prog)
624 {
625 if (!gl_nir_link_uniforms(ctx, prog, true))
626 return false;
627
628 link_util_calculate_subroutine_compat(prog);
629 link_util_check_uniform_resources(ctx, prog);
630 link_util_check_subroutine_resources(prog);
631 check_image_resources(ctx, prog);
632 gl_nir_link_assign_atomic_counter_resources(ctx, prog);
633 gl_nir_link_check_atomic_counter_resources(ctx, prog);
634
635 if (prog->data->LinkStatus == LINKING_FAILURE)
636 return false;
637
638 return true;
639 }