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
385 panfrost_batch_update_access(batch, rsrc, false);
386 }
387
388 void
panfrost_batch_write_rsrc(struct panfrost_batch * batch,struct panfrost_resource * rsrc,enum pipe_shader_type stage)389 panfrost_batch_write_rsrc(struct panfrost_batch *batch,
390 struct panfrost_resource *rsrc,
391 enum pipe_shader_type stage)
392 {
393 uint32_t access = PAN_BO_ACCESS_WRITE | panfrost_access_for_stage(stage);
394
395 panfrost_batch_add_bo_old(batch, rsrc->bo, access);
396
397 if (rsrc->separate_stencil)
398 panfrost_batch_add_bo_old(batch, rsrc->separate_stencil->bo, access);
399
400 panfrost_batch_update_access(batch, rsrc, true);
401 }
402
403 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)404 panfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
405 uint32_t create_flags, enum pipe_shader_type stage,
406 const char *label)
407 {
408 struct panfrost_bo *bo;
409
410 bo = panfrost_bo_create(pan_device(batch->ctx->base.screen), size,
411 create_flags, label);
412 assert(bo);
413 panfrost_batch_add_bo(batch, bo, stage);
414
415 /* panfrost_batch_add_bo() has retained a reference and
416 * panfrost_bo_create() initialize the refcnt to 1, so let's
417 * unreference the BO here so it gets released when the batch is
418 * destroyed (unless it's retained by someone else in the meantime).
419 */
420 panfrost_bo_unreference(bo);
421 return bo;
422 }
423
424 struct panfrost_bo *
panfrost_batch_get_scratchpad(struct panfrost_batch * batch,unsigned size_per_thread,unsigned thread_tls_alloc,unsigned core_id_range)425 panfrost_batch_get_scratchpad(struct panfrost_batch *batch,
426 unsigned size_per_thread,
427 unsigned thread_tls_alloc, unsigned core_id_range)
428 {
429 unsigned size = panfrost_get_total_stack_size(
430 size_per_thread, thread_tls_alloc, core_id_range);
431
432 if (batch->scratchpad) {
433 assert(panfrost_bo_size(batch->scratchpad) >= size);
434 } else {
435 batch->scratchpad =
436 panfrost_batch_create_bo(batch, size, PAN_BO_INVISIBLE,
437 PIPE_SHADER_VERTEX, "Thread local storage");
438
439 panfrost_batch_add_bo(batch, batch->scratchpad, PIPE_SHADER_FRAGMENT);
440 }
441
442 return batch->scratchpad;
443 }
444
445 struct panfrost_bo *
panfrost_batch_get_shared_memory(struct panfrost_batch * batch,unsigned size,unsigned workgroup_count)446 panfrost_batch_get_shared_memory(struct panfrost_batch *batch, unsigned size,
447 unsigned workgroup_count)
448 {
449 if (batch->shared_memory) {
450 assert(panfrost_bo_size(batch->shared_memory) >= size);
451 } else {
452 batch->shared_memory = panfrost_batch_create_bo(
453 batch, size, PAN_BO_INVISIBLE, PIPE_SHADER_VERTEX,
454 "Workgroup shared memory");
455 }
456
457 return batch->shared_memory;
458 }
459
460 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)461 panfrost_batch_to_fb_info(const struct panfrost_batch *batch,
462 struct pan_fb_info *fb, struct pan_image_view *rts,
463 struct pan_image_view *zs, struct pan_image_view *s,
464 bool reserve)
465 {
466 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
467 struct panfrost_screen *screen = pan_screen(batch->ctx->base.screen);
468
469 memset(fb, 0, sizeof(*fb));
470 memset(rts, 0, sizeof(*rts) * 8);
471 memset(zs, 0, sizeof(*zs));
472 memset(s, 0, sizeof(*s));
473
474 fb->tile_buf_budget = dev->optimal_tib_size;
475 fb->width = batch->key.width;
476 fb->height = batch->key.height;
477 fb->extent.minx = batch->minx;
478 fb->extent.miny = batch->miny;
479 fb->extent.maxx = batch->maxx - 1;
480 fb->extent.maxy = batch->maxy - 1;
481 fb->nr_samples = util_framebuffer_get_num_samples(&batch->key);
482 fb->force_samples = (batch->line_smoothing == U_TRISTATE_YES) ? 16 : 0;
483 fb->rt_count = batch->key.nr_cbufs;
484 fb->sprite_coord_origin = (batch->sprite_coord_origin == U_TRISTATE_YES);
485 fb->first_provoking_vertex =
486 (batch->first_provoking_vertex == U_TRISTATE_YES);
487
488 static const unsigned char id_swz[] = {
489 PIPE_SWIZZLE_X,
490 PIPE_SWIZZLE_Y,
491 PIPE_SWIZZLE_Z,
492 PIPE_SWIZZLE_W,
493 };
494
495 for (unsigned i = 0; i < fb->rt_count; i++) {
496 struct pipe_surface *surf = batch->key.cbufs[i];
497
498 if (!surf)
499 continue;
500
501 struct panfrost_resource *prsrc = pan_resource(surf->texture);
502 unsigned mask = PIPE_CLEAR_COLOR0 << i;
503
504 if (batch->clear & mask) {
505 fb->rts[i].clear = true;
506 memcpy(fb->rts[i].clear_value, batch->clear_color[i],
507 sizeof((fb->rts[i].clear_value)));
508 }
509
510 fb->rts[i].discard = !reserve && !(batch->resolve & mask);
511
512 /* Clamp the rendering area to the damage extent. The
513 * KHR_partial_update spec states that trying to render outside of
514 * the damage region is "undefined behavior", so we should be safe.
515 */
516 if (!fb->rts[i].discard) {
517 fb->extent.minx = MAX2(fb->extent.minx, prsrc->damage.extent.minx);
518 fb->extent.miny = MAX2(fb->extent.miny, prsrc->damage.extent.miny);
519 fb->extent.maxx = MIN2(fb->extent.maxx, prsrc->damage.extent.maxx - 1);
520 fb->extent.maxy = MIN2(fb->extent.maxy, prsrc->damage.extent.maxy - 1);
521 assert(fb->extent.minx <= fb->extent.maxx);
522 assert(fb->extent.miny <= fb->extent.maxy);
523 }
524
525 rts[i].format = surf->format;
526 rts[i].dim = MALI_TEXTURE_DIMENSION_2D;
527 rts[i].last_level = rts[i].first_level = surf->u.tex.level;
528 rts[i].first_layer = surf->u.tex.first_layer;
529 rts[i].last_layer = surf->u.tex.last_layer;
530 panfrost_set_image_view_planes(&rts[i], surf->texture);
531 rts[i].nr_samples =
532 surf->nr_samples ?: MAX2(surf->texture->nr_samples, 1);
533 memcpy(rts[i].swizzle, id_swz, sizeof(rts[i].swizzle));
534 fb->rts[i].crc_valid = &prsrc->valid.crc;
535 fb->rts[i].view = &rts[i];
536
537 /* Preload if the RT is read or updated */
538 if (!(batch->clear & mask) &&
539 ((batch->read & mask) ||
540 ((batch->draws & mask) &&
541 BITSET_TEST(prsrc->valid.data, fb->rts[i].view->first_level))))
542 fb->rts[i].preload = true;
543 }
544
545 const struct pan_image_view *s_view = NULL, *z_view = NULL;
546 struct panfrost_resource *z_rsrc = NULL, *s_rsrc = NULL;
547
548 if (batch->key.zsbuf) {
549 struct pipe_surface *surf = batch->key.zsbuf;
550 z_rsrc = pan_resource(surf->texture);
551
552 zs->format = surf->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
553 ? PIPE_FORMAT_Z32_FLOAT
554 : surf->format;
555 zs->dim = MALI_TEXTURE_DIMENSION_2D;
556 zs->last_level = zs->first_level = surf->u.tex.level;
557 zs->first_layer = surf->u.tex.first_layer;
558 zs->last_layer = surf->u.tex.last_layer;
559 zs->planes[0] = &z_rsrc->image;
560 zs->nr_samples = surf->nr_samples ?: MAX2(surf->texture->nr_samples, 1);
561 memcpy(zs->swizzle, id_swz, sizeof(zs->swizzle));
562 fb->zs.view.zs = zs;
563 z_view = zs;
564 if (util_format_is_depth_and_stencil(zs->format)) {
565 s_view = zs;
566 s_rsrc = z_rsrc;
567 }
568
569 if (z_rsrc->separate_stencil) {
570 s_rsrc = z_rsrc->separate_stencil;
571 s->format = PIPE_FORMAT_S8_UINT;
572 s->dim = MALI_TEXTURE_DIMENSION_2D;
573 s->last_level = s->first_level = surf->u.tex.level;
574 s->first_layer = surf->u.tex.first_layer;
575 s->last_layer = surf->u.tex.last_layer;
576 s->planes[0] = &s_rsrc->image;
577 s->nr_samples = surf->nr_samples ?: MAX2(surf->texture->nr_samples, 1);
578 memcpy(s->swizzle, id_swz, sizeof(s->swizzle));
579 fb->zs.view.s = s;
580 s_view = s;
581 }
582 }
583
584 if (batch->clear & PIPE_CLEAR_DEPTH) {
585 fb->zs.clear.z = true;
586 fb->zs.clear_value.depth = batch->clear_depth;
587 }
588
589 if (batch->clear & PIPE_CLEAR_STENCIL) {
590 fb->zs.clear.s = true;
591 fb->zs.clear_value.stencil = batch->clear_stencil;
592 }
593
594 fb->zs.discard.z = !reserve && !(batch->resolve & PIPE_CLEAR_DEPTH);
595 fb->zs.discard.s = !reserve && !(batch->resolve & PIPE_CLEAR_STENCIL);
596
597 if (!fb->zs.clear.z && z_rsrc &&
598 ((batch->read & PIPE_CLEAR_DEPTH) ||
599 ((batch->draws & PIPE_CLEAR_DEPTH) &&
600 BITSET_TEST(z_rsrc->valid.data, z_view->first_level))))
601 fb->zs.preload.z = true;
602
603 if (!fb->zs.clear.s && s_rsrc &&
604 ((batch->read & PIPE_CLEAR_STENCIL) ||
605 ((batch->draws & PIPE_CLEAR_STENCIL) &&
606 BITSET_TEST(s_rsrc->valid.data, s_view->first_level))))
607 fb->zs.preload.s = true;
608
609 /* Preserve both component if we have a combined ZS view and
610 * one component needs to be preserved.
611 */
612 if (z_view && z_view == s_view && fb->zs.discard.z != fb->zs.discard.s) {
613 bool valid = BITSET_TEST(z_rsrc->valid.data, z_view->first_level);
614
615 fb->zs.discard.z = false;
616 fb->zs.discard.s = false;
617 fb->zs.preload.z = !fb->zs.clear.z && valid;
618 fb->zs.preload.s = !fb->zs.clear.s && valid;
619 }
620
621 screen->vtbl.select_tile_size(fb);
622 }
623
624 static void
panfrost_emit_tile_map(struct panfrost_batch * batch,struct pan_fb_info * fb)625 panfrost_emit_tile_map(struct panfrost_batch *batch, struct pan_fb_info *fb)
626 {
627 if (batch->key.nr_cbufs < 1 || !batch->key.cbufs[0])
628 return;
629
630 struct pipe_surface *surf = batch->key.cbufs[0];
631 struct panfrost_resource *pres = surf ? pan_resource(surf->texture) : NULL;
632
633 if (pres && pres->damage.tile_map.enable) {
634 fb->tile_map.base =
635 pan_pool_upload_aligned(&batch->pool.base, pres->damage.tile_map.data,
636 pres->damage.tile_map.size, 64);
637 fb->tile_map.stride = pres->damage.tile_map.stride;
638 }
639 }
640
641 static void
panfrost_batch_submit(struct panfrost_context * ctx,struct panfrost_batch * batch)642 panfrost_batch_submit(struct panfrost_context *ctx,
643 struct panfrost_batch *batch)
644 {
645 struct pipe_screen *pscreen = ctx->base.screen;
646 struct panfrost_screen *screen = pan_screen(pscreen);
647 bool has_frag = panfrost_has_fragment_job(batch);
648 int ret;
649
650 /* Nothing to do! */
651 if (!has_frag && batch->compute_count == 0 && !batch->has_time_query)
652 goto out;
653
654 if (batch->key.zsbuf && has_frag) {
655 struct pipe_surface *surf = batch->key.zsbuf;
656 struct panfrost_resource *z_rsrc = pan_resource(surf->texture);
657
658 /* if there are multiple levels or layers, we optimize only the first */
659 if (surf->u.tex.level == 0 && surf->u.tex.first_layer == 0) {
660 /* Shared depth/stencil resources are not supported, and would
661 * break this optimisation. */
662 assert(!(z_rsrc->base.bind & PAN_BIND_SHARED_MASK));
663
664 if (batch->clear & PIPE_CLEAR_STENCIL) {
665 z_rsrc->stencil_value = batch->clear_stencil;
666 z_rsrc->constant_stencil = true;
667 } else if (z_rsrc->constant_stencil) {
668 batch->clear_stencil = z_rsrc->stencil_value;
669 batch->clear |= PIPE_CLEAR_STENCIL;
670 }
671 }
672
673 if (batch->draws & PIPE_CLEAR_STENCIL)
674 z_rsrc->constant_stencil = false;
675 }
676
677 struct pan_fb_info fb;
678 struct pan_image_view rts[8], zs, s;
679
680 panfrost_batch_to_fb_info(batch, &fb, rts, &zs, &s, false);
681 panfrost_emit_tile_map(batch, &fb);
682
683 ret = screen->vtbl.submit_batch(batch, &fb);
684 if (ret)
685 mesa_loge("panfrost_batch_submit failed: %d\n", ret);
686
687 /* We must reset the damage info of our render targets here even
688 * though a damage reset normally happens when the DRI layer swaps
689 * buffers. That's because there can be implicit flushes the GL
690 * app is not aware of, and those might impact the damage region: if
691 * part of the damaged portion is drawn during those implicit flushes,
692 * you have to reload those areas before next draws are pushed, and
693 * since the driver can't easily know what's been modified by the draws
694 * it flushed, the easiest solution is to reload everything.
695 */
696 for (unsigned i = 0; i < batch->key.nr_cbufs; i++) {
697 if (!batch->key.cbufs[i])
698 continue;
699
700 panfrost_resource_set_damage_region(
701 ctx->base.screen, batch->key.cbufs[i]->texture, 0, NULL);
702 }
703
704 out:
705 panfrost_batch_cleanup(ctx, batch);
706 }
707
708 /* Submit all batches */
709
710 void
panfrost_flush_all_batches(struct panfrost_context * ctx,const char * reason)711 panfrost_flush_all_batches(struct panfrost_context *ctx, const char *reason)
712 {
713 if (reason)
714 perf_debug(ctx, "Flushing everything due to: %s", reason);
715
716 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
717 if (!batch)
718 return;
719
720 panfrost_batch_submit(ctx, batch);
721
722 for (unsigned i = 0; i < PAN_MAX_BATCHES; i++) {
723 if (ctx->batches.slots[i].seqnum)
724 panfrost_batch_submit(ctx, &ctx->batches.slots[i]);
725 }
726 }
727
728 void
panfrost_flush_writer(struct panfrost_context * ctx,struct panfrost_resource * rsrc,const char * reason)729 panfrost_flush_writer(struct panfrost_context *ctx,
730 struct panfrost_resource *rsrc, const char *reason)
731 {
732 struct hash_entry *entry = _mesa_hash_table_search(ctx->writers, rsrc);
733
734 if (entry) {
735 perf_debug(ctx, "Flushing writer due to: %s", reason);
736 panfrost_batch_submit(ctx, entry->data);
737 }
738 }
739
740 void
panfrost_flush_batches_accessing_rsrc(struct panfrost_context * ctx,struct panfrost_resource * rsrc,const char * reason)741 panfrost_flush_batches_accessing_rsrc(struct panfrost_context *ctx,
742 struct panfrost_resource *rsrc,
743 const char *reason)
744 {
745 unsigned i;
746 foreach_batch(ctx, i) {
747 struct panfrost_batch *batch = &ctx->batches.slots[i];
748
749 if (!panfrost_batch_uses_resource(batch, rsrc))
750 continue;
751
752 perf_debug(ctx, "Flushing user due to: %s", reason);
753 panfrost_batch_submit(ctx, batch);
754 }
755 }
756
757 bool
panfrost_any_batch_reads_rsrc(struct panfrost_context * ctx,struct panfrost_resource * rsrc)758 panfrost_any_batch_reads_rsrc(struct panfrost_context *ctx,
759 struct panfrost_resource *rsrc)
760 {
761 unsigned i;
762 foreach_batch(ctx, i) {
763 struct panfrost_batch *batch = &ctx->batches.slots[i];
764
765 if (panfrost_batch_uses_resource(batch, rsrc))
766 return true;
767 }
768
769 return false;
770 }
771
772 bool
panfrost_any_batch_writes_rsrc(struct panfrost_context * ctx,struct panfrost_resource * rsrc)773 panfrost_any_batch_writes_rsrc(struct panfrost_context *ctx,
774 struct panfrost_resource *rsrc)
775 {
776 return _mesa_hash_table_search(ctx->writers, rsrc) != NULL;
777 }
778
779 void
panfrost_batch_adjust_stack_size(struct panfrost_batch * batch)780 panfrost_batch_adjust_stack_size(struct panfrost_batch *batch)
781 {
782 struct panfrost_context *ctx = batch->ctx;
783
784 for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
785 struct panfrost_compiled_shader *ss = ctx->prog[i];
786
787 if (!ss)
788 continue;
789
790 batch->stack_size = MAX2(batch->stack_size, ss->info.tls_size);
791 }
792 }
793
794 void
panfrost_batch_clear(struct panfrost_batch * batch,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)795 panfrost_batch_clear(struct panfrost_batch *batch, unsigned buffers,
796 const union pipe_color_union *color, double depth,
797 unsigned stencil)
798 {
799 struct panfrost_context *ctx = batch->ctx;
800 struct panfrost_device *dev = pan_device(ctx->base.screen);
801
802 if (buffers & PIPE_CLEAR_COLOR) {
803 for (unsigned i = 0; i < ctx->pipe_framebuffer.nr_cbufs; ++i) {
804 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
805 continue;
806
807 enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
808 pan_pack_color(dev->blendable_formats, batch->clear_color[i], color,
809 format, false);
810 }
811 }
812
813 if (buffers & PIPE_CLEAR_DEPTH) {
814 batch->clear_depth = depth;
815 }
816
817 if (buffers & PIPE_CLEAR_STENCIL) {
818 batch->clear_stencil = stencil;
819 }
820
821 batch->clear |= buffers;
822 batch->resolve |= buffers;
823
824 /* Clearing affects the entire framebuffer (by definition -- this is
825 * the Gallium clear callback, which clears the whole framebuffer. If
826 * the scissor test were enabled from the GL side, the gallium frontend
827 * would emit a quad instead and we wouldn't go down this code path) */
828
829 panfrost_batch_union_scissor(batch, 0, 0, ctx->pipe_framebuffer.width,
830 ctx->pipe_framebuffer.height);
831 }
832
833 /* Given a new bounding rectangle (scissor), let the job cover the union of the
834 * new and old bounding rectangles */
835
836 void
panfrost_batch_union_scissor(struct panfrost_batch * batch,unsigned minx,unsigned miny,unsigned maxx,unsigned maxy)837 panfrost_batch_union_scissor(struct panfrost_batch *batch, unsigned minx,
838 unsigned miny, unsigned maxx, unsigned maxy)
839 {
840 batch->minx = MIN2(batch->minx, minx);
841 batch->miny = MIN2(batch->miny, miny);
842 batch->maxx = MAX2(batch->maxx, maxx);
843 batch->maxy = MAX2(batch->maxy, maxy);
844 }
845
846 /**
847 * Checks if rasterization should be skipped. If not, a TILER job must be
848 * created for each draw, or the IDVS flow must be used.
849 *
850 * As a special case, if there is no vertex shader, no primitives are generated,
851 * meaning the whole pipeline (including rasterization) should be skipped.
852 */
853 bool
panfrost_batch_skip_rasterization(struct panfrost_batch * batch)854 panfrost_batch_skip_rasterization(struct panfrost_batch *batch)
855 {
856 struct panfrost_context *ctx = batch->ctx;
857 struct pipe_rasterizer_state *rast = (void *)ctx->rasterizer;
858
859 return (rast->rasterizer_discard || batch->scissor_culls_everything ||
860 !batch->rsd[PIPE_SHADER_VERTEX]);
861 }
862