xref: /aosp_15_r20/external/mesa3d/src/imagination/rogue/nir/rogue_nir_pfo.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2022 Imagination Technologies Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * 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 THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "nir/nir.h"
25 #include "nir/nir_builder.h"
26 #include "nir/nir_search_helpers.h"
27 #include "rogue.h"
28 #include "util/macros.h"
29 
30 /**
31  * \file rogue_nir_pfo.c
32  *
33  * \brief Contains the rogue_nir_pfo pass.
34  */
35 
insert_pfo(nir_builder * b,nir_intrinsic_instr * store_output,nir_src * output_src)36 static void insert_pfo(nir_builder *b,
37                        nir_intrinsic_instr *store_output,
38                        nir_src *output_src)
39 {
40    /* TODO: Support complex PFO with blending. */
41    /* TODO: Verify type is vec4. */
42 
43    /* Pack the output color components into U8888 format. */
44    nir_def *new_output_src = nir_pack_unorm_4x8(b, output_src->ssa);
45 
46    /* Update the store_output intrinsic. */
47    nir_src_rewrite(output_src, new_output_src);
48    nir_intrinsic_set_write_mask(store_output, 1);
49    store_output->num_components = 1;
50    nir_intrinsic_set_src_type(store_output, nir_type_uint32);
51 }
52 
53 PUBLIC
rogue_nir_pfo(nir_shader * shader)54 void rogue_nir_pfo(nir_shader *shader)
55 {
56    nir_function_impl *impl = nir_shader_get_entrypoint(shader);
57    nir_builder b;
58 
59    /* Only apply to fragment shaders. */
60    if (shader->info.stage != MESA_SHADER_FRAGMENT)
61       return;
62 
63    b = nir_builder_create(impl);
64 
65    nir_foreach_block (block, impl) {
66       nir_foreach_instr_safe (instr, block) {
67          if (instr->type == nir_instr_type_intrinsic) {
68             /* Find the store_output intrinsic and pack the output value. */
69             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
70 
71             if (intr->intrinsic != nir_intrinsic_store_output)
72                continue;
73 
74             b.cursor = nir_before_instr(&intr->instr);
75             insert_pfo(&b, intr, &intr->src[0]);
76          } else if (instr->type == nir_instr_type_deref) {
77             /* Find variable derefs and update their type. */
78             nir_deref_instr *deref = nir_instr_as_deref(instr);
79 
80             if (!nir_deref_mode_is(deref, nir_var_shader_out))
81                continue;
82 
83             if (deref->deref_type != nir_deref_type_var)
84                continue;
85 
86             nir_variable *out = nir_deref_instr_get_variable(deref);
87 
88             deref->type = glsl_uintN_t_type(32);
89             out->type = glsl_uintN_t_type(32);
90          }
91       }
92    }
93 }
94