2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
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:
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
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
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"
34 static struct panfrost_batch
*
35 panfrost_create_batch(struct panfrost_context
*ctx
,
36 const struct pipe_framebuffer_state
*key
)
38 struct panfrost_batch
*batch
= rzalloc(ctx
, struct panfrost_batch
);
42 batch
->bos
= _mesa_set_create(batch
,
44 _mesa_key_pointer_equal
);
46 batch
->minx
= batch
->miny
= ~0;
47 batch
->maxx
= batch
->maxy
= 0;
48 batch
->transient_offset
= 0;
50 util_dynarray_init(&batch
->headers
, batch
);
51 util_dynarray_init(&batch
->gpu_headers
, batch
);
52 util_copy_framebuffer_state(&batch
->key
, key
);
58 panfrost_free_batch(struct panfrost_batch
*batch
)
63 struct panfrost_context
*ctx
= batch
->ctx
;
65 set_foreach(batch
->bos
, entry
) {
66 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
67 panfrost_bo_unreference(ctx
->base
.screen
, bo
);
70 _mesa_hash_table_remove_key(ctx
->batches
, &batch
->key
);
72 if (ctx
->batch
== batch
)
75 util_unreference_framebuffer_state(&batch
->key
);
79 static struct panfrost_batch
*
80 panfrost_get_batch(struct panfrost_context
*ctx
,
81 const struct pipe_framebuffer_state
*key
)
83 /* Lookup the job first */
84 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->batches
, key
);
89 /* Otherwise, let's create a job */
91 struct panfrost_batch
*batch
= panfrost_create_batch(ctx
, key
);
93 /* Save the created job */
94 _mesa_hash_table_insert(ctx
->batches
, &batch
->key
, batch
);
99 /* Get the job corresponding to the FBO we're currently rendering into */
101 struct panfrost_batch
*
102 panfrost_get_batch_for_fbo(struct panfrost_context
*ctx
)
104 /* If we're wallpapering, we special case to workaround
107 if (ctx
->wallpaper_batch
)
108 return ctx
->wallpaper_batch
;
110 /* If we already began rendering, use that */
113 assert(util_framebuffer_state_equal(&ctx
->batch
->key
,
114 &ctx
->pipe_framebuffer
));
118 /* If not, look up the job */
119 struct panfrost_batch
*batch
= panfrost_get_batch(ctx
,
120 &ctx
->pipe_framebuffer
);
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.
130 panfrost_batch_add_bo(struct panfrost_batch
*batch
, struct panfrost_bo
*bo
)
135 if (_mesa_set_search(batch
->bos
, bo
))
138 panfrost_bo_reference(bo
);
139 _mesa_set_add(batch
->bos
, bo
);
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. */
147 panfrost_batch_get_polygon_list(struct panfrost_batch
*batch
, unsigned size
)
149 if (batch
->polygon_list
) {
150 assert(batch
->polygon_list
->size
>= size
);
152 struct panfrost_screen
*screen
= pan_screen(batch
->ctx
->base
.screen
);
154 /* Create the BO as invisible, as there's no reason to map */
156 batch
->polygon_list
= panfrost_drm_create_bo(screen
,
157 size
, PAN_ALLOCATE_INVISIBLE
);
158 panfrost_batch_add_bo(batch
, batch
->polygon_list
);
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.
164 panfrost_bo_unreference(&screen
->base
, batch
->polygon_list
);
167 return batch
->polygon_list
->gpu
;
171 panfrost_batch_draw_wallpaper(struct panfrost_batch
*batch
)
173 /* Nothing to reload? TODO: MRT wallpapers */
174 if (batch
->key
.cbufs
[0] == NULL
)
177 /* Check if the buffer has any content on it worth preserving */
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
;
183 if (!rsrc
->slices
[level
].initialized
)
186 batch
->ctx
->wallpaper_batch
= batch
;
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.
192 unsigned damage_width
= (rsrc
->damage
.extent
.maxx
- rsrc
->damage
.extent
.minx
);
193 unsigned damage_height
= (rsrc
->damage
.extent
.maxy
- rsrc
->damage
.extent
.miny
);
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
);
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.
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),
212 batch
->maxy
= MIN2(ALIGN_POT(batch
->maxy
, MALI_TILE_LENGTH
* 2),
215 struct pipe_scissor_state damage
;
216 struct pipe_box rects
[4];
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
);
228 /* One damage rectangle means we can end up with at most 4 reload
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
237 * ____________________________
244 * |_______|___________|______|
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]);
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
261 if (!rects
[i
].width
|| !rects
[i
].height
)
264 /* Blit the wallpaper in */
265 panfrost_blit_wallpaper(batch
->ctx
, &rects
[i
]);
267 batch
->ctx
->wallpaper_batch
= NULL
;
271 panfrost_batch_submit(struct panfrost_batch
*batch
)
275 struct panfrost_context
*ctx
= batch
->ctx
;
279 if (!batch
->last_job
.gpu
&& !batch
->clear
)
282 if (!batch
->clear
&& batch
->last_tiler
.gpu
)
283 panfrost_batch_draw_wallpaper(batch
);
285 panfrost_scoreboard_link_batch(batch
);
287 ret
= panfrost_drm_submit_vs_fs_batch(batch
);
290 fprintf(stderr
, "panfrost_batch_submit failed: %d\n", ret
);
293 /* If this is the bound batch, the panfrost_context parameters are
294 * relevant so submitting it invalidates those paramaters, but if it's
295 * not bound, the context parameters are for some other batch so we
296 * can't invalidate them.
298 if (ctx
->batch
== batch
)
299 panfrost_invalidate_frame(ctx
);
301 /* The job has been submitted, let's invalidate the current FBO job
304 assert(!ctx
->batch
|| batch
== ctx
->batch
);
307 /* We always stall the pipeline for correct results since pipelined
308 * rendering is quite broken right now (to be fixed by the panfrost_job
309 * refactor, just take the perf hit for correctness)
311 drmSyncobjWait(pan_screen(ctx
->base
.screen
)->fd
, &ctx
->out_sync
, 1,
313 panfrost_free_batch(batch
);
317 panfrost_batch_set_requirements(struct panfrost_batch
*batch
)
319 struct panfrost_context
*ctx
= batch
->ctx
;
321 if (ctx
->rasterizer
&& ctx
->rasterizer
->base
.multisample
)
322 batch
->requirements
|= PAN_REQ_MSAA
;
324 if (ctx
->depth_stencil
&& ctx
->depth_stencil
->depth
.writemask
)
325 batch
->requirements
|= PAN_REQ_DEPTH_WRITE
;
328 /* Helper to smear a 32-bit color across 128-bit components */
331 pan_pack_color_32(uint32_t *packed
, uint32_t v
)
333 for (unsigned i
= 0; i
< 4; ++i
)
338 pan_pack_color_64(uint32_t *packed
, uint32_t lo
, uint32_t hi
)
340 for (unsigned i
= 0; i
< 4; i
+= 2) {
347 pan_pack_color(uint32_t *packed
, const union pipe_color_union
*color
, enum pipe_format format
)
349 /* Alpha magicked to 1.0 if there is no alpha */
351 bool has_alpha
= util_format_has_alpha(format
);
352 float clear_alpha
= has_alpha
? color
->f
[3] : 1.0f
;
354 /* Packed color depends on the framebuffer format */
356 const struct util_format_description
*desc
=
357 util_format_description(format
);
359 if (util_format_is_rgba8_variant(desc
)) {
360 pan_pack_color_32(packed
,
361 (float_to_ubyte(clear_alpha
) << 24) |
362 (float_to_ubyte(color
->f
[2]) << 16) |
363 (float_to_ubyte(color
->f
[1]) << 8) |
364 (float_to_ubyte(color
->f
[0]) << 0));
365 } else if (format
== PIPE_FORMAT_B5G6R5_UNORM
) {
366 /* First, we convert the components to R5, G6, B5 separately */
367 unsigned r5
= CLAMP(color
->f
[0], 0.0, 1.0) * 31.0;
368 unsigned g6
= CLAMP(color
->f
[1], 0.0, 1.0) * 63.0;
369 unsigned b5
= CLAMP(color
->f
[2], 0.0, 1.0) * 31.0;
371 /* Then we pack into a sparse u32. TODO: Why these shifts? */
372 pan_pack_color_32(packed
, (b5
<< 25) | (g6
<< 14) | (r5
<< 5));
373 } else if (format
== PIPE_FORMAT_B4G4R4A4_UNORM
) {
374 /* We scale the components against 0xF0 (=240.0), rather than 0xFF */
375 unsigned r4
= CLAMP(color
->f
[0], 0.0, 1.0) * 240.0;
376 unsigned g4
= CLAMP(color
->f
[1], 0.0, 1.0) * 240.0;
377 unsigned b4
= CLAMP(color
->f
[2], 0.0, 1.0) * 240.0;
378 unsigned a4
= CLAMP(clear_alpha
, 0.0, 1.0) * 240.0;
380 /* Pack on *byte* intervals */
381 pan_pack_color_32(packed
, (a4
<< 24) | (b4
<< 16) | (g4
<< 8) | r4
);
382 } else if (format
== PIPE_FORMAT_B5G5R5A1_UNORM
) {
383 /* Scale as expected but shift oddly */
384 unsigned r5
= round(CLAMP(color
->f
[0], 0.0, 1.0)) * 31.0;
385 unsigned g5
= round(CLAMP(color
->f
[1], 0.0, 1.0)) * 31.0;
386 unsigned b5
= round(CLAMP(color
->f
[2], 0.0, 1.0)) * 31.0;
387 unsigned a1
= round(CLAMP(clear_alpha
, 0.0, 1.0)) * 1.0;
389 pan_pack_color_32(packed
, (a1
<< 31) | (b5
<< 25) | (g5
<< 15) | (r5
<< 5));
391 /* Try Gallium's generic default path. Doesn't work for all
392 * formats but it's a good guess. */
394 union util_color out
;
396 if (util_format_is_pure_integer(format
)) {
397 memcpy(out
.ui
, color
->ui
, 16);
399 util_pack_color(color
->f
, format
, &out
);
402 unsigned size
= util_format_get_blocksize(format
);
405 unsigned b
= out
.ui
[0];
406 unsigned s
= b
| (b
<< 8);
407 pan_pack_color_32(packed
, s
| (s
<< 16));
408 } else if (size
== 2)
409 pan_pack_color_32(packed
, out
.ui
[0] | (out
.ui
[0] << 16));
411 pan_pack_color_32(packed
, out
.ui
[0]);
413 pan_pack_color_64(packed
, out
.ui
[0], out
.ui
[1]);
415 memcpy(packed
, out
.ui
, 16);
417 unreachable("Unknown generic format size packing clear colour");
422 panfrost_batch_clear(struct panfrost_batch
*batch
,
424 const union pipe_color_union
*color
,
425 double depth
, unsigned stencil
)
427 struct panfrost_context
*ctx
= batch
->ctx
;
429 if (buffers
& PIPE_CLEAR_COLOR
) {
430 for (unsigned i
= 0; i
< PIPE_MAX_COLOR_BUFS
; ++i
) {
431 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
434 enum pipe_format format
= ctx
->pipe_framebuffer
.cbufs
[i
]->format
;
435 pan_pack_color(batch
->clear_color
[i
], color
, format
);
439 if (buffers
& PIPE_CLEAR_DEPTH
) {
440 batch
->clear_depth
= depth
;
443 if (buffers
& PIPE_CLEAR_STENCIL
) {
444 batch
->clear_stencil
= stencil
;
447 batch
->clear
|= buffers
;
449 /* Clearing affects the entire framebuffer (by definition -- this is
450 * the Gallium clear callback, which clears the whole framebuffer. If
451 * the scissor test were enabled from the GL side, the state tracker
452 * would emit a quad instead and we wouldn't go down this code path) */
454 panfrost_batch_union_scissor(batch
, 0, 0,
455 ctx
->pipe_framebuffer
.width
,
456 ctx
->pipe_framebuffer
.height
);
460 panfrost_batch_compare(const void *a
, const void *b
)
462 return util_framebuffer_state_equal(a
, b
);
466 panfrost_batch_hash(const void *key
)
468 return _mesa_hash_data(key
, sizeof(struct pipe_framebuffer_state
));
471 /* Given a new bounding rectangle (scissor), let the job cover the union of the
472 * new and old bounding rectangles */
475 panfrost_batch_union_scissor(struct panfrost_batch
*batch
,
476 unsigned minx
, unsigned miny
,
477 unsigned maxx
, unsigned maxy
)
479 batch
->minx
= MIN2(batch
->minx
, minx
);
480 batch
->miny
= MIN2(batch
->miny
, miny
);
481 batch
->maxx
= MAX2(batch
->maxx
, maxx
);
482 batch
->maxy
= MAX2(batch
->maxy
, maxy
);
486 panfrost_batch_intersection_scissor(struct panfrost_batch
*batch
,
487 unsigned minx
, unsigned miny
,
488 unsigned maxx
, unsigned maxy
)
490 batch
->minx
= MAX2(batch
->minx
, minx
);
491 batch
->miny
= MAX2(batch
->miny
, miny
);
492 batch
->maxx
= MIN2(batch
->maxx
, maxx
);
493 batch
->maxy
= MIN2(batch
->maxy
, maxy
);
496 /* Are we currently rendering to the screen (rather than an FBO)? */
499 panfrost_batch_is_scanout(struct panfrost_batch
*batch
)
501 /* If there is no color buffer, it's an FBO */
502 if (batch
->key
.nr_cbufs
!= 1)
505 /* If we're too early that no framebuffer was sent, it's scanout */
506 if (!batch
->key
.cbufs
[0])
509 return batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_DISPLAY_TARGET
||
510 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SCANOUT
||
511 batch
->key
.cbufs
[0]->texture
->bind
& PIPE_BIND_SHARED
;
515 panfrost_batch_init(struct panfrost_context
*ctx
)
517 ctx
->batches
= _mesa_hash_table_create(ctx
,
519 panfrost_batch_compare
);