freedreno: replace fnv1a hash function with xxhash
[mesa.git] / src / gallium / drivers / freedreno / freedreno_batch_cache.c
index 408d48ccdb6037728f613af4b93e04a875d29207..329ed41fba363e34b9e9cdb7393cd234c10ebad7 100644 (file)
@@ -28,6 +28,8 @@
 #include "util/set.h"
 #include "util/list.h"
 #include "util/u_string.h"
+#define XXH_INLINE_ALL
+#include "util/xxhash.h"
 
 #include "freedreno_batch.h"
 #include "freedreno_batch_cache.h"
@@ -81,7 +83,8 @@ struct key {
        struct {
                struct pipe_resource *texture;
                union pipe_surface_desc u;
-               uint16_t pos, format;
+               uint8_t pos, samples;
+               uint16_t format;
        } surf[0];
 };
 
@@ -97,9 +100,9 @@ static uint32_t
 key_hash(const void *_key)
 {
        const struct key *key = _key;
-       uint32_t hash = _mesa_fnv32_1a_offset_bias;
-       hash = _mesa_fnv32_1a_accumulate_block(hash, key, offsetof(struct key, surf[0]));
-       hash = _mesa_fnv32_1a_accumulate_block(hash, key->surf, sizeof(key->surf[0]) * key->num_surfs);
+       uint32_t hash = 0;
+       hash = XXH32(key, offsetof(struct key, surf[0]), hash);
+       hash = XXH32(key->surf, sizeof(key->surf[0]) * key->num_surfs , hash);
        return hash;
 }
 
@@ -158,7 +161,7 @@ bc_flush(struct fd_batch_cache *cache, struct fd_context *ctx, bool deferred)
                fd_context_unlock(ctx);
 
                for (unsigned i = 0; i < n; i++) {
-                       fd_batch_flush(batches[i], false, false);
+                       fd_batch_flush(batches[i]);
                }
        }
 
@@ -190,14 +193,14 @@ fd_bc_invalidate_context(struct fd_context *ctx)
        struct fd_batch_cache *cache = &ctx->screen->batch_cache;
        struct fd_batch *batch;
 
-       mtx_lock(&ctx->screen->lock);
+       fd_screen_lock(ctx->screen);
 
        foreach_batch(batch, cache, cache->batch_mask) {
                if (batch->ctx == ctx)
                        fd_bc_invalidate_batch(batch, true);
        }
 
-       mtx_unlock(&ctx->screen->lock);
+       fd_screen_unlock(ctx->screen);
 }
 
 /**
@@ -249,7 +252,7 @@ fd_bc_invalidate_resource(struct fd_resource *rsc, bool destroy)
        struct fd_screen *screen = fd_screen(rsc->base.screen);
        struct fd_batch *batch;
 
-       mtx_lock(&screen->lock);
+       fd_screen_lock(screen);
 
        if (destroy) {
                foreach_batch(batch, &screen->batch_cache, rsc->batch_mask) {
@@ -266,7 +269,7 @@ fd_bc_invalidate_resource(struct fd_resource *rsc, bool destroy)
 
        rsc->bc_batch_mask = 0;
 
-       mtx_unlock(&screen->lock);
+       fd_screen_unlock(screen);
 }
 
 struct fd_batch *
@@ -275,7 +278,7 @@ fd_bc_alloc_batch(struct fd_batch_cache *cache, struct fd_context *ctx, bool non
        struct fd_batch *batch;
        uint32_t idx;
 
-       mtx_lock(&ctx->screen->lock);
+       fd_screen_lock(ctx->screen);
 
        while ((idx = ffs(~cache->batch_mask)) == 0) {
 #if 0
@@ -294,9 +297,6 @@ fd_bc_alloc_batch(struct fd_batch_cache *cache, struct fd_context *ctx, bool non
                 */
                struct fd_batch *flush_batch = NULL;
                for (unsigned i = 0; i < ARRAY_SIZE(cache->batches); i++) {
-                       if ((cache->batches[i] == ctx->batch) ||
-                                       !cache->batches[i]->needs_flush)
-                               continue;
                        if (!flush_batch || (cache->batches[i]->seqno < flush_batch->seqno))
                                fd_batch_reference_locked(&flush_batch, cache->batches[i]);
                }
@@ -304,10 +304,10 @@ fd_bc_alloc_batch(struct fd_batch_cache *cache, struct fd_context *ctx, bool non
                /* we can drop lock temporarily here, since we hold a ref,
                 * flush_batch won't disappear under us.
                 */
-               mtx_unlock(&ctx->screen->lock);
+               fd_screen_unlock(ctx->screen);
                DBG("%p: too many batches!  flush forced!", flush_batch);
-               fd_batch_flush(flush_batch, true, false);
-               mtx_lock(&ctx->screen->lock);
+               fd_batch_flush(flush_batch);
+               fd_screen_lock(ctx->screen);
 
                /* While the resources get cleaned up automatically, the flush_batch
                 * doesn't get removed from the dependencies of other batches, so
@@ -344,7 +344,7 @@ fd_bc_alloc_batch(struct fd_batch_cache *cache, struct fd_context *ctx, bool non
        cache->batches[idx] = batch;
 
 out:
-       mtx_unlock(&ctx->screen->lock);
+       fd_screen_unlock(ctx->screen);
 
        return batch;
 }
@@ -379,7 +379,15 @@ batch_from_key(struct fd_batch_cache *cache, struct key *key,
        if (!batch)
                return NULL;
 
-       mtx_lock(&ctx->screen->lock);
+       /* reset max_scissor, which will be adjusted on draws
+        * according to the actual scissor.
+        */
+       batch->max_scissor.minx = ~0;
+       batch->max_scissor.miny = ~0;
+       batch->max_scissor.maxx = 0;
+       batch->max_scissor.maxy = 0;
+
+       fd_screen_lock(ctx->screen);
 
        _mesa_hash_table_insert_pre_hashed(cache->ht, hash, key, batch);
        batch->key = key;
@@ -390,7 +398,7 @@ batch_from_key(struct fd_batch_cache *cache, struct key *key,
                rsc->bc_batch_mask = (1 << batch->idx);
        }
 
-       mtx_unlock(&ctx->screen->lock);
+       fd_screen_unlock(ctx->screen);
 
        return batch;
 }
@@ -401,6 +409,7 @@ key_surf(struct key *key, unsigned idx, unsigned pos, struct pipe_surface *psurf
        key->surf[idx].texture = psurf->texture;
        key->surf[idx].u = psurf->u;
        key->surf[idx].pos = pos;
+       key->surf[idx].samples = MAX2(1, psurf->nr_samples);
        key->surf[idx].format = psurf->format;
 }