Path: blob/21.2-virgl/src/panfrost/shared/pan_minmax_cache.c
4560 views
/*1* Copyright (c) 2020 Collabora, Ltd.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors (Collabora):23* Alyssa Rosenzweig <[email protected]>24*/2526/* Index buffer min/max cache. We need to calculate the min/max for arbitrary27* slices (start, start + count) of the index buffer at drawtime. As this can28* be quite expensive, we cache. Conceptually, we just use a hash table mapping29* the key (start, count) to the value (min, max). In practice, mesa's hash30* table implementation is higher overhead than we would like and makes31* handling memory usage a little complicated. So we use this data structure32* instead. Searching is O(n) to the size, but the size is capped at the33* PANFROST_MINMAX_SIZE constant (so this is a tradeoff between cache hit/miss34* ratio and cache search speed). Note that keys are adjacent so we get cache35* line alignment benefits. Insertion is O(1) and in-order until the cache36* fills up, after that it evicts the oldest cached value in a ring facilitated37* by index.38*/3940#include "pan_minmax_cache.h"4142bool43panfrost_minmax_cache_get(struct panfrost_minmax_cache *cache, unsigned start, unsigned count,44unsigned *min_index, unsigned *max_index)45{46uint64_t ht_key = (((uint64_t)count) << 32) | start;47bool found = false;4849if (!cache)50return false;5152for (unsigned i = 0; i < cache->size; ++i) {53if (cache->keys[i] == ht_key) {54uint64_t hit = cache->values[i];5556*min_index = hit & 0xffffffff;57*max_index = hit >> 32;58found = true;59break;60}61}6263return found;64}6566void67panfrost_minmax_cache_add(struct panfrost_minmax_cache *cache, unsigned start, unsigned count,68unsigned min_index, unsigned max_index)69{70uint64_t ht_key = (((uint64_t)count) << 32) | start;71uint64_t value = min_index | (((uint64_t)max_index) << 32);72unsigned index = 0;7374if (!cache)75return;7677if (cache->size == PANFROST_MINMAX_SIZE) {78index = cache->index++;79cache->index = cache->index % PANFROST_MINMAX_SIZE;80} else {81index = cache->size++;82}8384cache->keys[index] = ht_key;85cache->values[index] = value;8687}8889/* If we've been caching min/max indices and we update the index90* buffer, that may invalidate the min/max. Check what's been cached vs91* what we've written, and throw out invalid entries. */9293void94panfrost_minmax_cache_invalidate(struct panfrost_minmax_cache *cache, struct pipe_transfer *transfer)95{96/* Ensure there is a cache to invalidate and a write */97if (!cache)98return;99100if (!(transfer->usage & PIPE_MAP_WRITE))101return;102103unsigned valid_count = 0;104105for (unsigned i = 0; i < cache->size; ++i) {106uint64_t key = cache->keys[i];107108uint32_t start = key & 0xffffffff;109uint32_t count = key >> 32;110111/* 1D range intersection */112bool invalid = MAX2(transfer->box.x, start) < MIN2(transfer->box.x + transfer->box.width, start + count);113if (!invalid) {114cache->keys[valid_count] = key;115cache->values[valid_count] = cache->values[i];116valid_count++;117}118}119120cache->size = valid_count;121cache->index = 0;122}123124125