summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/nir/nir_to_tgsi_info.c
blob: 4dbf252c89f6749158a6315159cb486d3a962634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
 * Copyright 2017 Advanced Micro Devices, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * This is ported mostly out of radeonsi, if we can drop TGSI, we can likely
 * make a lot this go away.
 */

#include "nir_to_tgsi_info.h"
#include "util/u_math.h"
#include "util/u_prim.h"
#include "nir.h"
#include "nir_deref.h"
#include "tgsi/tgsi_scan.h"
#include "tgsi/tgsi_from_mesa.h"

static nir_variable* tex_get_texture_var(const nir_tex_instr *instr)
{
   for (unsigned i = 0; i < instr->num_srcs; i++) {
      switch (instr->src[i].src_type) {
      case nir_tex_src_texture_deref:
         return nir_deref_instr_get_variable(nir_src_as_deref(instr->src[i].src));
      default:
         break;
      }
   }

   return NULL;
}

static nir_variable* intrinsic_get_var(const nir_intrinsic_instr *instr)
{
   return nir_deref_instr_get_variable(nir_src_as_deref(instr->src[0]));
}


static void gather_usage_helper(const nir_deref_instr **deref_ptr,
                                unsigned location,
                                uint8_t mask,
                                uint8_t *usage_mask)
{
   for (; *deref_ptr; deref_ptr++) {
      const nir_deref_instr *deref = *deref_ptr;
      switch (deref->deref_type) {
      case nir_deref_type_array: {
         bool is_compact = nir_deref_instr_get_variable(deref)->data.compact;
         unsigned elem_size = is_compact ? DIV_ROUND_UP(glsl_get_length(deref->type), 4) :
            glsl_count_attribute_slots(deref->type, false);
         if (nir_src_is_const(deref->arr.index)) {
            if (is_compact) {
               location += nir_src_as_uint(deref->arr.index) / 4;
               mask <<= nir_src_as_uint(deref->arr.index) % 4;
            } else
               location += elem_size * nir_src_as_uint(deref->arr.index);
         } else {
            unsigned array_elems =
               glsl_get_length(deref_ptr[-1]->type);
            for (unsigned i = 0; i < array_elems; i++) {
               gather_usage_helper(deref_ptr + 1,
                                   location + elem_size * i,
                                   mask, usage_mask);
            }
            return;
         }
         break;
      }
      case nir_deref_type_struct: {
         const struct glsl_type *parent_type =
            deref_ptr[-1]->type;
         unsigned index = deref->strct.index;
         for (unsigned i = 0; i < index; i++) {
            const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
            location += glsl_count_attribute_slots(ft, false);
         }
         break;
      }
      default:
         unreachable("Unhandled deref type in gather_components_used_helper");
      }
   }

   usage_mask[location] |= mask & 0xf;
   if (mask & 0xf0)
      usage_mask[location + 1] |= (mask >> 4) & 0xf;
}

static void gather_usage(const nir_deref_instr *deref,
                         uint8_t mask,
                         uint8_t *usage_mask)
{
   nir_deref_path path;
   nir_deref_path_init(&path, (nir_deref_instr *)deref, NULL);

   unsigned location_frac = path.path[0]->var->data.location_frac;
   if (glsl_type_is_64bit(deref->type)) {
      uint8_t new_mask = 0;
      for (unsigned i = 0; i < 4; i++) {
         if (mask & (1 << i))
            new_mask |= 0x3 << (2 * i);
      }
      mask = new_mask << location_frac;
   } else {
      mask <<= location_frac;
      mask &= 0xf;
   }

   gather_usage_helper((const nir_deref_instr **)&path.path[1],
                       path.path[0]->var->data.driver_location,
                       mask, usage_mask);

   nir_deref_path_finish(&path);
}

static void gather_intrinsic_load_deref_info(const nir_shader *nir,
                                             const nir_intrinsic_instr *instr,
                                             const nir_deref_instr *deref,
                                             bool need_texcoord,
                                             const nir_variable *var,
                                             struct tgsi_shader_info *info)
{
   assert(var && var->data.mode == nir_var_shader_in);

