1 /*
2 * Copyright (C) 2020-2023 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 "compiler/nir/nir_builder.h"
25 #include "pan_context.h"
26
27 struct ctx {
28 unsigned arch;
29 struct panfrost_sysvals *sysvals;
30 struct hash_table_u64 *sysval_to_id;
31 unsigned sysval_ubo;
32 };
33
34 static unsigned
lookup_sysval(struct hash_table_u64 * sysval_to_id,struct panfrost_sysvals * sysvals,int sysval)35 lookup_sysval(struct hash_table_u64 *sysval_to_id,
36 struct panfrost_sysvals *sysvals, int sysval)
37 {
38 /* Try to lookup */
39 void *cached = _mesa_hash_table_u64_search(sysval_to_id, sysval);
40
41 if (cached) {
42 unsigned id = ((uintptr_t)cached) - 1;
43 assert(id < MAX_SYSVAL_COUNT);
44 assert(sysvals->sysvals[id] == sysval);
45 return id;
46 }
47
48 /* Else assign */
49 unsigned id = sysvals->sysval_count++;
50 assert(id < MAX_SYSVAL_COUNT);
51 _mesa_hash_table_u64_insert(sysval_to_id, sysval,
52 (void *)((uintptr_t)id + 1));
53 sysvals->sysvals[id] = sysval;
54 return id;
55 }
56
57 static unsigned
sysval_for_intrinsic(unsigned arch,nir_intrinsic_instr * intr,unsigned * offset)58 sysval_for_intrinsic(unsigned arch, nir_intrinsic_instr *intr, unsigned *offset)
59 {
60 switch (intr->intrinsic) {
61 case nir_intrinsic_load_ssbo_address:
62 if (arch >= 9)
63 return ~0;
64
65 assert(nir_src_as_uint(intr->src[1]) == 0);
66 return PAN_SYSVAL(SSBO, nir_src_as_uint(intr->src[0]));
67 case nir_intrinsic_get_ssbo_size:
68 *offset = 8;
69 return PAN_SYSVAL(SSBO, nir_src_as_uint(intr->src[0]));
70
71 case nir_intrinsic_load_sampler_lod_parameters_pan:
72 /* This is only used for a workaround on Mali-T720, where we don't
73 * support dynamic samplers.
74 */
75 return PAN_SYSVAL(SAMPLER, nir_src_as_uint(intr->src[0]));
76
77 case nir_intrinsic_load_xfb_address:
78 return PAN_SYSVAL(XFB, nir_intrinsic_base(intr));
79
80 case nir_intrinsic_load_work_dim:
81 return PAN_SYSVAL_WORK_DIM;
82
83 case nir_intrinsic_load_sample_positions_pan:
84 return PAN_SYSVAL_SAMPLE_POSITIONS;
85
86 case nir_intrinsic_load_num_vertices:
87 return PAN_SYSVAL_NUM_VERTICES;
88
89 case nir_intrinsic_load_first_vertex:
90 return PAN_SYSVAL_VERTEX_INSTANCE_OFFSETS;
91 case nir_intrinsic_load_base_vertex:
92 *offset = 4;
93 return PAN_SYSVAL_VERTEX_INSTANCE_OFFSETS;
94 case nir_intrinsic_load_base_instance:
95 *offset = 8;
96 return PAN_SYSVAL_VERTEX_INSTANCE_OFFSETS;
97
98 case nir_intrinsic_load_draw_id:
99 if (arch >= 10)
100 return ~0;
101
102 return PAN_SYSVAL_DRAWID;
103
104 case nir_intrinsic_load_multisampled_pan:
105 return PAN_SYSVAL_MULTISAMPLED;
106
107 case nir_intrinsic_load_viewport_scale:
108 return PAN_SYSVAL_VIEWPORT_SCALE;
109
110 case nir_intrinsic_load_viewport_offset:
111 return PAN_SYSVAL_VIEWPORT_OFFSET;
112
113 case nir_intrinsic_load_num_workgroups:
114 return PAN_SYSVAL_NUM_WORK_GROUPS;
115
116 case nir_intrinsic_load_workgroup_size:
117 return PAN_SYSVAL_LOCAL_GROUP_SIZE;
118
119 case nir_intrinsic_load_rt_conversion_pan: {
120 unsigned size = nir_alu_type_get_type_size(nir_intrinsic_src_type(intr));
121 unsigned rt = nir_intrinsic_base(intr);
122
123 return PAN_SYSVAL(RT_CONVERSION, rt | (size << 4));
124 }
125
126 case nir_intrinsic_image_size: {
127 uint32_t uindex = nir_src_as_uint(intr->src[0]);
128 bool is_array = nir_intrinsic_image_array(intr);
129 unsigned dim = nir_intrinsic_dest_components(intr) - is_array;
130
131 return PAN_SYSVAL(IMAGE_SIZE, PAN_TXS_SYSVAL_ID(uindex, dim, is_array));
132 }
133
134 default:
135 return ~0;
136 }
137 }
138
139 static bool
lower(nir_builder * b,nir_instr * instr,void * data)140 lower(nir_builder *b, nir_instr *instr, void *data)
141 {
142 struct ctx *ctx = data;
143 nir_def *old = NULL;
144 unsigned sysval = ~0, offset = 0;
145 b->cursor = nir_before_instr(instr);
146
147 if (instr->type == nir_instr_type_intrinsic) {
148 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
149 old = &intr->def;
150 sysval = sysval_for_intrinsic(ctx->arch, intr, &offset);
151
152 if (sysval == ~0)
153 return false;
154 } else if (instr->type == nir_instr_type_tex) {
155 nir_tex_instr *tex = nir_instr_as_tex(instr);
156 old = &tex->def;
157
158 if (tex->op != nir_texop_txs)
159 return false;
160
161 /* XXX: This is broken for dynamic indexing */
162 sysval = PAN_SYSVAL(TEXTURE_SIZE,
163 PAN_TXS_SYSVAL_ID(tex->texture_index,
164 nir_tex_instr_dest_size(tex) -
165 (tex->is_array ? 1 : 0),
166 tex->is_array));
167 } else {
168 return false;
169 }
170
171 /* Allocate a UBO for the sysvals if we haven't yet */
172 if (ctx->sysvals->sysval_count == 0)
173 ctx->sysval_ubo = b->shader->info.num_ubos++;
174
175 unsigned vec4_index = lookup_sysval(ctx->sysval_to_id, ctx->sysvals, sysval);
176 unsigned ubo_offset = (vec4_index * 16) + offset;
177
178 b->cursor = nir_after_instr(instr);
179 nir_def *val = nir_load_ubo(
180 b, old->num_components, old->bit_size, nir_imm_int(b, ctx->sysval_ubo),
181 nir_imm_int(b, ubo_offset), .align_mul = old->bit_size / 8,
182 .align_offset = 0, .range_base = offset, .range = old->bit_size / 8);
183 nir_def_rewrite_uses(old, val);
184 return true;
185 }
186
187 bool
panfrost_nir_lower_sysvals(nir_shader * shader,unsigned arch,struct panfrost_sysvals * sysvals)188 panfrost_nir_lower_sysvals(nir_shader *shader, unsigned arch,
189 struct panfrost_sysvals *sysvals)
190 {
191 bool progress = false;
192
193 /* The lowerings for SSBOs, etc require constants, so fold now */
194 do {
195 progress = false;
196
197 NIR_PASS(progress, shader, nir_copy_prop);
198 NIR_PASS(progress, shader, nir_opt_constant_folding);
199 NIR_PASS(progress, shader, nir_opt_dce);
200 } while (progress);
201
202 struct ctx ctx = {
203 .arch = arch,
204 .sysvals = sysvals,
205 .sysval_to_id = _mesa_hash_table_u64_create(NULL),
206 };
207
208 memset(sysvals, 0, sizeof(*sysvals));
209
210 nir_shader_instructions_pass(
211 shader, lower, nir_metadata_control_flow, &ctx);
212
213 _mesa_hash_table_u64_destroy(ctx.sysval_to_id);
214 return true;
215 }
216