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