Building Mesa3D on Windows

If you just need prebuilt binaries, click here.

Introduction

Let's start by answering an obvious question: why would you want Mesa3D on Windows?
The answer is simple: old software. You see, sometimes old apps and games that use OpenGL do not work on modern systems because the implementation of older OpenGL versions in modern video drivers (especially AMD's) is dreadful or inexistent. Mesa3D comes with a software renderer, which means it can run those applications by emulating OpenGL on the CPU. Of course, it's slow, but it's fast enough to play Quake 3 and Star Trek Voyager Elite Force (on an i7), so that's good enough.
Please excuse me if I'll refer to Mesa3D, Gallium, Gallium on LLVMPipe, etc. simply as "Mesa".

For this tutorial we need:

  • A really fast CPU
  • 4GB of RAM
  • 30GB of disk space
  • Windows 10 x64, fully updated. I seriously recommend using a virtual machine, as we'll be making a bit of a mess in the system.
  • 7-Zip or another archive manager (Winrar, etc.)
  • A few hours to spare

Preparations

The first thing we need to download is Win flex-bison.

Make a new folder in your C drive called winflexbison and extract the downloaded archive into it.

Now right click Windows and go to System > Advanced system settings > Environment Variables and edit the PATH system variable. Add C:\winflexbison to it, save and close.

Now we need to download Visual Studio. I will be using Visual Studio 2015 Community Edition, which can be downloaded for free from the Visual Studio website.
We will use Visual Studio to compile LLVM, which we will then use to build Mesa.

Launch the setup and select only Visual C++ under languages. Remove everything else.

Visual Studio will take a long time to download and install. Meanwhile, we'll download more things we need.
Download CMake for Windows x64. Don't install it yet, it may interfere with Visual Studio.

We will also need Python 2.7. Go to the Python website and get the latest version of Python 2.7 for x86, not x64. Do not try any other version of Python. Only 2.7.
Don't install it yet, it may interfere with Visual Studio.

Now get pywin32 for Python 2.7 on Win32. Choose the latest build then get the one for win32-py2.7.
Don't install it yet.

Also download LLVM 3.7.1. Since Mesa 17, LLVM 3.9 is also supported. Do not try other versions.
We need LLVM to build Mesa. Gallium on LLVM gives much higher performance compared to Gallium on softpipe. Later we'll see how to build Mesa with and without LLVM.

Finally, we need to get the latest version of Mesa.

Wait for Visual Studio to finish. When it's done, click Launch and let it do the first startup, then close it.

Now go to your Downloads folder and start installing the other tools.
We'll start with CMake. Make sure to add it to the system PATH, and let create a desktop icon.

Now install Python 2.7.
Let it install for all users, and add python.exe to the system PATH.

Now install pywin32. Nothing special here.

Now right click Windows and open a Command Prompt (not as administrator). We need to download mako and scons, which are required to build Mesa.
Type in these commands:

pip install mako
pip install -U pip setuptools --egg scons

And now we're finally done installing stuff.
The next step is to build LLVM, which will in turn be used for Mesa.

Building LLVM

Extract the downloaded LLVM source tar to your desktop using 7-Zip.

Run CMake from the shortcut on your desktop, select the LLVM source directory for the source code, and a new llvmbuild directory on your dekstop for the binaries.
It will look like this:

Now use the Add Entry button and add the following parameters that tell LLVM what the target system is (i686 CPU, Windows x86).
Be careful when copying them. It is case sensitive, and do not add spaces by accident.

Name Type Value
LLVM_DEFAULT_TARGET_TRIPLE STRING i686-pc-win32
LLVM_HOST_TRIPLE STRING i686-pc-win32
LLVM_TARGETS_TO_BUILD STRING X86
LLVM_TARGET_ARCH STRING i686
LLVM_USE_CRT_DEBUG STRING MTd
LLVM_USE_CRT_RELEASE STRING MT

When you're done and it looks like this, press Configure.

It will ask which version of Visual Studio you're using. If you followed this tutorial, select Visual Studio 2015. Do not select ARM or Win64!.
It will take a few minutes.

When it's done, it will add a bunch of parameters of its own. Click Generate and wait a few seconds.

And finally, click Open Project to load up Visual Studio.

It will take 1-2 minutes to load. When it's done, select Release mode for compiling. It will load for another 1-2 minutes.

Right click the LLVM solution and select Build Solution.

This is going to take a while. Perfect time to watch a Star Trek episode.

After about 30 minutes, LLVM is ready. It should look something like this. Nothing should have failed.

Before we can build Mesa, we need to move some files around that are needed to compile it. Be very careful here, as it's easy to get it wrong.
On your desktop, open llvmbuild\Release\lib. Here you'll find a bunch of object files. Select them all, right click -> copy.

Go back to llvmbuild\lib, and right click -> paste. It should now look like this.

On your desktop, open both llvm-3.7.1.src and llvmbuild, and copy the include directory from the source to the build directory. Overwrite any existing files.

Now right click Windows and go to System > Advanced system settings > Environment Variables and add a new system variable called LLVM, that points to the llvmbuild directory on your desktop. Save and close everything.
If you want to build Mesa without LLVM, do not set this variable.

LLVM is now configured and ready. If you plan to build Mesa on this machine again, you will not need to repeat these steps.

Building Mesa

Go to your Downloads folder, open the Mesa source tar with 7-Zip and extract it to your desktop.

Since version 17.2.0 it's necessary to make a little modification before we can build Mesa. Inside the extracted directory, open src/mesa/main/version.c using Notepad++ or another editor and remove the line that says

#include<git_sha1.h>

Enter the extracted directory on your desktop, select File and open a Command Prompt (not as administrator).

Type this command to start building Mesa:

scons platform=windows build=release libgl-gdi

Watch the rest of your Star Trek episode, and when it's over it should be done compiling.

Now go to your desktop and enter the mesa directory, then build\windows-x86\gallium\targets\libgl-gdi, and here you will see opengl32.dll.

Optional: Build libtxc_dxtn

Libtxc_dxtn is an extension for Mesa that allows it to use textures compressed with S3TC. Due to licensing issues, it did not make it into Mesa so we'll have to build it separately. Having this module improves compatiblity with old games since S3TC was quite common back then.
If you decide that you want S3TC support, it is recommended that you also enable S3TC caching in Mesa: open src/gallium/drivers/llvmpipe/lp_tex_sample.h in Dev-C++, set LP_USE_TEXTURE_CACHE to 1 instead of 0 and save. This will drastically improve S3TC performance. You will have to rebuild Mesa of course if you do this.

If you don't want to build this extension manually, you can simply download the pre-built DLL and copy it where you have opengl32.dll.

Also, we need to spend a couple of words about the licensing issues: S3TC is still covered by patents held by VIA Technologies that are still valid in some countries. So technically libtxc_dxtn is a direct violation of those patents if it's used in those countries.

To build libtxc_dxtn you will need this modified version of libtxc_dxtn. The library is several years old and had to be modified in order to build on modern systems (specifically, some constants from old versions of gl.h had to be copied). The absolutely retarded build system was also removed. Here's a link to the original library, no need to download it.

The modified library can be easily built using Dev-C++, so download it. Make sure it's the version with GCC.

Do a full installation, run it for the first time and close it.

Now extract the downloaded library to your desktop. Inside you'll find the dxtn project for Dev-C++.

Open the project and hit the compile button. Make sure it's set to build with GCC 32-bit.
If it says something about missing paths, just click yes and continue.
You might also want to set the project settings. By default it's set to optimize for Sandy Bridge CPUs and newer (Intel Core 2xxx from 2011).

Compiling will take a couple of seconds.
Now you have dxtn.dll in the project's directory. Copy it to Mesa's build\windows-x86\gallium\targets\libgl-gdi directory, where opengl32.dll is.

Finally, we have a full version Mesa!

And there you have it, Mesa on Windows!

Credits

Thanks to Lorenzo "lowenz" Donizetti for figuring out some of the cryptic stuff going on with LLVM and for discovering libtxc_dxtn.

Share this article

Comments