+static void
+v3d_nir_lower_gs_early(struct v3d_compile *c)
+{
+ /* Split our I/O vars and dead code eliminate the unused
+ * components.
+ */
+ NIR_PASS_V(c->s, nir_lower_io_to_scalar_early,
+ nir_var_shader_in | nir_var_shader_out);
+ uint64_t used_outputs[4] = {0};
+ for (int i = 0; i < c->gs_key->num_used_outputs; i++) {
+ int slot = v3d_slot_get_slot(c->gs_key->used_outputs[i]);
+ int comp = v3d_slot_get_component(c->gs_key->used_outputs[i]);
+ used_outputs[comp] |= 1ull << slot;
+ }
+ NIR_PASS_V(c->s, nir_remove_unused_io_vars,
+ nir_var_shader_out, used_outputs, NULL); /* demotes to globals */
+ NIR_PASS_V(c->s, nir_lower_global_vars_to_local);
+ v3d_optimize_nir(c->s);
+ NIR_PASS_V(c->s, nir_remove_dead_variables, nir_var_shader_in, NULL);
+
+ /* This must go before nir_lower_io */
+ if (c->gs_key->per_vertex_point_size)
+ NIR_PASS_V(c->s, nir_lower_point_size, 1.0f, 0.0f);
+
+ NIR_PASS_V(c->s, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
+ type_size_vec4,
+ (nir_lower_io_options)0);
+ /* clean up nir_lower_io's deref_var remains */
+ NIR_PASS_V(c->s, nir_opt_dce);
+}
+
+static void
+v3d_fixup_fs_output_types(struct v3d_compile *c)
+{
+ nir_foreach_shader_out_variable(var, c->s) {
+ uint32_t mask = 0;
+
+ switch (var->data.location) {
+ case FRAG_RESULT_COLOR:
+ mask = ~0;
+ break;
+ case FRAG_RESULT_DATA0:
+ case FRAG_RESULT_DATA1:
+ case FRAG_RESULT_DATA2:
+ case FRAG_RESULT_DATA3:
+ mask = 1 << (var->data.location - FRAG_RESULT_DATA0);
+ break;
+ }
+
+ if (c->fs_key->int_color_rb & mask) {
+ var->type =
+ glsl_vector_type(GLSL_TYPE_INT,
+ glsl_get_components(var->type));
+ } else if (c->fs_key->uint_color_rb & mask) {
+ var->type =
+ glsl_vector_type(GLSL_TYPE_UINT,
+ glsl_get_components(var->type));
+ }
+ }
+}
+
+static void
+v3d_nir_lower_fs_early(struct v3d_compile *c)
+{
+ if (c->fs_key->int_color_rb || c->fs_key->uint_color_rb)
+ v3d_fixup_fs_output_types(c);
+
+ NIR_PASS_V(c->s, v3d_nir_lower_logic_ops, c);
+
+ if (c->fs_key->line_smoothing) {
+ v3d_nir_lower_line_smooth(c->s);
+ NIR_PASS_V(c->s, nir_lower_global_vars_to_local);
+ /* The lowering pass can introduce new sysval reads */
+ nir_shader_gather_info(c->s, nir_shader_get_entrypoint(c->s));
+ }
+
+ /* If the shader has no non-TLB side effects, we can promote it to
+ * enabling early_fragment_tests even if the user didn't.
+ */
+ if (!(c->s->info.num_images ||
+ c->s->info.num_ssbos)) {
+ c->s->info.fs.early_fragment_tests = true;
+ }
+}
+
+static void
+v3d_nir_lower_gs_late(struct v3d_compile *c)
+{
+ if (c->key->ucp_enables) {
+ NIR_PASS_V(c->s, nir_lower_clip_gs, c->key->ucp_enables,
+ false, NULL);
+ }
+
+ /* Note: GS output scalarizing must happen after nir_lower_clip_gs. */
+ NIR_PASS_V(c->s, nir_lower_io_to_scalar, nir_var_shader_out);
+}
+
+static void
+v3d_nir_lower_vs_late(struct v3d_compile *c)
+{
+ if (c->vs_key->clamp_color)