1 /*
2 * Copyright (c) 2020 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 /* Index buffer min/max cache. We need to calculate the min/max for arbitrary
28 * slices (start, start + count) of the index buffer at drawtime. As this can
29 * be quite expensive, we cache. Conceptually, we just use a hash table mapping
30 * the key (start, count) to the value (min, max). In practice, mesa's hash
31 * table implementation is higher overhead than we would like and makes
32 * handling memory usage a little complicated. So we use this data structure
33 * instead. Searching is O(n) to the size, but the size is capped at the
34 * PANFROST_MINMAX_SIZE constant (so this is a tradeoff between cache hit/miss
35 * ratio and cache search speed). Note that keys are adjacent so we get cache
36 * line alignment benefits. Insertion is O(1) and in-order until the cache
37 * fills up, after that it evicts the oldest cached value in a ring facilitated
38 * by index.
39 */
40
41 #include "pan_minmax_cache.h"
42
43 bool
panfrost_minmax_cache_get(struct panfrost_minmax_cache * cache,unsigned start,unsigned count,unsigned * min_index,unsigned * max_index)44 panfrost_minmax_cache_get(struct panfrost_minmax_cache *cache, unsigned start,
45 unsigned count, unsigned *min_index,
46 unsigned *max_index)
47 {
48 uint64_t ht_key = (((uint64_t)count) << 32) | start;
49 bool found = false;
50
51 if (!cache)
52 return false;
53
54 for (unsigned i = 0; i < cache->size; ++i) {
55 if (cache->keys[i] == ht_key) {
56 uint64_t hit = cache->values[i];
57
58 *min_index = hit & 0xffffffff;
59 *max_index = hit >> 32;
60 found = true;
61 break;
62 }
63 }
64
65 return found;
66 }
67
68 void
panfrost_minmax_cache_add(struct panfrost_minmax_cache * cache,unsigned start,unsigned count,unsigned min_index,unsigned max_index)69 panfrost_minmax_cache_add(struct panfrost_minmax_cache *cache, unsigned start,
70 unsigned count, unsigned min_index,
71 unsigned max_index)
72 {
73 uint64_t ht_key = (((uint64_t)count) << 32) | start;
74 uint64_t value = min_index | (((uint64_t)max_index) << 32);
75 unsigned index = 0;
76
77 if (!cache)
78 return;
79
80 if (cache->size == PANFROST_MINMAX_SIZE) {
81 index = cache->index++;
82 cache->index = cache->index % PANFROST_MINMAX_SIZE;
83 } else {
84 index = cache->size++;
85 }
86
87 cache->keys[index] = ht_key;
88 cache->values[index] = value;
89 }
90
91 /* If we've been caching min/max indices and we update the index
92 * buffer, that may invalidate the min/max. Check what's been cached vs
93 * what we've written, and throw out invalid entries. */
94
95 void
panfrost_minmax_cache_invalidate(struct panfrost_minmax_cache * cache,struct pipe_transfer * transfer)96 panfrost_minmax_cache_invalidate(struct panfrost_minmax_cache *cache,
97 struct pipe_transfer *transfer)
98 {
99 /* Ensure there is a cache to invalidate and a write */
100 if (!cache)
101 return;
102
103 if (!(transfer->usage & PIPE_MAP_WRITE))
104 return;
105
106 unsigned valid_count = 0;
107
108 for (unsigned i = 0; i < cache->size; ++i) {
109 uint64_t key = cache->keys[i];
110
111 uint32_t start = key & 0xffffffff;
112 uint32_t count = key >> 32;
113
114 /* 1D range intersection */
115 bool invalid = MAX2(transfer->box.x, start) <
116 MIN2(transfer->box.x + transfer->box.width, start + count);
117 if (!invalid) {
118 cache->keys[valid_count] = key;
119 cache->values[valid_count] = cache->values[i];
120 valid_count++;
121 }
122 }
123
124 cache->size = valid_count;
125 cache->index = 0;
126 }
127