panfrost: Factor batch/resource out of instancing routines
[mesa.git] / src / gallium / drivers / panfrost / pan_attributes.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig
3 * Copyright (C) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include "pan_bo.h"
27 #include "pan_context.h"
28
29 /* See mali_job for notes on how this works. But basically, for small vertex
30 * counts, we have a lookup table, and for large vertex counts, we look at the
31 * high bits as a heuristic. This has to match exactly how the hardware
32 * calculates this (which is why the algorithm is so weird) or else instancing
33 * will break. */
34
35 /* Given an odd number (of the form 2k + 1), compute k */
36 #define ODD(odd) ((odd - 1) >> 1)
37
38 /* Given the shift/odd pair, recover the original padded integer */
39
40 unsigned
41 pan_expand_shift_odd(struct pan_shift_odd o)
42 {
43 unsigned odd = 2*o.odd + 1;
44 unsigned shift = 1 << o.shift;
45 return odd * shift;
46 }
47
48 static inline struct pan_shift_odd
49 pan_factored(unsigned pot, unsigned odd)
50 {
51 struct pan_shift_odd out;
52
53 assert(util_is_power_of_two_or_zero(pot));
54 assert(odd & 1);
55
56 /* Odd is of the form (2k + 1) = (k << 1) + 1 = (k << 1) | 1.
57 *
58 * So (odd >> 1) = ((k << 1) | 1) >> 1 = ((k << 1) >> 1) | (1 >> 1)
59 * = k | 0 = k */
60
61 out.odd = (odd >> 1);
62
63 /* POT is the form (1 << shift) */
64 out.shift = __builtin_ctz(pot);
65
66 return out;
67 }
68
69
70 /* For small vertices. Second argument is whether the primitive takes a
71 * power-of-two argument, which determines how rounding works. True for POINTS
72 * and LINES, false for TRIANGLES. Presumably true for QUADS but you'd be crazy
73 * to try instanced quads on ES class hardware <3 */
74
75 static struct {
76 unsigned pot;
77 unsigned odd;
78 } small_lut[] = {
79 { 0, 1 },
80 { 1, 1 },
81 { 2, 1 },
82 { 1, 3 },
83 { 4, 1 },
84 { 1, 5 },
85 { 2, 3 },
86 { 1, 7 },
87 { 8, 1 },
88 { 1, 9 },
89 { 2, 5 },
90 { 4, 3 }, /* 11 */
91 { 4, 3 },
92 { 2, 7 }, /* 13 */
93 { 2, 7 },
94 { 16, 1 }, /* 15 */
95 { 16, 1 },
96 { 2, 9 },
97 { 4, 5 }, /* 20 */
98 { 4, 5 }
99 };
100
101 static struct pan_shift_odd
102 panfrost_small_padded_vertex_count(unsigned idx)
103 {
104 return pan_factored(
105 small_lut[idx].pot,
106 small_lut[idx].odd);
107 }
108
109 static struct pan_shift_odd
110 panfrost_large_padded_vertex_count(uint32_t vertex_count)
111 {
112 struct pan_shift_odd out = { 0 };
113
114 /* First, we have to find the highest set one */
115 unsigned highest = 32 - __builtin_clz(vertex_count);
116
117 /* Using that, we mask out the highest 4-bits */
118 unsigned n = highest - 4;
119 unsigned nibble = (vertex_count >> n) & 0xF;
120
121 /* Great, we have the nibble. Now we can just try possibilities. Note
122 * that we don't care about the bottom most bit in most cases, and we
123 * know the top bit must be 1 */
124
125 unsigned middle_two = (nibble >> 1) & 0x3;
126
127 switch (middle_two) {
128 case 0b00:
129 if (nibble & 1)
130 return pan_factored(1 << n, 9);
131 else
132 return pan_factored(1 << (n + 1), 5);
133 case 0b01:
134 return pan_factored(1 << (n + 2), 3);
135 case 0b10:
136 return pan_factored(1 << (n + 1), 7);
137 case 0b11:
138 return pan_factored(1 << (n + 4), 1);
139 default:
140 unreachable("Invalid two bits");
141 }
142
143 return out;
144 }
145
146 struct pan_shift_odd
147 panfrost_padded_vertex_count(
148 unsigned vertex_count,
149 bool pot)
150 {
151 assert(vertex_count > 0);
152
153 if (vertex_count < 20) {
154 /* Add an off-by-one if it won't align naturally (quirk of the hardware) */
155 //if (!pot)
156 // vertex_count++;
157
158 return panfrost_small_padded_vertex_count(vertex_count);
159 } else
160 return panfrost_large_padded_vertex_count(vertex_count);
161 }
162
163 /* The much, much more irritating case -- instancing is enabled. See
164 * panfrost_job.h for notes on how this works */
165
166 static unsigned
167 panfrost_vertex_instanced(
168 unsigned padded_count,
169 unsigned instance_shift, unsigned instance_odd,
170 unsigned divisor,
171 union mali_attr *attrs)
172 {
173 /* Depending if there is an instance divisor or not, packing varies.
174 * When there is a divisor, the hardware-level divisor is actually the
175 * product of the instance divisor and the padded count */
176
177 unsigned hw_divisor = padded_count * divisor;
178
179 if (divisor == 0) {
180 /* Per-vertex attributes use the MODULO mode. First, compute
181 * the modulus */
182
183 attrs->elements |= MALI_ATTR_MODULO;
184 attrs->shift = instance_shift;
185 attrs->extra_flags = instance_odd;
186
187 return 1;
188 } else if (util_is_power_of_two_or_zero(hw_divisor)) {
189 /* If there is a divisor but the hardware divisor works out to
190 * a power of two (not terribly exceptional), we can use an
191 * easy path (just shifting) */
192
193 attrs->elements |= MALI_ATTR_POT_DIVIDE;
194 attrs->shift = __builtin_ctz(hw_divisor);
195
196 return 1;
197 } else {
198 /* We have a NPOT divisor. Here's the fun one (multipling by
199 * the inverse and shifting) */
200
201 /* floor(log2(d)) */
202 unsigned shift = util_logbase2(hw_divisor);
203
204 /* m = ceil(2^(32 + shift) / d) */
205 uint64_t shift_hi = 32 + shift;
206 uint64_t t = 1ll << shift_hi;
207 double t_f = t;
208 double hw_divisor_d = hw_divisor;
209 double m_f = ceil(t_f / hw_divisor_d);
210 unsigned m = m_f;
211
212 /* Default case */
213 uint32_t magic_divisor = m, extra_flags = 0;
214
215 /* e = 2^(shift + 32) % d */
216 uint64_t e = t % hw_divisor;
217
218 /* Apply round-down algorithm? e <= 2^shift?. XXX: The blob
219 * seems to use a different condition */
220 if (e <= (1ll << shift)) {
221 magic_divisor = m - 1;
222 extra_flags = 1;
223 }
224
225 /* Top flag implicitly set */
226 assert(magic_divisor & (1u << 31));
227 magic_divisor &= ~(1u << 31);
228
229 /* Upload to two different slots */
230
231 attrs[0].elements |= MALI_ATTR_NPOT_DIVIDE;
232 attrs[0].shift = shift;
233 attrs[0].extra_flags = extra_flags;
234
235 attrs[1].unk = 0x20;
236 attrs[1].magic_divisor = magic_divisor;
237 attrs[1].zero = 0;
238 attrs[1].divisor = divisor;
239
240 return 2;
241 }
242 }
243
244 void
245 panfrost_emit_vertex_data(struct panfrost_batch *batch)
246 {
247 struct panfrost_context *ctx = batch->ctx;
248 struct panfrost_vertex_state *so = ctx->vertex;
249
250 /* Staged mali_attr, and index into them. i =/= k, depending on the
251 * vertex buffer mask and instancing. Twice as much room is allocated,
252 * for a worst case of NPOT_DIVIDEs which take up extra slot */
253 union mali_attr attrs[PIPE_MAX_ATTRIBS * 2];
254 unsigned k = 0;
255
256 unsigned vertex_count = ctx->vertex_count;
257 unsigned instanced_count = ctx->instance_count;
258
259 for (unsigned i = 0; i < so->num_elements; ++i) {
260 /* We map a mali_attr to be 1:1 with the mali_attr_meta, which
261 * means duplicating some vertex buffers (who cares? aside from
262 * maybe some caching implications but I somehow doubt that
263 * matters) */
264
265 struct pipe_vertex_element *elem = &so->pipe[i];
266 unsigned vbi = elem->vertex_buffer_index;
267
268 /* The exception to 1:1 mapping is that we can have multiple
269 * entries (NPOT divisors), so we fixup anyways */
270
271 so->hw[i].index = k;
272
273 if (!(ctx->vb_mask & (1 << vbi))) continue;
274
275 struct pipe_vertex_buffer *buf = &ctx->vertex_buffers[vbi];
276 struct panfrost_resource *rsrc = (struct panfrost_resource *) (buf->buffer.resource);
277
278 if (!rsrc) continue;
279
280 /* Align to 64 bytes by masking off the lower bits. This
281 * will be adjusted back when we fixup the src_offset in
282 * mali_attr_meta */
283
284 mali_ptr raw_addr = rsrc->bo->gpu + buf->buffer_offset;
285 mali_ptr addr = raw_addr & ~63;
286 unsigned chopped_addr = raw_addr - addr;
287
288 /* Add a dependency of the batch on the vertex buffer */
289 panfrost_batch_add_bo(batch, rsrc->bo,
290 PAN_BO_ACCESS_SHARED |
291 PAN_BO_ACCESS_READ |
292 PAN_BO_ACCESS_VERTEX_TILER);
293
294 /* Set common fields */
295 attrs[k].elements = addr;
296 attrs[k].stride = buf->stride;
297
298 /* Since we advanced the base pointer, we shrink the buffer
299 * size */
300 attrs[k].size = rsrc->base.width0 - buf->buffer_offset;
301
302 /* We need to add the extra size we masked off (for
303 * correctness) so the data doesn't get clamped away */
304 attrs[k].size += chopped_addr;
305
306 /* For non-instancing make sure we initialize */
307 attrs[k].shift = attrs[k].extra_flags = 0;
308
309 /* Instancing uses a dramatically different code path than
310 * linear, so dispatch for the actual emission now that the
311 * common code is finished */
312
313 unsigned divisor = elem->instance_divisor;
314
315 if (divisor && instanced_count == 1) {
316 /* Silly corner case where there's a divisor(=1) but
317 * there's no legitimate instancing. So we want *every*
318 * attribute to be the same. So set stride to zero so
319 * we don't go anywhere. */
320
321 attrs[k].size = attrs[k].stride + chopped_addr;
322 attrs[k].stride = 0;
323 attrs[k++].elements |= MALI_ATTR_LINEAR;
324 } else if (instanced_count <= 1) {
325 /* Normal, non-instanced attributes */
326 attrs[k++].elements |= MALI_ATTR_LINEAR;
327 } else {
328 unsigned instance_shift = batch->ctx->payloads[PIPE_SHADER_FRAGMENT].instance_shift;
329 unsigned instance_odd = batch->ctx->payloads[PIPE_SHADER_FRAGMENT].instance_odd;
330
331 k += panfrost_vertex_instanced(batch->ctx->padded_count,
332 instance_shift, instance_odd, divisor, &attrs[k]);
333 }
334 }
335
336 /* Upload whatever we emitted and go */
337
338 ctx->payloads[PIPE_SHADER_VERTEX].postfix.attributes =
339 panfrost_upload_transient(batch, attrs, k * sizeof(union mali_attr));
340 }
341
342