GPU acceleration NixOS provides various APIs that benefit from GPU hardware acceleration, such as VA-API and VDPAU for video playback; OpenGL and Vulkan for 3D graphics; and OpenCL for general-purpose computing. This chapter describes how to set up GPU hardware acceleration (as far as this is not done automatically) and how to verify that hardware acceleration is indeed used. Most of the aforementioned APIs are agnostic with regards to which display server is used. Consequently, these instructions should apply both to the X Window System and Wayland compositors.
OpenCL OpenCL is a general compute API. It is used by various applications such as Blender and Darktable to accelerate certain operations. OpenCL applications load drivers through the Installable Client Driver (ICD) mechanism. In this mechanism, an ICD file specifies the path to the OpenCL driver for a particular GPU family. In NixOS, there are two ways to make ICD files visible to the ICD loader. The first is through the OCL_ICD_VENDORS environment variable. This variable can contain a directory which is scanned by the ICL loader for ICD files. For example: $ export \ OCL_ICD_VENDORS=`nix-build '<nixpkgs>' --no-out-link -A rocm-opencl-icd`/etc/OpenCL/vendors/ The second mechanism is to add the OpenCL driver package to . This links the ICD file under /run/opengl-driver, where it will be visible to the ICD loader. The proper installation of OpenCL drivers can be verified through the clinfo command of the clinfo package. This command will report the number of hardware devices that is found and give detailed information for each device: $ clinfo | head -n3 Number of platforms 1 Platform Name AMD Accelerated Parallel Processing Platform Vendor Advanced Micro Devices, Inc.
AMD Modern AMD Graphics Core Next (GCN) GPUs are supported through the rocm-opencl-icd package. Adding this package to enables OpenCL support. However, OpenCL Image support is provided through the non-free rocm-runtime-ext package. This package can be added to the same configuration option, but requires that allowUnfree option is is enabled for nixpkgs. Full OpenCL support on supported AMD GPUs is thus enabled as follows: = [ rocm-opencl-icd rocm-runtime-ext ]; It is also possible to use the OpenCL Image extension without a system-wide installation of the rocm-runtime-ext package by setting the ROCR_EXT_DIR environment variable to the directory that contains the extension: $ export \ ROCR_EXT_DIR=`nix-build '<nixpkgs>' --no-out-link -A rocm-runtime-ext`/lib/rocm-runtime-ext With either approach, you can verify that OpenCL Image support is indeed working with the clinfo command: $ clinfo | grep Image Image support Yes
Vulkan Vulkan is a graphics and compute API for GPUs. It is used directly by games or indirectly though compatibility layers like DXVK. By default, if is enabled, mesa is installed and provides Vulkan for supported hardware. Similar to OpenCL, Vulkan drivers are loaded through the Installable Client Driver (ICD) mechanism. ICD files for Vulkan are JSON files that specify the path to the driver library and the supported Vulkan version. All successfully loaded drivers are exposed to the application as different GPUs. In NixOS, there are two ways to make ICD files visible to Vulkan applications: an environment variable and a module option. The first option is through the VK_ICD_FILENAMES environment variable. This variable can contain multiple JSON files, separated by :. For example: $ export \ VK_ICD_FILENAMES=`nix-build '<nixpkgs>' --no-out-link -A amdvlk`/share/vulkan/icd.d/amd_icd64.json The second mechanism is to add the Vulkan driver package to . This links the ICD file under /run/opengl-driver, where it will be visible to the ICD loader. The proper installation of Vulkan drivers can be verified through the vulkaninfo command of the vulkan-tools package. This command will report the hardware devices and drivers found, in this example output amdvlk and radv: $ vulkaninfo | grep GPU GPU id : 0 (Unknown AMD GPU) GPU id : 1 (AMD RADV NAVI10 (LLVM 9.0.1)) ... GPU0: deviceType = PHYSICAL_DEVICE_TYPE_DISCRETE_GPU deviceName = Unknown AMD GPU GPU1: deviceType = PHYSICAL_DEVICE_TYPE_DISCRETE_GPU A simple graphical application that uses Vulkan is vkcube from the vulkan-tools package.
AMD Modern AMD Graphics Core Next (GCN) GPUs are supported through either radv, which is part of mesa, or the amdvlk package. Adding the amdvlk package to makes both drivers available for applications and lets them choose. A specific driver can be forced as follows: = [ amdvlk ]; # For amdvlk .VK_ICD_FILENAMES = "/run/opengl-driver/share/vulkan/icd.d/amd_icd64.json"; # For radv .VK_ICD_FILENAMES = "/run/opengl-driver/share/vulkan/icd.d/radeon_icd.x86_64.json";