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