<?xml version="1.0" encoding="UTF-8"?>
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/dri-howto.xml,v 1.38 2007/04/26 03:45:02 nightmorph Exp $ -->
<!DOCTYPE guide SYSTEM "/dtd/guide.dtd">

<guide link="/doc/en/dri-howto.xml">

<title>Hardware 3D Acceleration Guide</title>

<author title="Author">
  <mail link="dberkholz@gentoo.org">Donnie Berkholz</mail>
</author>
<author title="Editor">
  <mail link="peesh@gentoo.org">Jorge Paulo</mail>
</author>
<author title="Editor">
  <mail link="nightmorph@gentoo.org">Joshua Saddler</mail>
</author>

<abstract>
This document is a guide to getting 3D acceleration working using the DRM with
Xorg in Gentoo Linux.
</abstract>

<!-- The content of this document is licensed under the CC-BY-SA license -->
<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->
<license/>

<version>1.8</version>
<date>2007-04-25</date>

<chapter>
<title>Introduction</title>
<section>
<title>What is hardware 3D acceleration and why do I want it?</title>
<body>

<p>
With hardware 3D acceleration, three-dimensional rendering uses the graphics 
processor on your video card instead of taking up valuable CPU resources 
drawing 3D images. It's also referred to as "hardware acceleration" instead of 
"software acceleration" because without this 3D acceleration your CPU is forced
to draw everything itself using the Mesa software rendering libraries, which 
takes up quite a bit of processing power. While Xorg typically supports 2D
hardware acceleration, it often lacks hardware 3D acceleration. 
Three-dimensional hardware acceleration is valuable in situations requiring 
rendering of 3D objects such as games, 3D CAD and modeling.
</p>

</body>
</section>
<section>
<title>How do I get hardware 3D acceleration?</title>
<body>

<p>
In many cases, both binary and open-source drivers exist. Open-source drivers
are preferable since we're using Linux and open source is one of its underlying
principles. Sometimes, binary drivers are the only option, like with nVidia's
cards. Binary drivers include <c>x11-drivers/nvidia-drivers</c> and
<c>x11-drivers/nvidia-legacy-drivers</c> for nVidia cards and
<c>x11-drivers/ati-drivers</c> for ATI cards. Other open-source drivers include
<c>media-video/ati-gatos</c> for ATI cards, which aims to support ATI's video
capabilities more fully.
</p>

</body>
</section>
<section>
<title>What is DRI?</title>
<body>

<p>
The <uri link="http://dri.freedesktop.org/wiki/">Direct Rendering
Infrastructure</uri>, also known as the DRI, is a framework for allowing direct
access to graphics hardware in a safe and efficient manner. It includes changes
to the X server, to several client libraries and to the kernel. The first major
use for the DRI is to create fast OpenGL implementations.
</p>

</body>
</section>
<section>
<title>What is the DRM and how does it relate to regular Xorg?</title>
<body>

<p>
The DRM (Direct Rendering Manager) is an <e>enhancement</e> to Xorg that adds 3D
acceleration for cards by adding the kernel module necessary for direct
rendering.
</p>

</body>
</section>
<section>
<title>Purpose</title>
<body>

<p>
This guide is for people who can't get direct rendering working with just Xorg.
The DRM works for the following drivers:
</p>

<ul>
  <li>3dfx</li>
  <li>i8x0</li>
  <li>matrox</li>
  <li>rage128</li>
  <li>radeon</li>
  <li>mach64</li>
  <li>sis300</li>
  <li>via</li>
</ul>

<p>
See the <uri link="http://dri.freedesktop.org/">DRI homepage</uri> for more info
and documentation.
</p>

</body>
</section>
<section>
<title>Feedback</title>
<body>

<p>
With suggestions, questions, etc., e-mail <mail 
link="dberkholz@gentoo.org">Donnie Berkholz</mail>.
</p>

</body>
</section>
</chapter>

<chapter>
<title>Install Xorg and configure your kernel</title>
<section>
<title>Install Xorg</title>
<body>

<p>
Please read our <uri link="/doc/en/xorg-config.xml">Xorg Configuration
Guide</uri> to get Xorg up and running.
</p>

</body>
</section>
<section>
<title>Configure your kernel</title>
<body>

<p>
Probe for your chipset and enable just that one.
</p>

<pre caption="Checking your AGP chipset">
# <i>emerge pciutils; lspci | grep AGP</i>
# <i>00:01.0 PCI bridge: Intel Corp. 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 03)</i>
<comment>(Your output may not match the above due to different hardware.)</comment>
</pre>

<p>
If your chipset is not supported by the kernel you might have some succes by
passing <c>agp=try_unsupported</c> as a kernel parameter. This will use Intel's
generic routines for AGP support. To add this parameter, edit your bootloader
configuration file!
</p>

<p>
Most, if not all, kernels should have these options. This was configured using a
standard <c>gentoo-sources</c> kernel.
</p>

