nir: allow specifying filter callback in lower_alu_to_scalar
[mesa.git] / src / mesa / state_tracker / st_glsl_to_nir.cpp
1 /*
2 * Copyright © 2015 Red Hat
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "st_nir.h"
25
26 #include "pipe/p_defines.h"
27 #include "pipe/p_screen.h"
28 #include "pipe/p_context.h"
29
30 #include "program/program.h"
31 #include "program/prog_statevars.h"
32 #include "program/prog_parameter.h"
33 #include "program/ir_to_mesa.h"
34 #include "main/mtypes.h"
35 #include "main/errors.h"
36 #include "main/shaderapi.h"
37 #include "main/uniforms.h"
38
39 #include "main/shaderobj.h"
40 #include "st_context.h"
41 #include "st_glsl_types.h"
42 #include "st_program.h"
43
44 #include "compiler/nir/nir.h"
45 #include "compiler/glsl_types.h"
46 #include "compiler/glsl/glsl_to_nir.h"
47 #include "compiler/glsl/gl_nir.h"
48 #include "compiler/glsl/ir.h"
49 #include "compiler/glsl/ir_optimization.h"
50 #include "compiler/glsl/string_to_uint_map.h"
51
52 static int
53 type_size(const struct glsl_type *type)
54 {
55 return type->count_attribute_slots(false);
56 }
57
58 /* Depending on PIPE_CAP_TGSI_TEXCOORD (st->needs_texcoord_semantic) we
59 * may need to fix up varying slots so the glsl->nir path is aligned
60 * with the anything->tgsi->nir path.
61 */
62 static void
63 st_nir_fixup_varying_slots(struct st_context *st, struct exec_list *var_list)
64 {
65 if (st->needs_texcoord_semantic)
66 return;
67
68 nir_foreach_variable(var, var_list) {
69 if (var->data.location >= VARYING_SLOT_VAR0) {
70 var->data.location += 9;
71 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
72 (var->data.location <= VARYING_SLOT_TEX7)) {
73 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
74 }
75 }
76 }
77
78 /* input location assignment for VS inputs must be handled specially, so
79 * that it is aligned w/ st's vbo state.
80 * (This isn't the case with, for ex, FS inputs, which only need to agree
81 * on varying-slot w/ the VS outputs)
82 */
83 static void
84 st_nir_assign_vs_in_locations(nir_shader *nir)
85 {
86 nir->num_inputs = util_bitcount64(nir->info.inputs_read);
87 nir_foreach_variable_safe(var, &nir->inputs) {
88 /* NIR already assigns dual-slot inputs to two locations so all we have
89 * to do is compact everything down.
90 */
91 if (var->data.location == VERT_ATTRIB_EDGEFLAG) {
92 /* bit of a hack, mirroring st_translate_vertex_program */
93 var->data.driver_location = nir->num_inputs++;
94 } else if (nir->info.inputs_read & BITFIELD64_BIT(var->data.location)) {
95 var->data.driver_location =
96 util_bitcount64(nir->info.inputs_read &
97 BITFIELD64_MASK(var->data.location));
98 } else {
99 /* Move unused input variables to the globals list (with no
100 * initialization), to avoid confusing drivers looking through the
101 * inputs array and expecting to find inputs with a driver_location
102 * set.
103 */
104 exec_node_remove(&var->node);
105 var->data.mode = nir_var_shader_temp;
106 exec_list_push_tail(&nir->globals, &var->node);
107 }
108 }
109 }
110
111 static int
112 st_nir_lookup_parameter_index(const struct gl_program_parameter_list *params,
113 const char *name)
114 {
115 int loc = _mesa_lookup_parameter_index(params, name);
116
117 /* is there a better way to do this? If we have something like:
118 *
119 * struct S {
120 * float f;
121 * vec4 v;
122 * };
123 * uniform S color;
124 *
125 * Then what we get in prog->Parameters looks like:
126 *
127 * 0: Name=color.f, Type=6, DataType=1406, Size=1
128 * 1: Name=color.v, Type=6, DataType=8b52, Size=4
129 *
130 * So the name doesn't match up and _mesa_lookup_parameter_index()
131 * fails. In this case just find the first matching "color.*"..
132 *
133 * Note for arrays you could end up w/ color[n].f, for example.
134 *
135 * glsl_to_tgsi works slightly differently in this regard. It is
136 * emitting something more low level, so it just translates the
137 * params list 1:1 to CONST[] regs. Going from GLSL IR to TGSI,
138 * it just calculates the additional offset of struct field members
139 * in glsl_to_tgsi_visitor::visit(ir_dereference_record *ir) or
140 * glsl_to_tgsi_visitor::visit(ir_dereference_array *ir). It never
141 * needs to work backwards to get base var loc from the param-list
142 * which already has them separated out.
143 */
144 if (loc < 0) {
145 int namelen = strlen(name);
146 for (unsigned i = 0; i < params->NumParameters; i++) {
147 struct gl_program_parameter *p = &params->Parameters[i];
148 if ((strncmp(p->Name, name, namelen) == 0) &&
149 ((p->Name[namelen] == '.') || (p->Name[namelen] == '['))) {
150 loc = i;
151 break;
152 }
153 }
154 }
155
156 return loc;
157 }
158
159 static void
160 st_nir_assign_uniform_locations(struct gl_context *ctx,
161 struct gl_program *prog,
162 struct exec_list *uniform_list)
163 {
164 int shaderidx = 0;
165 int imageidx = 0;
166
167 nir_foreach_variable(uniform, uniform_list) {
168 int loc;
169
170 /*
171 * UBO's have their own address spaces, so don't count them towards the
172 * number of global uniforms
173 */
174 if (uniform->data.mode == nir_var_mem_ubo || uniform->data.mode == nir_var_mem_ssbo)
175 continue;
176
177 const struct glsl_type *type = glsl_without_array(uniform->type);
178 if (!uniform->data.bindless && (type->is_sampler() || type->is_image())) {
179 if (type->is_sampler()) {
180 loc = shaderidx;
181 shaderidx += type_size(uniform->type);
182 } else {
183 loc = imageidx;
184 imageidx += type_size(uniform->type);
185 }
186 } else if (strncmp(uniform->name, "gl_", 3) == 0) {
187 const gl_state_index16 *const stateTokens = uniform->state_slots[0].tokens;
188 /* This state reference has already been setup by ir_to_mesa, but we'll
189 * get the same index back here.
190 */
191
192 unsigned comps;
193 if (glsl_type_is_struct_or_ifc(type)) {
194 comps = 4;
195 } else {
196 comps = glsl_get_vector_elements(type);
197 }
198
199 if (ctx->Const.PackedDriverUniformStorage) {
200 loc = _mesa_add_sized_state_reference(prog->Parameters,
201 stateTokens, comps, false);
202 loc = prog->Parameters->ParameterValueOffset[loc];
203 } else {
204 loc = _mesa_add_state_reference(prog->Parameters, stateTokens);
205 }
206 } else {
207 loc = st_nir_lookup_parameter_index(prog->Parameters, uniform->name);
208
209 /* We need to check that loc is not -1 here before accessing the
210 * array. It can be negative for example when we have a struct that
211 * only contains opaque types.
212 */
213 if (loc >= 0 && ctx->Const.PackedDriverUniformStorage) {
214 loc = prog->Parameters->ParameterValueOffset[loc];
215 }
216 }
217
218 uniform->data.driver_location = loc;
219 }
220 }
221
222 void
223 st_nir_opts(nir_shader *nir, bool scalar)
224 {
225 bool progress;
226 unsigned lower_flrp =
227 (nir->options->lower_flrp16 ? 16 : 0) |
228 (nir->options->lower_flrp32 ? 32 : 0) |
229 (nir->options->lower_flrp64 ? 64 : 0);
230
231 do {
232 progress = false;
233
234 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
235
236 /* Linking deals with unused inputs/outputs, but here we can remove
237 * things local to the shader in the hopes that we can cleanup other
238 * things. This pass will also remove variables with only stores, so we
239 * might be able to make progress after it.
240 */
241 NIR_PASS(progress, nir, nir_remove_dead_variables,
242 (nir_variable_mode)(nir_var_function_temp |
243 nir_var_shader_temp |
244 nir_var_mem_shared));
245
246 NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
247 NIR_PASS(progress, nir, nir_opt_dead_write_vars);
248
249 if (scalar) {
250 NIR_PASS_V(nir, nir_lower_alu_to_scalar, NULL, NULL);
251 NIR_PASS_V(nir, nir_lower_phis_to_scalar);
252 }
253
254 NIR_PASS_V(nir, nir_lower_alu);
255 NIR_PASS_V(nir, nir_lower_pack);
256 NIR_PASS(progress, nir, nir_copy_prop);
257 NIR_PASS(progress, nir, nir_opt_remove_phis);
258 NIR_PASS(progress, nir, nir_opt_dce);
259 if (nir_opt_trivial_continues(nir)) {
260 progress = true;
261 NIR_PASS(progress, nir, nir_copy_prop);
262 NIR_PASS(progress, nir, nir_opt_dce);
263 }
264 NIR_PASS(progress, nir, nir_opt_if, false);
265 NIR_PASS(progress, nir, nir_opt_dead_cf);
266 NIR_PASS(progress, nir, nir_opt_cse);
267 NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
268
269 NIR_PASS(progress, nir, nir_opt_algebraic);
270 NIR_PASS(progress, nir, nir_opt_constant_folding);
271
272 if (lower_flrp != 0) {
273 bool lower_flrp_progress = false;
274
275 NIR_PASS(lower_flrp_progress, nir, nir_lower_flrp,
276 lower_flrp,
277 false /* always_precise */,
278 nir->options->lower_ffma);
279 if (lower_flrp_progress) {
280 NIR_PASS(progress, nir,
281 nir_opt_constant_folding);
282 progress = true;
283 }
284
285 /* Nothing should rematerialize any flrps, so we only need to do this
286 * lowering once.
287 */
288 lower_flrp = 0;
289 }
290
291 NIR_PASS(progress, nir, gl_nir_opt_access);
292
293 NIR_PASS(progress, nir, nir_opt_undef);
294 NIR_PASS(progress, nir, nir_opt_conditional_discard);
295 if (nir->options->max_unroll_iterations) {
296 NIR_PASS(progress, nir, nir_opt_loop_unroll, (nir_variable_mode)0);
297 }
298 } while (progress);
299 }
300
301 /* First third of converting glsl_to_nir.. this leaves things in a pre-
302 * nir_lower_io state, so that shader variants can more easily insert/
303 * replace variables, etc.
304 */
305 static nir_shader *
306 st_glsl_to_nir(struct st_context *st, struct gl_program *prog,
307 struct gl_shader_program *shader_program,
308 gl_shader_stage stage)
309 {
310 const nir_shader_compiler_options *options =
311 st->ctx->Const.ShaderCompilerOptions[prog->info.stage].NirOptions;
312 enum pipe_shader_type type = pipe_shader_type_from_mesa(stage);
313 struct pipe_screen *screen = st->pipe->screen;
314 bool is_scalar = screen->get_shader_param(screen, type, PIPE_SHADER_CAP_SCALAR_ISA);
315 assert(options);
316 bool lower_64bit =
317 options->lower_int64_options || options->lower_doubles_options;
318
319 if (prog->nir)
320 return prog->nir;
321
322 nir_shader *nir = glsl_to_nir(st->ctx, shader_program, stage, options);
323
324 /* Set the next shader stage hint for VS and TES. */
325 if (!nir->info.separate_shader &&
326 (nir->info.stage == MESA_SHADER_VERTEX ||
327 nir->info.stage == MESA_SHADER_TESS_EVAL)) {
328
329 unsigned prev_stages = (1 << (prog->info.stage + 1)) - 1;
330 unsigned stages_mask =
331 ~prev_stages & shader_program->data->linked_stages;
332
333 nir->info.next_stage = stages_mask ?
334 (gl_shader_stage) u_bit_scan(&stages_mask) : MESA_SHADER_FRAGMENT;
335 } else {
336 nir->info.next_stage = MESA_SHADER_FRAGMENT;
337 }
338
339 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
340 if (!st->ctx->SoftFP64 && nir->info.uses_64bit &&
341 (options->lower_doubles_options & nir_lower_fp64_full_software) != 0) {
342 st->ctx->SoftFP64 = glsl_float64_funcs_to_nir(st->ctx, options);
343 }
344
345 nir_variable_mode mask =
346 (nir_variable_mode) (nir_var_shader_in | nir_var_shader_out);
347 nir_remove_dead_variables(nir, mask);
348
349 if (options->lower_all_io_to_temps ||
350 nir->info.stage == MESA_SHADER_VERTEX ||
351 nir->info.stage == MESA_SHADER_GEOMETRY) {
352 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
353 nir_shader_get_entrypoint(nir),
354 true, true);
355 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
356 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
357 nir_shader_get_entrypoint(nir),
358 true, false);
359 }
360
361 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
362 NIR_PASS_V(nir, nir_split_var_copies);
363 NIR_PASS_V(nir, nir_lower_var_copies);
364
365 if (is_scalar) {
366 NIR_PASS_V(nir, nir_lower_alu_to_scalar, NULL, NULL);
367 }
368
369 /* before buffers and vars_to_ssa */
370 NIR_PASS_V(nir, gl_nir_lower_bindless_images);
371 st_nir_opts(nir, is_scalar);
372
373 NIR_PASS_V(nir, gl_nir_lower_buffers, shader_program);
374 /* Do a round of constant folding to clean up address calculations */
375 NIR_PASS_V(nir, nir_opt_constant_folding);
376
377 if (lower_64bit) {
378 bool lowered_64bit_ops = false;
379 if (options->lower_doubles_options) {
380 NIR_PASS(lowered_64bit_ops, nir, nir_lower_doubles,
381 st->ctx->SoftFP64, options->lower_doubles_options);
382 }
383 if (options->lower_int64_options) {
384 NIR_PASS(lowered_64bit_ops, nir, nir_lower_int64,
385 options->lower_int64_options);
386 }
387
388 if (lowered_64bit_ops)
389 st_nir_opts(nir, is_scalar);
390 }
391
392 return nir;
393 }
394
395 /* Second third of converting glsl_to_nir. This creates uniforms, gathers
396 * info on varyings, etc after NIR link time opts have been applied.
397 */
398 static void
399 st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,
400 struct gl_shader_program *shader_program)
401 {
402 nir_shader *nir = prog->nir;
403
404 /* Make a pass over the IR to add state references for any built-in
405 * uniforms that are used. This has to be done now (during linking).
406 * Code generation doesn't happen until the first time this shader is
407 * used for rendering. Waiting until then to generate the parameters is
408 * too late. At that point, the values for the built-in uniforms won't
409 * get sent to the shader.
410 */
411 nir_foreach_variable(var, &nir->uniforms) {
412 if (strncmp(var->name, "gl_", 3) == 0) {
413 const nir_state_slot *const slots = var->state_slots;
414 assert(var->state_slots != NULL);
415
416 const struct glsl_type *type = glsl_without_array(var->type);
417 for (unsigned int i = 0; i < var->num_state_slots; i++) {
418 unsigned comps;
419 if (glsl_type_is_struct_or_ifc(type)) {
420 /* Builtin struct require specical handling for now we just
421 * make all members vec4. See st_nir_lower_builtin.
422 */
423 comps = 4;
424 } else {
425 comps = glsl_get_vector_elements(type);
426 }
427
428 if (st->ctx->Const.PackedDriverUniformStorage) {
429 _mesa_add_sized_state_reference(prog->Parameters,
430 slots[i].tokens,
431 comps, false);
432 } else {
433 _mesa_add_state_reference(prog->Parameters,
434 slots[i].tokens);
435 }
436 }
437 }
438 }
439
440 /* Avoid reallocation of the program parameter list, because the uniform
441 * storage is only associated with the original parameter list.
442 * This should be enough for Bitmap and DrawPixels constants.
443 */
444 _mesa_reserve_parameter_storage(prog->Parameters, 8);
445
446 /* This has to be done last. Any operation the can cause
447 * prog->ParameterValues to get reallocated (e.g., anything that adds a
448 * program constant) has to happen before creating this linkage.
449 */
450 _mesa_associate_uniform_storage(st->ctx, shader_program, prog);
451
452 st_set_prog_affected_state_flags(prog);
453
454 NIR_PASS_V(nir, st_nir_lower_builtin);
455 NIR_PASS_V(nir, gl_nir_lower_atomics, shader_program, true);
456 NIR_PASS_V(nir, nir_opt_intrinsics);
457
458 nir_variable_mode mask = nir_var_function_temp;
459 nir_remove_dead_variables(nir, mask);
460
461 if (st->ctx->_Shader->Flags & GLSL_DUMP) {
462 _mesa_log("\n");
463 _mesa_log("NIR IR for linked %s program %d:\n",
464 _mesa_shader_stage_to_string(prog->info.stage),
465 shader_program->Name);
466 nir_print_shader(nir, _mesa_get_log_file());
467 _mesa_log("\n\n");
468 }
469 }
470
471 static void
472 set_st_program(struct gl_program *prog,
473 struct gl_shader_program *shader_program,
474 nir_shader *nir)
475 {
476 struct st_vertex_program *stvp;
477 struct st_common_program *stp;
478 struct st_fragment_program *stfp;
479 struct st_compute_program *stcp;
480
481 switch (prog->info.stage) {
482 case MESA_SHADER_VERTEX:
483 stvp = (struct st_vertex_program *)prog;
484 stvp->shader_program = shader_program;
485 stvp->tgsi.type = PIPE_SHADER_IR_NIR;
486 stvp->tgsi.ir.nir = nir;
487 break;
488 case MESA_SHADER_GEOMETRY:
489 case MESA_SHADER_TESS_CTRL:
490 case MESA_SHADER_TESS_EVAL:
491 stp = (struct st_common_program *)prog;
492 stp->shader_program = shader_program;
493 stp->tgsi.type = PIPE_SHADER_IR_NIR;
494 stp->tgsi.ir.nir = nir;
495 break;
496 case MESA_SHADER_FRAGMENT:
497 stfp = (struct st_fragment_program *)prog;
498 stfp->shader_program = shader_program;
499 stfp->tgsi.type = PIPE_SHADER_IR_NIR;
500 stfp->tgsi.ir.nir = nir;
501 break;
502 case MESA_SHADER_COMPUTE:
503 stcp = (struct st_compute_program *)prog;
504 stcp->shader_program = shader_program;
505 stcp->tgsi.ir_type = PIPE_SHADER_IR_NIR;
506 stcp->tgsi.prog = nir;
507 break;
508 default:
509 unreachable("unknown shader stage");
510 }
511 }
512
513 static void
514 st_nir_get_mesa_program(struct gl_context *ctx,
515 struct gl_shader_program *shader_program,
516 struct gl_linked_shader *shader)
517 {
518 struct st_context *st = st_context(ctx);
519 struct pipe_screen *pscreen = ctx->st->pipe->screen;
520 struct gl_program *prog;
521
522 validate_ir_tree(shader->ir);
523
524 prog = shader->Program;
525
526 prog->Parameters = _mesa_new_parameter_list();
527
528 _mesa_copy_linked_program_data(shader_program, shader);
529 _mesa_generate_parameters_list_for_uniforms(ctx, shader_program, shader,
530 prog->Parameters);
531
532 /* Remove reads from output registers. */
533 if (!pscreen->get_param(pscreen, PIPE_CAP_TGSI_CAN_READ_OUTPUTS))
534 lower_output_reads(shader->Stage, shader->ir);
535
536 if (ctx->_Shader->Flags & GLSL_DUMP) {
537 _mesa_log("\n");
538 _mesa_log("GLSL IR for linked %s program %d:\n",
539 _mesa_shader_stage_to_string(shader->Stage),
540 shader_program->Name);
541 _mesa_print_ir(_mesa_get_log_file(), shader->ir, NULL);
542 _mesa_log("\n\n");
543 }
544
545 prog->ExternalSamplersUsed = gl_external_samplers(prog);
546 _mesa_update_shader_textures_used(shader_program, prog);
547
548 nir_shader *nir = st_glsl_to_nir(st, prog, shader_program, shader->Stage);
549
550 set_st_program(prog, shader_program, nir);
551 prog->nir = nir;
552 }
553
554 static void
555 st_nir_vectorize_io(nir_shader *producer, nir_shader *consumer)
556 {
557 NIR_PASS_V(producer, nir_lower_io_to_vector, nir_var_shader_out);
558 NIR_PASS_V(producer, nir_opt_combine_stores, nir_var_shader_out);
559 NIR_PASS_V(consumer, nir_lower_io_to_vector, nir_var_shader_in);
560
561 if ((producer)->info.stage != MESA_SHADER_TESS_CTRL) {
562 /* Calling lower_io_to_vector creates output variable writes with
563 * write-masks. We only support these for TCS outputs, so for other
564 * stages, we need to call nir_lower_io_to_temporaries to get rid of
565 * them. This, in turn, creates temporary variables and extra
566 * copy_deref intrinsics that we need to clean up.
567 */
568 NIR_PASS_V(producer, nir_lower_io_to_temporaries,
569 nir_shader_get_entrypoint(producer), true, false);
570 NIR_PASS_V(producer, nir_lower_global_vars_to_local);
571 NIR_PASS_V(producer, nir_split_var_copies);
572 NIR_PASS_V(producer, nir_lower_var_copies);
573 }
574 }
575
576 static void
577 st_nir_link_shaders(nir_shader **producer, nir_shader **consumer, bool scalar)
578 {
579 if (scalar) {
580 NIR_PASS_V(*producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
581 NIR_PASS_V(*consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
582 }
583
584 nir_lower_io_arrays_to_elements(*producer, *consumer);
585
586 st_nir_opts(*producer, scalar);
587 st_nir_opts(*consumer, scalar);
588
589 if (nir_link_opt_varyings(*producer, *consumer))
590 st_nir_opts(*consumer, scalar);
591
592 NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
593 NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
594
595 if (nir_remove_unused_varyings(*producer, *consumer)) {
596 NIR_PASS_V(*producer, nir_lower_global_vars_to_local);
597 NIR_PASS_V(*consumer, nir_lower_global_vars_to_local);
598
599 st_nir_opts(*producer, scalar);
600 st_nir_opts(*consumer, scalar);
601
602 /* Optimizations can cause varyings to become unused.
603 * nir_compact_varyings() depends on all dead varyings being removed so
604 * we need to call nir_remove_dead_variables() again here.
605 */
606 NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
607 NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
608 }
609 }
610
611 static void
612 st_lower_patch_vertices_in(struct gl_shader_program *shader_prog)
613 {
614 struct gl_linked_shader *linked_tcs =
615 shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL];
616 struct gl_linked_shader *linked_tes =
617 shader_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
618
619 /* If we have a TCS and TES linked together, lower TES patch vertices. */
620 if (linked_tcs && linked_tes) {
621 nir_shader *tcs_nir = linked_tcs->Program->nir;
622 nir_shader *tes_nir = linked_tes->Program->nir;
623
624 /* The TES input vertex count is the TCS output vertex count,
625 * lower TES gl_PatchVerticesIn to a constant.
626 */
627 uint32_t tes_patch_verts = tcs_nir->info.tess.tcs_vertices_out;
628 NIR_PASS_V(tes_nir, nir_lower_patch_vertices, tes_patch_verts, NULL);
629 }
630 }
631
632 extern "C" {
633
634 void
635 st_nir_lower_wpos_ytransform(struct nir_shader *nir,
636 struct gl_program *prog,
637 struct pipe_screen *pscreen)
638 {
639 if (nir->info.stage != MESA_SHADER_FRAGMENT)
640 return;
641
642 static const gl_state_index16 wposTransformState[STATE_LENGTH] = {
643 STATE_INTERNAL, STATE_FB_WPOS_Y_TRANSFORM
644 };
645 nir_lower_wpos_ytransform_options wpos_options = { { 0 } };
646
647 memcpy(wpos_options.state_tokens, wposTransformState,
648 sizeof(wpos_options.state_tokens));
649 wpos_options.fs_coord_origin_upper_left =
650 pscreen->get_param(pscreen,
651 PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT);
652 wpos_options.fs_coord_origin_lower_left =
653 pscreen->get_param(pscreen,
654 PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT);
655 wpos_options.fs_coord_pixel_center_integer =
656 pscreen->get_param(pscreen,
657 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER);
658 wpos_options.fs_coord_pixel_center_half_integer =
659 pscreen->get_param(pscreen,
660 PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER);
661
662 if (nir_lower_wpos_ytransform(nir, &wpos_options)) {
663 nir_validate_shader(nir, "after nir_lower_wpos_ytransform");
664 _mesa_add_state_reference(prog->Parameters, wposTransformState);
665 }
666 }
667
668 bool
669 st_link_nir(struct gl_context *ctx,
670 struct gl_shader_program *shader_program)
671 {
672 struct st_context *st = st_context(ctx);
673 struct pipe_screen *screen = st->pipe->screen;
674 bool is_scalar[MESA_SHADER_STAGES];
675
676 unsigned last_stage = 0;
677 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
678 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
679 if (shader == NULL)
680 continue;
681
682 /* Determine scalar property of each shader stage */
683 enum pipe_shader_type type = pipe_shader_type_from_mesa(shader->Stage);
684 is_scalar[i] = screen->get_shader_param(screen, type,
685 PIPE_SHADER_CAP_SCALAR_ISA);
686
687 st_nir_get_mesa_program(ctx, shader_program, shader);
688 last_stage = i;
689
690 if (is_scalar[i]) {
691 NIR_PASS_V(shader->Program->nir, nir_lower_load_const_to_scalar);
692 }
693 }
694
695 /* Linking the stages in the opposite order (from fragment to vertex)
696 * ensures that inter-shader outputs written to in an earlier stage
697 * are eliminated if they are (transitively) not used in a later
698 * stage.
699 */
700 int next = last_stage;
701 for (int i = next - 1; i >= 0; i--) {
702 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
703 if (shader == NULL)
704 continue;
705
706 st_nir_link_shaders(&shader->Program->nir,
707 &shader_program->_LinkedShaders[next]->Program->nir,
708 is_scalar[i]);
709 next = i;
710 }
711
712 int prev = -1;
713 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
714 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
715 if (shader == NULL)
716 continue;
717
718 nir_shader *nir = shader->Program->nir;
719
720 NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, shader->Program,
721 st->pipe->screen);
722
723 NIR_PASS_V(nir, nir_lower_system_values);
724 NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
725
726 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
727 shader->Program->info = nir->info;
728 if (i == MESA_SHADER_VERTEX) {
729 /* NIR expands dual-slot inputs out to two locations. We need to
730 * compact things back down GL-style single-slot inputs to avoid
731 * confusing the state tracker.
732 */
733 shader->Program->info.inputs_read =
734 nir_get_single_slot_attribs_mask(nir->info.inputs_read,
735 shader->Program->DualSlotInputs);
736 }
737
738 if (prev != -1) {
739 struct gl_program *prev_shader =
740 shader_program->_LinkedShaders[prev]->Program;
741
742 /* We can't use nir_compact_varyings with transform feedback, since
743 * the pipe_stream_output->output_register field is based on the
744 * pre-compacted driver_locations.
745 */
746 if (!(prev_shader->sh.LinkedTransformFeedback &&
747 prev_shader->sh.LinkedTransformFeedback->NumVarying > 0))
748 nir_compact_varyings(shader_program->_LinkedShaders[prev]->Program->nir,
749 nir, ctx->API != API_OPENGL_COMPAT);
750
751 if (ctx->Const.ShaderCompilerOptions[i].NirOptions->vectorize_io)
752 st_nir_vectorize_io(prev_shader->nir, nir);
753 }
754 prev = i;
755 }
756
757 st_lower_patch_vertices_in(shader_program);
758
759 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
760 struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
761 if (shader == NULL)
762 continue;
763
764 st_glsl_to_nir_post_opts(st, shader->Program, shader_program);
765
766 assert(shader->Program);
767 if (!ctx->Driver.ProgramStringNotify(ctx,
768 _mesa_shader_stage_to_program(i),
769 shader->Program)) {
770 _mesa_reference_program(ctx, &shader->Program, NULL);
771 return false;
772 }
773
774 nir_sweep(shader->Program->nir);
775
776 /* The GLSL IR won't be needed anymore. */
777 ralloc_free(shader->ir);
778 shader->ir = NULL;
779 }
780
781 return true;
782 }
783
784 void
785 st_nir_assign_varying_locations(struct st_context *st, nir_shader *nir)
786 {
787 if (nir->info.stage == MESA_SHADER_VERTEX) {
788 /* Needs special handling so drvloc matches the vbo state: */
789 st_nir_assign_vs_in_locations(nir);
790 /* Re-lower global vars, to deal with any dead VS inputs. */
791 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
792
793 nir_assign_io_var_locations(&nir->outputs,
794 &nir->num_outputs,
795 nir->info.stage);
796 st_nir_fixup_varying_slots(st, &nir->outputs);
797 } else if (nir->info.stage == MESA_SHADER_GEOMETRY ||
798 nir->info.stage == MESA_SHADER_TESS_CTRL ||
799 nir->info.stage == MESA_SHADER_TESS_EVAL) {
800 nir_assign_io_var_locations(&nir->inputs,
801 &nir->num_inputs,
802 nir->info.stage);
803 st_nir_fixup_varying_slots(st, &nir->inputs);
804
805 nir_assign_io_var_locations(&nir->outputs,
806 &nir->num_outputs,
807 nir->info.stage);
808 st_nir_fixup_varying_slots(st, &nir->outputs);
809 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
810 nir_assign_io_var_locations(&nir->inputs,
811 &nir->num_inputs,
812 nir->info.stage);
813 st_nir_fixup_varying_slots(st, &nir->inputs);
814 nir_assign_io_var_locations(&nir->outputs,
815 &nir->num_outputs,
816 nir->info.stage);
817 } else if (nir->info.stage == MESA_SHADER_COMPUTE) {
818 /* TODO? */
819 } else {
820 unreachable("invalid shader type");
821 }
822 }
823
824 void
825 st_nir_lower_samplers(struct pipe_screen *screen, nir_shader *nir,
826 struct gl_shader_program *shader_program,
827 struct gl_program *prog)
828 {
829 if (screen->get_param(screen, PIPE_CAP_NIR_SAMPLERS_AS_DEREF))
830 NIR_PASS_V(nir, gl_nir_lower_samplers_as_deref, shader_program);
831 else
832 NIR_PASS_V(nir, gl_nir_lower_samplers, shader_program);
833
834 if (prog) {
835 prog->info.textures_used = nir->info.textures_used;
836 prog->info.textures_used_by_txf = nir->info.textures_used_by_txf;
837 }
838 }
839
840 /* Last third of preparing nir from glsl, which happens after shader
841 * variant lowering.
842 */
843 void
844 st_finalize_nir(struct st_context *st, struct gl_program *prog,
845 struct gl_shader_program *shader_program, nir_shader *nir)
846 {
847 struct pipe_screen *screen = st->pipe->screen;
848 const nir_shader_compiler_options *options =
849 st->ctx->Const.ShaderCompilerOptions[prog->info.stage].NirOptions;
850
851 NIR_PASS_V(nir, nir_split_var_copies);
852 NIR_PASS_V(nir, nir_lower_var_copies);
853 if (options->lower_all_io_to_temps ||
854 options->lower_all_io_to_elements ||
855 nir->info.stage == MESA_SHADER_VERTEX ||
856 nir->info.stage == MESA_SHADER_GEOMETRY) {
857 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
858 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
859 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, true);
860 }
861
862 st_nir_assign_varying_locations(st, nir);
863
864 NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
865 st->ctx->Const.Program[nir->info.stage].MaxAtomicBuffers);
866
867 st_nir_assign_uniform_locations(st->ctx, prog,
868 &nir->uniforms);
869
870 /* Set num_uniforms in number of attribute slots (vec4s) */
871 nir->num_uniforms = DIV_ROUND_UP(prog->Parameters->NumParameterValues, 4);
872
873 if (st->ctx->Const.PackedDriverUniformStorage) {
874 NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,
875 (nir_lower_io_options)0);
876 NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 4);
877 } else {
878 NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_uniforms_type_size,
879 (nir_lower_io_options)0);
880 }
881
882 st_nir_lower_samplers(screen, nir, shader_program, prog);
883 }
884
885 } /* extern "C" */