nir: Allow for system values with variable numbers of destination components

Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5927>
diff --git a/src/compiler/nir/nir_builder_opcodes_h.py b/src/compiler/nir/nir_builder_opcodes_h.py
index ec212f4..2c37be7 100644
--- a/src/compiler/nir/nir_builder_opcodes_h.py
+++ b/src/compiler/nir/nir_builder_opcodes_h.py
@@ -53,12 +53,17 @@
 /* Generic builder for system values. */
 static inline nir_ssa_def *
 nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index,
-                      unsigned bit_size)
+                      unsigned num_components, unsigned bit_size)
 {
    nir_intrinsic_instr *load = nir_intrinsic_instr_create(build->shader, op);
+   if (nir_intrinsic_infos[op].dest_components > 0)
+      assert(num_components == nir_intrinsic_infos[op].dest_components);
+   else
+      load->num_components = num_components;
    load->const_index[0] = index;
+
    nir_ssa_dest_init(&load->instr, &load->dest,
-                     nir_intrinsic_infos[op].dest_components, bit_size, NULL);
+                     num_components, bit_size, NULL);
    nir_builder_instr_insert(build, &load->instr);
    return &load->dest.ssa;
 }
@@ -68,6 +73,8 @@
    res = ''
    if opcode.indices:
       res += ', unsigned ' + opcode.indices[0].lower()
+   if opcode.dest_components == 0:
+      res += ', unsigned num_components'
    if len(opcode.bit_sizes) != 1:
       res += ', unsigned bit_size'
    return res
@@ -79,6 +86,11 @@
    else:
       args.append('0')
 
+   if opcode.dest_components == 0:
+      args.append('num_components')
+   else:
+      args.append(str(opcode.dest_components))
+
    if len(opcode.bit_sizes) == 1:
       bit_size = opcode.bit_sizes[0]
       args.append(str(bit_size))
diff --git a/src/compiler/nir/nir_lower_system_values.c b/src/compiler/nir/nir_lower_system_values.c
index bc9d23c..bdc337a 100644
--- a/src/compiler/nir/nir_lower_system_values.c
+++ b/src/compiler/nir/nir_lower_system_values.c
@@ -305,6 +305,7 @@
       nir_intrinsic_op sysval_op =
          nir_intrinsic_from_system_value(var->data.location);
       return nir_load_system_value(b, sysval_op, 0,
+                                      intrin->dest.ssa.num_components,
                                       intrin->dest.ssa.bit_size);
    }
 
diff --git a/src/gallium/drivers/vc4/vc4_nir_lower_blend.c b/src/gallium/drivers/vc4/vc4_nir_lower_blend.c
index c915b8c..192abe9 100644
--- a/src/gallium/drivers/vc4/vc4_nir_lower_blend.c
+++ b/src/gallium/drivers/vc4/vc4_nir_lower_blend.c
@@ -100,7 +100,7 @@
                 return nir_load_system_value(b,
                                              nir_intrinsic_load_blend_const_color_r_float +
                                              channel,
-                                             0, 32);
+                                             0, 1, 32);
         case PIPE_BLENDFACTOR_CONST_ALPHA:
                 return nir_load_blend_const_color_a_float(b);
         case PIPE_BLENDFACTOR_ZERO:
@@ -118,7 +118,7 @@
                                 nir_load_system_value(b,
                                                       nir_intrinsic_load_blend_const_color_r_float +
                                                       channel,
-                                                      0, 32));
+                                                      0, 1, 32));
         case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
                 return nir_fsub(b, nir_imm_float(b, 1.0),
                                 nir_load_blend_const_color_a_float(b));