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