1 /*
2 * Copyright (C) 2019-2020 Collabora, Ltd.
3 * Copyright (C) 2019 Alyssa Rosenzweig
4 * Copyright (C) 2014-2017 Broadcom
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27 #include <assert.h>
28
29 #include "util/format/u_format.h"
30 #include "util/hash_table.h"
31 #include "util/ralloc.h"
32 #include "util/rounding.h"
33 #include "util/u_framebuffer.h"
34 #include "util/u_pack_color.h"
35 #include "pan_bo.h"
36 #include "pan_context.h"
37 #include "pan_util.h"
38
39 #define foreach_batch(ctx, idx) \
40 BITSET_FOREACH_SET(idx, ctx->batches.active, PAN_MAX_BATCHES)
41
42 static unsigned
panfrost_batch_idx(struct panfrost_batch * batch)43 panfrost_batch_idx(struct panfrost_batch *batch)
44 {
45 return batch - batch->ctx->batches.slots;
46 }
47
48 static bool
panfrost_any_batch_other_than(struct panfrost_context * ctx,unsigned index)49 panfrost_any_batch_other_than(struct panfrost_context *ctx, unsigned index)
50 {
51 unsigned i;
52 foreach_batch(ctx, i) {
53 if (i != index)
54 return true;
55 }
56
57 return false;
58 }
59
60 /* Adds the BO backing surface to a batch if the surface is non-null */
61
62 static void
panfrost_batch_add_surface(struct panfrost_batch * batch,struct pipe_surface * surf)63 panfrost_batch_add_surface(struct panfrost_batch *batch,
64 struct pipe_surface *surf)
65 {
66 if (surf) {
67 struct panfrost_resource *rsrc = pan_resource(surf->texture);
68 pan_legalize_format(batch->ctx, rsrc, surf->format, true, false);
69 panfrost_batch_write_rsrc(batch, rsrc, PIPE_SHADER_FRAGMENT);
70 }
71 }
72
73 static int
panfrost_batch_init(struct panfrost_context * ctx,const struct pipe_framebuffer_state * key,struct panfrost_batch * batch)74 panfrost_batch_init(struct panfrost_context *ctx,
75 const struct pipe_framebuffer_state *key,
76 struct panfrost_batch *batch)
77 {
78 struct pipe_screen *pscreen = ctx->base.screen;
79 struct panfrost_screen *screen = pan_screen(pscreen);
80 struct panfrost_device *dev = &screen->dev;
81
82 batch->ctx = ctx;
83
84 batch->seqnum = ++ctx->batches.seqnum;
85
86 util_dynarray_init(&batch->bos, NULL);
87
88 batch->minx = batch->miny = ~0;
89 batch->maxx = batch->maxy = 0;
90
91 util_copy_framebuffer_state(&batch->key, key);
92
93 /* Preallocate the main pool, since every batch has at least one job
94 * structure so it will be used */
95 if (panfrost_pool_init(&batch->pool, NULL, dev, 0, 65536, "Batch pool",
96 true, true))
97 return -1;
98
99 /* Don't preallocate the invisible pool, since not every batch will use
100 * the pre-allocation, particularly if the varyings are larger than the
101 * preallocation and a reallocation is needed after anyway. */
102 if (panfrost_pool_init(&batch->invisible_pool, NULL, dev,
103 PAN_BO_INVISIBLE, 65536, "Varyings", false, true))
104 return -1;
105
106 for (unsigned i = 0; i < batch->key.nr_cbufs; ++i)
107 panfrost_batch_add_surface(batch, batch->key.cbufs[i]);
108
109 panfrost_batch_add_surface(batch, batch->key.zsbuf);
110
111 return screen->vtbl.init_batch(batch);
112 }
113
114 static void
panfrost_batch_cleanup(struct panfrost_context * ctx,struct panfrost_batch * batch)115 panfrost_batch_cleanup(struct panfrost_context *ctx,
116 struct panfrost_batch *batch)
117 {
118 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
119 struct panfrost_device *dev = pan_device(ctx->base.screen);
120
121 assert(batch->seqnum);
122
123 if (ctx->batch == batch)
124 ctx->batch = NULL;
125
126 screen->vtbl.cleanup_batch(batch);
127
128 unsigned batch_idx = panfrost_batch_idx(batch);
129
130 pan_bo_access *flags = util_dynarray_begin(&batch->bos);
131 unsigned end_bo = util_dynarray_num_elements(&batch->bos, pan_bo_access);
132
133 for (int i = 0; i < end_bo; ++i) {
134 if (!flags[i])
135 continue;
136
137 struct panfrost_bo *bo = pan_lookup_bo(dev, i);
138 panfrost_bo_unreference(bo);
139 }
140
141 /* There is no more writer for anything we wrote */
142 hash_table_foreach(ctx->writers, ent) {
143 if (ent->data == batch)
144 _mesa_hash_table_remove(ctx->writers, ent);
145 }
146
147 panfrost_pool_cleanup(&batch->pool);
148 panfrost_pool_cleanup(&batch->invisible_pool);
149
150 util_unreference_framebuffer_state(&batch->key);
151
152 util_dynarray_fini(&batch->bos);
153
154 memset(batch, 0, sizeof(*batch));
155 BITSET_CLEAR(ctx->batches.active, batch_idx);
156 }
157
158 static void panfrost_batch_submit(struct panfrost_context *ctx,
159 struct panfrost_batch *batch);
160
161 static struct panfrost_batch *
panfrost_get_batch(struct panfrost_context * ctx,const struct pipe_framebuffer_state * key)162 panfrost_get_batch(struct panfrost_context *ctx,
163 const struct pipe_framebuffer_state *key)
164 {
165 struct panfrost_batch *batch = NULL;
166
167 for (unsigned i = 0; i < PAN_MAX_BATCHES; i++) {
168 if (ctx->batches.slots[i].seqnum &&
169 util_framebuffer_state_equal(&ctx->batches.slots[i].key, key)) {
170 /* We found a match, increase the seqnum for the LRU
171 * eviction logic.
172 */
173 ctx->batches.slots[i].seqnum = ++ctx->batches.seqnum;
174 return &ctx->batches.slots[i];
175 }
176
177 if (!batch || batch->seqnum > ctx->batches.slots[i].seqnum)
178 batch = &ctx->batches.slots[i];
179 }
180
181 assert(batch);
182
183 /* The selected slot is used, we need to flush the batch */
184 if (batch->seqnum) {
185 perf_debug(ctx, "Flushing batch due to seqnum overflow");
186 panfrost_batch_submit(ctx, batch);
187 }
188
189 if (panfrost_batch_init(ctx, key, batch)) {
190 mesa_loge("panfrost_batch_init failed");
191 panfrost_batch_cleanup(ctx, batch);
192 /* prevent this batch from being reused without initializing */
193 batch->seqnum = 0;
194 return NULL;
195 }
196
197 unsigned batch_idx = panfrost_batch_idx(batch);
198 BITSET_SET(ctx->batches.active, batch_idx);
199
200 return batch;
201 }
202
203 /* Get the job corresponding to the FBO we're currently rendering into */
204
205 struct panfrost_batch *
panfrost_get_batch_for_fbo(struct panfrost_context * ctx)206 panfrost_get_batch_for_fbo(struct panfrost_context *ctx)
207 {
208 /* If we already began rendering, use that */
209
210 if (ctx->batch) {
211 assert(util_framebuffer_state_equal(&ctx->batch->key,
212 &ctx->pipe_framebuffer));
213 return ctx->batch;
214 }
215
216 /* If not, look up the job */
217 struct panfrost_batch *batch =
218 panfrost_get_batch(ctx, &ctx->pipe_framebuffer);
219 if (!batch)
220 return NULL;
221
222 /* Set this job as the current FBO job. Will be reset when updating the
223 * FB state and when submitting or releasing a job.
224 */
225 ctx->batch = batch;
226 panfrost_dirty_state_all(ctx);
227 return batch;
228 }
229
230 struct panfrost_batch *
panfrost_get_fresh_batch_for_fbo(struct panfrost_context * ctx,const char * reason)231 panfrost_get_fresh_batch_for_fbo(struct panfrost_context *ctx,
232 const char *reason)
233 {
234 struct panfrost_batch *batch;
235
236 batch = panfrost_get_batch(ctx, &ctx->pipe_framebuffer);
237 panfrost_dirty_state_all(ctx);
238
239 /* We only need to submit and get a fresh batch if there is no
240 * draw/clear queued. Otherwise we may reuse the batch. */
241
242 if (batch->draw_count + batch->compute_count > 0) {
243 perf_debug(ctx, "Flushing the current FBO due to: %s", reason);
244 panfrost_batch_submit(ctx, batch);
245 batch = panfrost_get_batch(ctx, &ctx->pipe_framebuffer);
246 }
247
248 ctx->batch = batch;
249 return batch;
250 }
251
252 static bool panfrost_batch_uses_resource(struct panfrost_batch *batch,
253 struct panfrost_resource *rsrc);
254
255 static void
panfrost_batch_update_access(struct panfrost_batch * batch,struct panfrost_resource * rsrc,bool writes)256 panfrost_batch_update_access(struct panfrost_batch *batch,
257 struct panfrost_resource *rsrc, bool writes)
258 {
259 struct panfrost_context *ctx = batch->ctx;
260 uint32_t batch_idx = panfrost_batch_idx(batch);
261
262 if (writes) {
263 _mesa_hash_table_insert(ctx->writers, rsrc, batch);
264 }
265
266 /* The rest of this routine is just about flushing other batches. If there
267 * aren't any, we can skip a lot of work.
268 */
269 if (!panfrost_any_batch_other_than(ctx, batch_idx))
270 return;
271
272 struct hash_entry *entry = _mesa_hash_table_search(ctx->writers, rsrc);
273 struct panfrost_batch *writer = entry ? entry->data : NULL;
274
275 /* Both reads and writes flush the existing writer */
276 if (writer != NULL && writer != batch)
277 panfrost_batch_submit(ctx, writer);
278
279 /* Writes (only) flush readers too */
280 if (writes) {
281 unsigned i;
282 foreach_batch(ctx, i) {
283 struct panfrost_batch *batch = &ctx->batches.slots[i];
284
285 /* Skip the entry if this our batch. */
286 if (i == batch_idx)
287 continue;
288
289 /* Submit if it's a user */
290 if (panfrost_batch_uses_resource(batch, rsrc))
291 panfrost_batch_submit(ctx, batch);
292 }
293 }
294 }
295
296 static pan_bo_access *
panfrost_batch_get_bo_access(struct panfrost_batch * batch,unsigned handle)297 panfrost_batch_get_bo_access(struct panfrost_batch *batch, unsigned handle)
298 {
299 unsigned size = util_dynarray_num_elements(&batch->bos, pan_bo_access);
300
301 if (handle >= size) {
302 unsigned grow = handle + 1 - size;
303
304 memset(util_dynarray_grow(&batch->bos, pan_bo_access, grow), 0,
305 grow * sizeof(pan_bo_access));
306 }
307
308 return util_dynarray_element(&batch->bos, pan_bo_access, handle);
309 }
310
311 static bool
panfrost_batch_uses_resource(struct panfrost_batch * batch,struct panfrost_resource * rsrc)312 panfrost_batch_uses_resource(struct panfrost_batch *batch,
313 struct panfrost_resource *rsrc)
314 {
315 /* A resource is used iff its current BO is used */
316 uint32_t handle = panfrost_bo_handle(rsrc->bo);
317 unsigned size = util_dynarray_num_elements(&batch->bos, pan_bo_access);
318
319 /* If out of bounds, certainly not used */
320 if (handle >= size)
321 return false;
322
323 /* Otherwise check if nonzero access */
324 return !!(*util_dynarray_element(&batch->bos, pan_bo_access, handle));
325 }
326
327 static void
panfrost_batch_add_bo_old(struct panfrost_batch * batch,struct panfrost_bo * bo,uint32_t flags)328 panfrost_batch_add_bo_old(struct panfrost_batch *batch, struct panfrost_bo *bo,
329 uint32_t flags)
330 {
331 if (!bo)
332 return;
333
334 pan_bo_access *entry =
335 panfrost_batch_get_bo_access(batch, panfrost_bo_handle(bo));
336 pan_bo_access old_flags = *entry;
337
338 if (!old_flags) {
339 batch->num_bos++;
340 panfrost_bo_reference(bo);
341 }
342
343 if (old_flags == flags)
344 return;
345
346 flags |= old_flags;
347 *entry = flags;
348 }
349
350 static uint32_t
panfrost_access_for_stage(enum pipe_shader_type stage)351 panfrost_access_for_stage(enum pipe_shader_type stage)
352 {
353 return (stage == PIPE_SHADER_FRAGMENT) ? PAN_BO_ACCESS_FRAGMENT
354 : PAN_BO_ACCESS_VERTEX_TILER;
355 }
356
357 void
panfrost_batch_add_bo(struct panfrost_batch * batch,struct panfrost_bo * bo,enum pipe_shader_type stage)358 panfrost_batch_add_bo(struct panfrost_batch *batch, struct panfrost_bo *bo,
359 enum pipe_shader_type stage)
360 {
361 panfrost_batch_add_bo_old(
362 batch, bo, PAN_BO_ACCESS_READ | panfrost_access_for_stage(stage));
363 }
364
365 void
panfrost_batch_write_bo(struct panfrost_batch * batch,struct panfrost_bo * bo,enum pipe_shader_type stage)366 panfrost_batch_write_bo(struct panfrost_batch *batch, struct panfrost_bo *bo,
367 enum pipe_shader_type stage)
368 {
369 panfrost_batch_add_bo_old(
370 batch, bo, PAN_BO_ACCESS_WRITE | panfrost_access_for_stage(stage));
371 }
372
373 void
panfrost_batch_read_rsrc(struct panfrost_batch * batch,struct panfrost_resource * rsrc,enum pipe_shader_type stage)374 panfrost_batch_read_rsrc(struct panfrost_batch *batch,
375 struct panfrost_resource *rsrc,
376 enum pipe_shader_type stage)
377 {
378 uint32_t access = PAN_BO_ACCESS_READ | panfrost_access_for_stage(stage);
379
380 panfrost_batch_add_bo_old(batch, rsrc->bo, access);
381
382 if (rsrc->separate_stencil)
383 panfrost_batch_add_bo_old(batch, rsrc->separate_stencil->bo, access);
384 if (rsrc->shadow_image)
385 panfrost_batch_add_bo_old(batch, rsrc->shadow_image->bo, access);
386
387 panfrost_batch_update_access(batch, rsrc, false);
388 }
389
390 void
panfrost_batch_write_rsrc(struct panfrost_batch * batch,struct panfrost_resource * rsrc,enum pipe_shader_type stage)391 panfrost_batch_write_rsrc(struct panfrost_batch *batch,
392 struct panfrost_resource *rsrc,
393 enum pipe_shader_type stage)
394 {
395 uint32_t access = PAN_BO_ACCESS_WRITE | panfrost_access_for_stage(stage);
396
397 panfrost_batch_add_bo_old(batch, rsrc->bo, access);
398
399 if (rsrc->separate_stencil)
400 panfrost_batch_add_bo_old(batch, rsrc->separate_stencil->bo, access);
401 if (rsrc->shadow_image)
402 panfrost_batch_add_bo_old(batch, rsrc->shadow_image->bo, access);
403
404 panfrost_batch_update_access(batch, rsrc, true);
405 }
406
407 struct panfrost_bo *
panfrost_batch_create_bo(struct panfrost_batch * batch,size_t size,uint32_t create_flags,enum pipe_shader_type stage,const char * label)408 panfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
409 uint32_t create_flags, enum pipe_shader_type stage,
410 const char *label)
411 {
412 struct panfrost_bo *bo;
413
414 bo = panfrost_bo_create(pan_device(batch->ctx->base.screen), size,
415 create_flags, label);
416 if (bo) {
417 panfrost_batch_add_bo(batch, bo, stage);
418
419 /* panfrost_batch_add_bo() has retained a reference and
420 * panfrost_bo_create() initialize the refcnt to 1, so let's
421 * unreference the BO here so it gets released when the batch is
422 * destroyed (unless it's retained by someone else in the meantime).
423 */
424 panfrost_bo_unreference(bo);
425 }
426 return bo;
427 }
428
429 struct panfrost_bo *
panfrost_batch_get_scratchpad(struct panfrost_batch * batch,unsigned size_per_thread,unsigned thread_tls_alloc,unsigned core_id_range)430 panfrost_batch_get_scratchpad(struct panfrost_batch *batch,
431 unsigned size_per_thread,
432 unsigned thread_tls_alloc, unsigned core_id_range)
433 {
434 unsigned size = panfrost_get_total_stack_size(
435 size_per_thread, thread_tls_alloc, core_id_range);
436
437 if (batch->scratchpad) {
438 assert(panfrost_bo_size(batch->scratchpad) >= size);
439 } else {
440 batch->scratchpad =
441 panfrost_batch_create_bo(batch, size, PAN_BO_INVISIBLE,
442 PIPE_SHADER_VERTEX, "Thread local storage");
443
444 if (batch->scratchpad)
445 panfrost_batch_add_bo(batch, batch->scratchpad, PIPE_SHADER_FRAGMENT);
446 }
447
448 return batch->scratchpad;
449 }
450
451 struct panfrost_bo *
panfrost_batch_get_shared_memory(struct panfrost_batch * batch,unsigned size,unsigned workgroup_count)452 panfrost_batch_get_shared_memory(struct panfrost_batch *batch, unsigned size,
453 unsigned workgroup_count)
454 {
455 if (batch->shared_memory) {
456 assert(panfrost_bo_size(batch->shared_memory) >= size);
457 } else {
458 batch->shared_memory = panfrost_batch_create_bo(
459 batch, size, PAN_BO_INVISIBLE, PIPE_SHADER_VERTEX,
460 "Workgroup shared memory");
461 }
462
463 return batch->shared_memory;
464 }
465
466 static void
panfrost_batch_to_fb_info(const struct panfrost_batch * batch,struct pan_fb_info * fb,struct pan_image_view * rts,struct pan_image_view * zs,struct pan_image_view * s,bool reserve)467 panfrost_batch_to_fb_info(const struct panfrost_batch *batch,
468 struct pan_fb_info *fb, struct pan_image_view *rts,
469 struct pan_image_view *zs, struct pan_image_view *s,
470 bool reserve)
471 {
472 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
473 struct panfrost_screen *screen = pan_screen(batch->ctx->base.screen);
474
475 memset(fb, 0, sizeof(*fb));
476 memset(rts, 0, sizeof(*rts) * 8);
477 memset(zs, 0, sizeof(*zs));
478 memset(s, 0, sizeof(*s));
479
480 fb->tile_buf_budget = dev->optimal_tib_size;
481 fb->width = batch->key.width;
482 fb->height = batch->key.height;
483 fb->extent.minx = batch->minx;
484 fb->extent.miny = batch->miny;
485 fb->extent.maxx = batch->maxx - 1;
486 fb->extent.maxy = batch->maxy - 1;
487 fb->nr_samples = util_framebuffer_get_num_samples(&batch->key);
488 fb->force_samples = (batch->line_smoothing == U_TRISTATE_YES) ? 16 : 0;
489 fb->rt_count = batch->key.nr_cbufs;
490 fb->sprite_coord_origin = (batch->sprite_coord_origin == U_TRISTATE_YES);
491 fb->first_provoking_vertex =
492 (batch->first_provoking_vertex == U_TRISTATE_YES);
493
494 static const unsigned char id_swz[] = {
495 PIPE_SWIZZLE_X,
496 PIPE_SWIZZLE_Y,
497 PIPE_SWIZZLE_Z,
498 PIPE_SWIZZLE_W,
499 };
500
501 for (unsigned i = 0; i < fb->rt_count; i++) {
502 struct pipe_surface *surf = batch->key.cbufs[i];
503
504 if (!surf)
505 continue;
506
507 struct panfrost_resource *prsrc = pan_resource(surf->texture);
508 unsigned mask = PIPE_CLEAR_COLOR0 << i;
509
510 if (batch->clear & mask) {
511 fb->rts[i].clear = true;
512 memcpy(fb->rts[i].clear_value, batch->clear_color[i],
513 sizeof((fb->rts[i].clear_value)));
514 }
515
516 fb->rts[i].discard = !reserve && !(batch->resolve & mask);
517
518 /* Clamp the rendering area to the damage extent. The
519 * KHR_partial_update spec states that trying to render outside of
520 * the damage region is "undefined behavior", so we should be safe.
521 */
522 if (!fb->rts[i].discard) {
523 fb->extent.minx = MAX2(fb->extent.minx, prsrc->damage.extent.minx);
524 fb->extent.miny = MAX2(fb->extent.miny, prsrc->damage.extent.miny);
525 fb->extent.maxx = MIN2(fb->extent.maxx, prsrc->damage.extent.maxx - 1);
526 fb->extent.maxy = MIN2(fb->extent.maxy, prsrc->damage.extent.maxy - 1);
527 assert(fb->extent.minx <= fb->extent.maxx);
528 assert(fb->extent.miny <= fb->extent.maxy);
529 }
530
531 rts[i].format = surf->format;
532 rts[i].dim = MALI_TEXTURE_DIMENSION_2D;
533 rts[i].last_level = rts[i].first_level = surf->u.tex.level;
534 rts[i].first_layer = surf->u.tex.first_layer;
535 rts[i].last_layer = surf->u.tex.last_layer;
536 panfrost_set_image_view_planes(&rts[i], surf->texture);
537 rts[i].nr_samples =
538 surf->nr_samples ?: MAX2(surf->texture->nr_samples, 1);
539 memcpy(rts[i].swizzle, id_swz, sizeof(rts[i].swizzle));
540 fb->rts[i].crc_valid = &prsrc->valid.crc;
541 fb->rts[i].view = &rts[i];
542
543 /* Preload if the RT is read or updated */
544 if (!(batch->clear & mask) &&
545 ((batch->read & mask) ||
546 ((batch->draws & mask) &&
547 BITSET_TEST(prsrc->valid.data, fb->rts[i].view->first_level))))
548 fb->rts[i].preload = true;
549 }
550
551 const struct pan_image_view *s_view = NULL, *z_view = NULL;
552 struct panfrost_resource *z_rsrc = NULL, *s_rsrc = NULL;
553
554 if (batch->key.zsbuf) {
555 struct pipe_surface *surf = batch->key.zsbuf;
556 z_rsrc = pan_resource(surf->texture);
557
558 zs->format = surf->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
559 ? PIPE_FORMAT_Z32_FLOAT
560 : surf->format;
561 zs->dim = MALI_TEXTURE_DIMENSION_2D;
562 zs->last_level = zs->first_level = surf->u.tex.level;
563 zs->first_layer = surf->u.tex.first_layer;
564 zs->last_layer = surf->u.tex.last_layer;
565 zs->planes[0] = &z_rsrc->image;
566 zs->nr_samples = surf->nr_samples ?: MAX2(surf->texture->nr_samples, 1);
567 memcpy(zs->swizzle, id_swz, sizeof(zs->swizzle));
568 fb->zs.view.zs = zs;
569 z_view = zs;
570 if (util_format_is_depth_and_stencil(zs->format)) {
571 s_view = zs;
572 s_rsrc = z_rsrc;
573 }
574
575 if (z_rsrc->separate_stencil) {
576 s_rsrc = z_rsrc->separate_stencil;
577 s->format = PIPE_FORMAT_S8_UINT;
578 s->dim = MALI_TEXTURE_DIMENSION_2D;
579 s->last_level = s->first_level = surf->u.tex.level;
580 s->first_layer = surf->u.tex.first_layer;
581 s->last_layer = surf->u.tex.last_layer;
582 s->planes[0] = &s_rsrc->image;
583 s->nr_samples = surf->nr_samples ?: MAX2(surf->texture->nr_samples, 1);
584 memcpy(s->swizzle, id_swz, sizeof(s->swizzle));
585 fb->zs.view.s = s;
586 s_view = s;
587 }
588 }
589
590 if (batch->clear & PIPE_CLEAR_DEPTH) {
591 fb->zs.clear.z = true;
592 fb->zs.clear_value.depth = batch->clear_depth;
593 }
594
595 if (batch->clear & PIPE_CLEAR_STENCIL) {
596 fb->zs.clear.s = true;
597 fb->zs.clear_value.stencil = batch->clear_stencil;
598 }
599
600 fb->zs.discard.z = !reserve && !(batch->resolve & PIPE_CLEAR_DEPTH);
601 fb->zs.discard.s = !reserve && !(batch->resolve & PIPE_CLEAR_STENCIL);
602
603 if (!fb->zs.clear.z && z_rsrc &&
604 ((batch->read & PIPE_CLEAR_DEPTH) ||
605 ((batch->draws & PIPE_CLEAR_DEPTH) &&
606 BITSET_TEST(z_rsrc->valid.data, z_view->first_level))))
607 fb->zs.preload.z = true;
608
609 if (!fb->zs.clear.s && s_rsrc &&
610 ((batch->read & PIPE_CLEAR_STENCIL) ||
611 ((batch->draws & PIPE_CLEAR_STENCIL) &&
612 BITSET_TEST(s_rsrc->valid.data, s_view->first_level))))
613 fb->zs.preload.s = true;
614
615 /* Preserve both component if we have a combined ZS view and
616 * one component needs to be preserved.
617 */
618 if (z_view && z_view == s_view && fb->zs.discard.z != fb->zs.discard.s) {
619 bool valid = BITSET_TEST(z_rsrc->valid.data, z_view->first_level);
620
621 fb->zs.discard.z = false;
622 fb->zs.discard.s = false;
623 fb->zs.preload.z = !fb->zs.clear.z && valid;
624 fb->zs.preload.s = !fb->zs.clear.s && valid;
625 }
626
627 screen->vtbl.select_tile_size(fb);
628 }
629
630 static void
panfrost_emit_tile_map(struct panfrost_batch * batch,struct pan_fb_info * fb)631 panfrost_emit_tile_map(struct panfrost_batch *batch, struct pan_fb_info *fb)
632 {
633 if (batch->key.nr_cbufs < 1 || !batch->key.cbufs[0])
634 return;
635
636 struct pipe_surface *surf = batch->key.cbufs[0];
637 struct panfrost_resource *pres = surf ? pan_resource(surf->texture) : NULL;
638
639 if (pres && pres->damage.tile_map.enable) {
640 fb->tile_map.base =
641 pan_pool_upload_aligned(&batch->pool.base, pres->damage.tile_map.data,
642 pres->damage.tile_map.size, 64);
643 fb->tile_map.stride = pres->damage.tile_map.stride;
644 }
645 }
646
647 static void
panfrost_batch_submit(struct panfrost_context * ctx,struct panfrost_batch * batch)648 panfrost_batch_submit(struct panfrost_context *ctx,
649 struct panfrost_batch *batch)
650 {
651 struct pipe_screen *pscreen = ctx->base.screen;
652 struct panfrost_screen *screen = pan_screen(pscreen);
653 bool has_frag = panfrost_has_fragment_job(batch);
654 int ret;
655
656 /* Nothing to do! */
657 if (!has_frag && batch->compute_count == 0 && !batch->has_time_query)
658 goto out;
659
660 if (batch->key.zsbuf && has_frag) {
661 struct pipe_surface *surf = batch->key.zsbuf;
662 struct panfrost_resource *z_rsrc = pan_resource(surf->texture);
663
664 /* if there are multiple levels or layers, we optimize only the first */
665 if (surf->u.tex.level == 0 && surf->u.tex.first_layer == 0) {
666 /* Shared depth/stencil resources are not supported, and would
667 * break this optimisation. */
668 assert(!(z_rsrc->base.bind & PAN_BIND_SHARED_MASK));
669
670 if (batch->clear & PIPE_CLEAR_STENCIL) {
671 z_rsrc->stencil_value = batch->clear_stencil;
672 z_rsrc->constant_stencil = true;
673 } else if (z_rsrc->constant_stencil) {
674 batch->clear_stencil = z_rsrc->stencil_value;
675 batch->clear |= PIPE_CLEAR_STENCIL;
676 }
677 }
678
679 if (batch->draws & PIPE_CLEAR_STENCIL)
680 z_rsrc->constant_stencil = false;
681 }
682
683 struct pan_fb_info fb;
684 struct pan_image_view rts[8], zs, s;
685
686 panfrost_batch_to_fb_info(batch, &fb, rts, &zs, &s, false);
687 panfrost_emit_tile_map(batch, &fb);
688
689 ret = screen->vtbl.submit_batch(batch, &fb);
690 if (ret)
691 mesa_loge("panfrost_batch_submit failed: %d\n", ret);
692
693 /* We must reset the damage info of our render targets here even
694 * though a damage reset normally happens when the DRI layer swaps
695 * buffers. That's because there can be implicit flushes the GL
696 * app is not aware of, and those might impact the damage region: if
697 * part of the damaged portion is drawn during those implicit flushes,
698 * you have to reload those areas before next draws are pushed, and
699 * since the driver can't easily know what's been modified by the draws
700 * it flushed, the easiest solution is to reload everything.
701 */
702 for (unsigned i = 0; i < batch->key.nr_cbufs; i++) {
703 if (!batch->key.cbufs[i])
704 continue;
705
706 panfrost_resource_set_damage_region(
707 ctx->base.screen, batch->key.cbufs[i]->texture, 0, NULL);
708 }
709
710 out:
711 panfrost_batch_cleanup(ctx, batch);
712 }
713
714 /* Submit all batches */
715
716 void
panfrost_flush_all_batches(struct panfrost_context * ctx,const char * reason)717 panfrost_flush_all_batches(struct panfrost_context *ctx, const char *reason)
718 {
719 if (reason)
720 perf_debug(ctx, "Flushing everything due to: %s", reason);
721
722 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
723 if (!batch)
724 return;
725
726 panfrost_batch_submit(ctx, batch);
727
728 for (unsigned i = 0; i < PAN_MAX_BATCHES; i++) {
729 if (ctx->batches.slots[i].seqnum)
730 panfrost_batch_submit(ctx, &ctx->batches.slots[i]);
731 }
732 }
733
734 void
panfrost_flush_writer(struct panfrost_context * ctx,struct panfrost_resource * rsrc,const char * reason)735 panfrost_flush_writer(struct panfrost_context *ctx,
736 struct panfrost_resource *rsrc, const char *reason)
737 {
738 struct hash_entry *entry = _mesa_hash_table_search(ctx->writers, rsrc);
739
740 if (entry) {
741 perf_debug(ctx, "Flushing writer due to: %s", reason);
742 panfrost_batch_submit(ctx, entry->data);
743 }
744 }
745
746 void
panfrost_flush_batches_accessing_rsrc(struct panfrost_context * ctx,struct panfrost_resource * rsrc,const char * reason)747 panfrost_flush_batches_accessing_rsrc(struct panfrost_context *ctx,
748 struct panfrost_resource *rsrc,
749 const char *reason)
750 {
751 unsigned i;
752 foreach_batch(ctx, i) {
753 struct panfrost_batch *batch = &ctx->batches.slots[i];
754
755 if (!panfrost_batch_uses_resource(batch, rsrc))
756 continue;
757
758 perf_debug(ctx, "Flushing user due to: %s", reason);
759 panfrost_batch_submit(ctx, batch);
760 }
761 }
762
763 bool
panfrost_any_batch_reads_rsrc(struct panfrost_context * ctx,struct panfrost_resource * rsrc)764 panfrost_any_batch_reads_rsrc(struct panfrost_context *ctx,
765 struct panfrost_resource *rsrc)
766 {
767 unsigned i;
768 foreach_batch(ctx, i) {
769 struct panfrost_batch *batch = &ctx->batches.slots[i];
770
771 if (panfrost_batch_uses_resource(batch, rsrc))
772 return true;
773 }
774
775 return false;
776 }
777
778 bool
panfrost_any_batch_writes_rsrc(struct panfrost_context * ctx,struct panfrost_resource * rsrc)779 panfrost_any_batch_writes_rsrc(struct panfrost_context *ctx,
780 struct panfrost_resource *rsrc)
781 {
782 return _mesa_hash_table_search(ctx->writers, rsrc) != NULL;
783 }
784
785 void
panfrost_batch_adjust_stack_size(struct panfrost_batch * batch)786 panfrost_batch_adjust_stack_size(struct panfrost_batch *batch)
787 {
788 struct panfrost_context *ctx = batch->ctx;
789
790 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
791 struct panfrost_compiled_shader *ss = ctx->prog[i];
792
793 if (!ss)
794 continue;
795
796 batch->stack_size = MAX2(batch->stack_size, ss->info.tls_size);
797 }
798 }
799
800 void
panfrost_batch_clear(struct panfrost_batch * batch,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)801 panfrost_batch_clear(struct panfrost_batch *batch, unsigned buffers,
802 const union pipe_color_union *color, double depth,
803 unsigned stencil)
804 {
805 struct panfrost_context *ctx = batch->ctx;
806 struct panfrost_device *dev = pan_device(ctx->base.screen);
807
808 if (buffers & PIPE_CLEAR_COLOR) {
809 for (unsigned i = 0; i < ctx->pipe_framebuffer.nr_cbufs; ++i) {
810 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
811 continue;
812
813 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
814 pan_pack_color(dev->blendable_formats, batch->clear_color[i], color,
815 format, false);
816 }
817 }
818
819 if (buffers & PIPE_CLEAR_DEPTH) {
820 batch->clear_depth = depth;
821 }
822
823 if (buffers & PIPE_CLEAR_STENCIL) {
824 batch->clear_stencil = stencil;
825 }
826
827 batch->clear |= buffers;
828 batch->resolve |= buffers;
829
830 /* Clearing affects the entire framebuffer (by definition -- this is
831 * the Gallium clear callback, which clears the whole framebuffer. If
832 * the scissor test were enabled from the GL side, the gallium frontend
833 * would emit a quad instead and we wouldn't go down this code path) */
834
835 panfrost_batch_union_scissor(batch, 0, 0, ctx->pipe_framebuffer.width,
836 ctx->pipe_framebuffer.height);
837 }
838
839 /* Given a new bounding rectangle (scissor), let the job cover the union of the
840 * new and old bounding rectangles */
841
842 void
panfrost_batch_union_scissor(struct panfrost_batch * batch,unsigned minx,unsigned miny,unsigned maxx,unsigned maxy)843 panfrost_batch_union_scissor(struct panfrost_batch *batch, unsigned minx,
844 unsigned miny, unsigned maxx, unsigned maxy)
845 {
846 batch->minx = MIN2(batch->minx, minx);
847 batch->miny = MIN2(batch->miny, miny);
848 batch->maxx = MAX2(batch->maxx, maxx);
849 batch->maxy = MAX2(batch->maxy, maxy);
850 }
851
852 /**
853 * Checks if rasterization should be skipped. If not, a TILER job must be
854 * created for each draw, or the IDVS flow must be used.
855 *
856 * As a special case, if there is no vertex shader, no primitives are generated,
857 * meaning the whole pipeline (including rasterization) should be skipped.
858 */
859 bool
panfrost_batch_skip_rasterization(struct panfrost_batch * batch)860 panfrost_batch_skip_rasterization(struct panfrost_batch *batch)
861 {
862 struct panfrost_context *ctx = batch->ctx;
863 struct pipe_rasterizer_state *rast = (void *)ctx->rasterizer;
864
865 return (rast->rasterizer_discard || batch->scissor_culls_everything ||
866 !batch->rsd[PIPE_SHADER_VERTEX]);
867 }
868