Debian Codegraph @

docs/drivers/freedreno/hw/lrz.rst

raw
1 Low Resolution Z Buffer
2 =======================
3
4 This doc is based on a6xx HW reverse engineering, a5xx should be similar to
5 a6xx before gen3.
6
7 Low Resolution Z buffer is very similar to a depth prepass that helps
8 the HW to avoid executing the fragment shader on those fragments that will
9 be subsequently discarded by the depth test afterwards.
10
11 The interesting part of this feature is that it allows applications
12 to submit the vertices in any order.
13
14 Citing official Adreno documentation:
15
16 ::
17
18 [A Low Resolution Z (LRZ)] pass is also referred to as draw order independent
19 depth rejection. During the binning pass, a low resolution Z-buffer is constructed,
20 and can reject LRZ-tile wide contributions to boost binning performance. This LRZ
21 is then used during the rendering pass to reject pixels efficiently before testing
22 against the full resolution Z-buffer.
23
24 Limitations
25 -----------
26
27 There are two main limitations of LRZ:
28
29 - Since LRZ is an early depth test, such test cannot be used when late-z is required;
30 - LRZ buffer could be formed only in one direction, changing depth comparison directions
31 without disabling LRZ would lead to a malformed LRZ buffer.
32
33 Pre-a650 (before gen3)
34 ----------------------
35
36 The direction is fully tracked on CPU. In render pass LRZ starts with
37 unknown direction, the direction is set first time when depth write occurs
38 and if it does change afterwards then the direction becomes invalid and LRZ is
39 disabled for the rest of the render pass.
40
41 Since the direction is not tracked by the GPU, it's impossible to know whether
42 LRZ is enabled during construction of secondary command buffers.
43
44 For the same reason, it's impossible to reuse LRZ between render passes.
45
46 A650+ (gen3+)
47 -------------
48
49 Now LRZ direction can be tracked on GPU. There are two parts:
50
51 - Direction byte which stores current LRZ direction - ``GRAS_LRZ_CNTL.DIR``.
52 - Parameters of the last used depth view - ``GRAS_LRZ_DEPTH_VIEW``.
53
54 The idea is the same as when LRZ tracked on CPU: when ``GRAS_LRZ_CNTL``
55 is used, its direction is compared to the previously known direction
56 and direction byte is set to disabled when directions are incompatible.
57
58 Additionally, to reuse LRZ between render passes, ``GRAS_LRZ_CNTL`` checks
59 if the current value of ``GRAS_LRZ_DEPTH_VIEW`` is equal to the value
60 stored in the buffer. If not, LRZ is disabled. This is necessary
61 because depth buffer may have several layers and mip levels, while the
62 LRZ buffer represents only a single layer + mip level.
63
64 A7XX
65 -------------
66
67 A7XX introduces the concept of bidirectional LRZ where there are two LRZ
68 buffers, one for each direction. This way LRZ doesn't need to be disabled
69 when the direction changes, by default, this behavior is disabled but the
70 LRZ buffers have to be allocated with this space in mind as fast clears
71 will always write metadata for both.
72
73 Additionally, there are now two seperate LRZ buffers (on top of one for
74 each direction, a total of four) - due to concurrent binning, one can be
75 used for binning and the other for rendering concurrently. These can be
76 flipped between via the `LRZ_FLIP_BUFFER` event which can be put inside
77 a conditional block for either the BV or BR.
78
79 LRZ Fast-Clear
80 --------------
81
82 The LRZ fast-clear buffer is initialized to zeroes and read/written
83 when ``GRAS_LRZ_CNTL.FC_ENABLE`` is set. It appears to store 1b/block.
84 ``0`` means block has original depth clear value, and ``1`` means that the
85 corresponding block in LRZ has been modified.
86
87 LRZ fast-clear conservatively clears LRZ buffer. At the point where LRZ is
88 written the LRZ block which corresponds to a single fast-clear bit is cleared:
89
90 - To ``0.0`` if depth comparison is ``GREATER``
91 - To ``1.0`` if depth comparison is ``LESS``
92
93 This way it's always valid to fast-clear.
94
95 On A7XX, the original depth clear value can be specified exactly allowing for
96 fast-clear to any value rather than just ``1.0`` or ``0.0``.
97
98 LRZ Feedback
99 -------------
100
101 Some draws do write depth but cannot contribute to LRZ during the BINNING pass
102 e.g. when fragment shader has "discard" in it, however they can contribute to LRZ
103 during the RENDERING pass via LRZ feedback mechanism. This may allow the draws
104 that follow to depth test against the updated LRZ, this is especially important
105 if such "bad" draws were at the start of the renderpass.
106
107 LRZ feedback happens during the RENDERING pass when ``LRZ_FEEDBACK_ZMODE_MASK``
108 is set, if draw has a6xx_ztest_mode that has corresponding flag set in
109 ``LRZ_FEEDBACK_ZMODE_MASK`` - its depth values would be used for feedback.
110
111 LRZ feedback alongside with LRZ testing also works during sysmem rendering.
112
113 LRZ Precision
114 -------------
115
116 LRZ always uses ``Z16_UNORM``. The epsilon for it is ``1.f / (1 << 16)`` which is
117 not enough to represent all values of ``Z32_UNORM`` or ``Z32_FLOAT``.
118 This especially raises questions in context of fast-clear, if fast-clear
119 uses a value which cannot be precisely represented by LRZ - we wouldn't
120 be able to round it in the correct direction since direction is tracked
121 on GPU.
122
123 However, it seems that depth comparisons with LRZ values have some "slack"
124 and nothing special should be done for such depth clear values.
125
126 How it was tested:
127
128 - Clear ``Z32_FLOAT`` attachment to ``1.f / (1 << 17)``
129
130 - LRZ buffer contains all zeroes.
131
132 - Do draws and check whether all samples are passing:
133
134 - ``OP_GREATER`` with ``(1.f / (1 << 17) + float32_epsilon)`` - passing;
135 - ``OP_GREATER`` with ``(1.f / (1 << 17) - float32_epsilon)`` - not passing;
136 - ``OP_LESS`` with ``(1.f / (1 << 17) - float32_epsilon)`` - samples;
137 - ``OP_LESS`` with ``(1.f / (1 << 17) + float32_epsilon)``- not passing;
138 - ``OP_LESS_OR_EQ`` with ``(1.f / (1 << 17) + float32_epsilon)`` - not passing.
139
140 In all cases resulting LRZ buffer is all zeroes and LRZ direction is updated.
141
142 LRZ Caches
143 ----------
144
145 ``LRZ_FLUSH`` flushes and invalidates LRZ caches, there are two caches:
146
147 - Cache for fast-clear buffer;
148 - Cache for direction byte + depth view params.
149
150 They could be cleared by ``LRZ_CLEAR``. To become visible in GPU memory
151 the caches should be flushed with ``LRZ_FLUSH`` afterwards.
152
153 ``GRAS_LRZ_CNTL`` reads from these caches.