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