Cross compiling Mesa for Windows

QtQuick 2.0 applications require OpenGL 2.1 or higher to run. Windows only provides OpenGL 1.1 by default, which is not sufficient. One workaround is to use ANGLE which implements the OpenGL ES 2 API on top of DirectX 9. The benefit of using ANGLE is that the graphics are hardware-accelerated but ANGLE requires Windows Vista or later. The other option is to use a software renderer such as Mesa which can run on Windows XP or later and may perform better than ANGLE when running inside virtual machines.

This article describes the process of cross-compiling Mesa for Windows on Arch Linux. The result is an opengl32.dll DLL that you can copy to the folder containing your application’s executable to use Mesa LLVMpipe software rendering.

Cross compiling is currently the only method that can compile the latest versions of Mesa for Windows using GCC. Compiling Mesa natively on Windows using GCC with Scons results in “the command line is too long” error during linking. There are known issues when compiling Mesa with optimizations using Visual Studio 2010. Compiling with Visual Studio 2012 is possible but requires Windows 7 and the default platform target does not support Windows XP. It is possible to cross compile using Cygwin or MSYS2 Scons.

Prebuilt binaries for Mesa are available from the MSYS2 project:

Install required Arch Linux packages

  1. sudo pacman -S base-devel mingw-w64-toolchain libxml2 python2

Compiling for Windows 32-bit

Create a working directory for build

  1. mkdir ~/mesa_win32
  2. cd ~/mesa_win32

Download LLVM and Mesa sources

  1. wget http://llvm.org/releases/3.4/llvm-3.4.src.tar.gz
  2. wget ftp://ftp.freedesktop.org/pub/mesa/10.0.2/MesaLib-10.0.2.tar.bz2

Extract sources

  1. tar -xvf llvm-3.4.src.tar.gz
  2. tar -xvf MesaLib-10.0.2.tar.bz2

Build LLVM

  1. cd ~/mesa_win32/llvm-3.4
  2. export PYTHON=/usr/bin/python2
  3. mkdir build
  4. cd build
  5. echo 'ac_cv_have_decl_strerror_s=${ac_cv_have_decl_strerror_s=no}' > config.cache
  6. ../configure -C --prefix=$HOME/mesa_win32/llvm-3.4.build --host=i686-w64-mingw32 --enable-optimized --disable-assertions --disable-pthreads --enable-targets=x86 --enable-bindings=none --disable-libffi --with-c-include-dirs=/usr/i686-w64-mingw32 --with-gcc-toolchain=/usr/i686-w64-mingw32 --with-default-sysroot=/usr/i686-w64-mingw32
  7. make
  8. make install

Note: Use of the strerror_s function is disabled by writing an entry to config.cache for Windows XP compatibility.

Build Mesa

  1. cd ~/mesa_win32/Mesa-10.0.2
  2. export LLVM=$HOME/mesa_win32/llvm-3.4.build
  3. LDFLAGS="-static -s" scons build=release platform=windows toolchain=crossmingw machine=x86 libgl-gdi
  4. mkdir ~/mesa_win32/dist
  5. cp -a build/windows-x86/gallium/targets/libgl-gdi/opengl32.dll ~/mesa_win32/dist/

Complete

You can now copy opengl32.dll from the ~/mesa_win32/dist folder to the folder containing your application’s executable.
Your Qt must be compiled with Desktop OpenGL to use opengl32.dll. The ANGLE build will not load the mesa library.

Compiling for Windows 64-bit

Create a working directory for build

  1. mkdir ~/mesa_win64
  2. cd ~/mesa_win64

Download LLVM and Mesa sources

  1. wget http://llvm.org/releases/3.4/llvm-3.4.src.tar.gz
  2. wget ftp://ftp.freedesktop.org/pub/mesa/10.0.2/MesaLib-10.0.2.tar.bz2

Extract sources

  1. tar -xvf llvm-3.4.src.tar.gz
  2. tar -xvf MesaLib-10.0.2.tar.bz2

Build LLVM

  1. cd ~/mesa_win64/llvm-3.4
  2. export PYTHON=/usr/bin/python2
  3. mkdir build
  4. cd build
  5. echo 'ac_cv_have_decl_strerror_s=${ac_cv_have_decl_strerror_s=no}' > config.cache
  6. ../configure -C --prefix=$HOME/mesa_win64/llvm-3.4.build --host=x86_64-w64-mingw32 --enable-optimized --disable-assertions --disable-pthreads --enable-targets=x86_64 --enable-bindings=none --disable-libffi --with-c-include-dirs=/usr/x86_64-w64-mingw32 --with-gcc-toolchain=/usr/x86_64-w64-mingw32 --with-default-sysroot=/usr/x86_64-w64-mingw32
  7. make
  8. make install

Note: Use of the strerror_s function is disabled by writing an entry to config.cache for Windows XP x64 compatibility.

Build Mesa

  1. cd ~/mesa_win64/Mesa-10.0.2
  2. LDFLAGS="-static -s" scons build=release platform=windows toolchain=crossmingw machine=x86_64 libgl-gdi
  3. mkdir ~/mesa_win64/dist
  4. cp -a build/windows-x86_64/gallium/targets/libgl-gdi/opengl32.dll ~/mesa_win64/dist/

Complete

You can now copy opengl32.dll from the ~/mesa_win64/dist folder to the folder containing your application’s executable.
Your Qt must be compiled with Desktop OpenGL to use opengl32.dll. The ANGLE build will not load the mesa library.

Categories: