1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (c) 2020 Collabora, Ltd. 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 (Collabora): 24bf215546Sopenharmony_ci * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com> 25bf215546Sopenharmony_ci */ 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci/* Index buffer min/max cache. We need to calculate the min/max for arbitrary 28bf215546Sopenharmony_ci * slices (start, start + count) of the index buffer at drawtime. As this can 29bf215546Sopenharmony_ci * be quite expensive, we cache. Conceptually, we just use a hash table mapping 30bf215546Sopenharmony_ci * the key (start, count) to the value (min, max). In practice, mesa's hash 31bf215546Sopenharmony_ci * table implementation is higher overhead than we would like and makes 32bf215546Sopenharmony_ci * handling memory usage a little complicated. So we use this data structure 33bf215546Sopenharmony_ci * instead. Searching is O(n) to the size, but the size is capped at the 34bf215546Sopenharmony_ci * PANFROST_MINMAX_SIZE constant (so this is a tradeoff between cache hit/miss 35bf215546Sopenharmony_ci * ratio and cache search speed). Note that keys are adjacent so we get cache 36bf215546Sopenharmony_ci * line alignment benefits. Insertion is O(1) and in-order until the cache 37bf215546Sopenharmony_ci * fills up, after that it evicts the oldest cached value in a ring facilitated 38bf215546Sopenharmony_ci * by index. 39bf215546Sopenharmony_ci */ 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci#include "pan_minmax_cache.h" 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_cibool 44bf215546Sopenharmony_cipanfrost_minmax_cache_get(struct panfrost_minmax_cache *cache, unsigned start, unsigned count, 45bf215546Sopenharmony_ci unsigned *min_index, unsigned *max_index) 46bf215546Sopenharmony_ci{ 47bf215546Sopenharmony_ci uint64_t ht_key = (((uint64_t)count) << 32) | start; 48bf215546Sopenharmony_ci bool found = false; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci if (!cache) 51bf215546Sopenharmony_ci return false; 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci for (unsigned i = 0; i < cache->size; ++i) { 54bf215546Sopenharmony_ci if (cache->keys[i] == ht_key) { 55bf215546Sopenharmony_ci uint64_t hit = cache->values[i]; 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci *min_index = hit & 0xffffffff; 58bf215546Sopenharmony_ci *max_index = hit >> 32; 59bf215546Sopenharmony_ci found = true; 60bf215546Sopenharmony_ci break; 61bf215546Sopenharmony_ci } 62bf215546Sopenharmony_ci } 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci return found; 65bf215546Sopenharmony_ci} 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_civoid 68bf215546Sopenharmony_cipanfrost_minmax_cache_add(struct panfrost_minmax_cache *cache, unsigned start, unsigned count, 69bf215546Sopenharmony_ci unsigned min_index, unsigned max_index) 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci uint64_t ht_key = (((uint64_t)count) << 32) | start; 72bf215546Sopenharmony_ci uint64_t value = min_index | (((uint64_t)max_index) << 32); 73bf215546Sopenharmony_ci unsigned index = 0; 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci if (!cache) 76bf215546Sopenharmony_ci return; 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci if (cache->size == PANFROST_MINMAX_SIZE) { 79bf215546Sopenharmony_ci index = cache->index++; 80bf215546Sopenharmony_ci cache->index = cache->index % PANFROST_MINMAX_SIZE; 81bf215546Sopenharmony_ci } else { 82bf215546Sopenharmony_ci index = cache->size++; 83bf215546Sopenharmony_ci } 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci cache->keys[index] = ht_key; 86bf215546Sopenharmony_ci cache->values[index] = value; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci} 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci/* If we've been caching min/max indices and we update the index 91bf215546Sopenharmony_ci * buffer, that may invalidate the min/max. Check what's been cached vs 92bf215546Sopenharmony_ci * what we've written, and throw out invalid entries. */ 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_civoid 95bf215546Sopenharmony_cipanfrost_minmax_cache_invalidate(struct panfrost_minmax_cache *cache, struct pipe_transfer *transfer) 96bf215546Sopenharmony_ci{ 97bf215546Sopenharmony_ci /* Ensure there is a cache to invalidate and a write */ 98bf215546Sopenharmony_ci if (!cache) 99bf215546Sopenharmony_ci return; 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci if (!(transfer->usage & PIPE_MAP_WRITE)) 102bf215546Sopenharmony_ci return; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci unsigned valid_count = 0; 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci for (unsigned i = 0; i < cache->size; ++i) { 107bf215546Sopenharmony_ci uint64_t key = cache->keys[i]; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci uint32_t start = key & 0xffffffff; 110bf215546Sopenharmony_ci uint32_t count = key >> 32; 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci /* 1D range intersection */ 113bf215546Sopenharmony_ci bool invalid = MAX2(transfer->box.x, start) < MIN2(transfer->box.x + transfer->box.width, start + count); 114bf215546Sopenharmony_ci if (!invalid) { 115bf215546Sopenharmony_ci cache->keys[valid_count] = key; 116bf215546Sopenharmony_ci cache->values[valid_count] = cache->values[i]; 117bf215546Sopenharmony_ci valid_count++; 118bf215546Sopenharmony_ci } 119bf215546Sopenharmony_ci } 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci cache->size = valid_count; 122bf215546Sopenharmony_ci cache->index = 0; 123bf215546Sopenharmony_ci} 124