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