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 
37 static nir_ssa_def *
v3d_nir_lower_txf_ms_instr(nir_builder * b,nir_instr * in_instr,void * data)38 v3d_nir_lower_txf_ms_instr(nir_builder *b, nir_instr *in_instr, void *data)
39 {
40         nir_tex_instr *instr = nir_instr_as_tex(in_instr);
41 
42         b->cursor = nir_before_instr(&instr->instr);
43 
44         int coord_index = nir_tex_instr_src_index(instr, nir_tex_src_coord);
45         int sample_index = nir_tex_instr_src_index(instr, nir_tex_src_ms_index);
46         nir_ssa_def *coord = instr->src[coord_index].src.ssa;
47         nir_ssa_def *sample = instr->src[sample_index].src.ssa;
48 
49         nir_ssa_def *one = nir_imm_int(b, 1);
50         nir_ssa_def *x = nir_iadd(b,
51                                   nir_ishl(b, nir_channel(b, coord, 0), one),
52                                   nir_iand(b, sample, one));
53         nir_ssa_def *y = nir_iadd(b,
54                                   nir_ishl(b, nir_channel(b, coord, 1), one),
55                                   nir_iand(b, nir_ushr(b, sample, one), one));
56         if (instr->is_array)
57                 coord = nir_vec3(b, x, y, nir_channel(b, coord, 2));
58         else
59                 coord = nir_vec2(b, x, y);
60 
61         nir_instr_rewrite_src(&instr->instr,
62                               &instr->src[nir_tex_src_coord].src,
63                               nir_src_for_ssa(coord));
64         nir_tex_instr_remove_src(instr, sample_index);
65         instr->op = nir_texop_txf;
66         instr->sampler_dim = GLSL_SAMPLER_DIM_2D;
67 
68         return NIR_LOWER_INSTR_PROGRESS;
69 }
70 
71 static bool
v3d_nir_lower_txf_ms_filter(const nir_instr * instr,const void * data)72 v3d_nir_lower_txf_ms_filter(const nir_instr *instr, const void *data)
73 {
74         return (instr->type == nir_instr_type_tex &&
75                 nir_instr_as_tex(instr)->op == nir_texop_txf_ms);
76 }
77 
78 void
v3d_nir_lower_txf_ms(nir_shader * s,struct v3d_compile * c)79 v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c)
80 {
81         nir_shader_lower_instructions(s,
82                                       v3d_nir_lower_txf_ms_filter,
83                                       v3d_nir_lower_txf_ms_instr,
84                                       NULL);
85 }
86