panfrost: Stop passing screen around for BO operations
[mesa.git] / src / gallium / drivers / panfrost / pan_job.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
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 <assert.h>
27
28 #include "drm-uapi/panfrost_drm.h"
29
30 #include "pan_bo.h"
31 #include "pan_context.h"
32 #include "util/hash_table.h"
33 #include "util/ralloc.h"
34 #include "util/u_format.h"
35 #include "util/u_pack_color.h"
36 #include "pan_util.h"
37 #include "pandecode/decode.h"
38
39 static struct panfrost_batch *
40 panfrost_create_batch(struct panfrost_context *ctx,
41 const struct pipe_framebuffer_state *key)
42 {
43 struct panfrost_batch *batch = rzalloc(ctx, struct panfrost_batch);
44
45 batch->ctx = ctx;
46
47 batch->bos = _mesa_set_create(batch,
48 _mesa_hash_pointer,
49 _mesa_key_pointer_equal);
50
51 batch->minx = batch->miny = ~0;
52 batch->maxx = batch->maxy = 0;
53 batch->transient_offset = 0;
54
55 util_dynarray_init(&batch->headers, batch);
56 util_dynarray_init(&batch->gpu_headers, batch);
57 util_copy_framebuffer_state(&batch->key, key);
58
59 return batch;
60 }
61
62 static void
63 panfrost_free_batch(struct panfrost_batch *batch)
64 {
65 if (!batch)
66 return;
67
68 struct panfrost_context *ctx = batch->ctx;
69
70 set_foreach(batch->bos, entry) {
71 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
72 panfrost_bo_unreference(bo);
73 }
74
75 _mesa_hash_table_remove_key(ctx->batches, &batch->key);
76
77 if (ctx->batch == batch)
78 ctx->batch = NULL;
79
80 util_unreference_framebuffer_state(&batch->key);
81 ralloc_free(batch);
82 }
83
84 static struct panfrost_batch *
85 panfrost_get_batch(struct panfrost_context *ctx,
86 const struct pipe_framebuffer_state *key)
87 {
88 /* Lookup the job first */
89 struct hash_entry *entry = _mesa_hash_table_search(ctx->batches, key);
90
91 if (entry)
92 return entry->data;
93
94 /* Otherwise, let's create a job */
95
96 struct panfrost_batch *batch = panfrost_create_batch(ctx, key);
97
98 /* Save the created job */
99 _mesa_hash_table_insert(ctx->batches, &batch->key, batch);
100
101 return batch;
102 }
103
104 /* Get the job corresponding to the FBO we're currently rendering into */
105
106 struct panfrost_batch *
107 panfrost_get_batch_for_fbo(struct panfrost_context *ctx)
108 {
109 /* If we're wallpapering, we special case to workaround
110 * u_blitter abuse */
111
112 if (ctx->wallpaper_batch)
113 return ctx->wallpaper_batch;
114
115 /* If we already began rendering, use that */
116
117 if (ctx->batch) {
118 assert(util_framebuffer_state_equal(&ctx->batch->key,
119 &ctx->pipe_framebuffer));
120 return ctx->batch;
121 }
122
123 /* If not, look up the job */
124 struct panfrost_batch *batch = panfrost_get_batch(ctx,
125 &ctx->pipe_framebuffer);
126
127 /* Set this job as the current FBO job. Will be reset when updating the
128 * FB state and when submitting or releasing a job.
129 */
130 ctx->batch = batch;
131 return batch;
132 }
133
134 void
135 panfrost_batch_add_bo(struct panfrost_batch *batch, struct panfrost_bo *bo)
136 {
137 if (!bo)
138 return;
139
140 if (_mesa_set_search(batch->bos, bo))
141 return;
142
143 panfrost_bo_reference(bo);
144 _mesa_set_add(batch->bos, bo);
145 }
146
147 /* Returns the polygon list's GPU address if available, or otherwise allocates
148 * the polygon list. It's perfectly fast to use allocate/free BO directly,
149 * since we'll hit the BO cache and this is one-per-batch anyway. */
150
151 mali_ptr
152 panfrost_batch_get_polygon_list(struct panfrost_batch *batch, unsigned size)
153 {
154 if (batch->polygon_list) {
155 assert(batch->polygon_list->size >= size);
156 } else {
157 struct panfrost_screen *screen = pan_screen(batch->ctx->base.screen);
158
159 /* Create the BO as invisible, as there's no reason to map */
160
161 batch->polygon_list = panfrost_bo_create(screen, size,
162 PAN_BO_INVISIBLE);
163 panfrost_batch_add_bo(batch, batch->polygon_list);
164
165 /* A BO reference has been retained by panfrost_batch_add_bo(),
166 * so we need to unreference it here if we want the BO to be
167 * automatically released when the batch is destroyed.
168 */
169 panfrost_bo_unreference(&screen->base, batch->polygon_list);
170 }
171
172 return batch->polygon_list->gpu;
173 }
174
175 static void
176 panfrost_batch_draw_wallpaper(struct panfrost_batch *batch)
177 {
178 /* Nothing to reload? TODO: MRT wallpapers */
179 if (batch->key.cbufs[0] == NULL)
180 return;
181
182 /* Check if the buffer has any content on it worth preserving */
183
184 struct pipe_surface *surf = batch->key.cbufs[0];
185 struct panfrost_resource *rsrc = pan_resource(surf->texture);
186 unsigned level = surf->u.tex.level;
187
188 if (!rsrc->slices[level].initialized)
189 return;
190
191 batch->ctx->wallpaper_batch = batch;
192
193 /* Clamp the rendering area to the damage extent. The
194 * KHR_partial_update() spec states that trying to render outside of
195 * the damage region is "undefined behavior", so we should be safe.
196 */
197 unsigned damage_width = (rsrc->damage.extent.maxx - rsrc->damage.extent.minx);
198 unsigned damage_height = (rsrc->damage.extent.maxy - rsrc->damage.extent.miny);
199
200 if (damage_width && damage_height) {
201 panfrost_batch_intersection_scissor(batch,
202 rsrc->damage.extent.minx,
203 rsrc->damage.extent.miny,
204 rsrc->damage.extent.maxx,
205 rsrc->damage.extent.maxy);
206 }
207
208 /* FIXME: Looks like aligning on a tile is not enough, but
209 * aligning on twice the tile size seems to works. We don't
210 * know exactly what happens here but this deserves extra
211 * investigation to figure it out.
212 */
213 batch->minx = batch->minx & ~((MALI_TILE_LENGTH * 2) - 1);
214 batch->miny = batch->miny & ~((MALI_TILE_LENGTH * 2) - 1);
215 batch->maxx = MIN2(ALIGN_POT(batch->maxx, MALI_TILE_LENGTH * 2),
216 rsrc->base.width0);
217 batch->maxy = MIN2(ALIGN_POT(batch->maxy, MALI_TILE_LENGTH * 2),
218 rsrc->base.height0);
219
220 struct pipe_scissor_state damage;
221 struct pipe_box rects[4];
222
223 /* Clamp the damage box to the rendering area. */
224 damage.minx = MAX2(batch->minx, rsrc->damage.biggest_rect.x);
225 damage.miny = MAX2(batch->miny, rsrc->damage.biggest_rect.y);
226 damage.maxx = MIN2(batch->maxx,
227 rsrc->damage.biggest_rect.x +
228 rsrc->damage.biggest_rect.width);
229 damage.maxy = MIN2(batch->maxy,
230 rsrc->damage.biggest_rect.y +
231 rsrc->damage.biggest_rect.height);
232
233 /* One damage rectangle means we can end up with at most 4 reload
234 * regions:
235 * 1: left region, only exists if damage.x > 0
236 * 2: right region, only exists if damage.x + damage.width < fb->width
237 * 3: top region, only exists if damage.y > 0. The intersection with
238 * the left and right regions are dropped
239 * 4: bottom region, only exists if damage.y + damage.height < fb->height.
240 * The intersection with the left and right regions are dropped
241 *
242 * ____________________________
243 * | | 3 | |
244 * | |___________| |
245 * | | damage | |
246 * | 1 | rect | 2 |
247 * | |___________| |
248 * | | 4 | |
249 * |_______|___________|______|
250 */
251 u_box_2d(batch->minx, batch->miny, damage.minx - batch->minx,
252 batch->maxy - batch->miny, &rects[0]);
253 u_box_2d(damage.maxx, batch->miny, batch->maxx - damage.maxx,
254 batch->maxy - batch->miny, &rects[1]);
255 u_box_2d(damage.minx, batch->miny, damage.maxx - damage.minx,
256 damage.miny - batch->miny, &rects[2]);
257 u_box_2d(damage.minx, damage.maxy, damage.maxx - damage.minx,
258 batch->maxy - damage.maxy, &rects[3]);
259
260 for (unsigned i = 0; i < 4; i++) {
261 /* Width and height are always >= 0 even if width is declared as a
262 * signed integer: u_box_2d() helper takes unsigned args and
263 * panfrost_set_damage_region() is taking care of clamping
264 * negative values.
265 */
266 if (!rects[i].width || !rects[i].height)
267 continue;
268
269 /* Blit the wallpaper in */
270 panfrost_blit_wallpaper(batch->ctx, &rects[i]);
271 }
272 batch->ctx->wallpaper_batch = NULL;
273 }
274
275 static int
276 panfrost_batch_submit_ioctl(struct panfrost_batch *batch,
277 mali_ptr first_job_desc,
278 uint32_t reqs)
279 {
280 struct panfrost_context *ctx = batch->ctx;
281 struct pipe_context *gallium = (struct pipe_context *) ctx;
282 struct panfrost_screen *screen = pan_screen(gallium->screen);
283 struct drm_panfrost_submit submit = {0,};
284 uint32_t *bo_handles;
285 int ret;
286
287 submit.in_syncs = (u64) (uintptr_t) &ctx->out_sync;
288 submit.in_sync_count = 1;
289
290 submit.out_sync = ctx->out_sync;
291
292 submit.jc = first_job_desc;
293 submit.requirements = reqs;
294
295 bo_handles = calloc(batch->bos->entries, sizeof(*bo_handles));
296 assert(bo_handles);
297
298 set_foreach(batch->bos, entry) {
299 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
300 assert(bo->gem_handle > 0);
301 bo_handles[submit.bo_handle_count++] = bo->gem_handle;
302 }
303
304 submit.bo_handles = (u64) (uintptr_t) bo_handles;
305 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
306 free(bo_handles);
307 if (ret) {
308 fprintf(stderr, "Error submitting: %m\n");
309 return errno;
310 }
311
312 /* Trace the job if we're doing that */
313 if (pan_debug & PAN_DBG_TRACE) {
314 /* Wait so we can get errors reported back */
315 drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
316 pandecode_jc(submit.jc, FALSE);
317 }
318
319 return 0;
320 }
321
322 static int
323 panfrost_batch_submit_jobs(struct panfrost_batch *batch)
324 {
325 struct panfrost_context *ctx = batch->ctx;
326 bool has_draws = batch->first_job.gpu;
327 int ret = 0;
328
329 panfrost_batch_add_bo(batch, ctx->scratchpad);
330 panfrost_batch_add_bo(batch, ctx->tiler_heap);
331
332 if (has_draws) {
333 ret = panfrost_batch_submit_ioctl(batch, batch->first_job.gpu, 0);
334 assert(!ret);
335 }
336
337 if (batch->first_tiler.gpu || batch->clear) {
338 mali_ptr fragjob = panfrost_fragment_job(batch, has_draws);
339
340 ret = panfrost_batch_submit_ioctl(batch, fragjob, PANFROST_JD_REQ_FS);
341 assert(!ret);
342 }
343
344 return ret;
345 }
346
347 void
348 panfrost_batch_submit(struct panfrost_batch *batch)
349 {
350 assert(batch);
351
352 struct panfrost_context *ctx = batch->ctx;
353 int ret;
354
355 /* Nothing to do! */
356 if (!batch->last_job.gpu && !batch->clear)
357 goto out;
358
359 if (!batch->clear && batch->last_tiler.gpu)
360 panfrost_batch_draw_wallpaper(batch);
361
362 panfrost_scoreboard_link_batch(batch);
363
364 ret = panfrost_batch_submit_jobs(batch);
365
366 if (ret)
367 fprintf(stderr, "panfrost_batch_submit failed: %d\n", ret);
368
369 out:
370 /* If this is the bound batch, the panfrost_context parameters are
371 * relevant so submitting it invalidates those paramaters, but if it's
372 * not bound, the context parameters are for some other batch so we
373 * can't invalidate them.
374 */
375 if (ctx->batch == batch)
376 panfrost_invalidate_frame(ctx);
377
378 /* The job has been submitted, let's invalidate the current FBO job
379 * cache.
380 */
381 assert(!ctx->batch || batch == ctx->batch);
382 ctx->batch = NULL;
383
384 /* We always stall the pipeline for correct results since pipelined
385 * rendering is quite broken right now (to be fixed by the panfrost_job
386 * refactor, just take the perf hit for correctness)
387 */
388 drmSyncobjWait(pan_screen(ctx->base.screen)->fd, &ctx->out_sync, 1,
389 INT64_MAX, 0, NULL);
390 panfrost_free_batch(batch);
391 }
392
393 void
394 panfrost_batch_set_requirements(struct panfrost_batch *batch)
395 {
396 struct panfrost_context *ctx = batch->ctx;
397
398 if (ctx->rasterizer && ctx->rasterizer->base.multisample)
399 batch->requirements |= PAN_REQ_MSAA;
400
401 if (ctx->depth_stencil && ctx->depth_stencil->depth.writemask)
402 batch->requirements |= PAN_REQ_DEPTH_WRITE;
403 }
404
405 /* Helper to smear a 32-bit color across 128-bit components */
406
407 static void
408 pan_pack_color_32(uint32_t *packed, uint32_t v)
409 {
410 for (unsigned i = 0; i < 4; ++i)
411 packed[i] = v;
412 }
413
414 static void
415 pan_pack_color_64(uint32_t *packed, uint32_t lo, uint32_t hi)
416 {
417 for (unsigned i = 0; i < 4; i += 2) {
418 packed[i + 0] = lo;
419 packed[i + 1] = hi;
420 }
421 }
422
423 static void
424 pan_pack_color(uint32_t *packed, const union pipe_color_union *color, enum pipe_format format)
425 {
426 /* Alpha magicked to 1.0 if there is no alpha */
427
428 bool has_alpha = util_format_has_alpha(format);
429 float clear_alpha = has_alpha ? color->f[3] : 1.0f;
430
431 /* Packed color depends on the framebuffer format */
432
433 const struct util_format_description *desc =
434 util_format_description(format);
435
436 if (util_format_is_rgba8_variant(desc)) {
437 pan_pack_color_32(packed,
438 (float_to_ubyte(clear_alpha) << 24) |
439 (float_to_ubyte(color->f[2]) << 16) |
440 (float_to_ubyte(color->f[1]) << 8) |
441 (float_to_ubyte(color->f[0]) << 0));
442 } else if (format == PIPE_FORMAT_B5G6R5_UNORM) {
443 /* First, we convert the components to R5, G6, B5 separately */
444 unsigned r5 = CLAMP(color->f[0], 0.0, 1.0) * 31.0;
445 unsigned g6 = CLAMP(color->f[1], 0.0, 1.0) * 63.0;
446 unsigned b5 = CLAMP(color->f[2], 0.0, 1.0) * 31.0;
447
448 /* Then we pack into a sparse u32. TODO: Why these shifts? */
449 pan_pack_color_32(packed, (b5 << 25) | (g6 << 14) | (r5 << 5));
450 } else if (format == PIPE_FORMAT_B4G4R4A4_UNORM) {
451 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
452 unsigned r4 = CLAMP(color->f[0], 0.0, 1.0) * 240.0;
453 unsigned g4 = CLAMP(color->f[1], 0.0, 1.0) * 240.0;
454 unsigned b4 = CLAMP(color->f[2], 0.0, 1.0) * 240.0;
455 unsigned a4 = CLAMP(clear_alpha, 0.0, 1.0) * 240.0;
456
457 /* Pack on *byte* intervals */
458 pan_pack_color_32(packed, (a4 << 24) | (b4 << 16) | (g4 << 8) | r4);
459 } else if (format == PIPE_FORMAT_B5G5R5A1_UNORM) {
460 /* Scale as expected but shift oddly */
461 unsigned r5 = round(CLAMP(color->f[0], 0.0, 1.0)) * 31.0;
462 unsigned g5 = round(CLAMP(color->f[1], 0.0, 1.0)) * 31.0;
463 unsigned b5 = round(CLAMP(color->f[2], 0.0, 1.0)) * 31.0;
464 unsigned a1 = round(CLAMP(clear_alpha, 0.0, 1.0)) * 1.0;
465
466 pan_pack_color_32(packed, (a1 << 31) | (b5 << 25) | (g5 << 15) | (r5 << 5));
467 } else {
468 /* Try Gallium's generic default path. Doesn't work for all
469 * formats but it's a good guess. */
470
471 union util_color out;
472
473 if (util_format_is_pure_integer(format)) {
474 memcpy(out.ui, color->ui, 16);
475 } else {
476 util_pack_color(color->f, format, &out);
477 }
478
479 unsigned size = util_format_get_blocksize(format);
480
481 if (size == 1) {
482 unsigned b = out.ui[0];
483 unsigned s = b | (b << 8);
484 pan_pack_color_32(packed, s | (s << 16));
485 } else if (size == 2)
486 pan_pack_color_32(packed, out.ui[0] | (out.ui[0] << 16));
487 else if (size == 4)
488 pan_pack_color_32(packed, out.ui[0]);
489 else if (size == 8)
490 pan_pack_color_64(packed, out.ui[0], out.ui[1]);
491 else if (size == 16)
492 memcpy(packed, out.ui, 16);
493 else
494 unreachable("Unknown generic format size packing clear colour");
495 }
496 }
497
498 void
499 panfrost_batch_clear(struct panfrost_batch *batch,
500 unsigned buffers,
501 const union pipe_color_union *color,
502 double depth, unsigned stencil)
503 {
504 struct panfrost_context *ctx = batch->ctx;
505
506 if (buffers & PIPE_CLEAR_COLOR) {
507 for (unsigned i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
508 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
509 continue;
510
511 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
512 pan_pack_color(batch->clear_color[i], color, format);
513 }
514 }
515
516 if (buffers & PIPE_CLEAR_DEPTH) {
517 batch->clear_depth = depth;
518 }
519
520 if (buffers & PIPE_CLEAR_STENCIL) {
521 batch->clear_stencil = stencil;
522 }
523
524 batch->clear |= buffers;
525
526 /* Clearing affects the entire framebuffer (by definition -- this is
527 * the Gallium clear callback, which clears the whole framebuffer. If
528 * the scissor test were enabled from the GL side, the state tracker
529 * would emit a quad instead and we wouldn't go down this code path) */
530
531 panfrost_batch_union_scissor(batch, 0, 0,
532 ctx->pipe_framebuffer.width,
533 ctx->pipe_framebuffer.height);
534 }
535
536 static bool
537 panfrost_batch_compare(const void *a, const void *b)
538 {
539 return util_framebuffer_state_equal(a, b);
540 }
541
542 static uint32_t
543 panfrost_batch_hash(const void *key)
544 {
545 return _mesa_hash_data(key, sizeof(struct pipe_framebuffer_state));
546 }
547
548 /* Given a new bounding rectangle (scissor), let the job cover the union of the
549 * new and old bounding rectangles */
550
551 void
552 panfrost_batch_union_scissor(struct panfrost_batch *batch,
553 unsigned minx, unsigned miny,
554 unsigned maxx, unsigned maxy)
555 {
556 batch->minx = MIN2(batch->minx, minx);
557 batch->miny = MIN2(batch->miny, miny);
558 batch->maxx = MAX2(batch->maxx, maxx);
559 batch->maxy = MAX2(batch->maxy, maxy);
560 }
561
562 void
563 panfrost_batch_intersection_scissor(struct panfrost_batch *batch,
564 unsigned minx, unsigned miny,
565 unsigned maxx, unsigned maxy)
566 {
567 batch->minx = MAX2(batch->minx, minx);
568 batch->miny = MAX2(batch->miny, miny);
569 batch->maxx = MIN2(batch->maxx, maxx);
570 batch->maxy = MIN2(batch->maxy, maxy);
571 }
572
573 /* Are we currently rendering to the screen (rather than an FBO)? */
574
575 bool
576 panfrost_batch_is_scanout(struct panfrost_batch *batch)
577 {
578 /* If there is no color buffer, it's an FBO */
579 if (batch->key.nr_cbufs != 1)
580 return false;
581
582 /* If we're too early that no framebuffer was sent, it's scanout */
583 if (!batch->key.cbufs[0])
584 return true;
585
586 return batch->key.cbufs[0]->texture->bind & PIPE_BIND_DISPLAY_TARGET ||
587 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SCANOUT ||
588 batch->key.cbufs[0]->texture->bind & PIPE_BIND_SHARED;
589 }
590
591 void
592 panfrost_batch_init(struct panfrost_context *ctx)
593 {
594 ctx->batches = _mesa_hash_table_create(ctx,
595 panfrost_batch_hash,
596 panfrost_batch_compare);
597 }