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 "nir_xfb_info.h"
27 #include "gl_nir.h"
28 #include "gl_nir_linker.h"
29 #include "gl_nir_link_varyings.h"
30 #include "linker_util.h"
31 #include "string_to_uint_map.h"
32 #include "main/shader_types.h"
33 #include "main/consts_exts.h"
34 #include "main/context.h"
35 #include "main/shaderobj.h"
36 #include "ir_uniform.h" /* for gl_uniform_storage */
37 #include "util/glheader.h"
38 #include "util/perf/cpu_trace.h"
39
40 /**
41 * This file included general link methods, using NIR, instead of IR as
42 * the counter-part glsl/linker.cpp
43 */
44
45 void
gl_nir_opts(nir_shader * nir)46 gl_nir_opts(nir_shader *nir)
47 {
48 bool progress;
49
50 MESA_TRACE_FUNC();
51
52 do {
53 progress = false;
54
55 NIR_PASS(_, nir, nir_lower_vars_to_ssa);
56
57 /* Linking deals with unused inputs/outputs, but here we can remove
58 * things local to the shader in the hopes that we can cleanup other
59 * things. This pass will also remove variables with only stores, so we
60 * might be able to make progress after it.
61 */
62 NIR_PASS(progress, nir, nir_remove_dead_variables,
63 nir_var_function_temp | nir_var_shader_temp |
64 nir_var_mem_shared,
65 NULL);
66
67 NIR_PASS(progress, nir, nir_opt_find_array_copies);
68 NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
69 NIR_PASS(progress, nir, nir_opt_dead_write_vars);
70
71 if (nir->options->lower_to_scalar) {
72 NIR_PASS(_, nir, nir_lower_alu_to_scalar,
73 nir->options->lower_to_scalar_filter, NULL);
74 NIR_PASS(_, nir, nir_lower_phis_to_scalar, false);
75 }
76
77 NIR_PASS(_, nir, nir_lower_alu);
78 NIR_PASS(_, nir, nir_lower_pack);
79 NIR_PASS(progress, nir, nir_copy_prop);
80 NIR_PASS(progress, nir, nir_opt_remove_phis);
81 NIR_PASS(progress, nir, nir_opt_dce);
82
83 bool opt_loop_progress = false;
84 NIR_PASS(opt_loop_progress, nir, nir_opt_loop);
85 if (opt_loop_progress) {
86 progress = true;
87 NIR_PASS(progress, nir, nir_copy_prop);
88 NIR_PASS(progress, nir, nir_opt_dce);
89 }
90 NIR_PASS(progress, nir, nir_opt_if, 0);
91 NIR_PASS(progress, nir, nir_opt_dead_cf);
92 NIR_PASS(progress, nir, nir_opt_cse);
93 NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
94
95 NIR_PASS(progress, nir, nir_opt_phi_precision);
96 NIR_PASS(progress, nir, nir_opt_algebraic);
97 NIR_PASS(progress, nir, nir_opt_constant_folding);
98
99 if (!nir->info.flrp_lowered) {
100 unsigned lower_flrp =
101 (nir->options->lower_flrp16 ? 16 : 0) |
102 (nir->options->lower_flrp32 ? 32 : 0) |
103 (nir->options->lower_flrp64 ? 64 : 0);
104
105 if (lower_flrp) {
106 bool lower_flrp_progress = false;
107
108 NIR_PASS(lower_flrp_progress, nir, nir_lower_flrp,
109 lower_flrp,
110 false /* always_precise */);
111 if (lower_flrp_progress) {
112 NIR_PASS(progress, nir,
113 nir_opt_constant_folding);
114 progress = true;
115 }
116 }
117
118 /* Nothing should rematerialize any flrps, so we only need to do this
119 * lowering once.
120 */
121 nir->info.flrp_lowered = true;
122 }
123
124 NIR_PASS(progress, nir, nir_opt_undef);
125 NIR_PASS(progress, nir, nir_opt_conditional_discard);
126 if (nir->options->max_unroll_iterations ||
127 (nir->options->max_unroll_iterations_fp64 &&
128 (nir->options->lower_doubles_options & nir_lower_fp64_full_software))) {
129 NIR_PASS(progress, nir, nir_opt_loop_unroll);
130 }
131 } while (progress);
132
133 NIR_PASS(_, nir, nir_lower_var_copies);
134 }
135
136 static void
replace_tex_src(nir_tex_src * dst,nir_tex_src_type src_type,nir_def * src_def,nir_instr * src_parent)137 replace_tex_src(nir_tex_src *dst, nir_tex_src_type src_type, nir_def *src_def,
138 nir_instr *src_parent)
139 {
140 *dst = nir_tex_src_for_ssa(src_type, src_def);
141 nir_src_set_parent_instr(&dst->src, src_parent);
142 list_addtail(&dst->src.use_link, &dst->src.ssa->uses);
143 }
144
145 void
gl_nir_inline_functions(nir_shader * shader)146 gl_nir_inline_functions(nir_shader *shader)
147 {
148 /* We have to lower away local constant initializers right before we
149 * inline functions. That way they get properly initialized at the top
150 * of the function and not at the top of its caller.
151 */
152 NIR_PASS(_, shader, nir_lower_variable_initializers, nir_var_all);
153 NIR_PASS(_, shader, nir_lower_returns);
154 NIR_PASS(_, shader, nir_inline_functions);
155 NIR_PASS(_, shader, nir_opt_deref);
156
157 /* We set func->is_entrypoint after nir_function_create if the function
158 * is named "main", so we can use nir_remove_non_entrypoints() for this.
159 * Now that we have inlined everything remove all of the functions except
160 * func->is_entrypoint.
161 */
162 nir_remove_non_entrypoints(shader);
163
164 /* Now that functions have been inlined remove deref_texture_src intrinisic
165 * as we can now see if the texture source is bindless or not.
166 */
167 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
168 nir_builder b = nir_builder_create(impl);
169
170 nir_foreach_block(block, impl) {
171 nir_foreach_instr_safe(instr, block) {
172 if (instr->type == nir_instr_type_tex) {
173 nir_tex_instr *intr = nir_instr_as_tex(instr);
174
175 b.cursor = nir_before_instr(instr);
176
177 if (intr->src[0].src_type == nir_tex_src_sampler_deref_intrinsic) {
178 assert(intr->src[1].src_type == nir_tex_src_texture_deref_intrinsic);
179 nir_intrinsic_instr *intrin =
180 nir_instr_as_intrinsic(intr->src[0].src.ssa->parent_instr);
181 nir_deref_instr *deref =
182 nir_instr_as_deref(intrin->src[0].ssa->parent_instr);
183
184 /* check for bindless handles */
185 if (!nir_deref_mode_is(deref, nir_var_uniform) ||
186 nir_deref_instr_get_variable(deref)->data.bindless) {
187 nir_def *load = nir_load_deref(&b, deref);
188 replace_tex_src(&intr->src[0], nir_tex_src_texture_handle,
189 load, instr);
190 replace_tex_src(&intr->src[1], nir_tex_src_sampler_handle,
191 load, instr);
192 } else {
193 replace_tex_src(&intr->src[0], nir_tex_src_texture_deref,
194 &deref->def, instr);
195 replace_tex_src(&intr->src[1], nir_tex_src_sampler_deref,
196 &deref->def, instr);
197 }
198 nir_instr_remove(&intrin->instr);
199 }
200 }
201 }
202 }
203
204 nir_validate_shader(shader, "after function inlining and return lowering");
205 }
206
207 struct emit_vertex_state {
208 int max_stream_allowed;
209 int invalid_stream_id;
210 bool invalid_stream_id_from_emit_vertex;
211 bool end_primitive_found;
212 unsigned used_streams;
213 };
214
215 /**
216 * Determine the highest stream id to which a (geometry) shader emits
217 * vertices. Also check whether End{Stream}Primitive is ever called.
218 */
219 static void
find_emit_vertex(struct emit_vertex_state * state,nir_shader * shader)220 find_emit_vertex(struct emit_vertex_state *state, nir_shader *shader) {
221 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
222
223 nir_foreach_block_safe(block, impl) {
224 nir_foreach_instr_safe(instr, block) {
225 if (instr->type == nir_instr_type_intrinsic) {
226 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
227
228 if (intr->intrinsic == nir_intrinsic_emit_vertex ||
229 intr->intrinsic == nir_intrinsic_end_primitive) {
230 int stream_id = nir_intrinsic_stream_id(intr);
231 bool from_emit_vertex =
232 intr->intrinsic == nir_intrinsic_emit_vertex;
233 state->end_primitive_found |=
234 intr->intrinsic == nir_intrinsic_end_primitive;
235
236 if (stream_id < 0) {
237 state->invalid_stream_id = stream_id;
238 state->invalid_stream_id_from_emit_vertex = from_emit_vertex;
239 return;
240 }
241
242 if (stream_id > state->max_stream_allowed) {
243 state->invalid_stream_id = stream_id;
244 state->invalid_stream_id_from_emit_vertex = from_emit_vertex;
245 return;
246 }
247
248 state->used_streams |= 1 << stream_id;
249 }
250 }
251 }
252 }
253 }
254
255 /**
256 * Check if geometry shaders emit to non-zero streams and do corresponding
257 * validations.
258 */
259 static void
validate_geometry_shader_emissions(const struct gl_constants * consts,struct gl_shader_program * prog)260 validate_geometry_shader_emissions(const struct gl_constants *consts,
261 struct gl_shader_program *prog)
262 {
263 struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
264
265 if (sh != NULL) {
266 struct emit_vertex_state state;
267 state.max_stream_allowed = consts->MaxVertexStreams - 1;
268 state.invalid_stream_id = 0;
269 state.invalid_stream_id_from_emit_vertex = false;
270 state.end_primitive_found = false;
271 state.used_streams = 0;
272
273 find_emit_vertex(&state, sh->Program->nir);
274
275 if (state.invalid_stream_id != 0) {
276 linker_error(prog, "Invalid call %s(%d). Accepted values for the "
277 "stream parameter are in the range [0, %d].\n",
278 state.invalid_stream_id_from_emit_vertex ?
279 "EmitStreamVertex" : "EndStreamPrimitive",
280 state.invalid_stream_id, state.max_stream_allowed);
281 }
282 sh->Program->nir->info.gs.active_stream_mask = state.used_streams;
283 sh->Program->nir->info.gs.uses_end_primitive = state.end_primitive_found;
284
285 /* From the ARB_gpu_shader5 spec:
286 *
287 * "Multiple vertex streams are supported only if the output primitive
288 * type is declared to be "points". A program will fail to link if it
289 * contains a geometry shader calling EmitStreamVertex() or
290 * EndStreamPrimitive() if its output primitive type is not "points".
291 *
292 * However, in the same spec:
293 *
294 * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
295 * with <stream> set to zero."
296 *
297 * And:
298 *
299 * "The function EndPrimitive() is equivalent to calling
300 * EndStreamPrimitive() with <stream> set to zero."
301 *
302 * Since we can call EmitVertex() and EndPrimitive() when we output
303 * primitives other than points, calling EmitStreamVertex(0) or
304 * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
305 * does. We can use info.gs.active_stream_mask to check whether only the
306 * first (zero) stream is active.
307 * stream.
308 */
309 if (sh->Program->nir->info.gs.active_stream_mask & ~(1 << 0) &&
310 sh->Program->info.gs.output_primitive != MESA_PRIM_POINTS) {
311 linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
312 "with n>0 requires point output\n");
313 }
314 }
315 }
316
317 /**
318 * Generate a string describing the mode of a variable
319 */
320 const char *
gl_nir_mode_string(const nir_variable * var)321 gl_nir_mode_string(const nir_variable *var)
322 {
323 switch (var->data.mode) {
324 case nir_var_shader_temp:
325 return (var->data.read_only) ? "global constant" : "global variable";
326
327 case nir_var_uniform:
328 case nir_var_image:
329 case nir_var_mem_ubo:
330 return "uniform";
331
332 case nir_var_mem_ssbo:
333 return "buffer";
334
335 case nir_var_shader_in:
336 return "shader input";
337
338 case nir_var_shader_out:
339 return "shader output";
340
341 case nir_var_system_value:
342 return "shader input";
343
344 case nir_var_function_temp:
345 return "local variable";
346
347 case nir_var_mem_shared:
348 return "shader shared";
349
350 case nir_num_variable_modes:
351 break;
352 }
353
354 assert(!"Should not get here.");
355 return "invalid variable";
356 }
357
358 static void
remove_dead_functions(nir_shader * shader)359 remove_dead_functions(nir_shader *shader)
360 {
361 struct set *fn_set =
362 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
363
364 /* Find all function prototypes adding them to a list then removing them
365 * if they are ever called.
366 */
367 nir_foreach_function_impl(impl, shader) {
368 _mesa_set_add(fn_set, impl->function);
369 }
370
371 nir_foreach_function_impl(impl, shader) {
372 nir_foreach_block(block, impl) {
373 nir_foreach_instr(instr, block) {
374 if (instr->type == nir_instr_type_call) {
375 nir_call_instr *call = nir_instr_as_call(instr);
376 _mesa_set_remove_key(fn_set, call->callee);
377 }
378 }
379 }
380 }
381
382 /* Any functions remaining in the list must be unused so remove them. */
383 set_foreach(fn_set, entry) {
384 nir_function *func = (nir_function *) entry->key;
385 if (!func->is_entrypoint)
386 exec_node_remove(&func->node);
387 }
388
389 _mesa_set_destroy(fn_set, NULL);
390 }
391
392 bool
gl_nir_can_add_pointsize_to_program(const struct gl_constants * consts,struct gl_program * prog)393 gl_nir_can_add_pointsize_to_program(const struct gl_constants *consts,
394 struct gl_program *prog)
395 {
396 nir_shader *nir = prog->nir;
397 if (!nir)
398 return true; /* fixedfunction */
399
400 assert(nir->info.stage == MESA_SHADER_VERTEX ||
401 nir->info.stage == MESA_SHADER_TESS_EVAL ||
402 nir->info.stage == MESA_SHADER_GEOMETRY);
403 if (nir->info.outputs_written & VARYING_BIT_PSIZ)
404 return false;
405
406 unsigned max_components = nir->info.stage == MESA_SHADER_GEOMETRY ?
407 consts->MaxGeometryTotalOutputComponents :
408 consts->Program[nir->info.stage].MaxOutputComponents;
409 unsigned num_components = 0;
410 unsigned needed_components = nir->info.stage == MESA_SHADER_GEOMETRY ? nir->info.gs.vertices_out : 1;
411 nir_foreach_shader_out_variable(var, nir) {
412 num_components += glsl_count_dword_slots(var->type, false);
413 }
414
415 /* Ensure that there is enough attribute space to emit at least one primitive */
416 if (num_components && nir->info.stage == MESA_SHADER_GEOMETRY) {
417 if (num_components + needed_components > consts->Program[nir->info.stage].MaxOutputComponents)
418 return false;
419 num_components *= nir->info.gs.vertices_out;
420 }
421
422 return num_components + needed_components <= max_components;
423 }
424
425 static void
gl_nir_link_opts(nir_shader * producer,nir_shader * consumer)426 gl_nir_link_opts(nir_shader *producer, nir_shader *consumer)
427 {
428 MESA_TRACE_FUNC();
429
430 if (producer->options->lower_to_scalar) {
431 NIR_PASS(_, producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
432 NIR_PASS(_, consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
433 }
434
435 nir_lower_io_arrays_to_elements(producer, consumer);
436
437 gl_nir_opts(producer);
438 gl_nir_opts(consumer);
439
440 if (nir_link_opt_varyings(producer, consumer))
441 gl_nir_opts(consumer);
442
443 NIR_PASS(_, producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
444 NIR_PASS(_, consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
445
446 if (nir_remove_unused_varyings(producer, consumer)) {
447 NIR_PASS(_, producer, nir_lower_global_vars_to_local);
448 NIR_PASS(_, consumer, nir_lower_global_vars_to_local);
449
450 gl_nir_opts(producer);
451 gl_nir_opts(consumer);
452
453 /* Optimizations can cause varyings to become unused.
454 * nir_compact_varyings() depends on all dead varyings being removed so
455 * we need to call nir_remove_dead_variables() again here.
456 */
457 NIR_PASS(_, producer, nir_remove_dead_variables, nir_var_shader_out,
458 NULL);
459 NIR_PASS(_, consumer, nir_remove_dead_variables, nir_var_shader_in,
460 NULL);
461 }
462
463 nir_link_varying_precision(producer, consumer);
464 }
465
466 static bool
can_remove_var(nir_variable * var,UNUSED void * data)467 can_remove_var(nir_variable *var, UNUSED void *data)
468 {
469 /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec
470 * says:
471 *
472 * "All members of a named uniform block declared with a shared or
473 * std140 layout qualifier are considered active, even if they are not
474 * referenced in any shader in the program. The uniform block itself is
475 * also considered active, even if no member of the block is
476 * referenced."
477 *
478 * Although the spec doesn't state it std430 layouts are expect to behave
479 * the same way. If the variable is in a uniform block with one of those
480 * layouts, do not eliminate it.
481 */
482 if (nir_variable_is_in_block(var) &&
483 (glsl_get_ifc_packing(var->interface_type) !=
484 GLSL_INTERFACE_PACKING_PACKED))
485 return false;
486
487 if (glsl_get_base_type(glsl_without_array(var->type)) ==
488 GLSL_TYPE_SUBROUTINE)
489 return false;
490
491 /* Uniform initializers could get used by another stage. However if its a
492 * hidden uniform then it should be safe to remove as this was a constant
493 * variable that has been lowered to a uniform.
494 */
495 if (var->constant_initializer && var->data.how_declared != nir_var_hidden)
496 return false;
497
498 return true;
499 }
500
501 static void
set_always_active_io(nir_shader * shader,nir_variable_mode io_mode)502 set_always_active_io(nir_shader *shader, nir_variable_mode io_mode)
503 {
504 assert(io_mode == nir_var_shader_in || io_mode == nir_var_shader_out);
505
506 nir_foreach_variable_with_modes(var, shader, io_mode) {
507 /* Don't set always active on builtins that haven't been redeclared */
508 if (var->data.how_declared == nir_var_declared_implicitly)
509 continue;
510
511 var->data.always_active_io = true;
512 }
513 }
514
515 /**
516 * When separate shader programs are enabled, only input/outputs between
517 * the stages of a multi-stage separate program can be safely removed
518 * from the shader interface. Other inputs/outputs must remain active.
519 */
520 static void
disable_varying_optimizations_for_sso(struct gl_shader_program * prog)521 disable_varying_optimizations_for_sso(struct gl_shader_program *prog)
522 {
523 unsigned first, last;
524 assert(prog->SeparateShader);
525
526 first = MESA_SHADER_STAGES;
527 last = 0;
528
529 /* Determine first and last stage. Excluding the compute stage */
530 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
531 if (!prog->_LinkedShaders[i])
532 continue;
533 if (first == MESA_SHADER_STAGES)
534 first = i;
535 last = i;
536 }
537
538 if (first == MESA_SHADER_STAGES)
539 return;
540
541 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
542 if (!prog->_LinkedShaders[stage])
543 continue;
544
545 /* Prevent the removal of inputs to the first and outputs from the last
546 * stage, unless they are the initial pipeline inputs or final pipeline
547 * outputs, respectively.
548 *
549 * The removal of IO between shaders in the same program is always
550 * allowed.
551 */
552 if (stage == first && stage != MESA_SHADER_VERTEX) {
553 set_always_active_io(prog->_LinkedShaders[stage]->Program->nir,
554 nir_var_shader_in);
555 }
556
557 if (stage == last && stage != MESA_SHADER_FRAGMENT) {
558 set_always_active_io(prog->_LinkedShaders[stage]->Program->nir,
559 nir_var_shader_out);
560 }
561 }
562 }
563
564 static bool
inout_has_same_location(const nir_variable * var,unsigned stage)565 inout_has_same_location(const nir_variable *var, unsigned stage)
566 {
567 if (!var->data.patch &&
568 ((var->data.mode == nir_var_shader_out &&
569 stage == MESA_SHADER_TESS_CTRL) ||
570 (var->data.mode == nir_var_shader_in &&
571 (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL ||
572 stage == MESA_SHADER_GEOMETRY))))
573 return true;
574 else
575 return false;
576 }
577
578 /**
579 * Create gl_shader_variable from nir_variable.
580 */
581 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)582 create_shader_variable(struct gl_shader_program *shProg,
583 const nir_variable *in,
584 const char *name, const struct glsl_type *type,
585 const struct glsl_type *interface_type,
586 bool use_implicit_location, int location,
587 const struct glsl_type *outermost_struct_type)
588 {
589 /* Allocate zero-initialized memory to ensure that bitfield padding
590 * is zero.
591 */
592 struct gl_shader_variable *out = rzalloc(shProg,
593 struct gl_shader_variable);
594 if (!out)
595 return NULL;
596
597 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
598 * expect to see gl_VertexID in the program resource list. Pretend.
599 */
600 if (in->data.mode == nir_var_system_value &&
601 in->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
602 out->name.string = ralloc_strdup(shProg, "gl_VertexID");
603 } else if ((in->data.mode == nir_var_shader_out &&
604 in->data.location == VARYING_SLOT_TESS_LEVEL_OUTER) ||
605 (in->data.mode == nir_var_system_value &&
606 in->data.location == SYSTEM_VALUE_TESS_LEVEL_OUTER)) {
607 out->name.string = ralloc_strdup(shProg, "gl_TessLevelOuter");
608 type = glsl_array_type(glsl_float_type(), 4, 0);
609 } else if ((in->data.mode == nir_var_shader_out &&
610 in->data.location == VARYING_SLOT_TESS_LEVEL_INNER) ||
611 (in->data.mode == nir_var_system_value &&
612 in->data.location == SYSTEM_VALUE_TESS_LEVEL_INNER)) {
613 out->name.string = ralloc_strdup(shProg, "gl_TessLevelInner");
614 type = glsl_array_type(glsl_float_type(), 2, 0);
615 } else {
616 out->name.string = ralloc_strdup(shProg, name);
617 }
618
619 resource_name_updated(&out->name);
620
621 if (!out->name.string)
622 return NULL;
623
624 /* The ARB_program_interface_query spec says:
625 *
626 * "Not all active variables are assigned valid locations; the
627 * following variables will have an effective location of -1:
628 *
629 * * uniforms declared as atomic counters;
630 *
631 * * members of a uniform block;
632 *
633 * * built-in inputs, outputs, and uniforms (starting with "gl_"); and
634 *
635 * * inputs or outputs not declared with a "location" layout
636 * qualifier, except for vertex shader inputs and fragment shader
637 * outputs."
638 */
639 if (glsl_get_base_type(in->type) == GLSL_TYPE_ATOMIC_UINT ||
640 is_gl_identifier(in->name) ||
641 !(in->data.explicit_location || use_implicit_location)) {
642 out->location = -1;
643 } else {
644 out->location = location;
645 }
646
647 out->type = type;
648 out->outermost_struct_type = outermost_struct_type;
649 out->interface_type = interface_type;
650 out->component = in->data.location_frac;
651 out->index = in->data.index;
652 out->patch = in->data.patch;
653 out->mode = in->data.mode;
654 out->interpolation = in->data.interpolation;
655 out->precision = in->data.precision;
656 out->explicit_location = in->data.explicit_location;
657
658 return out;
659 }
660
661 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)662 add_shader_variable(const struct gl_constants *consts,
663 struct gl_shader_program *shProg,
664 struct set *resource_set,
665 unsigned stage_mask,
666 GLenum programInterface, nir_variable *var,
667 const char *name, const struct glsl_type *type,
668 bool use_implicit_location, int location,
669 bool inouts_share_location,
670 const struct glsl_type *outermost_struct_type)
671 {
672 const struct glsl_type *interface_type = var->interface_type;
673
674 if (outermost_struct_type == NULL) {
675 if (var->data.from_named_ifc_block) {
676 const char *interface_name = glsl_get_type_name(interface_type);
677
678 if (glsl_type_is_array(interface_type)) {
679 /* Issue #16 of the ARB_program_interface_query spec says:
680 *
681 * "* If a variable is a member of an interface block without an
682 * instance name, it is enumerated using just the variable name.
683 *
684 * * If a variable is a member of an interface block with an
685 * instance name, it is enumerated as "BlockName.Member", where
686 * "BlockName" is the name of the interface block (not the
687 * instance name) and "Member" is the name of the variable."
688 *
689 * In particular, it indicates that it should be "BlockName",
690 * not "BlockName[array length]". The conformance suite and
691 * dEQP both require this behavior.
692 *
693 * Here, we unwrap the extra array level added by named interface
694 * block array lowering so we have the correct variable type. We
695 * also unwrap the interface type when constructing the name.
696 *
697 * We leave interface_type the same so that ES 3.x SSO pipeline
698 * validation can enforce the rules requiring array length to
699 * match on interface blocks.
700 */
701 type = glsl_get_array_element(type);
702
703 interface_name =
704 glsl_get_type_name(glsl_get_array_element(interface_type));
705 }
706
707 name = ralloc_asprintf(shProg, "%s.%s", interface_name, name);
708 }
709 }
710
711 switch (glsl_get_base_type(type)) {
712 case GLSL_TYPE_STRUCT: {
713 /* The ARB_program_interface_query spec says:
714 *
715 * "For an active variable declared as a structure, a separate entry
716 * will be generated for each active structure member. The name of
717 * each entry is formed by concatenating the name of the structure,
718 * the "." character, and the name of the structure member. If a
719 * structure member to enumerate is itself a structure or array,
720 * these enumeration rules are applied recursively."
721 */
722 if (outermost_struct_type == NULL)
723 outermost_struct_type = type;
724
725 unsigned field_location = location;
726 for (unsigned i = 0; i < glsl_get_length(type); i++) {
727 const struct glsl_type *field_type = glsl_get_struct_field(type, i);
728 const struct glsl_struct_field *field =
729 glsl_get_struct_field_data(type, i);
730
731 char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name);
732 if (!add_shader_variable(consts, shProg, resource_set,
733 stage_mask, programInterface,
734 var, field_name, field_type,
735 use_implicit_location, field_location,
736 false, outermost_struct_type))
737 return false;
738
739 field_location += glsl_count_attribute_slots(field_type, false);
740 }
741 return true;
742 }
743
744 case GLSL_TYPE_ARRAY: {
745 /* The ARB_program_interface_query spec says:
746 *
747 * "For an active variable declared as an array of basic types, a
748 * single entry will be generated, with its name string formed by
749 * concatenating the name of the array and the string "[0]"."
750 *
751 * "For an active variable declared as an array of an aggregate data
752 * type (structures or arrays), a separate entry will be generated
753 * for each active array element, unless noted immediately below.
754 * The name of each entry is formed by concatenating the name of
755 * the array, the "[" character, an integer identifying the element
756 * number, and the "]" character. These enumeration rules are
757 * applied recursively, treating each enumerated array element as a
758 * separate active variable."
759 */
760 const struct glsl_type *array_type = glsl_get_array_element(type);
761 if (glsl_get_base_type(array_type) == GLSL_TYPE_STRUCT ||
762 glsl_get_base_type(array_type) == GLSL_TYPE_ARRAY) {
763 unsigned elem_location = location;
764 unsigned stride = inouts_share_location ? 0 :
765 glsl_count_attribute_slots(array_type, false);
766 for (unsigned i = 0; i < glsl_get_length(type); i++) {
767 char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i);
768 if (!add_shader_variable(consts, shProg, resource_set,
769 stage_mask, programInterface,
770 var, elem, array_type,
771 use_implicit_location, elem_location,
772 false, outermost_struct_type))
773 return false;
774 elem_location += stride;
775 }
776 return true;
777 }
778 }
779 FALLTHROUGH;
780
781 default: {
782 /* The ARB_program_interface_query spec says:
783 *
784 * "For an active variable declared as a single instance of a basic
785 * type, a single entry will be generated, using the variable name
786 * from the shader source."
787 */
788 struct gl_shader_variable *sha_v =
789 create_shader_variable(shProg, var, name, type, interface_type,
790 use_implicit_location, location,
791 outermost_struct_type);
792 if (!sha_v)
793 return false;
794
795 return link_util_add_program_resource(shProg, resource_set,
796 programInterface, sha_v, stage_mask);
797 }
798 }
799 }
800
801 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)802 add_vars_with_modes(const struct gl_constants *consts,
803 struct gl_shader_program *prog, struct set *resource_set,
804 nir_shader *nir, nir_variable_mode modes,
805 unsigned stage, GLenum programInterface)
806 {
807 nir_foreach_variable_with_modes(var, nir, modes) {
808 if (var->data.how_declared == nir_var_hidden)
809 continue;
810
811 int loc_bias = 0;
812 switch(var->data.mode) {
813 case nir_var_system_value:
814 case nir_var_shader_in:
815 if (programInterface != GL_PROGRAM_INPUT)
816 continue;
817 loc_bias = (stage == MESA_SHADER_VERTEX) ? VERT_ATTRIB_GENERIC0
818 : VARYING_SLOT_VAR0;
819 break;
820 case nir_var_shader_out:
821 if (programInterface != GL_PROGRAM_OUTPUT)
822 continue;
823 loc_bias = (stage == MESA_SHADER_FRAGMENT) ? FRAG_RESULT_DATA0
824 : VARYING_SLOT_VAR0;
825 break;
826 default:
827 continue;
828 }
829
830 if (var->data.patch)
831 loc_bias = VARYING_SLOT_PATCH0;
832
833 if (prog->data->spirv) {
834 struct gl_shader_variable *sh_var =
835 rzalloc(prog, struct gl_shader_variable);
836
837 /* In the ARB_gl_spirv spec, names are considered optional debug info, so
838 * the linker needs to work without them. Returning them is optional.
839 * For simplicity, we ignore names.
840 */
841 sh_var->name.string = NULL;
842 resource_name_updated(&sh_var->name);
843 sh_var->type = var->type;
844 sh_var->location = var->data.location - loc_bias;
845 sh_var->explicit_location = var->data.explicit_location;
846 sh_var->index = var->data.index;
847
848 if (!link_util_add_program_resource(prog, resource_set,
849 programInterface,
850 sh_var, 1 << stage)) {
851 return false;
852 }
853 } else {
854 /* Skip packed varyings, packed varyings are handled separately
855 * by add_packed_varyings in the GLSL IR
856 * build_program_resource_list() call.
857 * TODO: handle packed varyings here instead. We likely want a NIR
858 * based packing pass first.
859 */
860 if (strncmp(var->name, "packed:", 7) == 0)
861 continue;
862
863 const bool vs_input_or_fs_output =
864 (stage == MESA_SHADER_VERTEX &&
865 var->data.mode == nir_var_shader_in) ||
866 (stage == MESA_SHADER_FRAGMENT &&
867 var->data.mode == nir_var_shader_out);
868
869 if (!add_shader_variable(consts, prog, resource_set,
870 1 << stage, programInterface,
871 var, var->name, var->type,
872 vs_input_or_fs_output,
873 var->data.location - loc_bias,
874 inout_has_same_location(var, stage),
875 NULL))
876 return false;
877 }
878 }
879
880 return true;
881 }
882
883 static bool
add_interface_variables(const struct gl_constants * consts,struct gl_shader_program * prog,struct set * resource_set,unsigned stage,GLenum programInterface)884 add_interface_variables(const struct gl_constants *consts,
885 struct gl_shader_program *prog,
886 struct set *resource_set,
887 unsigned stage, GLenum programInterface)
888 {
889 struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
890 if (!sh)
891 return true;
892
893 nir_shader *nir = sh->Program->nir;
894 assert(nir);
895
896 switch (programInterface) {
897 case GL_PROGRAM_INPUT: {
898 return add_vars_with_modes(consts, prog, resource_set,
899 nir, nir_var_shader_in | nir_var_system_value,
900 stage, programInterface);
901 }
902 case GL_PROGRAM_OUTPUT:
903 return add_vars_with_modes(consts, prog, resource_set,
904 nir, nir_var_shader_out,
905 stage, programInterface);
906 default:
907 assert("!Should not get here");
908 break;
909 }
910
911 return false;
912 }
913
914 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)915 nir_add_packed_var_to_resource_list(const struct gl_constants *consts,
916 struct gl_shader_program *shProg,
917 struct set *resource_set,
918 nir_variable *var,
919 unsigned stage, GLenum type)
920 {
921 if (!add_shader_variable(consts, shProg, resource_set, 1 << stage,
922 type, var, var->name, var->type, false,
923 var->data.location - VARYING_SLOT_VAR0,
924 inout_has_same_location(var, stage), NULL))
925 return false;
926
927 return true;
928 }
929
930 /**
931 * Initilise list of program resources that point to resource data.
932 */
933 void
init_program_resource_list(struct gl_shader_program * prog)934 init_program_resource_list(struct gl_shader_program *prog)
935 {
936 /* Rebuild resource list. */
937 if (prog->data->ProgramResourceList) {
938 ralloc_free(prog->data->ProgramResourceList);
939 prog->data->ProgramResourceList = NULL;
940 prog->data->NumProgramResourceList = 0;
941 }
942 }
943
944 void
nir_build_program_resource_list(const struct gl_constants * consts,struct gl_shader_program * prog,bool rebuild_resourse_list)945 nir_build_program_resource_list(const struct gl_constants *consts,
946 struct gl_shader_program *prog,
947 bool rebuild_resourse_list)
948 {
949 /* Rebuild resource list. */
950 if (rebuild_resourse_list)
951 init_program_resource_list(prog);
952
953 int input_stage = MESA_SHADER_STAGES, output_stage = 0;
954
955 /* Determine first input and final output stage. These are used to
956 * detect which variables should be enumerated in the resource list
957 * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
958 */
959 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
960 if (!prog->_LinkedShaders[i])
961 continue;
962 if (input_stage == MESA_SHADER_STAGES)
963 input_stage = i;
964 output_stage = i;
965 }
966
967 /* Empty shader, no resources. */
968 if (input_stage == MESA_SHADER_STAGES && output_stage == 0)
969 return;
970
971 struct set *resource_set = _mesa_pointer_set_create(NULL);
972
973 /* Add inputs and outputs to the resource list. */
974 if (!add_interface_variables(consts, prog, resource_set, input_stage,
975 GL_PROGRAM_INPUT)) {
976 return;
977 }
978
979 if (!add_interface_variables(consts, prog, resource_set, output_stage,
980 GL_PROGRAM_OUTPUT)) {
981 return;
982 }
983
984 /* Add transform feedback varyings and buffers. */
985 if (prog->last_vert_prog) {
986 struct gl_transform_feedback_info *linked_xfb =
987 prog->last_vert_prog->sh.LinkedTransformFeedback;
988
989 /* Add varyings. */
990 if (linked_xfb->NumVarying > 0) {
991 for (int i = 0; i < linked_xfb->NumVarying; i++) {
992 if (!link_util_add_program_resource(prog, resource_set,
993 GL_TRANSFORM_FEEDBACK_VARYING,
994 &linked_xfb->Varyings[i], 0))
995 return;
996 }
997 }
998
999 /* Add buffers. */
1000 for (unsigned i = 0; i < consts->MaxTransformFeedbackBuffers; i++) {
1001 if ((linked_xfb->ActiveBuffers >> i) & 1) {
1002 linked_xfb->Buffers[i].Binding = i;
1003 if (!link_util_add_program_resource(prog, resource_set,
1004 GL_TRANSFORM_FEEDBACK_BUFFER,
1005 &linked_xfb->Buffers[i], 0))
1006 return;
1007 }
1008 }
1009 }
1010
1011 /* Add uniforms
1012 *
1013 * Here, it is expected that nir_link_uniforms() has already been
1014 * called, so that UniformStorage table is already available.
1015 */
1016 int top_level_array_base_offset = -1;
1017 int top_level_array_size_in_bytes = -1;
1018 int second_element_offset = -1;
1019 int block_index = -1;
1020 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1021 struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
1022
1023 if (uniform->hidden) {
1024 for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) {
1025 if (!uniform->opaque[j].active ||
1026 glsl_get_base_type(uniform->type) != GLSL_TYPE_SUBROUTINE)
1027 continue;
1028
1029 GLenum type =
1030 _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j);
1031 /* add shader subroutines */
1032 if (!link_util_add_program_resource(prog, resource_set,
1033 type, uniform, 0))
1034 return;
1035 }
1036
1037 continue;
1038 }
1039
1040 if (!link_util_should_add_buffer_variable(prog, uniform,
1041 top_level_array_base_offset,
1042 top_level_array_size_in_bytes,
1043 second_element_offset, block_index))
1044 continue;
1045
1046
1047 if (prog->data->UniformStorage[i].offset >= second_element_offset) {
1048 top_level_array_base_offset =
1049 prog->data->UniformStorage[i].offset;
1050
1051 top_level_array_size_in_bytes =
1052 prog->data->UniformStorage[i].top_level_array_size *
1053 prog->data->UniformStorage[i].top_level_array_stride;
1054
1055 /* Set or reset the second element offset. For non arrays this
1056 * will be set to -1.
1057 */
1058 second_element_offset = top_level_array_size_in_bytes ?
1059 top_level_array_base_offset +
1060 prog->data->UniformStorage[i].top_level_array_stride : -1;
1061 }
1062 block_index = uniform->block_index;
1063
1064
1065 GLenum interface = uniform->is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM;
1066 if (!link_util_add_program_resource(prog, resource_set, interface, uniform,
1067 uniform->active_shader_mask)) {
1068 return;
1069 }
1070 }
1071
1072
1073 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
1074 if (!link_util_add_program_resource(prog, resource_set, GL_UNIFORM_BLOCK,
1075 &prog->data->UniformBlocks[i],
1076 prog->data->UniformBlocks[i].stageref))
1077 return;
1078 }
1079
1080 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
1081 if (!link_util_add_program_resource(prog, resource_set, GL_SHADER_STORAGE_BLOCK,
1082 &prog->data->ShaderStorageBlocks[i],
1083 prog->data->ShaderStorageBlocks[i].stageref))
1084 return;
1085 }
1086
1087 /* Add atomic counter buffers. */
1088 for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
1089 if (!link_util_add_program_resource(prog, resource_set, GL_ATOMIC_COUNTER_BUFFER,
1090 &prog->data->AtomicBuffers[i], 0))
1091 return;
1092 }
1093
1094 unsigned mask = prog->data->linked_stages;
1095 while (mask) {
1096 const int i = u_bit_scan(&mask);
1097 struct gl_program *p = prog->_LinkedShaders[i]->Program;
1098
1099 GLuint type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i);
1100 for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
1101 if (!link_util_add_program_resource(prog, resource_set,
1102 type,
1103 &p->sh.SubroutineFunctions[j],
1104 0))
1105 return;
1106 }
1107 }
1108
1109 _mesa_set_destroy(resource_set, NULL);
1110 }
1111
1112 static void
shared_type_info(const struct glsl_type * type,unsigned * size,unsigned * align)1113 shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
1114 {
1115 assert(glsl_type_is_vector_or_scalar(type));
1116
1117 uint32_t comp_size = glsl_type_is_boolean(type)
1118 ? 4 : glsl_get_bit_size(type) / 8;
1119 unsigned length = glsl_get_vector_elements(type);
1120 *size = comp_size * length,
1121 *align = comp_size * (length == 3 ? 4 : length);
1122 }
1123
1124 static bool
can_remove_varying_before_linking(nir_variable * var,void * data)1125 can_remove_varying_before_linking(nir_variable *var, void *data)
1126 {
1127 bool *is_sso = (bool *) data;
1128 if (*is_sso) {
1129 /* Allow the removal of unused builtins in SSO */
1130 return var->data.location > -1 && var->data.location < VARYING_SLOT_VAR0;
1131 } else
1132 return true;
1133 }
1134
1135 static void
remove_dead_varyings_pre_linking(nir_shader * nir)1136 remove_dead_varyings_pre_linking(nir_shader *nir)
1137 {
1138 struct nir_remove_dead_variables_options opts;
1139 bool is_sso = nir->info.separate_shader;
1140 opts.can_remove_var_data = &is_sso;
1141 opts.can_remove_var = &can_remove_varying_before_linking;
1142 nir_variable_mode mask = nir_var_shader_in | nir_var_shader_out;
1143 nir_remove_dead_variables(nir, mask, &opts);
1144 }
1145
1146 /* - create a gl_PointSize variable
1147 * - find every gl_Position write
1148 * - store 1.0 to gl_PointSize after every gl_Position write
1149 */
1150 bool
gl_nir_add_point_size(nir_shader * nir)1151 gl_nir_add_point_size(nir_shader *nir)
1152 {
1153 nir_variable *psiz = nir_create_variable_with_location(nir, nir_var_shader_out,
1154 VARYING_SLOT_PSIZ, glsl_float_type());
1155 psiz->data.how_declared = nir_var_hidden;
1156
1157 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1158 nir_builder b = nir_builder_create(impl);
1159 bool found = false;
1160 nir_foreach_block_safe(block, impl) {
1161 nir_foreach_instr_safe(instr, block) {
1162 if (instr->type == nir_instr_type_intrinsic) {
1163 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
1164 if (intr->intrinsic == nir_intrinsic_store_deref ||
1165 intr->intrinsic == nir_intrinsic_copy_deref) {
1166 nir_variable *var = nir_intrinsic_get_var(intr, 0);
1167 if (var->data.location == VARYING_SLOT_POS) {
1168 b.cursor = nir_after_instr(instr);
1169 nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
1170 nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
1171 found = true;
1172 }
1173 }
1174 }
1175 }
1176 }
1177 if (!found) {
1178 b.cursor = nir_before_impl(impl);
1179 nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
1180 nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
1181 }
1182
1183 nir->info.outputs_written |= VARYING_BIT_PSIZ;
1184
1185 /* We always modify the entrypoint */
1186 nir_metadata_preserve(impl, nir_metadata_control_flow);
1187 return true;
1188 }
1189
1190 static void
zero_array_members(nir_builder * b,nir_variable * var)1191 zero_array_members(nir_builder *b, nir_variable *var)
1192 {
1193 nir_deref_instr *deref = nir_build_deref_var(b, var);
1194 nir_def *zero = nir_imm_zero(b, 4, 32);
1195 for (int i = 0; i < glsl_array_size(var->type); i++) {
1196 nir_deref_instr *arr = nir_build_deref_array_imm(b, deref, i);
1197 uint32_t mask = BITFIELD_MASK(glsl_get_vector_elements(arr->type));
1198 nir_store_deref(b, arr, nir_channels(b, zero, mask), mask);
1199 }
1200 }
1201
1202 /* GL has an implicit default of 0 for unwritten gl_ClipDistance members;
1203 * to achieve this, write 0 to all members at the start of the shader and
1204 * let them be naturally overwritten later
1205 */
1206 static bool
gl_nir_zero_initialize_clip_distance(nir_shader * nir)1207 gl_nir_zero_initialize_clip_distance(nir_shader *nir)
1208 {
1209 nir_variable *clip_dist0 = nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_CLIP_DIST0);
1210 nir_variable *clip_dist1 = nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_CLIP_DIST1);
1211 if (!clip_dist0 && !clip_dist1)
1212 return false;
1213
1214 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1215 nir_builder b = nir_builder_at(nir_before_impl(impl));
1216 if (clip_dist0)
1217 zero_array_members(&b, clip_dist0);
1218
1219 if (clip_dist1)
1220 zero_array_members(&b, clip_dist1);
1221
1222 nir_metadata_preserve(impl, nir_metadata_control_flow);
1223 return true;
1224 }
1225
1226 static void
lower_patch_vertices_in(struct gl_shader_program * shader_prog)1227 lower_patch_vertices_in(struct gl_shader_program *shader_prog)
1228 {
1229 struct gl_linked_shader *linked_tcs =
1230 shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL];
1231 struct gl_linked_shader *linked_tes =
1232 shader_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
1233
1234 /* If we have a TCS and TES linked together, lower TES patch vertices. */
1235 if (linked_tcs && linked_tes) {
1236 nir_shader *tcs_nir = linked_tcs->Program->nir;
1237 nir_shader *tes_nir = linked_tes->Program->nir;
1238
1239 /* The TES input vertex count is the TCS output vertex count,
1240 * lower TES gl_PatchVerticesIn to a constant.
1241 */
1242 uint32_t tes_patch_verts = tcs_nir->info.tess.tcs_vertices_out;
1243 NIR_PASS(_, tes_nir, nir_lower_patch_vertices, tes_patch_verts, NULL);
1244 }
1245 }
1246
1247 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)1248 preprocess_shader(const struct gl_constants *consts,
1249 const struct gl_extensions *exts,
1250 struct gl_program *prog,
1251 struct gl_shader_program *shader_program,
1252 gl_shader_stage stage)
1253 {
1254 const struct gl_shader_compiler_options *gl_options =
1255 &consts->ShaderCompilerOptions[prog->info.stage];
1256 const nir_shader_compiler_options *options = gl_options->NirOptions;
1257 assert(options);
1258
1259 nir_shader *nir = prog->nir;
1260
1261 if (prog->info.stage == MESA_SHADER_FRAGMENT && consts->HasFBFetch) {
1262 nir_shader_gather_info(prog->nir, nir_shader_get_entrypoint(prog->nir));
1263 NIR_PASS(_, prog->nir, gl_nir_lower_blend_equation_advanced,
1264 exts->KHR_blend_equation_advanced_coherent);
1265 nir_lower_global_vars_to_local(prog->nir);
1266 NIR_PASS(_, prog->nir, nir_opt_combine_stores, nir_var_shader_out);
1267 }
1268
1269 /* Set the next shader stage hint for VS and TES. */
1270 if (!nir->info.separate_shader &&
1271 (nir->info.stage == MESA_SHADER_VERTEX ||
1272 nir->info.stage == MESA_SHADER_TESS_EVAL)) {
1273
1274 unsigned prev_stages = (1 << (prog->info.stage + 1)) - 1;
1275 unsigned stages_mask =
1276 ~prev_stages & shader_program->data->linked_stages;
1277
1278 nir->info.next_stage = stages_mask ?
1279 (gl_shader_stage) u_bit_scan(&stages_mask) : MESA_SHADER_FRAGMENT;
1280 } else {
1281 nir->info.next_stage = MESA_SHADER_FRAGMENT;
1282 }
1283
1284 prog->skip_pointsize_xfb = !(nir->info.outputs_written & VARYING_BIT_PSIZ);
1285 if (!consts->PointSizeFixed && prog->skip_pointsize_xfb &&
1286 stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&
1287 gl_nir_can_add_pointsize_to_program(consts, prog)) {
1288 NIR_PASS(_, nir, gl_nir_add_point_size);
1289 }
1290
1291 if (stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&
1292 (nir->info.outputs_written & (VARYING_BIT_CLIP_DIST0 | VARYING_BIT_CLIP_DIST1)))
1293 NIR_PASS(_, nir, gl_nir_zero_initialize_clip_distance);
1294
1295 if (options->lower_all_io_to_temps ||
1296 nir->info.stage == MESA_SHADER_VERTEX ||
1297 nir->info.stage == MESA_SHADER_GEOMETRY) {
1298 NIR_PASS(_, nir, nir_lower_io_to_temporaries,
1299 nir_shader_get_entrypoint(nir),
1300 true, true);
1301 } else if (nir->info.stage == MESA_SHADER_TESS_EVAL ||
1302 nir->info.stage == MESA_SHADER_FRAGMENT) {
1303 NIR_PASS(_, nir, nir_lower_io_to_temporaries,
1304 nir_shader_get_entrypoint(nir),
1305 true, false);
1306 }
1307
1308 NIR_PASS(_, nir, nir_lower_global_vars_to_local);
1309 NIR_PASS(_, nir, nir_split_var_copies);
1310 NIR_PASS(_, nir, nir_lower_var_copies);
1311
1312 if (gl_options->LowerPrecisionFloat16 && gl_options->LowerPrecisionInt16) {
1313 NIR_PASS(_, nir, nir_lower_mediump_vars, nir_var_function_temp | nir_var_shader_temp | nir_var_mem_shared);
1314 }
1315
1316 if (options->lower_to_scalar) {
1317 NIR_PASS(_, nir, nir_remove_dead_variables,
1318 nir_var_function_temp | nir_var_shader_temp |
1319 nir_var_mem_shared, NULL);
1320 NIR_PASS(_, nir, nir_opt_copy_prop_vars);
1321 NIR_PASS(_, nir, nir_lower_alu_to_scalar,
1322 options->lower_to_scalar_filter, NULL);
1323 }
1324
1325 NIR_PASS(_, nir, nir_opt_barrier_modes);
1326
1327 /* before buffers and vars_to_ssa */
1328 NIR_PASS(_, nir, gl_nir_lower_images, true);
1329
1330 if (prog->nir->info.stage == MESA_SHADER_COMPUTE) {
1331 NIR_PASS(_, prog->nir, nir_lower_vars_to_explicit_types,
1332 nir_var_mem_shared, shared_type_info);
1333 NIR_PASS(_, prog->nir, nir_lower_explicit_io,
1334 nir_var_mem_shared, nir_address_format_32bit_offset);
1335 }
1336
1337 /* Do a round of constant folding to clean up address calculations */
1338 NIR_PASS(_, nir, nir_opt_constant_folding);
1339 }
1340
1341 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)1342 prelink_lowering(const struct gl_constants *consts,
1343 const struct gl_extensions *exts,
1344 struct gl_shader_program *shader_program,
1345 struct gl_linked_shader **linked_shader, unsigned num_shaders)
1346 {
1347 for (unsigned i = 0; i < num_shaders; i++) {
1348 struct gl_linked_shader *shader = linked_shader[i];
1349 const nir_shader_compiler_options *options =
1350 consts->ShaderCompilerOptions[shader->Stage].NirOptions;
1351 struct gl_program *prog = shader->Program;
1352
1353 /* NIR drivers that support tess shaders and compact arrays need to use
1354 * GLSLTessLevelsAsInputs / PIPE_CAP_GLSL_TESS_LEVELS_AS_INPUTS. The NIR
1355 * linker doesn't support linking these as compat arrays of sysvals.
1356 */
1357 assert(consts->GLSLTessLevelsAsInputs || !options->compact_arrays ||
1358 !exts->ARB_tessellation_shader);
1359
1360
1361 /* ES 3.0+ vertex shaders may still have dead varyings but its now safe
1362 * to remove them as validation is now done according to the spec.
1363 */
1364 if (shader_program->IsES && shader_program->GLSL_Version >= 300 &&
1365 i == MESA_SHADER_VERTEX)
1366 remove_dead_varyings_pre_linking(prog->nir);
1367
1368 preprocess_shader(consts, exts, prog, shader_program, shader->Stage);
1369
1370 if (prog->nir->info.shared_size > consts->MaxComputeSharedMemorySize) {
1371 linker_error(shader_program, "Too much shared memory used (%u/%u)\n",
1372 prog->nir->info.shared_size,
1373 consts->MaxComputeSharedMemorySize);
1374 return false;
1375 }
1376
1377 if (options->lower_to_scalar) {
1378 NIR_PASS(_, shader->Program->nir, nir_lower_load_const_to_scalar);
1379 }
1380 }
1381
1382 lower_patch_vertices_in(shader_program);
1383
1384 /* Linking shaders also optimizes them. Separate shaders, compute shaders
1385 * and shaders with a fixed-func VS or FS that don't need linking are
1386 * optimized here.
1387 */
1388 if (num_shaders == 1)
1389 gl_nir_opts(linked_shader[0]->Program->nir);
1390
1391 /* nir_opt_access() needs to run before linking so that ImageAccess[]
1392 * and BindlessImage[].access are filled out with the correct modes.
1393 */
1394 for (unsigned i = 0; i < num_shaders; i++) {
1395 nir_shader *nir = linked_shader[i]->Program->nir;
1396
1397 nir_opt_access_options opt_access_options;
1398 opt_access_options.is_vulkan = false;
1399 NIR_PASS(_, nir, nir_opt_access, &opt_access_options);
1400
1401 if (!nir->options->compact_arrays) {
1402 NIR_PASS(_, nir, nir_lower_clip_cull_distance_to_vec4s);
1403 NIR_PASS(_, nir, nir_vectorize_tess_levels);
1404 }
1405
1406 /* Combine clip and cull outputs into one array and set:
1407 * - shader_info::clip_distance_array_size
1408 * - shader_info::cull_distance_array_size
1409 */
1410 if (consts->CombinedClipCullDistanceArrays)
1411 NIR_PASS(_, nir, nir_lower_clip_cull_distance_arrays);
1412 }
1413
1414 return true;
1415 }
1416
1417 static unsigned
get_varying_nir_var_mask(nir_shader * nir)1418 get_varying_nir_var_mask(nir_shader *nir)
1419 {
1420 return (nir->info.stage != MESA_SHADER_VERTEX ? nir_var_shader_in : 0) |
1421 (nir->info.stage != MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0);
1422 }
1423
1424 /**
1425 * Lower load_deref and store_deref on input/output variables to load_input
1426 * and store_output intrinsics, and perform varying optimizations and
1427 * compaction.
1428 */
1429 void
gl_nir_lower_optimize_varyings(const struct gl_constants * consts,struct gl_shader_program * prog,bool spirv)1430 gl_nir_lower_optimize_varyings(const struct gl_constants *consts,
1431 struct gl_shader_program *prog, bool spirv)
1432 {
1433 nir_shader *shaders[MESA_SHADER_STAGES];
1434 unsigned num_shaders = 0;
1435 unsigned max_ubos = UINT_MAX;
1436 unsigned max_uniform_comps = UINT_MAX;
1437
1438 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1439 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
1440
1441 if (!shader)
1442 continue;
1443
1444 nir_shader *nir = shader->Program->nir;
1445
1446 if (nir->info.stage == MESA_SHADER_COMPUTE)
1447 return;
1448
1449 if (!(nir->options->io_options & nir_io_glsl_lower_derefs) ||
1450 !(nir->options->io_options & nir_io_glsl_opt_varyings))
1451 return;
1452
1453 shaders[num_shaders] = nir;
1454 max_uniform_comps = MIN2(max_uniform_comps,
1455 consts->Program[i].MaxUniformComponents);
1456 max_ubos = MIN2(max_ubos, consts->Program[i].MaxUniformBlocks);
1457 num_shaders++;
1458 }
1459
1460 /* Lower IO derefs to load and store intrinsics. */
1461 for (unsigned i = 0; i < num_shaders; i++) {
1462 nir_shader *nir = shaders[i];
1463
1464 nir_lower_io_passes(nir, true);
1465 }
1466
1467 /* There is nothing to optimize for only 1 shader. */
1468 if (num_shaders == 1) {
1469 nir_shader *nir = shaders[0];
1470
1471 /* Even with a separate shader, it's still worth to re-vectorize IO from
1472 * scratch because the original shader might not be vectorized optimally.
1473 */
1474 NIR_PASS(_, nir, nir_lower_io_to_scalar, get_varying_nir_var_mask(nir),
1475 NULL, NULL);
1476 NIR_PASS(_, nir, nir_opt_vectorize_io, get_varying_nir_var_mask(nir));
1477 return;
1478 }
1479
1480 for (unsigned i = 0; i < num_shaders; i++) {
1481 nir_shader *nir = shaders[i];
1482
1483 /* nir_opt_varyings requires scalar IO. Scalarize all varyings (not just
1484 * the ones we optimize) because we want to re-vectorize everything to
1485 * get better vectorization and other goodies from nir_opt_vectorize_io.
1486 */
1487 NIR_PASS(_, nir, nir_lower_io_to_scalar, get_varying_nir_var_mask(nir),
1488 NULL, NULL);
1489
1490 /* nir_opt_varyings requires shaders to be optimized. */
1491 gl_nir_opts(nir);
1492 }
1493
1494 /* Optimize varyings from the first shader to the last shader first, and
1495 * then in the opposite order from the last changed producer.
1496 *
1497 * For example, VS->GS->FS is optimized in this order first:
1498 * (VS,GS), (GS,FS)
1499 *
1500 * That ensures that constants and undefs (dead inputs) are propagated
1501 * forward.
1502 *
1503 * If GS was changed while optimizing (GS,FS), (VS,GS) is optimized again
1504 * because removing outputs in GS can cause a chain reaction in making
1505 * GS inputs, VS outputs, and VS inputs dead.
1506 */
1507 unsigned highest_changed_producer = 0;
1508 for (unsigned i = 0; i < num_shaders - 1; i++) {
1509 nir_shader *producer = shaders[i];
1510 nir_shader *consumer = shaders[i + 1];
1511
1512 nir_opt_varyings_progress progress =
1513 nir_opt_varyings(producer, consumer, spirv, max_uniform_comps,
1514 max_ubos);
1515
1516 if (progress & nir_progress_producer) {
1517 gl_nir_opts(producer);
1518 highest_changed_producer = i;
1519 }
1520 if (progress & nir_progress_consumer)
1521 gl_nir_opts(consumer);
1522 }
1523
1524 /* Optimize varyings from the highest changed producer to the first
1525 * shader.
1526 */
1527 for (unsigned i = highest_changed_producer; i > 0; i--) {
1528 nir_shader *producer = shaders[i - 1];
1529 nir_shader *consumer = shaders[i];
1530
1531 nir_opt_varyings_progress progress =
1532 nir_opt_varyings(producer, consumer, spirv, max_uniform_comps,
1533 max_ubos);
1534
1535 if (progress & nir_progress_producer)
1536 gl_nir_opts(producer);
1537 if (progress & nir_progress_consumer)
1538 gl_nir_opts(consumer);
1539 }
1540
1541 /* Final cleanups. */
1542 for (unsigned i = 0; i < num_shaders; i++) {
1543 nir_shader *nir = shaders[i];
1544
1545 /* Re-vectorize IO. */
1546 NIR_PASS(_, nir, nir_opt_vectorize_io, get_varying_nir_var_mask(nir));
1547
1548 /* Recompute intrinsic bases, which are totally random after
1549 * optimizations and compaction. Do that for all inputs and outputs,
1550 * including VS inputs because those could have been removed too.
1551 */
1552 NIR_PASS_V(nir, nir_recompute_io_bases,
1553 nir_var_shader_in | nir_var_shader_out);
1554
1555 /* Regenerate transform feedback info because compaction in
1556 * nir_opt_varyings always moves them to other slots.
1557 */
1558 if (nir->xfb_info)
1559 nir_gather_xfb_info_from_intrinsics(nir);
1560 }
1561 }
1562
1563 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)1564 gl_nir_link_spirv(const struct gl_constants *consts,
1565 const struct gl_extensions *exts,
1566 struct gl_shader_program *prog,
1567 const struct gl_nir_linker_options *options)
1568 {
1569 struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
1570 unsigned num_shaders = 0;
1571
1572 MESA_TRACE_FUNC();
1573
1574 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1575 if (prog->_LinkedShaders[i]) {
1576 linked_shader[num_shaders++] = prog->_LinkedShaders[i];
1577
1578 remove_dead_varyings_pre_linking(prog->_LinkedShaders[i]->Program->nir);
1579 }
1580 }
1581
1582 if (!prelink_lowering(consts, exts, prog, linked_shader, num_shaders))
1583 return false;
1584
1585 gl_nir_link_assign_xfb_resources(consts, prog);
1586 gl_nir_lower_optimize_varyings(consts, prog, true);
1587
1588 if (!linked_shader[0]->Program->nir->info.io_lowered) {
1589 /* Linking the stages in the opposite order (from fragment to vertex)
1590 * ensures that inter-shader outputs written to in an earlier stage
1591 * are eliminated if they are (transitively) not used in a later
1592 * stage.
1593 */
1594 for (int i = num_shaders - 2; i >= 0; i--) {
1595 gl_nir_link_opts(linked_shader[i]->Program->nir,
1596 linked_shader[i + 1]->Program->nir);
1597 }
1598 }
1599
1600 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1601 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
1602 if (shader) {
1603 const nir_remove_dead_variables_options opts = {
1604 .can_remove_var = can_remove_var,
1605 };
1606 nir_remove_dead_variables(shader->Program->nir,
1607 nir_var_uniform | nir_var_image,
1608 &opts);
1609 }
1610 }
1611
1612 if (!gl_nir_link_uniform_blocks(consts, prog))
1613 return false;
1614
1615 if (!gl_nir_link_uniforms(consts, prog, options->fill_parameters))
1616 return false;
1617
1618 gl_nir_link_assign_atomic_counter_resources(consts, prog);
1619
1620 return true;
1621 }
1622
1623 bool
gl_nir_validate_intrastage_arrays(struct gl_shader_program * prog,nir_variable * var,nir_variable * existing,unsigned existing_stage,bool match_precision)1624 gl_nir_validate_intrastage_arrays(struct gl_shader_program *prog,
1625 nir_variable *var, nir_variable *existing,
1626 unsigned existing_stage,
1627 bool match_precision)
1628 {
1629 /* Consider the types to be "the same" if both types are arrays
1630 * of the same type and one of the arrays is implicitly sized.
1631 * In addition, set the type of the linked variable to the
1632 * explicitly sized array.
1633 */
1634 if (glsl_type_is_array(var->type) && glsl_type_is_array(existing->type)) {
1635 const glsl_type *no_array_var = glsl_get_array_element(var->type);
1636 const glsl_type *no_array_existing =
1637 glsl_get_array_element(existing->type);
1638 bool type_matches;
1639
1640 type_matches = (match_precision ?
1641 no_array_var == no_array_existing :
1642 glsl_type_compare_no_precision(no_array_var, no_array_existing));
1643
1644 if (type_matches &&
1645 ((glsl_array_size(var->type) == 0) ||
1646 (glsl_array_size(existing->type) == 0))) {
1647 if (glsl_array_size(var->type) != 0) {
1648 if ((int)glsl_array_size(var->type) <=
1649 existing->data.max_array_access) {
1650 linker_error(prog, "%s `%s' declared as type "
1651 "`%s' but outermost dimension has an index"
1652 " of `%i'\n",
1653 gl_nir_mode_string(var),
1654 var->name, glsl_get_type_name(var->type),
1655 existing->data.max_array_access);
1656 }
1657 existing->type = var->type;
1658
1659 nir_shader *s = prog->_LinkedShaders[existing_stage]->Program->nir;
1660 nir_fixup_deref_types(s);
1661 return true;
1662 } else if (glsl_array_size(existing->type) != 0) {
1663 if((int)glsl_array_size(existing->type) <= var->data.max_array_access &&
1664 !existing->data.from_ssbo_unsized_array) {
1665 linker_error(prog, "%s `%s' declared as type "
1666 "`%s' but outermost dimension has an index"
1667 " of `%i'\n",
1668 gl_nir_mode_string(var),
1669 var->name, glsl_get_type_name(existing->type),
1670 var->data.max_array_access);
1671 }
1672 return true;
1673 }
1674 }
1675 }
1676 return false;
1677 }
1678
1679 static bool
nir_constant_compare(const nir_constant * c1,const nir_constant * c2)1680 nir_constant_compare(const nir_constant *c1, const nir_constant *c2)
1681 {
1682 bool match = true;
1683
1684 match &= memcmp(c1->values, c2->values, sizeof(c1->values)) == 0;
1685 match &= c1->is_null_constant == c2->is_null_constant;
1686 match &= c1->num_elements == c2->num_elements;
1687 if (!match)
1688 return false;
1689
1690 for (unsigned i = 0; i < c1->num_elements; i++) {
1691 match &= nir_constant_compare(c1->elements[i], c2->elements[i]);
1692 }
1693
1694 return match;
1695 }
1696
1697 struct ifc_var {
1698 unsigned stage;
1699 nir_variable *var;
1700 };
1701
1702 /**
1703 * Perform validation of global variables used across multiple shaders
1704 */
1705 static void
cross_validate_globals(void * mem_ctx,const struct gl_constants * consts,struct gl_shader_program * prog,nir_shader * shader,struct hash_table * variables,bool uniforms_only)1706 cross_validate_globals(void *mem_ctx, const struct gl_constants *consts,
1707 struct gl_shader_program *prog,
1708 nir_shader *shader, struct hash_table *variables,
1709 bool uniforms_only)
1710 {
1711 nir_foreach_variable_in_shader(var, shader) {
1712 if (uniforms_only &&
1713 (var->data.mode != nir_var_uniform &&
1714 var->data.mode != nir_var_mem_ubo &&
1715 var->data.mode != nir_var_image &&
1716 var->data.mode != nir_var_mem_ssbo))
1717 continue;
1718
1719 /* don't cross validate subroutine uniforms */
1720 if (glsl_contains_subroutine(var->type))
1721 continue;
1722
1723 /* Don't cross validate interface instances. These are only relevant
1724 * inside a shader. The cross validation is done at the Interface Block
1725 * name level.
1726 */
1727 if (glsl_without_array(var->type) == var->interface_type)
1728 continue;
1729
1730 /* Don't cross validate compiler temporaries that are at global scope.
1731 * These will eventually get pulled into the shaders 'main'.
1732 */
1733 if (var->data.mode == nir_var_shader_temp &&
1734 var->data.how_declared == nir_var_hidden)
1735 continue;
1736
1737 /* If a global with this name has already been seen, verify that the
1738 * new instance has the same type. In addition, if the globals have
1739 * initializers, the values of the initializers must be the same.
1740 */
1741 struct hash_entry *entry =
1742 _mesa_hash_table_search(variables, var->name);
1743 if (entry != NULL) {
1744 struct ifc_var *existing_ifc = (struct ifc_var *) entry->data;
1745 nir_variable *existing = existing_ifc->var;
1746
1747 /* Check if types match. */
1748 if (var->type != existing->type) {
1749 if (!gl_nir_validate_intrastage_arrays(prog, var, existing,
1750 existing_ifc->stage, true)) {
1751 /* If it is an unsized array in a Shader Storage Block,
1752 * two different shaders can access to different elements.
1753 * Because of that, they might be converted to different
1754 * sized arrays, then check that they are compatible but
1755 * ignore the array size.
1756 */
1757 if (!(var->data.mode == nir_var_mem_ssbo &&
1758 var->data.from_ssbo_unsized_array &&
1759 existing->data.mode == nir_var_mem_ssbo &&
1760 existing->data.from_ssbo_unsized_array &&
1761 glsl_get_gl_type(var->type) == glsl_get_gl_type(existing->type))) {
1762 linker_error(prog, "%s `%s' declared as type "
1763 "`%s' and type `%s'\n",
1764 gl_nir_mode_string(var),
1765 var->name, glsl_get_type_name(var->type),
1766 glsl_get_type_name(existing->type));
1767 return;
1768 }
1769 }
1770 }
1771
1772 if (var->data.explicit_location) {
1773 if (existing->data.explicit_location
1774 && (var->data.location != existing->data.location)) {
1775 linker_error(prog, "explicit locations for %s "
1776 "`%s' have differing values\n",
1777 gl_nir_mode_string(var), var->name);
1778 return;
1779 }
1780
1781 if (var->data.location_frac != existing->data.location_frac) {
1782 linker_error(prog, "explicit components for %s `%s' have "
1783 "differing values\n", gl_nir_mode_string(var),
1784 var->name);
1785 return;
1786 }
1787
1788 existing->data.location = var->data.location;
1789 existing->data.explicit_location = true;
1790 } else {
1791 /* Check if uniform with implicit location was marked explicit
1792 * by earlier shader stage. If so, mark it explicit in this stage
1793 * too to make sure later processing does not treat it as
1794 * implicit one.
1795 */
1796 if (existing->data.explicit_location) {
1797 var->data.location = existing->data.location;
1798 var->data.explicit_location = true;
1799 }
1800 }
1801
1802 /* From the GLSL 4.20 specification:
1803 * "A link error will result if two compilation units in a program
1804 * specify different integer-constant bindings for the same
1805 * opaque-uniform name. However, it is not an error to specify a
1806 * binding on some but not all declarations for the same name"
1807 */
1808 if (var->data.explicit_binding) {
1809 if (existing->data.explicit_binding &&
1810 var->data.binding != existing->data.binding) {
1811 linker_error(prog, "explicit bindings for %s "
1812 "`%s' have differing values\n",
1813 gl_nir_mode_string(var), var->name);
1814 return;
1815 }
1816
1817 existing->data.binding = var->data.binding;
1818 existing->data.explicit_binding = true;
1819 }
1820
1821 if (glsl_contains_atomic(var->type) &&
1822 var->data.offset != existing->data.offset) {
1823 linker_error(prog, "offset specifications for %s "
1824 "`%s' have differing values\n",
1825 gl_nir_mode_string(var), var->name);
1826 return;
1827 }
1828
1829 /* Validate layout qualifiers for gl_FragDepth.
1830 *
1831 * From the AMD/ARB_conservative_depth specs:
1832 *
1833 * "If gl_FragDepth is redeclared in any fragment shader in a
1834 * program, it must be redeclared in all fragment shaders in
1835 * that program that have static assignments to
1836 * gl_FragDepth. All redeclarations of gl_FragDepth in all
1837 * fragment shaders in a single program must have the same set
1838 * of qualifiers."
1839 */
1840 if (strcmp(var->name, "gl_FragDepth") == 0) {
1841 bool layout_declared = var->data.depth_layout != nir_depth_layout_none;
1842 bool layout_differs =
1843 var->data.depth_layout != existing->data.depth_layout;
1844
1845 if (layout_declared && layout_differs) {
1846 linker_error(prog,
1847 "All redeclarations of gl_FragDepth in all "
1848 "fragment shaders in a single program must have "
1849 "the same set of qualifiers.\n");
1850 }
1851
1852 if (var->data.used && layout_differs) {
1853 linker_error(prog,
1854 "If gl_FragDepth is redeclared with a layout "
1855 "qualifier in any fragment shader, it must be "
1856 "redeclared with the same layout qualifier in "
1857 "all fragment shaders that have assignments to "
1858 "gl_FragDepth\n");
1859 }
1860 }
1861
1862 /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1863 *
1864 * "If a shared global has multiple initializers, the
1865 * initializers must all be constant expressions, and they
1866 * must all have the same value. Otherwise, a link error will
1867 * result. (A shared global having only one initializer does
1868 * not require that initializer to be a constant expression.)"
1869 *
1870 * Previous to 4.20 the GLSL spec simply said that initializers
1871 * must have the same value. In this case of non-constant
1872 * initializers, this was impossible to determine. As a result,
1873 * no vendor actually implemented that behavior. The 4.20
1874 * behavior matches the implemented behavior of at least one other
1875 * vendor, so we'll implement that for all GLSL versions.
1876 * If (at least) one of these constant expressions is implicit,
1877 * because it was added by glsl_zero_init, we skip the verification.
1878 */
1879 if (var->constant_initializer != NULL) {
1880 if (existing->constant_initializer != NULL &&
1881 !existing->data.is_implicit_initializer &&
1882 !var->data.is_implicit_initializer) {
1883 if (!nir_constant_compare(var->constant_initializer,
1884 existing->constant_initializer)) {
1885 linker_error(prog, "initializers for %s "
1886 "`%s' have differing values\n",
1887 gl_nir_mode_string(var), var->name);
1888 return;
1889 }
1890 } else {
1891 /* If the first-seen instance of a particular uniform did
1892 * not have an initializer but a later instance does,
1893 * replace the former with the later.
1894 */
1895 if (!var->data.is_implicit_initializer)
1896 _mesa_hash_table_insert(variables, existing->name, var);
1897 }
1898 }
1899
1900 if (var->data.has_initializer) {
1901 if (existing->data.has_initializer
1902 && (var->constant_initializer == NULL
1903 || existing->constant_initializer == NULL)) {
1904 linker_error(prog,
1905 "shared global variable `%s' has multiple "
1906 "non-constant initializers.\n",
1907 var->name);
1908 return;
1909 }
1910 }
1911
1912 if (existing->data.explicit_invariant != var->data.explicit_invariant) {
1913 linker_error(prog, "declarations for %s `%s' have "
1914 "mismatching invariant qualifiers\n",
1915 gl_nir_mode_string(var), var->name);
1916 return;
1917 }
1918 if (existing->data.centroid != var->data.centroid) {
1919 linker_error(prog, "declarations for %s `%s' have "
1920 "mismatching centroid qualifiers\n",
1921 gl_nir_mode_string(var), var->name);
1922 return;
1923 }
1924 if (existing->data.sample != var->data.sample) {
1925 linker_error(prog, "declarations for %s `%s` have "
1926 "mismatching sample qualifiers\n",
1927 gl_nir_mode_string(var), var->name);
1928 return;
1929 }
1930 if (existing->data.image.format != var->data.image.format) {
1931 linker_error(prog, "declarations for %s `%s` have "
1932 "mismatching image format qualifiers\n",
1933 gl_nir_mode_string(var), var->name);
1934 return;
1935 }
1936
1937 /* Check the precision qualifier matches for uniform variables on
1938 * GLSL ES.
1939 */
1940 if (!consts->AllowGLSLRelaxedES &&
1941 prog->IsES && !var->interface_type &&
1942 existing->data.precision != var->data.precision) {
1943 if ((existing->data.used && var->data.used) ||
1944 prog->GLSL_Version >= 300) {
1945 linker_error(prog, "declarations for %s `%s` have "
1946 "mismatching precision qualifiers\n",
1947 gl_nir_mode_string(var), var->name);
1948 return;
1949 } else {
1950 linker_warning(prog, "declarations for %s `%s` have "
1951 "mismatching precision qualifiers\n",
1952 gl_nir_mode_string(var), var->name);
1953 }
1954 }
1955
1956 /* In OpenGL GLSL 3.20 spec, section 4.3.9:
1957 *
1958 * "It is a link-time error if any particular shader interface
1959 * contains:
1960 *
1961 * - two different blocks, each having no instance name, and each
1962 * having a member of the same name, or
1963 *
1964 * - a variable outside a block, and a block with no instance name,
1965 * where the variable has the same name as a member in the block."
1966 */
1967 const glsl_type *var_itype = var->interface_type;
1968 const glsl_type *existing_itype = existing->interface_type;
1969 if (var_itype != existing_itype) {
1970 if (!var_itype || !existing_itype) {
1971 linker_error(prog, "declarations for %s `%s` are inside block "
1972 "`%s` and outside a block",
1973 gl_nir_mode_string(var), var->name,
1974 glsl_get_type_name(var_itype ? var_itype : existing_itype));
1975 return;
1976 } else if (strcmp(glsl_get_type_name(var_itype), glsl_get_type_name(existing_itype)) != 0) {
1977 linker_error(prog, "declarations for %s `%s` are inside blocks "
1978 "`%s` and `%s`",
1979 gl_nir_mode_string(var), var->name,
1980 glsl_get_type_name(existing_itype),
1981 glsl_get_type_name(var_itype));
1982 return;
1983 }
1984 }
1985 } else {
1986 struct ifc_var *ifc_var = ralloc(mem_ctx, struct ifc_var);
1987 ifc_var->var = var;
1988 ifc_var->stage = shader->info.stage;
1989 _mesa_hash_table_insert(variables, var->name, ifc_var);
1990 }
1991 }
1992 }
1993
1994 /**
1995 * Perform validation of uniforms used across multiple shader stages
1996 */
1997 static void
cross_validate_uniforms(const struct gl_constants * consts,struct gl_shader_program * prog)1998 cross_validate_uniforms(const struct gl_constants *consts,
1999 struct gl_shader_program *prog)
2000 {
2001 void *mem_ctx = ralloc_context(NULL);
2002 struct hash_table *variables =
2003 _mesa_hash_table_create(mem_ctx, _mesa_hash_string, _mesa_key_string_equal);
2004 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2005 if (prog->_LinkedShaders[i] == NULL)
2006 continue;
2007
2008 cross_validate_globals(mem_ctx, consts, prog,
2009 prog->_LinkedShaders[i]->Program->nir,
2010 variables, true);
2011 }
2012
2013 ralloc_free(mem_ctx);
2014 }
2015
2016 /**
2017 * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
2018 * for a variable, checks for overlaps between other uniforms using explicit
2019 * locations.
2020 */
2021 static int
reserve_explicit_locations(struct gl_shader_program * prog,struct string_to_uint_map * map,nir_variable * var)2022 reserve_explicit_locations(struct gl_shader_program *prog,
2023 struct string_to_uint_map *map, nir_variable *var)
2024 {
2025 unsigned slots = glsl_type_uniform_locations(var->type);
2026 unsigned max_loc = var->data.location + slots - 1;
2027 unsigned return_value = slots;
2028
2029 /* Resize remap table if locations do not fit in the current one. */
2030 if (max_loc + 1 > prog->NumUniformRemapTable) {
2031 prog->UniformRemapTable =
2032 reralloc(prog, prog->UniformRemapTable,
2033 struct gl_uniform_storage *,
2034 max_loc + 1);
2035
2036 if (!prog->UniformRemapTable) {
2037 linker_error(prog, "Out of memory during linking.\n");
2038 return -1;
2039 }
2040
2041 /* Initialize allocated space. */
2042 for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++)
2043 prog->UniformRemapTable[i] = NULL;
2044
2045 prog->NumUniformRemapTable = max_loc + 1;
2046 }
2047
2048 for (unsigned i = 0; i < slots; i++) {
2049 unsigned loc = var->data.location + i;
2050
2051 /* Check if location is already used. */
2052 if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
2053
2054 /* Possibly same uniform from a different stage, this is ok. */
2055 unsigned hash_loc;
2056 if (string_to_uint_map_get(map, &hash_loc, var->name) &&
2057 hash_loc == loc - i) {
2058 return_value = 0;
2059 continue;
2060 }
2061
2062 /* ARB_explicit_uniform_location specification states:
2063 *
2064 * "No two default-block uniform variables in the program can have
2065 * the same location, even if they are unused, otherwise a compiler
2066 * or linker error will be generated."
2067 */
2068 linker_error(prog,
2069 "location qualifier for uniform %s overlaps "
2070 "previously used location\n",
2071 var->name);
2072 return -1;
2073 }
2074
2075 /* Initialize location as inactive before optimization
2076 * rounds and location assignment.
2077 */
2078 prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
2079 }
2080
2081 /* Note, base location used for arrays. */
2082 string_to_uint_map_put(map, var->data.location, var->name);
2083
2084 return return_value;
2085 }
2086
2087 static bool
reserve_subroutine_explicit_locations(struct gl_shader_program * prog,struct gl_program * p,nir_variable * var)2088 reserve_subroutine_explicit_locations(struct gl_shader_program *prog,
2089 struct gl_program *p,
2090 nir_variable *var)
2091 {
2092 unsigned slots = glsl_type_uniform_locations(var->type);
2093 unsigned max_loc = var->data.location + slots - 1;
2094
2095 /* Resize remap table if locations do not fit in the current one. */
2096 if (max_loc + 1 > p->sh.NumSubroutineUniformRemapTable) {
2097 p->sh.SubroutineUniformRemapTable =
2098 reralloc(p, p->sh.SubroutineUniformRemapTable,
2099 struct gl_uniform_storage *,
2100 max_loc + 1);
2101
2102 if (!p->sh.SubroutineUniformRemapTable) {
2103 linker_error(prog, "Out of memory during linking.\n");
2104 return false;
2105 }
2106
2107 /* Initialize allocated space. */
2108 for (unsigned i = p->sh.NumSubroutineUniformRemapTable; i < max_loc + 1; i++)
2109 p->sh.SubroutineUniformRemapTable[i] = NULL;
2110
2111 p->sh.NumSubroutineUniformRemapTable = max_loc + 1;
2112 }
2113
2114 for (unsigned i = 0; i < slots; i++) {
2115 unsigned loc = var->data.location + i;
2116
2117 /* Check if location is already used. */
2118 if (p->sh.SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
2119
2120 /* ARB_explicit_uniform_location specification states:
2121 * "No two subroutine uniform variables can have the same location
2122 * in the same shader stage, otherwise a compiler or linker error
2123 * will be generated."
2124 */
2125 linker_error(prog,
2126 "location qualifier for uniform %s overlaps "
2127 "previously used location\n",
2128 var->name);
2129 return false;
2130 }
2131
2132 /* Initialize location as inactive before optimization
2133 * rounds and location assignment.
2134 */
2135 p->sh.SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
2136 }
2137
2138 return true;
2139 }
2140 /**
2141 * Check and reserve all explicit uniform locations, called before
2142 * any optimizations happen to handle also inactive uniforms and
2143 * inactive array elements that may get trimmed away.
2144 */
2145 static void
check_explicit_uniform_locations(const struct gl_extensions * exts,struct gl_shader_program * prog)2146 check_explicit_uniform_locations(const struct gl_extensions *exts,
2147 struct gl_shader_program *prog)
2148 {
2149 prog->NumExplicitUniformLocations = 0;
2150
2151 if (!exts->ARB_explicit_uniform_location)
2152 return;
2153
2154 /* This map is used to detect if overlapping explicit locations
2155 * occur with the same uniform (from different stage) or a different one.
2156 */
2157 struct string_to_uint_map *uniform_map = string_to_uint_map_ctor();
2158
2159 if (!uniform_map) {
2160 linker_error(prog, "Out of memory during linking.\n");
2161 return;
2162 }
2163
2164 unsigned entries_total = 0;
2165 unsigned mask = prog->data->linked_stages;
2166 while (mask) {
2167 const int i = u_bit_scan(&mask);
2168 struct gl_program *p = prog->_LinkedShaders[i]->Program;
2169
2170 unsigned modes = nir_var_uniform | nir_var_mem_ubo | nir_var_image;
2171 nir_foreach_variable_with_modes(var, p->nir, modes) {
2172 if (var->data.explicit_location) {
2173 bool ret = false;
2174 if (glsl_type_is_subroutine(glsl_without_array(var->type)))
2175 ret = reserve_subroutine_explicit_locations(prog, p, var);
2176 else {
2177 int slots = reserve_explicit_locations(prog, uniform_map,
2178 var);
2179 if (slots != -1) {
2180 ret = true;
2181 entries_total += slots;
2182 }
2183 }
2184 if (!ret) {
2185 string_to_uint_map_dtor(uniform_map);
2186 return;
2187 }
2188 }
2189 }
2190 }
2191
2192 link_util_update_empty_uniform_locations(prog);
2193
2194 string_to_uint_map_dtor(uniform_map);
2195 prog->NumExplicitUniformLocations = entries_total;
2196 }
2197
2198 static void
link_assign_subroutine_types(struct gl_shader_program * prog)2199 link_assign_subroutine_types(struct gl_shader_program *prog)
2200 {
2201 unsigned mask = prog->data->linked_stages;
2202 while (mask) {
2203 const int i = u_bit_scan(&mask);
2204 struct gl_program *p = prog->_LinkedShaders[i]->Program;
2205
2206 struct set *fn_decl_set =
2207 _mesa_set_create(NULL, _mesa_hash_string, _mesa_key_string_equal);
2208
2209 p->sh.MaxSubroutineFunctionIndex = 0;
2210 nir_foreach_function(fn, p->nir) {
2211 /* A function might be decalred multiple times but we should only
2212 * process it once
2213 */
2214 struct set_entry *entry = _mesa_set_search(fn_decl_set, fn->name);
2215 if (entry)
2216 continue;
2217
2218 _mesa_set_add(fn_decl_set, fn->name);
2219
2220 if (fn->is_subroutine)
2221 p->sh.NumSubroutineUniformTypes++;
2222
2223 if (!fn->num_subroutine_types)
2224 continue;
2225
2226 /* these should have been calculated earlier. */
2227 assert(fn->subroutine_index != -1);
2228 if (p->sh.NumSubroutineFunctions + 1 > MAX_SUBROUTINES) {
2229 linker_error(prog, "Too many subroutine functions declared.\n");
2230 return;
2231 }
2232 p->sh.SubroutineFunctions = reralloc(p, p->sh.SubroutineFunctions,
2233 struct gl_subroutine_function,
2234 p->sh.NumSubroutineFunctions + 1);
2235 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].name.string = ralloc_strdup(p, fn->name);
2236 resource_name_updated(&p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].name);
2237 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types;
2238 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types =
2239 ralloc_array(p, const struct glsl_type *,
2240 fn->num_subroutine_types);
2241
2242 /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the
2243 * GLSL 4.5 spec:
2244 *
2245 * "Each subroutine with an index qualifier in the shader must be
2246 * given a unique index, otherwise a compile or link error will be
2247 * generated."
2248 */
2249 for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
2250 if (p->sh.SubroutineFunctions[j].index != -1 &&
2251 p->sh.SubroutineFunctions[j].index == fn->subroutine_index) {
2252 linker_error(prog, "each subroutine index qualifier in the "
2253 "shader must be unique\n");
2254 return;
2255 }
2256 }
2257 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].index =
2258 fn->subroutine_index;
2259
2260 if (fn->subroutine_index > (int)p->sh.MaxSubroutineFunctionIndex)
2261 p->sh.MaxSubroutineFunctionIndex = fn->subroutine_index;
2262
2263 for (int j = 0; j < fn->num_subroutine_types; j++)
2264 p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types[j] = fn->subroutine_types[j];
2265 p->sh.NumSubroutineFunctions++;
2266 }
2267
2268 _mesa_set_destroy(fn_decl_set, NULL);
2269 }
2270 }
2271
2272 static void
verify_subroutine_associated_funcs(struct gl_shader_program * prog)2273 verify_subroutine_associated_funcs(struct gl_shader_program *prog)
2274 {
2275 unsigned mask = prog->data->linked_stages;
2276 while (mask) {
2277 const int i = u_bit_scan(&mask);
2278 struct gl_program *p = prog->_LinkedShaders[i]->Program;
2279
2280 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
2281 *
2282 * "A program will fail to compile or link if any shader
2283 * or stage contains two or more functions with the same
2284 * name if the name is associated with a subroutine type."
2285 */
2286 for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
2287 unsigned definitions = 0;
2288 char *name = p->sh.SubroutineFunctions[j].name.string;
2289
2290 /* Calculate number of function definitions with the same name */
2291 nir_foreach_function(fn, p->nir) {
2292 /* If the function is only declared not implemented continue */
2293 if (fn->impl != NULL)
2294 continue;
2295
2296 if (strcmp(fn->name, name) == 0) {
2297 if (++definitions > 1) {
2298 linker_error(prog, "%s shader contains two or more function "
2299 "definitions with name `%s', which is "
2300 "associated with a subroutine type.\n",
2301 _mesa_shader_stage_to_string(i),
2302 fn->name);
2303 return;
2304 }
2305 }
2306 }
2307 }
2308 }
2309 }
2310
2311 /**
2312 * Validate shader image resources.
2313 */
2314 static void
check_image_resources(const struct gl_constants * consts,const struct gl_extensions * exts,struct gl_shader_program * prog)2315 check_image_resources(const struct gl_constants *consts,
2316 const struct gl_extensions *exts,
2317 struct gl_shader_program *prog)
2318 {
2319 unsigned total_image_units = 0;
2320 unsigned fragment_outputs = 0;
2321 unsigned total_shader_storage_blocks = 0;
2322
2323 if (!exts->ARB_shader_image_load_store)
2324 return;
2325
2326 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2327 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
2328 if (!sh)
2329 continue;
2330
2331 total_image_units += sh->Program->info.num_images;
2332 total_shader_storage_blocks += sh->Program->info.num_ssbos;
2333 }
2334
2335 if (total_image_units > consts->MaxCombinedImageUniforms)
2336 linker_error(prog, "Too many combined image uniforms\n");
2337
2338 struct gl_linked_shader *frag_sh =
2339 prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2340 if (frag_sh) {
2341 uint64_t frag_outputs_written = frag_sh->Program->info.outputs_written;
2342 fragment_outputs = util_bitcount64(frag_outputs_written);
2343 }
2344
2345 if (total_image_units + fragment_outputs + total_shader_storage_blocks >
2346 consts->MaxCombinedShaderOutputResources)
2347 linker_error(prog, "Too many combined image uniforms, shader storage "
2348 " buffers and fragment outputs\n");
2349 }
2350
2351 static bool
is_sampler_array_accessed_indirectly(nir_deref_instr * deref)2352 is_sampler_array_accessed_indirectly(nir_deref_instr *deref)
2353 {
2354 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
2355 if (d->deref_type != nir_deref_type_array)
2356 continue;
2357
2358 if (nir_src_is_const(d->arr.index))
2359 continue;
2360
2361 return true;
2362 }
2363
2364 return false;
2365 }
2366
2367 /**
2368 * This check is done to make sure we allow only constant expression
2369 * indexing and "constant-index-expression" (indexing with an expression
2370 * that includes loop induction variable).
2371 */
2372 static bool
validate_sampler_array_indexing(const struct gl_constants * consts,struct gl_shader_program * prog)2373 validate_sampler_array_indexing(const struct gl_constants *consts,
2374 struct gl_shader_program *prog)
2375 {
2376 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2377 if (prog->_LinkedShaders[i] == NULL)
2378 continue;
2379
2380 bool no_dynamic_indexing =
2381 consts->ShaderCompilerOptions[i].NirOptions->force_indirect_unrolling_sampler;
2382
2383 bool uses_indirect_sampler_array_indexing = false;
2384 nir_foreach_function_impl(impl, prog->_LinkedShaders[i]->Program->nir) {
2385 nir_foreach_block(block, impl) {
2386 nir_foreach_instr(instr, block) {
2387 /* Check if a sampler array is accessed indirectly */
2388 if (instr->type == nir_instr_type_tex) {
2389 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
2390 int sampler_idx =
2391 nir_tex_instr_src_index(tex_instr, nir_tex_src_sampler_deref);
2392 if (sampler_idx >= 0) {
2393 nir_deref_instr *deref =
2394 nir_instr_as_deref(tex_instr->src[sampler_idx].src.ssa->parent_instr);
2395 if (is_sampler_array_accessed_indirectly(deref)) {
2396 uses_indirect_sampler_array_indexing = true;
2397 break;
2398 }
2399 }
2400 }
2401 }
2402
2403 if (uses_indirect_sampler_array_indexing)
2404 break;
2405 }
2406 if (uses_indirect_sampler_array_indexing)
2407 break;
2408 }
2409
2410 if (uses_indirect_sampler_array_indexing) {
2411 const char *msg = "sampler arrays indexed with non-constant "
2412 "expressions is forbidden in GLSL %s %u";
2413 /* Backend has indicated that it has no dynamic indexing support. */
2414 if (no_dynamic_indexing) {
2415 linker_error(prog, msg, prog->IsES ? "ES" : "", prog->GLSL_Version);
2416 return false;
2417 } else {
2418 linker_warning(prog, msg, prog->IsES ? "ES" : "",
2419 prog->GLSL_Version);
2420 }
2421 }
2422 }
2423
2424 return true;
2425 }
2426
2427 static nir_variable *
find_frag_builtin(nir_shader * shader,bool is_sysval,unsigned sysval,unsigned varying)2428 find_frag_builtin(nir_shader *shader, bool is_sysval, unsigned sysval,
2429 unsigned varying)
2430 {
2431
2432 unsigned location = is_sysval ? sysval : varying;
2433 nir_variable_mode mode =
2434 is_sysval ? nir_var_system_value : nir_var_shader_in;
2435
2436 return nir_find_variable_with_location(shader, mode, location);
2437 }
2438
2439 /**
2440 * Verifies the invariance of built-in special variables.
2441 */
2442 static bool
validate_invariant_builtins(const struct gl_constants * consts,struct gl_shader_program * prog,const struct gl_linked_shader * vert,const struct gl_linked_shader * frag)2443 validate_invariant_builtins(const struct gl_constants *consts,
2444 struct gl_shader_program *prog,
2445 const struct gl_linked_shader *vert,
2446 const struct gl_linked_shader *frag)
2447 {
2448 const nir_variable *var_vert;
2449 const nir_variable *var_frag;
2450
2451 if (!vert || !frag)
2452 return true;
2453
2454 /*
2455 * From OpenGL ES Shading Language 1.0 specification
2456 * (4.6.4 Invariance and Linkage):
2457 * "The invariance of varyings that are declared in both the vertex and
2458 * fragment shaders must match. For the built-in special variables,
2459 * gl_FragCoord can only be declared invariant if and only if
2460 * gl_Position is declared invariant. Similarly gl_PointCoord can only
2461 * be declared invariant if and only if gl_PointSize is declared
2462 * invariant. It is an error to declare gl_FrontFacing as invariant.
2463 * The invariance of gl_FrontFacing is the same as the invariance of
2464 * gl_Position."
2465 */
2466 var_frag = find_frag_builtin(frag->Program->nir,
2467 consts->GLSLFragCoordIsSysVal,
2468 SYSTEM_VALUE_FRAG_COORD, VARYING_SLOT_POS);
2469 if (var_frag && var_frag->data.invariant) {
2470 var_vert = nir_find_variable_with_location(vert->Program->nir,
2471 nir_var_shader_out,
2472 VARYING_SLOT_POS);
2473 if (var_vert && !var_vert->data.invariant) {
2474 linker_error(prog,
2475 "fragment shader built-in `%s' has invariant qualifier, "
2476 "but vertex shader built-in `%s' lacks invariant qualifier\n",
2477 var_frag->name, var_vert->name);
2478 return false;
2479 }
2480 }
2481
2482 var_frag = find_frag_builtin(frag->Program->nir,
2483 consts->GLSLPointCoordIsSysVal,
2484 SYSTEM_VALUE_POINT_COORD, VARYING_SLOT_PNTC);
2485 if (var_frag && var_frag->data.invariant) {
2486 var_vert = nir_find_variable_with_location(vert->Program->nir,
2487 nir_var_shader_out,
2488 VARYING_SLOT_PSIZ);
2489 if (var_vert && !var_vert->data.invariant) {
2490 linker_error(prog,
2491 "fragment shader built-in `%s' has invariant qualifier, "
2492 "but vertex shader built-in `%s' lacks invariant qualifier\n",
2493 var_frag->name, var_vert->name);
2494 return false;
2495 }
2496 }
2497
2498 var_frag = find_frag_builtin(frag->Program->nir,
2499 consts->GLSLFrontFacingIsSysVal,
2500 SYSTEM_VALUE_FRONT_FACE, VARYING_SLOT_FACE);
2501 if (var_frag && var_frag->data.invariant) {
2502 linker_error(prog,
2503 "fragment shader built-in `%s' can not be declared as invariant\n",
2504 var_frag->name);
2505 return false;
2506 }
2507
2508 return true;
2509 }
2510
2511 static void
find_assignments(nir_shader * shader,nir_variable * var1,nir_variable * var2,nir_variable * var3,bool * var1_written,bool * var2_written,bool * var3_written)2512 find_assignments(nir_shader *shader, nir_variable *var1, nir_variable *var2,
2513 nir_variable *var3, bool *var1_written, bool *var2_written,
2514 bool *var3_written)
2515 {
2516 nir_foreach_function_impl(impl, shader) {
2517 nir_foreach_block(block, impl) {
2518 nir_foreach_instr(instr, block) {
2519 if (instr->type == nir_instr_type_intrinsic) {
2520 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
2521 if (intrin->intrinsic == nir_intrinsic_store_deref ||
2522 intrin->intrinsic == nir_intrinsic_copy_deref) {
2523 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
2524 nir_variable *var = nir_deref_instr_get_variable(deref);
2525 if (!var)
2526 continue;
2527
2528 if (var == var1)
2529 *var1_written = true;
2530 else if (var == var2)
2531 *var2_written = true;
2532 else if (var == var3)
2533 *var3_written = true;
2534 }
2535 }
2536 }
2537 }
2538 }
2539 }
2540
2541 /**
2542 * Set clip_distance_array_size based and cull_distance_array_size on the given
2543 * shader.
2544 *
2545 * Also check for errors based on incorrect usage of gl_ClipVertex and
2546 * gl_ClipDistance and gl_CullDistance.
2547 * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance
2548 * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances.
2549 */
2550 static void
analyze_clip_cull_usage(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts,struct shader_info * info)2551 analyze_clip_cull_usage(struct gl_shader_program *prog, nir_shader *shader,
2552 const struct gl_constants *consts,
2553 struct shader_info *info)
2554 {
2555 if (consts->DoDCEBeforeClipCullAnalysis) {
2556 /* Remove dead functions to avoid raising an error (eg: dead function
2557 * writes to gl_ClipVertex, and main() writes to gl_ClipDistance).
2558 */
2559 remove_dead_functions(shader);
2560 }
2561
2562 info->clip_distance_array_size = 0;
2563 info->cull_distance_array_size = 0;
2564
2565 if (prog->GLSL_Version >= (prog->IsES ? 300 : 130)) {
2566 /* From section 7.1 (Vertex Shader Special Variables) of the
2567 * GLSL 1.30 spec:
2568 *
2569 * "It is an error for a shader to statically write both
2570 * gl_ClipVertex and gl_ClipDistance."
2571 *
2572 * This does not apply to GLSL ES shaders, since GLSL ES defines neither
2573 * gl_ClipVertex nor gl_ClipDistance. However with
2574 * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0.
2575 */
2576 nir_variable *clip_dist =
2577 nir_find_variable_with_location(shader,
2578 nir_var_shader_out,
2579 VARYING_SLOT_CLIP_DIST0);
2580 nir_variable *cull_dist =
2581 nir_find_variable_with_location(shader,
2582 nir_var_shader_out,
2583 VARYING_SLOT_CULL_DIST0);
2584 nir_variable *clip_vert =
2585 nir_find_variable_with_location(shader,
2586 nir_var_shader_out,
2587 VARYING_SLOT_CLIP_VERTEX);
2588
2589 bool clip_dist_written = false;
2590 bool cull_dist_written = false;
2591 bool clip_vert_written = false;
2592 find_assignments(shader, clip_dist, cull_dist, clip_vert,
2593 &clip_dist_written, &cull_dist_written,
2594 &clip_vert_written);
2595
2596 /* From the ARB_cull_distance spec:
2597 *
2598 * It is a compile-time or link-time error for the set of shaders forming
2599 * a program to statically read or write both gl_ClipVertex and either
2600 * gl_ClipDistance or gl_CullDistance.
2601 *
2602 * This does not apply to GLSL ES shaders, since GLSL ES doesn't define
2603 * gl_ClipVertex.
2604 */
2605 if (!prog->IsES) {
2606 if (clip_vert_written && clip_dist_written) {
2607 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
2608 "and `gl_ClipDistance'\n",
2609 _mesa_shader_stage_to_string(info->stage));
2610 return;
2611 }
2612 if (clip_vert_written && cull_dist_written) {
2613 linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
2614 "and `gl_CullDistance'\n",
2615 _mesa_shader_stage_to_string(info->stage));
2616 return;
2617 }
2618 }
2619
2620 if (clip_dist_written)
2621 info->clip_distance_array_size = glsl_get_length(clip_dist->type);
2622
2623 if (cull_dist_written)
2624 info->cull_distance_array_size = glsl_get_length(cull_dist->type);
2625
2626 /* From the ARB_cull_distance spec:
2627 *
2628 * It is a compile-time or link-time error for the set of shaders forming
2629 * a program to have the sum of the sizes of the gl_ClipDistance and
2630 * gl_CullDistance arrays to be larger than
2631 * gl_MaxCombinedClipAndCullDistances.
2632 */
2633 if ((uint32_t)(info->clip_distance_array_size + info->cull_distance_array_size) >
2634 consts->MaxClipPlanes) {
2635 linker_error(prog, "%s shader: the combined size of "
2636 "'gl_ClipDistance' and 'gl_CullDistance' size cannot "
2637 "be larger than "
2638 "gl_MaxCombinedClipAndCullDistances (%u)",
2639 _mesa_shader_stage_to_string(info->stage),
2640 consts->MaxClipPlanes);
2641 }
2642 }
2643 }
2644
2645 /**
2646 * Verify that a vertex shader executable meets all semantic requirements.
2647 *
2648 * Also sets info.clip_distance_array_size and
2649 * info.cull_distance_array_size as a side effect.
2650 *
2651 * \param shader Vertex shader executable to be verified
2652 */
2653 static void
validate_vertex_shader_executable(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts)2654 validate_vertex_shader_executable(struct gl_shader_program *prog,
2655 nir_shader *shader,
2656 const struct gl_constants *consts)
2657 {
2658 if (shader == NULL)
2659 return;
2660
2661 /* From the GLSL 1.10 spec, page 48:
2662 *
2663 * "The variable gl_Position is available only in the vertex
2664 * language and is intended for writing the homogeneous vertex
2665 * position. All executions of a well-formed vertex shader
2666 * executable must write a value into this variable. [...] The
2667 * variable gl_Position is available only in the vertex
2668 * language and is intended for writing the homogeneous vertex
2669 * position. All executions of a well-formed vertex shader
2670 * executable must write a value into this variable."
2671 *
2672 * while in GLSL 1.40 this text is changed to:
2673 *
2674 * "The variable gl_Position is available only in the vertex
2675 * language and is intended for writing the homogeneous vertex
2676 * position. It can be written at any time during shader
2677 * execution. It may also be read back by a vertex shader
2678 * after being written. This value will be used by primitive
2679 * assembly, clipping, culling, and other fixed functionality
2680 * operations, if present, that operate on primitives after
2681 * vertex processing has occurred. Its value is undefined if
2682 * the vertex shader executable does not write gl_Position."
2683 *
2684 * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
2685 * gl_Position is not an error.
2686 */
2687 if (prog->GLSL_Version < (prog->IsES ? 300 : 140)) {
2688 nir_variable *gl_position =
2689 nir_find_variable_with_location(shader,
2690 nir_var_shader_out,
2691 VARYING_SLOT_POS);
2692
2693 bool gl_position_written = false;
2694 find_assignments(shader, gl_position, NULL, NULL, &gl_position_written,
2695 NULL, NULL);
2696 if (!gl_position_written) {
2697 if (prog->IsES) {
2698 linker_warning(prog,
2699 "vertex shader does not write to `gl_Position'. "
2700 "Its value is undefined. \n");
2701 } else {
2702 linker_error(prog,
2703 "vertex shader does not write to `gl_Position'. \n");
2704 }
2705 return;
2706 }
2707 }
2708
2709 analyze_clip_cull_usage(prog, shader, consts, &shader->info);
2710 }
2711
2712 static void
validate_tess_eval_shader_executable(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts)2713 validate_tess_eval_shader_executable(struct gl_shader_program *prog,
2714 nir_shader *shader,
2715 const struct gl_constants *consts)
2716 {
2717 if (shader == NULL)
2718 return;
2719
2720 analyze_clip_cull_usage(prog, shader, consts, &shader->info);
2721 }
2722
2723 /**
2724 * Verify that a fragment shader executable meets all semantic requirements
2725 *
2726 * \param shader Fragment shader executable to be verified
2727 */
2728 static void
validate_fragment_shader_executable(struct gl_shader_program * prog,nir_shader * shader)2729 validate_fragment_shader_executable(struct gl_shader_program *prog,
2730 nir_shader *shader)
2731 {
2732 if (shader == NULL)
2733 return;
2734
2735 nir_variable *gl_frag_color =
2736 nir_find_variable_with_location(shader,
2737 nir_var_shader_out,
2738 FRAG_RESULT_COLOR);
2739 nir_variable *gl_frag_data =
2740 nir_find_variable_with_location(shader,
2741 nir_var_shader_out,
2742 FRAG_RESULT_DATA0);
2743
2744 bool gl_frag_color_written = false;
2745 bool gl_frag_data_written = false;
2746 find_assignments(shader, gl_frag_color, gl_frag_data, NULL,
2747 &gl_frag_color_written, &gl_frag_data_written, NULL);
2748
2749 if (gl_frag_color_written && gl_frag_data_written) {
2750 linker_error(prog, "fragment shader writes to both "
2751 "`gl_FragColor' and `gl_FragData'\n");
2752 }
2753 }
2754
2755 /**
2756 * Verify that a geometry shader executable meets all semantic requirements
2757 *
2758 * Also sets prog->Geom.VerticesIn, and info.clip_distance_array_sizeand
2759 * info.cull_distance_array_size as a side effect.
2760 *
2761 * \param shader Geometry shader executable to be verified
2762 */
2763 static void
validate_geometry_shader_executable(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts)2764 validate_geometry_shader_executable(struct gl_shader_program *prog,
2765 nir_shader *shader,
2766 const struct gl_constants *consts)
2767 {
2768 if (shader == NULL)
2769 return;
2770
2771 unsigned num_vertices =
2772 mesa_vertices_per_prim(shader->info.gs.input_primitive);
2773 shader->info.gs.vertices_in = num_vertices;
2774
2775 analyze_clip_cull_usage(prog, shader, consts, &shader->info);
2776 }
2777
2778 bool
gl_nir_link_glsl(struct gl_context * ctx,struct gl_shader_program * prog)2779 gl_nir_link_glsl(struct gl_context *ctx, struct gl_shader_program *prog)
2780 {
2781 const struct gl_constants *consts = &ctx->Const;
2782 const struct gl_extensions *exts = &ctx->Extensions;
2783 gl_api api = ctx->API;
2784
2785 if (prog->NumShaders == 0)
2786 return true;
2787
2788 MESA_TRACE_FUNC();
2789
2790 /* Link all shaders for a particular stage and validate the result.
2791 */
2792 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
2793 struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
2794 if (sh) {
2795 nir_shader *shader = sh->Program->nir;
2796
2797 switch (stage) {
2798 case MESA_SHADER_VERTEX:
2799 validate_vertex_shader_executable(prog, shader, consts);
2800 break;
2801 case MESA_SHADER_TESS_CTRL:
2802 /* nothing to be done */
2803 break;
2804 case MESA_SHADER_TESS_EVAL:
2805 validate_tess_eval_shader_executable(prog, shader, consts);
2806 break;
2807 case MESA_SHADER_GEOMETRY:
2808 validate_geometry_shader_executable(prog, shader, consts);
2809 break;
2810 case MESA_SHADER_FRAGMENT:
2811 validate_fragment_shader_executable(prog, shader);
2812 break;
2813 }
2814 if (!prog->data->LinkStatus) {
2815 _mesa_delete_linked_shader(ctx, sh);
2816
2817 prog->_LinkedShaders[stage] = NULL;
2818 prog->data->linked_stages ^= 1 << stage;
2819
2820 return false;
2821 }
2822 }
2823 }
2824
2825 /* Here begins the inter-stage linking phase. Some initial validation is
2826 * performed, then locations are assigned for uniforms, attributes, and
2827 * varyings.
2828 */
2829 cross_validate_uniforms(consts, prog);
2830 if (!prog->data->LinkStatus)
2831 return false;
2832
2833 check_explicit_uniform_locations(exts, prog);
2834
2835 link_assign_subroutine_types(prog);
2836 verify_subroutine_associated_funcs(prog);
2837 if (!prog->data->LinkStatus)
2838 return false;
2839
2840 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2841 if (prog->_LinkedShaders[i] == NULL)
2842 continue;
2843
2844 gl_nir_detect_recursion_linked(prog,
2845 prog->_LinkedShaders[i]->Program->nir);
2846 if (!prog->data->LinkStatus)
2847 return false;
2848
2849 gl_nir_inline_functions(prog->_LinkedShaders[i]->Program->nir);
2850 }
2851
2852 resize_tes_inputs(consts, prog);
2853 set_geom_shader_input_array_size(prog);
2854
2855 /* Validate the inputs of each stage with the output of the preceding
2856 * stage.
2857 */
2858 unsigned prev = MESA_SHADER_STAGES;
2859 for (unsigned i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
2860 if (prog->_LinkedShaders[i] == NULL)
2861 continue;
2862
2863 if (prev == MESA_SHADER_STAGES) {
2864 prev = i;
2865 continue;
2866 }
2867
2868 gl_nir_validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev],
2869 prog->_LinkedShaders[i]);
2870 if (!prog->data->LinkStatus)
2871 return false;
2872
2873 prev = i;
2874 }
2875
2876 /* Cross-validate uniform blocks between shader stages */
2877 gl_nir_validate_interstage_uniform_blocks(prog, prog->_LinkedShaders);
2878 if (!prog->data->LinkStatus)
2879 return false;
2880
2881 if (prog->IsES && prog->GLSL_Version == 100)
2882 if (!validate_invariant_builtins(consts, prog,
2883 prog->_LinkedShaders[MESA_SHADER_VERTEX],
2884 prog->_LinkedShaders[MESA_SHADER_FRAGMENT]))
2885 return false;
2886
2887 /* Check and validate stream emissions in geometry shaders */
2888 validate_geometry_shader_emissions(consts, prog);
2889
2890 prog->last_vert_prog = NULL;
2891 for (int i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
2892 if (prog->_LinkedShaders[i] == NULL)
2893 continue;
2894
2895 prog->last_vert_prog = prog->_LinkedShaders[i]->Program;
2896 break;
2897 }
2898
2899 unsigned first = MESA_SHADER_STAGES;
2900 unsigned last = 0;
2901
2902 /* Determine first and last stage. */
2903 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2904 if (!prog->_LinkedShaders[i])
2905 continue;
2906 if (first == MESA_SHADER_STAGES)
2907 first = i;
2908 last = i;
2909 }
2910
2911 /* Implement the GLSL 1.30+ rule for discard vs infinite loops.
2912 * This rule also applies to GLSL ES 3.00.
2913 */
2914 if (prog->GLSL_Version >= (prog->IsES ? 300 : 130)) {
2915 struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2916 if (sh)
2917 gl_nir_lower_discard_flow(sh->Program->nir);
2918 }
2919
2920 gl_nir_lower_named_interface_blocks(prog);
2921
2922 /* Validate the inputs of each stage with the output of the preceding
2923 * stage.
2924 */
2925 prev = first;
2926 for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) {
2927 if (prog->_LinkedShaders[i] == NULL)
2928 continue;
2929
2930 gl_nir_cross_validate_outputs_to_inputs(consts, prog,
2931 prog->_LinkedShaders[prev],
2932 prog->_LinkedShaders[i]);
2933 if (!prog->data->LinkStatus)
2934 return false;
2935
2936 prev = i;
2937 }
2938
2939 /* The cross validation of outputs/inputs above validates interstage
2940 * explicit locations. We need to do this also for the inputs in the first
2941 * stage and outputs of the last stage included in the program, since there
2942 * is no cross validation for these.
2943 */
2944 gl_nir_validate_first_and_last_interface_explicit_locations(consts, prog,
2945 (gl_shader_stage) first,
2946 (gl_shader_stage) last);
2947
2948 if (prog->SeparateShader)
2949 disable_varying_optimizations_for_sso(prog);
2950
2951 struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
2952 unsigned num_shaders = 0;
2953
2954 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2955 if (prog->_LinkedShaders[i]) {
2956 linked_shader[num_shaders++] = prog->_LinkedShaders[i];
2957
2958 /* Section 13.46 (Vertex Attribute Aliasing) of the OpenGL ES 3.2
2959 * specification says:
2960 *
2961 * "In general, the behavior of GLSL ES should not depend on
2962 * compiler optimizations which might be implementation-dependent.
2963 * Name matching rules in most languages, including C++ from which
2964 * GLSL ES is derived, are based on declarations rather than use.
2965 *
2966 * RESOLUTION: The existence of aliasing is determined by
2967 * declarations present after preprocessing."
2968 *
2969 * Because of this rule, we don't remove dead attributes before
2970 * attribute assignment for vertex shader inputs here.
2971 */
2972 if (!(prog->IsES && prog->GLSL_Version >= 300 && i == MESA_SHADER_VERTEX))
2973 remove_dead_varyings_pre_linking(prog->_LinkedShaders[i]->Program->nir);
2974 }
2975 }
2976
2977 if (!gl_assign_attribute_or_color_locations(consts, prog))
2978 return false;
2979
2980 if (!prelink_lowering(consts, exts, prog, linked_shader, num_shaders))
2981 return false;
2982
2983 if (!gl_nir_link_varyings(consts, exts, api, prog))
2984 return false;
2985
2986 /* Validation for special cases where we allow sampler array indexing
2987 * with loop induction variable. This check emits a warning or error
2988 * depending if backend can handle dynamic indexing.
2989 */
2990 if ((!prog->IsES && prog->GLSL_Version < 130) ||
2991 (prog->IsES && prog->GLSL_Version < 300)) {
2992 if (!validate_sampler_array_indexing(consts, prog))
2993 return false;
2994 }
2995
2996 if (prog->data->LinkStatus == LINKING_FAILURE)
2997 return false;
2998
2999 if (!linked_shader[0]->Program->nir->info.io_lowered) {
3000 /* Linking the stages in the opposite order (from fragment to vertex)
3001 * ensures that inter-shader outputs written to in an earlier stage
3002 * are eliminated if they are (transitively) not used in a later
3003 * stage.
3004 */
3005 for (int i = num_shaders - 2; i >= 0; i--) {
3006 gl_nir_link_opts(linked_shader[i]->Program->nir,
3007 linked_shader[i + 1]->Program->nir);
3008 }
3009 }
3010
3011 /* Tidy up any left overs from the linking process for single shaders.
3012 * For example varying arrays that get packed may have dead elements that
3013 * can be now be eliminated now that array access has been lowered.
3014 */
3015 if (num_shaders == 1)
3016 gl_nir_opts(linked_shader[0]->Program->nir);
3017
3018 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3019 struct gl_linked_shader *shader = prog->_LinkedShaders[i];
3020 if (shader) {
3021 if (consts->GLSLLowerConstArrays) {
3022 nir_lower_const_arrays_to_uniforms(shader->Program->nir,
3023 consts->Program[i].MaxUniformComponents);
3024 }
3025
3026 const nir_remove_dead_variables_options opts = {
3027 .can_remove_var = can_remove_var,
3028 };
3029 nir_remove_dead_variables(shader->Program->nir,
3030 nir_var_uniform | nir_var_image |
3031 nir_var_mem_ubo | nir_var_mem_ssbo |
3032 nir_var_system_value,
3033 &opts);
3034
3035 if (shader->Program->info.stage == MESA_SHADER_FRAGMENT) {
3036 nir_shader *nir = shader->Program->nir;
3037 nir_foreach_variable_in_shader(var, nir) {
3038 if (var->data.mode == nir_var_system_value &&
3039 (var->data.location == SYSTEM_VALUE_SAMPLE_ID ||
3040 var->data.location == SYSTEM_VALUE_SAMPLE_POS))
3041 nir->info.fs.uses_sample_shading = true;
3042
3043 if (var->data.mode == nir_var_shader_in && var->data.sample)
3044 nir->info.fs.uses_sample_shading = true;
3045
3046 if (var->data.mode == nir_var_shader_out &&
3047 var->data.fb_fetch_output)
3048 nir->info.fs.uses_sample_shading = true;
3049 }
3050 }
3051 }
3052 }
3053
3054 if (!gl_nir_link_uniform_blocks(consts, prog))
3055 return false;
3056
3057 if (!gl_nir_link_uniforms(consts, prog, true))
3058 return false;
3059
3060 link_util_calculate_subroutine_compat(prog);
3061 link_util_check_uniform_resources(consts, prog);
3062 link_util_check_subroutine_resources(prog);
3063 check_image_resources(consts, exts, prog);
3064 gl_nir_link_assign_atomic_counter_resources(consts, prog);
3065 gl_nir_link_check_atomic_counter_resources(consts, prog);
3066
3067 /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
3068 * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
3069 * anything about shader linking when one of the shaders (vertex or
3070 * fragment shader) is absent. So, the extension shouldn't change the
3071 * behavior specified in GLSL specification.
3072 *
3073 * From OpenGL ES 3.1 specification (7.3 Program Objects):
3074 * "Linking can fail for a variety of reasons as specified in the
3075 * OpenGL ES Shading Language Specification, as well as any of the
3076 * following reasons:
3077 *
3078 * ...
3079 *
3080 * * program contains objects to form either a vertex shader or
3081 * fragment shader, and program is not separable, and does not
3082 * contain objects to form both a vertex shader and fragment
3083 * shader."
3084 *
3085 * However, the only scenario in 3.1+ where we don't require them both is
3086 * when we have a compute shader. For example:
3087 *
3088 * - No shaders is a link error.
3089 * - Geom or Tess without a Vertex shader is a link error which means we
3090 * always require a Vertex shader and hence a Fragment shader.
3091 * - Finally a Compute shader linked with any other stage is a link error.
3092 */
3093 if (!prog->SeparateShader && _mesa_is_api_gles2(api) &&
3094 !prog->_LinkedShaders[MESA_SHADER_COMPUTE]) {
3095 if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
3096 linker_error(prog, "program lacks a vertex shader\n");
3097 } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
3098 linker_error(prog, "program lacks a fragment shader\n");
3099 }
3100 }
3101
3102 if (prog->data->LinkStatus == LINKING_FAILURE)
3103 return false;
3104
3105 return true;
3106 }
3107