<pre caption="Configuring the kernel">
# <i>ls -l /usr/src/linux </i>
lrwxrwxrwx 1 root root 22 2007-02-14 20:12 /usr/src/linux -> linux-2.6.18-gentoo-r4
<comment>(Make sure /usr/src/linux links to your current kernel.)</comment>
# <i>cd /usr/src/linux</i>
# <i>make menuconfig</i>
</pre>

<pre caption="make menuconfig options">
Processor type and features --->
&lt;*&gt; MTRR (Memory Type Range Register) support
Device drivers --->
   Character devices --->
   &lt;M&gt; /dev/agpgart (AGP Support)
   <comment>(The agpgart option is not present on 64-bit kernels; just choose your chipset support.)</comment>
   &lt;M&gt; Intel 440LX/BX/GX, I8xx and E7x05 support
<comment>(Enable your chipset instead of the above.)</comment>
   &lt;M&gt; Direct Rendering Manager (XFree86 4.1.0 and higher DRI support)
</pre>

</body>
</section>
<section>
<title>Compile and install your kernel</title>
<body>

<pre caption="Compiling and installing kernel">
# <i>make &amp;&amp; make modules_install</i>
</pre>

<p>
Don't forget to set up <path>grub.conf</path> or <path>lilo.conf</path> and run
<c>/sbin/lilo</c> if you use LILO.
</p>

</body>
</section>
</chapter>

<chapter>
<title>Configure direct rendering</title>
<section id="configure_xorg">
<title>Configure Xorg.conf</title>
<body>

<p>
Open <path>/etc/X11/xorg.conf</path> with your favorite text editor and edit 
it to enable DRI and GLX.
</p>

<pre caption="xorg.conf">
...
Section "Module"
  Load "dri"
  Load "glx"
  ...
EndSection
...
Section "Device"
  Driver "radeon"
  <comment>(Replace radeon with the name of your driver.)</comment>
  ...
EndSection
...
Section "dri"
  Mode 0666
EndSection
</pre>

</body>
</section>
<section>
<title>Changes to modules.autoload.d</title>
<body>

<p>
You will need to add the module name that your card uses to
<path>/etc/modules.autoload.d/kernel-2.6</path> to ensure that the module is
loaded automatically when the system starts up.
</p>

<pre caption="Editing /etc/modules.autoload.d/kernel-2.6">
<comment>(Change module name as required.)</comment>
intel-agp
</pre>

<note>
If you compiled <c>agpgart</c> as a module, you will also need to add it to
<path>/etc/modules.autoload.d/kernel-2.6</path>.
</note>

</body>
</section>
</chapter>

<chapter>
<title>Test 3D acceleration</title>
<section>
<title>Reboot to the new kernel</title>
<body>

<p>
Reboot your computer to your new kernel and login as a normal user. It's time to
see if you have direct rendering and how good it is. <c>glxinfo</c> and
<c>glxgears</c> are part of the <c>mesa-progs</c> package, so make sure it is
installed before you attempt to run these commands.
</p>

