1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org> 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci * 23bf215546Sopenharmony_ci * Authors: 24bf215546Sopenharmony_ci * Rob Clark <robclark@freedesktop.org> 25bf215546Sopenharmony_ci */ 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "util/hash_table.h" 28bf215546Sopenharmony_ci#include "util/list.h" 29bf215546Sopenharmony_ci#include "util/set.h" 30bf215546Sopenharmony_ci#include "util/u_string.h" 31bf215546Sopenharmony_ci#define XXH_INLINE_ALL 32bf215546Sopenharmony_ci#include "util/xxhash.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#include "freedreno_batch.h" 35bf215546Sopenharmony_ci#include "freedreno_batch_cache.h" 36bf215546Sopenharmony_ci#include "freedreno_context.h" 37bf215546Sopenharmony_ci#include "freedreno_resource.h" 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci/* Overview: 40bf215546Sopenharmony_ci * 41bf215546Sopenharmony_ci * The batch cache provides lookup for mapping pipe_framebuffer_state 42bf215546Sopenharmony_ci * to a batch. 43bf215546Sopenharmony_ci * 44bf215546Sopenharmony_ci * It does this via hashtable, with key that roughly matches the 45bf215546Sopenharmony_ci * pipe_framebuffer_state, as described below. 46bf215546Sopenharmony_ci * 47bf215546Sopenharmony_ci * Batch Cache hashtable key: 48bf215546Sopenharmony_ci * 49bf215546Sopenharmony_ci * To serialize the key, and to avoid dealing with holding a reference to 50bf215546Sopenharmony_ci * pipe_surface's (which hold a reference to pipe_resource and complicate 51bf215546Sopenharmony_ci * the whole refcnting thing), the key is variable length and inline's the 52bf215546Sopenharmony_ci * pertinent details of the pipe_surface. 53bf215546Sopenharmony_ci * 54bf215546Sopenharmony_ci * Batch: 55bf215546Sopenharmony_ci * 56bf215546Sopenharmony_ci * Each batch needs to hold a reference to each resource it depends on (ie. 57bf215546Sopenharmony_ci * anything that needs a mem2gmem). And a weak reference to resources it 58bf215546Sopenharmony_ci * renders to. (If both src[n] and dst[n] are not NULL then they are the 59bf215546Sopenharmony_ci * same.) 60bf215546Sopenharmony_ci * 61bf215546Sopenharmony_ci * When a resource is destroyed, we need to remove entries in the batch 62bf215546Sopenharmony_ci * cache that reference the resource, to avoid dangling pointer issues. 63bf215546Sopenharmony_ci * So each resource holds a hashset of batches which have reference them 64bf215546Sopenharmony_ci * in their hashtable key. 65bf215546Sopenharmony_ci * 66bf215546Sopenharmony_ci * When a batch has weak reference to no more resources (ie. all the 67bf215546Sopenharmony_ci * surfaces it rendered to are destroyed) the batch can be destroyed. 68bf215546Sopenharmony_ci * Could happen in an app that renders and never uses the result. More 69bf215546Sopenharmony_ci * common scenario, I think, will be that some, but not all, of the 70bf215546Sopenharmony_ci * surfaces are destroyed before the batch is submitted. 71bf215546Sopenharmony_ci * 72bf215546Sopenharmony_ci * If (for example), batch writes to zsbuf but that surface is destroyed 73bf215546Sopenharmony_ci * before batch is submitted, we can skip gmem2mem (but still need to 74bf215546Sopenharmony_ci * alloc gmem space as before. If the batch depended on previous contents 75bf215546Sopenharmony_ci * of that surface, it would be holding a reference so the surface would 76bf215546Sopenharmony_ci * not have been destroyed. 77bf215546Sopenharmony_ci */ 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_cistruct fd_batch_key { 80bf215546Sopenharmony_ci uint32_t width; 81bf215546Sopenharmony_ci uint32_t height; 82bf215546Sopenharmony_ci uint16_t layers; 83bf215546Sopenharmony_ci uint16_t samples; 84bf215546Sopenharmony_ci uint16_t num_surfs; 85bf215546Sopenharmony_ci uint16_t ctx_seqno; 86bf215546Sopenharmony_ci struct { 87bf215546Sopenharmony_ci struct pipe_resource *texture; 88bf215546Sopenharmony_ci union pipe_surface_desc u; 89bf215546Sopenharmony_ci uint8_t pos, samples; 90bf215546Sopenharmony_ci uint16_t format; 91bf215546Sopenharmony_ci } surf[0]; 92bf215546Sopenharmony_ci}; 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_cistatic struct fd_batch_key * 95bf215546Sopenharmony_cikey_alloc(unsigned num_surfs) 96bf215546Sopenharmony_ci{ 97bf215546Sopenharmony_ci struct fd_batch_key *key = CALLOC_VARIANT_LENGTH_STRUCT( 98bf215546Sopenharmony_ci fd_batch_key, sizeof(key->surf[0]) * num_surfs); 99bf215546Sopenharmony_ci return key; 100bf215546Sopenharmony_ci} 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ciuint32_t 103bf215546Sopenharmony_cifd_batch_key_hash(const void *_key) 104bf215546Sopenharmony_ci{ 105bf215546Sopenharmony_ci const struct fd_batch_key *key = _key; 106bf215546Sopenharmony_ci uint32_t hash = 0; 107bf215546Sopenharmony_ci hash = XXH32(key, offsetof(struct fd_batch_key, surf[0]), hash); 108bf215546Sopenharmony_ci hash = XXH32(key->surf, sizeof(key->surf[0]) * key->num_surfs, hash); 109bf215546Sopenharmony_ci return hash; 110bf215546Sopenharmony_ci} 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_cibool 113bf215546Sopenharmony_cifd_batch_key_equals(const void *_a, const void *_b) 114bf215546Sopenharmony_ci{ 115bf215546Sopenharmony_ci const struct fd_batch_key *a = _a; 116bf215546Sopenharmony_ci const struct fd_batch_key *b = _b; 117bf215546Sopenharmony_ci return (memcmp(a, b, offsetof(struct fd_batch_key, surf[0])) == 0) && 118bf215546Sopenharmony_ci (memcmp(a->surf, b->surf, sizeof(a->surf[0]) * a->num_surfs) == 0); 119bf215546Sopenharmony_ci} 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_cistruct fd_batch_key * 122bf215546Sopenharmony_cifd_batch_key_clone(void *mem_ctx, const struct fd_batch_key *key) 123bf215546Sopenharmony_ci{ 124bf215546Sopenharmony_ci unsigned sz = 125bf215546Sopenharmony_ci sizeof(struct fd_batch_key) + (sizeof(key->surf[0]) * key->num_surfs); 126bf215546Sopenharmony_ci struct fd_batch_key *new_key = rzalloc_size(mem_ctx, sz); 127bf215546Sopenharmony_ci memcpy(new_key, key, sz); 128bf215546Sopenharmony_ci return new_key; 129bf215546Sopenharmony_ci} 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_civoid 132bf215546Sopenharmony_cifd_bc_init(struct fd_batch_cache *cache) 133bf215546Sopenharmony_ci{ 134bf215546Sopenharmony_ci cache->ht = 135bf215546Sopenharmony_ci _mesa_hash_table_create(NULL, fd_batch_key_hash, fd_batch_key_equals); 136bf215546Sopenharmony_ci} 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_civoid 139bf215546Sopenharmony_cifd_bc_fini(struct fd_batch_cache *cache) 140bf215546Sopenharmony_ci{ 141bf215546Sopenharmony_ci _mesa_hash_table_destroy(cache->ht, NULL); 142bf215546Sopenharmony_ci} 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci/* Flushes all batches in the batch cache. Used at glFlush() and similar times. */ 145bf215546Sopenharmony_civoid 146bf215546Sopenharmony_cifd_bc_flush(struct fd_context *ctx, bool deferred) assert_dt 147bf215546Sopenharmony_ci{ 148bf215546Sopenharmony_ci struct fd_batch_cache *cache = &ctx->screen->batch_cache; 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci /* fd_batch_flush() (and fd_batch_add_dep() which calls it indirectly) 151bf215546Sopenharmony_ci * can cause batches to be unref'd and freed under our feet, so grab 152bf215546Sopenharmony_ci * a reference to all the batches we need up-front. 153bf215546Sopenharmony_ci */ 154bf215546Sopenharmony_ci struct fd_batch *batches[ARRAY_SIZE(cache->batches)] = {0}; 155bf215546Sopenharmony_ci struct fd_batch *batch; 156bf215546Sopenharmony_ci unsigned n = 0; 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci fd_screen_lock(ctx->screen); 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci foreach_batch (batch, cache, cache->batch_mask) { 161bf215546Sopenharmony_ci if (batch->ctx == ctx) { 162bf215546Sopenharmony_ci fd_batch_reference_locked(&batches[n++], batch); 163bf215546Sopenharmony_ci } 164bf215546Sopenharmony_ci } 165bf215546Sopenharmony_ci 166bf215546Sopenharmony_ci /* deferred flush doesn't actually flush, but it marks every other 167bf215546Sopenharmony_ci * batch associated with the context as dependent on the current 168bf215546Sopenharmony_ci * batch. So when the current batch gets flushed, all other batches 169bf215546Sopenharmony_ci * that came before also get flushed. 170bf215546Sopenharmony_ci */ 171bf215546Sopenharmony_ci if (deferred) { 172bf215546Sopenharmony_ci struct fd_batch *current_batch = fd_context_batch(ctx); 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci for (unsigned i = 0; i < n; i++) { 175bf215546Sopenharmony_ci if (batches[i] && (batches[i]->ctx == ctx) && 176bf215546Sopenharmony_ci (batches[i] != current_batch)) { 177bf215546Sopenharmony_ci fd_batch_add_dep(current_batch, batches[i]); 178bf215546Sopenharmony_ci } 179bf215546Sopenharmony_ci } 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci fd_batch_reference_locked(¤t_batch, NULL); 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 184bf215546Sopenharmony_ci } else { 185bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci for (unsigned i = 0; i < n; i++) { 188bf215546Sopenharmony_ci fd_batch_flush(batches[i]); 189bf215546Sopenharmony_ci } 190bf215546Sopenharmony_ci } 191bf215546Sopenharmony_ci 192bf215546Sopenharmony_ci for (unsigned i = 0; i < n; i++) { 193bf215546Sopenharmony_ci fd_batch_reference(&batches[i], NULL); 194bf215546Sopenharmony_ci } 195bf215546Sopenharmony_ci} 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci/** 198bf215546Sopenharmony_ci * Flushes the batch (if any) writing this resource. Must not hold the screen 199bf215546Sopenharmony_ci * lock. 200bf215546Sopenharmony_ci */ 201bf215546Sopenharmony_civoid 202bf215546Sopenharmony_cifd_bc_flush_writer(struct fd_context *ctx, struct fd_resource *rsc) assert_dt 203bf215546Sopenharmony_ci{ 204bf215546Sopenharmony_ci fd_screen_lock(ctx->screen); 205bf215546Sopenharmony_ci struct fd_batch *write_batch = NULL; 206bf215546Sopenharmony_ci fd_batch_reference_locked(&write_batch, rsc->track->write_batch); 207bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci if (write_batch) { 210bf215546Sopenharmony_ci fd_batch_flush(write_batch); 211bf215546Sopenharmony_ci fd_batch_reference(&write_batch, NULL); 212bf215546Sopenharmony_ci } 213bf215546Sopenharmony_ci} 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_ci/** 216bf215546Sopenharmony_ci * Flushes any batches reading this resource. Must not hold the screen lock. 217bf215546Sopenharmony_ci */ 218bf215546Sopenharmony_civoid 219bf215546Sopenharmony_cifd_bc_flush_readers(struct fd_context *ctx, struct fd_resource *rsc) assert_dt 220bf215546Sopenharmony_ci{ 221bf215546Sopenharmony_ci struct fd_batch *batch, *batches[32] = {}; 222bf215546Sopenharmony_ci uint32_t batch_count = 0; 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci /* This is a bit awkward, probably a fd_batch_flush_locked() 225bf215546Sopenharmony_ci * would make things simpler.. but we need to hold the lock 226bf215546Sopenharmony_ci * to iterate the batches which reference this resource. So 227bf215546Sopenharmony_ci * we must first grab references under a lock, then flush. 228bf215546Sopenharmony_ci */ 229bf215546Sopenharmony_ci fd_screen_lock(ctx->screen); 230bf215546Sopenharmony_ci foreach_batch (batch, &ctx->screen->batch_cache, rsc->track->batch_mask) 231bf215546Sopenharmony_ci fd_batch_reference_locked(&batches[batch_count++], batch); 232bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ci for (int i = 0; i < batch_count; i++) { 235bf215546Sopenharmony_ci fd_batch_flush(batches[i]); 236bf215546Sopenharmony_ci fd_batch_reference(&batches[i], NULL); 237bf215546Sopenharmony_ci } 238bf215546Sopenharmony_ci} 239bf215546Sopenharmony_ci 240bf215546Sopenharmony_civoid 241bf215546Sopenharmony_cifd_bc_dump(struct fd_context *ctx, const char *fmt, ...) 242bf215546Sopenharmony_ci{ 243bf215546Sopenharmony_ci struct fd_batch_cache *cache = &ctx->screen->batch_cache; 244bf215546Sopenharmony_ci 245bf215546Sopenharmony_ci if (!FD_DBG(MSGS)) 246bf215546Sopenharmony_ci return; 247bf215546Sopenharmony_ci 248bf215546Sopenharmony_ci fd_screen_lock(ctx->screen); 249bf215546Sopenharmony_ci 250bf215546Sopenharmony_ci va_list ap; 251bf215546Sopenharmony_ci va_start(ap, fmt); 252bf215546Sopenharmony_ci vprintf(fmt, ap); 253bf215546Sopenharmony_ci va_end(ap); 254bf215546Sopenharmony_ci 255bf215546Sopenharmony_ci for (int i = 0; i < ARRAY_SIZE(cache->batches); i++) { 256bf215546Sopenharmony_ci struct fd_batch *batch = cache->batches[i]; 257bf215546Sopenharmony_ci if (batch) { 258bf215546Sopenharmony_ci printf(" %p<%u>%s\n", batch, batch->seqno, 259bf215546Sopenharmony_ci batch->needs_flush ? ", NEEDS FLUSH" : ""); 260bf215546Sopenharmony_ci } 261bf215546Sopenharmony_ci } 262bf215546Sopenharmony_ci 263bf215546Sopenharmony_ci printf("----\n"); 264bf215546Sopenharmony_ci 265bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 266bf215546Sopenharmony_ci} 267bf215546Sopenharmony_ci 268bf215546Sopenharmony_ci/** 269bf215546Sopenharmony_ci * Note that when batch is flushed, it needs to remain in the cache so 270bf215546Sopenharmony_ci * that fd_bc_invalidate_resource() can work.. otherwise we can have 271bf215546Sopenharmony_ci * the case where a rsc is destroyed while a batch still has a dangling 272bf215546Sopenharmony_ci * reference to it. 273bf215546Sopenharmony_ci * 274bf215546Sopenharmony_ci * Note that the cmdstream (or, after the SUBMIT ioctl, the kernel) 275bf215546Sopenharmony_ci * would have a reference to the underlying bo, so it is ok for the 276bf215546Sopenharmony_ci * rsc to be destroyed before the batch. 277bf215546Sopenharmony_ci */ 278bf215546Sopenharmony_civoid 279bf215546Sopenharmony_cifd_bc_invalidate_batch(struct fd_batch *batch, bool remove) 280bf215546Sopenharmony_ci{ 281bf215546Sopenharmony_ci if (!batch) 282bf215546Sopenharmony_ci return; 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_ci struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache; 285bf215546Sopenharmony_ci struct fd_batch_key *key = batch->key; 286bf215546Sopenharmony_ci 287bf215546Sopenharmony_ci fd_screen_assert_locked(batch->ctx->screen); 288bf215546Sopenharmony_ci 289bf215546Sopenharmony_ci if (remove) { 290bf215546Sopenharmony_ci cache->batches[batch->idx] = NULL; 291bf215546Sopenharmony_ci cache->batch_mask &= ~(1 << batch->idx); 292bf215546Sopenharmony_ci } 293bf215546Sopenharmony_ci 294bf215546Sopenharmony_ci if (!key) 295bf215546Sopenharmony_ci return; 296bf215546Sopenharmony_ci 297bf215546Sopenharmony_ci DBG("%p: key=%p", batch, batch->key); 298bf215546Sopenharmony_ci for (unsigned idx = 0; idx < key->num_surfs; idx++) { 299bf215546Sopenharmony_ci struct fd_resource *rsc = fd_resource(key->surf[idx].texture); 300bf215546Sopenharmony_ci rsc->track->bc_batch_mask &= ~(1 << batch->idx); 301bf215546Sopenharmony_ci } 302bf215546Sopenharmony_ci 303bf215546Sopenharmony_ci struct hash_entry *entry = 304bf215546Sopenharmony_ci _mesa_hash_table_search_pre_hashed(cache->ht, batch->hash, key); 305bf215546Sopenharmony_ci _mesa_hash_table_remove(cache->ht, entry); 306bf215546Sopenharmony_ci} 307bf215546Sopenharmony_ci 308bf215546Sopenharmony_civoid 309bf215546Sopenharmony_cifd_bc_invalidate_resource(struct fd_resource *rsc, bool destroy) 310bf215546Sopenharmony_ci{ 311bf215546Sopenharmony_ci struct fd_screen *screen = fd_screen(rsc->b.b.screen); 312bf215546Sopenharmony_ci struct fd_batch *batch; 313bf215546Sopenharmony_ci 314bf215546Sopenharmony_ci fd_screen_lock(screen); 315bf215546Sopenharmony_ci 316bf215546Sopenharmony_ci if (destroy) { 317bf215546Sopenharmony_ci foreach_batch (batch, &screen->batch_cache, rsc->track->batch_mask) { 318bf215546Sopenharmony_ci struct set_entry *entry = _mesa_set_search_pre_hashed(batch->resources, rsc->hash, rsc); 319bf215546Sopenharmony_ci _mesa_set_remove(batch->resources, entry); 320bf215546Sopenharmony_ci } 321bf215546Sopenharmony_ci rsc->track->batch_mask = 0; 322bf215546Sopenharmony_ci 323bf215546Sopenharmony_ci fd_batch_reference_locked(&rsc->track->write_batch, NULL); 324bf215546Sopenharmony_ci } 325bf215546Sopenharmony_ci 326bf215546Sopenharmony_ci foreach_batch (batch, &screen->batch_cache, rsc->track->bc_batch_mask) 327bf215546Sopenharmony_ci fd_bc_invalidate_batch(batch, false); 328bf215546Sopenharmony_ci 329bf215546Sopenharmony_ci rsc->track->bc_batch_mask = 0; 330bf215546Sopenharmony_ci 331bf215546Sopenharmony_ci fd_screen_unlock(screen); 332bf215546Sopenharmony_ci} 333bf215546Sopenharmony_ci 334bf215546Sopenharmony_cistatic struct fd_batch * 335bf215546Sopenharmony_cialloc_batch_locked(struct fd_batch_cache *cache, struct fd_context *ctx, 336bf215546Sopenharmony_ci bool nondraw) assert_dt 337bf215546Sopenharmony_ci{ 338bf215546Sopenharmony_ci struct fd_batch *batch; 339bf215546Sopenharmony_ci uint32_t idx; 340bf215546Sopenharmony_ci 341bf215546Sopenharmony_ci fd_screen_assert_locked(ctx->screen); 342bf215546Sopenharmony_ci 343bf215546Sopenharmony_ci while ((idx = ffs(~cache->batch_mask)) == 0) { 344bf215546Sopenharmony_ci#if 0 345bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(cache->batches); i++) { 346bf215546Sopenharmony_ci batch = cache->batches[i]; 347bf215546Sopenharmony_ci debug_printf("%d: needs_flush=%d, depends:", batch->idx, batch->needs_flush); 348bf215546Sopenharmony_ci set_foreach (batch->dependencies, entry) { 349bf215546Sopenharmony_ci struct fd_batch *dep = (struct fd_batch *)entry->key; 350bf215546Sopenharmony_ci debug_printf(" %d", dep->idx); 351bf215546Sopenharmony_ci } 352bf215546Sopenharmony_ci debug_printf("\n"); 353bf215546Sopenharmony_ci } 354bf215546Sopenharmony_ci#endif 355bf215546Sopenharmony_ci /* TODO: is LRU the better policy? Or perhaps the batch that 356bf215546Sopenharmony_ci * depends on the fewest other batches? 357bf215546Sopenharmony_ci */ 358bf215546Sopenharmony_ci struct fd_batch *flush_batch = NULL; 359bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(cache->batches); i++) { 360bf215546Sopenharmony_ci if (!flush_batch || (cache->batches[i]->seqno < flush_batch->seqno)) 361bf215546Sopenharmony_ci fd_batch_reference_locked(&flush_batch, cache->batches[i]); 362bf215546Sopenharmony_ci } 363bf215546Sopenharmony_ci 364bf215546Sopenharmony_ci /* we can drop lock temporarily here, since we hold a ref, 365bf215546Sopenharmony_ci * flush_batch won't disappear under us. 366bf215546Sopenharmony_ci */ 367bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 368bf215546Sopenharmony_ci DBG("%p: too many batches! flush forced!", flush_batch); 369bf215546Sopenharmony_ci fd_batch_flush(flush_batch); 370bf215546Sopenharmony_ci fd_screen_lock(ctx->screen); 371bf215546Sopenharmony_ci 372bf215546Sopenharmony_ci /* While the resources get cleaned up automatically, the flush_batch 373bf215546Sopenharmony_ci * doesn't get removed from the dependencies of other batches, so 374bf215546Sopenharmony_ci * it won't be unref'd and will remain in the table. 375bf215546Sopenharmony_ci * 376bf215546Sopenharmony_ci * TODO maybe keep a bitmask of batches that depend on me, to make 377bf215546Sopenharmony_ci * this easier: 378bf215546Sopenharmony_ci */ 379bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(cache->batches); i++) { 380bf215546Sopenharmony_ci struct fd_batch *other = cache->batches[i]; 381bf215546Sopenharmony_ci if (!other) 382bf215546Sopenharmony_ci continue; 383bf215546Sopenharmony_ci if (other->dependents_mask & (1 << flush_batch->idx)) { 384bf215546Sopenharmony_ci other->dependents_mask &= ~(1 << flush_batch->idx); 385bf215546Sopenharmony_ci struct fd_batch *ref = flush_batch; 386bf215546Sopenharmony_ci fd_batch_reference_locked(&ref, NULL); 387bf215546Sopenharmony_ci } 388bf215546Sopenharmony_ci } 389bf215546Sopenharmony_ci 390bf215546Sopenharmony_ci fd_batch_reference_locked(&flush_batch, NULL); 391bf215546Sopenharmony_ci } 392bf215546Sopenharmony_ci 393bf215546Sopenharmony_ci idx--; /* bit zero returns 1 for ffs() */ 394bf215546Sopenharmony_ci 395bf215546Sopenharmony_ci batch = fd_batch_create(ctx, nondraw); 396bf215546Sopenharmony_ci if (!batch) 397bf215546Sopenharmony_ci return NULL; 398bf215546Sopenharmony_ci 399bf215546Sopenharmony_ci batch->seqno = cache->cnt++; 400bf215546Sopenharmony_ci batch->idx = idx; 401bf215546Sopenharmony_ci cache->batch_mask |= (1 << idx); 402bf215546Sopenharmony_ci 403bf215546Sopenharmony_ci assert(cache->batches[idx] == NULL); 404bf215546Sopenharmony_ci cache->batches[idx] = batch; 405bf215546Sopenharmony_ci 406bf215546Sopenharmony_ci return batch; 407bf215546Sopenharmony_ci} 408bf215546Sopenharmony_ci 409bf215546Sopenharmony_cistruct fd_batch * 410bf215546Sopenharmony_cifd_bc_alloc_batch(struct fd_context *ctx, bool nondraw) 411bf215546Sopenharmony_ci{ 412bf215546Sopenharmony_ci struct fd_batch_cache *cache = &ctx->screen->batch_cache; 413bf215546Sopenharmony_ci struct fd_batch *batch; 414bf215546Sopenharmony_ci 415bf215546Sopenharmony_ci /* For normal draw batches, pctx->set_framebuffer_state() handles 416bf215546Sopenharmony_ci * this, but for nondraw batches, this is a nice central location 417bf215546Sopenharmony_ci * to handle them all. 418bf215546Sopenharmony_ci */ 419bf215546Sopenharmony_ci if (nondraw) 420bf215546Sopenharmony_ci fd_context_switch_from(ctx); 421bf215546Sopenharmony_ci 422bf215546Sopenharmony_ci fd_screen_lock(ctx->screen); 423bf215546Sopenharmony_ci batch = alloc_batch_locked(cache, ctx, nondraw); 424bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 425bf215546Sopenharmony_ci 426bf215546Sopenharmony_ci if (batch && nondraw) 427bf215546Sopenharmony_ci fd_context_switch_to(ctx, batch); 428bf215546Sopenharmony_ci 429bf215546Sopenharmony_ci return batch; 430bf215546Sopenharmony_ci} 431bf215546Sopenharmony_ci 432bf215546Sopenharmony_cistatic struct fd_batch * 433bf215546Sopenharmony_cibatch_from_key(struct fd_context *ctx, struct fd_batch_key *key) assert_dt 434bf215546Sopenharmony_ci{ 435bf215546Sopenharmony_ci struct fd_batch_cache *cache = &ctx->screen->batch_cache; 436bf215546Sopenharmony_ci struct fd_batch *batch = NULL; 437bf215546Sopenharmony_ci uint32_t hash = fd_batch_key_hash(key); 438bf215546Sopenharmony_ci struct hash_entry *entry = 439bf215546Sopenharmony_ci _mesa_hash_table_search_pre_hashed(cache->ht, hash, key); 440bf215546Sopenharmony_ci 441bf215546Sopenharmony_ci if (entry) { 442bf215546Sopenharmony_ci free(key); 443bf215546Sopenharmony_ci fd_batch_reference_locked(&batch, (struct fd_batch *)entry->data); 444bf215546Sopenharmony_ci assert(!batch->flushed); 445bf215546Sopenharmony_ci return batch; 446bf215546Sopenharmony_ci } 447bf215546Sopenharmony_ci 448bf215546Sopenharmony_ci batch = alloc_batch_locked(cache, ctx, false); 449bf215546Sopenharmony_ci#ifdef DEBUG 450bf215546Sopenharmony_ci DBG("%p: hash=0x%08x, %ux%u, %u layers, %u samples", batch, hash, key->width, 451bf215546Sopenharmony_ci key->height, key->layers, key->samples); 452bf215546Sopenharmony_ci for (unsigned idx = 0; idx < key->num_surfs; idx++) { 453bf215546Sopenharmony_ci DBG("%p: surf[%u]: %p (%s) (%u,%u / %u,%u,%u)", batch, 454bf215546Sopenharmony_ci key->surf[idx].pos, key->surf[idx].texture, 455bf215546Sopenharmony_ci util_format_name(key->surf[idx].format), 456bf215546Sopenharmony_ci key->surf[idx].u.buf.first_element, key->surf[idx].u.buf.last_element, 457bf215546Sopenharmony_ci key->surf[idx].u.tex.first_layer, key->surf[idx].u.tex.last_layer, 458bf215546Sopenharmony_ci key->surf[idx].u.tex.level); 459bf215546Sopenharmony_ci } 460bf215546Sopenharmony_ci#endif 461bf215546Sopenharmony_ci if (!batch) 462bf215546Sopenharmony_ci return NULL; 463bf215546Sopenharmony_ci 464bf215546Sopenharmony_ci /* reset max_scissor, which will be adjusted on draws 465bf215546Sopenharmony_ci * according to the actual scissor. 466bf215546Sopenharmony_ci */ 467bf215546Sopenharmony_ci batch->max_scissor.minx = ~0; 468bf215546Sopenharmony_ci batch->max_scissor.miny = ~0; 469bf215546Sopenharmony_ci batch->max_scissor.maxx = 0; 470bf215546Sopenharmony_ci batch->max_scissor.maxy = 0; 471bf215546Sopenharmony_ci 472bf215546Sopenharmony_ci _mesa_hash_table_insert_pre_hashed(cache->ht, hash, key, batch); 473bf215546Sopenharmony_ci batch->key = key; 474bf215546Sopenharmony_ci batch->hash = hash; 475bf215546Sopenharmony_ci 476bf215546Sopenharmony_ci for (unsigned idx = 0; idx < key->num_surfs; idx++) { 477bf215546Sopenharmony_ci struct fd_resource *rsc = fd_resource(key->surf[idx].texture); 478bf215546Sopenharmony_ci rsc->track->bc_batch_mask = (1 << batch->idx); 479bf215546Sopenharmony_ci } 480bf215546Sopenharmony_ci 481bf215546Sopenharmony_ci return batch; 482bf215546Sopenharmony_ci} 483bf215546Sopenharmony_ci 484bf215546Sopenharmony_cistatic void 485bf215546Sopenharmony_cikey_surf(struct fd_batch_key *key, unsigned idx, unsigned pos, 486bf215546Sopenharmony_ci struct pipe_surface *psurf) 487bf215546Sopenharmony_ci{ 488bf215546Sopenharmony_ci key->surf[idx].texture = psurf->texture; 489bf215546Sopenharmony_ci key->surf[idx].u = psurf->u; 490bf215546Sopenharmony_ci key->surf[idx].pos = pos; 491bf215546Sopenharmony_ci key->surf[idx].samples = MAX2(1, psurf->nr_samples); 492bf215546Sopenharmony_ci key->surf[idx].format = psurf->format; 493bf215546Sopenharmony_ci} 494bf215546Sopenharmony_ci 495bf215546Sopenharmony_cistruct fd_batch * 496bf215546Sopenharmony_cifd_batch_from_fb(struct fd_context *ctx, 497bf215546Sopenharmony_ci const struct pipe_framebuffer_state *pfb) 498bf215546Sopenharmony_ci{ 499bf215546Sopenharmony_ci unsigned idx = 0, n = pfb->nr_cbufs + (pfb->zsbuf ? 1 : 0); 500bf215546Sopenharmony_ci struct fd_batch_key *key = key_alloc(n); 501bf215546Sopenharmony_ci 502bf215546Sopenharmony_ci key->width = pfb->width; 503bf215546Sopenharmony_ci key->height = pfb->height; 504bf215546Sopenharmony_ci key->layers = pfb->layers; 505bf215546Sopenharmony_ci key->samples = util_framebuffer_get_num_samples(pfb); 506bf215546Sopenharmony_ci key->ctx_seqno = ctx->seqno; 507bf215546Sopenharmony_ci 508bf215546Sopenharmony_ci if (pfb->zsbuf) 509bf215546Sopenharmony_ci key_surf(key, idx++, 0, pfb->zsbuf); 510bf215546Sopenharmony_ci 511bf215546Sopenharmony_ci for (unsigned i = 0; i < pfb->nr_cbufs; i++) 512bf215546Sopenharmony_ci if (pfb->cbufs[i]) 513bf215546Sopenharmony_ci key_surf(key, idx++, i + 1, pfb->cbufs[i]); 514bf215546Sopenharmony_ci 515bf215546Sopenharmony_ci key->num_surfs = idx; 516bf215546Sopenharmony_ci 517bf215546Sopenharmony_ci fd_screen_lock(ctx->screen); 518bf215546Sopenharmony_ci struct fd_batch *batch = batch_from_key(ctx, key); 519bf215546Sopenharmony_ci fd_screen_unlock(ctx->screen); 520bf215546Sopenharmony_ci 521bf215546Sopenharmony_ci return batch; 522bf215546Sopenharmony_ci} 523