Debian Codegraph @

src/panfrost/compiler/pan_nir_resize_varying_io.c

raw
1 /*
2 * Copyright (c) 2025 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is 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
18 * THE 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 "pan_nir.h"
25 #include "nir_builder.h"
26
27 static bool
28 resize_io_intr(nir_builder *b, nir_intrinsic_instr *intr, void *data)
29 {
30 const struct pan_varying_layout *varying_layout = data;
31
32 bool is_load;
33 switch (intr->intrinsic) {
34 case nir_intrinsic_store_output:
35 case nir_intrinsic_store_per_view_output:
36 if (b->shader->info.stage != MESA_SHADER_VERTEX)
37 return false;
38 is_load = false;
39 break;
40
41 case nir_intrinsic_load_input:
42 case nir_intrinsic_load_interpolated_input:
43 if (b->shader->info.stage != MESA_SHADER_FRAGMENT)
44 return false;
45 is_load = true;
46 break;
47
48 default:
49 return false;
50 }
51
52 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
53 const struct pan_varying_slot *slot =
54 pan_varying_layout_find_slot(varying_layout, sem.location);
55 if (slot == NULL) {
56 if (is_load) {
57 b->cursor = nir_after_instr(&intr->instr);
58
59 nir_def *zero = nir_imm_zero(b, intr->def.num_components,
60 intr->def.bit_size);
61 nir_def_replace(&intr->def, zero);
62 } else {
63 assert(!"We should never have a mismatch on outputs");
64 nir_instr_remove(&intr->instr);
65 }
66 return true;
67 }
68
69 nir_alu_type data_type;
70 if (is_load) {
71 data_type = nir_intrinsic_dest_type(intr);
72 assert(intr->def.bit_size == nir_alu_type_get_type_size(data_type));
73 } else {
74 data_type = nir_intrinsic_src_type(intr);
75 assert(nir_src_bit_size(intr->src[0]) ==
76 nir_alu_type_get_type_size(data_type));
77 }
78
79 const unsigned slot_bit_size = nir_alu_type_get_type_size(slot->alu_type);
80
81 /* We trust the base type in the shader and only adjust the bit size */
82 const nir_alu_type slot_type =
83 nir_alu_type_get_base_type(data_type) | slot_bit_size;
84
85 if (slot_bit_size == nir_alu_type_get_type_size(data_type)) {
86 if (!sem.medium_precision)
87 return false;
88
89 /* There's nothing to actually lower but we still want to smash off
90 * mediump so the back-end doesn't screw anything up on us.
91 *
92 * TODO: This is a hack to work around the back-end. It really
93 * shouldn't care and should just do whatever load it's told.
94 */
95 sem.medium_precision = false;
96 nir_intrinsic_set_io_semantics(intr, sem);
97 return true;
98 }
99
100 sem.medium_precision = false;
101 nir_intrinsic_set_io_semantics(intr, sem);
102
103 if (is_load) {
104 b->cursor = nir_after_instr(&intr->instr);
105
106 intr->def.bit_size = slot_bit_size;
107 nir_intrinsic_set_dest_type(intr, slot_type);
108
109 data = nir_type_convert(b, &intr->def, slot_type, data_type,
110 nir_rounding_mode_undef);
111
112 nir_def_rewrite_uses_after(&intr->def, data);
113 } else {
114 b->cursor = nir_before_instr(&intr->instr);
115
116 nir_def *data = nir_type_convert(b, intr->src[0].ssa, data_type,
117 slot_type, nir_rounding_mode_undef);
118
119 nir_src_rewrite(&intr->src[0], data);
120 nir_intrinsic_set_src_type(intr, slot_type);
121 }
122
123 return true;
124 }
125
126 bool
127 pan_nir_resize_varying_io(nir_shader *nir,
128 const struct pan_varying_layout *varying_layout)
129 {
130 return nir_shader_intrinsics_pass(nir, resize_io_intr,
131 nir_metadata_control_flow,
132 (void *)varying_layout);
133 }