+
+/* vertex shader that generates vertices */
+nir_shader *
+radv_meta_build_nir_vs_generate_vertices(void)
+{
+ const struct glsl_type *vec4 = glsl_vec4_type();
+
+ nir_builder b;
+ nir_variable *v_position;
+
+ nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
+ b.shader->info.name = ralloc_strdup(b.shader, "meta_vs_gen_verts");
+
+ nir_ssa_def *outvec = radv_meta_gen_rect_vertices(&b);
+
+ v_position = nir_variable_create(b.shader, nir_var_shader_out, vec4,
+ "gl_Position");
+ v_position->data.location = VARYING_SLOT_POS;
+
+ nir_store_var(&b, v_position, outvec, 0xf);
+
+ return b.shader;
+}
+
+nir_shader *
+radv_meta_build_nir_fs_noop(void)
+{
+ nir_builder b;
+
+ nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
+ b.shader->info.name = ralloc_asprintf(b.shader,
+ "meta_noop_fs");
+
+ return b.shader;
+}
+
+void radv_meta_build_resolve_shader_core(nir_builder *b,
+ bool is_integer,
+ int samples,
+ nir_variable *input_img,
+ nir_variable *color,
+ nir_ssa_def *img_coord)
+{
+ /* do a txf_ms on each sample */
+ nir_ssa_def *tmp;
+ nir_if *outer_if = NULL;
+
+ nir_ssa_def *input_img_deref = &nir_build_deref_var(b, input_img)->dest.ssa;
+
+ nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3);
+ tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
+ tex->op = nir_texop_txf_ms;
+ tex->src[0].src_type = nir_tex_src_coord;
+ tex->src[0].src = nir_src_for_ssa(img_coord);
+ tex->src[1].src_type = nir_tex_src_ms_index;
+ tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
+ tex->src[2].src_type = nir_tex_src_texture_deref;
+ tex->src[2].src = nir_src_for_ssa(input_img_deref);
+ tex->dest_type = nir_type_float;
+ tex->is_array = false;
+ tex->coord_components = 2;
+
+ nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
+ nir_builder_instr_insert(b, &tex->instr);
+
+ tmp = &tex->dest.ssa;
+
+ if (!is_integer && samples > 1) {
+ nir_tex_instr *tex_all_same = nir_tex_instr_create(b->shader, 2);
+ tex_all_same->sampler_dim = GLSL_SAMPLER_DIM_MS;
+ tex_all_same->op = nir_texop_samples_identical;
+ tex_all_same->src[0].src_type = nir_tex_src_coord;
+ tex_all_same->src[0].src = nir_src_for_ssa(img_coord);
+ tex_all_same->src[1].src_type = nir_tex_src_texture_deref;
+ tex_all_same->src[1].src = nir_src_for_ssa(input_img_deref);
+ tex_all_same->dest_type = nir_type_float;
+ tex_all_same->is_array = false;
+ tex_all_same->coord_components = 2;
+
+ nir_ssa_dest_init(&tex_all_same->instr, &tex_all_same->dest, 1, 32, "tex");
+ nir_builder_instr_insert(b, &tex_all_same->instr);
+
+ nir_ssa_def *all_same = nir_ieq(b, &tex_all_same->dest.ssa, nir_imm_int(b, 0));
+ nir_if *if_stmt = nir_if_create(b->shader);
+ if_stmt->condition = nir_src_for_ssa(all_same);
+ nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
+
+ b->cursor = nir_after_cf_list(&if_stmt->then_list);
+ for (int i = 1; i < samples; i++) {
+ nir_tex_instr *tex_add = nir_tex_instr_create(b->shader, 3);
+ tex_add->sampler_dim = GLSL_SAMPLER_DIM_MS;
+ tex_add->op = nir_texop_txf_ms;
+ tex_add->src[0].src_type = nir_tex_src_coord;
+ tex_add->src[0].src = nir_src_for_ssa(img_coord);
+ tex_add->src[1].src_type = nir_tex_src_ms_index;
+ tex_add->src[1].src = nir_src_for_ssa(nir_imm_int(b, i));
+ tex_add->src[2].src_type = nir_tex_src_texture_deref;
+ tex_add->src[2].src = nir_src_for_ssa(input_img_deref);
+ tex_add->dest_type = nir_type_float;
+ tex_add->is_array = false;
+ tex_add->coord_components = 2;
+
+ nir_ssa_dest_init(&tex_add->instr, &tex_add->dest, 4, 32, "tex");
+ nir_builder_instr_insert(b, &tex_add->instr);
+
+ tmp = nir_fadd(b, tmp, &tex_add->dest.ssa);
+ }
+
+ tmp = nir_fdiv(b, tmp, nir_imm_float(b, samples));
+ nir_store_var(b, color, tmp, 0xf);
+ b->cursor = nir_after_cf_list(&if_stmt->else_list);
+ outer_if = if_stmt;
+ }
+ nir_store_var(b, color, &tex->dest.ssa, 0xf);
+
+ if (outer_if)
+ b->cursor = nir_after_cf_node(&outer_if->cf_node);
+}