| 1 |
/* |
| 2 |
* Copyright (C) 2019 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "util/macros.h" |
| 7 |
#include "util/u_math.h" |
| 8 |
#include "pan_encoder.h" |
| 9 |
|
| 10 |
/* Mali GPUs are tiled-mode renderers, rather than immediate-mode. |
| 11 |
* Conceptually, the screen is divided into 16x16 tiles. Vertex shaders run. |
| 12 |
* Then, a fixed-function hardware block (the tiler) consumes the gl_Position |
| 13 |
* results. For each triangle specified, it marks each containing tile as |
| 14 |
* containing that triangle. This set of "triangles per tile" form the "polygon |
| 15 |
* list". Finally, the rasterization unit consumes the polygon list to invoke |
| 16 |
* the fragment shader. |
| 17 |
* |
| 18 |
* In practice, it's a bit more complicated than this. On Midgard chips with an |
| 19 |
* "advanced tiling unit" (all except T720/T820/T830), 16x16 is the logical |
| 20 |
* tile size, but Midgard features "hierarchical tiling", where power-of-two |
| 21 |
* multiples of the base tile size can be used: hierarchy level 0 (16x16), |
| 22 |
* level 1 (32x32), level 2 (64x64), per public information about Midgard's |
| 23 |
* tiling. In fact, tiling goes up to 4096x4096 (!), although in practice |
| 24 |
* 128x128 is the largest usually used (though higher modes are enabled). The |
| 25 |
* idea behind hierarchical tiling is to use low tiling levels for small |
| 26 |
* triangles and high levels for large triangles, to minimize memory bandwidth |
| 27 |
* and repeated fragment shader invocations (the former issue inherent to |
| 28 |
* immediate-mode rendering and the latter common in traditional tilers). |
| 29 |
* |
| 30 |
* The tiler itself works by reading varyings in and writing a polygon list |
| 31 |
* out. Unfortunately (for us), both of these buffers are managed in main |
| 32 |
* memory; although they ideally will be cached, it is the drivers' |
| 33 |
* responsibility to allocate these buffers. Varying buffer allocation is |
| 34 |
* handled elsewhere, as it is not tiler specific; the real issue is allocating |
| 35 |
* the polygon list. |
| 36 |
* |
| 37 |
* This is hard, because from the driver's perspective, we have no information |
| 38 |
* about what geometry will actually look like on screen; that information is |
| 39 |
* only gained from running the vertex shader. (Theoretically, we could run the |
| 40 |
* vertex shaders in software as a prepass, or in hardware with transform |
| 41 |
* feedback as a prepass, but either idea is ludicrous on so many levels). |
| 42 |
* |
| 43 |
* Instead, Mali uses a bit of a hybrid approach, splitting the polygon list |
| 44 |
* into three distinct pieces. First, the driver statically determines which |
| 45 |
* tile hierarchy levels to use (more on that later). At this point, we know the |
| 46 |
* framebuffer dimensions and all the possible tilings of the framebuffer, so |
| 47 |
* we know exactly how many tiles exist across all hierarchy levels. The first |
| 48 |
* piece of the polygon list is the header, which is exactly 8 bytes per tile, |
| 49 |
* plus padding and a small 64-byte prologue. (If that doesn't remind you of |
| 50 |
* AFBC, it should. See pan_afbc.c for some fun parallels). The next part is |
| 51 |
* the polygon list body, which seems to contain 512 bytes per tile, again |
| 52 |
* across every level of the hierarchy. These two parts form the polygon list |
| 53 |
* buffer. This buffer has a statically determinable size, approximately equal |
| 54 |
* to the # of tiles across all hierarchy levels * (8 bytes + 512 bytes), plus |
| 55 |
* alignment / minimum restrictions / etc. |
| 56 |
* |
| 57 |
* The third piece is the easy one (for us): the tiler heap. In essence, the |
| 58 |
* tiler heap is a gigantic slab that's as big as could possibly be necessary |
| 59 |
* in the worst case imaginable. Just... a gigantic allocation that we give a |
| 60 |
* start and end pointer to. What's the catch? The tiler heap is lazily |
| 61 |
* allocated; that is, a huge amount of memory is _reserved_, but only a tiny |
| 62 |
* bit is actually allocated upfront. The GPU just keeps using the |
| 63 |
* unallocated-but-reserved portions as it goes along, generating page faults |
| 64 |
* if it goes beyond the allocation, and then the kernel is instructed to |
| 65 |
* expand the allocation on page fault (known in the vendor kernel as growable |
| 66 |
* memory). This is quite a bit of bookkeeping of its own, but that task is |
| 67 |
* pushed to kernel space and we can mostly ignore it here, just remembering to |
| 68 |
* set the GROWABLE flag so the kernel actually uses this path rather than |
| 69 |
* allocating a gigantic amount up front and burning a hole in RAM. |
| 70 |
* |
| 71 |
* As far as determining which hierarchy levels to use, the simple answer is |
| 72 |
* that right now, we don't. In the tiler configuration fields (consistent from |
| 73 |
* the earliest Midgard's SFBD through the latest Bifrost traces we have), |
| 74 |
* there is a hierarchy_mask field, controlling which levels (tile sizes) are |
| 75 |
* enabled. Ideally, the hierarchical tiling dream -- mapping big polygons to |
| 76 |
* big tiles and small polygons to small tiles -- would be realized here as |
| 77 |
* well. As long as there are polygons at all needing tiling, we always have to |
| 78 |
* have big tiles available, in case there are big polygons. But we don't |
| 79 |
* necessarily need small tiles available. Ideally, when there are small |
| 80 |
* polygons, small tiles are enabled (to avoid waste from putting small |
| 81 |
* triangles in the big tiles); when there are not, small tiles are disabled to |
| 82 |
* avoid enabling more levels than necessary, which potentially costs in memory |
| 83 |
* bandwidth / power / tiler performance. |
| 84 |
* |
| 85 |
* Of course, the driver has to figure this out statically. When tile |
| 86 |
* hiearchies are actually established, this occurs by the tiler in |
| 87 |
* fixed-function hardware, after the vertex shaders have run and there is |
| 88 |
* sufficient information to figure out the size of triangles. The driver has |
| 89 |
* no such luxury, again barring insane hacks like additionally running the |
| 90 |
* vertex shaders in software or in hardware via transform feedback. Thus, for |
| 91 |
* the driver, we need a heuristic approach. |
| 92 |
* |
| 93 |
* There are lots of heuristics to guess triangle size statically you could |
| 94 |
* imagine, but one approach shines as particularly simple-stupid: assume all |
| 95 |
* on-screen triangles are equal size and spread equidistantly throughout the |
| 96 |
* screen. Let's be clear, this is NOT A VALID ASSUMPTION. But if we roll with |
| 97 |
* it, then we see: |
| 98 |
* |
| 99 |
* Triangle Area = (Screen Area / # of triangles) |
| 100 |
* = (Width * Height) / (# of triangles) |
| 101 |
* |
| 102 |
* Or if you prefer, we can also make a third CRAZY assumption that we only draw |
| 103 |
* right triangles with edges parallel/perpendicular to the sides of the screen |
| 104 |
* with no overdraw, forming a triangle grid across the screen: |
| 105 |
* |
| 106 |
* |--w--| |
| 107 |
* _____ | |
| 108 |
* | /| /| | |
| 109 |
* |/_|/_| h |
| 110 |
* | /| /| | |
| 111 |
* |/_|/_| | |
| 112 |
* |
| 113 |
* Then you can use some middle school geometry and algebra to work out the |
| 114 |
* triangle dimensions. I started working on this, but realised I didn't need |
| 115 |
* to to make my point, but couldn't bare to erase that ASCII art. Anyway. |
| 116 |
* |
| 117 |
* POINT IS, by considering the ratio of screen area and triangle count, we can |
| 118 |
* estimate the triangle size. For a small size, use small bins; for a large |
| 119 |
* size, use large bins. Intuitively, this metric makes sense: when there are |
| 120 |
* few triangles on a large screen, you're probably compositing a UI and |
| 121 |
* therefore the triangles are large; when there are a lot of triangles on a |
| 122 |
* small screen, you're probably rendering a 3D mesh and therefore the |
| 123 |
* triangles are tiny. (Or better said -- there will be tiny triangles, even if |
| 124 |
* there are also large triangles. There have to be unless you expect crazy |
| 125 |
* overdraw. Generally, it's better to allow more small bin sizes than |
| 126 |
* necessary than not allow enough.) |
| 127 |
* |
| 128 |
* From this heuristic (or whatever), we determine the minimum allowable tile |
| 129 |
* size, and we use that to decide the hierarchy masking, selecting from the |
| 130 |
* minimum "ideal" tile size to the maximum tile size (2048x2048 in practice). |
| 131 |
* |
| 132 |
* Once we have that mask and the framebuffer dimensions, we can compute the |
| 133 |
* size of the statically-sized polygon list structures, allocate them, and go! |
| 134 |
* |
| 135 |
* ----- |
| 136 |
* |
| 137 |
* On T720, T820, and T830, there is no support for hierarchical tiling. |
| 138 |
* Instead, the hardware allows the driver to select the tile size dynamically |
| 139 |
* on a per-framebuffer basis, including allowing rectangular/non-square tiles. |
| 140 |
* Rules for tile size selection are as follows: |
| 141 |
* |
| 142 |
* - Dimensions must be powers-of-two. |
| 143 |
* - The smallest tile is 16x16. |
| 144 |
* - The tile width/height is at most the framebuffer w/h (clamp up to 16 pix) |
| 145 |
* - There must be no more than 64 tiles in either dimension. |
| 146 |
* |
| 147 |
* Within these constraints, the driver is free to pick a tile size according |
| 148 |
* to some heuristic, similar to units with an advanced tiling unit. |
| 149 |
* |
| 150 |
* To pick a size without any heuristics, we may satisfy the constraints by |
| 151 |
* defaulting to 16x16 (a power-of-two). This fits the minimum. For the size |
| 152 |
* constraint, consider: |
| 153 |
* |
| 154 |
* # of tiles < 64 |
| 155 |
* ceil (fb / tile) < 64 |
| 156 |
* (fb / tile) <= (64 - 1) |
| 157 |
* tile <= fb / (64 - 1) <= next_power_of_two(fb / (64 - 1)) |
| 158 |
* |
| 159 |
* Hence we clamp up to align_pot(fb / (64 - 1)). |
| 160 |
|
| 161 |
* Extending to use a selection heuristic left for future work. |
| 162 |
* |
| 163 |
* Once the tile size (w, h) is chosen, we compute the hierarchy "mask": |
| 164 |
* |
| 165 |
* hierarchy_mask = (log2(h / 16) << 6) | log2(w / 16) |
| 166 |
* |
| 167 |
* Of course with no hierarchical tiling, this is not a mask; it's just a field |
| 168 |
* specifying the tile size. But I digress. |
| 169 |
* |
| 170 |
* We also compute the polgon list sizes (with framebuffer size W, H) as: |
| 171 |
* |
| 172 |
* full_size = 0x200 + 0x200 * ceil(W / w) * ceil(H / h) |
| 173 |
* offset = 8 * ceil(W / w) * ceil(H / h) |
| 174 |
* |
| 175 |
* It further appears necessary to round down offset to the nearest 0x200. |
| 176 |
* Possibly we would also round down full_size to the nearest 0x200 but |
| 177 |
* full_size/0x200 = (1 + ceil(W / w) * ceil(H / h)) is an integer so there's |
| 178 |
* nothing to do. |
| 179 |
*/ |
| 180 |
|
| 181 |
/* Hierarchical tiling spans from 16x16 to 4096x4096 tiles */ |
| 182 |
|
| 183 |
#define MIN_TILE_SIZE 16 |
| 184 |
#define MAX_TILE_SIZE 4096 |
| 185 |
|
| 186 |
/* Constants as shifts for easier power-of-two iteration */ |
| 187 |
|
| 188 |
#define MIN_TILE_SHIFT util_logbase2(MIN_TILE_SIZE) |
| 189 |
#define MAX_TILE_SHIFT util_logbase2(MAX_TILE_SIZE) |
| 190 |
|
| 191 |
/* The hierarchy has a 64-byte prologue */ |
| 192 |
#define PROLOGUE_SIZE 0x40 |
| 193 |
|
| 194 |
/* For each tile (across all hierarchy levels), there is 8 bytes of header */ |
| 195 |
#define HEADER_BYTES_PER_TILE 0x8 |
| 196 |
|
| 197 |
/* Likewise, each tile per level has 512 bytes of body */ |
| 198 |
#define FULL_BYTES_PER_TILE 0x200 |
| 199 |
|
| 200 |
static unsigned |
| 201 |
pan_hierarchy_size(unsigned width, unsigned height, unsigned mask, |
| 202 |
unsigned bytes_per_tile) |
| 203 |
{ |
| 204 |
unsigned size = PROLOGUE_SIZE; |
| 205 |
|
| 206 |
/* Iterate hierarchy levels */ |
| 207 |
u_foreach_bit(level, mask) { |
| 208 |
assert(level <= (MAX_TILE_SHIFT - MIN_TILE_SHIFT) && |
| 209 |
"invalid hierarchy mask"); |
| 210 |
|
| 211 |
/* Levels are power-of-two sizes */ |
| 212 |
unsigned tile_size = MIN_TILE_SIZE << level; |
| 213 |
|
| 214 |
size += DIV_ROUND_UP(width, tile_size) * DIV_ROUND_UP(height, tile_size) * |
| 215 |
bytes_per_tile; |
| 216 |
} |
| 217 |
|
| 218 |
/* This size will be used as an offset, so ensure it's aligned */ |
| 219 |
return ALIGN_POT(size, 0x200); |
| 220 |
} |
| 221 |
|
| 222 |
/* Implement the formula: |
| 223 |
* |
| 224 |
* 0x200 + bytes_per_tile * ceil(W / w) * ceil(H / h) |
| 225 |
* |
| 226 |
* rounding down the answer to the nearest 0x200. This is used to compute both |
| 227 |
* header and body sizes for GPUs without hierarchical tiling. Essentially, |
| 228 |
* computing a single hierarchy level, since there isn't any hierarchy! |
| 229 |
*/ |
| 230 |
|
| 231 |
static unsigned |
| 232 |
pan_flat_size(unsigned width, unsigned height, unsigned dim, |
| 233 |
unsigned bytes_per_tile) |
| 234 |
{ |
| 235 |
/* First, extract the tile dimensions */ |
| 236 |
unsigned tw = (1 << (dim & 0b111)) * 8; |
| 237 |
unsigned th = (1 << ((dim & (0b111 << 6)) >> 6)) * 8; |
| 238 |
|
| 239 |
/* Calculate the raw size */ |
| 240 |
unsigned raw = |
| 241 |
DIV_ROUND_UP(width, tw) * DIV_ROUND_UP(height, th) * bytes_per_tile; |
| 242 |
|
| 243 |
/* Round down and add offset */ |
| 244 |
return 0x200 + ((raw / 0x200) * 0x200); |
| 245 |
} |
| 246 |
|
| 247 |
/* Given a hierarchy mask and a framebuffer size, compute the header size */ |
| 248 |
|
| 249 |
unsigned |
| 250 |
pan_tiler_header_size(unsigned width, unsigned height, unsigned mask, |
| 251 |
bool hierarchy) |
| 252 |
{ |
| 253 |
if (hierarchy) |
| 254 |
return pan_hierarchy_size(width, height, mask, HEADER_BYTES_PER_TILE); |
| 255 |
else |
| 256 |
return pan_flat_size(width, height, mask, HEADER_BYTES_PER_TILE); |
| 257 |
} |
| 258 |
|
| 259 |
/* The combined header/body is sized similarly (but it is significantly |
| 260 |
* larger), except that it can be empty when the tiler disabled, rather than |
| 261 |
* getting clamped to a minimum size. |
| 262 |
*/ |
| 263 |
|
| 264 |
unsigned |
| 265 |
pan_tiler_full_size(unsigned width, unsigned height, unsigned mask, |
| 266 |
bool hierarchy) |
| 267 |
{ |
| 268 |
if (hierarchy) |
| 269 |
return pan_hierarchy_size(width, height, mask, FULL_BYTES_PER_TILE); |
| 270 |
else |
| 271 |
return pan_flat_size(width, height, mask, FULL_BYTES_PER_TILE); |
| 272 |
} |
| 273 |
|
| 274 |
/* On GPUs without hierarchical tiling, we choose a tile size directly and |
| 275 |
* stuff it into the field otherwise known as hierarchy mask (not a mask). */ |
| 276 |
|
| 277 |
static unsigned |
| 278 |
pan_choose_tile_size(unsigned width, unsigned height, unsigned vertex_count) |
| 279 |
{ |
| 280 |
/* Figure out the ideal tile size. Eventually a heuristic should be |
| 281 |
* used for this */ |
| 282 |
|
| 283 |
unsigned best_w = 16; |
| 284 |
unsigned best_h = 16; |
| 285 |
|
| 286 |
/* Clamp so there are less than 64 tiles in each direction */ |
| 287 |
|
| 288 |
best_w = MAX2(best_w, util_next_power_of_two(width / 63)); |
| 289 |
best_h = MAX2(best_h, util_next_power_of_two(height / 63)); |
| 290 |
|
| 291 |
/* We have our ideal tile size, so encode */ |
| 292 |
|
| 293 |
unsigned exp_w = util_logbase2(best_w / 16); |
| 294 |
unsigned exp_h = util_logbase2(best_h / 16); |
| 295 |
|
| 296 |
return exp_w | (exp_h << 6); |
| 297 |
} |
| 298 |
|
| 299 |
unsigned |
| 300 |
pan_choose_hierarchy_mask(unsigned width, unsigned height, |
| 301 |
unsigned vertex_count, bool hierarchy) |
| 302 |
{ |
| 303 |
/* If there is no geometry, we don't bother enabling anything */ |
| 304 |
|
| 305 |
if (!vertex_count) |
| 306 |
return 0x00; |
| 307 |
|
| 308 |
if (!hierarchy) |
| 309 |
return pan_choose_tile_size(width, height, vertex_count); |
| 310 |
|
| 311 |
/* Heuristic: choose the largest minimum bin size such that there are an |
| 312 |
* average of k vertices per bin at the lowest level. This is modeled as: |
| 313 |
* |
| 314 |
* k = vertex_count / ((fb width / bin width) * (fb height / bin height)) |
| 315 |
* |
| 316 |
* Bins are square, so solving for bin size = bin width = bin height: |
| 317 |
* |
| 318 |
* bin size = sqrt(((k) (fb width) (fb height) / vertex count)) |
| 319 |
* |
| 320 |
* k = 4 represents each bin as a QUAD. If the screen is completely tiled |
| 321 |
* into nonoverlapping uniform power-of-two squares, then this heuristic sets |
| 322 |
* the bin size to the quad size, which seems like an ok choice. |
| 323 |
*/ |
| 324 |
unsigned k = 4; |
| 325 |
unsigned log2_min_bin_size = |
| 326 |
util_logbase2_ceil((k * width * height) / vertex_count) / 2; |
| 327 |
|
| 328 |
/* Do not use bins larger than the framebuffer. They will be empty. */ |
| 329 |
unsigned log2_max_bin_size = util_logbase2_ceil(MAX2(width, height)); |
| 330 |
|
| 331 |
/* For small framebuffers, use one big tile */ |
| 332 |
log2_min_bin_size = MIN2(log2_min_bin_size, log2_max_bin_size); |
| 333 |
|
| 334 |
/* Clamp to valid bin sizes */ |
| 335 |
log2_min_bin_size = CLAMP(log2_min_bin_size, MIN_TILE_SHIFT, MAX_TILE_SHIFT); |
| 336 |
log2_max_bin_size = CLAMP(log2_max_bin_size, MIN_TILE_SHIFT, MAX_TILE_SHIFT); |
| 337 |
|
| 338 |
/* Bin indices are numbered from 0 started with MIN_TILE_SIZE */ |
| 339 |
unsigned min_bin_index = log2_min_bin_size - MIN_TILE_SHIFT; |
| 340 |
unsigned max_bin_index = log2_max_bin_size - MIN_TILE_SHIFT; |
| 341 |
|
| 342 |
/* Enable up to 8 bins starting from the heuristic selected minimum. 8 |
| 343 |
* is the implementation specific maximum in supported Midgard devices. |
| 344 |
*/ |
| 345 |
unsigned mask = |
| 346 |
(BITFIELD_MASK(8) << min_bin_index) & BITFIELD_MASK(max_bin_index + 1); |
| 347 |
|
| 348 |
assert(mask != 0 && "too few levels"); |
| 349 |
assert(util_bitcount(mask) <= 8 && "too many levels"); |
| 350 |
|
| 351 |
return mask; |
| 352 |
} |