   if (nir->info.stage == MESA_SHADER_FRAGMENT)
      gather_usage(deref, nir_ssa_def_components_read(&instr->dest.ssa),
                   info->input_usage_mask);

   switch (nir->info.stage) {
   case MESA_SHADER_VERTEX: {

      break;
   }
   default: {
      unsigned semantic_name, semantic_index;
      tgsi_get_gl_varying_semantic(var->data.location, need_texcoord,
                                   &semantic_name, &semantic_index);

      if (semantic_name == TGSI_SEMANTIC_COLOR) {
         uint8_t mask = nir_ssa_def_components_read(&instr->dest.ssa);
         info->colors_read |= mask << (semantic_index * 4);
      }
      if (semantic_name == TGSI_SEMANTIC_FACE) {
         info->uses_frontface = true;
      }
      break;
   }
   }
}

static void scan_instruction(const struct nir_shader *nir,
                             bool need_texcoord,
                             struct tgsi_shader_info *info,
                             const nir_instr *instr)
{
   info->num_tokens = 2; /* indicate that the shader is non-empty */
   info->num_instructions = 2;

   if (instr->type == nir_instr_type_alu) {
      const nir_alu_instr *alu = nir_instr_as_alu(instr);

      switch (alu->op) {
      case nir_op_fddx:
      case nir_op_fddy:
      case nir_op_fddx_fine:
      case nir_op_fddy_fine:
      case nir_op_fddx_coarse:
      case nir_op_fddy_coarse:
         info->uses_derivatives = true;
         break;
      default:
         break;
      }
   } else if (instr->type == nir_instr_type_tex) {
      nir_tex_instr *tex = nir_instr_as_tex(instr);
      const nir_variable *texture = tex_get_texture_var(tex);

      if (texture && texture->data.bindless)
         info->uses_bindless_samplers = true;

      switch (tex->op) {
      case nir_texop_tex:
      case nir_texop_txb:
      case nir_texop_lod:
         info->uses_derivatives = true;
         break;
      default:
         break;
      }
   } else if (instr->type == nir_instr_type_intrinsic) {
      nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);

      switch (intr->intrinsic) {
      case nir_intrinsic_load_front_face:
         info->uses_frontface = 1;
         break;
      case nir_intrinsic_load_instance_id:
         info->uses_instanceid = 1;
         break;
      case nir_intrinsic_load_invocation_id:
         info->uses_invocationid = true;
         break;
      case nir_intrinsic_load_num_workgroups:
         info->uses_grid_size = true;
         break;
      case nir_intrinsic_load_workgroup_size:
         /* The block size is translated to IMM with a fixed block size. */
         if (info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] == 0)
            info->uses_block_size = true;
         break;
      case nir_intrinsic_load_local_invocation_id:
      case nir_intrinsic_load_workgroup_id: {
         unsigned mask = nir_ssa_def_components_read(&intr->dest.ssa);
         while (mask) {
            unsigned i = u_bit_scan(&mask);

            if (intr->intrinsic == nir_intrinsic_load_workgroup_id)
               info->uses_block_id[i] = true;
            else
               info->uses_thread_id[i] = true;
         }
         break;
      }
      case nir_intrinsic_load_vertex_id:
         info->uses_vertexid = 1;
         break;
      case nir_intrinsic_load_vertex_id_zero_base:
         info->uses_vertexid_nobase = 1;
         break;
      case nir_intrinsic_load_base_vertex:
         info->uses_basevertex = 1;
         break;
      case nir_intrinsic_load_draw_id:
         info->uses_drawid = 1;
         break;
      case nir_intrinsic_load_primitive_id:
         info->uses_primid = 1;
         break;
      case nir_intrinsic_load_sample_mask_in:
         info->reads_samplemask = true;
         break;
      case nir_intrinsic_load_tess_level_inner:
      case nir_intrinsic_load_tess_level_outer:
         info->reads_tess_factors = true;
         break;
      case nir_intrinsic_bindless_image_load:
         info->uses_bindless_images = true;

         if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
            info->uses_bindless_buffer_load = true;
         else
            info->uses_bindless_image_load = true;
         break;
      case nir_intrinsic_bindless_image_size:
      case nir_intrinsic_bindless_image_samples:
         info->uses_bindless_images = true;
         break;
      case nir_intrinsic_bindless_image_store:
         info->uses_bindless_images = true;

         if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
            info->uses_bindless_buffer_store = true;
         else
            info->uses_bindless_image_store = true;

         info->writes_memory = true;
         break;
      case nir_intrinsic_image_deref_store:
      case nir_intrinsic_image_store:
         info->writes_memory = true;
         break;
      case nir_intrinsic_bindless_image_atomic:
      case nir_intrinsic_bindless_image_atomic_swap:
         info->uses_bindless_images = true;

         if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
            info->uses_bindless_buffer_atomic = true;
         else
            info->uses_bindless_image_atomic = true;

         info->writes_memory = true;
         break;
      case nir_intrinsic_image_deref_atomic:
      case nir_intrinsic_image_deref_atomic_swap:
      case nir_intrinsic_image_atomic:
      case nir_intrinsic_image_atomic_swap:
         info->writes_memory = true;
         break;
      case nir_intrinsic_store_ssbo:
      case nir_intrinsic_ssbo_atomic:
      case nir_intrinsic_ssbo_atomic_swap:
         info->writes_memory = true;
         break;
      case nir_intrinsic_load_deref: {
         const nir_variable *var = intrinsic_get_var(intr);
         const nir_variable_mode mode = var->data.mode;
         nir_deref_instr *const deref = nir_src_as_deref(intr->src[0]);
         enum glsl_base_type base_type =
            glsl_get_base_type(glsl_without_array(var->type));

         if (nir_deref_instr_has_indirect(deref)) {
            if (mode == nir_var_shader_in)
               info->indirect_files |= (1 << TGSI_FILE_INPUT);
         }
         if (mode == nir_var_shader_in) {
            gather_intrinsic_load_deref_info(nir, intr, deref, need_texcoord, var, info);

            switch (var->data.interpolation) {
            case INTERP_MODE_NONE:
               if (glsl_base_type_is_integer(base_type))
                  break;

               FALLTHROUGH;
            case INTERP_MODE_SMOOTH:
               if (var->data.sample)
                  info->uses_persp_sample = true;
               else if (var->data.centroid)
                  info->uses_persp_centroid = true;
               else
                  info->uses_persp_center = true;
               break;

            case INTERP_MODE_NOPERSPECTIVE:
               if (var->data.sample)
                  info->uses_linear_sample = true;
               else if (var->data.centroid)
                  info->uses_linear_centroid = true;
               else
                  info->uses_linear_center = true;
               break;
            }
         }
         break;
      }
      case nir_intrinsic_interp_deref_at_centroid:
      case nir_intrinsic_interp_deref_at_sample:
      case nir_intrinsic_interp_deref_at_offset: {
         enum glsl_interp_mode interp = intrinsic_get_var(intr)->data.interpolation;
         switch (interp) {
         case INTERP_MODE_SMOOTH:
         case INTERP_MODE_NONE:
            if (intr->intrinsic == nir_intrinsic_interp_deref_at_centroid)
               info->uses_persp_opcode_interp_centroid = true;
            else if (intr->intrinsic == nir_intrinsic_interp_deref_at_sample)
               info->uses_persp_opcode_interp_sample = true;
            else
               info->uses_persp_opcode_interp_offset = true;
            break;
         case INTERP_MODE_NOPERSPECTIVE:
            if (intr->intrinsic == nir_intrinsic_interp_deref_at_centroid)
               info->uses_linear_opcode_interp_centroid = true;
            else if (intr->intrinsic == nir_intrinsic_interp_deref_at_sample)
               info->uses_linear_opcode_interp_sample = true;
            else
               info->uses_linear_opcode_interp_offset = true;
            break;
         case INTERP_MODE_FLAT:
            break;
         default:
            unreachable("Unsupported interpoation type");
         }
         break;
      }
      default:
         break;
      }
   }
}

