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 "util/glheader.h"
37 #include "util/perf/cpu_trace.h"
38 
39 /**
40  * This file included general link methods, using NIR.
41  */
42 
43 void
gl_nir_opts(nir_shader * nir)44 gl_nir_opts(nir_shader *nir)
45 {
46    bool progress;
47 
48    MESA_TRACE_FUNC();
49 
50    do {
51       progress = false;
52 
53       NIR_PASS(_, nir, nir_lower_vars_to_ssa);
54 
55       /* Linking deals with unused inputs/outputs, but here we can remove
56        * things local to the shader in the hopes that we can cleanup other
57        * things. This pass will also remove variables with only stores, so we
58        * might be able to make progress after it.
59        */
60       NIR_PASS(progress, nir, nir_remove_dead_variables,
61                nir_var_function_temp | nir_var_shader_temp |
62                nir_var_mem_shared,
63                NULL);
64 
65       NIR_PASS(progress, nir, nir_opt_find_array_copies);
66       NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
67       NIR_PASS(progress, nir, nir_opt_dead_write_vars);
68 
69       if (nir->options->lower_to_scalar) {
70          NIR_PASS(_, nir, nir_lower_alu_to_scalar,
71                     nir->options->lower_to_scalar_filter, NULL);
72          NIR_PASS(_, nir, nir_lower_phis_to_scalar, false);
73       }
74 
75       NIR_PASS(_, nir, nir_lower_alu);
76       NIR_PASS(_, nir, nir_lower_pack);
77       NIR_PASS(progress, nir, nir_copy_prop);
78       NIR_PASS(progress, nir, nir_opt_remove_phis);
79       NIR_PASS(progress, nir, nir_opt_dce);
80 
81       bool opt_loop_progress = false;
82       NIR_PASS(opt_loop_progress, nir, nir_opt_loop);
83       if (opt_loop_progress) {
84          progress = true;
85          NIR_PASS(progress, nir, nir_copy_prop);
86          NIR_PASS(progress, nir, nir_opt_dce);
87       }
88       NIR_PASS(progress, nir, nir_opt_if, 0);
89       NIR_PASS(progress, nir, nir_opt_dead_cf);
90       NIR_PASS(progress, nir, nir_opt_cse);
91       NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
92 
93       NIR_PASS(progress, nir, nir_opt_phi_precision);
94       NIR_PASS(progress, nir, nir_opt_algebraic);
95       NIR_PASS(progress, nir, nir_opt_constant_folding);
96       NIR_PASS(progress, nir, nir_io_add_const_offset_to_base,
97                nir_var_shader_in | nir_var_shader_out);
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 static void
array_length_to_const(nir_shader * shader)208 array_length_to_const(nir_shader *shader)
209 {
210    nir_foreach_function_impl(impl, shader) {
211       nir_builder b = nir_builder_create(impl);
212       nir_foreach_block(block, impl) {
213          nir_foreach_instr_safe(instr, block) {
214             if (instr->type == nir_instr_type_intrinsic) {
215                nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
216                if (intrin->intrinsic == nir_intrinsic_deref_implicit_array_length) {
217                   b.cursor = nir_before_instr(instr);
218                   nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
219                   assert(deref->deref_type == nir_deref_type_var);
220 
221                   unsigned size = glsl_get_length(deref->var->type);
222                   nir_def *arr_size = nir_imm_intN_t(&b, size, 32);
223                   nir_def_rewrite_uses(&intrin->def, arr_size);
224                   nir_instr_remove(&intrin->instr);
225                }
226             }
227          }
228       }
229    }
230 }
231 
232 struct emit_vertex_state {
233    int max_stream_allowed;
234    int invalid_stream_id;
235    bool invalid_stream_id_from_emit_vertex;
236    bool end_primitive_found;
237    unsigned used_streams;
238 };
239 
240 /**
241  * Determine the highest stream id to which a (geometry) shader emits
242  * vertices. Also check whether End{Stream}Primitive is ever called.
243  */
244 static void
find_emit_vertex(struct emit_vertex_state * state,nir_shader * shader)245 find_emit_vertex(struct emit_vertex_state *state, nir_shader *shader) {
246    nir_function_impl *impl = nir_shader_get_entrypoint(shader);
247 
248    nir_foreach_block_safe(block, impl) {
249       nir_foreach_instr_safe(instr, block) {
250          if (instr->type == nir_instr_type_intrinsic) {
251             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
252 
253             if (intr->intrinsic == nir_intrinsic_emit_vertex ||
254                 intr->intrinsic == nir_intrinsic_end_primitive) {
255                int stream_id = nir_intrinsic_stream_id(intr);
256                bool from_emit_vertex =
257                   intr->intrinsic == nir_intrinsic_emit_vertex;
258                state->end_primitive_found |=
259                   intr->intrinsic == nir_intrinsic_end_primitive;
260 
261                if (stream_id < 0) {
262                   state->invalid_stream_id = stream_id;
263                   state->invalid_stream_id_from_emit_vertex = from_emit_vertex;
264                   return;
265                }
266 
267                if (stream_id > state->max_stream_allowed) {
268                   state->invalid_stream_id = stream_id;
269                   state->invalid_stream_id_from_emit_vertex = from_emit_vertex;
270                   return;
271                }
272 
273                state->used_streams |= 1 << stream_id;
274             }
275          }
276       }
277    }
278 }
279 
280 /**
281  * Check if geometry shaders emit to non-zero streams and do corresponding
282  * validations.
283  */
284 static void
validate_geometry_shader_emissions(const struct gl_constants * consts,struct gl_shader_program * prog)285 validate_geometry_shader_emissions(const struct gl_constants *consts,
286                                    struct gl_shader_program *prog)
287 {
288    struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
289 
290    if (sh != NULL) {
291       struct emit_vertex_state state;
292       state.max_stream_allowed = consts->MaxVertexStreams - 1;
293       state.invalid_stream_id = 0;
294       state.invalid_stream_id_from_emit_vertex = false;
295       state.end_primitive_found = false;
296       state.used_streams = 0;
297 
298       find_emit_vertex(&state, sh->Program->nir);
299 
300       if (state.invalid_stream_id != 0) {
301          linker_error(prog, "Invalid call %s(%d). Accepted values for the "
302                       "stream parameter are in the range [0, %d].\n",
303                       state.invalid_stream_id_from_emit_vertex ?
304                          "EmitStreamVertex" : "EndStreamPrimitive",
305                       state.invalid_stream_id, state.max_stream_allowed);
306       }
307       sh->Program->nir->info.gs.active_stream_mask = state.used_streams;
308       sh->Program->nir->info.gs.uses_end_primitive = state.end_primitive_found;
309 
310       /* From the ARB_gpu_shader5 spec:
311        *
312        *   "Multiple vertex streams are supported only if the output primitive
313        *    type is declared to be "points".  A program will fail to link if it
314        *    contains a geometry shader calling EmitStreamVertex() or
315        *    EndStreamPrimitive() if its output primitive type is not "points".
316        *
317        * However, in the same spec:
318        *
319        *   "The function EmitVertex() is equivalent to calling EmitStreamVertex()
320        *    with <stream> set to zero."
321        *
322        * And:
323        *
324        *   "The function EndPrimitive() is equivalent to calling
325        *    EndStreamPrimitive() with <stream> set to zero."
326        *
327        * Since we can call EmitVertex() and EndPrimitive() when we output
328        * primitives other than points, calling EmitStreamVertex(0) or
329        * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
330        * does. We can use info.gs.active_stream_mask to check whether only the
331        * first (zero) stream is active.
332        * stream.
333        */
334       if (sh->Program->nir->info.gs.active_stream_mask & ~(1 << 0) &&
335           sh->Program->nir->info.gs.output_primitive != MESA_PRIM_POINTS) {
336          linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
337                       "with n>0 requires point output\n");
338       }
339    }
340 }
341 
342 /* For derivatives in compute shaders, GLSL_NV_compute_shader_derivatives
343  * states:
344  *
345  *    If neither layout qualifier is specified, derivatives in compute
346  *    shaders return zero, which is consistent with the handling of built-in
347  *    texture functions like texture() in GLSL 4.50 compute shaders.
348  */
349 static void
lower_derivatives_without_layout(nir_builder * b)350 lower_derivatives_without_layout(nir_builder *b)
351 {
352    if (b->shader->info.stage != MESA_SHADER_COMPUTE ||
353        b->shader->info.derivative_group != DERIVATIVE_GROUP_NONE)
354       return;
355 
356    nir_foreach_function_impl(impl, b->shader) {
357       nir_foreach_block(block, impl) {
358          nir_foreach_instr_safe(instr, block) {
359             if (instr->type == nir_instr_type_intrinsic) {
360                nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
361                nir_intrinsic_op op = intrin->intrinsic;
362                if (op != nir_intrinsic_ddx && op != nir_intrinsic_ddx_fine && op != nir_intrinsic_ddx_coarse &&
363                    op != nir_intrinsic_ddy && op != nir_intrinsic_ddy_fine && op != nir_intrinsic_ddy_coarse)
364                   continue;
365 
366                nir_def *def = &intrin->def;
367                b->cursor = nir_before_instr(instr);
368                nir_def *zero = nir_imm_zero(b, def->num_components,
369                                             def->bit_size);
370                nir_def_replace(def, zero);
371             } else {
372                continue;
373             }
374          }
375       }
376    }
377 }
378 
379 /**
380  * Generate a string describing the mode of a variable
381  */
382 const char *
gl_nir_mode_string(const nir_variable * var)383 gl_nir_mode_string(const nir_variable *var)
384 {
385    switch (var->data.mode) {
386    case nir_var_shader_temp:
387       return (var->data.read_only) ? "global constant" : "global variable";
388 
389    case nir_var_uniform:
390    case nir_var_image:
391    case nir_var_mem_ubo:
392       return "uniform";
393 
394    case nir_var_mem_ssbo:
395       return "buffer";
396 
397    case nir_var_shader_in:
398       return "shader input";
399 
400    case nir_var_shader_out:
401       return "shader output";
402 
403    case nir_var_system_value:
404       return "shader input";
405 
406    case nir_var_function_temp:
407       return "local variable";
408 
409    case nir_var_mem_shared:
410       return "shader shared";
411 
412    case nir_num_variable_modes:
413       break;
414    }
415 
416    assert(!"Should not get here.");
417    return "invalid variable";
418 }
419 
420 static void
remove_dead_functions(nir_shader * shader)421 remove_dead_functions(nir_shader *shader)
422 {
423    struct set *fn_set =
424       _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
425 
426    /* Find all function prototypes adding them to a list then removing them
427     * if they are ever called.
428     */
429    nir_foreach_function_impl(impl, shader) {
430       _mesa_set_add(fn_set, impl->function);
431    }
432 
433    nir_foreach_function_impl(impl, shader) {
434       nir_foreach_block(block, impl) {
435          nir_foreach_instr(instr, block) {
436             if (instr->type == nir_instr_type_call) {
437                nir_call_instr *call = nir_instr_as_call(instr);
438                _mesa_set_remove_key(fn_set, call->callee);
439             }
440          }
441       }
442    }
443 
444    /* Any functions remaining in the list must be unused so remove them. */
445    set_foreach(fn_set, entry) {
446       nir_function *func = (nir_function *) entry->key;
447       if (!func->is_entrypoint)
448          exec_node_remove(&func->node);
449    }
450 
451    _mesa_set_destroy(fn_set, NULL);
452 }
453 
454 bool
gl_nir_can_add_pointsize_to_program(const struct gl_constants * consts,struct gl_program * prog)455 gl_nir_can_add_pointsize_to_program(const struct gl_constants *consts,
456                                     struct gl_program *prog)
457 {
458    nir_shader *nir = prog->nir;
459    if (!nir)
460       return true; /* fixedfunction */
461 
462    assert(nir->info.stage == MESA_SHADER_VERTEX ||
463           nir->info.stage == MESA_SHADER_TESS_EVAL ||
464           nir->info.stage == MESA_SHADER_GEOMETRY);
465    if (nir->info.outputs_written & VARYING_BIT_PSIZ)
466       return false;
467 
468    unsigned max_components = nir->info.stage == MESA_SHADER_GEOMETRY ?
469                              consts->MaxGeometryTotalOutputComponents :
470                              consts->Program[nir->info.stage].MaxOutputComponents;
471    unsigned num_components = 0;
472    unsigned needed_components = nir->info.stage == MESA_SHADER_GEOMETRY ? nir->info.gs.vertices_out : 1;
473    nir_foreach_shader_out_variable(var, nir) {
474       num_components += glsl_count_dword_slots(var->type, false);
475    }
476 
477    /* Ensure that there is enough attribute space to emit at least one primitive */
478    if (num_components && nir->info.stage == MESA_SHADER_GEOMETRY) {
479       if (num_components + needed_components > consts->Program[nir->info.stage].MaxOutputComponents)
480          return false;
481       num_components *= nir->info.gs.vertices_out;
482    }
483 
484    return num_components + needed_components <= max_components;
485 }
486 
487 static void
gl_nir_link_opts(nir_shader * producer,nir_shader * consumer)488 gl_nir_link_opts(nir_shader *producer, nir_shader *consumer)
489 {
490    MESA_TRACE_FUNC();
491 
492    if (producer->options->lower_to_scalar) {
493       NIR_PASS(_, producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
494       NIR_PASS(_, consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
495    }
496 
497    nir_lower_io_arrays_to_elements(producer, consumer);
498 
499    gl_nir_opts(producer);
500    gl_nir_opts(consumer);
501 
502    if (nir_link_opt_varyings(producer, consumer))
503       gl_nir_opts(consumer);
504 
505    NIR_PASS(_, producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
506    NIR_PASS(_, consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
507 
508    if (nir_remove_unused_varyings(producer, consumer)) {
509       NIR_PASS(_, producer, nir_lower_global_vars_to_local);
510       NIR_PASS(_, consumer, nir_lower_global_vars_to_local);
511 
512       gl_nir_opts(producer);
513       gl_nir_opts(consumer);
514 
515       /* Optimizations can cause varyings to become unused.
516        * nir_compact_varyings() depends on all dead varyings being removed so
517        * we need to call nir_remove_dead_variables() again here.
518        */
519       NIR_PASS(_, producer, nir_remove_dead_variables, nir_var_shader_out,
520                  NULL);
521       NIR_PASS(_, consumer, nir_remove_dead_variables, nir_var_shader_in,
522                  NULL);
523    }
524 
525    nir_link_varying_precision(producer, consumer);
526 }
527 
528 static bool
can_remove_var(nir_variable * var,UNUSED void * data)529 can_remove_var(nir_variable *var, UNUSED void *data)
530 {
531    /* Section 2.11.6 (Uniform Variables) of the OpenGL ES 3.0.3 spec
532     * says:
533     *
534     *     "All members of a named uniform block declared with a shared or
535     *     std140 layout qualifier are considered active, even if they are not
536     *     referenced in any shader in the program. The uniform block itself is
537     *     also considered active, even if no member of the block is
538     *     referenced."
539     *
540     * Although the spec doesn't state it std430 layouts are expect to behave
541     * the same way. If the variable is in a uniform block with one of those
542     * layouts, do not eliminate it.
543     */
544    if (nir_variable_is_in_block(var) &&
545        (glsl_get_ifc_packing(var->interface_type) !=
546         GLSL_INTERFACE_PACKING_PACKED))
547       return false;
548 
549    if (glsl_get_base_type(glsl_without_array(var->type)) ==
550        GLSL_TYPE_SUBROUTINE)
551       return false;
552 
553    /* Uniform initializers could get used by another stage. However if its a
554     * hidden uniform then it should be safe to remove as this was a constant
555     * variable that has been lowered to a uniform.
556     */
557    if (var->constant_initializer && var->data.how_declared != nir_var_hidden)
558       return false;
559 
560    return true;
561 }
562 
563 static void
set_always_active_io(nir_shader * shader,nir_variable_mode io_mode)564 set_always_active_io(nir_shader *shader, nir_variable_mode io_mode)
565 {
566    assert(io_mode == nir_var_shader_in || io_mode == nir_var_shader_out);
567 
568    nir_foreach_variable_with_modes(var, shader, io_mode) {
569       /* Don't set always active on builtins that haven't been redeclared */
570       if (var->data.how_declared == nir_var_declared_implicitly)
571          continue;
572 
573       var->data.always_active_io = true;
574    }
575 }
576 
577 /**
578  * When separate shader programs are enabled, only input/outputs between
579  * the stages of a multi-stage separate program can be safely removed
580  * from the shader interface. Other inputs/outputs must remain active.
581  */
582 static void
disable_varying_optimizations_for_sso(struct gl_shader_program * prog)583 disable_varying_optimizations_for_sso(struct gl_shader_program *prog)
584 {
585    unsigned first, last;
586    assert(prog->SeparateShader);
587 
588    first = MESA_SHADER_STAGES;
589    last = 0;
590 
591    /* Determine first and last stage. Excluding the compute stage */
592    for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
593       if (!prog->_LinkedShaders[i])
594          continue;
595       if (first == MESA_SHADER_STAGES)
596          first = i;
597       last = i;
598    }
599 
600    if (first == MESA_SHADER_STAGES)
601       return;
602 
603    for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
604       if (!prog->_LinkedShaders[stage])
605          continue;
606 
607       /* Prevent the removal of inputs to the first and outputs from the last
608        * stage, unless they are the initial pipeline inputs or final pipeline
609        * outputs, respectively.
610        *
611        * The removal of IO between shaders in the same program is always
612        * allowed.
613        */
614       if (stage == first && stage != MESA_SHADER_VERTEX) {
615          set_always_active_io(prog->_LinkedShaders[stage]->Program->nir,
616                               nir_var_shader_in);
617       }
618 
619       if (stage == last && stage != MESA_SHADER_FRAGMENT) {
620          set_always_active_io(prog->_LinkedShaders[stage]->Program->nir,
621                               nir_var_shader_out);
622       }
623    }
624 }
625 
626 static bool
inout_has_same_location(const nir_variable * var,unsigned stage)627 inout_has_same_location(const nir_variable *var, unsigned stage)
628 {
629    if (!var->data.patch &&
630        ((var->data.mode == nir_var_shader_out &&
631          stage == MESA_SHADER_TESS_CTRL) ||
632         (var->data.mode == nir_var_shader_in &&
633          (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL ||
634           stage == MESA_SHADER_GEOMETRY))))
635       return true;
636    else
637       return false;
638 }
639 
640 /**
641  * Create gl_shader_variable from nir_variable.
642  */
643 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)644 create_shader_variable(struct gl_shader_program *shProg,
645                        const nir_variable *in,
646                        const char *name, const struct glsl_type *type,
647                        const struct glsl_type *interface_type,
648                        bool use_implicit_location, int location,
649                        const struct glsl_type *outermost_struct_type)
650 {
651    /* Allocate zero-initialized memory to ensure that bitfield padding
652     * is zero.
653     */
654    struct gl_shader_variable *out = rzalloc(shProg,
655                                             struct gl_shader_variable);
656    if (!out)
657       return NULL;
658 
659    /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications
660     * expect to see gl_VertexID in the program resource list.  Pretend.
661     */
662    if (in->data.mode == nir_var_system_value &&
663        in->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
664       out->name.string = ralloc_strdup(shProg, "gl_VertexID");
665    } else if ((in->data.mode == nir_var_shader_out &&
666                in->data.location == VARYING_SLOT_TESS_LEVEL_OUTER) ||
667               (in->data.mode == nir_var_system_value &&
668                in->data.location == SYSTEM_VALUE_TESS_LEVEL_OUTER)) {
669       out->name.string = ralloc_strdup(shProg, "gl_TessLevelOuter");
670       type = glsl_array_type(glsl_float_type(), 4, 0);
671    } else if ((in->data.mode == nir_var_shader_out &&
672                in->data.location == VARYING_SLOT_TESS_LEVEL_INNER) ||
673               (in->data.mode == nir_var_system_value &&
674                in->data.location == SYSTEM_VALUE_TESS_LEVEL_INNER)) {
675       out->name.string = ralloc_strdup(shProg, "gl_TessLevelInner");
676       type = glsl_array_type(glsl_float_type(), 2, 0);
677    } else {
678       out->name.string = ralloc_strdup(shProg, name);
679    }
680 
681    resource_name_updated(&out->name);
682 
683    if (!out->name.string)
684       return NULL;
685 
686    /* The ARB_program_interface_query spec says:
687     *
688     *     "Not all active variables are assigned valid locations; the
689     *     following variables will have an effective location of -1:
690     *
691     *      * uniforms declared as atomic counters;
692     *
693     *      * members of a uniform block;
694     *
695     *      * built-in inputs, outputs, and uniforms (starting with "gl_"); and
696     *
697     *      * inputs or outputs not declared with a "location" layout
698     *        qualifier, except for vertex shader inputs and fragment shader
699     *        outputs."
700     */
701    if (glsl_get_base_type(in->type) == GLSL_TYPE_ATOMIC_UINT ||
702        is_gl_identifier(in->name) ||
703        !(in->data.explicit_location || use_implicit_location)) {
704       out->location = -1;
705    } else {
706       out->location = location;
707    }
708 
709    out->type = type;
710    out->outermost_struct_type = outermost_struct_type;
711    out->interface_type = interface_type;
712    out->component = in->data.location_frac;
713    out->index = in->data.index;
714    out->patch = in->data.patch;
715    out->mode = in->data.mode;
716    out->interpolation = in->data.interpolation;
717    out->precision = in->data.precision;
718    out->explicit_location = in->data.explicit_location;
719 
720    return out;
721 }
722 
723 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)724 add_shader_variable(const struct gl_constants *consts,
725                     struct gl_shader_program *shProg,
726                     struct set *resource_set,
727                     unsigned stage_mask,
728                     GLenum programInterface, nir_variable *var,
729                     const char *name, const struct glsl_type *type,
730                     bool use_implicit_location, int location,
731                     bool inouts_share_location,
732                     const struct glsl_type *outermost_struct_type)
733 {
734    const struct glsl_type *interface_type = var->interface_type;
735 
736    if (outermost_struct_type == NULL) {
737       if (var->data.from_named_ifc_block) {
738          const char *interface_name = glsl_get_type_name(interface_type);
739 
740          if (glsl_type_is_array(interface_type)) {
741             /* Issue #16 of the ARB_program_interface_query spec says:
742              *
743              * "* If a variable is a member of an interface block without an
744              *    instance name, it is enumerated using just the variable name.
745              *
746              *  * If a variable is a member of an interface block with an
747              *    instance name, it is enumerated as "BlockName.Member", where
748              *    "BlockName" is the name of the interface block (not the
749              *    instance name) and "Member" is the name of the variable."
750              *
751              * In particular, it indicates that it should be "BlockName",
752              * not "BlockName[array length]".  The conformance suite and
753              * dEQP both require this behavior.
754              *
755              * Here, we unwrap the extra array level added by named interface
756              * block array lowering so we have the correct variable type.  We
757              * also unwrap the interface type when constructing the name.
758              *
759              * We leave interface_type the same so that ES 3.x SSO pipeline
760              * validation can enforce the rules requiring array length to
761              * match on interface blocks.
762              */
763             type = glsl_get_array_element(type);
764 
765             interface_name =
766                glsl_get_type_name(glsl_get_array_element(interface_type));
767          }
768 
769          name = ralloc_asprintf(shProg, "%s.%s", interface_name, name);
770       }
771    }
772 
773    switch (glsl_get_base_type(type)) {
774    case GLSL_TYPE_STRUCT: {
775       /* The ARB_program_interface_query spec says:
776        *
777        *     "For an active variable declared as a structure, a separate entry
778        *     will be generated for each active structure member.  The name of
779        *     each entry is formed by concatenating the name of the structure,
780        *     the "."  character, and the name of the structure member.  If a
781        *     structure member to enumerate is itself a structure or array,
782        *     these enumeration rules are applied recursively."
783        */
784       if (outermost_struct_type == NULL)
785          outermost_struct_type = type;
786 
787       unsigned field_location = location;
788       for (unsigned i = 0; i < glsl_get_length(type); i++) {
789          const struct glsl_type *field_type = glsl_get_struct_field(type, i);
790          const struct glsl_struct_field *field =
791             glsl_get_struct_field_data(type, i);
792 
793          char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name);
794          if (!add_shader_variable(consts, shProg, resource_set,
795                                   stage_mask, programInterface,
796                                   var, field_name, field_type,
797                                   use_implicit_location, field_location,
798                                   false, outermost_struct_type))
799             return false;
800 
801          field_location += glsl_count_attribute_slots(field_type, false);
802       }
803       return true;
804    }
805 
806    case GLSL_TYPE_ARRAY: {
807       /* The ARB_program_interface_query spec says:
808        *
809        *     "For an active variable declared as an array of basic types, a
810        *      single entry will be generated, with its name string formed by
811        *      concatenating the name of the array and the string "[0]"."
812        *
813        *     "For an active variable declared as an array of an aggregate data
814        *      type (structures or arrays), a separate entry will be generated
815        *      for each active array element, unless noted immediately below.
816        *      The name of each entry is formed by concatenating the name of
817        *      the array, the "[" character, an integer identifying the element
818        *      number, and the "]" character.  These enumeration rules are
819        *      applied recursively, treating each enumerated array element as a
820        *      separate active variable."
821        */
822       const struct glsl_type *array_type = glsl_get_array_element(type);
823       if (glsl_get_base_type(array_type) == GLSL_TYPE_STRUCT ||
824           glsl_get_base_type(array_type) == GLSL_TYPE_ARRAY) {
825          unsigned elem_location = location;
826          unsigned stride = inouts_share_location ? 0 :
827                            glsl_count_attribute_slots(array_type, false);
828          for (unsigned i = 0; i < glsl_get_length(type); i++) {
829             char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i);
830             if (!add_shader_variable(consts, shProg, resource_set,
831                                      stage_mask, programInterface,
832                                      var, elem, array_type,
833                                      use_implicit_location, elem_location,
834                                      false, outermost_struct_type))
835                return false;
836             elem_location += stride;
837          }
838          return true;
839       }
840    }
841    FALLTHROUGH;
842 
843    default: {
844       /* The ARB_program_interface_query spec says:
845        *
846        *     "For an active variable declared as a single instance of a basic
847        *     type, a single entry will be generated, using the variable name
848        *     from the shader source."
849        */
850       struct gl_shader_variable *sha_v =
851          create_shader_variable(shProg, var, name, type, interface_type,
852                                 use_implicit_location, location,
853                                 outermost_struct_type);
854       if (!sha_v)
855          return false;
856 
857       return link_util_add_program_resource(shProg, resource_set,
858                                             programInterface, sha_v, stage_mask);
859    }
860    }
861 }
862 
863 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)864 add_vars_with_modes(const struct gl_constants *consts,
865                     struct gl_shader_program *prog, struct set *resource_set,
866                     nir_shader *nir, nir_variable_mode modes,
867                     unsigned stage, GLenum programInterface)
868 {
869    nir_foreach_variable_with_modes(var, nir, modes) {
870       if (var->data.how_declared == nir_var_hidden)
871          continue;
872 
873       int loc_bias = 0;
874       switch(var->data.mode) {
875       case nir_var_system_value:
876       case nir_var_shader_in:
877          if (programInterface != GL_PROGRAM_INPUT)
878             continue;
879          loc_bias = (stage == MESA_SHADER_VERTEX) ? VERT_ATTRIB_GENERIC0
880                                                   : VARYING_SLOT_VAR0;
881          break;
882       case nir_var_shader_out:
883          if (programInterface != GL_PROGRAM_OUTPUT)
884             continue;
885          loc_bias = (stage == MESA_SHADER_FRAGMENT) ? FRAG_RESULT_DATA0
886                                                     : VARYING_SLOT_VAR0;
887          break;
888       default:
889          continue;
890       }
891 
892       if (var->data.patch)
893          loc_bias = VARYING_SLOT_PATCH0;
894 
895       if (prog->data->spirv) {
896          struct gl_shader_variable *sh_var =
897             rzalloc(prog, struct gl_shader_variable);
898 
899          /* In the ARB_gl_spirv spec, names are considered optional debug info, so
900           * the linker needs to work without them. Returning them is optional.
901           * For simplicity, we ignore names.
902           */
903          sh_var->name.string = NULL;
904          resource_name_updated(&sh_var->name);
905          sh_var->type = var->type;
906          sh_var->location = var->data.location - loc_bias;
907          sh_var->explicit_location = var->data.explicit_location;
908          sh_var->index = var->data.index;
909 
910          if (!link_util_add_program_resource(prog, resource_set,
911                                              programInterface,
912                                              sh_var, 1 << stage)) {
913            return false;
914          }
915       } else {
916          /* Skip packed varyings, packed varyings are handled separately
917           * by add_packed_varyings in the GLSL IR
918           * build_program_resource_list() call.
919           * TODO: handle packed varyings here instead. We likely want a NIR
920           * based packing pass first.
921           */
922          if (strncmp(var->name, "packed:", 7) == 0)
923             continue;
924 
925          const bool vs_input_or_fs_output =
926             (stage == MESA_SHADER_VERTEX &&
927              var->data.mode == nir_var_shader_in) ||
928             (stage == MESA_SHADER_FRAGMENT &&
929              var->data.mode == nir_var_shader_out);
930 
931          if (!add_shader_variable(consts, prog, resource_set,
932                                   1 << stage, programInterface,
933                                   var, var->name, var->type,
934                                   vs_input_or_fs_output,
935                                   var->data.location - loc_bias,
936                                   inout_has_same_location(var, stage),
937                                   NULL))
938             return false;
939       }
940    }
941 
942    return true;
943 }
944 
945 static bool
add_interface_variables(const struct gl_constants * consts,struct gl_shader_program * prog,struct set * resource_set,unsigned stage,GLenum programInterface)946 add_interface_variables(const struct gl_constants *consts,
947                         struct gl_shader_program *prog,
948                         struct set *resource_set,
949                         unsigned stage, GLenum programInterface)
950 {
951    struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
952    if (!sh)
953       return true;
954 
955    nir_shader *nir = sh->Program->nir;
956    assert(nir);
957 
958    switch (programInterface) {
959    case GL_PROGRAM_INPUT: {
960       return add_vars_with_modes(consts, prog, resource_set,
961                                  nir, nir_var_shader_in | nir_var_system_value,
962                                  stage, programInterface);
963    }
964    case GL_PROGRAM_OUTPUT:
965       return add_vars_with_modes(consts, prog, resource_set,
966                                  nir, nir_var_shader_out,
967                                  stage, programInterface);
968    default:
969       assert("!Should not get here");
970       break;
971    }
972 
973    return false;
974 }
975 
976 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)977 nir_add_packed_var_to_resource_list(const struct gl_constants *consts,
978                                     struct gl_shader_program *shProg,
979                                     struct set *resource_set,
980                                     nir_variable *var,
981                                     unsigned stage, GLenum type)
982 {
983    if (!add_shader_variable(consts, shProg, resource_set, 1 << stage,
984                             type, var, var->name, var->type, false,
985                             var->data.location - VARYING_SLOT_VAR0,
986                             inout_has_same_location(var, stage), NULL))
987       return false;
988 
989    return true;
990 }
991 
992 /**
993  * Initilise list of program resources that point to resource data.
994  */
995 void
init_program_resource_list(struct gl_shader_program * prog)996 init_program_resource_list(struct gl_shader_program *prog)
997 {
998    /* Rebuild resource list. */
999    if (prog->data->ProgramResourceList) {
1000       ralloc_free(prog->data->ProgramResourceList);
1001       prog->data->ProgramResourceList = NULL;
1002       prog->data->NumProgramResourceList = 0;
1003    }
1004 }
1005 
1006 void
nir_build_program_resource_list(const struct gl_constants * consts,struct gl_shader_program * prog,bool rebuild_resourse_list)1007 nir_build_program_resource_list(const struct gl_constants *consts,
1008                                 struct gl_shader_program *prog,
1009                                 bool rebuild_resourse_list)
1010 {
1011    /* Rebuild resource list. */
1012    if (rebuild_resourse_list)
1013       init_program_resource_list(prog);
1014 
1015    int input_stage = MESA_SHADER_STAGES, output_stage = 0;
1016 
1017    /* Determine first input and final output stage. These are used to
1018     * detect which variables should be enumerated in the resource list
1019     * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
1020     */
1021    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1022       if (!prog->_LinkedShaders[i])
1023          continue;
1024       if (input_stage == MESA_SHADER_STAGES)
1025          input_stage = i;
1026       output_stage = i;
1027    }
1028 
1029    /* Empty shader, no resources. */
1030    if (input_stage == MESA_SHADER_STAGES && output_stage == 0)
1031       return;
1032 
1033    struct set *resource_set = _mesa_pointer_set_create(NULL);
1034 
1035    /* Add inputs and outputs to the resource list. */
1036    if (!add_interface_variables(consts, prog, resource_set, input_stage,
1037                                 GL_PROGRAM_INPUT)) {
1038       return;
1039    }
1040 
1041    if (!add_interface_variables(consts, prog, resource_set, output_stage,
1042                                 GL_PROGRAM_OUTPUT)) {
1043       return;
1044    }
1045 
1046    /* Add transform feedback varyings and buffers. */
1047    if (prog->last_vert_prog) {
1048       struct gl_transform_feedback_info *linked_xfb =
1049          prog->last_vert_prog->sh.LinkedTransformFeedback;
1050 
1051       /* Add varyings. */
1052       if (linked_xfb->NumVarying > 0) {
1053          for (int i = 0; i < linked_xfb->NumVarying; i++) {
1054             if (!link_util_add_program_resource(prog, resource_set,
1055                                                 GL_TRANSFORM_FEEDBACK_VARYING,
1056                                                 &linked_xfb->Varyings[i], 0))
1057             return;
1058          }
1059       }
1060 
1061       /* Add buffers. */
1062       for (unsigned i = 0; i < consts->MaxTransformFeedbackBuffers; i++) {
1063          if ((linked_xfb->ActiveBuffers >> i) & 1) {
1064             linked_xfb->Buffers[i].Binding = i;
1065             if (!link_util_add_program_resource(prog, resource_set,
1066                                                 GL_TRANSFORM_FEEDBACK_BUFFER,
1067                                                 &linked_xfb->Buffers[i], 0))
1068             return;
1069          }
1070       }
1071    }
1072 
1073    /* Add uniforms
1074     *
1075     * Here, it is expected that nir_link_uniforms() has already been
1076     * called, so that UniformStorage table is already available.
1077     */
1078    int top_level_array_base_offset = -1;
1079    int top_level_array_size_in_bytes = -1;
1080    int second_element_offset = -1;
1081    int block_index = -1;
1082    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
1083       struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
1084 
1085       if (uniform->hidden) {
1086          for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) {
1087             if (!uniform->opaque[j].active ||
1088                 glsl_get_base_type(uniform->type) != GLSL_TYPE_SUBROUTINE)
1089                continue;
1090 
1091             GLenum type =
1092                _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j);
1093             /* add shader subroutines */
1094             if (!link_util_add_program_resource(prog, resource_set,
1095                                                 type, uniform, 0))
1096                return;
1097          }
1098 
1099          continue;
1100       }
1101 
1102       if (!link_util_should_add_buffer_variable(prog, uniform,
1103                                                 top_level_array_base_offset,
1104                                                 top_level_array_size_in_bytes,
1105                                                 second_element_offset, block_index))
1106          continue;
1107 
1108 
1109       if (prog->data->UniformStorage[i].offset >= second_element_offset) {
1110          top_level_array_base_offset =
1111             prog->data->UniformStorage[i].offset;
1112 
1113          top_level_array_size_in_bytes =
1114             prog->data->UniformStorage[i].top_level_array_size *
1115             prog->data->UniformStorage[i].top_level_array_stride;
1116 
1117          /* Set or reset the second element offset. For non arrays this
1118           * will be set to -1.
1119           */
1120          second_element_offset = top_level_array_size_in_bytes ?
1121             top_level_array_base_offset +
1122             prog->data->UniformStorage[i].top_level_array_stride : -1;
1123       }
1124       block_index = uniform->block_index;
1125 
1126 
1127       GLenum interface = uniform->is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM;
1128       if (!link_util_add_program_resource(prog, resource_set, interface, uniform,
1129                                           uniform->active_shader_mask)) {
1130          return;
1131       }
1132    }
1133 
1134 
1135    for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
1136       if (!link_util_add_program_resource(prog, resource_set, GL_UNIFORM_BLOCK,
1137                                           &prog->data->UniformBlocks[i],
1138                                           prog->data->UniformBlocks[i].stageref))
1139          return;
1140    }
1141 
1142    for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
1143       if (!link_util_add_program_resource(prog, resource_set, GL_SHADER_STORAGE_BLOCK,
1144                                           &prog->data->ShaderStorageBlocks[i],
1145                                           prog->data->ShaderStorageBlocks[i].stageref))
1146          return;
1147    }
1148 
1149    /* Add atomic counter buffers. */
1150    for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
1151       if (!link_util_add_program_resource(prog, resource_set, GL_ATOMIC_COUNTER_BUFFER,
1152                                           &prog->data->AtomicBuffers[i], 0))
1153          return;
1154    }
1155 
1156    unsigned mask = prog->data->linked_stages;
1157    while (mask) {
1158       const int i = u_bit_scan(&mask);
1159       struct gl_program *p = prog->_LinkedShaders[i]->Program;
1160 
1161       GLuint type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i);
1162       for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
1163          if (!link_util_add_program_resource(prog, resource_set,
1164                                              type,
1165                                              &p->sh.SubroutineFunctions[j],
1166                                              0))
1167             return;
1168       }
1169    }
1170 
1171    _mesa_set_destroy(resource_set, NULL);
1172 }
1173 
1174 static void
shared_type_info(const struct glsl_type * type,unsigned * size,unsigned * align)1175 shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
1176 {
1177    assert(glsl_type_is_vector_or_scalar(type));
1178 
1179    uint32_t comp_size = glsl_type_is_boolean(type)
1180       ? 4 : glsl_get_bit_size(type) / 8;
1181    unsigned length = glsl_get_vector_elements(type);
1182    *size = comp_size * length,
1183    *align = comp_size * (length == 3 ? 4 : length);
1184 }
1185 
1186 static bool
can_remove_varying_before_linking(nir_variable * var,void * data)1187 can_remove_varying_before_linking(nir_variable *var, void *data)
1188 {
1189    bool *is_sso = (bool *) data;
1190    if (*is_sso) {
1191       /* Allow the removal of unused builtins in SSO */
1192       return var->data.location > -1 && var->data.location < VARYING_SLOT_VAR0;
1193    } else
1194       return true;
1195 }
1196 
1197 static void
remove_dead_varyings_pre_linking(nir_shader * nir)1198 remove_dead_varyings_pre_linking(nir_shader *nir)
1199 {
1200    struct nir_remove_dead_variables_options opts;
1201    bool is_sso = nir->info.separate_shader;
1202    opts.can_remove_var_data = &is_sso;
1203    opts.can_remove_var = &can_remove_varying_before_linking;
1204    nir_variable_mode mask = nir_var_shader_in | nir_var_shader_out;
1205    nir_remove_dead_variables(nir, mask, &opts);
1206 }
1207 
1208 /* - create a gl_PointSize variable
1209  * - find every gl_Position write
1210  * - store 1.0 to gl_PointSize after every gl_Position write
1211  */
1212 bool
gl_nir_add_point_size(nir_shader * nir)1213 gl_nir_add_point_size(nir_shader *nir)
1214 {
1215    nir_variable *psiz = nir_create_variable_with_location(nir, nir_var_shader_out,
1216                                                           VARYING_SLOT_PSIZ, glsl_float_type());
1217    psiz->data.how_declared = nir_var_hidden;
1218 
1219    nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1220    nir_builder b = nir_builder_create(impl);
1221    bool found = false;
1222    nir_foreach_block_safe(block, impl) {
1223       nir_foreach_instr_safe(instr, block) {
1224          if (instr->type == nir_instr_type_intrinsic) {
1225             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
1226             if (intr->intrinsic == nir_intrinsic_store_deref ||
1227                 intr->intrinsic == nir_intrinsic_copy_deref) {
1228                nir_variable *var = nir_intrinsic_get_var(intr, 0);
1229                if (var->data.location == VARYING_SLOT_POS) {
1230                   b.cursor = nir_after_instr(instr);
1231                   nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
1232                   nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
1233                   found = true;
1234                }
1235             }
1236          }
1237       }
1238    }
1239    if (!found) {
1240       b.cursor = nir_before_impl(impl);
1241       nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
1242       nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
1243    }
1244 
1245    nir->info.outputs_written |= VARYING_BIT_PSIZ;
1246 
1247    /* We always modify the entrypoint */
1248    nir_metadata_preserve(impl, nir_metadata_control_flow);
1249    return true;
1250 }
1251 
1252 static void
zero_array_members(nir_builder * b,nir_variable * var)1253 zero_array_members(nir_builder *b, nir_variable *var)
1254 {
1255    nir_deref_instr *deref = nir_build_deref_var(b, var);
1256    nir_def *zero = nir_imm_zero(b, 4, 32);
1257    for (int i = 0; i < glsl_array_size(var->type); i++) {
1258       nir_deref_instr *arr = nir_build_deref_array_imm(b, deref, i);
1259       uint32_t mask = BITFIELD_MASK(glsl_get_vector_elements(arr->type));
1260       nir_store_deref(b, arr, nir_channels(b, zero, mask), mask);
1261    }
1262 }
1263 
1264 /* GL has an implicit default of 0 for unwritten gl_ClipDistance members;
1265  * to achieve this, write 0 to all members at the start of the shader and
1266  * let them be naturally overwritten later
1267  */
1268 static bool
gl_nir_zero_initialize_clip_distance(nir_shader * nir)1269 gl_nir_zero_initialize_clip_distance(nir_shader *nir)
1270 {
1271    nir_variable *clip_dist0 = nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_CLIP_DIST0);
1272    nir_variable *clip_dist1 = nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_CLIP_DIST1);
1273    if (!clip_dist0 && !clip_dist1)
1274       return false;
1275 
1276    nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1277    nir_builder b = nir_builder_at(nir_before_impl(impl));
1278    if (clip_dist0)
1279       zero_array_members(&b, clip_dist0);
1280 
1281    if (clip_dist1)
1282       zero_array_members(&b, clip_dist1);
1283 
1284    nir_metadata_preserve(impl, nir_metadata_control_flow);
1285    return true;
1286 }
1287 
1288 static void
lower_patch_vertices_in(struct gl_shader_program * shader_prog)1289 lower_patch_vertices_in(struct gl_shader_program *shader_prog)
1290 {
1291    struct gl_linked_shader *linked_tcs =
1292       shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL];
1293    struct gl_linked_shader *linked_tes =
1294       shader_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
1295 
1296    /* If we have a TCS and TES linked together, lower TES patch vertices. */
1297    if (linked_tcs && linked_tes) {
1298       nir_shader *tcs_nir = linked_tcs->Program->nir;
1299       nir_shader *tes_nir = linked_tes->Program->nir;
1300 
1301       /* The TES input vertex count is the TCS output vertex count,
1302        * lower TES gl_PatchVerticesIn to a constant.
1303        */
1304       uint32_t tes_patch_verts = tcs_nir->info.tess.tcs_vertices_out;
1305       NIR_PASS(_, tes_nir, nir_lower_patch_vertices, tes_patch_verts, NULL);
1306    }
1307 }
1308 
1309 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)1310 preprocess_shader(const struct gl_constants *consts,
1311                   const struct gl_extensions *exts,
1312                   struct gl_program *prog,
1313                   struct gl_shader_program *shader_program,
1314                   gl_shader_stage stage)
1315 {
1316    const struct gl_shader_compiler_options *gl_options =
1317       &consts->ShaderCompilerOptions[prog->info.stage];
1318    const nir_shader_compiler_options *options = gl_options->NirOptions;
1319    assert(options);
1320 
1321    nir_shader *nir = prog->nir;
1322    nir_shader_gather_info(prog->nir, nir_shader_get_entrypoint(prog->nir));
1323 
1324    if (prog->info.stage == MESA_SHADER_FRAGMENT && consts->HasFBFetch) {
1325 
1326       NIR_PASS(_, prog->nir, gl_nir_lower_blend_equation_advanced,
1327                  exts->KHR_blend_equation_advanced_coherent);
1328       nir_lower_global_vars_to_local(prog->nir);
1329       NIR_PASS(_, prog->nir, nir_opt_combine_stores, nir_var_shader_out);
1330    }
1331 
1332    /* Set the next shader stage hint for VS and TES. */
1333    if (!nir->info.separate_shader &&
1334        (nir->info.stage == MESA_SHADER_VERTEX ||
1335         nir->info.stage == MESA_SHADER_TESS_EVAL)) {
1336 
1337       unsigned prev_stages = (1 << (prog->info.stage + 1)) - 1;
1338       unsigned stages_mask =
1339          ~prev_stages & shader_program->data->linked_stages;
1340 
1341       nir->info.next_stage = stages_mask ?
1342          (gl_shader_stage) u_bit_scan(&stages_mask) : MESA_SHADER_FRAGMENT;
1343    } else {
1344       nir->info.next_stage = MESA_SHADER_FRAGMENT;
1345    }
1346 
1347    prog->skip_pointsize_xfb = !(nir->info.outputs_written & VARYING_BIT_PSIZ);
1348    if (!consts->PointSizeFixed && prog->skip_pointsize_xfb &&
1349        stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&
1350        gl_nir_can_add_pointsize_to_program(consts, prog)) {
1351       NIR_PASS(_, nir, gl_nir_add_point_size);
1352    }
1353 
1354    if (stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&
1355        (nir->info.outputs_written & (VARYING_BIT_CLIP_DIST0 | VARYING_BIT_CLIP_DIST1)))
1356       NIR_PASS(_, nir, gl_nir_zero_initialize_clip_distance);
1357 
1358    if (options->lower_all_io_to_temps ||
1359        nir->info.stage == MESA_SHADER_VERTEX ||
1360        nir->info.stage == MESA_SHADER_GEOMETRY) {
1361       NIR_PASS(_, nir, nir_lower_io_to_temporaries,
1362                  nir_shader_get_entrypoint(nir),
1363                  true, true);
1364    } else if (nir->info.stage == MESA_SHADER_TESS_EVAL ||
1365               nir->info.stage == MESA_SHADER_FRAGMENT) {
1366       NIR_PASS(_, nir, nir_lower_io_to_temporaries,
1367                  nir_shader_get_entrypoint(nir),
1368                  true, false);
1369    }
1370 
1371    NIR_PASS(_, nir, nir_lower_global_vars_to_local);
1372    NIR_PASS(_, nir, nir_split_var_copies);
1373    NIR_PASS(_, nir, nir_lower_var_copies);
1374 
1375    if (gl_options->LowerPrecisionFloat16 && gl_options->LowerPrecisionInt16) {
1376       NIR_PASS(_, nir, nir_lower_mediump_vars, nir_var_function_temp | nir_var_shader_temp | nir_var_mem_shared);
1377    }
1378 
1379    if (options->lower_to_scalar) {
1380       NIR_PASS(_, nir, nir_remove_dead_variables,
1381                  nir_var_function_temp | nir_var_shader_temp |
1382                  nir_var_mem_shared, NULL);
1383       NIR_PASS(_, nir, nir_opt_copy_prop_vars);
1384       NIR_PASS(_, nir, nir_lower_alu_to_scalar,
1385                  options->lower_to_scalar_filter, NULL);
1386    }
1387 
1388    NIR_PASS(_, nir, nir_opt_barrier_modes);
1389 
1390    /* before buffers and vars_to_ssa */
1391    NIR_PASS(_, nir, gl_nir_lower_images, true);
1392 
1393    if (prog->nir->info.stage == MESA_SHADER_COMPUTE) {
1394       NIR_PASS(_, prog->nir, nir_lower_vars_to_explicit_types,
1395                  nir_var_mem_shared, shared_type_info);
1396       NIR_PASS(_, prog->nir, nir_lower_explicit_io,
1397                  nir_var_mem_shared, nir_address_format_32bit_offset);
1398    }
1399 
1400    /* Do a round of constant folding to clean up address calculations */
1401    NIR_PASS(_, nir, nir_opt_constant_folding);
1402 }
1403 
1404 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)1405 prelink_lowering(const struct gl_constants *consts,
1406                  const struct gl_extensions *exts,
1407                  struct gl_shader_program *shader_program,
1408                  struct gl_linked_shader **linked_shader, unsigned num_shaders)
1409 {
1410    for (unsigned i = 0; i < num_shaders; i++) {
1411       struct gl_linked_shader *shader = linked_shader[i];
1412       const nir_shader_compiler_options *options =
1413          consts->ShaderCompilerOptions[shader->Stage].NirOptions;
1414       struct gl_program *prog = shader->Program;
1415 
1416       /* NIR drivers that support tess shaders and compact arrays need to use
1417       * GLSLTessLevelsAsInputs / pipe_caps.glsl_tess_levels_as_inputs. The NIR
1418       * linker doesn't support linking these as compat arrays of sysvals.
1419       */
1420       assert(consts->GLSLTessLevelsAsInputs || !options->compact_arrays ||
1421              !exts->ARB_tessellation_shader);
1422 
1423 
1424       /* ES 3.0+ vertex shaders may still have dead varyings but its now safe
1425        * to remove them as validation is now done according to the spec.
1426        */
1427       if (shader_program->IsES && shader_program->GLSL_Version >= 300 &&
1428           i == MESA_SHADER_VERTEX)
1429          remove_dead_varyings_pre_linking(prog->nir);
1430 
1431       preprocess_shader(consts, exts, prog, shader_program, shader->Stage);
1432 
1433       if (prog->nir->info.shared_size > consts->MaxComputeSharedMemorySize) {
1434          linker_error(shader_program, "Too much shared memory used (%u/%u)\n",
1435                       prog->nir->info.shared_size,
1436                       consts->MaxComputeSharedMemorySize);
1437          return false;
1438       }
1439 
1440       if (options->lower_to_scalar) {
1441          NIR_PASS(_, shader->Program->nir, nir_lower_load_const_to_scalar);
1442       }
1443    }
1444 
1445    lower_patch_vertices_in(shader_program);
1446 
1447    /* Linking shaders also optimizes them. Separate shaders, compute shaders
1448     * and shaders with a fixed-func VS or FS that don't need linking are
1449     * optimized here.
1450     */
1451    if (num_shaders == 1)
1452       gl_nir_opts(linked_shader[0]->Program->nir);
1453 
1454    /* nir_opt_access() needs to run before linking so that ImageAccess[]
1455     * and BindlessImage[].access are filled out with the correct modes.
1456     */
1457    for (unsigned i = 0; i < num_shaders; i++) {
1458       nir_shader *nir = linked_shader[i]->Program->nir;
1459 
1460       nir_opt_access_options opt_access_options;
1461       opt_access_options.is_vulkan = false;
1462       NIR_PASS(_, nir, nir_opt_access, &opt_access_options);
1463 
1464       if (!nir->options->compact_arrays) {
1465          NIR_PASS(_, nir, nir_lower_clip_cull_distance_to_vec4s);
1466          NIR_PASS(_, nir, nir_vectorize_tess_levels);
1467       }
1468 
1469       /* Combine clip and cull outputs into one array and set:
1470        * - shader_info::clip_distance_array_size
1471        * - shader_info::cull_distance_array_size
1472        */
1473       if (!(nir->options->io_options &
1474             nir_io_separate_clip_cull_distance_arrays))
1475          NIR_PASS(_, nir, nir_lower_clip_cull_distance_arrays);
1476    }
1477 
1478    return true;
1479 }
1480 
1481 static unsigned
get_varying_nir_var_mask(nir_shader * nir)1482 get_varying_nir_var_mask(nir_shader *nir)
1483 {
1484    return (nir->info.stage != MESA_SHADER_VERTEX ? nir_var_shader_in : 0) |
1485           (nir->info.stage != MESA_SHADER_FRAGMENT ? nir_var_shader_out : 0);
1486 }
1487 
1488 static nir_opt_varyings_progress
optimize_varyings(nir_shader * producer,nir_shader * consumer,bool spirv,unsigned max_uniform_comps,unsigned max_ubos)1489 optimize_varyings(nir_shader *producer, nir_shader *consumer, bool spirv,
1490                   unsigned max_uniform_comps, unsigned max_ubos)
1491 {
1492    nir_opt_varyings_progress progress =
1493       nir_opt_varyings(producer, consumer, spirv, max_uniform_comps,
1494                        max_ubos);
1495 
1496    if (progress & nir_progress_producer)
1497       gl_nir_opts(producer);
1498    if (progress & nir_progress_consumer)
1499       gl_nir_opts(consumer);
1500 
1501    return progress;
1502 }
1503 
1504 /**
1505  * Lower load_deref and store_deref on input/output variables to load_input
1506  * and store_output intrinsics, and perform varying optimizations and
1507  * compaction.
1508  */
1509 void
gl_nir_lower_optimize_varyings(const struct gl_constants * consts,struct gl_shader_program * prog,bool spirv)1510 gl_nir_lower_optimize_varyings(const struct gl_constants *consts,
1511                                struct gl_shader_program *prog, bool spirv)
1512 {
1513    nir_shader *shaders[MESA_SHADER_STAGES];
1514    unsigned num_shaders = 0;
1515    unsigned max_ubos = UINT_MAX;
1516    unsigned max_uniform_comps = UINT_MAX;
1517    bool optimize_io = !debug_get_bool_option("MESA_GLSL_DISABLE_IO_OPT", false);
1518 
1519    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1520       struct gl_linked_shader *shader = prog->_LinkedShaders[i];
1521 
1522       if (!shader)
1523          continue;
1524 
1525       nir_shader *nir = shader->Program->nir;
1526 
1527       if (nir->info.stage == MESA_SHADER_COMPUTE)
1528          return;
1529 
1530       shaders[num_shaders] = nir;
1531       max_uniform_comps = MIN2(max_uniform_comps,
1532                                consts->Program[i].MaxUniformComponents);
1533       max_ubos = MIN2(max_ubos, consts->Program[i].MaxUniformBlocks);
1534       num_shaders++;
1535       optimize_io &= !(nir->options->io_options & nir_io_dont_optimize);
1536    }
1537 
1538    /* Lower IO derefs to load and store intrinsics. */
1539    for (unsigned i = 0; i < num_shaders; i++)
1540       nir_lower_io_passes(shaders[i], true);
1541 
1542    if (!optimize_io)
1543       return;
1544 
1545    /* There is nothing to optimize for only 1 shader. */
1546    if (num_shaders == 1) {
1547       nir_shader *nir = shaders[0];
1548 
1549       /* Even with a separate shader, it's still worth to re-vectorize IO from
1550        * scratch because the original shader might not be vectorized optimally.
1551        */
1552       NIR_PASS(_, nir, nir_lower_io_to_scalar, get_varying_nir_var_mask(nir),
1553                NULL, NULL);
1554       NIR_PASS(_, nir, nir_opt_vectorize_io, get_varying_nir_var_mask(nir));
1555       return;
1556    }
1557 
1558    for (unsigned i = 0; i < num_shaders; i++) {
1559       nir_shader *nir = shaders[i];
1560 
1561       /* nir_opt_varyings requires scalar IO. Scalarize all varyings (not just
1562        * the ones we optimize) because we want to re-vectorize everything to
1563        * get better vectorization and other goodies from nir_opt_vectorize_io.
1564        */
1565       NIR_PASS(_, nir, nir_lower_io_to_scalar, get_varying_nir_var_mask(nir),
1566                NULL, NULL);
1567 
1568       /* nir_opt_varyings requires shaders to be optimized. */
1569       gl_nir_opts(nir);
1570    }
1571 
1572    /* Optimize varyings from the first shader to the last shader first, and
1573     * then in the opposite order from the last changed producer.
1574     *
1575     * For example, VS->GS->FS is optimized in this order first:
1576     *    (VS,GS), (GS,FS)
1577     *
1578     * That ensures that constants and undefs (dead inputs) are propagated
1579     * forward.
1580     *
1581     * If GS was changed while optimizing (GS,FS), (VS,GS) is optimized again
1582     * because removing outputs in GS can cause a chain reaction in making
1583     * GS inputs, VS outputs, and VS inputs dead.
1584     */
1585    unsigned highest_changed_producer = 0;
1586    for (unsigned i = 0; i < num_shaders - 1; i++) {
1587       if (optimize_varyings(shaders[i], shaders[i + 1], spirv,
1588                             max_uniform_comps, max_ubos) & nir_progress_producer)
1589          highest_changed_producer = i;
1590    }
1591 
1592    /* Optimize varyings from the highest changed producer to the first
1593     * shader.
1594     */
1595    for (unsigned i = highest_changed_producer; i > 0; i--) {
1596       optimize_varyings(shaders[i - 1], shaders[i], spirv, max_uniform_comps,
1597                         max_ubos);
1598    }
1599 
1600    /* Final cleanups. */
1601    for (unsigned i = 0; i < num_shaders; i++) {
1602       nir_shader *nir = shaders[i];
1603 
1604       /* Re-vectorize IO. */
1605       NIR_PASS(_, nir, nir_opt_vectorize_io, get_varying_nir_var_mask(nir));
1606 
1607       /* Recompute intrinsic bases, which are totally random after
1608        * optimizations and compaction. Do that for all inputs and outputs,
1609        * including VS inputs because those could have been removed too.
1610        */
1611       NIR_PASS_V(nir, nir_recompute_io_bases,
1612                  nir_var_shader_in | nir_var_shader_out);
1613 
1614       /* Regenerate transform feedback info because compaction in
1615        * nir_opt_varyings always moves them to other slots.
1616        */
1617       if (nir->xfb_info)
1618          nir_gather_xfb_info_from_intrinsics(nir);
1619    }
1620 }
1621 
1622 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)1623 gl_nir_link_spirv(const struct gl_constants *consts,
1624                   const struct gl_extensions *exts,
1625                   struct gl_shader_program *prog,
1626                   const struct gl_nir_linker_options *options)
1627 {
1628    struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
1629    unsigned num_shaders = 0;
1630 
1631    MESA_TRACE_FUNC();
1632 
1633    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1634       if (prog->_LinkedShaders[i]) {
1635          linked_shader[num_shaders++] = prog->_LinkedShaders[i];
1636 
1637          remove_dead_varyings_pre_linking(prog->_LinkedShaders[i]->Program->nir);
1638       }
1639    }
1640 
1641    if (!prelink_lowering(consts, exts, prog, linked_shader, num_shaders))
1642       return false;
1643 
1644    gl_nir_link_assign_xfb_resources(consts, prog);
1645    gl_nir_lower_optimize_varyings(consts, prog, true);
1646 
1647    if (!linked_shader[0]->Program->nir->info.io_lowered) {
1648       /* Linking the stages in the opposite order (from fragment to vertex)
1649        * ensures that inter-shader outputs written to in an earlier stage
1650        * are eliminated if they are (transitively) not used in a later
1651        * stage.
1652        */
1653       for (int i = num_shaders - 2; i >= 0; i--) {
1654          gl_nir_link_opts(linked_shader[i]->Program->nir,
1655                           linked_shader[i + 1]->Program->nir);
1656       }
1657    }
1658 
1659    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1660       struct gl_linked_shader *shader = prog->_LinkedShaders[i];
1661       if (shader) {
1662          const nir_remove_dead_variables_options opts = {
1663             .can_remove_var = can_remove_var,
1664          };
1665          nir_remove_dead_variables(shader->Program->nir,
1666                                    nir_var_uniform | nir_var_image,
1667                                    &opts);
1668       }
1669    }
1670 
1671    if (!gl_nir_link_uniform_blocks(consts, prog))
1672       return false;
1673 
1674    if (!gl_nir_link_uniforms(consts, prog, options->fill_parameters))
1675       return false;
1676 
1677    gl_nir_link_assign_atomic_counter_resources(consts, prog);
1678 
1679    return true;
1680 }
1681 
1682 bool
gl_nir_validate_intrastage_arrays(struct gl_shader_program * prog,nir_variable * var,nir_variable * existing,nir_shader * existing_shader,bool match_precision)1683 gl_nir_validate_intrastage_arrays(struct gl_shader_program *prog,
1684                                   nir_variable *var, nir_variable *existing,
1685                                   nir_shader *existing_shader,
1686                                   bool match_precision)
1687 {
1688    /* Consider the types to be "the same" if both types are arrays
1689     * of the same type and one of the arrays is implicitly sized.
1690     * In addition, set the type of the linked variable to the
1691     * explicitly sized array.
1692     */
1693    if (glsl_type_is_array(var->type) && glsl_type_is_array(existing->type)) {
1694       const glsl_type *no_array_var = glsl_get_array_element(var->type);
1695       const glsl_type *no_array_existing =
1696          glsl_get_array_element(existing->type);
1697       bool type_matches;
1698 
1699       type_matches = (match_precision ?
1700                       no_array_var == no_array_existing :
1701                       glsl_type_compare_no_precision(no_array_var, no_array_existing));
1702 
1703       if (type_matches &&
1704           ((glsl_array_size(var->type) == 0) ||
1705            (glsl_array_size(existing->type) == 0))) {
1706          if (glsl_array_size(var->type) != 0) {
1707             if ((int)glsl_array_size(var->type) <=
1708                 existing->data.max_array_access) {
1709                linker_error(prog, "%s `%s' declared as type "
1710                            "`%s' but outermost dimension has an index"
1711                            " of `%i'\n",
1712                            gl_nir_mode_string(var),
1713                            var->name, glsl_get_type_name(var->type),
1714                            existing->data.max_array_access);
1715             }
1716             existing->type = var->type;
1717             nir_fixup_deref_types(existing_shader);
1718             return true;
1719          } else if (glsl_array_size(existing->type) != 0) {
1720             if((int)glsl_array_size(existing->type) <= var->data.max_array_access &&
1721                !existing->data.from_ssbo_unsized_array) {
1722                linker_error(prog, "%s `%s' declared as type "
1723                            "`%s' but outermost dimension has an index"
1724                            " of `%i'\n",
1725                            gl_nir_mode_string(var),
1726                            var->name, glsl_get_type_name(existing->type),
1727                            var->data.max_array_access);
1728             }
1729             return true;
1730          }
1731       }
1732    }
1733    return false;
1734 }
1735 
1736 static bool
nir_constant_compare(const nir_constant * c1,const nir_constant * c2)1737 nir_constant_compare(const nir_constant *c1, const nir_constant *c2)
1738 {
1739    bool match = true;
1740 
1741    match &= memcmp(c1->values, c2->values, sizeof(c1->values)) == 0;
1742    match &= c1->is_null_constant == c2->is_null_constant;
1743    match &= c1->num_elements == c2->num_elements;
1744    if (!match)
1745       return false;
1746 
1747    for (unsigned i = 0; i < c1->num_elements; i++) {
1748       match &= nir_constant_compare(c1->elements[i], c2->elements[i]);
1749    }
1750 
1751    return match;
1752 }
1753 
1754 struct ifc_var {
1755    nir_shader *shader;
1756    nir_variable *var;
1757 };
1758 
1759 /**
1760  * Perform validation of global variables used across multiple shaders
1761  */
1762 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)1763 cross_validate_globals(void *mem_ctx, const struct gl_constants *consts,
1764                        struct gl_shader_program *prog,
1765                        nir_shader *shader, struct hash_table *variables,
1766                        bool uniforms_only)
1767 {
1768    nir_foreach_variable_in_shader(var, shader) {
1769       if (uniforms_only &&
1770           (var->data.mode != nir_var_uniform &&
1771            var->data.mode != nir_var_mem_ubo &&
1772            var->data.mode != nir_var_image &&
1773            var->data.mode != nir_var_mem_ssbo))
1774          continue;
1775 
1776       /* don't cross validate subroutine uniforms */
1777       if (glsl_contains_subroutine(var->type))
1778          continue;
1779 
1780       /* Don't cross validate interface instances. These are only relevant
1781        * inside a shader. The cross validation is done at the Interface Block
1782        * name level.
1783        */
1784       if (glsl_without_array(var->type) == var->interface_type)
1785          continue;
1786 
1787       /* Don't cross validate compiler temporaries that are at global scope.
1788        *  These will eventually get pulled into the shaders 'main'.
1789        */
1790       if (var->data.mode == nir_var_shader_temp &&
1791           var->data.how_declared == nir_var_hidden)
1792          continue;
1793 
1794       /* If a global with this name has already been seen, verify that the
1795        * new instance has the same type.  In addition, if the globals have
1796        * initializers, the values of the initializers must be the same.
1797        */
1798       struct hash_entry *entry =
1799          _mesa_hash_table_search(variables, var->name);
1800       if (entry != NULL) {
1801          struct ifc_var *existing_ifc = (struct ifc_var *) entry->data;
1802          nir_variable *existing = existing_ifc->var;
1803 
1804          /* Check if types match. */
1805          if (var->type != existing->type) {
1806             if (!gl_nir_validate_intrastage_arrays(prog, var, existing,
1807                                                    existing_ifc->shader, true)) {
1808                /* If it is an unsized array in a Shader Storage Block,
1809                 * two different shaders can access to different elements.
1810                 * Because of that, they might be converted to different
1811                 * sized arrays, then check that they are compatible but
1812                 * ignore the array size.
1813                 */
1814                if (!(var->data.mode == nir_var_mem_ssbo &&
1815                      var->data.from_ssbo_unsized_array &&
1816                      existing->data.mode == nir_var_mem_ssbo &&
1817                      existing->data.from_ssbo_unsized_array &&
1818                      glsl_get_gl_type(var->type) == glsl_get_gl_type(existing->type))) {
1819                   linker_error(prog, "%s `%s' declared as type "
1820                                  "`%s' and type `%s'\n",
1821                                  gl_nir_mode_string(var),
1822                                  var->name, glsl_get_type_name(var->type),
1823                                  glsl_get_type_name(existing->type));
1824                   return;
1825                }
1826             }
1827          }
1828 
1829          if (var->data.explicit_location) {
1830             if (existing->data.explicit_location
1831                 && (var->data.location != existing->data.location)) {
1832                linker_error(prog, "explicit locations for %s "
1833                             "`%s' have differing values\n",
1834                             gl_nir_mode_string(var), var->name);
1835                return;
1836             }
1837 
1838             if (var->data.location_frac != existing->data.location_frac) {
1839                linker_error(prog, "explicit components for %s `%s' have "
1840                             "differing values\n", gl_nir_mode_string(var),
1841                             var->name);
1842                return;
1843             }
1844 
1845             existing->data.location = var->data.location;
1846             existing->data.explicit_location = true;
1847          } else {
1848             /* Check if uniform with implicit location was marked explicit
1849              * by earlier shader stage. If so, mark it explicit in this stage
1850              * too to make sure later processing does not treat it as
1851              * implicit one.
1852              */
1853             if (existing->data.explicit_location) {
1854                var->data.location = existing->data.location;
1855                var->data.explicit_location = true;
1856             }
1857          }
1858 
1859          /* From the GLSL 4.20 specification:
1860           * "A link error will result if two compilation units in a program
1861           *  specify different integer-constant bindings for the same
1862           *  opaque-uniform name.  However, it is not an error to specify a
1863           *  binding on some but not all declarations for the same name"
1864           */
1865          if (var->data.explicit_binding) {
1866             if (existing->data.explicit_binding &&
1867                 var->data.binding != existing->data.binding) {
1868                linker_error(prog, "explicit bindings for %s "
1869                             "`%s' have differing values\n",
1870                             gl_nir_mode_string(var), var->name);
1871                return;
1872             }
1873 
1874             existing->data.binding = var->data.binding;
1875             existing->data.explicit_binding = true;
1876          }
1877 
1878          if (glsl_contains_atomic(var->type) &&
1879              var->data.offset != existing->data.offset) {
1880             linker_error(prog, "offset specifications for %s "
1881                          "`%s' have differing values\n",
1882                          gl_nir_mode_string(var), var->name);
1883             return;
1884          }
1885 
1886          /* Validate layout qualifiers for gl_FragDepth.
1887           *
1888           * From the AMD/ARB_conservative_depth specs:
1889           *
1890           *    "If gl_FragDepth is redeclared in any fragment shader in a
1891           *    program, it must be redeclared in all fragment shaders in
1892           *    that program that have static assignments to
1893           *    gl_FragDepth. All redeclarations of gl_FragDepth in all
1894           *    fragment shaders in a single program must have the same set
1895           *    of qualifiers."
1896           */
1897          if (strcmp(var->name, "gl_FragDepth") == 0) {
1898             bool layout_declared = var->data.depth_layout != nir_depth_layout_none;
1899             bool layout_differs =
1900                var->data.depth_layout != existing->data.depth_layout;
1901 
1902             if (layout_declared && layout_differs) {
1903                linker_error(prog,
1904                             "All redeclarations of gl_FragDepth in all "
1905                             "fragment shaders in a single program must have "
1906                             "the same set of qualifiers.\n");
1907             }
1908 
1909             if (var->data.used && layout_differs) {
1910                linker_error(prog,
1911                             "If gl_FragDepth is redeclared with a layout "
1912                             "qualifier in any fragment shader, it must be "
1913                             "redeclared with the same layout qualifier in "
1914                             "all fragment shaders that have assignments to "
1915                             "gl_FragDepth\n");
1916             }
1917          }
1918 
1919          /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
1920           *
1921           *     "If a shared global has multiple initializers, the
1922           *     initializers must all be constant expressions, and they
1923           *     must all have the same value. Otherwise, a link error will
1924           *     result. (A shared global having only one initializer does
1925           *     not require that initializer to be a constant expression.)"
1926           *
1927           * Previous to 4.20 the GLSL spec simply said that initializers
1928           * must have the same value.  In this case of non-constant
1929           * initializers, this was impossible to determine.  As a result,
1930           * no vendor actually implemented that behavior.  The 4.20
1931           * behavior matches the implemented behavior of at least one other
1932           * vendor, so we'll implement that for all GLSL versions.
1933           * If (at least) one of these constant expressions is implicit,
1934           * because it was added by glsl_zero_init, we skip the verification.
1935           */
1936          if (var->constant_initializer != NULL) {
1937             if (existing->constant_initializer != NULL &&
1938                 !existing->data.is_implicit_initializer &&
1939                 !var->data.is_implicit_initializer) {
1940                if (!nir_constant_compare(var->constant_initializer,
1941                                          existing->constant_initializer)) {
1942                   linker_error(prog, "initializers for %s "
1943                                "`%s' have differing values\n",
1944                                gl_nir_mode_string(var), var->name);
1945                   return;
1946                }
1947             } else {
1948                /* If the first-seen instance of a particular uniform did
1949                 * not have an initializer but a later instance does,
1950                 * replace the former with the later.
1951                 */
1952                if (!var->data.is_implicit_initializer)
1953                   _mesa_hash_table_insert(variables, existing->name, var);
1954             }
1955          }
1956 
1957          if (var->data.has_initializer) {
1958             if (existing->data.has_initializer
1959                 && (var->constant_initializer == NULL
1960                     || existing->constant_initializer == NULL)) {
1961                linker_error(prog,
1962                             "shared global variable `%s' has multiple "
1963                             "non-constant initializers.\n",
1964                             var->name);
1965                return;
1966             }
1967          }
1968 
1969          if (existing->data.explicit_invariant != var->data.explicit_invariant) {
1970             linker_error(prog, "declarations for %s `%s' have "
1971                          "mismatching invariant qualifiers\n",
1972                          gl_nir_mode_string(var), var->name);
1973             return;
1974          }
1975          if (existing->data.centroid != var->data.centroid) {
1976             linker_error(prog, "declarations for %s `%s' have "
1977                          "mismatching centroid qualifiers\n",
1978                          gl_nir_mode_string(var), var->name);
1979             return;
1980          }
1981          if (existing->data.sample != var->data.sample) {
1982             linker_error(prog, "declarations for %s `%s` have "
1983                          "mismatching sample qualifiers\n",
1984                          gl_nir_mode_string(var), var->name);
1985             return;
1986          }
1987          if (existing->data.image.format != var->data.image.format) {
1988             linker_error(prog, "declarations for %s `%s` have "
1989                          "mismatching image format qualifiers\n",
1990                          gl_nir_mode_string(var), var->name);
1991             return;
1992          }
1993 
1994          /* Check the precision qualifier matches for uniform variables on
1995           * GLSL ES.
1996           */
1997          if (!consts->AllowGLSLRelaxedES &&
1998              prog->IsES && !var->interface_type &&
1999              existing->data.precision != var->data.precision) {
2000             if ((existing->data.used && var->data.used) ||
2001                 prog->GLSL_Version >= 300) {
2002                linker_error(prog, "declarations for %s `%s` have "
2003                             "mismatching precision qualifiers\n",
2004                             gl_nir_mode_string(var), var->name);
2005                return;
2006             } else {
2007                linker_warning(prog, "declarations for %s `%s` have "
2008                               "mismatching precision qualifiers\n",
2009                               gl_nir_mode_string(var), var->name);
2010             }
2011          }
2012 
2013          /* In OpenGL GLSL 3.20 spec, section 4.3.9:
2014           *
2015           *   "It is a link-time error if any particular shader interface
2016           *    contains:
2017           *
2018           *    - two different blocks, each having no instance name, and each
2019           *      having a member of the same name, or
2020           *
2021           *    - a variable outside a block, and a block with no instance name,
2022           *      where the variable has the same name as a member in the block."
2023           */
2024          const glsl_type *var_itype = var->interface_type;
2025          const glsl_type *existing_itype = existing->interface_type;
2026          if (var_itype != existing_itype) {
2027             if (!var_itype || !existing_itype) {
2028                linker_error(prog, "declarations for %s `%s` are inside block "
2029                             "`%s` and outside a block",
2030                             gl_nir_mode_string(var), var->name,
2031                             glsl_get_type_name(var_itype ? var_itype : existing_itype));
2032                return;
2033             } else if (strcmp(glsl_get_type_name(var_itype), glsl_get_type_name(existing_itype)) != 0) {
2034                linker_error(prog, "declarations for %s `%s` are inside blocks "
2035                             "`%s` and `%s`",
2036                             gl_nir_mode_string(var), var->name,
2037                             glsl_get_type_name(existing_itype),
2038                             glsl_get_type_name(var_itype));
2039                return;
2040             }
2041          }
2042       } else {
2043          struct ifc_var *ifc_var = ralloc(mem_ctx, struct ifc_var);
2044          ifc_var->var = var;
2045          ifc_var->shader = shader;
2046          _mesa_hash_table_insert(variables, var->name, ifc_var);
2047       }
2048    }
2049 }
2050 
2051 /**
2052  * Perform validation of uniforms used across multiple shader stages
2053  */
2054 static void
cross_validate_uniforms(const struct gl_constants * consts,struct gl_shader_program * prog)2055 cross_validate_uniforms(const struct gl_constants *consts,
2056                         struct gl_shader_program *prog)
2057 {
2058    void *mem_ctx = ralloc_context(NULL);
2059    struct hash_table *variables =
2060       _mesa_hash_table_create(mem_ctx, _mesa_hash_string, _mesa_key_string_equal);
2061    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2062       if (prog->_LinkedShaders[i] == NULL)
2063          continue;
2064 
2065       cross_validate_globals(mem_ctx, consts, prog,
2066                              prog->_LinkedShaders[i]->Program->nir,
2067                              variables, true);
2068    }
2069 
2070    ralloc_free(mem_ctx);
2071 }
2072 
2073 static bool
parameter_lists_match_exact(nir_parameter * params_a,nir_parameter * params_b,unsigned num_params_a,unsigned num_params_b)2074 parameter_lists_match_exact(nir_parameter *params_a, nir_parameter *params_b,
2075                             unsigned num_params_a, unsigned num_params_b)
2076 {
2077    if (num_params_a != num_params_b)
2078       return false;
2079 
2080    for (unsigned i = 0; i < num_params_a; i++) {
2081       if (params_a[i].type != params_b[i].type)
2082          return false;
2083    }
2084 
2085    return true;
2086 }
2087 
2088 static bool
exact_matching_signature(nir_function * other,nir_function * func)2089 exact_matching_signature(nir_function *other, nir_function *func)
2090 {
2091    return parameter_lists_match_exact(other->params, func->params,
2092                                       other->num_params, func->num_params);
2093 }
2094 
2095 static bool
validate_xfb_buffer_stride(const struct gl_constants * consts,unsigned idx,struct gl_shader_program * prog)2096 validate_xfb_buffer_stride(const struct gl_constants *consts, unsigned idx,
2097                            struct gl_shader_program *prog)
2098 {
2099    /* We will validate doubles at a later stage */
2100    if (prog->TransformFeedback.BufferStride[idx] % 4) {
2101       linker_error(prog, "invalid qualifier xfb_stride=%d must be a "
2102                    "multiple of 4 or if its applied to a type that is "
2103                    "or contains a double a multiple of 8.",
2104                    prog->TransformFeedback.BufferStride[idx]);
2105       return false;
2106    }
2107 
2108    if (prog->TransformFeedback.BufferStride[idx] / 4 >
2109        consts->MaxTransformFeedbackInterleavedComponents) {
2110       linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS "
2111                    "limit has been exceeded.");
2112       return false;
2113    }
2114 
2115    return true;
2116 }
2117 
2118 /**
2119  * Check for conflicting xfb_stride default qualifiers and store buffer stride
2120  * for later use.
2121  */
2122 static void
link_xfb_stride_layout_qualifiers(const struct gl_constants * consts,struct gl_shader_program * prog,struct gl_shader ** shader_list,unsigned num_shaders)2123 link_xfb_stride_layout_qualifiers(const struct gl_constants *consts,
2124                                   struct gl_shader_program *prog,
2125                                   struct gl_shader **shader_list,
2126                                   unsigned num_shaders)
2127 {
2128    for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
2129       prog->TransformFeedback.BufferStride[i] = 0;
2130    }
2131 
2132    for (unsigned i = 0; i < num_shaders; i++) {
2133       struct gl_shader *shader = shader_list[i];
2134 
2135       for (unsigned j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
2136          if (shader->TransformFeedbackBufferStride[j]) {
2137             if (prog->TransformFeedback.BufferStride[j] == 0) {
2138                prog->TransformFeedback.BufferStride[j] =
2139                   shader->TransformFeedbackBufferStride[j];
2140                if (!validate_xfb_buffer_stride(consts, j, prog))
2141                   return;
2142             } else if (prog->TransformFeedback.BufferStride[j] !=
2143                        shader->TransformFeedbackBufferStride[j]){
2144                linker_error(prog,
2145                             "intrastage shaders defined with conflicting "
2146                             "xfb_stride for buffer %d (%d and %d)\n", j,
2147                             prog->TransformFeedback.BufferStride[j],
2148                             shader->TransformFeedbackBufferStride[j]);
2149                return;
2150             }
2151          }
2152       }
2153    }
2154 }
2155 
2156 /**
2157  * Check for conflicting bindless/bound sampler/image layout qualifiers at
2158  * global scope.
2159  */
2160 static void
link_bindless_layout_qualifiers(struct gl_shader_program * prog,struct gl_shader ** shader_list,unsigned num_shaders)2161 link_bindless_layout_qualifiers(struct gl_shader_program *prog,
2162                                 struct gl_shader **shader_list,
2163                                 unsigned num_shaders)
2164 {
2165    bool bindless_sampler, bindless_image;
2166    bool bound_sampler, bound_image;
2167 
2168    bindless_sampler = bindless_image = false;
2169    bound_sampler = bound_image = false;
2170 
2171    for (unsigned i = 0; i < num_shaders; i++) {
2172       struct gl_shader *shader = shader_list[i];
2173 
2174       if (shader->bindless_sampler)
2175          bindless_sampler = true;
2176       if (shader->bindless_image)
2177          bindless_image = true;
2178       if (shader->bound_sampler)
2179          bound_sampler = true;
2180       if (shader->bound_image)
2181          bound_image = true;
2182 
2183       if ((bindless_sampler && bound_sampler) ||
2184           (bindless_image && bound_image)) {
2185          /* From section 4.4.6 of the ARB_bindless_texture spec:
2186           *
2187           *     "If both bindless_sampler and bound_sampler, or bindless_image
2188           *      and bound_image, are declared at global scope in any
2189           *      compilation unit, a link- time error will be generated."
2190           */
2191          linker_error(prog, "both bindless_sampler and bound_sampler, or "
2192                       "bindless_image and bound_image, can't be declared at "
2193                       "global scope");
2194       }
2195    }
2196 }
2197 
2198 /**
2199  * Check for conflicting viewport_relative settings across shaders, and sets
2200  * the value for the linked shader.
2201  */
2202 static void
link_layer_viewport_relative_qualifier(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)2203 link_layer_viewport_relative_qualifier(struct gl_shader_program *prog,
2204                                        struct gl_program *gl_prog,
2205                                        struct gl_shader **shader_list,
2206                                        unsigned num_shaders)
2207 {
2208    unsigned i;
2209 
2210    /* Find first shader with explicit layer declaration */
2211    for (i = 0; i < num_shaders; i++) {
2212       if (shader_list[i]->redeclares_gl_layer) {
2213          gl_prog->nir->info.layer_viewport_relative =
2214             shader_list[i]->layer_viewport_relative;
2215          break;
2216       }
2217    }
2218 
2219    /* Now make sure that each subsequent shader's explicit layer declaration
2220     * matches the first one's.
2221     */
2222    for (; i < num_shaders; i++) {
2223       if (shader_list[i]->redeclares_gl_layer &&
2224           shader_list[i]->layer_viewport_relative !=
2225           gl_prog->nir->info.layer_viewport_relative) {
2226          linker_error(prog, "all gl_Layer redeclarations must have identical "
2227                       "viewport_relative settings");
2228       }
2229    }
2230 }
2231 
2232 /**
2233  * Performs the cross-validation of tessellation control shader vertices and
2234  * layout qualifiers for the attached tessellation control shaders,
2235  * and propagates them to the linked TCS and linked shader program.
2236  */
2237 static void
link_tcs_out_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)2238 link_tcs_out_layout_qualifiers(struct gl_shader_program *prog,
2239                                struct gl_program *gl_prog,
2240                                struct gl_shader **shader_list,
2241                                unsigned num_shaders)
2242 {
2243    if (gl_prog->info.stage != MESA_SHADER_TESS_CTRL)
2244       return;
2245 
2246    gl_prog->nir->info.tess.tcs_vertices_out = 0;
2247 
2248    /* From the GLSL 4.0 spec (chapter 4.3.8.2):
2249     *
2250     *     "All tessellation control shader layout declarations in a program
2251     *      must specify the same output patch vertex count.  There must be at
2252     *      least one layout qualifier specifying an output patch vertex count
2253     *      in any program containing tessellation control shaders; however,
2254     *      such a declaration is not required in all tessellation control
2255     *      shaders."
2256     */
2257 
2258    for (unsigned i = 0; i < num_shaders; i++) {
2259       struct gl_shader *shader = shader_list[i];
2260 
2261       if (shader->info.TessCtrl.VerticesOut != 0) {
2262          if (gl_prog->nir->info.tess.tcs_vertices_out != 0 &&
2263              gl_prog->nir->info.tess.tcs_vertices_out !=
2264              (unsigned) shader->info.TessCtrl.VerticesOut) {
2265             linker_error(prog, "tessellation control shader defined with "
2266                          "conflicting output vertex count (%d and %d)\n",
2267                          gl_prog->nir->info.tess.tcs_vertices_out,
2268                          shader->info.TessCtrl.VerticesOut);
2269             return;
2270          }
2271          gl_prog->nir->info.tess.tcs_vertices_out =
2272             shader->info.TessCtrl.VerticesOut;
2273       }
2274    }
2275 
2276    /* Just do the intrastage -> interstage propagation right now,
2277     * since we already know we're in the right type of shader program
2278     * for doing it.
2279     */
2280    if (gl_prog->nir->info.tess.tcs_vertices_out == 0) {
2281       linker_error(prog, "tessellation control shader didn't declare "
2282                    "vertices out layout qualifier\n");
2283       return;
2284    }
2285 }
2286 
2287 
2288 /**
2289  * Performs the cross-validation of tessellation evaluation shader
2290  * primitive type, vertex spacing, ordering and point_mode layout qualifiers
2291  * for the attached tessellation evaluation shaders, and propagates them
2292  * to the linked TES and linked shader program.
2293  */
2294 static void
link_tes_in_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)2295 link_tes_in_layout_qualifiers(struct gl_shader_program *prog,
2296                               struct gl_program *gl_prog,
2297                               struct gl_shader **shader_list,
2298                               unsigned num_shaders)
2299 {
2300    if (gl_prog->info.stage != MESA_SHADER_TESS_EVAL)
2301       return;
2302 
2303    int point_mode = -1;
2304    unsigned vertex_order = 0;
2305 
2306    gl_prog->nir->info.tess._primitive_mode = TESS_PRIMITIVE_UNSPECIFIED;
2307    gl_prog->nir->info.tess.spacing = TESS_SPACING_UNSPECIFIED;
2308 
2309    /* From the GLSL 4.0 spec (chapter 4.3.8.1):
2310     *
2311     *     "At least one tessellation evaluation shader (compilation unit) in
2312     *      a program must declare a primitive mode in its input layout.
2313     *      Declaration vertex spacing, ordering, and point mode identifiers is
2314     *      optional.  It is not required that all tessellation evaluation
2315     *      shaders in a program declare a primitive mode.  If spacing or
2316     *      vertex ordering declarations are omitted, the tessellation
2317     *      primitive generator will use equal spacing or counter-clockwise
2318     *      vertex ordering, respectively.  If a point mode declaration is
2319     *      omitted, the tessellation primitive generator will produce lines or
2320     *      triangles according to the primitive mode."
2321     */
2322 
2323    for (unsigned i = 0; i < num_shaders; i++) {
2324       struct gl_shader *shader = shader_list[i];
2325 
2326       if (shader->info.TessEval._PrimitiveMode != TESS_PRIMITIVE_UNSPECIFIED) {
2327          if (gl_prog->nir->info.tess._primitive_mode != TESS_PRIMITIVE_UNSPECIFIED &&
2328              gl_prog->nir->info.tess._primitive_mode !=
2329              shader->info.TessEval._PrimitiveMode) {
2330             linker_error(prog, "tessellation evaluation shader defined with "
2331                          "conflicting input primitive modes.\n");
2332             return;
2333          }
2334          gl_prog->nir->info.tess._primitive_mode =
2335             shader->info.TessEval._PrimitiveMode;
2336       }
2337 
2338       if (shader->info.TessEval.Spacing != 0) {
2339          if (gl_prog->nir->info.tess.spacing != 0 &&
2340              gl_prog->nir->info.tess.spacing != shader->info.TessEval.Spacing) {
2341             linker_error(prog, "tessellation evaluation shader defined with "
2342                          "conflicting vertex spacing.\n");
2343             return;
2344          }
2345          gl_prog->nir->info.tess.spacing = shader->info.TessEval.Spacing;
2346       }
2347 
2348       if (shader->info.TessEval.VertexOrder != 0) {
2349          if (vertex_order != 0 &&
2350              vertex_order != shader->info.TessEval.VertexOrder) {
2351             linker_error(prog, "tessellation evaluation shader defined with "
2352                          "conflicting ordering.\n");
2353             return;
2354          }
2355          vertex_order = shader->info.TessEval.VertexOrder;
2356       }
2357 
2358       if (shader->info.TessEval.PointMode != -1) {
2359          if (point_mode != -1 &&
2360              point_mode != shader->info.TessEval.PointMode) {
2361             linker_error(prog, "tessellation evaluation shader defined with "
2362                          "conflicting point modes.\n");
2363             return;
2364          }
2365          point_mode = shader->info.TessEval.PointMode;
2366       }
2367 
2368    }
2369 
2370    /* Just do the intrastage -> interstage propagation right now,
2371     * since we already know we're in the right type of shader program
2372     * for doing it.
2373     */
2374    if (gl_prog->nir->info.tess._primitive_mode == TESS_PRIMITIVE_UNSPECIFIED) {
2375       linker_error(prog,
2376                    "tessellation evaluation shader didn't declare input "
2377                    "primitive modes.\n");
2378       return;
2379    }
2380 
2381    if (gl_prog->nir->info.tess.spacing == TESS_SPACING_UNSPECIFIED)
2382       gl_prog->nir->info.tess.spacing = TESS_SPACING_EQUAL;
2383 
2384    if (vertex_order == 0 || vertex_order == GL_CCW)
2385       gl_prog->nir->info.tess.ccw = true;
2386    else
2387       gl_prog->nir->info.tess.ccw = false;
2388 
2389 
2390    if (point_mode == -1 || point_mode == GL_FALSE)
2391       gl_prog->nir->info.tess.point_mode = false;
2392    else
2393       gl_prog->nir->info.tess.point_mode = true;
2394 }
2395 
2396 
2397 /**
2398  * Performs the cross-validation of layout qualifiers specified in
2399  * redeclaration of gl_FragCoord for the attached fragment shaders,
2400  * and propagates them to the linked FS and linked shader program.
2401  */
2402 static void
link_fs_inout_layout_qualifiers(struct gl_shader_program * prog,struct gl_linked_shader * linked_shader,struct gl_shader ** shader_list,unsigned num_shaders,bool arb_fragment_coord_conventions_enable)2403 link_fs_inout_layout_qualifiers(struct gl_shader_program *prog,
2404                                 struct gl_linked_shader *linked_shader,
2405                                 struct gl_shader **shader_list,
2406                                 unsigned num_shaders,
2407                                 bool arb_fragment_coord_conventions_enable)
2408 {
2409    bool redeclares_gl_fragcoord = false;
2410    bool uses_gl_fragcoord = false;
2411    bool origin_upper_left = false;
2412    bool pixel_center_integer = false;
2413 
2414    if (linked_shader->Stage != MESA_SHADER_FRAGMENT ||
2415        (prog->GLSL_Version < 150 && !arb_fragment_coord_conventions_enable))
2416       return;
2417 
2418    for (unsigned i = 0; i < num_shaders; i++) {
2419       struct gl_shader *shader = shader_list[i];
2420       /* From the GLSL 1.50 spec, page 39:
2421        *
2422        *   "If gl_FragCoord is redeclared in any fragment shader in a program,
2423        *    it must be redeclared in all the fragment shaders in that program
2424        *    that have a static use gl_FragCoord."
2425        */
2426       if ((redeclares_gl_fragcoord && !shader->redeclares_gl_fragcoord &&
2427            shader->uses_gl_fragcoord)
2428           || (shader->redeclares_gl_fragcoord && !redeclares_gl_fragcoord &&
2429               uses_gl_fragcoord)) {
2430              linker_error(prog, "fragment shader defined with conflicting "
2431                          "layout qualifiers for gl_FragCoord\n");
2432       }
2433 
2434       /* From the GLSL 1.50 spec, page 39:
2435        *
2436        *   "All redeclarations of gl_FragCoord in all fragment shaders in a
2437        *    single program must have the same set of qualifiers."
2438        */
2439       if (redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord &&
2440           (shader->origin_upper_left != origin_upper_left ||
2441            shader->pixel_center_integer != pixel_center_integer)) {
2442          linker_error(prog, "fragment shader defined with conflicting "
2443                       "layout qualifiers for gl_FragCoord\n");
2444       }
2445 
2446       /* Update the linked shader state.  Note that uses_gl_fragcoord should
2447        * accumulate the results.  The other values should replace.  If there
2448        * are multiple redeclarations, all the fields except uses_gl_fragcoord
2449        * are already known to be the same.
2450        */
2451       if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) {
2452          redeclares_gl_fragcoord = shader->redeclares_gl_fragcoord;
2453          uses_gl_fragcoord |= shader->uses_gl_fragcoord;
2454          origin_upper_left = shader->origin_upper_left;
2455          pixel_center_integer = shader->pixel_center_integer;
2456       }
2457 
2458       linked_shader->Program->nir->info.fs.early_fragment_tests |=
2459          shader->EarlyFragmentTests || shader->PostDepthCoverage;
2460       linked_shader->Program->nir->info.fs.inner_coverage |= shader->InnerCoverage;
2461       linked_shader->Program->nir->info.fs.post_depth_coverage |=
2462          shader->PostDepthCoverage;
2463       linked_shader->Program->nir->info.fs.pixel_interlock_ordered |=
2464          shader->PixelInterlockOrdered;
2465       linked_shader->Program->nir->info.fs.pixel_interlock_unordered |=
2466          shader->PixelInterlockUnordered;
2467       linked_shader->Program->nir->info.fs.sample_interlock_ordered |=
2468          shader->SampleInterlockOrdered;
2469       linked_shader->Program->nir->info.fs.sample_interlock_unordered |=
2470          shader->SampleInterlockUnordered;
2471       linked_shader->Program->nir->info.fs.advanced_blend_modes |= shader->BlendSupport;
2472    }
2473 
2474    linked_shader->Program->nir->info.fs.pixel_center_integer = pixel_center_integer;
2475    linked_shader->Program->nir->info.fs.origin_upper_left = origin_upper_left;
2476 }
2477 
2478 /**
2479  * Performs the cross-validation of geometry shader max_vertices and
2480  * primitive type layout qualifiers for the attached geometry shaders,
2481  * and propagates them to the linked GS and linked shader program.
2482  */
2483 static void
link_gs_inout_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)2484 link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
2485                                 struct gl_program *gl_prog,
2486                                 struct gl_shader **shader_list,
2487                                 unsigned num_shaders)
2488 {
2489    /* No in/out qualifiers defined for anything but GLSL 1.50+
2490     * geometry shaders so far.
2491     */
2492    if (gl_prog->info.stage != MESA_SHADER_GEOMETRY || prog->GLSL_Version < 150)
2493       return;
2494 
2495    int vertices_out = -1;
2496 
2497    gl_prog->nir->info.gs.invocations = 0;
2498    gl_prog->nir->info.gs.input_primitive = MESA_PRIM_UNKNOWN;
2499    gl_prog->nir->info.gs.output_primitive = MESA_PRIM_UNKNOWN;
2500 
2501    /* From the GLSL 1.50 spec, page 46:
2502     *
2503     *     "All geometry shader output layout declarations in a program
2504     *      must declare the same layout and same value for
2505     *      max_vertices. There must be at least one geometry output
2506     *      layout declaration somewhere in a program, but not all
2507     *      geometry shaders (compilation units) are required to
2508     *      declare it."
2509     */
2510 
2511    for (unsigned i = 0; i < num_shaders; i++) {
2512       struct gl_shader *shader = shader_list[i];
2513 
2514       if (shader->info.Geom.InputType != MESA_PRIM_UNKNOWN) {
2515          if (gl_prog->nir->info.gs.input_primitive != MESA_PRIM_UNKNOWN &&
2516              gl_prog->nir->info.gs.input_primitive !=
2517              shader->info.Geom.InputType) {
2518             linker_error(prog, "geometry shader defined with conflicting "
2519                          "input types\n");
2520             return;
2521          }
2522          gl_prog->nir->info.gs.input_primitive =
2523             (enum mesa_prim)shader->info.Geom.InputType;
2524       }
2525 
2526       if (shader->info.Geom.OutputType != MESA_PRIM_UNKNOWN) {
2527          if (gl_prog->nir->info.gs.output_primitive != MESA_PRIM_UNKNOWN &&
2528              gl_prog->nir->info.gs.output_primitive !=
2529              shader->info.Geom.OutputType) {
2530             linker_error(prog, "geometry shader defined with conflicting "
2531                          "output types\n");
2532             return;
2533          }
2534          gl_prog->nir->info.gs.output_primitive =
2535             (enum mesa_prim)shader->info.Geom.OutputType;
2536       }
2537 
2538       if (shader->info.Geom.VerticesOut != -1) {
2539          if (vertices_out != -1 &&
2540              vertices_out != shader->info.Geom.VerticesOut) {
2541             linker_error(prog, "geometry shader defined with conflicting "
2542                          "output vertex count (%d and %d)\n",
2543                          vertices_out, shader->info.Geom.VerticesOut);
2544             return;
2545          }
2546          vertices_out = shader->info.Geom.VerticesOut;
2547       }
2548 
2549       if (shader->info.Geom.Invocations != 0) {
2550          if (gl_prog->nir->info.gs.invocations != 0 &&
2551              gl_prog->nir->info.gs.invocations !=
2552              (unsigned) shader->info.Geom.Invocations) {
2553             linker_error(prog, "geometry shader defined with conflicting "
2554                          "invocation count (%d and %d)\n",
2555                          gl_prog->nir->info.gs.invocations,
2556                          shader->info.Geom.Invocations);
2557             return;
2558          }
2559          gl_prog->nir->info.gs.invocations = shader->info.Geom.Invocations;
2560       }
2561    }
2562 
2563    /* Just do the intrastage -> interstage propagation right now,
2564     * since we already know we're in the right type of shader program
2565     * for doing it.
2566     */
2567    if (gl_prog->nir->info.gs.input_primitive == MESA_PRIM_UNKNOWN) {
2568       linker_error(prog,
2569                    "geometry shader didn't declare primitive input type\n");
2570       return;
2571    }
2572 
2573    if (gl_prog->nir->info.gs.output_primitive == MESA_PRIM_UNKNOWN) {
2574       linker_error(prog,
2575                    "geometry shader didn't declare primitive output type\n");
2576       return;
2577    }
2578 
2579    if (vertices_out == -1) {
2580       linker_error(prog,
2581                    "geometry shader didn't declare max_vertices\n");
2582       return;
2583    } else {
2584       gl_prog->nir->info.gs.vertices_out = vertices_out;
2585    }
2586 
2587    if (gl_prog->nir->info.gs.invocations == 0)
2588       gl_prog->nir->info.gs.invocations = 1;
2589 }
2590 
2591 
2592 /**
2593  * Perform cross-validation of compute shader local_size_{x,y,z} layout and
2594  * derivative arrangement qualifiers for the attached compute shaders, and
2595  * propagate them to the linked CS and linked shader program.
2596  */
2597 static void
link_cs_input_layout_qualifiers(struct gl_shader_program * prog,struct gl_program * gl_prog,struct gl_shader ** shader_list,unsigned num_shaders)2598 link_cs_input_layout_qualifiers(struct gl_shader_program *prog,
2599                                 struct gl_program *gl_prog,
2600                                 struct gl_shader **shader_list,
2601                                 unsigned num_shaders)
2602 {
2603    /* This function is called for all shader stages, but it only has an effect
2604     * for compute shaders.
2605     */
2606    if (gl_prog->info.stage != MESA_SHADER_COMPUTE)
2607       return;
2608 
2609    for (int i = 0; i < 3; i++)
2610       gl_prog->nir->info.workgroup_size[i] = 0;
2611 
2612    gl_prog->nir->info.workgroup_size_variable = false;
2613 
2614    gl_prog->nir->info.derivative_group = DERIVATIVE_GROUP_NONE;
2615 
2616    /* From the ARB_compute_shader spec, in the section describing local size
2617     * declarations:
2618     *
2619     *     If multiple compute shaders attached to a single program object
2620     *     declare local work-group size, the declarations must be identical;
2621     *     otherwise a link-time error results. Furthermore, if a program
2622     *     object contains any compute shaders, at least one must contain an
2623     *     input layout qualifier specifying the local work sizes of the
2624     *     program, or a link-time error will occur.
2625     */
2626    for (unsigned sh = 0; sh < num_shaders; sh++) {
2627       struct gl_shader *shader = shader_list[sh];
2628 
2629       if (shader->info.Comp.LocalSize[0] != 0) {
2630          if (gl_prog->nir->info.workgroup_size[0] != 0) {
2631             for (int i = 0; i < 3; i++) {
2632                if (gl_prog->nir->info.workgroup_size[i] !=
2633                    shader->info.Comp.LocalSize[i]) {
2634                   linker_error(prog, "compute shader defined with conflicting "
2635                                "local sizes\n");
2636                   return;
2637                }
2638             }
2639          }
2640          for (int i = 0; i < 3; i++) {
2641             gl_prog->nir->info.workgroup_size[i] =
2642                shader->info.Comp.LocalSize[i];
2643          }
2644       } else if (shader->info.Comp.LocalSizeVariable) {
2645          if (gl_prog->nir->info.workgroup_size[0] != 0) {
2646             /* The ARB_compute_variable_group_size spec says:
2647              *
2648              *     If one compute shader attached to a program declares a
2649              *     variable local group size and a second compute shader
2650              *     attached to the same program declares a fixed local group
2651              *     size, a link-time error results.
2652              */
2653             linker_error(prog, "compute shader defined with both fixed and "
2654                          "variable local group size\n");
2655             return;
2656          }
2657          gl_prog->nir->info.workgroup_size_variable = true;
2658       }
2659 
2660       enum gl_derivative_group group = shader->info.Comp.DerivativeGroup;
2661       if (group != DERIVATIVE_GROUP_NONE) {
2662          if (gl_prog->nir->info.derivative_group != DERIVATIVE_GROUP_NONE &&
2663              gl_prog->nir->info.derivative_group != group) {
2664             linker_error(prog, "compute shader defined with conflicting "
2665                          "derivative groups\n");
2666             return;
2667          }
2668          gl_prog->nir->info.derivative_group = group;
2669       }
2670    }
2671 
2672    /* Just do the intrastage -> interstage propagation right now,
2673     * since we already know we're in the right type of shader program
2674     * for doing it.
2675     */
2676    if (gl_prog->nir->info.workgroup_size[0] == 0 &&
2677        !gl_prog->nir->info.workgroup_size_variable) {
2678       linker_error(prog, "compute shader must contain a fixed or a variable "
2679                          "local group size\n");
2680       return;
2681    }
2682 
2683    if (gl_prog->nir->info.derivative_group == DERIVATIVE_GROUP_QUADS) {
2684       if (gl_prog->nir->info.workgroup_size[0] % 2 != 0) {
2685          linker_error(prog, "derivative_group_quadsNV must be used with a "
2686                       "local group size whose first dimension "
2687                       "is a multiple of 2\n");
2688          return;
2689       }
2690       if (gl_prog->nir->info.workgroup_size[1] % 2 != 0) {
2691          linker_error(prog, "derivative_group_quadsNV must be used with a local"
2692                       "group size whose second dimension "
2693                       "is a multiple of 2\n");
2694          return;
2695       }
2696    } else if (gl_prog->nir->info.derivative_group == DERIVATIVE_GROUP_LINEAR) {
2697       if ((gl_prog->nir->info.workgroup_size[0] *
2698            gl_prog->nir->info.workgroup_size[1] *
2699            gl_prog->nir->info.workgroup_size[2]) % 4 != 0) {
2700          linker_error(prog, "derivative_group_linearNV must be used with a "
2701                       "local group size whose total number of invocations "
2702                       "is a multiple of 4\n");
2703          return;
2704       }
2705    }
2706 }
2707 
2708 
2709 /**
2710  * Combine a group of shaders for a single stage to generate a linked shader
2711  *
2712  * \note
2713  * If this function is supplied a single shader, it is cloned, and the new
2714  * shader is returned.
2715  */
2716 static struct gl_linked_shader *
link_intrastage_shaders(void * mem_ctx,struct gl_context * ctx,struct gl_shader_program * prog,struct gl_shader ** shader_list,unsigned num_shaders)2717 link_intrastage_shaders(void *mem_ctx,
2718                         struct gl_context *ctx,
2719                         struct gl_shader_program *prog,
2720                         struct gl_shader **shader_list,
2721                         unsigned num_shaders)
2722 {
2723    bool arb_fragment_coord_conventions_enable = false;
2724    bool KHR_shader_subgroup_basic_enable = false;
2725 
2726    /* Check that global variables defined in multiple shaders are consistent.
2727     */
2728    struct hash_table *variables =
2729       _mesa_hash_table_create(mem_ctx, _mesa_hash_string, _mesa_key_string_equal);
2730    for (unsigned i = 0; i < num_shaders; i++) {
2731       if (shader_list[i] == NULL)
2732          continue;
2733       cross_validate_globals(mem_ctx, &ctx->Const, prog, shader_list[i]->nir,
2734                              variables, false);
2735       if (shader_list[i]->ARB_fragment_coord_conventions_enable)
2736          arb_fragment_coord_conventions_enable = true;
2737       if (shader_list[i]->KHR_shader_subgroup_basic_enable)
2738          KHR_shader_subgroup_basic_enable = true;
2739    }
2740 
2741    if (!prog->data->LinkStatus)
2742       return NULL;
2743 
2744    /* Check that interface blocks defined in multiple shaders are consistent.
2745     */
2746    gl_nir_validate_intrastage_interface_blocks(prog,
2747                                                (const struct gl_shader **)shader_list,
2748                                                num_shaders);
2749    if (!prog->data->LinkStatus)
2750       return NULL;
2751 
2752    /* Check that there is only a single definition of each function signature
2753     * across all shaders.
2754     */
2755    for (unsigned i = 0; i < (num_shaders - 1); i++) {
2756       nir_foreach_function_impl(func, shader_list[i]->nir) {
2757          for (unsigned j = i + 1; j < num_shaders; j++) {
2758             nir_function *other =
2759                nir_shader_get_function_for_name(shader_list[j]->nir,
2760                                                 func->function->name);
2761 
2762             /* If the other shader has no function (and therefore no function
2763              * signatures) with the same name, skip to the next shader.
2764              */
2765             if (other == NULL || other->impl == NULL)
2766                continue;
2767 
2768             bool exact_match =
2769                exact_matching_signature(other, func->function);
2770 
2771                if (exact_match) {
2772                   linker_error(prog, "function `%s' is multiply defined\n",
2773                                func->function->name);
2774                   return NULL;
2775                }
2776          }
2777       }
2778    }
2779 
2780    /* Find the shader that defines main, and make a clone of it.
2781     *
2782     * Starting with the clone, search for undefined references.  If one is
2783     * found, find the shader that defines it.  Clone the reference and add
2784     * it to the shader.  Repeat until there are no undefined references or
2785     * until a reference cannot be resolved.
2786     */
2787    struct gl_shader *main = NULL;
2788    nir_function_impl *main_func = NULL;
2789    for (unsigned i = 0; i < num_shaders; i++) {
2790       main_func = nir_shader_get_entrypoint(shader_list[i]->nir);
2791       if (main_func) {
2792          main = shader_list[i];
2793          break;
2794       }
2795    }
2796 
2797    if (main == NULL) {
2798       linker_error(prog, "%s shader lacks `main'\n",
2799                    _mesa_shader_stage_to_string(shader_list[0]->Stage));
2800       return NULL;
2801    }
2802 
2803    struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
2804    linked->Stage = shader_list[0]->Stage;
2805 
2806    /* Create program and attach it to the linked shader */
2807    struct gl_program *gl_prog =
2808       ctx->Driver.NewProgram(ctx, shader_list[0]->Stage, prog->Name, false);
2809    if (!gl_prog) {
2810       prog->data->LinkStatus = LINKING_FAILURE;
2811       _mesa_delete_linked_shader(ctx, linked);
2812       return NULL;
2813    }
2814 
2815    _mesa_reference_shader_program_data(&gl_prog->sh.data, prog->data);
2816 
2817    /* Don't use _mesa_reference_program() just take ownership */
2818    linked->Program = gl_prog;
2819 
2820    linked->Program->nir = nir_shader_clone(NULL, main->nir);
2821 
2822    link_fs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders,
2823                                    arb_fragment_coord_conventions_enable);
2824    link_tcs_out_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2825    link_tes_in_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2826    link_gs_inout_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2827    link_cs_input_layout_qualifiers(prog, gl_prog, shader_list, num_shaders);
2828 
2829    if (linked->Stage != MESA_SHADER_FRAGMENT)
2830       link_xfb_stride_layout_qualifiers(&ctx->Const, prog, shader_list, num_shaders);
2831 
2832    link_bindless_layout_qualifiers(prog, shader_list, num_shaders);
2833 
2834    link_layer_viewport_relative_qualifier(prog, gl_prog, shader_list, num_shaders);
2835 
2836    gl_prog->nir->info.subgroup_size = KHR_shader_subgroup_basic_enable ?
2837       SUBGROUP_SIZE_API_CONSTANT : SUBGROUP_SIZE_UNIFORM;
2838 
2839    /* Move any instructions other than variable declarations or function
2840     * declarations into main.
2841     */
2842    if (!gl_nir_link_function_calls(prog, main, linked, shader_list, num_shaders)) {
2843       _mesa_delete_linked_shader(ctx, linked);
2844       return NULL;
2845    }
2846 
2847    /* Add calls to temp global instruction wrapper functions */
2848    main_func = nir_shader_get_entrypoint(linked->Program->nir);
2849    nir_builder b = nir_builder_create(main_func);
2850    nir_foreach_function_impl(impl, linked->Program->nir) {
2851       if (strncmp(impl->function->name, "gl_mesa_tmp", 11) == 0) {
2852          nir_call_instr *call = nir_call_instr_create(linked->Program->nir,
2853                                                       impl->function);
2854          b.cursor = nir_before_block(nir_start_block(main_func));
2855          nir_builder_instr_insert(&b, &call->instr);
2856       }
2857    }
2858 
2859    /* Make a pass over all variable declarations to ensure that arrays with
2860     * unspecified sizes have a size specified.  The size is inferred from the
2861     * max_array_access field.
2862     */
2863    gl_nir_linker_size_arrays(linked->Program->nir);
2864    nir_fixup_deref_types(linked->Program->nir);
2865 
2866    /* Now that we know the sizes of all the arrays, we can replace .length()
2867     * calls with a constant expression.
2868     */
2869    array_length_to_const(linked->Program->nir);
2870 
2871    if (!prog->data->LinkStatus) {
2872       _mesa_delete_linked_shader(ctx, linked);
2873       return NULL;
2874    }
2875 
2876    /* At this point linked should contain all of the linked IR, so
2877     * validate it to make sure nothing went wrong.
2878     */
2879    nir_validate_shader(linked->Program->nir, "post shader stage combine");
2880 
2881    lower_derivatives_without_layout(&b);
2882 
2883    /* Set the linked source BLAKE3. */
2884    if (num_shaders == 1) {
2885       memcpy(linked->Program->nir->info.source_blake3,
2886              shader_list[0]->compiled_source_blake3,
2887              BLAKE3_OUT_LEN);
2888    } else {
2889       struct mesa_blake3 blake3_ctx;
2890       _mesa_blake3_init(&blake3_ctx);
2891 
2892       for (unsigned i = 0; i < num_shaders; i++) {
2893          if (shader_list[i] == NULL)
2894             continue;
2895 
2896          _mesa_blake3_update(&blake3_ctx, shader_list[i]->compiled_source_blake3,
2897                              BLAKE3_OUT_LEN);
2898       }
2899       _mesa_blake3_final(&blake3_ctx, linked->Program->nir->info.source_blake3);
2900    }
2901 
2902    return linked;
2903 }
2904 
2905 /**
2906  * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
2907  * for a variable, checks for overlaps between other uniforms using explicit
2908  * locations.
2909  */
2910 static int
reserve_explicit_locations(struct gl_shader_program * prog,struct string_to_uint_map * map,nir_variable * var)2911 reserve_explicit_locations(struct gl_shader_program *prog,
2912                            struct string_to_uint_map *map, nir_variable *var)
2913 {
2914    unsigned slots = glsl_type_uniform_locations(var->type);
2915    unsigned max_loc = var->data.location + slots - 1;
2916    unsigned return_value = slots;
2917 
2918    /* Resize remap table if locations do not fit in the current one. */
2919    if (max_loc + 1 > prog->NumUniformRemapTable) {
2920       prog->UniformRemapTable =
2921          reralloc(prog, prog->UniformRemapTable,
2922                   struct gl_uniform_storage *,
2923                   max_loc + 1);
2924 
2925       if (!prog->UniformRemapTable) {
2926          linker_error(prog, "Out of memory during linking.\n");
2927          return -1;
2928       }
2929 
2930       /* Initialize allocated space. */
2931       for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++)
2932          prog->UniformRemapTable[i] = NULL;
2933 
2934       prog->NumUniformRemapTable = max_loc + 1;
2935    }
2936 
2937    for (unsigned i = 0; i < slots; i++) {
2938       unsigned loc = var->data.location + i;
2939 
2940       /* Check if location is already used. */
2941       if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
2942 
2943          /* Possibly same uniform from a different stage, this is ok. */
2944          unsigned hash_loc;
2945          if (string_to_uint_map_get(map, &hash_loc, var->name) &&
2946              hash_loc == loc - i) {
2947             return_value = 0;
2948             continue;
2949          }
2950 
2951          /* ARB_explicit_uniform_location specification states:
2952           *
2953           *     "No two default-block uniform variables in the program can have
2954           *     the same location, even if they are unused, otherwise a compiler
2955           *     or linker error will be generated."
2956           */
2957          linker_error(prog,
2958                       "location qualifier for uniform %s overlaps "
2959                       "previously used location\n",
2960                       var->name);
2961          return -1;
2962       }
2963 
2964       /* Initialize location as inactive before optimization
2965        * rounds and location assignment.
2966        */
2967       prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
2968    }
2969 
2970    /* Note, base location used for arrays. */
2971    string_to_uint_map_put(map, var->data.location, var->name);
2972 
2973    return return_value;
2974 }
2975 
2976 static bool
reserve_subroutine_explicit_locations(struct gl_shader_program * prog,struct gl_program * p,nir_variable * var)2977 reserve_subroutine_explicit_locations(struct gl_shader_program *prog,
2978                                       struct gl_program *p,
2979                                       nir_variable *var)
2980 {
2981    unsigned slots = glsl_type_uniform_locations(var->type);
2982    unsigned max_loc = var->data.location + slots - 1;
2983 
2984    /* Resize remap table if locations do not fit in the current one. */
2985    if (max_loc + 1 > p->sh.NumSubroutineUniformRemapTable) {
2986       p->sh.SubroutineUniformRemapTable =
2987          reralloc(p, p->sh.SubroutineUniformRemapTable,
2988                   struct gl_uniform_storage *,
2989                   max_loc + 1);
2990 
2991       if (!p->sh.SubroutineUniformRemapTable) {
2992          linker_error(prog, "Out of memory during linking.\n");
2993          return false;
2994       }
2995 
2996       /* Initialize allocated space. */
2997       for (unsigned i = p->sh.NumSubroutineUniformRemapTable; i < max_loc + 1; i++)
2998          p->sh.SubroutineUniformRemapTable[i] = NULL;
2999 
3000       p->sh.NumSubroutineUniformRemapTable = max_loc + 1;
3001    }
3002 
3003    for (unsigned i = 0; i < slots; i++) {
3004       unsigned loc = var->data.location + i;
3005 
3006       /* Check if location is already used. */
3007       if (p->sh.SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
3008 
3009          /* ARB_explicit_uniform_location specification states:
3010           *     "No two subroutine uniform variables can have the same location
3011           *     in the same shader stage, otherwise a compiler or linker error
3012           *     will be generated."
3013           */
3014          linker_error(prog,
3015                       "location qualifier for uniform %s overlaps "
3016                       "previously used location\n",
3017                       var->name);
3018          return false;
3019       }
3020 
3021       /* Initialize location as inactive before optimization
3022        * rounds and location assignment.
3023        */
3024       p->sh.SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
3025    }
3026 
3027    return true;
3028 }
3029 /**
3030  * Check and reserve all explicit uniform locations, called before
3031  * any optimizations happen to handle also inactive uniforms and
3032  * inactive array elements that may get trimmed away.
3033  */
3034 static void
check_explicit_uniform_locations(const struct gl_extensions * exts,struct gl_shader_program * prog)3035 check_explicit_uniform_locations(const struct gl_extensions *exts,
3036                                  struct gl_shader_program *prog)
3037 {
3038    prog->NumExplicitUniformLocations = 0;
3039 
3040    if (!exts->ARB_explicit_uniform_location)
3041       return;
3042 
3043    /* This map is used to detect if overlapping explicit locations
3044     * occur with the same uniform (from different stage) or a different one.
3045     */
3046    struct string_to_uint_map *uniform_map = string_to_uint_map_ctor();
3047 
3048    if (!uniform_map) {
3049       linker_error(prog, "Out of memory during linking.\n");
3050       return;
3051    }
3052 
3053    unsigned entries_total = 0;
3054    unsigned mask = prog->data->linked_stages;
3055    while (mask) {
3056       const int i = u_bit_scan(&mask);
3057       struct gl_program *p = prog->_LinkedShaders[i]->Program;
3058 
3059       unsigned modes = nir_var_uniform | nir_var_mem_ubo | nir_var_image;
3060       nir_foreach_variable_with_modes(var, p->nir, modes) {
3061          if (var->data.explicit_location) {
3062             bool ret = false;
3063             if (glsl_type_is_subroutine(glsl_without_array(var->type)))
3064                ret = reserve_subroutine_explicit_locations(prog, p, var);
3065             else {
3066                int slots = reserve_explicit_locations(prog, uniform_map,
3067                                                       var);
3068                if (slots != -1) {
3069                   ret = true;
3070                   entries_total += slots;
3071                }
3072             }
3073             if (!ret) {
3074                string_to_uint_map_dtor(uniform_map);
3075                return;
3076             }
3077          }
3078       }
3079    }
3080 
3081    link_util_update_empty_uniform_locations(prog);
3082 
3083    string_to_uint_map_dtor(uniform_map);
3084    prog->NumExplicitUniformLocations = entries_total;
3085 }
3086 
3087 static void
link_assign_subroutine_types(struct gl_shader_program * prog)3088 link_assign_subroutine_types(struct gl_shader_program *prog)
3089 {
3090    unsigned mask = prog->data->linked_stages;
3091    while (mask) {
3092       const int i = u_bit_scan(&mask);
3093       struct gl_program *p = prog->_LinkedShaders[i]->Program;
3094 
3095       struct set *fn_decl_set =
3096          _mesa_set_create(NULL, _mesa_hash_string, _mesa_key_string_equal);
3097 
3098       p->sh.MaxSubroutineFunctionIndex = 0;
3099       nir_foreach_function(fn, p->nir) {
3100          /* A function might be decalred multiple times but we should only
3101           * process it once
3102           */
3103          struct set_entry *entry = _mesa_set_search(fn_decl_set, fn->name);
3104          if (entry)
3105             continue;
3106 
3107          _mesa_set_add(fn_decl_set, fn->name);
3108 
3109          if (fn->is_subroutine)
3110             p->sh.NumSubroutineUniformTypes++;
3111 
3112          if (!fn->num_subroutine_types)
3113             continue;
3114 
3115          /* these should have been calculated earlier. */
3116          assert(fn->subroutine_index != -1);
3117          if (p->sh.NumSubroutineFunctions + 1 > MAX_SUBROUTINES) {
3118             linker_error(prog, "Too many subroutine functions declared.\n");
3119             return;
3120          }
3121          p->sh.SubroutineFunctions = reralloc(p, p->sh.SubroutineFunctions,
3122                                             struct gl_subroutine_function,
3123                                             p->sh.NumSubroutineFunctions + 1);
3124          p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].name.string = ralloc_strdup(p, fn->name);
3125          resource_name_updated(&p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].name);
3126          p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types;
3127          p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types =
3128             ralloc_array(p, const struct glsl_type *,
3129                          fn->num_subroutine_types);
3130 
3131          /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the
3132           * GLSL 4.5 spec:
3133           *
3134           *    "Each subroutine with an index qualifier in the shader must be
3135           *    given a unique index, otherwise a compile or link error will be
3136           *    generated."
3137           */
3138          for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
3139             if (p->sh.SubroutineFunctions[j].index != -1 &&
3140                 p->sh.SubroutineFunctions[j].index == fn->subroutine_index) {
3141                linker_error(prog, "each subroutine index qualifier in the "
3142                             "shader must be unique\n");
3143                return;
3144             }
3145          }
3146          p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].index =
3147             fn->subroutine_index;
3148 
3149          if (fn->subroutine_index > (int)p->sh.MaxSubroutineFunctionIndex)
3150             p->sh.MaxSubroutineFunctionIndex = fn->subroutine_index;
3151 
3152          for (int j = 0; j < fn->num_subroutine_types; j++)
3153             p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types[j] = fn->subroutine_types[j];
3154          p->sh.NumSubroutineFunctions++;
3155       }
3156 
3157       _mesa_set_destroy(fn_decl_set, NULL);
3158    }
3159 }
3160 
3161 static void
verify_subroutine_associated_funcs(struct gl_shader_program * prog)3162 verify_subroutine_associated_funcs(struct gl_shader_program *prog)
3163 {
3164    unsigned mask = prog->data->linked_stages;
3165    while (mask) {
3166       const int i = u_bit_scan(&mask);
3167       struct gl_program *p = prog->_LinkedShaders[i]->Program;
3168 
3169       /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
3170        *
3171        *   "A program will fail to compile or link if any shader
3172        *    or stage contains two or more functions with the same
3173        *    name if the name is associated with a subroutine type."
3174        */
3175       for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) {
3176          unsigned definitions = 0;
3177          char *name = p->sh.SubroutineFunctions[j].name.string;
3178 
3179          /* Calculate number of function definitions with the same name */
3180          nir_foreach_function(fn, p->nir) {
3181             /* If the function is only declared not implemented continue */
3182             if (fn->impl != NULL)
3183                continue;
3184 
3185             if (strcmp(fn->name, name) == 0) {
3186                if (++definitions > 1) {
3187                   linker_error(prog, "%s shader contains two or more function "
3188                                "definitions with name `%s', which is "
3189                                "associated with a subroutine type.\n",
3190                                _mesa_shader_stage_to_string(i),
3191                                fn->name);
3192                   return;
3193                }
3194             }
3195          }
3196       }
3197    }
3198 }
3199 
3200 /**
3201  * Validate shader image resources.
3202  */
3203 static void
check_image_resources(const struct gl_constants * consts,const struct gl_extensions * exts,struct gl_shader_program * prog)3204 check_image_resources(const struct gl_constants *consts,
3205                       const struct gl_extensions *exts,
3206                       struct gl_shader_program *prog)
3207 {
3208    unsigned total_image_units = 0;
3209    unsigned fragment_outputs = 0;
3210    unsigned total_shader_storage_blocks = 0;
3211 
3212    if (!exts->ARB_shader_image_load_store)
3213       return;
3214 
3215    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3216       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
3217       if (!sh)
3218          continue;
3219 
3220       total_image_units += sh->Program->info.num_images;
3221       total_shader_storage_blocks += sh->Program->info.num_ssbos;
3222    }
3223 
3224    if (total_image_units > consts->MaxCombinedImageUniforms)
3225       linker_error(prog, "Too many combined image uniforms\n");
3226 
3227    struct gl_linked_shader *frag_sh =
3228       prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
3229    if (frag_sh) {
3230       uint64_t frag_outputs_written = frag_sh->Program->info.outputs_written;
3231       fragment_outputs = util_bitcount64(frag_outputs_written);
3232    }
3233 
3234    if (total_image_units + fragment_outputs + total_shader_storage_blocks >
3235        consts->MaxCombinedShaderOutputResources)
3236       linker_error(prog, "Too many combined image uniforms, shader storage "
3237                          " buffers and fragment outputs\n");
3238 }
3239 
3240 static bool
is_sampler_array_accessed_indirectly(nir_deref_instr * deref)3241 is_sampler_array_accessed_indirectly(nir_deref_instr *deref)
3242 {
3243    for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
3244       if (d->deref_type != nir_deref_type_array)
3245          continue;
3246 
3247       if (nir_src_is_const(d->arr.index))
3248          continue;
3249 
3250       return true;
3251    }
3252 
3253    return false;
3254 }
3255 
3256 /**
3257  * This check is done to make sure we allow only constant expression
3258  * indexing and "constant-index-expression" (indexing with an expression
3259  * that includes loop induction variable).
3260  */
3261 static bool
validate_sampler_array_indexing(const struct gl_constants * consts,struct gl_shader_program * prog)3262 validate_sampler_array_indexing(const struct gl_constants *consts,
3263                                 struct gl_shader_program *prog)
3264 {
3265    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3266       if (prog->_LinkedShaders[i] == NULL)
3267          continue;
3268 
3269       bool no_dynamic_indexing =
3270          consts->ShaderCompilerOptions[i].NirOptions->force_indirect_unrolling_sampler;
3271 
3272       bool uses_indirect_sampler_array_indexing = false;
3273       nir_foreach_function_impl(impl, prog->_LinkedShaders[i]->Program->nir) {
3274          nir_foreach_block(block, impl) {
3275             nir_foreach_instr(instr, block) {
3276                /* Check if a sampler array is accessed indirectly */
3277                if (instr->type == nir_instr_type_tex) {
3278                   nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
3279                   int sampler_idx =
3280                      nir_tex_instr_src_index(tex_instr, nir_tex_src_sampler_deref);
3281                   if (sampler_idx >= 0) {
3282                      nir_deref_instr *deref =
3283                         nir_instr_as_deref(tex_instr->src[sampler_idx].src.ssa->parent_instr);
3284                      if (is_sampler_array_accessed_indirectly(deref)) {
3285                         uses_indirect_sampler_array_indexing = true;
3286                         break;
3287                      }
3288                   }
3289                }
3290             }
3291 
3292             if (uses_indirect_sampler_array_indexing)
3293                break;
3294          }
3295          if (uses_indirect_sampler_array_indexing)
3296             break;
3297       }
3298 
3299       if (uses_indirect_sampler_array_indexing) {
3300          const char *msg = "sampler arrays indexed with non-constant "
3301                            "expressions is forbidden in GLSL %s %u";
3302          /* Backend has indicated that it has no dynamic indexing support. */
3303          if (no_dynamic_indexing) {
3304             linker_error(prog, msg, prog->IsES ? "ES" : "", prog->GLSL_Version);
3305             return false;
3306          } else {
3307             linker_warning(prog, msg, prog->IsES ? "ES" : "",
3308                            prog->GLSL_Version);
3309          }
3310       }
3311    }
3312 
3313    return true;
3314 }
3315 
3316 static nir_variable *
find_frag_builtin(nir_shader * shader,bool is_sysval,unsigned sysval,unsigned varying)3317 find_frag_builtin(nir_shader *shader, bool is_sysval, unsigned sysval,
3318                   unsigned varying)
3319 {
3320 
3321    unsigned location = is_sysval ? sysval : varying;
3322    nir_variable_mode mode =
3323       is_sysval ? nir_var_system_value : nir_var_shader_in;
3324 
3325    return nir_find_variable_with_location(shader, mode, location);
3326 }
3327 
3328 /**
3329  * Verifies the invariance of built-in special variables.
3330  */
3331 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)3332 validate_invariant_builtins(const struct gl_constants *consts,
3333                             struct gl_shader_program *prog,
3334                             const struct gl_linked_shader *vert,
3335                             const struct gl_linked_shader *frag)
3336 {
3337    const nir_variable *var_vert;
3338    const nir_variable *var_frag;
3339 
3340    if (!vert || !frag)
3341       return true;
3342 
3343    /*
3344     * From OpenGL ES Shading Language 1.0 specification
3345     * (4.6.4 Invariance and Linkage):
3346     *     "The invariance of varyings that are declared in both the vertex and
3347     *     fragment shaders must match. For the built-in special variables,
3348     *     gl_FragCoord can only be declared invariant if and only if
3349     *     gl_Position is declared invariant. Similarly gl_PointCoord can only
3350     *     be declared invariant if and only if gl_PointSize is declared
3351     *     invariant. It is an error to declare gl_FrontFacing as invariant.
3352     *     The invariance of gl_FrontFacing is the same as the invariance of
3353     *     gl_Position."
3354     */
3355    var_frag = find_frag_builtin(frag->Program->nir,
3356                                 consts->GLSLFragCoordIsSysVal,
3357                                 SYSTEM_VALUE_FRAG_COORD, VARYING_SLOT_POS);
3358    if (var_frag && var_frag->data.invariant) {
3359       var_vert = nir_find_variable_with_location(vert->Program->nir,
3360                                                  nir_var_shader_out,
3361                                                  VARYING_SLOT_POS);
3362       if (var_vert && !var_vert->data.invariant) {
3363          linker_error(prog,
3364                       "fragment shader built-in `%s' has invariant qualifier, "
3365                       "but vertex shader built-in `%s' lacks invariant qualifier\n",
3366                       var_frag->name, var_vert->name);
3367          return false;
3368       }
3369    }
3370 
3371    var_frag = find_frag_builtin(frag->Program->nir,
3372                                 consts->GLSLPointCoordIsSysVal,
3373                                 SYSTEM_VALUE_POINT_COORD, VARYING_SLOT_PNTC);
3374    if (var_frag && var_frag->data.invariant) {
3375       var_vert = nir_find_variable_with_location(vert->Program->nir,
3376                                                  nir_var_shader_out,
3377                                                  VARYING_SLOT_PSIZ);
3378       if (var_vert && !var_vert->data.invariant) {
3379          linker_error(prog,
3380                       "fragment shader built-in `%s' has invariant qualifier, "
3381                       "but vertex shader built-in `%s' lacks invariant qualifier\n",
3382                       var_frag->name, var_vert->name);
3383          return false;
3384       }
3385    }
3386 
3387    var_frag = find_frag_builtin(frag->Program->nir,
3388                                 consts->GLSLFrontFacingIsSysVal,
3389                                 SYSTEM_VALUE_FRONT_FACE, VARYING_SLOT_FACE);
3390    if (var_frag && var_frag->data.invariant) {
3391       linker_error(prog,
3392                    "fragment shader built-in `%s' can not be declared as invariant\n",
3393                    var_frag->name);
3394       return false;
3395    }
3396 
3397    return true;
3398 }
3399 
3400 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)3401 find_assignments(nir_shader *shader, nir_variable *var1, nir_variable *var2,
3402                  nir_variable *var3, bool *var1_written, bool *var2_written,
3403                  bool *var3_written)
3404 {
3405    nir_foreach_function_impl(impl, shader) {
3406       nir_foreach_block(block, impl) {
3407          nir_foreach_instr(instr, block) {
3408             if (instr->type == nir_instr_type_intrinsic) {
3409                nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
3410                if (intrin->intrinsic == nir_intrinsic_store_deref ||
3411                    intrin->intrinsic == nir_intrinsic_copy_deref) {
3412                   nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
3413                   nir_variable *var = nir_deref_instr_get_variable(deref);
3414                   if (!var)
3415                      continue;
3416 
3417                   if (var == var1)
3418                      *var1_written = true;
3419                   else if (var == var2)
3420                      *var2_written = true;
3421                   else if (var == var3)
3422                      *var3_written = true;
3423                }
3424             }
3425          }
3426       }
3427    }
3428 }
3429 
3430 /**
3431  * Set clip_distance_array_size based and cull_distance_array_size on the given
3432  * shader.
3433  *
3434  * Also check for errors based on incorrect usage of gl_ClipVertex and
3435  * gl_ClipDistance and gl_CullDistance.
3436  * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance
3437  * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances.
3438  */
3439 static void
analyze_clip_cull_usage(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts,struct shader_info * info)3440 analyze_clip_cull_usage(struct gl_shader_program *prog, nir_shader *shader,
3441                         const struct gl_constants *consts,
3442                         struct shader_info *info)
3443 {
3444    if (consts->DoDCEBeforeClipCullAnalysis) {
3445       /* Remove dead functions to avoid raising an error (eg: dead function
3446        * writes to gl_ClipVertex, and main() writes to gl_ClipDistance).
3447        */
3448       remove_dead_functions(shader);
3449    }
3450 
3451    info->clip_distance_array_size = 0;
3452    info->cull_distance_array_size = 0;
3453 
3454    if (prog->GLSL_Version >= (prog->IsES ? 300 : 130)) {
3455       /* From section 7.1 (Vertex Shader Special Variables) of the
3456        * GLSL 1.30 spec:
3457        *
3458        *   "It is an error for a shader to statically write both
3459        *   gl_ClipVertex and gl_ClipDistance."
3460        *
3461        * This does not apply to GLSL ES shaders, since GLSL ES defines neither
3462        * gl_ClipVertex nor gl_ClipDistance. However with
3463        * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0.
3464        */
3465       nir_variable *clip_dist =
3466          nir_find_variable_with_location(shader,
3467                                          nir_var_shader_out,
3468                                          VARYING_SLOT_CLIP_DIST0);
3469       nir_variable *cull_dist =
3470          nir_find_variable_with_location(shader,
3471                                          nir_var_shader_out,
3472                                          VARYING_SLOT_CULL_DIST0);
3473       nir_variable *clip_vert =
3474          nir_find_variable_with_location(shader,
3475                                          nir_var_shader_out,
3476                                          VARYING_SLOT_CLIP_VERTEX);
3477 
3478       bool clip_dist_written = false;
3479       bool cull_dist_written = false;
3480       bool clip_vert_written = false;
3481       find_assignments(shader, clip_dist, cull_dist, clip_vert,
3482                        &clip_dist_written, &cull_dist_written,
3483                        &clip_vert_written);
3484 
3485       /* From the ARB_cull_distance spec:
3486        *
3487        * It is a compile-time or link-time error for the set of shaders forming
3488        * a program to statically read or write both gl_ClipVertex and either
3489        * gl_ClipDistance or gl_CullDistance.
3490        *
3491        * This does not apply to GLSL ES shaders, since GLSL ES doesn't define
3492        * gl_ClipVertex.
3493        */
3494       if (!prog->IsES) {
3495          if (clip_vert_written && clip_dist_written) {
3496             linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
3497                          "and `gl_ClipDistance'\n",
3498                          _mesa_shader_stage_to_string(info->stage));
3499             return;
3500          }
3501          if (clip_vert_written && cull_dist_written) {
3502             linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
3503                          "and `gl_CullDistance'\n",
3504                          _mesa_shader_stage_to_string(info->stage));
3505             return;
3506          }
3507       }
3508 
3509       if (clip_dist_written)
3510          info->clip_distance_array_size = glsl_get_length(clip_dist->type);
3511 
3512       if (cull_dist_written)
3513          info->cull_distance_array_size = glsl_get_length(cull_dist->type);
3514    }
3515 }
3516 
3517 /**
3518  * Verify that a vertex shader executable meets all semantic requirements.
3519  *
3520  * Also sets info.clip_distance_array_size and
3521  * info.cull_distance_array_size as a side effect.
3522  *
3523  * \param shader  Vertex shader executable to be verified
3524  */
3525 static void
validate_vertex_shader_executable(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts)3526 validate_vertex_shader_executable(struct gl_shader_program *prog,
3527                                   nir_shader *shader,
3528                                   const struct gl_constants *consts)
3529 {
3530    if (shader == NULL)
3531       return;
3532 
3533    /* From the GLSL 1.10 spec, page 48:
3534     *
3535     *     "The variable gl_Position is available only in the vertex
3536     *      language and is intended for writing the homogeneous vertex
3537     *      position. All executions of a well-formed vertex shader
3538     *      executable must write a value into this variable. [...] The
3539     *      variable gl_Position is available only in the vertex
3540     *      language and is intended for writing the homogeneous vertex
3541     *      position. All executions of a well-formed vertex shader
3542     *      executable must write a value into this variable."
3543     *
3544     * while in GLSL 1.40 this text is changed to:
3545     *
3546     *     "The variable gl_Position is available only in the vertex
3547     *      language and is intended for writing the homogeneous vertex
3548     *      position. It can be written at any time during shader
3549     *      execution. It may also be read back by a vertex shader
3550     *      after being written. This value will be used by primitive
3551     *      assembly, clipping, culling, and other fixed functionality
3552     *      operations, if present, that operate on primitives after
3553     *      vertex processing has occurred. Its value is undefined if
3554     *      the vertex shader executable does not write gl_Position."
3555     *
3556     * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
3557     * gl_Position is not an error.
3558     */
3559    if (prog->GLSL_Version < (prog->IsES ? 300 : 140)) {
3560       nir_variable *gl_position =
3561          nir_find_variable_with_location(shader,
3562                                          nir_var_shader_out,
3563                                          VARYING_SLOT_POS);
3564 
3565       bool gl_position_written = false;
3566       find_assignments(shader, gl_position, NULL, NULL, &gl_position_written,
3567                        NULL, NULL);
3568       if (!gl_position_written) {
3569         if (prog->IsES) {
3570           linker_warning(prog,
3571                          "vertex shader does not write to `gl_Position'. "
3572                          "Its value is undefined. \n");
3573         } else {
3574           linker_error(prog,
3575                        "vertex shader does not write to `gl_Position'. \n");
3576         }
3577          return;
3578       }
3579    }
3580 
3581    analyze_clip_cull_usage(prog, shader, consts, &shader->info);
3582 }
3583 
3584 static void
validate_tess_eval_shader_executable(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts)3585 validate_tess_eval_shader_executable(struct gl_shader_program *prog,
3586                                      nir_shader *shader,
3587                                      const struct gl_constants *consts)
3588 {
3589    if (shader == NULL)
3590       return;
3591 
3592    analyze_clip_cull_usage(prog, shader, consts, &shader->info);
3593 }
3594 
3595 /**
3596  * Verify that a fragment shader executable meets all semantic requirements
3597  *
3598  * \param shader  Fragment shader executable to be verified
3599  */
3600 static void
validate_fragment_shader_executable(struct gl_shader_program * prog,nir_shader * shader)3601 validate_fragment_shader_executable(struct gl_shader_program *prog,
3602                                     nir_shader *shader)
3603 {
3604    if (shader == NULL)
3605       return;
3606 
3607    nir_variable *gl_frag_color =
3608       nir_find_variable_with_location(shader,
3609                                       nir_var_shader_out,
3610                                       FRAG_RESULT_COLOR);
3611    nir_variable *gl_frag_data =
3612       nir_find_variable_with_location(shader,
3613                                       nir_var_shader_out,
3614                                       FRAG_RESULT_DATA0);
3615 
3616    bool gl_frag_color_written = false;
3617    bool gl_frag_data_written = false;
3618    find_assignments(shader, gl_frag_color, gl_frag_data, NULL,
3619                     &gl_frag_color_written, &gl_frag_data_written, NULL);
3620 
3621    if (gl_frag_color_written && gl_frag_data_written) {
3622       linker_error(prog,  "fragment shader writes to both "
3623                    "`gl_FragColor' and `gl_FragData'\n");
3624    }
3625 }
3626 
3627 /**
3628  * Verify that a geometry shader executable meets all semantic requirements
3629  *
3630  * Also sets prog->Geom.VerticesIn, and info.clip_distance_array_sizeand
3631  * info.cull_distance_array_size as a side effect.
3632  *
3633  * \param shader Geometry shader executable to be verified
3634  */
3635 static void
validate_geometry_shader_executable(struct gl_shader_program * prog,nir_shader * shader,const struct gl_constants * consts)3636 validate_geometry_shader_executable(struct gl_shader_program *prog,
3637                                     nir_shader *shader,
3638                                     const struct gl_constants *consts)
3639 {
3640    if (shader == NULL)
3641       return;
3642 
3643    unsigned num_vertices =
3644       mesa_vertices_per_prim(shader->info.gs.input_primitive);
3645    shader->info.gs.vertices_in  = num_vertices;
3646 
3647    analyze_clip_cull_usage(prog, shader, consts, &shader->info);
3648 }
3649 
3650 bool
gl_nir_link_glsl(struct gl_context * ctx,struct gl_shader_program * prog)3651 gl_nir_link_glsl(struct gl_context *ctx, struct gl_shader_program *prog)
3652 {
3653    const struct gl_constants *consts = &ctx->Const;
3654    const struct gl_extensions *exts = &ctx->Extensions;
3655    gl_api api = ctx->API;
3656 
3657    if (prog->NumShaders == 0)
3658       return true;
3659 
3660    MESA_TRACE_FUNC();
3661 
3662    void *mem_ctx = ralloc_context(NULL); /* temporary linker context */
3663 
3664    /* Separate the shaders into groups based on their type.
3665     */
3666    struct gl_shader **shader_list[MESA_SHADER_STAGES];
3667    unsigned num_shaders[MESA_SHADER_STAGES];
3668 
3669    for (int i = 0; i < MESA_SHADER_STAGES; i++) {
3670       shader_list[i] = (struct gl_shader **)
3671          calloc(prog->NumShaders, sizeof(struct gl_shader *));
3672       num_shaders[i] = 0;
3673    }
3674 
3675    unsigned min_version = UINT_MAX;
3676    unsigned max_version = 0;
3677    for (unsigned i = 0; i < prog->NumShaders; i++) {
3678       min_version = MIN2(min_version, prog->Shaders[i]->Version);
3679       max_version = MAX2(max_version, prog->Shaders[i]->Version);
3680 
3681       if (!consts->AllowGLSLRelaxedES &&
3682           prog->Shaders[i]->IsES != prog->Shaders[0]->IsES) {
3683          linker_error(prog, "all shaders must use same shading "
3684                       "language version\n");
3685          goto done;
3686       }
3687 
3688       gl_shader_stage shader_type = prog->Shaders[i]->Stage;
3689       shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i];
3690       num_shaders[shader_type]++;
3691    }
3692 
3693    /* In desktop GLSL, different shader versions may be linked together.  In
3694     * GLSL ES, all shader versions must be the same.
3695     */
3696    if (!consts->AllowGLSLRelaxedES && prog->Shaders[0]->IsES &&
3697        min_version != max_version) {
3698       linker_error(prog, "all shaders must use same shading "
3699                    "language version\n");
3700       goto done;
3701    }
3702 
3703    prog->GLSL_Version = max_version;
3704    prog->IsES = prog->Shaders[0]->IsES;
3705 
3706    /* Some shaders have to be linked with some other shaders present.
3707     */
3708    if (!prog->SeparateShader) {
3709       if (num_shaders[MESA_SHADER_GEOMETRY] > 0 &&
3710           num_shaders[MESA_SHADER_VERTEX] == 0) {
3711          linker_error(prog, "Geometry shader must be linked with "
3712                       "vertex shader\n");
3713          goto done;
3714       }
3715       if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
3716           num_shaders[MESA_SHADER_VERTEX] == 0) {
3717          linker_error(prog, "Tessellation evaluation shader must be linked "
3718                       "with vertex shader\n");
3719          goto done;
3720       }
3721       if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
3722           num_shaders[MESA_SHADER_VERTEX] == 0) {
3723          linker_error(prog, "Tessellation control shader must be linked with "
3724                       "vertex shader\n");
3725          goto done;
3726       }
3727 
3728       /* Section 7.3 of the OpenGL ES 3.2 specification says:
3729        *
3730        *    "Linking can fail for [...] any of the following reasons:
3731        *
3732        *     * program contains an object to form a tessellation control
3733        *       shader [...] and [...] the program is not separable and
3734        *       contains no object to form a tessellation evaluation shader"
3735        *
3736        * The OpenGL spec is contradictory. It allows linking without a tess
3737        * eval shader, but that can only be used with transform feedback and
3738        * rasterization disabled. However, transform feedback isn't allowed
3739        * with GL_PATCHES, so it can't be used.
3740        *
3741        * More investigation showed that the idea of transform feedback after
3742        * a tess control shader was dropped, because some hw vendors couldn't
3743        * support tessellation without a tess eval shader, but the linker
3744        * section wasn't updated to reflect that.
3745        *
3746        * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
3747        * spec bug.
3748        *
3749        * Do what's reasonable and always require a tess eval shader if a tess
3750        * control shader is present.
3751        */
3752       if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
3753           num_shaders[MESA_SHADER_TESS_EVAL] == 0) {
3754          linker_error(prog, "Tessellation control shader must be linked with "
3755                       "tessellation evaluation shader\n");
3756          goto done;
3757       }
3758 
3759       if (prog->IsES) {
3760          if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
3761              num_shaders[MESA_SHADER_TESS_CTRL] == 0) {
3762             linker_error(prog, "GLSL ES requires non-separable programs "
3763                          "containing a tessellation evaluation shader to also "
3764                          "be linked with a tessellation control shader\n");
3765             goto done;
3766          }
3767       }
3768    }
3769 
3770    /* Compute shaders have additional restrictions. */
3771    if (num_shaders[MESA_SHADER_COMPUTE] > 0 &&
3772        num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) {
3773       linker_error(prog, "Compute shaders may not be linked with any other "
3774                    "type of shader\n");
3775    }
3776 
3777    /* Link all shaders for a particular stage and validate the result.
3778     */
3779    for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
3780       if (num_shaders[stage] > 0) {
3781          struct gl_linked_shader *const sh =
3782             link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage],
3783                                     num_shaders[stage]);
3784 
3785          if (!prog->data->LinkStatus) {
3786             if (sh)
3787                _mesa_delete_linked_shader(ctx, sh);
3788             goto done;
3789          }
3790 
3791          prog->_LinkedShaders[stage] = sh;
3792          prog->data->linked_stages |= 1 << stage;
3793       }
3794    }
3795 
3796    /* Link all shaders for a particular stage and validate the result.
3797     */
3798    for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
3799       struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
3800       if (sh) {
3801          nir_shader *shader = sh->Program->nir;
3802 
3803          /* Parameters will be filled during NIR linking. */
3804          sh->Program->Parameters = _mesa_new_parameter_list();
3805          sh->Program->shader_program = prog;
3806          shader->info.separate_shader = prog->SeparateShader;
3807 
3808          switch (stage) {
3809          case MESA_SHADER_VERTEX:
3810             validate_vertex_shader_executable(prog, shader, consts);
3811             break;
3812          case MESA_SHADER_TESS_CTRL:
3813             /* nothing to be done */
3814             break;
3815          case MESA_SHADER_TESS_EVAL:
3816             validate_tess_eval_shader_executable(prog, shader, consts);
3817             break;
3818          case MESA_SHADER_GEOMETRY:
3819             validate_geometry_shader_executable(prog, shader, consts);
3820             break;
3821          case MESA_SHADER_FRAGMENT:
3822             validate_fragment_shader_executable(prog, shader);
3823             break;
3824          }
3825          if (!prog->data->LinkStatus) {
3826             _mesa_delete_linked_shader(ctx, sh);
3827 
3828             prog->_LinkedShaders[stage] = NULL;
3829             prog->data->linked_stages ^= 1 << stage;
3830 
3831             goto done;
3832          }
3833       }
3834    }
3835 
3836    /* Here begins the inter-stage linking phase.  Some initial validation is
3837     * performed, then locations are assigned for uniforms, attributes, and
3838     * varyings.
3839     */
3840    cross_validate_uniforms(consts, prog);
3841    if (!prog->data->LinkStatus)
3842       goto done;
3843 
3844    check_explicit_uniform_locations(exts, prog);
3845 
3846    link_assign_subroutine_types(prog);
3847    verify_subroutine_associated_funcs(prog);
3848    if (!prog->data->LinkStatus)
3849       goto done;
3850 
3851    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3852       if (prog->_LinkedShaders[i] == NULL)
3853          continue;
3854 
3855       gl_nir_detect_recursion_linked(prog,
3856                                      prog->_LinkedShaders[i]->Program->nir);
3857       if (!prog->data->LinkStatus)
3858          goto done;
3859 
3860       gl_nir_inline_functions(prog->_LinkedShaders[i]->Program->nir);
3861    }
3862 
3863    resize_tes_inputs(consts, prog);
3864    set_geom_shader_input_array_size(prog);
3865 
3866    /* Validate the inputs of each stage with the output of the preceding
3867     * stage.
3868     */
3869    unsigned prev = MESA_SHADER_STAGES;
3870    for (unsigned i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
3871       if (prog->_LinkedShaders[i] == NULL)
3872          continue;
3873 
3874       if (prev == MESA_SHADER_STAGES) {
3875          prev = i;
3876          continue;
3877       }
3878 
3879       gl_nir_validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev],
3880                                               prog->_LinkedShaders[i]);
3881       if (!prog->data->LinkStatus)
3882          goto done;
3883 
3884       prev = i;
3885    }
3886 
3887    /* Cross-validate uniform blocks between shader stages */
3888    gl_nir_validate_interstage_uniform_blocks(prog, prog->_LinkedShaders);
3889    if (!prog->data->LinkStatus)
3890       goto done;
3891 
3892    if (prog->IsES && prog->GLSL_Version == 100)
3893       if (!validate_invariant_builtins(consts, prog,
3894             prog->_LinkedShaders[MESA_SHADER_VERTEX],
3895             prog->_LinkedShaders[MESA_SHADER_FRAGMENT]))
3896          goto done;
3897 
3898    /* Check and validate stream emissions in geometry shaders */
3899    validate_geometry_shader_emissions(consts, prog);
3900 
3901    prog->last_vert_prog = NULL;
3902    for (int i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
3903       if (prog->_LinkedShaders[i] == NULL)
3904          continue;
3905 
3906       prog->last_vert_prog = prog->_LinkedShaders[i]->Program;
3907       break;
3908    }
3909 
3910    unsigned first = MESA_SHADER_STAGES;
3911    unsigned last = 0;
3912 
3913    /* Determine first and last stage. */
3914    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3915       if (!prog->_LinkedShaders[i])
3916          continue;
3917       if (first == MESA_SHADER_STAGES)
3918          first = i;
3919       last = i;
3920    }
3921 
3922    /* Implement the GLSL 1.30+ rule for discard vs infinite loops.
3923     * This rule also applies to GLSL ES 3.00.
3924     */
3925    if (prog->GLSL_Version >= (prog->IsES ? 300 : 130)) {
3926       struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
3927       if (sh)
3928          gl_nir_lower_discard_flow(sh->Program->nir);
3929    }
3930 
3931    gl_nir_lower_named_interface_blocks(prog);
3932 
3933    /* Validate the inputs of each stage with the output of the preceding
3934     * stage.
3935     */
3936    prev = first;
3937    for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) {
3938       if (prog->_LinkedShaders[i] == NULL)
3939          continue;
3940 
3941       gl_nir_cross_validate_outputs_to_inputs(consts, prog,
3942                                               prog->_LinkedShaders[prev],
3943                                               prog->_LinkedShaders[i]);
3944       if (!prog->data->LinkStatus)
3945          goto done;
3946 
3947       prev = i;
3948    }
3949 
3950    /* The cross validation of outputs/inputs above validates interstage
3951     * explicit locations. We need to do this also for the inputs in the first
3952     * stage and outputs of the last stage included in the program, since there
3953     * is no cross validation for these.
3954     */
3955    gl_nir_validate_first_and_last_interface_explicit_locations(consts, prog,
3956                                                                (gl_shader_stage) first,
3957                                                                (gl_shader_stage) last);
3958 
3959    if (prog->SeparateShader)
3960       disable_varying_optimizations_for_sso(prog);
3961 
3962    struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
3963    unsigned num_linked_shaders = 0;
3964 
3965    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
3966       if (prog->_LinkedShaders[i]) {
3967          linked_shader[num_linked_shaders++] = prog->_LinkedShaders[i];
3968 
3969          /* Section 13.46 (Vertex Attribute Aliasing) of the OpenGL ES 3.2
3970           * specification says:
3971           *
3972           *    "In general, the behavior of GLSL ES should not depend on
3973           *    compiler optimizations which might be implementation-dependent.
3974           *    Name matching rules in most languages, including C++ from which
3975           *    GLSL ES is derived, are based on declarations rather than use.
3976           *
3977           *    RESOLUTION: The existence of aliasing is determined by
3978           *    declarations present after preprocessing."
3979           *
3980           * Because of this rule, we don't remove dead attributes before
3981           * attribute assignment for vertex shader inputs here.
3982           */
3983          if (!(prog->IsES && prog->GLSL_Version >= 300 && i == MESA_SHADER_VERTEX))
3984             remove_dead_varyings_pre_linking(prog->_LinkedShaders[i]->Program->nir);
3985       }
3986    }
3987 
3988    if (!gl_assign_attribute_or_color_locations(consts, prog))
3989       goto done;
3990 
3991    if (!prelink_lowering(consts, exts, prog, linked_shader, num_linked_shaders))
3992       goto done;
3993 
3994    if (!gl_nir_link_varyings(consts, exts, api, prog))
3995       goto done;
3996 
3997    /* Validation for special cases where we allow sampler array indexing
3998     * with loop induction variable. This check emits a warning or error
3999     * depending if backend can handle dynamic indexing.
4000     */
4001    if ((!prog->IsES && prog->GLSL_Version < 130) ||
4002        (prog->IsES && prog->GLSL_Version < 300)) {
4003       if (!validate_sampler_array_indexing(consts, prog))
4004          goto done;
4005    }
4006 
4007    if (prog->data->LinkStatus == LINKING_FAILURE)
4008       goto done;
4009 
4010    if (!linked_shader[0]->Program->nir->info.io_lowered) {
4011       /* Linking the stages in the opposite order (from fragment to vertex)
4012        * ensures that inter-shader outputs written to in an earlier stage
4013        * are eliminated if they are (transitively) not used in a later
4014        * stage.
4015        */
4016       for (int i = num_linked_shaders - 2; i >= 0; i--) {
4017          gl_nir_link_opts(linked_shader[i]->Program->nir,
4018                           linked_shader[i + 1]->Program->nir);
4019       }
4020    }
4021 
4022    /* Tidy up any left overs from the linking process for single shaders.
4023     * For example varying arrays that get packed may have dead elements that
4024     * can be now be eliminated now that array access has been lowered.
4025     */
4026    if (num_linked_shaders == 1)
4027       gl_nir_opts(linked_shader[0]->Program->nir);
4028 
4029    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
4030       struct gl_linked_shader *shader = prog->_LinkedShaders[i];
4031       if (shader) {
4032          if (consts->GLSLLowerConstArrays) {
4033             nir_lower_const_arrays_to_uniforms(shader->Program->nir,
4034                                                consts->Program[i].MaxUniformComponents);
4035          }
4036 
4037          const nir_remove_dead_variables_options opts = {
4038             .can_remove_var = can_remove_var,
4039          };
4040          nir_remove_dead_variables(shader->Program->nir,
4041                                    nir_var_uniform | nir_var_image |
4042                                    nir_var_mem_ubo | nir_var_mem_ssbo |
4043                                    nir_var_system_value,
4044                                    &opts);
4045 
4046          if (shader->Program->info.stage == MESA_SHADER_FRAGMENT) {
4047             nir_shader *nir = shader->Program->nir;
4048             nir_foreach_variable_in_shader(var, nir) {
4049                if (var->data.mode == nir_var_system_value &&
4050                    (var->data.location == SYSTEM_VALUE_SAMPLE_ID ||
4051                     var->data.location == SYSTEM_VALUE_SAMPLE_POS))
4052                   nir->info.fs.uses_sample_shading = true;
4053 
4054                if (var->data.mode == nir_var_shader_in && var->data.sample)
4055                   nir->info.fs.uses_sample_shading = true;
4056 
4057                if (var->data.mode == nir_var_shader_out &&
4058                    var->data.fb_fetch_output)
4059                   nir->info.fs.uses_sample_shading = true;
4060             }
4061          }
4062       }
4063    }
4064 
4065    if (!gl_nir_link_uniform_blocks(consts, prog))
4066       goto done;
4067 
4068    if (!gl_nir_link_uniforms(consts, prog, true))
4069       goto done;
4070 
4071    link_util_calculate_subroutine_compat(prog);
4072    link_util_check_uniform_resources(consts, prog);
4073    link_util_check_subroutine_resources(prog);
4074    check_image_resources(consts, exts, prog);
4075    gl_nir_link_assign_atomic_counter_resources(consts, prog);
4076    gl_nir_link_check_atomic_counter_resources(consts, prog);
4077 
4078    /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both
4079     * be present in a linked program. GL_ARB_ES2_compatibility doesn't say
4080     * anything about shader linking when one of the shaders (vertex or
4081     * fragment shader) is absent. So, the extension shouldn't change the
4082     * behavior specified in GLSL specification.
4083     *
4084     * From OpenGL ES 3.1 specification (7.3 Program Objects):
4085     *     "Linking can fail for a variety of reasons as specified in the
4086     *     OpenGL ES Shading Language Specification, as well as any of the
4087     *     following reasons:
4088     *
4089     *     ...
4090     *
4091     *     * program contains objects to form either a vertex shader or
4092     *       fragment shader, and program is not separable, and does not
4093     *       contain objects to form both a vertex shader and fragment
4094     *       shader."
4095     *
4096     * However, the only scenario in 3.1+ where we don't require them both is
4097     * when we have a compute shader. For example:
4098     *
4099     * - No shaders is a link error.
4100     * - Geom or Tess without a Vertex shader is a link error which means we
4101     *   always require a Vertex shader and hence a Fragment shader.
4102     * - Finally a Compute shader linked with any other stage is a link error.
4103     */
4104    if (!prog->SeparateShader && _mesa_is_api_gles2(api) &&
4105        !prog->_LinkedShaders[MESA_SHADER_COMPUTE]) {
4106       if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
4107          linker_error(prog, "program lacks a vertex shader\n");
4108       } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
4109          linker_error(prog, "program lacks a fragment shader\n");
4110       }
4111    }
4112 
4113 done:
4114    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
4115       free(shader_list[i]);
4116    }
4117 
4118    ralloc_free(mem_ctx);
4119 
4120    if (prog->data->LinkStatus == LINKING_FAILURE)
4121       return false;
4122 
4123    return true;
4124 }
4125