1bf215546Sopenharmony_ciLLVMpipe
2bf215546Sopenharmony_ci========
3bf215546Sopenharmony_ci
4bf215546Sopenharmony_ciIntroduction
5bf215546Sopenharmony_ci------------
6bf215546Sopenharmony_ci
7bf215546Sopenharmony_ciThe Gallium llvmpipe driver is a software rasterizer that uses LLVM to
8bf215546Sopenharmony_cido runtime code generation. Shaders, point/line/triangle rasterization
9bf215546Sopenharmony_ciand vertex processing are implemented with LLVM IR which is translated
10bf215546Sopenharmony_cito x86, x86-64, or ppc64le machine code. Also, the driver is
11bf215546Sopenharmony_cimultithreaded to take advantage of multiple CPU cores (up to 8 at this
12bf215546Sopenharmony_citime). It's the fastest software rasterizer for Mesa.
13bf215546Sopenharmony_ci
14bf215546Sopenharmony_ciRequirements
15bf215546Sopenharmony_ci------------
16bf215546Sopenharmony_ci
17bf215546Sopenharmony_ci-  For x86 or amd64 processors, 64-bit mode is recommended. Support for
18bf215546Sopenharmony_ci   SSE2 is strongly encouraged. Support for SSE3 and SSE4.1 will yield
19bf215546Sopenharmony_ci   the most efficient code. The fewer features the CPU has the more
20bf215546Sopenharmony_ci   likely it is that you will run into underperforming, buggy, or
21bf215546Sopenharmony_ci   incomplete code.
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci   For ppc64le processors, use of the Altivec feature (the Vector
24bf215546Sopenharmony_ci   Facility) is recommended if supported; use of the VSX feature (the
25bf215546Sopenharmony_ci   Vector-Scalar Facility) is recommended if supported AND Mesa is built
26bf215546Sopenharmony_ci   with LLVM version 4.0 or later.
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci   See ``/proc/cpuinfo`` to know what your CPU supports.
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci-  Unless otherwise stated, LLVM version 3.9 or later is required.
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci   For Linux, on a recent Debian based distribution do:
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci   .. code-block:: console
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci      aptitude install llvm-dev
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   If you want development snapshot builds of LLVM for Debian and
39bf215546Sopenharmony_ci   derived distributions like Ubuntu, you can use the APT repository at
40bf215546Sopenharmony_ci   `apt.llvm.org <https://apt.llvm.org/>`__, which are maintained by
41bf215546Sopenharmony_ci   Debian's LLVM maintainer.
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   For a RPM-based distribution do:
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   .. code-block:: console
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci      yum install llvm-devel
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   For Windows you will need to build LLVM from source with MSVC or
50bf215546Sopenharmony_ci   MINGW (either natively or through cross compilers) and CMake, and set
51bf215546Sopenharmony_ci   the ``LLVM`` environment variable to the directory you installed it
52bf215546Sopenharmony_ci   to. LLVM will be statically linked, so when building on MSVC it needs
53bf215546Sopenharmony_ci   to be built with a matching CRT as Mesa, and you'll need to pass
54bf215546Sopenharmony_ci   ``-DLLVM_USE_CRT_xxx=yyy`` as described below.
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   +-----------------+----------------------------------------------------------------+
58bf215546Sopenharmony_ci   | LLVM build-type | Mesa build-type                                                |
59bf215546Sopenharmony_ci   |                 +--------------------------------+-------------------------------+
60bf215546Sopenharmony_ci   |                 | debug,checked                  | release,profile               |
61bf215546Sopenharmony_ci   +=================+================================+===============================+
62bf215546Sopenharmony_ci   | Debug           | ``-DLLVM_USE_CRT_DEBUG=MTd``   | ``-DLLVM_USE_CRT_DEBUG=MT``   |
63bf215546Sopenharmony_ci   +-----------------+--------------------------------+-------------------------------+
64bf215546Sopenharmony_ci   | Release         | ``-DLLVM_USE_CRT_RELEASE=MTd`` | ``-DLLVM_USE_CRT_RELEASE=MT`` |
65bf215546Sopenharmony_ci   +-----------------+--------------------------------+-------------------------------+
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   You can build only the x86 target by passing
68bf215546Sopenharmony_ci   ``-DLLVM_TARGETS_TO_BUILD=X86`` to cmake.
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ciBuilding
71bf215546Sopenharmony_ci--------
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ciTo build everything on Linux invoke meson as:
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci.. code-block:: console
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   mkdir build
78bf215546Sopenharmony_ci   cd build
79bf215546Sopenharmony_ci   meson -D glx=gallium-xlib -D gallium-drivers=swrast
80bf215546Sopenharmony_ci   ninja
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ciUsing
84bf215546Sopenharmony_ci-----
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ciLinux
87bf215546Sopenharmony_ci~~~~~
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ciOn Linux, building will create a drop-in alternative for ``libGL.so``
90bf215546Sopenharmony_ciinto
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci::
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   build/foo/gallium/targets/libgl-xlib/libGL.so
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_cior
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci::
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   lib/gallium/libGL.so
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ciTo use it set the ``LD_LIBRARY_PATH`` environment variable accordingly.
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ciWindows
105bf215546Sopenharmony_ci~~~~~~~
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ciOn Windows, building will create
108bf215546Sopenharmony_ci``build/windows-x86-debug/gallium/targets/libgl-gdi/opengl32.dll`` which
109bf215546Sopenharmony_ciis a drop-in alternative for system's ``opengl32.dll``, which will use
110bf215546Sopenharmony_cithe Mesa ICD, ``build/windows-x86-debug/gallium/targets/wgl/libgallium_wgl.dll``.
111bf215546Sopenharmony_ciTo use it put both dlls in the same directory as your application. It can also
112bf215546Sopenharmony_cibe used by replacing the native ICD driver, but it's quite an advanced usage, so if
113bf215546Sopenharmony_ciyou need to ask, don't even try it.
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ciThere is however an easy way to replace the OpenGL software renderer
116bf215546Sopenharmony_cithat comes with Microsoft Windows 7 (or later) with llvmpipe (that is,
117bf215546Sopenharmony_cion systems without any OpenGL drivers):
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci-  copy
120bf215546Sopenharmony_ci   ``build/windows-x86-debug/gallium/targets/wgl/libgallium_wgl.dll`` to
121bf215546Sopenharmony_ci   ``C:\Windows\SysWOW64\mesadrv.dll``
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci-  load this registry settings:
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   ::
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci      REGEDIT4
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci      ; https://technet.microsoft.com/en-us/library/cc749368.aspx
130bf215546Sopenharmony_ci      ; https://www.msfn.org/board/topic/143241-portable-windows-7-build-from-winpe-30/page-5#entry942596
131bf215546Sopenharmony_ci      [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL]
132bf215546Sopenharmony_ci      "DLL"="mesadrv.dll"
133bf215546Sopenharmony_ci      "DriverVersion"=dword:00000001
134bf215546Sopenharmony_ci      "Flags"=dword:00000001
135bf215546Sopenharmony_ci      "Version"=dword:00000002
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci-  Ditto for 64 bits drivers if you need them.
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ciProfiling
140bf215546Sopenharmony_ci---------
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ciLinux perf integration
143bf215546Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ciOn Linux, it is possible to have symbol resolution of JIT code with
146bf215546Sopenharmony_ci`Linux perf <https://perf.wiki.kernel.org/>`__:
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci::
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci   perf record -g /my/application
151bf215546Sopenharmony_ci   perf report
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ciWhen run inside Linux perf, llvmpipe will create a
154bf215546Sopenharmony_ci``/tmp/perf-XXXXX.map`` file with symbol address table. It also dumps
155bf215546Sopenharmony_ciassembly code to ``/tmp/perf-XXXXX.map.asm``, which can be used by the
156bf215546Sopenharmony_ci``bin/perf-annotate-jit.py`` script to produce disassembly of the
157bf215546Sopenharmony_cigenerated code annotated with the samples.
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ciYou can obtain a call graph via
160bf215546Sopenharmony_ci`Gprof2Dot <https://github.com/jrfonseca/gprof2dot#linux-perf>`__.
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ciUnit testing
163bf215546Sopenharmony_ci------------
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ciBuilding will also create several unit tests in
166bf215546Sopenharmony_ci``build/linux-???-debug/gallium/drivers/llvmpipe``:
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci-  ``lp_test_blend``: blending
169bf215546Sopenharmony_ci-  ``lp_test_conv``: SIMD vector conversion
170bf215546Sopenharmony_ci-  ``lp_test_format``: pixel unpacking/packing
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ciSome of these tests can output results and benchmarks to a tab-separated
173bf215546Sopenharmony_cifile for later analysis, e.g.:
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci::
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci   build/linux-x86_64-debug/gallium/drivers/llvmpipe/lp_test_blend -o blend.tsv
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ciDevelopment Notes
180bf215546Sopenharmony_ci-----------------
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci-  When looking at this code for the first time, start in lp_state_fs.c,
183bf215546Sopenharmony_ci   and then skim through the ``lp_bld_*`` functions called there, and
184bf215546Sopenharmony_ci   the comments at the top of the ``lp_bld_*.c`` functions.
185bf215546Sopenharmony_ci-  The driver-independent parts of the LLVM / Gallium code are found in
186bf215546Sopenharmony_ci   ``src/gallium/auxiliary/gallivm/``. The filenames and function
187bf215546Sopenharmony_ci   prefixes need to be renamed from ``lp_bld_`` to something else
188bf215546Sopenharmony_ci   though.
189bf215546Sopenharmony_ci-  We use LLVM-C bindings for now. They are not documented, but follow
190bf215546Sopenharmony_ci   the C++ interfaces very closely, and appear to be complete enough for
191bf215546Sopenharmony_ci   code generation. See `this stand-alone
192bf215546Sopenharmony_ci   example <https://npcontemplation.blogspot.com/2008/06/secret-of-llvm-c-bindings.html>`__.
193bf215546Sopenharmony_ci   See the ``llvm-c/Core.h`` file for reference.
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci.. _recommended_reading:
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ciRecommended Reading
198bf215546Sopenharmony_ci-------------------
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci-  Rasterization
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   -  `Triangle Scan Conversion using 2D Homogeneous
203bf215546Sopenharmony_ci      Coordinates <https://www.cs.unc.edu/~olano/papers/2dh-tri/>`__
204bf215546Sopenharmony_ci   -  `Rasterization on
205bf215546Sopenharmony_ci      Larrabee <http://www.drdobbs.com/parallel/rasterization-on-larrabee/217200602>`__
206bf215546Sopenharmony_ci      (`DevMaster
207bf215546Sopenharmony_ci      copy <http://devmaster.net/posts/2887/rasterization-on-larrabee>`__)
208bf215546Sopenharmony_ci   -  `Rasterization using half-space
209bf215546Sopenharmony_ci      functions <http://devmaster.net/posts/6133/rasterization-using-half-space-functions>`__
210bf215546Sopenharmony_ci   -  `Advanced
211bf215546Sopenharmony_ci      Rasterization <http://devmaster.net/posts/6145/advanced-rasterization>`__
212bf215546Sopenharmony_ci   -  `Optimizing Software Occlusion
213bf215546Sopenharmony_ci      Culling <https://fgiesen.wordpress.com/2013/02/17/optimizing-sw-occlusion-culling-index/>`__
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci-  Texture sampling
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   -  `Perspective Texture
218bf215546Sopenharmony_ci      Mapping <http://chrishecker.com/Miscellaneous_Technical_Articles#Perspective_Texture_Mapping>`__
219bf215546Sopenharmony_ci   -  `Texturing As In
220bf215546Sopenharmony_ci      Unreal <https://www.flipcode.com/archives/Texturing_As_In_Unreal.shtml>`__
221bf215546Sopenharmony_ci   -  `Run-Time MIP-Map
222bf215546Sopenharmony_ci      Filtering <http://www.gamasutra.com/view/feature/3301/runtime_mipmap_filtering.php>`__
223bf215546Sopenharmony_ci   -  `Will "brilinear" filtering
224bf215546Sopenharmony_ci      persist? <http://alt.3dcenter.org/artikel/2003/10-26_a_english.php>`__
225bf215546Sopenharmony_ci   -  `Trilinear
226bf215546Sopenharmony_ci      filtering <http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html>`__
227bf215546Sopenharmony_ci   -  `Texture
228bf215546Sopenharmony_ci      Swizzling <http://devmaster.net/posts/12785/texture-swizzling>`__
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci-  SIMD
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci   -  `Whole-Function
233bf215546Sopenharmony_ci      Vectorization <http://www.cdl.uni-saarland.de/projects/wfv/#header4>`__
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci-  Optimization
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   -  `Optimizing Pixomatic For Modern x86
238bf215546Sopenharmony_ci      Processors <http://www.drdobbs.com/optimizing-pixomatic-for-modern-x86-proc/184405807>`__
239bf215546Sopenharmony_ci   -  `Intel 64 and IA-32 Architectures Optimization Reference
240bf215546Sopenharmony_ci      Manual <http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html>`__
241bf215546Sopenharmony_ci   -  `Software optimization
242bf215546Sopenharmony_ci      resources <http://www.agner.org/optimize/>`__
243bf215546Sopenharmony_ci   -  `Intel Intrinsics
244bf215546Sopenharmony_ci      Guide <https://software.intel.com/en-us/articles/intel-intrinsics-guide>`__
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci-  LLVM
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci   -  `LLVM Language Reference
249bf215546Sopenharmony_ci      Manual <http://llvm.org/docs/LangRef.html>`__
250bf215546Sopenharmony_ci   -  `The secret of LLVM C
251bf215546Sopenharmony_ci      bindings <https://npcontemplation.blogspot.co.uk/2008/06/secret-of-llvm-c-bindings.html>`__
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_ci-  General
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   -  `A trip through the Graphics
256bf215546Sopenharmony_ci      Pipeline <https://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/>`__
257bf215546Sopenharmony_ci   -  `WARP Architecture and
258bf215546Sopenharmony_ci      Performance <https://msdn.microsoft.com/en-us/library/gg615082.aspx#architecture>`__
259