1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora, Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is 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
18bf215546Sopenharmony_ci * THE 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 "pan_ir.h"
25bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci/* Sample positions are supplied in a packed 8:8 fixed-point vec2 format in GPU
28bf215546Sopenharmony_ci * memory indexed by the sample. We lower in NIR to take advantage of possible
29bf215546Sopenharmony_ci * ALU optimizations at the end. This is convenient for Bifrost, since the
30bf215546Sopenharmony_ci * sample positions are passed in this format and it saves the driver from any
31bf215546Sopenharmony_ci * system value handling. For Midgard, it's a bit suboptimal (fp16 positions
32bf215546Sopenharmony_ci * could be supplied directly), but this lets us unify the implementation, and
33bf215546Sopenharmony_ci * it's a pretty trivial difference */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic bool
36bf215546Sopenharmony_cipan_lower_sample_pos_impl(struct nir_builder *b,
37bf215546Sopenharmony_ci                nir_instr *instr, UNUSED void *data)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci        if (instr->type != nir_instr_type_intrinsic)
40bf215546Sopenharmony_ci                return false;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci        nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
43bf215546Sopenharmony_ci        if (intr->intrinsic != nir_intrinsic_load_sample_pos)
44bf215546Sopenharmony_ci                return false;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci        b->cursor = nir_before_instr(instr);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci        /* Elements are 4 bytes */
49bf215546Sopenharmony_ci        nir_ssa_def *addr = nir_iadd(b,
50bf215546Sopenharmony_ci                        nir_load_sample_positions_pan(b),
51bf215546Sopenharmony_ci                        nir_u2u64(b, nir_imul_imm(b, nir_load_sample_id(b), 4)));
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci        /* Decode 8:8 fixed-point */
54bf215546Sopenharmony_ci        nir_ssa_def *raw = nir_load_global(b, addr, 2, 2, 16);
55bf215546Sopenharmony_ci        nir_ssa_def *decoded = nir_fmul_imm(b, nir_i2f16(b, raw), 1.0 / 256.0);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci        /* Make NIR validator happy */
58bf215546Sopenharmony_ci        if (decoded->bit_size != nir_dest_bit_size(intr->dest))
59bf215546Sopenharmony_ci                decoded = nir_f2fN(b, decoded, nir_dest_bit_size(intr->dest));
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci        nir_ssa_def_rewrite_uses(&intr->dest.ssa, decoded);
62bf215546Sopenharmony_ci        return true;
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cibool
66bf215546Sopenharmony_cipan_lower_sample_pos(nir_shader *shader)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci        if (shader->info.stage != MESA_SHADER_FRAGMENT)
69bf215546Sopenharmony_ci                return false;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci        return nir_shader_instructions_pass(shader,
72bf215546Sopenharmony_ci                        pan_lower_sample_pos_impl,
73bf215546Sopenharmony_ci                        nir_metadata_block_index | nir_metadata_dominance,
74bf215546Sopenharmony_ci                        NULL);
75bf215546Sopenharmony_ci}
76