Debian Codegraph @

src/panfrost/compiler/pan_nir_lower_sample_position.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "compiler/nir/nir_builder.h"
7 #include "pan_nir.h"
8
9 /* Sample positions are supplied in a packed 8:8 fixed-point vec2 format in GPU
10 * memory indexed by the sample. We lower in NIR to take advantage of possible
11 * ALU optimizations at the end. This is convenient for Bifrost, since the
12 * sample positions are passed in this format and it saves the driver from any
13 * system value handling. For Midgard, it's a bit suboptimal (fp16 positions
14 * could be supplied directly), but this lets us unify the implementation, and
15 * it's a pretty trivial difference */
16
17 static bool
18 pan_lower_sample_pos_impl(struct nir_builder *b, nir_intrinsic_instr *intr,
19 UNUSED void *data)
20 {
21 if (intr->intrinsic != nir_intrinsic_load_sample_pos &&
22 intr->intrinsic != nir_intrinsic_load_sample_pos_or_center)
23 return false;
24
25 b->cursor = nir_before_instr(&intr->instr);
26
27 if (!b->shader->info.fs.uses_sample_shading) {
28 assert(intr->intrinsic == nir_intrinsic_load_sample_pos_or_center);
29
30 /* When sample shading is disabled, lower to a constant (0.5,0.5).
31 *
32 * In Vulkan, sample shading state is always known statically. In
33 * OpenGL, it's possible to enable sample shading dynamically. The only
34 * thing that currently emits load_sample_pos_or_center is
35 * nir_lower_wpos_center, which is only used for Vulkan, so this is
36 * okay.
37 *
38 * In the case where multisample is disabled but sample shading is
39 * enabled , we would skip this branch and load (0.5,0.5) from index 0
40 * in the sample pos table.
41 *
42 * In theory we should get r61[13:23]=32 on Bifrost when sample shading
43 * is disabled, and can load (0.5,0.5) from sample_positions[32] with
44 * the same code we use for loading normal sample positions. This would
45 * allow dynamic sample shading state, but would require passing the raw
46 * sample ID register through to NIR. */
47 nir_def_replace(&intr->def, nir_imm_vec2(b, 0.5, 0.5));
48
49 return true;
50 }
51
52 /* Elements are 4 bytes */
53 nir_def *addr =
54 nir_iadd(b, nir_load_sample_positions_pan(b),
55 nir_u2u64(b, nir_imul_imm(b, nir_load_sample_id(b), 4)));
56
57 /* Decode 8:8 fixed-point */
58 nir_def *raw = nir_load_global(b, 2, 16, addr);
59 nir_def *decoded = nir_fmul_imm(b, nir_i2f16(b, raw), 1.0 / 256.0);
60
61 /* Make NIR validator happy */
62 if (decoded->bit_size != intr->def.bit_size)
63 decoded = nir_f2fN(b, decoded, intr->def.bit_size);
64
65 nir_def_rewrite_uses(&intr->def, decoded);
66 return true;
67 }
68
69 bool
70 pan_nir_lower_sample_pos(nir_shader *shader)
71 {
72 if (shader->info.stage != MESA_SHADER_FRAGMENT)
73 return false;
74
75 return nir_shader_intrinsics_pass(
76 shader, pan_lower_sample_pos_impl,
77 nir_metadata_control_flow, NULL);
78 }