Debian Codegraph @

docs/drivers/vc4.rst

raw
1 VC4
2 ===
3
4 Mesa's VC4 graphics driver supports multiple implementations of
5 Broadcom's VideoCore IV GPU. It is notably used in the Raspberry Pi 0
6 through Raspberry Pi 3 hardware, and the driver is included as an
7 option as of the 2016-02-09 Raspbian release using ``raspi-config``.
8 On most other distributions such as Debian or Fedora, you need no
9 configuration to enable the driver.
10
11 This Mesa driver talks directly to the `VC4
12 <https://www.kernel.org/doc/html/latest/gpu/vc4.html>`__ kernel DRM
13 driver for scheduling graphics commands, and that module also provides
14 KMS display support. The driver makes no use of the closed source VPU
15 firmware on the VideoCore IV block, instead talking directly to the
16 GPU block from Linux.
17
18 GLES2 support
19 -------------
20
21 The VC4 driver is a nearly conformant GLES2 driver, and the hardware
22 has achieved GLES2 conformance with other driver stacks.
23
24 OpenGL support
25 --------------
26
27 Along with GLES 2.0, the Mesa driver also exposes OpenGL 2.1, which is
28 mostly correct but with a few caveats.
29
30 * 4-byte index buffers.
31
32 GLES2.0, and VC4, don't have ``GL_UNSIGNED_INT`` index buffers. To support
33 them in VC4, we create a shadow copy of your index buffer with the
34 indices truncated to 2 bytes. This is incorrect (and will assertion
35 fail in debug builds of Mesa) if any of the indices were >65535. To
36 fix that, we would need to detect this case and rewrite the index
37 buffer and vertex buffers to do a series of draws each with small
38 indices and new vertex attrib bindings.
39
40 To avoid this problem, ensure that all index buffers are written using
41 ``GL_UNSIGNED_SHORT``, even at the cost of doing multiple draw calls
42 with updated vertex attrib bindings.
43
44 * Occlusion queries
45
46 The VC4 hardware has no support for occlusion queries. GL 2.0
47 requires that you support the occlusion queries extension, but you can
48 report 0 from ``glGetQueryiv(GL_SAMPLES_PASSED,
49 GL_QUERY_COUNTER_BITS)``. This is absurd, but it's how OpenGL handles
50 "we want the functions to be present everywhere, but we want it to be
51 optional for hardware to support it. Sadly, gallium doesn't yet allow
52 the driver to report 0 query bits.
53
54 * Primitive mode
55
56 VC4 doesn't support reducing triangles/quads/polygons to lines and
57 points like desktop GL. If front/back mode matched, we could rewrite
58 the index buffer to the new primitive type, but we don't. If
59 front/back mode don't match, we would need to run the vertex shader in
60 software, classify the prims, write new index buffers, and emit
61 (possibly many) new draw calls to rasterize the new prims in the same
62 order.
63
64 Bug Reporting
65 -------------
66
67 VC4 rendering bugs should go to Mesa's GitLab `issues
68 <https://gitlab.freedesktop.org/mesa/mesa/-/issues>`__ page.
69
70 By far the easiest way to communicate bug reports for rendering
71 problems is to take an apitrace. This passes exactly the drawing you
72 saw to the developer, without the developer needing to download and
73 build the application and replicate whatever steps you took to produce
74 the problem. Traces attached to bug reports should ideally be small.
75
76 For GPU hangs, if you can get a short apitrace that produces the
77 problem, that's still the best. If the problem takes a long time to
78 reproduce or you can't capture it in a trace, describing how to
79 reproduce and including a GPU hang dump would be the most
80 useful. Install `vc4-gpu-tools
81 <https://github.com/anholt/vc4-gpu-tools/>`__ and use
82 ``vc4_dump_hang_state my-app.hang``. Sometimes the hang file will
83 provide useful information.
84
85 Tiled Rendering
86 ---------------
87
88 VC4 is a tiled renderer, chopping the screen into 64x64 (non-MSAA) or
89 32x32 (MSAA) tiles and rendering the scene per tile. Rasterization
90 looks like::
91
92 (CPU) Allocate space to store a list of draw commands per tile
93 (CPU) Set up a command list per tile that does:
94 Either load the current tile's color buffer from memory, or clear it.
95 Either load the current tile's depth buffer from memory, or clear it.
96 Branch into the draw list for the tile
97 Store the depth buffer if anybody might read it.
98 Store the color buffer if anybody might read it.
99 (GPU) Initialize the per-tile draw call lists to empty.
100 (GPU) Run all draw calls collecting vertex data
101 (GPU) For each tile covered by a draw call's primitive.
102 Emit state packets to the list to update it to the current draw call's state.
103 Emit a primitive description into the tile's draw call list.
104
105 Tiled rendering avoids the need for large render target caches, at the
106 expense of increasing the cost of vertex processing. Unlike some tiled
107 renderers, VC4 has no non-tiled rendering mode.
108
109 Performance Tricks
110 ------------------
111
112 * Reducing memory bandwidth by clearing.
113
114 Even if your drawing is going to cover the entire render target, it's
115 more efficient for VC4 if you emit a ``glClear()`` of the color and
116 depth buffers. This means we can skip the load of the previous state
117 from memory, in favor of a cheap GPU-side ``memset()`` of the tile
118 buffer before we start running the draw calls.
119
120 * Reducing memory bandwidth with scissoring.
121
122 If all draw calls for the frame are with a ``glScissor()`` to only
123 part of the screen, then we can skip setting up the tiles for that
124 area, which means a little less memory used setting up the empty bins,
125 and a lot less memory used loading/storing the unchanged tiles.
126
127 * Reducing memory bandwidth with ``glInvalidateFramebuffer()``.
128
129 If we don't know who might use the contents of the framebuffer's depth
130 or color in the future, then we have to store it for later. If you use
131 glInvalidateFramebuffer() before accessing the results of your
132 rendering, then we can skip the store of the depth or color
133 buffer. Note that this is unimplemented.
134
135 * Avoid non-constant GLSL array indexing
136
137 In VC4 the only non-constant-index array access supported in hardware
138 is uniforms. For everything else (inputs, outputs, temporaries), we
139 have to lower them to an IF ladder like::
140
141 if (index == 0)
142 return array[0]
143 else if (index == 1)
144 return array[1]
145 ...
146
147 This is very expensive as we probably have to execute every branch of
148 every IF statement due to it being a SIMD machine. So, it is
149 recommended (if you can) to avoid non-uniform non-constant array
150 indexing.
151
152 Note that if you do variable indexing within a bounded loop that Mesa
153 can unroll, that can actually count as constant indexing.
154
155 * Increasing GPU memory Increase CMA pool size
156
157 The memory for the VC4 driver is allocated from the standard Linux CMA
158 pool. The size of this pool defaults to 64 MB. To increase this, pass
159 an additional parameter on the kernel command line. Edit the boot
160 partition's ``cmdline.txt`` to add::
161
162 cma=256M@256M
163
164 ``cmdline.txt`` is a single line with whitespace separated parameters.
165
166 The first value is the size of the pool and the second parameter is
167 the start address of the pool. The pool size can be increased further,
168 but it must fit into the memory, so size + start address must be below
169 1024M (Pi 2, 3, 3+) or 512M (Pi B, B+, Zero, Zero W). Also this
170 reduces the memory available to Linux.
171
172 * Decrease firmware memory
173
174 The firmware allocates a fixed chunk of memory before booting
175 Linux. If firmware functions are not required, this amount can be
176 reduced.
177
178 In ``config.txt`` edit ``gpu_mem`` to 16, if you do not need video decoding,
179 edit gpu_mem to 64 if you need video decoding.
180
181 Performance debugging
182 ---------------------
183
184 * Step 1: Known issues
185
186 The first tool to look at is running your application with the
187 environment variable ``VC4_DEBUG=perf`` set. This will report debug
188 information for many known causes of performance problems on the
189 console. Not all of them will cause visible performance improvements
190 when fixed, but it's a good first step to see what might going wrong.
191
192 * Step 2: CPU vs GPU
193
194 The primary question is figuring out whether the CPU is busy in your
195 application, the CPU is busy in the GL driver, the GPU is waiting for
196 the CPU, or the CPU is waiting for the GPU. Ideally, you get to the
197 point where the CPU is waiting for the GPU infrequently but for a
198 significant amount of time (however long it takes the GPU to draw a
199 frame).
200
201 Start with top while your application is running. Is the CPU usage
202 around 90%+? If so, then our performance analysis will be with
203 sysprof. If it's not very high, is the GPU staying busy? We don't have
204 a clean tool for this yet, but ``cat /debug/dri/0/v3d_regs`` could be
205 useful. If ``CT0CA`` != ``CT0EA`` or ``CT1CA`` != ``CT1EA``, that
206 means that the GPU is currently busy processing some rendering job.
207
208 * sysprof for CPU usage
209
210 If the CPU is totally busy and the GPU isn't terribly busy, there is
211 an excellent tool for debugging: sysprof. Install, run as root (so you
212 can get system-wide profiling), hit play and later stop. The top-left
213 area shows the flat profile sorted by total time of that symbol plus
214 its descendants. The top few are generally uninteresting (main() and
215 its descendants consuming a lot), but eventually you can get down to
216 something interesting. Click it, and to the right you get the
217 callchains to descendants -- where all that time actually went. On the
218 other hand, the lower left shows callers -- double-clicking those
219 selects that as the symbol to view, instead.
220
221 Note that you need debug symbols for the callgraphs in sysprof to
222 work, which is where most of its value is. Most distributions offer
223 debug symbol packages from their builds which can be installed
224 separately, and sysprof will find them. I've found that on arm, the
225 debug packages are not enough, and if someone could determine what is
226 necessary for callgraphs in debugging, that would be really helpful.
227
228 * perf for CPU waits on GPU
229
230 If the CPU is not very busy and the GPU is not very busy, then we're
231 probably ping-ponging between the two. Most cases of this would be
232 noticed by ``VC4_DEBUG=perf``, but not all. To see all cases where
233 this happens, use the perf tool from the Linux kernel (note: unrelated
234 to ``VC4_DEBUG=perf``)::
235
236 sudo perf record -f -g -e vc4:vc4_wait_for_seqno_begin -c 1 openarena
237
238 If you want to see the whole system's stalls for a period of time
239 (very useful!), use the -a flag instead of a particular command
240 name. Just ``^C`` when you're done capturing data.
241
242 At exit, you'll have ``perf.data`` in the current directory. You can print
243 out the results with::
244
245 perf report | less
246
247 * Debugging for GPU fully busy
248
249 As of Linux kernel 4.17 and Mesa 18.1, we now expose the hardware's
250 performance counters in OpenGL. Install apitrace, and trace your
251 application with::
252
253 apitrace trace <application> # for GLX applications
254 apitrace trace -a egl <application> # for EGL applications
255
256 Once you've captured a trace, you can see what counters are available
257 and replay it while looking while looking at some of those counters::
258
259 apitrace replay <application>.trace --list-metrics
260
261 apitrace replay <application>.trace --pdraw=GL_AMD_performance_monitor:QPU-total-clk-cycles-vertex-coord-shading
262
263 Multiple counters can be captured at once with commas separating them.
264
265 Once you've found what draw calls are surprisingly expensive in one of
266 the counters, you can work out which ones they were at the GL level by
267 opening the trace up in qapitrace and using ``^-G`` to jump to that call
268 number and ``^-L`` to look up the GL state at that call.
269
270 Trace Testing
271 -------------
272
273 shader-db is often used as a proxy for real-world app performance when
274 working on the compiler in Mesa. On VC4, there is a lot of
275 state-dependent code in the shaders (like blending or vertex attribute
276 format handling), so the typical `shader-db
277 <https://gitlab.freedesktop.org/mesa/shader-db>`__ will miss important
278 areas for optimization. Piglit can instead test apitraces, such as
279 those captured in
280 `traces-db <https://gitlab.freedesktop.org/gfx-ci/tracie/traces-db/>`__.
281
282 Hardware Documentation
283 ----------------------
284
285 For driver developers, Broadcom publicly released a `specification
286 <https://docs.broadcom.com/doc/12358545>`__ PDF for the 21553, which
287 is closely related to the VC4 GPU present in the Raspberry Pi. They
288 also released a `snapshot <https://docs.broadcom.com/docs/12358546>`__
289 of a corresponding Android graphics driver. That graphics driver was
290 ported to Raspbian for a demo, but was not expected to have ongoing
291 development.
292
293 Developers with NDA access with Broadcom or Raspberry Pi can
294 potentially get access to "simpenrose", the C software simulator of
295 the GPU. The Mesa driver includes a backend (``vc4_simulator.c``) to
296 use simpenrose from an x86 system with the i915 graphics driver with
297 all of the VC4 rendering commands emulated on simpenrose and memcpyed
298 to the real GPU.