Debian Codegraph @

src/panfrost/shared/pan_minmax_cache.c

raw
1 /*
2 * Copyright (c) 2020 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 /* Index buffer min/max cache. We need to calculate the min/max for arbitrary
7 * slices (start, start + count) of the index buffer at drawtime. As this can
8 * be quite expensive, we cache. Conceptually, we just use a hash table mapping
9 * the key (start, count) to the value (min, max). In practice, mesa's hash
10 * table implementation is higher overhead than we would like and makes
11 * handling memory usage a little complicated. So we use this data structure
12 * instead. Searching is O(n) to the size, but the size is capped at the
13 * PANFROST_MINMAX_SIZE constant (so this is a tradeoff between cache hit/miss
14 * ratio and cache search speed). Note that keys are adjacent so we get cache
15 * line alignment benefits. Insertion is O(1) and in-order until the cache
16 * fills up, after that it evicts the oldest cached value in a ring facilitated
17 * by index.
18 */
19
20 #include "pan_minmax_cache.h"
21 #include "util/macros.h"
22
23 /*
24 * note: a count of 0 would be an empty range, which we don't have to
25 * cache; so returning a 0 to indicate "do not cache" is sensible
26 * otherwise create a key that encodes the start, count, and index size
27 */
28 static uint64_t
29 pan_calc_cache_key(struct pan_minmax_cache *cache, unsigned index_size,
30 unsigned start, unsigned count)
31 {
32 uint64_t ht_key;
33
34 if (!cache)
35 return 0; /* do not cache if no cache! */
36
37 /* we're going to store the item size in the upper bits of the count;
38 * if the count is too big to do this safely, bail and do not use
39 * the cache (this case is going to be horrible no matter what we do,
40 * and is highly unlikely)
41 */
42 if (count > 0x3FFFFFFF)
43 return 0; /* do not cache */
44
45 /* find log2(index_size) or die tryin' */
46 switch (index_size) {
47 case 1:
48 index_size = 0;
49 break;
50 case 2:
51 index_size = 1;
52 break;
53 case 4:
54 index_size = 2;
55 break;
56 default:
57 UNREACHABLE("unknown index size");
58 }
59 count = count | (index_size << 30);
60 ht_key = ((uint64_t)count << 32) | start;
61 return ht_key;
62 }
63
64 bool
65 pan_minmax_cache_get(struct pan_minmax_cache *cache, unsigned index_size,
66 unsigned start, unsigned count, unsigned *min_index,
67 unsigned *max_index)
68 {
69 uint64_t ht_key = pan_calc_cache_key(cache, index_size, start, count);
70 bool found = false;
71
72 if (!ht_key)
73 return false;
74
75
76 for (unsigned i = 0; i < cache->size; ++i) {
77 if (cache->keys[i] == ht_key) {
78 uint64_t hit = cache->values[i];
79
80 *min_index = hit & 0xffffffff;
81 *max_index = hit >> 32;
82 found = true;
83 break;
84 }
85 }
86
87 return found;
88 }
89
90 void
91 pan_minmax_cache_add(struct pan_minmax_cache *cache, unsigned index_size,
92 unsigned start, unsigned count, unsigned min_index,
93 unsigned max_index)
94 {
95 uint64_t ht_key = pan_calc_cache_key(cache, index_size, start, count);
96 uint64_t value = min_index | (((uint64_t)max_index) << 32);
97 unsigned index = 0;
98
99 if (!ht_key)
100 return;
101
102 if (cache->size == PANFROST_MINMAX_SIZE) {
103 index = cache->index++;
104 cache->index = cache->index % PANFROST_MINMAX_SIZE;
105 } else {
106 index = cache->size++;
107 }
108
109 cache->keys[index] = ht_key;
110 cache->values[index] = value;
111 }
112
113 /* If we've been caching min/max indices and we update the index
114 * buffer, that may invalidate the min/max. Check what's been cached vs
115 * what we've written, and throw out invalid entries. */
116
117 void
118 pan_minmax_cache_invalidate(struct pan_minmax_cache *cache, unsigned index_size,
119 size_t offset, size_t size)
120 {
121 /* Ensure there is a cache to invalidate and a write */
122 if (!cache)
123 return;
124
125 /* convert offset and size to bytes, so that if we
126 update a region using a different item size we
127 still invalidate it */
128 offset *= index_size;
129 size *= index_size;
130 unsigned valid_count = 0;
131
132 for (unsigned i = 0; i < cache->size; ++i) {
133 uint64_t key = cache->keys[i];
134
135 /* the item size is in the upper 2 bits of the key
136 * as above, convert size and count to bytes to make
137 * region comparison agnostic to item size
138 */
139 uint32_t key_index_size = (key >> 62);
140 size_t count = ((key >> 32) & 0x3fffffff) << key_index_size;
141 size_t start = (key & 0xffffffff) << key_index_size;
142
143 /* 1D range intersection */
144 bool invalid = MAX2(offset, start) <
145 MIN2(offset + size, start + count);
146 if (!invalid) {
147 cache->keys[valid_count] = key;
148 cache->values[valid_count] = cache->values[i];
149 valid_count++;
150 }
151 }
152
153 cache->size = valid_count;
154 cache->index = 0;
155 }