• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  * Copyright © 2023 Valve Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  */
25 
26 #include "nir.h"
27 #include "nir_builder.h"
28 #include "radv_nir.h"
29 #include "radv_private.h"
30 
31 bool
radv_nir_lower_fs_intrinsics(nir_shader * nir,const struct radv_shader_stage * fs_stage,const struct radv_graphics_state_key * gfx_state)32 radv_nir_lower_fs_intrinsics(nir_shader *nir, const struct radv_shader_stage *fs_stage,
33                              const struct radv_graphics_state_key *gfx_state)
34 {
35    const struct radv_shader_info *info = &fs_stage->info;
36    const struct radv_shader_args *args = &fs_stage->args;
37    nir_function_impl *impl = nir_shader_get_entrypoint(nir);
38    bool progress = false;
39 
40    nir_builder b = nir_builder_create(impl);
41 
42    nir_foreach_block (block, impl) {
43       nir_foreach_instr_safe (instr, block) {
44          if (instr->type != nir_instr_type_intrinsic)
45             continue;
46 
47          nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
48          b.cursor = nir_after_instr(&intrin->instr);
49 
50          switch (intrin->intrinsic) {
51          case nir_intrinsic_load_sample_mask_in: {
52             nir_def *sample_coverage = nir_load_vector_arg_amd(&b, 1, .base = args->ac.sample_coverage.arg_index);
53 
54             nir_def *def = NULL;
55             if (info->ps.uses_sample_shading || gfx_state->ms.sample_shading_enable) {
56                /* gl_SampleMaskIn[0] = (SampleCoverage & (PsIterMask << gl_SampleID)). */
57                nir_def *ps_state = nir_load_scalar_arg_amd(&b, 1, .base = args->ps_state.arg_index);
58                nir_def *ps_iter_mask =
59                   nir_ubfe_imm(&b, ps_state, PS_STATE_PS_ITER_MASK__SHIFT, util_bitcount(PS_STATE_PS_ITER_MASK__MASK));
60                nir_def *sample_id = nir_load_sample_id(&b);
61                def = nir_iand(&b, sample_coverage, nir_ishl(&b, ps_iter_mask, sample_id));
62             } else {
63                def = sample_coverage;
64             }
65 
66             nir_def_rewrite_uses(&intrin->def, def);
67 
68             nir_instr_remove(instr);
69             progress = true;
70             break;
71          }
72          case nir_intrinsic_load_frag_coord: {
73             if (!gfx_state->adjust_frag_coord_z)
74                continue;
75 
76             if (!(nir_def_components_read(&intrin->def) & (1 << 2)))
77                continue;
78 
79             nir_def *frag_z = nir_channel(&b, &intrin->def, 2);
80 
81             /* adjusted_frag_z = fddx_fine(frag_z) * 0.0625 + frag_z */
82             nir_def *adjusted_frag_z = nir_fddx_fine(&b, frag_z);
83             adjusted_frag_z = nir_ffma_imm1(&b, adjusted_frag_z, 0.0625f, frag_z);
84 
85             /* VRS Rate X = Ancillary[2:3] */
86             nir_def *ancillary = nir_load_vector_arg_amd(&b, 1, .base = args->ac.ancillary.arg_index);
87             nir_def *x_rate = nir_ubfe_imm(&b, ancillary, 2, 2);
88 
89             /* xRate = xRate == 0x1 ? adjusted_frag_z : frag_z. */
90             nir_def *cond = nir_ieq_imm(&b, x_rate, 1);
91             frag_z = nir_bcsel(&b, cond, adjusted_frag_z, frag_z);
92 
93             nir_def *new_dest = nir_vector_insert_imm(&b, &intrin->def, frag_z, 2);
94             nir_def_rewrite_uses_after(&intrin->def, new_dest, new_dest->parent_instr);
95 
96             progress = true;
97             break;
98          }
99          case nir_intrinsic_load_barycentric_at_sample: {
100             nir_def *num_samples = nir_load_rasterization_samples_amd(&b);
101             nir_def *new_dest;
102 
103             if (gfx_state->dynamic_rasterization_samples) {
104                nir_def *res1, *res2;
105 
106                nir_push_if(&b, nir_ieq_imm(&b, num_samples, 1));
107                {
108                   res1 = nir_load_barycentric_pixel(&b, 32, .interp_mode = nir_intrinsic_interp_mode(intrin));
109                }
110                nir_push_else(&b, NULL);
111                {
112                   nir_def *sample_pos = nir_load_sample_positions_amd(&b, 32, intrin->src[0].ssa, num_samples);
113 
114                   /* sample_pos -= 0.5 */
115                   sample_pos = nir_fadd_imm(&b, sample_pos, -0.5f);
116 
117                   res2 = nir_load_barycentric_at_offset(&b, 32, sample_pos,
118                                                         .interp_mode = nir_intrinsic_interp_mode(intrin));
119                }
120                nir_pop_if(&b, NULL);
121 
122                new_dest = nir_if_phi(&b, res1, res2);
123             } else {
124                if (!gfx_state->ms.rasterization_samples) {
125                   new_dest = nir_load_barycentric_pixel(&b, 32, .interp_mode = nir_intrinsic_interp_mode(intrin));
126                } else {
127                   nir_def *sample_pos = nir_load_sample_positions_amd(&b, 32, intrin->src[0].ssa, num_samples);
128 
129                   /* sample_pos -= 0.5 */
130                   sample_pos = nir_fadd_imm(&b, sample_pos, -0.5f);
131 
132                   new_dest = nir_load_barycentric_at_offset(&b, 32, sample_pos,
133                                                             .interp_mode = nir_intrinsic_interp_mode(intrin));
134                }
135             }
136 
137             nir_def_rewrite_uses(&intrin->def, new_dest);
138             nir_instr_remove(instr);
139 
140             progress = true;
141             break;
142          }
143          default:
144             break;
145          }
146       }
147    }
148 
149    if (progress)
150       nir_metadata_preserve(impl, 0);
151    else
152       nir_metadata_preserve(impl, nir_metadata_all);
153 
154    return progress;
155 }
156