1/*
2 * Copyright © 2015 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "v3d_compiler.h"
25#include "compiler/nir/nir_builder.h"
26
27/** @file v3d_nir_lower_txf_ms.c
28 *
29 * V3D's MSAA surfaces are laid out in UIF textures where each pixel is a 2x2
30 * quad in the texture.  This pass lowers the txf_ms with a ms_index source to
31 * a plain txf with the sample_index pulling out the correct texel from the
32 * 2x2 quad.
33 */
34
35#define V3D_MAX_SAMPLES 4
36
37static void
38vc4_nir_lower_txf_ms_instr(struct v3d_compile *c, nir_builder *b,
39                           nir_tex_instr *instr)
40{
41        if (instr->op != nir_texop_txf_ms)
42                return;
43
44        b->cursor = nir_before_instr(&instr->instr);
45
46        int coord_index = nir_tex_instr_src_index(instr, nir_tex_src_coord);
47        int sample_index = nir_tex_instr_src_index(instr, nir_tex_src_ms_index);
48        nir_ssa_def *coord = instr->src[coord_index].src.ssa;
49        nir_ssa_def *sample = instr->src[sample_index].src.ssa;
50
51        nir_ssa_def *one = nir_imm_int(b, 1);
52        nir_ssa_def *x = nir_iadd(b,
53                                  nir_ishl(b, nir_channel(b, coord, 0), one),
54                                  nir_iand(b, sample, one));
55        nir_ssa_def *y = nir_iadd(b,
56                                  nir_ishl(b, nir_channel(b, coord, 1), one),
57                                  nir_iand(b, nir_ushr(b, sample, one), one));
58        if (instr->is_array)
59                coord = nir_vec3(b, x, y, nir_channel(b, coord, 2));
60        else
61                coord = nir_vec2(b, x, y);
62
63        nir_instr_rewrite_src(&instr->instr,
64                              &instr->src[nir_tex_src_coord].src,
65                              nir_src_for_ssa(coord));
66        nir_tex_instr_remove_src(instr, sample_index);
67        instr->op = nir_texop_txf;
68        instr->sampler_dim = GLSL_SAMPLER_DIM_2D;
69}
70
71void
72v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c)
73{
74        nir_foreach_function(function, s) {
75                if (!function->impl)
76                        continue;
77
78                nir_builder b;
79                nir_builder_init(&b, function->impl);
80
81                nir_foreach_block(block, function->impl) {
82                        nir_foreach_instr_safe(instr, block) {
83                                if (instr->type != nir_instr_type_tex)
84                                        continue;
85
86                                vc4_nir_lower_txf_ms_instr(c, &b,
87                                                           nir_instr_as_tex(instr));
88                        }
89                }
90
91                nir_metadata_preserve(function->impl,
92                                      nir_metadata_block_index |
93                                      nir_metadata_dominance);
94        }
95}
96