1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2022 Imagination Technologies Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
5bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
6bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights
7bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
9bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18bf215546Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir/nir.h"
25bf215546Sopenharmony_ci#include "nir/nir_builder.h"
26bf215546Sopenharmony_ci#include "nir/nir_search_helpers.h"
27bf215546Sopenharmony_ci#include "rogue_nir.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_cistatic void insert_pfo(nir_builder *b,
30bf215546Sopenharmony_ci                       nir_intrinsic_instr *store_output,
31bf215546Sopenharmony_ci                       nir_src *output_src)
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_ci   /* TODO: Support complex PFO with blending. */
34bf215546Sopenharmony_ci   /* TODO: Verify type is vec4. */
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci   /* Pack the output color components into U8888 format. */
37bf215546Sopenharmony_ci   nir_ssa_def *new_output_src_ssa = nir_pack_unorm_4x8(b, output_src->ssa);
38bf215546Sopenharmony_ci   nir_src new_output_src = nir_src_for_ssa(new_output_src_ssa);
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci   /* Update the store_output intrinsic. */
41bf215546Sopenharmony_ci   nir_instr_rewrite_src(&store_output->instr, output_src, new_output_src);
42bf215546Sopenharmony_ci   nir_intrinsic_set_write_mask(store_output, 1);
43bf215546Sopenharmony_ci   store_output->num_components = 1;
44bf215546Sopenharmony_ci   nir_intrinsic_set_src_type(store_output, nir_type_uint32);
45bf215546Sopenharmony_ci}
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_civoid rogue_nir_pfo(nir_shader *shader)
48bf215546Sopenharmony_ci{
49bf215546Sopenharmony_ci   nir_function_impl *impl = nir_shader_get_entrypoint(shader);
50bf215546Sopenharmony_ci   nir_builder b;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   /* Only apply to fragment shaders. */
53bf215546Sopenharmony_ci   if (shader->info.stage != MESA_SHADER_FRAGMENT)
54bf215546Sopenharmony_ci      return;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   nir_foreach_block (block, impl) {
59bf215546Sopenharmony_ci      nir_foreach_instr_safe (instr, block) {
60bf215546Sopenharmony_ci         if (instr->type == nir_instr_type_intrinsic) {
61bf215546Sopenharmony_ci            /* Find the store_output intrinsic and pack the output value. */
62bf215546Sopenharmony_ci            nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci            if (intr->intrinsic != nir_intrinsic_store_output)
65bf215546Sopenharmony_ci               continue;
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci            b.cursor = nir_before_instr(&intr->instr);
68bf215546Sopenharmony_ci            insert_pfo(&b, intr, &intr->src[0]);
69bf215546Sopenharmony_ci         } else if (instr->type == nir_instr_type_deref) {
70bf215546Sopenharmony_ci            /* Find variable derefs and update their type. */
71bf215546Sopenharmony_ci            nir_deref_instr *deref = nir_instr_as_deref(instr);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci            if (!nir_deref_mode_is(deref, nir_var_shader_out))
74bf215546Sopenharmony_ci               continue;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci            if (deref->deref_type != nir_deref_type_var)
77bf215546Sopenharmony_ci               continue;
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci            nir_variable *out = nir_deref_instr_get_variable(deref);
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci            deref->type = glsl_uintN_t_type(32);
82bf215546Sopenharmony_ci            out->type = glsl_uintN_t_type(32);
83bf215546Sopenharmony_ci         }
84bf215546Sopenharmony_ci      }
85bf215546Sopenharmony_ci   }
86bf215546Sopenharmony_ci}
87