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 void
vc4_nir_lower_txf_ms_instr(struct v3d_compile * c,nir_builder * b,nir_tex_instr * instr)38 vc4_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 coord = nir_ishl(b, coord, nir_imm_int(b, 1));
53 coord = nir_vec2(b,
54 nir_iadd(b,
55 nir_channel(b, coord, 0),
56 nir_iand(b, sample, one)),
57 nir_iadd(b,
58 nir_channel(b, coord, 1),
59 nir_iand(b, nir_ushr(b, sample, one), one)));
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
69 void
v3d_nir_lower_txf_ms(nir_shader * s,struct v3d_compile * c)70 v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c)
71 {
72 nir_foreach_function(function, s) {
73 if (!function->impl)
74 continue;
75
76 nir_builder b;
77 nir_builder_init(&b, function->impl);
78
79 nir_foreach_block(block, function->impl) {
80 nir_foreach_instr_safe(instr, block) {
81 if (instr->type != nir_instr_type_tex)
82 continue;
83
84 vc4_nir_lower_txf_ms_instr(c, &b,
85 nir_instr_as_tex(instr));
86 }
87 }
88
89 nir_metadata_preserve(function->impl,
90 nir_metadata_block_index |
91 nir_metadata_dominance);
92 }
93 }
94