void nir_tgsi_scan_shader(const struct nir_shader *nir,
                          struct tgsi_shader_info *info,
                          bool need_texcoord)
{
   unsigned i;

   info->processor = pipe_shader_type_from_mesa(nir->info.stage);
   info->num_tokens = 1; /* Presume empty */
   info->num_instructions = 1;

   info->properties[TGSI_PROPERTY_NEXT_SHADER] =
      pipe_shader_type_from_mesa(nir->info.next_stage);

   if (nir->info.stage == MESA_SHADER_VERTEX) {
      info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION] =
         nir->info.vs.window_space_position;
   }

   if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
      info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT] =
         nir->info.tess.tcs_vertices_out;
   }

   if (nir->info.stage == MESA_SHADER_TESS_EVAL) {
      info->properties[TGSI_PROPERTY_TES_PRIM_MODE] = u_tess_prim_from_shader(nir->info.tess._primitive_mode);

      STATIC_ASSERT((TESS_SPACING_EQUAL + 1) % 3 == PIPE_TESS_SPACING_EQUAL);
      STATIC_ASSERT((TESS_SPACING_FRACTIONAL_ODD + 1) % 3 ==
                    PIPE_TESS_SPACING_FRACTIONAL_ODD);
      STATIC_ASSERT((TESS_SPACING_FRACTIONAL_EVEN + 1) % 3 ==
                    PIPE_TESS_SPACING_FRACTIONAL_EVEN);

      info->properties[TGSI_PROPERTY_TES_SPACING] = (nir->info.tess.spacing + 1) % 3;
      info->properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW] = !nir->info.tess.ccw;
      info->properties[TGSI_PROPERTY_TES_POINT_MODE] = nir->info.tess.point_mode;
   }

   if (nir->info.stage == MESA_SHADER_GEOMETRY) {
      info->properties[TGSI_PROPERTY_GS_INPUT_PRIM] = nir->info.gs.input_primitive;
      info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM] = nir->info.gs.output_primitive;
      info->properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES] = nir->info.gs.vertices_out;
      info->properties[TGSI_PROPERTY_GS_INVOCATIONS] = nir->info.gs.invocations;
   }

   if (nir->info.stage == MESA_SHADER_FRAGMENT) {
      info->properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL] =
         nir->info.fs.early_fragment_tests | nir->info.fs.post_depth_coverage;
      info->properties[TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE] = nir->info.fs.post_depth_coverage;
      info->uses_fbfetch = nir->info.fs.uses_fbfetch_output;

      if (nir->info.fs.pixel_center_integer) {
         info->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] =
            TGSI_FS_COORD_PIXEL_CENTER_INTEGER;
      }

      if (nir->info.fs.depth_layout != FRAG_DEPTH_LAYOUT_NONE) {
         switch (nir->info.fs.depth_layout) {
         case FRAG_DEPTH_LAYOUT_ANY:
            info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_ANY;
            break;
         case FRAG_DEPTH_LAYOUT_GREATER:
            info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_GREATER;
            break;
         case FRAG_DEPTH_LAYOUT_LESS:
            info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_LESS;
            break;
         case FRAG_DEPTH_LAYOUT_UNCHANGED:
            info->properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT] = TGSI_FS_DEPTH_LAYOUT_UNCHANGED;
            break;
         default:
            unreachable("Unknow depth layout");
         }
      }
   }

   if (gl_shader_stage_is_compute(nir->info.stage)) {
      info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] = nir->info.workgroup_size[0];
      info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT] = nir->info.workgroup_size[1];
      info->properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH] = nir->info.workgroup_size[2];
   }

   i = 0;
   uint64_t processed_inputs = 0;
   nir_foreach_shader_in_variable(variable, nir) {
      unsigned semantic_name, semantic_index;

      const struct glsl_type *type = variable->type;
      if (nir_is_arrayed_io(variable, nir->info.stage)) {
         assert(glsl_type_is_array(type));
         type = glsl_get_array_element(type);
      }

      unsigned attrib_count = variable->data.compact ? DIV_ROUND_UP(glsl_get_length(type), 4) :
         glsl_count_attribute_slots(type, nir->info.stage == MESA_SHADER_VERTEX);

      i = variable->data.driver_location;

      /* Vertex shader inputs don't have semantics. The state
       * tracker has already mapped them to attributes via
       * variable->data.driver_location.
       */
      if (nir->info.stage == MESA_SHADER_VERTEX) {
         continue;
      }

      for (unsigned j = 0; j < attrib_count; j++, i++) {

         if (processed_inputs & ((uint64_t)1 << i))
            continue;

         processed_inputs |= ((uint64_t)1 << i);

         tgsi_get_gl_varying_semantic(variable->data.location + j, need_texcoord,
                                      &semantic_name, &semantic_index);

         info->input_semantic_name[i] = semantic_name;
         info->input_semantic_index[i] = semantic_index;

         if (semantic_name == TGSI_SEMANTIC_PRIMID)
            info->uses_primid = true;

         enum glsl_base_type base_type =
            glsl_get_base_type(glsl_without_array(variable->type));

         if (variable->data.centroid)
            info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_CENTROID;
         if (variable->data.sample)
            info->input_interpolate_loc[i] = TGSI_INTERPOLATE_LOC_SAMPLE;

         switch (variable->data.interpolation) {
         case INTERP_MODE_NONE:
            if (glsl_base_type_is_integer(base_type)) {
               info->input_interpolate[i] = TGSI_INTERPOLATE_CONSTANT;
               break;
            }

            if (semantic_name == TGSI_SEMANTIC_COLOR) {
               info->input_interpolate[i] = TGSI_INTERPOLATE_COLOR;
               break;
            }
            FALLTHROUGH;

         case INTERP_MODE_SMOOTH:
            assert(!glsl_base_type_is_integer(base_type));

            info->input_interpolate[i] = TGSI_INTERPOLATE_PERSPECTIVE;
            break;

         case INTERP_MODE_NOPERSPECTIVE:
            assert(!glsl_base_type_is_integer(base_type));

            info->input_interpolate[i] = TGSI_INTERPOLATE_LINEAR;
            break;

         case INTERP_MODE_FLAT:
            info->input_interpolate[i] = TGSI_INTERPOLATE_CONSTANT;
            break;
         }
      }
   }

   info->num_inputs = nir->num_inputs;
   if (nir->info.io_lowered) {
      info->num_inputs = util_bitcount64(nir->info.inputs_read);
      if (nir->info.inputs_read_indirectly)
         info->indirect_files |= 1 << TGSI_FILE_INPUT;
      info->file_max[TGSI_FILE_INPUT] = info->num_inputs - 1;
   } else {
      int max = info->file_max[TGSI_FILE_INPUT] = -1;
      nir_foreach_shader_in_variable(var, nir) {
         int slots = glsl_count_attribute_slots(var->type, false);
         int tmax = var->data.driver_location + slots - 1;
         if (tmax > max)
            max = tmax;
         info->file_max[TGSI_FILE_INPUT] = max;
      }
   }

   i = 0;
   uint64_t processed_outputs = 0;
   unsigned num_outputs = 0;
   nir_foreach_shader_out_variable(variable, nir) {
      unsigned semantic_name, semantic_index;

      i = variable->data.driver_location;

      const struct glsl_type *type = variable->type;
      if (nir_is_arrayed_io(variable, nir->info.stage)) {
         assert(glsl_type_is_array(type));
         type = glsl_get_array_element(type);
      }

      unsigned attrib_count = variable->data.compact ? DIV_ROUND_UP(glsl_get_length(type), 4) :
         glsl_count_attribute_slots(type, false);
      for (unsigned k = 0; k < attrib_count; k++, i++) {

         if (nir->info.stage == MESA_SHADER_FRAGMENT) {
            tgsi_get_gl_frag_result_semantic(variable->data.location + k,
                                             &semantic_name, &semantic_index);

            /* Adjust for dual source blending */
            if (variable->data.index > 0) {
               semantic_index++;
            }
         } else {
            tgsi_get_gl_varying_semantic(variable->data.location + k, need_texcoord,
                                         &semantic_name, &semantic_index);
         }

         unsigned num_components = 4;
         unsigned vector_elements = glsl_get_vector_elements(glsl_without_array(variable->type));
         if (vector_elements)
            num_components = vector_elements;

         unsigned component = variable->data.location_frac;
         if (glsl_type_is_64bit(glsl_without_array(variable->type))) {
            if (glsl_type_is_dual_slot(glsl_without_array(variable->type)) && k % 2) {
               num_components = (num_components * 2) - 4;
               component = 0;
            } else {
               num_components = MIN2(num_components * 2, 4);
            }
         }

         ubyte usagemask = 0;
         for (unsigned j = component; j < num_components + component; j++) {
            switch (j) {
            case 0:
               usagemask |= TGSI_WRITEMASK_X;
               break;
            case 1:
               usagemask |= TGSI_WRITEMASK_Y;
               break;
            case 2:
               usagemask |= TGSI_WRITEMASK_Z;
               break;
            case 3:
               usagemask |= TGSI_WRITEMASK_W;
               break;
            default:
               unreachable("error calculating component index");
            }
         }

         unsigned gs_out_streams;
         if (variable->data.stream & NIR_STREAM_PACKED) {
            gs_out_streams = variable->data.stream & ~NIR_STREAM_PACKED;
         } else {
            assert(variable->data.stream < 4);
            gs_out_streams = 0;
            for (unsigned j = 0; j < num_components; ++j)
               gs_out_streams |= variable->data.stream << (2 * (component + j));
         }

         unsigned streamx = gs_out_streams & 3;
         unsigned streamy = (gs_out_streams >> 2) & 3;
         unsigned streamz = (gs_out_streams >> 4) & 3;
         unsigned streamw = (gs_out_streams >> 6) & 3;

         if (usagemask & TGSI_WRITEMASK_X) {
            info->output_usagemask[i] |= TGSI_WRITEMASK_X;
            info->output_streams[i] |= streamx;
            info->num_stream_output_components[streamx]++;
         }
         if (usagemask & TGSI_WRITEMASK_Y) {
            info->output_usagemask[i] |= TGSI_WRITEMASK_Y;
            info->output_streams[i] |= streamy << 2;
            info->num_stream_output_components[streamy]++;
         }
         if (usagemask & TGSI_WRITEMASK_Z) {
            info->output_usagemask[i] |= TGSI_WRITEMASK_Z;
            info->output_streams[i] |= streamz << 4;
            info->num_stream_output_components[streamz]++;
         }
         if (usagemask & TGSI_WRITEMASK_W) {
            info->output_usagemask[i] |= TGSI_WRITEMASK_W;
            info->output_streams[i] |= streamw << 6;
            info->num_stream_output_components[streamw]++;
         }

         /* make sure we only count this location once against
          * the num_outputs counter.
          */
         if (processed_outputs & ((uint64_t)1 << i))
            continue;

         processed_outputs |= ((uint64_t)1 << i);
         num_outputs++;

         info->output_semantic_name[i] = semantic_name;
         info->output_semantic_index[i] = semantic_index;

         switch (semantic_name) {
         case TGSI_SEMANTIC_PRIMID:
            info->writes_primid = true;
            break;
         case TGSI_SEMANTIC_VIEWPORT_INDEX:
            info->writes_viewport_index = true;
            break;
         case TGSI_SEMANTIC_LAYER:
            info->writes_layer = true;
            break;
         case TGSI_SEMANTIC_PSIZE:
            info->writes_psize = true;
            break;
         case TGSI_SEMANTIC_CLIPVERTEX:
            info->writes_clipvertex = true;
            break;
         case TGSI_SEMANTIC_COLOR:
            info->colors_written |= 1 << semantic_index;
            break;
         case TGSI_SEMANTIC_STENCIL:
            if (!variable->data.fb_fetch_output)
               info->writes_stencil = true;
            break;
         case TGSI_SEMANTIC_SAMPLEMASK:
            info->writes_samplemask = true;
            break;
         case TGSI_SEMANTIC_EDGEFLAG:
            info->writes_edgeflag = true;
            break;
         case TGSI_SEMANTIC_POSITION:
            if (info->processor == PIPE_SHADER_FRAGMENT) {
               if (!variable->data.fb_fetch_output)
                  info->writes_z = true;
            } else {
               info->writes_position = true;
            }
            break;
         }

         if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
            switch (semantic_name) {
            case TGSI_SEMANTIC_PATCH:
               info->reads_perpatch_outputs = true;
               break;
            case TGSI_SEMANTIC_TESSINNER:
            case TGSI_SEMANTIC_TESSOUTER:
               info->reads_tessfactor_outputs = true;
               break;
            default:
               info->reads_pervertex_outputs = true;
            }
         }
      }

      unsigned loc = variable->data.location;
      if (nir->info.stage == MESA_SHADER_FRAGMENT &&
          loc == FRAG_RESULT_COLOR &&
          nir->info.outputs_written & (1ull << loc)) {
         assert(attrib_count == 1);
         info->properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] = true;
      }
   }

   if (nir->info.io_lowered) {
      uint64_t outputs_written = nir->info.outputs_written;

      while (outputs_written) {
         unsigned location = u_bit_scan64(&outputs_written);
         unsigned i = util_bitcount64(nir->info.outputs_written &
                                      BITFIELD64_MASK(location));
         unsigned semantic_name, semantic_index;

         tgsi_get_gl_varying_semantic(location, need_texcoord,
                                      &semantic_name, &semantic_index);

         info->output_semantic_name[i] = semantic_name;
         info->output_semantic_index[i] = semantic_index;
         info->output_usagemask[i] = 0xf;
      }
      num_outputs = util_bitcount64(nir->info.outputs_written);
      if (nir->info.outputs_accessed_indirectly)
         info->indirect_files |= 1 << TGSI_FILE_OUTPUT;
   }

   info->num_outputs = num_outputs;

   info->const_file_max[0] = nir->num_uniforms - 1;
   info->images_declared = nir->info.images_used[0];
   info->samplers_declared = nir->info.textures_used[0];

   info->file_max[TGSI_FILE_SAMPLER] = BITSET_LAST_BIT(nir->info.samplers_used) - 1;
   info->file_max[TGSI_FILE_SAMPLER_VIEW] = BITSET_LAST_BIT(nir->info.textures_used) - 1;
   info->file_mask[TGSI_FILE_SAMPLER] = nir->info.samplers_used[0];
   info->file_mask[TGSI_FILE_SAMPLER_VIEW] = nir->info.textures_used[0];
   info->file_max[TGSI_FILE_IMAGE] = BITSET_LAST_BIT(nir->info.images_used) - 1;
   info->file_mask[TGSI_FILE_IMAGE] = info->images_declared;

   info->num_written_clipdistance = nir->info.clip_distance_array_size;
   info->num_written_culldistance = nir->info.cull_distance_array_size;
   info->clipdist_writemask = u_bit_consecutive(0, info->num_written_clipdistance);
   info->culldist_writemask = u_bit_consecutive(0, info->num_written_culldistance);

   if (info->processor == PIPE_SHADER_FRAGMENT)
      info->uses_kill = nir->info.fs.uses_discard;

   nir_function *func = (struct nir_function *)
      exec_list_get_head_const(&nir->functions);

   nir_foreach_block(block, func->impl) {
      nir_foreach_instr(instr, block)
         scan_instruction(nir, need_texcoord, info, instr);
   }
}