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 "nir_builder.h"
26 #include "gl_nir.h"
27 #include "gl_nir_linker.h"
28 #include "gl_nir_link_varyings.h"
29 #include "linker_util.h"
30 #include "main/shader_types.h"
31 #include "main/consts_exts.h"
32 #include "main/shaderobj.h"
33 #include "ir_uniform.h" /* for gl_uniform_storage */
34 #include "util/glheader.h"
35 #include "util/perf/cpu_trace.h"
36
37 /**
38 * This file included general link methods, using NIR, instead of IR as
39 * the counter-part glsl/linker.cpp
40 */
41
42 void
gl_nir_opts(nir_shader * nir)43 gl_nir_opts(nir_shader *nir)
44 {
45 bool progress;
46
47 MESA_TRACE_FUNC();
48
49 do {
50 progress = false;
51
52 NIR_PASS(_, nir, nir_lower_vars_to_ssa);
53
54 /* Linking deals with unused inputs/outputs, but here we can remove
55 * things local to the shader in the hopes that we can cleanup other
56 * things. This pass will also remove variables with only stores, so we
57 * might be able to make progress after it.
58 */
59 NIR_PASS(progress, nir, nir_remove_dead_variables,
60 nir_var_function_temp | nir_var_shader_temp |
61 nir_var_mem_shared,
62 NULL);
63
64 NIR_PASS(progress, nir, nir_opt_find_array_copies);
65 NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
66 NIR_PASS(progress, nir, nir_opt_dead_write_vars);
67
68 if (nir->options->lower_to_scalar) {
69 NIR_PASS(_, nir, nir_lower_alu_to_scalar,
70 nir->options->lower_to_scalar_filter, NULL);
71 NIR_PASS(_, nir, nir_lower_phis_to_scalar, false);
72 }
73
74 NIR_PASS(_, nir, nir_lower_alu);
75 NIR_PASS(_, nir, nir_lower_pack);
76 NIR_PASS(progress, nir, nir_copy_prop);
77 NIR_PASS(progress, nir, nir_opt_remove_phis);
78 NIR_PASS(progress, nir, nir_opt_dce);
79 if (nir_opt_loop(nir)) {
80 progress = true;
81 NIR_PASS(progress, nir, nir_copy_prop);
82 NIR_PASS(progress, nir, nir_opt_dce);
83 }
84 NIR_PASS(progress, nir, nir_opt_if, 0);
85 NIR_PASS(progress, nir, nir_opt_dead_cf);
86 NIR_PASS(progress, nir, nir_opt_cse);
87 NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
88
89 NIR_PASS(progress, nir, nir_opt_phi_precision);
90 NIR_PASS(progress, nir, nir_opt_algebraic);
91 NIR_PASS(progress, nir, nir_opt_constant_folding);
92
93 if (!nir->info.flrp_lowered) {
94 unsigned lower_flrp =
95 (nir->options->lower_flrp16 ? 16 : 0) |
96 (nir->options->lower_flrp32 ? 32 : 0) |
97 (nir->options->lower_flrp64 ? 64 : 0);
98
99 if (lower_flrp) {
100 bool lower_flrp_progress = false;
101
102 NIR_PASS(lower_flrp_progress, nir, nir_lower_flrp,
103 lower_flrp,
104 false /* always_precise */);
105 if (lower_flrp_progress) {
106 NIR_PASS(progress, nir,
107 nir_opt_constant_folding);
108 progress = true;
109 }
110 }
111
112 /* Nothing should rematerialize any flrps, so we only need to do this
113 * lowering once.
114 */
115 nir->info.flrp_lowered = true;
116 }
117
118 NIR_PASS(progress, nir, nir_opt_undef);
119 NIR_PASS(progress, nir, nir_opt_conditional_discard);
120 if (nir->options->max_unroll_iterations ||
121 (nir->options->max_unroll_iterations_fp64 &&
122 (nir->options->lower_doubles_options & nir_lower_fp64_full_software))) {
123 NIR_PASS(progress, nir, nir_opt_loop_unroll);
124 }
125 } while (progress);
126
127 NIR_PASS(_, nir, nir_lower_var_copies);
128 }
129
130 struct emit_vertex_state {
131 int max_stream_allowed;
132 int invalid_stream_id;
133 bool invalid_stream_id_from_emit_vertex;
134 bool end_primitive_found;
135 unsigned used_streams;
136 };
137
138 /**
139 * Determine the highest stream id to which a (geometry) shader emits
140 * vertices. Also check whether End{Stream}Primitive is ever called.
141 */
142 static void
find_emit_vertex(struct emit_vertex_state * state,nir_shader * shader)143 find_emit_vertex(struct emit_vertex_state *state, nir_shader *shader) {
144 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
145
146 nir_foreach_block_safe(block, impl) {
147 nir_foreach_instr_safe(instr, block) {
148 if (instr->type == nir_instr_type_intrinsic) {
149 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
150
151 if (intr->intrinsic == nir_intrinsic_emit_vertex ||
152 intr->intrinsic == nir_intrinsic_end_primitive) {
153 int stream_id = nir_intrinsic_stream_id(intr);
154 bool from_emit_vertex =
155 intr->intrinsic == nir_intrinsic_emit_vertex;
156 state->end_primitive_found |=
157 intr->intrinsic == nir_intrinsic_end_primitive;
158
159 if (stream_id < 0) {
160 state->invalid_stream_id = stream_id;
161 state->invalid_stream_id_from_emit_vertex = from_emit_vertex;
162 return;
163 }
164
165 if (stream_id > state->max_stream_allowed) {
166 state->invalid_stream_id = stream_id;
167 state->invalid_stream_id_from_emit_vertex = from_emit_vertex;
168 return;
169 }
170
171 state->used_streams |= 1 << stream_id;
172 }
173 }
174 }
175 }
176 }
177
178 /**
179 * Check if geometry shaders emit to non-zero streams and do corresponding
180 * validations.
181 */
182 static void
validate_geometry_shader_emissions(const struct gl_constants * consts,struct gl_shader_program * prog)183 validate_geometry_shader_emissions(const struct gl_constants *consts,
184 struct gl_shader_program *prog)
185 {
186 struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
187
188 if (sh != NULL) {
189 struct emit_vertex_state state;
190 state.max_stream_allowed = consts->MaxVertexStreams - 1;
191 state.invalid_stream_id = 0;
192 state.invalid_stream_id_from_emit_vertex = false;
193 state.end_primitive_found = false;
194 state.used_streams = 0;
195
196 find_emit_vertex(&state, sh->Program->nir);
197
198 if (state.invalid_stream_id != 0) {
199 linker_error(prog, "Invalid call %s(%d). Accepted values for the "
200 "stream parameter are in the range [0, %d].\n",
201 state.invalid_stream_id_from_emit_vertex ?
202 "EmitStreamVertex" : "EndStreamPrimitive",
203 state.invalid_stream_id, state.max_stream_allowed);
204 }
205 prog->Geom.ActiveStreamMask = state.used_streams;
206 prog->Geom.UsesEndPrimitive = state.end_primitive_found;
207
208 /* From the ARB_gpu_shader5 spec:
209 *
210 * "Multiple vertex streams are supported only if the output primitive
211 * type is declared to be "points". A program will fail to link if it
212 * contains a geometry shader calling EmitStreamVertex() or
213 * EndStreamPrimitive() if its output primitive type is not "points".
214 *
215 * However, in the same spec:
216 *
217 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
218 * with <stream> set to zero."
219 *
220 * And:
221 *
222 * "The function EndPrimitive() is equivalent to calling
223 * EndStreamPrimitive() with <stream> set to zero."
224 *
225 * Since we can call EmitVertex() and EndPrimitive() when we output
226 * primitives other than points, calling EmitStreamVertex(0) or
227 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
228 * does. We can use prog->Geom.ActiveStreamMask to check whether only the
229 * first (zero) stream is active.
230 * stream.
231 */
232 if (prog->Geom.ActiveStreamMask & ~(1 << 0) &&
233 sh->Program->info.gs.output_primitive != MESA_PRIM_POINTS) {
234 linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
235 "with n>0 requires point output\n");
236 }
237 }
238 }
239
240 bool
gl_nir_can_add_pointsize_to_program(const struct gl_constants * consts,struct gl_program * prog)241 gl_nir_can_add_pointsize_to_program(const struct gl_constants *consts,
242 struct gl_program *prog)
243 {
244 nir_shader *nir = prog->nir;
245 if (!nir)
246 return true; /* fixedfunction */
247
248 assert(nir->info.stage == MESA_SHADER_VERTEX ||
249 nir->info.stage == MESA_SHADER_TESS_EVAL ||
250 nir->info.stage == MESA_SHADER_GEOMETRY);
251 if (nir->info.outputs_written & VARYING_BIT_PSIZ)
252 return false;
253
254 unsigned max_components = nir->info.stage == MESA_SHADER_GEOMETRY ?
255 consts->MaxGeometryTotalOutputComponents :
256 consts->Program[nir->info.stage].MaxOutputComponents;
257 unsigned num_components = 0;
258 unsigned needed_components = nir->info.stage == MESA_SHADER_GEOMETRY ? nir->info.gs.vertices_out : 1;
259 nir_foreach_shader_out_variable(var, nir) {
260 num_components += glsl_count_dword_slots(var->type, false);
261 }
262
263 /* Ensure that there is enough attribute space to emit at least one primitive */
264 if (num_components && nir->info.stage == MESA_SHADER_GEOMETRY) {
265 if (num_components + needed_components > consts->Program[nir->info.stage].MaxOutputComponents)
266 return false;
267 num_components *= nir->info.gs.vertices_out;
268 }
269
270 return num_components + needed_components <= max_components;
271 }
272
273 static void
gl_nir_link_opts(nir_shader * producer,nir_shader * consumer)274 gl_nir_link_opts(nir_shader *producer, nir_shader *consumer)
275 {
276 MESA_TRACE_FUNC();
277
278 if (producer->options->lower_to_scalar) {
279 NIR_PASS(_, producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
280 NIR_PASS(_, consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
281 }
282
283 nir_lower_io_arrays_to_elements(producer, consumer);
284
285 gl_nir_opts(producer);
286 gl_nir_opts(consumer);
287
288 if (nir_link_opt_varyings(producer, consumer))
289 gl_nir_opts(consumer);
290
291 NIR_PASS(_, producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
292 NIR_PASS(_, consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
293
294 if (nir_remove_unused_varyings(producer, consumer)) {
295 NIR_PASS(_, producer, nir_lower_global_vars_to_local);
296 NIR_PASS(_, consumer, nir_lower_global_vars_to_local);
297
298 gl_nir_opts(producer);
299 gl_nir_opts(consumer);
300
301 /* Optimizations can cause varyings to become unused.
302 * nir_compact_varyings() depends on all dead varyings being removed so
303 * we need to call nir_remove_dead_variables() again here.
304 */
305 NIR_PASS(_, producer, nir_remove_dead_variables, nir_var_shader_out,
306 NULL);
307 NIR_PASS(_, consumer, nir_remove_dead_variables, nir_var_shader_in,
308 NULL);
309 }
310
311 nir_link_varying_precision(producer, consumer);
312 }
313
314 static bool
can_remove_var(nir_variable * var,UNUSED void * data)315 can_remove_var(nir_variable *var, UNUSED void *data)
316 {
317 /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec
318 * says:
319 *
320 * "All members of a named uniform block declared with a shared or
321 * std140 layout qualifier are considered active, even if they are not
322 * referenced in any shader in the program. The uniform block itself is
323 * also considered active, even if no member of the block is
324 * referenced."
325 *
326 * Although the spec doesn't state it std430 layouts are expect to behave
327 * the same way. If the variable is in a uniform block with one of those
328 * layouts, do not eliminate it.
329 */
330 if (nir_variable_is_in_block(var) &&
331 (glsl_get_ifc_packing(var->interface_type) !=
332 GLSL_INTERFACE_PACKING_PACKED))
333 return false;
334
335 if (glsl_get_base_type(glsl_without_array(var->type)) ==
336 GLSL_TYPE_SUBROUTINE)
337 return false;
338
339 /* Uniform initializers could get used by another stage. However if its a
340 * hidden uniform then it should be safe to remove as this was a constant
341 * variable that has been lowered to a uniform.
342 */
343 if (var->constant_initializer && var->data.how_declared != nir_var_hidden)
344 return false;
345
346 return true;
347 }
348
349 static void
set_always_active_io(nir_shader * shader,nir_variable_mode io_mode)350 set_always_active_io(nir_shader *shader, nir_variable_mode io_mode)
351 {
352 assert(io_mode == nir_var_shader_in || io_mode == nir_var_shader_out);
353
354 nir_foreach_variable_with_modes(var, shader, io_mode) {
355 /* Don't set always active on builtins that haven't been redeclared */
356 if (var->data.how_declared == nir_var_declared_implicitly)
357 continue;
358
359 var->data.always_active_io = true;
360 }
361 }
362
363 /**
364 * When separate shader programs are enabled, only input/outputs between
365 * the stages of a multi-stage separate program can be safely removed
366 * from the shader interface. Other inputs/outputs must remain active.
367 */
368 static void
disable_varying_optimizations_for_sso(struct gl_shader_program * prog)369 disable_varying_optimizations_for_sso(struct gl_shader_program *prog)
370 {
371 unsigned first, last;
372 assert(prog->SeparateShader);
373
374 first = MESA_SHADER_STAGES;
375 last = 0;
376
377 /* Determine first and last stage. Excluding the compute stage */
378 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
379 if (!prog->_LinkedShaders[i])
380 continue;
381 if (first == MESA_SHADER_STAGES)
382 first = i;
383 last = i;
384 }
385
386 if (first == MESA_SHADER_STAGES)
387 return;
388
389 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
390 if (!prog->_LinkedShaders[stage])
391 continue;
392
393 /* Prevent the removal of inputs to the first and outputs from the last
394 * stage, unless they are the initial pipeline inputs or final pipeline
395 * outputs, respectively.
396 *
397 * The removal of IO between shaders in the same program is always
398 * allowed.
399 */
400 if (stage == first && stage != MESA_SHADER_VERTEX) {
401 set_always_active_io(prog->_LinkedShaders[stage]->Program->nir,
402 nir_var_shader_in);
403 }
404
405 if (stage == last && stage != MESA_SHADER_FRAGMENT) {
406 set_always_active_io(prog->_LinkedShaders[stage]->Program->nir,
407 nir_var_shader_out);
408 }
409 }
410 }
411
412 static bool
inout_has_same_location(const nir_variable * var,unsigned stage)413 inout_has_same_location(const nir_variable *var, unsigned stage)
414 {
415 if (!var->data.patch &&
416 ((var->data.mode == nir_var_shader_out &&
417 stage == MESA_SHADER_TESS_CTRL) ||
418 (var->data.mode == nir_var_shader_in &&
419 (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL ||
420 stage == MESA_SHADER_GEOMETRY))))
421 return true;
422 else
423 return false;
424 }
425
426 /**
427 * Create gl_shader_variable from nir_variable.
428 */
429 static struct gl_shader_variable *
create_shader_variable(struct gl_shader_program * shProg,const nir_variable * in,const char * name,const struct glsl_type * type,const struct glsl_type * interface_type,bool use_implicit_location,int location,const struct glsl_type * outermost_struct_type)430 create_shader_variable(struct gl_shader_program *shProg,
431 const nir_variable *in,
432 const char *name, const struct glsl_type *type,
433 const struct glsl_type *interface_type,
434 bool use_implicit_location, int location,
435 const struct glsl_type *outermost_struct_type)
436 {
437 /* Allocate zero-initialized memory to ensure that bitfield padding
438 * is zero.
439 */
440 struct gl_shader_variable *out = rzalloc(shProg,
441 struct gl_shader_variable);
442 if (!out)
443 return NULL;
444
445 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
446 * expect to see gl_VertexID in the program resource list. Pretend.
447 */
448 if (in->data.mode == nir_var_system_value &&
449 in->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
450 out->name.string = ralloc_strdup(shProg, "gl_VertexID");
451 } else if ((in->data.mode == nir_var_shader_out &&
452 in->data.location == VARYING_SLOT_TESS_LEVEL_OUTER) ||
453 (in->data.mode == nir_var_system_value &&
454 in->data.location == SYSTEM_VALUE_TESS_LEVEL_OUTER)) {
455 out->name.string = ralloc_strdup(shProg, "gl_TessLevelOuter");
456 type = glsl_array_type(glsl_float_type(), 4, 0);
457 } else if ((in->data.mode == nir_var_shader_out &&
458 in->data.location == VARYING_SLOT_TESS_LEVEL_INNER) ||
459 (in->data.mode == nir_var_system_value &&
460 in->data.location == SYSTEM_VALUE_TESS_LEVEL_INNER)) {
461 out->name.string = ralloc_strdup(shProg, "gl_TessLevelInner");
462 type = glsl_array_type(glsl_float_type(), 2, 0);
463 } else {
464 out->name.string = ralloc_strdup(shProg, name);
465 }
466
467 resource_name_updated(&out->name);
468
469 if (!out->name.string)
470 return NULL;
471
472 /* The ARB_program_interface_query spec says:
473 *
474 * "Not all active variables are assigned valid locations; the
475 * following variables will have an effective location of -1:
476 *
477 * * uniforms declared as atomic counters;
478 *
479 * * members of a uniform block;
480 *
481 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
482 *
483 * * inputs or outputs not declared with a "location" layout
484 * qualifier, except for vertex shader inputs and fragment shader
485 * outputs."
486 */
487 if (glsl_get_base_type(in->type) == GLSL_TYPE_ATOMIC_UINT ||
488 is_gl_identifier(in->name) ||
489 !(in->data.explicit_location || use_implicit_location)) {
490 out->location = -1;
491 } else {
492 out->location = location;
493 }
494
495 out->type = type;
496 out->outermost_struct_type = outermost_struct_type;
497 out->interface_type = interface_type;
498 out->component = in->data.location_frac;
499 out->index = in->data.index;
500 out->patch = in->data.patch;
501 out->mode = in->data.mode;
502 out->interpolation = in->data.interpolation;
503 out->precision = in->data.precision;
504 out->explicit_location = in->data.explicit_location;
505
506 return out;
507 }
508
509 static bool
add_shader_variable(const struct gl_constants * consts,struct gl_shader_program * shProg,struct set * resource_set,unsigned stage_mask,GLenum programInterface,nir_variable * var,const char * name,const struct glsl_type * type,bool use_implicit_location,int location,bool inouts_share_location,const struct glsl_type * outermost_struct_type)510 add_shader_variable(const struct gl_constants *consts,
511 struct gl_shader_program *shProg,
512 struct set *resource_set,
513 unsigned stage_mask,
514 GLenum programInterface, nir_variable *var,
515 const char *name, const struct glsl_type *type,
516 bool use_implicit_location, int location,
517 bool inouts_share_location,
518 const struct glsl_type *outermost_struct_type)
519 {
520 const struct glsl_type *interface_type = var->interface_type;
521
522 if (outermost_struct_type == NULL) {
523 if (var->data.from_named_ifc_block) {
524 const char *interface_name = glsl_get_type_name(interface_type);
525
526 if (glsl_type_is_array(interface_type)) {
527 /* Issue #16 of the ARB_program_interface_query spec says:
528 *
529 * "* If a variable is a member of an interface block without an
530 * instance name, it is enumerated using just the variable name.
531 *
532 * * If a variable is a member of an interface block with an
533 * instance name, it is enumerated as "BlockName.Member", where
534 * "BlockName" is the name of the interface block (not the
535 * instance name) and "Member" is the name of the variable."
536 *
537 * In particular, it indicates that it should be "BlockName",
538 * not "BlockName[array length]". The conformance suite and
539 * dEQP both require this behavior.
540 *
541 * Here, we unwrap the extra array level added by named interface
542 * block array lowering so we have the correct variable type. We
543 * also unwrap the interface type when constructing the name.
544 *
545 * We leave interface_type the same so that ES 3.x SSO pipeline
546 * validation can enforce the rules requiring array length to
547 * match on interface blocks.
548 */
549 type = glsl_get_array_element(type);
550
551 interface_name =
552 glsl_get_type_name(glsl_get_array_element(interface_type));
553 }
554
555 name = ralloc_asprintf(shProg, "%s.%s", interface_name, name);
556 }
557 }
558
559 switch (glsl_get_base_type(type)) {
560 case GLSL_TYPE_STRUCT: {
561 /* The ARB_program_interface_query spec says:
562 *
563 * "For an active variable declared as a structure, a separate entry
564 * will be generated for each active structure member. The name of
565 * each entry is formed by concatenating the name of the structure,
566 * the "." character, and the name of the structure member. If a
567 * structure member to enumerate is itself a structure or array,
568 * these enumeration rules are applied recursively."
569 */
570 if (outermost_struct_type == NULL)
571 outermost_struct_type = type;
572
573 unsigned field_location = location;
574 for (unsigned i = 0; i < glsl_get_length(type); i++) {
575 const struct glsl_type *field_type = glsl_get_struct_field(type, i);
576 const struct glsl_struct_field *field =
577 glsl_get_struct_field_data(type, i);
578
579 char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name);
580 if (!add_shader_variable(consts, shProg, resource_set,
581 stage_mask, programInterface,
582 var, field_name, field_type,
583 use_implicit_location, field_location,
584 false, outermost_struct_type))
585 return false;
586
587 field_location += glsl_count_attribute_slots(field_type, false);
588 }
589 return true;
590 }
591
592 case GLSL_TYPE_ARRAY: {
593 /* The ARB_program_interface_query spec says:
594 *
595 * "For an active variable declared as an array of basic types, a
596 * single entry will be generated, with its name string formed by
597 * concatenating the name of the array and the string "[0]"."
598 *
599 * "For an active variable declared as an array of an aggregate data
600 * type (structures or arrays), a separate entry will be generated
601 * for each active array element, unless noted immediately below.
602 * The name of each entry is formed by concatenating the name of
603 * the array, the "[" character, an integer identifying the element
604 * number, and the "]" character. These enumeration rules are
605 * applied recursively, treating each enumerated array element as a
606 * separate active variable."
607 */
608 const struct glsl_type *array_type = glsl_get_array_element(type);
609 if (glsl_get_base_type(array_type) == GLSL_TYPE_STRUCT ||
610 glsl_get_base_type(array_type) == GLSL_TYPE_ARRAY) {
611 unsigned elem_location = location;
612 unsigned stride = inouts_share_location ? 0 :
613 glsl_count_attribute_slots(array_type, false);
614 for (unsigned i = 0; i < glsl_get_length(type); i++) {
615 char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i);
616 if (!add_shader_variable(consts, shProg, resource_set,
617 stage_mask, programInterface,
618 var, elem, array_type,
619 use_implicit_location, elem_location,
620 false, outermost_struct_type))
621 return false;
622 elem_location += stride;
623 }
624 return true;
625 }
626 }
627 FALLTHROUGH;
628
629 default: {
630 /* The ARB_program_interface_query spec says:
631 *
632 * "For an active variable declared as a single instance of a basic
633 * type, a single entry will be generated, using the variable name
634 * from the shader source."
635 */
636 struct gl_shader_variable *sha_v =
637 create_shader_variable(shProg, var, name, type, interface_type,
638 use_implicit_location, location,
639 outermost_struct_type);
640 if (!sha_v)
641 return false;
642
643 return link_util_add_program_resource(shProg, resource_set,
644 programInterface, sha_v, stage_mask);
645 }
646 }
647 }
648
649 static bool
add_vars_with_modes(const struct gl_constants * consts,struct gl_shader_program * prog,struct set * resource_set,nir_shader * nir,nir_variable_mode modes,unsigned stage,GLenum programInterface)650 add_vars_with_modes(const struct gl_constants *consts,
651 struct gl_shader_program *prog, struct set *resource_set,
652 nir_shader *nir, nir_variable_mode modes,
653 unsigned stage, GLenum programInterface)
654 {
655 nir_foreach_variable_with_modes(var, nir, modes) {
656 if (var->data.how_declared == nir_var_hidden)
657 continue;
658
659 int loc_bias = 0;
660 switch(var->data.mode) {
661 case nir_var_system_value:
662 case nir_var_shader_in:
663 if (programInterface != GL_PROGRAM_INPUT)
664 continue;
665 loc_bias = (stage == MESA_SHADER_VERTEX) ? VERT_ATTRIB_GENERIC0
666 : VARYING_SLOT_VAR0;
667 break;
668 case nir_var_shader_out:
669 if (programInterface != GL_PROGRAM_OUTPUT)
670 continue;
671 loc_bias = (stage == MESA_SHADER_FRAGMENT) ? FRAG_RESULT_DATA0
672 : VARYING_SLOT_VAR0;
673 break;
674 default:
675 continue;
676 }
677
678 if (var->data.patch)
679 loc_bias = VARYING_SLOT_PATCH0;
680
681 if (prog->data->spirv) {
682 struct gl_shader_variable *sh_var =
683 rzalloc(prog, struct gl_shader_variable);
684
685 /* In the ARB_gl_spirv spec, names are considered optional debug info, so
686 * the linker needs to work without them. Returning them is optional.
687 * For simplicity, we ignore names.
688 */
689 sh_var->name.string = NULL;
690 resource_name_updated(&sh_var->name);
691 sh_var->type = var->type;
692 sh_var->location = var->data.location - loc_bias;
693 sh_var->explicit_location = var->data.explicit_location;
694 sh_var->index = var->data.index;
695
696 if (!link_util_add_program_resource(prog, resource_set,
697 programInterface,
698 sh_var, 1 << stage)) {
699 return false;
700 }
701 } else {
702 /* Skip packed varyings, packed varyings are handled separately
703 * by add_packed_varyings in the GLSL IR
704 * build_program_resource_list() call.
705 * TODO: handle packed varyings here instead. We likely want a NIR
706 * based packing pass first.
707 */
708 if (strncmp(var->name, "packed:", 7) == 0)
709 continue;
710
711 const bool vs_input_or_fs_output =
712 (stage == MESA_SHADER_VERTEX &&
713 var->data.mode == nir_var_shader_in) ||
714 (stage == MESA_SHADER_FRAGMENT &&
715 var->data.mode == nir_var_shader_out);
716
717 if (!add_shader_variable(consts, prog, resource_set,
718 1 << stage, programInterface,
719 var, var->name, var->type,
720 vs_input_or_fs_output,
721 var->data.location - loc_bias,
722 inout_has_same_location(var, stage),
723 NULL))
724 return false;
725 }
726 }
727
728 return true;
729 }
730
731 static bool
add_interface_variables(const struct gl_constants * consts,struct gl_shader_program * prog,struct set * resource_set,unsigned stage,GLenum programInterface)732 add_interface_variables(const struct gl_constants *consts,
733 struct gl_shader_program *prog,
734 struct set *resource_set,
735 unsigned stage, GLenum programInterface)
736 {
737 struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
738 if (!sh)
739 return true;
740
741 nir_shader *nir = sh->Program->nir;
742 assert(nir);
743
744 switch (programInterface) {
745 case GL_PROGRAM_INPUT: {
746 return add_vars_with_modes(consts, prog, resource_set,
747 nir, nir_var_shader_in | nir_var_system_value,
748 stage, programInterface);
749 }
750 case GL_PROGRAM_OUTPUT:
751 return add_vars_with_modes(consts, prog, resource_set,
752 nir, nir_var_shader_out,
753 stage, programInterface);
754 default:
755 assert("!Should not get here");
756 break;
757 }
758
759 return false;
760 }
761
762 bool
nir_add_packed_var_to_resource_list(const struct gl_constants * consts,struct gl_shader_program * shProg,struct set * resource_set,nir_variable * var,unsigned stage,GLenum type)763 nir_add_packed_var_to_resource_list(const struct gl_constants *consts,
764 struct gl_shader_program *shProg,
765 struct set *resource_set,
766 nir_variable *var,
767 unsigned stage, GLenum type)
768 {
769 if (!add_shader_variable(consts, shProg, resource_set, 1 << stage,
770 type, var, var->name, var->type, false,
771 var->data.location - VARYING_SLOT_VAR0,
772 inout_has_same_location(var, stage), NULL))
773 return false;
774
775 return true;
776 }
777
778 /**
779 * Initilise list of program resources that point to resource data.
780 */
781 void
init_program_resource_list(struct gl_shader_program * prog)782 init_program_resource_list(struct gl_shader_program *prog)
783 {
784 /* Rebuild resource list. */
785 if (prog->data->ProgramResourceList) {
786 ralloc_free(prog->data->ProgramResourceList);
787 prog->data->ProgramResourceList = NULL;
788 prog->data->NumProgramResourceList = 0;
789 }
790 }
791
792 /* TODO: as we keep adding features, this method is becoming more and more
793 * similar to its GLSL counterpart at linker.cpp. Eventually it would be good
794 * to check if they could be refactored, and reduce code duplication somehow
795 */
796 void
nir_build_program_resource_list(const struct gl_constants * consts,struct gl_shader_program * prog,bool rebuild_resourse_list)797 nir_build_program_resource_list(const struct gl_constants *consts,
798 struct gl_shader_program *prog,
799 bool rebuild_resourse_list)
800 {
801 /* Rebuild resource list. */
802 if (rebuild_resourse_list)
803 init_program_resource_list(prog);
804
805 int input_stage = MESA_SHADER_STAGES, output_stage = 0;
806
807 /* Determine first input and final output stage. These are used to
808 * detect which variables should be enumerated in the resource list
809 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
810 */
811 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
812 if (!prog->_LinkedShaders[i])
813 continue;
814 if (input_stage == MESA_SHADER_STAGES)
815 input_stage = i;
816 output_stage = i;
817 }
818
819 /* Empty shader, no resources. */
820 if (input_stage == MESA_SHADER_STAGES && output_stage == 0)
821 return;
822
823 struct set *resource_set = _mesa_pointer_set_create(NULL);
824
825 /* Add inputs and outputs to the resource list. */
826 if (!add_interface_variables(consts, prog, resource_set, input_stage,
827 GL_PROGRAM_INPUT)) {
828 return;
829 }
830
831 if (!add_interface_variables(consts, prog, resource_set, output_stage,
832 GL_PROGRAM_OUTPUT)) {
833 return;
834 }
835
836 /* Add transform feedback varyings and buffers. */
837 if (prog->last_vert_prog) {
838 struct gl_transform_feedback_info *linked_xfb =
839 prog->last_vert_prog->sh.LinkedTransformFeedback;
840
841 /* Add varyings. */
842 if (linked_xfb->NumVarying > 0) {
843 for (int i = 0; i < linked_xfb->NumVarying; i++) {
844 if (!link_util_add_program_resource(prog, resource_set,
845 GL_TRANSFORM_FEEDBACK_VARYING,
846 &linked_xfb->Varyings[i], 0))
847 return;
848 }
849 }
850
851 /* Add buffers. */
852 for (unsigned i = 0; i < consts->MaxTransformFeedbackBuffers; i++) {
853 if ((linked_xfb->ActiveBuffers >> i) & 1) {
854 linked_xfb->Buffers[i].Binding = i;
855 if (!link_util_add_program_resource(prog, resource_set,
856 GL_TRANSFORM_FEEDBACK_BUFFER,
857 &linked_xfb->Buffers[i], 0))
858 return;
859 }
860 }
861 }
862
863 /* Add uniforms
864 *
865 * Here, it is expected that nir_link_uniforms() has already been
866 * called, so that UniformStorage table is already available.
867 */
868 int top_level_array_base_offset = -1;
869 int top_level_array_size_in_bytes = -1;
870 int second_element_offset = -1;
871 int block_index = -1;
872 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
873 struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
874
875 if (uniform->hidden) {
876 for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) {
877 if (!uniform->opaque[j].active ||
878 glsl_get_base_type(uniform->type) != GLSL_TYPE_SUBROUTINE)
879 continue;
880
881 GLenum type =
882 _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j);
883 /* add shader subroutines */
884 if (!link_util_add_program_resource(prog, resource_set,
885 type, uniform, 0))
886 return;
887 }
888
889 continue;
890 }
891
892 if (!link_util_should_add_buffer_variable(prog, uniform,
893 top_level_array_base_offset,
894 top_level_array_size_in_bytes,
895 second_element_offset, block_index))
896 continue;
897
898
899 if (prog->data->UniformStorage[i].offset >= second_element_offset) {
900 top_level_array_base_offset =
901 prog->data->UniformStorage[i].offset;
902
903 top_level_array_size_in_bytes =
904 prog->data->UniformStorage[i].top_level_array_size *
905 prog->data->UniformStorage[i].top_level_array_stride;
906
907 /* Set or reset the second element offset. For non arrays this
908 * will be set to -1.
909 */
910 second_element_offset = top_level_array_size_in_bytes ?
911 top_level_array_base_offset +
912 prog->data->UniformStorage[i].top_level_array_stride : -1;
913 }
914 block_index = uniform->block_index;
915
916
917 GLenum interface = uniform->is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM;
918 if (!link_util_add_program_resource(prog, resource_set, interface, uniform,
919 uniform->active_shader_mask)) {
920 return;
921 }
922 }
923
924
925 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
926 if (!link_util_add_program_resource(prog, resource_set, GL_UNIFORM_BLOCK,
927 &prog->data->UniformBlocks[i],
928 prog->data->UniformBlocks[i].stageref))
929 return;
930 }
931
932 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
933 if (!link_util_add_program_resource(prog, resource_set, GL_SHADER_STORAGE_BLOCK,
934 &prog->data->ShaderStorageBlocks[i],
935 prog->data->ShaderStorageBlocks[i].stageref))
936 return;
937 }
938
939 /* Add atomic counter buffers. */
940 for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
941 if (!link_util_add_program_resource(prog, resource_set, GL_ATOMIC_COUNTER_BUFFER,
942 &prog->data->AtomicBuffers[i], 0))
943 return;
944 }
945
946 unsigned mask = prog->data->linked_stages;
947 while (mask) {
948 const int i = u_bit_scan(&mask);
949 struct gl_program *p = prog->_LinkedShaders[i]->Program;
950
951 GLuint type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i);
952 for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
953 if (!link_util_add_program_resource(prog, resource_set,
954 type,
955 &p->sh.SubroutineFunctions[j],
956 0))
957 return;
958 }
959 }
960
961 _mesa_set_destroy(resource_set, NULL);
962 }
963
964 static void
shared_type_info(const struct glsl_type * type,unsigned * size,unsigned * align)965 shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
966 {
967 assert(glsl_type_is_vector_or_scalar(type));
968
969 uint32_t comp_size = glsl_type_is_boolean(type)
970 ? 4 : glsl_get_bit_size(type) / 8;
971 unsigned length = glsl_get_vector_elements(type);
972 *size = comp_size * length,
973 *align = comp_size * (length == 3 ? 4 : length);
974 }
975
976 static bool
can_remove_varying_before_linking(nir_variable * var,void * data)977 can_remove_varying_before_linking(nir_variable *var, void *data)
978 {
979 bool *is_sso = (bool *) data;
980 if (*is_sso) {
981 /* Allow the removal of unused builtins in SSO */
982 return var->data.location > -1 && var->data.location < VARYING_SLOT_VAR0;
983 } else
984 return true;
985 }
986
987 static void
remove_dead_varyings_pre_linking(nir_shader * nir)988 remove_dead_varyings_pre_linking(nir_shader *nir)
989 {
990 struct nir_remove_dead_variables_options opts;
991 bool is_sso = nir->info.separate_shader;
992 opts.can_remove_var_data = &is_sso;
993 opts.can_remove_var = &can_remove_varying_before_linking;
994 nir_variable_mode mask = nir_var_shader_in | nir_var_shader_out;
995 nir_remove_dead_variables(nir, mask, &opts);
996 }
997
998 /* - create a gl_PointSize variable
999 * - find every gl_Position write
1000 * - store 1.0 to gl_PointSize after every gl_Position write
1001 */
1002 bool
gl_nir_add_point_size(nir_shader * nir)1003 gl_nir_add_point_size(nir_shader *nir)
1004 {
1005 nir_variable *psiz = nir_create_variable_with_location(nir, nir_var_shader_out,
1006 VARYING_SLOT_PSIZ, glsl_float_type());
1007 psiz->data.how_declared = nir_var_hidden;
1008
1009 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1010 nir_builder b = nir_builder_create(impl);
1011 bool found = false;
1012 nir_foreach_block_safe(block, impl) {
1013 nir_foreach_instr_safe(instr, block) {
1014 if (instr->type == nir_instr_type_intrinsic) {
1015 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
1016 if (intr->intrinsic == nir_intrinsic_store_deref ||
1017 intr->intrinsic == nir_intrinsic_copy_deref) {
1018 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1019 if (var->data.location == VARYING_SLOT_POS) {
1020 b.cursor = nir_after_instr(instr);
1021 nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
1022 nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
1023 found = true;
1024 }
1025 }
1026 }
1027 }
1028 }
1029 if (!found) {
1030 b.cursor = nir_before_impl(impl);
1031 nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
1032 nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
1033 }
1034
1035 /* We always modify the entrypoint */
1036 nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance);
1037 return true;
1038 }
1039
1040 static void
zero_array_members(nir_builder * b,nir_variable * var)1041 zero_array_members(nir_builder *b, nir_variable *var)
1042 {
1043 nir_deref_instr *deref = nir_build_deref_var(b, var);
1044 nir_def *zero = nir_imm_zero(b, 4, 32);
1045 for (int i = 0; i < glsl_array_size(var->type); i++) {
1046 nir_deref_instr *arr = nir_build_deref_array_imm(b, deref, i);
1047 uint32_t mask = BITFIELD_MASK(glsl_get_vector_elements(arr->type));
1048 nir_store_deref(b, arr, nir_channels(b, zero, mask), mask);
1049 }
1050 }
1051
1052 /* GL has an implicit default of 0 for unwritten gl_ClipDistance members;
1053 * to achieve this, write 0 to all members at the start of the shader and
1054 * let them be naturally overwritten later
1055 */
1056 static bool
gl_nir_zero_initialize_clip_distance(nir_shader * nir)1057 gl_nir_zero_initialize_clip_distance(nir_shader *nir)
1058 {
1059 nir_variable *clip_dist0 = nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_CLIP_DIST0);
1060 nir_variable *clip_dist1 = nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_CLIP_DIST1);
1061 if (!clip_dist0 && !clip_dist1)
1062 return false;
1063
1064 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1065 nir_builder b = nir_builder_at(nir_before_impl(impl));
1066 if (clip_dist0)
1067 zero_array_members(&b, clip_dist0);
1068
1069 if (clip_dist1)
1070 zero_array_members(&b, clip_dist1);
1071
1072 nir_metadata_preserve(impl, nir_metadata_dominance |
1073 nir_metadata_block_index);
1074 return true;
1075 }
1076
1077 static void
lower_patch_vertices_in(struct gl_shader_program * shader_prog)1078 lower_patch_vertices_in(struct gl_shader_program *shader_prog)
1079 {
1080 struct gl_linked_shader *linked_tcs =
1081 shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL];
1082 struct gl_linked_shader *linked_tes =
1083 shader_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
1084
1085 /* If we have a TCS and TES linked together, lower TES patch vertices. */
1086 if (linked_tcs && linked_tes) {
1087 nir_shader *tcs_nir = linked_tcs->Program->nir;
1088 nir_shader *tes_nir = linked_tes->Program->nir;
1089
1090 /* The TES input vertex count is the TCS output vertex count,
1091 * lower TES gl_PatchVerticesIn to a constant.
1092 */
1093 uint32_t tes_patch_verts = tcs_nir->info.tess.tcs_vertices_out;
1094 NIR_PASS(_, tes_nir, nir_lower_patch_vertices, tes_patch_verts, NULL);
1095 }
1096 }
1097
1098 static void
preprocess_shader(const struct gl_constants * consts,const struct gl_extensions * exts,struct gl_program * prog,struct gl_shader_program * shader_program,gl_shader_stage stage)1099 preprocess_shader(const struct gl_constants *consts,
1100 const struct gl_extensions *exts,
1101 struct gl_program *prog,
1102 struct gl_shader_program *shader_program,
1103 gl_shader_stage stage)
1104 {
1105 const struct gl_shader_compiler_options *gl_options =
1106 &consts->ShaderCompilerOptions[prog->info.stage];
1107 const nir_shader_compiler_options *options = gl_options->NirOptions;
1108 assert(options);
1109
1110 nir_shader *nir = prog->nir;
1111
1112 if (prog->info.stage == MESA_SHADER_FRAGMENT && consts->HasFBFetch) {
1113 nir_shader_gather_info(prog->nir, nir_shader_get_entrypoint(prog->nir));
1114 NIR_PASS(_, prog->nir, gl_nir_lower_blend_equation_advanced,
1115 exts->KHR_blend_equation_advanced_coherent);
1116 nir_lower_global_vars_to_local(prog->nir);
1117 NIR_PASS(_, prog->nir, nir_opt_combine_stores, nir_var_shader_out);
1118 }
1119
1120 /* Set the next shader stage hint for VS and TES. */
1121 if (!nir->info.separate_shader &&
1122 (nir->info.stage == MESA_SHADER_VERTEX ||
1123 nir->info.stage == MESA_SHADER_TESS_EVAL)) {
1124
1125 unsigned prev_stages = (1 << (prog->info.stage + 1)) - 1;
1126 unsigned stages_mask =
1127 ~prev_stages & shader_program->data->linked_stages;
1128
1129 nir->info.next_stage = stages_mask ?
1130 (gl_shader_stage) u_bit_scan(&stages_mask) : MESA_SHADER_FRAGMENT;
1131 } else {
1132 nir->info.next_stage = MESA_SHADER_FRAGMENT;
1133 }
1134
1135 prog->skip_pointsize_xfb = !(nir->info.outputs_written & VARYING_BIT_PSIZ);
1136 if (!consts->PointSizeFixed && prog->skip_pointsize_xfb &&
1137 stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&
1138 gl_nir_can_add_pointsize_to_program(consts, prog)) {
1139 NIR_PASS(_, nir, gl_nir_add_point_size);
1140 }
1141
1142 if (stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&
1143 (nir->info.outputs_written & (VARYING_BIT_CLIP_DIST0 | VARYING_BIT_CLIP_DIST1)))
1144 NIR_PASS(_, nir, gl_nir_zero_initialize_clip_distance);
1145
1146 if (options->lower_all_io_to_temps ||
1147 nir->info.stage == MESA_SHADER_VERTEX ||
1148 nir->info.stage == MESA_SHADER_GEOMETRY) {
1149 NIR_PASS(_, nir, nir_lower_io_to_temporaries,
1150 nir_shader_get_entrypoint(nir),
1151 true, true);
1152 } else if (nir->info.stage == MESA_SHADER_FRAGMENT ||
1153 !consts->SupportsReadingOutputs) {
1154 NIR_PASS(_, nir, nir_lower_io_to_temporaries,
1155 nir_shader_get_entrypoint(nir),
1156 true, false);
1157 }
1158
1159 NIR_PASS(_, nir, nir_lower_global_vars_to_local);
1160 NIR_PASS(_, nir, nir_split_var_copies);
1161 NIR_PASS(_, nir, nir_lower_var_copies);
1162
1163 if (gl_options->LowerPrecisionFloat16 && gl_options->LowerPrecisionInt16) {
1164 NIR_PASS(_, nir, nir_lower_mediump_vars, nir_var_function_temp | nir_var_shader_temp | nir_var_mem_shared);
1165 }
1166
1167 if (options->lower_to_scalar) {
1168 NIR_PASS(_, nir, nir_remove_dead_variables,
1169 nir_var_function_temp | nir_var_shader_temp |
1170 nir_var_mem_shared, NULL);
1171 NIR_PASS(_, nir, nir_opt_copy_prop_vars);
1172 NIR_PASS(_, nir, nir_lower_alu_to_scalar,
1173 options->lower_to_scalar_filter, NULL);
1174 }
1175
1176 NIR_PASS(_, nir, nir_opt_barrier_modes);
1177
1178 /* before buffers and vars_to_ssa */
1179 NIR_PASS(_, nir, gl_nir_lower_images, true);
1180
1181 if (prog->nir->info.stage == MESA_SHADER_COMPUTE) {
1182 NIR_PASS(_, prog->nir, nir_lower_vars_to_explicit_types,
1183 nir_var_mem_shared, shared_type_info);
1184 NIR_PASS(_, prog->nir, nir_lower_explicit_io,
1185 nir_var_mem_shared, nir_address_format_32bit_offset);
1186 }
1187
1188 /* Do a round of constant folding to clean up address calculations */
1189 NIR_PASS(_, nir, nir_opt_constant_folding);
1190 }
1191
1192 static bool
prelink_lowering(const struct gl_constants * consts,const struct gl_extensions * exts,struct gl_shader_program * shader_program,struct gl_linked_shader ** linked_shader,unsigned num_shaders)1193 prelink_lowering(const struct gl_constants *consts,
1194 const struct gl_extensions *exts,
1195 struct gl_shader_program *shader_program,
1196 struct gl_linked_shader **linked_shader, unsigned num_shaders)
1197 {
1198 for (unsigned i = 0; i < num_shaders; i++) {
1199 struct gl_linked_shader *shader = linked_shader[i];
1200 const nir_shader_compiler_options *options =
1201 consts->ShaderCompilerOptions[shader->Stage].NirOptions;
1202 struct gl_program *prog = shader->Program;
1203
1204 /* ES 3.0+ vertex shaders may still have dead varyings but its now safe
1205 * to remove them as validation is now done according to the spec.
1206 */
1207 if (shader_program->IsES && shader_program->GLSL_Version >= 300 &&
1208 i == MESA_SHADER_VERTEX)
1209 remove_dead_varyings_pre_linking(prog->nir);
1210
1211 preprocess_shader(consts, exts, prog, shader_program, shader->Stage);
1212
1213 if (prog->nir->info.shared_size > consts->MaxComputeSharedMemorySize) {
1214 linker_error(shader_program, "Too much shared memory used (%u/%u)\n",
1215 prog->nir->info.shared_size,
1216 consts->MaxComputeSharedMemorySize);
1217 return false;
1218 }
1219
1220 if (options->lower_to_scalar) {
1221 NIR_PASS(_, shader->Program->nir, nir_lower_load_const_to_scalar);
1222 }
1223 }
1224
1225 lower_patch_vertices_in(shader_program);
1226
1227 /* Linking shaders also optimizes them. Separate shaders, compute shaders
1228 * and shaders with a fixed-func VS or FS that don't need linking are
1229 * optimized here.
1230 */
1231 if (num_shaders == 1)
1232 gl_nir_opts(linked_shader[0]->Program->nir);
1233
1234 /* nir_opt_access() needs to run before linking so that ImageAccess[]
1235 * and BindlessImage[].access are filled out with the correct modes.
1236 */
1237 for (unsigned i = 0; i < num_shaders; i++) {
1238 nir_shader *nir = linked_shader[i]->Program->nir;
1239
1240 nir_opt_access_options opt_access_options;
1241 opt_access_options.is_vulkan = false;
1242 NIR_PASS(_, nir, nir_opt_access, &opt_access_options);
1243
1244 if (consts->ShaderCompilerOptions[i].LowerCombinedClipCullDistance) {
1245 NIR_PASS(_, nir, nir_lower_clip_cull_distance_to_vec4s);
1246 }
1247
1248 /* Combine clip and cull outputs into one array and set:
1249 * - shader_info::clip_distance_array_size
1250 * - shader_info::cull_distance_array_size
1251 */
1252 if (consts->CombinedClipCullDistanceArrays)
1253 NIR_PASS(_, nir, nir_lower_clip_cull_distance_arrays);
1254 }
1255
1256 return true;
1257 }
1258
1259 bool
gl_nir_link_spirv(const struct gl_constants * consts,const struct gl_extensions * exts,struct gl_shader_program * prog,const struct gl_nir_linker_options * options)1260 gl_nir_link_spirv(const struct gl_constants *consts,
1261 const struct gl_extensions *exts,
1262 struct gl_shader_program *prog,
1263 const struct gl_nir_linker_options *options)
1264 {
1265 struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
1266 unsigned num_shaders = 0;
1267
1268 MESA_TRACE_FUNC();
1269
1270 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1271 if (prog->_LinkedShaders[i]) {
1272 linked_shader[num_shaders++] = prog->_LinkedShaders[i];
1273
1274 remove_dead_varyings_pre_linking(prog->_LinkedShaders[i]->Program->nir);
1275 }
1276 }
1277
1278 if (!prelink_lowering(consts, exts, prog, linked_shader, num_shaders))
1279 return false;
1280
1281 /* Linking the stages in the opposite order (from fragment to vertex)
1282 * ensures that inter-shader outputs written to in an earlier stage
1283 * are eliminated if they are (transitively) not used in a later
1284 * stage.
1285 */
1286 for (int i = num_shaders - 2; i >= 0; i--) {
1287 gl_nir_link_opts(linked_shader[i]->Program->nir,
1288 linked_shader[i + 1]->Program->nir);
1289 }
1290
1291 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1292 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
1293 if (shader) {
1294 const nir_remove_dead_variables_options opts = {
1295 .can_remove_var = can_remove_var,
1296 };
1297 nir_remove_dead_variables(shader->Program->nir,
1298 nir_var_uniform | nir_var_image,
1299 &opts);
1300 }
1301 }
1302
1303 if (!gl_nir_link_uniform_blocks(consts, prog))
1304 return false;
1305
1306 if (!gl_nir_link_uniforms(consts, prog, options->fill_parameters))
1307 return false;
1308
1309 gl_nir_link_assign_atomic_counter_resources(consts, prog);
1310 gl_nir_link_assign_xfb_resources(consts, prog);
1311
1312 return true;
1313 }
1314
1315 /**
1316 * Validate shader image resources.
1317 */
1318 static void
check_image_resources(const struct gl_constants * consts,const struct gl_extensions * exts,struct gl_shader_program * prog)1319 check_image_resources(const struct gl_constants *consts,
1320 const struct gl_extensions *exts,
1321 struct gl_shader_program *prog)
1322 {
1323 unsigned total_image_units = 0;
1324 unsigned fragment_outputs = 0;
1325 unsigned total_shader_storage_blocks = 0;
1326
1327 if (!exts->ARB_shader_image_load_store)
1328 return;
1329
1330 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1331 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
1332 if (!sh)
1333 continue;
1334
1335 total_image_units += sh->Program->info.num_images;
1336 total_shader_storage_blocks += sh->Program->info.num_ssbos;
1337 }
1338
1339 if (total_image_units > consts->MaxCombinedImageUniforms)
1340 linker_error(prog, "Too many combined image uniforms\n");
1341
1342 struct gl_linked_shader *frag_sh =
1343 prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1344 if (frag_sh) {
1345 uint64_t frag_outputs_written = frag_sh->Program->info.outputs_written;
1346 fragment_outputs = util_bitcount64(frag_outputs_written);
1347 }
1348
1349 if (total_image_units + fragment_outputs + total_shader_storage_blocks >
1350 consts->MaxCombinedShaderOutputResources)
1351 linker_error(prog, "Too many combined image uniforms, shader storage "
1352 " buffers and fragment outputs\n");
1353 }
1354
1355 static bool
is_sampler_array_accessed_indirectly(nir_deref_instr * deref)1356 is_sampler_array_accessed_indirectly(nir_deref_instr *deref)
1357 {
1358 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
1359 if (d->deref_type != nir_deref_type_array)
1360 continue;
1361
1362 if (nir_src_is_const(d->arr.index))
1363 continue;
1364
1365 return true;
1366 }
1367
1368 return false;
1369 }
1370
1371 /**
1372 * This check is done to make sure we allow only constant expression
1373 * indexing and "constant-index-expression" (indexing with an expression
1374 * that includes loop induction variable).
1375 */
1376 static bool
validate_sampler_array_indexing(const struct gl_constants * consts,struct gl_shader_program * prog)1377 validate_sampler_array_indexing(const struct gl_constants *consts,
1378 struct gl_shader_program *prog)
1379 {
1380 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1381 if (prog->_LinkedShaders[i] == NULL)
1382 continue;
1383
1384 bool no_dynamic_indexing =
1385 consts->ShaderCompilerOptions[i].NirOptions->force_indirect_unrolling_sampler;
1386
1387 bool uses_indirect_sampler_array_indexing = false;
1388 nir_foreach_function_impl(impl, prog->_LinkedShaders[i]->Program->nir) {
1389 nir_foreach_block(block, impl) {
1390 nir_foreach_instr(instr, block) {
1391 /* Check if a sampler array is accessed indirectly */
1392 if (instr->type == nir_instr_type_tex) {
1393 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
1394 int sampler_idx =
1395 nir_tex_instr_src_index(tex_instr, nir_tex_src_sampler_deref);
1396 if (sampler_idx >= 0) {
1397 nir_deref_instr *deref =
1398 nir_instr_as_deref(tex_instr->src[sampler_idx].src.ssa->parent_instr);
1399 if (is_sampler_array_accessed_indirectly(deref)) {
1400 uses_indirect_sampler_array_indexing = true;
1401 break;
1402 }
1403 }
1404 }
1405 }
1406
1407 if (uses_indirect_sampler_array_indexing)
1408 break;
1409 }
1410 if (uses_indirect_sampler_array_indexing)
1411 break;
1412 }
1413
1414 if (uses_indirect_sampler_array_indexing) {
1415 const char *msg = "sampler arrays indexed with non-constant "
1416 "expressions is forbidden in GLSL %s %u";
1417 /* Backend has indicated that it has no dynamic indexing support. */
1418 if (no_dynamic_indexing) {
1419 linker_error(prog, msg, prog->IsES ? "ES" : "", prog->GLSL_Version);
1420 return false;
1421 } else {
1422 linker_warning(prog, msg, prog->IsES ? "ES" : "",
1423 prog->GLSL_Version);
1424 }
1425 }
1426 }
1427
1428 return true;
1429 }
1430
1431 bool
gl_nir_link_glsl(const struct gl_constants * consts,const struct gl_extensions * exts,gl_api api,struct gl_shader_program * prog)1432 gl_nir_link_glsl(const struct gl_constants *consts,
1433 const struct gl_extensions *exts,
1434 gl_api api,
1435 struct gl_shader_program *prog)
1436 {
1437 if (prog->NumShaders == 0)
1438 return true;
1439
1440 MESA_TRACE_FUNC();
1441
1442 /* Check and validate stream emissions in geometry shaders */
1443 validate_geometry_shader_emissions(consts, prog);
1444
1445 prog->last_vert_prog = NULL;
1446 for (int i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
1447 if (prog->_LinkedShaders[i] == NULL)
1448 continue;
1449
1450 prog->last_vert_prog = prog->_LinkedShaders[i]->Program;
1451 break;
1452 }
1453
1454 unsigned first = MESA_SHADER_STAGES;
1455 unsigned last = 0;
1456
1457 /* Determine first and last stage. */
1458 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1459 if (!prog->_LinkedShaders[i])
1460 continue;
1461 if (first == MESA_SHADER_STAGES)
1462 first = i;
1463 last = i;
1464 }
1465
1466 gl_nir_lower_named_interface_blocks(prog);
1467
1468 /* Validate the inputs of each stage with the output of the preceding
1469 * stage.
1470 */
1471 unsigned prev = first;
1472 for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) {
1473 if (prog->_LinkedShaders[i] == NULL)
1474 continue;
1475
1476 gl_nir_cross_validate_outputs_to_inputs(consts, prog,
1477 prog->_LinkedShaders[prev],
1478 prog->_LinkedShaders[i]);
1479 if (!prog->data->LinkStatus)
1480 return false;
1481
1482 prev = i;
1483 }
1484
1485 /* The cross validation of outputs/inputs above validates interstage
1486 * explicit locations. We need to do this also for the inputs in the first
1487 * stage and outputs of the last stage included in the program, since there
1488 * is no cross validation for these.
1489 */
1490 gl_nir_validate_first_and_last_interface_explicit_locations(consts, prog,
1491 (gl_shader_stage) first,
1492 (gl_shader_stage) last);
1493
1494 if (prog->SeparateShader)
1495 disable_varying_optimizations_for_sso(prog);
1496
1497 struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
1498 unsigned num_shaders = 0;
1499
1500 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1501 if (prog->_LinkedShaders[i]) {
1502 linked_shader[num_shaders++] = prog->_LinkedShaders[i];
1503
1504 /* Section 13.46 (Vertex Attribute Aliasing) of the OpenGL ES 3.2
1505 * specification says:
1506 *
1507 * "In general, the behavior of GLSL ES should not depend on
1508 * compiler optimizations which might be implementation-dependent.
1509 * Name matching rules in most languages, including C++ from which
1510 * GLSL ES is derived, are based on declarations rather than use.
1511 *
1512 * RESOLUTION: The existence of aliasing is determined by
1513 * declarations present after preprocessing."
1514 *
1515 * Because of this rule, we don't remove dead attributes before
1516 * attribute assignment for vertex shader inputs here.
1517 */
1518 if (!(prog->IsES && prog->GLSL_Version >= 300 && i == MESA_SHADER_VERTEX))
1519 remove_dead_varyings_pre_linking(prog->_LinkedShaders[i]->Program->nir);
1520 }
1521 }
1522
1523 if (!gl_assign_attribute_or_color_locations(consts, prog))
1524 return false;
1525
1526 if (!prelink_lowering(consts, exts, prog, linked_shader, num_shaders))
1527 return false;
1528
1529 if (!gl_nir_link_varyings(consts, exts, api, prog))
1530 return false;
1531
1532 /* Validation for special cases where we allow sampler array indexing
1533 * with loop induction variable. This check emits a warning or error
1534 * depending if backend can handle dynamic indexing.
1535 */
1536 if ((!prog->IsES && prog->GLSL_Version < 130) ||
1537 (prog->IsES && prog->GLSL_Version < 300)) {
1538 if (!validate_sampler_array_indexing(consts, prog))
1539 return false;
1540 }
1541
1542 if (prog->data->LinkStatus == LINKING_FAILURE)
1543 return false;
1544
1545 /* Linking the stages in the opposite order (from fragment to vertex)
1546 * ensures that inter-shader outputs written to in an earlier stage
1547 * are eliminated if they are (transitively) not used in a later
1548 * stage.
1549 */
1550 for (int i = num_shaders - 2; i >= 0; i--) {
1551 gl_nir_link_opts(linked_shader[i]->Program->nir,
1552 linked_shader[i + 1]->Program->nir);
1553 }
1554
1555 /* Tidy up any left overs from the linking process for single shaders.
1556 * For example varying arrays that get packed may have dead elements that
1557 * can be now be eliminated now that array access has been lowered.
1558 */
1559 if (num_shaders == 1)
1560 gl_nir_opts(linked_shader[0]->Program->nir);
1561
1562 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1563 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
1564 if (shader) {
1565 if (consts->GLSLLowerConstArrays) {
1566 nir_lower_const_arrays_to_uniforms(shader->Program->nir,
1567 consts->Program[i].MaxUniformComponents);
1568 }
1569
1570 const nir_remove_dead_variables_options opts = {
1571 .can_remove_var = can_remove_var,
1572 };
1573 nir_remove_dead_variables(shader->Program->nir,
1574 nir_var_uniform | nir_var_image |
1575 nir_var_mem_ubo | nir_var_mem_ssbo |
1576 nir_var_system_value,
1577 &opts);
1578
1579 if (shader->Program->info.stage == MESA_SHADER_FRAGMENT) {
1580 nir_shader *nir = shader->Program->nir;
1581 nir_foreach_variable_in_shader(var, nir) {
1582 if (var->data.mode == nir_var_system_value &&
1583 (var->data.location == SYSTEM_VALUE_SAMPLE_ID ||
1584 var->data.location == SYSTEM_VALUE_SAMPLE_POS))
1585 nir->info.fs.uses_sample_shading = true;
1586
1587 if (var->data.mode == nir_var_shader_in && var->data.sample)
1588 nir->info.fs.uses_sample_shading = true;
1589
1590 if (var->data.mode == nir_var_shader_out &&
1591 var->data.fb_fetch_output)
1592 nir->info.fs.uses_sample_shading = true;
1593 }
1594 }
1595 }
1596 }
1597
1598 if (!gl_nir_link_uniform_blocks(consts, prog))
1599 return false;
1600
1601 if (!gl_nir_link_uniforms(consts, prog, true))
1602 return false;
1603
1604 link_util_calculate_subroutine_compat(prog);
1605 link_util_check_uniform_resources(consts, prog);
1606 link_util_check_subroutine_resources(prog);
1607 check_image_resources(consts, exts, prog);
1608 gl_nir_link_assign_atomic_counter_resources(consts, prog);
1609 gl_nir_link_check_atomic_counter_resources(consts, prog);
1610
1611 /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
1612 * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
1613 * anything about shader linking when one of the shaders (vertex or
1614 * fragment shader) is absent. So, the extension shouldn't change the
1615 * behavior specified in GLSL specification.
1616 *
1617 * From OpenGL ES 3.1 specification (7.3 Program Objects):
1618 * "Linking can fail for a variety of reasons as specified in the
1619 * OpenGL ES Shading Language Specification, as well as any of the
1620 * following reasons:
1621 *
1622 * ...
1623 *
1624 * * program contains objects to form either a vertex shader or
1625 * fragment shader, and program is not separable, and does not
1626 * contain objects to form both a vertex shader and fragment
1627 * shader."
1628 *
1629 * However, the only scenario in 3.1+ where we don't require them both is
1630 * when we have a compute shader. For example:
1631 *
1632 * - No shaders is a link error.
1633 * - Geom or Tess without a Vertex shader is a link error which means we
1634 * always require a Vertex shader and hence a Fragment shader.
1635 * - Finally a Compute shader linked with any other stage is a link error.
1636 */
1637 if (!prog->SeparateShader && _mesa_is_api_gles2(api) &&
1638 !prog->_LinkedShaders[MESA_SHADER_COMPUTE]) {
1639 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
1640 linker_error(prog, "program lacks a vertex shader\n");
1641 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
1642 linker_error(prog, "program lacks a fragment shader\n");
1643 }
1644 }
1645
1646 if (prog->data->LinkStatus == LINKING_FAILURE)
1647 return false;
1648
1649 return true;
1650 }
1651