xref: /aosp_15_r20/external/mesa3d/src/broadcom/compiler/v3d_nir_lower_txf_ms.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 static nir_def *
v3d_nir_lower_txf_ms_instr(nir_builder * b,nir_instr * in_instr,void * data)36 v3d_nir_lower_txf_ms_instr(nir_builder *b, nir_instr *in_instr, void *data)
37 {
38         nir_tex_instr *instr = nir_instr_as_tex(in_instr);
39 
40         b->cursor = nir_before_instr(&instr->instr);
41 
42         nir_def *coord = nir_steal_tex_src(instr, nir_tex_src_coord);
43         nir_def *sample = nir_steal_tex_src(instr, nir_tex_src_ms_index);
44 
45         nir_def *one = nir_imm_int(b, 1);
46         nir_def *x = nir_iadd(b,
47                                   nir_ishl(b, nir_channel(b, coord, 0), one),
48                                   nir_iand(b, sample, one));
49         nir_def *y = nir_iadd(b,
50                                   nir_ishl(b, nir_channel(b, coord, 1), one),
51                                   nir_iand(b, nir_ushr(b, sample, one), one));
52         if (instr->is_array)
53                 coord = nir_vec3(b, x, y, nir_channel(b, coord, 2));
54         else
55                 coord = nir_vec2(b, x, y);
56 
57         nir_tex_instr_add_src(instr, nir_tex_src_coord, coord);
58         instr->op = nir_texop_txf;
59         instr->sampler_dim = GLSL_SAMPLER_DIM_2D;
60 
61         return NIR_LOWER_INSTR_PROGRESS;
62 }
63 
64 static bool
v3d_nir_lower_txf_ms_filter(const nir_instr * instr,const void * data)65 v3d_nir_lower_txf_ms_filter(const nir_instr *instr, const void *data)
66 {
67         return (instr->type == nir_instr_type_tex &&
68                 nir_instr_as_tex(instr)->op == nir_texop_txf_ms);
69 }
70 
71 bool
v3d_nir_lower_txf_ms(nir_shader * s)72 v3d_nir_lower_txf_ms(nir_shader *s)
73 {
74         return nir_shader_lower_instructions(s,
75                                              v3d_nir_lower_txf_ms_filter,
76                                              v3d_nir_lower_txf_ms_instr,
77                                              NULL);
78 }
79