| 1 |
Fragment Density Map |
| 2 |
==================== |
| 3 |
|
| 4 |
``VK_EXT_fragment_density_map`` is an extension which is intended to allow |
| 5 |
users to render parts of the screen at a lower resolution. It is designed to be |
| 6 |
implemented on tiled rendering GPU architectures such as Adreno, and the |
| 7 |
intention is that it is implemented by rendering some of the tiles at a lower |
| 8 |
resolution and scaling them up when resolving to system memory or when sampling |
| 9 |
the resulting image. This inherently means that it is "all or nothing," that |
| 10 |
is, it must be enabled or disabled for the entire render pass. While the idea is |
| 11 |
simple, the implementation in turnip is very subtle with lots of |
| 12 |
interactions with various different features. This page attempts to document |
| 13 |
the main principles behind the implementation. |
| 14 |
|
| 15 |
Coordinate Space Soup |
| 16 |
--------------------- |
| 17 |
|
| 18 |
In order to render a tile at lower resolution, we have to override the user's |
| 19 |
viewport and scissor for each tile depending on the scaling factor provided by |
| 20 |
the user. This becomes complicated fast, so let's start by defining a few |
| 21 |
coordinate spaces that we'll have to work with. |
| 22 |
|
| 23 |
Framebuffer space |
| 24 |
^^^^^^^^^^^^^^^^^ |
| 25 |
|
| 26 |
This is the space of the final rendered image. From the user's perspective |
| 27 |
everything is specified in this space, and fragments created by the rasterizer |
| 28 |
appear to be larger than 1 pixel. But this is not what actually happens in the |
| 29 |
hardware, it is a fiction created by the driver. The other spaces below are |
| 30 |
what the hardware actually "sees". |
| 31 |
|
| 32 |
GMEM Space |
| 33 |
^^^^^^^^^^ |
| 34 |
|
| 35 |
This space exists whenever tiled rendering/GMEM is used, even without FDM. It |
| 36 |
is the space used to access GMEM, with the origin at the upper left of the |
| 37 |
tile. The hardware automatically transforms rendering space into GMEM space |
| 38 |
whenever GMEM is accessed using the various ``*_WINDOW_OFFSET`` registers. The |
| 39 |
origin of this space will be called :math:`b_{cs}`, the common bin start, for |
| 40 |
reasons that are explained below. When using FDM, coordinates in this space |
| 41 |
must be multiplied by the scaling factor :math:`s` derived from the fragment |
| 42 |
density map, or equivalently divided by the fragment area (as defined by the |
| 43 |
Vulkan specification), with the origin still at the upper left of the tile. For |
| 44 |
example, if :math:`s_x = 1/2`, then the bin is half as wide as it would've been |
| 45 |
without FDM and all coordinates in this space must be divided by 2. |
| 46 |
|
| 47 |
Rendering space |
| 48 |
^^^^^^^^^^^^^^^ |
| 49 |
|
| 50 |
This is the space in which the hardware rasterizer operates and produces |
| 51 |
fragments. Normally this is the same as framebuffer space, but with FDM it is |
| 52 |
not. We transform the viewport and scissor from framebuffer space to |
| 53 |
rendering space by patching them per-tile in the driver and then when we |
| 54 |
resolve the tile we scale the resulting tile back to the correct resolution by |
| 55 |
blitting from the rendering space source to the framebuffer space destination. |
| 56 |
|
| 57 |
In order to come up with the correct transform from framebuffer space to |
| 58 |
rendering space, it has to shrink the coordinates by :math:`s` while |
| 59 |
mapping the original bin start in framebuffer space :math:`b_s` to |
| 60 |
:math:`b_{cs}`. Since :math:`b_{cs}` is entirely defined by the driver when |
| 61 |
programming ``*_WINDOW_OFFSET``, one tempting way to do this is to just |
| 62 |
multiply by :math:`s` and define :math:`b_{cs} = b_s * s`. It turns out, |
| 63 |
however, that this doesn't work. A key requirement is to handle cases where the |
| 64 |
same scene is rendered in multiple different views at the same time using |
| 65 |
``VK_KHR_multiview``, as in VR use-cases, and in this case we want :math:`s` to |
| 66 |
vary per view, but :math:`b_{cs}` is always the same for every view because |
| 67 |
there is only one ``*_WINDOW_OFFSET`` register for all layers (hence the name). |
| 68 |
|
| 69 |
We follow the blob by leaving :math:`b_{cs}` the same regardless of whether FDM |
| 70 |
is enabled or not. This means that normally :math:`b_s = b_{cs}`, although this |
| 71 |
is not the case if ``VK_EXT_fragment_density_map_offset`` is in use and the |
| 72 |
bins are shifted per-view. Since the coordinates need to be scaled by :math:`s`, |
| 73 |
we know that the transform needs to look like :math:`x' = s * x + o`, where |
| 74 |
only the offset :math:`o` is free. Plugging in the constraint that :math:`b_s` |
| 75 |
maps to :math:`b_{cs}`, we get that :math:`b_{cs} = s * b_s + o` or |
| 76 |
:math:`o = b_{cs} - s * b_s`. This is the function computed by |
| 77 |
``tu_fdm_per_bin_offset`` and used to calculate the transform for the viewport, |
| 78 |
scissor, and ``gl_FragCoord``. One critical thing is that the offset must be an |
| 79 |
integer, or in other words the framebuffer space bin start :math:`b_s` must be |
| 80 |
a multiple of :math:`1 / s`. This is a natural constraint anyway, because if |
| 81 |
it wasn't the case then the bin would start in the middle of a fragment which |
| 82 |
isn't possible to handle correctly. |
| 83 |
|
| 84 |
Viewport and Scissor Patching |
| 85 |
----------------------------- |
| 86 |
|
| 87 |
In order to have :math:`s` differ per view, we have to be able to override the |
| 88 |
viewport per view. That is, we need to transform the viewport for each view |
| 89 |
differently. If there is only one viewport, then we duplicate the user's |
| 90 |
viewport for each view and transform it using the :math:`b_s` and :math:`s` for |
| 91 |
that view, and we set a "per-view viewport" bit to select the viewport per view |
| 92 |
instead of using the default viewport 0. When |
| 93 |
``VK_VALVE_fragment_density_map_layered`` is in use, we instead have to insert |
| 94 |
shader code to achieve the same thing. |
| 95 |
|
| 96 |
If the user specifies multiple viewports but they are per-view because |
| 97 |
``VK_QCOM_multiview_per_view_viewport`` is enabled, then we can just set the |
| 98 |
per-view viewport bit and transform each user viewport individually by the |
| 99 |
corresponding scale. But if the user explicitly writes ``gl_ViewportIndex``, |
| 100 |
then there is nothing we can do and we have to make :math:`s` the same for all |
| 101 |
views by conservatively taking the minimum. Then we apply :math:`s` to all of |
| 102 |
the user-specified viewports. |
| 103 |
|
| 104 |
Because the bin size is now per-view, the usual mechanism of |
| 105 |
``*_WINDOW_SCISSOR`` for clipping fragments outside the bin doesn't work. |
| 106 |
Instead the driver needs to intersect the transformed user-specified scissor |
| 107 |
with the transformed rendering-space bin coordinates, effectively replacing |
| 108 |
``*_WINDOW_SCISSOR``. |
| 109 |
|
| 110 |
Fragment density map offset |
| 111 |
--------------------------- |
| 112 |
|
| 113 |
In order to "properly" implement ``VK_EXT_fragment_density_map_offset``, we |
| 114 |
need to add an extra row/column of bins at the end and then shift the binning |
| 115 |
grid up and to the left by an offset :math:`b_o`. This offset is based on the |
| 116 |
user's offset but has the opposite sign, i.e. when shifting the FDM to the left |
| 117 |
we have to shift the binning grid to the right, and once the user's offset |
| 118 |
becomes large enough then we "wrap around" and shift over the scaling factor |
| 119 |
:math:`s` to the next bin. This has to happen per-view. In turnip the function |
| 120 |
that computes :math:`b_o` is called ``tu_bin_offset``. Each tile then gets an |
| 121 |
offseted start :math:`b_s = b_{cs} - b_o` except for the first row/column which |
| 122 |
only shrink in height/width respectively. |
| 123 |
|
| 124 |
If we cannot make :math:`s` per-view, then we also cannot make :math:`b_s` |
| 125 |
per-view and so we cannot shift the bins over. Therefore we fall back to only |
| 126 |
shifting where :math:`s` is sampled from, which produces jittery and jarring |
| 127 |
transitions when a bin suddenly changes resolution. |
| 128 |
|
| 129 |
Bin merging |
| 130 |
----------- |
| 131 |
|
| 132 |
FDM shrinks the size of the bin in GMEM, which results in a lot of wasteful |
| 133 |
unused extra space in GMEM. a7xx mitigates this by introducing "bin merging". |
| 134 |
If two tiles next to each other have the same scaling for each view, then we |
| 135 |
combine them into one tile, as long as the combined size in rendering space |
| 136 |
isn't larger than the original size of an unscaled bin in framebuffer space. We |
| 137 |
can even merge larger groups of tiles. The only hardware feature needed for |
| 138 |
this to work is the ability to merge the visibility streams for the tiles, |
| 139 |
which was added on a7xx by a new bitmask in ``CP_SET_BIN_DATA5`` and variants. |
| 140 |
Only bins within the same visibility stream/VSC pipe can be merged. |
| 141 |
|
| 142 |
Hardware scaling registers and LRZ |
| 143 |
---------------------------------- |
| 144 |
|
| 145 |
One disadvantage of FDM on a6xx is that low-resolution tiles cannot use |
| 146 |
LRZ, because the LRZ hardware is not aware of the transform between framebuffer |
| 147 |
space and rendering space and applies the framebuffer-space LRZ values to the |
| 148 |
rendering-space fragments. In order to fix this, a740 adds new offset and scale |
| 149 |
registers. The offset :math:`o'` is applied to fragment coordinates during |
| 150 |
rasterization *after* LRZ, so that viewport, scissor, and LRZ are in a |
| 151 |
new "LRZ space" while the other operations (resolves and unresolves, and |
| 152 |
attachment writes) still happen in the rendering space which is now offset. |
| 153 |
:math:`o'` is specified for each layer. The scale :math:`s` is the same as |
| 154 |
before, and it is used to multiply the fragment area covered by each LRZ pixel. |
| 155 |
|
| 156 |
Without ``VK_EXT_fragment_density_map_offset``, we can simply make LRZ space |
| 157 |
equal to framebuffer space scaled down by :math:`s`. That is, we can set |
| 158 |
:math:`o'` to what :math:`o` was before and then set :math:`o` to 0, only |
| 159 |
scaling down the viewport but not shifting it and letting the hardware handle |
| 160 |
the shift. Then LRZ pixels will be scaled up appropriately and everything will |
| 161 |
work. However, this doesn't work if there is a bin offset :math:`b_o`. In order |
| 162 |
to make binning work, we shift the viewport and scissor by :math:`b_o` when |
| 163 |
binning. Unfortunately the offset registers do not have any effect when |
| 164 |
binning, so rendering space and LRZ space have to be the same when binning, and |
| 165 |
the visibility stream is generated from rendering space. This means that LRZ |
| 166 |
space also has to be shifted over compared to framebuffer space, and the LRZ |
| 167 |
buffer must be overallocated when FDM offset might be used with it (which is |
| 168 |
signalled by ``VK_IMAGE_CREATE_FRAGMENT_DENSITY_MAP_OFFSET_BIT_EXT``) because |
| 169 |
the LRZ image will be shifted by :math:`b_o`. |
| 170 |
|
| 171 |
In order for LRZ to work, LRZ space when rendering must be equal to LRZ space |
| 172 |
when binning scaled down by :math:`s`. The origin of LRZ space when binning is |
| 173 |
:math:`-b_o`, and this must be mapped to 0. The transform from |
| 174 |
framebuffer space to LRZ space is :math:`x' = x * s + o`, and the transform |
| 175 |
from framebuffer space to rendering space is :math:`x'' = x * s + o + o'`. |
| 176 |
We get that :math:`o + o' = b_{cs} - b_s * s`, similar to before, and |
| 177 |
:math:`0 = -b_o * s + o` so that :math:`o = b_o * s` and finally |
| 178 |
:math:`o' = b_{cs} - b_s * s - b_o * s`, or after rearranging |
| 179 |
:math:`o' = b_{cs} - (b_s + b_o) * s`. For all tiles except those in the first |
| 180 |
row or column, this simplifies to :math:`o' = b_{cs} - b_{cs} * s` because |
| 181 |
:math:`b_{cs} = b_s + b_o`. For tiles in the first row or column, :math:`b_s` |
| 182 |
and :math:`b_{cs}` are both 0 in one of the coordinates, so it becomes |
| 183 |
:math:`o' = -b_o * s` in that coordinate. This isn't representable in hardware, |
| 184 |
both because it is negative (which can be worked around by artifically |
| 185 |
shifting :math:`b_{cs}`) but more importantly because it may not meet the |
| 186 |
alignment requirements for the hardware register (which is currently 8 pixels). |
| 187 |
We have to just disable LRZ in this case. |