<pre caption="Testing rendering">
$ <i>startx</i>
<comment>(No need to load modules for your driver or agpgart, even if you compiled them as a module.)</comment>
<comment>(They will be loaded automatically.)</comment>
$ <i>glxinfo | grep rendering</i>
direct rendering: Yes
<comment>(If it says "No", you don't have 3D acceleration.)</comment>
$ <i>glxgears</i>
<comment>(Test your frames per second (FPS) at the default size. The number should be 
significantly higher than before configuring DRM. Do this while the CPU is as idle as 
possible.)</comment>
</pre>

<note>
FPS may be limited by your screen's refresh rate, so keep this in mind if
<c>glxgears</c> reports only about 70-100 FPS. <c>games-fps/ut2004-demo</c> is a
better benchmarking tool, as it can give you real-world performance results.
</note>

</body>
</section>
</chapter>

<chapter>
<title>Tweak your performance</title>
<section>
<title>Get the most out of direct rendering</title>
<body>

<p>
A few options may increase performance by up to 30 percent (or more) over the
default. Set them in <path>/etc/X11/xorg.conf</path>. However, you will first
need to check that your motherboard and video card support these options.
</p>

<p>
First, let's see if your video card can support fast writes. We'll do this by
closely inspecting the output from <c>lspci</c>. Specifically, we are looking
at the "VGA compatible controller" information.
</p>

<pre caption="Video card check">
# <i>lspci -vv</i>
01:00.0 VGA compatible controller: ATI Technologies Inc Radeon Mobility M6 LY (prog-if 00 [VGA])
. . .
Capabilities: [58] AGP version 2.0
    Status: RQ=48 Iso- ArqSz=0 Cal=0 SBA+ ITACoh- GART64- HTrans- 64bit- FW+ AGP3- Rate=x1,x2,x4
</pre>

<p>
While quite a lot of information is produced, we are looking for <b>FW</b> in
the "Status" subsection of the AGP "Capabilities" section. If <b>FW+</b> is
present in the "Status" line, it means the card supports fast writes. We can
now check if the motherboard supports fast writes.
</p>

<impo>
If you do not see <b>FW+</b>, but instead see <b>FW-</b>, you cannot enable
fast writes in <path>xorg.conf</path>. Your card does not support fast writes.
</impo>

<p>
Now let's make sure the motherboard supports fast writes as well. This time,
look at the "Host bridge" section of your <c>lspci</c> output.
</p>

<pre caption="Motherboard check">
# <i>lspci -vv</i>
00:00.0 Host bridge: Intel Corporation 82830 830 Chipset Host Bridge (rev 02)
. . .
Capabilities: [a0] AGP version 2.0
    Status: RQ=32 Iso- ArqSz=0 Cal=0 SBA+ ITACoh- GART64- HTrans- 64bit- FW+ AGP3- Rate=x1,x2,x4
</pre>

<p>
Again, examine the "Status" subsection of your AGP "Capabilities" section. Look
for <b>FW</b>. If you see <b>FW+</b>, your motherboard supports fast writes.
</p>

<impo>
Remember, both your video card information and your motherboard information
must show the same <b>FW</b> capability. If either device shows <b>FW-</b> in
"Status", you cannot enable fast writes in <path>xorg.conf</path>.
</impo>

<p>
Assuming that all has gone well and both your motherboard and video card
support fast writes, let's enable this option in
<path>/etc/X11/xorg.conf</path> and get the best performance out of your
hardware.
</p>

<pre caption="xorg.conf">
Section "Device"
  Option     "AGPMode" "4"
  <comment>(This increased FPS from 609 to 618.)</comment>
  Option     "AGPFastWrite" "True"
  <comment>(This had no measurable effect, but it may increase instability of your computer.)</comment>
  <comment>(You may also need to set it in your BIOS.)</comment>
  Option     "EnablePageFlip" "True"
  <comment>(This improved FPS from 618 to 702. It also is "risky" but few people have reported problems.)</comment>
  ...
EndSection
</pre>

<warn>
Enabling <c>AGPFastWrite</c> on a VIA chipset will very likely cause your
machine to lock up. VIA chipsets do not play nicely with fast writes, so use
this setting at your own risk.
</warn>

<note>
Remember, if you want fast writes to work properly, you will have to first
enable the appropriate option in your BIOS.
</note>

<p>
If you want to set even more features, check out the <uri
link="http://dri.freedesktop.org/wiki/FeatureMatrix">feature matrix</uri> on
the DRI web site or the <uri
link="http://dri.sourceforge.net/doc/dri_driver_features.phtml">features
listing</uri> on Sourceforge.
</p>

</body>
</section>
</chapter>

<chapter>
<title>Troubleshooting</title>
<section>
<title>It doesn't work. I don't have rendering, and I can't tell why.</title>
<body>

<p>
Try <c>modprobe radeon</c> before you start the X server (replace <c>radeon</c>
with the name of your driver). Also, try building agpgart into the kernel
instead of as a module.
</p>

</body>
</section>
<section>
<title>When I startx, I get this error: "[drm] failed to load kernel module agpgart"</title>
<body>

<p>
That's because you compiled agpgart into the kernel instead of as a module. 
Ignore it unless you're having problems.
</p>

</body>
</section>
<section>
<title>I have a Radeon, and I want TV-Out.</title>
<body>

<p>
Check out <c>ati-gatos</c> drivers. <c>emerge -av ati-gatos</c>.
</p>

</body>
</section>
<section>
<title>
It doesn't work. My card is so incredibly new and cool that it isn't supported
at all.
</title>
<body>

<p>
Try out the binary drivers. For <c>ati-drivers</c>, a listing is at
<uri>http://ati.amd.com/support/drivers/linux/linux-radeon.html</uri> (for x86)
and at <uri>http://ati.amd.com/support/drivers/linux64/linux64-radeon.html</uri>
(for amd64). If those don't support it, use fbdev. It's slow, but it works.
</p>

</body>
</section>
<section>
<title>I have a PCI card and it doesn't work.  Help!</title>
<body>

<p>
Edit <path>/etc/X11/xorg.conf</path>. In section "Device" enable ForcePCIMode. 
</p>

<pre caption="Enabling ForcePCIMode">
Option "ForcePCIMode" "True"
</pre>

</body>
</section>
</chapter>

<chapter>
<title>Acknowledgments</title>
<section>
<body>

<ol>
  <li>
    Christopher Webber for suggesting a troubleshooting question about changing 
    or recompiling kernels
  </li>
  <li>
    Steve, for suggesting consistency between the cases of dri and DRI in 
    XF86Config
  </li>
</ol>

</body>
</section>
</chapter>

<chapter>
<title>References</title>
<section>
<body>

<ol>
  <li><uri>http://forums.gentoo.org/viewtopic.php?t=46681</uri></li>
  <li><uri>http://forums.gentoo.org/viewtopic.php?t=29264</uri></li>
  <li><uri>http://dri.freedesktop.org/</uri></li>
  <li><uri>http://www.retinalburn.net/linux/dri_status.html</uri></li>
</ol>

</body>
</section>
</chapter>
</guide>
