1 /*
2 * Copyright (C) 2020 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 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 #include "pan_ir.h"
28 #include "compiler/nir/nir_builder.h"
29
30 /* TODO: ssbo_size */
31 static int
panfrost_sysval_for_ssbo(nir_intrinsic_instr * instr)32 panfrost_sysval_for_ssbo(nir_intrinsic_instr *instr)
33 {
34 nir_src index = instr->src[0];
35 assert(nir_src_is_const(index));
36 uint32_t uindex = nir_src_as_uint(index);
37
38 return PAN_SYSVAL(SSBO, uindex);
39 }
40
41 static int
panfrost_sysval_for_sampler(nir_intrinsic_instr * instr)42 panfrost_sysval_for_sampler(nir_intrinsic_instr *instr)
43 {
44 /* TODO: indirect samplers !!! */
45 nir_src index = instr->src[0];
46 assert(nir_src_is_const(index));
47 uint32_t uindex = nir_src_as_uint(index);
48
49 return PAN_SYSVAL(SAMPLER, uindex);
50 }
51
52 static int
panfrost_sysval_for_image_size(nir_intrinsic_instr * instr)53 panfrost_sysval_for_image_size(nir_intrinsic_instr *instr)
54 {
55 nir_src index = instr->src[0];
56 assert(nir_src_is_const(index));
57
58 bool is_array = nir_intrinsic_image_array(instr);
59 uint32_t uindex = nir_src_as_uint(index);
60 unsigned dim = nir_intrinsic_dest_components(instr) - is_array;
61
62 return PAN_SYSVAL(IMAGE_SIZE, PAN_TXS_SYSVAL_ID(uindex, dim, is_array));
63 }
64
65 static unsigned
panfrost_nir_sysval_for_intrinsic(nir_intrinsic_instr * instr)66 panfrost_nir_sysval_for_intrinsic(nir_intrinsic_instr *instr)
67 {
68 switch (instr->intrinsic) {
69 case nir_intrinsic_load_viewport_scale:
70 return PAN_SYSVAL_VIEWPORT_SCALE;
71 case nir_intrinsic_load_viewport_offset:
72 return PAN_SYSVAL_VIEWPORT_OFFSET;
73 case nir_intrinsic_load_num_workgroups:
74 return PAN_SYSVAL_NUM_WORK_GROUPS;
75 case nir_intrinsic_load_workgroup_size:
76 return PAN_SYSVAL_LOCAL_GROUP_SIZE;
77 case nir_intrinsic_load_work_dim:
78 return PAN_SYSVAL_WORK_DIM;
79 case nir_intrinsic_load_sample_positions_pan:
80 return PAN_SYSVAL_SAMPLE_POSITIONS;
81 case nir_intrinsic_load_first_vertex:
82 case nir_intrinsic_load_base_vertex:
83 case nir_intrinsic_load_base_instance:
84 return PAN_SYSVAL_VERTEX_INSTANCE_OFFSETS;
85 case nir_intrinsic_load_draw_id:
86 return PAN_SYSVAL_DRAWID;
87 case nir_intrinsic_load_ssbo_address:
88 case nir_intrinsic_get_ssbo_size:
89 return panfrost_sysval_for_ssbo(instr);
90 case nir_intrinsic_load_xfb_address:
91 return PAN_SYSVAL(XFB, nir_intrinsic_base(instr));
92 case nir_intrinsic_load_num_vertices:
93 return PAN_SYSVAL_NUM_VERTICES;
94 case nir_intrinsic_load_sampler_lod_parameters_pan:
95 return panfrost_sysval_for_sampler(instr);
96 case nir_intrinsic_image_size:
97 return panfrost_sysval_for_image_size(instr);
98 case nir_intrinsic_load_blend_const_color_rgba:
99 return PAN_SYSVAL_BLEND_CONSTANTS;
100 default:
101 return ~0;
102 }
103 }
104
105 int
panfrost_sysval_for_instr(nir_instr * instr,nir_dest * dest)106 panfrost_sysval_for_instr(nir_instr *instr, nir_dest *dest)
107 {
108 nir_intrinsic_instr *intr;
109 nir_dest *dst = NULL;
110 nir_tex_instr *tex;
111 unsigned sysval = ~0;
112
113 switch (instr->type) {
114 case nir_instr_type_intrinsic:
115 intr = nir_instr_as_intrinsic(instr);
116 sysval = panfrost_nir_sysval_for_intrinsic(intr);
117 dst = &intr->dest;
118 break;
119 case nir_instr_type_tex:
120 tex = nir_instr_as_tex(instr);
121 if (tex->op != nir_texop_txs)
122 break;
123
124 sysval = PAN_SYSVAL(TEXTURE_SIZE,
125 PAN_TXS_SYSVAL_ID(tex->texture_index,
126 nir_tex_instr_dest_size(tex) -
127 (tex->is_array ? 1 : 0),
128 tex->is_array));
129 dst = &tex->dest;
130 break;
131 default:
132 break;
133 }
134
135 if (dest && dst)
136 *dest = *dst;
137
138 return sysval;
139 }
140
141 static unsigned
pan_add_sysval(struct hash_table_u64 * sysval_to_id,struct panfrost_sysvals * sysvals,int sysval,unsigned id)142 pan_add_sysval(struct hash_table_u64 *sysval_to_id,
143 struct panfrost_sysvals *sysvals,
144 int sysval, unsigned id)
145 {
146 assert(id < MAX_SYSVAL_COUNT);
147 _mesa_hash_table_u64_insert(sysval_to_id, sysval, (void *) ((uintptr_t) id + 1));
148 sysvals->sysvals[id] = sysval;
149 return id;
150 }
151
152 unsigned
pan_lookup_sysval(struct hash_table_u64 * sysval_to_id,struct panfrost_sysvals * sysvals,int sysval)153 pan_lookup_sysval(struct hash_table_u64 *sysval_to_id,
154 struct panfrost_sysvals *sysvals,
155 int sysval)
156 {
157 /* Try to lookup */
158
159 void *cached = _mesa_hash_table_u64_search(sysval_to_id, sysval);
160
161 if (cached) {
162 unsigned id = ((uintptr_t) cached) - 1;
163 assert(id < MAX_SYSVAL_COUNT);
164 assert(sysvals->sysvals[id] == sysval);
165 return id;
166 }
167
168 /* Else assign */
169 return pan_add_sysval(sysval_to_id, sysvals, sysval,
170 sysvals->sysval_count++);
171 }
172
173 struct hash_table_u64 *
panfrost_init_sysvals(struct panfrost_sysvals * sysvals,struct panfrost_sysvals * fixed_sysvals,void * memctx)174 panfrost_init_sysvals(struct panfrost_sysvals *sysvals,
175 struct panfrost_sysvals *fixed_sysvals,
176 void *memctx)
177 {
178 memset(sysvals, 0, sizeof(*sysvals));
179 struct hash_table_u64 *sysval_to_id =
180 _mesa_hash_table_u64_create(memctx);
181
182 if (fixed_sysvals) {
183 for (unsigned i = 0; i < fixed_sysvals->sysval_count; i++) {
184 if (!fixed_sysvals->sysvals[i])
185 continue;
186
187 pan_add_sysval(sysval_to_id, sysvals,
188 fixed_sysvals->sysvals[i], i);
189 }
190 sysvals->sysval_count = fixed_sysvals->sysval_count;
191 }
192
193 return sysval_to_id;
194 }
195